blob: 0cda46ab35e5949ac23843071a4a769c546d3b77 [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 },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530192 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400193};
194
Johannes Berge31b8212010-10-05 19:39:30 +0200195/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000196static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200197 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200198 [NL80211_KEY_IDX] = { .type = NLA_U8 },
199 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
200 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
201 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
202 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200203 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100204 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
205};
206
207/* policy for the key default flags */
208static const struct nla_policy
209nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
210 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
211 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200212};
213
Johannes Bergff1b6e62011-05-04 15:37:28 +0200214/* policy for WoWLAN attributes */
215static const struct nla_policy
216nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
217 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
218 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
219 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
220 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200221 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
222 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
223 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
224 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200225};
226
Johannes Berge5497d72011-07-05 16:35:40 +0200227/* policy for GTK rekey offload attributes */
228static const struct nla_policy
229nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
230 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
231 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
232 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
233};
234
Holger Schuriga0438972009-11-11 11:30:02 +0100235/* ifidx get helper */
236static int nl80211_get_ifidx(struct netlink_callback *cb)
237{
238 int res;
239
240 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
241 nl80211_fam.attrbuf, nl80211_fam.maxattr,
242 nl80211_policy);
243 if (res)
244 return res;
245
246 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
247 return -EINVAL;
248
249 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
250 if (!res)
251 return -EINVAL;
252 return res;
253}
254
Johannes Berg67748892010-10-04 21:14:06 +0200255static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
256 struct netlink_callback *cb,
257 struct cfg80211_registered_device **rdev,
258 struct net_device **dev)
259{
260 int ifidx = cb->args[0];
261 int err;
262
263 if (!ifidx)
264 ifidx = nl80211_get_ifidx(cb);
265 if (ifidx < 0)
266 return ifidx;
267
268 cb->args[0] = ifidx;
269
270 rtnl_lock();
271
272 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
273 if (!*dev) {
274 err = -ENODEV;
275 goto out_rtnl;
276 }
277
278 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100279 if (IS_ERR(*rdev)) {
280 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200281 goto out_rtnl;
282 }
283
284 return 0;
285 out_rtnl:
286 rtnl_unlock();
287 return err;
288}
289
290static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
291{
292 cfg80211_unlock_rdev(rdev);
293 rtnl_unlock();
294}
295
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100296/* IE validation */
297static bool is_valid_ie_attr(const struct nlattr *attr)
298{
299 const u8 *pos;
300 int len;
301
302 if (!attr)
303 return true;
304
305 pos = nla_data(attr);
306 len = nla_len(attr);
307
308 while (len) {
309 u8 elemlen;
310
311 if (len < 2)
312 return false;
313 len -= 2;
314
315 elemlen = pos[1];
316 if (elemlen > len)
317 return false;
318
319 len -= elemlen;
320 pos += 2 + elemlen;
321 }
322
323 return true;
324}
325
Johannes Berg55682962007-09-20 13:09:35 -0400326/* message building helper */
327static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
328 int flags, u8 cmd)
329{
330 /* since there is no private header just add the generic one */
331 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
332}
333
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400334static int nl80211_msg_put_channel(struct sk_buff *msg,
335 struct ieee80211_channel *chan)
336{
337 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
338 chan->center_freq);
339
340 if (chan->flags & IEEE80211_CHAN_DISABLED)
341 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
342 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
343 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
344 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
345 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
346 if (chan->flags & IEEE80211_CHAN_RADAR)
347 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
348
349 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
350 DBM_TO_MBM(chan->max_power));
351
352 return 0;
353
354 nla_put_failure:
355 return -ENOBUFS;
356}
357
Johannes Berg55682962007-09-20 13:09:35 -0400358/* netlink command implementations */
359
Johannes Bergb9454e82009-07-08 13:29:08 +0200360struct key_parse {
361 struct key_params p;
362 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200363 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200364 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100365 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200366};
367
368static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
369{
370 struct nlattr *tb[NL80211_KEY_MAX + 1];
371 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
372 nl80211_key_policy);
373 if (err)
374 return err;
375
376 k->def = !!tb[NL80211_KEY_DEFAULT];
377 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
378
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100379 if (k->def) {
380 k->def_uni = true;
381 k->def_multi = true;
382 }
383 if (k->defmgmt)
384 k->def_multi = true;
385
Johannes Bergb9454e82009-07-08 13:29:08 +0200386 if (tb[NL80211_KEY_IDX])
387 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
388
389 if (tb[NL80211_KEY_DATA]) {
390 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
391 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
392 }
393
394 if (tb[NL80211_KEY_SEQ]) {
395 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
396 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
397 }
398
399 if (tb[NL80211_KEY_CIPHER])
400 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
401
Johannes Berge31b8212010-10-05 19:39:30 +0200402 if (tb[NL80211_KEY_TYPE]) {
403 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
404 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
405 return -EINVAL;
406 }
407
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100408 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
409 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
410 int err = nla_parse_nested(kdt,
411 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
412 tb[NL80211_KEY_DEFAULT_TYPES],
413 nl80211_key_default_policy);
414 if (err)
415 return err;
416
417 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
418 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
419 }
420
Johannes Bergb9454e82009-07-08 13:29:08 +0200421 return 0;
422}
423
424static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
425{
426 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
427 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
428 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
429 }
430
431 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
432 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
433 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
434 }
435
436 if (info->attrs[NL80211_ATTR_KEY_IDX])
437 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
438
439 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
440 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
441
442 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
443 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
444
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100445 if (k->def) {
446 k->def_uni = true;
447 k->def_multi = true;
448 }
449 if (k->defmgmt)
450 k->def_multi = true;
451
Johannes Berge31b8212010-10-05 19:39:30 +0200452 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
453 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
454 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
455 return -EINVAL;
456 }
457
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100458 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
459 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
460 int err = nla_parse_nested(
461 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
462 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
463 nl80211_key_default_policy);
464 if (err)
465 return err;
466
467 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
468 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
469 }
470
Johannes Bergb9454e82009-07-08 13:29:08 +0200471 return 0;
472}
473
474static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
475{
476 int err;
477
478 memset(k, 0, sizeof(*k));
479 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200480 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200481
482 if (info->attrs[NL80211_ATTR_KEY])
483 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
484 else
485 err = nl80211_parse_key_old(info, k);
486
487 if (err)
488 return err;
489
490 if (k->def && k->defmgmt)
491 return -EINVAL;
492
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100493 if (k->defmgmt) {
494 if (k->def_uni || !k->def_multi)
495 return -EINVAL;
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 if (k->idx != -1) {
499 if (k->defmgmt) {
500 if (k->idx < 4 || k->idx > 5)
501 return -EINVAL;
502 } else if (k->def) {
503 if (k->idx < 0 || k->idx > 3)
504 return -EINVAL;
505 } else {
506 if (k->idx < 0 || k->idx > 5)
507 return -EINVAL;
508 }
509 }
510
511 return 0;
512}
513
Johannes Bergfffd0932009-07-08 14:22:54 +0200514static struct cfg80211_cached_keys *
515nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
516 struct nlattr *keys)
517{
518 struct key_parse parse;
519 struct nlattr *key;
520 struct cfg80211_cached_keys *result;
521 int rem, err, def = 0;
522
523 result = kzalloc(sizeof(*result), GFP_KERNEL);
524 if (!result)
525 return ERR_PTR(-ENOMEM);
526
527 result->def = -1;
528 result->defmgmt = -1;
529
530 nla_for_each_nested(key, keys, rem) {
531 memset(&parse, 0, sizeof(parse));
532 parse.idx = -1;
533
534 err = nl80211_parse_key_new(key, &parse);
535 if (err)
536 goto error;
537 err = -EINVAL;
538 if (!parse.p.key)
539 goto error;
540 if (parse.idx < 0 || parse.idx > 4)
541 goto error;
542 if (parse.def) {
543 if (def)
544 goto error;
545 def = 1;
546 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100547 if (!parse.def_uni || !parse.def_multi)
548 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200549 } else if (parse.defmgmt)
550 goto error;
551 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200552 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200553 if (err)
554 goto error;
555 result->params[parse.idx].cipher = parse.p.cipher;
556 result->params[parse.idx].key_len = parse.p.key_len;
557 result->params[parse.idx].key = result->data[parse.idx];
558 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
559 }
560
561 return result;
562 error:
563 kfree(result);
564 return ERR_PTR(err);
565}
566
567static int nl80211_key_allowed(struct wireless_dev *wdev)
568{
569 ASSERT_WDEV_LOCK(wdev);
570
Johannes Bergfffd0932009-07-08 14:22:54 +0200571 switch (wdev->iftype) {
572 case NL80211_IFTYPE_AP:
573 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200574 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700575 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 break;
577 case NL80211_IFTYPE_ADHOC:
578 if (!wdev->current_bss)
579 return -ENOLINK;
580 break;
581 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200582 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200583 if (wdev->sme_state != CFG80211_SME_CONNECTED)
584 return -ENOLINK;
585 break;
586 default:
587 return -EINVAL;
588 }
589
590 return 0;
591}
592
Johannes Berg7527a782011-05-13 10:58:57 +0200593static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
594{
595 struct nlattr *nl_modes = nla_nest_start(msg, attr);
596 int i;
597
598 if (!nl_modes)
599 goto nla_put_failure;
600
601 i = 0;
602 while (ifmodes) {
603 if (ifmodes & 1)
604 NLA_PUT_FLAG(msg, i);
605 ifmodes >>= 1;
606 i++;
607 }
608
609 nla_nest_end(msg, nl_modes);
610 return 0;
611
612nla_put_failure:
613 return -ENOBUFS;
614}
615
616static int nl80211_put_iface_combinations(struct wiphy *wiphy,
617 struct sk_buff *msg)
618{
619 struct nlattr *nl_combis;
620 int i, j;
621
622 nl_combis = nla_nest_start(msg,
623 NL80211_ATTR_INTERFACE_COMBINATIONS);
624 if (!nl_combis)
625 goto nla_put_failure;
626
627 for (i = 0; i < wiphy->n_iface_combinations; i++) {
628 const struct ieee80211_iface_combination *c;
629 struct nlattr *nl_combi, *nl_limits;
630
631 c = &wiphy->iface_combinations[i];
632
633 nl_combi = nla_nest_start(msg, i + 1);
634 if (!nl_combi)
635 goto nla_put_failure;
636
637 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
638 if (!nl_limits)
639 goto nla_put_failure;
640
641 for (j = 0; j < c->n_limits; j++) {
642 struct nlattr *nl_limit;
643
644 nl_limit = nla_nest_start(msg, j + 1);
645 if (!nl_limit)
646 goto nla_put_failure;
647 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
648 c->limits[j].max);
649 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
650 c->limits[j].types))
651 goto nla_put_failure;
652 nla_nest_end(msg, nl_limit);
653 }
654
655 nla_nest_end(msg, nl_limits);
656
657 if (c->beacon_int_infra_match)
658 NLA_PUT_FLAG(msg,
659 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
660 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
661 c->num_different_channels);
662 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
663 c->max_interfaces);
664
665 nla_nest_end(msg, nl_combi);
666 }
667
668 nla_nest_end(msg, nl_combis);
669
670 return 0;
671nla_put_failure:
672 return -ENOBUFS;
673}
674
Johannes Berg55682962007-09-20 13:09:35 -0400675static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
676 struct cfg80211_registered_device *dev)
677{
678 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100679 struct nlattr *nl_bands, *nl_band;
680 struct nlattr *nl_freqs, *nl_freq;
681 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100682 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100683 enum ieee80211_band band;
684 struct ieee80211_channel *chan;
685 struct ieee80211_rate *rate;
686 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200687 const struct ieee80211_txrx_stypes *mgmt_stypes =
688 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400689
690 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
691 if (!hdr)
692 return -1;
693
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500694 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400695 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200696
Johannes Bergf5ea9122009-08-07 16:17:38 +0200697 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
698 cfg80211_rdev_list_generation);
699
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200700 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
701 dev->wiphy.retry_short);
702 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
703 dev->wiphy.retry_long);
704 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
705 dev->wiphy.frag_threshold);
706 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
707 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100708 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
709 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100710 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
711 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300712 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
713 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200714 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
715 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300716 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
717 dev->wiphy.max_sched_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100718
Johannes Berge31b8212010-10-05 19:39:30 +0200719 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
720 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700721 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
722 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300723 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
724 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Johannes Berge31b8212010-10-05 19:39:30 +0200725
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530726 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
727 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
728
Johannes Berg25e47c182009-04-02 20:14:06 +0200729 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
730 sizeof(u32) * dev->wiphy.n_cipher_suites,
731 dev->wiphy.cipher_suites);
732
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100733 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
734 dev->wiphy.max_num_pmkids);
735
Johannes Bergc0692b82010-08-27 14:26:53 +0300736 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
737 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
738
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900739 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
740 dev->wiphy.available_antennas_tx);
741 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
742 dev->wiphy.available_antennas_rx);
743
Bruno Randolf7f531e02010-12-16 11:30:22 +0900744 if ((dev->wiphy.available_antennas_tx ||
745 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900746 u32 tx_ant = 0, rx_ant = 0;
747 int res;
748 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
749 if (!res) {
750 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
751 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
752 }
753 }
754
Johannes Berg7527a782011-05-13 10:58:57 +0200755 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
756 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700757 goto nla_put_failure;
758
Johannes Bergee688b002008-01-24 19:38:39 +0100759 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
760 if (!nl_bands)
761 goto nla_put_failure;
762
763 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
764 if (!dev->wiphy.bands[band])
765 continue;
766
767 nl_band = nla_nest_start(msg, band);
768 if (!nl_band)
769 goto nla_put_failure;
770
Johannes Bergd51626d2008-10-09 12:20:13 +0200771 /* add HT info */
772 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
773 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
774 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
775 &dev->wiphy.bands[band]->ht_cap.mcs);
776 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
777 dev->wiphy.bands[band]->ht_cap.cap);
778 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
779 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
780 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
781 dev->wiphy.bands[band]->ht_cap.ampdu_density);
782 }
783
Johannes Bergee688b002008-01-24 19:38:39 +0100784 /* add frequencies */
785 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
786 if (!nl_freqs)
787 goto nla_put_failure;
788
789 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
790 nl_freq = nla_nest_start(msg, i);
791 if (!nl_freq)
792 goto nla_put_failure;
793
794 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100795
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400796 if (nl80211_msg_put_channel(msg, chan))
797 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200798
Johannes Bergee688b002008-01-24 19:38:39 +0100799 nla_nest_end(msg, nl_freq);
800 }
801
802 nla_nest_end(msg, nl_freqs);
803
804 /* add bitrates */
805 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
806 if (!nl_rates)
807 goto nla_put_failure;
808
809 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
810 nl_rate = nla_nest_start(msg, i);
811 if (!nl_rate)
812 goto nla_put_failure;
813
814 rate = &dev->wiphy.bands[band]->bitrates[i];
815 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
816 rate->bitrate);
817 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
818 NLA_PUT_FLAG(msg,
819 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
820
821 nla_nest_end(msg, nl_rate);
822 }
823
824 nla_nest_end(msg, nl_rates);
825
826 nla_nest_end(msg, nl_band);
827 }
828 nla_nest_end(msg, nl_bands);
829
Johannes Berg8fdc6212009-03-14 09:34:01 +0100830 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
831 if (!nl_cmds)
832 goto nla_put_failure;
833
834 i = 0;
835#define CMD(op, n) \
836 do { \
837 if (dev->ops->op) { \
838 i++; \
839 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
840 } \
841 } while (0)
842
843 CMD(add_virtual_intf, NEW_INTERFACE);
844 CMD(change_virtual_intf, SET_INTERFACE);
845 CMD(add_key, NEW_KEY);
846 CMD(add_beacon, NEW_BEACON);
847 CMD(add_station, NEW_STATION);
848 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800849 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100850 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200851 CMD(auth, AUTHENTICATE);
852 CMD(assoc, ASSOCIATE);
853 CMD(deauth, DEAUTHENTICATE);
854 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200855 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100856 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100857 CMD(set_pmksa, SET_PMKSA);
858 CMD(del_pmksa, DEL_PMKSA);
859 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100860 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200861 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200862 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100863 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100864 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200865 i++;
866 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
867 }
Johannes Bergf444de02010-05-05 15:25:02 +0200868 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400869 CMD(set_wds_peer, SET_WDS_PEER);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300870 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
871 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100872
873#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200874
Johannes Berg6829c8782009-07-02 09:13:27 +0200875 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200876 i++;
877 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
878 }
879
Johannes Berg6829c8782009-07-02 09:13:27 +0200880 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200881 i++;
882 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
883 }
884
Johannes Berg8fdc6212009-03-14 09:34:01 +0100885 nla_nest_end(msg, nl_cmds);
886
Johannes Berga2939112010-12-14 17:54:28 +0100887 if (dev->ops->remain_on_channel)
888 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
889 dev->wiphy.max_remain_on_channel_duration);
890
Jouni Malinend2da5872011-08-08 12:10:30 +0300891 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100892 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
893
Johannes Berg2e161f72010-08-12 15:38:38 +0200894 if (mgmt_stypes) {
895 u16 stypes;
896 struct nlattr *nl_ftypes, *nl_ifs;
897 enum nl80211_iftype ift;
898
899 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
900 if (!nl_ifs)
901 goto nla_put_failure;
902
903 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
904 nl_ftypes = nla_nest_start(msg, ift);
905 if (!nl_ftypes)
906 goto nla_put_failure;
907 i = 0;
908 stypes = mgmt_stypes[ift].tx;
909 while (stypes) {
910 if (stypes & 1)
911 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
912 (i << 4) | IEEE80211_FTYPE_MGMT);
913 stypes >>= 1;
914 i++;
915 }
916 nla_nest_end(msg, nl_ftypes);
917 }
918
Johannes Berg74b70a42010-08-24 12:15:53 +0200919 nla_nest_end(msg, nl_ifs);
920
Johannes Berg2e161f72010-08-12 15:38:38 +0200921 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
922 if (!nl_ifs)
923 goto nla_put_failure;
924
925 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
926 nl_ftypes = nla_nest_start(msg, ift);
927 if (!nl_ftypes)
928 goto nla_put_failure;
929 i = 0;
930 stypes = mgmt_stypes[ift].rx;
931 while (stypes) {
932 if (stypes & 1)
933 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
934 (i << 4) | IEEE80211_FTYPE_MGMT);
935 stypes >>= 1;
936 i++;
937 }
938 nla_nest_end(msg, nl_ftypes);
939 }
940 nla_nest_end(msg, nl_ifs);
941 }
942
Johannes Bergff1b6e62011-05-04 15:37:28 +0200943 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
944 struct nlattr *nl_wowlan;
945
946 nl_wowlan = nla_nest_start(msg,
947 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
948 if (!nl_wowlan)
949 goto nla_put_failure;
950
951 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
952 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
953 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
954 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
955 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
956 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200957 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
958 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
959 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
960 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
961 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
962 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
963 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
964 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
965 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
966 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200967 if (dev->wiphy.wowlan.n_patterns) {
968 struct nl80211_wowlan_pattern_support pat = {
969 .max_patterns = dev->wiphy.wowlan.n_patterns,
970 .min_pattern_len =
971 dev->wiphy.wowlan.pattern_min_len,
972 .max_pattern_len =
973 dev->wiphy.wowlan.pattern_max_len,
974 };
975 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
976 sizeof(pat), &pat);
977 }
978
979 nla_nest_end(msg, nl_wowlan);
980 }
981
Johannes Berg7527a782011-05-13 10:58:57 +0200982 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
983 dev->wiphy.software_iftypes))
984 goto nla_put_failure;
985
986 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
987 goto nla_put_failure;
988
Johannes Berg55682962007-09-20 13:09:35 -0400989 return genlmsg_end(msg, hdr);
990
991 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700992 genlmsg_cancel(msg, hdr);
993 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400994}
995
996static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
997{
998 int idx = 0;
999 int start = cb->args[0];
1000 struct cfg80211_registered_device *dev;
1001
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001002 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001003 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001004 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1005 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001006 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001007 continue;
1008 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1009 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001010 dev) < 0) {
1011 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001012 break;
Julius Volzb4637272008-07-08 14:02:19 +02001013 }
Johannes Berg55682962007-09-20 13:09:35 -04001014 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001015 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001016
1017 cb->args[0] = idx;
1018
1019 return skb->len;
1020}
1021
1022static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1023{
1024 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001025 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001026
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001027 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001028 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001029 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001030
Johannes Berg4c476992010-10-04 21:36:35 +02001031 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1032 nlmsg_free(msg);
1033 return -ENOBUFS;
1034 }
Johannes Berg55682962007-09-20 13:09:35 -04001035
Johannes Berg134e6372009-07-10 09:51:34 +00001036 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001037}
1038
Jouni Malinen31888482008-10-30 16:59:24 +02001039static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1040 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1041 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1042 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1043 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1044 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1045};
1046
1047static int parse_txq_params(struct nlattr *tb[],
1048 struct ieee80211_txq_params *txq_params)
1049{
1050 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1051 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1052 !tb[NL80211_TXQ_ATTR_AIFS])
1053 return -EINVAL;
1054
1055 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1056 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1057 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1058 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1059 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1060
1061 return 0;
1062}
1063
Johannes Bergf444de02010-05-05 15:25:02 +02001064static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1065{
1066 /*
1067 * You can only set the channel explicitly for AP, mesh
1068 * and WDS type interfaces; all others have their channel
1069 * managed via their respective "establish a connection"
1070 * command (connect, join, ...)
1071 *
1072 * Monitors are special as they are normally slaved to
1073 * whatever else is going on, so they behave as though
1074 * you tried setting the wiphy channel itself.
1075 */
1076 return !wdev ||
1077 wdev->iftype == NL80211_IFTYPE_AP ||
1078 wdev->iftype == NL80211_IFTYPE_WDS ||
1079 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001080 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1081 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001082}
1083
1084static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1085 struct wireless_dev *wdev,
1086 struct genl_info *info)
1087{
1088 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1089 u32 freq;
1090 int result;
1091
1092 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1093 return -EINVAL;
1094
1095 if (!nl80211_can_set_dev_channel(wdev))
1096 return -EOPNOTSUPP;
1097
1098 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1099 channel_type = nla_get_u32(info->attrs[
1100 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1101 if (channel_type != NL80211_CHAN_NO_HT &&
1102 channel_type != NL80211_CHAN_HT20 &&
1103 channel_type != NL80211_CHAN_HT40PLUS &&
1104 channel_type != NL80211_CHAN_HT40MINUS)
1105 return -EINVAL;
1106 }
1107
1108 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1109
1110 mutex_lock(&rdev->devlist_mtx);
1111 if (wdev) {
1112 wdev_lock(wdev);
1113 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1114 wdev_unlock(wdev);
1115 } else {
1116 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1117 }
1118 mutex_unlock(&rdev->devlist_mtx);
1119
1120 return result;
1121}
1122
1123static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1124{
Johannes Berg4c476992010-10-04 21:36:35 +02001125 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1126 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001127
Johannes Berg4c476992010-10-04 21:36:35 +02001128 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001129}
1130
Bill Jordane8347eb2010-10-01 13:54:28 -04001131static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1132{
Johannes Berg43b19952010-10-07 13:10:30 +02001133 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1134 struct net_device *dev = info->user_ptr[1];
1135 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001136 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001137
1138 if (!info->attrs[NL80211_ATTR_MAC])
1139 return -EINVAL;
1140
Johannes Berg43b19952010-10-07 13:10:30 +02001141 if (netif_running(dev))
1142 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001143
Johannes Berg43b19952010-10-07 13:10:30 +02001144 if (!rdev->ops->set_wds_peer)
1145 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001146
Johannes Berg43b19952010-10-07 13:10:30 +02001147 if (wdev->iftype != NL80211_IFTYPE_WDS)
1148 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001149
1150 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001151 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001152}
1153
1154
Johannes Berg55682962007-09-20 13:09:35 -04001155static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1156{
1157 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001158 struct net_device *netdev = NULL;
1159 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001160 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001161 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001162 u32 changed;
1163 u8 retry_short = 0, retry_long = 0;
1164 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001165 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001166
Johannes Bergf444de02010-05-05 15:25:02 +02001167 /*
1168 * Try to find the wiphy and netdev. Normally this
1169 * function shouldn't need the netdev, but this is
1170 * done for backward compatibility -- previously
1171 * setting the channel was done per wiphy, but now
1172 * it is per netdev. Previous userland like hostapd
1173 * also passed a netdev to set_wiphy, so that it is
1174 * possible to let that go to the right netdev!
1175 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001176 mutex_lock(&cfg80211_mutex);
1177
Johannes Bergf444de02010-05-05 15:25:02 +02001178 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1179 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1180
1181 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1182 if (netdev && netdev->ieee80211_ptr) {
1183 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1184 mutex_lock(&rdev->mtx);
1185 } else
1186 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001187 }
1188
Johannes Bergf444de02010-05-05 15:25:02 +02001189 if (!netdev) {
1190 rdev = __cfg80211_rdev_from_info(info);
1191 if (IS_ERR(rdev)) {
1192 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001193 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001194 }
1195 wdev = NULL;
1196 netdev = NULL;
1197 result = 0;
1198
1199 mutex_lock(&rdev->mtx);
1200 } else if (netif_running(netdev) &&
1201 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1202 wdev = netdev->ieee80211_ptr;
1203 else
1204 wdev = NULL;
1205
1206 /*
1207 * end workaround code, by now the rdev is available
1208 * and locked, and wdev may or may not be NULL.
1209 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001210
1211 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001212 result = cfg80211_dev_rename(
1213 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001214
1215 mutex_unlock(&cfg80211_mutex);
1216
1217 if (result)
1218 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001219
Jouni Malinen31888482008-10-30 16:59:24 +02001220 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1221 struct ieee80211_txq_params txq_params;
1222 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1223
1224 if (!rdev->ops->set_txq_params) {
1225 result = -EOPNOTSUPP;
1226 goto bad_res;
1227 }
1228
1229 nla_for_each_nested(nl_txq_params,
1230 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1231 rem_txq_params) {
1232 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1233 nla_data(nl_txq_params),
1234 nla_len(nl_txq_params),
1235 txq_params_policy);
1236 result = parse_txq_params(tb, &txq_params);
1237 if (result)
1238 goto bad_res;
1239
1240 result = rdev->ops->set_txq_params(&rdev->wiphy,
1241 &txq_params);
1242 if (result)
1243 goto bad_res;
1244 }
1245 }
1246
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001247 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001248 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001249 if (result)
1250 goto bad_res;
1251 }
1252
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001253 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1254 enum nl80211_tx_power_setting type;
1255 int idx, mbm = 0;
1256
1257 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001258 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001259 goto bad_res;
1260 }
1261
1262 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1263 type = nla_get_u32(info->attrs[idx]);
1264
1265 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1266 (type != NL80211_TX_POWER_AUTOMATIC)) {
1267 result = -EINVAL;
1268 goto bad_res;
1269 }
1270
1271 if (type != NL80211_TX_POWER_AUTOMATIC) {
1272 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1273 mbm = nla_get_u32(info->attrs[idx]);
1274 }
1275
1276 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1277 if (result)
1278 goto bad_res;
1279 }
1280
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001281 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1282 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1283 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001284 if ((!rdev->wiphy.available_antennas_tx &&
1285 !rdev->wiphy.available_antennas_rx) ||
1286 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001287 result = -EOPNOTSUPP;
1288 goto bad_res;
1289 }
1290
1291 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1292 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1293
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001294 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001295 * available antenna masks, except for the "all" mask */
1296 if ((~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 result = -EINVAL;
1299 goto bad_res;
1300 }
1301
Bruno Randolf7f531e02010-12-16 11:30:22 +09001302 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1303 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001304
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001305 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1306 if (result)
1307 goto bad_res;
1308 }
1309
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001310 changed = 0;
1311
1312 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1313 retry_short = nla_get_u8(
1314 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1315 if (retry_short == 0) {
1316 result = -EINVAL;
1317 goto bad_res;
1318 }
1319 changed |= WIPHY_PARAM_RETRY_SHORT;
1320 }
1321
1322 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1323 retry_long = nla_get_u8(
1324 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1325 if (retry_long == 0) {
1326 result = -EINVAL;
1327 goto bad_res;
1328 }
1329 changed |= WIPHY_PARAM_RETRY_LONG;
1330 }
1331
1332 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1333 frag_threshold = nla_get_u32(
1334 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1335 if (frag_threshold < 256) {
1336 result = -EINVAL;
1337 goto bad_res;
1338 }
1339 if (frag_threshold != (u32) -1) {
1340 /*
1341 * Fragments (apart from the last one) are required to
1342 * have even length. Make the fragmentation code
1343 * simpler by stripping LSB should someone try to use
1344 * odd threshold value.
1345 */
1346 frag_threshold &= ~0x1;
1347 }
1348 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1349 }
1350
1351 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1352 rts_threshold = nla_get_u32(
1353 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1354 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1355 }
1356
Lukáš Turek81077e82009-12-21 22:50:47 +01001357 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1358 coverage_class = nla_get_u8(
1359 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1360 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1361 }
1362
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001363 if (changed) {
1364 u8 old_retry_short, old_retry_long;
1365 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001366 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001367
1368 if (!rdev->ops->set_wiphy_params) {
1369 result = -EOPNOTSUPP;
1370 goto bad_res;
1371 }
1372
1373 old_retry_short = rdev->wiphy.retry_short;
1374 old_retry_long = rdev->wiphy.retry_long;
1375 old_frag_threshold = rdev->wiphy.frag_threshold;
1376 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001377 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001378
1379 if (changed & WIPHY_PARAM_RETRY_SHORT)
1380 rdev->wiphy.retry_short = retry_short;
1381 if (changed & WIPHY_PARAM_RETRY_LONG)
1382 rdev->wiphy.retry_long = retry_long;
1383 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1384 rdev->wiphy.frag_threshold = frag_threshold;
1385 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1386 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001387 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1388 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001389
1390 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1391 if (result) {
1392 rdev->wiphy.retry_short = old_retry_short;
1393 rdev->wiphy.retry_long = old_retry_long;
1394 rdev->wiphy.frag_threshold = old_frag_threshold;
1395 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001396 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001397 }
1398 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001399
Johannes Berg306d6112008-12-08 12:39:04 +01001400 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001401 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001402 if (netdev)
1403 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001404 return result;
1405}
1406
1407
1408static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001409 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001410 struct net_device *dev)
1411{
1412 void *hdr;
1413
1414 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1415 if (!hdr)
1416 return -1;
1417
1418 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001419 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001420 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001421 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001422
1423 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1424 rdev->devlist_generation ^
1425 (cfg80211_rdev_list_generation << 2));
1426
Johannes Berg55682962007-09-20 13:09:35 -04001427 return genlmsg_end(msg, hdr);
1428
1429 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001430 genlmsg_cancel(msg, hdr);
1431 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001432}
1433
1434static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1435{
1436 int wp_idx = 0;
1437 int if_idx = 0;
1438 int wp_start = cb->args[0];
1439 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001440 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001441 struct wireless_dev *wdev;
1442
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001443 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001444 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1445 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001446 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001447 if (wp_idx < wp_start) {
1448 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001449 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001450 }
Johannes Berg55682962007-09-20 13:09:35 -04001451 if_idx = 0;
1452
Johannes Bergf5ea9122009-08-07 16:17:38 +02001453 mutex_lock(&rdev->devlist_mtx);
1454 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001455 if (if_idx < if_start) {
1456 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001457 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001458 }
Johannes Berg55682962007-09-20 13:09:35 -04001459 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1460 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001461 rdev, wdev->netdev) < 0) {
1462 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001463 goto out;
1464 }
1465 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001466 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001467 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001468
1469 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001470 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001471 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001472 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001473
1474 cb->args[0] = wp_idx;
1475 cb->args[1] = if_idx;
1476
1477 return skb->len;
1478}
1479
1480static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1481{
1482 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001483 struct cfg80211_registered_device *dev = info->user_ptr[0];
1484 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001485
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001486 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001487 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001488 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001489
Johannes Bergd7264052009-04-19 16:23:20 +02001490 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001491 dev, netdev) < 0) {
1492 nlmsg_free(msg);
1493 return -ENOBUFS;
1494 }
Johannes Berg55682962007-09-20 13:09:35 -04001495
Johannes Berg134e6372009-07-10 09:51:34 +00001496 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001497}
1498
Michael Wu66f7ac52008-01-31 19:48:22 +01001499static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1500 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1501 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1502 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1503 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1504 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1505};
1506
1507static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1508{
1509 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1510 int flag;
1511
1512 *mntrflags = 0;
1513
1514 if (!nla)
1515 return -EINVAL;
1516
1517 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1518 nla, mntr_flags_policy))
1519 return -EINVAL;
1520
1521 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1522 if (flags[flag])
1523 *mntrflags |= (1<<flag);
1524
1525 return 0;
1526}
1527
Johannes Berg9bc383d2009-11-19 11:55:19 +01001528static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001529 struct net_device *netdev, u8 use_4addr,
1530 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001531{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001532 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001533 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001534 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001535 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001536 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001537
1538 switch (iftype) {
1539 case NL80211_IFTYPE_AP_VLAN:
1540 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1541 return 0;
1542 break;
1543 case NL80211_IFTYPE_STATION:
1544 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1545 return 0;
1546 break;
1547 default:
1548 break;
1549 }
1550
1551 return -EOPNOTSUPP;
1552}
1553
Johannes Berg55682962007-09-20 13:09:35 -04001554static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1555{
Johannes Berg4c476992010-10-04 21:36:35 +02001556 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001557 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001558 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001559 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001560 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001561 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001562 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001563
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001564 memset(&params, 0, sizeof(params));
1565
Johannes Berg04a773a2009-04-19 21:24:32 +02001566 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001567
Johannes Berg723b0382008-09-16 20:22:09 +02001568 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001569 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001570 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001571 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001572 if (ntype > NL80211_IFTYPE_MAX)
1573 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001574 }
1575
Johannes Berg92ffe052008-09-16 20:39:36 +02001576 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001577 struct wireless_dev *wdev = dev->ieee80211_ptr;
1578
Johannes Berg4c476992010-10-04 21:36:35 +02001579 if (ntype != NL80211_IFTYPE_MESH_POINT)
1580 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001581 if (netif_running(dev))
1582 return -EBUSY;
1583
1584 wdev_lock(wdev);
1585 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1586 IEEE80211_MAX_MESH_ID_LEN);
1587 wdev->mesh_id_up_len =
1588 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1589 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1590 wdev->mesh_id_up_len);
1591 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001592 }
1593
Felix Fietkau8b787642009-11-10 18:53:10 +01001594 if (info->attrs[NL80211_ATTR_4ADDR]) {
1595 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1596 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001597 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001598 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001599 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001600 } else {
1601 params.use_4addr = -1;
1602 }
1603
Johannes Berg92ffe052008-09-16 20:39:36 +02001604 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001605 if (ntype != NL80211_IFTYPE_MONITOR)
1606 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001607 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1608 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001609 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001610 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001611
1612 flags = &_flags;
1613 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001614 }
Johannes Berg3b858752009-03-12 09:55:09 +01001615
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001616 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001617 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001618 else
1619 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001620
Johannes Berg9bc383d2009-11-19 11:55:19 +01001621 if (!err && params.use_4addr != -1)
1622 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1623
Johannes Berg55682962007-09-20 13:09:35 -04001624 return err;
1625}
1626
1627static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1628{
Johannes Berg4c476992010-10-04 21:36:35 +02001629 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001630 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001631 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001632 int err;
1633 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001634 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001635
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001636 memset(&params, 0, sizeof(params));
1637
Johannes Berg55682962007-09-20 13:09:35 -04001638 if (!info->attrs[NL80211_ATTR_IFNAME])
1639 return -EINVAL;
1640
1641 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1642 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1643 if (type > NL80211_IFTYPE_MAX)
1644 return -EINVAL;
1645 }
1646
Johannes Berg79c97e92009-07-07 03:56:12 +02001647 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001648 !(rdev->wiphy.interface_modes & (1 << type)))
1649 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001650
Johannes Berg9bc383d2009-11-19 11:55:19 +01001651 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001652 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001653 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001654 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001655 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001656 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001657
Michael Wu66f7ac52008-01-31 19:48:22 +01001658 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1659 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1660 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001661 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001662 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001663 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001664 if (IS_ERR(dev))
1665 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001666
Johannes Berg29cbe682010-12-03 09:20:44 +01001667 if (type == NL80211_IFTYPE_MESH_POINT &&
1668 info->attrs[NL80211_ATTR_MESH_ID]) {
1669 struct wireless_dev *wdev = dev->ieee80211_ptr;
1670
1671 wdev_lock(wdev);
1672 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1673 IEEE80211_MAX_MESH_ID_LEN);
1674 wdev->mesh_id_up_len =
1675 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1676 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1677 wdev->mesh_id_up_len);
1678 wdev_unlock(wdev);
1679 }
1680
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001681 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001682}
1683
1684static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1685{
Johannes Berg4c476992010-10-04 21:36:35 +02001686 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1687 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001688
Johannes Berg4c476992010-10-04 21:36:35 +02001689 if (!rdev->ops->del_virtual_intf)
1690 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001691
Johannes Berg4c476992010-10-04 21:36:35 +02001692 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001693}
1694
Johannes Berg41ade002007-12-19 02:03:29 +01001695struct get_key_cookie {
1696 struct sk_buff *msg;
1697 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001698 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001699};
1700
1701static void get_key_callback(void *c, struct key_params *params)
1702{
Johannes Bergb9454e82009-07-08 13:29:08 +02001703 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001704 struct get_key_cookie *cookie = c;
1705
1706 if (params->key)
1707 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1708 params->key_len, params->key);
1709
1710 if (params->seq)
1711 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1712 params->seq_len, params->seq);
1713
1714 if (params->cipher)
1715 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1716 params->cipher);
1717
Johannes Bergb9454e82009-07-08 13:29:08 +02001718 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1719 if (!key)
1720 goto nla_put_failure;
1721
1722 if (params->key)
1723 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1724 params->key_len, params->key);
1725
1726 if (params->seq)
1727 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1728 params->seq_len, params->seq);
1729
1730 if (params->cipher)
1731 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1732 params->cipher);
1733
1734 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1735
1736 nla_nest_end(cookie->msg, key);
1737
Johannes Berg41ade002007-12-19 02:03:29 +01001738 return;
1739 nla_put_failure:
1740 cookie->error = 1;
1741}
1742
1743static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1744{
Johannes Berg4c476992010-10-04 21:36:35 +02001745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001746 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001747 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001748 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001749 const u8 *mac_addr = NULL;
1750 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001751 struct get_key_cookie cookie = {
1752 .error = 0,
1753 };
1754 void *hdr;
1755 struct sk_buff *msg;
1756
1757 if (info->attrs[NL80211_ATTR_KEY_IDX])
1758 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1759
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001760 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001761 return -EINVAL;
1762
1763 if (info->attrs[NL80211_ATTR_MAC])
1764 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1765
Johannes Berge31b8212010-10-05 19:39:30 +02001766 pairwise = !!mac_addr;
1767 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1768 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1769 if (kt >= NUM_NL80211_KEYTYPES)
1770 return -EINVAL;
1771 if (kt != NL80211_KEYTYPE_GROUP &&
1772 kt != NL80211_KEYTYPE_PAIRWISE)
1773 return -EINVAL;
1774 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1775 }
1776
Johannes Berg4c476992010-10-04 21:36:35 +02001777 if (!rdev->ops->get_key)
1778 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001779
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001780 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001781 if (!msg)
1782 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001783
1784 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1785 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001786 if (IS_ERR(hdr))
1787 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001788
1789 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001790 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001791
1792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1793 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1794 if (mac_addr)
1795 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1796
Johannes Berge31b8212010-10-05 19:39:30 +02001797 if (pairwise && mac_addr &&
1798 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1799 return -ENOENT;
1800
1801 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1802 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001803
1804 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001805 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001806
1807 if (cookie.error)
1808 goto nla_put_failure;
1809
1810 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001811 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001812
1813 nla_put_failure:
1814 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001815 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001816 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001817 return err;
1818}
1819
1820static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1821{
Johannes Berg4c476992010-10-04 21:36:35 +02001822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001823 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001824 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001825 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001826
Johannes Bergb9454e82009-07-08 13:29:08 +02001827 err = nl80211_parse_key(info, &key);
1828 if (err)
1829 return err;
1830
1831 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001832 return -EINVAL;
1833
Johannes Bergb9454e82009-07-08 13:29:08 +02001834 /* only support setting default key */
1835 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001836 return -EINVAL;
1837
Johannes Bergfffd0932009-07-08 14:22:54 +02001838 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001839
1840 if (key.def) {
1841 if (!rdev->ops->set_default_key) {
1842 err = -EOPNOTSUPP;
1843 goto out;
1844 }
1845
1846 err = nl80211_key_allowed(dev->ieee80211_ptr);
1847 if (err)
1848 goto out;
1849
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001850 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1851 key.def_uni, key.def_multi);
1852
1853 if (err)
1854 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001855
Johannes Berg3d23e342009-09-29 23:27:28 +02001856#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001857 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001858#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001859 } else {
1860 if (key.def_uni || !key.def_multi) {
1861 err = -EINVAL;
1862 goto out;
1863 }
1864
1865 if (!rdev->ops->set_default_mgmt_key) {
1866 err = -EOPNOTSUPP;
1867 goto out;
1868 }
1869
1870 err = nl80211_key_allowed(dev->ieee80211_ptr);
1871 if (err)
1872 goto out;
1873
1874 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1875 dev, key.idx);
1876 if (err)
1877 goto out;
1878
1879#ifdef CONFIG_CFG80211_WEXT
1880 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1881#endif
1882 }
1883
1884 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001885 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001886
Johannes Berg41ade002007-12-19 02:03:29 +01001887 return err;
1888}
1889
1890static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1891{
Johannes Berg4c476992010-10-04 21:36:35 +02001892 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001893 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001894 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001895 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001896 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001897
Johannes Bergb9454e82009-07-08 13:29:08 +02001898 err = nl80211_parse_key(info, &key);
1899 if (err)
1900 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001901
Johannes Bergb9454e82009-07-08 13:29:08 +02001902 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001903 return -EINVAL;
1904
Johannes Berg41ade002007-12-19 02:03:29 +01001905 if (info->attrs[NL80211_ATTR_MAC])
1906 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1907
Johannes Berge31b8212010-10-05 19:39:30 +02001908 if (key.type == -1) {
1909 if (mac_addr)
1910 key.type = NL80211_KEYTYPE_PAIRWISE;
1911 else
1912 key.type = NL80211_KEYTYPE_GROUP;
1913 }
1914
1915 /* for now */
1916 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1917 key.type != NL80211_KEYTYPE_GROUP)
1918 return -EINVAL;
1919
Johannes Berg4c476992010-10-04 21:36:35 +02001920 if (!rdev->ops->add_key)
1921 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001922
Johannes Berge31b8212010-10-05 19:39:30 +02001923 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1924 key.type == NL80211_KEYTYPE_PAIRWISE,
1925 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001926 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001927
1928 wdev_lock(dev->ieee80211_ptr);
1929 err = nl80211_key_allowed(dev->ieee80211_ptr);
1930 if (!err)
1931 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001932 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001933 mac_addr, &key.p);
1934 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001935
Johannes Berg41ade002007-12-19 02:03:29 +01001936 return err;
1937}
1938
1939static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1940{
Johannes Berg4c476992010-10-04 21:36:35 +02001941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001942 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001943 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001944 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001945 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001946
Johannes Bergb9454e82009-07-08 13:29:08 +02001947 err = nl80211_parse_key(info, &key);
1948 if (err)
1949 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001950
1951 if (info->attrs[NL80211_ATTR_MAC])
1952 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1953
Johannes Berge31b8212010-10-05 19:39:30 +02001954 if (key.type == -1) {
1955 if (mac_addr)
1956 key.type = NL80211_KEYTYPE_PAIRWISE;
1957 else
1958 key.type = NL80211_KEYTYPE_GROUP;
1959 }
1960
1961 /* for now */
1962 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1963 key.type != NL80211_KEYTYPE_GROUP)
1964 return -EINVAL;
1965
Johannes Berg4c476992010-10-04 21:36:35 +02001966 if (!rdev->ops->del_key)
1967 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001968
Johannes Bergfffd0932009-07-08 14:22:54 +02001969 wdev_lock(dev->ieee80211_ptr);
1970 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001971
1972 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1973 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1974 err = -ENOENT;
1975
Johannes Bergfffd0932009-07-08 14:22:54 +02001976 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001977 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1978 key.type == NL80211_KEYTYPE_PAIRWISE,
1979 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001980
Johannes Berg3d23e342009-09-29 23:27:28 +02001981#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001982 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001983 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001984 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001985 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001986 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1987 }
1988#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001989 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001990
Johannes Berg41ade002007-12-19 02:03:29 +01001991 return err;
1992}
1993
Johannes Berged1b6cc2007-12-19 02:03:32 +01001994static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1995{
1996 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1997 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001998 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1999 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002000 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002001 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002002 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002003
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002004 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2005 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2006 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2007 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002008 return -EINVAL;
2009
Johannes Berg074ac8d2010-09-16 14:58:22 +02002010 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002011 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2012 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002013
Johannes Berg56d18932011-05-09 18:41:15 +02002014 memset(&params, 0, sizeof(params));
2015
Johannes Berged1b6cc2007-12-19 02:03:32 +01002016 switch (info->genlhdr->cmd) {
2017 case NL80211_CMD_NEW_BEACON:
2018 /* these are required for NEW_BEACON */
2019 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2020 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002021 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2022 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002023
Johannes Berg56d18932011-05-09 18:41:15 +02002024 params.interval =
2025 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2026 params.dtim_period =
2027 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2028
2029 err = cfg80211_validate_beacon_int(rdev, params.interval);
2030 if (err)
2031 return err;
2032
Jouni Malinen32e9de82011-08-10 23:53:31 +03002033 /*
2034 * In theory, some of these attributes could be required for
2035 * NEW_BEACON, but since they were not used when the command was
2036 * originally added, keep them optional for old user space
2037 * programs to work with drivers that do not need the additional
2038 * information.
2039 */
2040 if (info->attrs[NL80211_ATTR_SSID]) {
2041 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2042 params.ssid_len =
2043 nla_len(info->attrs[NL80211_ATTR_SSID]);
2044 if (params.ssid_len == 0 ||
2045 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2046 return -EINVAL;
2047 }
2048
2049 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2050 params.hidden_ssid = nla_get_u32(
2051 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2052 if (params.hidden_ssid !=
2053 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2054 params.hidden_ssid !=
2055 NL80211_HIDDEN_SSID_ZERO_LEN &&
2056 params.hidden_ssid !=
2057 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2058 return -EINVAL;
2059 }
2060
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002061 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2062
2063 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2064 params.auth_type = nla_get_u32(
2065 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2066 if (!nl80211_valid_auth_type(params.auth_type))
2067 return -EINVAL;
2068 } else
2069 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2070
2071 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2072 NL80211_MAX_NR_CIPHER_SUITES);
2073 if (err)
2074 return err;
2075
Johannes Berg79c97e92009-07-07 03:56:12 +02002076 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002077 break;
2078 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002079 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002080 break;
2081 default:
2082 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002083 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002084 }
2085
Johannes Berg4c476992010-10-04 21:36:35 +02002086 if (!call)
2087 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002088
Johannes Berged1b6cc2007-12-19 02:03:32 +01002089 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2090 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2091 params.head_len =
2092 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2093 haveinfo = 1;
2094 }
2095
2096 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2097 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2098 params.tail_len =
2099 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2100 haveinfo = 1;
2101 }
2102
Johannes Berg4c476992010-10-04 21:36:35 +02002103 if (!haveinfo)
2104 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002105
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002106 if (info->attrs[NL80211_ATTR_IE]) {
2107 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2108 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2109 }
2110
2111 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2112 params.proberesp_ies =
2113 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2114 params.proberesp_ies_len =
2115 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2116 }
2117
2118 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2119 params.assocresp_ies =
2120 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2121 params.assocresp_ies_len =
2122 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2123 }
2124
Johannes Berg56d18932011-05-09 18:41:15 +02002125 err = call(&rdev->wiphy, dev, &params);
2126 if (!err && params.interval)
2127 wdev->beacon_interval = params.interval;
2128 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002129}
2130
2131static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2132{
Johannes Berg4c476992010-10-04 21:36:35 +02002133 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2134 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002135 struct wireless_dev *wdev = dev->ieee80211_ptr;
2136 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002137
Johannes Berg4c476992010-10-04 21:36:35 +02002138 if (!rdev->ops->del_beacon)
2139 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002140
Johannes Berg074ac8d2010-09-16 14:58:22 +02002141 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002142 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2143 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002144
Johannes Berg56d18932011-05-09 18:41:15 +02002145 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2146 if (!err)
2147 wdev->beacon_interval = 0;
2148 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002149}
2150
Johannes Berg5727ef12007-12-19 02:03:34 +01002151static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2152 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2153 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2154 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002155 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002156 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002157};
2158
Johannes Bergeccb8e82009-05-11 21:57:56 +03002159static int parse_station_flags(struct genl_info *info,
2160 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002161{
2162 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002163 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002164 int flag;
2165
Johannes Bergeccb8e82009-05-11 21:57:56 +03002166 /*
2167 * Try parsing the new attribute first so userspace
2168 * can specify both for older kernels.
2169 */
2170 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2171 if (nla) {
2172 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002173
Johannes Bergeccb8e82009-05-11 21:57:56 +03002174 sta_flags = nla_data(nla);
2175 params->sta_flags_mask = sta_flags->mask;
2176 params->sta_flags_set = sta_flags->set;
2177 if ((params->sta_flags_mask |
2178 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2179 return -EINVAL;
2180 return 0;
2181 }
2182
2183 /* if present, parse the old attribute */
2184
2185 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002186 if (!nla)
2187 return 0;
2188
2189 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2190 nla, sta_flags_policy))
2191 return -EINVAL;
2192
Johannes Bergeccb8e82009-05-11 21:57:56 +03002193 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2194 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002195
2196 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2197 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002198 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002199
2200 return 0;
2201}
2202
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002203static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2204 int attr)
2205{
2206 struct nlattr *rate;
2207 u16 bitrate;
2208
2209 rate = nla_nest_start(msg, attr);
2210 if (!rate)
2211 goto nla_put_failure;
2212
2213 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2214 bitrate = cfg80211_calculate_bitrate(info);
2215 if (bitrate > 0)
2216 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2217
2218 if (info->flags & RATE_INFO_FLAGS_MCS)
2219 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2220 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2221 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2222 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2223 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2224
2225 nla_nest_end(msg, rate);
2226 return true;
2227
2228nla_put_failure:
2229 return false;
2230}
2231
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002232static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2233 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002234 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002235{
2236 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002237 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002238
2239 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2240 if (!hdr)
2241 return -1;
2242
2243 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2244 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2245
Johannes Bergf5ea9122009-08-07 16:17:38 +02002246 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2247
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002248 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2249 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002250 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302251 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2252 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2253 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002254 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2255 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2256 sinfo->inactive_time);
2257 if (sinfo->filled & STATION_INFO_RX_BYTES)
2258 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2259 sinfo->rx_bytes);
2260 if (sinfo->filled & STATION_INFO_TX_BYTES)
2261 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2262 sinfo->tx_bytes);
2263 if (sinfo->filled & STATION_INFO_LLID)
2264 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2265 sinfo->llid);
2266 if (sinfo->filled & STATION_INFO_PLID)
2267 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2268 sinfo->plid);
2269 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2270 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2271 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002272 if (sinfo->filled & STATION_INFO_SIGNAL)
2273 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2274 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002275 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2276 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2277 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002278 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002279 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2280 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002281 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002282 }
2283 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2284 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2285 NL80211_STA_INFO_RX_BITRATE))
2286 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002287 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002288 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2289 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2290 sinfo->rx_packets);
2291 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2292 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2293 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002294 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2295 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2296 sinfo->tx_retries);
2297 if (sinfo->filled & STATION_INFO_TX_FAILED)
2298 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2299 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002300 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2301 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2302 if (!bss_param)
2303 goto nla_put_failure;
2304
2305 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2306 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2307 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2308 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2309 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2310 NLA_PUT_FLAG(msg,
2311 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2312 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2313 sinfo->bss_param.dtim_period);
2314 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2315 sinfo->bss_param.beacon_interval);
2316
2317 nla_nest_end(msg, bss_param);
2318 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002319 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002320
Felix Fietkau040bdf72011-08-10 19:00:33 -06002321 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002322 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2323 sinfo->assoc_req_ies);
2324
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002325 return genlmsg_end(msg, hdr);
2326
2327 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002328 genlmsg_cancel(msg, hdr);
2329 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002330}
2331
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002332static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002333 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002334{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335 struct station_info sinfo;
2336 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002337 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002338 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002340 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341
Johannes Berg67748892010-10-04 21:14:06 +02002342 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2343 if (err)
2344 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002345
2346 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002347 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002348 goto out_err;
2349 }
2350
Johannes Bergbba95fe2008-07-29 13:22:51 +02002351 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002352 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002353 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2354 mac_addr, &sinfo);
2355 if (err == -ENOENT)
2356 break;
2357 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002358 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002359
2360 if (nl80211_send_station(skb,
2361 NETLINK_CB(cb->skb).pid,
2362 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2363 netdev, mac_addr,
2364 &sinfo) < 0)
2365 goto out;
2366
2367 sta_idx++;
2368 }
2369
2370
2371 out:
2372 cb->args[1] = sta_idx;
2373 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002374 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002375 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002376
2377 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002379
Johannes Berg5727ef12007-12-19 02:03:34 +01002380static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2381{
Johannes Berg4c476992010-10-04 21:36:35 +02002382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2383 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002385 struct sk_buff *msg;
2386 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002387 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002388
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002389 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002390
2391 if (!info->attrs[NL80211_ATTR_MAC])
2392 return -EINVAL;
2393
2394 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2395
Johannes Berg4c476992010-10-04 21:36:35 +02002396 if (!rdev->ops->get_station)
2397 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002398
Johannes Berg79c97e92009-07-07 03:56:12 +02002399 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002400 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002401 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002402
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002403 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002404 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002405 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002406
2407 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002408 dev, mac_addr, &sinfo) < 0) {
2409 nlmsg_free(msg);
2410 return -ENOBUFS;
2411 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002412
Johannes Berg4c476992010-10-04 21:36:35 +02002413 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002414}
2415
2416/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002417 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002418 */
Johannes Berg463d0182009-07-14 00:33:35 +02002419static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002420 struct cfg80211_registered_device *rdev,
2421 struct net_device **vlan)
2422{
Johannes Berg463d0182009-07-14 00:33:35 +02002423 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002424 *vlan = NULL;
2425
2426 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002427 *vlan = dev_get_by_index(genl_info_net(info),
2428 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002429 if (!*vlan)
2430 return -ENODEV;
2431 if (!(*vlan)->ieee80211_ptr)
2432 return -EINVAL;
2433 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2434 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002435 if (!netif_running(*vlan))
2436 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002437 }
2438 return 0;
2439}
2440
2441static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2442{
Johannes Berg4c476992010-10-04 21:36:35 +02002443 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002444 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002445 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002446 struct station_parameters params;
2447 u8 *mac_addr = NULL;
2448
2449 memset(&params, 0, sizeof(params));
2450
2451 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002452 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002453
2454 if (info->attrs[NL80211_ATTR_STA_AID])
2455 return -EINVAL;
2456
2457 if (!info->attrs[NL80211_ATTR_MAC])
2458 return -EINVAL;
2459
2460 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2461
2462 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2463 params.supported_rates =
2464 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2465 params.supported_rates_len =
2466 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2467 }
2468
2469 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2470 params.listen_interval =
2471 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2472
Jouni Malinen36aedc92008-08-25 11:58:58 +03002473 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2474 params.ht_capa =
2475 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2476
Johannes Bergeccb8e82009-05-11 21:57:56 +03002477 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002478 return -EINVAL;
2479
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002480 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2481 params.plink_action =
2482 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2483
Javier Cardona9c3990a2011-05-03 16:57:11 -07002484 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2485 params.plink_state =
2486 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2487
Johannes Berg463d0182009-07-14 00:33:35 +02002488 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002489 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002490 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002491
2492 /* validate settings */
2493 err = 0;
2494
2495 switch (dev->ieee80211_ptr->iftype) {
2496 case NL80211_IFTYPE_AP:
2497 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002498 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002499 /* disallow mesh-specific things */
2500 if (params.plink_action)
2501 err = -EINVAL;
2502 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002503 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002504 case NL80211_IFTYPE_STATION:
2505 /* disallow everything but AUTHORIZED flag */
2506 if (params.plink_action)
2507 err = -EINVAL;
2508 if (params.vlan)
2509 err = -EINVAL;
2510 if (params.supported_rates)
2511 err = -EINVAL;
2512 if (params.ht_capa)
2513 err = -EINVAL;
2514 if (params.listen_interval >= 0)
2515 err = -EINVAL;
2516 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2517 err = -EINVAL;
2518 break;
2519 case NL80211_IFTYPE_MESH_POINT:
2520 /* disallow things mesh doesn't support */
2521 if (params.vlan)
2522 err = -EINVAL;
2523 if (params.ht_capa)
2524 err = -EINVAL;
2525 if (params.listen_interval >= 0)
2526 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002527 if (params.sta_flags_mask &
2528 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002529 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002530 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002531 err = -EINVAL;
2532 break;
2533 default:
2534 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002535 }
2536
Johannes Berg5727ef12007-12-19 02:03:34 +01002537 if (err)
2538 goto out;
2539
Johannes Berg79c97e92009-07-07 03:56:12 +02002540 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002541 err = -EOPNOTSUPP;
2542 goto out;
2543 }
2544
Johannes Berg79c97e92009-07-07 03:56:12 +02002545 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002546
2547 out:
2548 if (params.vlan)
2549 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002550
Johannes Berg5727ef12007-12-19 02:03:34 +01002551 return err;
2552}
2553
Eliad Pellerc75786c2011-08-23 14:37:46 +03002554static struct nla_policy
2555nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2556 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2557 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2558};
2559
Johannes Berg5727ef12007-12-19 02:03:34 +01002560static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2561{
Johannes Berg4c476992010-10-04 21:36:35 +02002562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002563 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002564 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002565 struct station_parameters params;
2566 u8 *mac_addr = NULL;
2567
2568 memset(&params, 0, sizeof(params));
2569
2570 if (!info->attrs[NL80211_ATTR_MAC])
2571 return -EINVAL;
2572
Johannes Berg5727ef12007-12-19 02:03:34 +01002573 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2574 return -EINVAL;
2575
2576 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2577 return -EINVAL;
2578
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002579 if (!info->attrs[NL80211_ATTR_STA_AID])
2580 return -EINVAL;
2581
Johannes Berg5727ef12007-12-19 02:03:34 +01002582 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2583 params.supported_rates =
2584 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2585 params.supported_rates_len =
2586 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2587 params.listen_interval =
2588 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002589
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002590 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2591 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2592 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002593
Jouni Malinen36aedc92008-08-25 11:58:58 +03002594 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2595 params.ht_capa =
2596 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002597
Javier Cardona96b78df2011-04-07 15:08:33 -07002598 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2599 params.plink_action =
2600 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2601
Johannes Bergeccb8e82009-05-11 21:57:56 +03002602 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002603 return -EINVAL;
2604
Eliad Pellerc75786c2011-08-23 14:37:46 +03002605 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002606 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2607 (params.sta_flags_set & NL80211_STA_FLAG_WME) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002608 info->attrs[NL80211_ATTR_STA_WME]) {
2609 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2610 struct nlattr *nla;
2611
2612 nla = info->attrs[NL80211_ATTR_STA_WME];
2613 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2614 nl80211_sta_wme_policy);
2615 if (err)
2616 return err;
2617
2618 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2619 params.uapsd_queues =
2620 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2621
2622 if (tb[NL80211_STA_WME_MAX_SP])
2623 params.max_sp =
2624 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2625 }
2626
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002627 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002628 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002629 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002630 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2631 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002632
Johannes Berg463d0182009-07-14 00:33:35 +02002633 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002634 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002635 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002636
2637 /* validate settings */
2638 err = 0;
2639
Johannes Berg79c97e92009-07-07 03:56:12 +02002640 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002641 err = -EOPNOTSUPP;
2642 goto out;
2643 }
2644
Johannes Berg79c97e92009-07-07 03:56:12 +02002645 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002646
2647 out:
2648 if (params.vlan)
2649 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002650 return err;
2651}
2652
2653static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2654{
Johannes Berg4c476992010-10-04 21:36:35 +02002655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2656 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002657 u8 *mac_addr = NULL;
2658
2659 if (info->attrs[NL80211_ATTR_MAC])
2660 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2661
Johannes Berge80cf852009-05-11 14:43:13 +02002662 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002663 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002664 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002665 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2666 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002667
Johannes Berg4c476992010-10-04 21:36:35 +02002668 if (!rdev->ops->del_station)
2669 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002670
Johannes Berg4c476992010-10-04 21:36:35 +02002671 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002672}
2673
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002674static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2675 int flags, struct net_device *dev,
2676 u8 *dst, u8 *next_hop,
2677 struct mpath_info *pinfo)
2678{
2679 void *hdr;
2680 struct nlattr *pinfoattr;
2681
2682 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2683 if (!hdr)
2684 return -1;
2685
2686 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2687 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2688 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2689
Johannes Bergf5ea9122009-08-07 16:17:38 +02002690 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2691
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002692 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2693 if (!pinfoattr)
2694 goto nla_put_failure;
2695 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2696 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2697 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002698 if (pinfo->filled & MPATH_INFO_SN)
2699 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2700 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002701 if (pinfo->filled & MPATH_INFO_METRIC)
2702 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2703 pinfo->metric);
2704 if (pinfo->filled & MPATH_INFO_EXPTIME)
2705 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2706 pinfo->exptime);
2707 if (pinfo->filled & MPATH_INFO_FLAGS)
2708 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2709 pinfo->flags);
2710 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2711 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2712 pinfo->discovery_timeout);
2713 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2714 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2715 pinfo->discovery_retries);
2716
2717 nla_nest_end(msg, pinfoattr);
2718
2719 return genlmsg_end(msg, hdr);
2720
2721 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002722 genlmsg_cancel(msg, hdr);
2723 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002724}
2725
2726static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002727 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002728{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002729 struct mpath_info pinfo;
2730 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002731 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002732 u8 dst[ETH_ALEN];
2733 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002734 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002735 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002736
Johannes Berg67748892010-10-04 21:14:06 +02002737 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2738 if (err)
2739 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002740
2741 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002742 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002743 goto out_err;
2744 }
2745
Jouni Malineneec60b02009-03-20 21:21:19 +02002746 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2747 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002748 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002749 }
2750
Johannes Bergbba95fe2008-07-29 13:22:51 +02002751 while (1) {
2752 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2753 dst, next_hop, &pinfo);
2754 if (err == -ENOENT)
2755 break;
2756 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002757 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002758
2759 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2760 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2761 netdev, dst, next_hop,
2762 &pinfo) < 0)
2763 goto out;
2764
2765 path_idx++;
2766 }
2767
2768
2769 out:
2770 cb->args[1] = path_idx;
2771 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002772 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002773 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002774 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002775}
2776
2777static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2778{
Johannes Berg4c476992010-10-04 21:36:35 +02002779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002780 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002781 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002782 struct mpath_info pinfo;
2783 struct sk_buff *msg;
2784 u8 *dst = NULL;
2785 u8 next_hop[ETH_ALEN];
2786
2787 memset(&pinfo, 0, sizeof(pinfo));
2788
2789 if (!info->attrs[NL80211_ATTR_MAC])
2790 return -EINVAL;
2791
2792 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2793
Johannes Berg4c476992010-10-04 21:36:35 +02002794 if (!rdev->ops->get_mpath)
2795 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002796
Johannes Berg4c476992010-10-04 21:36:35 +02002797 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2798 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002799
Johannes Berg79c97e92009-07-07 03:56:12 +02002800 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002801 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002802 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002803
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002804 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002805 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002806 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002807
2808 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002809 dev, dst, next_hop, &pinfo) < 0) {
2810 nlmsg_free(msg);
2811 return -ENOBUFS;
2812 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002813
Johannes Berg4c476992010-10-04 21:36:35 +02002814 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002815}
2816
2817static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2818{
Johannes Berg4c476992010-10-04 21:36:35 +02002819 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2820 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002821 u8 *dst = NULL;
2822 u8 *next_hop = NULL;
2823
2824 if (!info->attrs[NL80211_ATTR_MAC])
2825 return -EINVAL;
2826
2827 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2828 return -EINVAL;
2829
2830 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2831 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2832
Johannes Berg4c476992010-10-04 21:36:35 +02002833 if (!rdev->ops->change_mpath)
2834 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002835
Johannes Berg4c476992010-10-04 21:36:35 +02002836 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2837 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002838
Johannes Berg4c476992010-10-04 21:36:35 +02002839 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002840}
Johannes Berg4c476992010-10-04 21:36:35 +02002841
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002842static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2843{
Johannes Berg4c476992010-10-04 21:36:35 +02002844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2845 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002846 u8 *dst = NULL;
2847 u8 *next_hop = NULL;
2848
2849 if (!info->attrs[NL80211_ATTR_MAC])
2850 return -EINVAL;
2851
2852 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2853 return -EINVAL;
2854
2855 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2856 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2857
Johannes Berg4c476992010-10-04 21:36:35 +02002858 if (!rdev->ops->add_mpath)
2859 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002860
Johannes Berg4c476992010-10-04 21:36:35 +02002861 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2862 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002863
Johannes Berg4c476992010-10-04 21:36:35 +02002864 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002865}
2866
2867static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2868{
Johannes Berg4c476992010-10-04 21:36:35 +02002869 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2870 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002871 u8 *dst = NULL;
2872
2873 if (info->attrs[NL80211_ATTR_MAC])
2874 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2875
Johannes Berg4c476992010-10-04 21:36:35 +02002876 if (!rdev->ops->del_mpath)
2877 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002878
Johannes Berg4c476992010-10-04 21:36:35 +02002879 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002880}
2881
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002882static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2883{
Johannes Berg4c476992010-10-04 21:36:35 +02002884 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2885 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002886 struct bss_parameters params;
2887
2888 memset(&params, 0, sizeof(params));
2889 /* default to not changing parameters */
2890 params.use_cts_prot = -1;
2891 params.use_short_preamble = -1;
2892 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002893 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002894 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002895
2896 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2897 params.use_cts_prot =
2898 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2899 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2900 params.use_short_preamble =
2901 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2902 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2903 params.use_short_slot_time =
2904 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002905 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2906 params.basic_rates =
2907 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2908 params.basic_rates_len =
2909 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2910 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002911 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2912 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002913 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2914 params.ht_opmode =
2915 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002916
Johannes Berg4c476992010-10-04 21:36:35 +02002917 if (!rdev->ops->change_bss)
2918 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002919
Johannes Berg074ac8d2010-09-16 14:58:22 +02002920 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002921 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2922 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002923
Johannes Berg4c476992010-10-04 21:36:35 +02002924 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002925}
2926
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002927static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002928 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2929 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2930 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2931 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2932 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2933 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2934};
2935
2936static int parse_reg_rule(struct nlattr *tb[],
2937 struct ieee80211_reg_rule *reg_rule)
2938{
2939 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2940 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2941
2942 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2943 return -EINVAL;
2944 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2945 return -EINVAL;
2946 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2947 return -EINVAL;
2948 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2949 return -EINVAL;
2950 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2951 return -EINVAL;
2952
2953 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2954
2955 freq_range->start_freq_khz =
2956 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2957 freq_range->end_freq_khz =
2958 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2959 freq_range->max_bandwidth_khz =
2960 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2961
2962 power_rule->max_eirp =
2963 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2964
2965 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2966 power_rule->max_antenna_gain =
2967 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2968
2969 return 0;
2970}
2971
2972static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2973{
2974 int r;
2975 char *data = NULL;
2976
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002977 /*
2978 * You should only get this when cfg80211 hasn't yet initialized
2979 * completely when built-in to the kernel right between the time
2980 * window between nl80211_init() and regulatory_init(), if that is
2981 * even possible.
2982 */
2983 mutex_lock(&cfg80211_mutex);
2984 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002985 mutex_unlock(&cfg80211_mutex);
2986 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002987 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002988 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002989
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002990 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2991 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002992
2993 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2994
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002995 r = regulatory_hint_user(data);
2996
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002997 return r;
2998}
2999
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003000static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003001 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003002{
Johannes Berg4c476992010-10-04 21:36:35 +02003003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003004 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003005 struct wireless_dev *wdev = dev->ieee80211_ptr;
3006 struct mesh_config cur_params;
3007 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003008 void *hdr;
3009 struct nlattr *pinfoattr;
3010 struct sk_buff *msg;
3011
Johannes Berg29cbe682010-12-03 09:20:44 +01003012 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3013 return -EOPNOTSUPP;
3014
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003015 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003016 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003017
Johannes Berg29cbe682010-12-03 09:20:44 +01003018 wdev_lock(wdev);
3019 /* If not connected, get default parameters */
3020 if (!wdev->mesh_id_len)
3021 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3022 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003023 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003024 &cur_params);
3025 wdev_unlock(wdev);
3026
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003027 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003028 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003029
3030 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003032 if (!msg)
3033 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003034 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003035 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003036 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003037 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003038 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003039 if (!pinfoattr)
3040 goto nla_put_failure;
3041 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3042 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3043 cur_params.dot11MeshRetryTimeout);
3044 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3045 cur_params.dot11MeshConfirmTimeout);
3046 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3047 cur_params.dot11MeshHoldingTimeout);
3048 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3049 cur_params.dot11MeshMaxPeerLinks);
3050 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3051 cur_params.dot11MeshMaxRetries);
3052 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3053 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003054 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3055 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003056 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3057 cur_params.auto_open_plinks);
3058 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3059 cur_params.dot11MeshHWMPmaxPREQretries);
3060 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3061 cur_params.path_refresh_time);
3062 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3063 cur_params.min_discovery_timeout);
3064 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3065 cur_params.dot11MeshHWMPactivePathTimeout);
3066 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3067 cur_params.dot11MeshHWMPpreqMinInterval);
3068 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3069 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003070 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3071 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003072 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3073 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003074 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3075 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003076 nla_nest_end(msg, pinfoattr);
3077 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003078 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003079
Johannes Berg3b858752009-03-12 09:55:09 +01003080 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003081 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003082 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003083 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003084 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003085}
3086
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003087static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003088 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3089 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3090 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3091 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3092 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3093 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003094 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003095 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3096
3097 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3098 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3099 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3100 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3101 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3102 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003103 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003104 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003105 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003106};
3107
Javier Cardonac80d5452010-12-16 17:37:49 -08003108static const struct nla_policy
3109 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3110 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3111 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003112 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003113 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003114 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003115 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003116};
3117
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003118static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003119 struct mesh_config *cfg,
3120 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003121{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003122 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003123 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003124
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003125#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3126do {\
3127 if (table[attr_num]) {\
3128 cfg->param = nla_fn(table[attr_num]); \
3129 mask |= (1 << (attr_num - 1)); \
3130 } \
3131} while (0);\
3132
3133
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003134 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003135 return -EINVAL;
3136 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003137 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003138 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003139 return -EINVAL;
3140
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003141 /* This makes sure that there aren't more than 32 mesh config
3142 * parameters (otherwise our bitfield scheme would not work.) */
3143 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3144
3145 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003146 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3147 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3148 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3149 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3150 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3151 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3152 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3153 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3154 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3155 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3156 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3157 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003158 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3159 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003160 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3161 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3162 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3163 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3164 nla_get_u8);
3165 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3166 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3167 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3168 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3169 nla_get_u16);
3170 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3171 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3172 nla_get_u32);
3173 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3174 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3175 nla_get_u16);
3176 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3177 dot11MeshHWMPnetDiameterTraversalTime,
3178 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3179 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003180 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3181 dot11MeshHWMPRootMode, mask,
3182 NL80211_MESHCONF_HWMP_ROOTMODE,
3183 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003184 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3185 dot11MeshHWMPRannInterval, mask,
3186 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3187 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3189 dot11MeshGateAnnouncementProtocol, mask,
3190 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3191 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003192 if (mask_out)
3193 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003194
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003195 return 0;
3196
3197#undef FILL_IN_MESH_PARAM_IF_SET
3198}
3199
Javier Cardonac80d5452010-12-16 17:37:49 -08003200static int nl80211_parse_mesh_setup(struct genl_info *info,
3201 struct mesh_setup *setup)
3202{
3203 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3204
3205 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3206 return -EINVAL;
3207 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3208 info->attrs[NL80211_ATTR_MESH_SETUP],
3209 nl80211_mesh_setup_params_policy))
3210 return -EINVAL;
3211
3212 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3213 setup->path_sel_proto =
3214 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3215 IEEE80211_PATH_PROTOCOL_VENDOR :
3216 IEEE80211_PATH_PROTOCOL_HWMP;
3217
3218 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3219 setup->path_metric =
3220 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3221 IEEE80211_PATH_METRIC_VENDOR :
3222 IEEE80211_PATH_METRIC_AIRTIME;
3223
Javier Cardona581a8b02011-04-07 15:08:27 -07003224
3225 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003226 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003227 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003228 if (!is_valid_ie_attr(ieattr))
3229 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003230 setup->ie = nla_data(ieattr);
3231 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003232 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003233 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3234 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003235
3236 return 0;
3237}
3238
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003239static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003240 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003241{
3242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3243 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003244 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003245 struct mesh_config cfg;
3246 u32 mask;
3247 int err;
3248
Johannes Berg29cbe682010-12-03 09:20:44 +01003249 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3250 return -EOPNOTSUPP;
3251
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003252 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003253 return -EOPNOTSUPP;
3254
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003255 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003256 if (err)
3257 return err;
3258
Johannes Berg29cbe682010-12-03 09:20:44 +01003259 wdev_lock(wdev);
3260 if (!wdev->mesh_id_len)
3261 err = -ENOLINK;
3262
3263 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003264 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003265 mask, &cfg);
3266
3267 wdev_unlock(wdev);
3268
3269 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003270}
3271
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003272static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3273{
3274 struct sk_buff *msg;
3275 void *hdr = NULL;
3276 struct nlattr *nl_reg_rules;
3277 unsigned int i;
3278 int err = -EINVAL;
3279
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003280 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003281
3282 if (!cfg80211_regdomain)
3283 goto out;
3284
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003286 if (!msg) {
3287 err = -ENOBUFS;
3288 goto out;
3289 }
3290
3291 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3292 NL80211_CMD_GET_REG);
3293 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003294 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003295
3296 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3297 cfg80211_regdomain->alpha2);
3298
3299 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3300 if (!nl_reg_rules)
3301 goto nla_put_failure;
3302
3303 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3304 struct nlattr *nl_reg_rule;
3305 const struct ieee80211_reg_rule *reg_rule;
3306 const struct ieee80211_freq_range *freq_range;
3307 const struct ieee80211_power_rule *power_rule;
3308
3309 reg_rule = &cfg80211_regdomain->reg_rules[i];
3310 freq_range = &reg_rule->freq_range;
3311 power_rule = &reg_rule->power_rule;
3312
3313 nl_reg_rule = nla_nest_start(msg, i);
3314 if (!nl_reg_rule)
3315 goto nla_put_failure;
3316
3317 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3318 reg_rule->flags);
3319 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3320 freq_range->start_freq_khz);
3321 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3322 freq_range->end_freq_khz);
3323 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3324 freq_range->max_bandwidth_khz);
3325 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3326 power_rule->max_antenna_gain);
3327 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3328 power_rule->max_eirp);
3329
3330 nla_nest_end(msg, nl_reg_rule);
3331 }
3332
3333 nla_nest_end(msg, nl_reg_rules);
3334
3335 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003336 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003337 goto out;
3338
3339nla_put_failure:
3340 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003341put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003342 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003343 err = -EMSGSIZE;
3344out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003345 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003346 return err;
3347}
3348
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003349static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3350{
3351 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3352 struct nlattr *nl_reg_rule;
3353 char *alpha2 = NULL;
3354 int rem_reg_rules = 0, r = 0;
3355 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3356 struct ieee80211_regdomain *rd = NULL;
3357
3358 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3359 return -EINVAL;
3360
3361 if (!info->attrs[NL80211_ATTR_REG_RULES])
3362 return -EINVAL;
3363
3364 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3365
3366 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3367 rem_reg_rules) {
3368 num_rules++;
3369 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003370 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003371 }
3372
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003373 mutex_lock(&cfg80211_mutex);
3374
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003375 if (!reg_is_valid_request(alpha2)) {
3376 r = -EINVAL;
3377 goto bad_reg;
3378 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003379
3380 size_of_regd = sizeof(struct ieee80211_regdomain) +
3381 (num_rules * sizeof(struct ieee80211_reg_rule));
3382
3383 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003384 if (!rd) {
3385 r = -ENOMEM;
3386 goto bad_reg;
3387 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003388
3389 rd->n_reg_rules = num_rules;
3390 rd->alpha2[0] = alpha2[0];
3391 rd->alpha2[1] = alpha2[1];
3392
3393 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3394 rem_reg_rules) {
3395 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3396 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3397 reg_rule_policy);
3398 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3399 if (r)
3400 goto bad_reg;
3401
3402 rule_idx++;
3403
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003404 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3405 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003406 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003407 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003408 }
3409
3410 BUG_ON(rule_idx != num_rules);
3411
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003412 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003413
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003414 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003415
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003416 return r;
3417
Johannes Bergd2372b32008-10-24 20:32:20 +02003418 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003419 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003420 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003421 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003422}
3423
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003424static int validate_scan_freqs(struct nlattr *freqs)
3425{
3426 struct nlattr *attr1, *attr2;
3427 int n_channels = 0, tmp1, tmp2;
3428
3429 nla_for_each_nested(attr1, freqs, tmp1) {
3430 n_channels++;
3431 /*
3432 * Some hardware has a limited channel list for
3433 * scanning, and it is pretty much nonsensical
3434 * to scan for a channel twice, so disallow that
3435 * and don't require drivers to check that the
3436 * channel list they get isn't longer than what
3437 * they can scan, as long as they can scan all
3438 * the channels they registered at once.
3439 */
3440 nla_for_each_nested(attr2, freqs, tmp2)
3441 if (attr1 != attr2 &&
3442 nla_get_u32(attr1) == nla_get_u32(attr2))
3443 return 0;
3444 }
3445
3446 return n_channels;
3447}
3448
Johannes Berg2a519312009-02-10 21:25:55 +01003449static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3450{
Johannes Berg4c476992010-10-04 21:36:35 +02003451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3452 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003453 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003454 struct nlattr *attr;
3455 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003456 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003457 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003458
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003459 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3460 return -EINVAL;
3461
Johannes Berg79c97e92009-07-07 03:56:12 +02003462 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003463
Johannes Berg4c476992010-10-04 21:36:35 +02003464 if (!rdev->ops->scan)
3465 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003466
Johannes Berg4c476992010-10-04 21:36:35 +02003467 if (rdev->scan_req)
3468 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003469
3470 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003471 n_channels = validate_scan_freqs(
3472 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003473 if (!n_channels)
3474 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003475 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003476 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003477 n_channels = 0;
3478
Johannes Berg2a519312009-02-10 21:25:55 +01003479 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3480 if (wiphy->bands[band])
3481 n_channels += wiphy->bands[band]->n_channels;
3482 }
3483
3484 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3485 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3486 n_ssids++;
3487
Johannes Berg4c476992010-10-04 21:36:35 +02003488 if (n_ssids > wiphy->max_scan_ssids)
3489 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003490
Jouni Malinen70692ad2009-02-16 19:39:13 +02003491 if (info->attrs[NL80211_ATTR_IE])
3492 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3493 else
3494 ie_len = 0;
3495
Johannes Berg4c476992010-10-04 21:36:35 +02003496 if (ie_len > wiphy->max_scan_ie_len)
3497 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003498
Johannes Berg2a519312009-02-10 21:25:55 +01003499 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003500 + sizeof(*request->ssids) * n_ssids
3501 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003502 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003503 if (!request)
3504 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003505
Johannes Berg2a519312009-02-10 21:25:55 +01003506 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003507 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003508 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003509 if (ie_len) {
3510 if (request->ssids)
3511 request->ie = (void *)(request->ssids + n_ssids);
3512 else
3513 request->ie = (void *)(request->channels + n_channels);
3514 }
Johannes Berg2a519312009-02-10 21:25:55 +01003515
Johannes Berg584991d2009-11-02 13:32:03 +01003516 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003517 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3518 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003519 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003520 struct ieee80211_channel *chan;
3521
3522 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3523
3524 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003525 err = -EINVAL;
3526 goto out_free;
3527 }
Johannes Berg584991d2009-11-02 13:32:03 +01003528
3529 /* ignore disabled channels */
3530 if (chan->flags & IEEE80211_CHAN_DISABLED)
3531 continue;
3532
3533 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003534 i++;
3535 }
3536 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003537 enum ieee80211_band band;
3538
Johannes Berg2a519312009-02-10 21:25:55 +01003539 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003540 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3541 int j;
3542 if (!wiphy->bands[band])
3543 continue;
3544 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003545 struct ieee80211_channel *chan;
3546
3547 chan = &wiphy->bands[band]->channels[j];
3548
3549 if (chan->flags & IEEE80211_CHAN_DISABLED)
3550 continue;
3551
3552 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003553 i++;
3554 }
3555 }
3556 }
3557
Johannes Berg584991d2009-11-02 13:32:03 +01003558 if (!i) {
3559 err = -EINVAL;
3560 goto out_free;
3561 }
3562
3563 request->n_channels = i;
3564
Johannes Berg2a519312009-02-10 21:25:55 +01003565 i = 0;
3566 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3567 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003568 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003569 err = -EINVAL;
3570 goto out_free;
3571 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003572 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003573 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003574 i++;
3575 }
3576 }
3577
Jouni Malinen70692ad2009-02-16 19:39:13 +02003578 if (info->attrs[NL80211_ATTR_IE]) {
3579 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003580 memcpy((void *)request->ie,
3581 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003582 request->ie_len);
3583 }
3584
Johannes Berg34850ab2011-07-18 18:08:35 +02003585 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003586 if (wiphy->bands[i])
3587 request->rates[i] =
3588 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003589
3590 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3591 nla_for_each_nested(attr,
3592 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3593 tmp) {
3594 enum ieee80211_band band = nla_type(attr);
3595
Dan Carpenter84404622011-07-29 11:52:18 +03003596 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003597 err = -EINVAL;
3598 goto out_free;
3599 }
3600 err = ieee80211_get_ratemask(wiphy->bands[band],
3601 nla_data(attr),
3602 nla_len(attr),
3603 &request->rates[band]);
3604 if (err)
3605 goto out_free;
3606 }
3607 }
3608
Johannes Berg463d0182009-07-14 00:33:35 +02003609 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003610 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003611
Johannes Berg79c97e92009-07-07 03:56:12 +02003612 rdev->scan_req = request;
3613 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003614
Johannes Berg463d0182009-07-14 00:33:35 +02003615 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003616 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003617 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003618 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003619 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003620 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003621 kfree(request);
3622 }
Johannes Berg3b858752009-03-12 09:55:09 +01003623
Johannes Berg2a519312009-02-10 21:25:55 +01003624 return err;
3625}
3626
Luciano Coelho807f8a82011-05-11 17:09:35 +03003627static int nl80211_start_sched_scan(struct sk_buff *skb,
3628 struct genl_info *info)
3629{
3630 struct cfg80211_sched_scan_request *request;
3631 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3632 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003633 struct nlattr *attr;
3634 struct wiphy *wiphy;
3635 int err, tmp, n_ssids = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003636 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003637 enum ieee80211_band band;
3638 size_t ie_len;
3639
3640 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3641 !rdev->ops->sched_scan_start)
3642 return -EOPNOTSUPP;
3643
3644 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3645 return -EINVAL;
3646
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003647 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3648 return -EINVAL;
3649
3650 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3651 if (interval == 0)
3652 return -EINVAL;
3653
Luciano Coelho807f8a82011-05-11 17:09:35 +03003654 wiphy = &rdev->wiphy;
3655
3656 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3657 n_channels = validate_scan_freqs(
3658 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3659 if (!n_channels)
3660 return -EINVAL;
3661 } else {
3662 n_channels = 0;
3663
3664 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3665 if (wiphy->bands[band])
3666 n_channels += wiphy->bands[band]->n_channels;
3667 }
3668
3669 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3670 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3671 tmp)
3672 n_ssids++;
3673
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003674 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003675 return -EINVAL;
3676
3677 if (info->attrs[NL80211_ATTR_IE])
3678 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3679 else
3680 ie_len = 0;
3681
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003682 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003683 return -EINVAL;
3684
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003685 mutex_lock(&rdev->sched_scan_mtx);
3686
3687 if (rdev->sched_scan_req) {
3688 err = -EINPROGRESS;
3689 goto out;
3690 }
3691
Luciano Coelho807f8a82011-05-11 17:09:35 +03003692 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003693 + sizeof(*request->ssids) * n_ssids
3694 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003695 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003696 if (!request) {
3697 err = -ENOMEM;
3698 goto out;
3699 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003700
3701 if (n_ssids)
3702 request->ssids = (void *)&request->channels[n_channels];
3703 request->n_ssids = n_ssids;
3704 if (ie_len) {
3705 if (request->ssids)
3706 request->ie = (void *)(request->ssids + n_ssids);
3707 else
3708 request->ie = (void *)(request->channels + n_channels);
3709 }
3710
3711 i = 0;
3712 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3713 /* user specified, bail out if channel not found */
3714 nla_for_each_nested(attr,
3715 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3716 tmp) {
3717 struct ieee80211_channel *chan;
3718
3719 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3720
3721 if (!chan) {
3722 err = -EINVAL;
3723 goto out_free;
3724 }
3725
3726 /* ignore disabled channels */
3727 if (chan->flags & IEEE80211_CHAN_DISABLED)
3728 continue;
3729
3730 request->channels[i] = chan;
3731 i++;
3732 }
3733 } else {
3734 /* all channels */
3735 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3736 int j;
3737 if (!wiphy->bands[band])
3738 continue;
3739 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3740 struct ieee80211_channel *chan;
3741
3742 chan = &wiphy->bands[band]->channels[j];
3743
3744 if (chan->flags & IEEE80211_CHAN_DISABLED)
3745 continue;
3746
3747 request->channels[i] = chan;
3748 i++;
3749 }
3750 }
3751 }
3752
3753 if (!i) {
3754 err = -EINVAL;
3755 goto out_free;
3756 }
3757
3758 request->n_channels = i;
3759
3760 i = 0;
3761 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3762 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3763 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003764 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003765 err = -EINVAL;
3766 goto out_free;
3767 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003768 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003769 memcpy(request->ssids[i].ssid, nla_data(attr),
3770 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003771 i++;
3772 }
3773 }
3774
3775 if (info->attrs[NL80211_ATTR_IE]) {
3776 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3777 memcpy((void *)request->ie,
3778 nla_data(info->attrs[NL80211_ATTR_IE]),
3779 request->ie_len);
3780 }
3781
3782 request->dev = dev;
3783 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003784 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003785
3786 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3787 if (!err) {
3788 rdev->sched_scan_req = request;
3789 nl80211_send_sched_scan(rdev, dev,
3790 NL80211_CMD_START_SCHED_SCAN);
3791 goto out;
3792 }
3793
3794out_free:
3795 kfree(request);
3796out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003797 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003798 return err;
3799}
3800
3801static int nl80211_stop_sched_scan(struct sk_buff *skb,
3802 struct genl_info *info)
3803{
3804 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003805 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003806
3807 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3808 !rdev->ops->sched_scan_stop)
3809 return -EOPNOTSUPP;
3810
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003811 mutex_lock(&rdev->sched_scan_mtx);
3812 err = __cfg80211_stop_sched_scan(rdev, false);
3813 mutex_unlock(&rdev->sched_scan_mtx);
3814
3815 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003816}
3817
Johannes Berg9720bb32011-06-21 09:45:33 +02003818static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3819 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003820 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003821 struct wireless_dev *wdev,
3822 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003823{
Johannes Berg48ab9052009-07-10 18:42:31 +02003824 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003825 void *hdr;
3826 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003827 int i;
3828
3829 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003830
Johannes Berg9720bb32011-06-21 09:45:33 +02003831 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003832 NL80211_CMD_NEW_SCAN_RESULTS);
3833 if (!hdr)
3834 return -1;
3835
Johannes Berg9720bb32011-06-21 09:45:33 +02003836 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3837
Johannes Bergf5ea9122009-08-07 16:17:38 +02003838 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003839 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003840
3841 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3842 if (!bss)
3843 goto nla_put_failure;
3844 if (!is_zero_ether_addr(res->bssid))
3845 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3846 if (res->information_elements && res->len_information_elements)
3847 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3848 res->len_information_elements,
3849 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003850 if (res->beacon_ies && res->len_beacon_ies &&
3851 res->beacon_ies != res->information_elements)
3852 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3853 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003854 if (res->tsf)
3855 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3856 if (res->beacon_interval)
3857 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3858 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3859 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003860 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3861 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003862
Johannes Berg77965c92009-02-18 18:45:06 +01003863 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003864 case CFG80211_SIGNAL_TYPE_MBM:
3865 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3866 break;
3867 case CFG80211_SIGNAL_TYPE_UNSPEC:
3868 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3869 break;
3870 default:
3871 break;
3872 }
3873
Johannes Berg48ab9052009-07-10 18:42:31 +02003874 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003875 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003876 case NL80211_IFTYPE_STATION:
3877 if (intbss == wdev->current_bss)
3878 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3879 NL80211_BSS_STATUS_ASSOCIATED);
3880 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3881 if (intbss != wdev->auth_bsses[i])
3882 continue;
3883 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3884 NL80211_BSS_STATUS_AUTHENTICATED);
3885 break;
3886 }
3887 break;
3888 case NL80211_IFTYPE_ADHOC:
3889 if (intbss == wdev->current_bss)
3890 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3891 NL80211_BSS_STATUS_IBSS_JOINED);
3892 break;
3893 default:
3894 break;
3895 }
3896
Johannes Berg2a519312009-02-10 21:25:55 +01003897 nla_nest_end(msg, bss);
3898
3899 return genlmsg_end(msg, hdr);
3900
3901 nla_put_failure:
3902 genlmsg_cancel(msg, hdr);
3903 return -EMSGSIZE;
3904}
3905
3906static int nl80211_dump_scan(struct sk_buff *skb,
3907 struct netlink_callback *cb)
3908{
Johannes Berg48ab9052009-07-10 18:42:31 +02003909 struct cfg80211_registered_device *rdev;
3910 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003911 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003912 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003913 int start = cb->args[1], idx = 0;
3914 int err;
3915
Johannes Berg67748892010-10-04 21:14:06 +02003916 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3917 if (err)
3918 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003919
Johannes Berg48ab9052009-07-10 18:42:31 +02003920 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003921
Johannes Berg48ab9052009-07-10 18:42:31 +02003922 wdev_lock(wdev);
3923 spin_lock_bh(&rdev->bss_lock);
3924 cfg80211_bss_expire(rdev);
3925
Johannes Berg9720bb32011-06-21 09:45:33 +02003926 cb->seq = rdev->bss_generation;
3927
Johannes Berg48ab9052009-07-10 18:42:31 +02003928 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003929 if (++idx <= start)
3930 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02003931 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01003932 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003933 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003934 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003935 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003936 }
3937 }
3938
Johannes Berg48ab9052009-07-10 18:42:31 +02003939 spin_unlock_bh(&rdev->bss_lock);
3940 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003941
3942 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003943 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003944
Johannes Berg67748892010-10-04 21:14:06 +02003945 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003946}
3947
Holger Schurig61fa7132009-11-11 12:25:40 +01003948static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3949 int flags, struct net_device *dev,
3950 struct survey_info *survey)
3951{
3952 void *hdr;
3953 struct nlattr *infoattr;
3954
Holger Schurig61fa7132009-11-11 12:25:40 +01003955 hdr = nl80211hdr_put(msg, pid, seq, flags,
3956 NL80211_CMD_NEW_SURVEY_RESULTS);
3957 if (!hdr)
3958 return -ENOMEM;
3959
3960 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3961
3962 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3963 if (!infoattr)
3964 goto nla_put_failure;
3965
3966 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3967 survey->channel->center_freq);
3968 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3969 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3970 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003971 if (survey->filled & SURVEY_INFO_IN_USE)
3972 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003973 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3974 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3975 survey->channel_time);
3976 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3977 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3978 survey->channel_time_busy);
3979 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3980 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3981 survey->channel_time_ext_busy);
3982 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3983 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3984 survey->channel_time_rx);
3985 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3986 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3987 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003988
3989 nla_nest_end(msg, infoattr);
3990
3991 return genlmsg_end(msg, hdr);
3992
3993 nla_put_failure:
3994 genlmsg_cancel(msg, hdr);
3995 return -EMSGSIZE;
3996}
3997
3998static int nl80211_dump_survey(struct sk_buff *skb,
3999 struct netlink_callback *cb)
4000{
4001 struct survey_info survey;
4002 struct cfg80211_registered_device *dev;
4003 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004004 int survey_idx = cb->args[1];
4005 int res;
4006
Johannes Berg67748892010-10-04 21:14:06 +02004007 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4008 if (res)
4009 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004010
4011 if (!dev->ops->dump_survey) {
4012 res = -EOPNOTSUPP;
4013 goto out_err;
4014 }
4015
4016 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004017 struct ieee80211_channel *chan;
4018
Holger Schurig61fa7132009-11-11 12:25:40 +01004019 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4020 &survey);
4021 if (res == -ENOENT)
4022 break;
4023 if (res)
4024 goto out_err;
4025
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004026 /* Survey without a channel doesn't make sense */
4027 if (!survey.channel) {
4028 res = -EINVAL;
4029 goto out;
4030 }
4031
4032 chan = ieee80211_get_channel(&dev->wiphy,
4033 survey.channel->center_freq);
4034 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4035 survey_idx++;
4036 continue;
4037 }
4038
Holger Schurig61fa7132009-11-11 12:25:40 +01004039 if (nl80211_send_survey(skb,
4040 NETLINK_CB(cb->skb).pid,
4041 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4042 netdev,
4043 &survey) < 0)
4044 goto out;
4045 survey_idx++;
4046 }
4047
4048 out:
4049 cb->args[1] = survey_idx;
4050 res = skb->len;
4051 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004052 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004053 return res;
4054}
4055
Jouni Malinen255e7372009-03-20 21:21:17 +02004056static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4057{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004058 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004059}
4060
Samuel Ortizb23aa672009-07-01 21:26:54 +02004061static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4062{
4063 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4064 NL80211_WPA_VERSION_2));
4065}
4066
4067static bool nl80211_valid_akm_suite(u32 akm)
4068{
4069 return akm == WLAN_AKM_SUITE_8021X ||
4070 akm == WLAN_AKM_SUITE_PSK;
4071}
4072
4073static bool nl80211_valid_cipher_suite(u32 cipher)
4074{
4075 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
4076 cipher == WLAN_CIPHER_SUITE_WEP104 ||
4077 cipher == WLAN_CIPHER_SUITE_TKIP ||
4078 cipher == WLAN_CIPHER_SUITE_CCMP ||
4079 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
4080}
4081
4082
Jouni Malinen636a5d32009-03-19 13:39:22 +02004083static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4084{
Johannes Berg4c476992010-10-04 21:36:35 +02004085 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4086 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004087 struct ieee80211_channel *chan;
4088 const u8 *bssid, *ssid, *ie = NULL;
4089 int err, ssid_len, ie_len = 0;
4090 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004091 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004092 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004093
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004094 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4095 return -EINVAL;
4096
4097 if (!info->attrs[NL80211_ATTR_MAC])
4098 return -EINVAL;
4099
Jouni Malinen17780922009-03-27 20:52:47 +02004100 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4101 return -EINVAL;
4102
Johannes Berg19957bb2009-07-02 17:20:43 +02004103 if (!info->attrs[NL80211_ATTR_SSID])
4104 return -EINVAL;
4105
4106 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4107 return -EINVAL;
4108
Johannes Bergfffd0932009-07-08 14:22:54 +02004109 err = nl80211_parse_key(info, &key);
4110 if (err)
4111 return err;
4112
4113 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004114 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4115 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004116 if (!key.p.key || !key.p.key_len)
4117 return -EINVAL;
4118 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4119 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4120 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4121 key.p.key_len != WLAN_KEY_LEN_WEP104))
4122 return -EINVAL;
4123 if (key.idx > 4)
4124 return -EINVAL;
4125 } else {
4126 key.p.key_len = 0;
4127 key.p.key = NULL;
4128 }
4129
Johannes Bergafea0b72010-08-10 09:46:42 +02004130 if (key.idx >= 0) {
4131 int i;
4132 bool ok = false;
4133 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4134 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4135 ok = true;
4136 break;
4137 }
4138 }
Johannes Berg4c476992010-10-04 21:36:35 +02004139 if (!ok)
4140 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004141 }
4142
Johannes Berg4c476992010-10-04 21:36:35 +02004143 if (!rdev->ops->auth)
4144 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004145
Johannes Berg074ac8d2010-09-16 14:58:22 +02004146 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004147 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4148 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004149
Johannes Berg19957bb2009-07-02 17:20:43 +02004150 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004151 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004152 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004153 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4154 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004155
Johannes Berg19957bb2009-07-02 17:20:43 +02004156 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4157 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4158
4159 if (info->attrs[NL80211_ATTR_IE]) {
4160 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4161 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4162 }
4163
4164 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004165 if (!nl80211_valid_auth_type(auth_type))
4166 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004167
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004168 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4169
Johannes Berg4c476992010-10-04 21:36:35 +02004170 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4171 ssid, ssid_len, ie, ie_len,
4172 key.p.key, key.p.key_len, key.idx,
4173 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004174}
4175
Johannes Bergc0692b82010-08-27 14:26:53 +03004176static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4177 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004178 struct cfg80211_crypto_settings *settings,
4179 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004180{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004181 memset(settings, 0, sizeof(*settings));
4182
Samuel Ortizb23aa672009-07-01 21:26:54 +02004183 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4184
Johannes Bergc0692b82010-08-27 14:26:53 +03004185 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4186 u16 proto;
4187 proto = nla_get_u16(
4188 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4189 settings->control_port_ethertype = cpu_to_be16(proto);
4190 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4191 proto != ETH_P_PAE)
4192 return -EINVAL;
4193 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4194 settings->control_port_no_encrypt = true;
4195 } else
4196 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4197
Samuel Ortizb23aa672009-07-01 21:26:54 +02004198 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4199 void *data;
4200 int len, i;
4201
4202 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4203 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4204 settings->n_ciphers_pairwise = len / sizeof(u32);
4205
4206 if (len % sizeof(u32))
4207 return -EINVAL;
4208
Johannes Berg3dc27d22009-07-02 21:36:37 +02004209 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004210 return -EINVAL;
4211
4212 memcpy(settings->ciphers_pairwise, data, len);
4213
4214 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4215 if (!nl80211_valid_cipher_suite(
4216 settings->ciphers_pairwise[i]))
4217 return -EINVAL;
4218 }
4219
4220 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4221 settings->cipher_group =
4222 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4223 if (!nl80211_valid_cipher_suite(settings->cipher_group))
4224 return -EINVAL;
4225 }
4226
4227 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4228 settings->wpa_versions =
4229 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4230 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4231 return -EINVAL;
4232 }
4233
4234 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4235 void *data;
4236 int len, i;
4237
4238 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4239 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4240 settings->n_akm_suites = len / sizeof(u32);
4241
4242 if (len % sizeof(u32))
4243 return -EINVAL;
4244
4245 memcpy(settings->akm_suites, data, len);
4246
4247 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4248 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
4249 return -EINVAL;
4250 }
4251
4252 return 0;
4253}
4254
Jouni Malinen636a5d32009-03-19 13:39:22 +02004255static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4256{
Johannes Berg4c476992010-10-04 21:36:35 +02004257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4258 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004259 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004260 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004261 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004262 int err, ssid_len, ie_len = 0;
4263 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004264
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004265 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4266 return -EINVAL;
4267
4268 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004269 !info->attrs[NL80211_ATTR_SSID] ||
4270 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004271 return -EINVAL;
4272
Johannes Berg4c476992010-10-04 21:36:35 +02004273 if (!rdev->ops->assoc)
4274 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004275
Johannes Berg074ac8d2010-09-16 14:58:22 +02004276 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004277 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4278 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004279
Johannes Berg19957bb2009-07-02 17:20:43 +02004280 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004281
Johannes Berg19957bb2009-07-02 17:20:43 +02004282 chan = ieee80211_get_channel(&rdev->wiphy,
4283 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004284 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4285 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004286
Johannes Berg19957bb2009-07-02 17:20:43 +02004287 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4288 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004289
4290 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004291 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4292 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004293 }
4294
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004295 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004296 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004297 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004298 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004299 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004300 else if (mfp != NL80211_MFP_NO)
4301 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004302 }
4303
Johannes Berg3e5d7642009-07-07 14:37:26 +02004304 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4305 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4306
Johannes Bergc0692b82010-08-27 14:26:53 +03004307 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004308 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004309 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4310 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004311 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004312
Jouni Malinen636a5d32009-03-19 13:39:22 +02004313 return err;
4314}
4315
4316static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4317{
Johannes Berg4c476992010-10-04 21:36:35 +02004318 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4319 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004320 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004321 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004322 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004323 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004324
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004325 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4326 return -EINVAL;
4327
4328 if (!info->attrs[NL80211_ATTR_MAC])
4329 return -EINVAL;
4330
4331 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4332 return -EINVAL;
4333
Johannes Berg4c476992010-10-04 21:36:35 +02004334 if (!rdev->ops->deauth)
4335 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004336
Johannes Berg074ac8d2010-09-16 14:58:22 +02004337 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004338 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4339 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004340
Johannes Berg19957bb2009-07-02 17:20:43 +02004341 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004342
Johannes Berg19957bb2009-07-02 17:20:43 +02004343 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4344 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004345 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004346 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004347 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004348
4349 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004350 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4351 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004352 }
4353
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004354 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4355
Johannes Berg4c476992010-10-04 21:36:35 +02004356 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4357 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004358}
4359
4360static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4361{
Johannes Berg4c476992010-10-04 21:36:35 +02004362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4363 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004364 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004365 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004366 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004367 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004368
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004369 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4370 return -EINVAL;
4371
4372 if (!info->attrs[NL80211_ATTR_MAC])
4373 return -EINVAL;
4374
4375 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4376 return -EINVAL;
4377
Johannes Berg4c476992010-10-04 21:36:35 +02004378 if (!rdev->ops->disassoc)
4379 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004380
Johannes Berg074ac8d2010-09-16 14:58:22 +02004381 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004382 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4383 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004384
Johannes Berg19957bb2009-07-02 17:20:43 +02004385 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004386
Johannes Berg19957bb2009-07-02 17:20:43 +02004387 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4388 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004389 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004390 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004391 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004392
4393 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004394 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4395 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004396 }
4397
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004398 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4399
Johannes Berg4c476992010-10-04 21:36:35 +02004400 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4401 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004402}
4403
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004404static bool
4405nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4406 int mcast_rate[IEEE80211_NUM_BANDS],
4407 int rateval)
4408{
4409 struct wiphy *wiphy = &rdev->wiphy;
4410 bool found = false;
4411 int band, i;
4412
4413 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4414 struct ieee80211_supported_band *sband;
4415
4416 sband = wiphy->bands[band];
4417 if (!sband)
4418 continue;
4419
4420 for (i = 0; i < sband->n_bitrates; i++) {
4421 if (sband->bitrates[i].bitrate == rateval) {
4422 mcast_rate[band] = i + 1;
4423 found = true;
4424 break;
4425 }
4426 }
4427 }
4428
4429 return found;
4430}
4431
Johannes Berg04a773a2009-04-19 21:24:32 +02004432static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4433{
Johannes Berg4c476992010-10-04 21:36:35 +02004434 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4435 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004436 struct cfg80211_ibss_params ibss;
4437 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004438 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004439 int err;
4440
Johannes Berg8e30bc52009-04-22 17:45:38 +02004441 memset(&ibss, 0, sizeof(ibss));
4442
Johannes Berg04a773a2009-04-19 21:24:32 +02004443 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4444 return -EINVAL;
4445
4446 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4447 !info->attrs[NL80211_ATTR_SSID] ||
4448 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4449 return -EINVAL;
4450
Johannes Berg8e30bc52009-04-22 17:45:38 +02004451 ibss.beacon_interval = 100;
4452
4453 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4454 ibss.beacon_interval =
4455 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4456 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4457 return -EINVAL;
4458 }
4459
Johannes Berg4c476992010-10-04 21:36:35 +02004460 if (!rdev->ops->join_ibss)
4461 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4464 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004465
Johannes Berg79c97e92009-07-07 03:56:12 +02004466 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004467
4468 if (info->attrs[NL80211_ATTR_MAC])
4469 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4470 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4471 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4472
4473 if (info->attrs[NL80211_ATTR_IE]) {
4474 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4475 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4476 }
4477
4478 ibss.channel = ieee80211_get_channel(wiphy,
4479 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4480 if (!ibss.channel ||
4481 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004482 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4483 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004484
4485 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004486 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004487
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004488 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4489 u8 *rates =
4490 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4491 int n_rates =
4492 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4493 struct ieee80211_supported_band *sband =
4494 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004495 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004496
Johannes Berg34850ab2011-07-18 18:08:35 +02004497 err = ieee80211_get_ratemask(sband, rates, n_rates,
4498 &ibss.basic_rates);
4499 if (err)
4500 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004501 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004502
4503 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4504 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4505 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4506 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004507
Johannes Berg4c476992010-10-04 21:36:35 +02004508 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4509 connkeys = nl80211_parse_connkeys(rdev,
4510 info->attrs[NL80211_ATTR_KEYS]);
4511 if (IS_ERR(connkeys))
4512 return PTR_ERR(connkeys);
4513 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004514
Johannes Berg4c476992010-10-04 21:36:35 +02004515 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004516 if (err)
4517 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004518 return err;
4519}
4520
4521static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4522{
Johannes Berg4c476992010-10-04 21:36:35 +02004523 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4524 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004525
Johannes Berg4c476992010-10-04 21:36:35 +02004526 if (!rdev->ops->leave_ibss)
4527 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004528
Johannes Berg4c476992010-10-04 21:36:35 +02004529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4530 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004531
Johannes Berg4c476992010-10-04 21:36:35 +02004532 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004533}
4534
Johannes Bergaff89a92009-07-01 21:26:51 +02004535#ifdef CONFIG_NL80211_TESTMODE
4536static struct genl_multicast_group nl80211_testmode_mcgrp = {
4537 .name = "testmode",
4538};
4539
4540static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4541{
Johannes Berg4c476992010-10-04 21:36:35 +02004542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004543 int err;
4544
4545 if (!info->attrs[NL80211_ATTR_TESTDATA])
4546 return -EINVAL;
4547
Johannes Bergaff89a92009-07-01 21:26:51 +02004548 err = -EOPNOTSUPP;
4549 if (rdev->ops->testmode_cmd) {
4550 rdev->testmode_info = info;
4551 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4552 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4553 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4554 rdev->testmode_info = NULL;
4555 }
4556
Johannes Bergaff89a92009-07-01 21:26:51 +02004557 return err;
4558}
4559
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004560static int nl80211_testmode_dump(struct sk_buff *skb,
4561 struct netlink_callback *cb)
4562{
4563 struct cfg80211_registered_device *dev;
4564 int err;
4565 long phy_idx;
4566 void *data = NULL;
4567 int data_len = 0;
4568
4569 if (cb->args[0]) {
4570 /*
4571 * 0 is a valid index, but not valid for args[0],
4572 * so we need to offset by 1.
4573 */
4574 phy_idx = cb->args[0] - 1;
4575 } else {
4576 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4577 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4578 nl80211_policy);
4579 if (err)
4580 return err;
4581 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4582 return -EINVAL;
4583 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4584 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4585 cb->args[1] =
4586 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4587 }
4588
4589 if (cb->args[1]) {
4590 data = nla_data((void *)cb->args[1]);
4591 data_len = nla_len((void *)cb->args[1]);
4592 }
4593
4594 mutex_lock(&cfg80211_mutex);
4595 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4596 if (!dev) {
4597 mutex_unlock(&cfg80211_mutex);
4598 return -ENOENT;
4599 }
4600 cfg80211_lock_rdev(dev);
4601 mutex_unlock(&cfg80211_mutex);
4602
4603 if (!dev->ops->testmode_dump) {
4604 err = -EOPNOTSUPP;
4605 goto out_err;
4606 }
4607
4608 while (1) {
4609 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4610 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4611 NL80211_CMD_TESTMODE);
4612 struct nlattr *tmdata;
4613
4614 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4615 genlmsg_cancel(skb, hdr);
4616 break;
4617 }
4618
4619 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4620 if (!tmdata) {
4621 genlmsg_cancel(skb, hdr);
4622 break;
4623 }
4624 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4625 data, data_len);
4626 nla_nest_end(skb, tmdata);
4627
4628 if (err == -ENOBUFS || err == -ENOENT) {
4629 genlmsg_cancel(skb, hdr);
4630 break;
4631 } else if (err) {
4632 genlmsg_cancel(skb, hdr);
4633 goto out_err;
4634 }
4635
4636 genlmsg_end(skb, hdr);
4637 }
4638
4639 err = skb->len;
4640 /* see above */
4641 cb->args[0] = phy_idx + 1;
4642 out_err:
4643 cfg80211_unlock_rdev(dev);
4644 return err;
4645}
4646
Johannes Bergaff89a92009-07-01 21:26:51 +02004647static struct sk_buff *
4648__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4649 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4650{
4651 struct sk_buff *skb;
4652 void *hdr;
4653 struct nlattr *data;
4654
4655 skb = nlmsg_new(approxlen + 100, gfp);
4656 if (!skb)
4657 return NULL;
4658
4659 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4660 if (!hdr) {
4661 kfree_skb(skb);
4662 return NULL;
4663 }
4664
4665 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4666 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4667
4668 ((void **)skb->cb)[0] = rdev;
4669 ((void **)skb->cb)[1] = hdr;
4670 ((void **)skb->cb)[2] = data;
4671
4672 return skb;
4673
4674 nla_put_failure:
4675 kfree_skb(skb);
4676 return NULL;
4677}
4678
4679struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4680 int approxlen)
4681{
4682 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4683
4684 if (WARN_ON(!rdev->testmode_info))
4685 return NULL;
4686
4687 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4688 rdev->testmode_info->snd_pid,
4689 rdev->testmode_info->snd_seq,
4690 GFP_KERNEL);
4691}
4692EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4693
4694int cfg80211_testmode_reply(struct sk_buff *skb)
4695{
4696 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4697 void *hdr = ((void **)skb->cb)[1];
4698 struct nlattr *data = ((void **)skb->cb)[2];
4699
4700 if (WARN_ON(!rdev->testmode_info)) {
4701 kfree_skb(skb);
4702 return -EINVAL;
4703 }
4704
4705 nla_nest_end(skb, data);
4706 genlmsg_end(skb, hdr);
4707 return genlmsg_reply(skb, rdev->testmode_info);
4708}
4709EXPORT_SYMBOL(cfg80211_testmode_reply);
4710
4711struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4712 int approxlen, gfp_t gfp)
4713{
4714 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4715
4716 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4717}
4718EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4719
4720void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4721{
4722 void *hdr = ((void **)skb->cb)[1];
4723 struct nlattr *data = ((void **)skb->cb)[2];
4724
4725 nla_nest_end(skb, data);
4726 genlmsg_end(skb, hdr);
4727 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4728}
4729EXPORT_SYMBOL(cfg80211_testmode_event);
4730#endif
4731
Samuel Ortizb23aa672009-07-01 21:26:54 +02004732static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4733{
Johannes Berg4c476992010-10-04 21:36:35 +02004734 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4735 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004736 struct cfg80211_connect_params connect;
4737 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004738 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004739 int err;
4740
4741 memset(&connect, 0, sizeof(connect));
4742
4743 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4744 return -EINVAL;
4745
4746 if (!info->attrs[NL80211_ATTR_SSID] ||
4747 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4748 return -EINVAL;
4749
4750 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4751 connect.auth_type =
4752 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4753 if (!nl80211_valid_auth_type(connect.auth_type))
4754 return -EINVAL;
4755 } else
4756 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4757
4758 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4759
Johannes Bergc0692b82010-08-27 14:26:53 +03004760 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004761 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004762 if (err)
4763 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004764
Johannes Berg074ac8d2010-09-16 14:58:22 +02004765 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004766 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4767 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004768
Johannes Berg79c97e92009-07-07 03:56:12 +02004769 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004770
Samuel Ortizb23aa672009-07-01 21:26:54 +02004771 if (info->attrs[NL80211_ATTR_MAC])
4772 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4773 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4774 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4775
4776 if (info->attrs[NL80211_ATTR_IE]) {
4777 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4778 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4779 }
4780
4781 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4782 connect.channel =
4783 ieee80211_get_channel(wiphy,
4784 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4785 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004786 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4787 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004788 }
4789
Johannes Bergfffd0932009-07-08 14:22:54 +02004790 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4791 connkeys = nl80211_parse_connkeys(rdev,
4792 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004793 if (IS_ERR(connkeys))
4794 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004795 }
4796
4797 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004798 if (err)
4799 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004800 return err;
4801}
4802
4803static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4804{
Johannes Berg4c476992010-10-04 21:36:35 +02004805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4806 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004807 u16 reason;
4808
4809 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4810 reason = WLAN_REASON_DEAUTH_LEAVING;
4811 else
4812 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4813
4814 if (reason == 0)
4815 return -EINVAL;
4816
Johannes Berg074ac8d2010-09-16 14:58:22 +02004817 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004818 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4819 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004820
Johannes Berg4c476992010-10-04 21:36:35 +02004821 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004822}
4823
Johannes Berg463d0182009-07-14 00:33:35 +02004824static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4825{
Johannes Berg4c476992010-10-04 21:36:35 +02004826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004827 struct net *net;
4828 int err;
4829 u32 pid;
4830
4831 if (!info->attrs[NL80211_ATTR_PID])
4832 return -EINVAL;
4833
4834 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4835
Johannes Berg463d0182009-07-14 00:33:35 +02004836 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004837 if (IS_ERR(net))
4838 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004839
4840 err = 0;
4841
4842 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004843 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4844 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004845
Johannes Berg463d0182009-07-14 00:33:35 +02004846 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004847 return err;
4848}
4849
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004850static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4851{
Johannes Berg4c476992010-10-04 21:36:35 +02004852 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004853 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4854 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004855 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004856 struct cfg80211_pmksa pmksa;
4857
4858 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4859
4860 if (!info->attrs[NL80211_ATTR_MAC])
4861 return -EINVAL;
4862
4863 if (!info->attrs[NL80211_ATTR_PMKID])
4864 return -EINVAL;
4865
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004866 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4867 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4868
Johannes Berg074ac8d2010-09-16 14:58:22 +02004869 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004870 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4871 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004872
4873 switch (info->genlhdr->cmd) {
4874 case NL80211_CMD_SET_PMKSA:
4875 rdev_ops = rdev->ops->set_pmksa;
4876 break;
4877 case NL80211_CMD_DEL_PMKSA:
4878 rdev_ops = rdev->ops->del_pmksa;
4879 break;
4880 default:
4881 WARN_ON(1);
4882 break;
4883 }
4884
Johannes Berg4c476992010-10-04 21:36:35 +02004885 if (!rdev_ops)
4886 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004887
Johannes Berg4c476992010-10-04 21:36:35 +02004888 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004889}
4890
4891static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4892{
Johannes Berg4c476992010-10-04 21:36:35 +02004893 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4894 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004895
Johannes Berg074ac8d2010-09-16 14:58:22 +02004896 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004897 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4898 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004899
Johannes Berg4c476992010-10-04 21:36:35 +02004900 if (!rdev->ops->flush_pmksa)
4901 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004902
Johannes Berg4c476992010-10-04 21:36:35 +02004903 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004904}
4905
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004906static int nl80211_remain_on_channel(struct sk_buff *skb,
4907 struct genl_info *info)
4908{
Johannes Berg4c476992010-10-04 21:36:35 +02004909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4910 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004911 struct ieee80211_channel *chan;
4912 struct sk_buff *msg;
4913 void *hdr;
4914 u64 cookie;
4915 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4916 u32 freq, duration;
4917 int err;
4918
4919 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4920 !info->attrs[NL80211_ATTR_DURATION])
4921 return -EINVAL;
4922
4923 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4924
4925 /*
4926 * We should be on that channel for at least one jiffie,
4927 * and more than 5 seconds seems excessive.
4928 */
Johannes Berga2939112010-12-14 17:54:28 +01004929 if (!duration || !msecs_to_jiffies(duration) ||
4930 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004931 return -EINVAL;
4932
Johannes Berg4c476992010-10-04 21:36:35 +02004933 if (!rdev->ops->remain_on_channel)
4934 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004935
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004936 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4937 channel_type = nla_get_u32(
4938 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4939 if (channel_type != NL80211_CHAN_NO_HT &&
4940 channel_type != NL80211_CHAN_HT20 &&
4941 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004942 channel_type != NL80211_CHAN_HT40MINUS)
4943 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004944 }
4945
4946 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4947 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004948 if (chan == NULL)
4949 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004950
4951 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004952 if (!msg)
4953 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004954
4955 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4956 NL80211_CMD_REMAIN_ON_CHANNEL);
4957
4958 if (IS_ERR(hdr)) {
4959 err = PTR_ERR(hdr);
4960 goto free_msg;
4961 }
4962
4963 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4964 channel_type, duration, &cookie);
4965
4966 if (err)
4967 goto free_msg;
4968
4969 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4970
4971 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004972
4973 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004974
4975 nla_put_failure:
4976 err = -ENOBUFS;
4977 free_msg:
4978 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004979 return err;
4980}
4981
4982static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4983 struct genl_info *info)
4984{
Johannes Berg4c476992010-10-04 21:36:35 +02004985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4986 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004987 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004988
4989 if (!info->attrs[NL80211_ATTR_COOKIE])
4990 return -EINVAL;
4991
Johannes Berg4c476992010-10-04 21:36:35 +02004992 if (!rdev->ops->cancel_remain_on_channel)
4993 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004994
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004995 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4996
Johannes Berg4c476992010-10-04 21:36:35 +02004997 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004998}
4999
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005000static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5001 u8 *rates, u8 rates_len)
5002{
5003 u8 i;
5004 u32 mask = 0;
5005
5006 for (i = 0; i < rates_len; i++) {
5007 int rate = (rates[i] & 0x7f) * 5;
5008 int ridx;
5009 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5010 struct ieee80211_rate *srate =
5011 &sband->bitrates[ridx];
5012 if (rate == srate->bitrate) {
5013 mask |= 1 << ridx;
5014 break;
5015 }
5016 }
5017 if (ridx == sband->n_bitrates)
5018 return 0; /* rate not found */
5019 }
5020
5021 return mask;
5022}
5023
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005024static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005025 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5026 .len = NL80211_MAX_SUPP_RATES },
5027};
5028
5029static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5030 struct genl_info *info)
5031{
5032 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005034 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005035 int rem, i;
5036 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005037 struct nlattr *tx_rates;
5038 struct ieee80211_supported_band *sband;
5039
5040 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5041 return -EINVAL;
5042
Johannes Berg4c476992010-10-04 21:36:35 +02005043 if (!rdev->ops->set_bitrate_mask)
5044 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005045
5046 memset(&mask, 0, sizeof(mask));
5047 /* Default to all rates enabled */
5048 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5049 sband = rdev->wiphy.bands[i];
5050 mask.control[i].legacy =
5051 sband ? (1 << sband->n_bitrates) - 1 : 0;
5052 }
5053
5054 /*
5055 * The nested attribute uses enum nl80211_band as the index. This maps
5056 * directly to the enum ieee80211_band values used in cfg80211.
5057 */
5058 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5059 {
5060 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005061 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5062 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005063 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005064 if (sband == NULL)
5065 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005066 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5067 nla_len(tx_rates), nl80211_txattr_policy);
5068 if (tb[NL80211_TXRATE_LEGACY]) {
5069 mask.control[band].legacy = rateset_to_mask(
5070 sband,
5071 nla_data(tb[NL80211_TXRATE_LEGACY]),
5072 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005073 if (mask.control[band].legacy == 0)
5074 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005075 }
5076 }
5077
Johannes Berg4c476992010-10-04 21:36:35 +02005078 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005079}
5080
Johannes Berg2e161f72010-08-12 15:38:38 +02005081static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005082{
Johannes Berg4c476992010-10-04 21:36:35 +02005083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5084 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005085 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005086
5087 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5088 return -EINVAL;
5089
Johannes Berg2e161f72010-08-12 15:38:38 +02005090 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5091 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005092
Johannes Berg9d38d852010-06-09 17:20:33 +02005093 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005094 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005095 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5096 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5097 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005098 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005099 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5100 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005101
5102 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005103 if (!rdev->ops->mgmt_tx)
5104 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005105
Johannes Berg4c476992010-10-04 21:36:35 +02005106 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005107 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005108 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5109 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005110}
5111
Johannes Berg2e161f72010-08-12 15:38:38 +02005112static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005113{
Johannes Berg4c476992010-10-04 21:36:35 +02005114 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5115 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005116 struct ieee80211_channel *chan;
5117 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005118 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005119 u32 freq;
5120 int err;
5121 void *hdr;
5122 u64 cookie;
5123 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005124 unsigned int wait = 0;
5125 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02005126
5127 if (!info->attrs[NL80211_ATTR_FRAME] ||
5128 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5129 return -EINVAL;
5130
Johannes Berg4c476992010-10-04 21:36:35 +02005131 if (!rdev->ops->mgmt_tx)
5132 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005133
Johannes Berg9d38d852010-06-09 17:20:33 +02005134 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005135 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005136 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5137 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5138 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005139 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005140 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5141 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005142
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005143 if (info->attrs[NL80211_ATTR_DURATION]) {
5144 if (!rdev->ops->mgmt_tx_cancel_wait)
5145 return -EINVAL;
5146 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5147 }
5148
Jouni Malinen026331c2010-02-15 12:53:10 +02005149 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5150 channel_type = nla_get_u32(
5151 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5152 if (channel_type != NL80211_CHAN_NO_HT &&
5153 channel_type != NL80211_CHAN_HT20 &&
5154 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005155 channel_type != NL80211_CHAN_HT40MINUS)
5156 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005157 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005158 }
5159
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005160 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5161
Jouni Malinen026331c2010-02-15 12:53:10 +02005162 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5163 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005164 if (chan == NULL)
5165 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005166
5167 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005168 if (!msg)
5169 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005170
5171 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005172 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005173
5174 if (IS_ERR(hdr)) {
5175 err = PTR_ERR(hdr);
5176 goto free_msg;
5177 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005178 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5179 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005180 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5181 nla_len(info->attrs[NL80211_ATTR_FRAME]),
5182 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005183 if (err)
5184 goto free_msg;
5185
5186 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5187
5188 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005189 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005190
5191 nla_put_failure:
5192 err = -ENOBUFS;
5193 free_msg:
5194 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005195 return err;
5196}
5197
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005198static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5199{
5200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5201 struct net_device *dev = info->user_ptr[1];
5202 u64 cookie;
5203
5204 if (!info->attrs[NL80211_ATTR_COOKIE])
5205 return -EINVAL;
5206
5207 if (!rdev->ops->mgmt_tx_cancel_wait)
5208 return -EOPNOTSUPP;
5209
5210 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5211 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5213 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5214 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5215 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5216 return -EOPNOTSUPP;
5217
5218 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5219
5220 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5221}
5222
Kalle Valoffb9eb32010-02-17 17:58:10 +02005223static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5224{
Johannes Berg4c476992010-10-04 21:36:35 +02005225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005226 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005227 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005228 u8 ps_state;
5229 bool state;
5230 int err;
5231
Johannes Berg4c476992010-10-04 21:36:35 +02005232 if (!info->attrs[NL80211_ATTR_PS_STATE])
5233 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005234
5235 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5236
Johannes Berg4c476992010-10-04 21:36:35 +02005237 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5238 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005239
5240 wdev = dev->ieee80211_ptr;
5241
Johannes Berg4c476992010-10-04 21:36:35 +02005242 if (!rdev->ops->set_power_mgmt)
5243 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005244
5245 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5246
5247 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005248 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005249
Johannes Berg4c476992010-10-04 21:36:35 +02005250 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5251 wdev->ps_timeout);
5252 if (!err)
5253 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005254 return err;
5255}
5256
5257static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5258{
Johannes Berg4c476992010-10-04 21:36:35 +02005259 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005260 enum nl80211_ps_state ps_state;
5261 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005262 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005263 struct sk_buff *msg;
5264 void *hdr;
5265 int err;
5266
Kalle Valoffb9eb32010-02-17 17:58:10 +02005267 wdev = dev->ieee80211_ptr;
5268
Johannes Berg4c476992010-10-04 21:36:35 +02005269 if (!rdev->ops->set_power_mgmt)
5270 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005271
5272 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005273 if (!msg)
5274 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005275
5276 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5277 NL80211_CMD_GET_POWER_SAVE);
5278 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005279 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005280 goto free_msg;
5281 }
5282
5283 if (wdev->ps)
5284 ps_state = NL80211_PS_ENABLED;
5285 else
5286 ps_state = NL80211_PS_DISABLED;
5287
5288 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5289
5290 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005291 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005292
Johannes Berg4c476992010-10-04 21:36:35 +02005293 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005294 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005295 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005296 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005297 return err;
5298}
5299
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005300static struct nla_policy
5301nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5302 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5303 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5304 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5305};
5306
5307static int nl80211_set_cqm_rssi(struct genl_info *info,
5308 s32 threshold, u32 hysteresis)
5309{
Johannes Berg4c476992010-10-04 21:36:35 +02005310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005311 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005312 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005313
5314 if (threshold > 0)
5315 return -EINVAL;
5316
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005317 wdev = dev->ieee80211_ptr;
5318
Johannes Berg4c476992010-10-04 21:36:35 +02005319 if (!rdev->ops->set_cqm_rssi_config)
5320 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005321
Johannes Berg074ac8d2010-09-16 14:58:22 +02005322 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005323 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5324 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005325
Johannes Berg4c476992010-10-04 21:36:35 +02005326 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5327 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005328}
5329
5330static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5331{
5332 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5333 struct nlattr *cqm;
5334 int err;
5335
5336 cqm = info->attrs[NL80211_ATTR_CQM];
5337 if (!cqm) {
5338 err = -EINVAL;
5339 goto out;
5340 }
5341
5342 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5343 nl80211_attr_cqm_policy);
5344 if (err)
5345 goto out;
5346
5347 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5348 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5349 s32 threshold;
5350 u32 hysteresis;
5351 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5352 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5353 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5354 } else
5355 err = -EINVAL;
5356
5357out:
5358 return err;
5359}
5360
Johannes Berg29cbe682010-12-03 09:20:44 +01005361static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5362{
5363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5364 struct net_device *dev = info->user_ptr[1];
5365 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005366 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005367 int err;
5368
5369 /* start with default */
5370 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005371 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005372
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005373 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005374 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005375 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005376 if (err)
5377 return err;
5378 }
5379
5380 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5381 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5382 return -EINVAL;
5383
Javier Cardonac80d5452010-12-16 17:37:49 -08005384 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5385 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5386
5387 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5388 /* parse additional setup parameters if given */
5389 err = nl80211_parse_mesh_setup(info, &setup);
5390 if (err)
5391 return err;
5392 }
5393
5394 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005395}
5396
5397static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5398{
5399 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5400 struct net_device *dev = info->user_ptr[1];
5401
5402 return cfg80211_leave_mesh(rdev, dev);
5403}
5404
Johannes Bergff1b6e62011-05-04 15:37:28 +02005405static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5406{
5407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5408 struct sk_buff *msg;
5409 void *hdr;
5410
5411 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5412 return -EOPNOTSUPP;
5413
5414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5415 if (!msg)
5416 return -ENOMEM;
5417
5418 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5419 NL80211_CMD_GET_WOWLAN);
5420 if (!hdr)
5421 goto nla_put_failure;
5422
5423 if (rdev->wowlan) {
5424 struct nlattr *nl_wowlan;
5425
5426 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5427 if (!nl_wowlan)
5428 goto nla_put_failure;
5429
5430 if (rdev->wowlan->any)
5431 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5432 if (rdev->wowlan->disconnect)
5433 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5434 if (rdev->wowlan->magic_pkt)
5435 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005436 if (rdev->wowlan->gtk_rekey_failure)
5437 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5438 if (rdev->wowlan->eap_identity_req)
5439 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5440 if (rdev->wowlan->four_way_handshake)
5441 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5442 if (rdev->wowlan->rfkill_release)
5443 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005444 if (rdev->wowlan->n_patterns) {
5445 struct nlattr *nl_pats, *nl_pat;
5446 int i, pat_len;
5447
5448 nl_pats = nla_nest_start(msg,
5449 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5450 if (!nl_pats)
5451 goto nla_put_failure;
5452
5453 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5454 nl_pat = nla_nest_start(msg, i + 1);
5455 if (!nl_pat)
5456 goto nla_put_failure;
5457 pat_len = rdev->wowlan->patterns[i].pattern_len;
5458 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5459 DIV_ROUND_UP(pat_len, 8),
5460 rdev->wowlan->patterns[i].mask);
5461 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5462 pat_len,
5463 rdev->wowlan->patterns[i].pattern);
5464 nla_nest_end(msg, nl_pat);
5465 }
5466 nla_nest_end(msg, nl_pats);
5467 }
5468
5469 nla_nest_end(msg, nl_wowlan);
5470 }
5471
5472 genlmsg_end(msg, hdr);
5473 return genlmsg_reply(msg, info);
5474
5475nla_put_failure:
5476 nlmsg_free(msg);
5477 return -ENOBUFS;
5478}
5479
5480static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5481{
5482 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5483 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5484 struct cfg80211_wowlan no_triggers = {};
5485 struct cfg80211_wowlan new_triggers = {};
5486 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5487 int err, i;
5488
5489 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5490 return -EOPNOTSUPP;
5491
5492 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5493 goto no_triggers;
5494
5495 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5496 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5497 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5498 nl80211_wowlan_policy);
5499 if (err)
5500 return err;
5501
5502 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5503 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5504 return -EINVAL;
5505 new_triggers.any = true;
5506 }
5507
5508 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5509 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5510 return -EINVAL;
5511 new_triggers.disconnect = true;
5512 }
5513
5514 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5515 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5516 return -EINVAL;
5517 new_triggers.magic_pkt = true;
5518 }
5519
Johannes Berg77dbbb12011-07-13 10:48:55 +02005520 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5521 return -EINVAL;
5522
5523 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5524 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5525 return -EINVAL;
5526 new_triggers.gtk_rekey_failure = true;
5527 }
5528
5529 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5530 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5531 return -EINVAL;
5532 new_triggers.eap_identity_req = true;
5533 }
5534
5535 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5536 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5537 return -EINVAL;
5538 new_triggers.four_way_handshake = true;
5539 }
5540
5541 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5542 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5543 return -EINVAL;
5544 new_triggers.rfkill_release = true;
5545 }
5546
Johannes Bergff1b6e62011-05-04 15:37:28 +02005547 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5548 struct nlattr *pat;
5549 int n_patterns = 0;
5550 int rem, pat_len, mask_len;
5551 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5552
5553 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5554 rem)
5555 n_patterns++;
5556 if (n_patterns > wowlan->n_patterns)
5557 return -EINVAL;
5558
5559 new_triggers.patterns = kcalloc(n_patterns,
5560 sizeof(new_triggers.patterns[0]),
5561 GFP_KERNEL);
5562 if (!new_triggers.patterns)
5563 return -ENOMEM;
5564
5565 new_triggers.n_patterns = n_patterns;
5566 i = 0;
5567
5568 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5569 rem) {
5570 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5571 nla_data(pat), nla_len(pat), NULL);
5572 err = -EINVAL;
5573 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5574 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5575 goto error;
5576 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5577 mask_len = DIV_ROUND_UP(pat_len, 8);
5578 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5579 mask_len)
5580 goto error;
5581 if (pat_len > wowlan->pattern_max_len ||
5582 pat_len < wowlan->pattern_min_len)
5583 goto error;
5584
5585 new_triggers.patterns[i].mask =
5586 kmalloc(mask_len + pat_len, GFP_KERNEL);
5587 if (!new_triggers.patterns[i].mask) {
5588 err = -ENOMEM;
5589 goto error;
5590 }
5591 new_triggers.patterns[i].pattern =
5592 new_triggers.patterns[i].mask + mask_len;
5593 memcpy(new_triggers.patterns[i].mask,
5594 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5595 mask_len);
5596 new_triggers.patterns[i].pattern_len = pat_len;
5597 memcpy(new_triggers.patterns[i].pattern,
5598 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5599 pat_len);
5600 i++;
5601 }
5602 }
5603
5604 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5605 struct cfg80211_wowlan *ntrig;
5606 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5607 GFP_KERNEL);
5608 if (!ntrig) {
5609 err = -ENOMEM;
5610 goto error;
5611 }
5612 cfg80211_rdev_free_wowlan(rdev);
5613 rdev->wowlan = ntrig;
5614 } else {
5615 no_triggers:
5616 cfg80211_rdev_free_wowlan(rdev);
5617 rdev->wowlan = NULL;
5618 }
5619
5620 return 0;
5621 error:
5622 for (i = 0; i < new_triggers.n_patterns; i++)
5623 kfree(new_triggers.patterns[i].mask);
5624 kfree(new_triggers.patterns);
5625 return err;
5626}
5627
Johannes Berge5497d72011-07-05 16:35:40 +02005628static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5629{
5630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5631 struct net_device *dev = info->user_ptr[1];
5632 struct wireless_dev *wdev = dev->ieee80211_ptr;
5633 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5634 struct cfg80211_gtk_rekey_data rekey_data;
5635 int err;
5636
5637 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5638 return -EINVAL;
5639
5640 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5641 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5642 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5643 nl80211_rekey_policy);
5644 if (err)
5645 return err;
5646
5647 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5648 return -ERANGE;
5649 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5650 return -ERANGE;
5651 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5652 return -ERANGE;
5653
5654 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5655 NL80211_KEK_LEN);
5656 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5657 NL80211_KCK_LEN);
5658 memcpy(rekey_data.replay_ctr,
5659 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5660 NL80211_REPLAY_CTR_LEN);
5661
5662 wdev_lock(wdev);
5663 if (!wdev->current_bss) {
5664 err = -ENOTCONN;
5665 goto out;
5666 }
5667
5668 if (!rdev->ops->set_rekey_data) {
5669 err = -EOPNOTSUPP;
5670 goto out;
5671 }
5672
5673 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5674 out:
5675 wdev_unlock(wdev);
5676 return err;
5677}
5678
Johannes Berg4c476992010-10-04 21:36:35 +02005679#define NL80211_FLAG_NEED_WIPHY 0x01
5680#define NL80211_FLAG_NEED_NETDEV 0x02
5681#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005682#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5683#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5684 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005685
5686static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5687 struct genl_info *info)
5688{
5689 struct cfg80211_registered_device *rdev;
5690 struct net_device *dev;
5691 int err;
5692 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5693
5694 if (rtnl)
5695 rtnl_lock();
5696
5697 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5698 rdev = cfg80211_get_dev_from_info(info);
5699 if (IS_ERR(rdev)) {
5700 if (rtnl)
5701 rtnl_unlock();
5702 return PTR_ERR(rdev);
5703 }
5704 info->user_ptr[0] = rdev;
5705 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5706 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5707 if (err) {
5708 if (rtnl)
5709 rtnl_unlock();
5710 return err;
5711 }
Johannes Berg41265712010-10-04 21:14:05 +02005712 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5713 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005714 cfg80211_unlock_rdev(rdev);
5715 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005716 if (rtnl)
5717 rtnl_unlock();
5718 return -ENETDOWN;
5719 }
Johannes Berg4c476992010-10-04 21:36:35 +02005720 info->user_ptr[0] = rdev;
5721 info->user_ptr[1] = dev;
5722 }
5723
5724 return 0;
5725}
5726
5727static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5728 struct genl_info *info)
5729{
5730 if (info->user_ptr[0])
5731 cfg80211_unlock_rdev(info->user_ptr[0]);
5732 if (info->user_ptr[1])
5733 dev_put(info->user_ptr[1]);
5734 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5735 rtnl_unlock();
5736}
5737
Johannes Berg55682962007-09-20 13:09:35 -04005738static struct genl_ops nl80211_ops[] = {
5739 {
5740 .cmd = NL80211_CMD_GET_WIPHY,
5741 .doit = nl80211_get_wiphy,
5742 .dumpit = nl80211_dump_wiphy,
5743 .policy = nl80211_policy,
5744 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005745 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005746 },
5747 {
5748 .cmd = NL80211_CMD_SET_WIPHY,
5749 .doit = nl80211_set_wiphy,
5750 .policy = nl80211_policy,
5751 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005752 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005753 },
5754 {
5755 .cmd = NL80211_CMD_GET_INTERFACE,
5756 .doit = nl80211_get_interface,
5757 .dumpit = nl80211_dump_interface,
5758 .policy = nl80211_policy,
5759 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005760 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005761 },
5762 {
5763 .cmd = NL80211_CMD_SET_INTERFACE,
5764 .doit = nl80211_set_interface,
5765 .policy = nl80211_policy,
5766 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005767 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5768 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005769 },
5770 {
5771 .cmd = NL80211_CMD_NEW_INTERFACE,
5772 .doit = nl80211_new_interface,
5773 .policy = nl80211_policy,
5774 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005775 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5776 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005777 },
5778 {
5779 .cmd = NL80211_CMD_DEL_INTERFACE,
5780 .doit = nl80211_del_interface,
5781 .policy = nl80211_policy,
5782 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005783 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5784 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005785 },
Johannes Berg41ade002007-12-19 02:03:29 +01005786 {
5787 .cmd = NL80211_CMD_GET_KEY,
5788 .doit = nl80211_get_key,
5789 .policy = nl80211_policy,
5790 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005791 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5792 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005793 },
5794 {
5795 .cmd = NL80211_CMD_SET_KEY,
5796 .doit = nl80211_set_key,
5797 .policy = nl80211_policy,
5798 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005799 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005800 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005801 },
5802 {
5803 .cmd = NL80211_CMD_NEW_KEY,
5804 .doit = nl80211_new_key,
5805 .policy = nl80211_policy,
5806 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005807 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005808 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005809 },
5810 {
5811 .cmd = NL80211_CMD_DEL_KEY,
5812 .doit = nl80211_del_key,
5813 .policy = nl80211_policy,
5814 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005815 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005816 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005817 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005818 {
5819 .cmd = NL80211_CMD_SET_BEACON,
5820 .policy = nl80211_policy,
5821 .flags = GENL_ADMIN_PERM,
5822 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005823 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5824 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005825 },
5826 {
5827 .cmd = NL80211_CMD_NEW_BEACON,
5828 .policy = nl80211_policy,
5829 .flags = GENL_ADMIN_PERM,
5830 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005831 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5832 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005833 },
5834 {
5835 .cmd = NL80211_CMD_DEL_BEACON,
5836 .policy = nl80211_policy,
5837 .flags = GENL_ADMIN_PERM,
5838 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005839 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5840 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005841 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005842 {
5843 .cmd = NL80211_CMD_GET_STATION,
5844 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005845 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005846 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005847 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5848 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005849 },
5850 {
5851 .cmd = NL80211_CMD_SET_STATION,
5852 .doit = nl80211_set_station,
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,
Johannes Berg5727ef12007-12-19 02:03:34 +01005857 },
5858 {
5859 .cmd = NL80211_CMD_NEW_STATION,
5860 .doit = nl80211_new_station,
5861 .policy = nl80211_policy,
5862 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005863 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005864 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005865 },
5866 {
5867 .cmd = NL80211_CMD_DEL_STATION,
5868 .doit = nl80211_del_station,
5869 .policy = nl80211_policy,
5870 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005871 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5872 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005873 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005874 {
5875 .cmd = NL80211_CMD_GET_MPATH,
5876 .doit = nl80211_get_mpath,
5877 .dumpit = nl80211_dump_mpath,
5878 .policy = nl80211_policy,
5879 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005880 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005881 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005882 },
5883 {
5884 .cmd = NL80211_CMD_SET_MPATH,
5885 .doit = nl80211_set_mpath,
5886 .policy = nl80211_policy,
5887 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005888 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005889 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005890 },
5891 {
5892 .cmd = NL80211_CMD_NEW_MPATH,
5893 .doit = nl80211_new_mpath,
5894 .policy = nl80211_policy,
5895 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005896 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005897 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005898 },
5899 {
5900 .cmd = NL80211_CMD_DEL_MPATH,
5901 .doit = nl80211_del_mpath,
5902 .policy = nl80211_policy,
5903 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005904 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5905 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005906 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005907 {
5908 .cmd = NL80211_CMD_SET_BSS,
5909 .doit = nl80211_set_bss,
5910 .policy = nl80211_policy,
5911 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005912 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5913 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005914 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005915 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005916 .cmd = NL80211_CMD_GET_REG,
5917 .doit = nl80211_get_reg,
5918 .policy = nl80211_policy,
5919 /* can be retrieved by unprivileged users */
5920 },
5921 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005922 .cmd = NL80211_CMD_SET_REG,
5923 .doit = nl80211_set_reg,
5924 .policy = nl80211_policy,
5925 .flags = GENL_ADMIN_PERM,
5926 },
5927 {
5928 .cmd = NL80211_CMD_REQ_SET_REG,
5929 .doit = nl80211_req_set_reg,
5930 .policy = nl80211_policy,
5931 .flags = GENL_ADMIN_PERM,
5932 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005933 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005934 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5935 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005936 .policy = nl80211_policy,
5937 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005938 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5939 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005940 },
5941 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005942 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5943 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005944 .policy = nl80211_policy,
5945 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005947 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005948 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005949 {
Johannes Berg2a519312009-02-10 21:25:55 +01005950 .cmd = NL80211_CMD_TRIGGER_SCAN,
5951 .doit = nl80211_trigger_scan,
5952 .policy = nl80211_policy,
5953 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005955 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005956 },
5957 {
5958 .cmd = NL80211_CMD_GET_SCAN,
5959 .policy = nl80211_policy,
5960 .dumpit = nl80211_dump_scan,
5961 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005962 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005963 .cmd = NL80211_CMD_START_SCHED_SCAN,
5964 .doit = nl80211_start_sched_scan,
5965 .policy = nl80211_policy,
5966 .flags = GENL_ADMIN_PERM,
5967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5968 NL80211_FLAG_NEED_RTNL,
5969 },
5970 {
5971 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
5972 .doit = nl80211_stop_sched_scan,
5973 .policy = nl80211_policy,
5974 .flags = GENL_ADMIN_PERM,
5975 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5976 NL80211_FLAG_NEED_RTNL,
5977 },
5978 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02005979 .cmd = NL80211_CMD_AUTHENTICATE,
5980 .doit = nl80211_authenticate,
5981 .policy = nl80211_policy,
5982 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005983 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005984 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005985 },
5986 {
5987 .cmd = NL80211_CMD_ASSOCIATE,
5988 .doit = nl80211_associate,
5989 .policy = nl80211_policy,
5990 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005991 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005992 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005993 },
5994 {
5995 .cmd = NL80211_CMD_DEAUTHENTICATE,
5996 .doit = nl80211_deauthenticate,
5997 .policy = nl80211_policy,
5998 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005999 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006000 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006001 },
6002 {
6003 .cmd = NL80211_CMD_DISASSOCIATE,
6004 .doit = nl80211_disassociate,
6005 .policy = nl80211_policy,
6006 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006007 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006008 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006009 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006010 {
6011 .cmd = NL80211_CMD_JOIN_IBSS,
6012 .doit = nl80211_join_ibss,
6013 .policy = nl80211_policy,
6014 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006015 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006016 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006017 },
6018 {
6019 .cmd = NL80211_CMD_LEAVE_IBSS,
6020 .doit = nl80211_leave_ibss,
6021 .policy = nl80211_policy,
6022 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006023 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006024 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006025 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006026#ifdef CONFIG_NL80211_TESTMODE
6027 {
6028 .cmd = NL80211_CMD_TESTMODE,
6029 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006030 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006031 .policy = nl80211_policy,
6032 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006033 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6034 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006035 },
6036#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006037 {
6038 .cmd = NL80211_CMD_CONNECT,
6039 .doit = nl80211_connect,
6040 .policy = nl80211_policy,
6041 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006042 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006043 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006044 },
6045 {
6046 .cmd = NL80211_CMD_DISCONNECT,
6047 .doit = nl80211_disconnect,
6048 .policy = nl80211_policy,
6049 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006050 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006051 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006052 },
Johannes Berg463d0182009-07-14 00:33:35 +02006053 {
6054 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6055 .doit = nl80211_wiphy_netns,
6056 .policy = nl80211_policy,
6057 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006058 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6059 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006060 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006061 {
6062 .cmd = NL80211_CMD_GET_SURVEY,
6063 .policy = nl80211_policy,
6064 .dumpit = nl80211_dump_survey,
6065 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006066 {
6067 .cmd = NL80211_CMD_SET_PMKSA,
6068 .doit = nl80211_setdel_pmksa,
6069 .policy = nl80211_policy,
6070 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006071 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6072 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006073 },
6074 {
6075 .cmd = NL80211_CMD_DEL_PMKSA,
6076 .doit = nl80211_setdel_pmksa,
6077 .policy = nl80211_policy,
6078 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006079 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6080 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006081 },
6082 {
6083 .cmd = NL80211_CMD_FLUSH_PMKSA,
6084 .doit = nl80211_flush_pmksa,
6085 .policy = nl80211_policy,
6086 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006087 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6088 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006089 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006090 {
6091 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6092 .doit = nl80211_remain_on_channel,
6093 .policy = nl80211_policy,
6094 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006095 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006096 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006097 },
6098 {
6099 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6100 .doit = nl80211_cancel_remain_on_channel,
6101 .policy = nl80211_policy,
6102 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006103 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006104 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006105 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006106 {
6107 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6108 .doit = nl80211_set_tx_bitrate_mask,
6109 .policy = nl80211_policy,
6110 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006111 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6112 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006113 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006114 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006115 .cmd = NL80211_CMD_REGISTER_FRAME,
6116 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006117 .policy = nl80211_policy,
6118 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006119 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6120 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006121 },
6122 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006123 .cmd = NL80211_CMD_FRAME,
6124 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006125 .policy = nl80211_policy,
6126 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006127 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006128 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006129 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006130 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006131 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6132 .doit = nl80211_tx_mgmt_cancel_wait,
6133 .policy = nl80211_policy,
6134 .flags = GENL_ADMIN_PERM,
6135 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6136 NL80211_FLAG_NEED_RTNL,
6137 },
6138 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006139 .cmd = NL80211_CMD_SET_POWER_SAVE,
6140 .doit = nl80211_set_power_save,
6141 .policy = nl80211_policy,
6142 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006143 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6144 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006145 },
6146 {
6147 .cmd = NL80211_CMD_GET_POWER_SAVE,
6148 .doit = nl80211_get_power_save,
6149 .policy = nl80211_policy,
6150 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006151 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6152 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006153 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006154 {
6155 .cmd = NL80211_CMD_SET_CQM,
6156 .doit = nl80211_set_cqm,
6157 .policy = nl80211_policy,
6158 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006159 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6160 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006161 },
Johannes Bergf444de02010-05-05 15:25:02 +02006162 {
6163 .cmd = NL80211_CMD_SET_CHANNEL,
6164 .doit = nl80211_set_channel,
6165 .policy = nl80211_policy,
6166 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006167 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6168 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006169 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006170 {
6171 .cmd = NL80211_CMD_SET_WDS_PEER,
6172 .doit = nl80211_set_wds_peer,
6173 .policy = nl80211_policy,
6174 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006175 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6176 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006177 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006178 {
6179 .cmd = NL80211_CMD_JOIN_MESH,
6180 .doit = nl80211_join_mesh,
6181 .policy = nl80211_policy,
6182 .flags = GENL_ADMIN_PERM,
6183 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6184 NL80211_FLAG_NEED_RTNL,
6185 },
6186 {
6187 .cmd = NL80211_CMD_LEAVE_MESH,
6188 .doit = nl80211_leave_mesh,
6189 .policy = nl80211_policy,
6190 .flags = GENL_ADMIN_PERM,
6191 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6192 NL80211_FLAG_NEED_RTNL,
6193 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006194 {
6195 .cmd = NL80211_CMD_GET_WOWLAN,
6196 .doit = nl80211_get_wowlan,
6197 .policy = nl80211_policy,
6198 /* can be retrieved by unprivileged users */
6199 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6200 NL80211_FLAG_NEED_RTNL,
6201 },
6202 {
6203 .cmd = NL80211_CMD_SET_WOWLAN,
6204 .doit = nl80211_set_wowlan,
6205 .policy = nl80211_policy,
6206 .flags = GENL_ADMIN_PERM,
6207 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6208 NL80211_FLAG_NEED_RTNL,
6209 },
Johannes Berge5497d72011-07-05 16:35:40 +02006210 {
6211 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6212 .doit = nl80211_set_rekey_data,
6213 .policy = nl80211_policy,
6214 .flags = GENL_ADMIN_PERM,
6215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6216 NL80211_FLAG_NEED_RTNL,
6217 },
Johannes Berg55682962007-09-20 13:09:35 -04006218};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006219
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006220static struct genl_multicast_group nl80211_mlme_mcgrp = {
6221 .name = "mlme",
6222};
Johannes Berg55682962007-09-20 13:09:35 -04006223
6224/* multicast groups */
6225static struct genl_multicast_group nl80211_config_mcgrp = {
6226 .name = "config",
6227};
Johannes Berg2a519312009-02-10 21:25:55 +01006228static struct genl_multicast_group nl80211_scan_mcgrp = {
6229 .name = "scan",
6230};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006231static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6232 .name = "regulatory",
6233};
Johannes Berg55682962007-09-20 13:09:35 -04006234
6235/* notification functions */
6236
6237void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6238{
6239 struct sk_buff *msg;
6240
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006241 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006242 if (!msg)
6243 return;
6244
6245 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6246 nlmsg_free(msg);
6247 return;
6248 }
6249
Johannes Berg463d0182009-07-14 00:33:35 +02006250 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6251 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006252}
6253
Johannes Berg362a4152009-05-24 16:43:15 +02006254static int nl80211_add_scan_req(struct sk_buff *msg,
6255 struct cfg80211_registered_device *rdev)
6256{
6257 struct cfg80211_scan_request *req = rdev->scan_req;
6258 struct nlattr *nest;
6259 int i;
6260
Johannes Berg667503d2009-07-07 03:56:11 +02006261 ASSERT_RDEV_LOCK(rdev);
6262
Johannes Berg362a4152009-05-24 16:43:15 +02006263 if (WARN_ON(!req))
6264 return 0;
6265
6266 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6267 if (!nest)
6268 goto nla_put_failure;
6269 for (i = 0; i < req->n_ssids; i++)
6270 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6271 nla_nest_end(msg, nest);
6272
6273 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6274 if (!nest)
6275 goto nla_put_failure;
6276 for (i = 0; i < req->n_channels; i++)
6277 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6278 nla_nest_end(msg, nest);
6279
6280 if (req->ie)
6281 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6282
6283 return 0;
6284 nla_put_failure:
6285 return -ENOBUFS;
6286}
6287
Johannes Berga538e2d2009-06-16 19:56:42 +02006288static int nl80211_send_scan_msg(struct sk_buff *msg,
6289 struct cfg80211_registered_device *rdev,
6290 struct net_device *netdev,
6291 u32 pid, u32 seq, int flags,
6292 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006293{
6294 void *hdr;
6295
6296 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6297 if (!hdr)
6298 return -1;
6299
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006300 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006301 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6302
Johannes Berg362a4152009-05-24 16:43:15 +02006303 /* ignore errors and send incomplete event anyway */
6304 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006305
6306 return genlmsg_end(msg, hdr);
6307
6308 nla_put_failure:
6309 genlmsg_cancel(msg, hdr);
6310 return -EMSGSIZE;
6311}
6312
Luciano Coelho807f8a82011-05-11 17:09:35 +03006313static int
6314nl80211_send_sched_scan_msg(struct sk_buff *msg,
6315 struct cfg80211_registered_device *rdev,
6316 struct net_device *netdev,
6317 u32 pid, u32 seq, int flags, u32 cmd)
6318{
6319 void *hdr;
6320
6321 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6322 if (!hdr)
6323 return -1;
6324
6325 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6326 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6327
6328 return genlmsg_end(msg, hdr);
6329
6330 nla_put_failure:
6331 genlmsg_cancel(msg, hdr);
6332 return -EMSGSIZE;
6333}
6334
Johannes Berga538e2d2009-06-16 19:56:42 +02006335void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6336 struct net_device *netdev)
6337{
6338 struct sk_buff *msg;
6339
6340 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6341 if (!msg)
6342 return;
6343
6344 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6345 NL80211_CMD_TRIGGER_SCAN) < 0) {
6346 nlmsg_free(msg);
6347 return;
6348 }
6349
Johannes Berg463d0182009-07-14 00:33:35 +02006350 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6351 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006352}
6353
Johannes Berg2a519312009-02-10 21:25:55 +01006354void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6355 struct net_device *netdev)
6356{
6357 struct sk_buff *msg;
6358
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006360 if (!msg)
6361 return;
6362
Johannes Berga538e2d2009-06-16 19:56:42 +02006363 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6364 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006365 nlmsg_free(msg);
6366 return;
6367 }
6368
Johannes Berg463d0182009-07-14 00:33:35 +02006369 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6370 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006371}
6372
6373void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6374 struct net_device *netdev)
6375{
6376 struct sk_buff *msg;
6377
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006378 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006379 if (!msg)
6380 return;
6381
Johannes Berga538e2d2009-06-16 19:56:42 +02006382 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6383 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006384 nlmsg_free(msg);
6385 return;
6386 }
6387
Johannes Berg463d0182009-07-14 00:33:35 +02006388 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6389 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006390}
6391
Luciano Coelho807f8a82011-05-11 17:09:35 +03006392void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6393 struct net_device *netdev)
6394{
6395 struct sk_buff *msg;
6396
6397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6398 if (!msg)
6399 return;
6400
6401 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6402 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6403 nlmsg_free(msg);
6404 return;
6405 }
6406
6407 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6408 nl80211_scan_mcgrp.id, GFP_KERNEL);
6409}
6410
6411void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6412 struct net_device *netdev, u32 cmd)
6413{
6414 struct sk_buff *msg;
6415
6416 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6417 if (!msg)
6418 return;
6419
6420 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6421 nlmsg_free(msg);
6422 return;
6423 }
6424
6425 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6426 nl80211_scan_mcgrp.id, GFP_KERNEL);
6427}
6428
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006429/*
6430 * This can happen on global regulatory changes or device specific settings
6431 * based on custom world regulatory domains.
6432 */
6433void nl80211_send_reg_change_event(struct regulatory_request *request)
6434{
6435 struct sk_buff *msg;
6436 void *hdr;
6437
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006438 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006439 if (!msg)
6440 return;
6441
6442 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6443 if (!hdr) {
6444 nlmsg_free(msg);
6445 return;
6446 }
6447
6448 /* Userspace can always count this one always being set */
6449 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6450
6451 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6452 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6453 NL80211_REGDOM_TYPE_WORLD);
6454 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6455 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6456 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6457 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6458 request->intersect)
6459 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6460 NL80211_REGDOM_TYPE_INTERSECTION);
6461 else {
6462 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6463 NL80211_REGDOM_TYPE_COUNTRY);
6464 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6465 }
6466
6467 if (wiphy_idx_valid(request->wiphy_idx))
6468 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6469
6470 if (genlmsg_end(msg, hdr) < 0) {
6471 nlmsg_free(msg);
6472 return;
6473 }
6474
Johannes Bergbc43b282009-07-25 10:54:13 +02006475 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006476 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006477 GFP_ATOMIC);
6478 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006479
6480 return;
6481
6482nla_put_failure:
6483 genlmsg_cancel(msg, hdr);
6484 nlmsg_free(msg);
6485}
6486
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006487static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6488 struct net_device *netdev,
6489 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006490 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006491{
6492 struct sk_buff *msg;
6493 void *hdr;
6494
Johannes Berge6d6e342009-07-01 21:26:47 +02006495 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006496 if (!msg)
6497 return;
6498
6499 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6500 if (!hdr) {
6501 nlmsg_free(msg);
6502 return;
6503 }
6504
6505 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6506 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6507 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6508
6509 if (genlmsg_end(msg, hdr) < 0) {
6510 nlmsg_free(msg);
6511 return;
6512 }
6513
Johannes Berg463d0182009-07-14 00:33:35 +02006514 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6515 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006516 return;
6517
6518 nla_put_failure:
6519 genlmsg_cancel(msg, hdr);
6520 nlmsg_free(msg);
6521}
6522
6523void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006524 struct net_device *netdev, const u8 *buf,
6525 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006526{
6527 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006528 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006529}
6530
6531void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6532 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006533 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006534{
Johannes Berge6d6e342009-07-01 21:26:47 +02006535 nl80211_send_mlme_event(rdev, netdev, buf, len,
6536 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006537}
6538
Jouni Malinen53b46b82009-03-27 20:53:56 +02006539void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006540 struct net_device *netdev, const u8 *buf,
6541 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006542{
6543 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006544 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006545}
6546
Jouni Malinen53b46b82009-03-27 20:53:56 +02006547void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6548 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006549 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006550{
6551 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006552 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006553}
6554
Jouni Malinencf4e5942010-12-16 00:52:40 +02006555void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6556 struct net_device *netdev, const u8 *buf,
6557 size_t len, gfp_t gfp)
6558{
6559 nl80211_send_mlme_event(rdev, netdev, buf, len,
6560 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6561}
6562
6563void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6564 struct net_device *netdev, const u8 *buf,
6565 size_t len, gfp_t gfp)
6566{
6567 nl80211_send_mlme_event(rdev, netdev, buf, len,
6568 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6569}
6570
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006571static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6572 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006573 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006574{
6575 struct sk_buff *msg;
6576 void *hdr;
6577
Johannes Berge6d6e342009-07-01 21:26:47 +02006578 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006579 if (!msg)
6580 return;
6581
6582 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6583 if (!hdr) {
6584 nlmsg_free(msg);
6585 return;
6586 }
6587
6588 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6589 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6590 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6591 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6592
6593 if (genlmsg_end(msg, hdr) < 0) {
6594 nlmsg_free(msg);
6595 return;
6596 }
6597
Johannes Berg463d0182009-07-14 00:33:35 +02006598 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6599 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006600 return;
6601
6602 nla_put_failure:
6603 genlmsg_cancel(msg, hdr);
6604 nlmsg_free(msg);
6605}
6606
6607void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006608 struct net_device *netdev, const u8 *addr,
6609 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006610{
6611 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006612 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006613}
6614
6615void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006616 struct net_device *netdev, const u8 *addr,
6617 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006618{
Johannes Berge6d6e342009-07-01 21:26:47 +02006619 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6620 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006621}
6622
Samuel Ortizb23aa672009-07-01 21:26:54 +02006623void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6624 struct net_device *netdev, const u8 *bssid,
6625 const u8 *req_ie, size_t req_ie_len,
6626 const u8 *resp_ie, size_t resp_ie_len,
6627 u16 status, gfp_t gfp)
6628{
6629 struct sk_buff *msg;
6630 void *hdr;
6631
6632 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6633 if (!msg)
6634 return;
6635
6636 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6637 if (!hdr) {
6638 nlmsg_free(msg);
6639 return;
6640 }
6641
6642 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6643 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6644 if (bssid)
6645 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6646 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6647 if (req_ie)
6648 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6649 if (resp_ie)
6650 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6651
6652 if (genlmsg_end(msg, hdr) < 0) {
6653 nlmsg_free(msg);
6654 return;
6655 }
6656
Johannes Berg463d0182009-07-14 00:33:35 +02006657 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6658 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006659 return;
6660
6661 nla_put_failure:
6662 genlmsg_cancel(msg, hdr);
6663 nlmsg_free(msg);
6664
6665}
6666
6667void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6668 struct net_device *netdev, const u8 *bssid,
6669 const u8 *req_ie, size_t req_ie_len,
6670 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6671{
6672 struct sk_buff *msg;
6673 void *hdr;
6674
6675 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6676 if (!msg)
6677 return;
6678
6679 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6680 if (!hdr) {
6681 nlmsg_free(msg);
6682 return;
6683 }
6684
6685 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6686 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6687 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6688 if (req_ie)
6689 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6690 if (resp_ie)
6691 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6692
6693 if (genlmsg_end(msg, hdr) < 0) {
6694 nlmsg_free(msg);
6695 return;
6696 }
6697
Johannes Berg463d0182009-07-14 00:33:35 +02006698 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6699 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006700 return;
6701
6702 nla_put_failure:
6703 genlmsg_cancel(msg, hdr);
6704 nlmsg_free(msg);
6705
6706}
6707
6708void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6709 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006710 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006711{
6712 struct sk_buff *msg;
6713 void *hdr;
6714
Johannes Berg667503d2009-07-07 03:56:11 +02006715 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006716 if (!msg)
6717 return;
6718
6719 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6720 if (!hdr) {
6721 nlmsg_free(msg);
6722 return;
6723 }
6724
6725 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6726 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6727 if (from_ap && reason)
6728 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6729 if (from_ap)
6730 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6731 if (ie)
6732 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6733
6734 if (genlmsg_end(msg, hdr) < 0) {
6735 nlmsg_free(msg);
6736 return;
6737 }
6738
Johannes Berg463d0182009-07-14 00:33:35 +02006739 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6740 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006741 return;
6742
6743 nla_put_failure:
6744 genlmsg_cancel(msg, hdr);
6745 nlmsg_free(msg);
6746
6747}
6748
Johannes Berg04a773a2009-04-19 21:24:32 +02006749void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6750 struct net_device *netdev, const u8 *bssid,
6751 gfp_t gfp)
6752{
6753 struct sk_buff *msg;
6754 void *hdr;
6755
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006756 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006757 if (!msg)
6758 return;
6759
6760 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6761 if (!hdr) {
6762 nlmsg_free(msg);
6763 return;
6764 }
6765
6766 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6767 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6768 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6769
6770 if (genlmsg_end(msg, hdr) < 0) {
6771 nlmsg_free(msg);
6772 return;
6773 }
6774
Johannes Berg463d0182009-07-14 00:33:35 +02006775 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6776 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006777 return;
6778
6779 nla_put_failure:
6780 genlmsg_cancel(msg, hdr);
6781 nlmsg_free(msg);
6782}
6783
Javier Cardonac93b5e72011-04-07 15:08:34 -07006784void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6785 struct net_device *netdev,
6786 const u8 *macaddr, const u8* ie, u8 ie_len,
6787 gfp_t gfp)
6788{
6789 struct sk_buff *msg;
6790 void *hdr;
6791
6792 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6793 if (!msg)
6794 return;
6795
6796 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6797 if (!hdr) {
6798 nlmsg_free(msg);
6799 return;
6800 }
6801
6802 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6803 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6804 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6805 if (ie_len && ie)
6806 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6807
6808 if (genlmsg_end(msg, hdr) < 0) {
6809 nlmsg_free(msg);
6810 return;
6811 }
6812
6813 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6814 nl80211_mlme_mcgrp.id, gfp);
6815 return;
6816
6817 nla_put_failure:
6818 genlmsg_cancel(msg, hdr);
6819 nlmsg_free(msg);
6820}
6821
Jouni Malinena3b8b052009-03-27 21:59:49 +02006822void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6823 struct net_device *netdev, const u8 *addr,
6824 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006825 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006826{
6827 struct sk_buff *msg;
6828 void *hdr;
6829
Johannes Berge6d6e342009-07-01 21:26:47 +02006830 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006831 if (!msg)
6832 return;
6833
6834 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6835 if (!hdr) {
6836 nlmsg_free(msg);
6837 return;
6838 }
6839
6840 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6841 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6842 if (addr)
6843 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6844 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03006845 if (key_id != -1)
6846 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006847 if (tsc)
6848 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6849
6850 if (genlmsg_end(msg, hdr) < 0) {
6851 nlmsg_free(msg);
6852 return;
6853 }
6854
Johannes Berg463d0182009-07-14 00:33:35 +02006855 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6856 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006857 return;
6858
6859 nla_put_failure:
6860 genlmsg_cancel(msg, hdr);
6861 nlmsg_free(msg);
6862}
6863
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006864void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6865 struct ieee80211_channel *channel_before,
6866 struct ieee80211_channel *channel_after)
6867{
6868 struct sk_buff *msg;
6869 void *hdr;
6870 struct nlattr *nl_freq;
6871
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006872 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006873 if (!msg)
6874 return;
6875
6876 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6877 if (!hdr) {
6878 nlmsg_free(msg);
6879 return;
6880 }
6881
6882 /*
6883 * Since we are applying the beacon hint to a wiphy we know its
6884 * wiphy_idx is valid
6885 */
6886 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6887
6888 /* Before */
6889 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6890 if (!nl_freq)
6891 goto nla_put_failure;
6892 if (nl80211_msg_put_channel(msg, channel_before))
6893 goto nla_put_failure;
6894 nla_nest_end(msg, nl_freq);
6895
6896 /* After */
6897 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6898 if (!nl_freq)
6899 goto nla_put_failure;
6900 if (nl80211_msg_put_channel(msg, channel_after))
6901 goto nla_put_failure;
6902 nla_nest_end(msg, nl_freq);
6903
6904 if (genlmsg_end(msg, hdr) < 0) {
6905 nlmsg_free(msg);
6906 return;
6907 }
6908
Johannes Berg463d0182009-07-14 00:33:35 +02006909 rcu_read_lock();
6910 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6911 GFP_ATOMIC);
6912 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006913
6914 return;
6915
6916nla_put_failure:
6917 genlmsg_cancel(msg, hdr);
6918 nlmsg_free(msg);
6919}
6920
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006921static void nl80211_send_remain_on_chan_event(
6922 int cmd, struct cfg80211_registered_device *rdev,
6923 struct net_device *netdev, u64 cookie,
6924 struct ieee80211_channel *chan,
6925 enum nl80211_channel_type channel_type,
6926 unsigned int duration, gfp_t gfp)
6927{
6928 struct sk_buff *msg;
6929 void *hdr;
6930
6931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6932 if (!msg)
6933 return;
6934
6935 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6936 if (!hdr) {
6937 nlmsg_free(msg);
6938 return;
6939 }
6940
6941 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6942 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6943 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6944 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6945 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6946
6947 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6948 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6949
6950 if (genlmsg_end(msg, hdr) < 0) {
6951 nlmsg_free(msg);
6952 return;
6953 }
6954
6955 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6956 nl80211_mlme_mcgrp.id, gfp);
6957 return;
6958
6959 nla_put_failure:
6960 genlmsg_cancel(msg, hdr);
6961 nlmsg_free(msg);
6962}
6963
6964void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6965 struct net_device *netdev, u64 cookie,
6966 struct ieee80211_channel *chan,
6967 enum nl80211_channel_type channel_type,
6968 unsigned int duration, gfp_t gfp)
6969{
6970 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6971 rdev, netdev, cookie, chan,
6972 channel_type, duration, gfp);
6973}
6974
6975void nl80211_send_remain_on_channel_cancel(
6976 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6977 u64 cookie, struct ieee80211_channel *chan,
6978 enum nl80211_channel_type channel_type, gfp_t gfp)
6979{
6980 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6981 rdev, netdev, cookie, chan,
6982 channel_type, 0, gfp);
6983}
6984
Johannes Berg98b62182009-12-23 13:15:44 +01006985void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6986 struct net_device *dev, const u8 *mac_addr,
6987 struct station_info *sinfo, gfp_t gfp)
6988{
6989 struct sk_buff *msg;
6990
6991 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6992 if (!msg)
6993 return;
6994
6995 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6996 nlmsg_free(msg);
6997 return;
6998 }
6999
7000 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7001 nl80211_mlme_mcgrp.id, gfp);
7002}
7003
Jouni Malinenec15e682011-03-23 15:29:52 +02007004void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7005 struct net_device *dev, const u8 *mac_addr,
7006 gfp_t gfp)
7007{
7008 struct sk_buff *msg;
7009 void *hdr;
7010
7011 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7012 if (!msg)
7013 return;
7014
7015 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7016 if (!hdr) {
7017 nlmsg_free(msg);
7018 return;
7019 }
7020
7021 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7022 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7023
7024 if (genlmsg_end(msg, hdr) < 0) {
7025 nlmsg_free(msg);
7026 return;
7027 }
7028
7029 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7030 nl80211_mlme_mcgrp.id, gfp);
7031 return;
7032
7033 nla_put_failure:
7034 genlmsg_cancel(msg, hdr);
7035 nlmsg_free(msg);
7036}
7037
Johannes Berg2e161f72010-08-12 15:38:38 +02007038int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7039 struct net_device *netdev, u32 nlpid,
7040 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007041{
7042 struct sk_buff *msg;
7043 void *hdr;
7044 int err;
7045
7046 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7047 if (!msg)
7048 return -ENOMEM;
7049
Johannes Berg2e161f72010-08-12 15:38:38 +02007050 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007051 if (!hdr) {
7052 nlmsg_free(msg);
7053 return -ENOMEM;
7054 }
7055
7056 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7057 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7058 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7059 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7060
7061 err = genlmsg_end(msg, hdr);
7062 if (err < 0) {
7063 nlmsg_free(msg);
7064 return err;
7065 }
7066
7067 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7068 if (err < 0)
7069 return err;
7070 return 0;
7071
7072 nla_put_failure:
7073 genlmsg_cancel(msg, hdr);
7074 nlmsg_free(msg);
7075 return -ENOBUFS;
7076}
7077
Johannes Berg2e161f72010-08-12 15:38:38 +02007078void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7079 struct net_device *netdev, u64 cookie,
7080 const u8 *buf, size_t len, bool ack,
7081 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007082{
7083 struct sk_buff *msg;
7084 void *hdr;
7085
7086 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7087 if (!msg)
7088 return;
7089
Johannes Berg2e161f72010-08-12 15:38:38 +02007090 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007091 if (!hdr) {
7092 nlmsg_free(msg);
7093 return;
7094 }
7095
7096 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7097 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7098 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7099 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7100 if (ack)
7101 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7102
7103 if (genlmsg_end(msg, hdr) < 0) {
7104 nlmsg_free(msg);
7105 return;
7106 }
7107
7108 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7109 return;
7110
7111 nla_put_failure:
7112 genlmsg_cancel(msg, hdr);
7113 nlmsg_free(msg);
7114}
7115
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007116void
7117nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7118 struct net_device *netdev,
7119 enum nl80211_cqm_rssi_threshold_event rssi_event,
7120 gfp_t gfp)
7121{
7122 struct sk_buff *msg;
7123 struct nlattr *pinfoattr;
7124 void *hdr;
7125
7126 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7127 if (!msg)
7128 return;
7129
7130 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7131 if (!hdr) {
7132 nlmsg_free(msg);
7133 return;
7134 }
7135
7136 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7137 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7138
7139 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7140 if (!pinfoattr)
7141 goto nla_put_failure;
7142
7143 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7144 rssi_event);
7145
7146 nla_nest_end(msg, pinfoattr);
7147
7148 if (genlmsg_end(msg, hdr) < 0) {
7149 nlmsg_free(msg);
7150 return;
7151 }
7152
7153 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7154 nl80211_mlme_mcgrp.id, gfp);
7155 return;
7156
7157 nla_put_failure:
7158 genlmsg_cancel(msg, hdr);
7159 nlmsg_free(msg);
7160}
7161
Johannes Berge5497d72011-07-05 16:35:40 +02007162void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7163 struct net_device *netdev, const u8 *bssid,
7164 const u8 *replay_ctr, gfp_t gfp)
7165{
7166 struct sk_buff *msg;
7167 struct nlattr *rekey_attr;
7168 void *hdr;
7169
7170 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7171 if (!msg)
7172 return;
7173
7174 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7175 if (!hdr) {
7176 nlmsg_free(msg);
7177 return;
7178 }
7179
7180 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7181 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7182 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7183
7184 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7185 if (!rekey_attr)
7186 goto nla_put_failure;
7187
7188 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7189 NL80211_REPLAY_CTR_LEN, replay_ctr);
7190
7191 nla_nest_end(msg, rekey_attr);
7192
7193 if (genlmsg_end(msg, hdr) < 0) {
7194 nlmsg_free(msg);
7195 return;
7196 }
7197
7198 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7199 nl80211_mlme_mcgrp.id, gfp);
7200 return;
7201
7202 nla_put_failure:
7203 genlmsg_cancel(msg, hdr);
7204 nlmsg_free(msg);
7205}
7206
Johannes Bergc063dbf2010-11-24 08:10:05 +01007207void
7208nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7209 struct net_device *netdev, const u8 *peer,
7210 u32 num_packets, gfp_t gfp)
7211{
7212 struct sk_buff *msg;
7213 struct nlattr *pinfoattr;
7214 void *hdr;
7215
7216 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7217 if (!msg)
7218 return;
7219
7220 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7221 if (!hdr) {
7222 nlmsg_free(msg);
7223 return;
7224 }
7225
7226 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7227 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7228 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7229
7230 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7231 if (!pinfoattr)
7232 goto nla_put_failure;
7233
7234 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7235
7236 nla_nest_end(msg, pinfoattr);
7237
7238 if (genlmsg_end(msg, hdr) < 0) {
7239 nlmsg_free(msg);
7240 return;
7241 }
7242
7243 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7244 nl80211_mlme_mcgrp.id, gfp);
7245 return;
7246
7247 nla_put_failure:
7248 genlmsg_cancel(msg, hdr);
7249 nlmsg_free(msg);
7250}
7251
Jouni Malinen026331c2010-02-15 12:53:10 +02007252static int nl80211_netlink_notify(struct notifier_block * nb,
7253 unsigned long state,
7254 void *_notify)
7255{
7256 struct netlink_notify *notify = _notify;
7257 struct cfg80211_registered_device *rdev;
7258 struct wireless_dev *wdev;
7259
7260 if (state != NETLINK_URELEASE)
7261 return NOTIFY_DONE;
7262
7263 rcu_read_lock();
7264
7265 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7266 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007267 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007268
7269 rcu_read_unlock();
7270
7271 return NOTIFY_DONE;
7272}
7273
7274static struct notifier_block nl80211_netlink_notifier = {
7275 .notifier_call = nl80211_netlink_notify,
7276};
7277
Johannes Berg55682962007-09-20 13:09:35 -04007278/* initialisation/exit functions */
7279
7280int nl80211_init(void)
7281{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007282 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007283
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007284 err = genl_register_family_with_ops(&nl80211_fam,
7285 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007286 if (err)
7287 return err;
7288
Johannes Berg55682962007-09-20 13:09:35 -04007289 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7290 if (err)
7291 goto err_out;
7292
Johannes Berg2a519312009-02-10 21:25:55 +01007293 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7294 if (err)
7295 goto err_out;
7296
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007297 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7298 if (err)
7299 goto err_out;
7300
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007301 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7302 if (err)
7303 goto err_out;
7304
Johannes Bergaff89a92009-07-01 21:26:51 +02007305#ifdef CONFIG_NL80211_TESTMODE
7306 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7307 if (err)
7308 goto err_out;
7309#endif
7310
Jouni Malinen026331c2010-02-15 12:53:10 +02007311 err = netlink_register_notifier(&nl80211_netlink_notifier);
7312 if (err)
7313 goto err_out;
7314
Johannes Berg55682962007-09-20 13:09:35 -04007315 return 0;
7316 err_out:
7317 genl_unregister_family(&nl80211_fam);
7318 return err;
7319}
7320
7321void nl80211_exit(void)
7322{
Jouni Malinen026331c2010-02-15 12:53:10 +02007323 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007324 genl_unregister_family(&nl80211_fam);
7325}