blob: 4c1eb9472ddb322793f382035b76d760862e0f3f [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 Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f782010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530207 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530208 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400209};
210
Johannes Berge31b8212010-10-05 19:39:30 +0200211/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000212static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200213 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200214 [NL80211_KEY_IDX] = { .type = NLA_U8 },
215 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200216 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200217 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
218 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200219 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100220 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
221};
222
223/* policy for the key default flags */
224static const struct nla_policy
225nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
226 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
227 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200228};
229
Johannes Bergff1b6e62011-05-04 15:37:28 +0200230/* policy for WoWLAN attributes */
231static const struct nla_policy
232nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
233 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
235 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200237 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200241};
242
Johannes Berge5497d72011-07-05 16:35:40 +0200243/* policy for GTK rekey offload attributes */
244static const struct nla_policy
245nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
246 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
247 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
248 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249};
250
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300251static const struct nla_policy
252nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
253 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_SSID_LEN },
255};
256
Holger Schuriga0438972009-11-11 11:30:02 +0100257/* ifidx get helper */
258static int nl80211_get_ifidx(struct netlink_callback *cb)
259{
260 int res;
261
262 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
263 nl80211_fam.attrbuf, nl80211_fam.maxattr,
264 nl80211_policy);
265 if (res)
266 return res;
267
268 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
269 return -EINVAL;
270
271 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
272 if (!res)
273 return -EINVAL;
274 return res;
275}
276
Johannes Berg67748892010-10-04 21:14:06 +0200277static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
278 struct netlink_callback *cb,
279 struct cfg80211_registered_device **rdev,
280 struct net_device **dev)
281{
282 int ifidx = cb->args[0];
283 int err;
284
285 if (!ifidx)
286 ifidx = nl80211_get_ifidx(cb);
287 if (ifidx < 0)
288 return ifidx;
289
290 cb->args[0] = ifidx;
291
292 rtnl_lock();
293
294 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
295 if (!*dev) {
296 err = -ENODEV;
297 goto out_rtnl;
298 }
299
300 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100301 if (IS_ERR(*rdev)) {
302 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200303 goto out_rtnl;
304 }
305
306 return 0;
307 out_rtnl:
308 rtnl_unlock();
309 return err;
310}
311
312static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313{
314 cfg80211_unlock_rdev(rdev);
315 rtnl_unlock();
316}
317
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100318/* IE validation */
319static bool is_valid_ie_attr(const struct nlattr *attr)
320{
321 const u8 *pos;
322 int len;
323
324 if (!attr)
325 return true;
326
327 pos = nla_data(attr);
328 len = nla_len(attr);
329
330 while (len) {
331 u8 elemlen;
332
333 if (len < 2)
334 return false;
335 len -= 2;
336
337 elemlen = pos[1];
338 if (elemlen > len)
339 return false;
340
341 len -= elemlen;
342 pos += 2 + elemlen;
343 }
344
345 return true;
346}
347
Johannes Berg55682962007-09-20 13:09:35 -0400348/* message building helper */
349static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
350 int flags, u8 cmd)
351{
352 /* since there is no private header just add the generic one */
353 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354}
355
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400356static int nl80211_msg_put_channel(struct sk_buff *msg,
357 struct ieee80211_channel *chan)
358{
359 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360 chan->center_freq);
361
362 if (chan->flags & IEEE80211_CHAN_DISABLED)
363 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
364 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
365 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
366 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
367 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
368 if (chan->flags & IEEE80211_CHAN_RADAR)
369 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
370
371 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
372 DBM_TO_MBM(chan->max_power));
373
374 return 0;
375
376 nla_put_failure:
377 return -ENOBUFS;
378}
379
Johannes Berg55682962007-09-20 13:09:35 -0400380/* netlink command implementations */
381
Johannes Bergb9454e82009-07-08 13:29:08 +0200382struct key_parse {
383 struct key_params p;
384 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200385 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200386 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100387 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200388};
389
390static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
391{
392 struct nlattr *tb[NL80211_KEY_MAX + 1];
393 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
394 nl80211_key_policy);
395 if (err)
396 return err;
397
398 k->def = !!tb[NL80211_KEY_DEFAULT];
399 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
400
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100401 if (k->def) {
402 k->def_uni = true;
403 k->def_multi = true;
404 }
405 if (k->defmgmt)
406 k->def_multi = true;
407
Johannes Bergb9454e82009-07-08 13:29:08 +0200408 if (tb[NL80211_KEY_IDX])
409 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
410
411 if (tb[NL80211_KEY_DATA]) {
412 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
413 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
414 }
415
416 if (tb[NL80211_KEY_SEQ]) {
417 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
418 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
419 }
420
421 if (tb[NL80211_KEY_CIPHER])
422 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
423
Johannes Berge31b8212010-10-05 19:39:30 +0200424 if (tb[NL80211_KEY_TYPE]) {
425 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
426 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
427 return -EINVAL;
428 }
429
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100430 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
431 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100432 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
433 tb[NL80211_KEY_DEFAULT_TYPES],
434 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100435 if (err)
436 return err;
437
438 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
439 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
440 }
441
Johannes Bergb9454e82009-07-08 13:29:08 +0200442 return 0;
443}
444
445static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
446{
447 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
448 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
449 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
450 }
451
452 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
453 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
454 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
455 }
456
457 if (info->attrs[NL80211_ATTR_KEY_IDX])
458 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
459
460 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
461 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
462
463 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
464 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
465
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100466 if (k->def) {
467 k->def_uni = true;
468 k->def_multi = true;
469 }
470 if (k->defmgmt)
471 k->def_multi = true;
472
Johannes Berge31b8212010-10-05 19:39:30 +0200473 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
474 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
475 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
476 return -EINVAL;
477 }
478
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100479 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
480 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
481 int err = nla_parse_nested(
482 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
483 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
484 nl80211_key_default_policy);
485 if (err)
486 return err;
487
488 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
489 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
490 }
491
Johannes Bergb9454e82009-07-08 13:29:08 +0200492 return 0;
493}
494
495static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
496{
497 int err;
498
499 memset(k, 0, sizeof(*k));
500 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200501 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200502
503 if (info->attrs[NL80211_ATTR_KEY])
504 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
505 else
506 err = nl80211_parse_key_old(info, k);
507
508 if (err)
509 return err;
510
511 if (k->def && k->defmgmt)
512 return -EINVAL;
513
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100514 if (k->defmgmt) {
515 if (k->def_uni || !k->def_multi)
516 return -EINVAL;
517 }
518
Johannes Bergb9454e82009-07-08 13:29:08 +0200519 if (k->idx != -1) {
520 if (k->defmgmt) {
521 if (k->idx < 4 || k->idx > 5)
522 return -EINVAL;
523 } else if (k->def) {
524 if (k->idx < 0 || k->idx > 3)
525 return -EINVAL;
526 } else {
527 if (k->idx < 0 || k->idx > 5)
528 return -EINVAL;
529 }
530 }
531
532 return 0;
533}
534
Johannes Bergfffd0932009-07-08 14:22:54 +0200535static struct cfg80211_cached_keys *
536nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
537 struct nlattr *keys)
538{
539 struct key_parse parse;
540 struct nlattr *key;
541 struct cfg80211_cached_keys *result;
542 int rem, err, def = 0;
543
544 result = kzalloc(sizeof(*result), GFP_KERNEL);
545 if (!result)
546 return ERR_PTR(-ENOMEM);
547
548 result->def = -1;
549 result->defmgmt = -1;
550
551 nla_for_each_nested(key, keys, rem) {
552 memset(&parse, 0, sizeof(parse));
553 parse.idx = -1;
554
555 err = nl80211_parse_key_new(key, &parse);
556 if (err)
557 goto error;
558 err = -EINVAL;
559 if (!parse.p.key)
560 goto error;
561 if (parse.idx < 0 || parse.idx > 4)
562 goto error;
563 if (parse.def) {
564 if (def)
565 goto error;
566 def = 1;
567 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100568 if (!parse.def_uni || !parse.def_multi)
569 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200570 } else if (parse.defmgmt)
571 goto error;
572 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200573 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200574 if (err)
575 goto error;
576 result->params[parse.idx].cipher = parse.p.cipher;
577 result->params[parse.idx].key_len = parse.p.key_len;
578 result->params[parse.idx].key = result->data[parse.idx];
579 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
580 }
581
582 return result;
583 error:
584 kfree(result);
585 return ERR_PTR(err);
586}
587
588static int nl80211_key_allowed(struct wireless_dev *wdev)
589{
590 ASSERT_WDEV_LOCK(wdev);
591
Johannes Bergfffd0932009-07-08 14:22:54 +0200592 switch (wdev->iftype) {
593 case NL80211_IFTYPE_AP:
594 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200595 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700596 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200597 break;
598 case NL80211_IFTYPE_ADHOC:
599 if (!wdev->current_bss)
600 return -ENOLINK;
601 break;
602 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200603 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200604 if (wdev->sme_state != CFG80211_SME_CONNECTED)
605 return -ENOLINK;
606 break;
607 default:
608 return -EINVAL;
609 }
610
611 return 0;
612}
613
Johannes Berg7527a782011-05-13 10:58:57 +0200614static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
615{
616 struct nlattr *nl_modes = nla_nest_start(msg, attr);
617 int i;
618
619 if (!nl_modes)
620 goto nla_put_failure;
621
622 i = 0;
623 while (ifmodes) {
624 if (ifmodes & 1)
625 NLA_PUT_FLAG(msg, i);
626 ifmodes >>= 1;
627 i++;
628 }
629
630 nla_nest_end(msg, nl_modes);
631 return 0;
632
633nla_put_failure:
634 return -ENOBUFS;
635}
636
637static int nl80211_put_iface_combinations(struct wiphy *wiphy,
638 struct sk_buff *msg)
639{
640 struct nlattr *nl_combis;
641 int i, j;
642
643 nl_combis = nla_nest_start(msg,
644 NL80211_ATTR_INTERFACE_COMBINATIONS);
645 if (!nl_combis)
646 goto nla_put_failure;
647
648 for (i = 0; i < wiphy->n_iface_combinations; i++) {
649 const struct ieee80211_iface_combination *c;
650 struct nlattr *nl_combi, *nl_limits;
651
652 c = &wiphy->iface_combinations[i];
653
654 nl_combi = nla_nest_start(msg, i + 1);
655 if (!nl_combi)
656 goto nla_put_failure;
657
658 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
659 if (!nl_limits)
660 goto nla_put_failure;
661
662 for (j = 0; j < c->n_limits; j++) {
663 struct nlattr *nl_limit;
664
665 nl_limit = nla_nest_start(msg, j + 1);
666 if (!nl_limit)
667 goto nla_put_failure;
668 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
669 c->limits[j].max);
670 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
671 c->limits[j].types))
672 goto nla_put_failure;
673 nla_nest_end(msg, nl_limit);
674 }
675
676 nla_nest_end(msg, nl_limits);
677
678 if (c->beacon_int_infra_match)
679 NLA_PUT_FLAG(msg,
680 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
681 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
682 c->num_different_channels);
683 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
684 c->max_interfaces);
685
686 nla_nest_end(msg, nl_combi);
687 }
688
689 nla_nest_end(msg, nl_combis);
690
691 return 0;
692nla_put_failure:
693 return -ENOBUFS;
694}
695
Johannes Berg55682962007-09-20 13:09:35 -0400696static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
697 struct cfg80211_registered_device *dev)
698{
699 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100700 struct nlattr *nl_bands, *nl_band;
701 struct nlattr *nl_freqs, *nl_freq;
702 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100703 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100704 enum ieee80211_band band;
705 struct ieee80211_channel *chan;
706 struct ieee80211_rate *rate;
707 int i;
Johannes Berg2e161f782010-08-12 15:38:38 +0200708 const struct ieee80211_txrx_stypes *mgmt_stypes =
709 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400710
711 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
712 if (!hdr)
713 return -1;
714
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500715 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400716 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200717
Johannes Bergf5ea9122009-08-07 16:17:38 +0200718 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
719 cfg80211_rdev_list_generation);
720
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200721 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
722 dev->wiphy.retry_short);
723 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
724 dev->wiphy.retry_long);
725 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
726 dev->wiphy.frag_threshold);
727 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
728 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100729 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
730 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100731 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
732 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300733 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
734 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200735 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
736 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300737 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
738 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300739 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
740 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100741
Johannes Berge31b8212010-10-05 19:39:30 +0200742 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
743 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700744 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
745 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300746 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
747 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530748 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
749 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300750 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
751 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
752 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
753 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530754
Johannes Berg25e47c182009-04-02 20:14:06 +0200755 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
756 sizeof(u32) * dev->wiphy.n_cipher_suites,
757 dev->wiphy.cipher_suites);
758
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100759 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
760 dev->wiphy.max_num_pmkids);
761
Johannes Bergc0692b82010-08-27 14:26:53 +0300762 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
763 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
764
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900765 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
766 dev->wiphy.available_antennas_tx);
767 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
768 dev->wiphy.available_antennas_rx);
769
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200770 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
771 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
772 dev->wiphy.probe_resp_offload);
773
Bruno Randolf7f531e02010-12-16 11:30:22 +0900774 if ((dev->wiphy.available_antennas_tx ||
775 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900776 u32 tx_ant = 0, rx_ant = 0;
777 int res;
778 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
779 if (!res) {
780 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
781 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
782 }
783 }
784
Johannes Berg7527a782011-05-13 10:58:57 +0200785 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
786 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700787 goto nla_put_failure;
788
Johannes Bergee688b002008-01-24 19:38:39 +0100789 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
790 if (!nl_bands)
791 goto nla_put_failure;
792
793 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
794 if (!dev->wiphy.bands[band])
795 continue;
796
797 nl_band = nla_nest_start(msg, band);
798 if (!nl_band)
799 goto nla_put_failure;
800
Johannes Bergd51626d2008-10-09 12:20:13 +0200801 /* add HT info */
802 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
803 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
804 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
805 &dev->wiphy.bands[band]->ht_cap.mcs);
806 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
807 dev->wiphy.bands[band]->ht_cap.cap);
808 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
809 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
810 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
811 dev->wiphy.bands[band]->ht_cap.ampdu_density);
812 }
813
Johannes Bergee688b002008-01-24 19:38:39 +0100814 /* add frequencies */
815 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
816 if (!nl_freqs)
817 goto nla_put_failure;
818
819 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
820 nl_freq = nla_nest_start(msg, i);
821 if (!nl_freq)
822 goto nla_put_failure;
823
824 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100825
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400826 if (nl80211_msg_put_channel(msg, chan))
827 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200828
Johannes Bergee688b002008-01-24 19:38:39 +0100829 nla_nest_end(msg, nl_freq);
830 }
831
832 nla_nest_end(msg, nl_freqs);
833
834 /* add bitrates */
835 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
836 if (!nl_rates)
837 goto nla_put_failure;
838
839 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
840 nl_rate = nla_nest_start(msg, i);
841 if (!nl_rate)
842 goto nla_put_failure;
843
844 rate = &dev->wiphy.bands[band]->bitrates[i];
845 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
846 rate->bitrate);
847 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
848 NLA_PUT_FLAG(msg,
849 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
850
851 nla_nest_end(msg, nl_rate);
852 }
853
854 nla_nest_end(msg, nl_rates);
855
856 nla_nest_end(msg, nl_band);
857 }
858 nla_nest_end(msg, nl_bands);
859
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
861 if (!nl_cmds)
862 goto nla_put_failure;
863
864 i = 0;
865#define CMD(op, n) \
866 do { \
867 if (dev->ops->op) { \
868 i++; \
869 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
870 } \
871 } while (0)
872
873 CMD(add_virtual_intf, NEW_INTERFACE);
874 CMD(change_virtual_intf, SET_INTERFACE);
875 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100876 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100877 CMD(add_station, NEW_STATION);
878 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800879 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100880 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200881 CMD(auth, AUTHENTICATE);
882 CMD(assoc, ASSOCIATE);
883 CMD(deauth, DEAUTHENTICATE);
884 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200885 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100886 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100887 CMD(set_pmksa, SET_PMKSA);
888 CMD(del_pmksa, DEL_PMKSA);
889 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100890 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
891 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200892 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f782010-08-12 15:38:38 +0200893 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100894 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100895 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200896 i++;
897 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
898 }
Johannes Bergf444de02010-05-05 15:25:02 +0200899 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400900 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300901 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
902 CMD(tdls_mgmt, TDLS_MGMT);
903 CMD(tdls_oper, TDLS_OPER);
904 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300905 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
906 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100907 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100908 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100909 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
910 i++;
911 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
912 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100913
Kalle Valo4745fc02011-11-17 19:06:10 +0200914#ifdef CONFIG_NL80211_TESTMODE
915 CMD(testmode_cmd, TESTMODE);
916#endif
917
Johannes Berg8fdc6212009-03-14 09:34:01 +0100918#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919
Johannes Berg6829c872009-07-02 09:13:27 +0200920 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200921 i++;
922 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
923 }
924
Johannes Berg6829c872009-07-02 09:13:27 +0200925 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200926 i++;
927 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
928 }
929
Johannes Berg8fdc6212009-03-14 09:34:01 +0100930 nla_nest_end(msg, nl_cmds);
931
Johannes Berg7c4ef712011-11-18 15:33:48 +0100932 if (dev->ops->remain_on_channel &&
933 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100934 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
935 dev->wiphy.max_remain_on_channel_duration);
936
Johannes Berg7c4ef712011-11-18 15:33:48 +0100937 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100938 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
939
Johannes Berg2e161f782010-08-12 15:38:38 +0200940 if (mgmt_stypes) {
941 u16 stypes;
942 struct nlattr *nl_ftypes, *nl_ifs;
943 enum nl80211_iftype ift;
944
945 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
946 if (!nl_ifs)
947 goto nla_put_failure;
948
949 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
950 nl_ftypes = nla_nest_start(msg, ift);
951 if (!nl_ftypes)
952 goto nla_put_failure;
953 i = 0;
954 stypes = mgmt_stypes[ift].tx;
955 while (stypes) {
956 if (stypes & 1)
957 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
958 (i << 4) | IEEE80211_FTYPE_MGMT);
959 stypes >>= 1;
960 i++;
961 }
962 nla_nest_end(msg, nl_ftypes);
963 }
964
Johannes Berg74b70a42010-08-24 12:15:53 +0200965 nla_nest_end(msg, nl_ifs);
966
Johannes Berg2e161f782010-08-12 15:38:38 +0200967 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
968 if (!nl_ifs)
969 goto nla_put_failure;
970
971 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
972 nl_ftypes = nla_nest_start(msg, ift);
973 if (!nl_ftypes)
974 goto nla_put_failure;
975 i = 0;
976 stypes = mgmt_stypes[ift].rx;
977 while (stypes) {
978 if (stypes & 1)
979 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
980 (i << 4) | IEEE80211_FTYPE_MGMT);
981 stypes >>= 1;
982 i++;
983 }
984 nla_nest_end(msg, nl_ftypes);
985 }
986 nla_nest_end(msg, nl_ifs);
987 }
988
Johannes Bergff1b6e62011-05-04 15:37:28 +0200989 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
990 struct nlattr *nl_wowlan;
991
992 nl_wowlan = nla_nest_start(msg,
993 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
994 if (!nl_wowlan)
995 goto nla_put_failure;
996
997 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
998 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
999 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
1000 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1001 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1002 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02001003 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1004 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1005 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1006 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1007 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1008 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1009 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1010 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1011 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1012 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001013 if (dev->wiphy.wowlan.n_patterns) {
1014 struct nl80211_wowlan_pattern_support pat = {
1015 .max_patterns = dev->wiphy.wowlan.n_patterns,
1016 .min_pattern_len =
1017 dev->wiphy.wowlan.pattern_min_len,
1018 .max_pattern_len =
1019 dev->wiphy.wowlan.pattern_max_len,
1020 };
1021 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1022 sizeof(pat), &pat);
1023 }
1024
1025 nla_nest_end(msg, nl_wowlan);
1026 }
1027
Johannes Berg7527a782011-05-13 10:58:57 +02001028 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1029 dev->wiphy.software_iftypes))
1030 goto nla_put_failure;
1031
1032 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1033 goto nla_put_failure;
1034
Johannes Berg562a7482011-11-07 12:39:33 +01001035 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1036 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1037 dev->wiphy.ap_sme_capa);
1038
Johannes Berg1f074bd2011-11-06 14:13:33 +01001039 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1040
Ben Greear7e7c8922011-11-18 11:31:59 -08001041 if (dev->wiphy.ht_capa_mod_mask)
1042 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1043 sizeof(*dev->wiphy.ht_capa_mod_mask),
1044 dev->wiphy.ht_capa_mod_mask);
1045
Johannes Berg55682962007-09-20 13:09:35 -04001046 return genlmsg_end(msg, hdr);
1047
1048 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001049 genlmsg_cancel(msg, hdr);
1050 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001051}
1052
1053static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1054{
1055 int idx = 0;
1056 int start = cb->args[0];
1057 struct cfg80211_registered_device *dev;
1058
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001059 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001060 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001061 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1062 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001063 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001064 continue;
1065 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1066 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001067 dev) < 0) {
1068 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001069 break;
Julius Volzb4637272008-07-08 14:02:19 +02001070 }
Johannes Berg55682962007-09-20 13:09:35 -04001071 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001072 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001073
1074 cb->args[0] = idx;
1075
1076 return skb->len;
1077}
1078
1079static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1080{
1081 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001082 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001083
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001084 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001085 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001086 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001087
Johannes Berg4c476992010-10-04 21:36:35 +02001088 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1089 nlmsg_free(msg);
1090 return -ENOBUFS;
1091 }
Johannes Berg55682962007-09-20 13:09:35 -04001092
Johannes Berg134e6372009-07-10 09:51:34 +00001093 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001094}
1095
Jouni Malinen31888482008-10-30 16:59:24 +02001096static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1097 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1098 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1099 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1100 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1101 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1102};
1103
1104static int parse_txq_params(struct nlattr *tb[],
1105 struct ieee80211_txq_params *txq_params)
1106{
1107 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1108 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1109 !tb[NL80211_TXQ_ATTR_AIFS])
1110 return -EINVAL;
1111
1112 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1113 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1114 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1115 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1116 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1117
1118 return 0;
1119}
1120
Johannes Bergf444de02010-05-05 15:25:02 +02001121static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1122{
1123 /*
1124 * You can only set the channel explicitly for AP, mesh
1125 * and WDS type interfaces; all others have their channel
1126 * managed via their respective "establish a connection"
1127 * command (connect, join, ...)
1128 *
1129 * Monitors are special as they are normally slaved to
1130 * whatever else is going on, so they behave as though
1131 * you tried setting the wiphy channel itself.
1132 */
1133 return !wdev ||
1134 wdev->iftype == NL80211_IFTYPE_AP ||
1135 wdev->iftype == NL80211_IFTYPE_WDS ||
1136 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001137 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1138 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001139}
1140
1141static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1142 struct wireless_dev *wdev,
1143 struct genl_info *info)
1144{
1145 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1146 u32 freq;
1147 int result;
1148
1149 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1150 return -EINVAL;
1151
1152 if (!nl80211_can_set_dev_channel(wdev))
1153 return -EOPNOTSUPP;
1154
1155 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1156 channel_type = nla_get_u32(info->attrs[
1157 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1158 if (channel_type != NL80211_CHAN_NO_HT &&
1159 channel_type != NL80211_CHAN_HT20 &&
1160 channel_type != NL80211_CHAN_HT40PLUS &&
1161 channel_type != NL80211_CHAN_HT40MINUS)
1162 return -EINVAL;
1163 }
1164
1165 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1166
1167 mutex_lock(&rdev->devlist_mtx);
1168 if (wdev) {
1169 wdev_lock(wdev);
1170 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1171 wdev_unlock(wdev);
1172 } else {
1173 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1174 }
1175 mutex_unlock(&rdev->devlist_mtx);
1176
1177 return result;
1178}
1179
1180static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1181{
Johannes Berg4c476992010-10-04 21:36:35 +02001182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1183 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001184
Johannes Berg4c476992010-10-04 21:36:35 +02001185 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001186}
1187
Bill Jordane8347eb2010-10-01 13:54:28 -04001188static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1189{
Johannes Berg43b19952010-10-07 13:10:30 +02001190 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1191 struct net_device *dev = info->user_ptr[1];
1192 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001193 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001194
1195 if (!info->attrs[NL80211_ATTR_MAC])
1196 return -EINVAL;
1197
Johannes Berg43b19952010-10-07 13:10:30 +02001198 if (netif_running(dev))
1199 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001200
Johannes Berg43b19952010-10-07 13:10:30 +02001201 if (!rdev->ops->set_wds_peer)
1202 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001203
Johannes Berg43b19952010-10-07 13:10:30 +02001204 if (wdev->iftype != NL80211_IFTYPE_WDS)
1205 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001206
1207 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001208 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001209}
1210
1211
Johannes Berg55682962007-09-20 13:09:35 -04001212static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1213{
1214 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001215 struct net_device *netdev = NULL;
1216 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001217 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001218 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001219 u32 changed;
1220 u8 retry_short = 0, retry_long = 0;
1221 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001222 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001223
Johannes Bergf444de02010-05-05 15:25:02 +02001224 /*
1225 * Try to find the wiphy and netdev. Normally this
1226 * function shouldn't need the netdev, but this is
1227 * done for backward compatibility -- previously
1228 * setting the channel was done per wiphy, but now
1229 * it is per netdev. Previous userland like hostapd
1230 * also passed a netdev to set_wiphy, so that it is
1231 * possible to let that go to the right netdev!
1232 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001233 mutex_lock(&cfg80211_mutex);
1234
Johannes Bergf444de02010-05-05 15:25:02 +02001235 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1236 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1237
1238 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1239 if (netdev && netdev->ieee80211_ptr) {
1240 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1241 mutex_lock(&rdev->mtx);
1242 } else
1243 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001244 }
1245
Johannes Bergf444de02010-05-05 15:25:02 +02001246 if (!netdev) {
1247 rdev = __cfg80211_rdev_from_info(info);
1248 if (IS_ERR(rdev)) {
1249 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001250 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001251 }
1252 wdev = NULL;
1253 netdev = NULL;
1254 result = 0;
1255
1256 mutex_lock(&rdev->mtx);
1257 } else if (netif_running(netdev) &&
1258 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1259 wdev = netdev->ieee80211_ptr;
1260 else
1261 wdev = NULL;
1262
1263 /*
1264 * end workaround code, by now the rdev is available
1265 * and locked, and wdev may or may not be NULL.
1266 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001267
1268 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001269 result = cfg80211_dev_rename(
1270 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001271
1272 mutex_unlock(&cfg80211_mutex);
1273
1274 if (result)
1275 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001276
Jouni Malinen31888482008-10-30 16:59:24 +02001277 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1278 struct ieee80211_txq_params txq_params;
1279 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1280
1281 if (!rdev->ops->set_txq_params) {
1282 result = -EOPNOTSUPP;
1283 goto bad_res;
1284 }
1285
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001286 if (!netdev) {
1287 result = -EINVAL;
1288 goto bad_res;
1289 }
1290
Johannes Berg133a3ff2011-11-03 14:50:13 +01001291 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1292 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1293 result = -EINVAL;
1294 goto bad_res;
1295 }
1296
Jouni Malinen31888482008-10-30 16:59:24 +02001297 nla_for_each_nested(nl_txq_params,
1298 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1299 rem_txq_params) {
1300 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1301 nla_data(nl_txq_params),
1302 nla_len(nl_txq_params),
1303 txq_params_policy);
1304 result = parse_txq_params(tb, &txq_params);
1305 if (result)
1306 goto bad_res;
1307
1308 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001309 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001310 &txq_params);
1311 if (result)
1312 goto bad_res;
1313 }
1314 }
1315
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001316 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001317 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001318 if (result)
1319 goto bad_res;
1320 }
1321
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001322 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1323 enum nl80211_tx_power_setting type;
1324 int idx, mbm = 0;
1325
1326 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001327 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001328 goto bad_res;
1329 }
1330
1331 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1332 type = nla_get_u32(info->attrs[idx]);
1333
1334 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1335 (type != NL80211_TX_POWER_AUTOMATIC)) {
1336 result = -EINVAL;
1337 goto bad_res;
1338 }
1339
1340 if (type != NL80211_TX_POWER_AUTOMATIC) {
1341 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1342 mbm = nla_get_u32(info->attrs[idx]);
1343 }
1344
1345 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1346 if (result)
1347 goto bad_res;
1348 }
1349
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001350 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1351 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1352 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001353 if ((!rdev->wiphy.available_antennas_tx &&
1354 !rdev->wiphy.available_antennas_rx) ||
1355 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001356 result = -EOPNOTSUPP;
1357 goto bad_res;
1358 }
1359
1360 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1361 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1362
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001363 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001364 * available antenna masks, except for the "all" mask */
1365 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1366 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001367 result = -EINVAL;
1368 goto bad_res;
1369 }
1370
Bruno Randolf7f531e02010-12-16 11:30:22 +09001371 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1372 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001373
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001374 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1375 if (result)
1376 goto bad_res;
1377 }
1378
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001379 changed = 0;
1380
1381 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1382 retry_short = nla_get_u8(
1383 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1384 if (retry_short == 0) {
1385 result = -EINVAL;
1386 goto bad_res;
1387 }
1388 changed |= WIPHY_PARAM_RETRY_SHORT;
1389 }
1390
1391 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1392 retry_long = nla_get_u8(
1393 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1394 if (retry_long == 0) {
1395 result = -EINVAL;
1396 goto bad_res;
1397 }
1398 changed |= WIPHY_PARAM_RETRY_LONG;
1399 }
1400
1401 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1402 frag_threshold = nla_get_u32(
1403 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1404 if (frag_threshold < 256) {
1405 result = -EINVAL;
1406 goto bad_res;
1407 }
1408 if (frag_threshold != (u32) -1) {
1409 /*
1410 * Fragments (apart from the last one) are required to
1411 * have even length. Make the fragmentation code
1412 * simpler by stripping LSB should someone try to use
1413 * odd threshold value.
1414 */
1415 frag_threshold &= ~0x1;
1416 }
1417 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1418 }
1419
1420 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1421 rts_threshold = nla_get_u32(
1422 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1423 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1424 }
1425
Lukáš Turek81077e82009-12-21 22:50:47 +01001426 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1427 coverage_class = nla_get_u8(
1428 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1429 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1430 }
1431
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001432 if (changed) {
1433 u8 old_retry_short, old_retry_long;
1434 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001435 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001436
1437 if (!rdev->ops->set_wiphy_params) {
1438 result = -EOPNOTSUPP;
1439 goto bad_res;
1440 }
1441
1442 old_retry_short = rdev->wiphy.retry_short;
1443 old_retry_long = rdev->wiphy.retry_long;
1444 old_frag_threshold = rdev->wiphy.frag_threshold;
1445 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001446 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001447
1448 if (changed & WIPHY_PARAM_RETRY_SHORT)
1449 rdev->wiphy.retry_short = retry_short;
1450 if (changed & WIPHY_PARAM_RETRY_LONG)
1451 rdev->wiphy.retry_long = retry_long;
1452 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1453 rdev->wiphy.frag_threshold = frag_threshold;
1454 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1455 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001456 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1457 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001458
1459 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1460 if (result) {
1461 rdev->wiphy.retry_short = old_retry_short;
1462 rdev->wiphy.retry_long = old_retry_long;
1463 rdev->wiphy.frag_threshold = old_frag_threshold;
1464 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001465 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001466 }
1467 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001468
Johannes Berg306d6112008-12-08 12:39:04 +01001469 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001470 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001471 if (netdev)
1472 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001473 return result;
1474}
1475
1476
1477static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001478 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001479 struct net_device *dev)
1480{
1481 void *hdr;
1482
1483 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1484 if (!hdr)
1485 return -1;
1486
1487 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001488 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001489 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001490 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001491
1492 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1493 rdev->devlist_generation ^
1494 (cfg80211_rdev_list_generation << 2));
1495
Johannes Berg55682962007-09-20 13:09:35 -04001496 return genlmsg_end(msg, hdr);
1497
1498 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001499 genlmsg_cancel(msg, hdr);
1500 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001501}
1502
1503static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1504{
1505 int wp_idx = 0;
1506 int if_idx = 0;
1507 int wp_start = cb->args[0];
1508 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001509 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001510 struct wireless_dev *wdev;
1511
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001512 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001513 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1514 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001515 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001516 if (wp_idx < wp_start) {
1517 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001518 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001519 }
Johannes Berg55682962007-09-20 13:09:35 -04001520 if_idx = 0;
1521
Johannes Bergf5ea9122009-08-07 16:17:38 +02001522 mutex_lock(&rdev->devlist_mtx);
1523 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001524 if (if_idx < if_start) {
1525 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001526 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001527 }
Johannes Berg55682962007-09-20 13:09:35 -04001528 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1529 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001530 rdev, wdev->netdev) < 0) {
1531 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001532 goto out;
1533 }
1534 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001535 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001536 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001537
1538 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001539 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001540 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001541 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001542
1543 cb->args[0] = wp_idx;
1544 cb->args[1] = if_idx;
1545
1546 return skb->len;
1547}
1548
1549static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1550{
1551 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001552 struct cfg80211_registered_device *dev = info->user_ptr[0];
1553 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001554
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001555 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001556 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001557 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001558
Johannes Bergd7264052009-04-19 16:23:20 +02001559 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001560 dev, netdev) < 0) {
1561 nlmsg_free(msg);
1562 return -ENOBUFS;
1563 }
Johannes Berg55682962007-09-20 13:09:35 -04001564
Johannes Berg134e6372009-07-10 09:51:34 +00001565 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001566}
1567
Michael Wu66f7ac52008-01-31 19:48:22 +01001568static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1569 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1570 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1571 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1572 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1573 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1574};
1575
1576static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1577{
1578 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1579 int flag;
1580
1581 *mntrflags = 0;
1582
1583 if (!nla)
1584 return -EINVAL;
1585
1586 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1587 nla, mntr_flags_policy))
1588 return -EINVAL;
1589
1590 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1591 if (flags[flag])
1592 *mntrflags |= (1<<flag);
1593
1594 return 0;
1595}
1596
Johannes Berg9bc383d2009-11-19 11:55:19 +01001597static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001598 struct net_device *netdev, u8 use_4addr,
1599 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001600{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001601 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001602 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001603 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001604 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001605 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001606
1607 switch (iftype) {
1608 case NL80211_IFTYPE_AP_VLAN:
1609 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1610 return 0;
1611 break;
1612 case NL80211_IFTYPE_STATION:
1613 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1614 return 0;
1615 break;
1616 default:
1617 break;
1618 }
1619
1620 return -EOPNOTSUPP;
1621}
1622
Johannes Berg55682962007-09-20 13:09:35 -04001623static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1624{
Johannes Berg4c476992010-10-04 21:36:35 +02001625 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001626 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001627 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001628 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001629 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001630 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001631 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001632
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001633 memset(&params, 0, sizeof(params));
1634
Johannes Berg04a773a2009-04-19 21:24:32 +02001635 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001636
Johannes Berg723b0382008-09-16 20:22:09 +02001637 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001638 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001639 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001640 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001641 if (ntype > NL80211_IFTYPE_MAX)
1642 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001643 }
1644
Johannes Berg92ffe052008-09-16 20:39:36 +02001645 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001646 struct wireless_dev *wdev = dev->ieee80211_ptr;
1647
Johannes Berg4c476992010-10-04 21:36:35 +02001648 if (ntype != NL80211_IFTYPE_MESH_POINT)
1649 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001650 if (netif_running(dev))
1651 return -EBUSY;
1652
1653 wdev_lock(wdev);
1654 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1655 IEEE80211_MAX_MESH_ID_LEN);
1656 wdev->mesh_id_up_len =
1657 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1658 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1659 wdev->mesh_id_up_len);
1660 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001661 }
1662
Felix Fietkau8b787642009-11-10 18:53:10 +01001663 if (info->attrs[NL80211_ATTR_4ADDR]) {
1664 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1665 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001666 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001667 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001668 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001669 } else {
1670 params.use_4addr = -1;
1671 }
1672
Johannes Berg92ffe052008-09-16 20:39:36 +02001673 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001674 if (ntype != NL80211_IFTYPE_MONITOR)
1675 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001676 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1677 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001678 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001679 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001680
1681 flags = &_flags;
1682 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001683 }
Johannes Berg3b858752009-03-12 09:55:09 +01001684
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001685 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001686 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001687 else
1688 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001689
Johannes Berg9bc383d2009-11-19 11:55:19 +01001690 if (!err && params.use_4addr != -1)
1691 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1692
Johannes Berg55682962007-09-20 13:09:35 -04001693 return err;
1694}
1695
1696static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1697{
Johannes Berg4c476992010-10-04 21:36:35 +02001698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001699 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001700 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001701 int err;
1702 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001703 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001704
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001705 memset(&params, 0, sizeof(params));
1706
Johannes Berg55682962007-09-20 13:09:35 -04001707 if (!info->attrs[NL80211_ATTR_IFNAME])
1708 return -EINVAL;
1709
1710 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1711 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1712 if (type > NL80211_IFTYPE_MAX)
1713 return -EINVAL;
1714 }
1715
Johannes Berg79c97e92009-07-07 03:56:12 +02001716 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001717 !(rdev->wiphy.interface_modes & (1 << type)))
1718 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001719
Johannes Berg9bc383d2009-11-19 11:55:19 +01001720 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001721 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001722 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001723 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001724 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001725 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001726
Michael Wu66f7ac52008-01-31 19:48:22 +01001727 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1728 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1729 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001730 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001731 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001732 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001733 if (IS_ERR(dev))
1734 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001735
Johannes Berg29cbe682010-12-03 09:20:44 +01001736 if (type == NL80211_IFTYPE_MESH_POINT &&
1737 info->attrs[NL80211_ATTR_MESH_ID]) {
1738 struct wireless_dev *wdev = dev->ieee80211_ptr;
1739
1740 wdev_lock(wdev);
1741 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1742 IEEE80211_MAX_MESH_ID_LEN);
1743 wdev->mesh_id_up_len =
1744 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1745 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1746 wdev->mesh_id_up_len);
1747 wdev_unlock(wdev);
1748 }
1749
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001750 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001751}
1752
1753static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1754{
Johannes Berg4c476992010-10-04 21:36:35 +02001755 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1756 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001757
Johannes Berg4c476992010-10-04 21:36:35 +02001758 if (!rdev->ops->del_virtual_intf)
1759 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001760
Johannes Berg4c476992010-10-04 21:36:35 +02001761 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001762}
1763
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001764static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1765{
1766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1767 struct net_device *dev = info->user_ptr[1];
1768 u16 noack_map;
1769
1770 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1771 return -EINVAL;
1772
1773 if (!rdev->ops->set_noack_map)
1774 return -EOPNOTSUPP;
1775
1776 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1777
1778 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1779}
1780
Johannes Berg41ade002007-12-19 02:03:29 +01001781struct get_key_cookie {
1782 struct sk_buff *msg;
1783 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001784 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001785};
1786
1787static void get_key_callback(void *c, struct key_params *params)
1788{
Johannes Bergb9454e82009-07-08 13:29:08 +02001789 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001790 struct get_key_cookie *cookie = c;
1791
1792 if (params->key)
1793 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1794 params->key_len, params->key);
1795
1796 if (params->seq)
1797 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1798 params->seq_len, params->seq);
1799
1800 if (params->cipher)
1801 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1802 params->cipher);
1803
Johannes Bergb9454e82009-07-08 13:29:08 +02001804 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1805 if (!key)
1806 goto nla_put_failure;
1807
1808 if (params->key)
1809 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1810 params->key_len, params->key);
1811
1812 if (params->seq)
1813 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1814 params->seq_len, params->seq);
1815
1816 if (params->cipher)
1817 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1818 params->cipher);
1819
1820 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1821
1822 nla_nest_end(cookie->msg, key);
1823
Johannes Berg41ade002007-12-19 02:03:29 +01001824 return;
1825 nla_put_failure:
1826 cookie->error = 1;
1827}
1828
1829static int nl80211_get_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 Berg41ade002007-12-19 02:03:29 +01001832 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001833 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001834 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001835 const u8 *mac_addr = NULL;
1836 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001837 struct get_key_cookie cookie = {
1838 .error = 0,
1839 };
1840 void *hdr;
1841 struct sk_buff *msg;
1842
1843 if (info->attrs[NL80211_ATTR_KEY_IDX])
1844 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1845
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001846 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001847 return -EINVAL;
1848
1849 if (info->attrs[NL80211_ATTR_MAC])
1850 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1851
Johannes Berge31b8212010-10-05 19:39:30 +02001852 pairwise = !!mac_addr;
1853 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1854 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1855 if (kt >= NUM_NL80211_KEYTYPES)
1856 return -EINVAL;
1857 if (kt != NL80211_KEYTYPE_GROUP &&
1858 kt != NL80211_KEYTYPE_PAIRWISE)
1859 return -EINVAL;
1860 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1861 }
1862
Johannes Berg4c476992010-10-04 21:36:35 +02001863 if (!rdev->ops->get_key)
1864 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001865
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001867 if (!msg)
1868 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001869
1870 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1871 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001872 if (IS_ERR(hdr))
1873 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001874
1875 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001876 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001877
1878 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1879 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1880 if (mac_addr)
1881 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1882
Johannes Berge31b8212010-10-05 19:39:30 +02001883 if (pairwise && mac_addr &&
1884 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1885 return -ENOENT;
1886
1887 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1888 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001889
1890 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001891 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001892
1893 if (cookie.error)
1894 goto nla_put_failure;
1895
1896 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001897 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001898
1899 nla_put_failure:
1900 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001901 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001902 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001903 return err;
1904}
1905
1906static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1907{
Johannes Berg4c476992010-10-04 21:36:35 +02001908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001909 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001910 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001911 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001912
Johannes Bergb9454e82009-07-08 13:29:08 +02001913 err = nl80211_parse_key(info, &key);
1914 if (err)
1915 return err;
1916
1917 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001918 return -EINVAL;
1919
Johannes Bergb9454e82009-07-08 13:29:08 +02001920 /* only support setting default key */
1921 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001922 return -EINVAL;
1923
Johannes Bergfffd0932009-07-08 14:22:54 +02001924 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001925
1926 if (key.def) {
1927 if (!rdev->ops->set_default_key) {
1928 err = -EOPNOTSUPP;
1929 goto out;
1930 }
1931
1932 err = nl80211_key_allowed(dev->ieee80211_ptr);
1933 if (err)
1934 goto out;
1935
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001936 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1937 key.def_uni, key.def_multi);
1938
1939 if (err)
1940 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001941
Johannes Berg3d23e342009-09-29 23:27:28 +02001942#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001943 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001944#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001945 } else {
1946 if (key.def_uni || !key.def_multi) {
1947 err = -EINVAL;
1948 goto out;
1949 }
1950
1951 if (!rdev->ops->set_default_mgmt_key) {
1952 err = -EOPNOTSUPP;
1953 goto out;
1954 }
1955
1956 err = nl80211_key_allowed(dev->ieee80211_ptr);
1957 if (err)
1958 goto out;
1959
1960 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1961 dev, key.idx);
1962 if (err)
1963 goto out;
1964
1965#ifdef CONFIG_CFG80211_WEXT
1966 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1967#endif
1968 }
1969
1970 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001971 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001972
Johannes Berg41ade002007-12-19 02:03:29 +01001973 return err;
1974}
1975
1976static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1977{
Johannes Berg4c476992010-10-04 21:36:35 +02001978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001979 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001980 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001981 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001982 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001983
Johannes Bergb9454e82009-07-08 13:29:08 +02001984 err = nl80211_parse_key(info, &key);
1985 if (err)
1986 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001987
Johannes Bergb9454e82009-07-08 13:29:08 +02001988 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001989 return -EINVAL;
1990
Johannes Berg41ade002007-12-19 02:03:29 +01001991 if (info->attrs[NL80211_ATTR_MAC])
1992 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1993
Johannes Berge31b8212010-10-05 19:39:30 +02001994 if (key.type == -1) {
1995 if (mac_addr)
1996 key.type = NL80211_KEYTYPE_PAIRWISE;
1997 else
1998 key.type = NL80211_KEYTYPE_GROUP;
1999 }
2000
2001 /* for now */
2002 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2003 key.type != NL80211_KEYTYPE_GROUP)
2004 return -EINVAL;
2005
Johannes Berg4c476992010-10-04 21:36:35 +02002006 if (!rdev->ops->add_key)
2007 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002008
Johannes Berge31b8212010-10-05 19:39:30 +02002009 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2010 key.type == NL80211_KEYTYPE_PAIRWISE,
2011 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002012 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002013
2014 wdev_lock(dev->ieee80211_ptr);
2015 err = nl80211_key_allowed(dev->ieee80211_ptr);
2016 if (!err)
2017 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002018 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002019 mac_addr, &key.p);
2020 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002021
Johannes Berg41ade002007-12-19 02:03:29 +01002022 return err;
2023}
2024
2025static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2026{
Johannes Berg4c476992010-10-04 21:36:35 +02002027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002028 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002029 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002030 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002031 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002032
Johannes Bergb9454e82009-07-08 13:29:08 +02002033 err = nl80211_parse_key(info, &key);
2034 if (err)
2035 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002036
2037 if (info->attrs[NL80211_ATTR_MAC])
2038 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2039
Johannes Berge31b8212010-10-05 19:39:30 +02002040 if (key.type == -1) {
2041 if (mac_addr)
2042 key.type = NL80211_KEYTYPE_PAIRWISE;
2043 else
2044 key.type = NL80211_KEYTYPE_GROUP;
2045 }
2046
2047 /* for now */
2048 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2049 key.type != NL80211_KEYTYPE_GROUP)
2050 return -EINVAL;
2051
Johannes Berg4c476992010-10-04 21:36:35 +02002052 if (!rdev->ops->del_key)
2053 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002054
Johannes Bergfffd0932009-07-08 14:22:54 +02002055 wdev_lock(dev->ieee80211_ptr);
2056 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002057
2058 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2059 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2060 err = -ENOENT;
2061
Johannes Bergfffd0932009-07-08 14:22:54 +02002062 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002063 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2064 key.type == NL80211_KEYTYPE_PAIRWISE,
2065 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002066
Johannes Berg3d23e342009-09-29 23:27:28 +02002067#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002068 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002069 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002070 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002071 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002072 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2073 }
2074#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002075 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002076
Johannes Berg41ade002007-12-19 02:03:29 +01002077 return err;
2078}
2079
Johannes Berg88600202012-02-13 15:17:18 +01002080static int nl80211_parse_beacon(struct genl_info *info,
2081 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002082{
Johannes Berg88600202012-02-13 15:17:18 +01002083 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002084
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002085 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2086 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2087 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2088 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002089 return -EINVAL;
2090
Johannes Berg88600202012-02-13 15:17:18 +01002091 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002092
Johannes Berged1b6cc2007-12-19 02:03:32 +01002093 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002094 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2095 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2096 if (!bcn->head_len)
2097 return -EINVAL;
2098 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002099 }
2100
2101 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002102 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2103 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002104 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002105 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002106 }
2107
Johannes Berg4c476992010-10-04 21:36:35 +02002108 if (!haveinfo)
2109 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002110
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002111 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002112 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2113 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002114 }
2115
2116 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002117 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002118 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002119 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002120 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2121 }
2122
2123 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002124 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002125 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002126 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002127 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2128 }
2129
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002130 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002131 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002132 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002133 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002134 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2135 }
2136
Johannes Berg88600202012-02-13 15:17:18 +01002137 return 0;
2138}
2139
2140static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2141{
2142 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2143 struct net_device *dev = info->user_ptr[1];
2144 struct wireless_dev *wdev = dev->ieee80211_ptr;
2145 struct cfg80211_ap_settings params;
2146 int err;
2147
2148 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2149 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2150 return -EOPNOTSUPP;
2151
2152 if (!rdev->ops->start_ap)
2153 return -EOPNOTSUPP;
2154
2155 if (wdev->beacon_interval)
2156 return -EALREADY;
2157
2158 memset(&params, 0, sizeof(params));
2159
2160 /* these are required for START_AP */
2161 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2162 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2163 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2164 return -EINVAL;
2165
2166 err = nl80211_parse_beacon(info, &params.beacon);
2167 if (err)
2168 return err;
2169
2170 params.beacon_interval =
2171 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2172 params.dtim_period =
2173 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2174
2175 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2176 if (err)
2177 return err;
2178
2179 /*
2180 * In theory, some of these attributes should be required here
2181 * but since they were not used when the command was originally
2182 * added, keep them optional for old user space programs to let
2183 * them continue to work with drivers that do not need the
2184 * additional information -- drivers must check!
2185 */
2186 if (info->attrs[NL80211_ATTR_SSID]) {
2187 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2188 params.ssid_len =
2189 nla_len(info->attrs[NL80211_ATTR_SSID]);
2190 if (params.ssid_len == 0 ||
2191 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2192 return -EINVAL;
2193 }
2194
2195 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2196 params.hidden_ssid = nla_get_u32(
2197 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2198 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2199 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2200 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2201 return -EINVAL;
2202 }
2203
2204 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2205
2206 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2207 params.auth_type = nla_get_u32(
2208 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2209 if (!nl80211_valid_auth_type(params.auth_type))
2210 return -EINVAL;
2211 } else
2212 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2213
2214 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2215 NL80211_MAX_NR_CIPHER_SUITES);
2216 if (err)
2217 return err;
2218
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302219 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2220 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2221 return -EOPNOTSUPP;
2222 params.inactivity_timeout = nla_get_u16(
2223 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2224 }
2225
Johannes Berg88600202012-02-13 15:17:18 +01002226 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2227 if (!err)
2228 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002229 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002230}
2231
Johannes Berg88600202012-02-13 15:17:18 +01002232static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2233{
2234 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2235 struct net_device *dev = info->user_ptr[1];
2236 struct wireless_dev *wdev = dev->ieee80211_ptr;
2237 struct cfg80211_beacon_data params;
2238 int err;
2239
2240 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2241 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2242 return -EOPNOTSUPP;
2243
2244 if (!rdev->ops->change_beacon)
2245 return -EOPNOTSUPP;
2246
2247 if (!wdev->beacon_interval)
2248 return -EINVAL;
2249
2250 err = nl80211_parse_beacon(info, &params);
2251 if (err)
2252 return err;
2253
2254 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2255}
2256
2257static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002258{
Johannes Berg4c476992010-10-04 21:36:35 +02002259 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2260 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002261 struct wireless_dev *wdev = dev->ieee80211_ptr;
2262 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002263
Johannes Berg88600202012-02-13 15:17:18 +01002264 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002265 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002266
Johannes Berg074ac8d2010-09-16 14:58:22 +02002267 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002268 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2269 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002270
Johannes Berg88600202012-02-13 15:17:18 +01002271 if (!wdev->beacon_interval)
2272 return -ENOENT;
2273
2274 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002275 if (!err)
2276 wdev->beacon_interval = 0;
2277 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002278}
2279
Johannes Berg5727ef12007-12-19 02:03:34 +01002280static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2281 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2282 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2283 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002284 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002285 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002286 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002287};
2288
Johannes Bergeccb8e82009-05-11 21:57:56 +03002289static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002290 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002291 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002292{
2293 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002294 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002295 int flag;
2296
Johannes Bergeccb8e82009-05-11 21:57:56 +03002297 /*
2298 * Try parsing the new attribute first so userspace
2299 * can specify both for older kernels.
2300 */
2301 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2302 if (nla) {
2303 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002304
Johannes Bergeccb8e82009-05-11 21:57:56 +03002305 sta_flags = nla_data(nla);
2306 params->sta_flags_mask = sta_flags->mask;
2307 params->sta_flags_set = sta_flags->set;
2308 if ((params->sta_flags_mask |
2309 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2310 return -EINVAL;
2311 return 0;
2312 }
2313
2314 /* if present, parse the old attribute */
2315
2316 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002317 if (!nla)
2318 return 0;
2319
2320 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2321 nla, sta_flags_policy))
2322 return -EINVAL;
2323
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002324 /*
2325 * Only allow certain flags for interface types so that
2326 * other attributes are silently ignored. Remember that
2327 * this is backward compatibility code with old userspace
2328 * and shouldn't be hit in other cases anyway.
2329 */
2330 switch (iftype) {
2331 case NL80211_IFTYPE_AP:
2332 case NL80211_IFTYPE_AP_VLAN:
2333 case NL80211_IFTYPE_P2P_GO:
2334 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2335 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2336 BIT(NL80211_STA_FLAG_WME) |
2337 BIT(NL80211_STA_FLAG_MFP);
2338 break;
2339 case NL80211_IFTYPE_P2P_CLIENT:
2340 case NL80211_IFTYPE_STATION:
2341 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2342 BIT(NL80211_STA_FLAG_TDLS_PEER);
2343 break;
2344 case NL80211_IFTYPE_MESH_POINT:
2345 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2346 BIT(NL80211_STA_FLAG_MFP) |
2347 BIT(NL80211_STA_FLAG_AUTHORIZED);
2348 default:
2349 return -EINVAL;
2350 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002351
2352 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2353 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002354 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002355
2356 return 0;
2357}
2358
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002359static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2360 int attr)
2361{
2362 struct nlattr *rate;
2363 u16 bitrate;
2364
2365 rate = nla_nest_start(msg, attr);
2366 if (!rate)
2367 goto nla_put_failure;
2368
2369 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2370 bitrate = cfg80211_calculate_bitrate(info);
2371 if (bitrate > 0)
2372 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2373
2374 if (info->flags & RATE_INFO_FLAGS_MCS)
2375 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2376 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2377 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2378 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2379 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2380
2381 nla_nest_end(msg, rate);
2382 return true;
2383
2384nla_put_failure:
2385 return false;
2386}
2387
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002388static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2389 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002390 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002391{
2392 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002393 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002394
2395 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2396 if (!hdr)
2397 return -1;
2398
2399 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2400 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2401
Johannes Bergf5ea9122009-08-07 16:17:38 +02002402 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2403
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002404 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2405 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002406 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302407 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2408 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2409 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2411 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2412 sinfo->inactive_time);
2413 if (sinfo->filled & STATION_INFO_RX_BYTES)
2414 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2415 sinfo->rx_bytes);
2416 if (sinfo->filled & STATION_INFO_TX_BYTES)
2417 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2418 sinfo->tx_bytes);
2419 if (sinfo->filled & STATION_INFO_LLID)
2420 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2421 sinfo->llid);
2422 if (sinfo->filled & STATION_INFO_PLID)
2423 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2424 sinfo->plid);
2425 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2426 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2427 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002428 if (sinfo->filled & STATION_INFO_SIGNAL)
2429 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2430 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002431 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2432 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2433 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002434 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002435 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2436 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002437 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002438 }
2439 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2440 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2441 NL80211_STA_INFO_RX_BITRATE))
2442 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002443 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002444 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2445 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2446 sinfo->rx_packets);
2447 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2448 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2449 sinfo->tx_packets);
Bruno Randolfb206b4ef2010-10-06 18:34:12 +09002450 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2451 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2452 sinfo->tx_retries);
2453 if (sinfo->filled & STATION_INFO_TX_FAILED)
2454 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2455 sinfo->tx_failed);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002456 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2457 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2458 sinfo->beacon_loss_count);
Paul Stewartf4263c92011-03-31 09:25:41 -07002459 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2460 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2461 if (!bss_param)
2462 goto nla_put_failure;
2463
2464 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2465 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2466 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2467 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2468 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2469 NLA_PUT_FLAG(msg,
2470 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2471 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2472 sinfo->bss_param.dtim_period);
2473 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2474 sinfo->bss_param.beacon_interval);
2475
2476 nla_nest_end(msg, bss_param);
2477 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002478 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2479 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2480 sizeof(struct nl80211_sta_flag_update),
2481 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002482 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002483
Felix Fietkau040bdf72011-08-10 19:00:33 -06002484 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002485 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2486 sinfo->assoc_req_ies);
2487
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002488 return genlmsg_end(msg, hdr);
2489
2490 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002491 genlmsg_cancel(msg, hdr);
2492 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002493}
2494
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002495static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002496 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002497{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002498 struct station_info sinfo;
2499 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002500 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002501 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002502 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002504
Johannes Berg67748892010-10-04 21:14:06 +02002505 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2506 if (err)
2507 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002508
2509 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002510 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002511 goto out_err;
2512 }
2513
Johannes Bergbba95fe2008-07-29 13:22:51 +02002514 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002515 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002516 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2517 mac_addr, &sinfo);
2518 if (err == -ENOENT)
2519 break;
2520 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002521 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002522
2523 if (nl80211_send_station(skb,
2524 NETLINK_CB(cb->skb).pid,
2525 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2526 netdev, mac_addr,
2527 &sinfo) < 0)
2528 goto out;
2529
2530 sta_idx++;
2531 }
2532
2533
2534 out:
2535 cb->args[1] = sta_idx;
2536 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002537 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002538 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002539
2540 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002541}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002542
Johannes Berg5727ef12007-12-19 02:03:34 +01002543static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2544{
Johannes Berg4c476992010-10-04 21:36:35 +02002545 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2546 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002547 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002548 struct sk_buff *msg;
2549 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002550 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002551
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002552 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002553
2554 if (!info->attrs[NL80211_ATTR_MAC])
2555 return -EINVAL;
2556
2557 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2558
Johannes Berg4c476992010-10-04 21:36:35 +02002559 if (!rdev->ops->get_station)
2560 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002561
Johannes Berg79c97e92009-07-07 03:56:12 +02002562 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002563 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002564 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002565
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002566 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002567 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002568 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002569
2570 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002571 dev, mac_addr, &sinfo) < 0) {
2572 nlmsg_free(msg);
2573 return -ENOBUFS;
2574 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002575
Johannes Berg4c476992010-10-04 21:36:35 +02002576 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002577}
2578
2579/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002580 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002581 */
Johannes Berg80b99892011-11-18 16:23:01 +01002582static struct net_device *get_vlan(struct genl_info *info,
2583 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002584{
Johannes Berg463d0182009-07-14 00:33:35 +02002585 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002586 struct net_device *v;
2587 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002588
Johannes Berg80b99892011-11-18 16:23:01 +01002589 if (!vlanattr)
2590 return NULL;
2591
2592 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2593 if (!v)
2594 return ERR_PTR(-ENODEV);
2595
2596 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2597 ret = -EINVAL;
2598 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002599 }
Johannes Berg80b99892011-11-18 16:23:01 +01002600
2601 if (!netif_running(v)) {
2602 ret = -ENETDOWN;
2603 goto error;
2604 }
2605
2606 return v;
2607 error:
2608 dev_put(v);
2609 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002610}
2611
2612static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2613{
Johannes Berg4c476992010-10-04 21:36:35 +02002614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002615 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002616 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002617 struct station_parameters params;
2618 u8 *mac_addr = NULL;
2619
2620 memset(&params, 0, sizeof(params));
2621
2622 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002623 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002624
2625 if (info->attrs[NL80211_ATTR_STA_AID])
2626 return -EINVAL;
2627
2628 if (!info->attrs[NL80211_ATTR_MAC])
2629 return -EINVAL;
2630
2631 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2632
2633 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2634 params.supported_rates =
2635 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2636 params.supported_rates_len =
2637 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2638 }
2639
2640 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2641 params.listen_interval =
2642 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2643
Jouni Malinen36aedc92008-08-25 11:58:58 +03002644 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2645 params.ht_capa =
2646 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2647
Johannes Bergbdd90d52011-12-14 12:20:27 +01002648 if (!rdev->ops->change_station)
2649 return -EOPNOTSUPP;
2650
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002651 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002652 return -EINVAL;
2653
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002654 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2655 params.plink_action =
2656 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2657
Javier Cardona9c3990a2011-05-03 16:57:11 -07002658 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2659 params.plink_state =
2660 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2661
Johannes Berga97f4422009-06-18 17:23:43 +02002662 switch (dev->ieee80211_ptr->iftype) {
2663 case NL80211_IFTYPE_AP:
2664 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002665 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002666 /* disallow mesh-specific things */
2667 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002668 return -EINVAL;
2669
2670 /* TDLS can't be set, ... */
2671 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2672 return -EINVAL;
2673 /*
2674 * ... but don't bother the driver with it. This works around
2675 * a hostapd/wpa_supplicant issue -- it always includes the
2676 * TLDS_PEER flag in the mask even for AP mode.
2677 */
2678 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2679
2680 /* accept only the listed bits */
2681 if (params.sta_flags_mask &
2682 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2683 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2684 BIT(NL80211_STA_FLAG_WME) |
2685 BIT(NL80211_STA_FLAG_MFP)))
2686 return -EINVAL;
2687
2688 /* must be last in here for error handling */
2689 params.vlan = get_vlan(info, rdev);
2690 if (IS_ERR(params.vlan))
2691 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002692 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002693 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002694 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002695 /*
2696 * Don't allow userspace to change the TDLS_PEER flag,
2697 * but silently ignore attempts to change it since we
2698 * don't have state here to verify that it doesn't try
2699 * to change the flag.
2700 */
2701 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002702 /* fall through */
2703 case NL80211_IFTYPE_ADHOC:
2704 /* disallow things sta doesn't support */
2705 if (params.plink_action)
2706 return -EINVAL;
2707 if (params.ht_capa)
2708 return -EINVAL;
2709 if (params.listen_interval >= 0)
2710 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002711 /* reject any changes other than AUTHORIZED */
2712 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2713 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002714 break;
2715 case NL80211_IFTYPE_MESH_POINT:
2716 /* disallow things mesh doesn't support */
2717 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002718 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002719 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002720 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002721 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002722 return -EINVAL;
2723 /*
2724 * No special handling for TDLS here -- the userspace
2725 * mesh code doesn't have this bug.
2726 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002727 if (params.sta_flags_mask &
2728 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002729 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002730 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002731 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002732 break;
2733 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002734 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002735 }
2736
Johannes Bergbdd90d52011-12-14 12:20:27 +01002737 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002738
Johannes Berg79c97e92009-07-07 03:56:12 +02002739 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002740
Johannes Berg5727ef12007-12-19 02:03:34 +01002741 if (params.vlan)
2742 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002743
Johannes Berg5727ef12007-12-19 02:03:34 +01002744 return err;
2745}
2746
Eliad Pellerc75786c2011-08-23 14:37:46 +03002747static struct nla_policy
2748nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2749 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2750 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2751};
2752
Johannes Berg5727ef12007-12-19 02:03:34 +01002753static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2754{
Johannes Berg4c476992010-10-04 21:36:35 +02002755 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002756 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002757 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002758 struct station_parameters params;
2759 u8 *mac_addr = NULL;
2760
2761 memset(&params, 0, sizeof(params));
2762
2763 if (!info->attrs[NL80211_ATTR_MAC])
2764 return -EINVAL;
2765
Johannes Berg5727ef12007-12-19 02:03:34 +01002766 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2767 return -EINVAL;
2768
2769 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2770 return -EINVAL;
2771
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002772 if (!info->attrs[NL80211_ATTR_STA_AID])
2773 return -EINVAL;
2774
Johannes Berg5727ef12007-12-19 02:03:34 +01002775 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2776 params.supported_rates =
2777 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2778 params.supported_rates_len =
2779 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2780 params.listen_interval =
2781 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002782
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002783 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2784 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2785 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002786
Jouni Malinen36aedc92008-08-25 11:58:58 +03002787 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2788 params.ht_capa =
2789 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002790
Javier Cardona96b78df2011-04-07 15:08:33 -07002791 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2792 params.plink_action =
2793 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2794
Johannes Bergbdd90d52011-12-14 12:20:27 +01002795 if (!rdev->ops->add_station)
2796 return -EOPNOTSUPP;
2797
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002798 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002799 return -EINVAL;
2800
Johannes Bergbdd90d52011-12-14 12:20:27 +01002801 switch (dev->ieee80211_ptr->iftype) {
2802 case NL80211_IFTYPE_AP:
2803 case NL80211_IFTYPE_AP_VLAN:
2804 case NL80211_IFTYPE_P2P_GO:
2805 /* parse WME attributes if sta is WME capable */
2806 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2807 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2808 info->attrs[NL80211_ATTR_STA_WME]) {
2809 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2810 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002811
Johannes Bergbdd90d52011-12-14 12:20:27 +01002812 nla = info->attrs[NL80211_ATTR_STA_WME];
2813 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2814 nl80211_sta_wme_policy);
2815 if (err)
2816 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002817
Johannes Bergbdd90d52011-12-14 12:20:27 +01002818 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2819 params.uapsd_queues =
2820 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2821 if (params.uapsd_queues &
2822 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2823 return -EINVAL;
2824
2825 if (tb[NL80211_STA_WME_MAX_SP])
2826 params.max_sp =
2827 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2828
2829 if (params.max_sp &
2830 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2831 return -EINVAL;
2832
2833 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2834 }
2835 /* TDLS peers cannot be added */
2836 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002837 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002838 /* but don't bother the driver with it */
2839 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002840
Johannes Bergbdd90d52011-12-14 12:20:27 +01002841 /* must be last in here for error handling */
2842 params.vlan = get_vlan(info, rdev);
2843 if (IS_ERR(params.vlan))
2844 return PTR_ERR(params.vlan);
2845 break;
2846 case NL80211_IFTYPE_MESH_POINT:
2847 /* TDLS peers cannot be added */
2848 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002849 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002850 break;
2851 case NL80211_IFTYPE_STATION:
2852 /* Only TDLS peers can be added */
2853 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2854 return -EINVAL;
2855 /* Can only add if TDLS ... */
2856 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2857 return -EOPNOTSUPP;
2858 /* ... with external setup is supported */
2859 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2860 return -EOPNOTSUPP;
2861 break;
2862 default:
2863 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002864 }
2865
Johannes Bergbdd90d52011-12-14 12:20:27 +01002866 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002867
Johannes Berg79c97e92009-07-07 03:56:12 +02002868 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002869
Johannes Berg5727ef12007-12-19 02:03:34 +01002870 if (params.vlan)
2871 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002872 return err;
2873}
2874
2875static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2876{
Johannes Berg4c476992010-10-04 21:36:35 +02002877 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2878 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002879 u8 *mac_addr = NULL;
2880
2881 if (info->attrs[NL80211_ATTR_MAC])
2882 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2883
Johannes Berge80cf852009-05-11 14:43:13 +02002884 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002885 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002886 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002887 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2888 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002889
Johannes Berg4c476992010-10-04 21:36:35 +02002890 if (!rdev->ops->del_station)
2891 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002892
Johannes Berg4c476992010-10-04 21:36:35 +02002893 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002894}
2895
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002896static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2897 int flags, struct net_device *dev,
2898 u8 *dst, u8 *next_hop,
2899 struct mpath_info *pinfo)
2900{
2901 void *hdr;
2902 struct nlattr *pinfoattr;
2903
2904 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2905 if (!hdr)
2906 return -1;
2907
2908 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2909 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2910 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2911
Johannes Bergf5ea9122009-08-07 16:17:38 +02002912 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2913
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002914 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2915 if (!pinfoattr)
2916 goto nla_put_failure;
2917 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2918 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2919 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002920 if (pinfo->filled & MPATH_INFO_SN)
2921 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2922 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002923 if (pinfo->filled & MPATH_INFO_METRIC)
2924 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2925 pinfo->metric);
2926 if (pinfo->filled & MPATH_INFO_EXPTIME)
2927 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2928 pinfo->exptime);
2929 if (pinfo->filled & MPATH_INFO_FLAGS)
2930 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2931 pinfo->flags);
2932 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2933 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2934 pinfo->discovery_timeout);
2935 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2936 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2937 pinfo->discovery_retries);
2938
2939 nla_nest_end(msg, pinfoattr);
2940
2941 return genlmsg_end(msg, hdr);
2942
2943 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002944 genlmsg_cancel(msg, hdr);
2945 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002946}
2947
2948static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002949 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002950{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002951 struct mpath_info pinfo;
2952 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002953 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002954 u8 dst[ETH_ALEN];
2955 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002956 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002957 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002958
Johannes Berg67748892010-10-04 21:14:06 +02002959 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2960 if (err)
2961 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002962
2963 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002964 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002965 goto out_err;
2966 }
2967
Jouni Malineneec60b02009-03-20 21:21:19 +02002968 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2969 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002970 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002971 }
2972
Johannes Bergbba95fe2008-07-29 13:22:51 +02002973 while (1) {
2974 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2975 dst, next_hop, &pinfo);
2976 if (err == -ENOENT)
2977 break;
2978 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002979 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002980
2981 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2982 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2983 netdev, dst, next_hop,
2984 &pinfo) < 0)
2985 goto out;
2986
2987 path_idx++;
2988 }
2989
2990
2991 out:
2992 cb->args[1] = path_idx;
2993 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002994 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002995 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002996 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002997}
2998
2999static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3000{
Johannes Berg4c476992010-10-04 21:36:35 +02003001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003002 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003003 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003004 struct mpath_info pinfo;
3005 struct sk_buff *msg;
3006 u8 *dst = NULL;
3007 u8 next_hop[ETH_ALEN];
3008
3009 memset(&pinfo, 0, sizeof(pinfo));
3010
3011 if (!info->attrs[NL80211_ATTR_MAC])
3012 return -EINVAL;
3013
3014 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3015
Johannes Berg4c476992010-10-04 21:36:35 +02003016 if (!rdev->ops->get_mpath)
3017 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003018
Johannes Berg4c476992010-10-04 21:36:35 +02003019 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3020 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003021
Johannes Berg79c97e92009-07-07 03:56:12 +02003022 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003023 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003024 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003025
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003026 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003027 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003028 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003029
3030 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003031 dev, dst, next_hop, &pinfo) < 0) {
3032 nlmsg_free(msg);
3033 return -ENOBUFS;
3034 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003035
Johannes Berg4c476992010-10-04 21:36:35 +02003036 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003037}
3038
3039static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3040{
Johannes Berg4c476992010-10-04 21:36:35 +02003041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3042 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003043 u8 *dst = NULL;
3044 u8 *next_hop = NULL;
3045
3046 if (!info->attrs[NL80211_ATTR_MAC])
3047 return -EINVAL;
3048
3049 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3050 return -EINVAL;
3051
3052 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3053 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3054
Johannes Berg4c476992010-10-04 21:36:35 +02003055 if (!rdev->ops->change_mpath)
3056 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003057
Johannes Berg4c476992010-10-04 21:36:35 +02003058 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3059 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003060
Johannes Berg4c476992010-10-04 21:36:35 +02003061 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003062}
Johannes Berg4c476992010-10-04 21:36:35 +02003063
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003064static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3065{
Johannes Berg4c476992010-10-04 21:36:35 +02003066 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3067 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003068 u8 *dst = NULL;
3069 u8 *next_hop = NULL;
3070
3071 if (!info->attrs[NL80211_ATTR_MAC])
3072 return -EINVAL;
3073
3074 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3075 return -EINVAL;
3076
3077 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3078 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3079
Johannes Berg4c476992010-10-04 21:36:35 +02003080 if (!rdev->ops->add_mpath)
3081 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003082
Johannes Berg4c476992010-10-04 21:36:35 +02003083 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3084 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003085
Johannes Berg4c476992010-10-04 21:36:35 +02003086 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003087}
3088
3089static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3090{
Johannes Berg4c476992010-10-04 21:36:35 +02003091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3092 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003093 u8 *dst = NULL;
3094
3095 if (info->attrs[NL80211_ATTR_MAC])
3096 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3097
Johannes Berg4c476992010-10-04 21:36:35 +02003098 if (!rdev->ops->del_mpath)
3099 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003100
Johannes Berg4c476992010-10-04 21:36:35 +02003101 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003102}
3103
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003104static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3105{
Johannes Berg4c476992010-10-04 21:36:35 +02003106 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3107 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003108 struct bss_parameters params;
3109
3110 memset(&params, 0, sizeof(params));
3111 /* default to not changing parameters */
3112 params.use_cts_prot = -1;
3113 params.use_short_preamble = -1;
3114 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003115 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003116 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003117
3118 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3119 params.use_cts_prot =
3120 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3121 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3122 params.use_short_preamble =
3123 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3124 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3125 params.use_short_slot_time =
3126 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003127 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3128 params.basic_rates =
3129 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3130 params.basic_rates_len =
3131 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3132 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003133 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3134 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003135 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3136 params.ht_opmode =
3137 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003138
Johannes Berg4c476992010-10-04 21:36:35 +02003139 if (!rdev->ops->change_bss)
3140 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003141
Johannes Berg074ac8d2010-09-16 14:58:22 +02003142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003143 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3144 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003145
Johannes Berg4c476992010-10-04 21:36:35 +02003146 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003147}
3148
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003149static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003150 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3151 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3152 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3153 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3154 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3155 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3156};
3157
3158static int parse_reg_rule(struct nlattr *tb[],
3159 struct ieee80211_reg_rule *reg_rule)
3160{
3161 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3162 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3163
3164 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3165 return -EINVAL;
3166 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3167 return -EINVAL;
3168 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3169 return -EINVAL;
3170 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3171 return -EINVAL;
3172 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3173 return -EINVAL;
3174
3175 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3176
3177 freq_range->start_freq_khz =
3178 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3179 freq_range->end_freq_khz =
3180 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3181 freq_range->max_bandwidth_khz =
3182 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3183
3184 power_rule->max_eirp =
3185 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3186
3187 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3188 power_rule->max_antenna_gain =
3189 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3190
3191 return 0;
3192}
3193
3194static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3195{
3196 int r;
3197 char *data = NULL;
3198
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003199 /*
3200 * You should only get this when cfg80211 hasn't yet initialized
3201 * completely when built-in to the kernel right between the time
3202 * window between nl80211_init() and regulatory_init(), if that is
3203 * even possible.
3204 */
3205 mutex_lock(&cfg80211_mutex);
3206 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003207 mutex_unlock(&cfg80211_mutex);
3208 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003209 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003210 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003211
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003212 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3213 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003214
3215 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3216
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003217 r = regulatory_hint_user(data);
3218
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003219 return r;
3220}
3221
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003222static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003223 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003224{
Johannes Berg4c476992010-10-04 21:36:35 +02003225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003226 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003227 struct wireless_dev *wdev = dev->ieee80211_ptr;
3228 struct mesh_config cur_params;
3229 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003230 void *hdr;
3231 struct nlattr *pinfoattr;
3232 struct sk_buff *msg;
3233
Johannes Berg29cbe682010-12-03 09:20:44 +01003234 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3235 return -EOPNOTSUPP;
3236
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003237 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003238 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003239
Johannes Berg29cbe682010-12-03 09:20:44 +01003240 wdev_lock(wdev);
3241 /* If not connected, get default parameters */
3242 if (!wdev->mesh_id_len)
3243 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3244 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003245 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003246 &cur_params);
3247 wdev_unlock(wdev);
3248
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003249 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003250 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003251
3252 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003253 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003254 if (!msg)
3255 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003256 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003257 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003258 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003259 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003260 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003261 if (!pinfoattr)
3262 goto nla_put_failure;
3263 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3264 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3265 cur_params.dot11MeshRetryTimeout);
3266 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3267 cur_params.dot11MeshConfirmTimeout);
3268 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3269 cur_params.dot11MeshHoldingTimeout);
3270 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3271 cur_params.dot11MeshMaxPeerLinks);
3272 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3273 cur_params.dot11MeshMaxRetries);
3274 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3275 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003276 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3277 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003278 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3279 cur_params.auto_open_plinks);
3280 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3281 cur_params.dot11MeshHWMPmaxPREQretries);
3282 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3283 cur_params.path_refresh_time);
3284 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3285 cur_params.min_discovery_timeout);
3286 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3287 cur_params.dot11MeshHWMPactivePathTimeout);
3288 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3289 cur_params.dot11MeshHWMPpreqMinInterval);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003290 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3291 cur_params.dot11MeshHWMPperrMinInterval);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003292 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3293 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003294 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3295 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003296 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3297 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003298 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3299 cur_params.dot11MeshGateAnnouncementProtocol);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003300 NLA_PUT_U8(msg, NL80211_MESHCONF_FORWARDING,
3301 cur_params.dot11MeshForwarding);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003302 NLA_PUT_U32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3303 cur_params.rssi_threshold);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003304 nla_nest_end(msg, pinfoattr);
3305 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003306 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003307
Johannes Berg3b858752009-03-12 09:55:09 +01003308 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003309 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003310 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003311 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003312 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003313}
3314
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003315static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003316 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3317 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3318 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3319 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3320 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3321 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003322 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003323 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3324
3325 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3326 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3327 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3328 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3329 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003330 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003331 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003332 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003333 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003334 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003335 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003336 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003337};
3338
Javier Cardonac80d5452010-12-16 17:37:49 -08003339static const struct nla_policy
3340 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3341 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3342 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003343 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003344 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003345 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003346 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003347};
3348
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003349static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003350 struct mesh_config *cfg,
3351 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003352{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003353 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003354 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003355
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003356#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3357do {\
3358 if (table[attr_num]) {\
3359 cfg->param = nla_fn(table[attr_num]); \
3360 mask |= (1 << (attr_num - 1)); \
3361 } \
3362} while (0);\
3363
3364
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003365 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003366 return -EINVAL;
3367 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003368 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003369 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003370 return -EINVAL;
3371
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003372 /* This makes sure that there aren't more than 32 mesh config
3373 * parameters (otherwise our bitfield scheme would not work.) */
3374 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3375
3376 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003377 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3378 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3379 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3380 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3381 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3382 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3384 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3385 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3386 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3387 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3388 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003389 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3390 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003391 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3392 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3393 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3394 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3395 nla_get_u8);
3396 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3397 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3398 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3399 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3400 nla_get_u16);
3401 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3402 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3403 nla_get_u32);
3404 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3405 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3406 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003407 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3408 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3409 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003410 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3411 dot11MeshHWMPnetDiameterTraversalTime,
3412 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3413 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003414 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3415 dot11MeshHWMPRootMode, mask,
3416 NL80211_MESHCONF_HWMP_ROOTMODE,
3417 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003418 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3419 dot11MeshHWMPRannInterval, mask,
3420 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3421 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003422 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3423 dot11MeshGateAnnouncementProtocol, mask,
3424 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3425 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003426 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3427 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003428 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3429 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003430 if (mask_out)
3431 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003432
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003433 return 0;
3434
3435#undef FILL_IN_MESH_PARAM_IF_SET
3436}
3437
Javier Cardonac80d5452010-12-16 17:37:49 -08003438static int nl80211_parse_mesh_setup(struct genl_info *info,
3439 struct mesh_setup *setup)
3440{
3441 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3442
3443 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3444 return -EINVAL;
3445 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3446 info->attrs[NL80211_ATTR_MESH_SETUP],
3447 nl80211_mesh_setup_params_policy))
3448 return -EINVAL;
3449
3450 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3451 setup->path_sel_proto =
3452 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3453 IEEE80211_PATH_PROTOCOL_VENDOR :
3454 IEEE80211_PATH_PROTOCOL_HWMP;
3455
3456 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3457 setup->path_metric =
3458 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3459 IEEE80211_PATH_METRIC_VENDOR :
3460 IEEE80211_PATH_METRIC_AIRTIME;
3461
Javier Cardona581a8b02011-04-07 15:08:27 -07003462
3463 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003464 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003465 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003466 if (!is_valid_ie_attr(ieattr))
3467 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003468 setup->ie = nla_data(ieattr);
3469 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003470 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003471 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3472 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003473
3474 return 0;
3475}
3476
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003477static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003478 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003479{
3480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3481 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003482 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003483 struct mesh_config cfg;
3484 u32 mask;
3485 int err;
3486
Johannes Berg29cbe682010-12-03 09:20:44 +01003487 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3488 return -EOPNOTSUPP;
3489
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003490 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003491 return -EOPNOTSUPP;
3492
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003493 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003494 if (err)
3495 return err;
3496
Johannes Berg29cbe682010-12-03 09:20:44 +01003497 wdev_lock(wdev);
3498 if (!wdev->mesh_id_len)
3499 err = -ENOLINK;
3500
3501 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003502 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003503 mask, &cfg);
3504
3505 wdev_unlock(wdev);
3506
3507 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003508}
3509
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003510static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3511{
3512 struct sk_buff *msg;
3513 void *hdr = NULL;
3514 struct nlattr *nl_reg_rules;
3515 unsigned int i;
3516 int err = -EINVAL;
3517
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003518 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003519
3520 if (!cfg80211_regdomain)
3521 goto out;
3522
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003523 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003524 if (!msg) {
3525 err = -ENOBUFS;
3526 goto out;
3527 }
3528
3529 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3530 NL80211_CMD_GET_REG);
3531 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003532 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003533
3534 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3535 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003536 if (cfg80211_regdomain->dfs_region)
3537 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3538 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003539
3540 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3541 if (!nl_reg_rules)
3542 goto nla_put_failure;
3543
3544 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3545 struct nlattr *nl_reg_rule;
3546 const struct ieee80211_reg_rule *reg_rule;
3547 const struct ieee80211_freq_range *freq_range;
3548 const struct ieee80211_power_rule *power_rule;
3549
3550 reg_rule = &cfg80211_regdomain->reg_rules[i];
3551 freq_range = &reg_rule->freq_range;
3552 power_rule = &reg_rule->power_rule;
3553
3554 nl_reg_rule = nla_nest_start(msg, i);
3555 if (!nl_reg_rule)
3556 goto nla_put_failure;
3557
3558 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3559 reg_rule->flags);
3560 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3561 freq_range->start_freq_khz);
3562 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3563 freq_range->end_freq_khz);
3564 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3565 freq_range->max_bandwidth_khz);
3566 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3567 power_rule->max_antenna_gain);
3568 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3569 power_rule->max_eirp);
3570
3571 nla_nest_end(msg, nl_reg_rule);
3572 }
3573
3574 nla_nest_end(msg, nl_reg_rules);
3575
3576 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003577 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003578 goto out;
3579
3580nla_put_failure:
3581 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003582put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003583 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003584 err = -EMSGSIZE;
3585out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003586 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003587 return err;
3588}
3589
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003590static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3591{
3592 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3593 struct nlattr *nl_reg_rule;
3594 char *alpha2 = NULL;
3595 int rem_reg_rules = 0, r = 0;
3596 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003597 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003598 struct ieee80211_regdomain *rd = NULL;
3599
3600 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3601 return -EINVAL;
3602
3603 if (!info->attrs[NL80211_ATTR_REG_RULES])
3604 return -EINVAL;
3605
3606 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3607
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003608 if (info->attrs[NL80211_ATTR_DFS_REGION])
3609 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3610
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003611 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3612 rem_reg_rules) {
3613 num_rules++;
3614 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003615 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003616 }
3617
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003618 mutex_lock(&cfg80211_mutex);
3619
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003620 if (!reg_is_valid_request(alpha2)) {
3621 r = -EINVAL;
3622 goto bad_reg;
3623 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003624
3625 size_of_regd = sizeof(struct ieee80211_regdomain) +
3626 (num_rules * sizeof(struct ieee80211_reg_rule));
3627
3628 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003629 if (!rd) {
3630 r = -ENOMEM;
3631 goto bad_reg;
3632 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003633
3634 rd->n_reg_rules = num_rules;
3635 rd->alpha2[0] = alpha2[0];
3636 rd->alpha2[1] = alpha2[1];
3637
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003638 /*
3639 * Disable DFS master mode if the DFS region was
3640 * not supported or known on this kernel.
3641 */
3642 if (reg_supported_dfs_region(dfs_region))
3643 rd->dfs_region = dfs_region;
3644
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003645 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3646 rem_reg_rules) {
3647 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3648 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3649 reg_rule_policy);
3650 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3651 if (r)
3652 goto bad_reg;
3653
3654 rule_idx++;
3655
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003656 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3657 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003658 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003659 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003660 }
3661
3662 BUG_ON(rule_idx != num_rules);
3663
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003664 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003665
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003666 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003667
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003668 return r;
3669
Johannes Bergd2372b32008-10-24 20:32:20 +02003670 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003671 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003672 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003673 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003674}
3675
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003676static int validate_scan_freqs(struct nlattr *freqs)
3677{
3678 struct nlattr *attr1, *attr2;
3679 int n_channels = 0, tmp1, tmp2;
3680
3681 nla_for_each_nested(attr1, freqs, tmp1) {
3682 n_channels++;
3683 /*
3684 * Some hardware has a limited channel list for
3685 * scanning, and it is pretty much nonsensical
3686 * to scan for a channel twice, so disallow that
3687 * and don't require drivers to check that the
3688 * channel list they get isn't longer than what
3689 * they can scan, as long as they can scan all
3690 * the channels they registered at once.
3691 */
3692 nla_for_each_nested(attr2, freqs, tmp2)
3693 if (attr1 != attr2 &&
3694 nla_get_u32(attr1) == nla_get_u32(attr2))
3695 return 0;
3696 }
3697
3698 return n_channels;
3699}
3700
Johannes Berg2a519312009-02-10 21:25:55 +01003701static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3702{
Johannes Berg4c476992010-10-04 21:36:35 +02003703 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3704 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003705 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003706 struct nlattr *attr;
3707 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003708 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003709 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003710
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003711 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3712 return -EINVAL;
3713
Johannes Berg79c97e92009-07-07 03:56:12 +02003714 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003715
Johannes Berg4c476992010-10-04 21:36:35 +02003716 if (!rdev->ops->scan)
3717 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003718
Johannes Berg4c476992010-10-04 21:36:35 +02003719 if (rdev->scan_req)
3720 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003721
3722 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003723 n_channels = validate_scan_freqs(
3724 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003725 if (!n_channels)
3726 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003727 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003728 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003729 n_channels = 0;
3730
Johannes Berg2a519312009-02-10 21:25:55 +01003731 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3732 if (wiphy->bands[band])
3733 n_channels += wiphy->bands[band]->n_channels;
3734 }
3735
3736 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3737 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3738 n_ssids++;
3739
Johannes Berg4c476992010-10-04 21:36:35 +02003740 if (n_ssids > wiphy->max_scan_ssids)
3741 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003742
Jouni Malinen70692ad2009-02-16 19:39:13 +02003743 if (info->attrs[NL80211_ATTR_IE])
3744 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3745 else
3746 ie_len = 0;
3747
Johannes Berg4c476992010-10-04 21:36:35 +02003748 if (ie_len > wiphy->max_scan_ie_len)
3749 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003750
Johannes Berg2a519312009-02-10 21:25:55 +01003751 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003752 + sizeof(*request->ssids) * n_ssids
3753 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003754 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003755 if (!request)
3756 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003757
Johannes Berg2a519312009-02-10 21:25:55 +01003758 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003759 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003760 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003761 if (ie_len) {
3762 if (request->ssids)
3763 request->ie = (void *)(request->ssids + n_ssids);
3764 else
3765 request->ie = (void *)(request->channels + n_channels);
3766 }
Johannes Berg2a519312009-02-10 21:25:55 +01003767
Johannes Berg584991d2009-11-02 13:32:03 +01003768 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003769 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3770 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003771 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003772 struct ieee80211_channel *chan;
3773
3774 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3775
3776 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003777 err = -EINVAL;
3778 goto out_free;
3779 }
Johannes Berg584991d2009-11-02 13:32:03 +01003780
3781 /* ignore disabled channels */
3782 if (chan->flags & IEEE80211_CHAN_DISABLED)
3783 continue;
3784
3785 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003786 i++;
3787 }
3788 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003789 enum ieee80211_band band;
3790
Johannes Berg2a519312009-02-10 21:25:55 +01003791 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003792 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3793 int j;
3794 if (!wiphy->bands[band])
3795 continue;
3796 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003797 struct ieee80211_channel *chan;
3798
3799 chan = &wiphy->bands[band]->channels[j];
3800
3801 if (chan->flags & IEEE80211_CHAN_DISABLED)
3802 continue;
3803
3804 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003805 i++;
3806 }
3807 }
3808 }
3809
Johannes Berg584991d2009-11-02 13:32:03 +01003810 if (!i) {
3811 err = -EINVAL;
3812 goto out_free;
3813 }
3814
3815 request->n_channels = i;
3816
Johannes Berg2a519312009-02-10 21:25:55 +01003817 i = 0;
3818 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3819 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003820 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003821 err = -EINVAL;
3822 goto out_free;
3823 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003824 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003825 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003826 i++;
3827 }
3828 }
3829
Jouni Malinen70692ad2009-02-16 19:39:13 +02003830 if (info->attrs[NL80211_ATTR_IE]) {
3831 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a54b2009-04-01 11:58:36 +02003832 memcpy((void *)request->ie,
3833 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003834 request->ie_len);
3835 }
3836
Johannes Berg34850ab2011-07-18 18:08:35 +02003837 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003838 if (wiphy->bands[i])
3839 request->rates[i] =
3840 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003841
3842 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3843 nla_for_each_nested(attr,
3844 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3845 tmp) {
3846 enum ieee80211_band band = nla_type(attr);
3847
Dan Carpenter84404622011-07-29 11:52:18 +03003848 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003849 err = -EINVAL;
3850 goto out_free;
3851 }
3852 err = ieee80211_get_ratemask(wiphy->bands[band],
3853 nla_data(attr),
3854 nla_len(attr),
3855 &request->rates[band]);
3856 if (err)
3857 goto out_free;
3858 }
3859 }
3860
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303861 request->no_cck =
3862 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3863
Johannes Berg463d0182009-07-14 00:33:35 +02003864 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003865 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003866
Johannes Berg79c97e92009-07-07 03:56:12 +02003867 rdev->scan_req = request;
3868 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003869
Johannes Berg463d0182009-07-14 00:33:35 +02003870 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003871 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003872 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003873 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003874 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003875 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003876 kfree(request);
3877 }
Johannes Berg3b858752009-03-12 09:55:09 +01003878
Johannes Berg2a519312009-02-10 21:25:55 +01003879 return err;
3880}
3881
Luciano Coelho807f8a82011-05-11 17:09:35 +03003882static int nl80211_start_sched_scan(struct sk_buff *skb,
3883 struct genl_info *info)
3884{
3885 struct cfg80211_sched_scan_request *request;
3886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3887 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003888 struct nlattr *attr;
3889 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003890 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003891 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003892 enum ieee80211_band band;
3893 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003894 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003895
3896 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3897 !rdev->ops->sched_scan_start)
3898 return -EOPNOTSUPP;
3899
3900 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3901 return -EINVAL;
3902
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003903 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3904 return -EINVAL;
3905
3906 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3907 if (interval == 0)
3908 return -EINVAL;
3909
Luciano Coelho807f8a82011-05-11 17:09:35 +03003910 wiphy = &rdev->wiphy;
3911
3912 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3913 n_channels = validate_scan_freqs(
3914 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3915 if (!n_channels)
3916 return -EINVAL;
3917 } else {
3918 n_channels = 0;
3919
3920 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3921 if (wiphy->bands[band])
3922 n_channels += wiphy->bands[band]->n_channels;
3923 }
3924
3925 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3926 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3927 tmp)
3928 n_ssids++;
3929
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003930 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003931 return -EINVAL;
3932
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003933 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3934 nla_for_each_nested(attr,
3935 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3936 tmp)
3937 n_match_sets++;
3938
3939 if (n_match_sets > wiphy->max_match_sets)
3940 return -EINVAL;
3941
Luciano Coelho807f8a82011-05-11 17:09:35 +03003942 if (info->attrs[NL80211_ATTR_IE])
3943 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3944 else
3945 ie_len = 0;
3946
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003947 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003948 return -EINVAL;
3949
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003950 mutex_lock(&rdev->sched_scan_mtx);
3951
3952 if (rdev->sched_scan_req) {
3953 err = -EINPROGRESS;
3954 goto out;
3955 }
3956
Luciano Coelho807f8a82011-05-11 17:09:35 +03003957 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003958 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003959 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003960 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003961 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003962 if (!request) {
3963 err = -ENOMEM;
3964 goto out;
3965 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003966
3967 if (n_ssids)
3968 request->ssids = (void *)&request->channels[n_channels];
3969 request->n_ssids = n_ssids;
3970 if (ie_len) {
3971 if (request->ssids)
3972 request->ie = (void *)(request->ssids + n_ssids);
3973 else
3974 request->ie = (void *)(request->channels + n_channels);
3975 }
3976
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003977 if (n_match_sets) {
3978 if (request->ie)
3979 request->match_sets = (void *)(request->ie + ie_len);
3980 else if (request->ssids)
3981 request->match_sets =
3982 (void *)(request->ssids + n_ssids);
3983 else
3984 request->match_sets =
3985 (void *)(request->channels + n_channels);
3986 }
3987 request->n_match_sets = n_match_sets;
3988
Luciano Coelho807f8a82011-05-11 17:09:35 +03003989 i = 0;
3990 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3991 /* user specified, bail out if channel not found */
3992 nla_for_each_nested(attr,
3993 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3994 tmp) {
3995 struct ieee80211_channel *chan;
3996
3997 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3998
3999 if (!chan) {
4000 err = -EINVAL;
4001 goto out_free;
4002 }
4003
4004 /* ignore disabled channels */
4005 if (chan->flags & IEEE80211_CHAN_DISABLED)
4006 continue;
4007
4008 request->channels[i] = chan;
4009 i++;
4010 }
4011 } else {
4012 /* all channels */
4013 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4014 int j;
4015 if (!wiphy->bands[band])
4016 continue;
4017 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4018 struct ieee80211_channel *chan;
4019
4020 chan = &wiphy->bands[band]->channels[j];
4021
4022 if (chan->flags & IEEE80211_CHAN_DISABLED)
4023 continue;
4024
4025 request->channels[i] = chan;
4026 i++;
4027 }
4028 }
4029 }
4030
4031 if (!i) {
4032 err = -EINVAL;
4033 goto out_free;
4034 }
4035
4036 request->n_channels = i;
4037
4038 i = 0;
4039 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4040 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4041 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004042 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004043 err = -EINVAL;
4044 goto out_free;
4045 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004046 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004047 memcpy(request->ssids[i].ssid, nla_data(attr),
4048 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004049 i++;
4050 }
4051 }
4052
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004053 i = 0;
4054 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4055 nla_for_each_nested(attr,
4056 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4057 tmp) {
4058 struct nlattr *ssid;
4059
4060 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4061 nla_data(attr), nla_len(attr),
4062 nl80211_match_policy);
4063 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4064 if (ssid) {
4065 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4066 err = -EINVAL;
4067 goto out_free;
4068 }
4069 memcpy(request->match_sets[i].ssid.ssid,
4070 nla_data(ssid), nla_len(ssid));
4071 request->match_sets[i].ssid.ssid_len =
4072 nla_len(ssid);
4073 }
4074 i++;
4075 }
4076 }
4077
Luciano Coelho807f8a82011-05-11 17:09:35 +03004078 if (info->attrs[NL80211_ATTR_IE]) {
4079 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4080 memcpy((void *)request->ie,
4081 nla_data(info->attrs[NL80211_ATTR_IE]),
4082 request->ie_len);
4083 }
4084
4085 request->dev = dev;
4086 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004087 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004088
4089 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4090 if (!err) {
4091 rdev->sched_scan_req = request;
4092 nl80211_send_sched_scan(rdev, dev,
4093 NL80211_CMD_START_SCHED_SCAN);
4094 goto out;
4095 }
4096
4097out_free:
4098 kfree(request);
4099out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004100 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004101 return err;
4102}
4103
4104static int nl80211_stop_sched_scan(struct sk_buff *skb,
4105 struct genl_info *info)
4106{
4107 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004108 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004109
4110 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4111 !rdev->ops->sched_scan_stop)
4112 return -EOPNOTSUPP;
4113
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004114 mutex_lock(&rdev->sched_scan_mtx);
4115 err = __cfg80211_stop_sched_scan(rdev, false);
4116 mutex_unlock(&rdev->sched_scan_mtx);
4117
4118 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004119}
4120
Johannes Berg9720bb32011-06-21 09:45:33 +02004121static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4122 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004123 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004124 struct wireless_dev *wdev,
4125 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004126{
Johannes Berg48ab9052009-07-10 18:42:31 +02004127 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004128 void *hdr;
4129 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004130
4131 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004132
Johannes Berg9720bb32011-06-21 09:45:33 +02004133 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004134 NL80211_CMD_NEW_SCAN_RESULTS);
4135 if (!hdr)
4136 return -1;
4137
Johannes Berg9720bb32011-06-21 09:45:33 +02004138 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4139
Johannes Bergf5ea9122009-08-07 16:17:38 +02004140 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02004141 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01004142
4143 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4144 if (!bss)
4145 goto nla_put_failure;
4146 if (!is_zero_ether_addr(res->bssid))
4147 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4148 if (res->information_elements && res->len_information_elements)
4149 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4150 res->len_information_elements,
4151 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004152 if (res->beacon_ies && res->len_beacon_ies &&
4153 res->beacon_ies != res->information_elements)
4154 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4155 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004156 if (res->tsf)
4157 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4158 if (res->beacon_interval)
4159 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4160 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4161 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004162 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4163 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004164
Johannes Berg77965c972009-02-18 18:45:06 +01004165 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004166 case CFG80211_SIGNAL_TYPE_MBM:
4167 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4168 break;
4169 case CFG80211_SIGNAL_TYPE_UNSPEC:
4170 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4171 break;
4172 default:
4173 break;
4174 }
4175
Johannes Berg48ab9052009-07-10 18:42:31 +02004176 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004177 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004178 case NL80211_IFTYPE_STATION:
4179 if (intbss == wdev->current_bss)
4180 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4181 NL80211_BSS_STATUS_ASSOCIATED);
Johannes Berg48ab9052009-07-10 18:42:31 +02004182 break;
4183 case NL80211_IFTYPE_ADHOC:
4184 if (intbss == wdev->current_bss)
4185 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4186 NL80211_BSS_STATUS_IBSS_JOINED);
4187 break;
4188 default:
4189 break;
4190 }
4191
Johannes Berg2a519312009-02-10 21:25:55 +01004192 nla_nest_end(msg, bss);
4193
4194 return genlmsg_end(msg, hdr);
4195
4196 nla_put_failure:
4197 genlmsg_cancel(msg, hdr);
4198 return -EMSGSIZE;
4199}
4200
4201static int nl80211_dump_scan(struct sk_buff *skb,
4202 struct netlink_callback *cb)
4203{
Johannes Berg48ab9052009-07-10 18:42:31 +02004204 struct cfg80211_registered_device *rdev;
4205 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004206 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004207 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004208 int start = cb->args[1], idx = 0;
4209 int err;
4210
Johannes Berg67748892010-10-04 21:14:06 +02004211 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4212 if (err)
4213 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004214
Johannes Berg48ab9052009-07-10 18:42:31 +02004215 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004216
Johannes Berg48ab9052009-07-10 18:42:31 +02004217 wdev_lock(wdev);
4218 spin_lock_bh(&rdev->bss_lock);
4219 cfg80211_bss_expire(rdev);
4220
Johannes Berg9720bb32011-06-21 09:45:33 +02004221 cb->seq = rdev->bss_generation;
4222
Johannes Berg48ab9052009-07-10 18:42:31 +02004223 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004224 if (++idx <= start)
4225 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004226 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004227 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004228 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004229 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004230 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004231 }
4232 }
4233
Johannes Berg48ab9052009-07-10 18:42:31 +02004234 spin_unlock_bh(&rdev->bss_lock);
4235 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004236
4237 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004238 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004239
Johannes Berg67748892010-10-04 21:14:06 +02004240 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004241}
4242
Holger Schurig61fa7132009-11-11 12:25:40 +01004243static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4244 int flags, struct net_device *dev,
4245 struct survey_info *survey)
4246{
4247 void *hdr;
4248 struct nlattr *infoattr;
4249
Holger Schurig61fa7132009-11-11 12:25:40 +01004250 hdr = nl80211hdr_put(msg, pid, seq, flags,
4251 NL80211_CMD_NEW_SURVEY_RESULTS);
4252 if (!hdr)
4253 return -ENOMEM;
4254
4255 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4256
4257 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4258 if (!infoattr)
4259 goto nla_put_failure;
4260
4261 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4262 survey->channel->center_freq);
4263 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4264 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4265 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004266 if (survey->filled & SURVEY_INFO_IN_USE)
4267 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004268 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4269 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4270 survey->channel_time);
4271 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4272 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4273 survey->channel_time_busy);
4274 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4275 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4276 survey->channel_time_ext_busy);
4277 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4278 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4279 survey->channel_time_rx);
4280 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4281 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4282 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004283
4284 nla_nest_end(msg, infoattr);
4285
4286 return genlmsg_end(msg, hdr);
4287
4288 nla_put_failure:
4289 genlmsg_cancel(msg, hdr);
4290 return -EMSGSIZE;
4291}
4292
4293static int nl80211_dump_survey(struct sk_buff *skb,
4294 struct netlink_callback *cb)
4295{
4296 struct survey_info survey;
4297 struct cfg80211_registered_device *dev;
4298 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004299 int survey_idx = cb->args[1];
4300 int res;
4301
Johannes Berg67748892010-10-04 21:14:06 +02004302 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4303 if (res)
4304 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004305
4306 if (!dev->ops->dump_survey) {
4307 res = -EOPNOTSUPP;
4308 goto out_err;
4309 }
4310
4311 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004312 struct ieee80211_channel *chan;
4313
Holger Schurig61fa7132009-11-11 12:25:40 +01004314 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4315 &survey);
4316 if (res == -ENOENT)
4317 break;
4318 if (res)
4319 goto out_err;
4320
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004321 /* Survey without a channel doesn't make sense */
4322 if (!survey.channel) {
4323 res = -EINVAL;
4324 goto out;
4325 }
4326
4327 chan = ieee80211_get_channel(&dev->wiphy,
4328 survey.channel->center_freq);
4329 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4330 survey_idx++;
4331 continue;
4332 }
4333
Holger Schurig61fa7132009-11-11 12:25:40 +01004334 if (nl80211_send_survey(skb,
4335 NETLINK_CB(cb->skb).pid,
4336 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4337 netdev,
4338 &survey) < 0)
4339 goto out;
4340 survey_idx++;
4341 }
4342
4343 out:
4344 cb->args[1] = survey_idx;
4345 res = skb->len;
4346 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004347 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004348 return res;
4349}
4350
Jouni Malinen255e7372009-03-20 21:21:17 +02004351static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4352{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004353 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004354}
4355
Samuel Ortizb23aa672009-07-01 21:26:54 +02004356static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4357{
4358 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4359 NL80211_WPA_VERSION_2));
4360}
4361
Jouni Malinen636a5d32009-03-19 13:39:22 +02004362static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4363{
Johannes Berg4c476992010-10-04 21:36:35 +02004364 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4365 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004366 struct ieee80211_channel *chan;
4367 const u8 *bssid, *ssid, *ie = NULL;
4368 int err, ssid_len, ie_len = 0;
4369 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004370 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004371 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004372
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004373 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4374 return -EINVAL;
4375
4376 if (!info->attrs[NL80211_ATTR_MAC])
4377 return -EINVAL;
4378
Jouni Malinen17780922009-03-27 20:52:47 +02004379 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4380 return -EINVAL;
4381
Johannes Berg19957bb2009-07-02 17:20:43 +02004382 if (!info->attrs[NL80211_ATTR_SSID])
4383 return -EINVAL;
4384
4385 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4386 return -EINVAL;
4387
Johannes Bergfffd0932009-07-08 14:22:54 +02004388 err = nl80211_parse_key(info, &key);
4389 if (err)
4390 return err;
4391
4392 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004393 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4394 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004395 if (!key.p.key || !key.p.key_len)
4396 return -EINVAL;
4397 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4398 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4399 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4400 key.p.key_len != WLAN_KEY_LEN_WEP104))
4401 return -EINVAL;
4402 if (key.idx > 4)
4403 return -EINVAL;
4404 } else {
4405 key.p.key_len = 0;
4406 key.p.key = NULL;
4407 }
4408
Johannes Bergafea0b72010-08-10 09:46:42 +02004409 if (key.idx >= 0) {
4410 int i;
4411 bool ok = false;
4412 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4413 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4414 ok = true;
4415 break;
4416 }
4417 }
Johannes Berg4c476992010-10-04 21:36:35 +02004418 if (!ok)
4419 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004420 }
4421
Johannes Berg4c476992010-10-04 21:36:35 +02004422 if (!rdev->ops->auth)
4423 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004424
Johannes Berg074ac8d2010-09-16 14:58:22 +02004425 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004426 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4427 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004428
Johannes Berg19957bb2009-07-02 17:20:43 +02004429 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004430 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004431 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004432 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4433 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004434
Johannes Berg19957bb2009-07-02 17:20:43 +02004435 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4436 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4437
4438 if (info->attrs[NL80211_ATTR_IE]) {
4439 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4440 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4441 }
4442
4443 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004444 if (!nl80211_valid_auth_type(auth_type))
4445 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004446
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004447 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4448
Johannes Berg95de8172012-01-20 13:55:25 +01004449 /*
4450 * Since we no longer track auth state, ignore
4451 * requests to only change local state.
4452 */
4453 if (local_state_change)
4454 return 0;
4455
Johannes Berg4c476992010-10-04 21:36:35 +02004456 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4457 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004458 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004459}
4460
Johannes Bergc0692b82010-08-27 14:26:53 +03004461static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4462 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004463 struct cfg80211_crypto_settings *settings,
4464 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004465{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004466 memset(settings, 0, sizeof(*settings));
4467
Samuel Ortizb23aa672009-07-01 21:26:54 +02004468 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4469
Johannes Bergc0692b82010-08-27 14:26:53 +03004470 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4471 u16 proto;
4472 proto = nla_get_u16(
4473 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4474 settings->control_port_ethertype = cpu_to_be16(proto);
4475 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4476 proto != ETH_P_PAE)
4477 return -EINVAL;
4478 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4479 settings->control_port_no_encrypt = true;
4480 } else
4481 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4482
Samuel Ortizb23aa672009-07-01 21:26:54 +02004483 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4484 void *data;
4485 int len, i;
4486
4487 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4488 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4489 settings->n_ciphers_pairwise = len / sizeof(u32);
4490
4491 if (len % sizeof(u32))
4492 return -EINVAL;
4493
Johannes Berg3dc27d22009-07-02 21:36:37 +02004494 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004495 return -EINVAL;
4496
4497 memcpy(settings->ciphers_pairwise, data, len);
4498
4499 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004500 if (!cfg80211_supported_cipher_suite(
4501 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004502 settings->ciphers_pairwise[i]))
4503 return -EINVAL;
4504 }
4505
4506 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4507 settings->cipher_group =
4508 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004509 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4510 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004511 return -EINVAL;
4512 }
4513
4514 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4515 settings->wpa_versions =
4516 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4517 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4518 return -EINVAL;
4519 }
4520
4521 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4522 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004523 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004524
4525 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4526 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4527 settings->n_akm_suites = len / sizeof(u32);
4528
4529 if (len % sizeof(u32))
4530 return -EINVAL;
4531
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004532 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4533 return -EINVAL;
4534
Samuel Ortizb23aa672009-07-01 21:26:54 +02004535 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004536 }
4537
4538 return 0;
4539}
4540
Jouni Malinen636a5d32009-03-19 13:39:22 +02004541static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4542{
Johannes Berg4c476992010-10-04 21:36:35 +02004543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4544 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004545 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004546 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004547 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004548 int err, ssid_len, ie_len = 0;
4549 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004550 u32 flags = 0;
4551 struct ieee80211_ht_cap *ht_capa = NULL;
4552 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004553
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004554 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4555 return -EINVAL;
4556
4557 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004558 !info->attrs[NL80211_ATTR_SSID] ||
4559 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004560 return -EINVAL;
4561
Johannes Berg4c476992010-10-04 21:36:35 +02004562 if (!rdev->ops->assoc)
4563 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004564
Johannes Berg074ac8d2010-09-16 14:58:22 +02004565 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004566 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4567 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004568
Johannes Berg19957bb2009-07-02 17:20:43 +02004569 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004570
Johannes Berg19957bb2009-07-02 17:20:43 +02004571 chan = ieee80211_get_channel(&rdev->wiphy,
4572 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004573 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4574 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004575
Johannes Berg19957bb2009-07-02 17:20:43 +02004576 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4577 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004578
4579 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004580 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4581 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004582 }
4583
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004584 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004585 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004586 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004587 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004588 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004589 else if (mfp != NL80211_MFP_NO)
4590 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004591 }
4592
Johannes Berg3e5d7642009-07-07 14:37:26 +02004593 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4594 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4595
Ben Greear7e7c8922011-11-18 11:31:59 -08004596 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4597 flags |= ASSOC_REQ_DISABLE_HT;
4598
4599 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4600 ht_capa_mask =
4601 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4602
4603 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4604 if (!ht_capa_mask)
4605 return -EINVAL;
4606 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4607 }
4608
Johannes Bergc0692b82010-08-27 14:26:53 +03004609 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004610 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004611 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4612 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004613 &crypto, flags, ht_capa,
4614 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004615
Jouni Malinen636a5d32009-03-19 13:39:22 +02004616 return err;
4617}
4618
4619static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4620{
Johannes Berg4c476992010-10-04 21:36:35 +02004621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4622 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004623 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004624 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004625 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004626 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004627
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004628 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4629 return -EINVAL;
4630
4631 if (!info->attrs[NL80211_ATTR_MAC])
4632 return -EINVAL;
4633
4634 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4635 return -EINVAL;
4636
Johannes Berg4c476992010-10-04 21:36:35 +02004637 if (!rdev->ops->deauth)
4638 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004639
Johannes Berg074ac8d2010-09-16 14:58:22 +02004640 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004641 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4642 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004643
Johannes Berg19957bb2009-07-02 17:20:43 +02004644 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004645
Johannes Berg19957bb2009-07-02 17:20:43 +02004646 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4647 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004648 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004649 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004650 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004651
4652 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004653 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4654 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004655 }
4656
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004657 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4658
Johannes Berg4c476992010-10-04 21:36:35 +02004659 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4660 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004661}
4662
4663static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4664{
Johannes Berg4c476992010-10-04 21:36:35 +02004665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4666 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004667 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004668 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004669 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004670 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004671
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004672 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4673 return -EINVAL;
4674
4675 if (!info->attrs[NL80211_ATTR_MAC])
4676 return -EINVAL;
4677
4678 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4679 return -EINVAL;
4680
Johannes Berg4c476992010-10-04 21:36:35 +02004681 if (!rdev->ops->disassoc)
4682 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004683
Johannes Berg074ac8d2010-09-16 14:58:22 +02004684 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004685 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4686 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004687
Johannes Berg19957bb2009-07-02 17:20:43 +02004688 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004689
Johannes Berg19957bb2009-07-02 17:20:43 +02004690 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4691 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004692 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004693 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004694 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004695
4696 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004697 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4698 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004699 }
4700
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004701 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4702
Johannes Berg4c476992010-10-04 21:36:35 +02004703 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4704 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004705}
4706
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004707static bool
4708nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4709 int mcast_rate[IEEE80211_NUM_BANDS],
4710 int rateval)
4711{
4712 struct wiphy *wiphy = &rdev->wiphy;
4713 bool found = false;
4714 int band, i;
4715
4716 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4717 struct ieee80211_supported_band *sband;
4718
4719 sband = wiphy->bands[band];
4720 if (!sband)
4721 continue;
4722
4723 for (i = 0; i < sband->n_bitrates; i++) {
4724 if (sband->bitrates[i].bitrate == rateval) {
4725 mcast_rate[band] = i + 1;
4726 found = true;
4727 break;
4728 }
4729 }
4730 }
4731
4732 return found;
4733}
4734
Johannes Berg04a773a2009-04-19 21:24:32 +02004735static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4736{
Johannes Berg4c476992010-10-04 21:36:35 +02004737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4738 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004739 struct cfg80211_ibss_params ibss;
4740 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004741 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004742 int err;
4743
Johannes Berg8e30bc52009-04-22 17:45:38 +02004744 memset(&ibss, 0, sizeof(ibss));
4745
Johannes Berg04a773a2009-04-19 21:24:32 +02004746 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4747 return -EINVAL;
4748
4749 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4750 !info->attrs[NL80211_ATTR_SSID] ||
4751 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4752 return -EINVAL;
4753
Johannes Berg8e30bc52009-04-22 17:45:38 +02004754 ibss.beacon_interval = 100;
4755
4756 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4757 ibss.beacon_interval =
4758 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4759 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4760 return -EINVAL;
4761 }
4762
Johannes Berg4c476992010-10-04 21:36:35 +02004763 if (!rdev->ops->join_ibss)
4764 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004765
Johannes Berg4c476992010-10-04 21:36:35 +02004766 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4767 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004768
Johannes Berg79c97e92009-07-07 03:56:12 +02004769 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004770
Johannes Berg39193492011-09-16 13:45:25 +02004771 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004772 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004773
4774 if (!is_valid_ether_addr(ibss.bssid))
4775 return -EINVAL;
4776 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004777 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4778 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4779
4780 if (info->attrs[NL80211_ATTR_IE]) {
4781 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4782 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4783 }
4784
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004785 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4786 enum nl80211_channel_type channel_type;
4787
4788 channel_type = nla_get_u32(
4789 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4790 if (channel_type != NL80211_CHAN_NO_HT &&
4791 channel_type != NL80211_CHAN_HT20 &&
4792 channel_type != NL80211_CHAN_HT40MINUS &&
4793 channel_type != NL80211_CHAN_HT40PLUS)
4794 return -EINVAL;
4795
4796 if (channel_type != NL80211_CHAN_NO_HT &&
4797 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4798 return -EINVAL;
4799
4800 ibss.channel_type = channel_type;
4801 } else {
4802 ibss.channel_type = NL80211_CHAN_NO_HT;
4803 }
4804
4805 ibss.channel = rdev_freq_to_chan(rdev,
4806 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4807 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004808 if (!ibss.channel ||
4809 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004810 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4811 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004812
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004813 /* Both channels should be able to initiate communication */
4814 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4815 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4816 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4817 ibss.channel_type))
4818 return -EINVAL;
4819
Johannes Berg04a773a2009-04-19 21:24:32 +02004820 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004821 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004822
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004823 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4824 u8 *rates =
4825 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4826 int n_rates =
4827 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4828 struct ieee80211_supported_band *sband =
4829 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004830
Johannes Berg34850ab2011-07-18 18:08:35 +02004831 err = ieee80211_get_ratemask(sband, rates, n_rates,
4832 &ibss.basic_rates);
4833 if (err)
4834 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004835 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004836
4837 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4838 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4839 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4840 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004841
Johannes Berg4c476992010-10-04 21:36:35 +02004842 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4843 connkeys = nl80211_parse_connkeys(rdev,
4844 info->attrs[NL80211_ATTR_KEYS]);
4845 if (IS_ERR(connkeys))
4846 return PTR_ERR(connkeys);
4847 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004848
Antonio Quartulli267335d2012-01-31 20:25:47 +01004849 ibss.control_port =
4850 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4851
Johannes Berg4c476992010-10-04 21:36:35 +02004852 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004853 if (err)
4854 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004855 return err;
4856}
4857
4858static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4859{
Johannes Berg4c476992010-10-04 21:36:35 +02004860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4861 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004862
Johannes Berg4c476992010-10-04 21:36:35 +02004863 if (!rdev->ops->leave_ibss)
4864 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004865
Johannes Berg4c476992010-10-04 21:36:35 +02004866 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4867 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004868
Johannes Berg4c476992010-10-04 21:36:35 +02004869 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004870}
4871
Johannes Bergaff89a92009-07-01 21:26:51 +02004872#ifdef CONFIG_NL80211_TESTMODE
4873static struct genl_multicast_group nl80211_testmode_mcgrp = {
4874 .name = "testmode",
4875};
4876
4877static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4878{
Johannes Berg4c476992010-10-04 21:36:35 +02004879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004880 int err;
4881
4882 if (!info->attrs[NL80211_ATTR_TESTDATA])
4883 return -EINVAL;
4884
Johannes Bergaff89a92009-07-01 21:26:51 +02004885 err = -EOPNOTSUPP;
4886 if (rdev->ops->testmode_cmd) {
4887 rdev->testmode_info = info;
4888 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4889 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4890 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4891 rdev->testmode_info = NULL;
4892 }
4893
Johannes Bergaff89a92009-07-01 21:26:51 +02004894 return err;
4895}
4896
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004897static int nl80211_testmode_dump(struct sk_buff *skb,
4898 struct netlink_callback *cb)
4899{
Johannes Berg00918d32011-12-13 17:22:05 +01004900 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004901 int err;
4902 long phy_idx;
4903 void *data = NULL;
4904 int data_len = 0;
4905
4906 if (cb->args[0]) {
4907 /*
4908 * 0 is a valid index, but not valid for args[0],
4909 * so we need to offset by 1.
4910 */
4911 phy_idx = cb->args[0] - 1;
4912 } else {
4913 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4914 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4915 nl80211_policy);
4916 if (err)
4917 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01004918 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
4919 phy_idx = nla_get_u32(
4920 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4921 } else {
4922 struct net_device *netdev;
4923
4924 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
4925 nl80211_fam.attrbuf,
4926 &rdev, &netdev);
4927 if (err)
4928 return err;
4929 dev_put(netdev);
4930 phy_idx = rdev->wiphy_idx;
4931 cfg80211_unlock_rdev(rdev);
4932 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004933 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4934 cb->args[1] =
4935 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4936 }
4937
4938 if (cb->args[1]) {
4939 data = nla_data((void *)cb->args[1]);
4940 data_len = nla_len((void *)cb->args[1]);
4941 }
4942
4943 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01004944 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4945 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004946 mutex_unlock(&cfg80211_mutex);
4947 return -ENOENT;
4948 }
Johannes Berg00918d32011-12-13 17:22:05 +01004949 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004950 mutex_unlock(&cfg80211_mutex);
4951
Johannes Berg00918d32011-12-13 17:22:05 +01004952 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004953 err = -EOPNOTSUPP;
4954 goto out_err;
4955 }
4956
4957 while (1) {
4958 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4959 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4960 NL80211_CMD_TESTMODE);
4961 struct nlattr *tmdata;
4962
Johannes Berg00918d32011-12-13 17:22:05 +01004963 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004964 genlmsg_cancel(skb, hdr);
4965 break;
4966 }
4967
4968 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4969 if (!tmdata) {
4970 genlmsg_cancel(skb, hdr);
4971 break;
4972 }
Johannes Berg00918d32011-12-13 17:22:05 +01004973 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
4974 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004975 nla_nest_end(skb, tmdata);
4976
4977 if (err == -ENOBUFS || err == -ENOENT) {
4978 genlmsg_cancel(skb, hdr);
4979 break;
4980 } else if (err) {
4981 genlmsg_cancel(skb, hdr);
4982 goto out_err;
4983 }
4984
4985 genlmsg_end(skb, hdr);
4986 }
4987
4988 err = skb->len;
4989 /* see above */
4990 cb->args[0] = phy_idx + 1;
4991 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01004992 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004993 return err;
4994}
4995
Johannes Bergaff89a92009-07-01 21:26:51 +02004996static struct sk_buff *
4997__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4998 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4999{
5000 struct sk_buff *skb;
5001 void *hdr;
5002 struct nlattr *data;
5003
5004 skb = nlmsg_new(approxlen + 100, gfp);
5005 if (!skb)
5006 return NULL;
5007
5008 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5009 if (!hdr) {
5010 kfree_skb(skb);
5011 return NULL;
5012 }
5013
5014 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5015 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5016
5017 ((void **)skb->cb)[0] = rdev;
5018 ((void **)skb->cb)[1] = hdr;
5019 ((void **)skb->cb)[2] = data;
5020
5021 return skb;
5022
5023 nla_put_failure:
5024 kfree_skb(skb);
5025 return NULL;
5026}
5027
5028struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5029 int approxlen)
5030{
5031 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5032
5033 if (WARN_ON(!rdev->testmode_info))
5034 return NULL;
5035
5036 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5037 rdev->testmode_info->snd_pid,
5038 rdev->testmode_info->snd_seq,
5039 GFP_KERNEL);
5040}
5041EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5042
5043int cfg80211_testmode_reply(struct sk_buff *skb)
5044{
5045 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5046 void *hdr = ((void **)skb->cb)[1];
5047 struct nlattr *data = ((void **)skb->cb)[2];
5048
5049 if (WARN_ON(!rdev->testmode_info)) {
5050 kfree_skb(skb);
5051 return -EINVAL;
5052 }
5053
5054 nla_nest_end(skb, data);
5055 genlmsg_end(skb, hdr);
5056 return genlmsg_reply(skb, rdev->testmode_info);
5057}
5058EXPORT_SYMBOL(cfg80211_testmode_reply);
5059
5060struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5061 int approxlen, gfp_t gfp)
5062{
5063 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5064
5065 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5066}
5067EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5068
5069void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5070{
5071 void *hdr = ((void **)skb->cb)[1];
5072 struct nlattr *data = ((void **)skb->cb)[2];
5073
5074 nla_nest_end(skb, data);
5075 genlmsg_end(skb, hdr);
5076 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5077}
5078EXPORT_SYMBOL(cfg80211_testmode_event);
5079#endif
5080
Samuel Ortizb23aa672009-07-01 21:26:54 +02005081static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5082{
Johannes Berg4c476992010-10-04 21:36:35 +02005083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5084 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005085 struct cfg80211_connect_params connect;
5086 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005087 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005088 int err;
5089
5090 memset(&connect, 0, sizeof(connect));
5091
5092 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5093 return -EINVAL;
5094
5095 if (!info->attrs[NL80211_ATTR_SSID] ||
5096 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5097 return -EINVAL;
5098
5099 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5100 connect.auth_type =
5101 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5102 if (!nl80211_valid_auth_type(connect.auth_type))
5103 return -EINVAL;
5104 } else
5105 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5106
5107 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5108
Johannes Bergc0692b82010-08-27 14:26:53 +03005109 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005110 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005111 if (err)
5112 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005113
Johannes Berg074ac8d2010-09-16 14:58:22 +02005114 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005115 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5116 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005117
Johannes Berg79c97e92009-07-07 03:56:12 +02005118 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005119
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305120 connect.bg_scan_period = -1;
5121 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5122 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5123 connect.bg_scan_period =
5124 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5125 }
5126
Samuel Ortizb23aa672009-07-01 21:26:54 +02005127 if (info->attrs[NL80211_ATTR_MAC])
5128 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5129 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5130 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5131
5132 if (info->attrs[NL80211_ATTR_IE]) {
5133 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5134 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5135 }
5136
5137 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5138 connect.channel =
5139 ieee80211_get_channel(wiphy,
5140 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5141 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005142 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5143 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005144 }
5145
Johannes Bergfffd0932009-07-08 14:22:54 +02005146 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5147 connkeys = nl80211_parse_connkeys(rdev,
5148 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005149 if (IS_ERR(connkeys))
5150 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005151 }
5152
Ben Greear7e7c8922011-11-18 11:31:59 -08005153 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5154 connect.flags |= ASSOC_REQ_DISABLE_HT;
5155
5156 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5157 memcpy(&connect.ht_capa_mask,
5158 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5159 sizeof(connect.ht_capa_mask));
5160
5161 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5162 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5163 return -EINVAL;
5164 memcpy(&connect.ht_capa,
5165 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5166 sizeof(connect.ht_capa));
5167 }
5168
Johannes Bergfffd0932009-07-08 14:22:54 +02005169 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005170 if (err)
5171 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005172 return err;
5173}
5174
5175static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5176{
Johannes Berg4c476992010-10-04 21:36:35 +02005177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5178 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005179 u16 reason;
5180
5181 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5182 reason = WLAN_REASON_DEAUTH_LEAVING;
5183 else
5184 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5185
5186 if (reason == 0)
5187 return -EINVAL;
5188
Johannes Berg074ac8d2010-09-16 14:58:22 +02005189 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005190 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5191 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005192
Johannes Berg4c476992010-10-04 21:36:35 +02005193 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005194}
5195
Johannes Berg463d0182009-07-14 00:33:35 +02005196static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5197{
Johannes Berg4c476992010-10-04 21:36:35 +02005198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005199 struct net *net;
5200 int err;
5201 u32 pid;
5202
5203 if (!info->attrs[NL80211_ATTR_PID])
5204 return -EINVAL;
5205
5206 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5207
Johannes Berg463d0182009-07-14 00:33:35 +02005208 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005209 if (IS_ERR(net))
5210 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005211
5212 err = 0;
5213
5214 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005215 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5216 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005217
Johannes Berg463d0182009-07-14 00:33:35 +02005218 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005219 return err;
5220}
5221
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005222static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5223{
Johannes Berg4c476992010-10-04 21:36:35 +02005224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005225 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5226 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005227 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005228 struct cfg80211_pmksa pmksa;
5229
5230 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5231
5232 if (!info->attrs[NL80211_ATTR_MAC])
5233 return -EINVAL;
5234
5235 if (!info->attrs[NL80211_ATTR_PMKID])
5236 return -EINVAL;
5237
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005238 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5239 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5240
Johannes Berg074ac8d2010-09-16 14:58:22 +02005241 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005242 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5243 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005244
5245 switch (info->genlhdr->cmd) {
5246 case NL80211_CMD_SET_PMKSA:
5247 rdev_ops = rdev->ops->set_pmksa;
5248 break;
5249 case NL80211_CMD_DEL_PMKSA:
5250 rdev_ops = rdev->ops->del_pmksa;
5251 break;
5252 default:
5253 WARN_ON(1);
5254 break;
5255 }
5256
Johannes Berg4c476992010-10-04 21:36:35 +02005257 if (!rdev_ops)
5258 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005259
Johannes Berg4c476992010-10-04 21:36:35 +02005260 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005261}
5262
5263static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5264{
Johannes Berg4c476992010-10-04 21:36:35 +02005265 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5266 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005267
Johannes Berg074ac8d2010-09-16 14:58:22 +02005268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005269 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5270 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005271
Johannes Berg4c476992010-10-04 21:36:35 +02005272 if (!rdev->ops->flush_pmksa)
5273 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005274
Johannes Berg4c476992010-10-04 21:36:35 +02005275 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005276}
5277
Arik Nemtsov109086c2011-09-28 14:12:50 +03005278static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5279{
5280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5281 struct net_device *dev = info->user_ptr[1];
5282 u8 action_code, dialog_token;
5283 u16 status_code;
5284 u8 *peer;
5285
5286 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5287 !rdev->ops->tdls_mgmt)
5288 return -EOPNOTSUPP;
5289
5290 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5291 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5292 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5293 !info->attrs[NL80211_ATTR_IE] ||
5294 !info->attrs[NL80211_ATTR_MAC])
5295 return -EINVAL;
5296
5297 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5298 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5299 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5300 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5301
5302 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5303 dialog_token, status_code,
5304 nla_data(info->attrs[NL80211_ATTR_IE]),
5305 nla_len(info->attrs[NL80211_ATTR_IE]));
5306}
5307
5308static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5309{
5310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5311 struct net_device *dev = info->user_ptr[1];
5312 enum nl80211_tdls_operation operation;
5313 u8 *peer;
5314
5315 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5316 !rdev->ops->tdls_oper)
5317 return -EOPNOTSUPP;
5318
5319 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5320 !info->attrs[NL80211_ATTR_MAC])
5321 return -EINVAL;
5322
5323 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5324 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5325
5326 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5327}
5328
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005329static int nl80211_remain_on_channel(struct sk_buff *skb,
5330 struct genl_info *info)
5331{
Johannes Berg4c476992010-10-04 21:36:35 +02005332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5333 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005334 struct ieee80211_channel *chan;
5335 struct sk_buff *msg;
5336 void *hdr;
5337 u64 cookie;
5338 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5339 u32 freq, duration;
5340 int err;
5341
5342 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5343 !info->attrs[NL80211_ATTR_DURATION])
5344 return -EINVAL;
5345
5346 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5347
5348 /*
5349 * We should be on that channel for at least one jiffie,
5350 * and more than 5 seconds seems excessive.
5351 */
Johannes Berga2939112010-12-14 17:54:28 +01005352 if (!duration || !msecs_to_jiffies(duration) ||
5353 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005354 return -EINVAL;
5355
Johannes Berg7c4ef712011-11-18 15:33:48 +01005356 if (!rdev->ops->remain_on_channel ||
5357 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005358 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005359
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005360 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5361 channel_type = nla_get_u32(
5362 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5363 if (channel_type != NL80211_CHAN_NO_HT &&
5364 channel_type != NL80211_CHAN_HT20 &&
5365 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005366 channel_type != NL80211_CHAN_HT40MINUS)
5367 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005368 }
5369
5370 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5371 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005372 if (chan == NULL)
5373 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005374
5375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005376 if (!msg)
5377 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005378
5379 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5380 NL80211_CMD_REMAIN_ON_CHANNEL);
5381
5382 if (IS_ERR(hdr)) {
5383 err = PTR_ERR(hdr);
5384 goto free_msg;
5385 }
5386
5387 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5388 channel_type, duration, &cookie);
5389
5390 if (err)
5391 goto free_msg;
5392
5393 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5394
5395 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005396
5397 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005398
5399 nla_put_failure:
5400 err = -ENOBUFS;
5401 free_msg:
5402 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005403 return err;
5404}
5405
5406static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5407 struct genl_info *info)
5408{
Johannes Berg4c476992010-10-04 21:36:35 +02005409 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5410 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005411 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005412
5413 if (!info->attrs[NL80211_ATTR_COOKIE])
5414 return -EINVAL;
5415
Johannes Berg4c476992010-10-04 21:36:35 +02005416 if (!rdev->ops->cancel_remain_on_channel)
5417 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005418
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005419 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5420
Johannes Berg4c476992010-10-04 21:36:35 +02005421 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005422}
5423
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005424static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5425 u8 *rates, u8 rates_len)
5426{
5427 u8 i;
5428 u32 mask = 0;
5429
5430 for (i = 0; i < rates_len; i++) {
5431 int rate = (rates[i] & 0x7f) * 5;
5432 int ridx;
5433 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5434 struct ieee80211_rate *srate =
5435 &sband->bitrates[ridx];
5436 if (rate == srate->bitrate) {
5437 mask |= 1 << ridx;
5438 break;
5439 }
5440 }
5441 if (ridx == sband->n_bitrates)
5442 return 0; /* rate not found */
5443 }
5444
5445 return mask;
5446}
5447
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005448static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5449 u8 *rates, u8 rates_len,
5450 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5451{
5452 u8 i;
5453
5454 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5455
5456 for (i = 0; i < rates_len; i++) {
5457 int ridx, rbit;
5458
5459 ridx = rates[i] / 8;
5460 rbit = BIT(rates[i] % 8);
5461
5462 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005463 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005464 return false;
5465
5466 /* check availability */
5467 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5468 mcs[ridx] |= rbit;
5469 else
5470 return false;
5471 }
5472
5473 return true;
5474}
5475
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005476static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005477 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5478 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005479 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5480 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005481};
5482
5483static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5484 struct genl_info *info)
5485{
5486 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005488 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005489 int rem, i;
5490 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005491 struct nlattr *tx_rates;
5492 struct ieee80211_supported_band *sband;
5493
5494 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5495 return -EINVAL;
5496
Johannes Berg4c476992010-10-04 21:36:35 +02005497 if (!rdev->ops->set_bitrate_mask)
5498 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005499
5500 memset(&mask, 0, sizeof(mask));
5501 /* Default to all rates enabled */
5502 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5503 sband = rdev->wiphy.bands[i];
5504 mask.control[i].legacy =
5505 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005506 if (sband)
5507 memcpy(mask.control[i].mcs,
5508 sband->ht_cap.mcs.rx_mask,
5509 sizeof(mask.control[i].mcs));
5510 else
5511 memset(mask.control[i].mcs, 0,
5512 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005513 }
5514
5515 /*
5516 * The nested attribute uses enum nl80211_band as the index. This maps
5517 * directly to the enum ieee80211_band values used in cfg80211.
5518 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005519 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005520 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5521 {
5522 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005523 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5524 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005525 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005526 if (sband == NULL)
5527 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005528 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5529 nla_len(tx_rates), nl80211_txattr_policy);
5530 if (tb[NL80211_TXRATE_LEGACY]) {
5531 mask.control[band].legacy = rateset_to_mask(
5532 sband,
5533 nla_data(tb[NL80211_TXRATE_LEGACY]),
5534 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005535 }
5536 if (tb[NL80211_TXRATE_MCS]) {
5537 if (!ht_rateset_to_mask(
5538 sband,
5539 nla_data(tb[NL80211_TXRATE_MCS]),
5540 nla_len(tb[NL80211_TXRATE_MCS]),
5541 mask.control[band].mcs))
5542 return -EINVAL;
5543 }
5544
5545 if (mask.control[band].legacy == 0) {
5546 /* don't allow empty legacy rates if HT
5547 * is not even supported. */
5548 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5549 return -EINVAL;
5550
5551 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5552 if (mask.control[band].mcs[i])
5553 break;
5554
5555 /* legacy and mcs rates may not be both empty */
5556 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005557 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005558 }
5559 }
5560
Johannes Berg4c476992010-10-04 21:36:35 +02005561 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005562}
5563
Johannes Berg2e161f782010-08-12 15:38:38 +02005564static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005565{
Johannes Berg4c476992010-10-04 21:36:35 +02005566 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5567 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f782010-08-12 15:38:38 +02005568 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005569
5570 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5571 return -EINVAL;
5572
Johannes Berg2e161f782010-08-12 15:38:38 +02005573 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5574 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005575
Johannes Berg9d38d852010-06-09 17:20:33 +02005576 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005577 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005578 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5579 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5580 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005581 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005582 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5583 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005584
5585 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005586 if (!rdev->ops->mgmt_tx)
5587 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005588
Johannes Berg4c476992010-10-04 21:36:35 +02005589 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f782010-08-12 15:38:38 +02005590 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005591 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5592 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005593}
5594
Johannes Berg2e161f782010-08-12 15:38:38 +02005595static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005596{
Johannes Berg4c476992010-10-04 21:36:35 +02005597 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5598 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005599 struct ieee80211_channel *chan;
5600 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005601 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005602 u32 freq;
5603 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005604 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005605 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005606 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005607 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005608 bool offchan, no_cck, dont_wait_for_ack;
5609
5610 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005611
5612 if (!info->attrs[NL80211_ATTR_FRAME] ||
5613 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5614 return -EINVAL;
5615
Johannes Berg4c476992010-10-04 21:36:35 +02005616 if (!rdev->ops->mgmt_tx)
5617 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005618
Johannes Berg9d38d852010-06-09 17:20:33 +02005619 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005620 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005621 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5622 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5623 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005624 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005625 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5626 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005627
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005628 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005629 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005630 return -EINVAL;
5631 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5632 }
5633
Jouni Malinen026331c2010-02-15 12:53:10 +02005634 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5635 channel_type = nla_get_u32(
5636 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5637 if (channel_type != NL80211_CHAN_NO_HT &&
5638 channel_type != NL80211_CHAN_HT20 &&
5639 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005640 channel_type != NL80211_CHAN_HT40MINUS)
5641 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005642 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005643 }
5644
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005645 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5646
Johannes Berg7c4ef712011-11-18 15:33:48 +01005647 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5648 return -EINVAL;
5649
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305650 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5651
Jouni Malinen026331c2010-02-15 12:53:10 +02005652 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5653 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005654 if (chan == NULL)
5655 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005656
Johannes Berge247bd902011-11-04 11:18:21 +01005657 if (!dont_wait_for_ack) {
5658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5659 if (!msg)
5660 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005661
Johannes Berge247bd902011-11-04 11:18:21 +01005662 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5663 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005664
Johannes Berge247bd902011-11-04 11:18:21 +01005665 if (IS_ERR(hdr)) {
5666 err = PTR_ERR(hdr);
5667 goto free_msg;
5668 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005669 }
Johannes Berge247bd902011-11-04 11:18:21 +01005670
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005671 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5672 channel_type_valid, wait,
Johannes Berg2e161f782010-08-12 15:38:38 +02005673 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5674 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005675 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005676 if (err)
5677 goto free_msg;
5678
Johannes Berge247bd902011-11-04 11:18:21 +01005679 if (msg) {
5680 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005681
Johannes Berge247bd902011-11-04 11:18:21 +01005682 genlmsg_end(msg, hdr);
5683 return genlmsg_reply(msg, info);
5684 }
5685
5686 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005687
5688 nla_put_failure:
5689 err = -ENOBUFS;
5690 free_msg:
5691 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005692 return err;
5693}
5694
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005695static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5696{
5697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5698 struct net_device *dev = info->user_ptr[1];
5699 u64 cookie;
5700
5701 if (!info->attrs[NL80211_ATTR_COOKIE])
5702 return -EINVAL;
5703
5704 if (!rdev->ops->mgmt_tx_cancel_wait)
5705 return -EOPNOTSUPP;
5706
5707 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5708 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5709 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5710 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5711 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5712 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5713 return -EOPNOTSUPP;
5714
5715 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5716
5717 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5718}
5719
Kalle Valoffb9eb32010-02-17 17:58:10 +02005720static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5721{
Johannes Berg4c476992010-10-04 21:36:35 +02005722 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005723 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005724 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005725 u8 ps_state;
5726 bool state;
5727 int err;
5728
Johannes Berg4c476992010-10-04 21:36:35 +02005729 if (!info->attrs[NL80211_ATTR_PS_STATE])
5730 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005731
5732 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5733
Johannes Berg4c476992010-10-04 21:36:35 +02005734 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5735 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005736
5737 wdev = dev->ieee80211_ptr;
5738
Johannes Berg4c476992010-10-04 21:36:35 +02005739 if (!rdev->ops->set_power_mgmt)
5740 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005741
5742 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5743
5744 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005745 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005746
Johannes Berg4c476992010-10-04 21:36:35 +02005747 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5748 wdev->ps_timeout);
5749 if (!err)
5750 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005751 return err;
5752}
5753
5754static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5755{
Johannes Berg4c476992010-10-04 21:36:35 +02005756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005757 enum nl80211_ps_state ps_state;
5758 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005759 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005760 struct sk_buff *msg;
5761 void *hdr;
5762 int err;
5763
Kalle Valoffb9eb32010-02-17 17:58:10 +02005764 wdev = dev->ieee80211_ptr;
5765
Johannes Berg4c476992010-10-04 21:36:35 +02005766 if (!rdev->ops->set_power_mgmt)
5767 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005768
5769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005770 if (!msg)
5771 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005772
5773 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5774 NL80211_CMD_GET_POWER_SAVE);
5775 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005776 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005777 goto free_msg;
5778 }
5779
5780 if (wdev->ps)
5781 ps_state = NL80211_PS_ENABLED;
5782 else
5783 ps_state = NL80211_PS_DISABLED;
5784
5785 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5786
5787 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005788 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005789
Johannes Berg4c476992010-10-04 21:36:35 +02005790 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005791 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005792 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005793 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005794 return err;
5795}
5796
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005797static struct nla_policy
5798nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5799 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5800 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5801 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5802};
5803
5804static int nl80211_set_cqm_rssi(struct genl_info *info,
5805 s32 threshold, u32 hysteresis)
5806{
Johannes Berg4c476992010-10-04 21:36:35 +02005807 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005808 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005809 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005810
5811 if (threshold > 0)
5812 return -EINVAL;
5813
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005814 wdev = dev->ieee80211_ptr;
5815
Johannes Berg4c476992010-10-04 21:36:35 +02005816 if (!rdev->ops->set_cqm_rssi_config)
5817 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005818
Johannes Berg074ac8d2010-09-16 14:58:22 +02005819 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005820 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5821 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005822
Johannes Berg4c476992010-10-04 21:36:35 +02005823 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5824 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005825}
5826
5827static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5828{
5829 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5830 struct nlattr *cqm;
5831 int err;
5832
5833 cqm = info->attrs[NL80211_ATTR_CQM];
5834 if (!cqm) {
5835 err = -EINVAL;
5836 goto out;
5837 }
5838
5839 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5840 nl80211_attr_cqm_policy);
5841 if (err)
5842 goto out;
5843
5844 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5845 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5846 s32 threshold;
5847 u32 hysteresis;
5848 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5849 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5850 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5851 } else
5852 err = -EINVAL;
5853
5854out:
5855 return err;
5856}
5857
Johannes Berg29cbe682010-12-03 09:20:44 +01005858static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5859{
5860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5861 struct net_device *dev = info->user_ptr[1];
5862 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005863 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005864 int err;
5865
5866 /* start with default */
5867 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005868 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005869
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005870 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005871 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005872 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005873 if (err)
5874 return err;
5875 }
5876
5877 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5878 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5879 return -EINVAL;
5880
Javier Cardonac80d5452010-12-16 17:37:49 -08005881 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5882 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5883
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08005884 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5885 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5886 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5887 return -EINVAL;
5888
Javier Cardonac80d5452010-12-16 17:37:49 -08005889 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5890 /* parse additional setup parameters if given */
5891 err = nl80211_parse_mesh_setup(info, &setup);
5892 if (err)
5893 return err;
5894 }
5895
5896 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005897}
5898
5899static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5900{
5901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5902 struct net_device *dev = info->user_ptr[1];
5903
5904 return cfg80211_leave_mesh(rdev, dev);
5905}
5906
Johannes Bergff1b6e62011-05-04 15:37:28 +02005907static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5908{
5909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5910 struct sk_buff *msg;
5911 void *hdr;
5912
5913 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5914 return -EOPNOTSUPP;
5915
5916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5917 if (!msg)
5918 return -ENOMEM;
5919
5920 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5921 NL80211_CMD_GET_WOWLAN);
5922 if (!hdr)
5923 goto nla_put_failure;
5924
5925 if (rdev->wowlan) {
5926 struct nlattr *nl_wowlan;
5927
5928 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5929 if (!nl_wowlan)
5930 goto nla_put_failure;
5931
5932 if (rdev->wowlan->any)
5933 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5934 if (rdev->wowlan->disconnect)
5935 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5936 if (rdev->wowlan->magic_pkt)
5937 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005938 if (rdev->wowlan->gtk_rekey_failure)
5939 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5940 if (rdev->wowlan->eap_identity_req)
5941 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5942 if (rdev->wowlan->four_way_handshake)
5943 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5944 if (rdev->wowlan->rfkill_release)
5945 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005946 if (rdev->wowlan->n_patterns) {
5947 struct nlattr *nl_pats, *nl_pat;
5948 int i, pat_len;
5949
5950 nl_pats = nla_nest_start(msg,
5951 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5952 if (!nl_pats)
5953 goto nla_put_failure;
5954
5955 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5956 nl_pat = nla_nest_start(msg, i + 1);
5957 if (!nl_pat)
5958 goto nla_put_failure;
5959 pat_len = rdev->wowlan->patterns[i].pattern_len;
5960 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5961 DIV_ROUND_UP(pat_len, 8),
5962 rdev->wowlan->patterns[i].mask);
5963 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5964 pat_len,
5965 rdev->wowlan->patterns[i].pattern);
5966 nla_nest_end(msg, nl_pat);
5967 }
5968 nla_nest_end(msg, nl_pats);
5969 }
5970
5971 nla_nest_end(msg, nl_wowlan);
5972 }
5973
5974 genlmsg_end(msg, hdr);
5975 return genlmsg_reply(msg, info);
5976
5977nla_put_failure:
5978 nlmsg_free(msg);
5979 return -ENOBUFS;
5980}
5981
5982static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5983{
5984 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5985 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5986 struct cfg80211_wowlan no_triggers = {};
5987 struct cfg80211_wowlan new_triggers = {};
5988 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5989 int err, i;
5990
5991 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5992 return -EOPNOTSUPP;
5993
5994 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5995 goto no_triggers;
5996
5997 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5998 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5999 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6000 nl80211_wowlan_policy);
6001 if (err)
6002 return err;
6003
6004 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6005 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6006 return -EINVAL;
6007 new_triggers.any = true;
6008 }
6009
6010 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6011 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6012 return -EINVAL;
6013 new_triggers.disconnect = true;
6014 }
6015
6016 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6017 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6018 return -EINVAL;
6019 new_triggers.magic_pkt = true;
6020 }
6021
Johannes Berg77dbbb12011-07-13 10:48:55 +02006022 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6023 return -EINVAL;
6024
6025 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6026 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6027 return -EINVAL;
6028 new_triggers.gtk_rekey_failure = true;
6029 }
6030
6031 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6032 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6033 return -EINVAL;
6034 new_triggers.eap_identity_req = true;
6035 }
6036
6037 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6038 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6039 return -EINVAL;
6040 new_triggers.four_way_handshake = true;
6041 }
6042
6043 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6044 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6045 return -EINVAL;
6046 new_triggers.rfkill_release = true;
6047 }
6048
Johannes Bergff1b6e62011-05-04 15:37:28 +02006049 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6050 struct nlattr *pat;
6051 int n_patterns = 0;
6052 int rem, pat_len, mask_len;
6053 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6054
6055 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6056 rem)
6057 n_patterns++;
6058 if (n_patterns > wowlan->n_patterns)
6059 return -EINVAL;
6060
6061 new_triggers.patterns = kcalloc(n_patterns,
6062 sizeof(new_triggers.patterns[0]),
6063 GFP_KERNEL);
6064 if (!new_triggers.patterns)
6065 return -ENOMEM;
6066
6067 new_triggers.n_patterns = n_patterns;
6068 i = 0;
6069
6070 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6071 rem) {
6072 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6073 nla_data(pat), nla_len(pat), NULL);
6074 err = -EINVAL;
6075 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6076 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6077 goto error;
6078 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6079 mask_len = DIV_ROUND_UP(pat_len, 8);
6080 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6081 mask_len)
6082 goto error;
6083 if (pat_len > wowlan->pattern_max_len ||
6084 pat_len < wowlan->pattern_min_len)
6085 goto error;
6086
6087 new_triggers.patterns[i].mask =
6088 kmalloc(mask_len + pat_len, GFP_KERNEL);
6089 if (!new_triggers.patterns[i].mask) {
6090 err = -ENOMEM;
6091 goto error;
6092 }
6093 new_triggers.patterns[i].pattern =
6094 new_triggers.patterns[i].mask + mask_len;
6095 memcpy(new_triggers.patterns[i].mask,
6096 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6097 mask_len);
6098 new_triggers.patterns[i].pattern_len = pat_len;
6099 memcpy(new_triggers.patterns[i].pattern,
6100 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6101 pat_len);
6102 i++;
6103 }
6104 }
6105
6106 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6107 struct cfg80211_wowlan *ntrig;
6108 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6109 GFP_KERNEL);
6110 if (!ntrig) {
6111 err = -ENOMEM;
6112 goto error;
6113 }
6114 cfg80211_rdev_free_wowlan(rdev);
6115 rdev->wowlan = ntrig;
6116 } else {
6117 no_triggers:
6118 cfg80211_rdev_free_wowlan(rdev);
6119 rdev->wowlan = NULL;
6120 }
6121
6122 return 0;
6123 error:
6124 for (i = 0; i < new_triggers.n_patterns; i++)
6125 kfree(new_triggers.patterns[i].mask);
6126 kfree(new_triggers.patterns);
6127 return err;
6128}
6129
Johannes Berge5497d72011-07-05 16:35:40 +02006130static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6131{
6132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6133 struct net_device *dev = info->user_ptr[1];
6134 struct wireless_dev *wdev = dev->ieee80211_ptr;
6135 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6136 struct cfg80211_gtk_rekey_data rekey_data;
6137 int err;
6138
6139 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6140 return -EINVAL;
6141
6142 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6143 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6144 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6145 nl80211_rekey_policy);
6146 if (err)
6147 return err;
6148
6149 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6150 return -ERANGE;
6151 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6152 return -ERANGE;
6153 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6154 return -ERANGE;
6155
6156 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6157 NL80211_KEK_LEN);
6158 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6159 NL80211_KCK_LEN);
6160 memcpy(rekey_data.replay_ctr,
6161 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6162 NL80211_REPLAY_CTR_LEN);
6163
6164 wdev_lock(wdev);
6165 if (!wdev->current_bss) {
6166 err = -ENOTCONN;
6167 goto out;
6168 }
6169
6170 if (!rdev->ops->set_rekey_data) {
6171 err = -EOPNOTSUPP;
6172 goto out;
6173 }
6174
6175 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6176 out:
6177 wdev_unlock(wdev);
6178 return err;
6179}
6180
Johannes Berg28946da2011-11-04 11:18:12 +01006181static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6182 struct genl_info *info)
6183{
6184 struct net_device *dev = info->user_ptr[1];
6185 struct wireless_dev *wdev = dev->ieee80211_ptr;
6186
6187 if (wdev->iftype != NL80211_IFTYPE_AP &&
6188 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6189 return -EINVAL;
6190
6191 if (wdev->ap_unexpected_nlpid)
6192 return -EBUSY;
6193
6194 wdev->ap_unexpected_nlpid = info->snd_pid;
6195 return 0;
6196}
6197
Johannes Berg7f6cf312011-11-04 11:18:15 +01006198static int nl80211_probe_client(struct sk_buff *skb,
6199 struct genl_info *info)
6200{
6201 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6202 struct net_device *dev = info->user_ptr[1];
6203 struct wireless_dev *wdev = dev->ieee80211_ptr;
6204 struct sk_buff *msg;
6205 void *hdr;
6206 const u8 *addr;
6207 u64 cookie;
6208 int err;
6209
6210 if (wdev->iftype != NL80211_IFTYPE_AP &&
6211 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6212 return -EOPNOTSUPP;
6213
6214 if (!info->attrs[NL80211_ATTR_MAC])
6215 return -EINVAL;
6216
6217 if (!rdev->ops->probe_client)
6218 return -EOPNOTSUPP;
6219
6220 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6221 if (!msg)
6222 return -ENOMEM;
6223
6224 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6225 NL80211_CMD_PROBE_CLIENT);
6226
6227 if (IS_ERR(hdr)) {
6228 err = PTR_ERR(hdr);
6229 goto free_msg;
6230 }
6231
6232 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6233
6234 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6235 if (err)
6236 goto free_msg;
6237
6238 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6239
6240 genlmsg_end(msg, hdr);
6241
6242 return genlmsg_reply(msg, info);
6243
6244 nla_put_failure:
6245 err = -ENOBUFS;
6246 free_msg:
6247 nlmsg_free(msg);
6248 return err;
6249}
6250
Johannes Berg5e760232011-11-04 11:18:17 +01006251static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6252{
6253 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6254
6255 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6256 return -EOPNOTSUPP;
6257
6258 if (rdev->ap_beacons_nlpid)
6259 return -EBUSY;
6260
6261 rdev->ap_beacons_nlpid = info->snd_pid;
6262
6263 return 0;
6264}
6265
Johannes Berg4c476992010-10-04 21:36:35 +02006266#define NL80211_FLAG_NEED_WIPHY 0x01
6267#define NL80211_FLAG_NEED_NETDEV 0x02
6268#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006269#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6270#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6271 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006272
6273static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6274 struct genl_info *info)
6275{
6276 struct cfg80211_registered_device *rdev;
6277 struct net_device *dev;
6278 int err;
6279 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6280
6281 if (rtnl)
6282 rtnl_lock();
6283
6284 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6285 rdev = cfg80211_get_dev_from_info(info);
6286 if (IS_ERR(rdev)) {
6287 if (rtnl)
6288 rtnl_unlock();
6289 return PTR_ERR(rdev);
6290 }
6291 info->user_ptr[0] = rdev;
6292 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006293 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6294 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006295 if (err) {
6296 if (rtnl)
6297 rtnl_unlock();
6298 return err;
6299 }
Johannes Berg41265712010-10-04 21:14:05 +02006300 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6301 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006302 cfg80211_unlock_rdev(rdev);
6303 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006304 if (rtnl)
6305 rtnl_unlock();
6306 return -ENETDOWN;
6307 }
Johannes Berg4c476992010-10-04 21:36:35 +02006308 info->user_ptr[0] = rdev;
6309 info->user_ptr[1] = dev;
6310 }
6311
6312 return 0;
6313}
6314
6315static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6316 struct genl_info *info)
6317{
6318 if (info->user_ptr[0])
6319 cfg80211_unlock_rdev(info->user_ptr[0]);
6320 if (info->user_ptr[1])
6321 dev_put(info->user_ptr[1]);
6322 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6323 rtnl_unlock();
6324}
6325
Johannes Berg55682962007-09-20 13:09:35 -04006326static struct genl_ops nl80211_ops[] = {
6327 {
6328 .cmd = NL80211_CMD_GET_WIPHY,
6329 .doit = nl80211_get_wiphy,
6330 .dumpit = nl80211_dump_wiphy,
6331 .policy = nl80211_policy,
6332 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006333 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006334 },
6335 {
6336 .cmd = NL80211_CMD_SET_WIPHY,
6337 .doit = nl80211_set_wiphy,
6338 .policy = nl80211_policy,
6339 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006340 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006341 },
6342 {
6343 .cmd = NL80211_CMD_GET_INTERFACE,
6344 .doit = nl80211_get_interface,
6345 .dumpit = nl80211_dump_interface,
6346 .policy = nl80211_policy,
6347 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006348 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006349 },
6350 {
6351 .cmd = NL80211_CMD_SET_INTERFACE,
6352 .doit = nl80211_set_interface,
6353 .policy = nl80211_policy,
6354 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006355 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6356 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006357 },
6358 {
6359 .cmd = NL80211_CMD_NEW_INTERFACE,
6360 .doit = nl80211_new_interface,
6361 .policy = nl80211_policy,
6362 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006363 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6364 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006365 },
6366 {
6367 .cmd = NL80211_CMD_DEL_INTERFACE,
6368 .doit = nl80211_del_interface,
6369 .policy = nl80211_policy,
6370 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006371 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6372 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006373 },
Johannes Berg41ade002007-12-19 02:03:29 +01006374 {
6375 .cmd = NL80211_CMD_GET_KEY,
6376 .doit = nl80211_get_key,
6377 .policy = nl80211_policy,
6378 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006379 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6380 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006381 },
6382 {
6383 .cmd = NL80211_CMD_SET_KEY,
6384 .doit = nl80211_set_key,
6385 .policy = nl80211_policy,
6386 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006387 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006388 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006389 },
6390 {
6391 .cmd = NL80211_CMD_NEW_KEY,
6392 .doit = nl80211_new_key,
6393 .policy = nl80211_policy,
6394 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006395 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006396 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006397 },
6398 {
6399 .cmd = NL80211_CMD_DEL_KEY,
6400 .doit = nl80211_del_key,
6401 .policy = nl80211_policy,
6402 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006403 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006404 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006405 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006406 {
6407 .cmd = NL80211_CMD_SET_BEACON,
6408 .policy = nl80211_policy,
6409 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006410 .doit = nl80211_set_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006411 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6412 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006413 },
6414 {
Johannes Berg88600202012-02-13 15:17:18 +01006415 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006416 .policy = nl80211_policy,
6417 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006418 .doit = nl80211_start_ap,
Johannes Berg4c476992010-10-04 21:36:35 +02006419 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6420 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006421 },
6422 {
Johannes Berg88600202012-02-13 15:17:18 +01006423 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006424 .policy = nl80211_policy,
6425 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006426 .doit = nl80211_stop_ap,
Johannes Berg4c476992010-10-04 21:36:35 +02006427 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6428 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006429 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006430 {
6431 .cmd = NL80211_CMD_GET_STATION,
6432 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006433 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006434 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006435 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6436 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006437 },
6438 {
6439 .cmd = NL80211_CMD_SET_STATION,
6440 .doit = nl80211_set_station,
6441 .policy = nl80211_policy,
6442 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006443 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6444 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006445 },
6446 {
6447 .cmd = NL80211_CMD_NEW_STATION,
6448 .doit = nl80211_new_station,
6449 .policy = nl80211_policy,
6450 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006451 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006452 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006453 },
6454 {
6455 .cmd = NL80211_CMD_DEL_STATION,
6456 .doit = nl80211_del_station,
6457 .policy = nl80211_policy,
6458 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006459 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6460 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006461 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006462 {
6463 .cmd = NL80211_CMD_GET_MPATH,
6464 .doit = nl80211_get_mpath,
6465 .dumpit = nl80211_dump_mpath,
6466 .policy = nl80211_policy,
6467 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006468 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006469 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006470 },
6471 {
6472 .cmd = NL80211_CMD_SET_MPATH,
6473 .doit = nl80211_set_mpath,
6474 .policy = nl80211_policy,
6475 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006476 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006477 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006478 },
6479 {
6480 .cmd = NL80211_CMD_NEW_MPATH,
6481 .doit = nl80211_new_mpath,
6482 .policy = nl80211_policy,
6483 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006484 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006485 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006486 },
6487 {
6488 .cmd = NL80211_CMD_DEL_MPATH,
6489 .doit = nl80211_del_mpath,
6490 .policy = nl80211_policy,
6491 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006492 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6493 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006494 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006495 {
6496 .cmd = NL80211_CMD_SET_BSS,
6497 .doit = nl80211_set_bss,
6498 .policy = nl80211_policy,
6499 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006500 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6501 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006502 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006503 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006504 .cmd = NL80211_CMD_GET_REG,
6505 .doit = nl80211_get_reg,
6506 .policy = nl80211_policy,
6507 /* can be retrieved by unprivileged users */
6508 },
6509 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006510 .cmd = NL80211_CMD_SET_REG,
6511 .doit = nl80211_set_reg,
6512 .policy = nl80211_policy,
6513 .flags = GENL_ADMIN_PERM,
6514 },
6515 {
6516 .cmd = NL80211_CMD_REQ_SET_REG,
6517 .doit = nl80211_req_set_reg,
6518 .policy = nl80211_policy,
6519 .flags = GENL_ADMIN_PERM,
6520 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006521 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006522 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6523 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006524 .policy = nl80211_policy,
6525 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006526 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6527 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006528 },
6529 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006530 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6531 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006532 .policy = nl80211_policy,
6533 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006535 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006536 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006537 {
Johannes Berg2a519312009-02-10 21:25:55 +01006538 .cmd = NL80211_CMD_TRIGGER_SCAN,
6539 .doit = nl80211_trigger_scan,
6540 .policy = nl80211_policy,
6541 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006543 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006544 },
6545 {
6546 .cmd = NL80211_CMD_GET_SCAN,
6547 .policy = nl80211_policy,
6548 .dumpit = nl80211_dump_scan,
6549 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006550 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006551 .cmd = NL80211_CMD_START_SCHED_SCAN,
6552 .doit = nl80211_start_sched_scan,
6553 .policy = nl80211_policy,
6554 .flags = GENL_ADMIN_PERM,
6555 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6556 NL80211_FLAG_NEED_RTNL,
6557 },
6558 {
6559 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6560 .doit = nl80211_stop_sched_scan,
6561 .policy = nl80211_policy,
6562 .flags = GENL_ADMIN_PERM,
6563 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6564 NL80211_FLAG_NEED_RTNL,
6565 },
6566 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006567 .cmd = NL80211_CMD_AUTHENTICATE,
6568 .doit = nl80211_authenticate,
6569 .policy = nl80211_policy,
6570 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006571 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006572 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006573 },
6574 {
6575 .cmd = NL80211_CMD_ASSOCIATE,
6576 .doit = nl80211_associate,
6577 .policy = nl80211_policy,
6578 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006579 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006580 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006581 },
6582 {
6583 .cmd = NL80211_CMD_DEAUTHENTICATE,
6584 .doit = nl80211_deauthenticate,
6585 .policy = nl80211_policy,
6586 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006587 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006588 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006589 },
6590 {
6591 .cmd = NL80211_CMD_DISASSOCIATE,
6592 .doit = nl80211_disassociate,
6593 .policy = nl80211_policy,
6594 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006595 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006596 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006597 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006598 {
6599 .cmd = NL80211_CMD_JOIN_IBSS,
6600 .doit = nl80211_join_ibss,
6601 .policy = nl80211_policy,
6602 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006603 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006604 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006605 },
6606 {
6607 .cmd = NL80211_CMD_LEAVE_IBSS,
6608 .doit = nl80211_leave_ibss,
6609 .policy = nl80211_policy,
6610 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006611 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006612 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006613 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006614#ifdef CONFIG_NL80211_TESTMODE
6615 {
6616 .cmd = NL80211_CMD_TESTMODE,
6617 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006618 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006619 .policy = nl80211_policy,
6620 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006621 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6622 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006623 },
6624#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006625 {
6626 .cmd = NL80211_CMD_CONNECT,
6627 .doit = nl80211_connect,
6628 .policy = nl80211_policy,
6629 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006630 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006631 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006632 },
6633 {
6634 .cmd = NL80211_CMD_DISCONNECT,
6635 .doit = nl80211_disconnect,
6636 .policy = nl80211_policy,
6637 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006638 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006639 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006640 },
Johannes Berg463d0182009-07-14 00:33:35 +02006641 {
6642 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6643 .doit = nl80211_wiphy_netns,
6644 .policy = nl80211_policy,
6645 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006646 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6647 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006648 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006649 {
6650 .cmd = NL80211_CMD_GET_SURVEY,
6651 .policy = nl80211_policy,
6652 .dumpit = nl80211_dump_survey,
6653 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006654 {
6655 .cmd = NL80211_CMD_SET_PMKSA,
6656 .doit = nl80211_setdel_pmksa,
6657 .policy = nl80211_policy,
6658 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006659 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6660 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006661 },
6662 {
6663 .cmd = NL80211_CMD_DEL_PMKSA,
6664 .doit = nl80211_setdel_pmksa,
6665 .policy = nl80211_policy,
6666 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006667 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6668 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006669 },
6670 {
6671 .cmd = NL80211_CMD_FLUSH_PMKSA,
6672 .doit = nl80211_flush_pmksa,
6673 .policy = nl80211_policy,
6674 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006675 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6676 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006677 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006678 {
6679 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6680 .doit = nl80211_remain_on_channel,
6681 .policy = nl80211_policy,
6682 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006683 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006684 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006685 },
6686 {
6687 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6688 .doit = nl80211_cancel_remain_on_channel,
6689 .policy = nl80211_policy,
6690 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006691 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006692 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006693 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006694 {
6695 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6696 .doit = nl80211_set_tx_bitrate_mask,
6697 .policy = nl80211_policy,
6698 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006699 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6700 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006701 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006702 {
Johannes Berg2e161f782010-08-12 15:38:38 +02006703 .cmd = NL80211_CMD_REGISTER_FRAME,
6704 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006705 .policy = nl80211_policy,
6706 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006707 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6708 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006709 },
6710 {
Johannes Berg2e161f782010-08-12 15:38:38 +02006711 .cmd = NL80211_CMD_FRAME,
6712 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006713 .policy = nl80211_policy,
6714 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006715 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006716 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006717 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006718 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006719 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6720 .doit = nl80211_tx_mgmt_cancel_wait,
6721 .policy = nl80211_policy,
6722 .flags = GENL_ADMIN_PERM,
6723 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6724 NL80211_FLAG_NEED_RTNL,
6725 },
6726 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006727 .cmd = NL80211_CMD_SET_POWER_SAVE,
6728 .doit = nl80211_set_power_save,
6729 .policy = nl80211_policy,
6730 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006731 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6732 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006733 },
6734 {
6735 .cmd = NL80211_CMD_GET_POWER_SAVE,
6736 .doit = nl80211_get_power_save,
6737 .policy = nl80211_policy,
6738 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006739 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6740 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006741 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006742 {
6743 .cmd = NL80211_CMD_SET_CQM,
6744 .doit = nl80211_set_cqm,
6745 .policy = nl80211_policy,
6746 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006747 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6748 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006749 },
Johannes Bergf444de02010-05-05 15:25:02 +02006750 {
6751 .cmd = NL80211_CMD_SET_CHANNEL,
6752 .doit = nl80211_set_channel,
6753 .policy = nl80211_policy,
6754 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006755 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6756 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006757 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006758 {
6759 .cmd = NL80211_CMD_SET_WDS_PEER,
6760 .doit = nl80211_set_wds_peer,
6761 .policy = nl80211_policy,
6762 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006763 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6764 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006765 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006766 {
6767 .cmd = NL80211_CMD_JOIN_MESH,
6768 .doit = nl80211_join_mesh,
6769 .policy = nl80211_policy,
6770 .flags = GENL_ADMIN_PERM,
6771 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6772 NL80211_FLAG_NEED_RTNL,
6773 },
6774 {
6775 .cmd = NL80211_CMD_LEAVE_MESH,
6776 .doit = nl80211_leave_mesh,
6777 .policy = nl80211_policy,
6778 .flags = GENL_ADMIN_PERM,
6779 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6780 NL80211_FLAG_NEED_RTNL,
6781 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006782 {
6783 .cmd = NL80211_CMD_GET_WOWLAN,
6784 .doit = nl80211_get_wowlan,
6785 .policy = nl80211_policy,
6786 /* can be retrieved by unprivileged users */
6787 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6788 NL80211_FLAG_NEED_RTNL,
6789 },
6790 {
6791 .cmd = NL80211_CMD_SET_WOWLAN,
6792 .doit = nl80211_set_wowlan,
6793 .policy = nl80211_policy,
6794 .flags = GENL_ADMIN_PERM,
6795 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6796 NL80211_FLAG_NEED_RTNL,
6797 },
Johannes Berge5497d72011-07-05 16:35:40 +02006798 {
6799 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6800 .doit = nl80211_set_rekey_data,
6801 .policy = nl80211_policy,
6802 .flags = GENL_ADMIN_PERM,
6803 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6804 NL80211_FLAG_NEED_RTNL,
6805 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006806 {
6807 .cmd = NL80211_CMD_TDLS_MGMT,
6808 .doit = nl80211_tdls_mgmt,
6809 .policy = nl80211_policy,
6810 .flags = GENL_ADMIN_PERM,
6811 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6812 NL80211_FLAG_NEED_RTNL,
6813 },
6814 {
6815 .cmd = NL80211_CMD_TDLS_OPER,
6816 .doit = nl80211_tdls_oper,
6817 .policy = nl80211_policy,
6818 .flags = GENL_ADMIN_PERM,
6819 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6820 NL80211_FLAG_NEED_RTNL,
6821 },
Johannes Berg28946da2011-11-04 11:18:12 +01006822 {
6823 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6824 .doit = nl80211_register_unexpected_frame,
6825 .policy = nl80211_policy,
6826 .flags = GENL_ADMIN_PERM,
6827 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6828 NL80211_FLAG_NEED_RTNL,
6829 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006830 {
6831 .cmd = NL80211_CMD_PROBE_CLIENT,
6832 .doit = nl80211_probe_client,
6833 .policy = nl80211_policy,
6834 .flags = GENL_ADMIN_PERM,
6835 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6836 NL80211_FLAG_NEED_RTNL,
6837 },
Johannes Berg5e760232011-11-04 11:18:17 +01006838 {
6839 .cmd = NL80211_CMD_REGISTER_BEACONS,
6840 .doit = nl80211_register_beacons,
6841 .policy = nl80211_policy,
6842 .flags = GENL_ADMIN_PERM,
6843 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6844 NL80211_FLAG_NEED_RTNL,
6845 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006846 {
6847 .cmd = NL80211_CMD_SET_NOACK_MAP,
6848 .doit = nl80211_set_noack_map,
6849 .policy = nl80211_policy,
6850 .flags = GENL_ADMIN_PERM,
6851 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6852 NL80211_FLAG_NEED_RTNL,
6853 },
6854
Johannes Berg55682962007-09-20 13:09:35 -04006855};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006856
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006857static struct genl_multicast_group nl80211_mlme_mcgrp = {
6858 .name = "mlme",
6859};
Johannes Berg55682962007-09-20 13:09:35 -04006860
6861/* multicast groups */
6862static struct genl_multicast_group nl80211_config_mcgrp = {
6863 .name = "config",
6864};
Johannes Berg2a519312009-02-10 21:25:55 +01006865static struct genl_multicast_group nl80211_scan_mcgrp = {
6866 .name = "scan",
6867};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006868static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6869 .name = "regulatory",
6870};
Johannes Berg55682962007-09-20 13:09:35 -04006871
6872/* notification functions */
6873
6874void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6875{
6876 struct sk_buff *msg;
6877
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006879 if (!msg)
6880 return;
6881
6882 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6883 nlmsg_free(msg);
6884 return;
6885 }
6886
Johannes Berg463d0182009-07-14 00:33:35 +02006887 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6888 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006889}
6890
Johannes Berg362a4152009-05-24 16:43:15 +02006891static int nl80211_add_scan_req(struct sk_buff *msg,
6892 struct cfg80211_registered_device *rdev)
6893{
6894 struct cfg80211_scan_request *req = rdev->scan_req;
6895 struct nlattr *nest;
6896 int i;
6897
Johannes Berg667503d2009-07-07 03:56:11 +02006898 ASSERT_RDEV_LOCK(rdev);
6899
Johannes Berg362a4152009-05-24 16:43:15 +02006900 if (WARN_ON(!req))
6901 return 0;
6902
6903 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6904 if (!nest)
6905 goto nla_put_failure;
6906 for (i = 0; i < req->n_ssids; i++)
6907 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6908 nla_nest_end(msg, nest);
6909
6910 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6911 if (!nest)
6912 goto nla_put_failure;
6913 for (i = 0; i < req->n_channels; i++)
6914 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6915 nla_nest_end(msg, nest);
6916
6917 if (req->ie)
6918 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6919
6920 return 0;
6921 nla_put_failure:
6922 return -ENOBUFS;
6923}
6924
Johannes Berga538e2d2009-06-16 19:56:42 +02006925static int nl80211_send_scan_msg(struct sk_buff *msg,
6926 struct cfg80211_registered_device *rdev,
6927 struct net_device *netdev,
6928 u32 pid, u32 seq, int flags,
6929 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006930{
6931 void *hdr;
6932
6933 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6934 if (!hdr)
6935 return -1;
6936
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006937 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006938 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6939
Johannes Berg362a4152009-05-24 16:43:15 +02006940 /* ignore errors and send incomplete event anyway */
6941 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006942
6943 return genlmsg_end(msg, hdr);
6944
6945 nla_put_failure:
6946 genlmsg_cancel(msg, hdr);
6947 return -EMSGSIZE;
6948}
6949
Luciano Coelho807f8a82011-05-11 17:09:35 +03006950static int
6951nl80211_send_sched_scan_msg(struct sk_buff *msg,
6952 struct cfg80211_registered_device *rdev,
6953 struct net_device *netdev,
6954 u32 pid, u32 seq, int flags, u32 cmd)
6955{
6956 void *hdr;
6957
6958 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6959 if (!hdr)
6960 return -1;
6961
6962 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6963 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6964
6965 return genlmsg_end(msg, hdr);
6966
6967 nla_put_failure:
6968 genlmsg_cancel(msg, hdr);
6969 return -EMSGSIZE;
6970}
6971
Johannes Berga538e2d2009-06-16 19:56:42 +02006972void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6973 struct net_device *netdev)
6974{
6975 struct sk_buff *msg;
6976
6977 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6978 if (!msg)
6979 return;
6980
6981 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6982 NL80211_CMD_TRIGGER_SCAN) < 0) {
6983 nlmsg_free(msg);
6984 return;
6985 }
6986
Johannes Berg463d0182009-07-14 00:33:35 +02006987 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6988 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006989}
6990
Johannes Berg2a519312009-02-10 21:25:55 +01006991void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6992 struct net_device *netdev)
6993{
6994 struct sk_buff *msg;
6995
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006996 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006997 if (!msg)
6998 return;
6999
Johannes Berga538e2d2009-06-16 19:56:42 +02007000 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7001 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007002 nlmsg_free(msg);
7003 return;
7004 }
7005
Johannes Berg463d0182009-07-14 00:33:35 +02007006 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7007 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007008}
7009
7010void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7011 struct net_device *netdev)
7012{
7013 struct sk_buff *msg;
7014
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007016 if (!msg)
7017 return;
7018
Johannes Berga538e2d2009-06-16 19:56:42 +02007019 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7020 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007021 nlmsg_free(msg);
7022 return;
7023 }
7024
Johannes Berg463d0182009-07-14 00:33:35 +02007025 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7026 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007027}
7028
Luciano Coelho807f8a82011-05-11 17:09:35 +03007029void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7030 struct net_device *netdev)
7031{
7032 struct sk_buff *msg;
7033
7034 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7035 if (!msg)
7036 return;
7037
7038 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7039 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7040 nlmsg_free(msg);
7041 return;
7042 }
7043
7044 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7045 nl80211_scan_mcgrp.id, GFP_KERNEL);
7046}
7047
7048void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7049 struct net_device *netdev, u32 cmd)
7050{
7051 struct sk_buff *msg;
7052
7053 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7054 if (!msg)
7055 return;
7056
7057 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7058 nlmsg_free(msg);
7059 return;
7060 }
7061
7062 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7063 nl80211_scan_mcgrp.id, GFP_KERNEL);
7064}
7065
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007066/*
7067 * This can happen on global regulatory changes or device specific settings
7068 * based on custom world regulatory domains.
7069 */
7070void nl80211_send_reg_change_event(struct regulatory_request *request)
7071{
7072 struct sk_buff *msg;
7073 void *hdr;
7074
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007075 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007076 if (!msg)
7077 return;
7078
7079 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7080 if (!hdr) {
7081 nlmsg_free(msg);
7082 return;
7083 }
7084
7085 /* Userspace can always count this one always being set */
7086 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
7087
7088 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
7089 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7090 NL80211_REGDOM_TYPE_WORLD);
7091 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
7092 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7093 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
7094 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7095 request->intersect)
7096 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7097 NL80211_REGDOM_TYPE_INTERSECTION);
7098 else {
7099 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7100 NL80211_REGDOM_TYPE_COUNTRY);
7101 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
7102 }
7103
7104 if (wiphy_idx_valid(request->wiphy_idx))
7105 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
7106
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007107 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007108
Johannes Bergbc43b282009-07-25 10:54:13 +02007109 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007110 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007111 GFP_ATOMIC);
7112 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007113
7114 return;
7115
7116nla_put_failure:
7117 genlmsg_cancel(msg, hdr);
7118 nlmsg_free(msg);
7119}
7120
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007121static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7122 struct net_device *netdev,
7123 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007124 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007125{
7126 struct sk_buff *msg;
7127 void *hdr;
7128
Johannes Berge6d6e342009-07-01 21:26:47 +02007129 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007130 if (!msg)
7131 return;
7132
7133 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7134 if (!hdr) {
7135 nlmsg_free(msg);
7136 return;
7137 }
7138
7139 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7140 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7141 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7142
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007143 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007144
Johannes Berg463d0182009-07-14 00:33:35 +02007145 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7146 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007147 return;
7148
7149 nla_put_failure:
7150 genlmsg_cancel(msg, hdr);
7151 nlmsg_free(msg);
7152}
7153
7154void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007155 struct net_device *netdev, const u8 *buf,
7156 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007157{
7158 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007159 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007160}
7161
7162void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7163 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007164 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007165{
Johannes Berge6d6e342009-07-01 21:26:47 +02007166 nl80211_send_mlme_event(rdev, netdev, buf, len,
7167 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007168}
7169
Jouni Malinen53b46b82009-03-27 20:53:56 +02007170void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007171 struct net_device *netdev, const u8 *buf,
7172 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007173{
7174 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007175 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007176}
7177
Jouni Malinen53b46b82009-03-27 20:53:56 +02007178void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7179 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007180 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007181{
7182 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007183 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007184}
7185
Jouni Malinencf4e5942010-12-16 00:52:40 +02007186void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7187 struct net_device *netdev, const u8 *buf,
7188 size_t len, gfp_t gfp)
7189{
7190 nl80211_send_mlme_event(rdev, netdev, buf, len,
7191 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7192}
7193
7194void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7195 struct net_device *netdev, const u8 *buf,
7196 size_t len, gfp_t gfp)
7197{
7198 nl80211_send_mlme_event(rdev, netdev, buf, len,
7199 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7200}
7201
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007202static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7203 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007204 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007205{
7206 struct sk_buff *msg;
7207 void *hdr;
7208
Johannes Berge6d6e342009-07-01 21:26:47 +02007209 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007210 if (!msg)
7211 return;
7212
7213 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7214 if (!hdr) {
7215 nlmsg_free(msg);
7216 return;
7217 }
7218
7219 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7220 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7221 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7222 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7223
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007224 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007225
Johannes Berg463d0182009-07-14 00:33:35 +02007226 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7227 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007228 return;
7229
7230 nla_put_failure:
7231 genlmsg_cancel(msg, hdr);
7232 nlmsg_free(msg);
7233}
7234
7235void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007236 struct net_device *netdev, const u8 *addr,
7237 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007238{
7239 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007240 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007241}
7242
7243void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007244 struct net_device *netdev, const u8 *addr,
7245 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007246{
Johannes Berge6d6e342009-07-01 21:26:47 +02007247 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7248 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007249}
7250
Samuel Ortizb23aa672009-07-01 21:26:54 +02007251void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7252 struct net_device *netdev, const u8 *bssid,
7253 const u8 *req_ie, size_t req_ie_len,
7254 const u8 *resp_ie, size_t resp_ie_len,
7255 u16 status, gfp_t gfp)
7256{
7257 struct sk_buff *msg;
7258 void *hdr;
7259
7260 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7261 if (!msg)
7262 return;
7263
7264 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7265 if (!hdr) {
7266 nlmsg_free(msg);
7267 return;
7268 }
7269
7270 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7272 if (bssid)
7273 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7274 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7275 if (req_ie)
7276 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7277 if (resp_ie)
7278 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7279
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007280 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007281
Johannes Berg463d0182009-07-14 00:33:35 +02007282 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7283 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007284 return;
7285
7286 nla_put_failure:
7287 genlmsg_cancel(msg, hdr);
7288 nlmsg_free(msg);
7289
7290}
7291
7292void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7293 struct net_device *netdev, const u8 *bssid,
7294 const u8 *req_ie, size_t req_ie_len,
7295 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7296{
7297 struct sk_buff *msg;
7298 void *hdr;
7299
7300 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7301 if (!msg)
7302 return;
7303
7304 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7305 if (!hdr) {
7306 nlmsg_free(msg);
7307 return;
7308 }
7309
7310 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7311 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7312 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7313 if (req_ie)
7314 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7315 if (resp_ie)
7316 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7317
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007318 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007319
Johannes Berg463d0182009-07-14 00:33:35 +02007320 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7321 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007322 return;
7323
7324 nla_put_failure:
7325 genlmsg_cancel(msg, hdr);
7326 nlmsg_free(msg);
7327
7328}
7329
7330void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7331 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007332 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007333{
7334 struct sk_buff *msg;
7335 void *hdr;
7336
Johannes Berg667503d2009-07-07 03:56:11 +02007337 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007338 if (!msg)
7339 return;
7340
7341 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7342 if (!hdr) {
7343 nlmsg_free(msg);
7344 return;
7345 }
7346
7347 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7348 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7349 if (from_ap && reason)
7350 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7351 if (from_ap)
7352 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7353 if (ie)
7354 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7355
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007356 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007357
Johannes Berg463d0182009-07-14 00:33:35 +02007358 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7359 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007360 return;
7361
7362 nla_put_failure:
7363 genlmsg_cancel(msg, hdr);
7364 nlmsg_free(msg);
7365
7366}
7367
Johannes Berg04a773a2009-04-19 21:24:32 +02007368void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7369 struct net_device *netdev, const u8 *bssid,
7370 gfp_t gfp)
7371{
7372 struct sk_buff *msg;
7373 void *hdr;
7374
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007376 if (!msg)
7377 return;
7378
7379 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7380 if (!hdr) {
7381 nlmsg_free(msg);
7382 return;
7383 }
7384
7385 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7386 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7387 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7388
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007389 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007390
Johannes Berg463d0182009-07-14 00:33:35 +02007391 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7392 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007393 return;
7394
7395 nla_put_failure:
7396 genlmsg_cancel(msg, hdr);
7397 nlmsg_free(msg);
7398}
7399
Javier Cardonac93b5e72011-04-07 15:08:34 -07007400void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7401 struct net_device *netdev,
7402 const u8 *macaddr, const u8* ie, u8 ie_len,
7403 gfp_t gfp)
7404{
7405 struct sk_buff *msg;
7406 void *hdr;
7407
7408 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7409 if (!msg)
7410 return;
7411
7412 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7413 if (!hdr) {
7414 nlmsg_free(msg);
7415 return;
7416 }
7417
7418 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7419 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7420 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7421 if (ie_len && ie)
7422 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7423
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007424 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007425
7426 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7427 nl80211_mlme_mcgrp.id, gfp);
7428 return;
7429
7430 nla_put_failure:
7431 genlmsg_cancel(msg, hdr);
7432 nlmsg_free(msg);
7433}
7434
Jouni Malinena3b8b052009-03-27 21:59:49 +02007435void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7436 struct net_device *netdev, const u8 *addr,
7437 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007438 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007439{
7440 struct sk_buff *msg;
7441 void *hdr;
7442
Johannes Berge6d6e342009-07-01 21:26:47 +02007443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007444 if (!msg)
7445 return;
7446
7447 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7448 if (!hdr) {
7449 nlmsg_free(msg);
7450 return;
7451 }
7452
7453 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7454 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7455 if (addr)
7456 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7457 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007458 if (key_id != -1)
7459 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007460 if (tsc)
7461 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7462
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007463 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007464
Johannes Berg463d0182009-07-14 00:33:35 +02007465 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7466 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007467 return;
7468
7469 nla_put_failure:
7470 genlmsg_cancel(msg, hdr);
7471 nlmsg_free(msg);
7472}
7473
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007474void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7475 struct ieee80211_channel *channel_before,
7476 struct ieee80211_channel *channel_after)
7477{
7478 struct sk_buff *msg;
7479 void *hdr;
7480 struct nlattr *nl_freq;
7481
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007483 if (!msg)
7484 return;
7485
7486 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7487 if (!hdr) {
7488 nlmsg_free(msg);
7489 return;
7490 }
7491
7492 /*
7493 * Since we are applying the beacon hint to a wiphy we know its
7494 * wiphy_idx is valid
7495 */
7496 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7497
7498 /* Before */
7499 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7500 if (!nl_freq)
7501 goto nla_put_failure;
7502 if (nl80211_msg_put_channel(msg, channel_before))
7503 goto nla_put_failure;
7504 nla_nest_end(msg, nl_freq);
7505
7506 /* After */
7507 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7508 if (!nl_freq)
7509 goto nla_put_failure;
7510 if (nl80211_msg_put_channel(msg, channel_after))
7511 goto nla_put_failure;
7512 nla_nest_end(msg, nl_freq);
7513
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007514 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007515
Johannes Berg463d0182009-07-14 00:33:35 +02007516 rcu_read_lock();
7517 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7518 GFP_ATOMIC);
7519 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007520
7521 return;
7522
7523nla_put_failure:
7524 genlmsg_cancel(msg, hdr);
7525 nlmsg_free(msg);
7526}
7527
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007528static void nl80211_send_remain_on_chan_event(
7529 int cmd, struct cfg80211_registered_device *rdev,
7530 struct net_device *netdev, u64 cookie,
7531 struct ieee80211_channel *chan,
7532 enum nl80211_channel_type channel_type,
7533 unsigned int duration, gfp_t gfp)
7534{
7535 struct sk_buff *msg;
7536 void *hdr;
7537
7538 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7539 if (!msg)
7540 return;
7541
7542 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7543 if (!hdr) {
7544 nlmsg_free(msg);
7545 return;
7546 }
7547
7548 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7549 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7550 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7551 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7552 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7553
7554 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7555 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7556
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007557 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007558
7559 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7560 nl80211_mlme_mcgrp.id, gfp);
7561 return;
7562
7563 nla_put_failure:
7564 genlmsg_cancel(msg, hdr);
7565 nlmsg_free(msg);
7566}
7567
7568void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7569 struct net_device *netdev, u64 cookie,
7570 struct ieee80211_channel *chan,
7571 enum nl80211_channel_type channel_type,
7572 unsigned int duration, gfp_t gfp)
7573{
7574 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7575 rdev, netdev, cookie, chan,
7576 channel_type, duration, gfp);
7577}
7578
7579void nl80211_send_remain_on_channel_cancel(
7580 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7581 u64 cookie, struct ieee80211_channel *chan,
7582 enum nl80211_channel_type channel_type, gfp_t gfp)
7583{
7584 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7585 rdev, netdev, cookie, chan,
7586 channel_type, 0, gfp);
7587}
7588
Johannes Berg98b62182009-12-23 13:15:44 +01007589void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7590 struct net_device *dev, const u8 *mac_addr,
7591 struct station_info *sinfo, gfp_t gfp)
7592{
7593 struct sk_buff *msg;
7594
7595 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7596 if (!msg)
7597 return;
7598
7599 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7600 nlmsg_free(msg);
7601 return;
7602 }
7603
7604 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7605 nl80211_mlme_mcgrp.id, gfp);
7606}
7607
Jouni Malinenec15e682011-03-23 15:29:52 +02007608void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7609 struct net_device *dev, const u8 *mac_addr,
7610 gfp_t gfp)
7611{
7612 struct sk_buff *msg;
7613 void *hdr;
7614
7615 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7616 if (!msg)
7617 return;
7618
7619 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7620 if (!hdr) {
7621 nlmsg_free(msg);
7622 return;
7623 }
7624
7625 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7626 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7627
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007628 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007629
7630 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7631 nl80211_mlme_mcgrp.id, gfp);
7632 return;
7633
7634 nla_put_failure:
7635 genlmsg_cancel(msg, hdr);
7636 nlmsg_free(msg);
7637}
7638
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007639static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7640 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007641{
7642 struct wireless_dev *wdev = dev->ieee80211_ptr;
7643 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7644 struct sk_buff *msg;
7645 void *hdr;
7646 int err;
7647 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7648
7649 if (!nlpid)
7650 return false;
7651
7652 msg = nlmsg_new(100, gfp);
7653 if (!msg)
7654 return true;
7655
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007656 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007657 if (!hdr) {
7658 nlmsg_free(msg);
7659 return true;
7660 }
7661
7662 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7664 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7665
7666 err = genlmsg_end(msg, hdr);
7667 if (err < 0) {
7668 nlmsg_free(msg);
7669 return true;
7670 }
7671
7672 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7673 return true;
7674
7675 nla_put_failure:
7676 genlmsg_cancel(msg, hdr);
7677 nlmsg_free(msg);
7678 return true;
7679}
7680
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007681bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7682{
7683 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7684 addr, gfp);
7685}
7686
7687bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7688 const u8 *addr, gfp_t gfp)
7689{
7690 return __nl80211_unexpected_frame(dev,
7691 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7692 addr, gfp);
7693}
7694
Johannes Berg2e161f782010-08-12 15:38:38 +02007695int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7696 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007697 int freq, int sig_dbm,
7698 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007699{
7700 struct sk_buff *msg;
7701 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007702
7703 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7704 if (!msg)
7705 return -ENOMEM;
7706
Johannes Berg2e161f782010-08-12 15:38:38 +02007707 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007708 if (!hdr) {
7709 nlmsg_free(msg);
7710 return -ENOMEM;
7711 }
7712
7713 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7714 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7715 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Johannes Berg804483e2012-03-05 22:18:41 +01007716 if (sig_dbm)
7717 NLA_PUT_U32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm);
Jouni Malinen026331c2010-02-15 12:53:10 +02007718 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7719
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007720 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007721
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007722 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007723
7724 nla_put_failure:
7725 genlmsg_cancel(msg, hdr);
7726 nlmsg_free(msg);
7727 return -ENOBUFS;
7728}
7729
Johannes Berg2e161f782010-08-12 15:38:38 +02007730void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7731 struct net_device *netdev, u64 cookie,
7732 const u8 *buf, size_t len, bool ack,
7733 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007734{
7735 struct sk_buff *msg;
7736 void *hdr;
7737
7738 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7739 if (!msg)
7740 return;
7741
Johannes Berg2e161f782010-08-12 15:38:38 +02007742 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007743 if (!hdr) {
7744 nlmsg_free(msg);
7745 return;
7746 }
7747
7748 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7749 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7750 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7751 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7752 if (ack)
7753 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7754
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007755 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007756
7757 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7758 return;
7759
7760 nla_put_failure:
7761 genlmsg_cancel(msg, hdr);
7762 nlmsg_free(msg);
7763}
7764
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007765void
7766nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7767 struct net_device *netdev,
7768 enum nl80211_cqm_rssi_threshold_event rssi_event,
7769 gfp_t gfp)
7770{
7771 struct sk_buff *msg;
7772 struct nlattr *pinfoattr;
7773 void *hdr;
7774
7775 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7776 if (!msg)
7777 return;
7778
7779 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7780 if (!hdr) {
7781 nlmsg_free(msg);
7782 return;
7783 }
7784
7785 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7786 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7787
7788 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7789 if (!pinfoattr)
7790 goto nla_put_failure;
7791
7792 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7793 rssi_event);
7794
7795 nla_nest_end(msg, pinfoattr);
7796
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007797 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007798
7799 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7800 nl80211_mlme_mcgrp.id, gfp);
7801 return;
7802
7803 nla_put_failure:
7804 genlmsg_cancel(msg, hdr);
7805 nlmsg_free(msg);
7806}
7807
Johannes Berge5497d72011-07-05 16:35:40 +02007808void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7809 struct net_device *netdev, const u8 *bssid,
7810 const u8 *replay_ctr, gfp_t gfp)
7811{
7812 struct sk_buff *msg;
7813 struct nlattr *rekey_attr;
7814 void *hdr;
7815
7816 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7817 if (!msg)
7818 return;
7819
7820 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7821 if (!hdr) {
7822 nlmsg_free(msg);
7823 return;
7824 }
7825
7826 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7827 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7828 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7829
7830 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7831 if (!rekey_attr)
7832 goto nla_put_failure;
7833
7834 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7835 NL80211_REPLAY_CTR_LEN, replay_ctr);
7836
7837 nla_nest_end(msg, rekey_attr);
7838
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007839 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007840
7841 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7842 nl80211_mlme_mcgrp.id, gfp);
7843 return;
7844
7845 nla_put_failure:
7846 genlmsg_cancel(msg, hdr);
7847 nlmsg_free(msg);
7848}
7849
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007850void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7851 struct net_device *netdev, int index,
7852 const u8 *bssid, bool preauth, gfp_t gfp)
7853{
7854 struct sk_buff *msg;
7855 struct nlattr *attr;
7856 void *hdr;
7857
7858 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7859 if (!msg)
7860 return;
7861
7862 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7863 if (!hdr) {
7864 nlmsg_free(msg);
7865 return;
7866 }
7867
7868 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7870
7871 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7872 if (!attr)
7873 goto nla_put_failure;
7874
7875 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7876 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7877 if (preauth)
7878 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7879
7880 nla_nest_end(msg, attr);
7881
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007882 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007883
7884 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7885 nl80211_mlme_mcgrp.id, gfp);
7886 return;
7887
7888 nla_put_failure:
7889 genlmsg_cancel(msg, hdr);
7890 nlmsg_free(msg);
7891}
7892
Johannes Bergc063dbf2010-11-24 08:10:05 +01007893void
7894nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7895 struct net_device *netdev, const u8 *peer,
7896 u32 num_packets, gfp_t gfp)
7897{
7898 struct sk_buff *msg;
7899 struct nlattr *pinfoattr;
7900 void *hdr;
7901
7902 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7903 if (!msg)
7904 return;
7905
7906 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7907 if (!hdr) {
7908 nlmsg_free(msg);
7909 return;
7910 }
7911
7912 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7913 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7914 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7915
7916 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7917 if (!pinfoattr)
7918 goto nla_put_failure;
7919
7920 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7921
7922 nla_nest_end(msg, pinfoattr);
7923
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007924 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007925
7926 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7927 nl80211_mlme_mcgrp.id, gfp);
7928 return;
7929
7930 nla_put_failure:
7931 genlmsg_cancel(msg, hdr);
7932 nlmsg_free(msg);
7933}
7934
Johannes Berg7f6cf312011-11-04 11:18:15 +01007935void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7936 u64 cookie, bool acked, gfp_t gfp)
7937{
7938 struct wireless_dev *wdev = dev->ieee80211_ptr;
7939 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7940 struct sk_buff *msg;
7941 void *hdr;
7942 int err;
7943
7944 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7945 if (!msg)
7946 return;
7947
7948 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7949 if (!hdr) {
7950 nlmsg_free(msg);
7951 return;
7952 }
7953
7954 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7955 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7956 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7957 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7958 if (acked)
7959 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7960
7961 err = genlmsg_end(msg, hdr);
7962 if (err < 0) {
7963 nlmsg_free(msg);
7964 return;
7965 }
7966
7967 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7968 nl80211_mlme_mcgrp.id, gfp);
7969 return;
7970
7971 nla_put_failure:
7972 genlmsg_cancel(msg, hdr);
7973 nlmsg_free(msg);
7974}
7975EXPORT_SYMBOL(cfg80211_probe_status);
7976
Johannes Berg5e760232011-11-04 11:18:17 +01007977void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7978 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01007979 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01007980{
7981 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7982 struct sk_buff *msg;
7983 void *hdr;
7984 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7985
7986 if (!nlpid)
7987 return;
7988
7989 msg = nlmsg_new(len + 100, gfp);
7990 if (!msg)
7991 return;
7992
7993 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7994 if (!hdr) {
7995 nlmsg_free(msg);
7996 return;
7997 }
7998
7999 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8000 if (freq)
8001 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Johannes Berg804483e2012-03-05 22:18:41 +01008002 if (sig_dbm)
8003 NLA_PUT_U32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm);
Johannes Berg5e760232011-11-04 11:18:17 +01008004 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
8005
8006 genlmsg_end(msg, hdr);
8007
8008 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8009 return;
8010
8011 nla_put_failure:
8012 genlmsg_cancel(msg, hdr);
8013 nlmsg_free(msg);
8014}
8015EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8016
Jouni Malinen026331c2010-02-15 12:53:10 +02008017static int nl80211_netlink_notify(struct notifier_block * nb,
8018 unsigned long state,
8019 void *_notify)
8020{
8021 struct netlink_notify *notify = _notify;
8022 struct cfg80211_registered_device *rdev;
8023 struct wireless_dev *wdev;
8024
8025 if (state != NETLINK_URELEASE)
8026 return NOTIFY_DONE;
8027
8028 rcu_read_lock();
8029
Johannes Berg5e760232011-11-04 11:18:17 +01008030 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008031 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f782010-08-12 15:38:38 +02008032 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008033 if (rdev->ap_beacons_nlpid == notify->pid)
8034 rdev->ap_beacons_nlpid = 0;
8035 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008036
8037 rcu_read_unlock();
8038
8039 return NOTIFY_DONE;
8040}
8041
8042static struct notifier_block nl80211_netlink_notifier = {
8043 .notifier_call = nl80211_netlink_notify,
8044};
8045
Johannes Berg55682962007-09-20 13:09:35 -04008046/* initialisation/exit functions */
8047
8048int nl80211_init(void)
8049{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008050 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008051
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008052 err = genl_register_family_with_ops(&nl80211_fam,
8053 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008054 if (err)
8055 return err;
8056
Johannes Berg55682962007-09-20 13:09:35 -04008057 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8058 if (err)
8059 goto err_out;
8060
Johannes Berg2a519312009-02-10 21:25:55 +01008061 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8062 if (err)
8063 goto err_out;
8064
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008065 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8066 if (err)
8067 goto err_out;
8068
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008069 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8070 if (err)
8071 goto err_out;
8072
Johannes Bergaff89a92009-07-01 21:26:51 +02008073#ifdef CONFIG_NL80211_TESTMODE
8074 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8075 if (err)
8076 goto err_out;
8077#endif
8078
Jouni Malinen026331c2010-02-15 12:53:10 +02008079 err = netlink_register_notifier(&nl80211_netlink_notifier);
8080 if (err)
8081 goto err_out;
8082
Johannes Berg55682962007-09-20 13:09:35 -04008083 return 0;
8084 err_out:
8085 genl_unregister_family(&nl80211_fam);
8086 return err;
8087}
8088
8089void nl80211_exit(void)
8090{
Jouni Malinen026331c2010-02-15 12:53:10 +02008091 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008092 genl_unregister_family(&nl80211_fam);
8093}