blob: 3799623e7f465ca5789f48588be467f26d9da186 [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 Malinendc6382c2009-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 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300193 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530194 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400195};
196
Johannes Berge31b8212010-10-05 19:39:30 +0200197/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000198static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200199 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200200 [NL80211_KEY_IDX] = { .type = NLA_U8 },
201 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
202 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
203 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
204 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200205 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100206 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
207};
208
209/* policy for the key default flags */
210static const struct nla_policy
211nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
212 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
213 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200214};
215
Johannes Bergff1b6e62011-05-04 15:37:28 +0200216/* policy for WoWLAN attributes */
217static const struct nla_policy
218nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
219 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
220 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
221 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
222 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200223 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
224 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
225 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
226 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200227};
228
Johannes Berge5497d72011-07-05 16:35:40 +0200229/* policy for GTK rekey offload attributes */
230static const struct nla_policy
231nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
232 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
233 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
234 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
235};
236
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300237static const struct nla_policy
238nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
239 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
240 .len = IEEE80211_MAX_SSID_LEN },
241};
242
Holger Schuriga0438972009-11-11 11:30:02 +0100243/* ifidx get helper */
244static int nl80211_get_ifidx(struct netlink_callback *cb)
245{
246 int res;
247
248 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
249 nl80211_fam.attrbuf, nl80211_fam.maxattr,
250 nl80211_policy);
251 if (res)
252 return res;
253
254 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
255 return -EINVAL;
256
257 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
258 if (!res)
259 return -EINVAL;
260 return res;
261}
262
Johannes Berg67748892010-10-04 21:14:06 +0200263static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
264 struct netlink_callback *cb,
265 struct cfg80211_registered_device **rdev,
266 struct net_device **dev)
267{
268 int ifidx = cb->args[0];
269 int err;
270
271 if (!ifidx)
272 ifidx = nl80211_get_ifidx(cb);
273 if (ifidx < 0)
274 return ifidx;
275
276 cb->args[0] = ifidx;
277
278 rtnl_lock();
279
280 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
281 if (!*dev) {
282 err = -ENODEV;
283 goto out_rtnl;
284 }
285
286 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100287 if (IS_ERR(*rdev)) {
288 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200289 goto out_rtnl;
290 }
291
292 return 0;
293 out_rtnl:
294 rtnl_unlock();
295 return err;
296}
297
298static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
299{
300 cfg80211_unlock_rdev(rdev);
301 rtnl_unlock();
302}
303
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100304/* IE validation */
305static bool is_valid_ie_attr(const struct nlattr *attr)
306{
307 const u8 *pos;
308 int len;
309
310 if (!attr)
311 return true;
312
313 pos = nla_data(attr);
314 len = nla_len(attr);
315
316 while (len) {
317 u8 elemlen;
318
319 if (len < 2)
320 return false;
321 len -= 2;
322
323 elemlen = pos[1];
324 if (elemlen > len)
325 return false;
326
327 len -= elemlen;
328 pos += 2 + elemlen;
329 }
330
331 return true;
332}
333
Johannes Berg55682962007-09-20 13:09:35 -0400334/* message building helper */
335static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
336 int flags, u8 cmd)
337{
338 /* since there is no private header just add the generic one */
339 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
340}
341
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400342static int nl80211_msg_put_channel(struct sk_buff *msg,
343 struct ieee80211_channel *chan)
344{
345 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
346 chan->center_freq);
347
348 if (chan->flags & IEEE80211_CHAN_DISABLED)
349 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
350 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
351 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
352 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
353 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
354 if (chan->flags & IEEE80211_CHAN_RADAR)
355 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
356
357 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
358 DBM_TO_MBM(chan->max_power));
359
360 return 0;
361
362 nla_put_failure:
363 return -ENOBUFS;
364}
365
Johannes Berg55682962007-09-20 13:09:35 -0400366/* netlink command implementations */
367
Johannes Bergb9454e82009-07-08 13:29:08 +0200368struct key_parse {
369 struct key_params p;
370 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200371 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200372 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100373 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200374};
375
376static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
377{
378 struct nlattr *tb[NL80211_KEY_MAX + 1];
379 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
380 nl80211_key_policy);
381 if (err)
382 return err;
383
384 k->def = !!tb[NL80211_KEY_DEFAULT];
385 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
386
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100387 if (k->def) {
388 k->def_uni = true;
389 k->def_multi = true;
390 }
391 if (k->defmgmt)
392 k->def_multi = true;
393
Johannes Bergb9454e82009-07-08 13:29:08 +0200394 if (tb[NL80211_KEY_IDX])
395 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
396
397 if (tb[NL80211_KEY_DATA]) {
398 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
399 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
400 }
401
402 if (tb[NL80211_KEY_SEQ]) {
403 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
404 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
405 }
406
407 if (tb[NL80211_KEY_CIPHER])
408 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
409
Johannes Berge31b8212010-10-05 19:39:30 +0200410 if (tb[NL80211_KEY_TYPE]) {
411 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
412 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
413 return -EINVAL;
414 }
415
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100416 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
417 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
418 int err = nla_parse_nested(kdt,
419 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
420 tb[NL80211_KEY_DEFAULT_TYPES],
421 nl80211_key_default_policy);
422 if (err)
423 return err;
424
425 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
426 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
427 }
428
Johannes Bergb9454e82009-07-08 13:29:08 +0200429 return 0;
430}
431
432static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
433{
434 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
435 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
436 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
437 }
438
439 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
440 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
441 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
442 }
443
444 if (info->attrs[NL80211_ATTR_KEY_IDX])
445 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
446
447 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
448 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
449
450 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
451 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
452
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100453 if (k->def) {
454 k->def_uni = true;
455 k->def_multi = true;
456 }
457 if (k->defmgmt)
458 k->def_multi = true;
459
Johannes Berge31b8212010-10-05 19:39:30 +0200460 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
461 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
462 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
463 return -EINVAL;
464 }
465
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100466 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
467 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
468 int err = nla_parse_nested(
469 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
470 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
471 nl80211_key_default_policy);
472 if (err)
473 return err;
474
475 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
476 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
477 }
478
Johannes Bergb9454e82009-07-08 13:29:08 +0200479 return 0;
480}
481
482static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
483{
484 int err;
485
486 memset(k, 0, sizeof(*k));
487 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200488 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200489
490 if (info->attrs[NL80211_ATTR_KEY])
491 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
492 else
493 err = nl80211_parse_key_old(info, k);
494
495 if (err)
496 return err;
497
498 if (k->def && k->defmgmt)
499 return -EINVAL;
500
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100501 if (k->defmgmt) {
502 if (k->def_uni || !k->def_multi)
503 return -EINVAL;
504 }
505
Johannes Bergb9454e82009-07-08 13:29:08 +0200506 if (k->idx != -1) {
507 if (k->defmgmt) {
508 if (k->idx < 4 || k->idx > 5)
509 return -EINVAL;
510 } else if (k->def) {
511 if (k->idx < 0 || k->idx > 3)
512 return -EINVAL;
513 } else {
514 if (k->idx < 0 || k->idx > 5)
515 return -EINVAL;
516 }
517 }
518
519 return 0;
520}
521
Johannes Bergfffd0932009-07-08 14:22:54 +0200522static struct cfg80211_cached_keys *
523nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
524 struct nlattr *keys)
525{
526 struct key_parse parse;
527 struct nlattr *key;
528 struct cfg80211_cached_keys *result;
529 int rem, err, def = 0;
530
531 result = kzalloc(sizeof(*result), GFP_KERNEL);
532 if (!result)
533 return ERR_PTR(-ENOMEM);
534
535 result->def = -1;
536 result->defmgmt = -1;
537
538 nla_for_each_nested(key, keys, rem) {
539 memset(&parse, 0, sizeof(parse));
540 parse.idx = -1;
541
542 err = nl80211_parse_key_new(key, &parse);
543 if (err)
544 goto error;
545 err = -EINVAL;
546 if (!parse.p.key)
547 goto error;
548 if (parse.idx < 0 || parse.idx > 4)
549 goto error;
550 if (parse.def) {
551 if (def)
552 goto error;
553 def = 1;
554 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100555 if (!parse.def_uni || !parse.def_multi)
556 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200557 } else if (parse.defmgmt)
558 goto error;
559 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200560 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200561 if (err)
562 goto error;
563 result->params[parse.idx].cipher = parse.p.cipher;
564 result->params[parse.idx].key_len = parse.p.key_len;
565 result->params[parse.idx].key = result->data[parse.idx];
566 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
567 }
568
569 return result;
570 error:
571 kfree(result);
572 return ERR_PTR(err);
573}
574
575static int nl80211_key_allowed(struct wireless_dev *wdev)
576{
577 ASSERT_WDEV_LOCK(wdev);
578
Johannes Bergfffd0932009-07-08 14:22:54 +0200579 switch (wdev->iftype) {
580 case NL80211_IFTYPE_AP:
581 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200582 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700583 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200584 break;
585 case NL80211_IFTYPE_ADHOC:
586 if (!wdev->current_bss)
587 return -ENOLINK;
588 break;
589 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200590 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200591 if (wdev->sme_state != CFG80211_SME_CONNECTED)
592 return -ENOLINK;
593 break;
594 default:
595 return -EINVAL;
596 }
597
598 return 0;
599}
600
Johannes Berg7527a782011-05-13 10:58:57 +0200601static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
602{
603 struct nlattr *nl_modes = nla_nest_start(msg, attr);
604 int i;
605
606 if (!nl_modes)
607 goto nla_put_failure;
608
609 i = 0;
610 while (ifmodes) {
611 if (ifmodes & 1)
612 NLA_PUT_FLAG(msg, i);
613 ifmodes >>= 1;
614 i++;
615 }
616
617 nla_nest_end(msg, nl_modes);
618 return 0;
619
620nla_put_failure:
621 return -ENOBUFS;
622}
623
624static int nl80211_put_iface_combinations(struct wiphy *wiphy,
625 struct sk_buff *msg)
626{
627 struct nlattr *nl_combis;
628 int i, j;
629
630 nl_combis = nla_nest_start(msg,
631 NL80211_ATTR_INTERFACE_COMBINATIONS);
632 if (!nl_combis)
633 goto nla_put_failure;
634
635 for (i = 0; i < wiphy->n_iface_combinations; i++) {
636 const struct ieee80211_iface_combination *c;
637 struct nlattr *nl_combi, *nl_limits;
638
639 c = &wiphy->iface_combinations[i];
640
641 nl_combi = nla_nest_start(msg, i + 1);
642 if (!nl_combi)
643 goto nla_put_failure;
644
645 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
646 if (!nl_limits)
647 goto nla_put_failure;
648
649 for (j = 0; j < c->n_limits; j++) {
650 struct nlattr *nl_limit;
651
652 nl_limit = nla_nest_start(msg, j + 1);
653 if (!nl_limit)
654 goto nla_put_failure;
655 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
656 c->limits[j].max);
657 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
658 c->limits[j].types))
659 goto nla_put_failure;
660 nla_nest_end(msg, nl_limit);
661 }
662
663 nla_nest_end(msg, nl_limits);
664
665 if (c->beacon_int_infra_match)
666 NLA_PUT_FLAG(msg,
667 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
668 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
669 c->num_different_channels);
670 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
671 c->max_interfaces);
672
673 nla_nest_end(msg, nl_combi);
674 }
675
676 nla_nest_end(msg, nl_combis);
677
678 return 0;
679nla_put_failure:
680 return -ENOBUFS;
681}
682
Johannes Berg55682962007-09-20 13:09:35 -0400683static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
684 struct cfg80211_registered_device *dev)
685{
686 void *hdr;
Johannes Bergee688b02008-01-24 19:38:39 +0100687 struct nlattr *nl_bands, *nl_band;
688 struct nlattr *nl_freqs, *nl_freq;
689 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100690 struct nlattr *nl_cmds;
Johannes Bergee688b02008-01-24 19:38:39 +0100691 enum ieee80211_band band;
692 struct ieee80211_channel *chan;
693 struct ieee80211_rate *rate;
694 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200695 const struct ieee80211_txrx_stypes *mgmt_stypes =
696 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400697
698 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
699 if (!hdr)
700 return -1;
701
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500702 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400703 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200704
Johannes Bergf5ea9122009-08-07 16:17:38 +0200705 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
706 cfg80211_rdev_list_generation);
707
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200708 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
709 dev->wiphy.retry_short);
710 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
711 dev->wiphy.retry_long);
712 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
713 dev->wiphy.frag_threshold);
714 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
715 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100716 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
717 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100718 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
719 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300720 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
721 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200722 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
723 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300724 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
725 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300726 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
727 dev->wiphy.max_match_sets);
Johannes Bergee688b02008-01-24 19:38:39 +0100728
Johannes Berge31b8212010-10-05 19:39:30 +0200729 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
730 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700731 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
732 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300733 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
734 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Johannes Berge31b8212010-10-05 19:39:30 +0200735
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530736 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
737 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
738
Johannes Berg25e47c12009-04-02 20:14:06 +0200739 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
740 sizeof(u32) * dev->wiphy.n_cipher_suites,
741 dev->wiphy.cipher_suites);
742
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100743 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
744 dev->wiphy.max_num_pmkids);
745
Johannes Bergc0692b82010-08-27 14:26:53 +0300746 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
747 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
748
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900749 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
750 dev->wiphy.available_antennas_tx);
751 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
752 dev->wiphy.available_antennas_rx);
753
Bruno Randolf7f531e02010-12-16 11:30:22 +0900754 if ((dev->wiphy.available_antennas_tx ||
755 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900756 u32 tx_ant = 0, rx_ant = 0;
757 int res;
758 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
759 if (!res) {
760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
761 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
762 }
763 }
764
Johannes Berg7527a782011-05-13 10:58:57 +0200765 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
766 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700767 goto nla_put_failure;
768
Johannes Bergee688b02008-01-24 19:38:39 +0100769 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
770 if (!nl_bands)
771 goto nla_put_failure;
772
773 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
774 if (!dev->wiphy.bands[band])
775 continue;
776
777 nl_band = nla_nest_start(msg, band);
778 if (!nl_band)
779 goto nla_put_failure;
780
Johannes Bergd51626d2008-10-09 12:20:13 +0200781 /* add HT info */
782 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
783 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
784 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
785 &dev->wiphy.bands[band]->ht_cap.mcs);
786 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
787 dev->wiphy.bands[band]->ht_cap.cap);
788 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
789 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
790 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
791 dev->wiphy.bands[band]->ht_cap.ampdu_density);
792 }
793
Johannes Bergee688b02008-01-24 19:38:39 +0100794 /* add frequencies */
795 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
796 if (!nl_freqs)
797 goto nla_put_failure;
798
799 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
800 nl_freq = nla_nest_start(msg, i);
801 if (!nl_freq)
802 goto nla_put_failure;
803
804 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b02008-01-24 19:38:39 +0100805
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400806 if (nl80211_msg_put_channel(msg, chan))
807 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200808
Johannes Bergee688b02008-01-24 19:38:39 +0100809 nla_nest_end(msg, nl_freq);
810 }
811
812 nla_nest_end(msg, nl_freqs);
813
814 /* add bitrates */
815 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
816 if (!nl_rates)
817 goto nla_put_failure;
818
819 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
820 nl_rate = nla_nest_start(msg, i);
821 if (!nl_rate)
822 goto nla_put_failure;
823
824 rate = &dev->wiphy.bands[band]->bitrates[i];
825 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
826 rate->bitrate);
827 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
828 NLA_PUT_FLAG(msg,
829 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
830
831 nla_nest_end(msg, nl_rate);
832 }
833
834 nla_nest_end(msg, nl_rates);
835
836 nla_nest_end(msg, nl_band);
837 }
838 nla_nest_end(msg, nl_bands);
839
Johannes Berg8fdc6212009-03-14 09:34:01 +0100840 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
841 if (!nl_cmds)
842 goto nla_put_failure;
843
844 i = 0;
845#define CMD(op, n) \
846 do { \
847 if (dev->ops->op) { \
848 i++; \
849 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
850 } \
851 } while (0)
852
853 CMD(add_virtual_intf, NEW_INTERFACE);
854 CMD(change_virtual_intf, SET_INTERFACE);
855 CMD(add_key, NEW_KEY);
856 CMD(add_beacon, NEW_BEACON);
857 CMD(add_station, NEW_STATION);
858 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800859 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200861 CMD(auth, AUTHENTICATE);
862 CMD(assoc, ASSOCIATE);
863 CMD(deauth, DEAUTHENTICATE);
864 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200865 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100866 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100867 CMD(set_pmksa, SET_PMKSA);
868 CMD(del_pmksa, DEL_PMKSA);
869 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100870 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200871 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200872 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100873 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100874 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200875 i++;
876 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
877 }
Johannes Bergf444de02010-05-05 15:25:02 +0200878 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400879 CMD(set_wds_peer, SET_WDS_PEER);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300880 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
881 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100882
883#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200884
Johannes Berg6829c872009-07-02 09:13:27 +0200885 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200886 i++;
887 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
888 }
889
Johannes Berg6829c872009-07-02 09:13:27 +0200890 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200891 i++;
892 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
893 }
894
Johannes Berg8fdc6212009-03-14 09:34:01 +0100895 nla_nest_end(msg, nl_cmds);
896
Johannes Berga2939112010-12-14 17:54:28 +0100897 if (dev->ops->remain_on_channel)
898 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
899 dev->wiphy.max_remain_on_channel_duration);
900
Jouni Malinend2da5872011-08-08 12:10:30 +0300901 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100902 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
903
Johannes Berg2e161f72010-08-12 15:38:38 +0200904 if (mgmt_stypes) {
905 u16 stypes;
906 struct nlattr *nl_ftypes, *nl_ifs;
907 enum nl80211_iftype ift;
908
909 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
910 if (!nl_ifs)
911 goto nla_put_failure;
912
913 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
914 nl_ftypes = nla_nest_start(msg, ift);
915 if (!nl_ftypes)
916 goto nla_put_failure;
917 i = 0;
918 stypes = mgmt_stypes[ift].tx;
919 while (stypes) {
920 if (stypes & 1)
921 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
922 (i << 4) | IEEE80211_FTYPE_MGMT);
923 stypes >>= 1;
924 i++;
925 }
926 nla_nest_end(msg, nl_ftypes);
927 }
928
Johannes Berg74b70a42010-08-24 12:15:53 +0200929 nla_nest_end(msg, nl_ifs);
930
Johannes Berg2e161f72010-08-12 15:38:38 +0200931 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
932 if (!nl_ifs)
933 goto nla_put_failure;
934
935 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
936 nl_ftypes = nla_nest_start(msg, ift);
937 if (!nl_ftypes)
938 goto nla_put_failure;
939 i = 0;
940 stypes = mgmt_stypes[ift].rx;
941 while (stypes) {
942 if (stypes & 1)
943 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
944 (i << 4) | IEEE80211_FTYPE_MGMT);
945 stypes >>= 1;
946 i++;
947 }
948 nla_nest_end(msg, nl_ftypes);
949 }
950 nla_nest_end(msg, nl_ifs);
951 }
952
Johannes Bergff1b6e62011-05-04 15:37:28 +0200953 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
954 struct nlattr *nl_wowlan;
955
956 nl_wowlan = nla_nest_start(msg,
957 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
958 if (!nl_wowlan)
959 goto nla_put_failure;
960
961 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
962 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
963 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
964 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
965 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
966 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200967 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
968 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
969 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
970 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
971 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
972 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
973 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
974 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
975 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
976 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200977 if (dev->wiphy.wowlan.n_patterns) {
978 struct nl80211_wowlan_pattern_support pat = {
979 .max_patterns = dev->wiphy.wowlan.n_patterns,
980 .min_pattern_len =
981 dev->wiphy.wowlan.pattern_min_len,
982 .max_pattern_len =
983 dev->wiphy.wowlan.pattern_max_len,
984 };
985 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
986 sizeof(pat), &pat);
987 }
988
989 nla_nest_end(msg, nl_wowlan);
990 }
991
Johannes Berg7527a782011-05-13 10:58:57 +0200992 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
993 dev->wiphy.software_iftypes))
994 goto nla_put_failure;
995
996 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
997 goto nla_put_failure;
998
Johannes Berg55682962007-09-20 13:09:35 -0400999 return genlmsg_end(msg, hdr);
1000
1001 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001002 genlmsg_cancel(msg, hdr);
1003 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001004}
1005
1006static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1007{
1008 int idx = 0;
1009 int start = cb->args[0];
1010 struct cfg80211_registered_device *dev;
1011
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001012 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001013 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001014 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1015 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001016 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001017 continue;
1018 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1019 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001020 dev) < 0) {
1021 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001022 break;
Julius Volzb4637272008-07-08 14:02:19 +02001023 }
Johannes Berg55682962007-09-20 13:09:35 -04001024 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001025 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001026
1027 cb->args[0] = idx;
1028
1029 return skb->len;
1030}
1031
1032static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1033{
1034 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001035 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001036
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001037 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001038 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001039 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001040
Johannes Berg4c476992010-10-04 21:36:35 +02001041 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1042 nlmsg_free(msg);
1043 return -ENOBUFS;
1044 }
Johannes Berg55682962007-09-20 13:09:35 -04001045
Johannes Berg134e6372009-07-10 09:51:34 +00001046 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001047}
1048
Jouni Malinen31888482008-10-30 16:59:24 +02001049static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1050 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1051 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1052 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1053 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1054 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1055};
1056
1057static int parse_txq_params(struct nlattr *tb[],
1058 struct ieee80211_txq_params *txq_params)
1059{
1060 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1061 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1062 !tb[NL80211_TXQ_ATTR_AIFS])
1063 return -EINVAL;
1064
1065 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1066 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1067 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1068 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1069 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1070
1071 return 0;
1072}
1073
Johannes Bergf444de02010-05-05 15:25:02 +02001074static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1075{
1076 /*
1077 * You can only set the channel explicitly for AP, mesh
1078 * and WDS type interfaces; all others have their channel
1079 * managed via their respective "establish a connection"
1080 * command (connect, join, ...)
1081 *
1082 * Monitors are special as they are normally slaved to
1083 * whatever else is going on, so they behave as though
1084 * you tried setting the wiphy channel itself.
1085 */
1086 return !wdev ||
1087 wdev->iftype == NL80211_IFTYPE_AP ||
1088 wdev->iftype == NL80211_IFTYPE_WDS ||
1089 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001090 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1091 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001092}
1093
1094static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1095 struct wireless_dev *wdev,
1096 struct genl_info *info)
1097{
1098 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1099 u32 freq;
1100 int result;
1101
1102 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1103 return -EINVAL;
1104
1105 if (!nl80211_can_set_dev_channel(wdev))
1106 return -EOPNOTSUPP;
1107
1108 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1109 channel_type = nla_get_u32(info->attrs[
1110 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1111 if (channel_type != NL80211_CHAN_NO_HT &&
1112 channel_type != NL80211_CHAN_HT20 &&
1113 channel_type != NL80211_CHAN_HT40PLUS &&
1114 channel_type != NL80211_CHAN_HT40MINUS)
1115 return -EINVAL;
1116 }
1117
1118 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1119
1120 mutex_lock(&rdev->devlist_mtx);
1121 if (wdev) {
1122 wdev_lock(wdev);
1123 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1124 wdev_unlock(wdev);
1125 } else {
1126 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1127 }
1128 mutex_unlock(&rdev->devlist_mtx);
1129
1130 return result;
1131}
1132
1133static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1134{
Johannes Berg4c476992010-10-04 21:36:35 +02001135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1136 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001137
Johannes Berg4c476992010-10-04 21:36:35 +02001138 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001139}
1140
Bill Jordane8347eb2010-10-01 13:54:28 -04001141static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1142{
Johannes Berg43b19952010-10-07 13:10:30 +02001143 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1144 struct net_device *dev = info->user_ptr[1];
1145 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001146 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001147
1148 if (!info->attrs[NL80211_ATTR_MAC])
1149 return -EINVAL;
1150
Johannes Berg43b19952010-10-07 13:10:30 +02001151 if (netif_running(dev))
1152 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001153
Johannes Berg43b19952010-10-07 13:10:30 +02001154 if (!rdev->ops->set_wds_peer)
1155 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001156
Johannes Berg43b19952010-10-07 13:10:30 +02001157 if (wdev->iftype != NL80211_IFTYPE_WDS)
1158 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001159
1160 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001161 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001162}
1163
1164
Johannes Berg55682962007-09-20 13:09:35 -04001165static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1166{
1167 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001168 struct net_device *netdev = NULL;
1169 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001170 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001171 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001172 u32 changed;
1173 u8 retry_short = 0, retry_long = 0;
1174 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001175 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001176
Johannes Bergf444de02010-05-05 15:25:02 +02001177 /*
1178 * Try to find the wiphy and netdev. Normally this
1179 * function shouldn't need the netdev, but this is
1180 * done for backward compatibility -- previously
1181 * setting the channel was done per wiphy, but now
1182 * it is per netdev. Previous userland like hostapd
1183 * also passed a netdev to set_wiphy, so that it is
1184 * possible to let that go to the right netdev!
1185 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001186 mutex_lock(&cfg80211_mutex);
1187
Johannes Bergf444de02010-05-05 15:25:02 +02001188 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1189 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1190
1191 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1192 if (netdev && netdev->ieee80211_ptr) {
1193 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1194 mutex_lock(&rdev->mtx);
1195 } else
1196 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001197 }
1198
Johannes Bergf444de02010-05-05 15:25:02 +02001199 if (!netdev) {
1200 rdev = __cfg80211_rdev_from_info(info);
1201 if (IS_ERR(rdev)) {
1202 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001203 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001204 }
1205 wdev = NULL;
1206 netdev = NULL;
1207 result = 0;
1208
1209 mutex_lock(&rdev->mtx);
1210 } else if (netif_running(netdev) &&
1211 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1212 wdev = netdev->ieee80211_ptr;
1213 else
1214 wdev = NULL;
1215
1216 /*
1217 * end workaround code, by now the rdev is available
1218 * and locked, and wdev may or may not be NULL.
1219 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001220
1221 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001222 result = cfg80211_dev_rename(
1223 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001224
1225 mutex_unlock(&cfg80211_mutex);
1226
1227 if (result)
1228 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001229
Jouni Malinen31888482008-10-30 16:59:24 +02001230 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1231 struct ieee80211_txq_params txq_params;
1232 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1233
1234 if (!rdev->ops->set_txq_params) {
1235 result = -EOPNOTSUPP;
1236 goto bad_res;
1237 }
1238
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001239 if (!netdev) {
1240 result = -EINVAL;
1241 goto bad_res;
1242 }
1243
Jouni Malinen31888482008-10-30 16:59:24 +02001244 nla_for_each_nested(nl_txq_params,
1245 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1246 rem_txq_params) {
1247 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1248 nla_data(nl_txq_params),
1249 nla_len(nl_txq_params),
1250 txq_params_policy);
1251 result = parse_txq_params(tb, &txq_params);
1252 if (result)
1253 goto bad_res;
1254
1255 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001256 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001257 &txq_params);
1258 if (result)
1259 goto bad_res;
1260 }
1261 }
1262
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001263 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001264 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001265 if (result)
1266 goto bad_res;
1267 }
1268
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001269 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1270 enum nl80211_tx_power_setting type;
1271 int idx, mbm = 0;
1272
1273 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001274 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001275 goto bad_res;
1276 }
1277
1278 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1279 type = nla_get_u32(info->attrs[idx]);
1280
1281 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1282 (type != NL80211_TX_POWER_AUTOMATIC)) {
1283 result = -EINVAL;
1284 goto bad_res;
1285 }
1286
1287 if (type != NL80211_TX_POWER_AUTOMATIC) {
1288 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1289 mbm = nla_get_u32(info->attrs[idx]);
1290 }
1291
1292 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1293 if (result)
1294 goto bad_res;
1295 }
1296
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001297 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1298 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1299 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001300 if ((!rdev->wiphy.available_antennas_tx &&
1301 !rdev->wiphy.available_antennas_rx) ||
1302 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001303 result = -EOPNOTSUPP;
1304 goto bad_res;
1305 }
1306
1307 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1308 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1309
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001310 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001311 * available antenna masks, except for the "all" mask */
1312 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1313 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001314 result = -EINVAL;
1315 goto bad_res;
1316 }
1317
Bruno Randolf7f531e02010-12-16 11:30:22 +09001318 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1319 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001320
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001321 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1322 if (result)
1323 goto bad_res;
1324 }
1325
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001326 changed = 0;
1327
1328 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1329 retry_short = nla_get_u8(
1330 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1331 if (retry_short == 0) {
1332 result = -EINVAL;
1333 goto bad_res;
1334 }
1335 changed |= WIPHY_PARAM_RETRY_SHORT;
1336 }
1337
1338 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1339 retry_long = nla_get_u8(
1340 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1341 if (retry_long == 0) {
1342 result = -EINVAL;
1343 goto bad_res;
1344 }
1345 changed |= WIPHY_PARAM_RETRY_LONG;
1346 }
1347
1348 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1349 frag_threshold = nla_get_u32(
1350 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1351 if (frag_threshold < 256) {
1352 result = -EINVAL;
1353 goto bad_res;
1354 }
1355 if (frag_threshold != (u32) -1) {
1356 /*
1357 * Fragments (apart from the last one) are required to
1358 * have even length. Make the fragmentation code
1359 * simpler by stripping LSB should someone try to use
1360 * odd threshold value.
1361 */
1362 frag_threshold &= ~0x1;
1363 }
1364 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1365 }
1366
1367 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1368 rts_threshold = nla_get_u32(
1369 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1370 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1371 }
1372
Lukáš Turek81077e82009-12-21 22:50:47 +01001373 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1374 coverage_class = nla_get_u8(
1375 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1376 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1377 }
1378
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001379 if (changed) {
1380 u8 old_retry_short, old_retry_long;
1381 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001382 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001383
1384 if (!rdev->ops->set_wiphy_params) {
1385 result = -EOPNOTSUPP;
1386 goto bad_res;
1387 }
1388
1389 old_retry_short = rdev->wiphy.retry_short;
1390 old_retry_long = rdev->wiphy.retry_long;
1391 old_frag_threshold = rdev->wiphy.frag_threshold;
1392 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001393 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001394
1395 if (changed & WIPHY_PARAM_RETRY_SHORT)
1396 rdev->wiphy.retry_short = retry_short;
1397 if (changed & WIPHY_PARAM_RETRY_LONG)
1398 rdev->wiphy.retry_long = retry_long;
1399 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1400 rdev->wiphy.frag_threshold = frag_threshold;
1401 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1402 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001403 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1404 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001405
1406 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1407 if (result) {
1408 rdev->wiphy.retry_short = old_retry_short;
1409 rdev->wiphy.retry_long = old_retry_long;
1410 rdev->wiphy.frag_threshold = old_frag_threshold;
1411 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001412 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001413 }
1414 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001415
Johannes Berg306d6112008-12-08 12:39:04 +01001416 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001417 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001418 if (netdev)
1419 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001420 return result;
1421}
1422
1423
1424static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001425 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001426 struct net_device *dev)
1427{
1428 void *hdr;
1429
1430 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1431 if (!hdr)
1432 return -1;
1433
1434 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001435 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001436 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001437 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001438
1439 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1440 rdev->devlist_generation ^
1441 (cfg80211_rdev_list_generation << 2));
1442
Johannes Berg55682962007-09-20 13:09:35 -04001443 return genlmsg_end(msg, hdr);
1444
1445 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001446 genlmsg_cancel(msg, hdr);
1447 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001448}
1449
1450static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1451{
1452 int wp_idx = 0;
1453 int if_idx = 0;
1454 int wp_start = cb->args[0];
1455 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001456 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001457 struct wireless_dev *wdev;
1458
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001459 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001460 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1461 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001462 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001463 if (wp_idx < wp_start) {
1464 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001465 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001466 }
Johannes Berg55682962007-09-20 13:09:35 -04001467 if_idx = 0;
1468
Johannes Bergf5ea9122009-08-07 16:17:38 +02001469 mutex_lock(&rdev->devlist_mtx);
1470 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001471 if (if_idx < if_start) {
1472 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001473 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001474 }
Johannes Berg55682962007-09-20 13:09:35 -04001475 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1476 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001477 rdev, wdev->netdev) < 0) {
1478 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001479 goto out;
1480 }
1481 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001482 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001483 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001484
1485 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001486 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001487 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001488 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001489
1490 cb->args[0] = wp_idx;
1491 cb->args[1] = if_idx;
1492
1493 return skb->len;
1494}
1495
1496static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1497{
1498 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001499 struct cfg80211_registered_device *dev = info->user_ptr[0];
1500 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001501
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001502 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001503 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001504 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001505
Johannes Bergd7264052009-04-19 16:23:20 +02001506 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001507 dev, netdev) < 0) {
1508 nlmsg_free(msg);
1509 return -ENOBUFS;
1510 }
Johannes Berg55682962007-09-20 13:09:35 -04001511
Johannes Berg134e6372009-07-10 09:51:34 +00001512 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001513}
1514
Michael Wu66f7ac52008-01-31 19:48:22 +01001515static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1516 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1517 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1518 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1519 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1520 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1521};
1522
1523static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1524{
1525 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1526 int flag;
1527
1528 *mntrflags = 0;
1529
1530 if (!nla)
1531 return -EINVAL;
1532
1533 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1534 nla, mntr_flags_policy))
1535 return -EINVAL;
1536
1537 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1538 if (flags[flag])
1539 *mntrflags |= (1<<flag);
1540
1541 return 0;
1542}
1543
Johannes Berg9bc383d2009-11-19 11:55:19 +01001544static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001545 struct net_device *netdev, u8 use_4addr,
1546 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001547{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001548 if (!use_4addr) {
Jiri Pirkof350a0a2010-06-15 06:50:45 +00001549 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001550 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001551 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001552 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001553
1554 switch (iftype) {
1555 case NL80211_IFTYPE_AP_VLAN:
1556 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1557 return 0;
1558 break;
1559 case NL80211_IFTYPE_STATION:
1560 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1561 return 0;
1562 break;
1563 default:
1564 break;
1565 }
1566
1567 return -EOPNOTSUPP;
1568}
1569
Johannes Berg55682962007-09-20 13:09:35 -04001570static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1571{
Johannes Berg4c476992010-10-04 21:36:35 +02001572 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001573 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001574 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001575 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001576 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001577 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001578 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001579
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001580 memset(&params, 0, sizeof(params));
1581
Johannes Berg04a773a2009-04-19 21:24:32 +02001582 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001583
Johannes Berg723b0382008-09-16 20:22:09 +02001584 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001585 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001586 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001587 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001588 if (ntype > NL80211_IFTYPE_MAX)
1589 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001590 }
1591
Johannes Berg92ffe052008-09-16 20:39:36 +02001592 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001593 struct wireless_dev *wdev = dev->ieee80211_ptr;
1594
Johannes Berg4c476992010-10-04 21:36:35 +02001595 if (ntype != NL80211_IFTYPE_MESH_POINT)
1596 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001597 if (netif_running(dev))
1598 return -EBUSY;
1599
1600 wdev_lock(wdev);
1601 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1602 IEEE80211_MAX_MESH_ID_LEN);
1603 wdev->mesh_id_up_len =
1604 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1605 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1606 wdev->mesh_id_up_len);
1607 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001608 }
1609
Felix Fietkau8b787642009-11-10 18:53:10 +01001610 if (info->attrs[NL80211_ATTR_4ADDR]) {
1611 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1612 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001613 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001614 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001615 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001616 } else {
1617 params.use_4addr = -1;
1618 }
1619
Johannes Berg92ffe052008-09-16 20:39:36 +02001620 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001621 if (ntype != NL80211_IFTYPE_MONITOR)
1622 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001623 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1624 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001625 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001626 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001627
1628 flags = &_flags;
1629 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001630 }
Johannes Berg3b858752009-03-12 09:55:09 +01001631
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001632 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001633 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001634 else
1635 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001636
Johannes Berg9bc383d2009-11-19 11:55:19 +01001637 if (!err && params.use_4addr != -1)
1638 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1639
Johannes Berg55682962007-09-20 13:09:35 -04001640 return err;
1641}
1642
1643static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1644{
Johannes Berg4c476992010-10-04 21:36:35 +02001645 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001646 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001647 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001648 int err;
1649 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001650 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001651
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001652 memset(&params, 0, sizeof(params));
1653
Johannes Berg55682962007-09-20 13:09:35 -04001654 if (!info->attrs[NL80211_ATTR_IFNAME])
1655 return -EINVAL;
1656
1657 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1658 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1659 if (type > NL80211_IFTYPE_MAX)
1660 return -EINVAL;
1661 }
1662
Johannes Berg79c97e92009-07-07 03:56:12 +02001663 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001664 !(rdev->wiphy.interface_modes & (1 << type)))
1665 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001666
Johannes Berg9bc383d2009-11-19 11:55:19 +01001667 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001668 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001669 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001670 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001671 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001672 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001673
Michael Wu66f7ac52008-01-31 19:48:22 +01001674 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1675 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1676 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001677 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001678 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001679 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001680 if (IS_ERR(dev))
1681 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001682
Johannes Berg29cbe682010-12-03 09:20:44 +01001683 if (type == NL80211_IFTYPE_MESH_POINT &&
1684 info->attrs[NL80211_ATTR_MESH_ID]) {
1685 struct wireless_dev *wdev = dev->ieee80211_ptr;
1686
1687 wdev_lock(wdev);
1688 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1689 IEEE80211_MAX_MESH_ID_LEN);
1690 wdev->mesh_id_up_len =
1691 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1692 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1693 wdev->mesh_id_up_len);
1694 wdev_unlock(wdev);
1695 }
1696
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001697 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001698}
1699
1700static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1701{
Johannes Berg4c476992010-10-04 21:36:35 +02001702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1703 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001704
Johannes Berg4c476992010-10-04 21:36:35 +02001705 if (!rdev->ops->del_virtual_intf)
1706 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001707
Johannes Berg4c476992010-10-04 21:36:35 +02001708 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001709}
1710
Johannes Berg41ade002007-12-19 02:03:29 +01001711struct get_key_cookie {
1712 struct sk_buff *msg;
1713 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001714 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001715};
1716
1717static void get_key_callback(void *c, struct key_params *params)
1718{
Johannes Bergb9454e82009-07-08 13:29:08 +02001719 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001720 struct get_key_cookie *cookie = c;
1721
1722 if (params->key)
1723 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1724 params->key_len, params->key);
1725
1726 if (params->seq)
1727 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1728 params->seq_len, params->seq);
1729
1730 if (params->cipher)
1731 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1732 params->cipher);
1733
Johannes Bergb9454e82009-07-08 13:29:08 +02001734 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1735 if (!key)
1736 goto nla_put_failure;
1737
1738 if (params->key)
1739 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1740 params->key_len, params->key);
1741
1742 if (params->seq)
1743 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1744 params->seq_len, params->seq);
1745
1746 if (params->cipher)
1747 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1748 params->cipher);
1749
1750 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1751
1752 nla_nest_end(cookie->msg, key);
1753
Johannes Berg41ade002007-12-19 02:03:29 +01001754 return;
1755 nla_put_failure:
1756 cookie->error = 1;
1757}
1758
1759static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1760{
Johannes Berg4c476992010-10-04 21:36:35 +02001761 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001762 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001763 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001764 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001765 const u8 *mac_addr = NULL;
1766 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001767 struct get_key_cookie cookie = {
1768 .error = 0,
1769 };
1770 void *hdr;
1771 struct sk_buff *msg;
1772
1773 if (info->attrs[NL80211_ATTR_KEY_IDX])
1774 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1775
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001776 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001777 return -EINVAL;
1778
1779 if (info->attrs[NL80211_ATTR_MAC])
1780 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1781
Johannes Berge31b8212010-10-05 19:39:30 +02001782 pairwise = !!mac_addr;
1783 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1784 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1785 if (kt >= NUM_NL80211_KEYTYPES)
1786 return -EINVAL;
1787 if (kt != NL80211_KEYTYPE_GROUP &&
1788 kt != NL80211_KEYTYPE_PAIRWISE)
1789 return -EINVAL;
1790 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1791 }
1792
Johannes Berg4c476992010-10-04 21:36:35 +02001793 if (!rdev->ops->get_key)
1794 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001795
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001796 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001797 if (!msg)
1798 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001799
1800 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1801 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001802 if (IS_ERR(hdr))
1803 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001804
1805 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001806 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001807
1808 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1809 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1810 if (mac_addr)
1811 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1812
Johannes Berge31b8212010-10-05 19:39:30 +02001813 if (pairwise && mac_addr &&
1814 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1815 return -ENOENT;
1816
1817 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1818 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001819
1820 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001821 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001822
1823 if (cookie.error)
1824 goto nla_put_failure;
1825
1826 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001827 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001828
1829 nla_put_failure:
1830 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001831 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001832 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001833 return err;
1834}
1835
1836static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1837{
Johannes Berg4c476992010-10-04 21:36:35 +02001838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001839 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001840 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001841 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001842
Johannes Bergb9454e82009-07-08 13:29:08 +02001843 err = nl80211_parse_key(info, &key);
1844 if (err)
1845 return err;
1846
1847 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001848 return -EINVAL;
1849
Johannes Bergb9454e82009-07-08 13:29:08 +02001850 /* only support setting default key */
1851 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001852 return -EINVAL;
1853
Johannes Bergfffd0932009-07-08 14:22:54 +02001854 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001855
1856 if (key.def) {
1857 if (!rdev->ops->set_default_key) {
1858 err = -EOPNOTSUPP;
1859 goto out;
1860 }
1861
1862 err = nl80211_key_allowed(dev->ieee80211_ptr);
1863 if (err)
1864 goto out;
1865
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001866 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1867 key.def_uni, key.def_multi);
1868
1869 if (err)
1870 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001871
Johannes Berg3d23e342009-09-29 23:27:28 +02001872#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001873 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001874#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001875 } else {
1876 if (key.def_uni || !key.def_multi) {
1877 err = -EINVAL;
1878 goto out;
1879 }
1880
1881 if (!rdev->ops->set_default_mgmt_key) {
1882 err = -EOPNOTSUPP;
1883 goto out;
1884 }
1885
1886 err = nl80211_key_allowed(dev->ieee80211_ptr);
1887 if (err)
1888 goto out;
1889
1890 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1891 dev, key.idx);
1892 if (err)
1893 goto out;
1894
1895#ifdef CONFIG_CFG80211_WEXT
1896 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1897#endif
1898 }
1899
1900 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001901 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001902
Johannes Berg41ade002007-12-19 02:03:29 +01001903 return err;
1904}
1905
1906static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1907{
Johannes Berg4c476992010-10-04 21:36:35 +02001908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001909 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001910 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001911 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001912 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001913
Johannes Bergb9454e82009-07-08 13:29:08 +02001914 err = nl80211_parse_key(info, &key);
1915 if (err)
1916 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001917
Johannes Bergb9454e82009-07-08 13:29:08 +02001918 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001919 return -EINVAL;
1920
Johannes Berg41ade002007-12-19 02:03:29 +01001921 if (info->attrs[NL80211_ATTR_MAC])
1922 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1923
Johannes Berge31b8212010-10-05 19:39:30 +02001924 if (key.type == -1) {
1925 if (mac_addr)
1926 key.type = NL80211_KEYTYPE_PAIRWISE;
1927 else
1928 key.type = NL80211_KEYTYPE_GROUP;
1929 }
1930
1931 /* for now */
1932 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1933 key.type != NL80211_KEYTYPE_GROUP)
1934 return -EINVAL;
1935
Johannes Berg4c476992010-10-04 21:36:35 +02001936 if (!rdev->ops->add_key)
1937 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001938
Johannes Berge31b8212010-10-05 19:39:30 +02001939 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1940 key.type == NL80211_KEYTYPE_PAIRWISE,
1941 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001942 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001943
1944 wdev_lock(dev->ieee80211_ptr);
1945 err = nl80211_key_allowed(dev->ieee80211_ptr);
1946 if (!err)
1947 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001948 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001949 mac_addr, &key.p);
1950 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001951
Johannes Berg41ade002007-12-19 02:03:29 +01001952 return err;
1953}
1954
1955static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1956{
Johannes Berg4c476992010-10-04 21:36:35 +02001957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001958 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001959 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001960 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001961 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001962
Johannes Bergb9454e82009-07-08 13:29:08 +02001963 err = nl80211_parse_key(info, &key);
1964 if (err)
1965 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001966
1967 if (info->attrs[NL80211_ATTR_MAC])
1968 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1969
Johannes Berge31b8212010-10-05 19:39:30 +02001970 if (key.type == -1) {
1971 if (mac_addr)
1972 key.type = NL80211_KEYTYPE_PAIRWISE;
1973 else
1974 key.type = NL80211_KEYTYPE_GROUP;
1975 }
1976
1977 /* for now */
1978 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1979 key.type != NL80211_KEYTYPE_GROUP)
1980 return -EINVAL;
1981
Johannes Berg4c476992010-10-04 21:36:35 +02001982 if (!rdev->ops->del_key)
1983 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001984
Johannes Bergfffd0932009-07-08 14:22:54 +02001985 wdev_lock(dev->ieee80211_ptr);
1986 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001987
1988 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1989 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1990 err = -ENOENT;
1991
Johannes Bergfffd0932009-07-08 14:22:54 +02001992 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001993 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1994 key.type == NL80211_KEYTYPE_PAIRWISE,
1995 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001996
Johannes Berg3d23e342009-09-29 23:27:28 +02001997#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001998 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001999 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002000 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002001 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002002 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2003 }
2004#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002005 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002006
Johannes Berg41ade002007-12-19 02:03:29 +01002007 return err;
2008}
2009
Johannes Berged1b6cc2007-12-19 02:03:32 +01002010static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2011{
2012 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2013 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002014 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2015 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002016 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002017 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002018 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002019
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002020 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2021 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2022 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2023 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002024 return -EINVAL;
2025
Johannes Berg074ac8d2010-09-16 14:58:22 +02002026 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002027 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2028 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002029
Johannes Berg56d18932011-05-09 18:41:15 +02002030 memset(&params, 0, sizeof(params));
2031
Johannes Berged1b6cc2007-12-19 02:03:32 +01002032 switch (info->genlhdr->cmd) {
2033 case NL80211_CMD_NEW_BEACON:
2034 /* these are required for NEW_BEACON */
2035 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2036 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002037 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2038 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002039
Johannes Berg56d18932011-05-09 18:41:15 +02002040 params.interval =
2041 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2042 params.dtim_period =
2043 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2044
2045 err = cfg80211_validate_beacon_int(rdev, params.interval);
2046 if (err)
2047 return err;
2048
Jouni Malinen32e9de82011-08-10 23:53:31 +03002049 /*
2050 * In theory, some of these attributes could be required for
2051 * NEW_BEACON, but since they were not used when the command was
2052 * originally added, keep them optional for old user space
2053 * programs to work with drivers that do not need the additional
2054 * information.
2055 */
2056 if (info->attrs[NL80211_ATTR_SSID]) {
2057 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2058 params.ssid_len =
2059 nla_len(info->attrs[NL80211_ATTR_SSID]);
2060 if (params.ssid_len == 0 ||
2061 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2062 return -EINVAL;
2063 }
2064
2065 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2066 params.hidden_ssid = nla_get_u32(
2067 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2068 if (params.hidden_ssid !=
2069 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2070 params.hidden_ssid !=
2071 NL80211_HIDDEN_SSID_ZERO_LEN &&
2072 params.hidden_ssid !=
2073 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2074 return -EINVAL;
2075 }
2076
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002077 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2078
2079 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2080 params.auth_type = nla_get_u32(
2081 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2082 if (!nl80211_valid_auth_type(params.auth_type))
2083 return -EINVAL;
2084 } else
2085 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2086
2087 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2088 NL80211_MAX_NR_CIPHER_SUITES);
2089 if (err)
2090 return err;
2091
Johannes Berg79c97e92009-07-07 03:56:12 +02002092 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002093 break;
2094 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002095 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002096 break;
2097 default:
2098 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002099 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002100 }
2101
Johannes Berg4c476992010-10-04 21:36:35 +02002102 if (!call)
2103 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002104
Johannes Berged1b6cc2007-12-19 02:03:32 +01002105 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2106 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2107 params.head_len =
2108 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2109 haveinfo = 1;
2110 }
2111
2112 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2113 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2114 params.tail_len =
2115 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2116 haveinfo = 1;
2117 }
2118
Johannes Berg4c476992010-10-04 21:36:35 +02002119 if (!haveinfo)
2120 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002121
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002122 if (info->attrs[NL80211_ATTR_IE]) {
2123 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2124 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2125 }
2126
2127 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2128 params.proberesp_ies =
2129 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2130 params.proberesp_ies_len =
2131 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2132 }
2133
2134 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2135 params.assocresp_ies =
2136 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2137 params.assocresp_ies_len =
2138 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2139 }
2140
Johannes Berg56d18932011-05-09 18:41:15 +02002141 err = call(&rdev->wiphy, dev, &params);
2142 if (!err && params.interval)
2143 wdev->beacon_interval = params.interval;
2144 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002145}
2146
2147static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2148{
Johannes Berg4c476992010-10-04 21:36:35 +02002149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2150 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002151 struct wireless_dev *wdev = dev->ieee80211_ptr;
2152 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002153
Johannes Berg4c476992010-10-04 21:36:35 +02002154 if (!rdev->ops->del_beacon)
2155 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002156
Johannes Berg074ac8d2010-09-16 14:58:22 +02002157 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002158 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2159 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002160
Johannes Berg56d18932011-05-09 18:41:15 +02002161 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2162 if (!err)
2163 wdev->beacon_interval = 0;
2164 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002165}
2166
Johannes Berg5727ef12007-12-19 02:03:34 +01002167static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2168 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2169 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2170 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002171 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002172 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002173};
2174
Johannes Bergeccb8e82009-05-11 21:57:56 +03002175static int parse_station_flags(struct genl_info *info,
2176 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002177{
2178 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002179 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002180 int flag;
2181
Johannes Bergeccb8e82009-05-11 21:57:56 +03002182 /*
2183 * Try parsing the new attribute first so userspace
2184 * can specify both for older kernels.
2185 */
2186 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2187 if (nla) {
2188 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002189
Johannes Bergeccb8e82009-05-11 21:57:56 +03002190 sta_flags = nla_data(nla);
2191 params->sta_flags_mask = sta_flags->mask;
2192 params->sta_flags_set = sta_flags->set;
2193 if ((params->sta_flags_mask |
2194 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2195 return -EINVAL;
2196 return 0;
2197 }
2198
2199 /* if present, parse the old attribute */
2200
2201 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002202 if (!nla)
2203 return 0;
2204
2205 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2206 nla, sta_flags_policy))
2207 return -EINVAL;
2208
Johannes Bergeccb8e82009-05-11 21:57:56 +03002209 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2210 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002211
2212 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2213 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002214 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002215
2216 return 0;
2217}
2218
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002219static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2220 int attr)
2221{
2222 struct nlattr *rate;
2223 u16 bitrate;
2224
2225 rate = nla_nest_start(msg, attr);
2226 if (!rate)
2227 goto nla_put_failure;
2228
2229 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2230 bitrate = cfg80211_calculate_bitrate(info);
2231 if (bitrate > 0)
2232 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2233
2234 if (info->flags & RATE_INFO_FLAGS_MCS)
2235 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2236 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2237 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2238 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2239 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2240
2241 nla_nest_end(msg, rate);
2242 return true;
2243
2244nla_put_failure:
2245 return false;
2246}
2247
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002248static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2249 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002250 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002251{
2252 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002253 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002254
2255 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2256 if (!hdr)
2257 return -1;
2258
2259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2260 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2261
Johannes Bergf5ea9122009-08-07 16:17:38 +02002262 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2263
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002264 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2265 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002266 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302267 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2268 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2269 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002270 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2271 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2272 sinfo->inactive_time);
2273 if (sinfo->filled & STATION_INFO_RX_BYTES)
2274 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2275 sinfo->rx_bytes);
2276 if (sinfo->filled & STATION_INFO_TX_BYTES)
2277 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2278 sinfo->tx_bytes);
2279 if (sinfo->filled & STATION_INFO_LLID)
2280 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2281 sinfo->llid);
2282 if (sinfo->filled & STATION_INFO_PLID)
2283 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2284 sinfo->plid);
2285 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2286 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2287 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002288 if (sinfo->filled & STATION_INFO_SIGNAL)
2289 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2290 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002291 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2292 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2293 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002294 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002295 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2296 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002297 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002298 }
2299 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2300 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2301 NL80211_STA_INFO_RX_BITRATE))
2302 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002303 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002304 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2305 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2306 sinfo->rx_packets);
2307 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2308 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2309 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002310 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2311 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2312 sinfo->tx_retries);
2313 if (sinfo->filled & STATION_INFO_TX_FAILED)
2314 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2315 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002316 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2317 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2318 if (!bss_param)
2319 goto nla_put_failure;
2320
2321 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2322 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2323 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2324 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2325 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2326 NLA_PUT_FLAG(msg,
2327 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2328 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2329 sinfo->bss_param.dtim_period);
2330 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2331 sinfo->bss_param.beacon_interval);
2332
2333 nla_nest_end(msg, bss_param);
2334 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002336
Felix Fietkau040bdf72011-08-10 19:00:33 -06002337 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002338 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2339 sinfo->assoc_req_ies);
2340
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002341 return genlmsg_end(msg, hdr);
2342
2343 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002344 genlmsg_cancel(msg, hdr);
2345 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002346}
2347
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002348static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002349 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002350{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002351 struct station_info sinfo;
2352 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002353 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002354 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002355 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002356 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002357
Johannes Berg67748892010-10-04 21:14:06 +02002358 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2359 if (err)
2360 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002361
2362 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002363 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002364 goto out_err;
2365 }
2366
Johannes Bergbba95fe2008-07-29 13:22:51 +02002367 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002368 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002369 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2370 mac_addr, &sinfo);
2371 if (err == -ENOENT)
2372 break;
2373 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002374 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002375
2376 if (nl80211_send_station(skb,
2377 NETLINK_CB(cb->skb).pid,
2378 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2379 netdev, mac_addr,
2380 &sinfo) < 0)
2381 goto out;
2382
2383 sta_idx++;
2384 }
2385
2386
2387 out:
2388 cb->args[1] = sta_idx;
2389 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002390 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002391 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002392
2393 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002395
Johannes Berg5727ef12007-12-19 02:03:34 +01002396static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2397{
Johannes Berg4c476992010-10-04 21:36:35 +02002398 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2399 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002400 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002401 struct sk_buff *msg;
2402 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002403 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002404
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002405 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002406
2407 if (!info->attrs[NL80211_ATTR_MAC])
2408 return -EINVAL;
2409
2410 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2411
Johannes Berg4c476992010-10-04 21:36:35 +02002412 if (!rdev->ops->get_station)
2413 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002414
Johannes Berg79c97e92009-07-07 03:56:12 +02002415 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002416 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002417 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002418
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002420 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002421 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002422
2423 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002424 dev, mac_addr, &sinfo) < 0) {
2425 nlmsg_free(msg);
2426 return -ENOBUFS;
2427 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002428
Johannes Berg4c476992010-10-04 21:36:35 +02002429 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002430}
2431
2432/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002433 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002434 */
Johannes Berg463d0182009-07-14 00:33:35 +02002435static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002436 struct cfg80211_registered_device *rdev,
2437 struct net_device **vlan)
2438{
Johannes Berg463d0182009-07-14 00:33:35 +02002439 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002440 *vlan = NULL;
2441
2442 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002443 *vlan = dev_get_by_index(genl_info_net(info),
2444 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002445 if (!*vlan)
2446 return -ENODEV;
2447 if (!(*vlan)->ieee80211_ptr)
2448 return -EINVAL;
2449 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2450 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002451 if (!netif_running(*vlan))
2452 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002453 }
2454 return 0;
2455}
2456
2457static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2458{
Johannes Berg4c476992010-10-04 21:36:35 +02002459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002460 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002461 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002462 struct station_parameters params;
2463 u8 *mac_addr = NULL;
2464
2465 memset(&params, 0, sizeof(params));
2466
2467 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002468 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002469
2470 if (info->attrs[NL80211_ATTR_STA_AID])
2471 return -EINVAL;
2472
2473 if (!info->attrs[NL80211_ATTR_MAC])
2474 return -EINVAL;
2475
2476 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2477
2478 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2479 params.supported_rates =
2480 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2481 params.supported_rates_len =
2482 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2483 }
2484
2485 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2486 params.listen_interval =
2487 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2488
Jouni Malinen36aedc92008-08-25 11:58:58 +03002489 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2490 params.ht_capa =
2491 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2492
Johannes Bergeccb8e82009-05-11 21:57:56 +03002493 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002494 return -EINVAL;
2495
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002496 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2497 params.plink_action =
2498 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2499
Javier Cardona9c3990a2011-05-03 16:57:11 -07002500 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2501 params.plink_state =
2502 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2503
Johannes Berg463d0182009-07-14 00:33:35 +02002504 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002505 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002506 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002507
2508 /* validate settings */
2509 err = 0;
2510
2511 switch (dev->ieee80211_ptr->iftype) {
2512 case NL80211_IFTYPE_AP:
2513 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002514 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002515 /* disallow mesh-specific things */
2516 if (params.plink_action)
2517 err = -EINVAL;
2518 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002519 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002520 case NL80211_IFTYPE_STATION:
2521 /* disallow everything but AUTHORIZED flag */
2522 if (params.plink_action)
2523 err = -EINVAL;
2524 if (params.vlan)
2525 err = -EINVAL;
2526 if (params.supported_rates)
2527 err = -EINVAL;
2528 if (params.ht_capa)
2529 err = -EINVAL;
2530 if (params.listen_interval >= 0)
2531 err = -EINVAL;
2532 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2533 err = -EINVAL;
2534 break;
2535 case NL80211_IFTYPE_MESH_POINT:
2536 /* disallow things mesh doesn't support */
2537 if (params.vlan)
2538 err = -EINVAL;
2539 if (params.ht_capa)
2540 err = -EINVAL;
2541 if (params.listen_interval >= 0)
2542 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002543 if (params.sta_flags_mask &
2544 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002545 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002546 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002547 err = -EINVAL;
2548 break;
2549 default:
2550 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002551 }
2552
Johannes Berg5727ef12007-12-19 02:03:34 +01002553 if (err)
2554 goto out;
2555
Johannes Berg79c97e92009-07-07 03:56:12 +02002556 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002557 err = -EOPNOTSUPP;
2558 goto out;
2559 }
2560
Johannes Berg79c97e92009-07-07 03:56:12 +02002561 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002562
2563 out:
2564 if (params.vlan)
2565 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002566
Johannes Berg5727ef12007-12-19 02:03:34 +01002567 return err;
2568}
2569
Eliad Pellerc75786c2011-08-23 14:37:46 +03002570static struct nla_policy
2571nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2572 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2573 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2574};
2575
Johannes Berg5727ef12007-12-19 02:03:34 +01002576static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2577{
Johannes Berg4c476992010-10-04 21:36:35 +02002578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002579 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002580 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002581 struct station_parameters params;
2582 u8 *mac_addr = NULL;
2583
2584 memset(&params, 0, sizeof(params));
2585
2586 if (!info->attrs[NL80211_ATTR_MAC])
2587 return -EINVAL;
2588
Johannes Berg5727ef12007-12-19 02:03:34 +01002589 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2590 return -EINVAL;
2591
2592 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2593 return -EINVAL;
2594
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002595 if (!info->attrs[NL80211_ATTR_STA_AID])
2596 return -EINVAL;
2597
Johannes Berg5727ef12007-12-19 02:03:34 +01002598 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2599 params.supported_rates =
2600 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2601 params.supported_rates_len =
2602 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2603 params.listen_interval =
2604 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002605
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002606 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2607 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2608 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002609
Jouni Malinen36aedc92008-08-25 11:58:58 +03002610 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2611 params.ht_capa =
2612 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002613
Javier Cardona96b78df2011-04-07 15:08:33 -07002614 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2615 params.plink_action =
2616 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2617
Johannes Bergeccb8e82009-05-11 21:57:56 +03002618 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002619 return -EINVAL;
2620
Eliad Pellerc75786c2011-08-23 14:37:46 +03002621 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002622 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002623 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002624 info->attrs[NL80211_ATTR_STA_WME]) {
2625 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2626 struct nlattr *nla;
2627
2628 nla = info->attrs[NL80211_ATTR_STA_WME];
2629 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2630 nl80211_sta_wme_policy);
2631 if (err)
2632 return err;
2633
2634 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2635 params.uapsd_queues =
2636 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002637 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2638 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002639
2640 if (tb[NL80211_STA_WME_MAX_SP])
2641 params.max_sp =
2642 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002643
2644 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2645 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002646
2647 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002648 }
2649
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002650 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002651 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002652 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002653 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2654 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002655
Johannes Berg463d0182009-07-14 00:33:35 +02002656 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002657 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002658 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002659
2660 /* validate settings */
2661 err = 0;
2662
Johannes Berg79c97e92009-07-07 03:56:12 +02002663 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002664 err = -EOPNOTSUPP;
2665 goto out;
2666 }
2667
Johannes Berg79c97e92009-07-07 03:56:12 +02002668 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002669
2670 out:
2671 if (params.vlan)
2672 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002673 return err;
2674}
2675
2676static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2677{
Johannes Berg4c476992010-10-04 21:36:35 +02002678 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2679 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002680 u8 *mac_addr = NULL;
2681
2682 if (info->attrs[NL80211_ATTR_MAC])
2683 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2684
Johannes Berge80cf852009-05-11 14:43:13 +02002685 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002686 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002687 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002688 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2689 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002690
Johannes Berg4c476992010-10-04 21:36:35 +02002691 if (!rdev->ops->del_station)
2692 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002693
Johannes Berg4c476992010-10-04 21:36:35 +02002694 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002695}
2696
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002697static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2698 int flags, struct net_device *dev,
2699 u8 *dst, u8 *next_hop,
2700 struct mpath_info *pinfo)
2701{
2702 void *hdr;
2703 struct nlattr *pinfoattr;
2704
2705 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2706 if (!hdr)
2707 return -1;
2708
2709 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2710 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2711 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2712
Johannes Bergf5ea9122009-08-07 16:17:38 +02002713 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2714
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002715 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2716 if (!pinfoattr)
2717 goto nla_put_failure;
2718 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2719 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2720 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002721 if (pinfo->filled & MPATH_INFO_SN)
2722 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2723 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002724 if (pinfo->filled & MPATH_INFO_METRIC)
2725 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2726 pinfo->metric);
2727 if (pinfo->filled & MPATH_INFO_EXPTIME)
2728 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2729 pinfo->exptime);
2730 if (pinfo->filled & MPATH_INFO_FLAGS)
2731 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2732 pinfo->flags);
2733 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2734 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2735 pinfo->discovery_timeout);
2736 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2737 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2738 pinfo->discovery_retries);
2739
2740 nla_nest_end(msg, pinfoattr);
2741
2742 return genlmsg_end(msg, hdr);
2743
2744 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002745 genlmsg_cancel(msg, hdr);
2746 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002747}
2748
2749static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002750 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002751{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002752 struct mpath_info pinfo;
2753 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002754 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002755 u8 dst[ETH_ALEN];
2756 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002757 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002758 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002759
Johannes Berg67748892010-10-04 21:14:06 +02002760 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2761 if (err)
2762 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002763
2764 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002765 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002766 goto out_err;
2767 }
2768
Jouni Malineneec60b02009-03-20 21:21:19 +02002769 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2770 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002771 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002772 }
2773
Johannes Bergbba95fe2008-07-29 13:22:51 +02002774 while (1) {
2775 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2776 dst, next_hop, &pinfo);
2777 if (err == -ENOENT)
2778 break;
2779 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002780 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002781
2782 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2783 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2784 netdev, dst, next_hop,
2785 &pinfo) < 0)
2786 goto out;
2787
2788 path_idx++;
2789 }
2790
2791
2792 out:
2793 cb->args[1] = path_idx;
2794 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002795 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002796 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002797 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002798}
2799
2800static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2801{
Johannes Berg4c476992010-10-04 21:36:35 +02002802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002803 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002804 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002805 struct mpath_info pinfo;
2806 struct sk_buff *msg;
2807 u8 *dst = NULL;
2808 u8 next_hop[ETH_ALEN];
2809
2810 memset(&pinfo, 0, sizeof(pinfo));
2811
2812 if (!info->attrs[NL80211_ATTR_MAC])
2813 return -EINVAL;
2814
2815 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2816
Johannes Berg4c476992010-10-04 21:36:35 +02002817 if (!rdev->ops->get_mpath)
2818 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002819
Johannes Berg4c476992010-10-04 21:36:35 +02002820 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2821 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002822
Johannes Berg79c97e92009-07-07 03:56:12 +02002823 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002824 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002825 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002826
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002827 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002828 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002829 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002830
2831 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002832 dev, dst, next_hop, &pinfo) < 0) {
2833 nlmsg_free(msg);
2834 return -ENOBUFS;
2835 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002836
Johannes Berg4c476992010-10-04 21:36:35 +02002837 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002838}
2839
2840static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2841{
Johannes Berg4c476992010-10-04 21:36:35 +02002842 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2843 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002844 u8 *dst = NULL;
2845 u8 *next_hop = NULL;
2846
2847 if (!info->attrs[NL80211_ATTR_MAC])
2848 return -EINVAL;
2849
2850 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2851 return -EINVAL;
2852
2853 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2854 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2855
Johannes Berg4c476992010-10-04 21:36:35 +02002856 if (!rdev->ops->change_mpath)
2857 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002858
Johannes Berg4c476992010-10-04 21:36:35 +02002859 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2860 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002861
Johannes Berg4c476992010-10-04 21:36:35 +02002862 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002863}
Johannes Berg4c476992010-10-04 21:36:35 +02002864
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002865static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2866{
Johannes Berg4c476992010-10-04 21:36:35 +02002867 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2868 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002869 u8 *dst = NULL;
2870 u8 *next_hop = NULL;
2871
2872 if (!info->attrs[NL80211_ATTR_MAC])
2873 return -EINVAL;
2874
2875 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2876 return -EINVAL;
2877
2878 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2879 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2880
Johannes Berg4c476992010-10-04 21:36:35 +02002881 if (!rdev->ops->add_mpath)
2882 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002883
Johannes Berg4c476992010-10-04 21:36:35 +02002884 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2885 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002886
Johannes Berg4c476992010-10-04 21:36:35 +02002887 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002888}
2889
2890static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2891{
Johannes Berg4c476992010-10-04 21:36:35 +02002892 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2893 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002894 u8 *dst = NULL;
2895
2896 if (info->attrs[NL80211_ATTR_MAC])
2897 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2898
Johannes Berg4c476992010-10-04 21:36:35 +02002899 if (!rdev->ops->del_mpath)
2900 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002901
Johannes Berg4c476992010-10-04 21:36:35 +02002902 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002903}
2904
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002905static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2906{
Johannes Berg4c476992010-10-04 21:36:35 +02002907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2908 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002909 struct bss_parameters params;
2910
2911 memset(&params, 0, sizeof(params));
2912 /* default to not changing parameters */
2913 params.use_cts_prot = -1;
2914 params.use_short_preamble = -1;
2915 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002916 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002917 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002918
2919 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2920 params.use_cts_prot =
2921 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2922 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2923 params.use_short_preamble =
2924 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2925 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2926 params.use_short_slot_time =
2927 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002928 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2929 params.basic_rates =
2930 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2931 params.basic_rates_len =
2932 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2933 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002934 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2935 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002936 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2937 params.ht_opmode =
2938 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002939
Johannes Berg4c476992010-10-04 21:36:35 +02002940 if (!rdev->ops->change_bss)
2941 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002942
Johannes Berg074ac8d2010-09-16 14:58:22 +02002943 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002944 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2945 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002946
Johannes Berg4c476992010-10-04 21:36:35 +02002947 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002948}
2949
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002950static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002951 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2952 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2953 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2954 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2955 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2956 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2957};
2958
2959static int parse_reg_rule(struct nlattr *tb[],
2960 struct ieee80211_reg_rule *reg_rule)
2961{
2962 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2963 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2964
2965 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2966 return -EINVAL;
2967 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2968 return -EINVAL;
2969 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2970 return -EINVAL;
2971 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2972 return -EINVAL;
2973 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2974 return -EINVAL;
2975
2976 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2977
2978 freq_range->start_freq_khz =
2979 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2980 freq_range->end_freq_khz =
2981 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2982 freq_range->max_bandwidth_khz =
2983 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2984
2985 power_rule->max_eirp =
2986 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2987
2988 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2989 power_rule->max_antenna_gain =
2990 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2991
2992 return 0;
2993}
2994
2995static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2996{
2997 int r;
2998 char *data = NULL;
2999
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003000 /*
3001 * You should only get this when cfg80211 hasn't yet initialized
3002 * completely when built-in to the kernel right between the time
3003 * window between nl80211_init() and regulatory_init(), if that is
3004 * even possible.
3005 */
3006 mutex_lock(&cfg80211_mutex);
3007 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003008 mutex_unlock(&cfg80211_mutex);
3009 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003010 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003011 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003012
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003013 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3014 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003015
3016 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3017
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003018 r = regulatory_hint_user(data);
3019
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003020 return r;
3021}
3022
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003023static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003024 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003025{
Johannes Berg4c476992010-10-04 21:36:35 +02003026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003027 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003028 struct wireless_dev *wdev = dev->ieee80211_ptr;
3029 struct mesh_config cur_params;
3030 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003031 void *hdr;
3032 struct nlattr *pinfoattr;
3033 struct sk_buff *msg;
3034
Johannes Berg29cbe682010-12-03 09:20:44 +01003035 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3036 return -EOPNOTSUPP;
3037
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003038 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003039 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003040
Johannes Berg29cbe682010-12-03 09:20:44 +01003041 wdev_lock(wdev);
3042 /* If not connected, get default parameters */
3043 if (!wdev->mesh_id_len)
3044 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3045 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003046 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003047 &cur_params);
3048 wdev_unlock(wdev);
3049
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003050 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003051 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003052
3053 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003054 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003055 if (!msg)
3056 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003057 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003058 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003059 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003060 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003061 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003062 if (!pinfoattr)
3063 goto nla_put_failure;
3064 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3065 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3066 cur_params.dot11MeshRetryTimeout);
3067 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3068 cur_params.dot11MeshConfirmTimeout);
3069 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3070 cur_params.dot11MeshHoldingTimeout);
3071 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3072 cur_params.dot11MeshMaxPeerLinks);
3073 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3074 cur_params.dot11MeshMaxRetries);
3075 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3076 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003077 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3078 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003079 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3080 cur_params.auto_open_plinks);
3081 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3082 cur_params.dot11MeshHWMPmaxPREQretries);
3083 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3084 cur_params.path_refresh_time);
3085 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3086 cur_params.min_discovery_timeout);
3087 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3088 cur_params.dot11MeshHWMPactivePathTimeout);
3089 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3090 cur_params.dot11MeshHWMPpreqMinInterval);
3091 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3092 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003093 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3094 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003095 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3096 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003097 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3098 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003099 nla_nest_end(msg, pinfoattr);
3100 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003101 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003102
Johannes Berg3b858752009-03-12 09:55:09 +01003103 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003104 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003105 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003106 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003107 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003108}
3109
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003110static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003111 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3112 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3113 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3114 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3115 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3116 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003117 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003118 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3119
3120 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3121 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3122 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3123 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3124 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3125 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003126 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003127 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003128 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003129};
3130
Javier Cardonac80d5452010-12-16 17:37:49 -08003131static const struct nla_policy
3132 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3133 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3134 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003135 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003136 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003137 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003138 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003139};
3140
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003141static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003142 struct mesh_config *cfg,
3143 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003144{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003145 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003146 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003147
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003148#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3149do {\
3150 if (table[attr_num]) {\
3151 cfg->param = nla_fn(table[attr_num]); \
3152 mask |= (1 << (attr_num - 1)); \
3153 } \
3154} while (0);\
3155
3156
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003157 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003158 return -EINVAL;
3159 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003160 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003161 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003162 return -EINVAL;
3163
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003164 /* This makes sure that there aren't more than 32 mesh config
3165 * parameters (otherwise our bitfield scheme would not work.) */
3166 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3167
3168 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003169 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3170 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3171 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3172 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3173 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3174 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3175 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3176 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3177 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3178 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3179 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3180 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003181 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3182 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003183 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3184 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3185 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3186 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3187 nla_get_u8);
3188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3189 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3190 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3191 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3192 nla_get_u16);
3193 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3194 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3195 nla_get_u32);
3196 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3197 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3198 nla_get_u16);
3199 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3200 dot11MeshHWMPnetDiameterTraversalTime,
3201 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3202 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003203 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3204 dot11MeshHWMPRootMode, mask,
3205 NL80211_MESHCONF_HWMP_ROOTMODE,
3206 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003207 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3208 dot11MeshHWMPRannInterval, mask,
3209 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3210 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003211 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3212 dot11MeshGateAnnouncementProtocol, mask,
3213 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3214 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003215 if (mask_out)
3216 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003217
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003218 return 0;
3219
3220#undef FILL_IN_MESH_PARAM_IF_SET
3221}
3222
Javier Cardonac80d5452010-12-16 17:37:49 -08003223static int nl80211_parse_mesh_setup(struct genl_info *info,
3224 struct mesh_setup *setup)
3225{
3226 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3227
3228 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3229 return -EINVAL;
3230 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3231 info->attrs[NL80211_ATTR_MESH_SETUP],
3232 nl80211_mesh_setup_params_policy))
3233 return -EINVAL;
3234
3235 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3236 setup->path_sel_proto =
3237 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3238 IEEE80211_PATH_PROTOCOL_VENDOR :
3239 IEEE80211_PATH_PROTOCOL_HWMP;
3240
3241 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3242 setup->path_metric =
3243 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3244 IEEE80211_PATH_METRIC_VENDOR :
3245 IEEE80211_PATH_METRIC_AIRTIME;
3246
Javier Cardona581a8b02011-04-07 15:08:27 -07003247
3248 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003249 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003250 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003251 if (!is_valid_ie_attr(ieattr))
3252 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003253 setup->ie = nla_data(ieattr);
3254 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003255 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003256 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3257 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003258
3259 return 0;
3260}
3261
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003262static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003263 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003264{
3265 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3266 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003267 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003268 struct mesh_config cfg;
3269 u32 mask;
3270 int err;
3271
Johannes Berg29cbe682010-12-03 09:20:44 +01003272 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3273 return -EOPNOTSUPP;
3274
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003275 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003276 return -EOPNOTSUPP;
3277
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003278 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003279 if (err)
3280 return err;
3281
Johannes Berg29cbe682010-12-03 09:20:44 +01003282 wdev_lock(wdev);
3283 if (!wdev->mesh_id_len)
3284 err = -ENOLINK;
3285
3286 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003287 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003288 mask, &cfg);
3289
3290 wdev_unlock(wdev);
3291
3292 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003293}
3294
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003295static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3296{
3297 struct sk_buff *msg;
3298 void *hdr = NULL;
3299 struct nlattr *nl_reg_rules;
3300 unsigned int i;
3301 int err = -EINVAL;
3302
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003303 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003304
3305 if (!cfg80211_regdomain)
3306 goto out;
3307
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003308 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003309 if (!msg) {
3310 err = -ENOBUFS;
3311 goto out;
3312 }
3313
3314 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3315 NL80211_CMD_GET_REG);
3316 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003317 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003318
3319 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3320 cfg80211_regdomain->alpha2);
3321
3322 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3323 if (!nl_reg_rules)
3324 goto nla_put_failure;
3325
3326 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3327 struct nlattr *nl_reg_rule;
3328 const struct ieee80211_reg_rule *reg_rule;
3329 const struct ieee80211_freq_range *freq_range;
3330 const struct ieee80211_power_rule *power_rule;
3331
3332 reg_rule = &cfg80211_regdomain->reg_rules[i];
3333 freq_range = &reg_rule->freq_range;
3334 power_rule = &reg_rule->power_rule;
3335
3336 nl_reg_rule = nla_nest_start(msg, i);
3337 if (!nl_reg_rule)
3338 goto nla_put_failure;
3339
3340 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3341 reg_rule->flags);
3342 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3343 freq_range->start_freq_khz);
3344 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3345 freq_range->end_freq_khz);
3346 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3347 freq_range->max_bandwidth_khz);
3348 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3349 power_rule->max_antenna_gain);
3350 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3351 power_rule->max_eirp);
3352
3353 nla_nest_end(msg, nl_reg_rule);
3354 }
3355
3356 nla_nest_end(msg, nl_reg_rules);
3357
3358 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003359 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003360 goto out;
3361
3362nla_put_failure:
3363 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003364put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003365 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003366 err = -EMSGSIZE;
3367out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003368 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003369 return err;
3370}
3371
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003372static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3373{
3374 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3375 struct nlattr *nl_reg_rule;
3376 char *alpha2 = NULL;
3377 int rem_reg_rules = 0, r = 0;
3378 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3379 struct ieee80211_regdomain *rd = NULL;
3380
3381 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3382 return -EINVAL;
3383
3384 if (!info->attrs[NL80211_ATTR_REG_RULES])
3385 return -EINVAL;
3386
3387 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3388
3389 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3390 rem_reg_rules) {
3391 num_rules++;
3392 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003393 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003394 }
3395
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003396 mutex_lock(&cfg80211_mutex);
3397
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003398 if (!reg_is_valid_request(alpha2)) {
3399 r = -EINVAL;
3400 goto bad_reg;
3401 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003402
3403 size_of_regd = sizeof(struct ieee80211_regdomain) +
3404 (num_rules * sizeof(struct ieee80211_reg_rule));
3405
3406 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003407 if (!rd) {
3408 r = -ENOMEM;
3409 goto bad_reg;
3410 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003411
3412 rd->n_reg_rules = num_rules;
3413 rd->alpha2[0] = alpha2[0];
3414 rd->alpha2[1] = alpha2[1];
3415
3416 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3417 rem_reg_rules) {
3418 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3419 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3420 reg_rule_policy);
3421 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3422 if (r)
3423 goto bad_reg;
3424
3425 rule_idx++;
3426
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003427 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3428 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003429 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003430 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003431 }
3432
3433 BUG_ON(rule_idx != num_rules);
3434
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003435 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003436
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003437 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003438
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003439 return r;
3440
Johannes Bergd2372b32008-10-24 20:32:20 +02003441 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003442 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003443 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003444 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003445}
3446
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003447static int validate_scan_freqs(struct nlattr *freqs)
3448{
3449 struct nlattr *attr1, *attr2;
3450 int n_channels = 0, tmp1, tmp2;
3451
3452 nla_for_each_nested(attr1, freqs, tmp1) {
3453 n_channels++;
3454 /*
3455 * Some hardware has a limited channel list for
3456 * scanning, and it is pretty much nonsensical
3457 * to scan for a channel twice, so disallow that
3458 * and don't require drivers to check that the
3459 * channel list they get isn't longer than what
3460 * they can scan, as long as they can scan all
3461 * the channels they registered at once.
3462 */
3463 nla_for_each_nested(attr2, freqs, tmp2)
3464 if (attr1 != attr2 &&
3465 nla_get_u32(attr1) == nla_get_u32(attr2))
3466 return 0;
3467 }
3468
3469 return n_channels;
3470}
3471
Johannes Berg2a519312009-02-10 21:25:55 +01003472static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3473{
Johannes Berg4c476992010-10-04 21:36:35 +02003474 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3475 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003476 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003477 struct nlattr *attr;
3478 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003479 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003480 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003481
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003482 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3483 return -EINVAL;
3484
Johannes Berg79c97e92009-07-07 03:56:12 +02003485 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003486
Johannes Berg4c476992010-10-04 21:36:35 +02003487 if (!rdev->ops->scan)
3488 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003489
Johannes Berg4c476992010-10-04 21:36:35 +02003490 if (rdev->scan_req)
3491 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003492
3493 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003494 n_channels = validate_scan_freqs(
3495 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003496 if (!n_channels)
3497 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003498 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003499 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003500 n_channels = 0;
3501
Johannes Berg2a519312009-02-10 21:25:55 +01003502 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3503 if (wiphy->bands[band])
3504 n_channels += wiphy->bands[band]->n_channels;
3505 }
3506
3507 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3508 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3509 n_ssids++;
3510
Johannes Berg4c476992010-10-04 21:36:35 +02003511 if (n_ssids > wiphy->max_scan_ssids)
3512 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003513
Jouni Malinen70692ad2009-02-16 19:39:13 +02003514 if (info->attrs[NL80211_ATTR_IE])
3515 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3516 else
3517 ie_len = 0;
3518
Johannes Berg4c476992010-10-04 21:36:35 +02003519 if (ie_len > wiphy->max_scan_ie_len)
3520 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003521
Johannes Berg2a519312009-02-10 21:25:55 +01003522 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003523 + sizeof(*request->ssids) * n_ssids
3524 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003525 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003526 if (!request)
3527 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003528
Johannes Berg2a519312009-02-10 21:25:55 +01003529 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003530 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003531 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003532 if (ie_len) {
3533 if (request->ssids)
3534 request->ie = (void *)(request->ssids + n_ssids);
3535 else
3536 request->ie = (void *)(request->channels + n_channels);
3537 }
Johannes Berg2a519312009-02-10 21:25:55 +01003538
Johannes Berg584991d2009-11-02 13:32:03 +01003539 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003540 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3541 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003542 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003543 struct ieee80211_channel *chan;
3544
3545 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3546
3547 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003548 err = -EINVAL;
3549 goto out_free;
3550 }
Johannes Berg584991d2009-11-02 13:32:03 +01003551
3552 /* ignore disabled channels */
3553 if (chan->flags & IEEE80211_CHAN_DISABLED)
3554 continue;
3555
3556 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003557 i++;
3558 }
3559 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003560 enum ieee80211_band band;
3561
Johannes Berg2a519312009-02-10 21:25:55 +01003562 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003563 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3564 int j;
3565 if (!wiphy->bands[band])
3566 continue;
3567 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003568 struct ieee80211_channel *chan;
3569
3570 chan = &wiphy->bands[band]->channels[j];
3571
3572 if (chan->flags & IEEE80211_CHAN_DISABLED)
3573 continue;
3574
3575 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003576 i++;
3577 }
3578 }
3579 }
3580
Johannes Berg584991d2009-11-02 13:32:03 +01003581 if (!i) {
3582 err = -EINVAL;
3583 goto out_free;
3584 }
3585
3586 request->n_channels = i;
3587
Johannes Berg2a519312009-02-10 21:25:55 +01003588 i = 0;
3589 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3590 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003591 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003592 err = -EINVAL;
3593 goto out_free;
3594 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003595 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003596 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003597 i++;
3598 }
3599 }
3600
Jouni Malinen70692ad2009-02-16 19:39:13 +02003601 if (info->attrs[NL80211_ATTR_IE]) {
3602 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003603 memcpy((void *)request->ie,
3604 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003605 request->ie_len);
3606 }
3607
Johannes Berg34850ab2011-07-18 18:08:35 +02003608 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003609 if (wiphy->bands[i])
3610 request->rates[i] =
3611 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003612
3613 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3614 nla_for_each_nested(attr,
3615 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3616 tmp) {
3617 enum ieee80211_band band = nla_type(attr);
3618
Dan Carpenter84404622011-07-29 11:52:18 +03003619 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003620 err = -EINVAL;
3621 goto out_free;
3622 }
3623 err = ieee80211_get_ratemask(wiphy->bands[band],
3624 nla_data(attr),
3625 nla_len(attr),
3626 &request->rates[band]);
3627 if (err)
3628 goto out_free;
3629 }
3630 }
3631
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303632 request->no_cck =
3633 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3634
Johannes Berg463d0182009-07-14 00:33:35 +02003635 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003636 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003637
Johannes Berg79c97e92009-07-07 03:56:12 +02003638 rdev->scan_req = request;
3639 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003640
Johannes Berg463d0182009-07-14 00:33:35 +02003641 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003642 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003643 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003644 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003645 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003646 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003647 kfree(request);
3648 }
Johannes Berg3b858752009-03-12 09:55:09 +01003649
Johannes Berg2a519312009-02-10 21:25:55 +01003650 return err;
3651}
3652
Luciano Coelho807f8a82011-05-11 17:09:35 +03003653static int nl80211_start_sched_scan(struct sk_buff *skb,
3654 struct genl_info *info)
3655{
3656 struct cfg80211_sched_scan_request *request;
3657 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3658 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003659 struct nlattr *attr;
3660 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003661 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003662 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003663 enum ieee80211_band band;
3664 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003665 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003666
3667 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3668 !rdev->ops->sched_scan_start)
3669 return -EOPNOTSUPP;
3670
3671 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3672 return -EINVAL;
3673
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003674 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3675 return -EINVAL;
3676
3677 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3678 if (interval == 0)
3679 return -EINVAL;
3680
Luciano Coelho807f8a82011-05-11 17:09:35 +03003681 wiphy = &rdev->wiphy;
3682
3683 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3684 n_channels = validate_scan_freqs(
3685 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3686 if (!n_channels)
3687 return -EINVAL;
3688 } else {
3689 n_channels = 0;
3690
3691 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3692 if (wiphy->bands[band])
3693 n_channels += wiphy->bands[band]->n_channels;
3694 }
3695
3696 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3697 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3698 tmp)
3699 n_ssids++;
3700
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003701 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003702 return -EINVAL;
3703
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003704 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3705 nla_for_each_nested(attr,
3706 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3707 tmp)
3708 n_match_sets++;
3709
3710 if (n_match_sets > wiphy->max_match_sets)
3711 return -EINVAL;
3712
Luciano Coelho807f8a82011-05-11 17:09:35 +03003713 if (info->attrs[NL80211_ATTR_IE])
3714 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3715 else
3716 ie_len = 0;
3717
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003718 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003719 return -EINVAL;
3720
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003721 mutex_lock(&rdev->sched_scan_mtx);
3722
3723 if (rdev->sched_scan_req) {
3724 err = -EINPROGRESS;
3725 goto out;
3726 }
3727
Luciano Coelho807f8a82011-05-11 17:09:35 +03003728 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003729 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003730 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003731 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003732 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003733 if (!request) {
3734 err = -ENOMEM;
3735 goto out;
3736 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003737
3738 if (n_ssids)
3739 request->ssids = (void *)&request->channels[n_channels];
3740 request->n_ssids = n_ssids;
3741 if (ie_len) {
3742 if (request->ssids)
3743 request->ie = (void *)(request->ssids + n_ssids);
3744 else
3745 request->ie = (void *)(request->channels + n_channels);
3746 }
3747
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003748 if (n_match_sets) {
3749 if (request->ie)
3750 request->match_sets = (void *)(request->ie + ie_len);
3751 else if (request->ssids)
3752 request->match_sets =
3753 (void *)(request->ssids + n_ssids);
3754 else
3755 request->match_sets =
3756 (void *)(request->channels + n_channels);
3757 }
3758 request->n_match_sets = n_match_sets;
3759
Luciano Coelho807f8a82011-05-11 17:09:35 +03003760 i = 0;
3761 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3762 /* user specified, bail out if channel not found */
3763 nla_for_each_nested(attr,
3764 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3765 tmp) {
3766 struct ieee80211_channel *chan;
3767
3768 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3769
3770 if (!chan) {
3771 err = -EINVAL;
3772 goto out_free;
3773 }
3774
3775 /* ignore disabled channels */
3776 if (chan->flags & IEEE80211_CHAN_DISABLED)
3777 continue;
3778
3779 request->channels[i] = chan;
3780 i++;
3781 }
3782 } else {
3783 /* all channels */
3784 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3785 int j;
3786 if (!wiphy->bands[band])
3787 continue;
3788 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3789 struct ieee80211_channel *chan;
3790
3791 chan = &wiphy->bands[band]->channels[j];
3792
3793 if (chan->flags & IEEE80211_CHAN_DISABLED)
3794 continue;
3795
3796 request->channels[i] = chan;
3797 i++;
3798 }
3799 }
3800 }
3801
3802 if (!i) {
3803 err = -EINVAL;
3804 goto out_free;
3805 }
3806
3807 request->n_channels = i;
3808
3809 i = 0;
3810 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3811 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3812 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003813 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003814 err = -EINVAL;
3815 goto out_free;
3816 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003817 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003818 memcpy(request->ssids[i].ssid, nla_data(attr),
3819 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003820 i++;
3821 }
3822 }
3823
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003824 i = 0;
3825 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3826 nla_for_each_nested(attr,
3827 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3828 tmp) {
3829 struct nlattr *ssid;
3830
3831 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3832 nla_data(attr), nla_len(attr),
3833 nl80211_match_policy);
3834 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3835 if (ssid) {
3836 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3837 err = -EINVAL;
3838 goto out_free;
3839 }
3840 memcpy(request->match_sets[i].ssid.ssid,
3841 nla_data(ssid), nla_len(ssid));
3842 request->match_sets[i].ssid.ssid_len =
3843 nla_len(ssid);
3844 }
3845 i++;
3846 }
3847 }
3848
Luciano Coelho807f8a82011-05-11 17:09:35 +03003849 if (info->attrs[NL80211_ATTR_IE]) {
3850 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3851 memcpy((void *)request->ie,
3852 nla_data(info->attrs[NL80211_ATTR_IE]),
3853 request->ie_len);
3854 }
3855
3856 request->dev = dev;
3857 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003858 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003859
3860 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3861 if (!err) {
3862 rdev->sched_scan_req = request;
3863 nl80211_send_sched_scan(rdev, dev,
3864 NL80211_CMD_START_SCHED_SCAN);
3865 goto out;
3866 }
3867
3868out_free:
3869 kfree(request);
3870out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003871 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003872 return err;
3873}
3874
3875static int nl80211_stop_sched_scan(struct sk_buff *skb,
3876 struct genl_info *info)
3877{
3878 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003879 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003880
3881 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3882 !rdev->ops->sched_scan_stop)
3883 return -EOPNOTSUPP;
3884
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003885 mutex_lock(&rdev->sched_scan_mtx);
3886 err = __cfg80211_stop_sched_scan(rdev, false);
3887 mutex_unlock(&rdev->sched_scan_mtx);
3888
3889 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003890}
3891
Johannes Berg9720bb32011-06-21 09:45:33 +02003892static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3893 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003894 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003895 struct wireless_dev *wdev,
3896 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003897{
Johannes Berg48ab9052009-07-10 18:42:31 +02003898 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003899 void *hdr;
3900 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003901 int i;
3902
3903 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003904
Johannes Berg9720bb32011-06-21 09:45:33 +02003905 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003906 NL80211_CMD_NEW_SCAN_RESULTS);
3907 if (!hdr)
3908 return -1;
3909
Johannes Berg9720bb32011-06-21 09:45:33 +02003910 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3911
Johannes Bergf5ea9122009-08-07 16:17:38 +02003912 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003913 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003914
3915 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3916 if (!bss)
3917 goto nla_put_failure;
3918 if (!is_zero_ether_addr(res->bssid))
3919 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3920 if (res->information_elements && res->len_information_elements)
3921 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3922 res->len_information_elements,
3923 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003924 if (res->beacon_ies && res->len_beacon_ies &&
3925 res->beacon_ies != res->information_elements)
3926 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3927 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003928 if (res->tsf)
3929 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3930 if (res->beacon_interval)
3931 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3932 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3933 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003934 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3935 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003936
Johannes Berg77965c92009-02-18 18:45:06 +01003937 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003938 case CFG80211_SIGNAL_TYPE_MBM:
3939 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3940 break;
3941 case CFG80211_SIGNAL_TYPE_UNSPEC:
3942 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3943 break;
3944 default:
3945 break;
3946 }
3947
Johannes Berg48ab9052009-07-10 18:42:31 +02003948 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003949 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003950 case NL80211_IFTYPE_STATION:
3951 if (intbss == wdev->current_bss)
3952 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3953 NL80211_BSS_STATUS_ASSOCIATED);
3954 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3955 if (intbss != wdev->auth_bsses[i])
3956 continue;
3957 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3958 NL80211_BSS_STATUS_AUTHENTICATED);
3959 break;
3960 }
3961 break;
3962 case NL80211_IFTYPE_ADHOC:
3963 if (intbss == wdev->current_bss)
3964 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3965 NL80211_BSS_STATUS_IBSS_JOINED);
3966 break;
3967 default:
3968 break;
3969 }
3970
Johannes Berg2a519312009-02-10 21:25:55 +01003971 nla_nest_end(msg, bss);
3972
3973 return genlmsg_end(msg, hdr);
3974
3975 nla_put_failure:
3976 genlmsg_cancel(msg, hdr);
3977 return -EMSGSIZE;
3978}
3979
3980static int nl80211_dump_scan(struct sk_buff *skb,
3981 struct netlink_callback *cb)
3982{
Johannes Berg48ab9052009-07-10 18:42:31 +02003983 struct cfg80211_registered_device *rdev;
3984 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003985 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003986 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003987 int start = cb->args[1], idx = 0;
3988 int err;
3989
Johannes Berg67748892010-10-04 21:14:06 +02003990 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3991 if (err)
3992 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003993
Johannes Berg48ab9052009-07-10 18:42:31 +02003994 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003995
Johannes Berg48ab9052009-07-10 18:42:31 +02003996 wdev_lock(wdev);
3997 spin_lock_bh(&rdev->bss_lock);
3998 cfg80211_bss_expire(rdev);
3999
Johannes Berg9720bb32011-06-21 09:45:33 +02004000 cb->seq = rdev->bss_generation;
4001
Johannes Berg48ab9052009-07-10 18:42:31 +02004002 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004003 if (++idx <= start)
4004 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004005 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004006 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004007 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004008 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004009 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004010 }
4011 }
4012
Johannes Berg48ab9052009-07-10 18:42:31 +02004013 spin_unlock_bh(&rdev->bss_lock);
4014 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004015
4016 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004017 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004018
Johannes Berg67748892010-10-04 21:14:06 +02004019 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004020}
4021
Holger Schurig61fa7132009-11-11 12:25:40 +01004022static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4023 int flags, struct net_device *dev,
4024 struct survey_info *survey)
4025{
4026 void *hdr;
4027 struct nlattr *infoattr;
4028
Holger Schurig61fa7132009-11-11 12:25:40 +01004029 hdr = nl80211hdr_put(msg, pid, seq, flags,
4030 NL80211_CMD_NEW_SURVEY_RESULTS);
4031 if (!hdr)
4032 return -ENOMEM;
4033
4034 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4035
4036 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4037 if (!infoattr)
4038 goto nla_put_failure;
4039
4040 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4041 survey->channel->center_freq);
4042 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4043 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4044 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004045 if (survey->filled & SURVEY_INFO_IN_USE)
4046 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c29a2010-10-09 02:39:29 +02004047 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4048 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4049 survey->channel_time);
4050 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4051 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4052 survey->channel_time_busy);
4053 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4054 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4055 survey->channel_time_ext_busy);
4056 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4057 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4058 survey->channel_time_rx);
4059 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4060 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4061 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004062
4063 nla_nest_end(msg, infoattr);
4064
4065 return genlmsg_end(msg, hdr);
4066
4067 nla_put_failure:
4068 genlmsg_cancel(msg, hdr);
4069 return -EMSGSIZE;
4070}
4071
4072static int nl80211_dump_survey(struct sk_buff *skb,
4073 struct netlink_callback *cb)
4074{
4075 struct survey_info survey;
4076 struct cfg80211_registered_device *dev;
4077 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004078 int survey_idx = cb->args[1];
4079 int res;
4080
Johannes Berg67748892010-10-04 21:14:06 +02004081 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4082 if (res)
4083 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004084
4085 if (!dev->ops->dump_survey) {
4086 res = -EOPNOTSUPP;
4087 goto out_err;
4088 }
4089
4090 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004091 struct ieee80211_channel *chan;
4092
Holger Schurig61fa7132009-11-11 12:25:40 +01004093 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4094 &survey);
4095 if (res == -ENOENT)
4096 break;
4097 if (res)
4098 goto out_err;
4099
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004100 /* Survey without a channel doesn't make sense */
4101 if (!survey.channel) {
4102 res = -EINVAL;
4103 goto out;
4104 }
4105
4106 chan = ieee80211_get_channel(&dev->wiphy,
4107 survey.channel->center_freq);
4108 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4109 survey_idx++;
4110 continue;
4111 }
4112
Holger Schurig61fa7132009-11-11 12:25:40 +01004113 if (nl80211_send_survey(skb,
4114 NETLINK_CB(cb->skb).pid,
4115 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4116 netdev,
4117 &survey) < 0)
4118 goto out;
4119 survey_idx++;
4120 }
4121
4122 out:
4123 cb->args[1] = survey_idx;
4124 res = skb->len;
4125 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004126 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004127 return res;
4128}
4129
Jouni Malinen255e7372009-03-20 21:21:17 +02004130static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4131{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004132 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004133}
4134
Samuel Ortizb23aa672009-07-01 21:26:54 +02004135static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4136{
4137 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4138 NL80211_WPA_VERSION_2));
4139}
4140
Jouni Malinen636a5d32009-03-19 13:39:22 +02004141static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4142{
Johannes Berg4c476992010-10-04 21:36:35 +02004143 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4144 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004145 struct ieee80211_channel *chan;
4146 const u8 *bssid, *ssid, *ie = NULL;
4147 int err, ssid_len, ie_len = 0;
4148 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004149 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004150 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004151
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004152 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4153 return -EINVAL;
4154
4155 if (!info->attrs[NL80211_ATTR_MAC])
4156 return -EINVAL;
4157
Jouni Malinen17780922009-03-27 20:52:47 +02004158 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4159 return -EINVAL;
4160
Johannes Berg19957bb2009-07-02 17:20:43 +02004161 if (!info->attrs[NL80211_ATTR_SSID])
4162 return -EINVAL;
4163
4164 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4165 return -EINVAL;
4166
Johannes Bergfffd0932009-07-08 14:22:54 +02004167 err = nl80211_parse_key(info, &key);
4168 if (err)
4169 return err;
4170
4171 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004172 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4173 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004174 if (!key.p.key || !key.p.key_len)
4175 return -EINVAL;
4176 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4177 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4178 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4179 key.p.key_len != WLAN_KEY_LEN_WEP104))
4180 return -EINVAL;
4181 if (key.idx > 4)
4182 return -EINVAL;
4183 } else {
4184 key.p.key_len = 0;
4185 key.p.key = NULL;
4186 }
4187
Johannes Bergafea0b72010-08-10 09:46:42 +02004188 if (key.idx >= 0) {
4189 int i;
4190 bool ok = false;
4191 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4192 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4193 ok = true;
4194 break;
4195 }
4196 }
Johannes Berg4c476992010-10-04 21:36:35 +02004197 if (!ok)
4198 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004199 }
4200
Johannes Berg4c476992010-10-04 21:36:35 +02004201 if (!rdev->ops->auth)
4202 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004203
Johannes Berg074ac8d2010-09-16 14:58:22 +02004204 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004205 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4206 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004207
Johannes Berg19957bb2009-07-02 17:20:43 +02004208 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004209 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004210 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004211 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4212 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004213
Johannes Berg19957bb2009-07-02 17:20:43 +02004214 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4215 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4216
4217 if (info->attrs[NL80211_ATTR_IE]) {
4218 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4219 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4220 }
4221
4222 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004223 if (!nl80211_valid_auth_type(auth_type))
4224 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004225
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004226 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4227
Johannes Berg4c476992010-10-04 21:36:35 +02004228 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4229 ssid, ssid_len, ie, ie_len,
4230 key.p.key, key.p.key_len, key.idx,
4231 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004232}
4233
Johannes Bergc0692b82010-08-27 14:26:53 +03004234static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4235 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004236 struct cfg80211_crypto_settings *settings,
4237 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004238{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004239 memset(settings, 0, sizeof(*settings));
4240
Samuel Ortizb23aa672009-07-01 21:26:54 +02004241 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4242
Johannes Bergc0692b82010-08-27 14:26:53 +03004243 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4244 u16 proto;
4245 proto = nla_get_u16(
4246 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4247 settings->control_port_ethertype = cpu_to_be16(proto);
4248 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4249 proto != ETH_P_PAE)
4250 return -EINVAL;
4251 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4252 settings->control_port_no_encrypt = true;
4253 } else
4254 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4255
Samuel Ortizb23aa672009-07-01 21:26:54 +02004256 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4257 void *data;
4258 int len, i;
4259
4260 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4261 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4262 settings->n_ciphers_pairwise = len / sizeof(u32);
4263
4264 if (len % sizeof(u32))
4265 return -EINVAL;
4266
Johannes Berg3dc27d22009-07-02 21:36:37 +02004267 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004268 return -EINVAL;
4269
4270 memcpy(settings->ciphers_pairwise, data, len);
4271
4272 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004273 if (!cfg80211_supported_cipher_suite(
4274 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004275 settings->ciphers_pairwise[i]))
4276 return -EINVAL;
4277 }
4278
4279 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4280 settings->cipher_group =
4281 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004282 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4283 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004284 return -EINVAL;
4285 }
4286
4287 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4288 settings->wpa_versions =
4289 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4290 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4291 return -EINVAL;
4292 }
4293
4294 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4295 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004296 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004297
4298 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4299 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4300 settings->n_akm_suites = len / sizeof(u32);
4301
4302 if (len % sizeof(u32))
4303 return -EINVAL;
4304
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004305 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4306 return -EINVAL;
4307
Samuel Ortizb23aa672009-07-01 21:26:54 +02004308 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004309 }
4310
4311 return 0;
4312}
4313
Jouni Malinen636a5d32009-03-19 13:39:22 +02004314static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4315{
Johannes Berg4c476992010-10-04 21:36:35 +02004316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4317 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004318 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004319 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004320 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004321 int err, ssid_len, ie_len = 0;
4322 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004323
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004324 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4325 return -EINVAL;
4326
4327 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004328 !info->attrs[NL80211_ATTR_SSID] ||
4329 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004330 return -EINVAL;
4331
Johannes Berg4c476992010-10-04 21:36:35 +02004332 if (!rdev->ops->assoc)
4333 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004334
Johannes Berg074ac8d2010-09-16 14:58:22 +02004335 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004336 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4337 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004338
Johannes Berg19957bb2009-07-02 17:20:43 +02004339 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004340
Johannes Berg19957bb2009-07-02 17:20:43 +02004341 chan = ieee80211_get_channel(&rdev->wiphy,
4342 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004343 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4344 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004345
Johannes Berg19957bb2009-07-02 17:20:43 +02004346 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4347 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
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 Malinendc6382c2009-05-06 22:09:37 +03004354 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004355 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004356 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004357 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004358 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004359 else if (mfp != NL80211_MFP_NO)
4360 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004361 }
4362
Johannes Berg3e5d7642009-07-07 14:37:26 +02004363 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4364 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4365
Johannes Bergc0692b82010-08-27 14:26:53 +03004366 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004367 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004368 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4369 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004370 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004371
Jouni Malinen636a5d32009-03-19 13:39:22 +02004372 return err;
4373}
4374
4375static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4376{
Johannes Berg4c476992010-10-04 21:36:35 +02004377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4378 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004379 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004380 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004381 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004382 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004383
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004384 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4385 return -EINVAL;
4386
4387 if (!info->attrs[NL80211_ATTR_MAC])
4388 return -EINVAL;
4389
4390 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4391 return -EINVAL;
4392
Johannes Berg4c476992010-10-04 21:36:35 +02004393 if (!rdev->ops->deauth)
4394 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004395
Johannes Berg074ac8d2010-09-16 14:58:22 +02004396 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004397 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4398 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004399
Johannes Berg19957bb2009-07-02 17:20:43 +02004400 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004401
Johannes Berg19957bb2009-07-02 17:20:43 +02004402 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4403 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004404 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004405 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004406 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004407
4408 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004409 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4410 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004411 }
4412
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004413 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4414
Johannes Berg4c476992010-10-04 21:36:35 +02004415 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4416 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004417}
4418
4419static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4420{
Johannes Berg4c476992010-10-04 21:36:35 +02004421 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4422 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004423 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004424 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004425 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004426 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004427
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004428 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4429 return -EINVAL;
4430
4431 if (!info->attrs[NL80211_ATTR_MAC])
4432 return -EINVAL;
4433
4434 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4435 return -EINVAL;
4436
Johannes Berg4c476992010-10-04 21:36:35 +02004437 if (!rdev->ops->disassoc)
4438 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004439
Johannes Berg074ac8d2010-09-16 14:58:22 +02004440 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004441 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4442 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004443
Johannes Berg19957bb2009-07-02 17:20:43 +02004444 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004445
Johannes Berg19957bb2009-07-02 17:20:43 +02004446 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4447 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004448 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004449 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004450 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004451
4452 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004453 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4454 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004455 }
4456
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004457 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4458
Johannes Berg4c476992010-10-04 21:36:35 +02004459 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4460 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004461}
4462
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004463static bool
4464nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4465 int mcast_rate[IEEE80211_NUM_BANDS],
4466 int rateval)
4467{
4468 struct wiphy *wiphy = &rdev->wiphy;
4469 bool found = false;
4470 int band, i;
4471
4472 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4473 struct ieee80211_supported_band *sband;
4474
4475 sband = wiphy->bands[band];
4476 if (!sband)
4477 continue;
4478
4479 for (i = 0; i < sband->n_bitrates; i++) {
4480 if (sband->bitrates[i].bitrate == rateval) {
4481 mcast_rate[band] = i + 1;
4482 found = true;
4483 break;
4484 }
4485 }
4486 }
4487
4488 return found;
4489}
4490
Johannes Berg04a773a2009-04-19 21:24:32 +02004491static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4494 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004495 struct cfg80211_ibss_params ibss;
4496 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004497 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004498 int err;
4499
Johannes Berg8e30bc52009-04-22 17:45:38 +02004500 memset(&ibss, 0, sizeof(ibss));
4501
Johannes Berg04a773a2009-04-19 21:24:32 +02004502 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4503 return -EINVAL;
4504
4505 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4506 !info->attrs[NL80211_ATTR_SSID] ||
4507 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4508 return -EINVAL;
4509
Johannes Berg8e30bc52009-04-22 17:45:38 +02004510 ibss.beacon_interval = 100;
4511
4512 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4513 ibss.beacon_interval =
4514 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4515 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4516 return -EINVAL;
4517 }
4518
Johannes Berg4c476992010-10-04 21:36:35 +02004519 if (!rdev->ops->join_ibss)
4520 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004521
Johannes Berg4c476992010-10-04 21:36:35 +02004522 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4523 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004524
Johannes Berg79c97e92009-07-07 03:56:12 +02004525 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004526
Johannes Berg39193492011-09-16 13:45:25 +02004527 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004528 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004529
4530 if (!is_valid_ether_addr(ibss.bssid))
4531 return -EINVAL;
4532 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004533 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4534 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4535
4536 if (info->attrs[NL80211_ATTR_IE]) {
4537 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4538 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4539 }
4540
4541 ibss.channel = ieee80211_get_channel(wiphy,
4542 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4543 if (!ibss.channel ||
4544 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004545 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4546 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004547
4548 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004549 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004550
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004551 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4552 u8 *rates =
4553 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4554 int n_rates =
4555 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4556 struct ieee80211_supported_band *sband =
4557 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004558 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004559
Johannes Berg34850ab2011-07-18 18:08:35 +02004560 err = ieee80211_get_ratemask(sband, rates, n_rates,
4561 &ibss.basic_rates);
4562 if (err)
4563 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004564 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004565
4566 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4567 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4568 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4569 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004570
Johannes Berg4c476992010-10-04 21:36:35 +02004571 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4572 connkeys = nl80211_parse_connkeys(rdev,
4573 info->attrs[NL80211_ATTR_KEYS]);
4574 if (IS_ERR(connkeys))
4575 return PTR_ERR(connkeys);
4576 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004577
Johannes Berg4c476992010-10-04 21:36:35 +02004578 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004579 if (err)
4580 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004581 return err;
4582}
4583
4584static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4585{
Johannes Berg4c476992010-10-04 21:36:35 +02004586 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4587 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004588
Johannes Berg4c476992010-10-04 21:36:35 +02004589 if (!rdev->ops->leave_ibss)
4590 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004591
Johannes Berg4c476992010-10-04 21:36:35 +02004592 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4593 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004594
Johannes Berg4c476992010-10-04 21:36:35 +02004595 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004596}
4597
Johannes Bergaff89a92009-07-01 21:26:51 +02004598#ifdef CONFIG_NL80211_TESTMODE
4599static struct genl_multicast_group nl80211_testmode_mcgrp = {
4600 .name = "testmode",
4601};
4602
4603static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4604{
Johannes Berg4c476992010-10-04 21:36:35 +02004605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004606 int err;
4607
4608 if (!info->attrs[NL80211_ATTR_TESTDATA])
4609 return -EINVAL;
4610
Johannes Bergaff89a92009-07-01 21:26:51 +02004611 err = -EOPNOTSUPP;
4612 if (rdev->ops->testmode_cmd) {
4613 rdev->testmode_info = info;
4614 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4615 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4616 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4617 rdev->testmode_info = NULL;
4618 }
4619
Johannes Bergaff89a92009-07-01 21:26:51 +02004620 return err;
4621}
4622
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004623static int nl80211_testmode_dump(struct sk_buff *skb,
4624 struct netlink_callback *cb)
4625{
4626 struct cfg80211_registered_device *dev;
4627 int err;
4628 long phy_idx;
4629 void *data = NULL;
4630 int data_len = 0;
4631
4632 if (cb->args[0]) {
4633 /*
4634 * 0 is a valid index, but not valid for args[0],
4635 * so we need to offset by 1.
4636 */
4637 phy_idx = cb->args[0] - 1;
4638 } else {
4639 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4640 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4641 nl80211_policy);
4642 if (err)
4643 return err;
4644 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4645 return -EINVAL;
4646 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4647 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4648 cb->args[1] =
4649 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4650 }
4651
4652 if (cb->args[1]) {
4653 data = nla_data((void *)cb->args[1]);
4654 data_len = nla_len((void *)cb->args[1]);
4655 }
4656
4657 mutex_lock(&cfg80211_mutex);
4658 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4659 if (!dev) {
4660 mutex_unlock(&cfg80211_mutex);
4661 return -ENOENT;
4662 }
4663 cfg80211_lock_rdev(dev);
4664 mutex_unlock(&cfg80211_mutex);
4665
4666 if (!dev->ops->testmode_dump) {
4667 err = -EOPNOTSUPP;
4668 goto out_err;
4669 }
4670
4671 while (1) {
4672 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4673 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4674 NL80211_CMD_TESTMODE);
4675 struct nlattr *tmdata;
4676
4677 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4678 genlmsg_cancel(skb, hdr);
4679 break;
4680 }
4681
4682 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4683 if (!tmdata) {
4684 genlmsg_cancel(skb, hdr);
4685 break;
4686 }
4687 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4688 data, data_len);
4689 nla_nest_end(skb, tmdata);
4690
4691 if (err == -ENOBUFS || err == -ENOENT) {
4692 genlmsg_cancel(skb, hdr);
4693 break;
4694 } else if (err) {
4695 genlmsg_cancel(skb, hdr);
4696 goto out_err;
4697 }
4698
4699 genlmsg_end(skb, hdr);
4700 }
4701
4702 err = skb->len;
4703 /* see above */
4704 cb->args[0] = phy_idx + 1;
4705 out_err:
4706 cfg80211_unlock_rdev(dev);
4707 return err;
4708}
4709
Johannes Bergaff89a92009-07-01 21:26:51 +02004710static struct sk_buff *
4711__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4712 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4713{
4714 struct sk_buff *skb;
4715 void *hdr;
4716 struct nlattr *data;
4717
4718 skb = nlmsg_new(approxlen + 100, gfp);
4719 if (!skb)
4720 return NULL;
4721
4722 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4723 if (!hdr) {
4724 kfree_skb(skb);
4725 return NULL;
4726 }
4727
4728 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4729 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4730
4731 ((void **)skb->cb)[0] = rdev;
4732 ((void **)skb->cb)[1] = hdr;
4733 ((void **)skb->cb)[2] = data;
4734
4735 return skb;
4736
4737 nla_put_failure:
4738 kfree_skb(skb);
4739 return NULL;
4740}
4741
4742struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4743 int approxlen)
4744{
4745 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4746
4747 if (WARN_ON(!rdev->testmode_info))
4748 return NULL;
4749
4750 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4751 rdev->testmode_info->snd_pid,
4752 rdev->testmode_info->snd_seq,
4753 GFP_KERNEL);
4754}
4755EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4756
4757int cfg80211_testmode_reply(struct sk_buff *skb)
4758{
4759 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4760 void *hdr = ((void **)skb->cb)[1];
4761 struct nlattr *data = ((void **)skb->cb)[2];
4762
4763 if (WARN_ON(!rdev->testmode_info)) {
4764 kfree_skb(skb);
4765 return -EINVAL;
4766 }
4767
4768 nla_nest_end(skb, data);
4769 genlmsg_end(skb, hdr);
4770 return genlmsg_reply(skb, rdev->testmode_info);
4771}
4772EXPORT_SYMBOL(cfg80211_testmode_reply);
4773
4774struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4775 int approxlen, gfp_t gfp)
4776{
4777 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4778
4779 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4780}
4781EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4782
4783void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4784{
4785 void *hdr = ((void **)skb->cb)[1];
4786 struct nlattr *data = ((void **)skb->cb)[2];
4787
4788 nla_nest_end(skb, data);
4789 genlmsg_end(skb, hdr);
4790 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4791}
4792EXPORT_SYMBOL(cfg80211_testmode_event);
4793#endif
4794
Samuel Ortizb23aa672009-07-01 21:26:54 +02004795static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4796{
Johannes Berg4c476992010-10-04 21:36:35 +02004797 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4798 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004799 struct cfg80211_connect_params connect;
4800 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004801 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004802 int err;
4803
4804 memset(&connect, 0, sizeof(connect));
4805
4806 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4807 return -EINVAL;
4808
4809 if (!info->attrs[NL80211_ATTR_SSID] ||
4810 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4811 return -EINVAL;
4812
4813 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4814 connect.auth_type =
4815 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4816 if (!nl80211_valid_auth_type(connect.auth_type))
4817 return -EINVAL;
4818 } else
4819 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4820
4821 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4822
Johannes Bergc0692b82010-08-27 14:26:53 +03004823 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004824 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004825 if (err)
4826 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004827
Johannes Berg074ac8d2010-09-16 14:58:22 +02004828 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004829 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4830 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004831
Johannes Berg79c97e92009-07-07 03:56:12 +02004832 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004833
Samuel Ortizb23aa672009-07-01 21:26:54 +02004834 if (info->attrs[NL80211_ATTR_MAC])
4835 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4836 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4837 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4838
4839 if (info->attrs[NL80211_ATTR_IE]) {
4840 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4841 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4842 }
4843
4844 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4845 connect.channel =
4846 ieee80211_get_channel(wiphy,
4847 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4848 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004849 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4850 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004851 }
4852
Johannes Bergfffd0932009-07-08 14:22:54 +02004853 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4854 connkeys = nl80211_parse_connkeys(rdev,
4855 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004856 if (IS_ERR(connkeys))
4857 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004858 }
4859
4860 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004861 if (err)
4862 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004863 return err;
4864}
4865
4866static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4867{
Johannes Berg4c476992010-10-04 21:36:35 +02004868 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4869 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004870 u16 reason;
4871
4872 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4873 reason = WLAN_REASON_DEAUTH_LEAVING;
4874 else
4875 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4876
4877 if (reason == 0)
4878 return -EINVAL;
4879
Johannes Berg074ac8d2010-09-16 14:58:22 +02004880 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004881 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4882 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004883
Johannes Berg4c476992010-10-04 21:36:35 +02004884 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004885}
4886
Johannes Berg463d0182009-07-14 00:33:35 +02004887static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4888{
Johannes Berg4c476992010-10-04 21:36:35 +02004889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004890 struct net *net;
4891 int err;
4892 u32 pid;
4893
4894 if (!info->attrs[NL80211_ATTR_PID])
4895 return -EINVAL;
4896
4897 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4898
Johannes Berg463d0182009-07-14 00:33:35 +02004899 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004900 if (IS_ERR(net))
4901 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004902
4903 err = 0;
4904
4905 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004906 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4907 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004908
Johannes Berg463d0182009-07-14 00:33:35 +02004909 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004910 return err;
4911}
4912
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004913static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4914{
Johannes Berg4c476992010-10-04 21:36:35 +02004915 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004916 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4917 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004918 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004919 struct cfg80211_pmksa pmksa;
4920
4921 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4922
4923 if (!info->attrs[NL80211_ATTR_MAC])
4924 return -EINVAL;
4925
4926 if (!info->attrs[NL80211_ATTR_PMKID])
4927 return -EINVAL;
4928
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004929 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4930 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4931
Johannes Berg074ac8d2010-09-16 14:58:22 +02004932 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004933 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4934 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004935
4936 switch (info->genlhdr->cmd) {
4937 case NL80211_CMD_SET_PMKSA:
4938 rdev_ops = rdev->ops->set_pmksa;
4939 break;
4940 case NL80211_CMD_DEL_PMKSA:
4941 rdev_ops = rdev->ops->del_pmksa;
4942 break;
4943 default:
4944 WARN_ON(1);
4945 break;
4946 }
4947
Johannes Berg4c476992010-10-04 21:36:35 +02004948 if (!rdev_ops)
4949 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004950
Johannes Berg4c476992010-10-04 21:36:35 +02004951 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004952}
4953
4954static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4955{
Johannes Berg4c476992010-10-04 21:36:35 +02004956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4957 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004958
Johannes Berg074ac8d2010-09-16 14:58:22 +02004959 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004960 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4961 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004962
Johannes Berg4c476992010-10-04 21:36:35 +02004963 if (!rdev->ops->flush_pmksa)
4964 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004965
Johannes Berg4c476992010-10-04 21:36:35 +02004966 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004967}
4968
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004969static int nl80211_remain_on_channel(struct sk_buff *skb,
4970 struct genl_info *info)
4971{
Johannes Berg4c476992010-10-04 21:36:35 +02004972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4973 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004974 struct ieee80211_channel *chan;
4975 struct sk_buff *msg;
4976 void *hdr;
4977 u64 cookie;
4978 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4979 u32 freq, duration;
4980 int err;
4981
4982 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4983 !info->attrs[NL80211_ATTR_DURATION])
4984 return -EINVAL;
4985
4986 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4987
4988 /*
4989 * We should be on that channel for at least one jiffie,
4990 * and more than 5 seconds seems excessive.
4991 */
Johannes Berga2939112010-12-14 17:54:28 +01004992 if (!duration || !msecs_to_jiffies(duration) ||
4993 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004994 return -EINVAL;
4995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 if (!rdev->ops->remain_on_channel)
4997 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004998
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004999 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5000 channel_type = nla_get_u32(
5001 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5002 if (channel_type != NL80211_CHAN_NO_HT &&
5003 channel_type != NL80211_CHAN_HT20 &&
5004 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005005 channel_type != NL80211_CHAN_HT40MINUS)
5006 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005007 }
5008
5009 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5010 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005011 if (chan == NULL)
5012 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005013
5014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005015 if (!msg)
5016 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005017
5018 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5019 NL80211_CMD_REMAIN_ON_CHANNEL);
5020
5021 if (IS_ERR(hdr)) {
5022 err = PTR_ERR(hdr);
5023 goto free_msg;
5024 }
5025
5026 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5027 channel_type, duration, &cookie);
5028
5029 if (err)
5030 goto free_msg;
5031
5032 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5033
5034 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005035
5036 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005037
5038 nla_put_failure:
5039 err = -ENOBUFS;
5040 free_msg:
5041 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005042 return err;
5043}
5044
5045static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5046 struct genl_info *info)
5047{
Johannes Berg4c476992010-10-04 21:36:35 +02005048 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5049 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005050 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005051
5052 if (!info->attrs[NL80211_ATTR_COOKIE])
5053 return -EINVAL;
5054
Johannes Berg4c476992010-10-04 21:36:35 +02005055 if (!rdev->ops->cancel_remain_on_channel)
5056 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005057
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005058 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5059
Johannes Berg4c476992010-10-04 21:36:35 +02005060 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005061}
5062
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005063static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5064 u8 *rates, u8 rates_len)
5065{
5066 u8 i;
5067 u32 mask = 0;
5068
5069 for (i = 0; i < rates_len; i++) {
5070 int rate = (rates[i] & 0x7f) * 5;
5071 int ridx;
5072 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5073 struct ieee80211_rate *srate =
5074 &sband->bitrates[ridx];
5075 if (rate == srate->bitrate) {
5076 mask |= 1 << ridx;
5077 break;
5078 }
5079 }
5080 if (ridx == sband->n_bitrates)
5081 return 0; /* rate not found */
5082 }
5083
5084 return mask;
5085}
5086
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005087static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005088 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5089 .len = NL80211_MAX_SUPP_RATES },
5090};
5091
5092static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5093 struct genl_info *info)
5094{
5095 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005097 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005098 int rem, i;
5099 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005100 struct nlattr *tx_rates;
5101 struct ieee80211_supported_band *sband;
5102
5103 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5104 return -EINVAL;
5105
Johannes Berg4c476992010-10-04 21:36:35 +02005106 if (!rdev->ops->set_bitrate_mask)
5107 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005108
5109 memset(&mask, 0, sizeof(mask));
5110 /* Default to all rates enabled */
5111 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5112 sband = rdev->wiphy.bands[i];
5113 mask.control[i].legacy =
5114 sband ? (1 << sband->n_bitrates) - 1 : 0;
5115 }
5116
5117 /*
5118 * The nested attribute uses enum nl80211_band as the index. This maps
5119 * directly to the enum ieee80211_band values used in cfg80211.
5120 */
5121 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5122 {
5123 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005124 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5125 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005126 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005127 if (sband == NULL)
5128 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005129 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5130 nla_len(tx_rates), nl80211_txattr_policy);
5131 if (tb[NL80211_TXRATE_LEGACY]) {
5132 mask.control[band].legacy = rateset_to_mask(
5133 sband,
5134 nla_data(tb[NL80211_TXRATE_LEGACY]),
5135 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005136 if (mask.control[band].legacy == 0)
5137 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005138 }
5139 }
5140
Johannes Berg4c476992010-10-04 21:36:35 +02005141 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005142}
5143
Johannes Berg2e161f72010-08-12 15:38:38 +02005144static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005145{
Johannes Berg4c476992010-10-04 21:36:35 +02005146 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5147 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005148 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005149
5150 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5151 return -EINVAL;
5152
Johannes Berg2e161f72010-08-12 15:38:38 +02005153 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5154 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005155
Johannes Berg9d38d852010-06-09 17:20:33 +02005156 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005157 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005158 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5159 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5160 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005161 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005162 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5163 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005164
5165 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005166 if (!rdev->ops->mgmt_tx)
5167 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005168
Johannes Berg4c476992010-10-04 21:36:35 +02005169 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005170 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005171 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5172 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005173}
5174
Johannes Berg2e161f72010-08-12 15:38:38 +02005175static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005176{
Johannes Berg4c476992010-10-04 21:36:35 +02005177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5178 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005179 struct ieee80211_channel *chan;
5180 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005181 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005182 u32 freq;
5183 int err;
5184 void *hdr;
5185 u64 cookie;
5186 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005187 unsigned int wait = 0;
5188 bool offchan;
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305189 bool no_cck;
Jouni Malinen026331c2010-02-15 12:53:10 +02005190
5191 if (!info->attrs[NL80211_ATTR_FRAME] ||
5192 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5193 return -EINVAL;
5194
Johannes Berg4c476992010-10-04 21:36:35 +02005195 if (!rdev->ops->mgmt_tx)
5196 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005197
Johannes Berg9d38d852010-06-09 17:20:33 +02005198 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005199 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005200 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5201 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5202 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005203 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005204 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5205 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005206
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005207 if (info->attrs[NL80211_ATTR_DURATION]) {
5208 if (!rdev->ops->mgmt_tx_cancel_wait)
5209 return -EINVAL;
5210 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5211 }
5212
Jouni Malinen026331c2010-02-15 12:53:10 +02005213 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5214 channel_type = nla_get_u32(
5215 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5216 if (channel_type != NL80211_CHAN_NO_HT &&
5217 channel_type != NL80211_CHAN_HT20 &&
5218 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005219 channel_type != NL80211_CHAN_HT40MINUS)
5220 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005221 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005222 }
5223
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005224 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5225
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305226 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5227
Jouni Malinen026331c2010-02-15 12:53:10 +02005228 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5229 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005230 if (chan == NULL)
5231 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005232
5233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005234 if (!msg)
5235 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005236
5237 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005238 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005239
5240 if (IS_ERR(hdr)) {
5241 err = PTR_ERR(hdr);
5242 goto free_msg;
5243 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005244 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5245 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005246 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5247 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305248 no_cck, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005249 if (err)
5250 goto free_msg;
5251
5252 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5253
5254 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005255 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005256
5257 nla_put_failure:
5258 err = -ENOBUFS;
5259 free_msg:
5260 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005261 return err;
5262}
5263
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005264static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5265{
5266 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5267 struct net_device *dev = info->user_ptr[1];
5268 u64 cookie;
5269
5270 if (!info->attrs[NL80211_ATTR_COOKIE])
5271 return -EINVAL;
5272
5273 if (!rdev->ops->mgmt_tx_cancel_wait)
5274 return -EOPNOTSUPP;
5275
5276 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5277 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5278 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5279 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5280 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5281 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5282 return -EOPNOTSUPP;
5283
5284 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5285
5286 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5287}
5288
Kalle Valoffb9eb32010-02-17 17:58:10 +02005289static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5290{
Johannes Berg4c476992010-10-04 21:36:35 +02005291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005292 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005293 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005294 u8 ps_state;
5295 bool state;
5296 int err;
5297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (!info->attrs[NL80211_ATTR_PS_STATE])
5299 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005300
5301 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5302
Johannes Berg4c476992010-10-04 21:36:35 +02005303 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5304 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005305
5306 wdev = dev->ieee80211_ptr;
5307
Johannes Berg4c476992010-10-04 21:36:35 +02005308 if (!rdev->ops->set_power_mgmt)
5309 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005310
5311 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5312
5313 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005314 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005315
Johannes Berg4c476992010-10-04 21:36:35 +02005316 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5317 wdev->ps_timeout);
5318 if (!err)
5319 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005320 return err;
5321}
5322
5323static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5324{
Johannes Berg4c476992010-10-04 21:36:35 +02005325 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005326 enum nl80211_ps_state ps_state;
5327 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005328 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005329 struct sk_buff *msg;
5330 void *hdr;
5331 int err;
5332
Kalle Valoffb9eb32010-02-17 17:58:10 +02005333 wdev = dev->ieee80211_ptr;
5334
Johannes Berg4c476992010-10-04 21:36:35 +02005335 if (!rdev->ops->set_power_mgmt)
5336 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005337
5338 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005339 if (!msg)
5340 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005341
5342 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5343 NL80211_CMD_GET_POWER_SAVE);
5344 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005345 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005346 goto free_msg;
5347 }
5348
5349 if (wdev->ps)
5350 ps_state = NL80211_PS_ENABLED;
5351 else
5352 ps_state = NL80211_PS_DISABLED;
5353
5354 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5355
5356 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005357 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005358
Johannes Berg4c476992010-10-04 21:36:35 +02005359 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005360 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005361 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005362 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005363 return err;
5364}
5365
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005366static struct nla_policy
5367nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5368 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5369 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5370 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5371};
5372
5373static int nl80211_set_cqm_rssi(struct genl_info *info,
5374 s32 threshold, u32 hysteresis)
5375{
Johannes Berg4c476992010-10-04 21:36:35 +02005376 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005377 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005378 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005379
5380 if (threshold > 0)
5381 return -EINVAL;
5382
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005383 wdev = dev->ieee80211_ptr;
5384
Johannes Berg4c476992010-10-04 21:36:35 +02005385 if (!rdev->ops->set_cqm_rssi_config)
5386 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005387
Johannes Berg074ac8d2010-09-16 14:58:22 +02005388 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005389 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5390 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005391
Johannes Berg4c476992010-10-04 21:36:35 +02005392 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5393 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005394}
5395
5396static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5397{
5398 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5399 struct nlattr *cqm;
5400 int err;
5401
5402 cqm = info->attrs[NL80211_ATTR_CQM];
5403 if (!cqm) {
5404 err = -EINVAL;
5405 goto out;
5406 }
5407
5408 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5409 nl80211_attr_cqm_policy);
5410 if (err)
5411 goto out;
5412
5413 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5414 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5415 s32 threshold;
5416 u32 hysteresis;
5417 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5418 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5419 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5420 } else
5421 err = -EINVAL;
5422
5423out:
5424 return err;
5425}
5426
Johannes Berg29cbe682010-12-03 09:20:44 +01005427static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5428{
5429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5430 struct net_device *dev = info->user_ptr[1];
5431 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005432 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005433 int err;
5434
5435 /* start with default */
5436 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005437 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005438
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005439 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005440 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005441 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005442 if (err)
5443 return err;
5444 }
5445
5446 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5447 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5448 return -EINVAL;
5449
Javier Cardonac80d5452010-12-16 17:37:49 -08005450 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5451 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5452
5453 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5454 /* parse additional setup parameters if given */
5455 err = nl80211_parse_mesh_setup(info, &setup);
5456 if (err)
5457 return err;
5458 }
5459
5460 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005461}
5462
5463static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5464{
5465 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5466 struct net_device *dev = info->user_ptr[1];
5467
5468 return cfg80211_leave_mesh(rdev, dev);
5469}
5470
Johannes Bergff1b6e62011-05-04 15:37:28 +02005471static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5472{
5473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5474 struct sk_buff *msg;
5475 void *hdr;
5476
5477 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5478 return -EOPNOTSUPP;
5479
5480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5481 if (!msg)
5482 return -ENOMEM;
5483
5484 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5485 NL80211_CMD_GET_WOWLAN);
5486 if (!hdr)
5487 goto nla_put_failure;
5488
5489 if (rdev->wowlan) {
5490 struct nlattr *nl_wowlan;
5491
5492 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5493 if (!nl_wowlan)
5494 goto nla_put_failure;
5495
5496 if (rdev->wowlan->any)
5497 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5498 if (rdev->wowlan->disconnect)
5499 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5500 if (rdev->wowlan->magic_pkt)
5501 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005502 if (rdev->wowlan->gtk_rekey_failure)
5503 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5504 if (rdev->wowlan->eap_identity_req)
5505 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5506 if (rdev->wowlan->four_way_handshake)
5507 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5508 if (rdev->wowlan->rfkill_release)
5509 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005510 if (rdev->wowlan->n_patterns) {
5511 struct nlattr *nl_pats, *nl_pat;
5512 int i, pat_len;
5513
5514 nl_pats = nla_nest_start(msg,
5515 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5516 if (!nl_pats)
5517 goto nla_put_failure;
5518
5519 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5520 nl_pat = nla_nest_start(msg, i + 1);
5521 if (!nl_pat)
5522 goto nla_put_failure;
5523 pat_len = rdev->wowlan->patterns[i].pattern_len;
5524 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5525 DIV_ROUND_UP(pat_len, 8),
5526 rdev->wowlan->patterns[i].mask);
5527 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5528 pat_len,
5529 rdev->wowlan->patterns[i].pattern);
5530 nla_nest_end(msg, nl_pat);
5531 }
5532 nla_nest_end(msg, nl_pats);
5533 }
5534
5535 nla_nest_end(msg, nl_wowlan);
5536 }
5537
5538 genlmsg_end(msg, hdr);
5539 return genlmsg_reply(msg, info);
5540
5541nla_put_failure:
5542 nlmsg_free(msg);
5543 return -ENOBUFS;
5544}
5545
5546static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5547{
5548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5549 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5550 struct cfg80211_wowlan no_triggers = {};
5551 struct cfg80211_wowlan new_triggers = {};
5552 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5553 int err, i;
5554
5555 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5556 return -EOPNOTSUPP;
5557
5558 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5559 goto no_triggers;
5560
5561 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5562 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5563 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5564 nl80211_wowlan_policy);
5565 if (err)
5566 return err;
5567
5568 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5569 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5570 return -EINVAL;
5571 new_triggers.any = true;
5572 }
5573
5574 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5575 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5576 return -EINVAL;
5577 new_triggers.disconnect = true;
5578 }
5579
5580 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5581 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5582 return -EINVAL;
5583 new_triggers.magic_pkt = true;
5584 }
5585
Johannes Berg77dbbb12011-07-13 10:48:55 +02005586 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5587 return -EINVAL;
5588
5589 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5590 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5591 return -EINVAL;
5592 new_triggers.gtk_rekey_failure = true;
5593 }
5594
5595 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5596 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5597 return -EINVAL;
5598 new_triggers.eap_identity_req = true;
5599 }
5600
5601 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5602 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5603 return -EINVAL;
5604 new_triggers.four_way_handshake = true;
5605 }
5606
5607 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5608 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5609 return -EINVAL;
5610 new_triggers.rfkill_release = true;
5611 }
5612
Johannes Bergff1b6e62011-05-04 15:37:28 +02005613 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5614 struct nlattr *pat;
5615 int n_patterns = 0;
5616 int rem, pat_len, mask_len;
5617 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5618
5619 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5620 rem)
5621 n_patterns++;
5622 if (n_patterns > wowlan->n_patterns)
5623 return -EINVAL;
5624
5625 new_triggers.patterns = kcalloc(n_patterns,
5626 sizeof(new_triggers.patterns[0]),
5627 GFP_KERNEL);
5628 if (!new_triggers.patterns)
5629 return -ENOMEM;
5630
5631 new_triggers.n_patterns = n_patterns;
5632 i = 0;
5633
5634 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5635 rem) {
5636 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5637 nla_data(pat), nla_len(pat), NULL);
5638 err = -EINVAL;
5639 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5640 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5641 goto error;
5642 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5643 mask_len = DIV_ROUND_UP(pat_len, 8);
5644 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5645 mask_len)
5646 goto error;
5647 if (pat_len > wowlan->pattern_max_len ||
5648 pat_len < wowlan->pattern_min_len)
5649 goto error;
5650
5651 new_triggers.patterns[i].mask =
5652 kmalloc(mask_len + pat_len, GFP_KERNEL);
5653 if (!new_triggers.patterns[i].mask) {
5654 err = -ENOMEM;
5655 goto error;
5656 }
5657 new_triggers.patterns[i].pattern =
5658 new_triggers.patterns[i].mask + mask_len;
5659 memcpy(new_triggers.patterns[i].mask,
5660 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5661 mask_len);
5662 new_triggers.patterns[i].pattern_len = pat_len;
5663 memcpy(new_triggers.patterns[i].pattern,
5664 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5665 pat_len);
5666 i++;
5667 }
5668 }
5669
5670 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5671 struct cfg80211_wowlan *ntrig;
5672 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5673 GFP_KERNEL);
5674 if (!ntrig) {
5675 err = -ENOMEM;
5676 goto error;
5677 }
5678 cfg80211_rdev_free_wowlan(rdev);
5679 rdev->wowlan = ntrig;
5680 } else {
5681 no_triggers:
5682 cfg80211_rdev_free_wowlan(rdev);
5683 rdev->wowlan = NULL;
5684 }
5685
5686 return 0;
5687 error:
5688 for (i = 0; i < new_triggers.n_patterns; i++)
5689 kfree(new_triggers.patterns[i].mask);
5690 kfree(new_triggers.patterns);
5691 return err;
5692}
5693
Johannes Berge5497d72011-07-05 16:35:40 +02005694static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5695{
5696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5697 struct net_device *dev = info->user_ptr[1];
5698 struct wireless_dev *wdev = dev->ieee80211_ptr;
5699 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5700 struct cfg80211_gtk_rekey_data rekey_data;
5701 int err;
5702
5703 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5704 return -EINVAL;
5705
5706 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5707 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5708 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5709 nl80211_rekey_policy);
5710 if (err)
5711 return err;
5712
5713 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5714 return -ERANGE;
5715 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5716 return -ERANGE;
5717 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5718 return -ERANGE;
5719
5720 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5721 NL80211_KEK_LEN);
5722 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5723 NL80211_KCK_LEN);
5724 memcpy(rekey_data.replay_ctr,
5725 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5726 NL80211_REPLAY_CTR_LEN);
5727
5728 wdev_lock(wdev);
5729 if (!wdev->current_bss) {
5730 err = -ENOTCONN;
5731 goto out;
5732 }
5733
5734 if (!rdev->ops->set_rekey_data) {
5735 err = -EOPNOTSUPP;
5736 goto out;
5737 }
5738
5739 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5740 out:
5741 wdev_unlock(wdev);
5742 return err;
5743}
5744
Johannes Berg4c476992010-10-04 21:36:35 +02005745#define NL80211_FLAG_NEED_WIPHY 0x01
5746#define NL80211_FLAG_NEED_NETDEV 0x02
5747#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005748#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5749#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5750 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005751
5752static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5753 struct genl_info *info)
5754{
5755 struct cfg80211_registered_device *rdev;
5756 struct net_device *dev;
5757 int err;
5758 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5759
5760 if (rtnl)
5761 rtnl_lock();
5762
5763 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5764 rdev = cfg80211_get_dev_from_info(info);
5765 if (IS_ERR(rdev)) {
5766 if (rtnl)
5767 rtnl_unlock();
5768 return PTR_ERR(rdev);
5769 }
5770 info->user_ptr[0] = rdev;
5771 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5772 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5773 if (err) {
5774 if (rtnl)
5775 rtnl_unlock();
5776 return err;
5777 }
Johannes Berg41265712010-10-04 21:14:05 +02005778 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5779 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005780 cfg80211_unlock_rdev(rdev);
5781 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005782 if (rtnl)
5783 rtnl_unlock();
5784 return -ENETDOWN;
5785 }
Johannes Berg4c476992010-10-04 21:36:35 +02005786 info->user_ptr[0] = rdev;
5787 info->user_ptr[1] = dev;
5788 }
5789
5790 return 0;
5791}
5792
5793static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5794 struct genl_info *info)
5795{
5796 if (info->user_ptr[0])
5797 cfg80211_unlock_rdev(info->user_ptr[0]);
5798 if (info->user_ptr[1])
5799 dev_put(info->user_ptr[1]);
5800 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5801 rtnl_unlock();
5802}
5803
Johannes Berg55682962007-09-20 13:09:35 -04005804static struct genl_ops nl80211_ops[] = {
5805 {
5806 .cmd = NL80211_CMD_GET_WIPHY,
5807 .doit = nl80211_get_wiphy,
5808 .dumpit = nl80211_dump_wiphy,
5809 .policy = nl80211_policy,
5810 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005811 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005812 },
5813 {
5814 .cmd = NL80211_CMD_SET_WIPHY,
5815 .doit = nl80211_set_wiphy,
5816 .policy = nl80211_policy,
5817 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005818 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005819 },
5820 {
5821 .cmd = NL80211_CMD_GET_INTERFACE,
5822 .doit = nl80211_get_interface,
5823 .dumpit = nl80211_dump_interface,
5824 .policy = nl80211_policy,
5825 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005826 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005827 },
5828 {
5829 .cmd = NL80211_CMD_SET_INTERFACE,
5830 .doit = nl80211_set_interface,
5831 .policy = nl80211_policy,
5832 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005833 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5834 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005835 },
5836 {
5837 .cmd = NL80211_CMD_NEW_INTERFACE,
5838 .doit = nl80211_new_interface,
5839 .policy = nl80211_policy,
5840 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005841 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5842 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005843 },
5844 {
5845 .cmd = NL80211_CMD_DEL_INTERFACE,
5846 .doit = nl80211_del_interface,
5847 .policy = nl80211_policy,
5848 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005849 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5850 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005851 },
Johannes Berg41ade002007-12-19 02:03:29 +01005852 {
5853 .cmd = NL80211_CMD_GET_KEY,
5854 .doit = nl80211_get_key,
5855 .policy = nl80211_policy,
5856 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005857 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5858 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005859 },
5860 {
5861 .cmd = NL80211_CMD_SET_KEY,
5862 .doit = nl80211_set_key,
5863 .policy = nl80211_policy,
5864 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005866 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005867 },
5868 {
5869 .cmd = NL80211_CMD_NEW_KEY,
5870 .doit = nl80211_new_key,
5871 .policy = nl80211_policy,
5872 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005873 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005874 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005875 },
5876 {
5877 .cmd = NL80211_CMD_DEL_KEY,
5878 .doit = nl80211_del_key,
5879 .policy = nl80211_policy,
5880 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005881 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005882 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005883 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005884 {
5885 .cmd = NL80211_CMD_SET_BEACON,
5886 .policy = nl80211_policy,
5887 .flags = GENL_ADMIN_PERM,
5888 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5890 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005891 },
5892 {
5893 .cmd = NL80211_CMD_NEW_BEACON,
5894 .policy = nl80211_policy,
5895 .flags = GENL_ADMIN_PERM,
5896 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005897 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5898 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005899 },
5900 {
5901 .cmd = NL80211_CMD_DEL_BEACON,
5902 .policy = nl80211_policy,
5903 .flags = GENL_ADMIN_PERM,
5904 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005905 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5906 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005907 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005908 {
5909 .cmd = NL80211_CMD_GET_STATION,
5910 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005911 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005912 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005913 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5914 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005915 },
5916 {
5917 .cmd = NL80211_CMD_SET_STATION,
5918 .doit = nl80211_set_station,
5919 .policy = nl80211_policy,
5920 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005921 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5922 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005923 },
5924 {
5925 .cmd = NL80211_CMD_NEW_STATION,
5926 .doit = nl80211_new_station,
5927 .policy = nl80211_policy,
5928 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005929 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005930 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005931 },
5932 {
5933 .cmd = NL80211_CMD_DEL_STATION,
5934 .doit = nl80211_del_station,
5935 .policy = nl80211_policy,
5936 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005937 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5938 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005939 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005940 {
5941 .cmd = NL80211_CMD_GET_MPATH,
5942 .doit = nl80211_get_mpath,
5943 .dumpit = nl80211_dump_mpath,
5944 .policy = nl80211_policy,
5945 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005947 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005948 },
5949 {
5950 .cmd = NL80211_CMD_SET_MPATH,
5951 .doit = nl80211_set_mpath,
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,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005956 },
5957 {
5958 .cmd = NL80211_CMD_NEW_MPATH,
5959 .doit = nl80211_new_mpath,
5960 .policy = nl80211_policy,
5961 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005962 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005963 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005964 },
5965 {
5966 .cmd = NL80211_CMD_DEL_MPATH,
5967 .doit = nl80211_del_mpath,
5968 .policy = nl80211_policy,
5969 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005970 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5971 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005972 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005973 {
5974 .cmd = NL80211_CMD_SET_BSS,
5975 .doit = nl80211_set_bss,
5976 .policy = nl80211_policy,
5977 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005978 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5979 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005980 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005981 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005982 .cmd = NL80211_CMD_GET_REG,
5983 .doit = nl80211_get_reg,
5984 .policy = nl80211_policy,
5985 /* can be retrieved by unprivileged users */
5986 },
5987 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005988 .cmd = NL80211_CMD_SET_REG,
5989 .doit = nl80211_set_reg,
5990 .policy = nl80211_policy,
5991 .flags = GENL_ADMIN_PERM,
5992 },
5993 {
5994 .cmd = NL80211_CMD_REQ_SET_REG,
5995 .doit = nl80211_req_set_reg,
5996 .policy = nl80211_policy,
5997 .flags = GENL_ADMIN_PERM,
5998 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005999 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006000 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6001 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006002 .policy = nl80211_policy,
6003 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006004 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6005 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006006 },
6007 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006008 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6009 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006010 .policy = nl80211_policy,
6011 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006012 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006013 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006014 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006015 {
Johannes Berg2a519312009-02-10 21:25:55 +01006016 .cmd = NL80211_CMD_TRIGGER_SCAN,
6017 .doit = nl80211_trigger_scan,
6018 .policy = nl80211_policy,
6019 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006020 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006021 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006022 },
6023 {
6024 .cmd = NL80211_CMD_GET_SCAN,
6025 .policy = nl80211_policy,
6026 .dumpit = nl80211_dump_scan,
6027 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006028 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006029 .cmd = NL80211_CMD_START_SCHED_SCAN,
6030 .doit = nl80211_start_sched_scan,
6031 .policy = nl80211_policy,
6032 .flags = GENL_ADMIN_PERM,
6033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6034 NL80211_FLAG_NEED_RTNL,
6035 },
6036 {
6037 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6038 .doit = nl80211_stop_sched_scan,
6039 .policy = nl80211_policy,
6040 .flags = GENL_ADMIN_PERM,
6041 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6042 NL80211_FLAG_NEED_RTNL,
6043 },
6044 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006045 .cmd = NL80211_CMD_AUTHENTICATE,
6046 .doit = nl80211_authenticate,
6047 .policy = nl80211_policy,
6048 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006049 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006050 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006051 },
6052 {
6053 .cmd = NL80211_CMD_ASSOCIATE,
6054 .doit = nl80211_associate,
6055 .policy = nl80211_policy,
6056 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006058 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006059 },
6060 {
6061 .cmd = NL80211_CMD_DEAUTHENTICATE,
6062 .doit = nl80211_deauthenticate,
6063 .policy = nl80211_policy,
6064 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006066 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006067 },
6068 {
6069 .cmd = NL80211_CMD_DISASSOCIATE,
6070 .doit = nl80211_disassociate,
6071 .policy = nl80211_policy,
6072 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006073 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006074 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006075 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006076 {
6077 .cmd = NL80211_CMD_JOIN_IBSS,
6078 .doit = nl80211_join_ibss,
6079 .policy = nl80211_policy,
6080 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006081 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006082 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006083 },
6084 {
6085 .cmd = NL80211_CMD_LEAVE_IBSS,
6086 .doit = nl80211_leave_ibss,
6087 .policy = nl80211_policy,
6088 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006089 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006090 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006091 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006092#ifdef CONFIG_NL80211_TESTMODE
6093 {
6094 .cmd = NL80211_CMD_TESTMODE,
6095 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006096 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006097 .policy = nl80211_policy,
6098 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006099 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6100 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006101 },
6102#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006103 {
6104 .cmd = NL80211_CMD_CONNECT,
6105 .doit = nl80211_connect,
6106 .policy = nl80211_policy,
6107 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006108 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006109 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006110 },
6111 {
6112 .cmd = NL80211_CMD_DISCONNECT,
6113 .doit = nl80211_disconnect,
6114 .policy = nl80211_policy,
6115 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006116 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006117 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006118 },
Johannes Berg463d0182009-07-14 00:33:35 +02006119 {
6120 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6121 .doit = nl80211_wiphy_netns,
6122 .policy = nl80211_policy,
6123 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006124 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6125 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006126 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006127 {
6128 .cmd = NL80211_CMD_GET_SURVEY,
6129 .policy = nl80211_policy,
6130 .dumpit = nl80211_dump_survey,
6131 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006132 {
6133 .cmd = NL80211_CMD_SET_PMKSA,
6134 .doit = nl80211_setdel_pmksa,
6135 .policy = nl80211_policy,
6136 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006137 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6138 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006139 },
6140 {
6141 .cmd = NL80211_CMD_DEL_PMKSA,
6142 .doit = nl80211_setdel_pmksa,
6143 .policy = nl80211_policy,
6144 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006145 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6146 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006147 },
6148 {
6149 .cmd = NL80211_CMD_FLUSH_PMKSA,
6150 .doit = nl80211_flush_pmksa,
6151 .policy = nl80211_policy,
6152 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006153 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6154 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006155 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006156 {
6157 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6158 .doit = nl80211_remain_on_channel,
6159 .policy = nl80211_policy,
6160 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006161 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006162 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006163 },
6164 {
6165 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6166 .doit = nl80211_cancel_remain_on_channel,
6167 .policy = nl80211_policy,
6168 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006169 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006170 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006171 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006172 {
6173 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6174 .doit = nl80211_set_tx_bitrate_mask,
6175 .policy = nl80211_policy,
6176 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006177 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6178 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006179 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006180 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006181 .cmd = NL80211_CMD_REGISTER_FRAME,
6182 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006183 .policy = nl80211_policy,
6184 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006185 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6186 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006187 },
6188 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006189 .cmd = NL80211_CMD_FRAME,
6190 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006191 .policy = nl80211_policy,
6192 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006193 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006194 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006195 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006196 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006197 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6198 .doit = nl80211_tx_mgmt_cancel_wait,
6199 .policy = nl80211_policy,
6200 .flags = GENL_ADMIN_PERM,
6201 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6202 NL80211_FLAG_NEED_RTNL,
6203 },
6204 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006205 .cmd = NL80211_CMD_SET_POWER_SAVE,
6206 .doit = nl80211_set_power_save,
6207 .policy = nl80211_policy,
6208 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006209 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6210 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006211 },
6212 {
6213 .cmd = NL80211_CMD_GET_POWER_SAVE,
6214 .doit = nl80211_get_power_save,
6215 .policy = nl80211_policy,
6216 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006217 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6218 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006219 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006220 {
6221 .cmd = NL80211_CMD_SET_CQM,
6222 .doit = nl80211_set_cqm,
6223 .policy = nl80211_policy,
6224 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006225 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6226 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006227 },
Johannes Bergf444de02010-05-05 15:25:02 +02006228 {
6229 .cmd = NL80211_CMD_SET_CHANNEL,
6230 .doit = nl80211_set_channel,
6231 .policy = nl80211_policy,
6232 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006233 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6234 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006235 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006236 {
6237 .cmd = NL80211_CMD_SET_WDS_PEER,
6238 .doit = nl80211_set_wds_peer,
6239 .policy = nl80211_policy,
6240 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006241 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6242 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006243 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006244 {
6245 .cmd = NL80211_CMD_JOIN_MESH,
6246 .doit = nl80211_join_mesh,
6247 .policy = nl80211_policy,
6248 .flags = GENL_ADMIN_PERM,
6249 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6250 NL80211_FLAG_NEED_RTNL,
6251 },
6252 {
6253 .cmd = NL80211_CMD_LEAVE_MESH,
6254 .doit = nl80211_leave_mesh,
6255 .policy = nl80211_policy,
6256 .flags = GENL_ADMIN_PERM,
6257 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6258 NL80211_FLAG_NEED_RTNL,
6259 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006260 {
6261 .cmd = NL80211_CMD_GET_WOWLAN,
6262 .doit = nl80211_get_wowlan,
6263 .policy = nl80211_policy,
6264 /* can be retrieved by unprivileged users */
6265 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6266 NL80211_FLAG_NEED_RTNL,
6267 },
6268 {
6269 .cmd = NL80211_CMD_SET_WOWLAN,
6270 .doit = nl80211_set_wowlan,
6271 .policy = nl80211_policy,
6272 .flags = GENL_ADMIN_PERM,
6273 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6274 NL80211_FLAG_NEED_RTNL,
6275 },
Johannes Berge5497d72011-07-05 16:35:40 +02006276 {
6277 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6278 .doit = nl80211_set_rekey_data,
6279 .policy = nl80211_policy,
6280 .flags = GENL_ADMIN_PERM,
6281 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6282 NL80211_FLAG_NEED_RTNL,
6283 },
Johannes Berg55682962007-09-20 13:09:35 -04006284};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006285
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006286static struct genl_multicast_group nl80211_mlme_mcgrp = {
6287 .name = "mlme",
6288};
Johannes Berg55682962007-09-20 13:09:35 -04006289
6290/* multicast groups */
6291static struct genl_multicast_group nl80211_config_mcgrp = {
6292 .name = "config",
6293};
Johannes Berg2a519312009-02-10 21:25:55 +01006294static struct genl_multicast_group nl80211_scan_mcgrp = {
6295 .name = "scan",
6296};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006297static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6298 .name = "regulatory",
6299};
Johannes Berg55682962007-09-20 13:09:35 -04006300
6301/* notification functions */
6302
6303void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6304{
6305 struct sk_buff *msg;
6306
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006307 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006308 if (!msg)
6309 return;
6310
6311 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6312 nlmsg_free(msg);
6313 return;
6314 }
6315
Johannes Berg463d0182009-07-14 00:33:35 +02006316 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6317 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006318}
6319
Johannes Berg362a4152009-05-24 16:43:15 +02006320static int nl80211_add_scan_req(struct sk_buff *msg,
6321 struct cfg80211_registered_device *rdev)
6322{
6323 struct cfg80211_scan_request *req = rdev->scan_req;
6324 struct nlattr *nest;
6325 int i;
6326
Johannes Berg667503d2009-07-07 03:56:11 +02006327 ASSERT_RDEV_LOCK(rdev);
6328
Johannes Berg362a4152009-05-24 16:43:15 +02006329 if (WARN_ON(!req))
6330 return 0;
6331
6332 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6333 if (!nest)
6334 goto nla_put_failure;
6335 for (i = 0; i < req->n_ssids; i++)
6336 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6337 nla_nest_end(msg, nest);
6338
6339 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6340 if (!nest)
6341 goto nla_put_failure;
6342 for (i = 0; i < req->n_channels; i++)
6343 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6344 nla_nest_end(msg, nest);
6345
6346 if (req->ie)
6347 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6348
6349 return 0;
6350 nla_put_failure:
6351 return -ENOBUFS;
6352}
6353
Johannes Berga538e2d2009-06-16 19:56:42 +02006354static int nl80211_send_scan_msg(struct sk_buff *msg,
6355 struct cfg80211_registered_device *rdev,
6356 struct net_device *netdev,
6357 u32 pid, u32 seq, int flags,
6358 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006359{
6360 void *hdr;
6361
6362 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6363 if (!hdr)
6364 return -1;
6365
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006366 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006367 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6368
Johannes Berg362a4152009-05-24 16:43:15 +02006369 /* ignore errors and send incomplete event anyway */
6370 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006371
6372 return genlmsg_end(msg, hdr);
6373
6374 nla_put_failure:
6375 genlmsg_cancel(msg, hdr);
6376 return -EMSGSIZE;
6377}
6378
Luciano Coelho807f8a82011-05-11 17:09:35 +03006379static int
6380nl80211_send_sched_scan_msg(struct sk_buff *msg,
6381 struct cfg80211_registered_device *rdev,
6382 struct net_device *netdev,
6383 u32 pid, u32 seq, int flags, u32 cmd)
6384{
6385 void *hdr;
6386
6387 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6388 if (!hdr)
6389 return -1;
6390
6391 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6392 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6393
6394 return genlmsg_end(msg, hdr);
6395
6396 nla_put_failure:
6397 genlmsg_cancel(msg, hdr);
6398 return -EMSGSIZE;
6399}
6400
Johannes Berga538e2d2009-06-16 19:56:42 +02006401void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6402 struct net_device *netdev)
6403{
6404 struct sk_buff *msg;
6405
6406 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6407 if (!msg)
6408 return;
6409
6410 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6411 NL80211_CMD_TRIGGER_SCAN) < 0) {
6412 nlmsg_free(msg);
6413 return;
6414 }
6415
Johannes Berg463d0182009-07-14 00:33:35 +02006416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6417 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006418}
6419
Johannes Berg2a519312009-02-10 21:25:55 +01006420void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6421 struct net_device *netdev)
6422{
6423 struct sk_buff *msg;
6424
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006426 if (!msg)
6427 return;
6428
Johannes Berga538e2d2009-06-16 19:56:42 +02006429 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6430 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006431 nlmsg_free(msg);
6432 return;
6433 }
6434
Johannes Berg463d0182009-07-14 00:33:35 +02006435 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6436 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006437}
6438
6439void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6440 struct net_device *netdev)
6441{
6442 struct sk_buff *msg;
6443
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006445 if (!msg)
6446 return;
6447
Johannes Berga538e2d2009-06-16 19:56:42 +02006448 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6449 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006450 nlmsg_free(msg);
6451 return;
6452 }
6453
Johannes Berg463d0182009-07-14 00:33:35 +02006454 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6455 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006456}
6457
Luciano Coelho807f8a82011-05-11 17:09:35 +03006458void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6459 struct net_device *netdev)
6460{
6461 struct sk_buff *msg;
6462
6463 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6464 if (!msg)
6465 return;
6466
6467 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6468 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6469 nlmsg_free(msg);
6470 return;
6471 }
6472
6473 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6474 nl80211_scan_mcgrp.id, GFP_KERNEL);
6475}
6476
6477void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6478 struct net_device *netdev, u32 cmd)
6479{
6480 struct sk_buff *msg;
6481
6482 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6483 if (!msg)
6484 return;
6485
6486 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6487 nlmsg_free(msg);
6488 return;
6489 }
6490
6491 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6492 nl80211_scan_mcgrp.id, GFP_KERNEL);
6493}
6494
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006495/*
6496 * This can happen on global regulatory changes or device specific settings
6497 * based on custom world regulatory domains.
6498 */
6499void nl80211_send_reg_change_event(struct regulatory_request *request)
6500{
6501 struct sk_buff *msg;
6502 void *hdr;
6503
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006504 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006505 if (!msg)
6506 return;
6507
6508 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6509 if (!hdr) {
6510 nlmsg_free(msg);
6511 return;
6512 }
6513
6514 /* Userspace can always count this one always being set */
6515 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6516
6517 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6518 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6519 NL80211_REGDOM_TYPE_WORLD);
6520 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6521 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6522 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6523 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6524 request->intersect)
6525 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6526 NL80211_REGDOM_TYPE_INTERSECTION);
6527 else {
6528 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6529 NL80211_REGDOM_TYPE_COUNTRY);
6530 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6531 }
6532
6533 if (wiphy_idx_valid(request->wiphy_idx))
6534 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6535
6536 if (genlmsg_end(msg, hdr) < 0) {
6537 nlmsg_free(msg);
6538 return;
6539 }
6540
Johannes Bergbc43b282009-07-25 10:54:13 +02006541 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006542 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006543 GFP_ATOMIC);
6544 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006545
6546 return;
6547
6548nla_put_failure:
6549 genlmsg_cancel(msg, hdr);
6550 nlmsg_free(msg);
6551}
6552
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006553static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6554 struct net_device *netdev,
6555 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006556 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006557{
6558 struct sk_buff *msg;
6559 void *hdr;
6560
Johannes Berge6d6e342009-07-01 21:26:47 +02006561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006562 if (!msg)
6563 return;
6564
6565 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6566 if (!hdr) {
6567 nlmsg_free(msg);
6568 return;
6569 }
6570
6571 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6572 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6573 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6574
6575 if (genlmsg_end(msg, hdr) < 0) {
6576 nlmsg_free(msg);
6577 return;
6578 }
6579
Johannes Berg463d0182009-07-14 00:33:35 +02006580 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6581 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006582 return;
6583
6584 nla_put_failure:
6585 genlmsg_cancel(msg, hdr);
6586 nlmsg_free(msg);
6587}
6588
6589void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006590 struct net_device *netdev, const u8 *buf,
6591 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006592{
6593 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006594 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006595}
6596
6597void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6598 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006599 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006600{
Johannes Berge6d6e342009-07-01 21:26:47 +02006601 nl80211_send_mlme_event(rdev, netdev, buf, len,
6602 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006603}
6604
Jouni Malinen53b46b82009-03-27 20:53:56 +02006605void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006606 struct net_device *netdev, const u8 *buf,
6607 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006608{
6609 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006610 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006611}
6612
Jouni Malinen53b46b82009-03-27 20:53:56 +02006613void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6614 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006615 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006616{
6617 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006618 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006619}
6620
Jouni Malinencf4e5942010-12-16 00:52:40 +02006621void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6622 struct net_device *netdev, const u8 *buf,
6623 size_t len, gfp_t gfp)
6624{
6625 nl80211_send_mlme_event(rdev, netdev, buf, len,
6626 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6627}
6628
6629void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6630 struct net_device *netdev, const u8 *buf,
6631 size_t len, gfp_t gfp)
6632{
6633 nl80211_send_mlme_event(rdev, netdev, buf, len,
6634 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6635}
6636
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006637static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6638 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006639 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006640{
6641 struct sk_buff *msg;
6642 void *hdr;
6643
Johannes Berge6d6e342009-07-01 21:26:47 +02006644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006645 if (!msg)
6646 return;
6647
6648 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6649 if (!hdr) {
6650 nlmsg_free(msg);
6651 return;
6652 }
6653
6654 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6655 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6656 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6657 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6658
6659 if (genlmsg_end(msg, hdr) < 0) {
6660 nlmsg_free(msg);
6661 return;
6662 }
6663
Johannes Berg463d0182009-07-14 00:33:35 +02006664 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6665 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006666 return;
6667
6668 nla_put_failure:
6669 genlmsg_cancel(msg, hdr);
6670 nlmsg_free(msg);
6671}
6672
6673void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006674 struct net_device *netdev, const u8 *addr,
6675 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006676{
6677 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006678 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006679}
6680
6681void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006682 struct net_device *netdev, const u8 *addr,
6683 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006684{
Johannes Berge6d6e342009-07-01 21:26:47 +02006685 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6686 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006687}
6688
Samuel Ortizb23aa672009-07-01 21:26:54 +02006689void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6690 struct net_device *netdev, const u8 *bssid,
6691 const u8 *req_ie, size_t req_ie_len,
6692 const u8 *resp_ie, size_t resp_ie_len,
6693 u16 status, gfp_t gfp)
6694{
6695 struct sk_buff *msg;
6696 void *hdr;
6697
6698 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6699 if (!msg)
6700 return;
6701
6702 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6703 if (!hdr) {
6704 nlmsg_free(msg);
6705 return;
6706 }
6707
6708 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6709 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6710 if (bssid)
6711 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6712 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6713 if (req_ie)
6714 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6715 if (resp_ie)
6716 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6717
6718 if (genlmsg_end(msg, hdr) < 0) {
6719 nlmsg_free(msg);
6720 return;
6721 }
6722
Johannes Berg463d0182009-07-14 00:33:35 +02006723 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6724 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006725 return;
6726
6727 nla_put_failure:
6728 genlmsg_cancel(msg, hdr);
6729 nlmsg_free(msg);
6730
6731}
6732
6733void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6734 struct net_device *netdev, const u8 *bssid,
6735 const u8 *req_ie, size_t req_ie_len,
6736 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6737{
6738 struct sk_buff *msg;
6739 void *hdr;
6740
6741 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6742 if (!msg)
6743 return;
6744
6745 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6746 if (!hdr) {
6747 nlmsg_free(msg);
6748 return;
6749 }
6750
6751 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6752 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6753 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6754 if (req_ie)
6755 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6756 if (resp_ie)
6757 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6758
6759 if (genlmsg_end(msg, hdr) < 0) {
6760 nlmsg_free(msg);
6761 return;
6762 }
6763
Johannes Berg463d0182009-07-14 00:33:35 +02006764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6765 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006766 return;
6767
6768 nla_put_failure:
6769 genlmsg_cancel(msg, hdr);
6770 nlmsg_free(msg);
6771
6772}
6773
6774void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6775 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006776 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006777{
6778 struct sk_buff *msg;
6779 void *hdr;
6780
Johannes Berg667503d2009-07-07 03:56:11 +02006781 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006782 if (!msg)
6783 return;
6784
6785 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6786 if (!hdr) {
6787 nlmsg_free(msg);
6788 return;
6789 }
6790
6791 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6793 if (from_ap && reason)
6794 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6795 if (from_ap)
6796 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6797 if (ie)
6798 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6799
6800 if (genlmsg_end(msg, hdr) < 0) {
6801 nlmsg_free(msg);
6802 return;
6803 }
6804
Johannes Berg463d0182009-07-14 00:33:35 +02006805 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6806 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006807 return;
6808
6809 nla_put_failure:
6810 genlmsg_cancel(msg, hdr);
6811 nlmsg_free(msg);
6812
6813}
6814
Johannes Berg04a773a2009-04-19 21:24:32 +02006815void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6816 struct net_device *netdev, const u8 *bssid,
6817 gfp_t gfp)
6818{
6819 struct sk_buff *msg;
6820 void *hdr;
6821
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006823 if (!msg)
6824 return;
6825
6826 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6827 if (!hdr) {
6828 nlmsg_free(msg);
6829 return;
6830 }
6831
6832 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6833 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6834 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6835
6836 if (genlmsg_end(msg, hdr) < 0) {
6837 nlmsg_free(msg);
6838 return;
6839 }
6840
Johannes Berg463d0182009-07-14 00:33:35 +02006841 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6842 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006843 return;
6844
6845 nla_put_failure:
6846 genlmsg_cancel(msg, hdr);
6847 nlmsg_free(msg);
6848}
6849
Javier Cardonac93b5e72011-04-07 15:08:34 -07006850void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6851 struct net_device *netdev,
6852 const u8 *macaddr, const u8* ie, u8 ie_len,
6853 gfp_t gfp)
6854{
6855 struct sk_buff *msg;
6856 void *hdr;
6857
6858 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6859 if (!msg)
6860 return;
6861
6862 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6863 if (!hdr) {
6864 nlmsg_free(msg);
6865 return;
6866 }
6867
6868 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6870 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6871 if (ie_len && ie)
6872 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6873
6874 if (genlmsg_end(msg, hdr) < 0) {
6875 nlmsg_free(msg);
6876 return;
6877 }
6878
6879 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6880 nl80211_mlme_mcgrp.id, gfp);
6881 return;
6882
6883 nla_put_failure:
6884 genlmsg_cancel(msg, hdr);
6885 nlmsg_free(msg);
6886}
6887
Jouni Malinena3b8b052009-03-27 21:59:49 +02006888void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6889 struct net_device *netdev, const u8 *addr,
6890 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006891 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006892{
6893 struct sk_buff *msg;
6894 void *hdr;
6895
Johannes Berge6d6e342009-07-01 21:26:47 +02006896 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006897 if (!msg)
6898 return;
6899
6900 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6901 if (!hdr) {
6902 nlmsg_free(msg);
6903 return;
6904 }
6905
6906 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6907 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6908 if (addr)
6909 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6910 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03006911 if (key_id != -1)
6912 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006913 if (tsc)
6914 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6915
6916 if (genlmsg_end(msg, hdr) < 0) {
6917 nlmsg_free(msg);
6918 return;
6919 }
6920
Johannes Berg463d0182009-07-14 00:33:35 +02006921 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6922 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006923 return;
6924
6925 nla_put_failure:
6926 genlmsg_cancel(msg, hdr);
6927 nlmsg_free(msg);
6928}
6929
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006930void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6931 struct ieee80211_channel *channel_before,
6932 struct ieee80211_channel *channel_after)
6933{
6934 struct sk_buff *msg;
6935 void *hdr;
6936 struct nlattr *nl_freq;
6937
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006938 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006939 if (!msg)
6940 return;
6941
6942 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6943 if (!hdr) {
6944 nlmsg_free(msg);
6945 return;
6946 }
6947
6948 /*
6949 * Since we are applying the beacon hint to a wiphy we know its
6950 * wiphy_idx is valid
6951 */
6952 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6953
6954 /* Before */
6955 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6956 if (!nl_freq)
6957 goto nla_put_failure;
6958 if (nl80211_msg_put_channel(msg, channel_before))
6959 goto nla_put_failure;
6960 nla_nest_end(msg, nl_freq);
6961
6962 /* After */
6963 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6964 if (!nl_freq)
6965 goto nla_put_failure;
6966 if (nl80211_msg_put_channel(msg, channel_after))
6967 goto nla_put_failure;
6968 nla_nest_end(msg, nl_freq);
6969
6970 if (genlmsg_end(msg, hdr) < 0) {
6971 nlmsg_free(msg);
6972 return;
6973 }
6974
Johannes Berg463d0182009-07-14 00:33:35 +02006975 rcu_read_lock();
6976 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6977 GFP_ATOMIC);
6978 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006979
6980 return;
6981
6982nla_put_failure:
6983 genlmsg_cancel(msg, hdr);
6984 nlmsg_free(msg);
6985}
6986
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006987static void nl80211_send_remain_on_chan_event(
6988 int cmd, struct cfg80211_registered_device *rdev,
6989 struct net_device *netdev, u64 cookie,
6990 struct ieee80211_channel *chan,
6991 enum nl80211_channel_type channel_type,
6992 unsigned int duration, gfp_t gfp)
6993{
6994 struct sk_buff *msg;
6995 void *hdr;
6996
6997 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6998 if (!msg)
6999 return;
7000
7001 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7002 if (!hdr) {
7003 nlmsg_free(msg);
7004 return;
7005 }
7006
7007 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7009 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7010 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7011 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7012
7013 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7014 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7015
7016 if (genlmsg_end(msg, hdr) < 0) {
7017 nlmsg_free(msg);
7018 return;
7019 }
7020
7021 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7022 nl80211_mlme_mcgrp.id, gfp);
7023 return;
7024
7025 nla_put_failure:
7026 genlmsg_cancel(msg, hdr);
7027 nlmsg_free(msg);
7028}
7029
7030void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7031 struct net_device *netdev, u64 cookie,
7032 struct ieee80211_channel *chan,
7033 enum nl80211_channel_type channel_type,
7034 unsigned int duration, gfp_t gfp)
7035{
7036 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7037 rdev, netdev, cookie, chan,
7038 channel_type, duration, gfp);
7039}
7040
7041void nl80211_send_remain_on_channel_cancel(
7042 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7043 u64 cookie, struct ieee80211_channel *chan,
7044 enum nl80211_channel_type channel_type, gfp_t gfp)
7045{
7046 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7047 rdev, netdev, cookie, chan,
7048 channel_type, 0, gfp);
7049}
7050
Johannes Berg98b62182009-12-23 13:15:44 +01007051void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7052 struct net_device *dev, const u8 *mac_addr,
7053 struct station_info *sinfo, gfp_t gfp)
7054{
7055 struct sk_buff *msg;
7056
7057 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7058 if (!msg)
7059 return;
7060
7061 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7062 nlmsg_free(msg);
7063 return;
7064 }
7065
7066 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7067 nl80211_mlme_mcgrp.id, gfp);
7068}
7069
Jouni Malinenec15e682011-03-23 15:29:52 +02007070void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7071 struct net_device *dev, const u8 *mac_addr,
7072 gfp_t gfp)
7073{
7074 struct sk_buff *msg;
7075 void *hdr;
7076
7077 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7078 if (!msg)
7079 return;
7080
7081 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7082 if (!hdr) {
7083 nlmsg_free(msg);
7084 return;
7085 }
7086
7087 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7088 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7089
7090 if (genlmsg_end(msg, hdr) < 0) {
7091 nlmsg_free(msg);
7092 return;
7093 }
7094
7095 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7096 nl80211_mlme_mcgrp.id, gfp);
7097 return;
7098
7099 nla_put_failure:
7100 genlmsg_cancel(msg, hdr);
7101 nlmsg_free(msg);
7102}
7103
Johannes Berg2e161f72010-08-12 15:38:38 +02007104int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7105 struct net_device *netdev, u32 nlpid,
7106 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007107{
7108 struct sk_buff *msg;
7109 void *hdr;
7110 int err;
7111
7112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7113 if (!msg)
7114 return -ENOMEM;
7115
Johannes Berg2e161f72010-08-12 15:38:38 +02007116 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007117 if (!hdr) {
7118 nlmsg_free(msg);
7119 return -ENOMEM;
7120 }
7121
7122 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7123 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7124 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7125 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7126
7127 err = genlmsg_end(msg, hdr);
7128 if (err < 0) {
7129 nlmsg_free(msg);
7130 return err;
7131 }
7132
7133 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7134 if (err < 0)
7135 return err;
7136 return 0;
7137
7138 nla_put_failure:
7139 genlmsg_cancel(msg, hdr);
7140 nlmsg_free(msg);
7141 return -ENOBUFS;
7142}
7143
Johannes Berg2e161f72010-08-12 15:38:38 +02007144void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7145 struct net_device *netdev, u64 cookie,
7146 const u8 *buf, size_t len, bool ack,
7147 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007148{
7149 struct sk_buff *msg;
7150 void *hdr;
7151
7152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7153 if (!msg)
7154 return;
7155
Johannes Berg2e161f72010-08-12 15:38:38 +02007156 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007157 if (!hdr) {
7158 nlmsg_free(msg);
7159 return;
7160 }
7161
7162 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7163 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7164 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7165 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7166 if (ack)
7167 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7168
7169 if (genlmsg_end(msg, hdr) < 0) {
7170 nlmsg_free(msg);
7171 return;
7172 }
7173
7174 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7175 return;
7176
7177 nla_put_failure:
7178 genlmsg_cancel(msg, hdr);
7179 nlmsg_free(msg);
7180}
7181
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007182void
7183nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7184 struct net_device *netdev,
7185 enum nl80211_cqm_rssi_threshold_event rssi_event,
7186 gfp_t gfp)
7187{
7188 struct sk_buff *msg;
7189 struct nlattr *pinfoattr;
7190 void *hdr;
7191
7192 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7193 if (!msg)
7194 return;
7195
7196 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7197 if (!hdr) {
7198 nlmsg_free(msg);
7199 return;
7200 }
7201
7202 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7203 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7204
7205 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7206 if (!pinfoattr)
7207 goto nla_put_failure;
7208
7209 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7210 rssi_event);
7211
7212 nla_nest_end(msg, pinfoattr);
7213
7214 if (genlmsg_end(msg, hdr) < 0) {
7215 nlmsg_free(msg);
7216 return;
7217 }
7218
7219 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7220 nl80211_mlme_mcgrp.id, gfp);
7221 return;
7222
7223 nla_put_failure:
7224 genlmsg_cancel(msg, hdr);
7225 nlmsg_free(msg);
7226}
7227
Johannes Berge5497d72011-07-05 16:35:40 +02007228void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7229 struct net_device *netdev, const u8 *bssid,
7230 const u8 *replay_ctr, gfp_t gfp)
7231{
7232 struct sk_buff *msg;
7233 struct nlattr *rekey_attr;
7234 void *hdr;
7235
7236 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7237 if (!msg)
7238 return;
7239
7240 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7241 if (!hdr) {
7242 nlmsg_free(msg);
7243 return;
7244 }
7245
7246 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7247 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7248 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7249
7250 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7251 if (!rekey_attr)
7252 goto nla_put_failure;
7253
7254 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7255 NL80211_REPLAY_CTR_LEN, replay_ctr);
7256
7257 nla_nest_end(msg, rekey_attr);
7258
7259 if (genlmsg_end(msg, hdr) < 0) {
7260 nlmsg_free(msg);
7261 return;
7262 }
7263
7264 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7265 nl80211_mlme_mcgrp.id, gfp);
7266 return;
7267
7268 nla_put_failure:
7269 genlmsg_cancel(msg, hdr);
7270 nlmsg_free(msg);
7271}
7272
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007273void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7274 struct net_device *netdev, int index,
7275 const u8 *bssid, bool preauth, gfp_t gfp)
7276{
7277 struct sk_buff *msg;
7278 struct nlattr *attr;
7279 void *hdr;
7280
7281 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7282 if (!msg)
7283 return;
7284
7285 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7286 if (!hdr) {
7287 nlmsg_free(msg);
7288 return;
7289 }
7290
7291 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7292 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7293
7294 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7295 if (!attr)
7296 goto nla_put_failure;
7297
7298 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7299 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7300 if (preauth)
7301 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7302
7303 nla_nest_end(msg, attr);
7304
7305 if (genlmsg_end(msg, hdr) < 0) {
7306 nlmsg_free(msg);
7307 return;
7308 }
7309
7310 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7311 nl80211_mlme_mcgrp.id, gfp);
7312 return;
7313
7314 nla_put_failure:
7315 genlmsg_cancel(msg, hdr);
7316 nlmsg_free(msg);
7317}
7318
Johannes Bergc063dbf2010-11-24 08:10:05 +01007319void
7320nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7321 struct net_device *netdev, const u8 *peer,
7322 u32 num_packets, gfp_t gfp)
7323{
7324 struct sk_buff *msg;
7325 struct nlattr *pinfoattr;
7326 void *hdr;
7327
7328 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7329 if (!msg)
7330 return;
7331
7332 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7333 if (!hdr) {
7334 nlmsg_free(msg);
7335 return;
7336 }
7337
7338 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7339 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7340 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7341
7342 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7343 if (!pinfoattr)
7344 goto nla_put_failure;
7345
7346 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7347
7348 nla_nest_end(msg, pinfoattr);
7349
7350 if (genlmsg_end(msg, hdr) < 0) {
7351 nlmsg_free(msg);
7352 return;
7353 }
7354
7355 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7356 nl80211_mlme_mcgrp.id, gfp);
7357 return;
7358
7359 nla_put_failure:
7360 genlmsg_cancel(msg, hdr);
7361 nlmsg_free(msg);
7362}
7363
Jouni Malinen026331c2010-02-15 12:53:10 +02007364static int nl80211_netlink_notify(struct notifier_block * nb,
7365 unsigned long state,
7366 void *_notify)
7367{
7368 struct netlink_notify *notify = _notify;
7369 struct cfg80211_registered_device *rdev;
7370 struct wireless_dev *wdev;
7371
7372 if (state != NETLINK_URELEASE)
7373 return NOTIFY_DONE;
7374
7375 rcu_read_lock();
7376
7377 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7378 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007379 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007380
7381 rcu_read_unlock();
7382
7383 return NOTIFY_DONE;
7384}
7385
7386static struct notifier_block nl80211_netlink_notifier = {
7387 .notifier_call = nl80211_netlink_notify,
7388};
7389
Johannes Berg55682962007-09-20 13:09:35 -04007390/* initialisation/exit functions */
7391
7392int nl80211_init(void)
7393{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007394 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007395
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007396 err = genl_register_family_with_ops(&nl80211_fam,
7397 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007398 if (err)
7399 return err;
7400
Johannes Berg55682962007-09-20 13:09:35 -04007401 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7402 if (err)
7403 goto err_out;
7404
Johannes Berg2a519312009-02-10 21:25:55 +01007405 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7406 if (err)
7407 goto err_out;
7408
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007409 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7410 if (err)
7411 goto err_out;
7412
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007413 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7414 if (err)
7415 goto err_out;
7416
Johannes Bergaff89a92009-07-01 21:26:51 +02007417#ifdef CONFIG_NL80211_TESTMODE
7418 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7419 if (err)
7420 goto err_out;
7421#endif
7422
Jouni Malinen026331c2010-02-15 12:53:10 +02007423 err = netlink_register_notifier(&nl80211_netlink_notifier);
7424 if (err)
7425 goto err_out;
7426
Johannes Berg55682962007-09-20 13:09:35 -04007427 return 0;
7428 err_out:
7429 genl_unregister_family(&nl80211_fam);
7430 return err;
7431}
7432
7433void nl80211_exit(void)
7434{
Jouni Malinen026331c2010-02-15 12:53:10 +02007435 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007436 genl_unregister_family(&nl80211_fam);
7437}