blob: 3c6427abdf3425f353d929168218b076b711b6da [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020050static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020051 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040052 struct net_device **dev)
53{
Johannes Berg463d0182009-07-14 00:33:35 +020054 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040055 int ifindex;
56
Johannes Bergbba95fe2008-07-29 13:22:51 +020057 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040058 return -EINVAL;
59
Johannes Bergbba95fe2008-07-29 13:22:51 +020060 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020061 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040062 if (!*dev)
63 return -ENODEV;
64
Johannes Berg463d0182009-07-14 00:33:35 +020065 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020066 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040067 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020068 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040069 }
70
71 return 0;
72}
73
74/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000075static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040076 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
77 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070078 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020079 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020080 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053081 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020082 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
84 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010086 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040087
88 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
90 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010091
92 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020093 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010094
Johannes Bergb9454e82009-07-08 13:29:08 +020095 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010096 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
97 .len = WLAN_MAX_KEY_LEN },
98 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
99 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
100 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +0300101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berge31b8212010-10-05 19:39:30 +0200102 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100103
104 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
105 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
106 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
107 .len = IEEE80211_MAX_DATA_LEN },
108 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
109 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
111 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
112 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
113 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100115 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100116 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200117 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100118 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
119 .len = IEEE80211_MAX_MESH_ID_LEN },
120 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300121
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700122 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
123 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
124
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300125 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200128 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
129 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100130 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300131
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800132 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700133 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700134
Jouni Malinen36aedc92008-08-25 11:58:58 +0300135 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
136 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200137
138 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
139 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
140 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100141 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
142 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200143
144 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
145 .len = IEEE80211_MAX_SSID_LEN },
146 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
147 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200148 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300149 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300150 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300151 [NL80211_ATTR_STA_FLAGS2] = {
152 .len = sizeof(struct nl80211_sta_flag_update),
153 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300154 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300155 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
156 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200157 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
158 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
159 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200160 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100161 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100162 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
163 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100164 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
165 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200166 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200167 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
168 .len = IEEE80211_MAX_DATA_LEN },
169 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200170 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200171 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300172 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200173 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300174 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
175 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200176 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900177 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
178 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100179 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100180 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100181 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200182 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700183 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300184 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200185 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200186 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300187 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300188 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
190 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
191 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530192 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300193 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Johannes Berg55682962007-09-20 13:09:35 -0400194};
195
Johannes Berge31b8212010-10-05 19:39:30 +0200196/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000197static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200198 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200199 [NL80211_KEY_IDX] = { .type = NLA_U8 },
200 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
201 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
202 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
203 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200204 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100205 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
206};
207
208/* policy for the key default flags */
209static const struct nla_policy
210nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
211 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
212 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200213};
214
Johannes Bergff1b6e62011-05-04 15:37:28 +0200215/* policy for WoWLAN attributes */
216static const struct nla_policy
217nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
218 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
219 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
220 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
221 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200222 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
223 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
224 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
225 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200226};
227
Johannes Berge5497d72011-07-05 16:35:40 +0200228/* policy for GTK rekey offload attributes */
229static const struct nla_policy
230nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
231 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
232 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
233 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
234};
235
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300236static const struct nla_policy
237nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
238 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
239 .len = IEEE80211_MAX_SSID_LEN },
240};
241
Holger Schuriga0438972009-11-11 11:30:02 +0100242/* ifidx get helper */
243static int nl80211_get_ifidx(struct netlink_callback *cb)
244{
245 int res;
246
247 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
248 nl80211_fam.attrbuf, nl80211_fam.maxattr,
249 nl80211_policy);
250 if (res)
251 return res;
252
253 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
254 return -EINVAL;
255
256 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
257 if (!res)
258 return -EINVAL;
259 return res;
260}
261
Johannes Berg67748892010-10-04 21:14:06 +0200262static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
263 struct netlink_callback *cb,
264 struct cfg80211_registered_device **rdev,
265 struct net_device **dev)
266{
267 int ifidx = cb->args[0];
268 int err;
269
270 if (!ifidx)
271 ifidx = nl80211_get_ifidx(cb);
272 if (ifidx < 0)
273 return ifidx;
274
275 cb->args[0] = ifidx;
276
277 rtnl_lock();
278
279 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
280 if (!*dev) {
281 err = -ENODEV;
282 goto out_rtnl;
283 }
284
285 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100286 if (IS_ERR(*rdev)) {
287 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200288 goto out_rtnl;
289 }
290
291 return 0;
292 out_rtnl:
293 rtnl_unlock();
294 return err;
295}
296
297static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
298{
299 cfg80211_unlock_rdev(rdev);
300 rtnl_unlock();
301}
302
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100303/* IE validation */
304static bool is_valid_ie_attr(const struct nlattr *attr)
305{
306 const u8 *pos;
307 int len;
308
309 if (!attr)
310 return true;
311
312 pos = nla_data(attr);
313 len = nla_len(attr);
314
315 while (len) {
316 u8 elemlen;
317
318 if (len < 2)
319 return false;
320 len -= 2;
321
322 elemlen = pos[1];
323 if (elemlen > len)
324 return false;
325
326 len -= elemlen;
327 pos += 2 + elemlen;
328 }
329
330 return true;
331}
332
Johannes Berg55682962007-09-20 13:09:35 -0400333/* message building helper */
334static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
335 int flags, u8 cmd)
336{
337 /* since there is no private header just add the generic one */
338 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
339}
340
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400341static int nl80211_msg_put_channel(struct sk_buff *msg,
342 struct ieee80211_channel *chan)
343{
344 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
345 chan->center_freq);
346
347 if (chan->flags & IEEE80211_CHAN_DISABLED)
348 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
349 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
350 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
351 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
352 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
353 if (chan->flags & IEEE80211_CHAN_RADAR)
354 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
355
356 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
357 DBM_TO_MBM(chan->max_power));
358
359 return 0;
360
361 nla_put_failure:
362 return -ENOBUFS;
363}
364
Johannes Berg55682962007-09-20 13:09:35 -0400365/* netlink command implementations */
366
Johannes Bergb9454e82009-07-08 13:29:08 +0200367struct key_parse {
368 struct key_params p;
369 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200370 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200371 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100372 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200373};
374
375static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
376{
377 struct nlattr *tb[NL80211_KEY_MAX + 1];
378 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
379 nl80211_key_policy);
380 if (err)
381 return err;
382
383 k->def = !!tb[NL80211_KEY_DEFAULT];
384 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
385
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100386 if (k->def) {
387 k->def_uni = true;
388 k->def_multi = true;
389 }
390 if (k->defmgmt)
391 k->def_multi = true;
392
Johannes Bergb9454e82009-07-08 13:29:08 +0200393 if (tb[NL80211_KEY_IDX])
394 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
395
396 if (tb[NL80211_KEY_DATA]) {
397 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
398 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
399 }
400
401 if (tb[NL80211_KEY_SEQ]) {
402 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
403 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
404 }
405
406 if (tb[NL80211_KEY_CIPHER])
407 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
408
Johannes Berge31b8212010-10-05 19:39:30 +0200409 if (tb[NL80211_KEY_TYPE]) {
410 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
411 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
412 return -EINVAL;
413 }
414
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100415 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
416 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
417 int err = nla_parse_nested(kdt,
418 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
419 tb[NL80211_KEY_DEFAULT_TYPES],
420 nl80211_key_default_policy);
421 if (err)
422 return err;
423
424 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
425 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
426 }
427
Johannes Bergb9454e82009-07-08 13:29:08 +0200428 return 0;
429}
430
431static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
432{
433 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
434 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
435 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
436 }
437
438 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
439 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
440 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
441 }
442
443 if (info->attrs[NL80211_ATTR_KEY_IDX])
444 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
445
446 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
447 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
448
449 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
450 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
451
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100452 if (k->def) {
453 k->def_uni = true;
454 k->def_multi = true;
455 }
456 if (k->defmgmt)
457 k->def_multi = true;
458
Johannes Berge31b8212010-10-05 19:39:30 +0200459 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
460 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
461 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
462 return -EINVAL;
463 }
464
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100465 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
466 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
467 int err = nla_parse_nested(
468 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
469 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
470 nl80211_key_default_policy);
471 if (err)
472 return err;
473
474 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
475 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
476 }
477
Johannes Bergb9454e82009-07-08 13:29:08 +0200478 return 0;
479}
480
481static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
482{
483 int err;
484
485 memset(k, 0, sizeof(*k));
486 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200487 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200488
489 if (info->attrs[NL80211_ATTR_KEY])
490 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
491 else
492 err = nl80211_parse_key_old(info, k);
493
494 if (err)
495 return err;
496
497 if (k->def && k->defmgmt)
498 return -EINVAL;
499
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100500 if (k->defmgmt) {
501 if (k->def_uni || !k->def_multi)
502 return -EINVAL;
503 }
504
Johannes Bergb9454e82009-07-08 13:29:08 +0200505 if (k->idx != -1) {
506 if (k->defmgmt) {
507 if (k->idx < 4 || k->idx > 5)
508 return -EINVAL;
509 } else if (k->def) {
510 if (k->idx < 0 || k->idx > 3)
511 return -EINVAL;
512 } else {
513 if (k->idx < 0 || k->idx > 5)
514 return -EINVAL;
515 }
516 }
517
518 return 0;
519}
520
Johannes Bergfffd0932009-07-08 14:22:54 +0200521static struct cfg80211_cached_keys *
522nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
523 struct nlattr *keys)
524{
525 struct key_parse parse;
526 struct nlattr *key;
527 struct cfg80211_cached_keys *result;
528 int rem, err, def = 0;
529
530 result = kzalloc(sizeof(*result), GFP_KERNEL);
531 if (!result)
532 return ERR_PTR(-ENOMEM);
533
534 result->def = -1;
535 result->defmgmt = -1;
536
537 nla_for_each_nested(key, keys, rem) {
538 memset(&parse, 0, sizeof(parse));
539 parse.idx = -1;
540
541 err = nl80211_parse_key_new(key, &parse);
542 if (err)
543 goto error;
544 err = -EINVAL;
545 if (!parse.p.key)
546 goto error;
547 if (parse.idx < 0 || parse.idx > 4)
548 goto error;
549 if (parse.def) {
550 if (def)
551 goto error;
552 def = 1;
553 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100554 if (!parse.def_uni || !parse.def_multi)
555 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200556 } else if (parse.defmgmt)
557 goto error;
558 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200559 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200560 if (err)
561 goto error;
562 result->params[parse.idx].cipher = parse.p.cipher;
563 result->params[parse.idx].key_len = parse.p.key_len;
564 result->params[parse.idx].key = result->data[parse.idx];
565 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
566 }
567
568 return result;
569 error:
570 kfree(result);
571 return ERR_PTR(err);
572}
573
574static int nl80211_key_allowed(struct wireless_dev *wdev)
575{
576 ASSERT_WDEV_LOCK(wdev);
577
Johannes Bergfffd0932009-07-08 14:22:54 +0200578 switch (wdev->iftype) {
579 case NL80211_IFTYPE_AP:
580 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200581 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700582 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200583 break;
584 case NL80211_IFTYPE_ADHOC:
585 if (!wdev->current_bss)
586 return -ENOLINK;
587 break;
588 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200589 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200590 if (wdev->sme_state != CFG80211_SME_CONNECTED)
591 return -ENOLINK;
592 break;
593 default:
594 return -EINVAL;
595 }
596
597 return 0;
598}
599
Johannes Berg7527a782011-05-13 10:58:57 +0200600static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
601{
602 struct nlattr *nl_modes = nla_nest_start(msg, attr);
603 int i;
604
605 if (!nl_modes)
606 goto nla_put_failure;
607
608 i = 0;
609 while (ifmodes) {
610 if (ifmodes & 1)
611 NLA_PUT_FLAG(msg, i);
612 ifmodes >>= 1;
613 i++;
614 }
615
616 nla_nest_end(msg, nl_modes);
617 return 0;
618
619nla_put_failure:
620 return -ENOBUFS;
621}
622
623static int nl80211_put_iface_combinations(struct wiphy *wiphy,
624 struct sk_buff *msg)
625{
626 struct nlattr *nl_combis;
627 int i, j;
628
629 nl_combis = nla_nest_start(msg,
630 NL80211_ATTR_INTERFACE_COMBINATIONS);
631 if (!nl_combis)
632 goto nla_put_failure;
633
634 for (i = 0; i < wiphy->n_iface_combinations; i++) {
635 const struct ieee80211_iface_combination *c;
636 struct nlattr *nl_combi, *nl_limits;
637
638 c = &wiphy->iface_combinations[i];
639
640 nl_combi = nla_nest_start(msg, i + 1);
641 if (!nl_combi)
642 goto nla_put_failure;
643
644 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
645 if (!nl_limits)
646 goto nla_put_failure;
647
648 for (j = 0; j < c->n_limits; j++) {
649 struct nlattr *nl_limit;
650
651 nl_limit = nla_nest_start(msg, j + 1);
652 if (!nl_limit)
653 goto nla_put_failure;
654 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
655 c->limits[j].max);
656 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
657 c->limits[j].types))
658 goto nla_put_failure;
659 nla_nest_end(msg, nl_limit);
660 }
661
662 nla_nest_end(msg, nl_limits);
663
664 if (c->beacon_int_infra_match)
665 NLA_PUT_FLAG(msg,
666 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
667 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
668 c->num_different_channels);
669 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
670 c->max_interfaces);
671
672 nla_nest_end(msg, nl_combi);
673 }
674
675 nla_nest_end(msg, nl_combis);
676
677 return 0;
678nla_put_failure:
679 return -ENOBUFS;
680}
681
Johannes Berg55682962007-09-20 13:09:35 -0400682static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
683 struct cfg80211_registered_device *dev)
684{
685 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100686 struct nlattr *nl_bands, *nl_band;
687 struct nlattr *nl_freqs, *nl_freq;
688 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100689 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100690 enum ieee80211_band band;
691 struct ieee80211_channel *chan;
692 struct ieee80211_rate *rate;
693 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200694 const struct ieee80211_txrx_stypes *mgmt_stypes =
695 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400696
697 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
698 if (!hdr)
699 return -1;
700
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500701 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400702 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200703
Johannes Bergf5ea9122009-08-07 16:17:38 +0200704 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
705 cfg80211_rdev_list_generation);
706
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200707 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
708 dev->wiphy.retry_short);
709 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
710 dev->wiphy.retry_long);
711 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
712 dev->wiphy.frag_threshold);
713 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
714 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100715 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
716 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100717 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
718 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300719 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
720 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200721 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
722 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300723 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
724 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300725 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
726 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100727
Johannes Berge31b8212010-10-05 19:39:30 +0200728 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
729 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700730 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
731 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300732 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
733 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Johannes Berge31b8212010-10-05 19:39:30 +0200734
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530735 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
736 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
737
Johannes Berg25e47c182009-04-02 20:14:06 +0200738 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
739 sizeof(u32) * dev->wiphy.n_cipher_suites,
740 dev->wiphy.cipher_suites);
741
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100742 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
743 dev->wiphy.max_num_pmkids);
744
Johannes Bergc0692b82010-08-27 14:26:53 +0300745 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
746 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
747
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900748 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
749 dev->wiphy.available_antennas_tx);
750 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
751 dev->wiphy.available_antennas_rx);
752
Bruno Randolf7f531e02010-12-16 11:30:22 +0900753 if ((dev->wiphy.available_antennas_tx ||
754 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900755 u32 tx_ant = 0, rx_ant = 0;
756 int res;
757 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
758 if (!res) {
759 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
761 }
762 }
763
Johannes Berg7527a782011-05-13 10:58:57 +0200764 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
765 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700766 goto nla_put_failure;
767
Johannes Bergee688b002008-01-24 19:38:39 +0100768 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
769 if (!nl_bands)
770 goto nla_put_failure;
771
772 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
773 if (!dev->wiphy.bands[band])
774 continue;
775
776 nl_band = nla_nest_start(msg, band);
777 if (!nl_band)
778 goto nla_put_failure;
779
Johannes Bergd51626d2008-10-09 12:20:13 +0200780 /* add HT info */
781 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
782 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
783 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
784 &dev->wiphy.bands[band]->ht_cap.mcs);
785 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
786 dev->wiphy.bands[band]->ht_cap.cap);
787 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
788 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
789 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
790 dev->wiphy.bands[band]->ht_cap.ampdu_density);
791 }
792
Johannes Bergee688b002008-01-24 19:38:39 +0100793 /* add frequencies */
794 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
795 if (!nl_freqs)
796 goto nla_put_failure;
797
798 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
799 nl_freq = nla_nest_start(msg, i);
800 if (!nl_freq)
801 goto nla_put_failure;
802
803 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100804
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400805 if (nl80211_msg_put_channel(msg, chan))
806 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200807
Johannes Bergee688b002008-01-24 19:38:39 +0100808 nla_nest_end(msg, nl_freq);
809 }
810
811 nla_nest_end(msg, nl_freqs);
812
813 /* add bitrates */
814 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
815 if (!nl_rates)
816 goto nla_put_failure;
817
818 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
819 nl_rate = nla_nest_start(msg, i);
820 if (!nl_rate)
821 goto nla_put_failure;
822
823 rate = &dev->wiphy.bands[band]->bitrates[i];
824 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
825 rate->bitrate);
826 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
827 NLA_PUT_FLAG(msg,
828 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
829
830 nla_nest_end(msg, nl_rate);
831 }
832
833 nla_nest_end(msg, nl_rates);
834
835 nla_nest_end(msg, nl_band);
836 }
837 nla_nest_end(msg, nl_bands);
838
Johannes Berg8fdc6212009-03-14 09:34:01 +0100839 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
840 if (!nl_cmds)
841 goto nla_put_failure;
842
843 i = 0;
844#define CMD(op, n) \
845 do { \
846 if (dev->ops->op) { \
847 i++; \
848 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
849 } \
850 } while (0)
851
852 CMD(add_virtual_intf, NEW_INTERFACE);
853 CMD(change_virtual_intf, SET_INTERFACE);
854 CMD(add_key, NEW_KEY);
855 CMD(add_beacon, NEW_BEACON);
856 CMD(add_station, NEW_STATION);
857 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800858 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100859 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200860 CMD(auth, AUTHENTICATE);
861 CMD(assoc, ASSOCIATE);
862 CMD(deauth, DEAUTHENTICATE);
863 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200864 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100865 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100866 CMD(set_pmksa, SET_PMKSA);
867 CMD(del_pmksa, DEL_PMKSA);
868 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100869 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200870 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200871 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100872 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100873 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200874 i++;
875 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
876 }
Johannes Bergf444de02010-05-05 15:25:02 +0200877 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400878 CMD(set_wds_peer, SET_WDS_PEER);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300879 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
880 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100881
882#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200883
Johannes Berg6829c8782009-07-02 09:13:27 +0200884 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200885 i++;
886 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
887 }
888
Johannes Berg6829c8782009-07-02 09:13:27 +0200889 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200890 i++;
891 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
892 }
893
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894 nla_nest_end(msg, nl_cmds);
895
Johannes Berga2939112010-12-14 17:54:28 +0100896 if (dev->ops->remain_on_channel)
897 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
898 dev->wiphy.max_remain_on_channel_duration);
899
Jouni Malinend2da5872011-08-08 12:10:30 +0300900 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100901 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
902
Johannes Berg2e161f72010-08-12 15:38:38 +0200903 if (mgmt_stypes) {
904 u16 stypes;
905 struct nlattr *nl_ftypes, *nl_ifs;
906 enum nl80211_iftype ift;
907
908 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
909 if (!nl_ifs)
910 goto nla_put_failure;
911
912 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
913 nl_ftypes = nla_nest_start(msg, ift);
914 if (!nl_ftypes)
915 goto nla_put_failure;
916 i = 0;
917 stypes = mgmt_stypes[ift].tx;
918 while (stypes) {
919 if (stypes & 1)
920 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
921 (i << 4) | IEEE80211_FTYPE_MGMT);
922 stypes >>= 1;
923 i++;
924 }
925 nla_nest_end(msg, nl_ftypes);
926 }
927
Johannes Berg74b70a42010-08-24 12:15:53 +0200928 nla_nest_end(msg, nl_ifs);
929
Johannes Berg2e161f72010-08-12 15:38:38 +0200930 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
931 if (!nl_ifs)
932 goto nla_put_failure;
933
934 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
935 nl_ftypes = nla_nest_start(msg, ift);
936 if (!nl_ftypes)
937 goto nla_put_failure;
938 i = 0;
939 stypes = mgmt_stypes[ift].rx;
940 while (stypes) {
941 if (stypes & 1)
942 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
943 (i << 4) | IEEE80211_FTYPE_MGMT);
944 stypes >>= 1;
945 i++;
946 }
947 nla_nest_end(msg, nl_ftypes);
948 }
949 nla_nest_end(msg, nl_ifs);
950 }
951
Johannes Bergff1b6e62011-05-04 15:37:28 +0200952 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
953 struct nlattr *nl_wowlan;
954
955 nl_wowlan = nla_nest_start(msg,
956 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
957 if (!nl_wowlan)
958 goto nla_put_failure;
959
960 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
961 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
962 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
963 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
964 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
965 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200966 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
967 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
968 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
969 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
970 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
971 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
972 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
973 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
974 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
975 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200976 if (dev->wiphy.wowlan.n_patterns) {
977 struct nl80211_wowlan_pattern_support pat = {
978 .max_patterns = dev->wiphy.wowlan.n_patterns,
979 .min_pattern_len =
980 dev->wiphy.wowlan.pattern_min_len,
981 .max_pattern_len =
982 dev->wiphy.wowlan.pattern_max_len,
983 };
984 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
985 sizeof(pat), &pat);
986 }
987
988 nla_nest_end(msg, nl_wowlan);
989 }
990
Johannes Berg7527a782011-05-13 10:58:57 +0200991 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
992 dev->wiphy.software_iftypes))
993 goto nla_put_failure;
994
995 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
996 goto nla_put_failure;
997
Johannes Berg55682962007-09-20 13:09:35 -0400998 return genlmsg_end(msg, hdr);
999
1000 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001001 genlmsg_cancel(msg, hdr);
1002 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001003}
1004
1005static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1006{
1007 int idx = 0;
1008 int start = cb->args[0];
1009 struct cfg80211_registered_device *dev;
1010
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001011 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001012 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001013 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1014 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001015 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001016 continue;
1017 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1018 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001019 dev) < 0) {
1020 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001021 break;
Julius Volzb4637272008-07-08 14:02:19 +02001022 }
Johannes Berg55682962007-09-20 13:09:35 -04001023 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001024 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001025
1026 cb->args[0] = idx;
1027
1028 return skb->len;
1029}
1030
1031static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1032{
1033 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001034 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001035
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001036 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001037 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001038 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001039
Johannes Berg4c476992010-10-04 21:36:35 +02001040 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1041 nlmsg_free(msg);
1042 return -ENOBUFS;
1043 }
Johannes Berg55682962007-09-20 13:09:35 -04001044
Johannes Berg134e6372009-07-10 09:51:34 +00001045 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001046}
1047
Jouni Malinen31888482008-10-30 16:59:24 +02001048static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1049 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1050 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1051 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1052 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1053 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1054};
1055
1056static int parse_txq_params(struct nlattr *tb[],
1057 struct ieee80211_txq_params *txq_params)
1058{
1059 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1060 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1061 !tb[NL80211_TXQ_ATTR_AIFS])
1062 return -EINVAL;
1063
1064 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1065 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1066 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1067 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1068 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1069
1070 return 0;
1071}
1072
Johannes Bergf444de02010-05-05 15:25:02 +02001073static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1074{
1075 /*
1076 * You can only set the channel explicitly for AP, mesh
1077 * and WDS type interfaces; all others have their channel
1078 * managed via their respective "establish a connection"
1079 * command (connect, join, ...)
1080 *
1081 * Monitors are special as they are normally slaved to
1082 * whatever else is going on, so they behave as though
1083 * you tried setting the wiphy channel itself.
1084 */
1085 return !wdev ||
1086 wdev->iftype == NL80211_IFTYPE_AP ||
1087 wdev->iftype == NL80211_IFTYPE_WDS ||
1088 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001089 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1090 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001091}
1092
1093static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1094 struct wireless_dev *wdev,
1095 struct genl_info *info)
1096{
1097 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1098 u32 freq;
1099 int result;
1100
1101 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1102 return -EINVAL;
1103
1104 if (!nl80211_can_set_dev_channel(wdev))
1105 return -EOPNOTSUPP;
1106
1107 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1108 channel_type = nla_get_u32(info->attrs[
1109 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1110 if (channel_type != NL80211_CHAN_NO_HT &&
1111 channel_type != NL80211_CHAN_HT20 &&
1112 channel_type != NL80211_CHAN_HT40PLUS &&
1113 channel_type != NL80211_CHAN_HT40MINUS)
1114 return -EINVAL;
1115 }
1116
1117 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1118
1119 mutex_lock(&rdev->devlist_mtx);
1120 if (wdev) {
1121 wdev_lock(wdev);
1122 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1123 wdev_unlock(wdev);
1124 } else {
1125 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1126 }
1127 mutex_unlock(&rdev->devlist_mtx);
1128
1129 return result;
1130}
1131
1132static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1133{
Johannes Berg4c476992010-10-04 21:36:35 +02001134 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1135 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001136
Johannes Berg4c476992010-10-04 21:36:35 +02001137 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001138}
1139
Bill Jordane8347eb2010-10-01 13:54:28 -04001140static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1141{
Johannes Berg43b19952010-10-07 13:10:30 +02001142 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1143 struct net_device *dev = info->user_ptr[1];
1144 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001145 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001146
1147 if (!info->attrs[NL80211_ATTR_MAC])
1148 return -EINVAL;
1149
Johannes Berg43b19952010-10-07 13:10:30 +02001150 if (netif_running(dev))
1151 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001152
Johannes Berg43b19952010-10-07 13:10:30 +02001153 if (!rdev->ops->set_wds_peer)
1154 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001155
Johannes Berg43b19952010-10-07 13:10:30 +02001156 if (wdev->iftype != NL80211_IFTYPE_WDS)
1157 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001158
1159 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001160 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001161}
1162
1163
Johannes Berg55682962007-09-20 13:09:35 -04001164static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1165{
1166 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001167 struct net_device *netdev = NULL;
1168 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001169 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001170 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001171 u32 changed;
1172 u8 retry_short = 0, retry_long = 0;
1173 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001174 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001175
Johannes Bergf444de02010-05-05 15:25:02 +02001176 /*
1177 * Try to find the wiphy and netdev. Normally this
1178 * function shouldn't need the netdev, but this is
1179 * done for backward compatibility -- previously
1180 * setting the channel was done per wiphy, but now
1181 * it is per netdev. Previous userland like hostapd
1182 * also passed a netdev to set_wiphy, so that it is
1183 * possible to let that go to the right netdev!
1184 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001185 mutex_lock(&cfg80211_mutex);
1186
Johannes Bergf444de02010-05-05 15:25:02 +02001187 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1188 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1189
1190 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1191 if (netdev && netdev->ieee80211_ptr) {
1192 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1193 mutex_lock(&rdev->mtx);
1194 } else
1195 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001196 }
1197
Johannes Bergf444de02010-05-05 15:25:02 +02001198 if (!netdev) {
1199 rdev = __cfg80211_rdev_from_info(info);
1200 if (IS_ERR(rdev)) {
1201 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001202 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001203 }
1204 wdev = NULL;
1205 netdev = NULL;
1206 result = 0;
1207
1208 mutex_lock(&rdev->mtx);
1209 } else if (netif_running(netdev) &&
1210 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1211 wdev = netdev->ieee80211_ptr;
1212 else
1213 wdev = NULL;
1214
1215 /*
1216 * end workaround code, by now the rdev is available
1217 * and locked, and wdev may or may not be NULL.
1218 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001219
1220 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001221 result = cfg80211_dev_rename(
1222 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001223
1224 mutex_unlock(&cfg80211_mutex);
1225
1226 if (result)
1227 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001228
Jouni Malinen31888482008-10-30 16:59:24 +02001229 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1230 struct ieee80211_txq_params txq_params;
1231 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1232
1233 if (!rdev->ops->set_txq_params) {
1234 result = -EOPNOTSUPP;
1235 goto bad_res;
1236 }
1237
1238 nla_for_each_nested(nl_txq_params,
1239 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1240 rem_txq_params) {
1241 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1242 nla_data(nl_txq_params),
1243 nla_len(nl_txq_params),
1244 txq_params_policy);
1245 result = parse_txq_params(tb, &txq_params);
1246 if (result)
1247 goto bad_res;
1248
1249 result = rdev->ops->set_txq_params(&rdev->wiphy,
1250 &txq_params);
1251 if (result)
1252 goto bad_res;
1253 }
1254 }
1255
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001256 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001257 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001258 if (result)
1259 goto bad_res;
1260 }
1261
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001262 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1263 enum nl80211_tx_power_setting type;
1264 int idx, mbm = 0;
1265
1266 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001267 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001268 goto bad_res;
1269 }
1270
1271 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1272 type = nla_get_u32(info->attrs[idx]);
1273
1274 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1275 (type != NL80211_TX_POWER_AUTOMATIC)) {
1276 result = -EINVAL;
1277 goto bad_res;
1278 }
1279
1280 if (type != NL80211_TX_POWER_AUTOMATIC) {
1281 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1282 mbm = nla_get_u32(info->attrs[idx]);
1283 }
1284
1285 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1286 if (result)
1287 goto bad_res;
1288 }
1289
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001290 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1291 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1292 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001293 if ((!rdev->wiphy.available_antennas_tx &&
1294 !rdev->wiphy.available_antennas_rx) ||
1295 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001296 result = -EOPNOTSUPP;
1297 goto bad_res;
1298 }
1299
1300 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1301 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1302
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001303 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001304 * available antenna masks, except for the "all" mask */
1305 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1306 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001307 result = -EINVAL;
1308 goto bad_res;
1309 }
1310
Bruno Randolf7f531e02010-12-16 11:30:22 +09001311 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1312 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001313
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001314 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1315 if (result)
1316 goto bad_res;
1317 }
1318
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001319 changed = 0;
1320
1321 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1322 retry_short = nla_get_u8(
1323 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1324 if (retry_short == 0) {
1325 result = -EINVAL;
1326 goto bad_res;
1327 }
1328 changed |= WIPHY_PARAM_RETRY_SHORT;
1329 }
1330
1331 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1332 retry_long = nla_get_u8(
1333 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1334 if (retry_long == 0) {
1335 result = -EINVAL;
1336 goto bad_res;
1337 }
1338 changed |= WIPHY_PARAM_RETRY_LONG;
1339 }
1340
1341 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1342 frag_threshold = nla_get_u32(
1343 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1344 if (frag_threshold < 256) {
1345 result = -EINVAL;
1346 goto bad_res;
1347 }
1348 if (frag_threshold != (u32) -1) {
1349 /*
1350 * Fragments (apart from the last one) are required to
1351 * have even length. Make the fragmentation code
1352 * simpler by stripping LSB should someone try to use
1353 * odd threshold value.
1354 */
1355 frag_threshold &= ~0x1;
1356 }
1357 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1358 }
1359
1360 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1361 rts_threshold = nla_get_u32(
1362 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1363 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1364 }
1365
Lukáš Turek81077e82009-12-21 22:50:47 +01001366 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1367 coverage_class = nla_get_u8(
1368 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1369 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1370 }
1371
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001372 if (changed) {
1373 u8 old_retry_short, old_retry_long;
1374 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001375 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001376
1377 if (!rdev->ops->set_wiphy_params) {
1378 result = -EOPNOTSUPP;
1379 goto bad_res;
1380 }
1381
1382 old_retry_short = rdev->wiphy.retry_short;
1383 old_retry_long = rdev->wiphy.retry_long;
1384 old_frag_threshold = rdev->wiphy.frag_threshold;
1385 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001386 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001387
1388 if (changed & WIPHY_PARAM_RETRY_SHORT)
1389 rdev->wiphy.retry_short = retry_short;
1390 if (changed & WIPHY_PARAM_RETRY_LONG)
1391 rdev->wiphy.retry_long = retry_long;
1392 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1393 rdev->wiphy.frag_threshold = frag_threshold;
1394 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1395 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001396 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1397 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001398
1399 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1400 if (result) {
1401 rdev->wiphy.retry_short = old_retry_short;
1402 rdev->wiphy.retry_long = old_retry_long;
1403 rdev->wiphy.frag_threshold = old_frag_threshold;
1404 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001405 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001406 }
1407 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001408
Johannes Berg306d6112008-12-08 12:39:04 +01001409 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001410 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001411 if (netdev)
1412 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001413 return result;
1414}
1415
1416
1417static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001418 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001419 struct net_device *dev)
1420{
1421 void *hdr;
1422
1423 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1424 if (!hdr)
1425 return -1;
1426
1427 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001428 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001429 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001430 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001431
1432 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1433 rdev->devlist_generation ^
1434 (cfg80211_rdev_list_generation << 2));
1435
Johannes Berg55682962007-09-20 13:09:35 -04001436 return genlmsg_end(msg, hdr);
1437
1438 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001439 genlmsg_cancel(msg, hdr);
1440 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001441}
1442
1443static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1444{
1445 int wp_idx = 0;
1446 int if_idx = 0;
1447 int wp_start = cb->args[0];
1448 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001449 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001450 struct wireless_dev *wdev;
1451
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001452 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001453 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1454 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001455 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001456 if (wp_idx < wp_start) {
1457 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001458 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001459 }
Johannes Berg55682962007-09-20 13:09:35 -04001460 if_idx = 0;
1461
Johannes Bergf5ea9122009-08-07 16:17:38 +02001462 mutex_lock(&rdev->devlist_mtx);
1463 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001464 if (if_idx < if_start) {
1465 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001466 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001467 }
Johannes Berg55682962007-09-20 13:09:35 -04001468 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1469 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001470 rdev, wdev->netdev) < 0) {
1471 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001472 goto out;
1473 }
1474 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001475 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001476 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001477
1478 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001479 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001480 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001481 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001482
1483 cb->args[0] = wp_idx;
1484 cb->args[1] = if_idx;
1485
1486 return skb->len;
1487}
1488
1489static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1490{
1491 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001492 struct cfg80211_registered_device *dev = info->user_ptr[0];
1493 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001494
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001495 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001496 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001497 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001498
Johannes Bergd7264052009-04-19 16:23:20 +02001499 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001500 dev, netdev) < 0) {
1501 nlmsg_free(msg);
1502 return -ENOBUFS;
1503 }
Johannes Berg55682962007-09-20 13:09:35 -04001504
Johannes Berg134e6372009-07-10 09:51:34 +00001505 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001506}
1507
Michael Wu66f7ac52008-01-31 19:48:22 +01001508static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1509 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1510 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1511 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1512 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1513 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1514};
1515
1516static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1517{
1518 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1519 int flag;
1520
1521 *mntrflags = 0;
1522
1523 if (!nla)
1524 return -EINVAL;
1525
1526 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1527 nla, mntr_flags_policy))
1528 return -EINVAL;
1529
1530 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1531 if (flags[flag])
1532 *mntrflags |= (1<<flag);
1533
1534 return 0;
1535}
1536
Johannes Berg9bc383d2009-11-19 11:55:19 +01001537static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001538 struct net_device *netdev, u8 use_4addr,
1539 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001540{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001541 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001542 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001543 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001544 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001545 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001546
1547 switch (iftype) {
1548 case NL80211_IFTYPE_AP_VLAN:
1549 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1550 return 0;
1551 break;
1552 case NL80211_IFTYPE_STATION:
1553 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1554 return 0;
1555 break;
1556 default:
1557 break;
1558 }
1559
1560 return -EOPNOTSUPP;
1561}
1562
Johannes Berg55682962007-09-20 13:09:35 -04001563static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1564{
Johannes Berg4c476992010-10-04 21:36:35 +02001565 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001566 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001567 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001568 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001569 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001570 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001571 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001572
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001573 memset(&params, 0, sizeof(params));
1574
Johannes Berg04a773a2009-04-19 21:24:32 +02001575 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001576
Johannes Berg723b0382008-09-16 20:22:09 +02001577 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001578 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001579 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001580 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001581 if (ntype > NL80211_IFTYPE_MAX)
1582 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001583 }
1584
Johannes Berg92ffe052008-09-16 20:39:36 +02001585 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001586 struct wireless_dev *wdev = dev->ieee80211_ptr;
1587
Johannes Berg4c476992010-10-04 21:36:35 +02001588 if (ntype != NL80211_IFTYPE_MESH_POINT)
1589 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001590 if (netif_running(dev))
1591 return -EBUSY;
1592
1593 wdev_lock(wdev);
1594 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1595 IEEE80211_MAX_MESH_ID_LEN);
1596 wdev->mesh_id_up_len =
1597 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1598 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1599 wdev->mesh_id_up_len);
1600 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001601 }
1602
Felix Fietkau8b787642009-11-10 18:53:10 +01001603 if (info->attrs[NL80211_ATTR_4ADDR]) {
1604 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1605 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001606 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001607 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001608 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001609 } else {
1610 params.use_4addr = -1;
1611 }
1612
Johannes Berg92ffe052008-09-16 20:39:36 +02001613 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001614 if (ntype != NL80211_IFTYPE_MONITOR)
1615 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001616 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1617 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001618 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001619 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001620
1621 flags = &_flags;
1622 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001623 }
Johannes Berg3b858752009-03-12 09:55:09 +01001624
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001625 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001626 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001627 else
1628 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001629
Johannes Berg9bc383d2009-11-19 11:55:19 +01001630 if (!err && params.use_4addr != -1)
1631 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1632
Johannes Berg55682962007-09-20 13:09:35 -04001633 return err;
1634}
1635
1636static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1637{
Johannes Berg4c476992010-10-04 21:36:35 +02001638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001639 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001640 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001641 int err;
1642 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001643 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001644
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001645 memset(&params, 0, sizeof(params));
1646
Johannes Berg55682962007-09-20 13:09:35 -04001647 if (!info->attrs[NL80211_ATTR_IFNAME])
1648 return -EINVAL;
1649
1650 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1651 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1652 if (type > NL80211_IFTYPE_MAX)
1653 return -EINVAL;
1654 }
1655
Johannes Berg79c97e92009-07-07 03:56:12 +02001656 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001657 !(rdev->wiphy.interface_modes & (1 << type)))
1658 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001659
Johannes Berg9bc383d2009-11-19 11:55:19 +01001660 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001661 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001662 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001663 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001664 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001665 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001666
Michael Wu66f7ac52008-01-31 19:48:22 +01001667 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1668 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1669 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001670 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001671 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001672 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001673 if (IS_ERR(dev))
1674 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001675
Johannes Berg29cbe682010-12-03 09:20:44 +01001676 if (type == NL80211_IFTYPE_MESH_POINT &&
1677 info->attrs[NL80211_ATTR_MESH_ID]) {
1678 struct wireless_dev *wdev = dev->ieee80211_ptr;
1679
1680 wdev_lock(wdev);
1681 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1682 IEEE80211_MAX_MESH_ID_LEN);
1683 wdev->mesh_id_up_len =
1684 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1685 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1686 wdev->mesh_id_up_len);
1687 wdev_unlock(wdev);
1688 }
1689
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001690 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001691}
1692
1693static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1694{
Johannes Berg4c476992010-10-04 21:36:35 +02001695 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1696 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001697
Johannes Berg4c476992010-10-04 21:36:35 +02001698 if (!rdev->ops->del_virtual_intf)
1699 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001700
Johannes Berg4c476992010-10-04 21:36:35 +02001701 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001702}
1703
Johannes Berg41ade002007-12-19 02:03:29 +01001704struct get_key_cookie {
1705 struct sk_buff *msg;
1706 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001707 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001708};
1709
1710static void get_key_callback(void *c, struct key_params *params)
1711{
Johannes Bergb9454e82009-07-08 13:29:08 +02001712 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001713 struct get_key_cookie *cookie = c;
1714
1715 if (params->key)
1716 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1717 params->key_len, params->key);
1718
1719 if (params->seq)
1720 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1721 params->seq_len, params->seq);
1722
1723 if (params->cipher)
1724 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1725 params->cipher);
1726
Johannes Bergb9454e82009-07-08 13:29:08 +02001727 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1728 if (!key)
1729 goto nla_put_failure;
1730
1731 if (params->key)
1732 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1733 params->key_len, params->key);
1734
1735 if (params->seq)
1736 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1737 params->seq_len, params->seq);
1738
1739 if (params->cipher)
1740 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1741 params->cipher);
1742
1743 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1744
1745 nla_nest_end(cookie->msg, key);
1746
Johannes Berg41ade002007-12-19 02:03:29 +01001747 return;
1748 nla_put_failure:
1749 cookie->error = 1;
1750}
1751
1752static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1753{
Johannes Berg4c476992010-10-04 21:36:35 +02001754 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001755 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001756 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001757 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001758 const u8 *mac_addr = NULL;
1759 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001760 struct get_key_cookie cookie = {
1761 .error = 0,
1762 };
1763 void *hdr;
1764 struct sk_buff *msg;
1765
1766 if (info->attrs[NL80211_ATTR_KEY_IDX])
1767 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1768
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001769 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001770 return -EINVAL;
1771
1772 if (info->attrs[NL80211_ATTR_MAC])
1773 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1774
Johannes Berge31b8212010-10-05 19:39:30 +02001775 pairwise = !!mac_addr;
1776 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1777 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1778 if (kt >= NUM_NL80211_KEYTYPES)
1779 return -EINVAL;
1780 if (kt != NL80211_KEYTYPE_GROUP &&
1781 kt != NL80211_KEYTYPE_PAIRWISE)
1782 return -EINVAL;
1783 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1784 }
1785
Johannes Berg4c476992010-10-04 21:36:35 +02001786 if (!rdev->ops->get_key)
1787 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001788
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001789 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001790 if (!msg)
1791 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001792
1793 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1794 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001795 if (IS_ERR(hdr))
1796 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001797
1798 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001799 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001800
1801 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1802 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1803 if (mac_addr)
1804 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1805
Johannes Berge31b8212010-10-05 19:39:30 +02001806 if (pairwise && mac_addr &&
1807 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1808 return -ENOENT;
1809
1810 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1811 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001812
1813 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001814 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001815
1816 if (cookie.error)
1817 goto nla_put_failure;
1818
1819 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001820 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001821
1822 nla_put_failure:
1823 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001824 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001825 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001826 return err;
1827}
1828
1829static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1830{
Johannes Berg4c476992010-10-04 21:36:35 +02001831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001832 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001833 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001834 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001835
Johannes Bergb9454e82009-07-08 13:29:08 +02001836 err = nl80211_parse_key(info, &key);
1837 if (err)
1838 return err;
1839
1840 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001841 return -EINVAL;
1842
Johannes Bergb9454e82009-07-08 13:29:08 +02001843 /* only support setting default key */
1844 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001845 return -EINVAL;
1846
Johannes Bergfffd0932009-07-08 14:22:54 +02001847 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001848
1849 if (key.def) {
1850 if (!rdev->ops->set_default_key) {
1851 err = -EOPNOTSUPP;
1852 goto out;
1853 }
1854
1855 err = nl80211_key_allowed(dev->ieee80211_ptr);
1856 if (err)
1857 goto out;
1858
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001859 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1860 key.def_uni, key.def_multi);
1861
1862 if (err)
1863 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001864
Johannes Berg3d23e342009-09-29 23:27:28 +02001865#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001866 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001867#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001868 } else {
1869 if (key.def_uni || !key.def_multi) {
1870 err = -EINVAL;
1871 goto out;
1872 }
1873
1874 if (!rdev->ops->set_default_mgmt_key) {
1875 err = -EOPNOTSUPP;
1876 goto out;
1877 }
1878
1879 err = nl80211_key_allowed(dev->ieee80211_ptr);
1880 if (err)
1881 goto out;
1882
1883 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1884 dev, key.idx);
1885 if (err)
1886 goto out;
1887
1888#ifdef CONFIG_CFG80211_WEXT
1889 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1890#endif
1891 }
1892
1893 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001894 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001895
Johannes Berg41ade002007-12-19 02:03:29 +01001896 return err;
1897}
1898
1899static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1900{
Johannes Berg4c476992010-10-04 21:36:35 +02001901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001902 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001903 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001904 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001905 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001906
Johannes Bergb9454e82009-07-08 13:29:08 +02001907 err = nl80211_parse_key(info, &key);
1908 if (err)
1909 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001910
Johannes Bergb9454e82009-07-08 13:29:08 +02001911 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001912 return -EINVAL;
1913
Johannes Berg41ade002007-12-19 02:03:29 +01001914 if (info->attrs[NL80211_ATTR_MAC])
1915 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1916
Johannes Berge31b8212010-10-05 19:39:30 +02001917 if (key.type == -1) {
1918 if (mac_addr)
1919 key.type = NL80211_KEYTYPE_PAIRWISE;
1920 else
1921 key.type = NL80211_KEYTYPE_GROUP;
1922 }
1923
1924 /* for now */
1925 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1926 key.type != NL80211_KEYTYPE_GROUP)
1927 return -EINVAL;
1928
Johannes Berg4c476992010-10-04 21:36:35 +02001929 if (!rdev->ops->add_key)
1930 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001931
Johannes Berge31b8212010-10-05 19:39:30 +02001932 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1933 key.type == NL80211_KEYTYPE_PAIRWISE,
1934 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001935 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001936
1937 wdev_lock(dev->ieee80211_ptr);
1938 err = nl80211_key_allowed(dev->ieee80211_ptr);
1939 if (!err)
1940 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001941 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001942 mac_addr, &key.p);
1943 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001944
Johannes Berg41ade002007-12-19 02:03:29 +01001945 return err;
1946}
1947
1948static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1949{
Johannes Berg4c476992010-10-04 21:36:35 +02001950 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001951 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001952 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001953 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001954 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001955
Johannes Bergb9454e82009-07-08 13:29:08 +02001956 err = nl80211_parse_key(info, &key);
1957 if (err)
1958 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001959
1960 if (info->attrs[NL80211_ATTR_MAC])
1961 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1962
Johannes Berge31b8212010-10-05 19:39:30 +02001963 if (key.type == -1) {
1964 if (mac_addr)
1965 key.type = NL80211_KEYTYPE_PAIRWISE;
1966 else
1967 key.type = NL80211_KEYTYPE_GROUP;
1968 }
1969
1970 /* for now */
1971 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1972 key.type != NL80211_KEYTYPE_GROUP)
1973 return -EINVAL;
1974
Johannes Berg4c476992010-10-04 21:36:35 +02001975 if (!rdev->ops->del_key)
1976 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001977
Johannes Bergfffd0932009-07-08 14:22:54 +02001978 wdev_lock(dev->ieee80211_ptr);
1979 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001980
1981 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1982 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1983 err = -ENOENT;
1984
Johannes Bergfffd0932009-07-08 14:22:54 +02001985 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001986 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1987 key.type == NL80211_KEYTYPE_PAIRWISE,
1988 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001989
Johannes Berg3d23e342009-09-29 23:27:28 +02001990#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001991 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001992 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001993 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001994 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001995 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1996 }
1997#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001998 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001999
Johannes Berg41ade002007-12-19 02:03:29 +01002000 return err;
2001}
2002
Johannes Berged1b6cc2007-12-19 02:03:32 +01002003static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2004{
2005 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2006 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2008 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002009 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002010 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002011 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002012
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002013 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2014 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2015 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2016 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002017 return -EINVAL;
2018
Johannes Berg074ac8d2010-09-16 14:58:22 +02002019 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002020 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2021 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002022
Johannes Berg56d18932011-05-09 18:41:15 +02002023 memset(&params, 0, sizeof(params));
2024
Johannes Berged1b6cc2007-12-19 02:03:32 +01002025 switch (info->genlhdr->cmd) {
2026 case NL80211_CMD_NEW_BEACON:
2027 /* these are required for NEW_BEACON */
2028 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2029 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002030 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2031 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002032
Johannes Berg56d18932011-05-09 18:41:15 +02002033 params.interval =
2034 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2035 params.dtim_period =
2036 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2037
2038 err = cfg80211_validate_beacon_int(rdev, params.interval);
2039 if (err)
2040 return err;
2041
Jouni Malinen32e9de82011-08-10 23:53:31 +03002042 /*
2043 * In theory, some of these attributes could be required for
2044 * NEW_BEACON, but since they were not used when the command was
2045 * originally added, keep them optional for old user space
2046 * programs to work with drivers that do not need the additional
2047 * information.
2048 */
2049 if (info->attrs[NL80211_ATTR_SSID]) {
2050 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2051 params.ssid_len =
2052 nla_len(info->attrs[NL80211_ATTR_SSID]);
2053 if (params.ssid_len == 0 ||
2054 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2055 return -EINVAL;
2056 }
2057
2058 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2059 params.hidden_ssid = nla_get_u32(
2060 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2061 if (params.hidden_ssid !=
2062 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2063 params.hidden_ssid !=
2064 NL80211_HIDDEN_SSID_ZERO_LEN &&
2065 params.hidden_ssid !=
2066 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2067 return -EINVAL;
2068 }
2069
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002070 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2071
2072 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2073 params.auth_type = nla_get_u32(
2074 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2075 if (!nl80211_valid_auth_type(params.auth_type))
2076 return -EINVAL;
2077 } else
2078 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2079
2080 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2081 NL80211_MAX_NR_CIPHER_SUITES);
2082 if (err)
2083 return err;
2084
Johannes Berg79c97e92009-07-07 03:56:12 +02002085 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002086 break;
2087 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002088 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002089 break;
2090 default:
2091 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002092 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002093 }
2094
Johannes Berg4c476992010-10-04 21:36:35 +02002095 if (!call)
2096 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002097
Johannes Berged1b6cc2007-12-19 02:03:32 +01002098 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2099 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2100 params.head_len =
2101 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2102 haveinfo = 1;
2103 }
2104
2105 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2106 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2107 params.tail_len =
2108 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2109 haveinfo = 1;
2110 }
2111
Johannes Berg4c476992010-10-04 21:36:35 +02002112 if (!haveinfo)
2113 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002114
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002115 if (info->attrs[NL80211_ATTR_IE]) {
2116 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2117 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2118 }
2119
2120 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2121 params.proberesp_ies =
2122 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2123 params.proberesp_ies_len =
2124 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2125 }
2126
2127 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2128 params.assocresp_ies =
2129 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2130 params.assocresp_ies_len =
2131 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2132 }
2133
Johannes Berg56d18932011-05-09 18:41:15 +02002134 err = call(&rdev->wiphy, dev, &params);
2135 if (!err && params.interval)
2136 wdev->beacon_interval = params.interval;
2137 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002138}
2139
2140static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2141{
Johannes Berg4c476992010-10-04 21:36:35 +02002142 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2143 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002144 struct wireless_dev *wdev = dev->ieee80211_ptr;
2145 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002146
Johannes Berg4c476992010-10-04 21:36:35 +02002147 if (!rdev->ops->del_beacon)
2148 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002149
Johannes Berg074ac8d2010-09-16 14:58:22 +02002150 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002151 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2152 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002153
Johannes Berg56d18932011-05-09 18:41:15 +02002154 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2155 if (!err)
2156 wdev->beacon_interval = 0;
2157 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002158}
2159
Johannes Berg5727ef12007-12-19 02:03:34 +01002160static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2161 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2162 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2163 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002164 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002165 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002166};
2167
Johannes Bergeccb8e82009-05-11 21:57:56 +03002168static int parse_station_flags(struct genl_info *info,
2169 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002170{
2171 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002172 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002173 int flag;
2174
Johannes Bergeccb8e82009-05-11 21:57:56 +03002175 /*
2176 * Try parsing the new attribute first so userspace
2177 * can specify both for older kernels.
2178 */
2179 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2180 if (nla) {
2181 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002182
Johannes Bergeccb8e82009-05-11 21:57:56 +03002183 sta_flags = nla_data(nla);
2184 params->sta_flags_mask = sta_flags->mask;
2185 params->sta_flags_set = sta_flags->set;
2186 if ((params->sta_flags_mask |
2187 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2188 return -EINVAL;
2189 return 0;
2190 }
2191
2192 /* if present, parse the old attribute */
2193
2194 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002195 if (!nla)
2196 return 0;
2197
2198 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2199 nla, sta_flags_policy))
2200 return -EINVAL;
2201
Johannes Bergeccb8e82009-05-11 21:57:56 +03002202 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2203 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002204
2205 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2206 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002207 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002208
2209 return 0;
2210}
2211
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002212static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2213 int attr)
2214{
2215 struct nlattr *rate;
2216 u16 bitrate;
2217
2218 rate = nla_nest_start(msg, attr);
2219 if (!rate)
2220 goto nla_put_failure;
2221
2222 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2223 bitrate = cfg80211_calculate_bitrate(info);
2224 if (bitrate > 0)
2225 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2226
2227 if (info->flags & RATE_INFO_FLAGS_MCS)
2228 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2229 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2230 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2231 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2232 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2233
2234 nla_nest_end(msg, rate);
2235 return true;
2236
2237nla_put_failure:
2238 return false;
2239}
2240
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002241static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2242 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002243 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002244{
2245 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002246 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002247
2248 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2249 if (!hdr)
2250 return -1;
2251
2252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2253 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2254
Johannes Bergf5ea9122009-08-07 16:17:38 +02002255 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2256
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002257 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2258 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002259 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302260 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2261 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2262 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002263 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2264 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2265 sinfo->inactive_time);
2266 if (sinfo->filled & STATION_INFO_RX_BYTES)
2267 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2268 sinfo->rx_bytes);
2269 if (sinfo->filled & STATION_INFO_TX_BYTES)
2270 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2271 sinfo->tx_bytes);
2272 if (sinfo->filled & STATION_INFO_LLID)
2273 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2274 sinfo->llid);
2275 if (sinfo->filled & STATION_INFO_PLID)
2276 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2277 sinfo->plid);
2278 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2279 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2280 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002281 if (sinfo->filled & STATION_INFO_SIGNAL)
2282 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2283 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002284 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2285 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2286 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002287 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002288 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2289 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002290 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002291 }
2292 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2293 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2294 NL80211_STA_INFO_RX_BITRATE))
2295 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002296 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002297 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2298 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2299 sinfo->rx_packets);
2300 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2301 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2302 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002303 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2304 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2305 sinfo->tx_retries);
2306 if (sinfo->filled & STATION_INFO_TX_FAILED)
2307 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2308 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002309 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2310 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2311 if (!bss_param)
2312 goto nla_put_failure;
2313
2314 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2315 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2316 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2317 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2318 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2319 NLA_PUT_FLAG(msg,
2320 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2321 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2322 sinfo->bss_param.dtim_period);
2323 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2324 sinfo->bss_param.beacon_interval);
2325
2326 nla_nest_end(msg, bss_param);
2327 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002328 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002329
Felix Fietkau040bdf72011-08-10 19:00:33 -06002330 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002331 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2332 sinfo->assoc_req_ies);
2333
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002334 return genlmsg_end(msg, hdr);
2335
2336 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002337 genlmsg_cancel(msg, hdr);
2338 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002339}
2340
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002343{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002344 struct station_info sinfo;
2345 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002346 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002348 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002349 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002350
Johannes Berg67748892010-10-04 21:14:06 +02002351 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2352 if (err)
2353 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002354
2355 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002356 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002357 goto out_err;
2358 }
2359
Johannes Bergbba95fe2008-07-29 13:22:51 +02002360 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002361 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002362 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2363 mac_addr, &sinfo);
2364 if (err == -ENOENT)
2365 break;
2366 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002367 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002368
2369 if (nl80211_send_station(skb,
2370 NETLINK_CB(cb->skb).pid,
2371 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2372 netdev, mac_addr,
2373 &sinfo) < 0)
2374 goto out;
2375
2376 sta_idx++;
2377 }
2378
2379
2380 out:
2381 cb->args[1] = sta_idx;
2382 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002383 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002384 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002385
2386 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002388
Johannes Berg5727ef12007-12-19 02:03:34 +01002389static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2390{
Johannes Berg4c476992010-10-04 21:36:35 +02002391 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2392 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002393 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002394 struct sk_buff *msg;
2395 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002396 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002397
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002398 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002399
2400 if (!info->attrs[NL80211_ATTR_MAC])
2401 return -EINVAL;
2402
2403 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2404
Johannes Berg4c476992010-10-04 21:36:35 +02002405 if (!rdev->ops->get_station)
2406 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002407
Johannes Berg79c97e92009-07-07 03:56:12 +02002408 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002409 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002410 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002411
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002412 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002413 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002414 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002415
2416 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002417 dev, mac_addr, &sinfo) < 0) {
2418 nlmsg_free(msg);
2419 return -ENOBUFS;
2420 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002421
Johannes Berg4c476992010-10-04 21:36:35 +02002422 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002423}
2424
2425/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002426 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002427 */
Johannes Berg463d0182009-07-14 00:33:35 +02002428static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002429 struct cfg80211_registered_device *rdev,
2430 struct net_device **vlan)
2431{
Johannes Berg463d0182009-07-14 00:33:35 +02002432 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002433 *vlan = NULL;
2434
2435 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002436 *vlan = dev_get_by_index(genl_info_net(info),
2437 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002438 if (!*vlan)
2439 return -ENODEV;
2440 if (!(*vlan)->ieee80211_ptr)
2441 return -EINVAL;
2442 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2443 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002444 if (!netif_running(*vlan))
2445 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002446 }
2447 return 0;
2448}
2449
2450static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2451{
Johannes Berg4c476992010-10-04 21:36:35 +02002452 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002453 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002454 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002455 struct station_parameters params;
2456 u8 *mac_addr = NULL;
2457
2458 memset(&params, 0, sizeof(params));
2459
2460 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002461 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002462
2463 if (info->attrs[NL80211_ATTR_STA_AID])
2464 return -EINVAL;
2465
2466 if (!info->attrs[NL80211_ATTR_MAC])
2467 return -EINVAL;
2468
2469 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2470
2471 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2472 params.supported_rates =
2473 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2474 params.supported_rates_len =
2475 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2476 }
2477
2478 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2479 params.listen_interval =
2480 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2481
Jouni Malinen36aedc92008-08-25 11:58:58 +03002482 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2483 params.ht_capa =
2484 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2485
Johannes Bergeccb8e82009-05-11 21:57:56 +03002486 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002487 return -EINVAL;
2488
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002489 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2490 params.plink_action =
2491 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2492
Javier Cardona9c3990a2011-05-03 16:57:11 -07002493 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2494 params.plink_state =
2495 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2496
Johannes Berg463d0182009-07-14 00:33:35 +02002497 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002498 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002499 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002500
2501 /* validate settings */
2502 err = 0;
2503
2504 switch (dev->ieee80211_ptr->iftype) {
2505 case NL80211_IFTYPE_AP:
2506 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002507 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002508 /* disallow mesh-specific things */
2509 if (params.plink_action)
2510 err = -EINVAL;
2511 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002512 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002513 case NL80211_IFTYPE_STATION:
2514 /* disallow everything but AUTHORIZED flag */
2515 if (params.plink_action)
2516 err = -EINVAL;
2517 if (params.vlan)
2518 err = -EINVAL;
2519 if (params.supported_rates)
2520 err = -EINVAL;
2521 if (params.ht_capa)
2522 err = -EINVAL;
2523 if (params.listen_interval >= 0)
2524 err = -EINVAL;
2525 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2526 err = -EINVAL;
2527 break;
2528 case NL80211_IFTYPE_MESH_POINT:
2529 /* disallow things mesh doesn't support */
2530 if (params.vlan)
2531 err = -EINVAL;
2532 if (params.ht_capa)
2533 err = -EINVAL;
2534 if (params.listen_interval >= 0)
2535 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002536 if (params.sta_flags_mask &
2537 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002538 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002539 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002540 err = -EINVAL;
2541 break;
2542 default:
2543 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002544 }
2545
Johannes Berg5727ef12007-12-19 02:03:34 +01002546 if (err)
2547 goto out;
2548
Johannes Berg79c97e92009-07-07 03:56:12 +02002549 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002550 err = -EOPNOTSUPP;
2551 goto out;
2552 }
2553
Johannes Berg79c97e92009-07-07 03:56:12 +02002554 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002555
2556 out:
2557 if (params.vlan)
2558 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002559
Johannes Berg5727ef12007-12-19 02:03:34 +01002560 return err;
2561}
2562
Eliad Pellerc75786c2011-08-23 14:37:46 +03002563static struct nla_policy
2564nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2565 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2566 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2567};
2568
Johannes Berg5727ef12007-12-19 02:03:34 +01002569static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2570{
Johannes Berg4c476992010-10-04 21:36:35 +02002571 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002572 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002573 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002574 struct station_parameters params;
2575 u8 *mac_addr = NULL;
2576
2577 memset(&params, 0, sizeof(params));
2578
2579 if (!info->attrs[NL80211_ATTR_MAC])
2580 return -EINVAL;
2581
Johannes Berg5727ef12007-12-19 02:03:34 +01002582 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2583 return -EINVAL;
2584
2585 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2586 return -EINVAL;
2587
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002588 if (!info->attrs[NL80211_ATTR_STA_AID])
2589 return -EINVAL;
2590
Johannes Berg5727ef12007-12-19 02:03:34 +01002591 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2592 params.supported_rates =
2593 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2594 params.supported_rates_len =
2595 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2596 params.listen_interval =
2597 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002598
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002599 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2600 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2601 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002602
Jouni Malinen36aedc92008-08-25 11:58:58 +03002603 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2604 params.ht_capa =
2605 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002606
Javier Cardona96b78df2011-04-07 15:08:33 -07002607 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2608 params.plink_action =
2609 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2610
Johannes Bergeccb8e82009-05-11 21:57:56 +03002611 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002612 return -EINVAL;
2613
Eliad Pellerc75786c2011-08-23 14:37:46 +03002614 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002615 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2616 (params.sta_flags_set & NL80211_STA_FLAG_WME) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002617 info->attrs[NL80211_ATTR_STA_WME]) {
2618 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2619 struct nlattr *nla;
2620
2621 nla = info->attrs[NL80211_ATTR_STA_WME];
2622 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2623 nl80211_sta_wme_policy);
2624 if (err)
2625 return err;
2626
2627 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2628 params.uapsd_queues =
2629 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002630 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2631 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002632
2633 if (tb[NL80211_STA_WME_MAX_SP])
2634 params.max_sp =
2635 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002636
2637 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2638 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002639 }
2640
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002641 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002642 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002643 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002644 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2645 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002646
Johannes Berg463d0182009-07-14 00:33:35 +02002647 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002648 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002649 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002650
2651 /* validate settings */
2652 err = 0;
2653
Johannes Berg79c97e92009-07-07 03:56:12 +02002654 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002655 err = -EOPNOTSUPP;
2656 goto out;
2657 }
2658
Johannes Berg79c97e92009-07-07 03:56:12 +02002659 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002660
2661 out:
2662 if (params.vlan)
2663 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002664 return err;
2665}
2666
2667static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2668{
Johannes Berg4c476992010-10-04 21:36:35 +02002669 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2670 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002671 u8 *mac_addr = NULL;
2672
2673 if (info->attrs[NL80211_ATTR_MAC])
2674 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2675
Johannes Berge80cf852009-05-11 14:43:13 +02002676 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002677 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002678 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002679 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2680 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002681
Johannes Berg4c476992010-10-04 21:36:35 +02002682 if (!rdev->ops->del_station)
2683 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002684
Johannes Berg4c476992010-10-04 21:36:35 +02002685 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002686}
2687
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002688static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2689 int flags, struct net_device *dev,
2690 u8 *dst, u8 *next_hop,
2691 struct mpath_info *pinfo)
2692{
2693 void *hdr;
2694 struct nlattr *pinfoattr;
2695
2696 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2697 if (!hdr)
2698 return -1;
2699
2700 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2701 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2702 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2703
Johannes Bergf5ea9122009-08-07 16:17:38 +02002704 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2705
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002706 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2707 if (!pinfoattr)
2708 goto nla_put_failure;
2709 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2710 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2711 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002712 if (pinfo->filled & MPATH_INFO_SN)
2713 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2714 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002715 if (pinfo->filled & MPATH_INFO_METRIC)
2716 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2717 pinfo->metric);
2718 if (pinfo->filled & MPATH_INFO_EXPTIME)
2719 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2720 pinfo->exptime);
2721 if (pinfo->filled & MPATH_INFO_FLAGS)
2722 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2723 pinfo->flags);
2724 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2725 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2726 pinfo->discovery_timeout);
2727 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2728 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2729 pinfo->discovery_retries);
2730
2731 nla_nest_end(msg, pinfoattr);
2732
2733 return genlmsg_end(msg, hdr);
2734
2735 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002736 genlmsg_cancel(msg, hdr);
2737 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002738}
2739
2740static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002741 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002742{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002743 struct mpath_info pinfo;
2744 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002745 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002746 u8 dst[ETH_ALEN];
2747 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002748 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002749 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002750
Johannes Berg67748892010-10-04 21:14:06 +02002751 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2752 if (err)
2753 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002754
2755 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002756 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002757 goto out_err;
2758 }
2759
Jouni Malineneec60b02009-03-20 21:21:19 +02002760 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2761 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002762 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002763 }
2764
Johannes Bergbba95fe2008-07-29 13:22:51 +02002765 while (1) {
2766 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2767 dst, next_hop, &pinfo);
2768 if (err == -ENOENT)
2769 break;
2770 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002771 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002772
2773 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2774 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2775 netdev, dst, next_hop,
2776 &pinfo) < 0)
2777 goto out;
2778
2779 path_idx++;
2780 }
2781
2782
2783 out:
2784 cb->args[1] = path_idx;
2785 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002786 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002787 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002788 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002789}
2790
2791static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2792{
Johannes Berg4c476992010-10-04 21:36:35 +02002793 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002794 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002795 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002796 struct mpath_info pinfo;
2797 struct sk_buff *msg;
2798 u8 *dst = NULL;
2799 u8 next_hop[ETH_ALEN];
2800
2801 memset(&pinfo, 0, sizeof(pinfo));
2802
2803 if (!info->attrs[NL80211_ATTR_MAC])
2804 return -EINVAL;
2805
2806 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2807
Johannes Berg4c476992010-10-04 21:36:35 +02002808 if (!rdev->ops->get_mpath)
2809 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002810
Johannes Berg4c476992010-10-04 21:36:35 +02002811 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2812 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002813
Johannes Berg79c97e92009-07-07 03:56:12 +02002814 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002815 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002816 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002817
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002818 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002819 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002820 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002821
2822 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002823 dev, dst, next_hop, &pinfo) < 0) {
2824 nlmsg_free(msg);
2825 return -ENOBUFS;
2826 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002827
Johannes Berg4c476992010-10-04 21:36:35 +02002828 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002829}
2830
2831static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2832{
Johannes Berg4c476992010-10-04 21:36:35 +02002833 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2834 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002835 u8 *dst = NULL;
2836 u8 *next_hop = NULL;
2837
2838 if (!info->attrs[NL80211_ATTR_MAC])
2839 return -EINVAL;
2840
2841 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2842 return -EINVAL;
2843
2844 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2845 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2846
Johannes Berg4c476992010-10-04 21:36:35 +02002847 if (!rdev->ops->change_mpath)
2848 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002849
Johannes Berg4c476992010-10-04 21:36:35 +02002850 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2851 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002852
Johannes Berg4c476992010-10-04 21:36:35 +02002853 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002854}
Johannes Berg4c476992010-10-04 21:36:35 +02002855
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002856static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2857{
Johannes Berg4c476992010-10-04 21:36:35 +02002858 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2859 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002860 u8 *dst = NULL;
2861 u8 *next_hop = NULL;
2862
2863 if (!info->attrs[NL80211_ATTR_MAC])
2864 return -EINVAL;
2865
2866 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2867 return -EINVAL;
2868
2869 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2870 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2871
Johannes Berg4c476992010-10-04 21:36:35 +02002872 if (!rdev->ops->add_mpath)
2873 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002874
Johannes Berg4c476992010-10-04 21:36:35 +02002875 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2876 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002877
Johannes Berg4c476992010-10-04 21:36:35 +02002878 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002879}
2880
2881static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2882{
Johannes Berg4c476992010-10-04 21:36:35 +02002883 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2884 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002885 u8 *dst = NULL;
2886
2887 if (info->attrs[NL80211_ATTR_MAC])
2888 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2889
Johannes Berg4c476992010-10-04 21:36:35 +02002890 if (!rdev->ops->del_mpath)
2891 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002892
Johannes Berg4c476992010-10-04 21:36:35 +02002893 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002894}
2895
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002896static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2897{
Johannes Berg4c476992010-10-04 21:36:35 +02002898 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2899 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002900 struct bss_parameters params;
2901
2902 memset(&params, 0, sizeof(params));
2903 /* default to not changing parameters */
2904 params.use_cts_prot = -1;
2905 params.use_short_preamble = -1;
2906 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002907 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002908 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002909
2910 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2911 params.use_cts_prot =
2912 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2913 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2914 params.use_short_preamble =
2915 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2916 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2917 params.use_short_slot_time =
2918 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002919 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2920 params.basic_rates =
2921 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2922 params.basic_rates_len =
2923 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2924 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002925 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2926 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002927 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2928 params.ht_opmode =
2929 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002930
Johannes Berg4c476992010-10-04 21:36:35 +02002931 if (!rdev->ops->change_bss)
2932 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002933
Johannes Berg074ac8d2010-09-16 14:58:22 +02002934 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002935 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2936 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002937
Johannes Berg4c476992010-10-04 21:36:35 +02002938 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002939}
2940
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002941static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002942 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2943 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2944 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2945 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2946 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2947 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2948};
2949
2950static int parse_reg_rule(struct nlattr *tb[],
2951 struct ieee80211_reg_rule *reg_rule)
2952{
2953 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2954 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2955
2956 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2957 return -EINVAL;
2958 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2959 return -EINVAL;
2960 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2961 return -EINVAL;
2962 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2963 return -EINVAL;
2964 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2965 return -EINVAL;
2966
2967 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2968
2969 freq_range->start_freq_khz =
2970 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2971 freq_range->end_freq_khz =
2972 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2973 freq_range->max_bandwidth_khz =
2974 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2975
2976 power_rule->max_eirp =
2977 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2978
2979 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2980 power_rule->max_antenna_gain =
2981 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2982
2983 return 0;
2984}
2985
2986static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2987{
2988 int r;
2989 char *data = NULL;
2990
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002991 /*
2992 * You should only get this when cfg80211 hasn't yet initialized
2993 * completely when built-in to the kernel right between the time
2994 * window between nl80211_init() and regulatory_init(), if that is
2995 * even possible.
2996 */
2997 mutex_lock(&cfg80211_mutex);
2998 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002999 mutex_unlock(&cfg80211_mutex);
3000 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003001 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003002 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003003
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003004 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3005 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003006
3007 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3008
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003009 r = regulatory_hint_user(data);
3010
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003011 return r;
3012}
3013
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003014static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003015 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003016{
Johannes Berg4c476992010-10-04 21:36:35 +02003017 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003018 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003019 struct wireless_dev *wdev = dev->ieee80211_ptr;
3020 struct mesh_config cur_params;
3021 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003022 void *hdr;
3023 struct nlattr *pinfoattr;
3024 struct sk_buff *msg;
3025
Johannes Berg29cbe682010-12-03 09:20:44 +01003026 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3027 return -EOPNOTSUPP;
3028
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003029 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003030 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003031
Johannes Berg29cbe682010-12-03 09:20:44 +01003032 wdev_lock(wdev);
3033 /* If not connected, get default parameters */
3034 if (!wdev->mesh_id_len)
3035 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3036 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003037 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003038 &cur_params);
3039 wdev_unlock(wdev);
3040
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003041 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003042 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003043
3044 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003045 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003046 if (!msg)
3047 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003048 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003049 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003050 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003051 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003052 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003053 if (!pinfoattr)
3054 goto nla_put_failure;
3055 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3056 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3057 cur_params.dot11MeshRetryTimeout);
3058 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3059 cur_params.dot11MeshConfirmTimeout);
3060 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3061 cur_params.dot11MeshHoldingTimeout);
3062 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3063 cur_params.dot11MeshMaxPeerLinks);
3064 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3065 cur_params.dot11MeshMaxRetries);
3066 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3067 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003068 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3069 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003070 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3071 cur_params.auto_open_plinks);
3072 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3073 cur_params.dot11MeshHWMPmaxPREQretries);
3074 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3075 cur_params.path_refresh_time);
3076 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3077 cur_params.min_discovery_timeout);
3078 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3079 cur_params.dot11MeshHWMPactivePathTimeout);
3080 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3081 cur_params.dot11MeshHWMPpreqMinInterval);
3082 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3083 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003084 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3085 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003086 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3087 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003088 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3089 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003090 nla_nest_end(msg, pinfoattr);
3091 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003092 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003093
Johannes Berg3b858752009-03-12 09:55:09 +01003094 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003095 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003096 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003097 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003098 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003099}
3100
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003101static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003102 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3103 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3104 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3105 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3106 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3107 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003108 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003109 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3110
3111 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3112 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3113 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3114 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3115 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3116 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003117 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003118 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003119 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003120};
3121
Javier Cardonac80d5452010-12-16 17:37:49 -08003122static const struct nla_policy
3123 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3124 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3125 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003126 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003127 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003128 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003129 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003130};
3131
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003132static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003133 struct mesh_config *cfg,
3134 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003135{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003136 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003137 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003138
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003139#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3140do {\
3141 if (table[attr_num]) {\
3142 cfg->param = nla_fn(table[attr_num]); \
3143 mask |= (1 << (attr_num - 1)); \
3144 } \
3145} while (0);\
3146
3147
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003148 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003149 return -EINVAL;
3150 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003151 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003152 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003153 return -EINVAL;
3154
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003155 /* This makes sure that there aren't more than 32 mesh config
3156 * parameters (otherwise our bitfield scheme would not work.) */
3157 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3158
3159 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003160 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3161 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3162 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3163 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3164 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3165 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3166 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3167 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3168 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3169 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3170 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3171 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003172 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3173 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003174 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3175 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3176 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3177 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3178 nla_get_u8);
3179 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3180 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3181 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3182 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3183 nla_get_u16);
3184 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3185 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3186 nla_get_u32);
3187 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3188 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3189 nla_get_u16);
3190 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3191 dot11MeshHWMPnetDiameterTraversalTime,
3192 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3193 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003194 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3195 dot11MeshHWMPRootMode, mask,
3196 NL80211_MESHCONF_HWMP_ROOTMODE,
3197 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003198 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3199 dot11MeshHWMPRannInterval, mask,
3200 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3201 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003202 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3203 dot11MeshGateAnnouncementProtocol, mask,
3204 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3205 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003206 if (mask_out)
3207 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003208
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003209 return 0;
3210
3211#undef FILL_IN_MESH_PARAM_IF_SET
3212}
3213
Javier Cardonac80d5452010-12-16 17:37:49 -08003214static int nl80211_parse_mesh_setup(struct genl_info *info,
3215 struct mesh_setup *setup)
3216{
3217 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3218
3219 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3220 return -EINVAL;
3221 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3222 info->attrs[NL80211_ATTR_MESH_SETUP],
3223 nl80211_mesh_setup_params_policy))
3224 return -EINVAL;
3225
3226 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3227 setup->path_sel_proto =
3228 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3229 IEEE80211_PATH_PROTOCOL_VENDOR :
3230 IEEE80211_PATH_PROTOCOL_HWMP;
3231
3232 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3233 setup->path_metric =
3234 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3235 IEEE80211_PATH_METRIC_VENDOR :
3236 IEEE80211_PATH_METRIC_AIRTIME;
3237
Javier Cardona581a8b02011-04-07 15:08:27 -07003238
3239 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003240 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003241 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003242 if (!is_valid_ie_attr(ieattr))
3243 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003244 setup->ie = nla_data(ieattr);
3245 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003246 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003247 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3248 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003249
3250 return 0;
3251}
3252
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003253static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003254 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003255{
3256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3257 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003258 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003259 struct mesh_config cfg;
3260 u32 mask;
3261 int err;
3262
Johannes Berg29cbe682010-12-03 09:20:44 +01003263 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3264 return -EOPNOTSUPP;
3265
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003266 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003267 return -EOPNOTSUPP;
3268
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003269 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003270 if (err)
3271 return err;
3272
Johannes Berg29cbe682010-12-03 09:20:44 +01003273 wdev_lock(wdev);
3274 if (!wdev->mesh_id_len)
3275 err = -ENOLINK;
3276
3277 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003278 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003279 mask, &cfg);
3280
3281 wdev_unlock(wdev);
3282
3283 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003284}
3285
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003286static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3287{
3288 struct sk_buff *msg;
3289 void *hdr = NULL;
3290 struct nlattr *nl_reg_rules;
3291 unsigned int i;
3292 int err = -EINVAL;
3293
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003294 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003295
3296 if (!cfg80211_regdomain)
3297 goto out;
3298
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003300 if (!msg) {
3301 err = -ENOBUFS;
3302 goto out;
3303 }
3304
3305 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3306 NL80211_CMD_GET_REG);
3307 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003308 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003309
3310 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3311 cfg80211_regdomain->alpha2);
3312
3313 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3314 if (!nl_reg_rules)
3315 goto nla_put_failure;
3316
3317 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3318 struct nlattr *nl_reg_rule;
3319 const struct ieee80211_reg_rule *reg_rule;
3320 const struct ieee80211_freq_range *freq_range;
3321 const struct ieee80211_power_rule *power_rule;
3322
3323 reg_rule = &cfg80211_regdomain->reg_rules[i];
3324 freq_range = &reg_rule->freq_range;
3325 power_rule = &reg_rule->power_rule;
3326
3327 nl_reg_rule = nla_nest_start(msg, i);
3328 if (!nl_reg_rule)
3329 goto nla_put_failure;
3330
3331 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3332 reg_rule->flags);
3333 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3334 freq_range->start_freq_khz);
3335 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3336 freq_range->end_freq_khz);
3337 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3338 freq_range->max_bandwidth_khz);
3339 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3340 power_rule->max_antenna_gain);
3341 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3342 power_rule->max_eirp);
3343
3344 nla_nest_end(msg, nl_reg_rule);
3345 }
3346
3347 nla_nest_end(msg, nl_reg_rules);
3348
3349 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003350 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003351 goto out;
3352
3353nla_put_failure:
3354 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003355put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003356 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003357 err = -EMSGSIZE;
3358out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003359 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003360 return err;
3361}
3362
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003363static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3364{
3365 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3366 struct nlattr *nl_reg_rule;
3367 char *alpha2 = NULL;
3368 int rem_reg_rules = 0, r = 0;
3369 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3370 struct ieee80211_regdomain *rd = NULL;
3371
3372 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3373 return -EINVAL;
3374
3375 if (!info->attrs[NL80211_ATTR_REG_RULES])
3376 return -EINVAL;
3377
3378 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3379
3380 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3381 rem_reg_rules) {
3382 num_rules++;
3383 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003384 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003385 }
3386
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003387 mutex_lock(&cfg80211_mutex);
3388
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003389 if (!reg_is_valid_request(alpha2)) {
3390 r = -EINVAL;
3391 goto bad_reg;
3392 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003393
3394 size_of_regd = sizeof(struct ieee80211_regdomain) +
3395 (num_rules * sizeof(struct ieee80211_reg_rule));
3396
3397 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003398 if (!rd) {
3399 r = -ENOMEM;
3400 goto bad_reg;
3401 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003402
3403 rd->n_reg_rules = num_rules;
3404 rd->alpha2[0] = alpha2[0];
3405 rd->alpha2[1] = alpha2[1];
3406
3407 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3408 rem_reg_rules) {
3409 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3410 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3411 reg_rule_policy);
3412 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3413 if (r)
3414 goto bad_reg;
3415
3416 rule_idx++;
3417
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003418 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3419 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003420 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003421 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003422 }
3423
3424 BUG_ON(rule_idx != num_rules);
3425
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003426 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003427
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003428 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003429
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003430 return r;
3431
Johannes Bergd2372b32008-10-24 20:32:20 +02003432 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003433 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003434 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003435 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003436}
3437
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003438static int validate_scan_freqs(struct nlattr *freqs)
3439{
3440 struct nlattr *attr1, *attr2;
3441 int n_channels = 0, tmp1, tmp2;
3442
3443 nla_for_each_nested(attr1, freqs, tmp1) {
3444 n_channels++;
3445 /*
3446 * Some hardware has a limited channel list for
3447 * scanning, and it is pretty much nonsensical
3448 * to scan for a channel twice, so disallow that
3449 * and don't require drivers to check that the
3450 * channel list they get isn't longer than what
3451 * they can scan, as long as they can scan all
3452 * the channels they registered at once.
3453 */
3454 nla_for_each_nested(attr2, freqs, tmp2)
3455 if (attr1 != attr2 &&
3456 nla_get_u32(attr1) == nla_get_u32(attr2))
3457 return 0;
3458 }
3459
3460 return n_channels;
3461}
3462
Johannes Berg2a519312009-02-10 21:25:55 +01003463static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3464{
Johannes Berg4c476992010-10-04 21:36:35 +02003465 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3466 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003467 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003468 struct nlattr *attr;
3469 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003470 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003471 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003472
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003473 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3474 return -EINVAL;
3475
Johannes Berg79c97e92009-07-07 03:56:12 +02003476 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003477
Johannes Berg4c476992010-10-04 21:36:35 +02003478 if (!rdev->ops->scan)
3479 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003480
Johannes Berg4c476992010-10-04 21:36:35 +02003481 if (rdev->scan_req)
3482 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003483
3484 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003485 n_channels = validate_scan_freqs(
3486 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003487 if (!n_channels)
3488 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003489 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003490 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003491 n_channels = 0;
3492
Johannes Berg2a519312009-02-10 21:25:55 +01003493 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3494 if (wiphy->bands[band])
3495 n_channels += wiphy->bands[band]->n_channels;
3496 }
3497
3498 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3499 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3500 n_ssids++;
3501
Johannes Berg4c476992010-10-04 21:36:35 +02003502 if (n_ssids > wiphy->max_scan_ssids)
3503 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003504
Jouni Malinen70692ad2009-02-16 19:39:13 +02003505 if (info->attrs[NL80211_ATTR_IE])
3506 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3507 else
3508 ie_len = 0;
3509
Johannes Berg4c476992010-10-04 21:36:35 +02003510 if (ie_len > wiphy->max_scan_ie_len)
3511 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003512
Johannes Berg2a519312009-02-10 21:25:55 +01003513 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003514 + sizeof(*request->ssids) * n_ssids
3515 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003516 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003517 if (!request)
3518 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003519
Johannes Berg2a519312009-02-10 21:25:55 +01003520 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003521 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003522 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003523 if (ie_len) {
3524 if (request->ssids)
3525 request->ie = (void *)(request->ssids + n_ssids);
3526 else
3527 request->ie = (void *)(request->channels + n_channels);
3528 }
Johannes Berg2a519312009-02-10 21:25:55 +01003529
Johannes Berg584991d2009-11-02 13:32:03 +01003530 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003531 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3532 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003533 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003534 struct ieee80211_channel *chan;
3535
3536 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3537
3538 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003539 err = -EINVAL;
3540 goto out_free;
3541 }
Johannes Berg584991d2009-11-02 13:32:03 +01003542
3543 /* ignore disabled channels */
3544 if (chan->flags & IEEE80211_CHAN_DISABLED)
3545 continue;
3546
3547 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003548 i++;
3549 }
3550 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003551 enum ieee80211_band band;
3552
Johannes Berg2a519312009-02-10 21:25:55 +01003553 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003554 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3555 int j;
3556 if (!wiphy->bands[band])
3557 continue;
3558 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003559 struct ieee80211_channel *chan;
3560
3561 chan = &wiphy->bands[band]->channels[j];
3562
3563 if (chan->flags & IEEE80211_CHAN_DISABLED)
3564 continue;
3565
3566 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003567 i++;
3568 }
3569 }
3570 }
3571
Johannes Berg584991d2009-11-02 13:32:03 +01003572 if (!i) {
3573 err = -EINVAL;
3574 goto out_free;
3575 }
3576
3577 request->n_channels = i;
3578
Johannes Berg2a519312009-02-10 21:25:55 +01003579 i = 0;
3580 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3581 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003582 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003583 err = -EINVAL;
3584 goto out_free;
3585 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003586 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003587 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003588 i++;
3589 }
3590 }
3591
Jouni Malinen70692ad2009-02-16 19:39:13 +02003592 if (info->attrs[NL80211_ATTR_IE]) {
3593 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003594 memcpy((void *)request->ie,
3595 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003596 request->ie_len);
3597 }
3598
Johannes Berg34850ab2011-07-18 18:08:35 +02003599 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003600 if (wiphy->bands[i])
3601 request->rates[i] =
3602 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003603
3604 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3605 nla_for_each_nested(attr,
3606 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3607 tmp) {
3608 enum ieee80211_band band = nla_type(attr);
3609
Dan Carpenter84404622011-07-29 11:52:18 +03003610 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003611 err = -EINVAL;
3612 goto out_free;
3613 }
3614 err = ieee80211_get_ratemask(wiphy->bands[band],
3615 nla_data(attr),
3616 nla_len(attr),
3617 &request->rates[band]);
3618 if (err)
3619 goto out_free;
3620 }
3621 }
3622
Johannes Berg463d0182009-07-14 00:33:35 +02003623 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003624 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003625
Johannes Berg79c97e92009-07-07 03:56:12 +02003626 rdev->scan_req = request;
3627 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003628
Johannes Berg463d0182009-07-14 00:33:35 +02003629 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003630 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003631 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003632 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003633 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003634 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003635 kfree(request);
3636 }
Johannes Berg3b858752009-03-12 09:55:09 +01003637
Johannes Berg2a519312009-02-10 21:25:55 +01003638 return err;
3639}
3640
Luciano Coelho807f8a82011-05-11 17:09:35 +03003641static int nl80211_start_sched_scan(struct sk_buff *skb,
3642 struct genl_info *info)
3643{
3644 struct cfg80211_sched_scan_request *request;
3645 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3646 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003647 struct nlattr *attr;
3648 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003649 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003650 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003651 enum ieee80211_band band;
3652 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003653 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003654
3655 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3656 !rdev->ops->sched_scan_start)
3657 return -EOPNOTSUPP;
3658
3659 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3660 return -EINVAL;
3661
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003662 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3663 return -EINVAL;
3664
3665 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3666 if (interval == 0)
3667 return -EINVAL;
3668
Luciano Coelho807f8a82011-05-11 17:09:35 +03003669 wiphy = &rdev->wiphy;
3670
3671 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3672 n_channels = validate_scan_freqs(
3673 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3674 if (!n_channels)
3675 return -EINVAL;
3676 } else {
3677 n_channels = 0;
3678
3679 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3680 if (wiphy->bands[band])
3681 n_channels += wiphy->bands[band]->n_channels;
3682 }
3683
3684 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3685 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3686 tmp)
3687 n_ssids++;
3688
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003689 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003690 return -EINVAL;
3691
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003692 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3693 nla_for_each_nested(attr,
3694 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3695 tmp)
3696 n_match_sets++;
3697
3698 if (n_match_sets > wiphy->max_match_sets)
3699 return -EINVAL;
3700
Luciano Coelho807f8a82011-05-11 17:09:35 +03003701 if (info->attrs[NL80211_ATTR_IE])
3702 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3703 else
3704 ie_len = 0;
3705
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003706 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003707 return -EINVAL;
3708
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003709 mutex_lock(&rdev->sched_scan_mtx);
3710
3711 if (rdev->sched_scan_req) {
3712 err = -EINPROGRESS;
3713 goto out;
3714 }
3715
Luciano Coelho807f8a82011-05-11 17:09:35 +03003716 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003717 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003718 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003719 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003720 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003721 if (!request) {
3722 err = -ENOMEM;
3723 goto out;
3724 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003725
3726 if (n_ssids)
3727 request->ssids = (void *)&request->channels[n_channels];
3728 request->n_ssids = n_ssids;
3729 if (ie_len) {
3730 if (request->ssids)
3731 request->ie = (void *)(request->ssids + n_ssids);
3732 else
3733 request->ie = (void *)(request->channels + n_channels);
3734 }
3735
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003736 if (n_match_sets) {
3737 if (request->ie)
3738 request->match_sets = (void *)(request->ie + ie_len);
3739 else if (request->ssids)
3740 request->match_sets =
3741 (void *)(request->ssids + n_ssids);
3742 else
3743 request->match_sets =
3744 (void *)(request->channels + n_channels);
3745 }
3746 request->n_match_sets = n_match_sets;
3747
Luciano Coelho807f8a82011-05-11 17:09:35 +03003748 i = 0;
3749 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3750 /* user specified, bail out if channel not found */
3751 nla_for_each_nested(attr,
3752 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3753 tmp) {
3754 struct ieee80211_channel *chan;
3755
3756 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3757
3758 if (!chan) {
3759 err = -EINVAL;
3760 goto out_free;
3761 }
3762
3763 /* ignore disabled channels */
3764 if (chan->flags & IEEE80211_CHAN_DISABLED)
3765 continue;
3766
3767 request->channels[i] = chan;
3768 i++;
3769 }
3770 } else {
3771 /* all channels */
3772 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3773 int j;
3774 if (!wiphy->bands[band])
3775 continue;
3776 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3777 struct ieee80211_channel *chan;
3778
3779 chan = &wiphy->bands[band]->channels[j];
3780
3781 if (chan->flags & IEEE80211_CHAN_DISABLED)
3782 continue;
3783
3784 request->channels[i] = chan;
3785 i++;
3786 }
3787 }
3788 }
3789
3790 if (!i) {
3791 err = -EINVAL;
3792 goto out_free;
3793 }
3794
3795 request->n_channels = i;
3796
3797 i = 0;
3798 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3799 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3800 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003801 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003802 err = -EINVAL;
3803 goto out_free;
3804 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003805 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003806 memcpy(request->ssids[i].ssid, nla_data(attr),
3807 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003808 i++;
3809 }
3810 }
3811
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003812 i = 0;
3813 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3814 nla_for_each_nested(attr,
3815 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3816 tmp) {
3817 struct nlattr *ssid;
3818
3819 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3820 nla_data(attr), nla_len(attr),
3821 nl80211_match_policy);
3822 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3823 if (ssid) {
3824 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3825 err = -EINVAL;
3826 goto out_free;
3827 }
3828 memcpy(request->match_sets[i].ssid.ssid,
3829 nla_data(ssid), nla_len(ssid));
3830 request->match_sets[i].ssid.ssid_len =
3831 nla_len(ssid);
3832 }
3833 i++;
3834 }
3835 }
3836
Luciano Coelho807f8a82011-05-11 17:09:35 +03003837 if (info->attrs[NL80211_ATTR_IE]) {
3838 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3839 memcpy((void *)request->ie,
3840 nla_data(info->attrs[NL80211_ATTR_IE]),
3841 request->ie_len);
3842 }
3843
3844 request->dev = dev;
3845 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003846 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003847
3848 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3849 if (!err) {
3850 rdev->sched_scan_req = request;
3851 nl80211_send_sched_scan(rdev, dev,
3852 NL80211_CMD_START_SCHED_SCAN);
3853 goto out;
3854 }
3855
3856out_free:
3857 kfree(request);
3858out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003859 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003860 return err;
3861}
3862
3863static int nl80211_stop_sched_scan(struct sk_buff *skb,
3864 struct genl_info *info)
3865{
3866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003867 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003868
3869 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3870 !rdev->ops->sched_scan_stop)
3871 return -EOPNOTSUPP;
3872
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003873 mutex_lock(&rdev->sched_scan_mtx);
3874 err = __cfg80211_stop_sched_scan(rdev, false);
3875 mutex_unlock(&rdev->sched_scan_mtx);
3876
3877 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003878}
3879
Johannes Berg9720bb32011-06-21 09:45:33 +02003880static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3881 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003882 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003883 struct wireless_dev *wdev,
3884 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003885{
Johannes Berg48ab9052009-07-10 18:42:31 +02003886 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003887 void *hdr;
3888 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003889 int i;
3890
3891 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003892
Johannes Berg9720bb32011-06-21 09:45:33 +02003893 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003894 NL80211_CMD_NEW_SCAN_RESULTS);
3895 if (!hdr)
3896 return -1;
3897
Johannes Berg9720bb32011-06-21 09:45:33 +02003898 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3899
Johannes Bergf5ea9122009-08-07 16:17:38 +02003900 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003901 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003902
3903 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3904 if (!bss)
3905 goto nla_put_failure;
3906 if (!is_zero_ether_addr(res->bssid))
3907 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3908 if (res->information_elements && res->len_information_elements)
3909 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3910 res->len_information_elements,
3911 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003912 if (res->beacon_ies && res->len_beacon_ies &&
3913 res->beacon_ies != res->information_elements)
3914 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3915 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003916 if (res->tsf)
3917 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3918 if (res->beacon_interval)
3919 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3920 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3921 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003922 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3923 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003924
Johannes Berg77965c92009-02-18 18:45:06 +01003925 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003926 case CFG80211_SIGNAL_TYPE_MBM:
3927 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3928 break;
3929 case CFG80211_SIGNAL_TYPE_UNSPEC:
3930 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3931 break;
3932 default:
3933 break;
3934 }
3935
Johannes Berg48ab9052009-07-10 18:42:31 +02003936 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003937 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003938 case NL80211_IFTYPE_STATION:
3939 if (intbss == wdev->current_bss)
3940 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3941 NL80211_BSS_STATUS_ASSOCIATED);
3942 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3943 if (intbss != wdev->auth_bsses[i])
3944 continue;
3945 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3946 NL80211_BSS_STATUS_AUTHENTICATED);
3947 break;
3948 }
3949 break;
3950 case NL80211_IFTYPE_ADHOC:
3951 if (intbss == wdev->current_bss)
3952 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3953 NL80211_BSS_STATUS_IBSS_JOINED);
3954 break;
3955 default:
3956 break;
3957 }
3958
Johannes Berg2a519312009-02-10 21:25:55 +01003959 nla_nest_end(msg, bss);
3960
3961 return genlmsg_end(msg, hdr);
3962
3963 nla_put_failure:
3964 genlmsg_cancel(msg, hdr);
3965 return -EMSGSIZE;
3966}
3967
3968static int nl80211_dump_scan(struct sk_buff *skb,
3969 struct netlink_callback *cb)
3970{
Johannes Berg48ab9052009-07-10 18:42:31 +02003971 struct cfg80211_registered_device *rdev;
3972 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003973 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003974 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003975 int start = cb->args[1], idx = 0;
3976 int err;
3977
Johannes Berg67748892010-10-04 21:14:06 +02003978 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3979 if (err)
3980 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003981
Johannes Berg48ab9052009-07-10 18:42:31 +02003982 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003983
Johannes Berg48ab9052009-07-10 18:42:31 +02003984 wdev_lock(wdev);
3985 spin_lock_bh(&rdev->bss_lock);
3986 cfg80211_bss_expire(rdev);
3987
Johannes Berg9720bb32011-06-21 09:45:33 +02003988 cb->seq = rdev->bss_generation;
3989
Johannes Berg48ab9052009-07-10 18:42:31 +02003990 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003991 if (++idx <= start)
3992 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02003993 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01003994 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003995 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003996 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003997 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003998 }
3999 }
4000
Johannes Berg48ab9052009-07-10 18:42:31 +02004001 spin_unlock_bh(&rdev->bss_lock);
4002 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004003
4004 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004005 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004006
Johannes Berg67748892010-10-04 21:14:06 +02004007 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004008}
4009
Holger Schurig61fa7132009-11-11 12:25:40 +01004010static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4011 int flags, struct net_device *dev,
4012 struct survey_info *survey)
4013{
4014 void *hdr;
4015 struct nlattr *infoattr;
4016
Holger Schurig61fa7132009-11-11 12:25:40 +01004017 hdr = nl80211hdr_put(msg, pid, seq, flags,
4018 NL80211_CMD_NEW_SURVEY_RESULTS);
4019 if (!hdr)
4020 return -ENOMEM;
4021
4022 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4023
4024 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4025 if (!infoattr)
4026 goto nla_put_failure;
4027
4028 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4029 survey->channel->center_freq);
4030 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4031 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4032 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004033 if (survey->filled & SURVEY_INFO_IN_USE)
4034 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004035 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4036 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4037 survey->channel_time);
4038 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4039 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4040 survey->channel_time_busy);
4041 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4042 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4043 survey->channel_time_ext_busy);
4044 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4045 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4046 survey->channel_time_rx);
4047 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4048 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4049 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004050
4051 nla_nest_end(msg, infoattr);
4052
4053 return genlmsg_end(msg, hdr);
4054
4055 nla_put_failure:
4056 genlmsg_cancel(msg, hdr);
4057 return -EMSGSIZE;
4058}
4059
4060static int nl80211_dump_survey(struct sk_buff *skb,
4061 struct netlink_callback *cb)
4062{
4063 struct survey_info survey;
4064 struct cfg80211_registered_device *dev;
4065 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004066 int survey_idx = cb->args[1];
4067 int res;
4068
Johannes Berg67748892010-10-04 21:14:06 +02004069 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4070 if (res)
4071 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004072
4073 if (!dev->ops->dump_survey) {
4074 res = -EOPNOTSUPP;
4075 goto out_err;
4076 }
4077
4078 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004079 struct ieee80211_channel *chan;
4080
Holger Schurig61fa7132009-11-11 12:25:40 +01004081 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4082 &survey);
4083 if (res == -ENOENT)
4084 break;
4085 if (res)
4086 goto out_err;
4087
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004088 /* Survey without a channel doesn't make sense */
4089 if (!survey.channel) {
4090 res = -EINVAL;
4091 goto out;
4092 }
4093
4094 chan = ieee80211_get_channel(&dev->wiphy,
4095 survey.channel->center_freq);
4096 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4097 survey_idx++;
4098 continue;
4099 }
4100
Holger Schurig61fa7132009-11-11 12:25:40 +01004101 if (nl80211_send_survey(skb,
4102 NETLINK_CB(cb->skb).pid,
4103 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4104 netdev,
4105 &survey) < 0)
4106 goto out;
4107 survey_idx++;
4108 }
4109
4110 out:
4111 cb->args[1] = survey_idx;
4112 res = skb->len;
4113 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004114 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004115 return res;
4116}
4117
Jouni Malinen255e7372009-03-20 21:21:17 +02004118static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4119{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004120 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004121}
4122
Samuel Ortizb23aa672009-07-01 21:26:54 +02004123static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4124{
4125 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4126 NL80211_WPA_VERSION_2));
4127}
4128
4129static bool nl80211_valid_akm_suite(u32 akm)
4130{
4131 return akm == WLAN_AKM_SUITE_8021X ||
4132 akm == WLAN_AKM_SUITE_PSK;
4133}
4134
4135static bool nl80211_valid_cipher_suite(u32 cipher)
4136{
4137 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
4138 cipher == WLAN_CIPHER_SUITE_WEP104 ||
4139 cipher == WLAN_CIPHER_SUITE_TKIP ||
4140 cipher == WLAN_CIPHER_SUITE_CCMP ||
4141 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
4142}
4143
4144
Jouni Malinen636a5d32009-03-19 13:39:22 +02004145static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4146{
Johannes Berg4c476992010-10-04 21:36:35 +02004147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4148 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004149 struct ieee80211_channel *chan;
4150 const u8 *bssid, *ssid, *ie = NULL;
4151 int err, ssid_len, ie_len = 0;
4152 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004153 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004154 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004155
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004156 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4157 return -EINVAL;
4158
4159 if (!info->attrs[NL80211_ATTR_MAC])
4160 return -EINVAL;
4161
Jouni Malinen17780922009-03-27 20:52:47 +02004162 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4163 return -EINVAL;
4164
Johannes Berg19957bb2009-07-02 17:20:43 +02004165 if (!info->attrs[NL80211_ATTR_SSID])
4166 return -EINVAL;
4167
4168 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4169 return -EINVAL;
4170
Johannes Bergfffd0932009-07-08 14:22:54 +02004171 err = nl80211_parse_key(info, &key);
4172 if (err)
4173 return err;
4174
4175 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004176 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4177 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004178 if (!key.p.key || !key.p.key_len)
4179 return -EINVAL;
4180 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4181 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4182 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4183 key.p.key_len != WLAN_KEY_LEN_WEP104))
4184 return -EINVAL;
4185 if (key.idx > 4)
4186 return -EINVAL;
4187 } else {
4188 key.p.key_len = 0;
4189 key.p.key = NULL;
4190 }
4191
Johannes Bergafea0b72010-08-10 09:46:42 +02004192 if (key.idx >= 0) {
4193 int i;
4194 bool ok = false;
4195 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4196 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4197 ok = true;
4198 break;
4199 }
4200 }
Johannes Berg4c476992010-10-04 21:36:35 +02004201 if (!ok)
4202 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004203 }
4204
Johannes Berg4c476992010-10-04 21:36:35 +02004205 if (!rdev->ops->auth)
4206 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004207
Johannes Berg074ac8d2010-09-16 14:58:22 +02004208 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004209 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4210 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004211
Johannes Berg19957bb2009-07-02 17:20:43 +02004212 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004213 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004214 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004215 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4216 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004217
Johannes Berg19957bb2009-07-02 17:20:43 +02004218 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4219 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4220
4221 if (info->attrs[NL80211_ATTR_IE]) {
4222 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4223 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4224 }
4225
4226 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004227 if (!nl80211_valid_auth_type(auth_type))
4228 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004229
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004230 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4231
Johannes Berg4c476992010-10-04 21:36:35 +02004232 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4233 ssid, ssid_len, ie, ie_len,
4234 key.p.key, key.p.key_len, key.idx,
4235 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004236}
4237
Johannes Bergc0692b82010-08-27 14:26:53 +03004238static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4239 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004240 struct cfg80211_crypto_settings *settings,
4241 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004242{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004243 memset(settings, 0, sizeof(*settings));
4244
Samuel Ortizb23aa672009-07-01 21:26:54 +02004245 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4246
Johannes Bergc0692b82010-08-27 14:26:53 +03004247 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4248 u16 proto;
4249 proto = nla_get_u16(
4250 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4251 settings->control_port_ethertype = cpu_to_be16(proto);
4252 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4253 proto != ETH_P_PAE)
4254 return -EINVAL;
4255 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4256 settings->control_port_no_encrypt = true;
4257 } else
4258 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4259
Samuel Ortizb23aa672009-07-01 21:26:54 +02004260 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4261 void *data;
4262 int len, i;
4263
4264 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4265 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4266 settings->n_ciphers_pairwise = len / sizeof(u32);
4267
4268 if (len % sizeof(u32))
4269 return -EINVAL;
4270
Johannes Berg3dc27d22009-07-02 21:36:37 +02004271 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004272 return -EINVAL;
4273
4274 memcpy(settings->ciphers_pairwise, data, len);
4275
4276 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4277 if (!nl80211_valid_cipher_suite(
4278 settings->ciphers_pairwise[i]))
4279 return -EINVAL;
4280 }
4281
4282 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4283 settings->cipher_group =
4284 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4285 if (!nl80211_valid_cipher_suite(settings->cipher_group))
4286 return -EINVAL;
4287 }
4288
4289 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4290 settings->wpa_versions =
4291 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4292 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4293 return -EINVAL;
4294 }
4295
4296 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4297 void *data;
4298 int len, i;
4299
4300 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4301 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4302 settings->n_akm_suites = len / sizeof(u32);
4303
4304 if (len % sizeof(u32))
4305 return -EINVAL;
4306
4307 memcpy(settings->akm_suites, data, len);
4308
4309 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4310 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
4311 return -EINVAL;
4312 }
4313
4314 return 0;
4315}
4316
Jouni Malinen636a5d32009-03-19 13:39:22 +02004317static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4318{
Johannes Berg4c476992010-10-04 21:36:35 +02004319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4320 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004321 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004322 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004323 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004324 int err, ssid_len, ie_len = 0;
4325 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004326
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004327 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4328 return -EINVAL;
4329
4330 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004331 !info->attrs[NL80211_ATTR_SSID] ||
4332 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004333 return -EINVAL;
4334
Johannes Berg4c476992010-10-04 21:36:35 +02004335 if (!rdev->ops->assoc)
4336 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004337
Johannes Berg074ac8d2010-09-16 14:58:22 +02004338 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004339 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4340 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004341
Johannes Berg19957bb2009-07-02 17:20:43 +02004342 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004343
Johannes Berg19957bb2009-07-02 17:20:43 +02004344 chan = ieee80211_get_channel(&rdev->wiphy,
4345 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004346 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4347 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004348
Johannes Berg19957bb2009-07-02 17:20:43 +02004349 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4350 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004351
4352 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004353 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4354 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004355 }
4356
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004357 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004358 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004359 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004360 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004361 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004362 else if (mfp != NL80211_MFP_NO)
4363 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004364 }
4365
Johannes Berg3e5d7642009-07-07 14:37:26 +02004366 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4367 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4368
Johannes Bergc0692b82010-08-27 14:26:53 +03004369 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004370 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004371 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4372 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004373 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004374
Jouni Malinen636a5d32009-03-19 13:39:22 +02004375 return err;
4376}
4377
4378static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4379{
Johannes Berg4c476992010-10-04 21:36:35 +02004380 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4381 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004382 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004383 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004384 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004385 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004386
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004387 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4388 return -EINVAL;
4389
4390 if (!info->attrs[NL80211_ATTR_MAC])
4391 return -EINVAL;
4392
4393 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4394 return -EINVAL;
4395
Johannes Berg4c476992010-10-04 21:36:35 +02004396 if (!rdev->ops->deauth)
4397 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004398
Johannes Berg074ac8d2010-09-16 14:58:22 +02004399 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004400 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4401 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004402
Johannes Berg19957bb2009-07-02 17:20:43 +02004403 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004404
Johannes Berg19957bb2009-07-02 17:20:43 +02004405 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4406 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004407 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004408 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004409 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004410
4411 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004412 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4413 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004414 }
4415
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004416 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4417
Johannes Berg4c476992010-10-04 21:36:35 +02004418 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4419 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004420}
4421
4422static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4423{
Johannes Berg4c476992010-10-04 21:36:35 +02004424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4425 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004426 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004427 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004428 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004429 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004430
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004431 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4432 return -EINVAL;
4433
4434 if (!info->attrs[NL80211_ATTR_MAC])
4435 return -EINVAL;
4436
4437 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4438 return -EINVAL;
4439
Johannes Berg4c476992010-10-04 21:36:35 +02004440 if (!rdev->ops->disassoc)
4441 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004442
Johannes Berg074ac8d2010-09-16 14:58:22 +02004443 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004444 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4445 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004446
Johannes Berg19957bb2009-07-02 17:20:43 +02004447 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004448
Johannes Berg19957bb2009-07-02 17:20:43 +02004449 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4450 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004451 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004452 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004453 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004454
4455 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004456 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4457 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004458 }
4459
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004460 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4461
Johannes Berg4c476992010-10-04 21:36:35 +02004462 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4463 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004464}
4465
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004466static bool
4467nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4468 int mcast_rate[IEEE80211_NUM_BANDS],
4469 int rateval)
4470{
4471 struct wiphy *wiphy = &rdev->wiphy;
4472 bool found = false;
4473 int band, i;
4474
4475 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4476 struct ieee80211_supported_band *sband;
4477
4478 sband = wiphy->bands[band];
4479 if (!sband)
4480 continue;
4481
4482 for (i = 0; i < sband->n_bitrates; i++) {
4483 if (sband->bitrates[i].bitrate == rateval) {
4484 mcast_rate[band] = i + 1;
4485 found = true;
4486 break;
4487 }
4488 }
4489 }
4490
4491 return found;
4492}
4493
Johannes Berg04a773a2009-04-19 21:24:32 +02004494static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4495{
Johannes Berg4c476992010-10-04 21:36:35 +02004496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4497 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004498 struct cfg80211_ibss_params ibss;
4499 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004500 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004501 int err;
4502
Johannes Berg8e30bc52009-04-22 17:45:38 +02004503 memset(&ibss, 0, sizeof(ibss));
4504
Johannes Berg04a773a2009-04-19 21:24:32 +02004505 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4506 return -EINVAL;
4507
4508 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4509 !info->attrs[NL80211_ATTR_SSID] ||
4510 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4511 return -EINVAL;
4512
Johannes Berg8e30bc52009-04-22 17:45:38 +02004513 ibss.beacon_interval = 100;
4514
4515 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4516 ibss.beacon_interval =
4517 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4518 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4519 return -EINVAL;
4520 }
4521
Johannes Berg4c476992010-10-04 21:36:35 +02004522 if (!rdev->ops->join_ibss)
4523 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004524
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4526 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004527
Johannes Berg79c97e92009-07-07 03:56:12 +02004528 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004529
Johannes Berg39193492011-09-16 13:45:25 +02004530 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004531 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004532
4533 if (!is_valid_ether_addr(ibss.bssid))
4534 return -EINVAL;
4535 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004536 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4537 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4538
4539 if (info->attrs[NL80211_ATTR_IE]) {
4540 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4541 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4542 }
4543
4544 ibss.channel = ieee80211_get_channel(wiphy,
4545 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4546 if (!ibss.channel ||
4547 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004548 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4549 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004550
4551 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004552 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004553
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004554 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4555 u8 *rates =
4556 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4557 int n_rates =
4558 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4559 struct ieee80211_supported_band *sband =
4560 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004561 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004562
Johannes Berg34850ab2011-07-18 18:08:35 +02004563 err = ieee80211_get_ratemask(sband, rates, n_rates,
4564 &ibss.basic_rates);
4565 if (err)
4566 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004567 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004568
4569 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4570 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4571 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4572 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004573
Johannes Berg4c476992010-10-04 21:36:35 +02004574 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4575 connkeys = nl80211_parse_connkeys(rdev,
4576 info->attrs[NL80211_ATTR_KEYS]);
4577 if (IS_ERR(connkeys))
4578 return PTR_ERR(connkeys);
4579 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004580
Johannes Berg4c476992010-10-04 21:36:35 +02004581 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004582 if (err)
4583 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004584 return err;
4585}
4586
4587static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4588{
Johannes Berg4c476992010-10-04 21:36:35 +02004589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4590 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004591
Johannes Berg4c476992010-10-04 21:36:35 +02004592 if (!rdev->ops->leave_ibss)
4593 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004594
Johannes Berg4c476992010-10-04 21:36:35 +02004595 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4596 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004597
Johannes Berg4c476992010-10-04 21:36:35 +02004598 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004599}
4600
Johannes Bergaff89a92009-07-01 21:26:51 +02004601#ifdef CONFIG_NL80211_TESTMODE
4602static struct genl_multicast_group nl80211_testmode_mcgrp = {
4603 .name = "testmode",
4604};
4605
4606static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4607{
Johannes Berg4c476992010-10-04 21:36:35 +02004608 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004609 int err;
4610
4611 if (!info->attrs[NL80211_ATTR_TESTDATA])
4612 return -EINVAL;
4613
Johannes Bergaff89a92009-07-01 21:26:51 +02004614 err = -EOPNOTSUPP;
4615 if (rdev->ops->testmode_cmd) {
4616 rdev->testmode_info = info;
4617 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4618 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4619 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4620 rdev->testmode_info = NULL;
4621 }
4622
Johannes Bergaff89a92009-07-01 21:26:51 +02004623 return err;
4624}
4625
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004626static int nl80211_testmode_dump(struct sk_buff *skb,
4627 struct netlink_callback *cb)
4628{
4629 struct cfg80211_registered_device *dev;
4630 int err;
4631 long phy_idx;
4632 void *data = NULL;
4633 int data_len = 0;
4634
4635 if (cb->args[0]) {
4636 /*
4637 * 0 is a valid index, but not valid for args[0],
4638 * so we need to offset by 1.
4639 */
4640 phy_idx = cb->args[0] - 1;
4641 } else {
4642 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4643 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4644 nl80211_policy);
4645 if (err)
4646 return err;
4647 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4648 return -EINVAL;
4649 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4650 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4651 cb->args[1] =
4652 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4653 }
4654
4655 if (cb->args[1]) {
4656 data = nla_data((void *)cb->args[1]);
4657 data_len = nla_len((void *)cb->args[1]);
4658 }
4659
4660 mutex_lock(&cfg80211_mutex);
4661 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4662 if (!dev) {
4663 mutex_unlock(&cfg80211_mutex);
4664 return -ENOENT;
4665 }
4666 cfg80211_lock_rdev(dev);
4667 mutex_unlock(&cfg80211_mutex);
4668
4669 if (!dev->ops->testmode_dump) {
4670 err = -EOPNOTSUPP;
4671 goto out_err;
4672 }
4673
4674 while (1) {
4675 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4676 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4677 NL80211_CMD_TESTMODE);
4678 struct nlattr *tmdata;
4679
4680 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4681 genlmsg_cancel(skb, hdr);
4682 break;
4683 }
4684
4685 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4686 if (!tmdata) {
4687 genlmsg_cancel(skb, hdr);
4688 break;
4689 }
4690 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4691 data, data_len);
4692 nla_nest_end(skb, tmdata);
4693
4694 if (err == -ENOBUFS || err == -ENOENT) {
4695 genlmsg_cancel(skb, hdr);
4696 break;
4697 } else if (err) {
4698 genlmsg_cancel(skb, hdr);
4699 goto out_err;
4700 }
4701
4702 genlmsg_end(skb, hdr);
4703 }
4704
4705 err = skb->len;
4706 /* see above */
4707 cb->args[0] = phy_idx + 1;
4708 out_err:
4709 cfg80211_unlock_rdev(dev);
4710 return err;
4711}
4712
Johannes Bergaff89a92009-07-01 21:26:51 +02004713static struct sk_buff *
4714__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4715 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4716{
4717 struct sk_buff *skb;
4718 void *hdr;
4719 struct nlattr *data;
4720
4721 skb = nlmsg_new(approxlen + 100, gfp);
4722 if (!skb)
4723 return NULL;
4724
4725 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4726 if (!hdr) {
4727 kfree_skb(skb);
4728 return NULL;
4729 }
4730
4731 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4732 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4733
4734 ((void **)skb->cb)[0] = rdev;
4735 ((void **)skb->cb)[1] = hdr;
4736 ((void **)skb->cb)[2] = data;
4737
4738 return skb;
4739
4740 nla_put_failure:
4741 kfree_skb(skb);
4742 return NULL;
4743}
4744
4745struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4746 int approxlen)
4747{
4748 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4749
4750 if (WARN_ON(!rdev->testmode_info))
4751 return NULL;
4752
4753 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4754 rdev->testmode_info->snd_pid,
4755 rdev->testmode_info->snd_seq,
4756 GFP_KERNEL);
4757}
4758EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4759
4760int cfg80211_testmode_reply(struct sk_buff *skb)
4761{
4762 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4763 void *hdr = ((void **)skb->cb)[1];
4764 struct nlattr *data = ((void **)skb->cb)[2];
4765
4766 if (WARN_ON(!rdev->testmode_info)) {
4767 kfree_skb(skb);
4768 return -EINVAL;
4769 }
4770
4771 nla_nest_end(skb, data);
4772 genlmsg_end(skb, hdr);
4773 return genlmsg_reply(skb, rdev->testmode_info);
4774}
4775EXPORT_SYMBOL(cfg80211_testmode_reply);
4776
4777struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4778 int approxlen, gfp_t gfp)
4779{
4780 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4781
4782 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4783}
4784EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4785
4786void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4787{
4788 void *hdr = ((void **)skb->cb)[1];
4789 struct nlattr *data = ((void **)skb->cb)[2];
4790
4791 nla_nest_end(skb, data);
4792 genlmsg_end(skb, hdr);
4793 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4794}
4795EXPORT_SYMBOL(cfg80211_testmode_event);
4796#endif
4797
Samuel Ortizb23aa672009-07-01 21:26:54 +02004798static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4799{
Johannes Berg4c476992010-10-04 21:36:35 +02004800 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4801 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004802 struct cfg80211_connect_params connect;
4803 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004804 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004805 int err;
4806
4807 memset(&connect, 0, sizeof(connect));
4808
4809 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4810 return -EINVAL;
4811
4812 if (!info->attrs[NL80211_ATTR_SSID] ||
4813 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4814 return -EINVAL;
4815
4816 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4817 connect.auth_type =
4818 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4819 if (!nl80211_valid_auth_type(connect.auth_type))
4820 return -EINVAL;
4821 } else
4822 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4823
4824 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4825
Johannes Bergc0692b82010-08-27 14:26:53 +03004826 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004827 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004828 if (err)
4829 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004830
Johannes Berg074ac8d2010-09-16 14:58:22 +02004831 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004832 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4833 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004834
Johannes Berg79c97e92009-07-07 03:56:12 +02004835 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004836
Samuel Ortizb23aa672009-07-01 21:26:54 +02004837 if (info->attrs[NL80211_ATTR_MAC])
4838 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4839 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4840 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4841
4842 if (info->attrs[NL80211_ATTR_IE]) {
4843 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4844 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4845 }
4846
4847 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4848 connect.channel =
4849 ieee80211_get_channel(wiphy,
4850 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4851 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004852 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4853 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004854 }
4855
Johannes Bergfffd0932009-07-08 14:22:54 +02004856 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4857 connkeys = nl80211_parse_connkeys(rdev,
4858 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004859 if (IS_ERR(connkeys))
4860 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004861 }
4862
4863 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004864 if (err)
4865 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004866 return err;
4867}
4868
4869static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4870{
Johannes Berg4c476992010-10-04 21:36:35 +02004871 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4872 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004873 u16 reason;
4874
4875 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4876 reason = WLAN_REASON_DEAUTH_LEAVING;
4877 else
4878 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4879
4880 if (reason == 0)
4881 return -EINVAL;
4882
Johannes Berg074ac8d2010-09-16 14:58:22 +02004883 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004884 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4885 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004886
Johannes Berg4c476992010-10-04 21:36:35 +02004887 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004888}
4889
Johannes Berg463d0182009-07-14 00:33:35 +02004890static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4891{
Johannes Berg4c476992010-10-04 21:36:35 +02004892 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004893 struct net *net;
4894 int err;
4895 u32 pid;
4896
4897 if (!info->attrs[NL80211_ATTR_PID])
4898 return -EINVAL;
4899
4900 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4901
Johannes Berg463d0182009-07-14 00:33:35 +02004902 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004903 if (IS_ERR(net))
4904 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004905
4906 err = 0;
4907
4908 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004909 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4910 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004911
Johannes Berg463d0182009-07-14 00:33:35 +02004912 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004913 return err;
4914}
4915
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004916static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4917{
Johannes Berg4c476992010-10-04 21:36:35 +02004918 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004919 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4920 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004921 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004922 struct cfg80211_pmksa pmksa;
4923
4924 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4925
4926 if (!info->attrs[NL80211_ATTR_MAC])
4927 return -EINVAL;
4928
4929 if (!info->attrs[NL80211_ATTR_PMKID])
4930 return -EINVAL;
4931
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004932 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4933 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4934
Johannes Berg074ac8d2010-09-16 14:58:22 +02004935 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004936 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4937 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004938
4939 switch (info->genlhdr->cmd) {
4940 case NL80211_CMD_SET_PMKSA:
4941 rdev_ops = rdev->ops->set_pmksa;
4942 break;
4943 case NL80211_CMD_DEL_PMKSA:
4944 rdev_ops = rdev->ops->del_pmksa;
4945 break;
4946 default:
4947 WARN_ON(1);
4948 break;
4949 }
4950
Johannes Berg4c476992010-10-04 21:36:35 +02004951 if (!rdev_ops)
4952 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004953
Johannes Berg4c476992010-10-04 21:36:35 +02004954 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004955}
4956
4957static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4958{
Johannes Berg4c476992010-10-04 21:36:35 +02004959 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4960 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004961
Johannes Berg074ac8d2010-09-16 14:58:22 +02004962 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004963 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4964 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004965
Johannes Berg4c476992010-10-04 21:36:35 +02004966 if (!rdev->ops->flush_pmksa)
4967 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004968
Johannes Berg4c476992010-10-04 21:36:35 +02004969 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004970}
4971
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004972static int nl80211_remain_on_channel(struct sk_buff *skb,
4973 struct genl_info *info)
4974{
Johannes Berg4c476992010-10-04 21:36:35 +02004975 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4976 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004977 struct ieee80211_channel *chan;
4978 struct sk_buff *msg;
4979 void *hdr;
4980 u64 cookie;
4981 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4982 u32 freq, duration;
4983 int err;
4984
4985 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4986 !info->attrs[NL80211_ATTR_DURATION])
4987 return -EINVAL;
4988
4989 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4990
4991 /*
4992 * We should be on that channel for at least one jiffie,
4993 * and more than 5 seconds seems excessive.
4994 */
Johannes Berga2939112010-12-14 17:54:28 +01004995 if (!duration || !msecs_to_jiffies(duration) ||
4996 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004997 return -EINVAL;
4998
Johannes Berg4c476992010-10-04 21:36:35 +02004999 if (!rdev->ops->remain_on_channel)
5000 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005001
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005002 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5003 channel_type = nla_get_u32(
5004 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5005 if (channel_type != NL80211_CHAN_NO_HT &&
5006 channel_type != NL80211_CHAN_HT20 &&
5007 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005008 channel_type != NL80211_CHAN_HT40MINUS)
5009 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005010 }
5011
5012 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5013 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005014 if (chan == NULL)
5015 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005016
5017 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005018 if (!msg)
5019 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005020
5021 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5022 NL80211_CMD_REMAIN_ON_CHANNEL);
5023
5024 if (IS_ERR(hdr)) {
5025 err = PTR_ERR(hdr);
5026 goto free_msg;
5027 }
5028
5029 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5030 channel_type, duration, &cookie);
5031
5032 if (err)
5033 goto free_msg;
5034
5035 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5036
5037 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005038
5039 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005040
5041 nla_put_failure:
5042 err = -ENOBUFS;
5043 free_msg:
5044 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005045 return err;
5046}
5047
5048static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5049 struct genl_info *info)
5050{
Johannes Berg4c476992010-10-04 21:36:35 +02005051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5052 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005053 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005054
5055 if (!info->attrs[NL80211_ATTR_COOKIE])
5056 return -EINVAL;
5057
Johannes Berg4c476992010-10-04 21:36:35 +02005058 if (!rdev->ops->cancel_remain_on_channel)
5059 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005060
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005061 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5062
Johannes Berg4c476992010-10-04 21:36:35 +02005063 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005064}
5065
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005066static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5067 u8 *rates, u8 rates_len)
5068{
5069 u8 i;
5070 u32 mask = 0;
5071
5072 for (i = 0; i < rates_len; i++) {
5073 int rate = (rates[i] & 0x7f) * 5;
5074 int ridx;
5075 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5076 struct ieee80211_rate *srate =
5077 &sband->bitrates[ridx];
5078 if (rate == srate->bitrate) {
5079 mask |= 1 << ridx;
5080 break;
5081 }
5082 }
5083 if (ridx == sband->n_bitrates)
5084 return 0; /* rate not found */
5085 }
5086
5087 return mask;
5088}
5089
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005090static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005091 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5092 .len = NL80211_MAX_SUPP_RATES },
5093};
5094
5095static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5096 struct genl_info *info)
5097{
5098 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005099 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005100 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005101 int rem, i;
5102 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005103 struct nlattr *tx_rates;
5104 struct ieee80211_supported_band *sband;
5105
5106 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5107 return -EINVAL;
5108
Johannes Berg4c476992010-10-04 21:36:35 +02005109 if (!rdev->ops->set_bitrate_mask)
5110 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005111
5112 memset(&mask, 0, sizeof(mask));
5113 /* Default to all rates enabled */
5114 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5115 sband = rdev->wiphy.bands[i];
5116 mask.control[i].legacy =
5117 sband ? (1 << sband->n_bitrates) - 1 : 0;
5118 }
5119
5120 /*
5121 * The nested attribute uses enum nl80211_band as the index. This maps
5122 * directly to the enum ieee80211_band values used in cfg80211.
5123 */
5124 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5125 {
5126 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005127 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5128 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005129 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005130 if (sband == NULL)
5131 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005132 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5133 nla_len(tx_rates), nl80211_txattr_policy);
5134 if (tb[NL80211_TXRATE_LEGACY]) {
5135 mask.control[band].legacy = rateset_to_mask(
5136 sband,
5137 nla_data(tb[NL80211_TXRATE_LEGACY]),
5138 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005139 if (mask.control[band].legacy == 0)
5140 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005141 }
5142 }
5143
Johannes Berg4c476992010-10-04 21:36:35 +02005144 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005145}
5146
Johannes Berg2e161f72010-08-12 15:38:38 +02005147static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005148{
Johannes Berg4c476992010-10-04 21:36:35 +02005149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5150 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005151 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005152
5153 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5154 return -EINVAL;
5155
Johannes Berg2e161f72010-08-12 15:38:38 +02005156 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5157 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005158
Johannes Berg9d38d852010-06-09 17:20:33 +02005159 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005160 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005161 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5162 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5163 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005164 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005165 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5166 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005167
5168 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005169 if (!rdev->ops->mgmt_tx)
5170 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005171
Johannes Berg4c476992010-10-04 21:36:35 +02005172 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005173 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005174 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5175 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005176}
5177
Johannes Berg2e161f72010-08-12 15:38:38 +02005178static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005179{
Johannes Berg4c476992010-10-04 21:36:35 +02005180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5181 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005182 struct ieee80211_channel *chan;
5183 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005184 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005185 u32 freq;
5186 int err;
5187 void *hdr;
5188 u64 cookie;
5189 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005190 unsigned int wait = 0;
5191 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02005192
5193 if (!info->attrs[NL80211_ATTR_FRAME] ||
5194 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5195 return -EINVAL;
5196
Johannes Berg4c476992010-10-04 21:36:35 +02005197 if (!rdev->ops->mgmt_tx)
5198 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005199
Johannes Berg9d38d852010-06-09 17:20:33 +02005200 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005201 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005202 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5203 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5204 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005205 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005206 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5207 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005208
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005209 if (info->attrs[NL80211_ATTR_DURATION]) {
5210 if (!rdev->ops->mgmt_tx_cancel_wait)
5211 return -EINVAL;
5212 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5213 }
5214
Jouni Malinen026331c2010-02-15 12:53:10 +02005215 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5216 channel_type = nla_get_u32(
5217 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5218 if (channel_type != NL80211_CHAN_NO_HT &&
5219 channel_type != NL80211_CHAN_HT20 &&
5220 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005221 channel_type != NL80211_CHAN_HT40MINUS)
5222 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005223 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005224 }
5225
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005226 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5227
Jouni Malinen026331c2010-02-15 12:53:10 +02005228 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5229 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005230 if (chan == NULL)
5231 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005232
5233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005234 if (!msg)
5235 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005236
5237 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005238 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005239
5240 if (IS_ERR(hdr)) {
5241 err = PTR_ERR(hdr);
5242 goto free_msg;
5243 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005244 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5245 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005246 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5247 nla_len(info->attrs[NL80211_ATTR_FRAME]),
5248 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005249 if (err)
5250 goto free_msg;
5251
5252 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5253
5254 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005255 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005256
5257 nla_put_failure:
5258 err = -ENOBUFS;
5259 free_msg:
5260 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005261 return err;
5262}
5263
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005264static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5265{
5266 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5267 struct net_device *dev = info->user_ptr[1];
5268 u64 cookie;
5269
5270 if (!info->attrs[NL80211_ATTR_COOKIE])
5271 return -EINVAL;
5272
5273 if (!rdev->ops->mgmt_tx_cancel_wait)
5274 return -EOPNOTSUPP;
5275
5276 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5277 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5278 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5279 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5280 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5281 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5282 return -EOPNOTSUPP;
5283
5284 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5285
5286 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5287}
5288
Kalle Valoffb9eb32010-02-17 17:58:10 +02005289static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5290{
Johannes Berg4c476992010-10-04 21:36:35 +02005291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005292 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005293 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005294 u8 ps_state;
5295 bool state;
5296 int err;
5297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (!info->attrs[NL80211_ATTR_PS_STATE])
5299 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005300
5301 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5302
Johannes Berg4c476992010-10-04 21:36:35 +02005303 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5304 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005305
5306 wdev = dev->ieee80211_ptr;
5307
Johannes Berg4c476992010-10-04 21:36:35 +02005308 if (!rdev->ops->set_power_mgmt)
5309 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005310
5311 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5312
5313 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005314 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005315
Johannes Berg4c476992010-10-04 21:36:35 +02005316 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5317 wdev->ps_timeout);
5318 if (!err)
5319 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005320 return err;
5321}
5322
5323static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5324{
Johannes Berg4c476992010-10-04 21:36:35 +02005325 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005326 enum nl80211_ps_state ps_state;
5327 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005328 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005329 struct sk_buff *msg;
5330 void *hdr;
5331 int err;
5332
Kalle Valoffb9eb32010-02-17 17:58:10 +02005333 wdev = dev->ieee80211_ptr;
5334
Johannes Berg4c476992010-10-04 21:36:35 +02005335 if (!rdev->ops->set_power_mgmt)
5336 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005337
5338 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005339 if (!msg)
5340 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005341
5342 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5343 NL80211_CMD_GET_POWER_SAVE);
5344 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005345 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005346 goto free_msg;
5347 }
5348
5349 if (wdev->ps)
5350 ps_state = NL80211_PS_ENABLED;
5351 else
5352 ps_state = NL80211_PS_DISABLED;
5353
5354 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5355
5356 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005357 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005358
Johannes Berg4c476992010-10-04 21:36:35 +02005359 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005360 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005361 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005362 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005363 return err;
5364}
5365
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005366static struct nla_policy
5367nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5368 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5369 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5370 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5371};
5372
5373static int nl80211_set_cqm_rssi(struct genl_info *info,
5374 s32 threshold, u32 hysteresis)
5375{
Johannes Berg4c476992010-10-04 21:36:35 +02005376 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005377 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005378 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005379
5380 if (threshold > 0)
5381 return -EINVAL;
5382
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005383 wdev = dev->ieee80211_ptr;
5384
Johannes Berg4c476992010-10-04 21:36:35 +02005385 if (!rdev->ops->set_cqm_rssi_config)
5386 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005387
Johannes Berg074ac8d2010-09-16 14:58:22 +02005388 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005389 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5390 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005391
Johannes Berg4c476992010-10-04 21:36:35 +02005392 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5393 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005394}
5395
5396static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5397{
5398 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5399 struct nlattr *cqm;
5400 int err;
5401
5402 cqm = info->attrs[NL80211_ATTR_CQM];
5403 if (!cqm) {
5404 err = -EINVAL;
5405 goto out;
5406 }
5407
5408 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5409 nl80211_attr_cqm_policy);
5410 if (err)
5411 goto out;
5412
5413 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5414 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5415 s32 threshold;
5416 u32 hysteresis;
5417 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5418 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5419 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5420 } else
5421 err = -EINVAL;
5422
5423out:
5424 return err;
5425}
5426
Johannes Berg29cbe682010-12-03 09:20:44 +01005427static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5428{
5429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5430 struct net_device *dev = info->user_ptr[1];
5431 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005432 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005433 int err;
5434
5435 /* start with default */
5436 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005437 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005438
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005439 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005440 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005441 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005442 if (err)
5443 return err;
5444 }
5445
5446 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5447 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5448 return -EINVAL;
5449
Javier Cardonac80d5452010-12-16 17:37:49 -08005450 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5451 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5452
5453 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5454 /* parse additional setup parameters if given */
5455 err = nl80211_parse_mesh_setup(info, &setup);
5456 if (err)
5457 return err;
5458 }
5459
5460 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005461}
5462
5463static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5464{
5465 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5466 struct net_device *dev = info->user_ptr[1];
5467
5468 return cfg80211_leave_mesh(rdev, dev);
5469}
5470
Johannes Bergff1b6e62011-05-04 15:37:28 +02005471static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5472{
5473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5474 struct sk_buff *msg;
5475 void *hdr;
5476
5477 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5478 return -EOPNOTSUPP;
5479
5480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5481 if (!msg)
5482 return -ENOMEM;
5483
5484 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5485 NL80211_CMD_GET_WOWLAN);
5486 if (!hdr)
5487 goto nla_put_failure;
5488
5489 if (rdev->wowlan) {
5490 struct nlattr *nl_wowlan;
5491
5492 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5493 if (!nl_wowlan)
5494 goto nla_put_failure;
5495
5496 if (rdev->wowlan->any)
5497 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5498 if (rdev->wowlan->disconnect)
5499 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5500 if (rdev->wowlan->magic_pkt)
5501 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005502 if (rdev->wowlan->gtk_rekey_failure)
5503 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5504 if (rdev->wowlan->eap_identity_req)
5505 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5506 if (rdev->wowlan->four_way_handshake)
5507 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5508 if (rdev->wowlan->rfkill_release)
5509 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005510 if (rdev->wowlan->n_patterns) {
5511 struct nlattr *nl_pats, *nl_pat;
5512 int i, pat_len;
5513
5514 nl_pats = nla_nest_start(msg,
5515 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5516 if (!nl_pats)
5517 goto nla_put_failure;
5518
5519 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5520 nl_pat = nla_nest_start(msg, i + 1);
5521 if (!nl_pat)
5522 goto nla_put_failure;
5523 pat_len = rdev->wowlan->patterns[i].pattern_len;
5524 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5525 DIV_ROUND_UP(pat_len, 8),
5526 rdev->wowlan->patterns[i].mask);
5527 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5528 pat_len,
5529 rdev->wowlan->patterns[i].pattern);
5530 nla_nest_end(msg, nl_pat);
5531 }
5532 nla_nest_end(msg, nl_pats);
5533 }
5534
5535 nla_nest_end(msg, nl_wowlan);
5536 }
5537
5538 genlmsg_end(msg, hdr);
5539 return genlmsg_reply(msg, info);
5540
5541nla_put_failure:
5542 nlmsg_free(msg);
5543 return -ENOBUFS;
5544}
5545
5546static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5547{
5548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5549 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5550 struct cfg80211_wowlan no_triggers = {};
5551 struct cfg80211_wowlan new_triggers = {};
5552 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5553 int err, i;
5554
5555 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5556 return -EOPNOTSUPP;
5557
5558 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5559 goto no_triggers;
5560
5561 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5562 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5563 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5564 nl80211_wowlan_policy);
5565 if (err)
5566 return err;
5567
5568 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5569 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5570 return -EINVAL;
5571 new_triggers.any = true;
5572 }
5573
5574 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5575 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5576 return -EINVAL;
5577 new_triggers.disconnect = true;
5578 }
5579
5580 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5581 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5582 return -EINVAL;
5583 new_triggers.magic_pkt = true;
5584 }
5585
Johannes Berg77dbbb12011-07-13 10:48:55 +02005586 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5587 return -EINVAL;
5588
5589 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5590 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5591 return -EINVAL;
5592 new_triggers.gtk_rekey_failure = true;
5593 }
5594
5595 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5596 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5597 return -EINVAL;
5598 new_triggers.eap_identity_req = true;
5599 }
5600
5601 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5602 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5603 return -EINVAL;
5604 new_triggers.four_way_handshake = true;
5605 }
5606
5607 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5608 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5609 return -EINVAL;
5610 new_triggers.rfkill_release = true;
5611 }
5612
Johannes Bergff1b6e62011-05-04 15:37:28 +02005613 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5614 struct nlattr *pat;
5615 int n_patterns = 0;
5616 int rem, pat_len, mask_len;
5617 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5618
5619 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5620 rem)
5621 n_patterns++;
5622 if (n_patterns > wowlan->n_patterns)
5623 return -EINVAL;
5624
5625 new_triggers.patterns = kcalloc(n_patterns,
5626 sizeof(new_triggers.patterns[0]),
5627 GFP_KERNEL);
5628 if (!new_triggers.patterns)
5629 return -ENOMEM;
5630
5631 new_triggers.n_patterns = n_patterns;
5632 i = 0;
5633
5634 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5635 rem) {
5636 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5637 nla_data(pat), nla_len(pat), NULL);
5638 err = -EINVAL;
5639 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5640 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5641 goto error;
5642 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5643 mask_len = DIV_ROUND_UP(pat_len, 8);
5644 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5645 mask_len)
5646 goto error;
5647 if (pat_len > wowlan->pattern_max_len ||
5648 pat_len < wowlan->pattern_min_len)
5649 goto error;
5650
5651 new_triggers.patterns[i].mask =
5652 kmalloc(mask_len + pat_len, GFP_KERNEL);
5653 if (!new_triggers.patterns[i].mask) {
5654 err = -ENOMEM;
5655 goto error;
5656 }
5657 new_triggers.patterns[i].pattern =
5658 new_triggers.patterns[i].mask + mask_len;
5659 memcpy(new_triggers.patterns[i].mask,
5660 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5661 mask_len);
5662 new_triggers.patterns[i].pattern_len = pat_len;
5663 memcpy(new_triggers.patterns[i].pattern,
5664 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5665 pat_len);
5666 i++;
5667 }
5668 }
5669
5670 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5671 struct cfg80211_wowlan *ntrig;
5672 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5673 GFP_KERNEL);
5674 if (!ntrig) {
5675 err = -ENOMEM;
5676 goto error;
5677 }
5678 cfg80211_rdev_free_wowlan(rdev);
5679 rdev->wowlan = ntrig;
5680 } else {
5681 no_triggers:
5682 cfg80211_rdev_free_wowlan(rdev);
5683 rdev->wowlan = NULL;
5684 }
5685
5686 return 0;
5687 error:
5688 for (i = 0; i < new_triggers.n_patterns; i++)
5689 kfree(new_triggers.patterns[i].mask);
5690 kfree(new_triggers.patterns);
5691 return err;
5692}
5693
Johannes Berge5497d72011-07-05 16:35:40 +02005694static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5695{
5696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5697 struct net_device *dev = info->user_ptr[1];
5698 struct wireless_dev *wdev = dev->ieee80211_ptr;
5699 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5700 struct cfg80211_gtk_rekey_data rekey_data;
5701 int err;
5702
5703 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5704 return -EINVAL;
5705
5706 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5707 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5708 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5709 nl80211_rekey_policy);
5710 if (err)
5711 return err;
5712
5713 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5714 return -ERANGE;
5715 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5716 return -ERANGE;
5717 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5718 return -ERANGE;
5719
5720 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5721 NL80211_KEK_LEN);
5722 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5723 NL80211_KCK_LEN);
5724 memcpy(rekey_data.replay_ctr,
5725 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5726 NL80211_REPLAY_CTR_LEN);
5727
5728 wdev_lock(wdev);
5729 if (!wdev->current_bss) {
5730 err = -ENOTCONN;
5731 goto out;
5732 }
5733
5734 if (!rdev->ops->set_rekey_data) {
5735 err = -EOPNOTSUPP;
5736 goto out;
5737 }
5738
5739 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5740 out:
5741 wdev_unlock(wdev);
5742 return err;
5743}
5744
Johannes Berg4c476992010-10-04 21:36:35 +02005745#define NL80211_FLAG_NEED_WIPHY 0x01
5746#define NL80211_FLAG_NEED_NETDEV 0x02
5747#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005748#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5749#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5750 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005751
5752static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5753 struct genl_info *info)
5754{
5755 struct cfg80211_registered_device *rdev;
5756 struct net_device *dev;
5757 int err;
5758 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5759
5760 if (rtnl)
5761 rtnl_lock();
5762
5763 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5764 rdev = cfg80211_get_dev_from_info(info);
5765 if (IS_ERR(rdev)) {
5766 if (rtnl)
5767 rtnl_unlock();
5768 return PTR_ERR(rdev);
5769 }
5770 info->user_ptr[0] = rdev;
5771 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5772 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5773 if (err) {
5774 if (rtnl)
5775 rtnl_unlock();
5776 return err;
5777 }
Johannes Berg41265712010-10-04 21:14:05 +02005778 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5779 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005780 cfg80211_unlock_rdev(rdev);
5781 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005782 if (rtnl)
5783 rtnl_unlock();
5784 return -ENETDOWN;
5785 }
Johannes Berg4c476992010-10-04 21:36:35 +02005786 info->user_ptr[0] = rdev;
5787 info->user_ptr[1] = dev;
5788 }
5789
5790 return 0;
5791}
5792
5793static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5794 struct genl_info *info)
5795{
5796 if (info->user_ptr[0])
5797 cfg80211_unlock_rdev(info->user_ptr[0]);
5798 if (info->user_ptr[1])
5799 dev_put(info->user_ptr[1]);
5800 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5801 rtnl_unlock();
5802}
5803
Johannes Berg55682962007-09-20 13:09:35 -04005804static struct genl_ops nl80211_ops[] = {
5805 {
5806 .cmd = NL80211_CMD_GET_WIPHY,
5807 .doit = nl80211_get_wiphy,
5808 .dumpit = nl80211_dump_wiphy,
5809 .policy = nl80211_policy,
5810 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005811 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005812 },
5813 {
5814 .cmd = NL80211_CMD_SET_WIPHY,
5815 .doit = nl80211_set_wiphy,
5816 .policy = nl80211_policy,
5817 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005818 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005819 },
5820 {
5821 .cmd = NL80211_CMD_GET_INTERFACE,
5822 .doit = nl80211_get_interface,
5823 .dumpit = nl80211_dump_interface,
5824 .policy = nl80211_policy,
5825 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005826 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005827 },
5828 {
5829 .cmd = NL80211_CMD_SET_INTERFACE,
5830 .doit = nl80211_set_interface,
5831 .policy = nl80211_policy,
5832 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005833 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5834 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005835 },
5836 {
5837 .cmd = NL80211_CMD_NEW_INTERFACE,
5838 .doit = nl80211_new_interface,
5839 .policy = nl80211_policy,
5840 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005841 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5842 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005843 },
5844 {
5845 .cmd = NL80211_CMD_DEL_INTERFACE,
5846 .doit = nl80211_del_interface,
5847 .policy = nl80211_policy,
5848 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005849 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5850 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005851 },
Johannes Berg41ade002007-12-19 02:03:29 +01005852 {
5853 .cmd = NL80211_CMD_GET_KEY,
5854 .doit = nl80211_get_key,
5855 .policy = nl80211_policy,
5856 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005857 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5858 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005859 },
5860 {
5861 .cmd = NL80211_CMD_SET_KEY,
5862 .doit = nl80211_set_key,
5863 .policy = nl80211_policy,
5864 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005866 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005867 },
5868 {
5869 .cmd = NL80211_CMD_NEW_KEY,
5870 .doit = nl80211_new_key,
5871 .policy = nl80211_policy,
5872 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005873 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005874 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005875 },
5876 {
5877 .cmd = NL80211_CMD_DEL_KEY,
5878 .doit = nl80211_del_key,
5879 .policy = nl80211_policy,
5880 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005881 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005882 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005883 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005884 {
5885 .cmd = NL80211_CMD_SET_BEACON,
5886 .policy = nl80211_policy,
5887 .flags = GENL_ADMIN_PERM,
5888 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5890 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005891 },
5892 {
5893 .cmd = NL80211_CMD_NEW_BEACON,
5894 .policy = nl80211_policy,
5895 .flags = GENL_ADMIN_PERM,
5896 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005897 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5898 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005899 },
5900 {
5901 .cmd = NL80211_CMD_DEL_BEACON,
5902 .policy = nl80211_policy,
5903 .flags = GENL_ADMIN_PERM,
5904 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005905 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5906 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005907 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005908 {
5909 .cmd = NL80211_CMD_GET_STATION,
5910 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005911 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005912 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005913 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5914 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005915 },
5916 {
5917 .cmd = NL80211_CMD_SET_STATION,
5918 .doit = nl80211_set_station,
5919 .policy = nl80211_policy,
5920 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005921 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5922 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005923 },
5924 {
5925 .cmd = NL80211_CMD_NEW_STATION,
5926 .doit = nl80211_new_station,
5927 .policy = nl80211_policy,
5928 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005929 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005930 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005931 },
5932 {
5933 .cmd = NL80211_CMD_DEL_STATION,
5934 .doit = nl80211_del_station,
5935 .policy = nl80211_policy,
5936 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005937 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5938 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005939 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005940 {
5941 .cmd = NL80211_CMD_GET_MPATH,
5942 .doit = nl80211_get_mpath,
5943 .dumpit = nl80211_dump_mpath,
5944 .policy = nl80211_policy,
5945 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005947 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005948 },
5949 {
5950 .cmd = NL80211_CMD_SET_MPATH,
5951 .doit = nl80211_set_mpath,
5952 .policy = nl80211_policy,
5953 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005955 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005956 },
5957 {
5958 .cmd = NL80211_CMD_NEW_MPATH,
5959 .doit = nl80211_new_mpath,
5960 .policy = nl80211_policy,
5961 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005962 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005963 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005964 },
5965 {
5966 .cmd = NL80211_CMD_DEL_MPATH,
5967 .doit = nl80211_del_mpath,
5968 .policy = nl80211_policy,
5969 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005970 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5971 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005972 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005973 {
5974 .cmd = NL80211_CMD_SET_BSS,
5975 .doit = nl80211_set_bss,
5976 .policy = nl80211_policy,
5977 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005978 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5979 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005980 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005981 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005982 .cmd = NL80211_CMD_GET_REG,
5983 .doit = nl80211_get_reg,
5984 .policy = nl80211_policy,
5985 /* can be retrieved by unprivileged users */
5986 },
5987 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005988 .cmd = NL80211_CMD_SET_REG,
5989 .doit = nl80211_set_reg,
5990 .policy = nl80211_policy,
5991 .flags = GENL_ADMIN_PERM,
5992 },
5993 {
5994 .cmd = NL80211_CMD_REQ_SET_REG,
5995 .doit = nl80211_req_set_reg,
5996 .policy = nl80211_policy,
5997 .flags = GENL_ADMIN_PERM,
5998 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005999 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006000 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6001 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006002 .policy = nl80211_policy,
6003 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006004 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6005 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006006 },
6007 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006008 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6009 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006010 .policy = nl80211_policy,
6011 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006012 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006013 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006014 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006015 {
Johannes Berg2a519312009-02-10 21:25:55 +01006016 .cmd = NL80211_CMD_TRIGGER_SCAN,
6017 .doit = nl80211_trigger_scan,
6018 .policy = nl80211_policy,
6019 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006020 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006021 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006022 },
6023 {
6024 .cmd = NL80211_CMD_GET_SCAN,
6025 .policy = nl80211_policy,
6026 .dumpit = nl80211_dump_scan,
6027 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006028 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006029 .cmd = NL80211_CMD_START_SCHED_SCAN,
6030 .doit = nl80211_start_sched_scan,
6031 .policy = nl80211_policy,
6032 .flags = GENL_ADMIN_PERM,
6033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6034 NL80211_FLAG_NEED_RTNL,
6035 },
6036 {
6037 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6038 .doit = nl80211_stop_sched_scan,
6039 .policy = nl80211_policy,
6040 .flags = GENL_ADMIN_PERM,
6041 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6042 NL80211_FLAG_NEED_RTNL,
6043 },
6044 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006045 .cmd = NL80211_CMD_AUTHENTICATE,
6046 .doit = nl80211_authenticate,
6047 .policy = nl80211_policy,
6048 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006049 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006050 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006051 },
6052 {
6053 .cmd = NL80211_CMD_ASSOCIATE,
6054 .doit = nl80211_associate,
6055 .policy = nl80211_policy,
6056 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006058 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006059 },
6060 {
6061 .cmd = NL80211_CMD_DEAUTHENTICATE,
6062 .doit = nl80211_deauthenticate,
6063 .policy = nl80211_policy,
6064 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006066 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006067 },
6068 {
6069 .cmd = NL80211_CMD_DISASSOCIATE,
6070 .doit = nl80211_disassociate,
6071 .policy = nl80211_policy,
6072 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006073 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006074 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006075 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006076 {
6077 .cmd = NL80211_CMD_JOIN_IBSS,
6078 .doit = nl80211_join_ibss,
6079 .policy = nl80211_policy,
6080 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006081 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006082 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006083 },
6084 {
6085 .cmd = NL80211_CMD_LEAVE_IBSS,
6086 .doit = nl80211_leave_ibss,
6087 .policy = nl80211_policy,
6088 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006089 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006090 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006091 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006092#ifdef CONFIG_NL80211_TESTMODE
6093 {
6094 .cmd = NL80211_CMD_TESTMODE,
6095 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006096 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006097 .policy = nl80211_policy,
6098 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006099 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6100 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006101 },
6102#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006103 {
6104 .cmd = NL80211_CMD_CONNECT,
6105 .doit = nl80211_connect,
6106 .policy = nl80211_policy,
6107 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006108 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006109 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006110 },
6111 {
6112 .cmd = NL80211_CMD_DISCONNECT,
6113 .doit = nl80211_disconnect,
6114 .policy = nl80211_policy,
6115 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006116 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006117 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006118 },
Johannes Berg463d0182009-07-14 00:33:35 +02006119 {
6120 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6121 .doit = nl80211_wiphy_netns,
6122 .policy = nl80211_policy,
6123 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006124 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6125 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006126 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006127 {
6128 .cmd = NL80211_CMD_GET_SURVEY,
6129 .policy = nl80211_policy,
6130 .dumpit = nl80211_dump_survey,
6131 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006132 {
6133 .cmd = NL80211_CMD_SET_PMKSA,
6134 .doit = nl80211_setdel_pmksa,
6135 .policy = nl80211_policy,
6136 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006137 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6138 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006139 },
6140 {
6141 .cmd = NL80211_CMD_DEL_PMKSA,
6142 .doit = nl80211_setdel_pmksa,
6143 .policy = nl80211_policy,
6144 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006145 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6146 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006147 },
6148 {
6149 .cmd = NL80211_CMD_FLUSH_PMKSA,
6150 .doit = nl80211_flush_pmksa,
6151 .policy = nl80211_policy,
6152 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006153 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6154 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006155 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006156 {
6157 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6158 .doit = nl80211_remain_on_channel,
6159 .policy = nl80211_policy,
6160 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006161 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006162 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006163 },
6164 {
6165 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6166 .doit = nl80211_cancel_remain_on_channel,
6167 .policy = nl80211_policy,
6168 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006169 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006170 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006171 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006172 {
6173 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6174 .doit = nl80211_set_tx_bitrate_mask,
6175 .policy = nl80211_policy,
6176 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006177 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6178 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006179 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006180 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006181 .cmd = NL80211_CMD_REGISTER_FRAME,
6182 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006183 .policy = nl80211_policy,
6184 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006185 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6186 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006187 },
6188 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006189 .cmd = NL80211_CMD_FRAME,
6190 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006191 .policy = nl80211_policy,
6192 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006193 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006194 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006195 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006196 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006197 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6198 .doit = nl80211_tx_mgmt_cancel_wait,
6199 .policy = nl80211_policy,
6200 .flags = GENL_ADMIN_PERM,
6201 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6202 NL80211_FLAG_NEED_RTNL,
6203 },
6204 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006205 .cmd = NL80211_CMD_SET_POWER_SAVE,
6206 .doit = nl80211_set_power_save,
6207 .policy = nl80211_policy,
6208 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006209 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6210 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006211 },
6212 {
6213 .cmd = NL80211_CMD_GET_POWER_SAVE,
6214 .doit = nl80211_get_power_save,
6215 .policy = nl80211_policy,
6216 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006217 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6218 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006219 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006220 {
6221 .cmd = NL80211_CMD_SET_CQM,
6222 .doit = nl80211_set_cqm,
6223 .policy = nl80211_policy,
6224 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006225 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6226 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006227 },
Johannes Bergf444de02010-05-05 15:25:02 +02006228 {
6229 .cmd = NL80211_CMD_SET_CHANNEL,
6230 .doit = nl80211_set_channel,
6231 .policy = nl80211_policy,
6232 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006233 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6234 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006235 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006236 {
6237 .cmd = NL80211_CMD_SET_WDS_PEER,
6238 .doit = nl80211_set_wds_peer,
6239 .policy = nl80211_policy,
6240 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006241 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6242 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006243 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006244 {
6245 .cmd = NL80211_CMD_JOIN_MESH,
6246 .doit = nl80211_join_mesh,
6247 .policy = nl80211_policy,
6248 .flags = GENL_ADMIN_PERM,
6249 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6250 NL80211_FLAG_NEED_RTNL,
6251 },
6252 {
6253 .cmd = NL80211_CMD_LEAVE_MESH,
6254 .doit = nl80211_leave_mesh,
6255 .policy = nl80211_policy,
6256 .flags = GENL_ADMIN_PERM,
6257 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6258 NL80211_FLAG_NEED_RTNL,
6259 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006260 {
6261 .cmd = NL80211_CMD_GET_WOWLAN,
6262 .doit = nl80211_get_wowlan,
6263 .policy = nl80211_policy,
6264 /* can be retrieved by unprivileged users */
6265 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6266 NL80211_FLAG_NEED_RTNL,
6267 },
6268 {
6269 .cmd = NL80211_CMD_SET_WOWLAN,
6270 .doit = nl80211_set_wowlan,
6271 .policy = nl80211_policy,
6272 .flags = GENL_ADMIN_PERM,
6273 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6274 NL80211_FLAG_NEED_RTNL,
6275 },
Johannes Berge5497d72011-07-05 16:35:40 +02006276 {
6277 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6278 .doit = nl80211_set_rekey_data,
6279 .policy = nl80211_policy,
6280 .flags = GENL_ADMIN_PERM,
6281 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6282 NL80211_FLAG_NEED_RTNL,
6283 },
Johannes Berg55682962007-09-20 13:09:35 -04006284};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006285
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006286static struct genl_multicast_group nl80211_mlme_mcgrp = {
6287 .name = "mlme",
6288};
Johannes Berg55682962007-09-20 13:09:35 -04006289
6290/* multicast groups */
6291static struct genl_multicast_group nl80211_config_mcgrp = {
6292 .name = "config",
6293};
Johannes Berg2a519312009-02-10 21:25:55 +01006294static struct genl_multicast_group nl80211_scan_mcgrp = {
6295 .name = "scan",
6296};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006297static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6298 .name = "regulatory",
6299};
Johannes Berg55682962007-09-20 13:09:35 -04006300
6301/* notification functions */
6302
6303void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6304{
6305 struct sk_buff *msg;
6306
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006307 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006308 if (!msg)
6309 return;
6310
6311 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6312 nlmsg_free(msg);
6313 return;
6314 }
6315
Johannes Berg463d0182009-07-14 00:33:35 +02006316 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6317 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006318}
6319
Johannes Berg362a4152009-05-24 16:43:15 +02006320static int nl80211_add_scan_req(struct sk_buff *msg,
6321 struct cfg80211_registered_device *rdev)
6322{
6323 struct cfg80211_scan_request *req = rdev->scan_req;
6324 struct nlattr *nest;
6325 int i;
6326
Johannes Berg667503d2009-07-07 03:56:11 +02006327 ASSERT_RDEV_LOCK(rdev);
6328
Johannes Berg362a4152009-05-24 16:43:15 +02006329 if (WARN_ON(!req))
6330 return 0;
6331
6332 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6333 if (!nest)
6334 goto nla_put_failure;
6335 for (i = 0; i < req->n_ssids; i++)
6336 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6337 nla_nest_end(msg, nest);
6338
6339 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6340 if (!nest)
6341 goto nla_put_failure;
6342 for (i = 0; i < req->n_channels; i++)
6343 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6344 nla_nest_end(msg, nest);
6345
6346 if (req->ie)
6347 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6348
6349 return 0;
6350 nla_put_failure:
6351 return -ENOBUFS;
6352}
6353
Johannes Berga538e2d2009-06-16 19:56:42 +02006354static int nl80211_send_scan_msg(struct sk_buff *msg,
6355 struct cfg80211_registered_device *rdev,
6356 struct net_device *netdev,
6357 u32 pid, u32 seq, int flags,
6358 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006359{
6360 void *hdr;
6361
6362 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6363 if (!hdr)
6364 return -1;
6365
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006366 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006367 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6368
Johannes Berg362a4152009-05-24 16:43:15 +02006369 /* ignore errors and send incomplete event anyway */
6370 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006371
6372 return genlmsg_end(msg, hdr);
6373
6374 nla_put_failure:
6375 genlmsg_cancel(msg, hdr);
6376 return -EMSGSIZE;
6377}
6378
Luciano Coelho807f8a82011-05-11 17:09:35 +03006379static int
6380nl80211_send_sched_scan_msg(struct sk_buff *msg,
6381 struct cfg80211_registered_device *rdev,
6382 struct net_device *netdev,
6383 u32 pid, u32 seq, int flags, u32 cmd)
6384{
6385 void *hdr;
6386
6387 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6388 if (!hdr)
6389 return -1;
6390
6391 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6392 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6393
6394 return genlmsg_end(msg, hdr);
6395
6396 nla_put_failure:
6397 genlmsg_cancel(msg, hdr);
6398 return -EMSGSIZE;
6399}
6400
Johannes Berga538e2d2009-06-16 19:56:42 +02006401void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6402 struct net_device *netdev)
6403{
6404 struct sk_buff *msg;
6405
6406 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6407 if (!msg)
6408 return;
6409
6410 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6411 NL80211_CMD_TRIGGER_SCAN) < 0) {
6412 nlmsg_free(msg);
6413 return;
6414 }
6415
Johannes Berg463d0182009-07-14 00:33:35 +02006416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6417 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006418}
6419
Johannes Berg2a519312009-02-10 21:25:55 +01006420void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6421 struct net_device *netdev)
6422{
6423 struct sk_buff *msg;
6424
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006426 if (!msg)
6427 return;
6428
Johannes Berga538e2d2009-06-16 19:56:42 +02006429 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6430 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006431 nlmsg_free(msg);
6432 return;
6433 }
6434
Johannes Berg463d0182009-07-14 00:33:35 +02006435 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6436 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006437}
6438
6439void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6440 struct net_device *netdev)
6441{
6442 struct sk_buff *msg;
6443
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006445 if (!msg)
6446 return;
6447
Johannes Berga538e2d2009-06-16 19:56:42 +02006448 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6449 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006450 nlmsg_free(msg);
6451 return;
6452 }
6453
Johannes Berg463d0182009-07-14 00:33:35 +02006454 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6455 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006456}
6457
Luciano Coelho807f8a82011-05-11 17:09:35 +03006458void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6459 struct net_device *netdev)
6460{
6461 struct sk_buff *msg;
6462
6463 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6464 if (!msg)
6465 return;
6466
6467 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6468 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6469 nlmsg_free(msg);
6470 return;
6471 }
6472
6473 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6474 nl80211_scan_mcgrp.id, GFP_KERNEL);
6475}
6476
6477void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6478 struct net_device *netdev, u32 cmd)
6479{
6480 struct sk_buff *msg;
6481
6482 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6483 if (!msg)
6484 return;
6485
6486 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6487 nlmsg_free(msg);
6488 return;
6489 }
6490
6491 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6492 nl80211_scan_mcgrp.id, GFP_KERNEL);
6493}
6494
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006495/*
6496 * This can happen on global regulatory changes or device specific settings
6497 * based on custom world regulatory domains.
6498 */
6499void nl80211_send_reg_change_event(struct regulatory_request *request)
6500{
6501 struct sk_buff *msg;
6502 void *hdr;
6503
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006504 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006505 if (!msg)
6506 return;
6507
6508 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6509 if (!hdr) {
6510 nlmsg_free(msg);
6511 return;
6512 }
6513
6514 /* Userspace can always count this one always being set */
6515 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6516
6517 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6518 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6519 NL80211_REGDOM_TYPE_WORLD);
6520 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6521 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6522 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6523 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6524 request->intersect)
6525 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6526 NL80211_REGDOM_TYPE_INTERSECTION);
6527 else {
6528 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6529 NL80211_REGDOM_TYPE_COUNTRY);
6530 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6531 }
6532
6533 if (wiphy_idx_valid(request->wiphy_idx))
6534 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6535
6536 if (genlmsg_end(msg, hdr) < 0) {
6537 nlmsg_free(msg);
6538 return;
6539 }
6540
Johannes Bergbc43b282009-07-25 10:54:13 +02006541 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006542 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006543 GFP_ATOMIC);
6544 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006545
6546 return;
6547
6548nla_put_failure:
6549 genlmsg_cancel(msg, hdr);
6550 nlmsg_free(msg);
6551}
6552
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006553static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6554 struct net_device *netdev,
6555 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006556 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006557{
6558 struct sk_buff *msg;
6559 void *hdr;
6560
Johannes Berge6d6e342009-07-01 21:26:47 +02006561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006562 if (!msg)
6563 return;
6564
6565 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6566 if (!hdr) {
6567 nlmsg_free(msg);
6568 return;
6569 }
6570
6571 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6572 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6573 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6574
6575 if (genlmsg_end(msg, hdr) < 0) {
6576 nlmsg_free(msg);
6577 return;
6578 }
6579
Johannes Berg463d0182009-07-14 00:33:35 +02006580 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6581 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006582 return;
6583
6584 nla_put_failure:
6585 genlmsg_cancel(msg, hdr);
6586 nlmsg_free(msg);
6587}
6588
6589void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006590 struct net_device *netdev, const u8 *buf,
6591 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006592{
6593 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006594 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006595}
6596
6597void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6598 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006599 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006600{
Johannes Berge6d6e342009-07-01 21:26:47 +02006601 nl80211_send_mlme_event(rdev, netdev, buf, len,
6602 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006603}
6604
Jouni Malinen53b46b82009-03-27 20:53:56 +02006605void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006606 struct net_device *netdev, const u8 *buf,
6607 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006608{
6609 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006610 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006611}
6612
Jouni Malinen53b46b82009-03-27 20:53:56 +02006613void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6614 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006615 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006616{
6617 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006618 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006619}
6620
Jouni Malinencf4e5942010-12-16 00:52:40 +02006621void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6622 struct net_device *netdev, const u8 *buf,
6623 size_t len, gfp_t gfp)
6624{
6625 nl80211_send_mlme_event(rdev, netdev, buf, len,
6626 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6627}
6628
6629void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6630 struct net_device *netdev, const u8 *buf,
6631 size_t len, gfp_t gfp)
6632{
6633 nl80211_send_mlme_event(rdev, netdev, buf, len,
6634 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6635}
6636
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006637static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6638 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006639 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006640{
6641 struct sk_buff *msg;
6642 void *hdr;
6643
Johannes Berge6d6e342009-07-01 21:26:47 +02006644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006645 if (!msg)
6646 return;
6647
6648 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6649 if (!hdr) {
6650 nlmsg_free(msg);
6651 return;
6652 }
6653
6654 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6655 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6656 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6657 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6658
6659 if (genlmsg_end(msg, hdr) < 0) {
6660 nlmsg_free(msg);
6661 return;
6662 }
6663
Johannes Berg463d0182009-07-14 00:33:35 +02006664 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6665 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006666 return;
6667
6668 nla_put_failure:
6669 genlmsg_cancel(msg, hdr);
6670 nlmsg_free(msg);
6671}
6672
6673void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006674 struct net_device *netdev, const u8 *addr,
6675 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006676{
6677 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006678 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006679}
6680
6681void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006682 struct net_device *netdev, const u8 *addr,
6683 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006684{
Johannes Berge6d6e342009-07-01 21:26:47 +02006685 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6686 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006687}
6688
Samuel Ortizb23aa672009-07-01 21:26:54 +02006689void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6690 struct net_device *netdev, const u8 *bssid,
6691 const u8 *req_ie, size_t req_ie_len,
6692 const u8 *resp_ie, size_t resp_ie_len,
6693 u16 status, gfp_t gfp)
6694{
6695 struct sk_buff *msg;
6696 void *hdr;
6697
6698 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6699 if (!msg)
6700 return;
6701
6702 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6703 if (!hdr) {
6704 nlmsg_free(msg);
6705 return;
6706 }
6707
6708 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6709 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6710 if (bssid)
6711 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6712 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6713 if (req_ie)
6714 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6715 if (resp_ie)
6716 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6717
6718 if (genlmsg_end(msg, hdr) < 0) {
6719 nlmsg_free(msg);
6720 return;
6721 }
6722
Johannes Berg463d0182009-07-14 00:33:35 +02006723 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6724 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006725 return;
6726
6727 nla_put_failure:
6728 genlmsg_cancel(msg, hdr);
6729 nlmsg_free(msg);
6730
6731}
6732
6733void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6734 struct net_device *netdev, const u8 *bssid,
6735 const u8 *req_ie, size_t req_ie_len,
6736 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6737{
6738 struct sk_buff *msg;
6739 void *hdr;
6740
6741 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6742 if (!msg)
6743 return;
6744
6745 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6746 if (!hdr) {
6747 nlmsg_free(msg);
6748 return;
6749 }
6750
6751 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6752 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6753 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6754 if (req_ie)
6755 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6756 if (resp_ie)
6757 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6758
6759 if (genlmsg_end(msg, hdr) < 0) {
6760 nlmsg_free(msg);
6761 return;
6762 }
6763
Johannes Berg463d0182009-07-14 00:33:35 +02006764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6765 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006766 return;
6767
6768 nla_put_failure:
6769 genlmsg_cancel(msg, hdr);
6770 nlmsg_free(msg);
6771
6772}
6773
6774void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6775 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006776 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006777{
6778 struct sk_buff *msg;
6779 void *hdr;
6780
Johannes Berg667503d2009-07-07 03:56:11 +02006781 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006782 if (!msg)
6783 return;
6784
6785 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6786 if (!hdr) {
6787 nlmsg_free(msg);
6788 return;
6789 }
6790
6791 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6793 if (from_ap && reason)
6794 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6795 if (from_ap)
6796 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6797 if (ie)
6798 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6799
6800 if (genlmsg_end(msg, hdr) < 0) {
6801 nlmsg_free(msg);
6802 return;
6803 }
6804
Johannes Berg463d0182009-07-14 00:33:35 +02006805 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6806 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006807 return;
6808
6809 nla_put_failure:
6810 genlmsg_cancel(msg, hdr);
6811 nlmsg_free(msg);
6812
6813}
6814
Johannes Berg04a773a2009-04-19 21:24:32 +02006815void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6816 struct net_device *netdev, const u8 *bssid,
6817 gfp_t gfp)
6818{
6819 struct sk_buff *msg;
6820 void *hdr;
6821
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006823 if (!msg)
6824 return;
6825
6826 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6827 if (!hdr) {
6828 nlmsg_free(msg);
6829 return;
6830 }
6831
6832 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6833 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6834 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6835
6836 if (genlmsg_end(msg, hdr) < 0) {
6837 nlmsg_free(msg);
6838 return;
6839 }
6840
Johannes Berg463d0182009-07-14 00:33:35 +02006841 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6842 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006843 return;
6844
6845 nla_put_failure:
6846 genlmsg_cancel(msg, hdr);
6847 nlmsg_free(msg);
6848}
6849
Javier Cardonac93b5e72011-04-07 15:08:34 -07006850void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6851 struct net_device *netdev,
6852 const u8 *macaddr, const u8* ie, u8 ie_len,
6853 gfp_t gfp)
6854{
6855 struct sk_buff *msg;
6856 void *hdr;
6857
6858 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6859 if (!msg)
6860 return;
6861
6862 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6863 if (!hdr) {
6864 nlmsg_free(msg);
6865 return;
6866 }
6867
6868 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6870 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6871 if (ie_len && ie)
6872 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6873
6874 if (genlmsg_end(msg, hdr) < 0) {
6875 nlmsg_free(msg);
6876 return;
6877 }
6878
6879 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6880 nl80211_mlme_mcgrp.id, gfp);
6881 return;
6882
6883 nla_put_failure:
6884 genlmsg_cancel(msg, hdr);
6885 nlmsg_free(msg);
6886}
6887
Jouni Malinena3b8b052009-03-27 21:59:49 +02006888void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6889 struct net_device *netdev, const u8 *addr,
6890 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006891 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006892{
6893 struct sk_buff *msg;
6894 void *hdr;
6895
Johannes Berge6d6e342009-07-01 21:26:47 +02006896 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006897 if (!msg)
6898 return;
6899
6900 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6901 if (!hdr) {
6902 nlmsg_free(msg);
6903 return;
6904 }
6905
6906 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6907 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6908 if (addr)
6909 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6910 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03006911 if (key_id != -1)
6912 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006913 if (tsc)
6914 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6915
6916 if (genlmsg_end(msg, hdr) < 0) {
6917 nlmsg_free(msg);
6918 return;
6919 }
6920
Johannes Berg463d0182009-07-14 00:33:35 +02006921 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6922 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006923 return;
6924
6925 nla_put_failure:
6926 genlmsg_cancel(msg, hdr);
6927 nlmsg_free(msg);
6928}
6929
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006930void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6931 struct ieee80211_channel *channel_before,
6932 struct ieee80211_channel *channel_after)
6933{
6934 struct sk_buff *msg;
6935 void *hdr;
6936 struct nlattr *nl_freq;
6937
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006938 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006939 if (!msg)
6940 return;
6941
6942 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6943 if (!hdr) {
6944 nlmsg_free(msg);
6945 return;
6946 }
6947
6948 /*
6949 * Since we are applying the beacon hint to a wiphy we know its
6950 * wiphy_idx is valid
6951 */
6952 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6953
6954 /* Before */
6955 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6956 if (!nl_freq)
6957 goto nla_put_failure;
6958 if (nl80211_msg_put_channel(msg, channel_before))
6959 goto nla_put_failure;
6960 nla_nest_end(msg, nl_freq);
6961
6962 /* After */
6963 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6964 if (!nl_freq)
6965 goto nla_put_failure;
6966 if (nl80211_msg_put_channel(msg, channel_after))
6967 goto nla_put_failure;
6968 nla_nest_end(msg, nl_freq);
6969
6970 if (genlmsg_end(msg, hdr) < 0) {
6971 nlmsg_free(msg);
6972 return;
6973 }
6974
Johannes Berg463d0182009-07-14 00:33:35 +02006975 rcu_read_lock();
6976 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6977 GFP_ATOMIC);
6978 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006979
6980 return;
6981
6982nla_put_failure:
6983 genlmsg_cancel(msg, hdr);
6984 nlmsg_free(msg);
6985}
6986
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006987static void nl80211_send_remain_on_chan_event(
6988 int cmd, struct cfg80211_registered_device *rdev,
6989 struct net_device *netdev, u64 cookie,
6990 struct ieee80211_channel *chan,
6991 enum nl80211_channel_type channel_type,
6992 unsigned int duration, gfp_t gfp)
6993{
6994 struct sk_buff *msg;
6995 void *hdr;
6996
6997 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6998 if (!msg)
6999 return;
7000
7001 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7002 if (!hdr) {
7003 nlmsg_free(msg);
7004 return;
7005 }
7006
7007 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7009 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7010 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7011 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7012
7013 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7014 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7015
7016 if (genlmsg_end(msg, hdr) < 0) {
7017 nlmsg_free(msg);
7018 return;
7019 }
7020
7021 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7022 nl80211_mlme_mcgrp.id, gfp);
7023 return;
7024
7025 nla_put_failure:
7026 genlmsg_cancel(msg, hdr);
7027 nlmsg_free(msg);
7028}
7029
7030void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7031 struct net_device *netdev, u64 cookie,
7032 struct ieee80211_channel *chan,
7033 enum nl80211_channel_type channel_type,
7034 unsigned int duration, gfp_t gfp)
7035{
7036 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7037 rdev, netdev, cookie, chan,
7038 channel_type, duration, gfp);
7039}
7040
7041void nl80211_send_remain_on_channel_cancel(
7042 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7043 u64 cookie, struct ieee80211_channel *chan,
7044 enum nl80211_channel_type channel_type, gfp_t gfp)
7045{
7046 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7047 rdev, netdev, cookie, chan,
7048 channel_type, 0, gfp);
7049}
7050
Johannes Berg98b62182009-12-23 13:15:44 +01007051void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7052 struct net_device *dev, const u8 *mac_addr,
7053 struct station_info *sinfo, gfp_t gfp)
7054{
7055 struct sk_buff *msg;
7056
7057 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7058 if (!msg)
7059 return;
7060
7061 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7062 nlmsg_free(msg);
7063 return;
7064 }
7065
7066 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7067 nl80211_mlme_mcgrp.id, gfp);
7068}
7069
Jouni Malinenec15e682011-03-23 15:29:52 +02007070void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7071 struct net_device *dev, const u8 *mac_addr,
7072 gfp_t gfp)
7073{
7074 struct sk_buff *msg;
7075 void *hdr;
7076
7077 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7078 if (!msg)
7079 return;
7080
7081 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7082 if (!hdr) {
7083 nlmsg_free(msg);
7084 return;
7085 }
7086
7087 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7088 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7089
7090 if (genlmsg_end(msg, hdr) < 0) {
7091 nlmsg_free(msg);
7092 return;
7093 }
7094
7095 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7096 nl80211_mlme_mcgrp.id, gfp);
7097 return;
7098
7099 nla_put_failure:
7100 genlmsg_cancel(msg, hdr);
7101 nlmsg_free(msg);
7102}
7103
Johannes Berg2e161f72010-08-12 15:38:38 +02007104int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7105 struct net_device *netdev, u32 nlpid,
7106 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007107{
7108 struct sk_buff *msg;
7109 void *hdr;
7110 int err;
7111
7112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7113 if (!msg)
7114 return -ENOMEM;
7115
Johannes Berg2e161f72010-08-12 15:38:38 +02007116 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007117 if (!hdr) {
7118 nlmsg_free(msg);
7119 return -ENOMEM;
7120 }
7121
7122 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7123 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7124 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7125 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7126
7127 err = genlmsg_end(msg, hdr);
7128 if (err < 0) {
7129 nlmsg_free(msg);
7130 return err;
7131 }
7132
7133 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7134 if (err < 0)
7135 return err;
7136 return 0;
7137
7138 nla_put_failure:
7139 genlmsg_cancel(msg, hdr);
7140 nlmsg_free(msg);
7141 return -ENOBUFS;
7142}
7143
Johannes Berg2e161f72010-08-12 15:38:38 +02007144void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7145 struct net_device *netdev, u64 cookie,
7146 const u8 *buf, size_t len, bool ack,
7147 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007148{
7149 struct sk_buff *msg;
7150 void *hdr;
7151
7152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7153 if (!msg)
7154 return;
7155
Johannes Berg2e161f72010-08-12 15:38:38 +02007156 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007157 if (!hdr) {
7158 nlmsg_free(msg);
7159 return;
7160 }
7161
7162 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7163 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7164 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7165 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7166 if (ack)
7167 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7168
7169 if (genlmsg_end(msg, hdr) < 0) {
7170 nlmsg_free(msg);
7171 return;
7172 }
7173
7174 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7175 return;
7176
7177 nla_put_failure:
7178 genlmsg_cancel(msg, hdr);
7179 nlmsg_free(msg);
7180}
7181
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007182void
7183nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7184 struct net_device *netdev,
7185 enum nl80211_cqm_rssi_threshold_event rssi_event,
7186 gfp_t gfp)
7187{
7188 struct sk_buff *msg;
7189 struct nlattr *pinfoattr;
7190 void *hdr;
7191
7192 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7193 if (!msg)
7194 return;
7195
7196 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7197 if (!hdr) {
7198 nlmsg_free(msg);
7199 return;
7200 }
7201
7202 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7203 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7204
7205 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7206 if (!pinfoattr)
7207 goto nla_put_failure;
7208
7209 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7210 rssi_event);
7211
7212 nla_nest_end(msg, pinfoattr);
7213
7214 if (genlmsg_end(msg, hdr) < 0) {
7215 nlmsg_free(msg);
7216 return;
7217 }
7218
7219 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7220 nl80211_mlme_mcgrp.id, gfp);
7221 return;
7222
7223 nla_put_failure:
7224 genlmsg_cancel(msg, hdr);
7225 nlmsg_free(msg);
7226}
7227
Johannes Berge5497d72011-07-05 16:35:40 +02007228void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7229 struct net_device *netdev, const u8 *bssid,
7230 const u8 *replay_ctr, gfp_t gfp)
7231{
7232 struct sk_buff *msg;
7233 struct nlattr *rekey_attr;
7234 void *hdr;
7235
7236 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7237 if (!msg)
7238 return;
7239
7240 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7241 if (!hdr) {
7242 nlmsg_free(msg);
7243 return;
7244 }
7245
7246 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7247 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7248 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7249
7250 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7251 if (!rekey_attr)
7252 goto nla_put_failure;
7253
7254 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7255 NL80211_REPLAY_CTR_LEN, replay_ctr);
7256
7257 nla_nest_end(msg, rekey_attr);
7258
7259 if (genlmsg_end(msg, hdr) < 0) {
7260 nlmsg_free(msg);
7261 return;
7262 }
7263
7264 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7265 nl80211_mlme_mcgrp.id, gfp);
7266 return;
7267
7268 nla_put_failure:
7269 genlmsg_cancel(msg, hdr);
7270 nlmsg_free(msg);
7271}
7272
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007273void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7274 struct net_device *netdev, int index,
7275 const u8 *bssid, bool preauth, gfp_t gfp)
7276{
7277 struct sk_buff *msg;
7278 struct nlattr *attr;
7279 void *hdr;
7280
7281 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7282 if (!msg)
7283 return;
7284
7285 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7286 if (!hdr) {
7287 nlmsg_free(msg);
7288 return;
7289 }
7290
7291 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7292 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7293
7294 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7295 if (!attr)
7296 goto nla_put_failure;
7297
7298 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7299 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7300 if (preauth)
7301 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7302
7303 nla_nest_end(msg, attr);
7304
7305 if (genlmsg_end(msg, hdr) < 0) {
7306 nlmsg_free(msg);
7307 return;
7308 }
7309
7310 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7311 nl80211_mlme_mcgrp.id, gfp);
7312 return;
7313
7314 nla_put_failure:
7315 genlmsg_cancel(msg, hdr);
7316 nlmsg_free(msg);
7317}
7318
Johannes Bergc063dbf2010-11-24 08:10:05 +01007319void
7320nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7321 struct net_device *netdev, const u8 *peer,
7322 u32 num_packets, gfp_t gfp)
7323{
7324 struct sk_buff *msg;
7325 struct nlattr *pinfoattr;
7326 void *hdr;
7327
7328 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7329 if (!msg)
7330 return;
7331
7332 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7333 if (!hdr) {
7334 nlmsg_free(msg);
7335 return;
7336 }
7337
7338 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7339 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7340 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7341
7342 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7343 if (!pinfoattr)
7344 goto nla_put_failure;
7345
7346 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7347
7348 nla_nest_end(msg, pinfoattr);
7349
7350 if (genlmsg_end(msg, hdr) < 0) {
7351 nlmsg_free(msg);
7352 return;
7353 }
7354
7355 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7356 nl80211_mlme_mcgrp.id, gfp);
7357 return;
7358
7359 nla_put_failure:
7360 genlmsg_cancel(msg, hdr);
7361 nlmsg_free(msg);
7362}
7363
Jouni Malinen026331c2010-02-15 12:53:10 +02007364static int nl80211_netlink_notify(struct notifier_block * nb,
7365 unsigned long state,
7366 void *_notify)
7367{
7368 struct netlink_notify *notify = _notify;
7369 struct cfg80211_registered_device *rdev;
7370 struct wireless_dev *wdev;
7371
7372 if (state != NETLINK_URELEASE)
7373 return NOTIFY_DONE;
7374
7375 rcu_read_lock();
7376
7377 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7378 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007379 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007380
7381 rcu_read_unlock();
7382
7383 return NOTIFY_DONE;
7384}
7385
7386static struct notifier_block nl80211_netlink_notifier = {
7387 .notifier_call = nl80211_netlink_notify,
7388};
7389
Johannes Berg55682962007-09-20 13:09:35 -04007390/* initialisation/exit functions */
7391
7392int nl80211_init(void)
7393{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007394 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007395
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007396 err = genl_register_family_with_ops(&nl80211_fam,
7397 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007398 if (err)
7399 return err;
7400
Johannes Berg55682962007-09-20 13:09:35 -04007401 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7402 if (err)
7403 goto err_out;
7404
Johannes Berg2a519312009-02-10 21:25:55 +01007405 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7406 if (err)
7407 goto err_out;
7408
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007409 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7410 if (err)
7411 goto err_out;
7412
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007413 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7414 if (err)
7415 goto err_out;
7416
Johannes Bergaff89a92009-07-01 21:26:51 +02007417#ifdef CONFIG_NL80211_TESTMODE
7418 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7419 if (err)
7420 goto err_out;
7421#endif
7422
Jouni Malinen026331c2010-02-15 12:53:10 +02007423 err = netlink_register_notifier(&nl80211_netlink_notifier);
7424 if (err)
7425 goto err_out;
7426
Johannes Berg55682962007-09-20 13:09:35 -04007427 return 0;
7428 err_out:
7429 genl_unregister_family(&nl80211_fam);
7430 return err;
7431}
7432
7433void nl80211_exit(void)
7434{
Jouni Malinen026331c2010-02-15 12:53:10 +02007435 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007436 genl_unregister_family(&nl80211_fam);
7437}