blob: bf3fc4f264f50af6f78413c9537d460d704a52de [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 Bergee688b002008-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 Bergee688b002008-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 Bergee688b002008-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 Bergee688b002008-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 Bergee688b002008-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 Bergee688b002008-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 Bergee688b002008-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 Pirkof350a0a82010-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 Malinen3cfcf6ac2009-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;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002646 }
2647
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002648 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002649 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002650 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002651 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2652 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002653
Johannes Berg463d0182009-07-14 00:33:35 +02002654 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002655 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002656 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002657
2658 /* validate settings */
2659 err = 0;
2660
Johannes Berg79c97e92009-07-07 03:56:12 +02002661 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002662 err = -EOPNOTSUPP;
2663 goto out;
2664 }
2665
Johannes Berg79c97e92009-07-07 03:56:12 +02002666 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002667
2668 out:
2669 if (params.vlan)
2670 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002671 return err;
2672}
2673
2674static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2675{
Johannes Berg4c476992010-10-04 21:36:35 +02002676 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2677 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002678 u8 *mac_addr = NULL;
2679
2680 if (info->attrs[NL80211_ATTR_MAC])
2681 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2682
Johannes Berge80cf852009-05-11 14:43:13 +02002683 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002684 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002685 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002686 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2687 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002688
Johannes Berg4c476992010-10-04 21:36:35 +02002689 if (!rdev->ops->del_station)
2690 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002691
Johannes Berg4c476992010-10-04 21:36:35 +02002692 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002693}
2694
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002695static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2696 int flags, struct net_device *dev,
2697 u8 *dst, u8 *next_hop,
2698 struct mpath_info *pinfo)
2699{
2700 void *hdr;
2701 struct nlattr *pinfoattr;
2702
2703 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2704 if (!hdr)
2705 return -1;
2706
2707 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2708 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2709 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2710
Johannes Bergf5ea9122009-08-07 16:17:38 +02002711 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2712
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002713 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2714 if (!pinfoattr)
2715 goto nla_put_failure;
2716 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2717 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2718 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002719 if (pinfo->filled & MPATH_INFO_SN)
2720 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2721 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002722 if (pinfo->filled & MPATH_INFO_METRIC)
2723 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2724 pinfo->metric);
2725 if (pinfo->filled & MPATH_INFO_EXPTIME)
2726 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2727 pinfo->exptime);
2728 if (pinfo->filled & MPATH_INFO_FLAGS)
2729 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2730 pinfo->flags);
2731 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2732 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2733 pinfo->discovery_timeout);
2734 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2735 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2736 pinfo->discovery_retries);
2737
2738 nla_nest_end(msg, pinfoattr);
2739
2740 return genlmsg_end(msg, hdr);
2741
2742 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002743 genlmsg_cancel(msg, hdr);
2744 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002745}
2746
2747static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002748 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002749{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002750 struct mpath_info pinfo;
2751 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002752 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002753 u8 dst[ETH_ALEN];
2754 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002755 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002756 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002757
Johannes Berg67748892010-10-04 21:14:06 +02002758 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2759 if (err)
2760 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002761
2762 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002763 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002764 goto out_err;
2765 }
2766
Jouni Malineneec60b02009-03-20 21:21:19 +02002767 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2768 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002769 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002770 }
2771
Johannes Bergbba95fe2008-07-29 13:22:51 +02002772 while (1) {
2773 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2774 dst, next_hop, &pinfo);
2775 if (err == -ENOENT)
2776 break;
2777 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002778 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002779
2780 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2781 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2782 netdev, dst, next_hop,
2783 &pinfo) < 0)
2784 goto out;
2785
2786 path_idx++;
2787 }
2788
2789
2790 out:
2791 cb->args[1] = path_idx;
2792 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002793 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002794 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002795 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002796}
2797
2798static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2799{
Johannes Berg4c476992010-10-04 21:36:35 +02002800 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002801 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002802 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002803 struct mpath_info pinfo;
2804 struct sk_buff *msg;
2805 u8 *dst = NULL;
2806 u8 next_hop[ETH_ALEN];
2807
2808 memset(&pinfo, 0, sizeof(pinfo));
2809
2810 if (!info->attrs[NL80211_ATTR_MAC])
2811 return -EINVAL;
2812
2813 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2814
Johannes Berg4c476992010-10-04 21:36:35 +02002815 if (!rdev->ops->get_mpath)
2816 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002817
Johannes Berg4c476992010-10-04 21:36:35 +02002818 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2819 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002820
Johannes Berg79c97e92009-07-07 03:56:12 +02002821 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002822 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002823 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002824
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002825 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002826 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002827 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002828
2829 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002830 dev, dst, next_hop, &pinfo) < 0) {
2831 nlmsg_free(msg);
2832 return -ENOBUFS;
2833 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002834
Johannes Berg4c476992010-10-04 21:36:35 +02002835 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002836}
2837
2838static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2839{
Johannes Berg4c476992010-10-04 21:36:35 +02002840 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2841 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002842 u8 *dst = NULL;
2843 u8 *next_hop = NULL;
2844
2845 if (!info->attrs[NL80211_ATTR_MAC])
2846 return -EINVAL;
2847
2848 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2849 return -EINVAL;
2850
2851 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2852 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2853
Johannes Berg4c476992010-10-04 21:36:35 +02002854 if (!rdev->ops->change_mpath)
2855 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002856
Johannes Berg4c476992010-10-04 21:36:35 +02002857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2858 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002859
Johannes Berg4c476992010-10-04 21:36:35 +02002860 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002861}
Johannes Berg4c476992010-10-04 21:36:35 +02002862
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002863static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2864{
Johannes Berg4c476992010-10-04 21:36:35 +02002865 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2866 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002867 u8 *dst = NULL;
2868 u8 *next_hop = NULL;
2869
2870 if (!info->attrs[NL80211_ATTR_MAC])
2871 return -EINVAL;
2872
2873 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2874 return -EINVAL;
2875
2876 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2877 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2878
Johannes Berg4c476992010-10-04 21:36:35 +02002879 if (!rdev->ops->add_mpath)
2880 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002881
Johannes Berg4c476992010-10-04 21:36:35 +02002882 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2883 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002884
Johannes Berg4c476992010-10-04 21:36:35 +02002885 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002886}
2887
2888static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2889{
Johannes Berg4c476992010-10-04 21:36:35 +02002890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2891 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002892 u8 *dst = NULL;
2893
2894 if (info->attrs[NL80211_ATTR_MAC])
2895 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2896
Johannes Berg4c476992010-10-04 21:36:35 +02002897 if (!rdev->ops->del_mpath)
2898 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002899
Johannes Berg4c476992010-10-04 21:36:35 +02002900 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002901}
2902
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002903static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2904{
Johannes Berg4c476992010-10-04 21:36:35 +02002905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2906 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002907 struct bss_parameters params;
2908
2909 memset(&params, 0, sizeof(params));
2910 /* default to not changing parameters */
2911 params.use_cts_prot = -1;
2912 params.use_short_preamble = -1;
2913 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002914 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002915 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002916
2917 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2918 params.use_cts_prot =
2919 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2920 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2921 params.use_short_preamble =
2922 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2923 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2924 params.use_short_slot_time =
2925 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002926 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2927 params.basic_rates =
2928 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2929 params.basic_rates_len =
2930 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2931 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002932 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2933 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002934 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2935 params.ht_opmode =
2936 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002937
Johannes Berg4c476992010-10-04 21:36:35 +02002938 if (!rdev->ops->change_bss)
2939 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002940
Johannes Berg074ac8d2010-09-16 14:58:22 +02002941 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002942 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2943 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002944
Johannes Berg4c476992010-10-04 21:36:35 +02002945 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002946}
2947
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002948static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002949 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2950 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2951 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2952 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2953 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2954 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2955};
2956
2957static int parse_reg_rule(struct nlattr *tb[],
2958 struct ieee80211_reg_rule *reg_rule)
2959{
2960 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2961 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2962
2963 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2964 return -EINVAL;
2965 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2966 return -EINVAL;
2967 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2968 return -EINVAL;
2969 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2970 return -EINVAL;
2971 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2972 return -EINVAL;
2973
2974 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2975
2976 freq_range->start_freq_khz =
2977 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2978 freq_range->end_freq_khz =
2979 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2980 freq_range->max_bandwidth_khz =
2981 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2982
2983 power_rule->max_eirp =
2984 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2985
2986 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2987 power_rule->max_antenna_gain =
2988 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2989
2990 return 0;
2991}
2992
2993static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2994{
2995 int r;
2996 char *data = NULL;
2997
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002998 /*
2999 * You should only get this when cfg80211 hasn't yet initialized
3000 * completely when built-in to the kernel right between the time
3001 * window between nl80211_init() and regulatory_init(), if that is
3002 * even possible.
3003 */
3004 mutex_lock(&cfg80211_mutex);
3005 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003006 mutex_unlock(&cfg80211_mutex);
3007 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003008 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003009 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003010
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003011 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3012 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003013
3014 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3015
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003016 r = regulatory_hint_user(data);
3017
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003018 return r;
3019}
3020
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003021static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003022 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003023{
Johannes Berg4c476992010-10-04 21:36:35 +02003024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003025 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003026 struct wireless_dev *wdev = dev->ieee80211_ptr;
3027 struct mesh_config cur_params;
3028 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003029 void *hdr;
3030 struct nlattr *pinfoattr;
3031 struct sk_buff *msg;
3032
Johannes Berg29cbe682010-12-03 09:20:44 +01003033 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3034 return -EOPNOTSUPP;
3035
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003036 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003037 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003038
Johannes Berg29cbe682010-12-03 09:20:44 +01003039 wdev_lock(wdev);
3040 /* If not connected, get default parameters */
3041 if (!wdev->mesh_id_len)
3042 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3043 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003044 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003045 &cur_params);
3046 wdev_unlock(wdev);
3047
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003048 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003049 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003050
3051 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003052 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003053 if (!msg)
3054 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003055 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003056 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003057 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003058 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003059 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003060 if (!pinfoattr)
3061 goto nla_put_failure;
3062 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3063 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3064 cur_params.dot11MeshRetryTimeout);
3065 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3066 cur_params.dot11MeshConfirmTimeout);
3067 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3068 cur_params.dot11MeshHoldingTimeout);
3069 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3070 cur_params.dot11MeshMaxPeerLinks);
3071 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3072 cur_params.dot11MeshMaxRetries);
3073 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3074 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003075 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3076 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003077 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3078 cur_params.auto_open_plinks);
3079 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3080 cur_params.dot11MeshHWMPmaxPREQretries);
3081 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3082 cur_params.path_refresh_time);
3083 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3084 cur_params.min_discovery_timeout);
3085 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3086 cur_params.dot11MeshHWMPactivePathTimeout);
3087 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3088 cur_params.dot11MeshHWMPpreqMinInterval);
3089 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3090 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003091 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3092 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003093 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3094 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003095 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3096 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003097 nla_nest_end(msg, pinfoattr);
3098 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003099 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003100
Johannes Berg3b858752009-03-12 09:55:09 +01003101 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003102 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003103 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003104 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003105 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003106}
3107
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003108static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003109 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3110 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3111 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3112 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3113 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3114 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003115 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003116 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3117
3118 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3119 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3120 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3121 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3122 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3123 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003124 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003125 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003126 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003127};
3128
Javier Cardonac80d5452010-12-16 17:37:49 -08003129static const struct nla_policy
3130 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3131 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3132 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003133 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003134 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003135 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003136 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003137};
3138
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003139static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003140 struct mesh_config *cfg,
3141 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003142{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003143 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003144 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003145
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003146#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3147do {\
3148 if (table[attr_num]) {\
3149 cfg->param = nla_fn(table[attr_num]); \
3150 mask |= (1 << (attr_num - 1)); \
3151 } \
3152} while (0);\
3153
3154
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003155 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003156 return -EINVAL;
3157 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003158 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003159 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003160 return -EINVAL;
3161
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003162 /* This makes sure that there aren't more than 32 mesh config
3163 * parameters (otherwise our bitfield scheme would not work.) */
3164 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3165
3166 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003167 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3168 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3169 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3170 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3171 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3172 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3173 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3174 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3175 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3176 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3177 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3178 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003179 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3180 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003181 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3182 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3183 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3184 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3185 nla_get_u8);
3186 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3187 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3189 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3190 nla_get_u16);
3191 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3192 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3193 nla_get_u32);
3194 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3195 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3196 nla_get_u16);
3197 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3198 dot11MeshHWMPnetDiameterTraversalTime,
3199 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3200 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003201 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3202 dot11MeshHWMPRootMode, mask,
3203 NL80211_MESHCONF_HWMP_ROOTMODE,
3204 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003205 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3206 dot11MeshHWMPRannInterval, mask,
3207 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3208 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003209 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3210 dot11MeshGateAnnouncementProtocol, mask,
3211 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3212 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003213 if (mask_out)
3214 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003215
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003216 return 0;
3217
3218#undef FILL_IN_MESH_PARAM_IF_SET
3219}
3220
Javier Cardonac80d5452010-12-16 17:37:49 -08003221static int nl80211_parse_mesh_setup(struct genl_info *info,
3222 struct mesh_setup *setup)
3223{
3224 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3225
3226 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3227 return -EINVAL;
3228 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3229 info->attrs[NL80211_ATTR_MESH_SETUP],
3230 nl80211_mesh_setup_params_policy))
3231 return -EINVAL;
3232
3233 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3234 setup->path_sel_proto =
3235 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3236 IEEE80211_PATH_PROTOCOL_VENDOR :
3237 IEEE80211_PATH_PROTOCOL_HWMP;
3238
3239 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3240 setup->path_metric =
3241 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3242 IEEE80211_PATH_METRIC_VENDOR :
3243 IEEE80211_PATH_METRIC_AIRTIME;
3244
Javier Cardona581a8b02011-04-07 15:08:27 -07003245
3246 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003247 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003248 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003249 if (!is_valid_ie_attr(ieattr))
3250 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003251 setup->ie = nla_data(ieattr);
3252 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003253 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003254 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3255 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003256
3257 return 0;
3258}
3259
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003260static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003261 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003262{
3263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3264 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003265 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003266 struct mesh_config cfg;
3267 u32 mask;
3268 int err;
3269
Johannes Berg29cbe682010-12-03 09:20:44 +01003270 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3271 return -EOPNOTSUPP;
3272
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003273 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003274 return -EOPNOTSUPP;
3275
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003276 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003277 if (err)
3278 return err;
3279
Johannes Berg29cbe682010-12-03 09:20:44 +01003280 wdev_lock(wdev);
3281 if (!wdev->mesh_id_len)
3282 err = -ENOLINK;
3283
3284 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003285 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003286 mask, &cfg);
3287
3288 wdev_unlock(wdev);
3289
3290 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003291}
3292
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003293static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3294{
3295 struct sk_buff *msg;
3296 void *hdr = NULL;
3297 struct nlattr *nl_reg_rules;
3298 unsigned int i;
3299 int err = -EINVAL;
3300
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003301 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003302
3303 if (!cfg80211_regdomain)
3304 goto out;
3305
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003307 if (!msg) {
3308 err = -ENOBUFS;
3309 goto out;
3310 }
3311
3312 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3313 NL80211_CMD_GET_REG);
3314 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003315 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003316
3317 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3318 cfg80211_regdomain->alpha2);
3319
3320 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3321 if (!nl_reg_rules)
3322 goto nla_put_failure;
3323
3324 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3325 struct nlattr *nl_reg_rule;
3326 const struct ieee80211_reg_rule *reg_rule;
3327 const struct ieee80211_freq_range *freq_range;
3328 const struct ieee80211_power_rule *power_rule;
3329
3330 reg_rule = &cfg80211_regdomain->reg_rules[i];
3331 freq_range = &reg_rule->freq_range;
3332 power_rule = &reg_rule->power_rule;
3333
3334 nl_reg_rule = nla_nest_start(msg, i);
3335 if (!nl_reg_rule)
3336 goto nla_put_failure;
3337
3338 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3339 reg_rule->flags);
3340 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3341 freq_range->start_freq_khz);
3342 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3343 freq_range->end_freq_khz);
3344 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3345 freq_range->max_bandwidth_khz);
3346 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3347 power_rule->max_antenna_gain);
3348 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3349 power_rule->max_eirp);
3350
3351 nla_nest_end(msg, nl_reg_rule);
3352 }
3353
3354 nla_nest_end(msg, nl_reg_rules);
3355
3356 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003357 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003358 goto out;
3359
3360nla_put_failure:
3361 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003362put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003363 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003364 err = -EMSGSIZE;
3365out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003366 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003367 return err;
3368}
3369
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003370static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3371{
3372 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3373 struct nlattr *nl_reg_rule;
3374 char *alpha2 = NULL;
3375 int rem_reg_rules = 0, r = 0;
3376 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3377 struct ieee80211_regdomain *rd = NULL;
3378
3379 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3380 return -EINVAL;
3381
3382 if (!info->attrs[NL80211_ATTR_REG_RULES])
3383 return -EINVAL;
3384
3385 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3386
3387 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3388 rem_reg_rules) {
3389 num_rules++;
3390 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003391 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003392 }
3393
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003394 mutex_lock(&cfg80211_mutex);
3395
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003396 if (!reg_is_valid_request(alpha2)) {
3397 r = -EINVAL;
3398 goto bad_reg;
3399 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003400
3401 size_of_regd = sizeof(struct ieee80211_regdomain) +
3402 (num_rules * sizeof(struct ieee80211_reg_rule));
3403
3404 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003405 if (!rd) {
3406 r = -ENOMEM;
3407 goto bad_reg;
3408 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003409
3410 rd->n_reg_rules = num_rules;
3411 rd->alpha2[0] = alpha2[0];
3412 rd->alpha2[1] = alpha2[1];
3413
3414 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3415 rem_reg_rules) {
3416 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3417 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3418 reg_rule_policy);
3419 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3420 if (r)
3421 goto bad_reg;
3422
3423 rule_idx++;
3424
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003425 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3426 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003427 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003428 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003429 }
3430
3431 BUG_ON(rule_idx != num_rules);
3432
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003433 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003434
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003435 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003436
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003437 return r;
3438
Johannes Bergd2372b32008-10-24 20:32:20 +02003439 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003440 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003441 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003442 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003443}
3444
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003445static int validate_scan_freqs(struct nlattr *freqs)
3446{
3447 struct nlattr *attr1, *attr2;
3448 int n_channels = 0, tmp1, tmp2;
3449
3450 nla_for_each_nested(attr1, freqs, tmp1) {
3451 n_channels++;
3452 /*
3453 * Some hardware has a limited channel list for
3454 * scanning, and it is pretty much nonsensical
3455 * to scan for a channel twice, so disallow that
3456 * and don't require drivers to check that the
3457 * channel list they get isn't longer than what
3458 * they can scan, as long as they can scan all
3459 * the channels they registered at once.
3460 */
3461 nla_for_each_nested(attr2, freqs, tmp2)
3462 if (attr1 != attr2 &&
3463 nla_get_u32(attr1) == nla_get_u32(attr2))
3464 return 0;
3465 }
3466
3467 return n_channels;
3468}
3469
Johannes Berg2a519312009-02-10 21:25:55 +01003470static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3471{
Johannes Berg4c476992010-10-04 21:36:35 +02003472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3473 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003474 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003475 struct nlattr *attr;
3476 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003477 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003478 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003479
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003480 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3481 return -EINVAL;
3482
Johannes Berg79c97e92009-07-07 03:56:12 +02003483 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003484
Johannes Berg4c476992010-10-04 21:36:35 +02003485 if (!rdev->ops->scan)
3486 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003487
Johannes Berg4c476992010-10-04 21:36:35 +02003488 if (rdev->scan_req)
3489 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003490
3491 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003492 n_channels = validate_scan_freqs(
3493 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003494 if (!n_channels)
3495 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003496 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003497 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003498 n_channels = 0;
3499
Johannes Berg2a519312009-02-10 21:25:55 +01003500 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3501 if (wiphy->bands[band])
3502 n_channels += wiphy->bands[band]->n_channels;
3503 }
3504
3505 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3506 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3507 n_ssids++;
3508
Johannes Berg4c476992010-10-04 21:36:35 +02003509 if (n_ssids > wiphy->max_scan_ssids)
3510 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003511
Jouni Malinen70692ad2009-02-16 19:39:13 +02003512 if (info->attrs[NL80211_ATTR_IE])
3513 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3514 else
3515 ie_len = 0;
3516
Johannes Berg4c476992010-10-04 21:36:35 +02003517 if (ie_len > wiphy->max_scan_ie_len)
3518 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003519
Johannes Berg2a519312009-02-10 21:25:55 +01003520 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003521 + sizeof(*request->ssids) * n_ssids
3522 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003523 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003524 if (!request)
3525 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003526
Johannes Berg2a519312009-02-10 21:25:55 +01003527 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003528 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003529 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003530 if (ie_len) {
3531 if (request->ssids)
3532 request->ie = (void *)(request->ssids + n_ssids);
3533 else
3534 request->ie = (void *)(request->channels + n_channels);
3535 }
Johannes Berg2a519312009-02-10 21:25:55 +01003536
Johannes Berg584991d2009-11-02 13:32:03 +01003537 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003538 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3539 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003540 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003541 struct ieee80211_channel *chan;
3542
3543 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3544
3545 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003546 err = -EINVAL;
3547 goto out_free;
3548 }
Johannes Berg584991d2009-11-02 13:32:03 +01003549
3550 /* ignore disabled channels */
3551 if (chan->flags & IEEE80211_CHAN_DISABLED)
3552 continue;
3553
3554 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003555 i++;
3556 }
3557 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003558 enum ieee80211_band band;
3559
Johannes Berg2a519312009-02-10 21:25:55 +01003560 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003561 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3562 int j;
3563 if (!wiphy->bands[band])
3564 continue;
3565 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003566 struct ieee80211_channel *chan;
3567
3568 chan = &wiphy->bands[band]->channels[j];
3569
3570 if (chan->flags & IEEE80211_CHAN_DISABLED)
3571 continue;
3572
3573 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003574 i++;
3575 }
3576 }
3577 }
3578
Johannes Berg584991d2009-11-02 13:32:03 +01003579 if (!i) {
3580 err = -EINVAL;
3581 goto out_free;
3582 }
3583
3584 request->n_channels = i;
3585
Johannes Berg2a519312009-02-10 21:25:55 +01003586 i = 0;
3587 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3588 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003589 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003590 err = -EINVAL;
3591 goto out_free;
3592 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003593 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003594 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003595 i++;
3596 }
3597 }
3598
Jouni Malinen70692ad2009-02-16 19:39:13 +02003599 if (info->attrs[NL80211_ATTR_IE]) {
3600 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003601 memcpy((void *)request->ie,
3602 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003603 request->ie_len);
3604 }
3605
Johannes Berg34850ab2011-07-18 18:08:35 +02003606 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003607 if (wiphy->bands[i])
3608 request->rates[i] =
3609 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003610
3611 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3612 nla_for_each_nested(attr,
3613 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3614 tmp) {
3615 enum ieee80211_band band = nla_type(attr);
3616
Dan Carpenter84404622011-07-29 11:52:18 +03003617 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003618 err = -EINVAL;
3619 goto out_free;
3620 }
3621 err = ieee80211_get_ratemask(wiphy->bands[band],
3622 nla_data(attr),
3623 nla_len(attr),
3624 &request->rates[band]);
3625 if (err)
3626 goto out_free;
3627 }
3628 }
3629
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303630 request->no_cck =
3631 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3632
Johannes Berg463d0182009-07-14 00:33:35 +02003633 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003634 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003635
Johannes Berg79c97e92009-07-07 03:56:12 +02003636 rdev->scan_req = request;
3637 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003638
Johannes Berg463d0182009-07-14 00:33:35 +02003639 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003640 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003641 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003642 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003643 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003644 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003645 kfree(request);
3646 }
Johannes Berg3b858752009-03-12 09:55:09 +01003647
Johannes Berg2a519312009-02-10 21:25:55 +01003648 return err;
3649}
3650
Luciano Coelho807f8a82011-05-11 17:09:35 +03003651static int nl80211_start_sched_scan(struct sk_buff *skb,
3652 struct genl_info *info)
3653{
3654 struct cfg80211_sched_scan_request *request;
3655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3656 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003657 struct nlattr *attr;
3658 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003659 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003660 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003661 enum ieee80211_band band;
3662 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003663 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003664
3665 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3666 !rdev->ops->sched_scan_start)
3667 return -EOPNOTSUPP;
3668
3669 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3670 return -EINVAL;
3671
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003672 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3673 return -EINVAL;
3674
3675 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3676 if (interval == 0)
3677 return -EINVAL;
3678
Luciano Coelho807f8a82011-05-11 17:09:35 +03003679 wiphy = &rdev->wiphy;
3680
3681 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3682 n_channels = validate_scan_freqs(
3683 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3684 if (!n_channels)
3685 return -EINVAL;
3686 } else {
3687 n_channels = 0;
3688
3689 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3690 if (wiphy->bands[band])
3691 n_channels += wiphy->bands[band]->n_channels;
3692 }
3693
3694 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3695 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3696 tmp)
3697 n_ssids++;
3698
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003699 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003700 return -EINVAL;
3701
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003702 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3703 nla_for_each_nested(attr,
3704 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3705 tmp)
3706 n_match_sets++;
3707
3708 if (n_match_sets > wiphy->max_match_sets)
3709 return -EINVAL;
3710
Luciano Coelho807f8a82011-05-11 17:09:35 +03003711 if (info->attrs[NL80211_ATTR_IE])
3712 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3713 else
3714 ie_len = 0;
3715
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003716 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003717 return -EINVAL;
3718
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003719 mutex_lock(&rdev->sched_scan_mtx);
3720
3721 if (rdev->sched_scan_req) {
3722 err = -EINPROGRESS;
3723 goto out;
3724 }
3725
Luciano Coelho807f8a82011-05-11 17:09:35 +03003726 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003727 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003728 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003729 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003730 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003731 if (!request) {
3732 err = -ENOMEM;
3733 goto out;
3734 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003735
3736 if (n_ssids)
3737 request->ssids = (void *)&request->channels[n_channels];
3738 request->n_ssids = n_ssids;
3739 if (ie_len) {
3740 if (request->ssids)
3741 request->ie = (void *)(request->ssids + n_ssids);
3742 else
3743 request->ie = (void *)(request->channels + n_channels);
3744 }
3745
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003746 if (n_match_sets) {
3747 if (request->ie)
3748 request->match_sets = (void *)(request->ie + ie_len);
3749 else if (request->ssids)
3750 request->match_sets =
3751 (void *)(request->ssids + n_ssids);
3752 else
3753 request->match_sets =
3754 (void *)(request->channels + n_channels);
3755 }
3756 request->n_match_sets = n_match_sets;
3757
Luciano Coelho807f8a82011-05-11 17:09:35 +03003758 i = 0;
3759 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3760 /* user specified, bail out if channel not found */
3761 nla_for_each_nested(attr,
3762 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3763 tmp) {
3764 struct ieee80211_channel *chan;
3765
3766 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3767
3768 if (!chan) {
3769 err = -EINVAL;
3770 goto out_free;
3771 }
3772
3773 /* ignore disabled channels */
3774 if (chan->flags & IEEE80211_CHAN_DISABLED)
3775 continue;
3776
3777 request->channels[i] = chan;
3778 i++;
3779 }
3780 } else {
3781 /* all channels */
3782 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3783 int j;
3784 if (!wiphy->bands[band])
3785 continue;
3786 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3787 struct ieee80211_channel *chan;
3788
3789 chan = &wiphy->bands[band]->channels[j];
3790
3791 if (chan->flags & IEEE80211_CHAN_DISABLED)
3792 continue;
3793
3794 request->channels[i] = chan;
3795 i++;
3796 }
3797 }
3798 }
3799
3800 if (!i) {
3801 err = -EINVAL;
3802 goto out_free;
3803 }
3804
3805 request->n_channels = i;
3806
3807 i = 0;
3808 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3809 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3810 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003811 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003812 err = -EINVAL;
3813 goto out_free;
3814 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003815 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003816 memcpy(request->ssids[i].ssid, nla_data(attr),
3817 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003818 i++;
3819 }
3820 }
3821
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003822 i = 0;
3823 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3824 nla_for_each_nested(attr,
3825 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3826 tmp) {
3827 struct nlattr *ssid;
3828
3829 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3830 nla_data(attr), nla_len(attr),
3831 nl80211_match_policy);
3832 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3833 if (ssid) {
3834 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3835 err = -EINVAL;
3836 goto out_free;
3837 }
3838 memcpy(request->match_sets[i].ssid.ssid,
3839 nla_data(ssid), nla_len(ssid));
3840 request->match_sets[i].ssid.ssid_len =
3841 nla_len(ssid);
3842 }
3843 i++;
3844 }
3845 }
3846
Luciano Coelho807f8a82011-05-11 17:09:35 +03003847 if (info->attrs[NL80211_ATTR_IE]) {
3848 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3849 memcpy((void *)request->ie,
3850 nla_data(info->attrs[NL80211_ATTR_IE]),
3851 request->ie_len);
3852 }
3853
3854 request->dev = dev;
3855 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003856 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003857
3858 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3859 if (!err) {
3860 rdev->sched_scan_req = request;
3861 nl80211_send_sched_scan(rdev, dev,
3862 NL80211_CMD_START_SCHED_SCAN);
3863 goto out;
3864 }
3865
3866out_free:
3867 kfree(request);
3868out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003869 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003870 return err;
3871}
3872
3873static int nl80211_stop_sched_scan(struct sk_buff *skb,
3874 struct genl_info *info)
3875{
3876 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003877 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003878
3879 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3880 !rdev->ops->sched_scan_stop)
3881 return -EOPNOTSUPP;
3882
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003883 mutex_lock(&rdev->sched_scan_mtx);
3884 err = __cfg80211_stop_sched_scan(rdev, false);
3885 mutex_unlock(&rdev->sched_scan_mtx);
3886
3887 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003888}
3889
Johannes Berg9720bb32011-06-21 09:45:33 +02003890static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3891 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003892 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003893 struct wireless_dev *wdev,
3894 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003895{
Johannes Berg48ab9052009-07-10 18:42:31 +02003896 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003897 void *hdr;
3898 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003899 int i;
3900
3901 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003902
Johannes Berg9720bb32011-06-21 09:45:33 +02003903 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003904 NL80211_CMD_NEW_SCAN_RESULTS);
3905 if (!hdr)
3906 return -1;
3907
Johannes Berg9720bb32011-06-21 09:45:33 +02003908 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3909
Johannes Bergf5ea9122009-08-07 16:17:38 +02003910 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003911 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003912
3913 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3914 if (!bss)
3915 goto nla_put_failure;
3916 if (!is_zero_ether_addr(res->bssid))
3917 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3918 if (res->information_elements && res->len_information_elements)
3919 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3920 res->len_information_elements,
3921 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003922 if (res->beacon_ies && res->len_beacon_ies &&
3923 res->beacon_ies != res->information_elements)
3924 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3925 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003926 if (res->tsf)
3927 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3928 if (res->beacon_interval)
3929 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3930 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3931 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003932 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3933 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003934
Johannes Berg77965c92009-02-18 18:45:06 +01003935 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003936 case CFG80211_SIGNAL_TYPE_MBM:
3937 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3938 break;
3939 case CFG80211_SIGNAL_TYPE_UNSPEC:
3940 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3941 break;
3942 default:
3943 break;
3944 }
3945
Johannes Berg48ab9052009-07-10 18:42:31 +02003946 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003947 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003948 case NL80211_IFTYPE_STATION:
3949 if (intbss == wdev->current_bss)
3950 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3951 NL80211_BSS_STATUS_ASSOCIATED);
3952 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3953 if (intbss != wdev->auth_bsses[i])
3954 continue;
3955 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3956 NL80211_BSS_STATUS_AUTHENTICATED);
3957 break;
3958 }
3959 break;
3960 case NL80211_IFTYPE_ADHOC:
3961 if (intbss == wdev->current_bss)
3962 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3963 NL80211_BSS_STATUS_IBSS_JOINED);
3964 break;
3965 default:
3966 break;
3967 }
3968
Johannes Berg2a519312009-02-10 21:25:55 +01003969 nla_nest_end(msg, bss);
3970
3971 return genlmsg_end(msg, hdr);
3972
3973 nla_put_failure:
3974 genlmsg_cancel(msg, hdr);
3975 return -EMSGSIZE;
3976}
3977
3978static int nl80211_dump_scan(struct sk_buff *skb,
3979 struct netlink_callback *cb)
3980{
Johannes Berg48ab9052009-07-10 18:42:31 +02003981 struct cfg80211_registered_device *rdev;
3982 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003983 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003984 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003985 int start = cb->args[1], idx = 0;
3986 int err;
3987
Johannes Berg67748892010-10-04 21:14:06 +02003988 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3989 if (err)
3990 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003991
Johannes Berg48ab9052009-07-10 18:42:31 +02003992 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003993
Johannes Berg48ab9052009-07-10 18:42:31 +02003994 wdev_lock(wdev);
3995 spin_lock_bh(&rdev->bss_lock);
3996 cfg80211_bss_expire(rdev);
3997
Johannes Berg9720bb32011-06-21 09:45:33 +02003998 cb->seq = rdev->bss_generation;
3999
Johannes Berg48ab9052009-07-10 18:42:31 +02004000 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004001 if (++idx <= start)
4002 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004003 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004004 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004005 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004006 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004007 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004008 }
4009 }
4010
Johannes Berg48ab9052009-07-10 18:42:31 +02004011 spin_unlock_bh(&rdev->bss_lock);
4012 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004013
4014 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004015 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004016
Johannes Berg67748892010-10-04 21:14:06 +02004017 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004018}
4019
Holger Schurig61fa7132009-11-11 12:25:40 +01004020static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4021 int flags, struct net_device *dev,
4022 struct survey_info *survey)
4023{
4024 void *hdr;
4025 struct nlattr *infoattr;
4026
Holger Schurig61fa7132009-11-11 12:25:40 +01004027 hdr = nl80211hdr_put(msg, pid, seq, flags,
4028 NL80211_CMD_NEW_SURVEY_RESULTS);
4029 if (!hdr)
4030 return -ENOMEM;
4031
4032 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4033
4034 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4035 if (!infoattr)
4036 goto nla_put_failure;
4037
4038 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4039 survey->channel->center_freq);
4040 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4041 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4042 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004043 if (survey->filled & SURVEY_INFO_IN_USE)
4044 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004045 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4046 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4047 survey->channel_time);
4048 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4049 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4050 survey->channel_time_busy);
4051 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4052 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4053 survey->channel_time_ext_busy);
4054 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4055 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4056 survey->channel_time_rx);
4057 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4058 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4059 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004060
4061 nla_nest_end(msg, infoattr);
4062
4063 return genlmsg_end(msg, hdr);
4064
4065 nla_put_failure:
4066 genlmsg_cancel(msg, hdr);
4067 return -EMSGSIZE;
4068}
4069
4070static int nl80211_dump_survey(struct sk_buff *skb,
4071 struct netlink_callback *cb)
4072{
4073 struct survey_info survey;
4074 struct cfg80211_registered_device *dev;
4075 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004076 int survey_idx = cb->args[1];
4077 int res;
4078
Johannes Berg67748892010-10-04 21:14:06 +02004079 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4080 if (res)
4081 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004082
4083 if (!dev->ops->dump_survey) {
4084 res = -EOPNOTSUPP;
4085 goto out_err;
4086 }
4087
4088 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004089 struct ieee80211_channel *chan;
4090
Holger Schurig61fa7132009-11-11 12:25:40 +01004091 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4092 &survey);
4093 if (res == -ENOENT)
4094 break;
4095 if (res)
4096 goto out_err;
4097
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004098 /* Survey without a channel doesn't make sense */
4099 if (!survey.channel) {
4100 res = -EINVAL;
4101 goto out;
4102 }
4103
4104 chan = ieee80211_get_channel(&dev->wiphy,
4105 survey.channel->center_freq);
4106 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4107 survey_idx++;
4108 continue;
4109 }
4110
Holger Schurig61fa7132009-11-11 12:25:40 +01004111 if (nl80211_send_survey(skb,
4112 NETLINK_CB(cb->skb).pid,
4113 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4114 netdev,
4115 &survey) < 0)
4116 goto out;
4117 survey_idx++;
4118 }
4119
4120 out:
4121 cb->args[1] = survey_idx;
4122 res = skb->len;
4123 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004124 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004125 return res;
4126}
4127
Jouni Malinen255e7372009-03-20 21:21:17 +02004128static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4129{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004130 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004131}
4132
Samuel Ortizb23aa672009-07-01 21:26:54 +02004133static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4134{
4135 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4136 NL80211_WPA_VERSION_2));
4137}
4138
Jouni Malinen636a5d32009-03-19 13:39:22 +02004139static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4140{
Johannes Berg4c476992010-10-04 21:36:35 +02004141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4142 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004143 struct ieee80211_channel *chan;
4144 const u8 *bssid, *ssid, *ie = NULL;
4145 int err, ssid_len, ie_len = 0;
4146 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004147 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004148 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004149
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004150 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4151 return -EINVAL;
4152
4153 if (!info->attrs[NL80211_ATTR_MAC])
4154 return -EINVAL;
4155
Jouni Malinen17780922009-03-27 20:52:47 +02004156 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4157 return -EINVAL;
4158
Johannes Berg19957bb2009-07-02 17:20:43 +02004159 if (!info->attrs[NL80211_ATTR_SSID])
4160 return -EINVAL;
4161
4162 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4163 return -EINVAL;
4164
Johannes Bergfffd0932009-07-08 14:22:54 +02004165 err = nl80211_parse_key(info, &key);
4166 if (err)
4167 return err;
4168
4169 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004170 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4171 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004172 if (!key.p.key || !key.p.key_len)
4173 return -EINVAL;
4174 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4175 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4176 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4177 key.p.key_len != WLAN_KEY_LEN_WEP104))
4178 return -EINVAL;
4179 if (key.idx > 4)
4180 return -EINVAL;
4181 } else {
4182 key.p.key_len = 0;
4183 key.p.key = NULL;
4184 }
4185
Johannes Bergafea0b72010-08-10 09:46:42 +02004186 if (key.idx >= 0) {
4187 int i;
4188 bool ok = false;
4189 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4190 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4191 ok = true;
4192 break;
4193 }
4194 }
Johannes Berg4c476992010-10-04 21:36:35 +02004195 if (!ok)
4196 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004197 }
4198
Johannes Berg4c476992010-10-04 21:36:35 +02004199 if (!rdev->ops->auth)
4200 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004201
Johannes Berg074ac8d2010-09-16 14:58:22 +02004202 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004203 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4204 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004205
Johannes Berg19957bb2009-07-02 17:20:43 +02004206 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004207 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004208 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004209 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4210 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004211
Johannes Berg19957bb2009-07-02 17:20:43 +02004212 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4213 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4214
4215 if (info->attrs[NL80211_ATTR_IE]) {
4216 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4217 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4218 }
4219
4220 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004221 if (!nl80211_valid_auth_type(auth_type))
4222 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004223
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004224 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4225
Johannes Berg4c476992010-10-04 21:36:35 +02004226 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4227 ssid, ssid_len, ie, ie_len,
4228 key.p.key, key.p.key_len, key.idx,
4229 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004230}
4231
Johannes Bergc0692b82010-08-27 14:26:53 +03004232static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4233 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004234 struct cfg80211_crypto_settings *settings,
4235 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004236{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004237 memset(settings, 0, sizeof(*settings));
4238
Samuel Ortizb23aa672009-07-01 21:26:54 +02004239 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4240
Johannes Bergc0692b82010-08-27 14:26:53 +03004241 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4242 u16 proto;
4243 proto = nla_get_u16(
4244 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4245 settings->control_port_ethertype = cpu_to_be16(proto);
4246 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4247 proto != ETH_P_PAE)
4248 return -EINVAL;
4249 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4250 settings->control_port_no_encrypt = true;
4251 } else
4252 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4253
Samuel Ortizb23aa672009-07-01 21:26:54 +02004254 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4255 void *data;
4256 int len, i;
4257
4258 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4259 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4260 settings->n_ciphers_pairwise = len / sizeof(u32);
4261
4262 if (len % sizeof(u32))
4263 return -EINVAL;
4264
Johannes Berg3dc27d22009-07-02 21:36:37 +02004265 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004266 return -EINVAL;
4267
4268 memcpy(settings->ciphers_pairwise, data, len);
4269
4270 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004271 if (!cfg80211_supported_cipher_suite(
4272 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004273 settings->ciphers_pairwise[i]))
4274 return -EINVAL;
4275 }
4276
4277 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4278 settings->cipher_group =
4279 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004280 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4281 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004282 return -EINVAL;
4283 }
4284
4285 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4286 settings->wpa_versions =
4287 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4288 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4289 return -EINVAL;
4290 }
4291
4292 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4293 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004294 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004295
4296 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4297 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4298 settings->n_akm_suites = len / sizeof(u32);
4299
4300 if (len % sizeof(u32))
4301 return -EINVAL;
4302
4303 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004304 }
4305
4306 return 0;
4307}
4308
Jouni Malinen636a5d32009-03-19 13:39:22 +02004309static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4310{
Johannes Berg4c476992010-10-04 21:36:35 +02004311 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4312 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004313 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004314 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004315 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004316 int err, ssid_len, ie_len = 0;
4317 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004318
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004319 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4320 return -EINVAL;
4321
4322 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004323 !info->attrs[NL80211_ATTR_SSID] ||
4324 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004325 return -EINVAL;
4326
Johannes Berg4c476992010-10-04 21:36:35 +02004327 if (!rdev->ops->assoc)
4328 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004329
Johannes Berg074ac8d2010-09-16 14:58:22 +02004330 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004331 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4332 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004333
Johannes Berg19957bb2009-07-02 17:20:43 +02004334 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004335
Johannes Berg19957bb2009-07-02 17:20:43 +02004336 chan = ieee80211_get_channel(&rdev->wiphy,
4337 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004338 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4339 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004340
Johannes Berg19957bb2009-07-02 17:20:43 +02004341 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4342 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004343
4344 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004345 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4346 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004347 }
4348
Jouni Malinendc6382c2009-05-06 22:09:37 +03004349 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004350 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004351 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004352 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004353 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004354 else if (mfp != NL80211_MFP_NO)
4355 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004356 }
4357
Johannes Berg3e5d7642009-07-07 14:37:26 +02004358 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4359 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4360
Johannes Bergc0692b82010-08-27 14:26:53 +03004361 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004362 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004363 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4364 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004365 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004366
Jouni Malinen636a5d32009-03-19 13:39:22 +02004367 return err;
4368}
4369
4370static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4371{
Johannes Berg4c476992010-10-04 21:36:35 +02004372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4373 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004374 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004375 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004376 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004377 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004378
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004379 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4380 return -EINVAL;
4381
4382 if (!info->attrs[NL80211_ATTR_MAC])
4383 return -EINVAL;
4384
4385 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4386 return -EINVAL;
4387
Johannes Berg4c476992010-10-04 21:36:35 +02004388 if (!rdev->ops->deauth)
4389 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004390
Johannes Berg074ac8d2010-09-16 14:58:22 +02004391 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004392 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4393 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004394
Johannes Berg19957bb2009-07-02 17:20:43 +02004395 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004396
Johannes Berg19957bb2009-07-02 17:20:43 +02004397 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4398 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004399 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004400 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004401 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004402
4403 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004404 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4405 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004406 }
4407
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004408 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4409
Johannes Berg4c476992010-10-04 21:36:35 +02004410 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4411 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004412}
4413
4414static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4415{
Johannes Berg4c476992010-10-04 21:36:35 +02004416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4417 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004418 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004419 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004420 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004421 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004422
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004423 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4424 return -EINVAL;
4425
4426 if (!info->attrs[NL80211_ATTR_MAC])
4427 return -EINVAL;
4428
4429 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4430 return -EINVAL;
4431
Johannes Berg4c476992010-10-04 21:36:35 +02004432 if (!rdev->ops->disassoc)
4433 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004434
Johannes Berg074ac8d2010-09-16 14:58:22 +02004435 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004436 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4437 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004438
Johannes Berg19957bb2009-07-02 17:20:43 +02004439 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004440
Johannes Berg19957bb2009-07-02 17:20:43 +02004441 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4442 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004443 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004444 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004445 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004446
4447 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004448 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4449 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004450 }
4451
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004452 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4453
Johannes Berg4c476992010-10-04 21:36:35 +02004454 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4455 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004456}
4457
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004458static bool
4459nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4460 int mcast_rate[IEEE80211_NUM_BANDS],
4461 int rateval)
4462{
4463 struct wiphy *wiphy = &rdev->wiphy;
4464 bool found = false;
4465 int band, i;
4466
4467 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4468 struct ieee80211_supported_band *sband;
4469
4470 sband = wiphy->bands[band];
4471 if (!sband)
4472 continue;
4473
4474 for (i = 0; i < sband->n_bitrates; i++) {
4475 if (sband->bitrates[i].bitrate == rateval) {
4476 mcast_rate[band] = i + 1;
4477 found = true;
4478 break;
4479 }
4480 }
4481 }
4482
4483 return found;
4484}
4485
Johannes Berg04a773a2009-04-19 21:24:32 +02004486static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4487{
Johannes Berg4c476992010-10-04 21:36:35 +02004488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4489 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004490 struct cfg80211_ibss_params ibss;
4491 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004492 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004493 int err;
4494
Johannes Berg8e30bc52009-04-22 17:45:38 +02004495 memset(&ibss, 0, sizeof(ibss));
4496
Johannes Berg04a773a2009-04-19 21:24:32 +02004497 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4498 return -EINVAL;
4499
4500 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4501 !info->attrs[NL80211_ATTR_SSID] ||
4502 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4503 return -EINVAL;
4504
Johannes Berg8e30bc52009-04-22 17:45:38 +02004505 ibss.beacon_interval = 100;
4506
4507 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4508 ibss.beacon_interval =
4509 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4510 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4511 return -EINVAL;
4512 }
4513
Johannes Berg4c476992010-10-04 21:36:35 +02004514 if (!rdev->ops->join_ibss)
4515 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004516
Johannes Berg4c476992010-10-04 21:36:35 +02004517 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4518 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004519
Johannes Berg79c97e92009-07-07 03:56:12 +02004520 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004521
Johannes Berg39193492011-09-16 13:45:25 +02004522 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004523 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004524
4525 if (!is_valid_ether_addr(ibss.bssid))
4526 return -EINVAL;
4527 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004528 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4529 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4530
4531 if (info->attrs[NL80211_ATTR_IE]) {
4532 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4533 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4534 }
4535
4536 ibss.channel = ieee80211_get_channel(wiphy,
4537 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4538 if (!ibss.channel ||
4539 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004540 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4541 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004542
4543 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004544 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004545
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004546 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4547 u8 *rates =
4548 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4549 int n_rates =
4550 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4551 struct ieee80211_supported_band *sband =
4552 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004553 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004554
Johannes Berg34850ab2011-07-18 18:08:35 +02004555 err = ieee80211_get_ratemask(sband, rates, n_rates,
4556 &ibss.basic_rates);
4557 if (err)
4558 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004559 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004560
4561 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4562 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4563 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4564 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004565
Johannes Berg4c476992010-10-04 21:36:35 +02004566 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4567 connkeys = nl80211_parse_connkeys(rdev,
4568 info->attrs[NL80211_ATTR_KEYS]);
4569 if (IS_ERR(connkeys))
4570 return PTR_ERR(connkeys);
4571 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004572
Johannes Berg4c476992010-10-04 21:36:35 +02004573 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004574 if (err)
4575 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004576 return err;
4577}
4578
4579static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4580{
Johannes Berg4c476992010-10-04 21:36:35 +02004581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4582 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004583
Johannes Berg4c476992010-10-04 21:36:35 +02004584 if (!rdev->ops->leave_ibss)
4585 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004586
Johannes Berg4c476992010-10-04 21:36:35 +02004587 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4588 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004589
Johannes Berg4c476992010-10-04 21:36:35 +02004590 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004591}
4592
Johannes Bergaff89a92009-07-01 21:26:51 +02004593#ifdef CONFIG_NL80211_TESTMODE
4594static struct genl_multicast_group nl80211_testmode_mcgrp = {
4595 .name = "testmode",
4596};
4597
4598static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4599{
Johannes Berg4c476992010-10-04 21:36:35 +02004600 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004601 int err;
4602
4603 if (!info->attrs[NL80211_ATTR_TESTDATA])
4604 return -EINVAL;
4605
Johannes Bergaff89a92009-07-01 21:26:51 +02004606 err = -EOPNOTSUPP;
4607 if (rdev->ops->testmode_cmd) {
4608 rdev->testmode_info = info;
4609 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4610 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4611 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4612 rdev->testmode_info = NULL;
4613 }
4614
Johannes Bergaff89a92009-07-01 21:26:51 +02004615 return err;
4616}
4617
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004618static int nl80211_testmode_dump(struct sk_buff *skb,
4619 struct netlink_callback *cb)
4620{
4621 struct cfg80211_registered_device *dev;
4622 int err;
4623 long phy_idx;
4624 void *data = NULL;
4625 int data_len = 0;
4626
4627 if (cb->args[0]) {
4628 /*
4629 * 0 is a valid index, but not valid for args[0],
4630 * so we need to offset by 1.
4631 */
4632 phy_idx = cb->args[0] - 1;
4633 } else {
4634 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4635 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4636 nl80211_policy);
4637 if (err)
4638 return err;
4639 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4640 return -EINVAL;
4641 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4642 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4643 cb->args[1] =
4644 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4645 }
4646
4647 if (cb->args[1]) {
4648 data = nla_data((void *)cb->args[1]);
4649 data_len = nla_len((void *)cb->args[1]);
4650 }
4651
4652 mutex_lock(&cfg80211_mutex);
4653 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4654 if (!dev) {
4655 mutex_unlock(&cfg80211_mutex);
4656 return -ENOENT;
4657 }
4658 cfg80211_lock_rdev(dev);
4659 mutex_unlock(&cfg80211_mutex);
4660
4661 if (!dev->ops->testmode_dump) {
4662 err = -EOPNOTSUPP;
4663 goto out_err;
4664 }
4665
4666 while (1) {
4667 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4668 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4669 NL80211_CMD_TESTMODE);
4670 struct nlattr *tmdata;
4671
4672 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4673 genlmsg_cancel(skb, hdr);
4674 break;
4675 }
4676
4677 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4678 if (!tmdata) {
4679 genlmsg_cancel(skb, hdr);
4680 break;
4681 }
4682 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4683 data, data_len);
4684 nla_nest_end(skb, tmdata);
4685
4686 if (err == -ENOBUFS || err == -ENOENT) {
4687 genlmsg_cancel(skb, hdr);
4688 break;
4689 } else if (err) {
4690 genlmsg_cancel(skb, hdr);
4691 goto out_err;
4692 }
4693
4694 genlmsg_end(skb, hdr);
4695 }
4696
4697 err = skb->len;
4698 /* see above */
4699 cb->args[0] = phy_idx + 1;
4700 out_err:
4701 cfg80211_unlock_rdev(dev);
4702 return err;
4703}
4704
Johannes Bergaff89a92009-07-01 21:26:51 +02004705static struct sk_buff *
4706__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4707 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4708{
4709 struct sk_buff *skb;
4710 void *hdr;
4711 struct nlattr *data;
4712
4713 skb = nlmsg_new(approxlen + 100, gfp);
4714 if (!skb)
4715 return NULL;
4716
4717 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4718 if (!hdr) {
4719 kfree_skb(skb);
4720 return NULL;
4721 }
4722
4723 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4724 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4725
4726 ((void **)skb->cb)[0] = rdev;
4727 ((void **)skb->cb)[1] = hdr;
4728 ((void **)skb->cb)[2] = data;
4729
4730 return skb;
4731
4732 nla_put_failure:
4733 kfree_skb(skb);
4734 return NULL;
4735}
4736
4737struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4738 int approxlen)
4739{
4740 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4741
4742 if (WARN_ON(!rdev->testmode_info))
4743 return NULL;
4744
4745 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4746 rdev->testmode_info->snd_pid,
4747 rdev->testmode_info->snd_seq,
4748 GFP_KERNEL);
4749}
4750EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4751
4752int cfg80211_testmode_reply(struct sk_buff *skb)
4753{
4754 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4755 void *hdr = ((void **)skb->cb)[1];
4756 struct nlattr *data = ((void **)skb->cb)[2];
4757
4758 if (WARN_ON(!rdev->testmode_info)) {
4759 kfree_skb(skb);
4760 return -EINVAL;
4761 }
4762
4763 nla_nest_end(skb, data);
4764 genlmsg_end(skb, hdr);
4765 return genlmsg_reply(skb, rdev->testmode_info);
4766}
4767EXPORT_SYMBOL(cfg80211_testmode_reply);
4768
4769struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4770 int approxlen, gfp_t gfp)
4771{
4772 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4773
4774 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4775}
4776EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4777
4778void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4779{
4780 void *hdr = ((void **)skb->cb)[1];
4781 struct nlattr *data = ((void **)skb->cb)[2];
4782
4783 nla_nest_end(skb, data);
4784 genlmsg_end(skb, hdr);
4785 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4786}
4787EXPORT_SYMBOL(cfg80211_testmode_event);
4788#endif
4789
Samuel Ortizb23aa672009-07-01 21:26:54 +02004790static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4791{
Johannes Berg4c476992010-10-04 21:36:35 +02004792 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4793 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004794 struct cfg80211_connect_params connect;
4795 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004796 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004797 int err;
4798
4799 memset(&connect, 0, sizeof(connect));
4800
4801 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4802 return -EINVAL;
4803
4804 if (!info->attrs[NL80211_ATTR_SSID] ||
4805 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4806 return -EINVAL;
4807
4808 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4809 connect.auth_type =
4810 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4811 if (!nl80211_valid_auth_type(connect.auth_type))
4812 return -EINVAL;
4813 } else
4814 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4815
4816 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4817
Johannes Bergc0692b82010-08-27 14:26:53 +03004818 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004819 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004820 if (err)
4821 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004822
Johannes Berg074ac8d2010-09-16 14:58:22 +02004823 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004824 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4825 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004826
Johannes Berg79c97e92009-07-07 03:56:12 +02004827 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004828
Samuel Ortizb23aa672009-07-01 21:26:54 +02004829 if (info->attrs[NL80211_ATTR_MAC])
4830 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4831 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4832 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4833
4834 if (info->attrs[NL80211_ATTR_IE]) {
4835 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4836 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4837 }
4838
4839 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4840 connect.channel =
4841 ieee80211_get_channel(wiphy,
4842 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4843 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004844 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4845 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004846 }
4847
Johannes Bergfffd0932009-07-08 14:22:54 +02004848 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4849 connkeys = nl80211_parse_connkeys(rdev,
4850 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004851 if (IS_ERR(connkeys))
4852 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004853 }
4854
4855 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004856 if (err)
4857 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004858 return err;
4859}
4860
4861static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4862{
Johannes Berg4c476992010-10-04 21:36:35 +02004863 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4864 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004865 u16 reason;
4866
4867 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4868 reason = WLAN_REASON_DEAUTH_LEAVING;
4869 else
4870 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4871
4872 if (reason == 0)
4873 return -EINVAL;
4874
Johannes Berg074ac8d2010-09-16 14:58:22 +02004875 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004876 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4877 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004878
Johannes Berg4c476992010-10-04 21:36:35 +02004879 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004880}
4881
Johannes Berg463d0182009-07-14 00:33:35 +02004882static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4883{
Johannes Berg4c476992010-10-04 21:36:35 +02004884 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004885 struct net *net;
4886 int err;
4887 u32 pid;
4888
4889 if (!info->attrs[NL80211_ATTR_PID])
4890 return -EINVAL;
4891
4892 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4893
Johannes Berg463d0182009-07-14 00:33:35 +02004894 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004895 if (IS_ERR(net))
4896 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004897
4898 err = 0;
4899
4900 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004901 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4902 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004903
Johannes Berg463d0182009-07-14 00:33:35 +02004904 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004905 return err;
4906}
4907
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004908static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4909{
Johannes Berg4c476992010-10-04 21:36:35 +02004910 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004911 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4912 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004913 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004914 struct cfg80211_pmksa pmksa;
4915
4916 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4917
4918 if (!info->attrs[NL80211_ATTR_MAC])
4919 return -EINVAL;
4920
4921 if (!info->attrs[NL80211_ATTR_PMKID])
4922 return -EINVAL;
4923
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004924 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4925 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4926
Johannes Berg074ac8d2010-09-16 14:58:22 +02004927 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004928 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4929 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004930
4931 switch (info->genlhdr->cmd) {
4932 case NL80211_CMD_SET_PMKSA:
4933 rdev_ops = rdev->ops->set_pmksa;
4934 break;
4935 case NL80211_CMD_DEL_PMKSA:
4936 rdev_ops = rdev->ops->del_pmksa;
4937 break;
4938 default:
4939 WARN_ON(1);
4940 break;
4941 }
4942
Johannes Berg4c476992010-10-04 21:36:35 +02004943 if (!rdev_ops)
4944 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004945
Johannes Berg4c476992010-10-04 21:36:35 +02004946 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004947}
4948
4949static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4950{
Johannes Berg4c476992010-10-04 21:36:35 +02004951 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4952 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004953
Johannes Berg074ac8d2010-09-16 14:58:22 +02004954 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004955 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4956 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004957
Johannes Berg4c476992010-10-04 21:36:35 +02004958 if (!rdev->ops->flush_pmksa)
4959 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004960
Johannes Berg4c476992010-10-04 21:36:35 +02004961 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004962}
4963
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004964static int nl80211_remain_on_channel(struct sk_buff *skb,
4965 struct genl_info *info)
4966{
Johannes Berg4c476992010-10-04 21:36:35 +02004967 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4968 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004969 struct ieee80211_channel *chan;
4970 struct sk_buff *msg;
4971 void *hdr;
4972 u64 cookie;
4973 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4974 u32 freq, duration;
4975 int err;
4976
4977 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4978 !info->attrs[NL80211_ATTR_DURATION])
4979 return -EINVAL;
4980
4981 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4982
4983 /*
4984 * We should be on that channel for at least one jiffie,
4985 * and more than 5 seconds seems excessive.
4986 */
Johannes Berga2939112010-12-14 17:54:28 +01004987 if (!duration || !msecs_to_jiffies(duration) ||
4988 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004989 return -EINVAL;
4990
Johannes Berg4c476992010-10-04 21:36:35 +02004991 if (!rdev->ops->remain_on_channel)
4992 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004993
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004994 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4995 channel_type = nla_get_u32(
4996 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4997 if (channel_type != NL80211_CHAN_NO_HT &&
4998 channel_type != NL80211_CHAN_HT20 &&
4999 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005000 channel_type != NL80211_CHAN_HT40MINUS)
5001 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005002 }
5003
5004 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5005 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005006 if (chan == NULL)
5007 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005008
5009 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005010 if (!msg)
5011 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005012
5013 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5014 NL80211_CMD_REMAIN_ON_CHANNEL);
5015
5016 if (IS_ERR(hdr)) {
5017 err = PTR_ERR(hdr);
5018 goto free_msg;
5019 }
5020
5021 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5022 channel_type, duration, &cookie);
5023
5024 if (err)
5025 goto free_msg;
5026
5027 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5028
5029 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005030
5031 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005032
5033 nla_put_failure:
5034 err = -ENOBUFS;
5035 free_msg:
5036 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005037 return err;
5038}
5039
5040static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5041 struct genl_info *info)
5042{
Johannes Berg4c476992010-10-04 21:36:35 +02005043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5044 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005045 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005046
5047 if (!info->attrs[NL80211_ATTR_COOKIE])
5048 return -EINVAL;
5049
Johannes Berg4c476992010-10-04 21:36:35 +02005050 if (!rdev->ops->cancel_remain_on_channel)
5051 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005052
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005053 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5054
Johannes Berg4c476992010-10-04 21:36:35 +02005055 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005056}
5057
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005058static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5059 u8 *rates, u8 rates_len)
5060{
5061 u8 i;
5062 u32 mask = 0;
5063
5064 for (i = 0; i < rates_len; i++) {
5065 int rate = (rates[i] & 0x7f) * 5;
5066 int ridx;
5067 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5068 struct ieee80211_rate *srate =
5069 &sband->bitrates[ridx];
5070 if (rate == srate->bitrate) {
5071 mask |= 1 << ridx;
5072 break;
5073 }
5074 }
5075 if (ridx == sband->n_bitrates)
5076 return 0; /* rate not found */
5077 }
5078
5079 return mask;
5080}
5081
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005082static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005083 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5084 .len = NL80211_MAX_SUPP_RATES },
5085};
5086
5087static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5088 struct genl_info *info)
5089{
5090 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005092 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005093 int rem, i;
5094 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005095 struct nlattr *tx_rates;
5096 struct ieee80211_supported_band *sband;
5097
5098 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5099 return -EINVAL;
5100
Johannes Berg4c476992010-10-04 21:36:35 +02005101 if (!rdev->ops->set_bitrate_mask)
5102 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005103
5104 memset(&mask, 0, sizeof(mask));
5105 /* Default to all rates enabled */
5106 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5107 sband = rdev->wiphy.bands[i];
5108 mask.control[i].legacy =
5109 sband ? (1 << sband->n_bitrates) - 1 : 0;
5110 }
5111
5112 /*
5113 * The nested attribute uses enum nl80211_band as the index. This maps
5114 * directly to the enum ieee80211_band values used in cfg80211.
5115 */
5116 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5117 {
5118 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005119 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5120 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005121 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005122 if (sband == NULL)
5123 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005124 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5125 nla_len(tx_rates), nl80211_txattr_policy);
5126 if (tb[NL80211_TXRATE_LEGACY]) {
5127 mask.control[band].legacy = rateset_to_mask(
5128 sband,
5129 nla_data(tb[NL80211_TXRATE_LEGACY]),
5130 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005131 if (mask.control[band].legacy == 0)
5132 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005133 }
5134 }
5135
Johannes Berg4c476992010-10-04 21:36:35 +02005136 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005137}
5138
Johannes Berg2e161f72010-08-12 15:38:38 +02005139static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005140{
Johannes Berg4c476992010-10-04 21:36:35 +02005141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5142 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005143 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005144
5145 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5146 return -EINVAL;
5147
Johannes Berg2e161f72010-08-12 15:38:38 +02005148 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5149 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005150
Johannes Berg9d38d852010-06-09 17:20:33 +02005151 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005152 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005153 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5154 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5155 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005156 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005157 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5158 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005159
5160 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005161 if (!rdev->ops->mgmt_tx)
5162 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005163
Johannes Berg4c476992010-10-04 21:36:35 +02005164 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005165 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005166 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5167 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005168}
5169
Johannes Berg2e161f72010-08-12 15:38:38 +02005170static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005171{
Johannes Berg4c476992010-10-04 21:36:35 +02005172 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5173 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005174 struct ieee80211_channel *chan;
5175 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005176 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005177 u32 freq;
5178 int err;
5179 void *hdr;
5180 u64 cookie;
5181 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005182 unsigned int wait = 0;
5183 bool offchan;
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305184 bool no_cck;
Jouni Malinen026331c2010-02-15 12:53:10 +02005185
5186 if (!info->attrs[NL80211_ATTR_FRAME] ||
5187 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5188 return -EINVAL;
5189
Johannes Berg4c476992010-10-04 21:36:35 +02005190 if (!rdev->ops->mgmt_tx)
5191 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005192
Johannes Berg9d38d852010-06-09 17:20:33 +02005193 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005194 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005195 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5196 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5197 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005198 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005199 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5200 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005201
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005202 if (info->attrs[NL80211_ATTR_DURATION]) {
5203 if (!rdev->ops->mgmt_tx_cancel_wait)
5204 return -EINVAL;
5205 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5206 }
5207
Jouni Malinen026331c2010-02-15 12:53:10 +02005208 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5209 channel_type = nla_get_u32(
5210 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5211 if (channel_type != NL80211_CHAN_NO_HT &&
5212 channel_type != NL80211_CHAN_HT20 &&
5213 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005214 channel_type != NL80211_CHAN_HT40MINUS)
5215 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005216 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005217 }
5218
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005219 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5220
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305221 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5222
Jouni Malinen026331c2010-02-15 12:53:10 +02005223 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5224 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005225 if (chan == NULL)
5226 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005227
5228 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005229 if (!msg)
5230 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005231
5232 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005233 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005234
5235 if (IS_ERR(hdr)) {
5236 err = PTR_ERR(hdr);
5237 goto free_msg;
5238 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005239 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5240 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005241 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5242 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305243 no_cck, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005244 if (err)
5245 goto free_msg;
5246
5247 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5248
5249 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005250 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005251
5252 nla_put_failure:
5253 err = -ENOBUFS;
5254 free_msg:
5255 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005256 return err;
5257}
5258
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005259static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5260{
5261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5262 struct net_device *dev = info->user_ptr[1];
5263 u64 cookie;
5264
5265 if (!info->attrs[NL80211_ATTR_COOKIE])
5266 return -EINVAL;
5267
5268 if (!rdev->ops->mgmt_tx_cancel_wait)
5269 return -EOPNOTSUPP;
5270
5271 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5272 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5273 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5274 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5275 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5276 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5277 return -EOPNOTSUPP;
5278
5279 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5280
5281 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5282}
5283
Kalle Valoffb9eb32010-02-17 17:58:10 +02005284static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5285{
Johannes Berg4c476992010-10-04 21:36:35 +02005286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005287 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005288 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005289 u8 ps_state;
5290 bool state;
5291 int err;
5292
Johannes Berg4c476992010-10-04 21:36:35 +02005293 if (!info->attrs[NL80211_ATTR_PS_STATE])
5294 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005295
5296 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5299 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005300
5301 wdev = dev->ieee80211_ptr;
5302
Johannes Berg4c476992010-10-04 21:36:35 +02005303 if (!rdev->ops->set_power_mgmt)
5304 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005305
5306 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5307
5308 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005309 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005310
Johannes Berg4c476992010-10-04 21:36:35 +02005311 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5312 wdev->ps_timeout);
5313 if (!err)
5314 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005315 return err;
5316}
5317
5318static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5319{
Johannes Berg4c476992010-10-04 21:36:35 +02005320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005321 enum nl80211_ps_state ps_state;
5322 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005323 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005324 struct sk_buff *msg;
5325 void *hdr;
5326 int err;
5327
Kalle Valoffb9eb32010-02-17 17:58:10 +02005328 wdev = dev->ieee80211_ptr;
5329
Johannes Berg4c476992010-10-04 21:36:35 +02005330 if (!rdev->ops->set_power_mgmt)
5331 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005332
5333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005334 if (!msg)
5335 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005336
5337 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5338 NL80211_CMD_GET_POWER_SAVE);
5339 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005340 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005341 goto free_msg;
5342 }
5343
5344 if (wdev->ps)
5345 ps_state = NL80211_PS_ENABLED;
5346 else
5347 ps_state = NL80211_PS_DISABLED;
5348
5349 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5350
5351 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005352 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005353
Johannes Berg4c476992010-10-04 21:36:35 +02005354 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005355 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005356 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005357 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005358 return err;
5359}
5360
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005361static struct nla_policy
5362nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5363 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5364 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5365 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5366};
5367
5368static int nl80211_set_cqm_rssi(struct genl_info *info,
5369 s32 threshold, u32 hysteresis)
5370{
Johannes Berg4c476992010-10-04 21:36:35 +02005371 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005372 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005373 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005374
5375 if (threshold > 0)
5376 return -EINVAL;
5377
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005378 wdev = dev->ieee80211_ptr;
5379
Johannes Berg4c476992010-10-04 21:36:35 +02005380 if (!rdev->ops->set_cqm_rssi_config)
5381 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005382
Johannes Berg074ac8d2010-09-16 14:58:22 +02005383 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005384 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5385 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005386
Johannes Berg4c476992010-10-04 21:36:35 +02005387 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5388 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005389}
5390
5391static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5392{
5393 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5394 struct nlattr *cqm;
5395 int err;
5396
5397 cqm = info->attrs[NL80211_ATTR_CQM];
5398 if (!cqm) {
5399 err = -EINVAL;
5400 goto out;
5401 }
5402
5403 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5404 nl80211_attr_cqm_policy);
5405 if (err)
5406 goto out;
5407
5408 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5409 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5410 s32 threshold;
5411 u32 hysteresis;
5412 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5413 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5414 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5415 } else
5416 err = -EINVAL;
5417
5418out:
5419 return err;
5420}
5421
Johannes Berg29cbe682010-12-03 09:20:44 +01005422static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5423{
5424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5425 struct net_device *dev = info->user_ptr[1];
5426 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005427 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005428 int err;
5429
5430 /* start with default */
5431 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005432 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005433
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005434 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005435 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005436 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005437 if (err)
5438 return err;
5439 }
5440
5441 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5442 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5443 return -EINVAL;
5444
Javier Cardonac80d5452010-12-16 17:37:49 -08005445 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5446 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5447
5448 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5449 /* parse additional setup parameters if given */
5450 err = nl80211_parse_mesh_setup(info, &setup);
5451 if (err)
5452 return err;
5453 }
5454
5455 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005456}
5457
5458static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5459{
5460 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5461 struct net_device *dev = info->user_ptr[1];
5462
5463 return cfg80211_leave_mesh(rdev, dev);
5464}
5465
Johannes Bergff1b6e62011-05-04 15:37:28 +02005466static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5467{
5468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5469 struct sk_buff *msg;
5470 void *hdr;
5471
5472 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5473 return -EOPNOTSUPP;
5474
5475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5476 if (!msg)
5477 return -ENOMEM;
5478
5479 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5480 NL80211_CMD_GET_WOWLAN);
5481 if (!hdr)
5482 goto nla_put_failure;
5483
5484 if (rdev->wowlan) {
5485 struct nlattr *nl_wowlan;
5486
5487 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5488 if (!nl_wowlan)
5489 goto nla_put_failure;
5490
5491 if (rdev->wowlan->any)
5492 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5493 if (rdev->wowlan->disconnect)
5494 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5495 if (rdev->wowlan->magic_pkt)
5496 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005497 if (rdev->wowlan->gtk_rekey_failure)
5498 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5499 if (rdev->wowlan->eap_identity_req)
5500 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5501 if (rdev->wowlan->four_way_handshake)
5502 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5503 if (rdev->wowlan->rfkill_release)
5504 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005505 if (rdev->wowlan->n_patterns) {
5506 struct nlattr *nl_pats, *nl_pat;
5507 int i, pat_len;
5508
5509 nl_pats = nla_nest_start(msg,
5510 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5511 if (!nl_pats)
5512 goto nla_put_failure;
5513
5514 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5515 nl_pat = nla_nest_start(msg, i + 1);
5516 if (!nl_pat)
5517 goto nla_put_failure;
5518 pat_len = rdev->wowlan->patterns[i].pattern_len;
5519 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5520 DIV_ROUND_UP(pat_len, 8),
5521 rdev->wowlan->patterns[i].mask);
5522 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5523 pat_len,
5524 rdev->wowlan->patterns[i].pattern);
5525 nla_nest_end(msg, nl_pat);
5526 }
5527 nla_nest_end(msg, nl_pats);
5528 }
5529
5530 nla_nest_end(msg, nl_wowlan);
5531 }
5532
5533 genlmsg_end(msg, hdr);
5534 return genlmsg_reply(msg, info);
5535
5536nla_put_failure:
5537 nlmsg_free(msg);
5538 return -ENOBUFS;
5539}
5540
5541static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5542{
5543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5544 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5545 struct cfg80211_wowlan no_triggers = {};
5546 struct cfg80211_wowlan new_triggers = {};
5547 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5548 int err, i;
5549
5550 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5551 return -EOPNOTSUPP;
5552
5553 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5554 goto no_triggers;
5555
5556 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5557 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5558 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5559 nl80211_wowlan_policy);
5560 if (err)
5561 return err;
5562
5563 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5564 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5565 return -EINVAL;
5566 new_triggers.any = true;
5567 }
5568
5569 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5570 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5571 return -EINVAL;
5572 new_triggers.disconnect = true;
5573 }
5574
5575 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5576 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5577 return -EINVAL;
5578 new_triggers.magic_pkt = true;
5579 }
5580
Johannes Berg77dbbb12011-07-13 10:48:55 +02005581 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5582 return -EINVAL;
5583
5584 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5585 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5586 return -EINVAL;
5587 new_triggers.gtk_rekey_failure = true;
5588 }
5589
5590 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5591 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5592 return -EINVAL;
5593 new_triggers.eap_identity_req = true;
5594 }
5595
5596 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5597 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5598 return -EINVAL;
5599 new_triggers.four_way_handshake = true;
5600 }
5601
5602 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5603 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5604 return -EINVAL;
5605 new_triggers.rfkill_release = true;
5606 }
5607
Johannes Bergff1b6e62011-05-04 15:37:28 +02005608 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5609 struct nlattr *pat;
5610 int n_patterns = 0;
5611 int rem, pat_len, mask_len;
5612 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5613
5614 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5615 rem)
5616 n_patterns++;
5617 if (n_patterns > wowlan->n_patterns)
5618 return -EINVAL;
5619
5620 new_triggers.patterns = kcalloc(n_patterns,
5621 sizeof(new_triggers.patterns[0]),
5622 GFP_KERNEL);
5623 if (!new_triggers.patterns)
5624 return -ENOMEM;
5625
5626 new_triggers.n_patterns = n_patterns;
5627 i = 0;
5628
5629 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5630 rem) {
5631 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5632 nla_data(pat), nla_len(pat), NULL);
5633 err = -EINVAL;
5634 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5635 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5636 goto error;
5637 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5638 mask_len = DIV_ROUND_UP(pat_len, 8);
5639 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5640 mask_len)
5641 goto error;
5642 if (pat_len > wowlan->pattern_max_len ||
5643 pat_len < wowlan->pattern_min_len)
5644 goto error;
5645
5646 new_triggers.patterns[i].mask =
5647 kmalloc(mask_len + pat_len, GFP_KERNEL);
5648 if (!new_triggers.patterns[i].mask) {
5649 err = -ENOMEM;
5650 goto error;
5651 }
5652 new_triggers.patterns[i].pattern =
5653 new_triggers.patterns[i].mask + mask_len;
5654 memcpy(new_triggers.patterns[i].mask,
5655 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5656 mask_len);
5657 new_triggers.patterns[i].pattern_len = pat_len;
5658 memcpy(new_triggers.patterns[i].pattern,
5659 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5660 pat_len);
5661 i++;
5662 }
5663 }
5664
5665 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5666 struct cfg80211_wowlan *ntrig;
5667 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5668 GFP_KERNEL);
5669 if (!ntrig) {
5670 err = -ENOMEM;
5671 goto error;
5672 }
5673 cfg80211_rdev_free_wowlan(rdev);
5674 rdev->wowlan = ntrig;
5675 } else {
5676 no_triggers:
5677 cfg80211_rdev_free_wowlan(rdev);
5678 rdev->wowlan = NULL;
5679 }
5680
5681 return 0;
5682 error:
5683 for (i = 0; i < new_triggers.n_patterns; i++)
5684 kfree(new_triggers.patterns[i].mask);
5685 kfree(new_triggers.patterns);
5686 return err;
5687}
5688
Johannes Berge5497d72011-07-05 16:35:40 +02005689static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5690{
5691 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5692 struct net_device *dev = info->user_ptr[1];
5693 struct wireless_dev *wdev = dev->ieee80211_ptr;
5694 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5695 struct cfg80211_gtk_rekey_data rekey_data;
5696 int err;
5697
5698 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5699 return -EINVAL;
5700
5701 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5702 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5703 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5704 nl80211_rekey_policy);
5705 if (err)
5706 return err;
5707
5708 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5709 return -ERANGE;
5710 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5711 return -ERANGE;
5712 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5713 return -ERANGE;
5714
5715 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5716 NL80211_KEK_LEN);
5717 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5718 NL80211_KCK_LEN);
5719 memcpy(rekey_data.replay_ctr,
5720 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5721 NL80211_REPLAY_CTR_LEN);
5722
5723 wdev_lock(wdev);
5724 if (!wdev->current_bss) {
5725 err = -ENOTCONN;
5726 goto out;
5727 }
5728
5729 if (!rdev->ops->set_rekey_data) {
5730 err = -EOPNOTSUPP;
5731 goto out;
5732 }
5733
5734 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5735 out:
5736 wdev_unlock(wdev);
5737 return err;
5738}
5739
Johannes Berg4c476992010-10-04 21:36:35 +02005740#define NL80211_FLAG_NEED_WIPHY 0x01
5741#define NL80211_FLAG_NEED_NETDEV 0x02
5742#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005743#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5744#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5745 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005746
5747static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5748 struct genl_info *info)
5749{
5750 struct cfg80211_registered_device *rdev;
5751 struct net_device *dev;
5752 int err;
5753 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5754
5755 if (rtnl)
5756 rtnl_lock();
5757
5758 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5759 rdev = cfg80211_get_dev_from_info(info);
5760 if (IS_ERR(rdev)) {
5761 if (rtnl)
5762 rtnl_unlock();
5763 return PTR_ERR(rdev);
5764 }
5765 info->user_ptr[0] = rdev;
5766 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5767 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5768 if (err) {
5769 if (rtnl)
5770 rtnl_unlock();
5771 return err;
5772 }
Johannes Berg41265712010-10-04 21:14:05 +02005773 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5774 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005775 cfg80211_unlock_rdev(rdev);
5776 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005777 if (rtnl)
5778 rtnl_unlock();
5779 return -ENETDOWN;
5780 }
Johannes Berg4c476992010-10-04 21:36:35 +02005781 info->user_ptr[0] = rdev;
5782 info->user_ptr[1] = dev;
5783 }
5784
5785 return 0;
5786}
5787
5788static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5789 struct genl_info *info)
5790{
5791 if (info->user_ptr[0])
5792 cfg80211_unlock_rdev(info->user_ptr[0]);
5793 if (info->user_ptr[1])
5794 dev_put(info->user_ptr[1]);
5795 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5796 rtnl_unlock();
5797}
5798
Johannes Berg55682962007-09-20 13:09:35 -04005799static struct genl_ops nl80211_ops[] = {
5800 {
5801 .cmd = NL80211_CMD_GET_WIPHY,
5802 .doit = nl80211_get_wiphy,
5803 .dumpit = nl80211_dump_wiphy,
5804 .policy = nl80211_policy,
5805 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005806 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005807 },
5808 {
5809 .cmd = NL80211_CMD_SET_WIPHY,
5810 .doit = nl80211_set_wiphy,
5811 .policy = nl80211_policy,
5812 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005813 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005814 },
5815 {
5816 .cmd = NL80211_CMD_GET_INTERFACE,
5817 .doit = nl80211_get_interface,
5818 .dumpit = nl80211_dump_interface,
5819 .policy = nl80211_policy,
5820 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005821 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005822 },
5823 {
5824 .cmd = NL80211_CMD_SET_INTERFACE,
5825 .doit = nl80211_set_interface,
5826 .policy = nl80211_policy,
5827 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005828 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5829 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005830 },
5831 {
5832 .cmd = NL80211_CMD_NEW_INTERFACE,
5833 .doit = nl80211_new_interface,
5834 .policy = nl80211_policy,
5835 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005836 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5837 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005838 },
5839 {
5840 .cmd = NL80211_CMD_DEL_INTERFACE,
5841 .doit = nl80211_del_interface,
5842 .policy = nl80211_policy,
5843 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005844 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5845 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005846 },
Johannes Berg41ade002007-12-19 02:03:29 +01005847 {
5848 .cmd = NL80211_CMD_GET_KEY,
5849 .doit = nl80211_get_key,
5850 .policy = nl80211_policy,
5851 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005852 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5853 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005854 },
5855 {
5856 .cmd = NL80211_CMD_SET_KEY,
5857 .doit = nl80211_set_key,
5858 .policy = nl80211_policy,
5859 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005860 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005861 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005862 },
5863 {
5864 .cmd = NL80211_CMD_NEW_KEY,
5865 .doit = nl80211_new_key,
5866 .policy = nl80211_policy,
5867 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005868 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005869 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005870 },
5871 {
5872 .cmd = NL80211_CMD_DEL_KEY,
5873 .doit = nl80211_del_key,
5874 .policy = nl80211_policy,
5875 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005876 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005877 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005878 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005879 {
5880 .cmd = NL80211_CMD_SET_BEACON,
5881 .policy = nl80211_policy,
5882 .flags = GENL_ADMIN_PERM,
5883 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005884 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5885 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005886 },
5887 {
5888 .cmd = NL80211_CMD_NEW_BEACON,
5889 .policy = nl80211_policy,
5890 .flags = GENL_ADMIN_PERM,
5891 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005892 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5893 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005894 },
5895 {
5896 .cmd = NL80211_CMD_DEL_BEACON,
5897 .policy = nl80211_policy,
5898 .flags = GENL_ADMIN_PERM,
5899 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005900 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5901 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005902 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005903 {
5904 .cmd = NL80211_CMD_GET_STATION,
5905 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005906 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005907 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005908 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5909 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005910 },
5911 {
5912 .cmd = NL80211_CMD_SET_STATION,
5913 .doit = nl80211_set_station,
5914 .policy = nl80211_policy,
5915 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005916 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5917 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005918 },
5919 {
5920 .cmd = NL80211_CMD_NEW_STATION,
5921 .doit = nl80211_new_station,
5922 .policy = nl80211_policy,
5923 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005924 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005925 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005926 },
5927 {
5928 .cmd = NL80211_CMD_DEL_STATION,
5929 .doit = nl80211_del_station,
5930 .policy = nl80211_policy,
5931 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005932 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5933 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005934 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005935 {
5936 .cmd = NL80211_CMD_GET_MPATH,
5937 .doit = nl80211_get_mpath,
5938 .dumpit = nl80211_dump_mpath,
5939 .policy = nl80211_policy,
5940 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005941 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005942 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005943 },
5944 {
5945 .cmd = NL80211_CMD_SET_MPATH,
5946 .doit = nl80211_set_mpath,
5947 .policy = nl80211_policy,
5948 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005949 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005950 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005951 },
5952 {
5953 .cmd = NL80211_CMD_NEW_MPATH,
5954 .doit = nl80211_new_mpath,
5955 .policy = nl80211_policy,
5956 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005957 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005958 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005959 },
5960 {
5961 .cmd = NL80211_CMD_DEL_MPATH,
5962 .doit = nl80211_del_mpath,
5963 .policy = nl80211_policy,
5964 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005965 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5966 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005967 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005968 {
5969 .cmd = NL80211_CMD_SET_BSS,
5970 .doit = nl80211_set_bss,
5971 .policy = nl80211_policy,
5972 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005973 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5974 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005975 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005976 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005977 .cmd = NL80211_CMD_GET_REG,
5978 .doit = nl80211_get_reg,
5979 .policy = nl80211_policy,
5980 /* can be retrieved by unprivileged users */
5981 },
5982 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005983 .cmd = NL80211_CMD_SET_REG,
5984 .doit = nl80211_set_reg,
5985 .policy = nl80211_policy,
5986 .flags = GENL_ADMIN_PERM,
5987 },
5988 {
5989 .cmd = NL80211_CMD_REQ_SET_REG,
5990 .doit = nl80211_req_set_reg,
5991 .policy = nl80211_policy,
5992 .flags = GENL_ADMIN_PERM,
5993 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005994 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005995 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5996 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005997 .policy = nl80211_policy,
5998 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005999 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6000 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006001 },
6002 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006003 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6004 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006005 .policy = nl80211_policy,
6006 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006007 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006008 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006009 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006010 {
Johannes Berg2a519312009-02-10 21:25:55 +01006011 .cmd = NL80211_CMD_TRIGGER_SCAN,
6012 .doit = nl80211_trigger_scan,
6013 .policy = nl80211_policy,
6014 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006015 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006016 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006017 },
6018 {
6019 .cmd = NL80211_CMD_GET_SCAN,
6020 .policy = nl80211_policy,
6021 .dumpit = nl80211_dump_scan,
6022 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006023 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006024 .cmd = NL80211_CMD_START_SCHED_SCAN,
6025 .doit = nl80211_start_sched_scan,
6026 .policy = nl80211_policy,
6027 .flags = GENL_ADMIN_PERM,
6028 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6029 NL80211_FLAG_NEED_RTNL,
6030 },
6031 {
6032 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6033 .doit = nl80211_stop_sched_scan,
6034 .policy = nl80211_policy,
6035 .flags = GENL_ADMIN_PERM,
6036 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6037 NL80211_FLAG_NEED_RTNL,
6038 },
6039 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006040 .cmd = NL80211_CMD_AUTHENTICATE,
6041 .doit = nl80211_authenticate,
6042 .policy = nl80211_policy,
6043 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006044 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006045 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006046 },
6047 {
6048 .cmd = NL80211_CMD_ASSOCIATE,
6049 .doit = nl80211_associate,
6050 .policy = nl80211_policy,
6051 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006052 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006053 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006054 },
6055 {
6056 .cmd = NL80211_CMD_DEAUTHENTICATE,
6057 .doit = nl80211_deauthenticate,
6058 .policy = nl80211_policy,
6059 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006060 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006061 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006062 },
6063 {
6064 .cmd = NL80211_CMD_DISASSOCIATE,
6065 .doit = nl80211_disassociate,
6066 .policy = nl80211_policy,
6067 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006068 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006069 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006070 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006071 {
6072 .cmd = NL80211_CMD_JOIN_IBSS,
6073 .doit = nl80211_join_ibss,
6074 .policy = nl80211_policy,
6075 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006076 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006077 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006078 },
6079 {
6080 .cmd = NL80211_CMD_LEAVE_IBSS,
6081 .doit = nl80211_leave_ibss,
6082 .policy = nl80211_policy,
6083 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006084 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006085 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006086 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006087#ifdef CONFIG_NL80211_TESTMODE
6088 {
6089 .cmd = NL80211_CMD_TESTMODE,
6090 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006091 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006092 .policy = nl80211_policy,
6093 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006094 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6095 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006096 },
6097#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006098 {
6099 .cmd = NL80211_CMD_CONNECT,
6100 .doit = nl80211_connect,
6101 .policy = nl80211_policy,
6102 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006103 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006104 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006105 },
6106 {
6107 .cmd = NL80211_CMD_DISCONNECT,
6108 .doit = nl80211_disconnect,
6109 .policy = nl80211_policy,
6110 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006111 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006112 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006113 },
Johannes Berg463d0182009-07-14 00:33:35 +02006114 {
6115 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6116 .doit = nl80211_wiphy_netns,
6117 .policy = nl80211_policy,
6118 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006119 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6120 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006121 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006122 {
6123 .cmd = NL80211_CMD_GET_SURVEY,
6124 .policy = nl80211_policy,
6125 .dumpit = nl80211_dump_survey,
6126 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006127 {
6128 .cmd = NL80211_CMD_SET_PMKSA,
6129 .doit = nl80211_setdel_pmksa,
6130 .policy = nl80211_policy,
6131 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006132 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6133 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006134 },
6135 {
6136 .cmd = NL80211_CMD_DEL_PMKSA,
6137 .doit = nl80211_setdel_pmksa,
6138 .policy = nl80211_policy,
6139 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006140 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6141 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006142 },
6143 {
6144 .cmd = NL80211_CMD_FLUSH_PMKSA,
6145 .doit = nl80211_flush_pmksa,
6146 .policy = nl80211_policy,
6147 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006148 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6149 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006150 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006151 {
6152 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6153 .doit = nl80211_remain_on_channel,
6154 .policy = nl80211_policy,
6155 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006156 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006157 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006158 },
6159 {
6160 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6161 .doit = nl80211_cancel_remain_on_channel,
6162 .policy = nl80211_policy,
6163 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006164 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006165 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006166 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006167 {
6168 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6169 .doit = nl80211_set_tx_bitrate_mask,
6170 .policy = nl80211_policy,
6171 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006172 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6173 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006174 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006175 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006176 .cmd = NL80211_CMD_REGISTER_FRAME,
6177 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006178 .policy = nl80211_policy,
6179 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006180 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6181 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006182 },
6183 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006184 .cmd = NL80211_CMD_FRAME,
6185 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006186 .policy = nl80211_policy,
6187 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006188 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006189 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006190 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006191 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006192 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6193 .doit = nl80211_tx_mgmt_cancel_wait,
6194 .policy = nl80211_policy,
6195 .flags = GENL_ADMIN_PERM,
6196 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6197 NL80211_FLAG_NEED_RTNL,
6198 },
6199 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006200 .cmd = NL80211_CMD_SET_POWER_SAVE,
6201 .doit = nl80211_set_power_save,
6202 .policy = nl80211_policy,
6203 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006204 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6205 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006206 },
6207 {
6208 .cmd = NL80211_CMD_GET_POWER_SAVE,
6209 .doit = nl80211_get_power_save,
6210 .policy = nl80211_policy,
6211 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006212 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6213 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006214 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006215 {
6216 .cmd = NL80211_CMD_SET_CQM,
6217 .doit = nl80211_set_cqm,
6218 .policy = nl80211_policy,
6219 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006220 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6221 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006222 },
Johannes Bergf444de02010-05-05 15:25:02 +02006223 {
6224 .cmd = NL80211_CMD_SET_CHANNEL,
6225 .doit = nl80211_set_channel,
6226 .policy = nl80211_policy,
6227 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006228 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6229 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006230 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006231 {
6232 .cmd = NL80211_CMD_SET_WDS_PEER,
6233 .doit = nl80211_set_wds_peer,
6234 .policy = nl80211_policy,
6235 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006236 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6237 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006238 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006239 {
6240 .cmd = NL80211_CMD_JOIN_MESH,
6241 .doit = nl80211_join_mesh,
6242 .policy = nl80211_policy,
6243 .flags = GENL_ADMIN_PERM,
6244 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6245 NL80211_FLAG_NEED_RTNL,
6246 },
6247 {
6248 .cmd = NL80211_CMD_LEAVE_MESH,
6249 .doit = nl80211_leave_mesh,
6250 .policy = nl80211_policy,
6251 .flags = GENL_ADMIN_PERM,
6252 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6253 NL80211_FLAG_NEED_RTNL,
6254 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006255 {
6256 .cmd = NL80211_CMD_GET_WOWLAN,
6257 .doit = nl80211_get_wowlan,
6258 .policy = nl80211_policy,
6259 /* can be retrieved by unprivileged users */
6260 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6261 NL80211_FLAG_NEED_RTNL,
6262 },
6263 {
6264 .cmd = NL80211_CMD_SET_WOWLAN,
6265 .doit = nl80211_set_wowlan,
6266 .policy = nl80211_policy,
6267 .flags = GENL_ADMIN_PERM,
6268 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6269 NL80211_FLAG_NEED_RTNL,
6270 },
Johannes Berge5497d72011-07-05 16:35:40 +02006271 {
6272 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6273 .doit = nl80211_set_rekey_data,
6274 .policy = nl80211_policy,
6275 .flags = GENL_ADMIN_PERM,
6276 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6277 NL80211_FLAG_NEED_RTNL,
6278 },
Johannes Berg55682962007-09-20 13:09:35 -04006279};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006280
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006281static struct genl_multicast_group nl80211_mlme_mcgrp = {
6282 .name = "mlme",
6283};
Johannes Berg55682962007-09-20 13:09:35 -04006284
6285/* multicast groups */
6286static struct genl_multicast_group nl80211_config_mcgrp = {
6287 .name = "config",
6288};
Johannes Berg2a519312009-02-10 21:25:55 +01006289static struct genl_multicast_group nl80211_scan_mcgrp = {
6290 .name = "scan",
6291};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006292static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6293 .name = "regulatory",
6294};
Johannes Berg55682962007-09-20 13:09:35 -04006295
6296/* notification functions */
6297
6298void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6299{
6300 struct sk_buff *msg;
6301
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006302 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006303 if (!msg)
6304 return;
6305
6306 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6307 nlmsg_free(msg);
6308 return;
6309 }
6310
Johannes Berg463d0182009-07-14 00:33:35 +02006311 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6312 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006313}
6314
Johannes Berg362a4152009-05-24 16:43:15 +02006315static int nl80211_add_scan_req(struct sk_buff *msg,
6316 struct cfg80211_registered_device *rdev)
6317{
6318 struct cfg80211_scan_request *req = rdev->scan_req;
6319 struct nlattr *nest;
6320 int i;
6321
Johannes Berg667503dd2009-07-07 03:56:11 +02006322 ASSERT_RDEV_LOCK(rdev);
6323
Johannes Berg362a4152009-05-24 16:43:15 +02006324 if (WARN_ON(!req))
6325 return 0;
6326
6327 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6328 if (!nest)
6329 goto nla_put_failure;
6330 for (i = 0; i < req->n_ssids; i++)
6331 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6332 nla_nest_end(msg, nest);
6333
6334 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6335 if (!nest)
6336 goto nla_put_failure;
6337 for (i = 0; i < req->n_channels; i++)
6338 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6339 nla_nest_end(msg, nest);
6340
6341 if (req->ie)
6342 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6343
6344 return 0;
6345 nla_put_failure:
6346 return -ENOBUFS;
6347}
6348
Johannes Berga538e2d2009-06-16 19:56:42 +02006349static int nl80211_send_scan_msg(struct sk_buff *msg,
6350 struct cfg80211_registered_device *rdev,
6351 struct net_device *netdev,
6352 u32 pid, u32 seq, int flags,
6353 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006354{
6355 void *hdr;
6356
6357 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6358 if (!hdr)
6359 return -1;
6360
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006361 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006362 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6363
Johannes Berg362a4152009-05-24 16:43:15 +02006364 /* ignore errors and send incomplete event anyway */
6365 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006366
6367 return genlmsg_end(msg, hdr);
6368
6369 nla_put_failure:
6370 genlmsg_cancel(msg, hdr);
6371 return -EMSGSIZE;
6372}
6373
Luciano Coelho807f8a82011-05-11 17:09:35 +03006374static int
6375nl80211_send_sched_scan_msg(struct sk_buff *msg,
6376 struct cfg80211_registered_device *rdev,
6377 struct net_device *netdev,
6378 u32 pid, u32 seq, int flags, u32 cmd)
6379{
6380 void *hdr;
6381
6382 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6383 if (!hdr)
6384 return -1;
6385
6386 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6387 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6388
6389 return genlmsg_end(msg, hdr);
6390
6391 nla_put_failure:
6392 genlmsg_cancel(msg, hdr);
6393 return -EMSGSIZE;
6394}
6395
Johannes Berga538e2d2009-06-16 19:56:42 +02006396void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6397 struct net_device *netdev)
6398{
6399 struct sk_buff *msg;
6400
6401 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6402 if (!msg)
6403 return;
6404
6405 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6406 NL80211_CMD_TRIGGER_SCAN) < 0) {
6407 nlmsg_free(msg);
6408 return;
6409 }
6410
Johannes Berg463d0182009-07-14 00:33:35 +02006411 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6412 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006413}
6414
Johannes Berg2a519312009-02-10 21:25:55 +01006415void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6416 struct net_device *netdev)
6417{
6418 struct sk_buff *msg;
6419
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006420 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006421 if (!msg)
6422 return;
6423
Johannes Berga538e2d2009-06-16 19:56:42 +02006424 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6425 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006426 nlmsg_free(msg);
6427 return;
6428 }
6429
Johannes Berg463d0182009-07-14 00:33:35 +02006430 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6431 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006432}
6433
6434void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6435 struct net_device *netdev)
6436{
6437 struct sk_buff *msg;
6438
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006439 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006440 if (!msg)
6441 return;
6442
Johannes Berga538e2d2009-06-16 19:56:42 +02006443 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6444 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006445 nlmsg_free(msg);
6446 return;
6447 }
6448
Johannes Berg463d0182009-07-14 00:33:35 +02006449 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6450 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006451}
6452
Luciano Coelho807f8a82011-05-11 17:09:35 +03006453void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6454 struct net_device *netdev)
6455{
6456 struct sk_buff *msg;
6457
6458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6459 if (!msg)
6460 return;
6461
6462 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6463 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6464 nlmsg_free(msg);
6465 return;
6466 }
6467
6468 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6469 nl80211_scan_mcgrp.id, GFP_KERNEL);
6470}
6471
6472void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6473 struct net_device *netdev, u32 cmd)
6474{
6475 struct sk_buff *msg;
6476
6477 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6478 if (!msg)
6479 return;
6480
6481 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6482 nlmsg_free(msg);
6483 return;
6484 }
6485
6486 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6487 nl80211_scan_mcgrp.id, GFP_KERNEL);
6488}
6489
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006490/*
6491 * This can happen on global regulatory changes or device specific settings
6492 * based on custom world regulatory domains.
6493 */
6494void nl80211_send_reg_change_event(struct regulatory_request *request)
6495{
6496 struct sk_buff *msg;
6497 void *hdr;
6498
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006499 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006500 if (!msg)
6501 return;
6502
6503 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6504 if (!hdr) {
6505 nlmsg_free(msg);
6506 return;
6507 }
6508
6509 /* Userspace can always count this one always being set */
6510 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6511
6512 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6513 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6514 NL80211_REGDOM_TYPE_WORLD);
6515 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6516 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6517 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6518 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6519 request->intersect)
6520 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6521 NL80211_REGDOM_TYPE_INTERSECTION);
6522 else {
6523 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6524 NL80211_REGDOM_TYPE_COUNTRY);
6525 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6526 }
6527
6528 if (wiphy_idx_valid(request->wiphy_idx))
6529 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6530
6531 if (genlmsg_end(msg, hdr) < 0) {
6532 nlmsg_free(msg);
6533 return;
6534 }
6535
Johannes Bergbc43b282009-07-25 10:54:13 +02006536 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006537 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006538 GFP_ATOMIC);
6539 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006540
6541 return;
6542
6543nla_put_failure:
6544 genlmsg_cancel(msg, hdr);
6545 nlmsg_free(msg);
6546}
6547
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006548static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6549 struct net_device *netdev,
6550 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006551 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006552{
6553 struct sk_buff *msg;
6554 void *hdr;
6555
Johannes Berge6d6e342009-07-01 21:26:47 +02006556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006557 if (!msg)
6558 return;
6559
6560 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6561 if (!hdr) {
6562 nlmsg_free(msg);
6563 return;
6564 }
6565
6566 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6567 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6568 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6569
6570 if (genlmsg_end(msg, hdr) < 0) {
6571 nlmsg_free(msg);
6572 return;
6573 }
6574
Johannes Berg463d0182009-07-14 00:33:35 +02006575 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6576 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006577 return;
6578
6579 nla_put_failure:
6580 genlmsg_cancel(msg, hdr);
6581 nlmsg_free(msg);
6582}
6583
6584void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006585 struct net_device *netdev, const u8 *buf,
6586 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006587{
6588 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006589 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006590}
6591
6592void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6593 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006594 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006595{
Johannes Berge6d6e342009-07-01 21:26:47 +02006596 nl80211_send_mlme_event(rdev, netdev, buf, len,
6597 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006598}
6599
Jouni Malinen53b46b82009-03-27 20:53:56 +02006600void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006601 struct net_device *netdev, const u8 *buf,
6602 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006603{
6604 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006605 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006606}
6607
Jouni Malinen53b46b82009-03-27 20:53:56 +02006608void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6609 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006610 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006611{
6612 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006613 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006614}
6615
Jouni Malinencf4e5942010-12-16 00:52:40 +02006616void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6617 struct net_device *netdev, const u8 *buf,
6618 size_t len, gfp_t gfp)
6619{
6620 nl80211_send_mlme_event(rdev, netdev, buf, len,
6621 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6622}
6623
6624void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6625 struct net_device *netdev, const u8 *buf,
6626 size_t len, gfp_t gfp)
6627{
6628 nl80211_send_mlme_event(rdev, netdev, buf, len,
6629 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6630}
6631
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006632static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6633 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006634 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006635{
6636 struct sk_buff *msg;
6637 void *hdr;
6638
Johannes Berge6d6e342009-07-01 21:26:47 +02006639 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006640 if (!msg)
6641 return;
6642
6643 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6644 if (!hdr) {
6645 nlmsg_free(msg);
6646 return;
6647 }
6648
6649 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6650 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6651 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6652 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6653
6654 if (genlmsg_end(msg, hdr) < 0) {
6655 nlmsg_free(msg);
6656 return;
6657 }
6658
Johannes Berg463d0182009-07-14 00:33:35 +02006659 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6660 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006661 return;
6662
6663 nla_put_failure:
6664 genlmsg_cancel(msg, hdr);
6665 nlmsg_free(msg);
6666}
6667
6668void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006669 struct net_device *netdev, const u8 *addr,
6670 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006671{
6672 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006673 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006674}
6675
6676void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006677 struct net_device *netdev, const u8 *addr,
6678 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006679{
Johannes Berge6d6e342009-07-01 21:26:47 +02006680 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6681 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006682}
6683
Samuel Ortizb23aa672009-07-01 21:26:54 +02006684void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6685 struct net_device *netdev, const u8 *bssid,
6686 const u8 *req_ie, size_t req_ie_len,
6687 const u8 *resp_ie, size_t resp_ie_len,
6688 u16 status, gfp_t gfp)
6689{
6690 struct sk_buff *msg;
6691 void *hdr;
6692
6693 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6694 if (!msg)
6695 return;
6696
6697 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6698 if (!hdr) {
6699 nlmsg_free(msg);
6700 return;
6701 }
6702
6703 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6704 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6705 if (bssid)
6706 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6707 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6708 if (req_ie)
6709 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6710 if (resp_ie)
6711 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6712
6713 if (genlmsg_end(msg, hdr) < 0) {
6714 nlmsg_free(msg);
6715 return;
6716 }
6717
Johannes Berg463d0182009-07-14 00:33:35 +02006718 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6719 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006720 return;
6721
6722 nla_put_failure:
6723 genlmsg_cancel(msg, hdr);
6724 nlmsg_free(msg);
6725
6726}
6727
6728void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6729 struct net_device *netdev, const u8 *bssid,
6730 const u8 *req_ie, size_t req_ie_len,
6731 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6732{
6733 struct sk_buff *msg;
6734 void *hdr;
6735
6736 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6737 if (!msg)
6738 return;
6739
6740 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6741 if (!hdr) {
6742 nlmsg_free(msg);
6743 return;
6744 }
6745
6746 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6747 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6748 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6749 if (req_ie)
6750 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6751 if (resp_ie)
6752 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6753
6754 if (genlmsg_end(msg, hdr) < 0) {
6755 nlmsg_free(msg);
6756 return;
6757 }
6758
Johannes Berg463d0182009-07-14 00:33:35 +02006759 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6760 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006761 return;
6762
6763 nla_put_failure:
6764 genlmsg_cancel(msg, hdr);
6765 nlmsg_free(msg);
6766
6767}
6768
6769void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6770 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02006771 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006772{
6773 struct sk_buff *msg;
6774 void *hdr;
6775
Johannes Berg667503dd2009-07-07 03:56:11 +02006776 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006777 if (!msg)
6778 return;
6779
6780 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6781 if (!hdr) {
6782 nlmsg_free(msg);
6783 return;
6784 }
6785
6786 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6787 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6788 if (from_ap && reason)
6789 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6790 if (from_ap)
6791 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6792 if (ie)
6793 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6794
6795 if (genlmsg_end(msg, hdr) < 0) {
6796 nlmsg_free(msg);
6797 return;
6798 }
6799
Johannes Berg463d0182009-07-14 00:33:35 +02006800 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6801 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006802 return;
6803
6804 nla_put_failure:
6805 genlmsg_cancel(msg, hdr);
6806 nlmsg_free(msg);
6807
6808}
6809
Johannes Berg04a773a2009-04-19 21:24:32 +02006810void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6811 struct net_device *netdev, const u8 *bssid,
6812 gfp_t gfp)
6813{
6814 struct sk_buff *msg;
6815 void *hdr;
6816
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006817 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006818 if (!msg)
6819 return;
6820
6821 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6822 if (!hdr) {
6823 nlmsg_free(msg);
6824 return;
6825 }
6826
6827 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6828 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6829 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6830
6831 if (genlmsg_end(msg, hdr) < 0) {
6832 nlmsg_free(msg);
6833 return;
6834 }
6835
Johannes Berg463d0182009-07-14 00:33:35 +02006836 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6837 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006838 return;
6839
6840 nla_put_failure:
6841 genlmsg_cancel(msg, hdr);
6842 nlmsg_free(msg);
6843}
6844
Javier Cardonac93b5e72011-04-07 15:08:34 -07006845void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6846 struct net_device *netdev,
6847 const u8 *macaddr, const u8* ie, u8 ie_len,
6848 gfp_t gfp)
6849{
6850 struct sk_buff *msg;
6851 void *hdr;
6852
6853 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6854 if (!msg)
6855 return;
6856
6857 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6858 if (!hdr) {
6859 nlmsg_free(msg);
6860 return;
6861 }
6862
6863 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6864 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6865 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6866 if (ie_len && ie)
6867 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6868
6869 if (genlmsg_end(msg, hdr) < 0) {
6870 nlmsg_free(msg);
6871 return;
6872 }
6873
6874 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6875 nl80211_mlme_mcgrp.id, gfp);
6876 return;
6877
6878 nla_put_failure:
6879 genlmsg_cancel(msg, hdr);
6880 nlmsg_free(msg);
6881}
6882
Jouni Malinena3b8b052009-03-27 21:59:49 +02006883void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6884 struct net_device *netdev, const u8 *addr,
6885 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006886 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006887{
6888 struct sk_buff *msg;
6889 void *hdr;
6890
Johannes Berge6d6e342009-07-01 21:26:47 +02006891 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006892 if (!msg)
6893 return;
6894
6895 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6896 if (!hdr) {
6897 nlmsg_free(msg);
6898 return;
6899 }
6900
6901 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6902 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6903 if (addr)
6904 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6905 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03006906 if (key_id != -1)
6907 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006908 if (tsc)
6909 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6910
6911 if (genlmsg_end(msg, hdr) < 0) {
6912 nlmsg_free(msg);
6913 return;
6914 }
6915
Johannes Berg463d0182009-07-14 00:33:35 +02006916 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6917 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006918 return;
6919
6920 nla_put_failure:
6921 genlmsg_cancel(msg, hdr);
6922 nlmsg_free(msg);
6923}
6924
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006925void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6926 struct ieee80211_channel *channel_before,
6927 struct ieee80211_channel *channel_after)
6928{
6929 struct sk_buff *msg;
6930 void *hdr;
6931 struct nlattr *nl_freq;
6932
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006933 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006934 if (!msg)
6935 return;
6936
6937 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6938 if (!hdr) {
6939 nlmsg_free(msg);
6940 return;
6941 }
6942
6943 /*
6944 * Since we are applying the beacon hint to a wiphy we know its
6945 * wiphy_idx is valid
6946 */
6947 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6948
6949 /* Before */
6950 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6951 if (!nl_freq)
6952 goto nla_put_failure;
6953 if (nl80211_msg_put_channel(msg, channel_before))
6954 goto nla_put_failure;
6955 nla_nest_end(msg, nl_freq);
6956
6957 /* After */
6958 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6959 if (!nl_freq)
6960 goto nla_put_failure;
6961 if (nl80211_msg_put_channel(msg, channel_after))
6962 goto nla_put_failure;
6963 nla_nest_end(msg, nl_freq);
6964
6965 if (genlmsg_end(msg, hdr) < 0) {
6966 nlmsg_free(msg);
6967 return;
6968 }
6969
Johannes Berg463d0182009-07-14 00:33:35 +02006970 rcu_read_lock();
6971 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6972 GFP_ATOMIC);
6973 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006974
6975 return;
6976
6977nla_put_failure:
6978 genlmsg_cancel(msg, hdr);
6979 nlmsg_free(msg);
6980}
6981
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006982static void nl80211_send_remain_on_chan_event(
6983 int cmd, struct cfg80211_registered_device *rdev,
6984 struct net_device *netdev, u64 cookie,
6985 struct ieee80211_channel *chan,
6986 enum nl80211_channel_type channel_type,
6987 unsigned int duration, gfp_t gfp)
6988{
6989 struct sk_buff *msg;
6990 void *hdr;
6991
6992 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6993 if (!msg)
6994 return;
6995
6996 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6997 if (!hdr) {
6998 nlmsg_free(msg);
6999 return;
7000 }
7001
7002 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7003 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7004 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7005 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7006 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7007
7008 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7009 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7010
7011 if (genlmsg_end(msg, hdr) < 0) {
7012 nlmsg_free(msg);
7013 return;
7014 }
7015
7016 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7017 nl80211_mlme_mcgrp.id, gfp);
7018 return;
7019
7020 nla_put_failure:
7021 genlmsg_cancel(msg, hdr);
7022 nlmsg_free(msg);
7023}
7024
7025void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7026 struct net_device *netdev, u64 cookie,
7027 struct ieee80211_channel *chan,
7028 enum nl80211_channel_type channel_type,
7029 unsigned int duration, gfp_t gfp)
7030{
7031 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7032 rdev, netdev, cookie, chan,
7033 channel_type, duration, gfp);
7034}
7035
7036void nl80211_send_remain_on_channel_cancel(
7037 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7038 u64 cookie, struct ieee80211_channel *chan,
7039 enum nl80211_channel_type channel_type, gfp_t gfp)
7040{
7041 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7042 rdev, netdev, cookie, chan,
7043 channel_type, 0, gfp);
7044}
7045
Johannes Berg98b62182009-12-23 13:15:44 +01007046void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7047 struct net_device *dev, const u8 *mac_addr,
7048 struct station_info *sinfo, gfp_t gfp)
7049{
7050 struct sk_buff *msg;
7051
7052 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7053 if (!msg)
7054 return;
7055
7056 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7057 nlmsg_free(msg);
7058 return;
7059 }
7060
7061 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7062 nl80211_mlme_mcgrp.id, gfp);
7063}
7064
Jouni Malinenec15e682011-03-23 15:29:52 +02007065void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7066 struct net_device *dev, const u8 *mac_addr,
7067 gfp_t gfp)
7068{
7069 struct sk_buff *msg;
7070 void *hdr;
7071
7072 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7073 if (!msg)
7074 return;
7075
7076 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7077 if (!hdr) {
7078 nlmsg_free(msg);
7079 return;
7080 }
7081
7082 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7083 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7084
7085 if (genlmsg_end(msg, hdr) < 0) {
7086 nlmsg_free(msg);
7087 return;
7088 }
7089
7090 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7091 nl80211_mlme_mcgrp.id, gfp);
7092 return;
7093
7094 nla_put_failure:
7095 genlmsg_cancel(msg, hdr);
7096 nlmsg_free(msg);
7097}
7098
Johannes Berg2e161f72010-08-12 15:38:38 +02007099int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7100 struct net_device *netdev, u32 nlpid,
7101 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007102{
7103 struct sk_buff *msg;
7104 void *hdr;
7105 int err;
7106
7107 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7108 if (!msg)
7109 return -ENOMEM;
7110
Johannes Berg2e161f72010-08-12 15:38:38 +02007111 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007112 if (!hdr) {
7113 nlmsg_free(msg);
7114 return -ENOMEM;
7115 }
7116
7117 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7118 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7119 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7120 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7121
7122 err = genlmsg_end(msg, hdr);
7123 if (err < 0) {
7124 nlmsg_free(msg);
7125 return err;
7126 }
7127
7128 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7129 if (err < 0)
7130 return err;
7131 return 0;
7132
7133 nla_put_failure:
7134 genlmsg_cancel(msg, hdr);
7135 nlmsg_free(msg);
7136 return -ENOBUFS;
7137}
7138
Johannes Berg2e161f72010-08-12 15:38:38 +02007139void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7140 struct net_device *netdev, u64 cookie,
7141 const u8 *buf, size_t len, bool ack,
7142 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007143{
7144 struct sk_buff *msg;
7145 void *hdr;
7146
7147 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7148 if (!msg)
7149 return;
7150
Johannes Berg2e161f72010-08-12 15:38:38 +02007151 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007152 if (!hdr) {
7153 nlmsg_free(msg);
7154 return;
7155 }
7156
7157 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7158 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7159 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7160 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7161 if (ack)
7162 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7163
7164 if (genlmsg_end(msg, hdr) < 0) {
7165 nlmsg_free(msg);
7166 return;
7167 }
7168
7169 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7170 return;
7171
7172 nla_put_failure:
7173 genlmsg_cancel(msg, hdr);
7174 nlmsg_free(msg);
7175}
7176
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007177void
7178nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7179 struct net_device *netdev,
7180 enum nl80211_cqm_rssi_threshold_event rssi_event,
7181 gfp_t gfp)
7182{
7183 struct sk_buff *msg;
7184 struct nlattr *pinfoattr;
7185 void *hdr;
7186
7187 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7188 if (!msg)
7189 return;
7190
7191 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7192 if (!hdr) {
7193 nlmsg_free(msg);
7194 return;
7195 }
7196
7197 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7198 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7199
7200 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7201 if (!pinfoattr)
7202 goto nla_put_failure;
7203
7204 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7205 rssi_event);
7206
7207 nla_nest_end(msg, pinfoattr);
7208
7209 if (genlmsg_end(msg, hdr) < 0) {
7210 nlmsg_free(msg);
7211 return;
7212 }
7213
7214 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7215 nl80211_mlme_mcgrp.id, gfp);
7216 return;
7217
7218 nla_put_failure:
7219 genlmsg_cancel(msg, hdr);
7220 nlmsg_free(msg);
7221}
7222
Johannes Berge5497d72011-07-05 16:35:40 +02007223void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7224 struct net_device *netdev, const u8 *bssid,
7225 const u8 *replay_ctr, gfp_t gfp)
7226{
7227 struct sk_buff *msg;
7228 struct nlattr *rekey_attr;
7229 void *hdr;
7230
7231 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7232 if (!msg)
7233 return;
7234
7235 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7236 if (!hdr) {
7237 nlmsg_free(msg);
7238 return;
7239 }
7240
7241 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7243 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7244
7245 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7246 if (!rekey_attr)
7247 goto nla_put_failure;
7248
7249 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7250 NL80211_REPLAY_CTR_LEN, replay_ctr);
7251
7252 nla_nest_end(msg, rekey_attr);
7253
7254 if (genlmsg_end(msg, hdr) < 0) {
7255 nlmsg_free(msg);
7256 return;
7257 }
7258
7259 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7260 nl80211_mlme_mcgrp.id, gfp);
7261 return;
7262
7263 nla_put_failure:
7264 genlmsg_cancel(msg, hdr);
7265 nlmsg_free(msg);
7266}
7267
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007268void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7269 struct net_device *netdev, int index,
7270 const u8 *bssid, bool preauth, gfp_t gfp)
7271{
7272 struct sk_buff *msg;
7273 struct nlattr *attr;
7274 void *hdr;
7275
7276 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7277 if (!msg)
7278 return;
7279
7280 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7281 if (!hdr) {
7282 nlmsg_free(msg);
7283 return;
7284 }
7285
7286 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7287 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7288
7289 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7290 if (!attr)
7291 goto nla_put_failure;
7292
7293 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7294 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7295 if (preauth)
7296 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7297
7298 nla_nest_end(msg, attr);
7299
7300 if (genlmsg_end(msg, hdr) < 0) {
7301 nlmsg_free(msg);
7302 return;
7303 }
7304
7305 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7306 nl80211_mlme_mcgrp.id, gfp);
7307 return;
7308
7309 nla_put_failure:
7310 genlmsg_cancel(msg, hdr);
7311 nlmsg_free(msg);
7312}
7313
Johannes Bergc063dbf2010-11-24 08:10:05 +01007314void
7315nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7316 struct net_device *netdev, const u8 *peer,
7317 u32 num_packets, gfp_t gfp)
7318{
7319 struct sk_buff *msg;
7320 struct nlattr *pinfoattr;
7321 void *hdr;
7322
7323 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7324 if (!msg)
7325 return;
7326
7327 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7328 if (!hdr) {
7329 nlmsg_free(msg);
7330 return;
7331 }
7332
7333 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7334 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7335 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7336
7337 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7338 if (!pinfoattr)
7339 goto nla_put_failure;
7340
7341 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7342
7343 nla_nest_end(msg, pinfoattr);
7344
7345 if (genlmsg_end(msg, hdr) < 0) {
7346 nlmsg_free(msg);
7347 return;
7348 }
7349
7350 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7351 nl80211_mlme_mcgrp.id, gfp);
7352 return;
7353
7354 nla_put_failure:
7355 genlmsg_cancel(msg, hdr);
7356 nlmsg_free(msg);
7357}
7358
Jouni Malinen026331c2010-02-15 12:53:10 +02007359static int nl80211_netlink_notify(struct notifier_block * nb,
7360 unsigned long state,
7361 void *_notify)
7362{
7363 struct netlink_notify *notify = _notify;
7364 struct cfg80211_registered_device *rdev;
7365 struct wireless_dev *wdev;
7366
7367 if (state != NETLINK_URELEASE)
7368 return NOTIFY_DONE;
7369
7370 rcu_read_lock();
7371
7372 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7373 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007374 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007375
7376 rcu_read_unlock();
7377
7378 return NOTIFY_DONE;
7379}
7380
7381static struct notifier_block nl80211_netlink_notifier = {
7382 .notifier_call = nl80211_netlink_notify,
7383};
7384
Johannes Berg55682962007-09-20 13:09:35 -04007385/* initialisation/exit functions */
7386
7387int nl80211_init(void)
7388{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007389 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007390
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007391 err = genl_register_family_with_ops(&nl80211_fam,
7392 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007393 if (err)
7394 return err;
7395
Johannes Berg55682962007-09-20 13:09:35 -04007396 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7397 if (err)
7398 goto err_out;
7399
Johannes Berg2a519312009-02-10 21:25:55 +01007400 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7401 if (err)
7402 goto err_out;
7403
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007404 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7405 if (err)
7406 goto err_out;
7407
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007408 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7409 if (err)
7410 goto err_out;
7411
Johannes Bergaff89a92009-07-01 21:26:51 +02007412#ifdef CONFIG_NL80211_TESTMODE
7413 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7414 if (err)
7415 goto err_out;
7416#endif
7417
Jouni Malinen026331c2010-02-15 12:53:10 +02007418 err = netlink_register_notifier(&nl80211_netlink_notifier);
7419 if (err)
7420 goto err_out;
7421
Johannes Berg55682962007-09-20 13:09:35 -04007422 return 0;
7423 err_out:
7424 genl_unregister_family(&nl80211_fam);
7425 return err;
7426}
7427
7428void nl80211_exit(void)
7429{
Jouni Malinen026331c2010-02-15 12:53:10 +02007430 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007431 genl_unregister_family(&nl80211_fam);
7432}