blob: f1b0774d098b5d2eeed6ac4543d3441dff7f7c65 [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 Malinendc6382c2009-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 Berg2e161f72010-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 Berg77dbbb132011-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{
David S. Miller9360ffd2012-03-29 04:41:26 -0400359 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360 chan->center_freq))
361 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400362
David S. Miller9360ffd2012-03-29 04:41:26 -0400363 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
364 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
365 goto nla_put_failure;
366 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
367 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
368 goto nla_put_failure;
369 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
370 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
371 goto nla_put_failure;
372 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
373 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
374 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400375
David S. Miller9360ffd2012-03-29 04:41:26 -0400376 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
377 DBM_TO_MBM(chan->max_power)))
378 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400379
380 return 0;
381
382 nla_put_failure:
383 return -ENOBUFS;
384}
385
Johannes Berg55682962007-09-20 13:09:35 -0400386/* netlink command implementations */
387
Johannes Bergb9454e82009-07-08 13:29:08 +0200388struct key_parse {
389 struct key_params p;
390 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394};
395
396static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
397{
398 struct nlattr *tb[NL80211_KEY_MAX + 1];
399 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
400 nl80211_key_policy);
401 if (err)
402 return err;
403
404 k->def = !!tb[NL80211_KEY_DEFAULT];
405 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (k->def) {
408 k->def_uni = true;
409 k->def_multi = true;
410 }
411 if (k->defmgmt)
412 k->def_multi = true;
413
Johannes Bergb9454e82009-07-08 13:29:08 +0200414 if (tb[NL80211_KEY_IDX])
415 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
416
417 if (tb[NL80211_KEY_DATA]) {
418 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
419 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
420 }
421
422 if (tb[NL80211_KEY_SEQ]) {
423 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
424 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
425 }
426
427 if (tb[NL80211_KEY_CIPHER])
428 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
429
Johannes Berge31b8212010-10-05 19:39:30 +0200430 if (tb[NL80211_KEY_TYPE]) {
431 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
432 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
433 return -EINVAL;
434 }
435
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100436 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
437 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100438 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
439 tb[NL80211_KEY_DEFAULT_TYPES],
440 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100441 if (err)
442 return err;
443
444 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
445 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
446 }
447
Johannes Bergb9454e82009-07-08 13:29:08 +0200448 return 0;
449}
450
451static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
452{
453 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
454 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
455 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
456 }
457
458 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
459 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
460 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
461 }
462
463 if (info->attrs[NL80211_ATTR_KEY_IDX])
464 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
465
466 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
467 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
468
469 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
470 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
471
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100472 if (k->def) {
473 k->def_uni = true;
474 k->def_multi = true;
475 }
476 if (k->defmgmt)
477 k->def_multi = true;
478
Johannes Berge31b8212010-10-05 19:39:30 +0200479 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
480 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
481 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
482 return -EINVAL;
483 }
484
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100485 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
486 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
487 int err = nla_parse_nested(
488 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
489 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
490 nl80211_key_default_policy);
491 if (err)
492 return err;
493
494 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
495 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 return 0;
499}
500
501static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
502{
503 int err;
504
505 memset(k, 0, sizeof(*k));
506 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200507 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200508
509 if (info->attrs[NL80211_ATTR_KEY])
510 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
511 else
512 err = nl80211_parse_key_old(info, k);
513
514 if (err)
515 return err;
516
517 if (k->def && k->defmgmt)
518 return -EINVAL;
519
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (k->defmgmt) {
521 if (k->def_uni || !k->def_multi)
522 return -EINVAL;
523 }
524
Johannes Bergb9454e82009-07-08 13:29:08 +0200525 if (k->idx != -1) {
526 if (k->defmgmt) {
527 if (k->idx < 4 || k->idx > 5)
528 return -EINVAL;
529 } else if (k->def) {
530 if (k->idx < 0 || k->idx > 3)
531 return -EINVAL;
532 } else {
533 if (k->idx < 0 || k->idx > 5)
534 return -EINVAL;
535 }
536 }
537
538 return 0;
539}
540
Johannes Bergfffd0932009-07-08 14:22:54 +0200541static struct cfg80211_cached_keys *
542nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
543 struct nlattr *keys)
544{
545 struct key_parse parse;
546 struct nlattr *key;
547 struct cfg80211_cached_keys *result;
548 int rem, err, def = 0;
549
550 result = kzalloc(sizeof(*result), GFP_KERNEL);
551 if (!result)
552 return ERR_PTR(-ENOMEM);
553
554 result->def = -1;
555 result->defmgmt = -1;
556
557 nla_for_each_nested(key, keys, rem) {
558 memset(&parse, 0, sizeof(parse));
559 parse.idx = -1;
560
561 err = nl80211_parse_key_new(key, &parse);
562 if (err)
563 goto error;
564 err = -EINVAL;
565 if (!parse.p.key)
566 goto error;
567 if (parse.idx < 0 || parse.idx > 4)
568 goto error;
569 if (parse.def) {
570 if (def)
571 goto error;
572 def = 1;
573 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (!parse.def_uni || !parse.def_multi)
575 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 } else if (parse.defmgmt)
577 goto error;
578 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200579 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200580 if (err)
581 goto error;
582 result->params[parse.idx].cipher = parse.p.cipher;
583 result->params[parse.idx].key_len = parse.p.key_len;
584 result->params[parse.idx].key = result->data[parse.idx];
585 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
586 }
587
588 return result;
589 error:
590 kfree(result);
591 return ERR_PTR(err);
592}
593
594static int nl80211_key_allowed(struct wireless_dev *wdev)
595{
596 ASSERT_WDEV_LOCK(wdev);
597
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 switch (wdev->iftype) {
599 case NL80211_IFTYPE_AP:
600 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700602 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 break;
604 case NL80211_IFTYPE_ADHOC:
605 if (!wdev->current_bss)
606 return -ENOLINK;
607 break;
608 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200609 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200610 if (wdev->sme_state != CFG80211_SME_CONNECTED)
611 return -ENOLINK;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 return 0;
618}
619
Johannes Berg7527a782011-05-13 10:58:57 +0200620static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
621{
622 struct nlattr *nl_modes = nla_nest_start(msg, attr);
623 int i;
624
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400630 if ((ifmodes & 1) && nla_put_flag(msg, i))
631 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637 return 0;
638
639nla_put_failure:
640 return -ENOBUFS;
641}
642
643static int nl80211_put_iface_combinations(struct wiphy *wiphy,
644 struct sk_buff *msg)
645{
646 struct nlattr *nl_combis;
647 int i, j;
648
649 nl_combis = nla_nest_start(msg,
650 NL80211_ATTR_INTERFACE_COMBINATIONS);
651 if (!nl_combis)
652 goto nla_put_failure;
653
654 for (i = 0; i < wiphy->n_iface_combinations; i++) {
655 const struct ieee80211_iface_combination *c;
656 struct nlattr *nl_combi, *nl_limits;
657
658 c = &wiphy->iface_combinations[i];
659
660 nl_combi = nla_nest_start(msg, i + 1);
661 if (!nl_combi)
662 goto nla_put_failure;
663
664 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
665 if (!nl_limits)
666 goto nla_put_failure;
667
668 for (j = 0; j < c->n_limits; j++) {
669 struct nlattr *nl_limit;
670
671 nl_limit = nla_nest_start(msg, j + 1);
672 if (!nl_limit)
673 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400674 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
675 c->limits[j].max))
676 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200677 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
678 c->limits[j].types))
679 goto nla_put_failure;
680 nla_nest_end(msg, nl_limit);
681 }
682
683 nla_nest_end(msg, nl_limits);
684
David S. Miller9360ffd2012-03-29 04:41:26 -0400685 if (c->beacon_int_infra_match &&
686 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
687 goto nla_put_failure;
688 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
689 c->num_different_channels) ||
690 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
691 c->max_interfaces))
692 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200693
694 nla_nest_end(msg, nl_combi);
695 }
696
697 nla_nest_end(msg, nl_combis);
698
699 return 0;
700nla_put_failure:
701 return -ENOBUFS;
702}
703
Johannes Berg55682962007-09-20 13:09:35 -0400704static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
705 struct cfg80211_registered_device *dev)
706{
707 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100708 struct nlattr *nl_bands, *nl_band;
709 struct nlattr *nl_freqs, *nl_freq;
710 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100711 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100712 enum ieee80211_band band;
713 struct ieee80211_channel *chan;
714 struct ieee80211_rate *rate;
715 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200716 const struct ieee80211_txrx_stypes *mgmt_stypes =
717 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400718
719 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
720 if (!hdr)
721 return -1;
722
David S. Miller9360ffd2012-03-29 04:41:26 -0400723 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
724 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
725 nla_put_u32(msg, NL80211_ATTR_GENERATION,
726 cfg80211_rdev_list_generation) ||
727 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
728 dev->wiphy.retry_short) ||
729 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
730 dev->wiphy.retry_long) ||
731 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
732 dev->wiphy.frag_threshold) ||
733 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
734 dev->wiphy.rts_threshold) ||
735 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
736 dev->wiphy.coverage_class) ||
737 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
738 dev->wiphy.max_scan_ssids) ||
739 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
740 dev->wiphy.max_sched_scan_ssids) ||
741 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
742 dev->wiphy.max_scan_ie_len) ||
743 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
744 dev->wiphy.max_sched_scan_ie_len) ||
745 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
746 dev->wiphy.max_match_sets))
747 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200748
David S. Miller9360ffd2012-03-29 04:41:26 -0400749 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
750 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
751 goto nla_put_failure;
752 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
753 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
754 goto nla_put_failure;
755 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
756 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
757 goto nla_put_failure;
758 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
759 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
760 goto nla_put_failure;
761 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
762 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
763 goto nla_put_failure;
764 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
765 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
766 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200767
David S. Miller9360ffd2012-03-29 04:41:26 -0400768 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
769 sizeof(u32) * dev->wiphy.n_cipher_suites,
770 dev->wiphy.cipher_suites))
771 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100772
David S. Miller9360ffd2012-03-29 04:41:26 -0400773 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
774 dev->wiphy.max_num_pmkids))
775 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530776
David S. Miller9360ffd2012-03-29 04:41:26 -0400777 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
778 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
779 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200780
David S. Miller9360ffd2012-03-29 04:41:26 -0400781 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
782 dev->wiphy.available_antennas_tx) ||
783 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
784 dev->wiphy.available_antennas_rx))
785 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100786
David S. Miller9360ffd2012-03-29 04:41:26 -0400787 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
788 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
789 dev->wiphy.probe_resp_offload))
790 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200791
Bruno Randolf7f531e02010-12-16 11:30:22 +0900792 if ((dev->wiphy.available_antennas_tx ||
793 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900794 u32 tx_ant = 0, rx_ant = 0;
795 int res;
796 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
797 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
799 tx_ant) ||
800 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
801 rx_ant))
802 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900803 }
804 }
805
Johannes Berg7527a782011-05-13 10:58:57 +0200806 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
807 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700808 goto nla_put_failure;
809
Johannes Bergee688b002008-01-24 19:38:39 +0100810 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
811 if (!nl_bands)
812 goto nla_put_failure;
813
814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
815 if (!dev->wiphy.bands[band])
816 continue;
817
818 nl_band = nla_nest_start(msg, band);
819 if (!nl_band)
820 goto nla_put_failure;
821
Johannes Bergd51626d2008-10-09 12:20:13 +0200822 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
824 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
825 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
826 &dev->wiphy.bands[band]->ht_cap.mcs) ||
827 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
828 dev->wiphy.bands[band]->ht_cap.cap) ||
829 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
830 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
831 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
832 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
833 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200834
Johannes Bergee688b002008-01-24 19:38:39 +0100835 /* add frequencies */
836 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
837 if (!nl_freqs)
838 goto nla_put_failure;
839
840 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
841 nl_freq = nla_nest_start(msg, i);
842 if (!nl_freq)
843 goto nla_put_failure;
844
845 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100846
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400847 if (nl80211_msg_put_channel(msg, chan))
848 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200849
Johannes Bergee688b002008-01-24 19:38:39 +0100850 nla_nest_end(msg, nl_freq);
851 }
852
853 nla_nest_end(msg, nl_freqs);
854
855 /* add bitrates */
856 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
857 if (!nl_rates)
858 goto nla_put_failure;
859
860 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
861 nl_rate = nla_nest_start(msg, i);
862 if (!nl_rate)
863 goto nla_put_failure;
864
865 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
867 rate->bitrate))
868 goto nla_put_failure;
869 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
870 nla_put_flag(msg,
871 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
872 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100873
874 nla_nest_end(msg, nl_rate);
875 }
876
877 nla_nest_end(msg, nl_rates);
878
879 nla_nest_end(msg, nl_band);
880 }
881 nla_nest_end(msg, nl_bands);
882
Johannes Berg8fdc6212009-03-14 09:34:01 +0100883 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
884 if (!nl_cmds)
885 goto nla_put_failure;
886
887 i = 0;
888#define CMD(op, n) \
889 do { \
890 if (dev->ops->op) { \
891 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
893 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894 } \
895 } while (0)
896
897 CMD(add_virtual_intf, NEW_INTERFACE);
898 CMD(change_virtual_intf, SET_INTERFACE);
899 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100900 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100901 CMD(add_station, NEW_STATION);
902 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800903 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100904 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200905 CMD(auth, AUTHENTICATE);
906 CMD(assoc, ASSOCIATE);
907 CMD(deauth, DEAUTHENTICATE);
908 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200909 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100910 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100911 CMD(set_pmksa, SET_PMKSA);
912 CMD(del_pmksa, DEL_PMKSA);
913 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100914 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
915 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200916 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200917 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100918 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100919 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200920 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
922 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +0200923 }
Johannes Bergf444de02010-05-05 15:25:02 +0200924 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400925 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300926 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
927 CMD(tdls_mgmt, TDLS_MGMT);
928 CMD(tdls_oper, TDLS_OPER);
929 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300930 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
931 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100932 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100933 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100934 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
935 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
937 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +0100938 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100939
Kalle Valo4745fc02011-11-17 19:06:10 +0200940#ifdef CONFIG_NL80211_TESTMODE
941 CMD(testmode_cmd, TESTMODE);
942#endif
943
Johannes Berg8fdc6212009-03-14 09:34:01 +0100944#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200945
Johannes Berg6829c872009-07-02 09:13:27 +0200946 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200947 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400948 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
949 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200950 }
951
Johannes Berg6829c872009-07-02 09:13:27 +0200952 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200953 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400954 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
955 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200956 }
957
Johannes Berg8fdc6212009-03-14 09:34:01 +0100958 nla_nest_end(msg, nl_cmds);
959
Johannes Berg7c4ef712011-11-18 15:33:48 +0100960 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -0400961 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
962 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
963 dev->wiphy.max_remain_on_channel_duration))
964 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +0100965
David S. Miller9360ffd2012-03-29 04:41:26 -0400966 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
967 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
968 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100969
Johannes Berg2e161f72010-08-12 15:38:38 +0200970 if (mgmt_stypes) {
971 u16 stypes;
972 struct nlattr *nl_ftypes, *nl_ifs;
973 enum nl80211_iftype ift;
974
975 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
976 if (!nl_ifs)
977 goto nla_put_failure;
978
979 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
980 nl_ftypes = nla_nest_start(msg, ift);
981 if (!nl_ftypes)
982 goto nla_put_failure;
983 i = 0;
984 stypes = mgmt_stypes[ift].tx;
985 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400986 if ((stypes & 1) &&
987 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
988 (i << 4) | IEEE80211_FTYPE_MGMT))
989 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +0200990 stypes >>= 1;
991 i++;
992 }
993 nla_nest_end(msg, nl_ftypes);
994 }
995
Johannes Berg74b70a42010-08-24 12:15:53 +0200996 nla_nest_end(msg, nl_ifs);
997
Johannes Berg2e161f72010-08-12 15:38:38 +0200998 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
999 if (!nl_ifs)
1000 goto nla_put_failure;
1001
1002 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1003 nl_ftypes = nla_nest_start(msg, ift);
1004 if (!nl_ftypes)
1005 goto nla_put_failure;
1006 i = 0;
1007 stypes = mgmt_stypes[ift].rx;
1008 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001009 if ((stypes & 1) &&
1010 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1011 (i << 4) | IEEE80211_FTYPE_MGMT))
1012 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001013 stypes >>= 1;
1014 i++;
1015 }
1016 nla_nest_end(msg, nl_ftypes);
1017 }
1018 nla_nest_end(msg, nl_ifs);
1019 }
1020
Johannes Bergff1b6e62011-05-04 15:37:28 +02001021 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1022 struct nlattr *nl_wowlan;
1023
1024 nl_wowlan = nla_nest_start(msg,
1025 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1026 if (!nl_wowlan)
1027 goto nla_put_failure;
1028
David S. Miller9360ffd2012-03-29 04:41:26 -04001029 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1031 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1033 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1034 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1035 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1036 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1037 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1038 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1039 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1040 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1041 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1042 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1043 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1044 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1045 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001046 if (dev->wiphy.wowlan.n_patterns) {
1047 struct nl80211_wowlan_pattern_support pat = {
1048 .max_patterns = dev->wiphy.wowlan.n_patterns,
1049 .min_pattern_len =
1050 dev->wiphy.wowlan.pattern_min_len,
1051 .max_pattern_len =
1052 dev->wiphy.wowlan.pattern_max_len,
1053 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001054 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1055 sizeof(pat), &pat))
1056 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001057 }
1058
1059 nla_nest_end(msg, nl_wowlan);
1060 }
1061
Johannes Berg7527a782011-05-13 10:58:57 +02001062 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1063 dev->wiphy.software_iftypes))
1064 goto nla_put_failure;
1065
1066 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1067 goto nla_put_failure;
1068
David S. Miller9360ffd2012-03-29 04:41:26 -04001069 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1070 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1071 dev->wiphy.ap_sme_capa))
1072 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001073
David S. Miller9360ffd2012-03-29 04:41:26 -04001074 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1075 dev->wiphy.features))
1076 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001077
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (dev->wiphy.ht_capa_mod_mask &&
1079 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1080 sizeof(*dev->wiphy.ht_capa_mod_mask),
1081 dev->wiphy.ht_capa_mod_mask))
1082 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001083
Johannes Berg55682962007-09-20 13:09:35 -04001084 return genlmsg_end(msg, hdr);
1085
1086 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001087 genlmsg_cancel(msg, hdr);
1088 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001089}
1090
1091static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1092{
1093 int idx = 0;
1094 int start = cb->args[0];
1095 struct cfg80211_registered_device *dev;
1096
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001097 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001098 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001099 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1100 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001101 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001102 continue;
1103 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1104 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001105 dev) < 0) {
1106 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001107 break;
Julius Volzb4637272008-07-08 14:02:19 +02001108 }
Johannes Berg55682962007-09-20 13:09:35 -04001109 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001110 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001111
1112 cb->args[0] = idx;
1113
1114 return skb->len;
1115}
1116
1117static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1118{
1119 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001120 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001121
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001123 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001124 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001125
Johannes Berg4c476992010-10-04 21:36:35 +02001126 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1127 nlmsg_free(msg);
1128 return -ENOBUFS;
1129 }
Johannes Berg55682962007-09-20 13:09:35 -04001130
Johannes Berg134e6372009-07-10 09:51:34 +00001131 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001132}
1133
Jouni Malinen31888482008-10-30 16:59:24 +02001134static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1135 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1136 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1137 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1138 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1139 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1140};
1141
1142static int parse_txq_params(struct nlattr *tb[],
1143 struct ieee80211_txq_params *txq_params)
1144{
Johannes Berga3304b02012-03-28 11:04:24 +02001145 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001146 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1147 !tb[NL80211_TXQ_ATTR_AIFS])
1148 return -EINVAL;
1149
Johannes Berga3304b02012-03-28 11:04:24 +02001150 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001151 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1152 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1153 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1154 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1155
Johannes Berga3304b02012-03-28 11:04:24 +02001156 if (txq_params->ac >= NL80211_NUM_ACS)
1157 return -EINVAL;
1158
Jouni Malinen31888482008-10-30 16:59:24 +02001159 return 0;
1160}
1161
Johannes Bergf444de02010-05-05 15:25:02 +02001162static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1163{
1164 /*
1165 * You can only set the channel explicitly for AP, mesh
1166 * and WDS type interfaces; all others have their channel
1167 * managed via their respective "establish a connection"
1168 * command (connect, join, ...)
1169 *
1170 * Monitors are special as they are normally slaved to
1171 * whatever else is going on, so they behave as though
1172 * you tried setting the wiphy channel itself.
1173 */
1174 return !wdev ||
1175 wdev->iftype == NL80211_IFTYPE_AP ||
1176 wdev->iftype == NL80211_IFTYPE_WDS ||
1177 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001178 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1179 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001180}
1181
1182static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1183 struct wireless_dev *wdev,
1184 struct genl_info *info)
1185{
1186 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1187 u32 freq;
1188 int result;
1189
1190 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1191 return -EINVAL;
1192
1193 if (!nl80211_can_set_dev_channel(wdev))
1194 return -EOPNOTSUPP;
1195
1196 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1197 channel_type = nla_get_u32(info->attrs[
1198 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1199 if (channel_type != NL80211_CHAN_NO_HT &&
1200 channel_type != NL80211_CHAN_HT20 &&
1201 channel_type != NL80211_CHAN_HT40PLUS &&
1202 channel_type != NL80211_CHAN_HT40MINUS)
1203 return -EINVAL;
1204 }
1205
1206 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1207
1208 mutex_lock(&rdev->devlist_mtx);
1209 if (wdev) {
1210 wdev_lock(wdev);
1211 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1212 wdev_unlock(wdev);
1213 } else {
1214 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1215 }
1216 mutex_unlock(&rdev->devlist_mtx);
1217
1218 return result;
1219}
1220
1221static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1222{
Johannes Berg4c476992010-10-04 21:36:35 +02001223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1224 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001225
Johannes Berg4c476992010-10-04 21:36:35 +02001226 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001227}
1228
Bill Jordane8347eb2010-10-01 13:54:28 -04001229static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1230{
Johannes Berg43b19952010-10-07 13:10:30 +02001231 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1232 struct net_device *dev = info->user_ptr[1];
1233 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001234 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001235
1236 if (!info->attrs[NL80211_ATTR_MAC])
1237 return -EINVAL;
1238
Johannes Berg43b19952010-10-07 13:10:30 +02001239 if (netif_running(dev))
1240 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001241
Johannes Berg43b19952010-10-07 13:10:30 +02001242 if (!rdev->ops->set_wds_peer)
1243 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001244
Johannes Berg43b19952010-10-07 13:10:30 +02001245 if (wdev->iftype != NL80211_IFTYPE_WDS)
1246 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001247
1248 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001249 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001250}
1251
1252
Johannes Berg55682962007-09-20 13:09:35 -04001253static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1254{
1255 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001256 struct net_device *netdev = NULL;
1257 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001258 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001259 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001260 u32 changed;
1261 u8 retry_short = 0, retry_long = 0;
1262 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001263 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001264
Johannes Bergf444de02010-05-05 15:25:02 +02001265 /*
1266 * Try to find the wiphy and netdev. Normally this
1267 * function shouldn't need the netdev, but this is
1268 * done for backward compatibility -- previously
1269 * setting the channel was done per wiphy, but now
1270 * it is per netdev. Previous userland like hostapd
1271 * also passed a netdev to set_wiphy, so that it is
1272 * possible to let that go to the right netdev!
1273 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001274 mutex_lock(&cfg80211_mutex);
1275
Johannes Bergf444de02010-05-05 15:25:02 +02001276 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1277 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1278
1279 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1280 if (netdev && netdev->ieee80211_ptr) {
1281 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1282 mutex_lock(&rdev->mtx);
1283 } else
1284 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001285 }
1286
Johannes Bergf444de02010-05-05 15:25:02 +02001287 if (!netdev) {
1288 rdev = __cfg80211_rdev_from_info(info);
1289 if (IS_ERR(rdev)) {
1290 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001291 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001292 }
1293 wdev = NULL;
1294 netdev = NULL;
1295 result = 0;
1296
1297 mutex_lock(&rdev->mtx);
1298 } else if (netif_running(netdev) &&
1299 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1300 wdev = netdev->ieee80211_ptr;
1301 else
1302 wdev = NULL;
1303
1304 /*
1305 * end workaround code, by now the rdev is available
1306 * and locked, and wdev may or may not be NULL.
1307 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001308
1309 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001310 result = cfg80211_dev_rename(
1311 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001312
1313 mutex_unlock(&cfg80211_mutex);
1314
1315 if (result)
1316 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001317
Jouni Malinen31888482008-10-30 16:59:24 +02001318 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1319 struct ieee80211_txq_params txq_params;
1320 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1321
1322 if (!rdev->ops->set_txq_params) {
1323 result = -EOPNOTSUPP;
1324 goto bad_res;
1325 }
1326
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001327 if (!netdev) {
1328 result = -EINVAL;
1329 goto bad_res;
1330 }
1331
Johannes Berg133a3ff2011-11-03 14:50:13 +01001332 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1333 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1334 result = -EINVAL;
1335 goto bad_res;
1336 }
1337
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001338 if (!netif_running(netdev)) {
1339 result = -ENETDOWN;
1340 goto bad_res;
1341 }
1342
Jouni Malinen31888482008-10-30 16:59:24 +02001343 nla_for_each_nested(nl_txq_params,
1344 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1345 rem_txq_params) {
1346 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1347 nla_data(nl_txq_params),
1348 nla_len(nl_txq_params),
1349 txq_params_policy);
1350 result = parse_txq_params(tb, &txq_params);
1351 if (result)
1352 goto bad_res;
1353
1354 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001355 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001356 &txq_params);
1357 if (result)
1358 goto bad_res;
1359 }
1360 }
1361
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001362 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001363 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001364 if (result)
1365 goto bad_res;
1366 }
1367
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001368 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1369 enum nl80211_tx_power_setting type;
1370 int idx, mbm = 0;
1371
1372 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001373 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001374 goto bad_res;
1375 }
1376
1377 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1378 type = nla_get_u32(info->attrs[idx]);
1379
1380 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1381 (type != NL80211_TX_POWER_AUTOMATIC)) {
1382 result = -EINVAL;
1383 goto bad_res;
1384 }
1385
1386 if (type != NL80211_TX_POWER_AUTOMATIC) {
1387 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1388 mbm = nla_get_u32(info->attrs[idx]);
1389 }
1390
1391 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1392 if (result)
1393 goto bad_res;
1394 }
1395
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001396 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1397 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1398 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001399 if ((!rdev->wiphy.available_antennas_tx &&
1400 !rdev->wiphy.available_antennas_rx) ||
1401 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001402 result = -EOPNOTSUPP;
1403 goto bad_res;
1404 }
1405
1406 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1407 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1408
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001409 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001410 * available antenna masks, except for the "all" mask */
1411 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1412 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001413 result = -EINVAL;
1414 goto bad_res;
1415 }
1416
Bruno Randolf7f531e02010-12-16 11:30:22 +09001417 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1418 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001419
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001420 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1421 if (result)
1422 goto bad_res;
1423 }
1424
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001425 changed = 0;
1426
1427 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1428 retry_short = nla_get_u8(
1429 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1430 if (retry_short == 0) {
1431 result = -EINVAL;
1432 goto bad_res;
1433 }
1434 changed |= WIPHY_PARAM_RETRY_SHORT;
1435 }
1436
1437 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1438 retry_long = nla_get_u8(
1439 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1440 if (retry_long == 0) {
1441 result = -EINVAL;
1442 goto bad_res;
1443 }
1444 changed |= WIPHY_PARAM_RETRY_LONG;
1445 }
1446
1447 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1448 frag_threshold = nla_get_u32(
1449 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1450 if (frag_threshold < 256) {
1451 result = -EINVAL;
1452 goto bad_res;
1453 }
1454 if (frag_threshold != (u32) -1) {
1455 /*
1456 * Fragments (apart from the last one) are required to
1457 * have even length. Make the fragmentation code
1458 * simpler by stripping LSB should someone try to use
1459 * odd threshold value.
1460 */
1461 frag_threshold &= ~0x1;
1462 }
1463 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1464 }
1465
1466 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1467 rts_threshold = nla_get_u32(
1468 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1469 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1470 }
1471
Lukáš Turek81077e82009-12-21 22:50:47 +01001472 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1473 coverage_class = nla_get_u8(
1474 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1475 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1476 }
1477
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001478 if (changed) {
1479 u8 old_retry_short, old_retry_long;
1480 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001481 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001482
1483 if (!rdev->ops->set_wiphy_params) {
1484 result = -EOPNOTSUPP;
1485 goto bad_res;
1486 }
1487
1488 old_retry_short = rdev->wiphy.retry_short;
1489 old_retry_long = rdev->wiphy.retry_long;
1490 old_frag_threshold = rdev->wiphy.frag_threshold;
1491 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001492 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001493
1494 if (changed & WIPHY_PARAM_RETRY_SHORT)
1495 rdev->wiphy.retry_short = retry_short;
1496 if (changed & WIPHY_PARAM_RETRY_LONG)
1497 rdev->wiphy.retry_long = retry_long;
1498 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1499 rdev->wiphy.frag_threshold = frag_threshold;
1500 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1501 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001502 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1503 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001504
1505 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1506 if (result) {
1507 rdev->wiphy.retry_short = old_retry_short;
1508 rdev->wiphy.retry_long = old_retry_long;
1509 rdev->wiphy.frag_threshold = old_frag_threshold;
1510 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001511 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001512 }
1513 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001514
Johannes Berg306d6112008-12-08 12:39:04 +01001515 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001516 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001517 if (netdev)
1518 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001519 return result;
1520}
1521
1522
1523static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001524 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001525 struct net_device *dev)
1526{
1527 void *hdr;
1528
1529 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1530 if (!hdr)
1531 return -1;
1532
David S. Miller9360ffd2012-03-29 04:41:26 -04001533 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1534 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1535 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1536 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1537 dev->ieee80211_ptr->iftype) ||
1538 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1539 rdev->devlist_generation ^
1540 (cfg80211_rdev_list_generation << 2)))
1541 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001542
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001543 if (rdev->ops->get_channel) {
1544 struct ieee80211_channel *chan;
1545 enum nl80211_channel_type channel_type;
1546
1547 chan = rdev->ops->get_channel(&rdev->wiphy, &channel_type);
John W. Linville59ef43e2012-04-18 14:17:13 -04001548 if (chan &&
1549 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1550 chan->center_freq) ||
1551 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1552 channel_type)))
1553 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001554 }
1555
Johannes Berg55682962007-09-20 13:09:35 -04001556 return genlmsg_end(msg, hdr);
1557
1558 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001559 genlmsg_cancel(msg, hdr);
1560 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001561}
1562
1563static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1564{
1565 int wp_idx = 0;
1566 int if_idx = 0;
1567 int wp_start = cb->args[0];
1568 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001569 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001570 struct wireless_dev *wdev;
1571
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001572 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001573 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1574 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001575 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001576 if (wp_idx < wp_start) {
1577 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001578 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001579 }
Johannes Berg55682962007-09-20 13:09:35 -04001580 if_idx = 0;
1581
Johannes Bergf5ea9122009-08-07 16:17:38 +02001582 mutex_lock(&rdev->devlist_mtx);
1583 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001584 if (if_idx < if_start) {
1585 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001586 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001587 }
Johannes Berg55682962007-09-20 13:09:35 -04001588 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1589 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001590 rdev, wdev->netdev) < 0) {
1591 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001592 goto out;
1593 }
1594 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001595 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001596 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001597
1598 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001599 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001600 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001601 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001602
1603 cb->args[0] = wp_idx;
1604 cb->args[1] = if_idx;
1605
1606 return skb->len;
1607}
1608
1609static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1610{
1611 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001612 struct cfg80211_registered_device *dev = info->user_ptr[0];
1613 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001614
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001615 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001616 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001617 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001618
Johannes Bergd7264052009-04-19 16:23:20 +02001619 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001620 dev, netdev) < 0) {
1621 nlmsg_free(msg);
1622 return -ENOBUFS;
1623 }
Johannes Berg55682962007-09-20 13:09:35 -04001624
Johannes Berg134e6372009-07-10 09:51:34 +00001625 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001626}
1627
Michael Wu66f7ac52008-01-31 19:48:22 +01001628static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1629 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1630 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1631 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1632 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1633 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1634};
1635
1636static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1637{
1638 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1639 int flag;
1640
1641 *mntrflags = 0;
1642
1643 if (!nla)
1644 return -EINVAL;
1645
1646 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1647 nla, mntr_flags_policy))
1648 return -EINVAL;
1649
1650 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1651 if (flags[flag])
1652 *mntrflags |= (1<<flag);
1653
1654 return 0;
1655}
1656
Johannes Berg9bc383d2009-11-19 11:55:19 +01001657static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001658 struct net_device *netdev, u8 use_4addr,
1659 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001660{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001661 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001662 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001663 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001664 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001665 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001666
1667 switch (iftype) {
1668 case NL80211_IFTYPE_AP_VLAN:
1669 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1670 return 0;
1671 break;
1672 case NL80211_IFTYPE_STATION:
1673 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1674 return 0;
1675 break;
1676 default:
1677 break;
1678 }
1679
1680 return -EOPNOTSUPP;
1681}
1682
Johannes Berg55682962007-09-20 13:09:35 -04001683static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1684{
Johannes Berg4c476992010-10-04 21:36:35 +02001685 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001686 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001687 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001688 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001689 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001690 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001691 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001692
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001693 memset(&params, 0, sizeof(params));
1694
Johannes Berg04a773a2009-04-19 21:24:32 +02001695 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001696
Johannes Berg723b0382008-09-16 20:22:09 +02001697 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001698 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001699 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001700 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001701 if (ntype > NL80211_IFTYPE_MAX)
1702 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001703 }
1704
Johannes Berg92ffe052008-09-16 20:39:36 +02001705 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001706 struct wireless_dev *wdev = dev->ieee80211_ptr;
1707
Johannes Berg4c476992010-10-04 21:36:35 +02001708 if (ntype != NL80211_IFTYPE_MESH_POINT)
1709 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001710 if (netif_running(dev))
1711 return -EBUSY;
1712
1713 wdev_lock(wdev);
1714 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1715 IEEE80211_MAX_MESH_ID_LEN);
1716 wdev->mesh_id_up_len =
1717 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1718 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1719 wdev->mesh_id_up_len);
1720 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001721 }
1722
Felix Fietkau8b787642009-11-10 18:53:10 +01001723 if (info->attrs[NL80211_ATTR_4ADDR]) {
1724 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1725 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001726 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001727 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001728 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001729 } else {
1730 params.use_4addr = -1;
1731 }
1732
Johannes Berg92ffe052008-09-16 20:39:36 +02001733 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001734 if (ntype != NL80211_IFTYPE_MONITOR)
1735 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001736 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1737 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001738 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001739 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001740
1741 flags = &_flags;
1742 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001743 }
Johannes Berg3b858752009-03-12 09:55:09 +01001744
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001745 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001746 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001747 else
1748 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001749
Johannes Berg9bc383d2009-11-19 11:55:19 +01001750 if (!err && params.use_4addr != -1)
1751 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1752
Johannes Berg55682962007-09-20 13:09:35 -04001753 return err;
1754}
1755
1756static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1757{
Johannes Berg4c476992010-10-04 21:36:35 +02001758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001759 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001760 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001761 int err;
1762 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001763 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001764
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001765 memset(&params, 0, sizeof(params));
1766
Johannes Berg55682962007-09-20 13:09:35 -04001767 if (!info->attrs[NL80211_ATTR_IFNAME])
1768 return -EINVAL;
1769
1770 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1771 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1772 if (type > NL80211_IFTYPE_MAX)
1773 return -EINVAL;
1774 }
1775
Johannes Berg79c97e92009-07-07 03:56:12 +02001776 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001777 !(rdev->wiphy.interface_modes & (1 << type)))
1778 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001779
Johannes Berg9bc383d2009-11-19 11:55:19 +01001780 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001781 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001782 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001783 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001784 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001785 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001786
Michael Wu66f7ac52008-01-31 19:48:22 +01001787 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1788 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1789 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001790 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001791 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001792 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001793 if (IS_ERR(dev))
1794 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001795
Johannes Berg29cbe682010-12-03 09:20:44 +01001796 if (type == NL80211_IFTYPE_MESH_POINT &&
1797 info->attrs[NL80211_ATTR_MESH_ID]) {
1798 struct wireless_dev *wdev = dev->ieee80211_ptr;
1799
1800 wdev_lock(wdev);
1801 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1802 IEEE80211_MAX_MESH_ID_LEN);
1803 wdev->mesh_id_up_len =
1804 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1805 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1806 wdev->mesh_id_up_len);
1807 wdev_unlock(wdev);
1808 }
1809
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001810 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001811}
1812
1813static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1814{
Johannes Berg4c476992010-10-04 21:36:35 +02001815 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1816 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001817
Johannes Berg4c476992010-10-04 21:36:35 +02001818 if (!rdev->ops->del_virtual_intf)
1819 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001820
Johannes Berg4c476992010-10-04 21:36:35 +02001821 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001822}
1823
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001824static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1825{
1826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1827 struct net_device *dev = info->user_ptr[1];
1828 u16 noack_map;
1829
1830 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1831 return -EINVAL;
1832
1833 if (!rdev->ops->set_noack_map)
1834 return -EOPNOTSUPP;
1835
1836 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1837
1838 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1839}
1840
Johannes Berg41ade002007-12-19 02:03:29 +01001841struct get_key_cookie {
1842 struct sk_buff *msg;
1843 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001844 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001845};
1846
1847static void get_key_callback(void *c, struct key_params *params)
1848{
Johannes Bergb9454e82009-07-08 13:29:08 +02001849 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001850 struct get_key_cookie *cookie = c;
1851
David S. Miller9360ffd2012-03-29 04:41:26 -04001852 if ((params->key &&
1853 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1854 params->key_len, params->key)) ||
1855 (params->seq &&
1856 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1857 params->seq_len, params->seq)) ||
1858 (params->cipher &&
1859 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1860 params->cipher)))
1861 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001862
Johannes Bergb9454e82009-07-08 13:29:08 +02001863 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1864 if (!key)
1865 goto nla_put_failure;
1866
David S. Miller9360ffd2012-03-29 04:41:26 -04001867 if ((params->key &&
1868 nla_put(cookie->msg, NL80211_KEY_DATA,
1869 params->key_len, params->key)) ||
1870 (params->seq &&
1871 nla_put(cookie->msg, NL80211_KEY_SEQ,
1872 params->seq_len, params->seq)) ||
1873 (params->cipher &&
1874 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
1875 params->cipher)))
1876 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001877
David S. Miller9360ffd2012-03-29 04:41:26 -04001878 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
1879 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001880
1881 nla_nest_end(cookie->msg, key);
1882
Johannes Berg41ade002007-12-19 02:03:29 +01001883 return;
1884 nla_put_failure:
1885 cookie->error = 1;
1886}
1887
1888static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1889{
Johannes Berg4c476992010-10-04 21:36:35 +02001890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001891 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001892 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001893 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001894 const u8 *mac_addr = NULL;
1895 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001896 struct get_key_cookie cookie = {
1897 .error = 0,
1898 };
1899 void *hdr;
1900 struct sk_buff *msg;
1901
1902 if (info->attrs[NL80211_ATTR_KEY_IDX])
1903 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1904
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001905 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001906 return -EINVAL;
1907
1908 if (info->attrs[NL80211_ATTR_MAC])
1909 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1910
Johannes Berge31b8212010-10-05 19:39:30 +02001911 pairwise = !!mac_addr;
1912 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1913 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1914 if (kt >= NUM_NL80211_KEYTYPES)
1915 return -EINVAL;
1916 if (kt != NL80211_KEYTYPE_GROUP &&
1917 kt != NL80211_KEYTYPE_PAIRWISE)
1918 return -EINVAL;
1919 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1920 }
1921
Johannes Berg4c476992010-10-04 21:36:35 +02001922 if (!rdev->ops->get_key)
1923 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001924
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001925 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001926 if (!msg)
1927 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001928
1929 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1930 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001931 if (IS_ERR(hdr))
1932 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001933
1934 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001935 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001936
David S. Miller9360ffd2012-03-29 04:41:26 -04001937 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1938 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
1939 goto nla_put_failure;
1940 if (mac_addr &&
1941 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
1942 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001943
Johannes Berge31b8212010-10-05 19:39:30 +02001944 if (pairwise && mac_addr &&
1945 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1946 return -ENOENT;
1947
1948 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1949 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001950
1951 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001952 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001953
1954 if (cookie.error)
1955 goto nla_put_failure;
1956
1957 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001958 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001959
1960 nla_put_failure:
1961 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001962 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001963 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001964 return err;
1965}
1966
1967static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1968{
Johannes Berg4c476992010-10-04 21:36:35 +02001969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001970 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001971 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001972 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001973
Johannes Bergb9454e82009-07-08 13:29:08 +02001974 err = nl80211_parse_key(info, &key);
1975 if (err)
1976 return err;
1977
1978 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001979 return -EINVAL;
1980
Johannes Bergb9454e82009-07-08 13:29:08 +02001981 /* only support setting default key */
1982 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001983 return -EINVAL;
1984
Johannes Bergfffd0932009-07-08 14:22:54 +02001985 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001986
1987 if (key.def) {
1988 if (!rdev->ops->set_default_key) {
1989 err = -EOPNOTSUPP;
1990 goto out;
1991 }
1992
1993 err = nl80211_key_allowed(dev->ieee80211_ptr);
1994 if (err)
1995 goto out;
1996
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001997 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1998 key.def_uni, key.def_multi);
1999
2000 if (err)
2001 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002002
Johannes Berg3d23e342009-09-29 23:27:28 +02002003#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002004 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002005#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002006 } else {
2007 if (key.def_uni || !key.def_multi) {
2008 err = -EINVAL;
2009 goto out;
2010 }
2011
2012 if (!rdev->ops->set_default_mgmt_key) {
2013 err = -EOPNOTSUPP;
2014 goto out;
2015 }
2016
2017 err = nl80211_key_allowed(dev->ieee80211_ptr);
2018 if (err)
2019 goto out;
2020
2021 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2022 dev, key.idx);
2023 if (err)
2024 goto out;
2025
2026#ifdef CONFIG_CFG80211_WEXT
2027 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2028#endif
2029 }
2030
2031 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002032 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002033
Johannes Berg41ade002007-12-19 02:03:29 +01002034 return err;
2035}
2036
2037static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2038{
Johannes Berg4c476992010-10-04 21:36:35 +02002039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002040 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002041 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002042 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002043 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002044
Johannes Bergb9454e82009-07-08 13:29:08 +02002045 err = nl80211_parse_key(info, &key);
2046 if (err)
2047 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002048
Johannes Bergb9454e82009-07-08 13:29:08 +02002049 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002050 return -EINVAL;
2051
Johannes Berg41ade002007-12-19 02:03:29 +01002052 if (info->attrs[NL80211_ATTR_MAC])
2053 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2054
Johannes Berge31b8212010-10-05 19:39:30 +02002055 if (key.type == -1) {
2056 if (mac_addr)
2057 key.type = NL80211_KEYTYPE_PAIRWISE;
2058 else
2059 key.type = NL80211_KEYTYPE_GROUP;
2060 }
2061
2062 /* for now */
2063 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2064 key.type != NL80211_KEYTYPE_GROUP)
2065 return -EINVAL;
2066
Johannes Berg4c476992010-10-04 21:36:35 +02002067 if (!rdev->ops->add_key)
2068 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002069
Johannes Berge31b8212010-10-05 19:39:30 +02002070 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2071 key.type == NL80211_KEYTYPE_PAIRWISE,
2072 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002073 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002074
2075 wdev_lock(dev->ieee80211_ptr);
2076 err = nl80211_key_allowed(dev->ieee80211_ptr);
2077 if (!err)
2078 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002079 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002080 mac_addr, &key.p);
2081 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002082
Johannes Berg41ade002007-12-19 02:03:29 +01002083 return err;
2084}
2085
2086static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2087{
Johannes Berg4c476992010-10-04 21:36:35 +02002088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002089 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002090 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002091 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002092 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002093
Johannes Bergb9454e82009-07-08 13:29:08 +02002094 err = nl80211_parse_key(info, &key);
2095 if (err)
2096 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002097
2098 if (info->attrs[NL80211_ATTR_MAC])
2099 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2100
Johannes Berge31b8212010-10-05 19:39:30 +02002101 if (key.type == -1) {
2102 if (mac_addr)
2103 key.type = NL80211_KEYTYPE_PAIRWISE;
2104 else
2105 key.type = NL80211_KEYTYPE_GROUP;
2106 }
2107
2108 /* for now */
2109 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2110 key.type != NL80211_KEYTYPE_GROUP)
2111 return -EINVAL;
2112
Johannes Berg4c476992010-10-04 21:36:35 +02002113 if (!rdev->ops->del_key)
2114 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002115
Johannes Bergfffd0932009-07-08 14:22:54 +02002116 wdev_lock(dev->ieee80211_ptr);
2117 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002118
2119 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2120 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2121 err = -ENOENT;
2122
Johannes Bergfffd0932009-07-08 14:22:54 +02002123 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002124 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2125 key.type == NL80211_KEYTYPE_PAIRWISE,
2126 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002127
Johannes Berg3d23e342009-09-29 23:27:28 +02002128#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002129 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002130 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002131 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002132 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002133 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2134 }
2135#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002136 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002137
Johannes Berg41ade002007-12-19 02:03:29 +01002138 return err;
2139}
2140
Johannes Berg88600202012-02-13 15:17:18 +01002141static int nl80211_parse_beacon(struct genl_info *info,
2142 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002143{
Johannes Berg88600202012-02-13 15:17:18 +01002144 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002145
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002146 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2147 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2148 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2149 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002150 return -EINVAL;
2151
Johannes Berg88600202012-02-13 15:17:18 +01002152 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002153
Johannes Berged1b6cc2007-12-19 02:03:32 +01002154 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002155 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2156 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2157 if (!bcn->head_len)
2158 return -EINVAL;
2159 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002160 }
2161
2162 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002163 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2164 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002165 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002166 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002167 }
2168
Johannes Berg4c476992010-10-04 21:36:35 +02002169 if (!haveinfo)
2170 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002171
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002172 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002173 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2174 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002175 }
2176
2177 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002178 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002179 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002180 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002181 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2182 }
2183
2184 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002185 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002186 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002187 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002188 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2189 }
2190
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002191 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002192 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002193 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002194 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002195 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2196 }
2197
Johannes Berg88600202012-02-13 15:17:18 +01002198 return 0;
2199}
2200
2201static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2202{
2203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2204 struct net_device *dev = info->user_ptr[1];
2205 struct wireless_dev *wdev = dev->ieee80211_ptr;
2206 struct cfg80211_ap_settings params;
2207 int err;
2208
2209 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2210 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2211 return -EOPNOTSUPP;
2212
2213 if (!rdev->ops->start_ap)
2214 return -EOPNOTSUPP;
2215
2216 if (wdev->beacon_interval)
2217 return -EALREADY;
2218
2219 memset(&params, 0, sizeof(params));
2220
2221 /* these are required for START_AP */
2222 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2223 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2224 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2225 return -EINVAL;
2226
2227 err = nl80211_parse_beacon(info, &params.beacon);
2228 if (err)
2229 return err;
2230
2231 params.beacon_interval =
2232 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2233 params.dtim_period =
2234 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2235
2236 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2237 if (err)
2238 return err;
2239
2240 /*
2241 * In theory, some of these attributes should be required here
2242 * but since they were not used when the command was originally
2243 * added, keep them optional for old user space programs to let
2244 * them continue to work with drivers that do not need the
2245 * additional information -- drivers must check!
2246 */
2247 if (info->attrs[NL80211_ATTR_SSID]) {
2248 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2249 params.ssid_len =
2250 nla_len(info->attrs[NL80211_ATTR_SSID]);
2251 if (params.ssid_len == 0 ||
2252 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2253 return -EINVAL;
2254 }
2255
2256 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2257 params.hidden_ssid = nla_get_u32(
2258 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2259 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2260 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2261 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2262 return -EINVAL;
2263 }
2264
2265 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2266
2267 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2268 params.auth_type = nla_get_u32(
2269 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2270 if (!nl80211_valid_auth_type(params.auth_type))
2271 return -EINVAL;
2272 } else
2273 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2274
2275 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2276 NL80211_MAX_NR_CIPHER_SUITES);
2277 if (err)
2278 return err;
2279
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302280 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2281 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2282 return -EOPNOTSUPP;
2283 params.inactivity_timeout = nla_get_u16(
2284 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2285 }
2286
Johannes Berg88600202012-02-13 15:17:18 +01002287 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2288 if (!err)
2289 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002290 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002291}
2292
Johannes Berg88600202012-02-13 15:17:18 +01002293static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2294{
2295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2296 struct net_device *dev = info->user_ptr[1];
2297 struct wireless_dev *wdev = dev->ieee80211_ptr;
2298 struct cfg80211_beacon_data params;
2299 int err;
2300
2301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2303 return -EOPNOTSUPP;
2304
2305 if (!rdev->ops->change_beacon)
2306 return -EOPNOTSUPP;
2307
2308 if (!wdev->beacon_interval)
2309 return -EINVAL;
2310
2311 err = nl80211_parse_beacon(info, &params);
2312 if (err)
2313 return err;
2314
2315 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2316}
2317
2318static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002319{
Johannes Berg4c476992010-10-04 21:36:35 +02002320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2321 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002322 struct wireless_dev *wdev = dev->ieee80211_ptr;
2323 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002324
Johannes Berg88600202012-02-13 15:17:18 +01002325 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002326 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002327
Johannes Berg074ac8d2010-09-16 14:58:22 +02002328 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002329 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2330 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002331
Johannes Berg88600202012-02-13 15:17:18 +01002332 if (!wdev->beacon_interval)
2333 return -ENOENT;
2334
2335 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002336 if (!err)
2337 wdev->beacon_interval = 0;
2338 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002339}
2340
Johannes Berg5727ef12007-12-19 02:03:34 +01002341static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2342 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2343 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2344 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002345 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002346 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002347 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002348};
2349
Johannes Bergeccb8e82009-05-11 21:57:56 +03002350static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002351 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002352 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002353{
2354 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002355 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002356 int flag;
2357
Johannes Bergeccb8e82009-05-11 21:57:56 +03002358 /*
2359 * Try parsing the new attribute first so userspace
2360 * can specify both for older kernels.
2361 */
2362 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2363 if (nla) {
2364 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002365
Johannes Bergeccb8e82009-05-11 21:57:56 +03002366 sta_flags = nla_data(nla);
2367 params->sta_flags_mask = sta_flags->mask;
2368 params->sta_flags_set = sta_flags->set;
2369 if ((params->sta_flags_mask |
2370 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2371 return -EINVAL;
2372 return 0;
2373 }
2374
2375 /* if present, parse the old attribute */
2376
2377 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002378 if (!nla)
2379 return 0;
2380
2381 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2382 nla, sta_flags_policy))
2383 return -EINVAL;
2384
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002385 /*
2386 * Only allow certain flags for interface types so that
2387 * other attributes are silently ignored. Remember that
2388 * this is backward compatibility code with old userspace
2389 * and shouldn't be hit in other cases anyway.
2390 */
2391 switch (iftype) {
2392 case NL80211_IFTYPE_AP:
2393 case NL80211_IFTYPE_AP_VLAN:
2394 case NL80211_IFTYPE_P2P_GO:
2395 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2396 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2397 BIT(NL80211_STA_FLAG_WME) |
2398 BIT(NL80211_STA_FLAG_MFP);
2399 break;
2400 case NL80211_IFTYPE_P2P_CLIENT:
2401 case NL80211_IFTYPE_STATION:
2402 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2403 BIT(NL80211_STA_FLAG_TDLS_PEER);
2404 break;
2405 case NL80211_IFTYPE_MESH_POINT:
2406 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2407 BIT(NL80211_STA_FLAG_MFP) |
2408 BIT(NL80211_STA_FLAG_AUTHORIZED);
2409 default:
2410 return -EINVAL;
2411 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002412
Johannes Berg3383b5a2012-05-10 20:14:43 +02002413 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2414 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002415 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002416
Johannes Berg3383b5a2012-05-10 20:14:43 +02002417 /* no longer support new API additions in old API */
2418 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2419 return -EINVAL;
2420 }
2421 }
2422
Johannes Berg5727ef12007-12-19 02:03:34 +01002423 return 0;
2424}
2425
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002426static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2427 int attr)
2428{
2429 struct nlattr *rate;
2430 u16 bitrate;
2431
2432 rate = nla_nest_start(msg, attr);
2433 if (!rate)
2434 goto nla_put_failure;
2435
2436 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2437 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002438 if ((bitrate > 0 &&
2439 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2440 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2441 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2442 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2443 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2444 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2445 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2446 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002447
2448 nla_nest_end(msg, rate);
2449 return true;
2450
2451nla_put_failure:
2452 return false;
2453}
2454
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002455static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002456 int flags,
2457 struct cfg80211_registered_device *rdev,
2458 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002459 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002460{
2461 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002462 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002463
2464 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2465 if (!hdr)
2466 return -1;
2467
David S. Miller9360ffd2012-03-29 04:41:26 -04002468 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2469 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2470 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2471 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002472
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002473 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2474 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002475 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002476 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2477 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2478 sinfo->connected_time))
2479 goto nla_put_failure;
2480 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2481 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2482 sinfo->inactive_time))
2483 goto nla_put_failure;
2484 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2485 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2486 sinfo->rx_bytes))
2487 goto nla_put_failure;
2488 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2489 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2490 sinfo->tx_bytes))
2491 goto nla_put_failure;
2492 if ((sinfo->filled & STATION_INFO_LLID) &&
2493 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2494 goto nla_put_failure;
2495 if ((sinfo->filled & STATION_INFO_PLID) &&
2496 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2497 goto nla_put_failure;
2498 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2499 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2500 sinfo->plink_state))
2501 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002502 switch (rdev->wiphy.signal_type) {
2503 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002504 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2505 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2506 sinfo->signal))
2507 goto nla_put_failure;
2508 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2509 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2510 sinfo->signal_avg))
2511 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002512 break;
2513 default:
2514 break;
2515 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002516 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002517 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2518 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002519 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002520 }
2521 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2522 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2523 NL80211_STA_INFO_RX_BITRATE))
2524 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002525 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002526 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2527 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2528 sinfo->rx_packets))
2529 goto nla_put_failure;
2530 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2531 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2532 sinfo->tx_packets))
2533 goto nla_put_failure;
2534 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2535 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2536 sinfo->tx_retries))
2537 goto nla_put_failure;
2538 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2539 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2540 sinfo->tx_failed))
2541 goto nla_put_failure;
2542 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2543 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2544 sinfo->beacon_loss_count))
2545 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002546 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2547 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2548 if (!bss_param)
2549 goto nla_put_failure;
2550
David S. Miller9360ffd2012-03-29 04:41:26 -04002551 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2552 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2553 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2554 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2555 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2556 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2557 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2558 sinfo->bss_param.dtim_period) ||
2559 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2560 sinfo->bss_param.beacon_interval))
2561 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002562
2563 nla_nest_end(msg, bss_param);
2564 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002565 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2566 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2567 sizeof(struct nl80211_sta_flag_update),
2568 &sinfo->sta_flags))
2569 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002570 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2571 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2572 sinfo->t_offset))
2573 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002574 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002575
David S. Miller9360ffd2012-03-29 04:41:26 -04002576 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2577 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2578 sinfo->assoc_req_ies))
2579 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002580
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002581 return genlmsg_end(msg, hdr);
2582
2583 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002584 genlmsg_cancel(msg, hdr);
2585 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002586}
2587
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002588static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002589 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002590{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002591 struct station_info sinfo;
2592 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002593 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002594 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002595 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002596 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002597
Johannes Berg67748892010-10-04 21:14:06 +02002598 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2599 if (err)
2600 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002601
2602 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002603 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002604 goto out_err;
2605 }
2606
Johannes Bergbba95fe2008-07-29 13:22:51 +02002607 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002608 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002609 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2610 mac_addr, &sinfo);
2611 if (err == -ENOENT)
2612 break;
2613 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002614 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002615
2616 if (nl80211_send_station(skb,
2617 NETLINK_CB(cb->skb).pid,
2618 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002619 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002620 &sinfo) < 0)
2621 goto out;
2622
2623 sta_idx++;
2624 }
2625
2626
2627 out:
2628 cb->args[1] = sta_idx;
2629 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002630 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002631 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002632
2633 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002634}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002635
Johannes Berg5727ef12007-12-19 02:03:34 +01002636static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2637{
Johannes Berg4c476992010-10-04 21:36:35 +02002638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2639 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002640 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002641 struct sk_buff *msg;
2642 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002643 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002644
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002645 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002646
2647 if (!info->attrs[NL80211_ATTR_MAC])
2648 return -EINVAL;
2649
2650 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2651
Johannes Berg4c476992010-10-04 21:36:35 +02002652 if (!rdev->ops->get_station)
2653 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002654
Johannes Berg79c97e92009-07-07 03:56:12 +02002655 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002656 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002657 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002658
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002660 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002661 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002662
2663 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002664 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002665 nlmsg_free(msg);
2666 return -ENOBUFS;
2667 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002668
Johannes Berg4c476992010-10-04 21:36:35 +02002669 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002670}
2671
2672/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002673 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002674 */
Johannes Berg80b99892011-11-18 16:23:01 +01002675static struct net_device *get_vlan(struct genl_info *info,
2676 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002677{
Johannes Berg463d0182009-07-14 00:33:35 +02002678 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002679 struct net_device *v;
2680 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002681
Johannes Berg80b99892011-11-18 16:23:01 +01002682 if (!vlanattr)
2683 return NULL;
2684
2685 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2686 if (!v)
2687 return ERR_PTR(-ENODEV);
2688
2689 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2690 ret = -EINVAL;
2691 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002692 }
Johannes Berg80b99892011-11-18 16:23:01 +01002693
2694 if (!netif_running(v)) {
2695 ret = -ENETDOWN;
2696 goto error;
2697 }
2698
2699 return v;
2700 error:
2701 dev_put(v);
2702 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002703}
2704
2705static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2706{
Johannes Berg4c476992010-10-04 21:36:35 +02002707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002708 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002709 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002710 struct station_parameters params;
2711 u8 *mac_addr = NULL;
2712
2713 memset(&params, 0, sizeof(params));
2714
2715 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002716 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002717
2718 if (info->attrs[NL80211_ATTR_STA_AID])
2719 return -EINVAL;
2720
2721 if (!info->attrs[NL80211_ATTR_MAC])
2722 return -EINVAL;
2723
2724 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2725
2726 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2727 params.supported_rates =
2728 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2729 params.supported_rates_len =
2730 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2731 }
2732
2733 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2734 params.listen_interval =
2735 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2736
Jouni Malinen36aedc92008-08-25 11:58:58 +03002737 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2738 params.ht_capa =
2739 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2740
Johannes Bergbdd90d52011-12-14 12:20:27 +01002741 if (!rdev->ops->change_station)
2742 return -EOPNOTSUPP;
2743
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002744 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002745 return -EINVAL;
2746
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002747 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2748 params.plink_action =
2749 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2750
Javier Cardona9c3990a2011-05-03 16:57:11 -07002751 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2752 params.plink_state =
2753 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2754
Johannes Berga97f4422009-06-18 17:23:43 +02002755 switch (dev->ieee80211_ptr->iftype) {
2756 case NL80211_IFTYPE_AP:
2757 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002758 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002759 /* disallow mesh-specific things */
2760 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002761 return -EINVAL;
2762
2763 /* TDLS can't be set, ... */
2764 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2765 return -EINVAL;
2766 /*
2767 * ... but don't bother the driver with it. This works around
2768 * a hostapd/wpa_supplicant issue -- it always includes the
2769 * TLDS_PEER flag in the mask even for AP mode.
2770 */
2771 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2772
2773 /* accept only the listed bits */
2774 if (params.sta_flags_mask &
2775 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2776 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2777 BIT(NL80211_STA_FLAG_WME) |
2778 BIT(NL80211_STA_FLAG_MFP)))
2779 return -EINVAL;
2780
2781 /* must be last in here for error handling */
2782 params.vlan = get_vlan(info, rdev);
2783 if (IS_ERR(params.vlan))
2784 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002785 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002786 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002787 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002788 /*
2789 * Don't allow userspace to change the TDLS_PEER flag,
2790 * but silently ignore attempts to change it since we
2791 * don't have state here to verify that it doesn't try
2792 * to change the flag.
2793 */
2794 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002795 /* fall through */
2796 case NL80211_IFTYPE_ADHOC:
2797 /* disallow things sta doesn't support */
2798 if (params.plink_action)
2799 return -EINVAL;
2800 if (params.ht_capa)
2801 return -EINVAL;
2802 if (params.listen_interval >= 0)
2803 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002804 /* reject any changes other than AUTHORIZED */
2805 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2806 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002807 break;
2808 case NL80211_IFTYPE_MESH_POINT:
2809 /* disallow things mesh doesn't support */
2810 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002811 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002812 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002813 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002814 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002815 return -EINVAL;
2816 /*
2817 * No special handling for TDLS here -- the userspace
2818 * mesh code doesn't have this bug.
2819 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002820 if (params.sta_flags_mask &
2821 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002822 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002823 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002824 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002825 break;
2826 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002827 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002828 }
2829
Johannes Bergbdd90d52011-12-14 12:20:27 +01002830 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002831
Johannes Berg79c97e92009-07-07 03:56:12 +02002832 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002833
Johannes Berg5727ef12007-12-19 02:03:34 +01002834 if (params.vlan)
2835 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002836
Johannes Berg5727ef12007-12-19 02:03:34 +01002837 return err;
2838}
2839
Eliad Pellerc75786c2011-08-23 14:37:46 +03002840static struct nla_policy
2841nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2842 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2843 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2844};
2845
Johannes Berg5727ef12007-12-19 02:03:34 +01002846static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2847{
Johannes Berg4c476992010-10-04 21:36:35 +02002848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002849 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002850 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002851 struct station_parameters params;
2852 u8 *mac_addr = NULL;
2853
2854 memset(&params, 0, sizeof(params));
2855
2856 if (!info->attrs[NL80211_ATTR_MAC])
2857 return -EINVAL;
2858
Johannes Berg5727ef12007-12-19 02:03:34 +01002859 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2860 return -EINVAL;
2861
2862 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2863 return -EINVAL;
2864
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002865 if (!info->attrs[NL80211_ATTR_STA_AID])
2866 return -EINVAL;
2867
Johannes Berg5727ef12007-12-19 02:03:34 +01002868 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2869 params.supported_rates =
2870 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2871 params.supported_rates_len =
2872 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2873 params.listen_interval =
2874 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002875
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002876 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2877 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2878 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002879
Jouni Malinen36aedc92008-08-25 11:58:58 +03002880 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2881 params.ht_capa =
2882 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002883
Javier Cardona96b78df2011-04-07 15:08:33 -07002884 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2885 params.plink_action =
2886 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2887
Johannes Bergbdd90d52011-12-14 12:20:27 +01002888 if (!rdev->ops->add_station)
2889 return -EOPNOTSUPP;
2890
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002891 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002892 return -EINVAL;
2893
Johannes Bergbdd90d52011-12-14 12:20:27 +01002894 switch (dev->ieee80211_ptr->iftype) {
2895 case NL80211_IFTYPE_AP:
2896 case NL80211_IFTYPE_AP_VLAN:
2897 case NL80211_IFTYPE_P2P_GO:
2898 /* parse WME attributes if sta is WME capable */
2899 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2900 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2901 info->attrs[NL80211_ATTR_STA_WME]) {
2902 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2903 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002904
Johannes Bergbdd90d52011-12-14 12:20:27 +01002905 nla = info->attrs[NL80211_ATTR_STA_WME];
2906 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2907 nl80211_sta_wme_policy);
2908 if (err)
2909 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002910
Johannes Bergbdd90d52011-12-14 12:20:27 +01002911 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2912 params.uapsd_queues =
2913 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2914 if (params.uapsd_queues &
2915 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2916 return -EINVAL;
2917
2918 if (tb[NL80211_STA_WME_MAX_SP])
2919 params.max_sp =
2920 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2921
2922 if (params.max_sp &
2923 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2924 return -EINVAL;
2925
2926 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2927 }
2928 /* TDLS peers cannot be added */
2929 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002930 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002931 /* but don't bother the driver with it */
2932 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002933
Johannes Bergbdd90d52011-12-14 12:20:27 +01002934 /* must be last in here for error handling */
2935 params.vlan = get_vlan(info, rdev);
2936 if (IS_ERR(params.vlan))
2937 return PTR_ERR(params.vlan);
2938 break;
2939 case NL80211_IFTYPE_MESH_POINT:
2940 /* TDLS peers cannot be added */
2941 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002942 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002943 break;
2944 case NL80211_IFTYPE_STATION:
2945 /* Only TDLS peers can be added */
2946 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2947 return -EINVAL;
2948 /* Can only add if TDLS ... */
2949 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2950 return -EOPNOTSUPP;
2951 /* ... with external setup is supported */
2952 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2953 return -EOPNOTSUPP;
2954 break;
2955 default:
2956 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002957 }
2958
Johannes Bergbdd90d52011-12-14 12:20:27 +01002959 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002960
Johannes Berg79c97e92009-07-07 03:56:12 +02002961 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002962
Johannes Berg5727ef12007-12-19 02:03:34 +01002963 if (params.vlan)
2964 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002965 return err;
2966}
2967
2968static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2969{
Johannes Berg4c476992010-10-04 21:36:35 +02002970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2971 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002972 u8 *mac_addr = NULL;
2973
2974 if (info->attrs[NL80211_ATTR_MAC])
2975 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2976
Johannes Berge80cf852009-05-11 14:43:13 +02002977 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002978 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002979 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002980 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2981 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002982
Johannes Berg4c476992010-10-04 21:36:35 +02002983 if (!rdev->ops->del_station)
2984 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002985
Johannes Berg4c476992010-10-04 21:36:35 +02002986 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002987}
2988
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002989static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2990 int flags, struct net_device *dev,
2991 u8 *dst, u8 *next_hop,
2992 struct mpath_info *pinfo)
2993{
2994 void *hdr;
2995 struct nlattr *pinfoattr;
2996
2997 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2998 if (!hdr)
2999 return -1;
3000
David S. Miller9360ffd2012-03-29 04:41:26 -04003001 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3002 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3003 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3004 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3005 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003006
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003007 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3008 if (!pinfoattr)
3009 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003010 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3011 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3012 pinfo->frame_qlen))
3013 goto nla_put_failure;
3014 if (((pinfo->filled & MPATH_INFO_SN) &&
3015 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3016 ((pinfo->filled & MPATH_INFO_METRIC) &&
3017 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3018 pinfo->metric)) ||
3019 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3020 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3021 pinfo->exptime)) ||
3022 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3023 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3024 pinfo->flags)) ||
3025 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3026 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3027 pinfo->discovery_timeout)) ||
3028 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3029 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3030 pinfo->discovery_retries)))
3031 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003032
3033 nla_nest_end(msg, pinfoattr);
3034
3035 return genlmsg_end(msg, hdr);
3036
3037 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003038 genlmsg_cancel(msg, hdr);
3039 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003040}
3041
3042static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003043 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003044{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003045 struct mpath_info pinfo;
3046 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003047 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003048 u8 dst[ETH_ALEN];
3049 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003050 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003051 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003052
Johannes Berg67748892010-10-04 21:14:06 +02003053 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3054 if (err)
3055 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003056
3057 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003058 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003059 goto out_err;
3060 }
3061
Jouni Malineneec60b02009-03-20 21:21:19 +02003062 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3063 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003064 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003065 }
3066
Johannes Bergbba95fe2008-07-29 13:22:51 +02003067 while (1) {
3068 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3069 dst, next_hop, &pinfo);
3070 if (err == -ENOENT)
3071 break;
3072 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003073 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003074
3075 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3076 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3077 netdev, dst, next_hop,
3078 &pinfo) < 0)
3079 goto out;
3080
3081 path_idx++;
3082 }
3083
3084
3085 out:
3086 cb->args[1] = path_idx;
3087 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003088 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003089 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003090 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003091}
3092
3093static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3094{
Johannes Berg4c476992010-10-04 21:36:35 +02003095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003096 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003097 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003098 struct mpath_info pinfo;
3099 struct sk_buff *msg;
3100 u8 *dst = NULL;
3101 u8 next_hop[ETH_ALEN];
3102
3103 memset(&pinfo, 0, sizeof(pinfo));
3104
3105 if (!info->attrs[NL80211_ATTR_MAC])
3106 return -EINVAL;
3107
3108 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3109
Johannes Berg4c476992010-10-04 21:36:35 +02003110 if (!rdev->ops->get_mpath)
3111 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003112
Johannes Berg4c476992010-10-04 21:36:35 +02003113 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3114 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003115
Johannes Berg79c97e92009-07-07 03:56:12 +02003116 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003117 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003118 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003119
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003120 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003121 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003122 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003123
3124 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003125 dev, dst, next_hop, &pinfo) < 0) {
3126 nlmsg_free(msg);
3127 return -ENOBUFS;
3128 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003129
Johannes Berg4c476992010-10-04 21:36:35 +02003130 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003131}
3132
3133static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3134{
Johannes Berg4c476992010-10-04 21:36:35 +02003135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3136 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003137 u8 *dst = NULL;
3138 u8 *next_hop = NULL;
3139
3140 if (!info->attrs[NL80211_ATTR_MAC])
3141 return -EINVAL;
3142
3143 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3144 return -EINVAL;
3145
3146 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3147 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3148
Johannes Berg4c476992010-10-04 21:36:35 +02003149 if (!rdev->ops->change_mpath)
3150 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003151
Johannes Berg4c476992010-10-04 21:36:35 +02003152 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3153 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003154
Johannes Berg4c476992010-10-04 21:36:35 +02003155 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003156}
Johannes Berg4c476992010-10-04 21:36:35 +02003157
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003158static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3159{
Johannes Berg4c476992010-10-04 21:36:35 +02003160 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3161 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003162 u8 *dst = NULL;
3163 u8 *next_hop = NULL;
3164
3165 if (!info->attrs[NL80211_ATTR_MAC])
3166 return -EINVAL;
3167
3168 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3169 return -EINVAL;
3170
3171 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3172 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3173
Johannes Berg4c476992010-10-04 21:36:35 +02003174 if (!rdev->ops->add_mpath)
3175 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003176
Johannes Berg4c476992010-10-04 21:36:35 +02003177 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3178 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003179
Johannes Berg4c476992010-10-04 21:36:35 +02003180 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003181}
3182
3183static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3184{
Johannes Berg4c476992010-10-04 21:36:35 +02003185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3186 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003187 u8 *dst = NULL;
3188
3189 if (info->attrs[NL80211_ATTR_MAC])
3190 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3191
Johannes Berg4c476992010-10-04 21:36:35 +02003192 if (!rdev->ops->del_mpath)
3193 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003194
Johannes Berg4c476992010-10-04 21:36:35 +02003195 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003196}
3197
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003198static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3199{
Johannes Berg4c476992010-10-04 21:36:35 +02003200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3201 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003202 struct bss_parameters params;
3203
3204 memset(&params, 0, sizeof(params));
3205 /* default to not changing parameters */
3206 params.use_cts_prot = -1;
3207 params.use_short_preamble = -1;
3208 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003209 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003210 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003211
3212 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3213 params.use_cts_prot =
3214 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3215 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3216 params.use_short_preamble =
3217 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3218 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3219 params.use_short_slot_time =
3220 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003221 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3222 params.basic_rates =
3223 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3224 params.basic_rates_len =
3225 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3226 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003227 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3228 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003229 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3230 params.ht_opmode =
3231 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003232
Johannes Berg4c476992010-10-04 21:36:35 +02003233 if (!rdev->ops->change_bss)
3234 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003235
Johannes Berg074ac8d2010-09-16 14:58:22 +02003236 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003237 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3238 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003239
Johannes Berg4c476992010-10-04 21:36:35 +02003240 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003241}
3242
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003243static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003244 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3245 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3246 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3247 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3248 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3249 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3250};
3251
3252static int parse_reg_rule(struct nlattr *tb[],
3253 struct ieee80211_reg_rule *reg_rule)
3254{
3255 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3256 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3257
3258 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3259 return -EINVAL;
3260 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3261 return -EINVAL;
3262 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3263 return -EINVAL;
3264 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3265 return -EINVAL;
3266 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3267 return -EINVAL;
3268
3269 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3270
3271 freq_range->start_freq_khz =
3272 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3273 freq_range->end_freq_khz =
3274 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3275 freq_range->max_bandwidth_khz =
3276 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3277
3278 power_rule->max_eirp =
3279 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3280
3281 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3282 power_rule->max_antenna_gain =
3283 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3284
3285 return 0;
3286}
3287
3288static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3289{
3290 int r;
3291 char *data = NULL;
3292
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003293 /*
3294 * You should only get this when cfg80211 hasn't yet initialized
3295 * completely when built-in to the kernel right between the time
3296 * window between nl80211_init() and regulatory_init(), if that is
3297 * even possible.
3298 */
3299 mutex_lock(&cfg80211_mutex);
3300 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003301 mutex_unlock(&cfg80211_mutex);
3302 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003303 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003304 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003305
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003306 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3307 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003308
3309 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3310
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003311 r = regulatory_hint_user(data);
3312
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003313 return r;
3314}
3315
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003316static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003317 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003318{
Johannes Berg4c476992010-10-04 21:36:35 +02003319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003320 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003321 struct wireless_dev *wdev = dev->ieee80211_ptr;
3322 struct mesh_config cur_params;
3323 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003324 void *hdr;
3325 struct nlattr *pinfoattr;
3326 struct sk_buff *msg;
3327
Johannes Berg29cbe682010-12-03 09:20:44 +01003328 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3329 return -EOPNOTSUPP;
3330
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003331 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003332 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003333
Johannes Berg29cbe682010-12-03 09:20:44 +01003334 wdev_lock(wdev);
3335 /* If not connected, get default parameters */
3336 if (!wdev->mesh_id_len)
3337 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3338 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003339 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003340 &cur_params);
3341 wdev_unlock(wdev);
3342
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003343 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003344 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003345
3346 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003347 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003348 if (!msg)
3349 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003350 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003351 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003352 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003353 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003354 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003355 if (!pinfoattr)
3356 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003357 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3358 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3359 cur_params.dot11MeshRetryTimeout) ||
3360 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3361 cur_params.dot11MeshConfirmTimeout) ||
3362 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3363 cur_params.dot11MeshHoldingTimeout) ||
3364 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3365 cur_params.dot11MeshMaxPeerLinks) ||
3366 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3367 cur_params.dot11MeshMaxRetries) ||
3368 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3369 cur_params.dot11MeshTTL) ||
3370 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3371 cur_params.element_ttl) ||
3372 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3373 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003374 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3375 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003376 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3377 cur_params.dot11MeshHWMPmaxPREQretries) ||
3378 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3379 cur_params.path_refresh_time) ||
3380 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3381 cur_params.min_discovery_timeout) ||
3382 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3383 cur_params.dot11MeshHWMPactivePathTimeout) ||
3384 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3385 cur_params.dot11MeshHWMPpreqMinInterval) ||
3386 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3387 cur_params.dot11MeshHWMPperrMinInterval) ||
3388 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3389 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3390 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3391 cur_params.dot11MeshHWMPRootMode) ||
3392 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3393 cur_params.dot11MeshHWMPRannInterval) ||
3394 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3395 cur_params.dot11MeshGateAnnouncementProtocol) ||
3396 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3397 cur_params.dot11MeshForwarding) ||
3398 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003399 cur_params.rssi_threshold) ||
3400 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
3401 cur_params.ht_opmode))
David S. Miller9360ffd2012-03-29 04:41:26 -04003402 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003403 nla_nest_end(msg, pinfoattr);
3404 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003405 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003406
Johannes Berg3b858752009-03-12 09:55:09 +01003407 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003408 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003409 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003410 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003411 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003412}
3413
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003414static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003415 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3416 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3417 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3418 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3419 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3420 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003421 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003422 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003423 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003424
3425 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3426 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3427 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3428 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3429 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003430 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003431 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003432 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003433 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003434 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003435 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003436 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003437 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003438};
3439
Javier Cardonac80d5452010-12-16 17:37:49 -08003440static const struct nla_policy
3441 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003442 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003443 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3444 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003445 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003446 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003447 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003448 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003449};
3450
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003451static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003452 struct mesh_config *cfg,
3453 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003454{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003455 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003456 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003457
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003458#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3459do {\
3460 if (table[attr_num]) {\
3461 cfg->param = nla_fn(table[attr_num]); \
3462 mask |= (1 << (attr_num - 1)); \
3463 } \
3464} while (0);\
3465
3466
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003467 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003468 return -EINVAL;
3469 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003470 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003471 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003472 return -EINVAL;
3473
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003474 /* This makes sure that there aren't more than 32 mesh config
3475 * parameters (otherwise our bitfield scheme would not work.) */
3476 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3477
3478 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003479 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3480 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3481 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3482 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3483 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3484 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3485 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3486 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3487 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3488 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3489 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3490 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003491 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3492 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003493 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3494 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
Javier Cardonad299a1f2012-03-31 11:31:33 -07003495 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
3496 mask, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3497 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003498 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3499 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3500 nla_get_u8);
3501 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3502 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3503 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3504 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3505 nla_get_u16);
3506 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3507 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3508 nla_get_u32);
3509 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3510 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3511 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003512 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3513 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3514 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003515 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3516 dot11MeshHWMPnetDiameterTraversalTime,
3517 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3518 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003519 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3520 dot11MeshHWMPRootMode, mask,
3521 NL80211_MESHCONF_HWMP_ROOTMODE,
3522 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003523 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3524 dot11MeshHWMPRannInterval, mask,
3525 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3526 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003527 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3528 dot11MeshGateAnnouncementProtocol, mask,
3529 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3530 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003531 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3532 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003533 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3534 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003535 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
3536 mask, NL80211_MESHCONF_HT_OPMODE, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003537 if (mask_out)
3538 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003539
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003540 return 0;
3541
3542#undef FILL_IN_MESH_PARAM_IF_SET
3543}
3544
Javier Cardonac80d5452010-12-16 17:37:49 -08003545static int nl80211_parse_mesh_setup(struct genl_info *info,
3546 struct mesh_setup *setup)
3547{
3548 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3549
3550 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3551 return -EINVAL;
3552 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3553 info->attrs[NL80211_ATTR_MESH_SETUP],
3554 nl80211_mesh_setup_params_policy))
3555 return -EINVAL;
3556
Javier Cardonad299a1f2012-03-31 11:31:33 -07003557 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3558 setup->sync_method =
3559 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3560 IEEE80211_SYNC_METHOD_VENDOR :
3561 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3562
Javier Cardonac80d5452010-12-16 17:37:49 -08003563 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3564 setup->path_sel_proto =
3565 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3566 IEEE80211_PATH_PROTOCOL_VENDOR :
3567 IEEE80211_PATH_PROTOCOL_HWMP;
3568
3569 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3570 setup->path_metric =
3571 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3572 IEEE80211_PATH_METRIC_VENDOR :
3573 IEEE80211_PATH_METRIC_AIRTIME;
3574
Javier Cardona581a8b02011-04-07 15:08:27 -07003575
3576 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003577 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003578 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003579 if (!is_valid_ie_attr(ieattr))
3580 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003581 setup->ie = nla_data(ieattr);
3582 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003583 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003584 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3585 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003586
3587 return 0;
3588}
3589
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003590static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003591 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003592{
3593 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3594 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003595 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003596 struct mesh_config cfg;
3597 u32 mask;
3598 int err;
3599
Johannes Berg29cbe682010-12-03 09:20:44 +01003600 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3601 return -EOPNOTSUPP;
3602
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003603 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003604 return -EOPNOTSUPP;
3605
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003606 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003607 if (err)
3608 return err;
3609
Johannes Berg29cbe682010-12-03 09:20:44 +01003610 wdev_lock(wdev);
3611 if (!wdev->mesh_id_len)
3612 err = -ENOLINK;
3613
3614 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003615 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003616 mask, &cfg);
3617
3618 wdev_unlock(wdev);
3619
3620 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003621}
3622
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003623static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3624{
3625 struct sk_buff *msg;
3626 void *hdr = NULL;
3627 struct nlattr *nl_reg_rules;
3628 unsigned int i;
3629 int err = -EINVAL;
3630
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003631 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003632
3633 if (!cfg80211_regdomain)
3634 goto out;
3635
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003636 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003637 if (!msg) {
3638 err = -ENOBUFS;
3639 goto out;
3640 }
3641
3642 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3643 NL80211_CMD_GET_REG);
3644 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003645 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003646
David S. Miller9360ffd2012-03-29 04:41:26 -04003647 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3648 cfg80211_regdomain->alpha2) ||
3649 (cfg80211_regdomain->dfs_region &&
3650 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3651 cfg80211_regdomain->dfs_region)))
3652 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003653
3654 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3655 if (!nl_reg_rules)
3656 goto nla_put_failure;
3657
3658 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3659 struct nlattr *nl_reg_rule;
3660 const struct ieee80211_reg_rule *reg_rule;
3661 const struct ieee80211_freq_range *freq_range;
3662 const struct ieee80211_power_rule *power_rule;
3663
3664 reg_rule = &cfg80211_regdomain->reg_rules[i];
3665 freq_range = &reg_rule->freq_range;
3666 power_rule = &reg_rule->power_rule;
3667
3668 nl_reg_rule = nla_nest_start(msg, i);
3669 if (!nl_reg_rule)
3670 goto nla_put_failure;
3671
David S. Miller9360ffd2012-03-29 04:41:26 -04003672 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3673 reg_rule->flags) ||
3674 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3675 freq_range->start_freq_khz) ||
3676 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3677 freq_range->end_freq_khz) ||
3678 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3679 freq_range->max_bandwidth_khz) ||
3680 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3681 power_rule->max_antenna_gain) ||
3682 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3683 power_rule->max_eirp))
3684 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003685
3686 nla_nest_end(msg, nl_reg_rule);
3687 }
3688
3689 nla_nest_end(msg, nl_reg_rules);
3690
3691 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003692 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003693 goto out;
3694
3695nla_put_failure:
3696 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003697put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003698 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003699 err = -EMSGSIZE;
3700out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003701 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003702 return err;
3703}
3704
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003705static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3706{
3707 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3708 struct nlattr *nl_reg_rule;
3709 char *alpha2 = NULL;
3710 int rem_reg_rules = 0, r = 0;
3711 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003712 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003713 struct ieee80211_regdomain *rd = NULL;
3714
3715 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3716 return -EINVAL;
3717
3718 if (!info->attrs[NL80211_ATTR_REG_RULES])
3719 return -EINVAL;
3720
3721 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3722
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003723 if (info->attrs[NL80211_ATTR_DFS_REGION])
3724 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3725
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003726 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3727 rem_reg_rules) {
3728 num_rules++;
3729 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003730 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003731 }
3732
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003733 mutex_lock(&cfg80211_mutex);
3734
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003735 if (!reg_is_valid_request(alpha2)) {
3736 r = -EINVAL;
3737 goto bad_reg;
3738 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003739
3740 size_of_regd = sizeof(struct ieee80211_regdomain) +
3741 (num_rules * sizeof(struct ieee80211_reg_rule));
3742
3743 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003744 if (!rd) {
3745 r = -ENOMEM;
3746 goto bad_reg;
3747 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003748
3749 rd->n_reg_rules = num_rules;
3750 rd->alpha2[0] = alpha2[0];
3751 rd->alpha2[1] = alpha2[1];
3752
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003753 /*
3754 * Disable DFS master mode if the DFS region was
3755 * not supported or known on this kernel.
3756 */
3757 if (reg_supported_dfs_region(dfs_region))
3758 rd->dfs_region = dfs_region;
3759
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003760 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3761 rem_reg_rules) {
3762 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3763 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3764 reg_rule_policy);
3765 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3766 if (r)
3767 goto bad_reg;
3768
3769 rule_idx++;
3770
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003771 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3772 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003773 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003774 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003775 }
3776
3777 BUG_ON(rule_idx != num_rules);
3778
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003779 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003780
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003781 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003782
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003783 return r;
3784
Johannes Bergd2372b32008-10-24 20:32:20 +02003785 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003786 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003787 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003788 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003789}
3790
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003791static int validate_scan_freqs(struct nlattr *freqs)
3792{
3793 struct nlattr *attr1, *attr2;
3794 int n_channels = 0, tmp1, tmp2;
3795
3796 nla_for_each_nested(attr1, freqs, tmp1) {
3797 n_channels++;
3798 /*
3799 * Some hardware has a limited channel list for
3800 * scanning, and it is pretty much nonsensical
3801 * to scan for a channel twice, so disallow that
3802 * and don't require drivers to check that the
3803 * channel list they get isn't longer than what
3804 * they can scan, as long as they can scan all
3805 * the channels they registered at once.
3806 */
3807 nla_for_each_nested(attr2, freqs, tmp2)
3808 if (attr1 != attr2 &&
3809 nla_get_u32(attr1) == nla_get_u32(attr2))
3810 return 0;
3811 }
3812
3813 return n_channels;
3814}
3815
Johannes Berg2a519312009-02-10 21:25:55 +01003816static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3817{
Johannes Berg4c476992010-10-04 21:36:35 +02003818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3819 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003820 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003821 struct nlattr *attr;
3822 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003823 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003824 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003825
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003826 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3827 return -EINVAL;
3828
Johannes Berg79c97e92009-07-07 03:56:12 +02003829 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003830
Johannes Berg4c476992010-10-04 21:36:35 +02003831 if (!rdev->ops->scan)
3832 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003833
Johannes Berg4c476992010-10-04 21:36:35 +02003834 if (rdev->scan_req)
3835 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003836
3837 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003838 n_channels = validate_scan_freqs(
3839 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003840 if (!n_channels)
3841 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003842 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003843 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003844 n_channels = 0;
3845
Johannes Berg2a519312009-02-10 21:25:55 +01003846 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3847 if (wiphy->bands[band])
3848 n_channels += wiphy->bands[band]->n_channels;
3849 }
3850
3851 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3852 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3853 n_ssids++;
3854
Johannes Berg4c476992010-10-04 21:36:35 +02003855 if (n_ssids > wiphy->max_scan_ssids)
3856 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003857
Jouni Malinen70692ad2009-02-16 19:39:13 +02003858 if (info->attrs[NL80211_ATTR_IE])
3859 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3860 else
3861 ie_len = 0;
3862
Johannes Berg4c476992010-10-04 21:36:35 +02003863 if (ie_len > wiphy->max_scan_ie_len)
3864 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003865
Johannes Berg2a519312009-02-10 21:25:55 +01003866 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003867 + sizeof(*request->ssids) * n_ssids
3868 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003869 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003870 if (!request)
3871 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003872
Johannes Berg2a519312009-02-10 21:25:55 +01003873 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003874 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003875 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003876 if (ie_len) {
3877 if (request->ssids)
3878 request->ie = (void *)(request->ssids + n_ssids);
3879 else
3880 request->ie = (void *)(request->channels + n_channels);
3881 }
Johannes Berg2a519312009-02-10 21:25:55 +01003882
Johannes Berg584991d2009-11-02 13:32:03 +01003883 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003884 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3885 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003886 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003887 struct ieee80211_channel *chan;
3888
3889 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3890
3891 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003892 err = -EINVAL;
3893 goto out_free;
3894 }
Johannes Berg584991d2009-11-02 13:32:03 +01003895
3896 /* ignore disabled channels */
3897 if (chan->flags & IEEE80211_CHAN_DISABLED)
3898 continue;
3899
3900 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003901 i++;
3902 }
3903 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003904 enum ieee80211_band band;
3905
Johannes Berg2a519312009-02-10 21:25:55 +01003906 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003907 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3908 int j;
3909 if (!wiphy->bands[band])
3910 continue;
3911 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003912 struct ieee80211_channel *chan;
3913
3914 chan = &wiphy->bands[band]->channels[j];
3915
3916 if (chan->flags & IEEE80211_CHAN_DISABLED)
3917 continue;
3918
3919 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003920 i++;
3921 }
3922 }
3923 }
3924
Johannes Berg584991d2009-11-02 13:32:03 +01003925 if (!i) {
3926 err = -EINVAL;
3927 goto out_free;
3928 }
3929
3930 request->n_channels = i;
3931
Johannes Berg2a519312009-02-10 21:25:55 +01003932 i = 0;
3933 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3934 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003935 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003936 err = -EINVAL;
3937 goto out_free;
3938 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003939 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003940 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003941 i++;
3942 }
3943 }
3944
Jouni Malinen70692ad2009-02-16 19:39:13 +02003945 if (info->attrs[NL80211_ATTR_IE]) {
3946 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003947 memcpy((void *)request->ie,
3948 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003949 request->ie_len);
3950 }
3951
Johannes Berg34850ab2011-07-18 18:08:35 +02003952 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003953 if (wiphy->bands[i])
3954 request->rates[i] =
3955 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003956
3957 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3958 nla_for_each_nested(attr,
3959 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3960 tmp) {
3961 enum ieee80211_band band = nla_type(attr);
3962
Dan Carpenter84404622011-07-29 11:52:18 +03003963 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003964 err = -EINVAL;
3965 goto out_free;
3966 }
3967 err = ieee80211_get_ratemask(wiphy->bands[band],
3968 nla_data(attr),
3969 nla_len(attr),
3970 &request->rates[band]);
3971 if (err)
3972 goto out_free;
3973 }
3974 }
3975
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303976 request->no_cck =
3977 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3978
Johannes Berg463d0182009-07-14 00:33:35 +02003979 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003980 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003981
Johannes Berg79c97e92009-07-07 03:56:12 +02003982 rdev->scan_req = request;
3983 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003984
Johannes Berg463d0182009-07-14 00:33:35 +02003985 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003986 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003987 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003988 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003989 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003990 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003991 kfree(request);
3992 }
Johannes Berg3b858752009-03-12 09:55:09 +01003993
Johannes Berg2a519312009-02-10 21:25:55 +01003994 return err;
3995}
3996
Luciano Coelho807f8a82011-05-11 17:09:35 +03003997static int nl80211_start_sched_scan(struct sk_buff *skb,
3998 struct genl_info *info)
3999{
4000 struct cfg80211_sched_scan_request *request;
4001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4002 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004003 struct nlattr *attr;
4004 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004005 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004006 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004007 enum ieee80211_band band;
4008 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004009 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004010
4011 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4012 !rdev->ops->sched_scan_start)
4013 return -EOPNOTSUPP;
4014
4015 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4016 return -EINVAL;
4017
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004018 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4019 return -EINVAL;
4020
4021 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4022 if (interval == 0)
4023 return -EINVAL;
4024
Luciano Coelho807f8a82011-05-11 17:09:35 +03004025 wiphy = &rdev->wiphy;
4026
4027 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4028 n_channels = validate_scan_freqs(
4029 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4030 if (!n_channels)
4031 return -EINVAL;
4032 } else {
4033 n_channels = 0;
4034
4035 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4036 if (wiphy->bands[band])
4037 n_channels += wiphy->bands[band]->n_channels;
4038 }
4039
4040 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4041 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4042 tmp)
4043 n_ssids++;
4044
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004045 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004046 return -EINVAL;
4047
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004048 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4049 nla_for_each_nested(attr,
4050 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4051 tmp)
4052 n_match_sets++;
4053
4054 if (n_match_sets > wiphy->max_match_sets)
4055 return -EINVAL;
4056
Luciano Coelho807f8a82011-05-11 17:09:35 +03004057 if (info->attrs[NL80211_ATTR_IE])
4058 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4059 else
4060 ie_len = 0;
4061
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004062 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004063 return -EINVAL;
4064
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004065 mutex_lock(&rdev->sched_scan_mtx);
4066
4067 if (rdev->sched_scan_req) {
4068 err = -EINPROGRESS;
4069 goto out;
4070 }
4071
Luciano Coelho807f8a82011-05-11 17:09:35 +03004072 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004073 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004074 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004075 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004076 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004077 if (!request) {
4078 err = -ENOMEM;
4079 goto out;
4080 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004081
4082 if (n_ssids)
4083 request->ssids = (void *)&request->channels[n_channels];
4084 request->n_ssids = n_ssids;
4085 if (ie_len) {
4086 if (request->ssids)
4087 request->ie = (void *)(request->ssids + n_ssids);
4088 else
4089 request->ie = (void *)(request->channels + n_channels);
4090 }
4091
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004092 if (n_match_sets) {
4093 if (request->ie)
4094 request->match_sets = (void *)(request->ie + ie_len);
4095 else if (request->ssids)
4096 request->match_sets =
4097 (void *)(request->ssids + n_ssids);
4098 else
4099 request->match_sets =
4100 (void *)(request->channels + n_channels);
4101 }
4102 request->n_match_sets = n_match_sets;
4103
Luciano Coelho807f8a82011-05-11 17:09:35 +03004104 i = 0;
4105 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4106 /* user specified, bail out if channel not found */
4107 nla_for_each_nested(attr,
4108 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4109 tmp) {
4110 struct ieee80211_channel *chan;
4111
4112 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4113
4114 if (!chan) {
4115 err = -EINVAL;
4116 goto out_free;
4117 }
4118
4119 /* ignore disabled channels */
4120 if (chan->flags & IEEE80211_CHAN_DISABLED)
4121 continue;
4122
4123 request->channels[i] = chan;
4124 i++;
4125 }
4126 } else {
4127 /* all channels */
4128 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4129 int j;
4130 if (!wiphy->bands[band])
4131 continue;
4132 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4133 struct ieee80211_channel *chan;
4134
4135 chan = &wiphy->bands[band]->channels[j];
4136
4137 if (chan->flags & IEEE80211_CHAN_DISABLED)
4138 continue;
4139
4140 request->channels[i] = chan;
4141 i++;
4142 }
4143 }
4144 }
4145
4146 if (!i) {
4147 err = -EINVAL;
4148 goto out_free;
4149 }
4150
4151 request->n_channels = i;
4152
4153 i = 0;
4154 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4155 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4156 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004157 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004158 err = -EINVAL;
4159 goto out_free;
4160 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004161 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004162 memcpy(request->ssids[i].ssid, nla_data(attr),
4163 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004164 i++;
4165 }
4166 }
4167
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004168 i = 0;
4169 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4170 nla_for_each_nested(attr,
4171 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4172 tmp) {
4173 struct nlattr *ssid;
4174
4175 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4176 nla_data(attr), nla_len(attr),
4177 nl80211_match_policy);
4178 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4179 if (ssid) {
4180 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4181 err = -EINVAL;
4182 goto out_free;
4183 }
4184 memcpy(request->match_sets[i].ssid.ssid,
4185 nla_data(ssid), nla_len(ssid));
4186 request->match_sets[i].ssid.ssid_len =
4187 nla_len(ssid);
4188 }
4189 i++;
4190 }
4191 }
4192
Luciano Coelho807f8a82011-05-11 17:09:35 +03004193 if (info->attrs[NL80211_ATTR_IE]) {
4194 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4195 memcpy((void *)request->ie,
4196 nla_data(info->attrs[NL80211_ATTR_IE]),
4197 request->ie_len);
4198 }
4199
4200 request->dev = dev;
4201 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004202 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004203
4204 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4205 if (!err) {
4206 rdev->sched_scan_req = request;
4207 nl80211_send_sched_scan(rdev, dev,
4208 NL80211_CMD_START_SCHED_SCAN);
4209 goto out;
4210 }
4211
4212out_free:
4213 kfree(request);
4214out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004215 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004216 return err;
4217}
4218
4219static int nl80211_stop_sched_scan(struct sk_buff *skb,
4220 struct genl_info *info)
4221{
4222 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004223 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004224
4225 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4226 !rdev->ops->sched_scan_stop)
4227 return -EOPNOTSUPP;
4228
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004229 mutex_lock(&rdev->sched_scan_mtx);
4230 err = __cfg80211_stop_sched_scan(rdev, false);
4231 mutex_unlock(&rdev->sched_scan_mtx);
4232
4233 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004234}
4235
Johannes Berg9720bb32011-06-21 09:45:33 +02004236static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4237 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004238 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004239 struct wireless_dev *wdev,
4240 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004241{
Johannes Berg48ab9052009-07-10 18:42:31 +02004242 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004243 void *hdr;
4244 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004245
4246 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004247
Johannes Berg9720bb32011-06-21 09:45:33 +02004248 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004249 NL80211_CMD_NEW_SCAN_RESULTS);
4250 if (!hdr)
4251 return -1;
4252
Johannes Berg9720bb32011-06-21 09:45:33 +02004253 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4254
David S. Miller9360ffd2012-03-29 04:41:26 -04004255 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4256 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4257 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004258
4259 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4260 if (!bss)
4261 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004262 if ((!is_zero_ether_addr(res->bssid) &&
4263 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4264 (res->information_elements && res->len_information_elements &&
4265 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4266 res->len_information_elements,
4267 res->information_elements)) ||
4268 (res->beacon_ies && res->len_beacon_ies &&
4269 res->beacon_ies != res->information_elements &&
4270 nla_put(msg, NL80211_BSS_BEACON_IES,
4271 res->len_beacon_ies, res->beacon_ies)))
4272 goto nla_put_failure;
4273 if (res->tsf &&
4274 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4275 goto nla_put_failure;
4276 if (res->beacon_interval &&
4277 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4278 goto nla_put_failure;
4279 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4280 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4281 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4282 jiffies_to_msecs(jiffies - intbss->ts)))
4283 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004284
Johannes Berg77965c92009-02-18 18:45:06 +01004285 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004286 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004287 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4288 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004289 break;
4290 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004291 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4292 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004293 break;
4294 default:
4295 break;
4296 }
4297
Johannes Berg48ab9052009-07-10 18:42:31 +02004298 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004299 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004300 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004301 if (intbss == wdev->current_bss &&
4302 nla_put_u32(msg, NL80211_BSS_STATUS,
4303 NL80211_BSS_STATUS_ASSOCIATED))
4304 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004305 break;
4306 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004307 if (intbss == wdev->current_bss &&
4308 nla_put_u32(msg, NL80211_BSS_STATUS,
4309 NL80211_BSS_STATUS_IBSS_JOINED))
4310 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004311 break;
4312 default:
4313 break;
4314 }
4315
Johannes Berg2a519312009-02-10 21:25:55 +01004316 nla_nest_end(msg, bss);
4317
4318 return genlmsg_end(msg, hdr);
4319
4320 nla_put_failure:
4321 genlmsg_cancel(msg, hdr);
4322 return -EMSGSIZE;
4323}
4324
4325static int nl80211_dump_scan(struct sk_buff *skb,
4326 struct netlink_callback *cb)
4327{
Johannes Berg48ab9052009-07-10 18:42:31 +02004328 struct cfg80211_registered_device *rdev;
4329 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004330 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004331 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004332 int start = cb->args[1], idx = 0;
4333 int err;
4334
Johannes Berg67748892010-10-04 21:14:06 +02004335 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4336 if (err)
4337 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004338
Johannes Berg48ab9052009-07-10 18:42:31 +02004339 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004340
Johannes Berg48ab9052009-07-10 18:42:31 +02004341 wdev_lock(wdev);
4342 spin_lock_bh(&rdev->bss_lock);
4343 cfg80211_bss_expire(rdev);
4344
Johannes Berg9720bb32011-06-21 09:45:33 +02004345 cb->seq = rdev->bss_generation;
4346
Johannes Berg48ab9052009-07-10 18:42:31 +02004347 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004348 if (++idx <= start)
4349 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004350 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004351 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004352 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004353 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004354 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004355 }
4356 }
4357
Johannes Berg48ab9052009-07-10 18:42:31 +02004358 spin_unlock_bh(&rdev->bss_lock);
4359 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004360
4361 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004362 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004363
Johannes Berg67748892010-10-04 21:14:06 +02004364 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004365}
4366
Holger Schurig61fa7132009-11-11 12:25:40 +01004367static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4368 int flags, struct net_device *dev,
4369 struct survey_info *survey)
4370{
4371 void *hdr;
4372 struct nlattr *infoattr;
4373
Holger Schurig61fa7132009-11-11 12:25:40 +01004374 hdr = nl80211hdr_put(msg, pid, seq, flags,
4375 NL80211_CMD_NEW_SURVEY_RESULTS);
4376 if (!hdr)
4377 return -ENOMEM;
4378
David S. Miller9360ffd2012-03-29 04:41:26 -04004379 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4380 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004381
4382 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4383 if (!infoattr)
4384 goto nla_put_failure;
4385
David S. Miller9360ffd2012-03-29 04:41:26 -04004386 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4387 survey->channel->center_freq))
4388 goto nla_put_failure;
4389
4390 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4391 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4392 goto nla_put_failure;
4393 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4394 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4395 goto nla_put_failure;
4396 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4397 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4398 survey->channel_time))
4399 goto nla_put_failure;
4400 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4401 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4402 survey->channel_time_busy))
4403 goto nla_put_failure;
4404 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4405 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4406 survey->channel_time_ext_busy))
4407 goto nla_put_failure;
4408 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4409 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4410 survey->channel_time_rx))
4411 goto nla_put_failure;
4412 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4413 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4414 survey->channel_time_tx))
4415 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004416
4417 nla_nest_end(msg, infoattr);
4418
4419 return genlmsg_end(msg, hdr);
4420
4421 nla_put_failure:
4422 genlmsg_cancel(msg, hdr);
4423 return -EMSGSIZE;
4424}
4425
4426static int nl80211_dump_survey(struct sk_buff *skb,
4427 struct netlink_callback *cb)
4428{
4429 struct survey_info survey;
4430 struct cfg80211_registered_device *dev;
4431 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004432 int survey_idx = cb->args[1];
4433 int res;
4434
Johannes Berg67748892010-10-04 21:14:06 +02004435 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4436 if (res)
4437 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004438
4439 if (!dev->ops->dump_survey) {
4440 res = -EOPNOTSUPP;
4441 goto out_err;
4442 }
4443
4444 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004445 struct ieee80211_channel *chan;
4446
Holger Schurig61fa7132009-11-11 12:25:40 +01004447 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4448 &survey);
4449 if (res == -ENOENT)
4450 break;
4451 if (res)
4452 goto out_err;
4453
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004454 /* Survey without a channel doesn't make sense */
4455 if (!survey.channel) {
4456 res = -EINVAL;
4457 goto out;
4458 }
4459
4460 chan = ieee80211_get_channel(&dev->wiphy,
4461 survey.channel->center_freq);
4462 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4463 survey_idx++;
4464 continue;
4465 }
4466
Holger Schurig61fa7132009-11-11 12:25:40 +01004467 if (nl80211_send_survey(skb,
4468 NETLINK_CB(cb->skb).pid,
4469 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4470 netdev,
4471 &survey) < 0)
4472 goto out;
4473 survey_idx++;
4474 }
4475
4476 out:
4477 cb->args[1] = survey_idx;
4478 res = skb->len;
4479 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004480 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004481 return res;
4482}
4483
Jouni Malinen255e7372009-03-20 21:21:17 +02004484static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4485{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004486 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004487}
4488
Samuel Ortizb23aa672009-07-01 21:26:54 +02004489static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4490{
4491 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4492 NL80211_WPA_VERSION_2));
4493}
4494
Jouni Malinen636a5d32009-03-19 13:39:22 +02004495static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4496{
Johannes Berg4c476992010-10-04 21:36:35 +02004497 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4498 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004499 struct ieee80211_channel *chan;
4500 const u8 *bssid, *ssid, *ie = NULL;
4501 int err, ssid_len, ie_len = 0;
4502 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004503 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004504 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004505
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004506 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4507 return -EINVAL;
4508
4509 if (!info->attrs[NL80211_ATTR_MAC])
4510 return -EINVAL;
4511
Jouni Malinen17780922009-03-27 20:52:47 +02004512 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4513 return -EINVAL;
4514
Johannes Berg19957bb2009-07-02 17:20:43 +02004515 if (!info->attrs[NL80211_ATTR_SSID])
4516 return -EINVAL;
4517
4518 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4519 return -EINVAL;
4520
Johannes Bergfffd0932009-07-08 14:22:54 +02004521 err = nl80211_parse_key(info, &key);
4522 if (err)
4523 return err;
4524
4525 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004526 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4527 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004528 if (!key.p.key || !key.p.key_len)
4529 return -EINVAL;
4530 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4531 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4532 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4533 key.p.key_len != WLAN_KEY_LEN_WEP104))
4534 return -EINVAL;
4535 if (key.idx > 4)
4536 return -EINVAL;
4537 } else {
4538 key.p.key_len = 0;
4539 key.p.key = NULL;
4540 }
4541
Johannes Bergafea0b72010-08-10 09:46:42 +02004542 if (key.idx >= 0) {
4543 int i;
4544 bool ok = false;
4545 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4546 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4547 ok = true;
4548 break;
4549 }
4550 }
Johannes Berg4c476992010-10-04 21:36:35 +02004551 if (!ok)
4552 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004553 }
4554
Johannes Berg4c476992010-10-04 21:36:35 +02004555 if (!rdev->ops->auth)
4556 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004557
Johannes Berg074ac8d2010-09-16 14:58:22 +02004558 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004559 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4560 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004561
Johannes Berg19957bb2009-07-02 17:20:43 +02004562 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004563 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004564 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004565 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4566 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004567
Johannes Berg19957bb2009-07-02 17:20:43 +02004568 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4569 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4570
4571 if (info->attrs[NL80211_ATTR_IE]) {
4572 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4573 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4574 }
4575
4576 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004577 if (!nl80211_valid_auth_type(auth_type))
4578 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004579
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004580 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4581
Johannes Berg95de8172012-01-20 13:55:25 +01004582 /*
4583 * Since we no longer track auth state, ignore
4584 * requests to only change local state.
4585 */
4586 if (local_state_change)
4587 return 0;
4588
Johannes Berg4c476992010-10-04 21:36:35 +02004589 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4590 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004591 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004592}
4593
Johannes Bergc0692b82010-08-27 14:26:53 +03004594static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4595 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004596 struct cfg80211_crypto_settings *settings,
4597 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004598{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004599 memset(settings, 0, sizeof(*settings));
4600
Samuel Ortizb23aa672009-07-01 21:26:54 +02004601 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4602
Johannes Bergc0692b82010-08-27 14:26:53 +03004603 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4604 u16 proto;
4605 proto = nla_get_u16(
4606 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4607 settings->control_port_ethertype = cpu_to_be16(proto);
4608 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4609 proto != ETH_P_PAE)
4610 return -EINVAL;
4611 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4612 settings->control_port_no_encrypt = true;
4613 } else
4614 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4615
Samuel Ortizb23aa672009-07-01 21:26:54 +02004616 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4617 void *data;
4618 int len, i;
4619
4620 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4621 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4622 settings->n_ciphers_pairwise = len / sizeof(u32);
4623
4624 if (len % sizeof(u32))
4625 return -EINVAL;
4626
Johannes Berg3dc27d22009-07-02 21:36:37 +02004627 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004628 return -EINVAL;
4629
4630 memcpy(settings->ciphers_pairwise, data, len);
4631
4632 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004633 if (!cfg80211_supported_cipher_suite(
4634 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004635 settings->ciphers_pairwise[i]))
4636 return -EINVAL;
4637 }
4638
4639 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4640 settings->cipher_group =
4641 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004642 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4643 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004644 return -EINVAL;
4645 }
4646
4647 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4648 settings->wpa_versions =
4649 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4650 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4651 return -EINVAL;
4652 }
4653
4654 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4655 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004656 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004657
4658 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4659 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4660 settings->n_akm_suites = len / sizeof(u32);
4661
4662 if (len % sizeof(u32))
4663 return -EINVAL;
4664
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004665 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4666 return -EINVAL;
4667
Samuel Ortizb23aa672009-07-01 21:26:54 +02004668 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004669 }
4670
4671 return 0;
4672}
4673
Jouni Malinen636a5d32009-03-19 13:39:22 +02004674static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4675{
Johannes Berg4c476992010-10-04 21:36:35 +02004676 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4677 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004678 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004679 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004680 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004681 int err, ssid_len, ie_len = 0;
4682 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004683 u32 flags = 0;
4684 struct ieee80211_ht_cap *ht_capa = NULL;
4685 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004686
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004687 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4688 return -EINVAL;
4689
4690 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004691 !info->attrs[NL80211_ATTR_SSID] ||
4692 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004693 return -EINVAL;
4694
Johannes Berg4c476992010-10-04 21:36:35 +02004695 if (!rdev->ops->assoc)
4696 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004697
Johannes Berg074ac8d2010-09-16 14:58:22 +02004698 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004699 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4700 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004701
Johannes Berg19957bb2009-07-02 17:20:43 +02004702 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004703
Johannes Berg19957bb2009-07-02 17:20:43 +02004704 chan = ieee80211_get_channel(&rdev->wiphy,
4705 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004706 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4707 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004708
Johannes Berg19957bb2009-07-02 17:20:43 +02004709 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4710 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004711
4712 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004713 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4714 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004715 }
4716
Jouni Malinendc6382c2009-05-06 22:09:37 +03004717 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004718 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004719 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004720 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004721 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004722 else if (mfp != NL80211_MFP_NO)
4723 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004724 }
4725
Johannes Berg3e5d7642009-07-07 14:37:26 +02004726 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4727 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4728
Ben Greear7e7c8922011-11-18 11:31:59 -08004729 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4730 flags |= ASSOC_REQ_DISABLE_HT;
4731
4732 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4733 ht_capa_mask =
4734 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4735
4736 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4737 if (!ht_capa_mask)
4738 return -EINVAL;
4739 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4740 }
4741
Johannes Bergc0692b82010-08-27 14:26:53 +03004742 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004743 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004744 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4745 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004746 &crypto, flags, ht_capa,
4747 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004748
Jouni Malinen636a5d32009-03-19 13:39:22 +02004749 return err;
4750}
4751
4752static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4753{
Johannes Berg4c476992010-10-04 21:36:35 +02004754 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4755 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004756 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004757 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004758 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004759 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004760
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004761 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4762 return -EINVAL;
4763
4764 if (!info->attrs[NL80211_ATTR_MAC])
4765 return -EINVAL;
4766
4767 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4768 return -EINVAL;
4769
Johannes Berg4c476992010-10-04 21:36:35 +02004770 if (!rdev->ops->deauth)
4771 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004772
Johannes Berg074ac8d2010-09-16 14:58:22 +02004773 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004774 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4775 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004776
Johannes Berg19957bb2009-07-02 17:20:43 +02004777 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004778
Johannes Berg19957bb2009-07-02 17:20:43 +02004779 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4780 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004781 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004782 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004783 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004784
4785 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004786 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4787 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004788 }
4789
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004790 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4791
Johannes Berg4c476992010-10-04 21:36:35 +02004792 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4793 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004794}
4795
4796static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4797{
Johannes Berg4c476992010-10-04 21:36:35 +02004798 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4799 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004800 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004801 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004802 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004803 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004804
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004805 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4806 return -EINVAL;
4807
4808 if (!info->attrs[NL80211_ATTR_MAC])
4809 return -EINVAL;
4810
4811 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4812 return -EINVAL;
4813
Johannes Berg4c476992010-10-04 21:36:35 +02004814 if (!rdev->ops->disassoc)
4815 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004816
Johannes Berg074ac8d2010-09-16 14:58:22 +02004817 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004818 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4819 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004820
Johannes Berg19957bb2009-07-02 17:20:43 +02004821 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004822
Johannes Berg19957bb2009-07-02 17:20:43 +02004823 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4824 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004825 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004826 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004827 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004828
4829 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004830 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4831 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004832 }
4833
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004834 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4835
Johannes Berg4c476992010-10-04 21:36:35 +02004836 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4837 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004838}
4839
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004840static bool
4841nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4842 int mcast_rate[IEEE80211_NUM_BANDS],
4843 int rateval)
4844{
4845 struct wiphy *wiphy = &rdev->wiphy;
4846 bool found = false;
4847 int band, i;
4848
4849 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4850 struct ieee80211_supported_band *sband;
4851
4852 sband = wiphy->bands[band];
4853 if (!sband)
4854 continue;
4855
4856 for (i = 0; i < sband->n_bitrates; i++) {
4857 if (sband->bitrates[i].bitrate == rateval) {
4858 mcast_rate[band] = i + 1;
4859 found = true;
4860 break;
4861 }
4862 }
4863 }
4864
4865 return found;
4866}
4867
Johannes Berg04a773a2009-04-19 21:24:32 +02004868static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4869{
Johannes Berg4c476992010-10-04 21:36:35 +02004870 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4871 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004872 struct cfg80211_ibss_params ibss;
4873 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004874 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004875 int err;
4876
Johannes Berg8e30bc52009-04-22 17:45:38 +02004877 memset(&ibss, 0, sizeof(ibss));
4878
Johannes Berg04a773a2009-04-19 21:24:32 +02004879 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4880 return -EINVAL;
4881
4882 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4883 !info->attrs[NL80211_ATTR_SSID] ||
4884 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4885 return -EINVAL;
4886
Johannes Berg8e30bc52009-04-22 17:45:38 +02004887 ibss.beacon_interval = 100;
4888
4889 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4890 ibss.beacon_interval =
4891 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4892 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4893 return -EINVAL;
4894 }
4895
Johannes Berg4c476992010-10-04 21:36:35 +02004896 if (!rdev->ops->join_ibss)
4897 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004898
Johannes Berg4c476992010-10-04 21:36:35 +02004899 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4900 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004901
Johannes Berg79c97e92009-07-07 03:56:12 +02004902 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004903
Johannes Berg39193492011-09-16 13:45:25 +02004904 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004905 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004906
4907 if (!is_valid_ether_addr(ibss.bssid))
4908 return -EINVAL;
4909 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004910 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4911 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4912
4913 if (info->attrs[NL80211_ATTR_IE]) {
4914 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4915 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4916 }
4917
Alexander Simon54858ee2011-11-30 16:56:32 +01004918 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4919 enum nl80211_channel_type channel_type;
4920
4921 channel_type = nla_get_u32(
4922 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4923 if (channel_type != NL80211_CHAN_NO_HT &&
4924 channel_type != NL80211_CHAN_HT20 &&
4925 channel_type != NL80211_CHAN_HT40MINUS &&
4926 channel_type != NL80211_CHAN_HT40PLUS)
4927 return -EINVAL;
4928
4929 if (channel_type != NL80211_CHAN_NO_HT &&
4930 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4931 return -EINVAL;
4932
4933 ibss.channel_type = channel_type;
4934 } else {
4935 ibss.channel_type = NL80211_CHAN_NO_HT;
4936 }
4937
4938 ibss.channel = rdev_freq_to_chan(rdev,
4939 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4940 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004941 if (!ibss.channel ||
4942 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004943 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4944 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004945
Alexander Simon54858ee2011-11-30 16:56:32 +01004946 /* Both channels should be able to initiate communication */
4947 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4948 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4949 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4950 ibss.channel_type))
4951 return -EINVAL;
4952
Johannes Berg04a773a2009-04-19 21:24:32 +02004953 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004954 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004955
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004956 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4957 u8 *rates =
4958 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4959 int n_rates =
4960 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4961 struct ieee80211_supported_band *sband =
4962 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004963
Johannes Berg34850ab2011-07-18 18:08:35 +02004964 err = ieee80211_get_ratemask(sband, rates, n_rates,
4965 &ibss.basic_rates);
4966 if (err)
4967 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004968 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004969
4970 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4971 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4972 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4973 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004974
Johannes Berg4c476992010-10-04 21:36:35 +02004975 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4976 connkeys = nl80211_parse_connkeys(rdev,
4977 info->attrs[NL80211_ATTR_KEYS]);
4978 if (IS_ERR(connkeys))
4979 return PTR_ERR(connkeys);
4980 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004981
Antonio Quartulli267335d2012-01-31 20:25:47 +01004982 ibss.control_port =
4983 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4984
Johannes Berg4c476992010-10-04 21:36:35 +02004985 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004986 if (err)
4987 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004988 return err;
4989}
4990
4991static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4992{
Johannes Berg4c476992010-10-04 21:36:35 +02004993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4994 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 if (!rdev->ops->leave_ibss)
4997 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004998
Johannes Berg4c476992010-10-04 21:36:35 +02004999 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5000 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005001
Johannes Berg4c476992010-10-04 21:36:35 +02005002 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005003}
5004
Johannes Bergaff89a92009-07-01 21:26:51 +02005005#ifdef CONFIG_NL80211_TESTMODE
5006static struct genl_multicast_group nl80211_testmode_mcgrp = {
5007 .name = "testmode",
5008};
5009
5010static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5011{
Johannes Berg4c476992010-10-04 21:36:35 +02005012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005013 int err;
5014
5015 if (!info->attrs[NL80211_ATTR_TESTDATA])
5016 return -EINVAL;
5017
Johannes Bergaff89a92009-07-01 21:26:51 +02005018 err = -EOPNOTSUPP;
5019 if (rdev->ops->testmode_cmd) {
5020 rdev->testmode_info = info;
5021 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5022 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5023 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5024 rdev->testmode_info = NULL;
5025 }
5026
Johannes Bergaff89a92009-07-01 21:26:51 +02005027 return err;
5028}
5029
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005030static int nl80211_testmode_dump(struct sk_buff *skb,
5031 struct netlink_callback *cb)
5032{
Johannes Berg00918d32011-12-13 17:22:05 +01005033 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005034 int err;
5035 long phy_idx;
5036 void *data = NULL;
5037 int data_len = 0;
5038
5039 if (cb->args[0]) {
5040 /*
5041 * 0 is a valid index, but not valid for args[0],
5042 * so we need to offset by 1.
5043 */
5044 phy_idx = cb->args[0] - 1;
5045 } else {
5046 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5047 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5048 nl80211_policy);
5049 if (err)
5050 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005051 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5052 phy_idx = nla_get_u32(
5053 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5054 } else {
5055 struct net_device *netdev;
5056
5057 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5058 nl80211_fam.attrbuf,
5059 &rdev, &netdev);
5060 if (err)
5061 return err;
5062 dev_put(netdev);
5063 phy_idx = rdev->wiphy_idx;
5064 cfg80211_unlock_rdev(rdev);
5065 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005066 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5067 cb->args[1] =
5068 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5069 }
5070
5071 if (cb->args[1]) {
5072 data = nla_data((void *)cb->args[1]);
5073 data_len = nla_len((void *)cb->args[1]);
5074 }
5075
5076 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005077 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5078 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005079 mutex_unlock(&cfg80211_mutex);
5080 return -ENOENT;
5081 }
Johannes Berg00918d32011-12-13 17:22:05 +01005082 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005083 mutex_unlock(&cfg80211_mutex);
5084
Johannes Berg00918d32011-12-13 17:22:05 +01005085 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005086 err = -EOPNOTSUPP;
5087 goto out_err;
5088 }
5089
5090 while (1) {
5091 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5092 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5093 NL80211_CMD_TESTMODE);
5094 struct nlattr *tmdata;
5095
David S. Miller9360ffd2012-03-29 04:41:26 -04005096 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005097 genlmsg_cancel(skb, hdr);
5098 break;
5099 }
5100
5101 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5102 if (!tmdata) {
5103 genlmsg_cancel(skb, hdr);
5104 break;
5105 }
Johannes Berg00918d32011-12-13 17:22:05 +01005106 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5107 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005108 nla_nest_end(skb, tmdata);
5109
5110 if (err == -ENOBUFS || err == -ENOENT) {
5111 genlmsg_cancel(skb, hdr);
5112 break;
5113 } else if (err) {
5114 genlmsg_cancel(skb, hdr);
5115 goto out_err;
5116 }
5117
5118 genlmsg_end(skb, hdr);
5119 }
5120
5121 err = skb->len;
5122 /* see above */
5123 cb->args[0] = phy_idx + 1;
5124 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005125 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005126 return err;
5127}
5128
Johannes Bergaff89a92009-07-01 21:26:51 +02005129static struct sk_buff *
5130__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5131 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5132{
5133 struct sk_buff *skb;
5134 void *hdr;
5135 struct nlattr *data;
5136
5137 skb = nlmsg_new(approxlen + 100, gfp);
5138 if (!skb)
5139 return NULL;
5140
5141 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5142 if (!hdr) {
5143 kfree_skb(skb);
5144 return NULL;
5145 }
5146
David S. Miller9360ffd2012-03-29 04:41:26 -04005147 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5148 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005149 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5150
5151 ((void **)skb->cb)[0] = rdev;
5152 ((void **)skb->cb)[1] = hdr;
5153 ((void **)skb->cb)[2] = data;
5154
5155 return skb;
5156
5157 nla_put_failure:
5158 kfree_skb(skb);
5159 return NULL;
5160}
5161
5162struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5163 int approxlen)
5164{
5165 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5166
5167 if (WARN_ON(!rdev->testmode_info))
5168 return NULL;
5169
5170 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5171 rdev->testmode_info->snd_pid,
5172 rdev->testmode_info->snd_seq,
5173 GFP_KERNEL);
5174}
5175EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5176
5177int cfg80211_testmode_reply(struct sk_buff *skb)
5178{
5179 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5180 void *hdr = ((void **)skb->cb)[1];
5181 struct nlattr *data = ((void **)skb->cb)[2];
5182
5183 if (WARN_ON(!rdev->testmode_info)) {
5184 kfree_skb(skb);
5185 return -EINVAL;
5186 }
5187
5188 nla_nest_end(skb, data);
5189 genlmsg_end(skb, hdr);
5190 return genlmsg_reply(skb, rdev->testmode_info);
5191}
5192EXPORT_SYMBOL(cfg80211_testmode_reply);
5193
5194struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5195 int approxlen, gfp_t gfp)
5196{
5197 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5198
5199 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5200}
5201EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5202
5203void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5204{
5205 void *hdr = ((void **)skb->cb)[1];
5206 struct nlattr *data = ((void **)skb->cb)[2];
5207
5208 nla_nest_end(skb, data);
5209 genlmsg_end(skb, hdr);
5210 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5211}
5212EXPORT_SYMBOL(cfg80211_testmode_event);
5213#endif
5214
Samuel Ortizb23aa672009-07-01 21:26:54 +02005215static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5216{
Johannes Berg4c476992010-10-04 21:36:35 +02005217 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5218 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005219 struct cfg80211_connect_params connect;
5220 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005221 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005222 int err;
5223
5224 memset(&connect, 0, sizeof(connect));
5225
5226 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5227 return -EINVAL;
5228
5229 if (!info->attrs[NL80211_ATTR_SSID] ||
5230 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5231 return -EINVAL;
5232
5233 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5234 connect.auth_type =
5235 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5236 if (!nl80211_valid_auth_type(connect.auth_type))
5237 return -EINVAL;
5238 } else
5239 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5240
5241 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5242
Johannes Bergc0692b82010-08-27 14:26:53 +03005243 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005244 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005245 if (err)
5246 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005247
Johannes Berg074ac8d2010-09-16 14:58:22 +02005248 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005249 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5250 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005251
Johannes Berg79c97e92009-07-07 03:56:12 +02005252 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005253
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305254 connect.bg_scan_period = -1;
5255 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5256 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5257 connect.bg_scan_period =
5258 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5259 }
5260
Samuel Ortizb23aa672009-07-01 21:26:54 +02005261 if (info->attrs[NL80211_ATTR_MAC])
5262 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5263 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5264 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5265
5266 if (info->attrs[NL80211_ATTR_IE]) {
5267 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5268 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5269 }
5270
5271 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5272 connect.channel =
5273 ieee80211_get_channel(wiphy,
5274 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5275 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005276 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5277 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005278 }
5279
Johannes Bergfffd0932009-07-08 14:22:54 +02005280 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5281 connkeys = nl80211_parse_connkeys(rdev,
5282 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005283 if (IS_ERR(connkeys))
5284 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005285 }
5286
Ben Greear7e7c8922011-11-18 11:31:59 -08005287 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5288 connect.flags |= ASSOC_REQ_DISABLE_HT;
5289
5290 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5291 memcpy(&connect.ht_capa_mask,
5292 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5293 sizeof(connect.ht_capa_mask));
5294
5295 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5296 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5297 return -EINVAL;
5298 memcpy(&connect.ht_capa,
5299 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5300 sizeof(connect.ht_capa));
5301 }
5302
Johannes Bergfffd0932009-07-08 14:22:54 +02005303 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005304 if (err)
5305 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005306 return err;
5307}
5308
5309static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5310{
Johannes Berg4c476992010-10-04 21:36:35 +02005311 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5312 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005313 u16 reason;
5314
5315 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5316 reason = WLAN_REASON_DEAUTH_LEAVING;
5317 else
5318 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5319
5320 if (reason == 0)
5321 return -EINVAL;
5322
Johannes Berg074ac8d2010-09-16 14:58:22 +02005323 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005324 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5325 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005326
Johannes Berg4c476992010-10-04 21:36:35 +02005327 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005328}
5329
Johannes Berg463d0182009-07-14 00:33:35 +02005330static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5331{
Johannes Berg4c476992010-10-04 21:36:35 +02005332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005333 struct net *net;
5334 int err;
5335 u32 pid;
5336
5337 if (!info->attrs[NL80211_ATTR_PID])
5338 return -EINVAL;
5339
5340 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5341
Johannes Berg463d0182009-07-14 00:33:35 +02005342 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005343 if (IS_ERR(net))
5344 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005345
5346 err = 0;
5347
5348 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005349 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5350 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005351
Johannes Berg463d0182009-07-14 00:33:35 +02005352 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005353 return err;
5354}
5355
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005356static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5357{
Johannes Berg4c476992010-10-04 21:36:35 +02005358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005359 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5360 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005361 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005362 struct cfg80211_pmksa pmksa;
5363
5364 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5365
5366 if (!info->attrs[NL80211_ATTR_MAC])
5367 return -EINVAL;
5368
5369 if (!info->attrs[NL80211_ATTR_PMKID])
5370 return -EINVAL;
5371
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005372 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5373 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5374
Johannes Berg074ac8d2010-09-16 14:58:22 +02005375 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005376 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5377 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005378
5379 switch (info->genlhdr->cmd) {
5380 case NL80211_CMD_SET_PMKSA:
5381 rdev_ops = rdev->ops->set_pmksa;
5382 break;
5383 case NL80211_CMD_DEL_PMKSA:
5384 rdev_ops = rdev->ops->del_pmksa;
5385 break;
5386 default:
5387 WARN_ON(1);
5388 break;
5389 }
5390
Johannes Berg4c476992010-10-04 21:36:35 +02005391 if (!rdev_ops)
5392 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005393
Johannes Berg4c476992010-10-04 21:36:35 +02005394 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005395}
5396
5397static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5398{
Johannes Berg4c476992010-10-04 21:36:35 +02005399 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5400 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005401
Johannes Berg074ac8d2010-09-16 14:58:22 +02005402 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005403 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5404 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005405
Johannes Berg4c476992010-10-04 21:36:35 +02005406 if (!rdev->ops->flush_pmksa)
5407 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005408
Johannes Berg4c476992010-10-04 21:36:35 +02005409 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005410}
5411
Arik Nemtsov109086c2011-09-28 14:12:50 +03005412static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5413{
5414 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5415 struct net_device *dev = info->user_ptr[1];
5416 u8 action_code, dialog_token;
5417 u16 status_code;
5418 u8 *peer;
5419
5420 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5421 !rdev->ops->tdls_mgmt)
5422 return -EOPNOTSUPP;
5423
5424 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5425 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5426 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5427 !info->attrs[NL80211_ATTR_IE] ||
5428 !info->attrs[NL80211_ATTR_MAC])
5429 return -EINVAL;
5430
5431 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5432 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5433 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5434 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5435
5436 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5437 dialog_token, status_code,
5438 nla_data(info->attrs[NL80211_ATTR_IE]),
5439 nla_len(info->attrs[NL80211_ATTR_IE]));
5440}
5441
5442static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5443{
5444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5445 struct net_device *dev = info->user_ptr[1];
5446 enum nl80211_tdls_operation operation;
5447 u8 *peer;
5448
5449 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5450 !rdev->ops->tdls_oper)
5451 return -EOPNOTSUPP;
5452
5453 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5454 !info->attrs[NL80211_ATTR_MAC])
5455 return -EINVAL;
5456
5457 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5458 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5459
5460 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5461}
5462
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005463static int nl80211_remain_on_channel(struct sk_buff *skb,
5464 struct genl_info *info)
5465{
Johannes Berg4c476992010-10-04 21:36:35 +02005466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5467 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005468 struct ieee80211_channel *chan;
5469 struct sk_buff *msg;
5470 void *hdr;
5471 u64 cookie;
5472 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5473 u32 freq, duration;
5474 int err;
5475
5476 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5477 !info->attrs[NL80211_ATTR_DURATION])
5478 return -EINVAL;
5479
5480 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5481
5482 /*
5483 * We should be on that channel for at least one jiffie,
5484 * and more than 5 seconds seems excessive.
5485 */
Johannes Berga2939112010-12-14 17:54:28 +01005486 if (!duration || !msecs_to_jiffies(duration) ||
5487 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005488 return -EINVAL;
5489
Johannes Berg7c4ef712011-11-18 15:33:48 +01005490 if (!rdev->ops->remain_on_channel ||
5491 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005492 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005493
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005494 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5495 channel_type = nla_get_u32(
5496 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5497 if (channel_type != NL80211_CHAN_NO_HT &&
5498 channel_type != NL80211_CHAN_HT20 &&
5499 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005500 channel_type != NL80211_CHAN_HT40MINUS)
5501 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005502 }
5503
5504 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5505 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005506 if (chan == NULL)
5507 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005508
5509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005510 if (!msg)
5511 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005512
5513 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5514 NL80211_CMD_REMAIN_ON_CHANNEL);
5515
5516 if (IS_ERR(hdr)) {
5517 err = PTR_ERR(hdr);
5518 goto free_msg;
5519 }
5520
5521 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5522 channel_type, duration, &cookie);
5523
5524 if (err)
5525 goto free_msg;
5526
David S. Miller9360ffd2012-03-29 04:41:26 -04005527 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5528 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005529
5530 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005531
5532 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005533
5534 nla_put_failure:
5535 err = -ENOBUFS;
5536 free_msg:
5537 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005538 return err;
5539}
5540
5541static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5542 struct genl_info *info)
5543{
Johannes Berg4c476992010-10-04 21:36:35 +02005544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5545 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005546 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005547
5548 if (!info->attrs[NL80211_ATTR_COOKIE])
5549 return -EINVAL;
5550
Johannes Berg4c476992010-10-04 21:36:35 +02005551 if (!rdev->ops->cancel_remain_on_channel)
5552 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005553
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005554 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5555
Johannes Berg4c476992010-10-04 21:36:35 +02005556 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005557}
5558
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005559static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5560 u8 *rates, u8 rates_len)
5561{
5562 u8 i;
5563 u32 mask = 0;
5564
5565 for (i = 0; i < rates_len; i++) {
5566 int rate = (rates[i] & 0x7f) * 5;
5567 int ridx;
5568 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5569 struct ieee80211_rate *srate =
5570 &sband->bitrates[ridx];
5571 if (rate == srate->bitrate) {
5572 mask |= 1 << ridx;
5573 break;
5574 }
5575 }
5576 if (ridx == sband->n_bitrates)
5577 return 0; /* rate not found */
5578 }
5579
5580 return mask;
5581}
5582
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005583static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5584 u8 *rates, u8 rates_len,
5585 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5586{
5587 u8 i;
5588
5589 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5590
5591 for (i = 0; i < rates_len; i++) {
5592 int ridx, rbit;
5593
5594 ridx = rates[i] / 8;
5595 rbit = BIT(rates[i] % 8);
5596
5597 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005598 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005599 return false;
5600
5601 /* check availability */
5602 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5603 mcs[ridx] |= rbit;
5604 else
5605 return false;
5606 }
5607
5608 return true;
5609}
5610
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005611static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005612 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5613 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005614 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5615 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005616};
5617
5618static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5619 struct genl_info *info)
5620{
5621 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005623 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005624 int rem, i;
5625 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005626 struct nlattr *tx_rates;
5627 struct ieee80211_supported_band *sband;
5628
5629 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5630 return -EINVAL;
5631
Johannes Berg4c476992010-10-04 21:36:35 +02005632 if (!rdev->ops->set_bitrate_mask)
5633 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005634
5635 memset(&mask, 0, sizeof(mask));
5636 /* Default to all rates enabled */
5637 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5638 sband = rdev->wiphy.bands[i];
5639 mask.control[i].legacy =
5640 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005641 if (sband)
5642 memcpy(mask.control[i].mcs,
5643 sband->ht_cap.mcs.rx_mask,
5644 sizeof(mask.control[i].mcs));
5645 else
5646 memset(mask.control[i].mcs, 0,
5647 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005648 }
5649
5650 /*
5651 * The nested attribute uses enum nl80211_band as the index. This maps
5652 * directly to the enum ieee80211_band values used in cfg80211.
5653 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005654 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005655 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5656 {
5657 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005658 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5659 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005660 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005661 if (sband == NULL)
5662 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005663 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5664 nla_len(tx_rates), nl80211_txattr_policy);
5665 if (tb[NL80211_TXRATE_LEGACY]) {
5666 mask.control[band].legacy = rateset_to_mask(
5667 sband,
5668 nla_data(tb[NL80211_TXRATE_LEGACY]),
5669 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305670 if ((mask.control[band].legacy == 0) &&
5671 nla_len(tb[NL80211_TXRATE_LEGACY]))
5672 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005673 }
5674 if (tb[NL80211_TXRATE_MCS]) {
5675 if (!ht_rateset_to_mask(
5676 sband,
5677 nla_data(tb[NL80211_TXRATE_MCS]),
5678 nla_len(tb[NL80211_TXRATE_MCS]),
5679 mask.control[band].mcs))
5680 return -EINVAL;
5681 }
5682
5683 if (mask.control[band].legacy == 0) {
5684 /* don't allow empty legacy rates if HT
5685 * is not even supported. */
5686 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5687 return -EINVAL;
5688
5689 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5690 if (mask.control[band].mcs[i])
5691 break;
5692
5693 /* legacy and mcs rates may not be both empty */
5694 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005695 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005696 }
5697 }
5698
Johannes Berg4c476992010-10-04 21:36:35 +02005699 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005700}
5701
Johannes Berg2e161f72010-08-12 15:38:38 +02005702static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005703{
Johannes Berg4c476992010-10-04 21:36:35 +02005704 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5705 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005706 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005707
5708 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5709 return -EINVAL;
5710
Johannes Berg2e161f72010-08-12 15:38:38 +02005711 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5712 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005713
Johannes Berg9d38d852010-06-09 17:20:33 +02005714 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005715 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005716 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5717 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5718 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005719 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005720 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5721 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005722
5723 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005724 if (!rdev->ops->mgmt_tx)
5725 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005726
Johannes Berg4c476992010-10-04 21:36:35 +02005727 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005728 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005729 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5730 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005731}
5732
Johannes Berg2e161f72010-08-12 15:38:38 +02005733static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005734{
Johannes Berg4c476992010-10-04 21:36:35 +02005735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5736 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005737 struct ieee80211_channel *chan;
5738 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005739 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005740 u32 freq;
5741 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005742 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005743 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005744 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005745 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005746 bool offchan, no_cck, dont_wait_for_ack;
5747
5748 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005749
5750 if (!info->attrs[NL80211_ATTR_FRAME] ||
5751 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5752 return -EINVAL;
5753
Johannes Berg4c476992010-10-04 21:36:35 +02005754 if (!rdev->ops->mgmt_tx)
5755 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005756
Johannes Berg9d38d852010-06-09 17:20:33 +02005757 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005758 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005759 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5760 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5761 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005762 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5764 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005765
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005766 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005767 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005768 return -EINVAL;
5769 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5770 }
5771
Jouni Malinen026331c2010-02-15 12:53:10 +02005772 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5773 channel_type = nla_get_u32(
5774 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5775 if (channel_type != NL80211_CHAN_NO_HT &&
5776 channel_type != NL80211_CHAN_HT20 &&
5777 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005778 channel_type != NL80211_CHAN_HT40MINUS)
5779 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005780 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005781 }
5782
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005783 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5784
Johannes Berg7c4ef712011-11-18 15:33:48 +01005785 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5786 return -EINVAL;
5787
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305788 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5789
Jouni Malinen026331c2010-02-15 12:53:10 +02005790 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5791 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005792 if (chan == NULL)
5793 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005794
Johannes Berge247bd902011-11-04 11:18:21 +01005795 if (!dont_wait_for_ack) {
5796 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5797 if (!msg)
5798 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005799
Johannes Berge247bd902011-11-04 11:18:21 +01005800 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5801 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005802
Johannes Berge247bd902011-11-04 11:18:21 +01005803 if (IS_ERR(hdr)) {
5804 err = PTR_ERR(hdr);
5805 goto free_msg;
5806 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005807 }
Johannes Berge247bd902011-11-04 11:18:21 +01005808
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005809 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5810 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005811 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5812 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005813 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005814 if (err)
5815 goto free_msg;
5816
Johannes Berge247bd902011-11-04 11:18:21 +01005817 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005818 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5819 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005820
Johannes Berge247bd902011-11-04 11:18:21 +01005821 genlmsg_end(msg, hdr);
5822 return genlmsg_reply(msg, info);
5823 }
5824
5825 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005826
5827 nla_put_failure:
5828 err = -ENOBUFS;
5829 free_msg:
5830 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005831 return err;
5832}
5833
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005834static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5835{
5836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5837 struct net_device *dev = info->user_ptr[1];
5838 u64 cookie;
5839
5840 if (!info->attrs[NL80211_ATTR_COOKIE])
5841 return -EINVAL;
5842
5843 if (!rdev->ops->mgmt_tx_cancel_wait)
5844 return -EOPNOTSUPP;
5845
5846 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5847 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5848 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5849 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5850 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5851 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5852 return -EOPNOTSUPP;
5853
5854 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5855
5856 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5857}
5858
Kalle Valoffb9eb32010-02-17 17:58:10 +02005859static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5860{
Johannes Berg4c476992010-10-04 21:36:35 +02005861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005862 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005863 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005864 u8 ps_state;
5865 bool state;
5866 int err;
5867
Johannes Berg4c476992010-10-04 21:36:35 +02005868 if (!info->attrs[NL80211_ATTR_PS_STATE])
5869 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005870
5871 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5872
Johannes Berg4c476992010-10-04 21:36:35 +02005873 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5874 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005875
5876 wdev = dev->ieee80211_ptr;
5877
Johannes Berg4c476992010-10-04 21:36:35 +02005878 if (!rdev->ops->set_power_mgmt)
5879 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005880
5881 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5882
5883 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005884 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005885
Johannes Berg4c476992010-10-04 21:36:35 +02005886 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5887 wdev->ps_timeout);
5888 if (!err)
5889 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005890 return err;
5891}
5892
5893static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5894{
Johannes Berg4c476992010-10-04 21:36:35 +02005895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005896 enum nl80211_ps_state ps_state;
5897 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005898 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005899 struct sk_buff *msg;
5900 void *hdr;
5901 int err;
5902
Kalle Valoffb9eb32010-02-17 17:58:10 +02005903 wdev = dev->ieee80211_ptr;
5904
Johannes Berg4c476992010-10-04 21:36:35 +02005905 if (!rdev->ops->set_power_mgmt)
5906 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005907
5908 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005909 if (!msg)
5910 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005911
5912 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5913 NL80211_CMD_GET_POWER_SAVE);
5914 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005915 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005916 goto free_msg;
5917 }
5918
5919 if (wdev->ps)
5920 ps_state = NL80211_PS_ENABLED;
5921 else
5922 ps_state = NL80211_PS_DISABLED;
5923
David S. Miller9360ffd2012-03-29 04:41:26 -04005924 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5925 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005926
5927 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005928 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005929
Johannes Berg4c476992010-10-04 21:36:35 +02005930 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005931 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005932 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005933 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005934 return err;
5935}
5936
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005937static struct nla_policy
5938nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5939 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5940 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5941 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5942};
5943
5944static int nl80211_set_cqm_rssi(struct genl_info *info,
5945 s32 threshold, u32 hysteresis)
5946{
Johannes Berg4c476992010-10-04 21:36:35 +02005947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005948 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005949 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005950
5951 if (threshold > 0)
5952 return -EINVAL;
5953
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005954 wdev = dev->ieee80211_ptr;
5955
Johannes Berg4c476992010-10-04 21:36:35 +02005956 if (!rdev->ops->set_cqm_rssi_config)
5957 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005958
Johannes Berg074ac8d2010-09-16 14:58:22 +02005959 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005960 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5961 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005962
Johannes Berg4c476992010-10-04 21:36:35 +02005963 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5964 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005965}
5966
5967static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5968{
5969 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5970 struct nlattr *cqm;
5971 int err;
5972
5973 cqm = info->attrs[NL80211_ATTR_CQM];
5974 if (!cqm) {
5975 err = -EINVAL;
5976 goto out;
5977 }
5978
5979 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5980 nl80211_attr_cqm_policy);
5981 if (err)
5982 goto out;
5983
5984 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5985 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5986 s32 threshold;
5987 u32 hysteresis;
5988 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5989 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5990 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5991 } else
5992 err = -EINVAL;
5993
5994out:
5995 return err;
5996}
5997
Johannes Berg29cbe682010-12-03 09:20:44 +01005998static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5999{
6000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6001 struct net_device *dev = info->user_ptr[1];
6002 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006003 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006004 int err;
6005
6006 /* start with default */
6007 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006008 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006009
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006010 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006011 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006012 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006013 if (err)
6014 return err;
6015 }
6016
6017 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6018 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6019 return -EINVAL;
6020
Javier Cardonac80d5452010-12-16 17:37:49 -08006021 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6022 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6023
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006024 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6025 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6026 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6027 return -EINVAL;
6028
Javier Cardonac80d5452010-12-16 17:37:49 -08006029 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6030 /* parse additional setup parameters if given */
6031 err = nl80211_parse_mesh_setup(info, &setup);
6032 if (err)
6033 return err;
6034 }
6035
6036 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006037}
6038
6039static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6040{
6041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6042 struct net_device *dev = info->user_ptr[1];
6043
6044 return cfg80211_leave_mesh(rdev, dev);
6045}
6046
Johannes Bergff1b6e62011-05-04 15:37:28 +02006047static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6048{
6049 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6050 struct sk_buff *msg;
6051 void *hdr;
6052
6053 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6054 return -EOPNOTSUPP;
6055
6056 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6057 if (!msg)
6058 return -ENOMEM;
6059
6060 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6061 NL80211_CMD_GET_WOWLAN);
6062 if (!hdr)
6063 goto nla_put_failure;
6064
6065 if (rdev->wowlan) {
6066 struct nlattr *nl_wowlan;
6067
6068 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6069 if (!nl_wowlan)
6070 goto nla_put_failure;
6071
David S. Miller9360ffd2012-03-29 04:41:26 -04006072 if ((rdev->wowlan->any &&
6073 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6074 (rdev->wowlan->disconnect &&
6075 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6076 (rdev->wowlan->magic_pkt &&
6077 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6078 (rdev->wowlan->gtk_rekey_failure &&
6079 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6080 (rdev->wowlan->eap_identity_req &&
6081 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6082 (rdev->wowlan->four_way_handshake &&
6083 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6084 (rdev->wowlan->rfkill_release &&
6085 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6086 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006087 if (rdev->wowlan->n_patterns) {
6088 struct nlattr *nl_pats, *nl_pat;
6089 int i, pat_len;
6090
6091 nl_pats = nla_nest_start(msg,
6092 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6093 if (!nl_pats)
6094 goto nla_put_failure;
6095
6096 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6097 nl_pat = nla_nest_start(msg, i + 1);
6098 if (!nl_pat)
6099 goto nla_put_failure;
6100 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006101 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6102 DIV_ROUND_UP(pat_len, 8),
6103 rdev->wowlan->patterns[i].mask) ||
6104 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6105 pat_len,
6106 rdev->wowlan->patterns[i].pattern))
6107 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006108 nla_nest_end(msg, nl_pat);
6109 }
6110 nla_nest_end(msg, nl_pats);
6111 }
6112
6113 nla_nest_end(msg, nl_wowlan);
6114 }
6115
6116 genlmsg_end(msg, hdr);
6117 return genlmsg_reply(msg, info);
6118
6119nla_put_failure:
6120 nlmsg_free(msg);
6121 return -ENOBUFS;
6122}
6123
6124static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6125{
6126 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6127 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6128 struct cfg80211_wowlan no_triggers = {};
6129 struct cfg80211_wowlan new_triggers = {};
6130 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6131 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006132 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006133
6134 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6135 return -EOPNOTSUPP;
6136
6137 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6138 goto no_triggers;
6139
6140 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6141 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6142 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6143 nl80211_wowlan_policy);
6144 if (err)
6145 return err;
6146
6147 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6148 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6149 return -EINVAL;
6150 new_triggers.any = true;
6151 }
6152
6153 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6154 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6155 return -EINVAL;
6156 new_triggers.disconnect = true;
6157 }
6158
6159 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6160 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6161 return -EINVAL;
6162 new_triggers.magic_pkt = true;
6163 }
6164
Johannes Berg77dbbb132011-07-13 10:48:55 +02006165 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6166 return -EINVAL;
6167
6168 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6169 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6170 return -EINVAL;
6171 new_triggers.gtk_rekey_failure = true;
6172 }
6173
6174 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6175 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6176 return -EINVAL;
6177 new_triggers.eap_identity_req = true;
6178 }
6179
6180 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6181 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6182 return -EINVAL;
6183 new_triggers.four_way_handshake = true;
6184 }
6185
6186 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6187 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6188 return -EINVAL;
6189 new_triggers.rfkill_release = true;
6190 }
6191
Johannes Bergff1b6e62011-05-04 15:37:28 +02006192 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6193 struct nlattr *pat;
6194 int n_patterns = 0;
6195 int rem, pat_len, mask_len;
6196 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6197
6198 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6199 rem)
6200 n_patterns++;
6201 if (n_patterns > wowlan->n_patterns)
6202 return -EINVAL;
6203
6204 new_triggers.patterns = kcalloc(n_patterns,
6205 sizeof(new_triggers.patterns[0]),
6206 GFP_KERNEL);
6207 if (!new_triggers.patterns)
6208 return -ENOMEM;
6209
6210 new_triggers.n_patterns = n_patterns;
6211 i = 0;
6212
6213 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6214 rem) {
6215 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6216 nla_data(pat), nla_len(pat), NULL);
6217 err = -EINVAL;
6218 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6219 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6220 goto error;
6221 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6222 mask_len = DIV_ROUND_UP(pat_len, 8);
6223 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6224 mask_len)
6225 goto error;
6226 if (pat_len > wowlan->pattern_max_len ||
6227 pat_len < wowlan->pattern_min_len)
6228 goto error;
6229
6230 new_triggers.patterns[i].mask =
6231 kmalloc(mask_len + pat_len, GFP_KERNEL);
6232 if (!new_triggers.patterns[i].mask) {
6233 err = -ENOMEM;
6234 goto error;
6235 }
6236 new_triggers.patterns[i].pattern =
6237 new_triggers.patterns[i].mask + mask_len;
6238 memcpy(new_triggers.patterns[i].mask,
6239 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6240 mask_len);
6241 new_triggers.patterns[i].pattern_len = pat_len;
6242 memcpy(new_triggers.patterns[i].pattern,
6243 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6244 pat_len);
6245 i++;
6246 }
6247 }
6248
6249 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6250 struct cfg80211_wowlan *ntrig;
6251 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6252 GFP_KERNEL);
6253 if (!ntrig) {
6254 err = -ENOMEM;
6255 goto error;
6256 }
6257 cfg80211_rdev_free_wowlan(rdev);
6258 rdev->wowlan = ntrig;
6259 } else {
6260 no_triggers:
6261 cfg80211_rdev_free_wowlan(rdev);
6262 rdev->wowlan = NULL;
6263 }
6264
Johannes Berg6d525632012-04-04 15:05:25 +02006265 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6266 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6267
Johannes Bergff1b6e62011-05-04 15:37:28 +02006268 return 0;
6269 error:
6270 for (i = 0; i < new_triggers.n_patterns; i++)
6271 kfree(new_triggers.patterns[i].mask);
6272 kfree(new_triggers.patterns);
6273 return err;
6274}
6275
Johannes Berge5497d72011-07-05 16:35:40 +02006276static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6277{
6278 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6279 struct net_device *dev = info->user_ptr[1];
6280 struct wireless_dev *wdev = dev->ieee80211_ptr;
6281 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6282 struct cfg80211_gtk_rekey_data rekey_data;
6283 int err;
6284
6285 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6286 return -EINVAL;
6287
6288 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6289 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6290 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6291 nl80211_rekey_policy);
6292 if (err)
6293 return err;
6294
6295 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6296 return -ERANGE;
6297 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6298 return -ERANGE;
6299 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6300 return -ERANGE;
6301
6302 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6303 NL80211_KEK_LEN);
6304 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6305 NL80211_KCK_LEN);
6306 memcpy(rekey_data.replay_ctr,
6307 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6308 NL80211_REPLAY_CTR_LEN);
6309
6310 wdev_lock(wdev);
6311 if (!wdev->current_bss) {
6312 err = -ENOTCONN;
6313 goto out;
6314 }
6315
6316 if (!rdev->ops->set_rekey_data) {
6317 err = -EOPNOTSUPP;
6318 goto out;
6319 }
6320
6321 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6322 out:
6323 wdev_unlock(wdev);
6324 return err;
6325}
6326
Johannes Berg28946da2011-11-04 11:18:12 +01006327static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6328 struct genl_info *info)
6329{
6330 struct net_device *dev = info->user_ptr[1];
6331 struct wireless_dev *wdev = dev->ieee80211_ptr;
6332
6333 if (wdev->iftype != NL80211_IFTYPE_AP &&
6334 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6335 return -EINVAL;
6336
6337 if (wdev->ap_unexpected_nlpid)
6338 return -EBUSY;
6339
6340 wdev->ap_unexpected_nlpid = info->snd_pid;
6341 return 0;
6342}
6343
Johannes Berg7f6cf312011-11-04 11:18:15 +01006344static int nl80211_probe_client(struct sk_buff *skb,
6345 struct genl_info *info)
6346{
6347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6348 struct net_device *dev = info->user_ptr[1];
6349 struct wireless_dev *wdev = dev->ieee80211_ptr;
6350 struct sk_buff *msg;
6351 void *hdr;
6352 const u8 *addr;
6353 u64 cookie;
6354 int err;
6355
6356 if (wdev->iftype != NL80211_IFTYPE_AP &&
6357 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6358 return -EOPNOTSUPP;
6359
6360 if (!info->attrs[NL80211_ATTR_MAC])
6361 return -EINVAL;
6362
6363 if (!rdev->ops->probe_client)
6364 return -EOPNOTSUPP;
6365
6366 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6367 if (!msg)
6368 return -ENOMEM;
6369
6370 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6371 NL80211_CMD_PROBE_CLIENT);
6372
6373 if (IS_ERR(hdr)) {
6374 err = PTR_ERR(hdr);
6375 goto free_msg;
6376 }
6377
6378 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6379
6380 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6381 if (err)
6382 goto free_msg;
6383
David S. Miller9360ffd2012-03-29 04:41:26 -04006384 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6385 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006386
6387 genlmsg_end(msg, hdr);
6388
6389 return genlmsg_reply(msg, info);
6390
6391 nla_put_failure:
6392 err = -ENOBUFS;
6393 free_msg:
6394 nlmsg_free(msg);
6395 return err;
6396}
6397
Johannes Berg5e760232011-11-04 11:18:17 +01006398static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6399{
6400 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6401
6402 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6403 return -EOPNOTSUPP;
6404
6405 if (rdev->ap_beacons_nlpid)
6406 return -EBUSY;
6407
6408 rdev->ap_beacons_nlpid = info->snd_pid;
6409
6410 return 0;
6411}
6412
Johannes Berg4c476992010-10-04 21:36:35 +02006413#define NL80211_FLAG_NEED_WIPHY 0x01
6414#define NL80211_FLAG_NEED_NETDEV 0x02
6415#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006416#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6417#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6418 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006419
6420static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6421 struct genl_info *info)
6422{
6423 struct cfg80211_registered_device *rdev;
6424 struct net_device *dev;
6425 int err;
6426 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6427
6428 if (rtnl)
6429 rtnl_lock();
6430
6431 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6432 rdev = cfg80211_get_dev_from_info(info);
6433 if (IS_ERR(rdev)) {
6434 if (rtnl)
6435 rtnl_unlock();
6436 return PTR_ERR(rdev);
6437 }
6438 info->user_ptr[0] = rdev;
6439 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006440 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6441 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006442 if (err) {
6443 if (rtnl)
6444 rtnl_unlock();
6445 return err;
6446 }
Johannes Berg41265712010-10-04 21:14:05 +02006447 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6448 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006449 cfg80211_unlock_rdev(rdev);
6450 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006451 if (rtnl)
6452 rtnl_unlock();
6453 return -ENETDOWN;
6454 }
Johannes Berg4c476992010-10-04 21:36:35 +02006455 info->user_ptr[0] = rdev;
6456 info->user_ptr[1] = dev;
6457 }
6458
6459 return 0;
6460}
6461
6462static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6463 struct genl_info *info)
6464{
6465 if (info->user_ptr[0])
6466 cfg80211_unlock_rdev(info->user_ptr[0]);
6467 if (info->user_ptr[1])
6468 dev_put(info->user_ptr[1]);
6469 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6470 rtnl_unlock();
6471}
6472
Johannes Berg55682962007-09-20 13:09:35 -04006473static struct genl_ops nl80211_ops[] = {
6474 {
6475 .cmd = NL80211_CMD_GET_WIPHY,
6476 .doit = nl80211_get_wiphy,
6477 .dumpit = nl80211_dump_wiphy,
6478 .policy = nl80211_policy,
6479 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006480 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006481 },
6482 {
6483 .cmd = NL80211_CMD_SET_WIPHY,
6484 .doit = nl80211_set_wiphy,
6485 .policy = nl80211_policy,
6486 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006487 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006488 },
6489 {
6490 .cmd = NL80211_CMD_GET_INTERFACE,
6491 .doit = nl80211_get_interface,
6492 .dumpit = nl80211_dump_interface,
6493 .policy = nl80211_policy,
6494 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006495 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006496 },
6497 {
6498 .cmd = NL80211_CMD_SET_INTERFACE,
6499 .doit = nl80211_set_interface,
6500 .policy = nl80211_policy,
6501 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006502 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6503 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006504 },
6505 {
6506 .cmd = NL80211_CMD_NEW_INTERFACE,
6507 .doit = nl80211_new_interface,
6508 .policy = nl80211_policy,
6509 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006510 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6511 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006512 },
6513 {
6514 .cmd = NL80211_CMD_DEL_INTERFACE,
6515 .doit = nl80211_del_interface,
6516 .policy = nl80211_policy,
6517 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006518 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6519 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006520 },
Johannes Berg41ade002007-12-19 02:03:29 +01006521 {
6522 .cmd = NL80211_CMD_GET_KEY,
6523 .doit = nl80211_get_key,
6524 .policy = nl80211_policy,
6525 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006527 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006528 },
6529 {
6530 .cmd = NL80211_CMD_SET_KEY,
6531 .doit = nl80211_set_key,
6532 .policy = nl80211_policy,
6533 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006535 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006536 },
6537 {
6538 .cmd = NL80211_CMD_NEW_KEY,
6539 .doit = nl80211_new_key,
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 Berg41ade002007-12-19 02:03:29 +01006544 },
6545 {
6546 .cmd = NL80211_CMD_DEL_KEY,
6547 .doit = nl80211_del_key,
6548 .policy = nl80211_policy,
6549 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006551 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006552 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006553 {
6554 .cmd = NL80211_CMD_SET_BEACON,
6555 .policy = nl80211_policy,
6556 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006557 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006559 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006560 },
6561 {
Johannes Berg88600202012-02-13 15:17:18 +01006562 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006563 .policy = nl80211_policy,
6564 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006565 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006567 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006568 },
6569 {
Johannes Berg88600202012-02-13 15:17:18 +01006570 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006571 .policy = nl80211_policy,
6572 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006573 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006574 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006575 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006576 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006577 {
6578 .cmd = NL80211_CMD_GET_STATION,
6579 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006580 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006581 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006582 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6583 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006584 },
6585 {
6586 .cmd = NL80211_CMD_SET_STATION,
6587 .doit = nl80211_set_station,
6588 .policy = nl80211_policy,
6589 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006590 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006591 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006592 },
6593 {
6594 .cmd = NL80211_CMD_NEW_STATION,
6595 .doit = nl80211_new_station,
6596 .policy = nl80211_policy,
6597 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006599 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006600 },
6601 {
6602 .cmd = NL80211_CMD_DEL_STATION,
6603 .doit = nl80211_del_station,
6604 .policy = nl80211_policy,
6605 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006607 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006608 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006609 {
6610 .cmd = NL80211_CMD_GET_MPATH,
6611 .doit = nl80211_get_mpath,
6612 .dumpit = nl80211_dump_mpath,
6613 .policy = nl80211_policy,
6614 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006615 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006616 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006617 },
6618 {
6619 .cmd = NL80211_CMD_SET_MPATH,
6620 .doit = nl80211_set_mpath,
6621 .policy = nl80211_policy,
6622 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006623 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006624 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006625 },
6626 {
6627 .cmd = NL80211_CMD_NEW_MPATH,
6628 .doit = nl80211_new_mpath,
6629 .policy = nl80211_policy,
6630 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006631 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006632 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006633 },
6634 {
6635 .cmd = NL80211_CMD_DEL_MPATH,
6636 .doit = nl80211_del_mpath,
6637 .policy = nl80211_policy,
6638 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006639 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006640 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006641 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006642 {
6643 .cmd = NL80211_CMD_SET_BSS,
6644 .doit = nl80211_set_bss,
6645 .policy = nl80211_policy,
6646 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006648 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006649 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006650 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006651 .cmd = NL80211_CMD_GET_REG,
6652 .doit = nl80211_get_reg,
6653 .policy = nl80211_policy,
6654 /* can be retrieved by unprivileged users */
6655 },
6656 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006657 .cmd = NL80211_CMD_SET_REG,
6658 .doit = nl80211_set_reg,
6659 .policy = nl80211_policy,
6660 .flags = GENL_ADMIN_PERM,
6661 },
6662 {
6663 .cmd = NL80211_CMD_REQ_SET_REG,
6664 .doit = nl80211_req_set_reg,
6665 .policy = nl80211_policy,
6666 .flags = GENL_ADMIN_PERM,
6667 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006668 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006669 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6670 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006671 .policy = nl80211_policy,
6672 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006673 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006674 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006675 },
6676 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006677 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6678 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006679 .policy = nl80211_policy,
6680 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006681 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006682 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006683 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006684 {
Johannes Berg2a519312009-02-10 21:25:55 +01006685 .cmd = NL80211_CMD_TRIGGER_SCAN,
6686 .doit = nl80211_trigger_scan,
6687 .policy = nl80211_policy,
6688 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006689 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006690 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006691 },
6692 {
6693 .cmd = NL80211_CMD_GET_SCAN,
6694 .policy = nl80211_policy,
6695 .dumpit = nl80211_dump_scan,
6696 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006697 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006698 .cmd = NL80211_CMD_START_SCHED_SCAN,
6699 .doit = nl80211_start_sched_scan,
6700 .policy = nl80211_policy,
6701 .flags = GENL_ADMIN_PERM,
6702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6703 NL80211_FLAG_NEED_RTNL,
6704 },
6705 {
6706 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6707 .doit = nl80211_stop_sched_scan,
6708 .policy = nl80211_policy,
6709 .flags = GENL_ADMIN_PERM,
6710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6711 NL80211_FLAG_NEED_RTNL,
6712 },
6713 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006714 .cmd = NL80211_CMD_AUTHENTICATE,
6715 .doit = nl80211_authenticate,
6716 .policy = nl80211_policy,
6717 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006718 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006719 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006720 },
6721 {
6722 .cmd = NL80211_CMD_ASSOCIATE,
6723 .doit = nl80211_associate,
6724 .policy = nl80211_policy,
6725 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006726 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006727 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006728 },
6729 {
6730 .cmd = NL80211_CMD_DEAUTHENTICATE,
6731 .doit = nl80211_deauthenticate,
6732 .policy = nl80211_policy,
6733 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006734 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006735 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006736 },
6737 {
6738 .cmd = NL80211_CMD_DISASSOCIATE,
6739 .doit = nl80211_disassociate,
6740 .policy = nl80211_policy,
6741 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006742 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006743 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006744 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006745 {
6746 .cmd = NL80211_CMD_JOIN_IBSS,
6747 .doit = nl80211_join_ibss,
6748 .policy = nl80211_policy,
6749 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006750 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006751 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006752 },
6753 {
6754 .cmd = NL80211_CMD_LEAVE_IBSS,
6755 .doit = nl80211_leave_ibss,
6756 .policy = nl80211_policy,
6757 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006758 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006759 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006760 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006761#ifdef CONFIG_NL80211_TESTMODE
6762 {
6763 .cmd = NL80211_CMD_TESTMODE,
6764 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006765 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006766 .policy = nl80211_policy,
6767 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006768 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6769 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006770 },
6771#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006772 {
6773 .cmd = NL80211_CMD_CONNECT,
6774 .doit = nl80211_connect,
6775 .policy = nl80211_policy,
6776 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006777 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006778 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006779 },
6780 {
6781 .cmd = NL80211_CMD_DISCONNECT,
6782 .doit = nl80211_disconnect,
6783 .policy = nl80211_policy,
6784 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006785 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006786 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006787 },
Johannes Berg463d0182009-07-14 00:33:35 +02006788 {
6789 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6790 .doit = nl80211_wiphy_netns,
6791 .policy = nl80211_policy,
6792 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006793 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6794 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006795 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006796 {
6797 .cmd = NL80211_CMD_GET_SURVEY,
6798 .policy = nl80211_policy,
6799 .dumpit = nl80211_dump_survey,
6800 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006801 {
6802 .cmd = NL80211_CMD_SET_PMKSA,
6803 .doit = nl80211_setdel_pmksa,
6804 .policy = nl80211_policy,
6805 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006806 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006807 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006808 },
6809 {
6810 .cmd = NL80211_CMD_DEL_PMKSA,
6811 .doit = nl80211_setdel_pmksa,
6812 .policy = nl80211_policy,
6813 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006814 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006815 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006816 },
6817 {
6818 .cmd = NL80211_CMD_FLUSH_PMKSA,
6819 .doit = nl80211_flush_pmksa,
6820 .policy = nl80211_policy,
6821 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006822 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006823 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006824 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006825 {
6826 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6827 .doit = nl80211_remain_on_channel,
6828 .policy = nl80211_policy,
6829 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006830 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006831 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006832 },
6833 {
6834 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6835 .doit = nl80211_cancel_remain_on_channel,
6836 .policy = nl80211_policy,
6837 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006838 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006839 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006840 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006841 {
6842 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6843 .doit = nl80211_set_tx_bitrate_mask,
6844 .policy = nl80211_policy,
6845 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006846 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6847 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006848 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006849 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006850 .cmd = NL80211_CMD_REGISTER_FRAME,
6851 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006852 .policy = nl80211_policy,
6853 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006854 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6855 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006856 },
6857 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006858 .cmd = NL80211_CMD_FRAME,
6859 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006860 .policy = nl80211_policy,
6861 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006862 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006863 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006864 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006865 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006866 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6867 .doit = nl80211_tx_mgmt_cancel_wait,
6868 .policy = nl80211_policy,
6869 .flags = GENL_ADMIN_PERM,
6870 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6871 NL80211_FLAG_NEED_RTNL,
6872 },
6873 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006874 .cmd = NL80211_CMD_SET_POWER_SAVE,
6875 .doit = nl80211_set_power_save,
6876 .policy = nl80211_policy,
6877 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006878 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6879 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006880 },
6881 {
6882 .cmd = NL80211_CMD_GET_POWER_SAVE,
6883 .doit = nl80211_get_power_save,
6884 .policy = nl80211_policy,
6885 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006886 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6887 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006888 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006889 {
6890 .cmd = NL80211_CMD_SET_CQM,
6891 .doit = nl80211_set_cqm,
6892 .policy = nl80211_policy,
6893 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006894 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6895 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006896 },
Johannes Bergf444de02010-05-05 15:25:02 +02006897 {
6898 .cmd = NL80211_CMD_SET_CHANNEL,
6899 .doit = nl80211_set_channel,
6900 .policy = nl80211_policy,
6901 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006902 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6903 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006904 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006905 {
6906 .cmd = NL80211_CMD_SET_WDS_PEER,
6907 .doit = nl80211_set_wds_peer,
6908 .policy = nl80211_policy,
6909 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006910 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6911 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006912 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006913 {
6914 .cmd = NL80211_CMD_JOIN_MESH,
6915 .doit = nl80211_join_mesh,
6916 .policy = nl80211_policy,
6917 .flags = GENL_ADMIN_PERM,
6918 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6919 NL80211_FLAG_NEED_RTNL,
6920 },
6921 {
6922 .cmd = NL80211_CMD_LEAVE_MESH,
6923 .doit = nl80211_leave_mesh,
6924 .policy = nl80211_policy,
6925 .flags = GENL_ADMIN_PERM,
6926 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6927 NL80211_FLAG_NEED_RTNL,
6928 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006929 {
6930 .cmd = NL80211_CMD_GET_WOWLAN,
6931 .doit = nl80211_get_wowlan,
6932 .policy = nl80211_policy,
6933 /* can be retrieved by unprivileged users */
6934 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6935 NL80211_FLAG_NEED_RTNL,
6936 },
6937 {
6938 .cmd = NL80211_CMD_SET_WOWLAN,
6939 .doit = nl80211_set_wowlan,
6940 .policy = nl80211_policy,
6941 .flags = GENL_ADMIN_PERM,
6942 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6943 NL80211_FLAG_NEED_RTNL,
6944 },
Johannes Berge5497d72011-07-05 16:35:40 +02006945 {
6946 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6947 .doit = nl80211_set_rekey_data,
6948 .policy = nl80211_policy,
6949 .flags = GENL_ADMIN_PERM,
6950 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6951 NL80211_FLAG_NEED_RTNL,
6952 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006953 {
6954 .cmd = NL80211_CMD_TDLS_MGMT,
6955 .doit = nl80211_tdls_mgmt,
6956 .policy = nl80211_policy,
6957 .flags = GENL_ADMIN_PERM,
6958 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6959 NL80211_FLAG_NEED_RTNL,
6960 },
6961 {
6962 .cmd = NL80211_CMD_TDLS_OPER,
6963 .doit = nl80211_tdls_oper,
6964 .policy = nl80211_policy,
6965 .flags = GENL_ADMIN_PERM,
6966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6967 NL80211_FLAG_NEED_RTNL,
6968 },
Johannes Berg28946da2011-11-04 11:18:12 +01006969 {
6970 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6971 .doit = nl80211_register_unexpected_frame,
6972 .policy = nl80211_policy,
6973 .flags = GENL_ADMIN_PERM,
6974 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6975 NL80211_FLAG_NEED_RTNL,
6976 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006977 {
6978 .cmd = NL80211_CMD_PROBE_CLIENT,
6979 .doit = nl80211_probe_client,
6980 .policy = nl80211_policy,
6981 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006982 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01006983 NL80211_FLAG_NEED_RTNL,
6984 },
Johannes Berg5e760232011-11-04 11:18:17 +01006985 {
6986 .cmd = NL80211_CMD_REGISTER_BEACONS,
6987 .doit = nl80211_register_beacons,
6988 .policy = nl80211_policy,
6989 .flags = GENL_ADMIN_PERM,
6990 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6991 NL80211_FLAG_NEED_RTNL,
6992 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006993 {
6994 .cmd = NL80211_CMD_SET_NOACK_MAP,
6995 .doit = nl80211_set_noack_map,
6996 .policy = nl80211_policy,
6997 .flags = GENL_ADMIN_PERM,
6998 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6999 NL80211_FLAG_NEED_RTNL,
7000 },
7001
Johannes Berg55682962007-09-20 13:09:35 -04007002};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007003
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007004static struct genl_multicast_group nl80211_mlme_mcgrp = {
7005 .name = "mlme",
7006};
Johannes Berg55682962007-09-20 13:09:35 -04007007
7008/* multicast groups */
7009static struct genl_multicast_group nl80211_config_mcgrp = {
7010 .name = "config",
7011};
Johannes Berg2a519312009-02-10 21:25:55 +01007012static struct genl_multicast_group nl80211_scan_mcgrp = {
7013 .name = "scan",
7014};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007015static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7016 .name = "regulatory",
7017};
Johannes Berg55682962007-09-20 13:09:35 -04007018
7019/* notification functions */
7020
7021void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7022{
7023 struct sk_buff *msg;
7024
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007026 if (!msg)
7027 return;
7028
7029 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7030 nlmsg_free(msg);
7031 return;
7032 }
7033
Johannes Berg463d0182009-07-14 00:33:35 +02007034 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7035 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007036}
7037
Johannes Berg362a4152009-05-24 16:43:15 +02007038static int nl80211_add_scan_req(struct sk_buff *msg,
7039 struct cfg80211_registered_device *rdev)
7040{
7041 struct cfg80211_scan_request *req = rdev->scan_req;
7042 struct nlattr *nest;
7043 int i;
7044
Johannes Berg667503d2009-07-07 03:56:11 +02007045 ASSERT_RDEV_LOCK(rdev);
7046
Johannes Berg362a4152009-05-24 16:43:15 +02007047 if (WARN_ON(!req))
7048 return 0;
7049
7050 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7051 if (!nest)
7052 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007053 for (i = 0; i < req->n_ssids; i++) {
7054 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7055 goto nla_put_failure;
7056 }
Johannes Berg362a4152009-05-24 16:43:15 +02007057 nla_nest_end(msg, nest);
7058
7059 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7060 if (!nest)
7061 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007062 for (i = 0; i < req->n_channels; i++) {
7063 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7064 goto nla_put_failure;
7065 }
Johannes Berg362a4152009-05-24 16:43:15 +02007066 nla_nest_end(msg, nest);
7067
David S. Miller9360ffd2012-03-29 04:41:26 -04007068 if (req->ie &&
7069 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7070 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007071
7072 return 0;
7073 nla_put_failure:
7074 return -ENOBUFS;
7075}
7076
Johannes Berga538e2d2009-06-16 19:56:42 +02007077static int nl80211_send_scan_msg(struct sk_buff *msg,
7078 struct cfg80211_registered_device *rdev,
7079 struct net_device *netdev,
7080 u32 pid, u32 seq, int flags,
7081 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007082{
7083 void *hdr;
7084
7085 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7086 if (!hdr)
7087 return -1;
7088
David S. Miller9360ffd2012-03-29 04:41:26 -04007089 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7090 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7091 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007092
Johannes Berg362a4152009-05-24 16:43:15 +02007093 /* ignore errors and send incomplete event anyway */
7094 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007095
7096 return genlmsg_end(msg, hdr);
7097
7098 nla_put_failure:
7099 genlmsg_cancel(msg, hdr);
7100 return -EMSGSIZE;
7101}
7102
Luciano Coelho807f8a82011-05-11 17:09:35 +03007103static int
7104nl80211_send_sched_scan_msg(struct sk_buff *msg,
7105 struct cfg80211_registered_device *rdev,
7106 struct net_device *netdev,
7107 u32 pid, u32 seq, int flags, u32 cmd)
7108{
7109 void *hdr;
7110
7111 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7112 if (!hdr)
7113 return -1;
7114
David S. Miller9360ffd2012-03-29 04:41:26 -04007115 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7116 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7117 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007118
7119 return genlmsg_end(msg, hdr);
7120
7121 nla_put_failure:
7122 genlmsg_cancel(msg, hdr);
7123 return -EMSGSIZE;
7124}
7125
Johannes Berga538e2d2009-06-16 19:56:42 +02007126void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7127 struct net_device *netdev)
7128{
7129 struct sk_buff *msg;
7130
7131 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7132 if (!msg)
7133 return;
7134
7135 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7136 NL80211_CMD_TRIGGER_SCAN) < 0) {
7137 nlmsg_free(msg);
7138 return;
7139 }
7140
Johannes Berg463d0182009-07-14 00:33:35 +02007141 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7142 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007143}
7144
Johannes Berg2a519312009-02-10 21:25:55 +01007145void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7146 struct net_device *netdev)
7147{
7148 struct sk_buff *msg;
7149
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007150 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007151 if (!msg)
7152 return;
7153
Johannes Berga538e2d2009-06-16 19:56:42 +02007154 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7155 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007156 nlmsg_free(msg);
7157 return;
7158 }
7159
Johannes Berg463d0182009-07-14 00:33:35 +02007160 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7161 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007162}
7163
7164void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7165 struct net_device *netdev)
7166{
7167 struct sk_buff *msg;
7168
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007169 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007170 if (!msg)
7171 return;
7172
Johannes Berga538e2d2009-06-16 19:56:42 +02007173 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7174 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007175 nlmsg_free(msg);
7176 return;
7177 }
7178
Johannes Berg463d0182009-07-14 00:33:35 +02007179 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7180 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007181}
7182
Luciano Coelho807f8a82011-05-11 17:09:35 +03007183void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7184 struct net_device *netdev)
7185{
7186 struct sk_buff *msg;
7187
7188 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7189 if (!msg)
7190 return;
7191
7192 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7193 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7194 nlmsg_free(msg);
7195 return;
7196 }
7197
7198 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7199 nl80211_scan_mcgrp.id, GFP_KERNEL);
7200}
7201
7202void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7203 struct net_device *netdev, u32 cmd)
7204{
7205 struct sk_buff *msg;
7206
7207 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7208 if (!msg)
7209 return;
7210
7211 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7212 nlmsg_free(msg);
7213 return;
7214 }
7215
7216 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7217 nl80211_scan_mcgrp.id, GFP_KERNEL);
7218}
7219
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007220/*
7221 * This can happen on global regulatory changes or device specific settings
7222 * based on custom world regulatory domains.
7223 */
7224void nl80211_send_reg_change_event(struct regulatory_request *request)
7225{
7226 struct sk_buff *msg;
7227 void *hdr;
7228
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007229 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007230 if (!msg)
7231 return;
7232
7233 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7234 if (!hdr) {
7235 nlmsg_free(msg);
7236 return;
7237 }
7238
7239 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007240 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7241 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007242
David S. Miller9360ffd2012-03-29 04:41:26 -04007243 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7244 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7245 NL80211_REGDOM_TYPE_WORLD))
7246 goto nla_put_failure;
7247 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7248 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7249 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7250 goto nla_put_failure;
7251 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7252 request->intersect) {
7253 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7254 NL80211_REGDOM_TYPE_INTERSECTION))
7255 goto nla_put_failure;
7256 } else {
7257 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7258 NL80211_REGDOM_TYPE_COUNTRY) ||
7259 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7260 request->alpha2))
7261 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007262 }
7263
David S. Miller9360ffd2012-03-29 04:41:26 -04007264 if (wiphy_idx_valid(request->wiphy_idx) &&
7265 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7266 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007267
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007268 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007269
Johannes Bergbc43b282009-07-25 10:54:13 +02007270 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007271 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007272 GFP_ATOMIC);
7273 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007274
7275 return;
7276
7277nla_put_failure:
7278 genlmsg_cancel(msg, hdr);
7279 nlmsg_free(msg);
7280}
7281
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007282static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7283 struct net_device *netdev,
7284 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007285 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007286{
7287 struct sk_buff *msg;
7288 void *hdr;
7289
Johannes Berge6d6e342009-07-01 21:26:47 +02007290 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007291 if (!msg)
7292 return;
7293
7294 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7295 if (!hdr) {
7296 nlmsg_free(msg);
7297 return;
7298 }
7299
David S. Miller9360ffd2012-03-29 04:41:26 -04007300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7301 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7302 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7303 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007304
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007305 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007306
Johannes Berg463d0182009-07-14 00:33:35 +02007307 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7308 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007309 return;
7310
7311 nla_put_failure:
7312 genlmsg_cancel(msg, hdr);
7313 nlmsg_free(msg);
7314}
7315
7316void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007317 struct net_device *netdev, const u8 *buf,
7318 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007319{
7320 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007321 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007322}
7323
7324void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7325 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007326 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007327{
Johannes Berge6d6e342009-07-01 21:26:47 +02007328 nl80211_send_mlme_event(rdev, netdev, buf, len,
7329 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007330}
7331
Jouni Malinen53b46b82009-03-27 20:53:56 +02007332void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007333 struct net_device *netdev, const u8 *buf,
7334 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007335{
7336 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007337 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007338}
7339
Jouni Malinen53b46b82009-03-27 20:53:56 +02007340void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7341 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007342 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007343{
7344 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007345 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007346}
7347
Jouni Malinencf4e5942010-12-16 00:52:40 +02007348void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7349 struct net_device *netdev, const u8 *buf,
7350 size_t len, gfp_t gfp)
7351{
7352 nl80211_send_mlme_event(rdev, netdev, buf, len,
7353 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7354}
7355
7356void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7357 struct net_device *netdev, const u8 *buf,
7358 size_t len, gfp_t gfp)
7359{
7360 nl80211_send_mlme_event(rdev, netdev, buf, len,
7361 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7362}
7363
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007364static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7365 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007366 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007367{
7368 struct sk_buff *msg;
7369 void *hdr;
7370
Johannes Berge6d6e342009-07-01 21:26:47 +02007371 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007372 if (!msg)
7373 return;
7374
7375 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7376 if (!hdr) {
7377 nlmsg_free(msg);
7378 return;
7379 }
7380
David S. Miller9360ffd2012-03-29 04:41:26 -04007381 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7382 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7383 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7384 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7385 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007386
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007387 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007388
Johannes Berg463d0182009-07-14 00:33:35 +02007389 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7390 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007391 return;
7392
7393 nla_put_failure:
7394 genlmsg_cancel(msg, hdr);
7395 nlmsg_free(msg);
7396}
7397
7398void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007399 struct net_device *netdev, const u8 *addr,
7400 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007401{
7402 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007403 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007404}
7405
7406void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007407 struct net_device *netdev, const u8 *addr,
7408 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007409{
Johannes Berge6d6e342009-07-01 21:26:47 +02007410 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7411 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007412}
7413
Samuel Ortizb23aa672009-07-01 21:26:54 +02007414void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7415 struct net_device *netdev, const u8 *bssid,
7416 const u8 *req_ie, size_t req_ie_len,
7417 const u8 *resp_ie, size_t resp_ie_len,
7418 u16 status, gfp_t gfp)
7419{
7420 struct sk_buff *msg;
7421 void *hdr;
7422
7423 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7424 if (!msg)
7425 return;
7426
7427 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7428 if (!hdr) {
7429 nlmsg_free(msg);
7430 return;
7431 }
7432
David S. Miller9360ffd2012-03-29 04:41:26 -04007433 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7434 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7435 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7436 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7437 (req_ie &&
7438 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7439 (resp_ie &&
7440 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7441 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007442
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007443 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007444
Johannes Berg463d0182009-07-14 00:33:35 +02007445 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7446 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007447 return;
7448
7449 nla_put_failure:
7450 genlmsg_cancel(msg, hdr);
7451 nlmsg_free(msg);
7452
7453}
7454
7455void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7456 struct net_device *netdev, const u8 *bssid,
7457 const u8 *req_ie, size_t req_ie_len,
7458 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7459{
7460 struct sk_buff *msg;
7461 void *hdr;
7462
7463 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7464 if (!msg)
7465 return;
7466
7467 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7468 if (!hdr) {
7469 nlmsg_free(msg);
7470 return;
7471 }
7472
David S. Miller9360ffd2012-03-29 04:41:26 -04007473 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7474 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7475 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7476 (req_ie &&
7477 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7478 (resp_ie &&
7479 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7480 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007481
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007482 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007483
Johannes Berg463d0182009-07-14 00:33:35 +02007484 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7485 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007486 return;
7487
7488 nla_put_failure:
7489 genlmsg_cancel(msg, hdr);
7490 nlmsg_free(msg);
7491
7492}
7493
7494void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7495 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007496 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007497{
7498 struct sk_buff *msg;
7499 void *hdr;
7500
Johannes Berg667503d2009-07-07 03:56:11 +02007501 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007502 if (!msg)
7503 return;
7504
7505 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7506 if (!hdr) {
7507 nlmsg_free(msg);
7508 return;
7509 }
7510
David S. Miller9360ffd2012-03-29 04:41:26 -04007511 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7512 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7513 (from_ap && reason &&
7514 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7515 (from_ap &&
7516 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7517 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7518 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007519
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007520 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007521
Johannes Berg463d0182009-07-14 00:33:35 +02007522 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7523 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007524 return;
7525
7526 nla_put_failure:
7527 genlmsg_cancel(msg, hdr);
7528 nlmsg_free(msg);
7529
7530}
7531
Johannes Berg04a773a2009-04-19 21:24:32 +02007532void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7533 struct net_device *netdev, const u8 *bssid,
7534 gfp_t gfp)
7535{
7536 struct sk_buff *msg;
7537 void *hdr;
7538
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007539 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007540 if (!msg)
7541 return;
7542
7543 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7544 if (!hdr) {
7545 nlmsg_free(msg);
7546 return;
7547 }
7548
David S. Miller9360ffd2012-03-29 04:41:26 -04007549 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7550 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7551 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7552 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007553
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007554 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007555
Johannes Berg463d0182009-07-14 00:33:35 +02007556 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7557 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007558 return;
7559
7560 nla_put_failure:
7561 genlmsg_cancel(msg, hdr);
7562 nlmsg_free(msg);
7563}
7564
Javier Cardonac93b5e72011-04-07 15:08:34 -07007565void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7566 struct net_device *netdev,
7567 const u8 *macaddr, const u8* ie, u8 ie_len,
7568 gfp_t gfp)
7569{
7570 struct sk_buff *msg;
7571 void *hdr;
7572
7573 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7574 if (!msg)
7575 return;
7576
7577 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7578 if (!hdr) {
7579 nlmsg_free(msg);
7580 return;
7581 }
7582
David S. Miller9360ffd2012-03-29 04:41:26 -04007583 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7584 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7585 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7586 (ie_len && ie &&
7587 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7588 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007589
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007590 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007591
7592 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7593 nl80211_mlme_mcgrp.id, gfp);
7594 return;
7595
7596 nla_put_failure:
7597 genlmsg_cancel(msg, hdr);
7598 nlmsg_free(msg);
7599}
7600
Jouni Malinena3b8b052009-03-27 21:59:49 +02007601void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7602 struct net_device *netdev, const u8 *addr,
7603 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007604 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007605{
7606 struct sk_buff *msg;
7607 void *hdr;
7608
Johannes Berge6d6e342009-07-01 21:26:47 +02007609 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007610 if (!msg)
7611 return;
7612
7613 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7614 if (!hdr) {
7615 nlmsg_free(msg);
7616 return;
7617 }
7618
David S. Miller9360ffd2012-03-29 04:41:26 -04007619 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7620 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7621 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7622 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7623 (key_id != -1 &&
7624 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7625 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7626 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007627
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007628 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007629
Johannes Berg463d0182009-07-14 00:33:35 +02007630 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7631 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007632 return;
7633
7634 nla_put_failure:
7635 genlmsg_cancel(msg, hdr);
7636 nlmsg_free(msg);
7637}
7638
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007639void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7640 struct ieee80211_channel *channel_before,
7641 struct ieee80211_channel *channel_after)
7642{
7643 struct sk_buff *msg;
7644 void *hdr;
7645 struct nlattr *nl_freq;
7646
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007647 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007648 if (!msg)
7649 return;
7650
7651 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7652 if (!hdr) {
7653 nlmsg_free(msg);
7654 return;
7655 }
7656
7657 /*
7658 * Since we are applying the beacon hint to a wiphy we know its
7659 * wiphy_idx is valid
7660 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007661 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7662 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007663
7664 /* Before */
7665 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7666 if (!nl_freq)
7667 goto nla_put_failure;
7668 if (nl80211_msg_put_channel(msg, channel_before))
7669 goto nla_put_failure;
7670 nla_nest_end(msg, nl_freq);
7671
7672 /* After */
7673 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7674 if (!nl_freq)
7675 goto nla_put_failure;
7676 if (nl80211_msg_put_channel(msg, channel_after))
7677 goto nla_put_failure;
7678 nla_nest_end(msg, nl_freq);
7679
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007680 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007681
Johannes Berg463d0182009-07-14 00:33:35 +02007682 rcu_read_lock();
7683 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7684 GFP_ATOMIC);
7685 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007686
7687 return;
7688
7689nla_put_failure:
7690 genlmsg_cancel(msg, hdr);
7691 nlmsg_free(msg);
7692}
7693
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007694static void nl80211_send_remain_on_chan_event(
7695 int cmd, struct cfg80211_registered_device *rdev,
7696 struct net_device *netdev, u64 cookie,
7697 struct ieee80211_channel *chan,
7698 enum nl80211_channel_type channel_type,
7699 unsigned int duration, gfp_t gfp)
7700{
7701 struct sk_buff *msg;
7702 void *hdr;
7703
7704 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7705 if (!msg)
7706 return;
7707
7708 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7709 if (!hdr) {
7710 nlmsg_free(msg);
7711 return;
7712 }
7713
David S. Miller9360ffd2012-03-29 04:41:26 -04007714 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7715 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7716 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7717 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7718 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7719 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007720
David S. Miller9360ffd2012-03-29 04:41:26 -04007721 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7722 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7723 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007724
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007725 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007726
7727 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7728 nl80211_mlme_mcgrp.id, gfp);
7729 return;
7730
7731 nla_put_failure:
7732 genlmsg_cancel(msg, hdr);
7733 nlmsg_free(msg);
7734}
7735
7736void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7737 struct net_device *netdev, u64 cookie,
7738 struct ieee80211_channel *chan,
7739 enum nl80211_channel_type channel_type,
7740 unsigned int duration, gfp_t gfp)
7741{
7742 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7743 rdev, netdev, cookie, chan,
7744 channel_type, duration, gfp);
7745}
7746
7747void nl80211_send_remain_on_channel_cancel(
7748 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7749 u64 cookie, struct ieee80211_channel *chan,
7750 enum nl80211_channel_type channel_type, gfp_t gfp)
7751{
7752 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7753 rdev, netdev, cookie, chan,
7754 channel_type, 0, gfp);
7755}
7756
Johannes Berg98b62182009-12-23 13:15:44 +01007757void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7758 struct net_device *dev, const u8 *mac_addr,
7759 struct station_info *sinfo, gfp_t gfp)
7760{
7761 struct sk_buff *msg;
7762
7763 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7764 if (!msg)
7765 return;
7766
John W. Linville66266b32012-03-15 13:25:41 -04007767 if (nl80211_send_station(msg, 0, 0, 0,
7768 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007769 nlmsg_free(msg);
7770 return;
7771 }
7772
7773 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7774 nl80211_mlme_mcgrp.id, gfp);
7775}
7776
Jouni Malinenec15e682011-03-23 15:29:52 +02007777void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7778 struct net_device *dev, const u8 *mac_addr,
7779 gfp_t gfp)
7780{
7781 struct sk_buff *msg;
7782 void *hdr;
7783
7784 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7785 if (!msg)
7786 return;
7787
7788 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7789 if (!hdr) {
7790 nlmsg_free(msg);
7791 return;
7792 }
7793
David S. Miller9360ffd2012-03-29 04:41:26 -04007794 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7795 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7796 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007797
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007798 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007799
7800 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7801 nl80211_mlme_mcgrp.id, gfp);
7802 return;
7803
7804 nla_put_failure:
7805 genlmsg_cancel(msg, hdr);
7806 nlmsg_free(msg);
7807}
7808
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007809static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7810 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007811{
7812 struct wireless_dev *wdev = dev->ieee80211_ptr;
7813 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7814 struct sk_buff *msg;
7815 void *hdr;
7816 int err;
7817 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7818
7819 if (!nlpid)
7820 return false;
7821
7822 msg = nlmsg_new(100, gfp);
7823 if (!msg)
7824 return true;
7825
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007826 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007827 if (!hdr) {
7828 nlmsg_free(msg);
7829 return true;
7830 }
7831
David S. Miller9360ffd2012-03-29 04:41:26 -04007832 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7833 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7834 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7835 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007836
7837 err = genlmsg_end(msg, hdr);
7838 if (err < 0) {
7839 nlmsg_free(msg);
7840 return true;
7841 }
7842
7843 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7844 return true;
7845
7846 nla_put_failure:
7847 genlmsg_cancel(msg, hdr);
7848 nlmsg_free(msg);
7849 return true;
7850}
7851
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007852bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7853{
7854 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7855 addr, gfp);
7856}
7857
7858bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7859 const u8 *addr, gfp_t gfp)
7860{
7861 return __nl80211_unexpected_frame(dev,
7862 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7863 addr, gfp);
7864}
7865
Johannes Berg2e161f72010-08-12 15:38:38 +02007866int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7867 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007868 int freq, int sig_dbm,
7869 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007870{
7871 struct sk_buff *msg;
7872 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007873
7874 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7875 if (!msg)
7876 return -ENOMEM;
7877
Johannes Berg2e161f72010-08-12 15:38:38 +02007878 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007879 if (!hdr) {
7880 nlmsg_free(msg);
7881 return -ENOMEM;
7882 }
7883
David S. Miller9360ffd2012-03-29 04:41:26 -04007884 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7885 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7886 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7887 (sig_dbm &&
7888 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7889 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7890 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007891
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007892 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007893
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007894 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007895
7896 nla_put_failure:
7897 genlmsg_cancel(msg, hdr);
7898 nlmsg_free(msg);
7899 return -ENOBUFS;
7900}
7901
Johannes Berg2e161f72010-08-12 15:38:38 +02007902void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7903 struct net_device *netdev, u64 cookie,
7904 const u8 *buf, size_t len, bool ack,
7905 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007906{
7907 struct sk_buff *msg;
7908 void *hdr;
7909
7910 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7911 if (!msg)
7912 return;
7913
Johannes Berg2e161f72010-08-12 15:38:38 +02007914 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007915 if (!hdr) {
7916 nlmsg_free(msg);
7917 return;
7918 }
7919
David S. Miller9360ffd2012-03-29 04:41:26 -04007920 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7921 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7922 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7923 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7924 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7925 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007926
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007927 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007928
7929 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7930 return;
7931
7932 nla_put_failure:
7933 genlmsg_cancel(msg, hdr);
7934 nlmsg_free(msg);
7935}
7936
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007937void
7938nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7939 struct net_device *netdev,
7940 enum nl80211_cqm_rssi_threshold_event rssi_event,
7941 gfp_t gfp)
7942{
7943 struct sk_buff *msg;
7944 struct nlattr *pinfoattr;
7945 void *hdr;
7946
7947 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7948 if (!msg)
7949 return;
7950
7951 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7952 if (!hdr) {
7953 nlmsg_free(msg);
7954 return;
7955 }
7956
David S. Miller9360ffd2012-03-29 04:41:26 -04007957 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7958 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7959 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007960
7961 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7962 if (!pinfoattr)
7963 goto nla_put_failure;
7964
David S. Miller9360ffd2012-03-29 04:41:26 -04007965 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7966 rssi_event))
7967 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007968
7969 nla_nest_end(msg, pinfoattr);
7970
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007971 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007972
7973 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7974 nl80211_mlme_mcgrp.id, gfp);
7975 return;
7976
7977 nla_put_failure:
7978 genlmsg_cancel(msg, hdr);
7979 nlmsg_free(msg);
7980}
7981
Johannes Berge5497d72011-07-05 16:35:40 +02007982void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7983 struct net_device *netdev, const u8 *bssid,
7984 const u8 *replay_ctr, gfp_t gfp)
7985{
7986 struct sk_buff *msg;
7987 struct nlattr *rekey_attr;
7988 void *hdr;
7989
7990 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7991 if (!msg)
7992 return;
7993
7994 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7995 if (!hdr) {
7996 nlmsg_free(msg);
7997 return;
7998 }
7999
David S. Miller9360ffd2012-03-29 04:41:26 -04008000 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8001 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8002 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8003 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008004
8005 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8006 if (!rekey_attr)
8007 goto nla_put_failure;
8008
David S. Miller9360ffd2012-03-29 04:41:26 -04008009 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8010 NL80211_REPLAY_CTR_LEN, replay_ctr))
8011 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008012
8013 nla_nest_end(msg, rekey_attr);
8014
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008015 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008016
8017 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8018 nl80211_mlme_mcgrp.id, gfp);
8019 return;
8020
8021 nla_put_failure:
8022 genlmsg_cancel(msg, hdr);
8023 nlmsg_free(msg);
8024}
8025
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008026void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8027 struct net_device *netdev, int index,
8028 const u8 *bssid, bool preauth, gfp_t gfp)
8029{
8030 struct sk_buff *msg;
8031 struct nlattr *attr;
8032 void *hdr;
8033
8034 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8035 if (!msg)
8036 return;
8037
8038 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8039 if (!hdr) {
8040 nlmsg_free(msg);
8041 return;
8042 }
8043
David S. Miller9360ffd2012-03-29 04:41:26 -04008044 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8045 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8046 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008047
8048 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8049 if (!attr)
8050 goto nla_put_failure;
8051
David S. Miller9360ffd2012-03-29 04:41:26 -04008052 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8053 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8054 (preauth &&
8055 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8056 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008057
8058 nla_nest_end(msg, attr);
8059
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008060 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008061
8062 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8063 nl80211_mlme_mcgrp.id, gfp);
8064 return;
8065
8066 nla_put_failure:
8067 genlmsg_cancel(msg, hdr);
8068 nlmsg_free(msg);
8069}
8070
Thomas Pedersen53145262012-04-06 13:35:47 -07008071void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8072 struct net_device *netdev, int freq,
8073 enum nl80211_channel_type type, gfp_t gfp)
8074{
8075 struct sk_buff *msg;
8076 void *hdr;
8077
8078 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8079 if (!msg)
8080 return;
8081
8082 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8083 if (!hdr) {
8084 nlmsg_free(msg);
8085 return;
8086 }
8087
John W. Linville7eab0f62012-04-12 14:25:14 -04008088 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8089 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8090 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8091 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008092
8093 genlmsg_end(msg, hdr);
8094
8095 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8096 nl80211_mlme_mcgrp.id, gfp);
8097 return;
8098
8099 nla_put_failure:
8100 genlmsg_cancel(msg, hdr);
8101 nlmsg_free(msg);
8102}
8103
Johannes Bergc063dbf2010-11-24 08:10:05 +01008104void
8105nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8106 struct net_device *netdev, const u8 *peer,
8107 u32 num_packets, gfp_t gfp)
8108{
8109 struct sk_buff *msg;
8110 struct nlattr *pinfoattr;
8111 void *hdr;
8112
8113 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8114 if (!msg)
8115 return;
8116
8117 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8118 if (!hdr) {
8119 nlmsg_free(msg);
8120 return;
8121 }
8122
David S. Miller9360ffd2012-03-29 04:41:26 -04008123 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8124 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8125 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8126 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008127
8128 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8129 if (!pinfoattr)
8130 goto nla_put_failure;
8131
David S. Miller9360ffd2012-03-29 04:41:26 -04008132 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8133 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008134
8135 nla_nest_end(msg, pinfoattr);
8136
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008137 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008138
8139 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8140 nl80211_mlme_mcgrp.id, gfp);
8141 return;
8142
8143 nla_put_failure:
8144 genlmsg_cancel(msg, hdr);
8145 nlmsg_free(msg);
8146}
8147
Johannes Berg7f6cf312011-11-04 11:18:15 +01008148void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8149 u64 cookie, bool acked, gfp_t gfp)
8150{
8151 struct wireless_dev *wdev = dev->ieee80211_ptr;
8152 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8153 struct sk_buff *msg;
8154 void *hdr;
8155 int err;
8156
8157 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8158 if (!msg)
8159 return;
8160
8161 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8162 if (!hdr) {
8163 nlmsg_free(msg);
8164 return;
8165 }
8166
David S. Miller9360ffd2012-03-29 04:41:26 -04008167 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8168 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8169 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8170 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8171 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8172 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008173
8174 err = genlmsg_end(msg, hdr);
8175 if (err < 0) {
8176 nlmsg_free(msg);
8177 return;
8178 }
8179
8180 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8181 nl80211_mlme_mcgrp.id, gfp);
8182 return;
8183
8184 nla_put_failure:
8185 genlmsg_cancel(msg, hdr);
8186 nlmsg_free(msg);
8187}
8188EXPORT_SYMBOL(cfg80211_probe_status);
8189
Johannes Berg5e760232011-11-04 11:18:17 +01008190void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8191 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008192 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008193{
8194 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8195 struct sk_buff *msg;
8196 void *hdr;
8197 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8198
8199 if (!nlpid)
8200 return;
8201
8202 msg = nlmsg_new(len + 100, gfp);
8203 if (!msg)
8204 return;
8205
8206 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8207 if (!hdr) {
8208 nlmsg_free(msg);
8209 return;
8210 }
8211
David S. Miller9360ffd2012-03-29 04:41:26 -04008212 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8213 (freq &&
8214 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8215 (sig_dbm &&
8216 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8217 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8218 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008219
8220 genlmsg_end(msg, hdr);
8221
8222 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8223 return;
8224
8225 nla_put_failure:
8226 genlmsg_cancel(msg, hdr);
8227 nlmsg_free(msg);
8228}
8229EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8230
Jouni Malinen026331c2010-02-15 12:53:10 +02008231static int nl80211_netlink_notify(struct notifier_block * nb,
8232 unsigned long state,
8233 void *_notify)
8234{
8235 struct netlink_notify *notify = _notify;
8236 struct cfg80211_registered_device *rdev;
8237 struct wireless_dev *wdev;
8238
8239 if (state != NETLINK_URELEASE)
8240 return NOTIFY_DONE;
8241
8242 rcu_read_lock();
8243
Johannes Berg5e760232011-11-04 11:18:17 +01008244 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008245 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008246 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008247 if (rdev->ap_beacons_nlpid == notify->pid)
8248 rdev->ap_beacons_nlpid = 0;
8249 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008250
8251 rcu_read_unlock();
8252
8253 return NOTIFY_DONE;
8254}
8255
8256static struct notifier_block nl80211_netlink_notifier = {
8257 .notifier_call = nl80211_netlink_notify,
8258};
8259
Johannes Berg55682962007-09-20 13:09:35 -04008260/* initialisation/exit functions */
8261
8262int nl80211_init(void)
8263{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008264 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008265
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008266 err = genl_register_family_with_ops(&nl80211_fam,
8267 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008268 if (err)
8269 return err;
8270
Johannes Berg55682962007-09-20 13:09:35 -04008271 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8272 if (err)
8273 goto err_out;
8274
Johannes Berg2a519312009-02-10 21:25:55 +01008275 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8276 if (err)
8277 goto err_out;
8278
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008279 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8280 if (err)
8281 goto err_out;
8282
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008283 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8284 if (err)
8285 goto err_out;
8286
Johannes Bergaff89a92009-07-01 21:26:51 +02008287#ifdef CONFIG_NL80211_TESTMODE
8288 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8289 if (err)
8290 goto err_out;
8291#endif
8292
Jouni Malinen026331c2010-02-15 12:53:10 +02008293 err = netlink_register_notifier(&nl80211_netlink_notifier);
8294 if (err)
8295 goto err_out;
8296
Johannes Berg55682962007-09-20 13:09:35 -04008297 return 0;
8298 err_out:
8299 genl_unregister_family(&nl80211_fam);
8300 return err;
8301}
8302
8303void nl80211_exit(void)
8304{
Jouni Malinen026331c2010-02-15 12:53:10 +02008305 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008306 genl_unregister_family(&nl80211_fam);
8307}