blob: ff1a6c7fbe3392d17860d866399606449b9270d4 [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 Malinen36aedc902008-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 Berg77dbbb12011-07-13 10:48:55 +0200237 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200241};
242
Johannes Berge5497d72011-07-05 16:35:40 +0200243/* policy for GTK rekey offload attributes */
244static const struct nla_policy
245nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
246 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
247 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
248 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249};
250
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300251static const struct nla_policy
252nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
253 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_SSID_LEN },
255};
256
Holger Schuriga0438972009-11-11 11:30:02 +0100257/* ifidx get helper */
258static int nl80211_get_ifidx(struct netlink_callback *cb)
259{
260 int res;
261
262 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
263 nl80211_fam.attrbuf, nl80211_fam.maxattr,
264 nl80211_policy);
265 if (res)
266 return res;
267
268 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
269 return -EINVAL;
270
271 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
272 if (!res)
273 return -EINVAL;
274 return res;
275}
276
Johannes Berg67748892010-10-04 21:14:06 +0200277static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
278 struct netlink_callback *cb,
279 struct cfg80211_registered_device **rdev,
280 struct net_device **dev)
281{
282 int ifidx = cb->args[0];
283 int err;
284
285 if (!ifidx)
286 ifidx = nl80211_get_ifidx(cb);
287 if (ifidx < 0)
288 return ifidx;
289
290 cb->args[0] = ifidx;
291
292 rtnl_lock();
293
294 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
295 if (!*dev) {
296 err = -ENODEV;
297 goto out_rtnl;
298 }
299
300 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100301 if (IS_ERR(*rdev)) {
302 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200303 goto out_rtnl;
304 }
305
306 return 0;
307 out_rtnl:
308 rtnl_unlock();
309 return err;
310}
311
312static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313{
314 cfg80211_unlock_rdev(rdev);
315 rtnl_unlock();
316}
317
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100318/* IE validation */
319static bool is_valid_ie_attr(const struct nlattr *attr)
320{
321 const u8 *pos;
322 int len;
323
324 if (!attr)
325 return true;
326
327 pos = nla_data(attr);
328 len = nla_len(attr);
329
330 while (len) {
331 u8 elemlen;
332
333 if (len < 2)
334 return false;
335 len -= 2;
336
337 elemlen = pos[1];
338 if (elemlen > len)
339 return false;
340
341 len -= elemlen;
342 pos += 2 + elemlen;
343 }
344
345 return true;
346}
347
Johannes Berg55682962007-09-20 13:09:35 -0400348/* message building helper */
349static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
350 int flags, u8 cmd)
351{
352 /* since there is no private header just add the generic one */
353 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354}
355
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400356static int nl80211_msg_put_channel(struct sk_buff *msg,
357 struct ieee80211_channel *chan)
358{
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 Berg5e7602302011-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 Berg5e7602302011-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
Johannes Berg55682962007-09-20 13:09:35 -04001543 return genlmsg_end(msg, hdr);
1544
1545 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001546 genlmsg_cancel(msg, hdr);
1547 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001548}
1549
1550static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1551{
1552 int wp_idx = 0;
1553 int if_idx = 0;
1554 int wp_start = cb->args[0];
1555 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001556 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001557 struct wireless_dev *wdev;
1558
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001559 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001560 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1561 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001562 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001563 if (wp_idx < wp_start) {
1564 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001565 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001566 }
Johannes Berg55682962007-09-20 13:09:35 -04001567 if_idx = 0;
1568
Johannes Bergf5ea9122009-08-07 16:17:38 +02001569 mutex_lock(&rdev->devlist_mtx);
1570 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001571 if (if_idx < if_start) {
1572 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001573 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001574 }
Johannes Berg55682962007-09-20 13:09:35 -04001575 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1576 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001577 rdev, wdev->netdev) < 0) {
1578 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001579 goto out;
1580 }
1581 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001582 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001583 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001584
1585 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001586 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001587 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001588 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001589
1590 cb->args[0] = wp_idx;
1591 cb->args[1] = if_idx;
1592
1593 return skb->len;
1594}
1595
1596static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1597{
1598 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001599 struct cfg80211_registered_device *dev = info->user_ptr[0];
1600 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001601
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001602 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001603 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001604 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001605
Johannes Bergd7264052009-04-19 16:23:20 +02001606 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001607 dev, netdev) < 0) {
1608 nlmsg_free(msg);
1609 return -ENOBUFS;
1610 }
Johannes Berg55682962007-09-20 13:09:35 -04001611
Johannes Berg134e6372009-07-10 09:51:34 +00001612 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001613}
1614
Michael Wu66f7ac52008-01-31 19:48:22 +01001615static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1616 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1617 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1618 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1619 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1620 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1621};
1622
1623static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1624{
1625 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1626 int flag;
1627
1628 *mntrflags = 0;
1629
1630 if (!nla)
1631 return -EINVAL;
1632
1633 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1634 nla, mntr_flags_policy))
1635 return -EINVAL;
1636
1637 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1638 if (flags[flag])
1639 *mntrflags |= (1<<flag);
1640
1641 return 0;
1642}
1643
Johannes Berg9bc383d2009-11-19 11:55:19 +01001644static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001645 struct net_device *netdev, u8 use_4addr,
1646 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001647{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001648 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001649 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001650 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001651 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001652 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001653
1654 switch (iftype) {
1655 case NL80211_IFTYPE_AP_VLAN:
1656 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1657 return 0;
1658 break;
1659 case NL80211_IFTYPE_STATION:
1660 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1661 return 0;
1662 break;
1663 default:
1664 break;
1665 }
1666
1667 return -EOPNOTSUPP;
1668}
1669
Johannes Berg55682962007-09-20 13:09:35 -04001670static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1671{
Johannes Berg4c476992010-10-04 21:36:35 +02001672 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001673 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001674 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001675 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001676 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001677 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001678 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001679
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001680 memset(&params, 0, sizeof(params));
1681
Johannes Berg04a773a2009-04-19 21:24:32 +02001682 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001683
Johannes Berg723b0382008-09-16 20:22:09 +02001684 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001685 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001686 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001687 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001688 if (ntype > NL80211_IFTYPE_MAX)
1689 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001690 }
1691
Johannes Berg92ffe052008-09-16 20:39:36 +02001692 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001693 struct wireless_dev *wdev = dev->ieee80211_ptr;
1694
Johannes Berg4c476992010-10-04 21:36:35 +02001695 if (ntype != NL80211_IFTYPE_MESH_POINT)
1696 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001697 if (netif_running(dev))
1698 return -EBUSY;
1699
1700 wdev_lock(wdev);
1701 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1702 IEEE80211_MAX_MESH_ID_LEN);
1703 wdev->mesh_id_up_len =
1704 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1705 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1706 wdev->mesh_id_up_len);
1707 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001708 }
1709
Felix Fietkau8b787642009-11-10 18:53:10 +01001710 if (info->attrs[NL80211_ATTR_4ADDR]) {
1711 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1712 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001713 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001714 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001715 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001716 } else {
1717 params.use_4addr = -1;
1718 }
1719
Johannes Berg92ffe052008-09-16 20:39:36 +02001720 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001721 if (ntype != NL80211_IFTYPE_MONITOR)
1722 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001723 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1724 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001725 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001726 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001727
1728 flags = &_flags;
1729 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001730 }
Johannes Berg3b858752009-03-12 09:55:09 +01001731
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001732 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001733 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001734 else
1735 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001736
Johannes Berg9bc383d2009-11-19 11:55:19 +01001737 if (!err && params.use_4addr != -1)
1738 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1739
Johannes Berg55682962007-09-20 13:09:35 -04001740 return err;
1741}
1742
1743static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1744{
Johannes Berg4c476992010-10-04 21:36:35 +02001745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001746 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001747 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001748 int err;
1749 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001750 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001751
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001752 memset(&params, 0, sizeof(params));
1753
Johannes Berg55682962007-09-20 13:09:35 -04001754 if (!info->attrs[NL80211_ATTR_IFNAME])
1755 return -EINVAL;
1756
1757 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1758 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1759 if (type > NL80211_IFTYPE_MAX)
1760 return -EINVAL;
1761 }
1762
Johannes Berg79c97e92009-07-07 03:56:12 +02001763 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001764 !(rdev->wiphy.interface_modes & (1 << type)))
1765 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001766
Johannes Berg9bc383d2009-11-19 11:55:19 +01001767 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001768 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001769 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001770 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001771 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001772 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001773
Michael Wu66f7ac52008-01-31 19:48:22 +01001774 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1775 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1776 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001777 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001778 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001779 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001780 if (IS_ERR(dev))
1781 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001782
Johannes Berg29cbe682010-12-03 09:20:44 +01001783 if (type == NL80211_IFTYPE_MESH_POINT &&
1784 info->attrs[NL80211_ATTR_MESH_ID]) {
1785 struct wireless_dev *wdev = dev->ieee80211_ptr;
1786
1787 wdev_lock(wdev);
1788 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1789 IEEE80211_MAX_MESH_ID_LEN);
1790 wdev->mesh_id_up_len =
1791 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1792 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1793 wdev->mesh_id_up_len);
1794 wdev_unlock(wdev);
1795 }
1796
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001797 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001798}
1799
1800static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1801{
Johannes Berg4c476992010-10-04 21:36:35 +02001802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1803 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001804
Johannes Berg4c476992010-10-04 21:36:35 +02001805 if (!rdev->ops->del_virtual_intf)
1806 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001807
Johannes Berg4c476992010-10-04 21:36:35 +02001808 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001809}
1810
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001811static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1812{
1813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1814 struct net_device *dev = info->user_ptr[1];
1815 u16 noack_map;
1816
1817 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1818 return -EINVAL;
1819
1820 if (!rdev->ops->set_noack_map)
1821 return -EOPNOTSUPP;
1822
1823 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1824
1825 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1826}
1827
Johannes Berg41ade002007-12-19 02:03:29 +01001828struct get_key_cookie {
1829 struct sk_buff *msg;
1830 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001831 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001832};
1833
1834static void get_key_callback(void *c, struct key_params *params)
1835{
Johannes Bergb9454e82009-07-08 13:29:08 +02001836 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001837 struct get_key_cookie *cookie = c;
1838
David S. Miller9360ffd2012-03-29 04:41:26 -04001839 if ((params->key &&
1840 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1841 params->key_len, params->key)) ||
1842 (params->seq &&
1843 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1844 params->seq_len, params->seq)) ||
1845 (params->cipher &&
1846 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1847 params->cipher)))
1848 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001849
Johannes Bergb9454e82009-07-08 13:29:08 +02001850 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1851 if (!key)
1852 goto nla_put_failure;
1853
David S. Miller9360ffd2012-03-29 04:41:26 -04001854 if ((params->key &&
1855 nla_put(cookie->msg, NL80211_KEY_DATA,
1856 params->key_len, params->key)) ||
1857 (params->seq &&
1858 nla_put(cookie->msg, NL80211_KEY_SEQ,
1859 params->seq_len, params->seq)) ||
1860 (params->cipher &&
1861 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
1862 params->cipher)))
1863 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001864
David S. Miller9360ffd2012-03-29 04:41:26 -04001865 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
1866 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001867
1868 nla_nest_end(cookie->msg, key);
1869
Johannes Berg41ade002007-12-19 02:03:29 +01001870 return;
1871 nla_put_failure:
1872 cookie->error = 1;
1873}
1874
1875static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1876{
Johannes Berg4c476992010-10-04 21:36:35 +02001877 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001878 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001879 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001880 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001881 const u8 *mac_addr = NULL;
1882 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001883 struct get_key_cookie cookie = {
1884 .error = 0,
1885 };
1886 void *hdr;
1887 struct sk_buff *msg;
1888
1889 if (info->attrs[NL80211_ATTR_KEY_IDX])
1890 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1891
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001892 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001893 return -EINVAL;
1894
1895 if (info->attrs[NL80211_ATTR_MAC])
1896 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1897
Johannes Berge31b8212010-10-05 19:39:30 +02001898 pairwise = !!mac_addr;
1899 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1900 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1901 if (kt >= NUM_NL80211_KEYTYPES)
1902 return -EINVAL;
1903 if (kt != NL80211_KEYTYPE_GROUP &&
1904 kt != NL80211_KEYTYPE_PAIRWISE)
1905 return -EINVAL;
1906 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1907 }
1908
Johannes Berg4c476992010-10-04 21:36:35 +02001909 if (!rdev->ops->get_key)
1910 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001911
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001912 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001913 if (!msg)
1914 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001915
1916 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1917 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001918 if (IS_ERR(hdr))
1919 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001920
1921 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001922 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001923
David S. Miller9360ffd2012-03-29 04:41:26 -04001924 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1925 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
1926 goto nla_put_failure;
1927 if (mac_addr &&
1928 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
1929 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001930
Johannes Berge31b8212010-10-05 19:39:30 +02001931 if (pairwise && mac_addr &&
1932 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1933 return -ENOENT;
1934
1935 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1936 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001937
1938 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001939 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001940
1941 if (cookie.error)
1942 goto nla_put_failure;
1943
1944 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001945 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001946
1947 nla_put_failure:
1948 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001949 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001950 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001951 return err;
1952}
1953
1954static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1955{
Johannes Berg4c476992010-10-04 21:36:35 +02001956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001957 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001958 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001959 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001960
Johannes Bergb9454e82009-07-08 13:29:08 +02001961 err = nl80211_parse_key(info, &key);
1962 if (err)
1963 return err;
1964
1965 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001966 return -EINVAL;
1967
Johannes Bergb9454e82009-07-08 13:29:08 +02001968 /* only support setting default key */
1969 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001970 return -EINVAL;
1971
Johannes Bergfffd0932009-07-08 14:22:54 +02001972 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001973
1974 if (key.def) {
1975 if (!rdev->ops->set_default_key) {
1976 err = -EOPNOTSUPP;
1977 goto out;
1978 }
1979
1980 err = nl80211_key_allowed(dev->ieee80211_ptr);
1981 if (err)
1982 goto out;
1983
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001984 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1985 key.def_uni, key.def_multi);
1986
1987 if (err)
1988 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001989
Johannes Berg3d23e342009-09-29 23:27:28 +02001990#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001991 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001992#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001993 } else {
1994 if (key.def_uni || !key.def_multi) {
1995 err = -EINVAL;
1996 goto out;
1997 }
1998
1999 if (!rdev->ops->set_default_mgmt_key) {
2000 err = -EOPNOTSUPP;
2001 goto out;
2002 }
2003
2004 err = nl80211_key_allowed(dev->ieee80211_ptr);
2005 if (err)
2006 goto out;
2007
2008 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2009 dev, key.idx);
2010 if (err)
2011 goto out;
2012
2013#ifdef CONFIG_CFG80211_WEXT
2014 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2015#endif
2016 }
2017
2018 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002019 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002020
Johannes Berg41ade002007-12-19 02:03:29 +01002021 return err;
2022}
2023
2024static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2025{
Johannes Berg4c476992010-10-04 21:36:35 +02002026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002027 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002028 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002029 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002030 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002031
Johannes Bergb9454e82009-07-08 13:29:08 +02002032 err = nl80211_parse_key(info, &key);
2033 if (err)
2034 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002035
Johannes Bergb9454e82009-07-08 13:29:08 +02002036 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002037 return -EINVAL;
2038
Johannes Berg41ade002007-12-19 02:03:29 +01002039 if (info->attrs[NL80211_ATTR_MAC])
2040 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2041
Johannes Berge31b8212010-10-05 19:39:30 +02002042 if (key.type == -1) {
2043 if (mac_addr)
2044 key.type = NL80211_KEYTYPE_PAIRWISE;
2045 else
2046 key.type = NL80211_KEYTYPE_GROUP;
2047 }
2048
2049 /* for now */
2050 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2051 key.type != NL80211_KEYTYPE_GROUP)
2052 return -EINVAL;
2053
Johannes Berg4c476992010-10-04 21:36:35 +02002054 if (!rdev->ops->add_key)
2055 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002056
Johannes Berge31b8212010-10-05 19:39:30 +02002057 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2058 key.type == NL80211_KEYTYPE_PAIRWISE,
2059 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002060 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002061
2062 wdev_lock(dev->ieee80211_ptr);
2063 err = nl80211_key_allowed(dev->ieee80211_ptr);
2064 if (!err)
2065 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002066 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002067 mac_addr, &key.p);
2068 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002069
Johannes Berg41ade002007-12-19 02:03:29 +01002070 return err;
2071}
2072
2073static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2074{
Johannes Berg4c476992010-10-04 21:36:35 +02002075 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002076 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002077 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002078 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002079 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002080
Johannes Bergb9454e82009-07-08 13:29:08 +02002081 err = nl80211_parse_key(info, &key);
2082 if (err)
2083 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002084
2085 if (info->attrs[NL80211_ATTR_MAC])
2086 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2087
Johannes Berge31b8212010-10-05 19:39:30 +02002088 if (key.type == -1) {
2089 if (mac_addr)
2090 key.type = NL80211_KEYTYPE_PAIRWISE;
2091 else
2092 key.type = NL80211_KEYTYPE_GROUP;
2093 }
2094
2095 /* for now */
2096 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2097 key.type != NL80211_KEYTYPE_GROUP)
2098 return -EINVAL;
2099
Johannes Berg4c476992010-10-04 21:36:35 +02002100 if (!rdev->ops->del_key)
2101 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002102
Johannes Bergfffd0932009-07-08 14:22:54 +02002103 wdev_lock(dev->ieee80211_ptr);
2104 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002105
2106 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2107 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2108 err = -ENOENT;
2109
Johannes Bergfffd0932009-07-08 14:22:54 +02002110 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002111 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2112 key.type == NL80211_KEYTYPE_PAIRWISE,
2113 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002114
Johannes Berg3d23e342009-09-29 23:27:28 +02002115#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002116 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002117 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002118 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002119 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002120 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2121 }
2122#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002123 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002124
Johannes Berg41ade002007-12-19 02:03:29 +01002125 return err;
2126}
2127
Johannes Berg88600202012-02-13 15:17:18 +01002128static int nl80211_parse_beacon(struct genl_info *info,
2129 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002130{
Johannes Berg88600202012-02-13 15:17:18 +01002131 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002132
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002133 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2134 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2135 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2136 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002137 return -EINVAL;
2138
Johannes Berg88600202012-02-13 15:17:18 +01002139 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002140
Johannes Berged1b6cc2007-12-19 02:03:32 +01002141 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002142 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2143 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2144 if (!bcn->head_len)
2145 return -EINVAL;
2146 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002147 }
2148
2149 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002150 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2151 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002152 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002153 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002154 }
2155
Johannes Berg4c476992010-10-04 21:36:35 +02002156 if (!haveinfo)
2157 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002158
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002159 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002160 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2161 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002162 }
2163
2164 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002165 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002166 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002167 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002168 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2169 }
2170
2171 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002172 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002173 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002174 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002175 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2176 }
2177
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002178 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002179 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002180 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002181 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002182 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2183 }
2184
Johannes Berg88600202012-02-13 15:17:18 +01002185 return 0;
2186}
2187
2188static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2189{
2190 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2191 struct net_device *dev = info->user_ptr[1];
2192 struct wireless_dev *wdev = dev->ieee80211_ptr;
2193 struct cfg80211_ap_settings params;
2194 int err;
2195
2196 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2197 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2198 return -EOPNOTSUPP;
2199
2200 if (!rdev->ops->start_ap)
2201 return -EOPNOTSUPP;
2202
2203 if (wdev->beacon_interval)
2204 return -EALREADY;
2205
2206 memset(&params, 0, sizeof(params));
2207
2208 /* these are required for START_AP */
2209 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2210 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2211 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2212 return -EINVAL;
2213
2214 err = nl80211_parse_beacon(info, &params.beacon);
2215 if (err)
2216 return err;
2217
2218 params.beacon_interval =
2219 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2220 params.dtim_period =
2221 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2222
2223 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2224 if (err)
2225 return err;
2226
2227 /*
2228 * In theory, some of these attributes should be required here
2229 * but since they were not used when the command was originally
2230 * added, keep them optional for old user space programs to let
2231 * them continue to work with drivers that do not need the
2232 * additional information -- drivers must check!
2233 */
2234 if (info->attrs[NL80211_ATTR_SSID]) {
2235 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2236 params.ssid_len =
2237 nla_len(info->attrs[NL80211_ATTR_SSID]);
2238 if (params.ssid_len == 0 ||
2239 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2240 return -EINVAL;
2241 }
2242
2243 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2244 params.hidden_ssid = nla_get_u32(
2245 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2246 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2247 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2248 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2249 return -EINVAL;
2250 }
2251
2252 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2253
2254 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2255 params.auth_type = nla_get_u32(
2256 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2257 if (!nl80211_valid_auth_type(params.auth_type))
2258 return -EINVAL;
2259 } else
2260 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2261
2262 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2263 NL80211_MAX_NR_CIPHER_SUITES);
2264 if (err)
2265 return err;
2266
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302267 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2268 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2269 return -EOPNOTSUPP;
2270 params.inactivity_timeout = nla_get_u16(
2271 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2272 }
2273
Johannes Berg88600202012-02-13 15:17:18 +01002274 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2275 if (!err)
2276 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002277 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002278}
2279
Johannes Berg88600202012-02-13 15:17:18 +01002280static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2281{
2282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2283 struct net_device *dev = info->user_ptr[1];
2284 struct wireless_dev *wdev = dev->ieee80211_ptr;
2285 struct cfg80211_beacon_data params;
2286 int err;
2287
2288 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2289 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2290 return -EOPNOTSUPP;
2291
2292 if (!rdev->ops->change_beacon)
2293 return -EOPNOTSUPP;
2294
2295 if (!wdev->beacon_interval)
2296 return -EINVAL;
2297
2298 err = nl80211_parse_beacon(info, &params);
2299 if (err)
2300 return err;
2301
2302 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2303}
2304
2305static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002306{
Johannes Berg4c476992010-10-04 21:36:35 +02002307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2308 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002309 struct wireless_dev *wdev = dev->ieee80211_ptr;
2310 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002311
Johannes Berg88600202012-02-13 15:17:18 +01002312 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002313 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002314
Johannes Berg074ac8d2010-09-16 14:58:22 +02002315 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002316 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2317 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002318
Johannes Berg88600202012-02-13 15:17:18 +01002319 if (!wdev->beacon_interval)
2320 return -ENOENT;
2321
2322 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002323 if (!err)
2324 wdev->beacon_interval = 0;
2325 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002326}
2327
Johannes Berg5727ef12007-12-19 02:03:34 +01002328static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2329 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2330 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2331 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002332 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002333 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002334 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002335};
2336
Johannes Bergeccb8e82009-05-11 21:57:56 +03002337static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002338 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002339 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002340{
2341 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002342 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002343 int flag;
2344
Johannes Bergeccb8e82009-05-11 21:57:56 +03002345 /*
2346 * Try parsing the new attribute first so userspace
2347 * can specify both for older kernels.
2348 */
2349 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2350 if (nla) {
2351 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002352
Johannes Bergeccb8e82009-05-11 21:57:56 +03002353 sta_flags = nla_data(nla);
2354 params->sta_flags_mask = sta_flags->mask;
2355 params->sta_flags_set = sta_flags->set;
2356 if ((params->sta_flags_mask |
2357 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2358 return -EINVAL;
2359 return 0;
2360 }
2361
2362 /* if present, parse the old attribute */
2363
2364 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002365 if (!nla)
2366 return 0;
2367
2368 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2369 nla, sta_flags_policy))
2370 return -EINVAL;
2371
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002372 /*
2373 * Only allow certain flags for interface types so that
2374 * other attributes are silently ignored. Remember that
2375 * this is backward compatibility code with old userspace
2376 * and shouldn't be hit in other cases anyway.
2377 */
2378 switch (iftype) {
2379 case NL80211_IFTYPE_AP:
2380 case NL80211_IFTYPE_AP_VLAN:
2381 case NL80211_IFTYPE_P2P_GO:
2382 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2383 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2384 BIT(NL80211_STA_FLAG_WME) |
2385 BIT(NL80211_STA_FLAG_MFP);
2386 break;
2387 case NL80211_IFTYPE_P2P_CLIENT:
2388 case NL80211_IFTYPE_STATION:
2389 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2390 BIT(NL80211_STA_FLAG_TDLS_PEER);
2391 break;
2392 case NL80211_IFTYPE_MESH_POINT:
2393 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2394 BIT(NL80211_STA_FLAG_MFP) |
2395 BIT(NL80211_STA_FLAG_AUTHORIZED);
2396 default:
2397 return -EINVAL;
2398 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002399
2400 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2401 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002402 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002403
2404 return 0;
2405}
2406
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002407static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2408 int attr)
2409{
2410 struct nlattr *rate;
2411 u16 bitrate;
2412
2413 rate = nla_nest_start(msg, attr);
2414 if (!rate)
2415 goto nla_put_failure;
2416
2417 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2418 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002419 if ((bitrate > 0 &&
2420 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2421 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2422 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2423 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2424 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2425 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2426 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2427 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002428
2429 nla_nest_end(msg, rate);
2430 return true;
2431
2432nla_put_failure:
2433 return false;
2434}
2435
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002436static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002437 int flags,
2438 struct cfg80211_registered_device *rdev,
2439 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002440 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002441{
2442 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002443 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002444
2445 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2446 if (!hdr)
2447 return -1;
2448
David S. Miller9360ffd2012-03-29 04:41:26 -04002449 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2450 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2451 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2452 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002453
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002454 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2455 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002456 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002457 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2458 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2459 sinfo->connected_time))
2460 goto nla_put_failure;
2461 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2462 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2463 sinfo->inactive_time))
2464 goto nla_put_failure;
2465 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2466 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2467 sinfo->rx_bytes))
2468 goto nla_put_failure;
2469 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2470 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2471 sinfo->tx_bytes))
2472 goto nla_put_failure;
2473 if ((sinfo->filled & STATION_INFO_LLID) &&
2474 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2475 goto nla_put_failure;
2476 if ((sinfo->filled & STATION_INFO_PLID) &&
2477 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2478 goto nla_put_failure;
2479 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2480 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2481 sinfo->plink_state))
2482 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002483 switch (rdev->wiphy.signal_type) {
2484 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002485 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2486 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2487 sinfo->signal))
2488 goto nla_put_failure;
2489 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2490 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2491 sinfo->signal_avg))
2492 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002493 break;
2494 default:
2495 break;
2496 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002497 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002498 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2499 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002500 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002501 }
2502 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2503 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2504 NL80211_STA_INFO_RX_BITRATE))
2505 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002506 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002507 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2508 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2509 sinfo->rx_packets))
2510 goto nla_put_failure;
2511 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2512 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2513 sinfo->tx_packets))
2514 goto nla_put_failure;
2515 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2516 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2517 sinfo->tx_retries))
2518 goto nla_put_failure;
2519 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2520 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2521 sinfo->tx_failed))
2522 goto nla_put_failure;
2523 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2524 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2525 sinfo->beacon_loss_count))
2526 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002527 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2528 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2529 if (!bss_param)
2530 goto nla_put_failure;
2531
David S. Miller9360ffd2012-03-29 04:41:26 -04002532 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2533 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2534 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2535 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2536 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2537 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2538 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2539 sinfo->bss_param.dtim_period) ||
2540 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2541 sinfo->bss_param.beacon_interval))
2542 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002543
2544 nla_nest_end(msg, bss_param);
2545 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002546 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2547 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2548 sizeof(struct nl80211_sta_flag_update),
2549 &sinfo->sta_flags))
2550 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002551 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2552 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2553 sinfo->t_offset))
2554 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002555 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002556
David S. Miller9360ffd2012-03-29 04:41:26 -04002557 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2558 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2559 sinfo->assoc_req_ies))
2560 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002561
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002562 return genlmsg_end(msg, hdr);
2563
2564 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002565 genlmsg_cancel(msg, hdr);
2566 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002567}
2568
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002569static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002570 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002571{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002572 struct station_info sinfo;
2573 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002574 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002575 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002576 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002577 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002578
Johannes Berg67748892010-10-04 21:14:06 +02002579 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2580 if (err)
2581 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002582
2583 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002584 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002585 goto out_err;
2586 }
2587
Johannes Bergbba95fe2008-07-29 13:22:51 +02002588 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002589 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002590 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2591 mac_addr, &sinfo);
2592 if (err == -ENOENT)
2593 break;
2594 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002595 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002596
2597 if (nl80211_send_station(skb,
2598 NETLINK_CB(cb->skb).pid,
2599 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002600 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002601 &sinfo) < 0)
2602 goto out;
2603
2604 sta_idx++;
2605 }
2606
2607
2608 out:
2609 cb->args[1] = sta_idx;
2610 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002611 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002612 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002613
2614 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002615}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002616
Johannes Berg5727ef12007-12-19 02:03:34 +01002617static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2618{
Johannes Berg4c476992010-10-04 21:36:35 +02002619 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2620 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002621 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002622 struct sk_buff *msg;
2623 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002624 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002625
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002626 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002627
2628 if (!info->attrs[NL80211_ATTR_MAC])
2629 return -EINVAL;
2630
2631 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2632
Johannes Berg4c476992010-10-04 21:36:35 +02002633 if (!rdev->ops->get_station)
2634 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002635
Johannes Berg79c97e92009-07-07 03:56:12 +02002636 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002637 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002638 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002639
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002641 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002642 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002643
2644 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002645 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002646 nlmsg_free(msg);
2647 return -ENOBUFS;
2648 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002649
Johannes Berg4c476992010-10-04 21:36:35 +02002650 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002651}
2652
2653/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002654 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002655 */
Johannes Berg80b99892011-11-18 16:23:01 +01002656static struct net_device *get_vlan(struct genl_info *info,
2657 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002658{
Johannes Berg463d0182009-07-14 00:33:35 +02002659 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002660 struct net_device *v;
2661 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002662
Johannes Berg80b99892011-11-18 16:23:01 +01002663 if (!vlanattr)
2664 return NULL;
2665
2666 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2667 if (!v)
2668 return ERR_PTR(-ENODEV);
2669
2670 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2671 ret = -EINVAL;
2672 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002673 }
Johannes Berg80b99892011-11-18 16:23:01 +01002674
2675 if (!netif_running(v)) {
2676 ret = -ENETDOWN;
2677 goto error;
2678 }
2679
2680 return v;
2681 error:
2682 dev_put(v);
2683 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002684}
2685
2686static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2687{
Johannes Berg4c476992010-10-04 21:36:35 +02002688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002689 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002690 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002691 struct station_parameters params;
2692 u8 *mac_addr = NULL;
2693
2694 memset(&params, 0, sizeof(params));
2695
2696 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002697 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002698
2699 if (info->attrs[NL80211_ATTR_STA_AID])
2700 return -EINVAL;
2701
2702 if (!info->attrs[NL80211_ATTR_MAC])
2703 return -EINVAL;
2704
2705 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2706
2707 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2708 params.supported_rates =
2709 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2710 params.supported_rates_len =
2711 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2712 }
2713
2714 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2715 params.listen_interval =
2716 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2717
Jouni Malinen36aedc902008-08-25 11:58:58 +03002718 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2719 params.ht_capa =
2720 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2721
Johannes Bergbdd90d52011-12-14 12:20:27 +01002722 if (!rdev->ops->change_station)
2723 return -EOPNOTSUPP;
2724
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002725 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002726 return -EINVAL;
2727
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002728 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2729 params.plink_action =
2730 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2731
Javier Cardona9c3990a2011-05-03 16:57:11 -07002732 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2733 params.plink_state =
2734 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2735
Johannes Berga97f4422009-06-18 17:23:43 +02002736 switch (dev->ieee80211_ptr->iftype) {
2737 case NL80211_IFTYPE_AP:
2738 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002739 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002740 /* disallow mesh-specific things */
2741 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002742 return -EINVAL;
2743
2744 /* TDLS can't be set, ... */
2745 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2746 return -EINVAL;
2747 /*
2748 * ... but don't bother the driver with it. This works around
2749 * a hostapd/wpa_supplicant issue -- it always includes the
2750 * TLDS_PEER flag in the mask even for AP mode.
2751 */
2752 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2753
2754 /* accept only the listed bits */
2755 if (params.sta_flags_mask &
2756 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2757 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2758 BIT(NL80211_STA_FLAG_WME) |
2759 BIT(NL80211_STA_FLAG_MFP)))
2760 return -EINVAL;
2761
2762 /* must be last in here for error handling */
2763 params.vlan = get_vlan(info, rdev);
2764 if (IS_ERR(params.vlan))
2765 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002766 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002767 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002768 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002769 /*
2770 * Don't allow userspace to change the TDLS_PEER flag,
2771 * but silently ignore attempts to change it since we
2772 * don't have state here to verify that it doesn't try
2773 * to change the flag.
2774 */
2775 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002776 /* fall through */
2777 case NL80211_IFTYPE_ADHOC:
2778 /* disallow things sta doesn't support */
2779 if (params.plink_action)
2780 return -EINVAL;
2781 if (params.ht_capa)
2782 return -EINVAL;
2783 if (params.listen_interval >= 0)
2784 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002785 /* reject any changes other than AUTHORIZED */
2786 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2787 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002788 break;
2789 case NL80211_IFTYPE_MESH_POINT:
2790 /* disallow things mesh doesn't support */
2791 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002792 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002793 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002794 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002795 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002796 return -EINVAL;
2797 /*
2798 * No special handling for TDLS here -- the userspace
2799 * mesh code doesn't have this bug.
2800 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002801 if (params.sta_flags_mask &
2802 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002803 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002804 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002805 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002806 break;
2807 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002808 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002809 }
2810
Johannes Bergbdd90d52011-12-14 12:20:27 +01002811 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002812
Johannes Berg79c97e92009-07-07 03:56:12 +02002813 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002814
Johannes Berg5727ef12007-12-19 02:03:34 +01002815 if (params.vlan)
2816 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002817
Johannes Berg5727ef12007-12-19 02:03:34 +01002818 return err;
2819}
2820
Eliad Pellerc75786c2011-08-23 14:37:46 +03002821static struct nla_policy
2822nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2823 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2824 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2825};
2826
Johannes Berg5727ef12007-12-19 02:03:34 +01002827static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2828{
Johannes Berg4c476992010-10-04 21:36:35 +02002829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002830 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002831 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002832 struct station_parameters params;
2833 u8 *mac_addr = NULL;
2834
2835 memset(&params, 0, sizeof(params));
2836
2837 if (!info->attrs[NL80211_ATTR_MAC])
2838 return -EINVAL;
2839
Johannes Berg5727ef12007-12-19 02:03:34 +01002840 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2841 return -EINVAL;
2842
2843 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2844 return -EINVAL;
2845
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002846 if (!info->attrs[NL80211_ATTR_STA_AID])
2847 return -EINVAL;
2848
Johannes Berg5727ef12007-12-19 02:03:34 +01002849 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2850 params.supported_rates =
2851 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2852 params.supported_rates_len =
2853 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2854 params.listen_interval =
2855 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002856
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002857 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2858 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2859 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002860
Jouni Malinen36aedc902008-08-25 11:58:58 +03002861 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2862 params.ht_capa =
2863 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002864
Javier Cardona96b78df2011-04-07 15:08:33 -07002865 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2866 params.plink_action =
2867 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2868
Johannes Bergbdd90d52011-12-14 12:20:27 +01002869 if (!rdev->ops->add_station)
2870 return -EOPNOTSUPP;
2871
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002872 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002873 return -EINVAL;
2874
Johannes Bergbdd90d52011-12-14 12:20:27 +01002875 switch (dev->ieee80211_ptr->iftype) {
2876 case NL80211_IFTYPE_AP:
2877 case NL80211_IFTYPE_AP_VLAN:
2878 case NL80211_IFTYPE_P2P_GO:
2879 /* parse WME attributes if sta is WME capable */
2880 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2881 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2882 info->attrs[NL80211_ATTR_STA_WME]) {
2883 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2884 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002885
Johannes Bergbdd90d52011-12-14 12:20:27 +01002886 nla = info->attrs[NL80211_ATTR_STA_WME];
2887 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2888 nl80211_sta_wme_policy);
2889 if (err)
2890 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002891
Johannes Bergbdd90d52011-12-14 12:20:27 +01002892 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2893 params.uapsd_queues =
2894 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2895 if (params.uapsd_queues &
2896 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2897 return -EINVAL;
2898
2899 if (tb[NL80211_STA_WME_MAX_SP])
2900 params.max_sp =
2901 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2902
2903 if (params.max_sp &
2904 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2905 return -EINVAL;
2906
2907 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2908 }
2909 /* TDLS peers cannot be added */
2910 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002911 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002912 /* but don't bother the driver with it */
2913 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002914
Johannes Bergbdd90d52011-12-14 12:20:27 +01002915 /* must be last in here for error handling */
2916 params.vlan = get_vlan(info, rdev);
2917 if (IS_ERR(params.vlan))
2918 return PTR_ERR(params.vlan);
2919 break;
2920 case NL80211_IFTYPE_MESH_POINT:
2921 /* TDLS peers cannot be added */
2922 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002923 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002924 break;
2925 case NL80211_IFTYPE_STATION:
2926 /* Only TDLS peers can be added */
2927 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2928 return -EINVAL;
2929 /* Can only add if TDLS ... */
2930 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2931 return -EOPNOTSUPP;
2932 /* ... with external setup is supported */
2933 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2934 return -EOPNOTSUPP;
2935 break;
2936 default:
2937 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002938 }
2939
Johannes Bergbdd90d52011-12-14 12:20:27 +01002940 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002941
Johannes Berg79c97e92009-07-07 03:56:12 +02002942 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002943
Johannes Berg5727ef12007-12-19 02:03:34 +01002944 if (params.vlan)
2945 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002946 return err;
2947}
2948
2949static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2950{
Johannes Berg4c476992010-10-04 21:36:35 +02002951 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2952 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002953 u8 *mac_addr = NULL;
2954
2955 if (info->attrs[NL80211_ATTR_MAC])
2956 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2957
Johannes Berge80cf852009-05-11 14:43:13 +02002958 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002959 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002960 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002961 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2962 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002963
Johannes Berg4c476992010-10-04 21:36:35 +02002964 if (!rdev->ops->del_station)
2965 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002966
Johannes Berg4c476992010-10-04 21:36:35 +02002967 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002968}
2969
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002970static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2971 int flags, struct net_device *dev,
2972 u8 *dst, u8 *next_hop,
2973 struct mpath_info *pinfo)
2974{
2975 void *hdr;
2976 struct nlattr *pinfoattr;
2977
2978 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2979 if (!hdr)
2980 return -1;
2981
David S. Miller9360ffd2012-03-29 04:41:26 -04002982 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2983 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
2984 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
2985 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
2986 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002987
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002988 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2989 if (!pinfoattr)
2990 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002991 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
2992 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2993 pinfo->frame_qlen))
2994 goto nla_put_failure;
2995 if (((pinfo->filled & MPATH_INFO_SN) &&
2996 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
2997 ((pinfo->filled & MPATH_INFO_METRIC) &&
2998 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
2999 pinfo->metric)) ||
3000 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3001 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3002 pinfo->exptime)) ||
3003 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3004 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3005 pinfo->flags)) ||
3006 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3007 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3008 pinfo->discovery_timeout)) ||
3009 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3010 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3011 pinfo->discovery_retries)))
3012 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003013
3014 nla_nest_end(msg, pinfoattr);
3015
3016 return genlmsg_end(msg, hdr);
3017
3018 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003019 genlmsg_cancel(msg, hdr);
3020 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003021}
3022
3023static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003024 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003025{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003026 struct mpath_info pinfo;
3027 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003028 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003029 u8 dst[ETH_ALEN];
3030 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003031 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003032 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003033
Johannes Berg67748892010-10-04 21:14:06 +02003034 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3035 if (err)
3036 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003037
3038 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003039 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003040 goto out_err;
3041 }
3042
Jouni Malineneec60b02009-03-20 21:21:19 +02003043 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3044 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003045 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003046 }
3047
Johannes Bergbba95fe2008-07-29 13:22:51 +02003048 while (1) {
3049 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3050 dst, next_hop, &pinfo);
3051 if (err == -ENOENT)
3052 break;
3053 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003054 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003055
3056 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3057 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3058 netdev, dst, next_hop,
3059 &pinfo) < 0)
3060 goto out;
3061
3062 path_idx++;
3063 }
3064
3065
3066 out:
3067 cb->args[1] = path_idx;
3068 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003069 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003070 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003071 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003072}
3073
3074static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3075{
Johannes Berg4c476992010-10-04 21:36:35 +02003076 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003077 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003078 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003079 struct mpath_info pinfo;
3080 struct sk_buff *msg;
3081 u8 *dst = NULL;
3082 u8 next_hop[ETH_ALEN];
3083
3084 memset(&pinfo, 0, sizeof(pinfo));
3085
3086 if (!info->attrs[NL80211_ATTR_MAC])
3087 return -EINVAL;
3088
3089 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3090
Johannes Berg4c476992010-10-04 21:36:35 +02003091 if (!rdev->ops->get_mpath)
3092 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003093
Johannes Berg4c476992010-10-04 21:36:35 +02003094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3095 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003096
Johannes Berg79c97e92009-07-07 03:56:12 +02003097 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003098 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003099 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003100
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003101 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003102 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003103 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003104
3105 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003106 dev, dst, next_hop, &pinfo) < 0) {
3107 nlmsg_free(msg);
3108 return -ENOBUFS;
3109 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003110
Johannes Berg4c476992010-10-04 21:36:35 +02003111 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003112}
3113
3114static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3115{
Johannes Berg4c476992010-10-04 21:36:35 +02003116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3117 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003118 u8 *dst = NULL;
3119 u8 *next_hop = NULL;
3120
3121 if (!info->attrs[NL80211_ATTR_MAC])
3122 return -EINVAL;
3123
3124 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3125 return -EINVAL;
3126
3127 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3128 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3129
Johannes Berg4c476992010-10-04 21:36:35 +02003130 if (!rdev->ops->change_mpath)
3131 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003132
Johannes Berg4c476992010-10-04 21:36:35 +02003133 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3134 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003135
Johannes Berg4c476992010-10-04 21:36:35 +02003136 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003137}
Johannes Berg4c476992010-10-04 21:36:35 +02003138
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003139static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3140{
Johannes Berg4c476992010-10-04 21:36:35 +02003141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3142 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003143 u8 *dst = NULL;
3144 u8 *next_hop = NULL;
3145
3146 if (!info->attrs[NL80211_ATTR_MAC])
3147 return -EINVAL;
3148
3149 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3150 return -EINVAL;
3151
3152 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3153 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3154
Johannes Berg4c476992010-10-04 21:36:35 +02003155 if (!rdev->ops->add_mpath)
3156 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003157
Johannes Berg4c476992010-10-04 21:36:35 +02003158 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3159 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003160
Johannes Berg4c476992010-10-04 21:36:35 +02003161 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003162}
3163
3164static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3165{
Johannes Berg4c476992010-10-04 21:36:35 +02003166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3167 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003168 u8 *dst = NULL;
3169
3170 if (info->attrs[NL80211_ATTR_MAC])
3171 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3172
Johannes Berg4c476992010-10-04 21:36:35 +02003173 if (!rdev->ops->del_mpath)
3174 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003175
Johannes Berg4c476992010-10-04 21:36:35 +02003176 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003177}
3178
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003179static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3180{
Johannes Berg4c476992010-10-04 21:36:35 +02003181 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3182 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003183 struct bss_parameters params;
3184
3185 memset(&params, 0, sizeof(params));
3186 /* default to not changing parameters */
3187 params.use_cts_prot = -1;
3188 params.use_short_preamble = -1;
3189 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003190 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003191 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003192
3193 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3194 params.use_cts_prot =
3195 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3196 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3197 params.use_short_preamble =
3198 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3199 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3200 params.use_short_slot_time =
3201 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003202 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3203 params.basic_rates =
3204 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3205 params.basic_rates_len =
3206 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3207 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003208 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3209 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003210 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3211 params.ht_opmode =
3212 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003213
Johannes Berg4c476992010-10-04 21:36:35 +02003214 if (!rdev->ops->change_bss)
3215 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003216
Johannes Berg074ac8d2010-09-16 14:58:22 +02003217 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003218 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3219 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003220
Johannes Berg4c476992010-10-04 21:36:35 +02003221 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003222}
3223
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003224static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003225 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3226 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3227 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3228 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3229 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3230 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3231};
3232
3233static int parse_reg_rule(struct nlattr *tb[],
3234 struct ieee80211_reg_rule *reg_rule)
3235{
3236 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3237 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3238
3239 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3240 return -EINVAL;
3241 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3242 return -EINVAL;
3243 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3244 return -EINVAL;
3245 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3246 return -EINVAL;
3247 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3248 return -EINVAL;
3249
3250 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3251
3252 freq_range->start_freq_khz =
3253 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3254 freq_range->end_freq_khz =
3255 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3256 freq_range->max_bandwidth_khz =
3257 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3258
3259 power_rule->max_eirp =
3260 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3261
3262 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3263 power_rule->max_antenna_gain =
3264 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3265
3266 return 0;
3267}
3268
3269static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3270{
3271 int r;
3272 char *data = NULL;
3273
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003274 /*
3275 * You should only get this when cfg80211 hasn't yet initialized
3276 * completely when built-in to the kernel right between the time
3277 * window between nl80211_init() and regulatory_init(), if that is
3278 * even possible.
3279 */
3280 mutex_lock(&cfg80211_mutex);
3281 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003282 mutex_unlock(&cfg80211_mutex);
3283 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003284 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003285 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003286
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003287 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3288 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003289
3290 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3291
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003292 r = regulatory_hint_user(data);
3293
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003294 return r;
3295}
3296
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003297static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003298 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003299{
Johannes Berg4c476992010-10-04 21:36:35 +02003300 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003301 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003302 struct wireless_dev *wdev = dev->ieee80211_ptr;
3303 struct mesh_config cur_params;
3304 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003305 void *hdr;
3306 struct nlattr *pinfoattr;
3307 struct sk_buff *msg;
3308
Johannes Berg29cbe682010-12-03 09:20:44 +01003309 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3310 return -EOPNOTSUPP;
3311
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003312 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003313 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003314
Johannes Berg29cbe682010-12-03 09:20:44 +01003315 wdev_lock(wdev);
3316 /* If not connected, get default parameters */
3317 if (!wdev->mesh_id_len)
3318 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3319 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003320 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003321 &cur_params);
3322 wdev_unlock(wdev);
3323
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003324 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003325 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003326
3327 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003328 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003329 if (!msg)
3330 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003331 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003332 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003333 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003334 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003335 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003336 if (!pinfoattr)
3337 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003338 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3339 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3340 cur_params.dot11MeshRetryTimeout) ||
3341 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3342 cur_params.dot11MeshConfirmTimeout) ||
3343 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3344 cur_params.dot11MeshHoldingTimeout) ||
3345 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3346 cur_params.dot11MeshMaxPeerLinks) ||
3347 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3348 cur_params.dot11MeshMaxRetries) ||
3349 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3350 cur_params.dot11MeshTTL) ||
3351 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3352 cur_params.element_ttl) ||
3353 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3354 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003355 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3356 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003357 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3358 cur_params.dot11MeshHWMPmaxPREQretries) ||
3359 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3360 cur_params.path_refresh_time) ||
3361 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3362 cur_params.min_discovery_timeout) ||
3363 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3364 cur_params.dot11MeshHWMPactivePathTimeout) ||
3365 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3366 cur_params.dot11MeshHWMPpreqMinInterval) ||
3367 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3368 cur_params.dot11MeshHWMPperrMinInterval) ||
3369 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3370 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3371 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3372 cur_params.dot11MeshHWMPRootMode) ||
3373 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3374 cur_params.dot11MeshHWMPRannInterval) ||
3375 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3376 cur_params.dot11MeshGateAnnouncementProtocol) ||
3377 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3378 cur_params.dot11MeshForwarding) ||
3379 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3380 cur_params.rssi_threshold))
3381 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003382 nla_nest_end(msg, pinfoattr);
3383 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003384 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003385
Johannes Berg3b858752009-03-12 09:55:09 +01003386 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003387 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003388 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003389 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003390 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003391}
3392
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003393static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003394 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3395 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3396 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3397 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3398 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3399 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003400 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003401 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003402 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003403
3404 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3405 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3406 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3407 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3408 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003409 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003410 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003411 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003412 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003413 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003414 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003415 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003416};
3417
Javier Cardonac80d5452010-12-16 17:37:49 -08003418static const struct nla_policy
3419 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003420 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003421 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3422 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003423 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003424 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003425 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003426 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003427};
3428
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003429static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003430 struct mesh_config *cfg,
3431 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003432{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003433 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003434 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003435
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003436#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3437do {\
3438 if (table[attr_num]) {\
3439 cfg->param = nla_fn(table[attr_num]); \
3440 mask |= (1 << (attr_num - 1)); \
3441 } \
3442} while (0);\
3443
3444
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003445 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003446 return -EINVAL;
3447 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003448 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003449 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003450 return -EINVAL;
3451
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003452 /* This makes sure that there aren't more than 32 mesh config
3453 * parameters (otherwise our bitfield scheme would not work.) */
3454 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3455
3456 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003457 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3458 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3459 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3460 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3461 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3462 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3463 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3464 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3465 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3466 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3467 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3468 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003469 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3470 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003471 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3472 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
Javier Cardonad299a1f2012-03-31 11:31:33 -07003473 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
3474 mask, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3475 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003476 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3477 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3478 nla_get_u8);
3479 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3480 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3481 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3482 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3483 nla_get_u16);
3484 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3485 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3486 nla_get_u32);
3487 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3488 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3489 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003490 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3491 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3492 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003493 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3494 dot11MeshHWMPnetDiameterTraversalTime,
3495 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3496 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003497 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3498 dot11MeshHWMPRootMode, mask,
3499 NL80211_MESHCONF_HWMP_ROOTMODE,
3500 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003501 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3502 dot11MeshHWMPRannInterval, mask,
3503 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3504 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003505 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3506 dot11MeshGateAnnouncementProtocol, mask,
3507 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3508 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003509 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3510 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003511 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3512 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003513 if (mask_out)
3514 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003515
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003516 return 0;
3517
3518#undef FILL_IN_MESH_PARAM_IF_SET
3519}
3520
Javier Cardonac80d5452010-12-16 17:37:49 -08003521static int nl80211_parse_mesh_setup(struct genl_info *info,
3522 struct mesh_setup *setup)
3523{
3524 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3525
3526 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3527 return -EINVAL;
3528 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3529 info->attrs[NL80211_ATTR_MESH_SETUP],
3530 nl80211_mesh_setup_params_policy))
3531 return -EINVAL;
3532
Javier Cardonad299a1f2012-03-31 11:31:33 -07003533 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3534 setup->sync_method =
3535 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3536 IEEE80211_SYNC_METHOD_VENDOR :
3537 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3538
Javier Cardonac80d5452010-12-16 17:37:49 -08003539 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3540 setup->path_sel_proto =
3541 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3542 IEEE80211_PATH_PROTOCOL_VENDOR :
3543 IEEE80211_PATH_PROTOCOL_HWMP;
3544
3545 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3546 setup->path_metric =
3547 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3548 IEEE80211_PATH_METRIC_VENDOR :
3549 IEEE80211_PATH_METRIC_AIRTIME;
3550
Javier Cardona581a8b02011-04-07 15:08:27 -07003551
3552 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003553 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003554 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003555 if (!is_valid_ie_attr(ieattr))
3556 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003557 setup->ie = nla_data(ieattr);
3558 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003559 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003560 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3561 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003562
3563 return 0;
3564}
3565
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003566static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003567 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003568{
3569 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3570 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003571 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003572 struct mesh_config cfg;
3573 u32 mask;
3574 int err;
3575
Johannes Berg29cbe682010-12-03 09:20:44 +01003576 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3577 return -EOPNOTSUPP;
3578
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003579 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003580 return -EOPNOTSUPP;
3581
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003582 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003583 if (err)
3584 return err;
3585
Johannes Berg29cbe682010-12-03 09:20:44 +01003586 wdev_lock(wdev);
3587 if (!wdev->mesh_id_len)
3588 err = -ENOLINK;
3589
3590 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003591 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003592 mask, &cfg);
3593
3594 wdev_unlock(wdev);
3595
3596 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003597}
3598
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003599static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3600{
3601 struct sk_buff *msg;
3602 void *hdr = NULL;
3603 struct nlattr *nl_reg_rules;
3604 unsigned int i;
3605 int err = -EINVAL;
3606
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003607 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003608
3609 if (!cfg80211_regdomain)
3610 goto out;
3611
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003612 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003613 if (!msg) {
3614 err = -ENOBUFS;
3615 goto out;
3616 }
3617
3618 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3619 NL80211_CMD_GET_REG);
3620 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003621 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003622
David S. Miller9360ffd2012-03-29 04:41:26 -04003623 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3624 cfg80211_regdomain->alpha2) ||
3625 (cfg80211_regdomain->dfs_region &&
3626 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3627 cfg80211_regdomain->dfs_region)))
3628 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003629
3630 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3631 if (!nl_reg_rules)
3632 goto nla_put_failure;
3633
3634 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3635 struct nlattr *nl_reg_rule;
3636 const struct ieee80211_reg_rule *reg_rule;
3637 const struct ieee80211_freq_range *freq_range;
3638 const struct ieee80211_power_rule *power_rule;
3639
3640 reg_rule = &cfg80211_regdomain->reg_rules[i];
3641 freq_range = &reg_rule->freq_range;
3642 power_rule = &reg_rule->power_rule;
3643
3644 nl_reg_rule = nla_nest_start(msg, i);
3645 if (!nl_reg_rule)
3646 goto nla_put_failure;
3647
David S. Miller9360ffd2012-03-29 04:41:26 -04003648 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3649 reg_rule->flags) ||
3650 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3651 freq_range->start_freq_khz) ||
3652 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3653 freq_range->end_freq_khz) ||
3654 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3655 freq_range->max_bandwidth_khz) ||
3656 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3657 power_rule->max_antenna_gain) ||
3658 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3659 power_rule->max_eirp))
3660 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003661
3662 nla_nest_end(msg, nl_reg_rule);
3663 }
3664
3665 nla_nest_end(msg, nl_reg_rules);
3666
3667 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003668 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003669 goto out;
3670
3671nla_put_failure:
3672 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003673put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003674 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003675 err = -EMSGSIZE;
3676out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003677 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003678 return err;
3679}
3680
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003681static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3682{
3683 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3684 struct nlattr *nl_reg_rule;
3685 char *alpha2 = NULL;
3686 int rem_reg_rules = 0, r = 0;
3687 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003688 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003689 struct ieee80211_regdomain *rd = NULL;
3690
3691 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3692 return -EINVAL;
3693
3694 if (!info->attrs[NL80211_ATTR_REG_RULES])
3695 return -EINVAL;
3696
3697 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3698
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003699 if (info->attrs[NL80211_ATTR_DFS_REGION])
3700 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3701
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003702 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3703 rem_reg_rules) {
3704 num_rules++;
3705 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003706 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003707 }
3708
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003709 mutex_lock(&cfg80211_mutex);
3710
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003711 if (!reg_is_valid_request(alpha2)) {
3712 r = -EINVAL;
3713 goto bad_reg;
3714 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003715
3716 size_of_regd = sizeof(struct ieee80211_regdomain) +
3717 (num_rules * sizeof(struct ieee80211_reg_rule));
3718
3719 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003720 if (!rd) {
3721 r = -ENOMEM;
3722 goto bad_reg;
3723 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003724
3725 rd->n_reg_rules = num_rules;
3726 rd->alpha2[0] = alpha2[0];
3727 rd->alpha2[1] = alpha2[1];
3728
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003729 /*
3730 * Disable DFS master mode if the DFS region was
3731 * not supported or known on this kernel.
3732 */
3733 if (reg_supported_dfs_region(dfs_region))
3734 rd->dfs_region = dfs_region;
3735
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003736 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3737 rem_reg_rules) {
3738 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3739 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3740 reg_rule_policy);
3741 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3742 if (r)
3743 goto bad_reg;
3744
3745 rule_idx++;
3746
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003747 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3748 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003749 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003750 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003751 }
3752
3753 BUG_ON(rule_idx != num_rules);
3754
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003755 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003756
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003757 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003758
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003759 return r;
3760
Johannes Bergd2372b32008-10-24 20:32:20 +02003761 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003762 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003763 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003764 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003765}
3766
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003767static int validate_scan_freqs(struct nlattr *freqs)
3768{
3769 struct nlattr *attr1, *attr2;
3770 int n_channels = 0, tmp1, tmp2;
3771
3772 nla_for_each_nested(attr1, freqs, tmp1) {
3773 n_channels++;
3774 /*
3775 * Some hardware has a limited channel list for
3776 * scanning, and it is pretty much nonsensical
3777 * to scan for a channel twice, so disallow that
3778 * and don't require drivers to check that the
3779 * channel list they get isn't longer than what
3780 * they can scan, as long as they can scan all
3781 * the channels they registered at once.
3782 */
3783 nla_for_each_nested(attr2, freqs, tmp2)
3784 if (attr1 != attr2 &&
3785 nla_get_u32(attr1) == nla_get_u32(attr2))
3786 return 0;
3787 }
3788
3789 return n_channels;
3790}
3791
Johannes Berg2a519312009-02-10 21:25:55 +01003792static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3793{
Johannes Berg4c476992010-10-04 21:36:35 +02003794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3795 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003796 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003797 struct nlattr *attr;
3798 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003799 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003800 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003801
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003802 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3803 return -EINVAL;
3804
Johannes Berg79c97e92009-07-07 03:56:12 +02003805 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003806
Johannes Berg4c476992010-10-04 21:36:35 +02003807 if (!rdev->ops->scan)
3808 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003809
Johannes Berg4c476992010-10-04 21:36:35 +02003810 if (rdev->scan_req)
3811 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003812
3813 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003814 n_channels = validate_scan_freqs(
3815 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003816 if (!n_channels)
3817 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003818 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003819 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003820 n_channels = 0;
3821
Johannes Berg2a519312009-02-10 21:25:55 +01003822 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3823 if (wiphy->bands[band])
3824 n_channels += wiphy->bands[band]->n_channels;
3825 }
3826
3827 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3828 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3829 n_ssids++;
3830
Johannes Berg4c476992010-10-04 21:36:35 +02003831 if (n_ssids > wiphy->max_scan_ssids)
3832 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003833
Jouni Malinen70692ad2009-02-16 19:39:13 +02003834 if (info->attrs[NL80211_ATTR_IE])
3835 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3836 else
3837 ie_len = 0;
3838
Johannes Berg4c476992010-10-04 21:36:35 +02003839 if (ie_len > wiphy->max_scan_ie_len)
3840 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003841
Johannes Berg2a519312009-02-10 21:25:55 +01003842 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003843 + sizeof(*request->ssids) * n_ssids
3844 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003845 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003846 if (!request)
3847 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003848
Johannes Berg2a519312009-02-10 21:25:55 +01003849 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003850 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003851 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003852 if (ie_len) {
3853 if (request->ssids)
3854 request->ie = (void *)(request->ssids + n_ssids);
3855 else
3856 request->ie = (void *)(request->channels + n_channels);
3857 }
Johannes Berg2a519312009-02-10 21:25:55 +01003858
Johannes Berg584991d2009-11-02 13:32:03 +01003859 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003860 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3861 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003862 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003863 struct ieee80211_channel *chan;
3864
3865 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3866
3867 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003868 err = -EINVAL;
3869 goto out_free;
3870 }
Johannes Berg584991d2009-11-02 13:32:03 +01003871
3872 /* ignore disabled channels */
3873 if (chan->flags & IEEE80211_CHAN_DISABLED)
3874 continue;
3875
3876 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003877 i++;
3878 }
3879 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003880 enum ieee80211_band band;
3881
Johannes Berg2a519312009-02-10 21:25:55 +01003882 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003883 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3884 int j;
3885 if (!wiphy->bands[band])
3886 continue;
3887 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003888 struct ieee80211_channel *chan;
3889
3890 chan = &wiphy->bands[band]->channels[j];
3891
3892 if (chan->flags & IEEE80211_CHAN_DISABLED)
3893 continue;
3894
3895 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003896 i++;
3897 }
3898 }
3899 }
3900
Johannes Berg584991d2009-11-02 13:32:03 +01003901 if (!i) {
3902 err = -EINVAL;
3903 goto out_free;
3904 }
3905
3906 request->n_channels = i;
3907
Johannes Berg2a519312009-02-10 21:25:55 +01003908 i = 0;
3909 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3910 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003911 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003912 err = -EINVAL;
3913 goto out_free;
3914 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003915 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003916 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003917 i++;
3918 }
3919 }
3920
Jouni Malinen70692ad2009-02-16 19:39:13 +02003921 if (info->attrs[NL80211_ATTR_IE]) {
3922 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003923 memcpy((void *)request->ie,
3924 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003925 request->ie_len);
3926 }
3927
Johannes Berg34850ab2011-07-18 18:08:35 +02003928 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003929 if (wiphy->bands[i])
3930 request->rates[i] =
3931 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003932
3933 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3934 nla_for_each_nested(attr,
3935 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3936 tmp) {
3937 enum ieee80211_band band = nla_type(attr);
3938
Dan Carpenter84404622011-07-29 11:52:18 +03003939 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003940 err = -EINVAL;
3941 goto out_free;
3942 }
3943 err = ieee80211_get_ratemask(wiphy->bands[band],
3944 nla_data(attr),
3945 nla_len(attr),
3946 &request->rates[band]);
3947 if (err)
3948 goto out_free;
3949 }
3950 }
3951
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303952 request->no_cck =
3953 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3954
Johannes Berg463d0182009-07-14 00:33:35 +02003955 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003956 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003957
Johannes Berg79c97e92009-07-07 03:56:12 +02003958 rdev->scan_req = request;
3959 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003960
Johannes Berg463d0182009-07-14 00:33:35 +02003961 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003962 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003963 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003964 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003965 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003966 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003967 kfree(request);
3968 }
Johannes Berg3b858752009-03-12 09:55:09 +01003969
Johannes Berg2a519312009-02-10 21:25:55 +01003970 return err;
3971}
3972
Luciano Coelho807f8a82011-05-11 17:09:35 +03003973static int nl80211_start_sched_scan(struct sk_buff *skb,
3974 struct genl_info *info)
3975{
3976 struct cfg80211_sched_scan_request *request;
3977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3978 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003979 struct nlattr *attr;
3980 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003981 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003982 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003983 enum ieee80211_band band;
3984 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003985 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003986
3987 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3988 !rdev->ops->sched_scan_start)
3989 return -EOPNOTSUPP;
3990
3991 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3992 return -EINVAL;
3993
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003994 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3995 return -EINVAL;
3996
3997 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3998 if (interval == 0)
3999 return -EINVAL;
4000
Luciano Coelho807f8a82011-05-11 17:09:35 +03004001 wiphy = &rdev->wiphy;
4002
4003 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4004 n_channels = validate_scan_freqs(
4005 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4006 if (!n_channels)
4007 return -EINVAL;
4008 } else {
4009 n_channels = 0;
4010
4011 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4012 if (wiphy->bands[band])
4013 n_channels += wiphy->bands[band]->n_channels;
4014 }
4015
4016 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4017 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4018 tmp)
4019 n_ssids++;
4020
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004021 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004022 return -EINVAL;
4023
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004024 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4025 nla_for_each_nested(attr,
4026 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4027 tmp)
4028 n_match_sets++;
4029
4030 if (n_match_sets > wiphy->max_match_sets)
4031 return -EINVAL;
4032
Luciano Coelho807f8a82011-05-11 17:09:35 +03004033 if (info->attrs[NL80211_ATTR_IE])
4034 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4035 else
4036 ie_len = 0;
4037
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004038 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004039 return -EINVAL;
4040
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004041 mutex_lock(&rdev->sched_scan_mtx);
4042
4043 if (rdev->sched_scan_req) {
4044 err = -EINPROGRESS;
4045 goto out;
4046 }
4047
Luciano Coelho807f8a82011-05-11 17:09:35 +03004048 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004049 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004050 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004051 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004052 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004053 if (!request) {
4054 err = -ENOMEM;
4055 goto out;
4056 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004057
4058 if (n_ssids)
4059 request->ssids = (void *)&request->channels[n_channels];
4060 request->n_ssids = n_ssids;
4061 if (ie_len) {
4062 if (request->ssids)
4063 request->ie = (void *)(request->ssids + n_ssids);
4064 else
4065 request->ie = (void *)(request->channels + n_channels);
4066 }
4067
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004068 if (n_match_sets) {
4069 if (request->ie)
4070 request->match_sets = (void *)(request->ie + ie_len);
4071 else if (request->ssids)
4072 request->match_sets =
4073 (void *)(request->ssids + n_ssids);
4074 else
4075 request->match_sets =
4076 (void *)(request->channels + n_channels);
4077 }
4078 request->n_match_sets = n_match_sets;
4079
Luciano Coelho807f8a82011-05-11 17:09:35 +03004080 i = 0;
4081 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4082 /* user specified, bail out if channel not found */
4083 nla_for_each_nested(attr,
4084 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4085 tmp) {
4086 struct ieee80211_channel *chan;
4087
4088 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4089
4090 if (!chan) {
4091 err = -EINVAL;
4092 goto out_free;
4093 }
4094
4095 /* ignore disabled channels */
4096 if (chan->flags & IEEE80211_CHAN_DISABLED)
4097 continue;
4098
4099 request->channels[i] = chan;
4100 i++;
4101 }
4102 } else {
4103 /* all channels */
4104 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4105 int j;
4106 if (!wiphy->bands[band])
4107 continue;
4108 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4109 struct ieee80211_channel *chan;
4110
4111 chan = &wiphy->bands[band]->channels[j];
4112
4113 if (chan->flags & IEEE80211_CHAN_DISABLED)
4114 continue;
4115
4116 request->channels[i] = chan;
4117 i++;
4118 }
4119 }
4120 }
4121
4122 if (!i) {
4123 err = -EINVAL;
4124 goto out_free;
4125 }
4126
4127 request->n_channels = i;
4128
4129 i = 0;
4130 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4131 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4132 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004133 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004134 err = -EINVAL;
4135 goto out_free;
4136 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004137 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004138 memcpy(request->ssids[i].ssid, nla_data(attr),
4139 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004140 i++;
4141 }
4142 }
4143
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004144 i = 0;
4145 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4146 nla_for_each_nested(attr,
4147 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4148 tmp) {
4149 struct nlattr *ssid;
4150
4151 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4152 nla_data(attr), nla_len(attr),
4153 nl80211_match_policy);
4154 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4155 if (ssid) {
4156 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4157 err = -EINVAL;
4158 goto out_free;
4159 }
4160 memcpy(request->match_sets[i].ssid.ssid,
4161 nla_data(ssid), nla_len(ssid));
4162 request->match_sets[i].ssid.ssid_len =
4163 nla_len(ssid);
4164 }
4165 i++;
4166 }
4167 }
4168
Luciano Coelho807f8a82011-05-11 17:09:35 +03004169 if (info->attrs[NL80211_ATTR_IE]) {
4170 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4171 memcpy((void *)request->ie,
4172 nla_data(info->attrs[NL80211_ATTR_IE]),
4173 request->ie_len);
4174 }
4175
4176 request->dev = dev;
4177 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004178 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004179
4180 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4181 if (!err) {
4182 rdev->sched_scan_req = request;
4183 nl80211_send_sched_scan(rdev, dev,
4184 NL80211_CMD_START_SCHED_SCAN);
4185 goto out;
4186 }
4187
4188out_free:
4189 kfree(request);
4190out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004191 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004192 return err;
4193}
4194
4195static int nl80211_stop_sched_scan(struct sk_buff *skb,
4196 struct genl_info *info)
4197{
4198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004199 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004200
4201 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4202 !rdev->ops->sched_scan_stop)
4203 return -EOPNOTSUPP;
4204
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004205 mutex_lock(&rdev->sched_scan_mtx);
4206 err = __cfg80211_stop_sched_scan(rdev, false);
4207 mutex_unlock(&rdev->sched_scan_mtx);
4208
4209 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004210}
4211
Johannes Berg9720bb32011-06-21 09:45:33 +02004212static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4213 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004214 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004215 struct wireless_dev *wdev,
4216 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004217{
Johannes Berg48ab9052009-07-10 18:42:31 +02004218 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004219 void *hdr;
4220 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004221
4222 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004223
Johannes Berg9720bb32011-06-21 09:45:33 +02004224 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004225 NL80211_CMD_NEW_SCAN_RESULTS);
4226 if (!hdr)
4227 return -1;
4228
Johannes Berg9720bb32011-06-21 09:45:33 +02004229 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4230
David S. Miller9360ffd2012-03-29 04:41:26 -04004231 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4232 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4233 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004234
4235 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4236 if (!bss)
4237 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004238 if ((!is_zero_ether_addr(res->bssid) &&
4239 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4240 (res->information_elements && res->len_information_elements &&
4241 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4242 res->len_information_elements,
4243 res->information_elements)) ||
4244 (res->beacon_ies && res->len_beacon_ies &&
4245 res->beacon_ies != res->information_elements &&
4246 nla_put(msg, NL80211_BSS_BEACON_IES,
4247 res->len_beacon_ies, res->beacon_ies)))
4248 goto nla_put_failure;
4249 if (res->tsf &&
4250 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4251 goto nla_put_failure;
4252 if (res->beacon_interval &&
4253 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4254 goto nla_put_failure;
4255 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4256 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4257 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4258 jiffies_to_msecs(jiffies - intbss->ts)))
4259 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004260
Johannes Berg77965c92009-02-18 18:45:06 +01004261 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004262 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004263 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4264 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004265 break;
4266 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004267 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4268 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004269 break;
4270 default:
4271 break;
4272 }
4273
Johannes Berg48ab9052009-07-10 18:42:31 +02004274 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004275 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004276 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004277 if (intbss == wdev->current_bss &&
4278 nla_put_u32(msg, NL80211_BSS_STATUS,
4279 NL80211_BSS_STATUS_ASSOCIATED))
4280 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004281 break;
4282 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004283 if (intbss == wdev->current_bss &&
4284 nla_put_u32(msg, NL80211_BSS_STATUS,
4285 NL80211_BSS_STATUS_IBSS_JOINED))
4286 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004287 break;
4288 default:
4289 break;
4290 }
4291
Johannes Berg2a519312009-02-10 21:25:55 +01004292 nla_nest_end(msg, bss);
4293
4294 return genlmsg_end(msg, hdr);
4295
4296 nla_put_failure:
4297 genlmsg_cancel(msg, hdr);
4298 return -EMSGSIZE;
4299}
4300
4301static int nl80211_dump_scan(struct sk_buff *skb,
4302 struct netlink_callback *cb)
4303{
Johannes Berg48ab9052009-07-10 18:42:31 +02004304 struct cfg80211_registered_device *rdev;
4305 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004306 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004307 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004308 int start = cb->args[1], idx = 0;
4309 int err;
4310
Johannes Berg67748892010-10-04 21:14:06 +02004311 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4312 if (err)
4313 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004314
Johannes Berg48ab9052009-07-10 18:42:31 +02004315 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004316
Johannes Berg48ab9052009-07-10 18:42:31 +02004317 wdev_lock(wdev);
4318 spin_lock_bh(&rdev->bss_lock);
4319 cfg80211_bss_expire(rdev);
4320
Johannes Berg9720bb32011-06-21 09:45:33 +02004321 cb->seq = rdev->bss_generation;
4322
Johannes Berg48ab9052009-07-10 18:42:31 +02004323 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004324 if (++idx <= start)
4325 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004326 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004327 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004328 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004329 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004330 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004331 }
4332 }
4333
Johannes Berg48ab9052009-07-10 18:42:31 +02004334 spin_unlock_bh(&rdev->bss_lock);
4335 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004336
4337 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004338 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004339
Johannes Berg67748892010-10-04 21:14:06 +02004340 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004341}
4342
Holger Schurig61fa7132009-11-11 12:25:40 +01004343static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4344 int flags, struct net_device *dev,
4345 struct survey_info *survey)
4346{
4347 void *hdr;
4348 struct nlattr *infoattr;
4349
Holger Schurig61fa7132009-11-11 12:25:40 +01004350 hdr = nl80211hdr_put(msg, pid, seq, flags,
4351 NL80211_CMD_NEW_SURVEY_RESULTS);
4352 if (!hdr)
4353 return -ENOMEM;
4354
David S. Miller9360ffd2012-03-29 04:41:26 -04004355 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4356 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004357
4358 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4359 if (!infoattr)
4360 goto nla_put_failure;
4361
David S. Miller9360ffd2012-03-29 04:41:26 -04004362 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4363 survey->channel->center_freq))
4364 goto nla_put_failure;
4365
4366 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4367 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4368 goto nla_put_failure;
4369 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4370 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4371 goto nla_put_failure;
4372 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4373 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4374 survey->channel_time))
4375 goto nla_put_failure;
4376 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4377 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4378 survey->channel_time_busy))
4379 goto nla_put_failure;
4380 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4381 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4382 survey->channel_time_ext_busy))
4383 goto nla_put_failure;
4384 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4385 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4386 survey->channel_time_rx))
4387 goto nla_put_failure;
4388 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4389 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4390 survey->channel_time_tx))
4391 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004392
4393 nla_nest_end(msg, infoattr);
4394
4395 return genlmsg_end(msg, hdr);
4396
4397 nla_put_failure:
4398 genlmsg_cancel(msg, hdr);
4399 return -EMSGSIZE;
4400}
4401
4402static int nl80211_dump_survey(struct sk_buff *skb,
4403 struct netlink_callback *cb)
4404{
4405 struct survey_info survey;
4406 struct cfg80211_registered_device *dev;
4407 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004408 int survey_idx = cb->args[1];
4409 int res;
4410
Johannes Berg67748892010-10-04 21:14:06 +02004411 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4412 if (res)
4413 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004414
4415 if (!dev->ops->dump_survey) {
4416 res = -EOPNOTSUPP;
4417 goto out_err;
4418 }
4419
4420 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004421 struct ieee80211_channel *chan;
4422
Holger Schurig61fa7132009-11-11 12:25:40 +01004423 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4424 &survey);
4425 if (res == -ENOENT)
4426 break;
4427 if (res)
4428 goto out_err;
4429
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004430 /* Survey without a channel doesn't make sense */
4431 if (!survey.channel) {
4432 res = -EINVAL;
4433 goto out;
4434 }
4435
4436 chan = ieee80211_get_channel(&dev->wiphy,
4437 survey.channel->center_freq);
4438 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4439 survey_idx++;
4440 continue;
4441 }
4442
Holger Schurig61fa7132009-11-11 12:25:40 +01004443 if (nl80211_send_survey(skb,
4444 NETLINK_CB(cb->skb).pid,
4445 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4446 netdev,
4447 &survey) < 0)
4448 goto out;
4449 survey_idx++;
4450 }
4451
4452 out:
4453 cb->args[1] = survey_idx;
4454 res = skb->len;
4455 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004456 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004457 return res;
4458}
4459
Jouni Malinen255e7372009-03-20 21:21:17 +02004460static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4461{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004462 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004463}
4464
Samuel Ortizb23aa672009-07-01 21:26:54 +02004465static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4466{
4467 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4468 NL80211_WPA_VERSION_2));
4469}
4470
Jouni Malinen636a5d32009-03-19 13:39:22 +02004471static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4472{
Johannes Berg4c476992010-10-04 21:36:35 +02004473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4474 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004475 struct ieee80211_channel *chan;
4476 const u8 *bssid, *ssid, *ie = NULL;
4477 int err, ssid_len, ie_len = 0;
4478 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004479 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004480 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004481
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004482 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4483 return -EINVAL;
4484
4485 if (!info->attrs[NL80211_ATTR_MAC])
4486 return -EINVAL;
4487
Jouni Malinen17780922009-03-27 20:52:47 +02004488 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4489 return -EINVAL;
4490
Johannes Berg19957bb2009-07-02 17:20:43 +02004491 if (!info->attrs[NL80211_ATTR_SSID])
4492 return -EINVAL;
4493
4494 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4495 return -EINVAL;
4496
Johannes Bergfffd0932009-07-08 14:22:54 +02004497 err = nl80211_parse_key(info, &key);
4498 if (err)
4499 return err;
4500
4501 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004502 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4503 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004504 if (!key.p.key || !key.p.key_len)
4505 return -EINVAL;
4506 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4507 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4508 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4509 key.p.key_len != WLAN_KEY_LEN_WEP104))
4510 return -EINVAL;
4511 if (key.idx > 4)
4512 return -EINVAL;
4513 } else {
4514 key.p.key_len = 0;
4515 key.p.key = NULL;
4516 }
4517
Johannes Bergafea0b72010-08-10 09:46:42 +02004518 if (key.idx >= 0) {
4519 int i;
4520 bool ok = false;
4521 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4522 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4523 ok = true;
4524 break;
4525 }
4526 }
Johannes Berg4c476992010-10-04 21:36:35 +02004527 if (!ok)
4528 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004529 }
4530
Johannes Berg4c476992010-10-04 21:36:35 +02004531 if (!rdev->ops->auth)
4532 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004533
Johannes Berg074ac8d2010-09-16 14:58:22 +02004534 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004535 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4536 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004537
Johannes Berg19957bb2009-07-02 17:20:43 +02004538 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004539 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004540 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004541 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4542 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004543
Johannes Berg19957bb2009-07-02 17:20:43 +02004544 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4545 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4546
4547 if (info->attrs[NL80211_ATTR_IE]) {
4548 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4549 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4550 }
4551
4552 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004553 if (!nl80211_valid_auth_type(auth_type))
4554 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004555
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004556 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4557
Johannes Berg95de8172012-01-20 13:55:25 +01004558 /*
4559 * Since we no longer track auth state, ignore
4560 * requests to only change local state.
4561 */
4562 if (local_state_change)
4563 return 0;
4564
Johannes Berg4c476992010-10-04 21:36:35 +02004565 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4566 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004567 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004568}
4569
Johannes Bergc0692b82010-08-27 14:26:53 +03004570static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4571 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004572 struct cfg80211_crypto_settings *settings,
4573 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004574{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004575 memset(settings, 0, sizeof(*settings));
4576
Samuel Ortizb23aa672009-07-01 21:26:54 +02004577 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4578
Johannes Bergc0692b82010-08-27 14:26:53 +03004579 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4580 u16 proto;
4581 proto = nla_get_u16(
4582 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4583 settings->control_port_ethertype = cpu_to_be16(proto);
4584 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4585 proto != ETH_P_PAE)
4586 return -EINVAL;
4587 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4588 settings->control_port_no_encrypt = true;
4589 } else
4590 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4591
Samuel Ortizb23aa672009-07-01 21:26:54 +02004592 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4593 void *data;
4594 int len, i;
4595
4596 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4597 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4598 settings->n_ciphers_pairwise = len / sizeof(u32);
4599
4600 if (len % sizeof(u32))
4601 return -EINVAL;
4602
Johannes Berg3dc27d22009-07-02 21:36:37 +02004603 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004604 return -EINVAL;
4605
4606 memcpy(settings->ciphers_pairwise, data, len);
4607
4608 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004609 if (!cfg80211_supported_cipher_suite(
4610 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004611 settings->ciphers_pairwise[i]))
4612 return -EINVAL;
4613 }
4614
4615 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4616 settings->cipher_group =
4617 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004618 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4619 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004620 return -EINVAL;
4621 }
4622
4623 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4624 settings->wpa_versions =
4625 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4626 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4627 return -EINVAL;
4628 }
4629
4630 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4631 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004632 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004633
4634 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4635 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4636 settings->n_akm_suites = len / sizeof(u32);
4637
4638 if (len % sizeof(u32))
4639 return -EINVAL;
4640
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004641 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4642 return -EINVAL;
4643
Samuel Ortizb23aa672009-07-01 21:26:54 +02004644 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004645 }
4646
4647 return 0;
4648}
4649
Jouni Malinen636a5d32009-03-19 13:39:22 +02004650static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4651{
Johannes Berg4c476992010-10-04 21:36:35 +02004652 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4653 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004654 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004655 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004656 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004657 int err, ssid_len, ie_len = 0;
4658 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004659 u32 flags = 0;
4660 struct ieee80211_ht_cap *ht_capa = NULL;
4661 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004662
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004663 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4664 return -EINVAL;
4665
4666 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004667 !info->attrs[NL80211_ATTR_SSID] ||
4668 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004669 return -EINVAL;
4670
Johannes Berg4c476992010-10-04 21:36:35 +02004671 if (!rdev->ops->assoc)
4672 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004673
Johannes Berg074ac8d2010-09-16 14:58:22 +02004674 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004675 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4676 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004677
Johannes Berg19957bb2009-07-02 17:20:43 +02004678 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004679
Johannes Berg19957bb2009-07-02 17:20:43 +02004680 chan = ieee80211_get_channel(&rdev->wiphy,
4681 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004682 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4683 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004684
Johannes Berg19957bb2009-07-02 17:20:43 +02004685 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4686 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004687
4688 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004689 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4690 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004691 }
4692
Jouni Malinendc6382c2009-05-06 22:09:37 +03004693 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004694 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004695 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004696 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004697 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004698 else if (mfp != NL80211_MFP_NO)
4699 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004700 }
4701
Johannes Berg3e5d7642009-07-07 14:37:26 +02004702 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4703 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4704
Ben Greear7e7c8922011-11-18 11:31:59 -08004705 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4706 flags |= ASSOC_REQ_DISABLE_HT;
4707
4708 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4709 ht_capa_mask =
4710 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4711
4712 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4713 if (!ht_capa_mask)
4714 return -EINVAL;
4715 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4716 }
4717
Johannes Bergc0692b82010-08-27 14:26:53 +03004718 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004719 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004720 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4721 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004722 &crypto, flags, ht_capa,
4723 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004724
Jouni Malinen636a5d32009-03-19 13:39:22 +02004725 return err;
4726}
4727
4728static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4729{
Johannes Berg4c476992010-10-04 21:36:35 +02004730 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4731 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004732 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004733 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004734 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004735 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004736
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004737 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4738 return -EINVAL;
4739
4740 if (!info->attrs[NL80211_ATTR_MAC])
4741 return -EINVAL;
4742
4743 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4744 return -EINVAL;
4745
Johannes Berg4c476992010-10-04 21:36:35 +02004746 if (!rdev->ops->deauth)
4747 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004748
Johannes Berg074ac8d2010-09-16 14:58:22 +02004749 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004750 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4751 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004752
Johannes Berg19957bb2009-07-02 17:20:43 +02004753 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004754
Johannes Berg19957bb2009-07-02 17:20:43 +02004755 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4756 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004757 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004758 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004759 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004760
4761 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004762 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4763 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004764 }
4765
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004766 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4767
Johannes Berg4c476992010-10-04 21:36:35 +02004768 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4769 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004770}
4771
4772static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4773{
Johannes Berg4c476992010-10-04 21:36:35 +02004774 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4775 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004776 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004777 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004778 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004779 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004780
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004781 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4782 return -EINVAL;
4783
4784 if (!info->attrs[NL80211_ATTR_MAC])
4785 return -EINVAL;
4786
4787 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4788 return -EINVAL;
4789
Johannes Berg4c476992010-10-04 21:36:35 +02004790 if (!rdev->ops->disassoc)
4791 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004792
Johannes Berg074ac8d2010-09-16 14:58:22 +02004793 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004794 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4795 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004796
Johannes Berg19957bb2009-07-02 17:20:43 +02004797 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004798
Johannes Berg19957bb2009-07-02 17:20:43 +02004799 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4800 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004801 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004802 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004803 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004804
4805 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004806 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4807 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004808 }
4809
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004810 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4811
Johannes Berg4c476992010-10-04 21:36:35 +02004812 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4813 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004814}
4815
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004816static bool
4817nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4818 int mcast_rate[IEEE80211_NUM_BANDS],
4819 int rateval)
4820{
4821 struct wiphy *wiphy = &rdev->wiphy;
4822 bool found = false;
4823 int band, i;
4824
4825 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4826 struct ieee80211_supported_band *sband;
4827
4828 sband = wiphy->bands[band];
4829 if (!sband)
4830 continue;
4831
4832 for (i = 0; i < sband->n_bitrates; i++) {
4833 if (sband->bitrates[i].bitrate == rateval) {
4834 mcast_rate[band] = i + 1;
4835 found = true;
4836 break;
4837 }
4838 }
4839 }
4840
4841 return found;
4842}
4843
Johannes Berg04a773a2009-04-19 21:24:32 +02004844static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4845{
Johannes Berg4c476992010-10-04 21:36:35 +02004846 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4847 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004848 struct cfg80211_ibss_params ibss;
4849 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004850 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004851 int err;
4852
Johannes Berg8e30bc52009-04-22 17:45:38 +02004853 memset(&ibss, 0, sizeof(ibss));
4854
Johannes Berg04a773a2009-04-19 21:24:32 +02004855 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4856 return -EINVAL;
4857
4858 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4859 !info->attrs[NL80211_ATTR_SSID] ||
4860 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4861 return -EINVAL;
4862
Johannes Berg8e30bc52009-04-22 17:45:38 +02004863 ibss.beacon_interval = 100;
4864
4865 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4866 ibss.beacon_interval =
4867 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4868 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4869 return -EINVAL;
4870 }
4871
Johannes Berg4c476992010-10-04 21:36:35 +02004872 if (!rdev->ops->join_ibss)
4873 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004874
Johannes Berg4c476992010-10-04 21:36:35 +02004875 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4876 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004877
Johannes Berg79c97e92009-07-07 03:56:12 +02004878 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004879
Johannes Berg39193492011-09-16 13:45:25 +02004880 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004881 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004882
4883 if (!is_valid_ether_addr(ibss.bssid))
4884 return -EINVAL;
4885 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004886 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4887 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4888
4889 if (info->attrs[NL80211_ATTR_IE]) {
4890 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4891 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4892 }
4893
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004894 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4895 enum nl80211_channel_type channel_type;
4896
4897 channel_type = nla_get_u32(
4898 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4899 if (channel_type != NL80211_CHAN_NO_HT &&
4900 channel_type != NL80211_CHAN_HT20 &&
4901 channel_type != NL80211_CHAN_HT40MINUS &&
4902 channel_type != NL80211_CHAN_HT40PLUS)
4903 return -EINVAL;
4904
4905 if (channel_type != NL80211_CHAN_NO_HT &&
4906 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4907 return -EINVAL;
4908
4909 ibss.channel_type = channel_type;
4910 } else {
4911 ibss.channel_type = NL80211_CHAN_NO_HT;
4912 }
4913
4914 ibss.channel = rdev_freq_to_chan(rdev,
4915 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4916 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004917 if (!ibss.channel ||
4918 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004919 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4920 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004921
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004922 /* Both channels should be able to initiate communication */
4923 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4924 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4925 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4926 ibss.channel_type))
4927 return -EINVAL;
4928
Johannes Berg04a773a2009-04-19 21:24:32 +02004929 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004930 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004931
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004932 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4933 u8 *rates =
4934 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4935 int n_rates =
4936 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4937 struct ieee80211_supported_band *sband =
4938 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004939
Johannes Berg34850ab2011-07-18 18:08:35 +02004940 err = ieee80211_get_ratemask(sband, rates, n_rates,
4941 &ibss.basic_rates);
4942 if (err)
4943 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004944 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004945
4946 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4947 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4948 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4949 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004950
Johannes Berg4c476992010-10-04 21:36:35 +02004951 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4952 connkeys = nl80211_parse_connkeys(rdev,
4953 info->attrs[NL80211_ATTR_KEYS]);
4954 if (IS_ERR(connkeys))
4955 return PTR_ERR(connkeys);
4956 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004957
Antonio Quartulli267335d2012-01-31 20:25:47 +01004958 ibss.control_port =
4959 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4960
Johannes Berg4c476992010-10-04 21:36:35 +02004961 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004962 if (err)
4963 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004964 return err;
4965}
4966
4967static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4968{
Johannes Berg4c476992010-10-04 21:36:35 +02004969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4970 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004971
Johannes Berg4c476992010-10-04 21:36:35 +02004972 if (!rdev->ops->leave_ibss)
4973 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004974
Johannes Berg4c476992010-10-04 21:36:35 +02004975 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4976 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004977
Johannes Berg4c476992010-10-04 21:36:35 +02004978 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004979}
4980
Johannes Bergaff89a92009-07-01 21:26:51 +02004981#ifdef CONFIG_NL80211_TESTMODE
4982static struct genl_multicast_group nl80211_testmode_mcgrp = {
4983 .name = "testmode",
4984};
4985
4986static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4987{
Johannes Berg4c476992010-10-04 21:36:35 +02004988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004989 int err;
4990
4991 if (!info->attrs[NL80211_ATTR_TESTDATA])
4992 return -EINVAL;
4993
Johannes Bergaff89a92009-07-01 21:26:51 +02004994 err = -EOPNOTSUPP;
4995 if (rdev->ops->testmode_cmd) {
4996 rdev->testmode_info = info;
4997 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4998 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4999 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5000 rdev->testmode_info = NULL;
5001 }
5002
Johannes Bergaff89a92009-07-01 21:26:51 +02005003 return err;
5004}
5005
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005006static int nl80211_testmode_dump(struct sk_buff *skb,
5007 struct netlink_callback *cb)
5008{
Johannes Berg00918d32011-12-13 17:22:05 +01005009 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005010 int err;
5011 long phy_idx;
5012 void *data = NULL;
5013 int data_len = 0;
5014
5015 if (cb->args[0]) {
5016 /*
5017 * 0 is a valid index, but not valid for args[0],
5018 * so we need to offset by 1.
5019 */
5020 phy_idx = cb->args[0] - 1;
5021 } else {
5022 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5023 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5024 nl80211_policy);
5025 if (err)
5026 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005027 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5028 phy_idx = nla_get_u32(
5029 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5030 } else {
5031 struct net_device *netdev;
5032
5033 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5034 nl80211_fam.attrbuf,
5035 &rdev, &netdev);
5036 if (err)
5037 return err;
5038 dev_put(netdev);
5039 phy_idx = rdev->wiphy_idx;
5040 cfg80211_unlock_rdev(rdev);
5041 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005042 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5043 cb->args[1] =
5044 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5045 }
5046
5047 if (cb->args[1]) {
5048 data = nla_data((void *)cb->args[1]);
5049 data_len = nla_len((void *)cb->args[1]);
5050 }
5051
5052 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005053 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5054 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005055 mutex_unlock(&cfg80211_mutex);
5056 return -ENOENT;
5057 }
Johannes Berg00918d32011-12-13 17:22:05 +01005058 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005059 mutex_unlock(&cfg80211_mutex);
5060
Johannes Berg00918d32011-12-13 17:22:05 +01005061 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005062 err = -EOPNOTSUPP;
5063 goto out_err;
5064 }
5065
5066 while (1) {
5067 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5068 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5069 NL80211_CMD_TESTMODE);
5070 struct nlattr *tmdata;
5071
David S. Miller9360ffd2012-03-29 04:41:26 -04005072 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005073 genlmsg_cancel(skb, hdr);
5074 break;
5075 }
5076
5077 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5078 if (!tmdata) {
5079 genlmsg_cancel(skb, hdr);
5080 break;
5081 }
Johannes Berg00918d32011-12-13 17:22:05 +01005082 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5083 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005084 nla_nest_end(skb, tmdata);
5085
5086 if (err == -ENOBUFS || err == -ENOENT) {
5087 genlmsg_cancel(skb, hdr);
5088 break;
5089 } else if (err) {
5090 genlmsg_cancel(skb, hdr);
5091 goto out_err;
5092 }
5093
5094 genlmsg_end(skb, hdr);
5095 }
5096
5097 err = skb->len;
5098 /* see above */
5099 cb->args[0] = phy_idx + 1;
5100 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005101 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005102 return err;
5103}
5104
Johannes Bergaff89a92009-07-01 21:26:51 +02005105static struct sk_buff *
5106__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5107 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5108{
5109 struct sk_buff *skb;
5110 void *hdr;
5111 struct nlattr *data;
5112
5113 skb = nlmsg_new(approxlen + 100, gfp);
5114 if (!skb)
5115 return NULL;
5116
5117 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5118 if (!hdr) {
5119 kfree_skb(skb);
5120 return NULL;
5121 }
5122
David S. Miller9360ffd2012-03-29 04:41:26 -04005123 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5124 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005125 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5126
5127 ((void **)skb->cb)[0] = rdev;
5128 ((void **)skb->cb)[1] = hdr;
5129 ((void **)skb->cb)[2] = data;
5130
5131 return skb;
5132
5133 nla_put_failure:
5134 kfree_skb(skb);
5135 return NULL;
5136}
5137
5138struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5139 int approxlen)
5140{
5141 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5142
5143 if (WARN_ON(!rdev->testmode_info))
5144 return NULL;
5145
5146 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5147 rdev->testmode_info->snd_pid,
5148 rdev->testmode_info->snd_seq,
5149 GFP_KERNEL);
5150}
5151EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5152
5153int cfg80211_testmode_reply(struct sk_buff *skb)
5154{
5155 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5156 void *hdr = ((void **)skb->cb)[1];
5157 struct nlattr *data = ((void **)skb->cb)[2];
5158
5159 if (WARN_ON(!rdev->testmode_info)) {
5160 kfree_skb(skb);
5161 return -EINVAL;
5162 }
5163
5164 nla_nest_end(skb, data);
5165 genlmsg_end(skb, hdr);
5166 return genlmsg_reply(skb, rdev->testmode_info);
5167}
5168EXPORT_SYMBOL(cfg80211_testmode_reply);
5169
5170struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5171 int approxlen, gfp_t gfp)
5172{
5173 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5174
5175 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5176}
5177EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5178
5179void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5180{
5181 void *hdr = ((void **)skb->cb)[1];
5182 struct nlattr *data = ((void **)skb->cb)[2];
5183
5184 nla_nest_end(skb, data);
5185 genlmsg_end(skb, hdr);
5186 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5187}
5188EXPORT_SYMBOL(cfg80211_testmode_event);
5189#endif
5190
Samuel Ortizb23aa672009-07-01 21:26:54 +02005191static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5192{
Johannes Berg4c476992010-10-04 21:36:35 +02005193 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5194 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005195 struct cfg80211_connect_params connect;
5196 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005197 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005198 int err;
5199
5200 memset(&connect, 0, sizeof(connect));
5201
5202 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5203 return -EINVAL;
5204
5205 if (!info->attrs[NL80211_ATTR_SSID] ||
5206 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5207 return -EINVAL;
5208
5209 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5210 connect.auth_type =
5211 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5212 if (!nl80211_valid_auth_type(connect.auth_type))
5213 return -EINVAL;
5214 } else
5215 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5216
5217 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5218
Johannes Bergc0692b82010-08-27 14:26:53 +03005219 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005220 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005221 if (err)
5222 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005223
Johannes Berg074ac8d2010-09-16 14:58:22 +02005224 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005225 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5226 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005227
Johannes Berg79c97e92009-07-07 03:56:12 +02005228 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005229
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305230 connect.bg_scan_period = -1;
5231 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5232 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5233 connect.bg_scan_period =
5234 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5235 }
5236
Samuel Ortizb23aa672009-07-01 21:26:54 +02005237 if (info->attrs[NL80211_ATTR_MAC])
5238 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5239 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5240 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5241
5242 if (info->attrs[NL80211_ATTR_IE]) {
5243 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5244 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5245 }
5246
5247 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5248 connect.channel =
5249 ieee80211_get_channel(wiphy,
5250 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5251 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005252 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5253 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005254 }
5255
Johannes Bergfffd0932009-07-08 14:22:54 +02005256 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5257 connkeys = nl80211_parse_connkeys(rdev,
5258 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005259 if (IS_ERR(connkeys))
5260 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005261 }
5262
Ben Greear7e7c8922011-11-18 11:31:59 -08005263 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5264 connect.flags |= ASSOC_REQ_DISABLE_HT;
5265
5266 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5267 memcpy(&connect.ht_capa_mask,
5268 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5269 sizeof(connect.ht_capa_mask));
5270
5271 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5272 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5273 return -EINVAL;
5274 memcpy(&connect.ht_capa,
5275 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5276 sizeof(connect.ht_capa));
5277 }
5278
Johannes Bergfffd0932009-07-08 14:22:54 +02005279 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005280 if (err)
5281 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005282 return err;
5283}
5284
5285static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5286{
Johannes Berg4c476992010-10-04 21:36:35 +02005287 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5288 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005289 u16 reason;
5290
5291 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5292 reason = WLAN_REASON_DEAUTH_LEAVING;
5293 else
5294 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5295
5296 if (reason == 0)
5297 return -EINVAL;
5298
Johannes Berg074ac8d2010-09-16 14:58:22 +02005299 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005300 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5301 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005302
Johannes Berg4c476992010-10-04 21:36:35 +02005303 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005304}
5305
Johannes Berg463d0182009-07-14 00:33:35 +02005306static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5307{
Johannes Berg4c476992010-10-04 21:36:35 +02005308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005309 struct net *net;
5310 int err;
5311 u32 pid;
5312
5313 if (!info->attrs[NL80211_ATTR_PID])
5314 return -EINVAL;
5315
5316 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5317
Johannes Berg463d0182009-07-14 00:33:35 +02005318 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005319 if (IS_ERR(net))
5320 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005321
5322 err = 0;
5323
5324 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005325 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5326 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005327
Johannes Berg463d0182009-07-14 00:33:35 +02005328 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005329 return err;
5330}
5331
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005332static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5333{
Johannes Berg4c476992010-10-04 21:36:35 +02005334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005335 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5336 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005337 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005338 struct cfg80211_pmksa pmksa;
5339
5340 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5341
5342 if (!info->attrs[NL80211_ATTR_MAC])
5343 return -EINVAL;
5344
5345 if (!info->attrs[NL80211_ATTR_PMKID])
5346 return -EINVAL;
5347
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005348 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5349 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5350
Johannes Berg074ac8d2010-09-16 14:58:22 +02005351 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005352 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5353 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005354
5355 switch (info->genlhdr->cmd) {
5356 case NL80211_CMD_SET_PMKSA:
5357 rdev_ops = rdev->ops->set_pmksa;
5358 break;
5359 case NL80211_CMD_DEL_PMKSA:
5360 rdev_ops = rdev->ops->del_pmksa;
5361 break;
5362 default:
5363 WARN_ON(1);
5364 break;
5365 }
5366
Johannes Berg4c476992010-10-04 21:36:35 +02005367 if (!rdev_ops)
5368 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005369
Johannes Berg4c476992010-10-04 21:36:35 +02005370 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005371}
5372
5373static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5374{
Johannes Berg4c476992010-10-04 21:36:35 +02005375 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5376 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005377
Johannes Berg074ac8d2010-09-16 14:58:22 +02005378 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005379 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5380 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005381
Johannes Berg4c476992010-10-04 21:36:35 +02005382 if (!rdev->ops->flush_pmksa)
5383 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005384
Johannes Berg4c476992010-10-04 21:36:35 +02005385 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005386}
5387
Arik Nemtsov109086c2011-09-28 14:12:50 +03005388static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5389{
5390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5391 struct net_device *dev = info->user_ptr[1];
5392 u8 action_code, dialog_token;
5393 u16 status_code;
5394 u8 *peer;
5395
5396 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5397 !rdev->ops->tdls_mgmt)
5398 return -EOPNOTSUPP;
5399
5400 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5401 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5402 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5403 !info->attrs[NL80211_ATTR_IE] ||
5404 !info->attrs[NL80211_ATTR_MAC])
5405 return -EINVAL;
5406
5407 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5408 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5409 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5410 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5411
5412 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5413 dialog_token, status_code,
5414 nla_data(info->attrs[NL80211_ATTR_IE]),
5415 nla_len(info->attrs[NL80211_ATTR_IE]));
5416}
5417
5418static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5419{
5420 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5421 struct net_device *dev = info->user_ptr[1];
5422 enum nl80211_tdls_operation operation;
5423 u8 *peer;
5424
5425 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5426 !rdev->ops->tdls_oper)
5427 return -EOPNOTSUPP;
5428
5429 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5430 !info->attrs[NL80211_ATTR_MAC])
5431 return -EINVAL;
5432
5433 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5434 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5435
5436 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5437}
5438
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005439static int nl80211_remain_on_channel(struct sk_buff *skb,
5440 struct genl_info *info)
5441{
Johannes Berg4c476992010-10-04 21:36:35 +02005442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5443 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005444 struct ieee80211_channel *chan;
5445 struct sk_buff *msg;
5446 void *hdr;
5447 u64 cookie;
5448 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5449 u32 freq, duration;
5450 int err;
5451
5452 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5453 !info->attrs[NL80211_ATTR_DURATION])
5454 return -EINVAL;
5455
5456 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5457
5458 /*
5459 * We should be on that channel for at least one jiffie,
5460 * and more than 5 seconds seems excessive.
5461 */
Johannes Berga2939112010-12-14 17:54:28 +01005462 if (!duration || !msecs_to_jiffies(duration) ||
5463 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005464 return -EINVAL;
5465
Johannes Berg7c4ef712011-11-18 15:33:48 +01005466 if (!rdev->ops->remain_on_channel ||
5467 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005468 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005469
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005470 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5471 channel_type = nla_get_u32(
5472 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5473 if (channel_type != NL80211_CHAN_NO_HT &&
5474 channel_type != NL80211_CHAN_HT20 &&
5475 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005476 channel_type != NL80211_CHAN_HT40MINUS)
5477 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005478 }
5479
5480 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5481 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005482 if (chan == NULL)
5483 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005484
5485 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005486 if (!msg)
5487 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005488
5489 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5490 NL80211_CMD_REMAIN_ON_CHANNEL);
5491
5492 if (IS_ERR(hdr)) {
5493 err = PTR_ERR(hdr);
5494 goto free_msg;
5495 }
5496
5497 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5498 channel_type, duration, &cookie);
5499
5500 if (err)
5501 goto free_msg;
5502
David S. Miller9360ffd2012-03-29 04:41:26 -04005503 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5504 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005505
5506 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005507
5508 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005509
5510 nla_put_failure:
5511 err = -ENOBUFS;
5512 free_msg:
5513 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005514 return err;
5515}
5516
5517static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5518 struct genl_info *info)
5519{
Johannes Berg4c476992010-10-04 21:36:35 +02005520 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5521 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005522 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005523
5524 if (!info->attrs[NL80211_ATTR_COOKIE])
5525 return -EINVAL;
5526
Johannes Berg4c476992010-10-04 21:36:35 +02005527 if (!rdev->ops->cancel_remain_on_channel)
5528 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005529
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005530 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5531
Johannes Berg4c476992010-10-04 21:36:35 +02005532 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005533}
5534
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005535static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5536 u8 *rates, u8 rates_len)
5537{
5538 u8 i;
5539 u32 mask = 0;
5540
5541 for (i = 0; i < rates_len; i++) {
5542 int rate = (rates[i] & 0x7f) * 5;
5543 int ridx;
5544 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5545 struct ieee80211_rate *srate =
5546 &sband->bitrates[ridx];
5547 if (rate == srate->bitrate) {
5548 mask |= 1 << ridx;
5549 break;
5550 }
5551 }
5552 if (ridx == sband->n_bitrates)
5553 return 0; /* rate not found */
5554 }
5555
5556 return mask;
5557}
5558
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005559static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5560 u8 *rates, u8 rates_len,
5561 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5562{
5563 u8 i;
5564
5565 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5566
5567 for (i = 0; i < rates_len; i++) {
5568 int ridx, rbit;
5569
5570 ridx = rates[i] / 8;
5571 rbit = BIT(rates[i] % 8);
5572
5573 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005574 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005575 return false;
5576
5577 /* check availability */
5578 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5579 mcs[ridx] |= rbit;
5580 else
5581 return false;
5582 }
5583
5584 return true;
5585}
5586
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005587static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005588 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5589 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005590 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5591 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005592};
5593
5594static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5595 struct genl_info *info)
5596{
5597 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005599 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005600 int rem, i;
5601 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005602 struct nlattr *tx_rates;
5603 struct ieee80211_supported_band *sband;
5604
5605 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5606 return -EINVAL;
5607
Johannes Berg4c476992010-10-04 21:36:35 +02005608 if (!rdev->ops->set_bitrate_mask)
5609 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005610
5611 memset(&mask, 0, sizeof(mask));
5612 /* Default to all rates enabled */
5613 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5614 sband = rdev->wiphy.bands[i];
5615 mask.control[i].legacy =
5616 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005617 if (sband)
5618 memcpy(mask.control[i].mcs,
5619 sband->ht_cap.mcs.rx_mask,
5620 sizeof(mask.control[i].mcs));
5621 else
5622 memset(mask.control[i].mcs, 0,
5623 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005624 }
5625
5626 /*
5627 * The nested attribute uses enum nl80211_band as the index. This maps
5628 * directly to the enum ieee80211_band values used in cfg80211.
5629 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005630 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005631 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5632 {
5633 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005634 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5635 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005636 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005637 if (sband == NULL)
5638 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005639 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5640 nla_len(tx_rates), nl80211_txattr_policy);
5641 if (tb[NL80211_TXRATE_LEGACY]) {
5642 mask.control[band].legacy = rateset_to_mask(
5643 sband,
5644 nla_data(tb[NL80211_TXRATE_LEGACY]),
5645 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005646 }
5647 if (tb[NL80211_TXRATE_MCS]) {
5648 if (!ht_rateset_to_mask(
5649 sband,
5650 nla_data(tb[NL80211_TXRATE_MCS]),
5651 nla_len(tb[NL80211_TXRATE_MCS]),
5652 mask.control[band].mcs))
5653 return -EINVAL;
5654 }
5655
5656 if (mask.control[band].legacy == 0) {
5657 /* don't allow empty legacy rates if HT
5658 * is not even supported. */
5659 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5660 return -EINVAL;
5661
5662 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5663 if (mask.control[band].mcs[i])
5664 break;
5665
5666 /* legacy and mcs rates may not be both empty */
5667 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005668 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005669 }
5670 }
5671
Johannes Berg4c476992010-10-04 21:36:35 +02005672 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005673}
5674
Johannes Berg2e161f72010-08-12 15:38:38 +02005675static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005676{
Johannes Berg4c476992010-10-04 21:36:35 +02005677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5678 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005679 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005680
5681 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5682 return -EINVAL;
5683
Johannes Berg2e161f72010-08-12 15:38:38 +02005684 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5685 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005686
Johannes Berg9d38d852010-06-09 17:20:33 +02005687 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005688 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005689 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5690 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5691 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005692 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005693 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5694 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005695
5696 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005697 if (!rdev->ops->mgmt_tx)
5698 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005699
Johannes Berg4c476992010-10-04 21:36:35 +02005700 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005701 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005702 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5703 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005704}
5705
Johannes Berg2e161f72010-08-12 15:38:38 +02005706static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005707{
Johannes Berg4c476992010-10-04 21:36:35 +02005708 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5709 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005710 struct ieee80211_channel *chan;
5711 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005712 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005713 u32 freq;
5714 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005715 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005716 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005717 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005718 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005719 bool offchan, no_cck, dont_wait_for_ack;
5720
5721 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005722
5723 if (!info->attrs[NL80211_ATTR_FRAME] ||
5724 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5725 return -EINVAL;
5726
Johannes Berg4c476992010-10-04 21:36:35 +02005727 if (!rdev->ops->mgmt_tx)
5728 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005729
Johannes Berg9d38d852010-06-09 17:20:33 +02005730 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005731 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005732 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5733 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5734 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005735 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005736 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5737 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005738
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005739 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005740 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005741 return -EINVAL;
5742 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5743 }
5744
Jouni Malinen026331c2010-02-15 12:53:10 +02005745 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5746 channel_type = nla_get_u32(
5747 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5748 if (channel_type != NL80211_CHAN_NO_HT &&
5749 channel_type != NL80211_CHAN_HT20 &&
5750 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005751 channel_type != NL80211_CHAN_HT40MINUS)
5752 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005753 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005754 }
5755
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005756 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5757
Johannes Berg7c4ef712011-11-18 15:33:48 +01005758 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5759 return -EINVAL;
5760
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305761 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5762
Jouni Malinen026331c2010-02-15 12:53:10 +02005763 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5764 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005765 if (chan == NULL)
5766 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005767
Johannes Berge247bd902011-11-04 11:18:21 +01005768 if (!dont_wait_for_ack) {
5769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5770 if (!msg)
5771 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005772
Johannes Berge247bd902011-11-04 11:18:21 +01005773 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5774 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005775
Johannes Berge247bd902011-11-04 11:18:21 +01005776 if (IS_ERR(hdr)) {
5777 err = PTR_ERR(hdr);
5778 goto free_msg;
5779 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005780 }
Johannes Berge247bd902011-11-04 11:18:21 +01005781
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005782 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5783 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005784 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5785 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005786 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005787 if (err)
5788 goto free_msg;
5789
Johannes Berge247bd902011-11-04 11:18:21 +01005790 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005791 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5792 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005793
Johannes Berge247bd902011-11-04 11:18:21 +01005794 genlmsg_end(msg, hdr);
5795 return genlmsg_reply(msg, info);
5796 }
5797
5798 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005799
5800 nla_put_failure:
5801 err = -ENOBUFS;
5802 free_msg:
5803 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005804 return err;
5805}
5806
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005807static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5808{
5809 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5810 struct net_device *dev = info->user_ptr[1];
5811 u64 cookie;
5812
5813 if (!info->attrs[NL80211_ATTR_COOKIE])
5814 return -EINVAL;
5815
5816 if (!rdev->ops->mgmt_tx_cancel_wait)
5817 return -EOPNOTSUPP;
5818
5819 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5820 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5821 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5822 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5823 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5824 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5825 return -EOPNOTSUPP;
5826
5827 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5828
5829 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5830}
5831
Kalle Valoffb9eb32010-02-17 17:58:10 +02005832static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5833{
Johannes Berg4c476992010-10-04 21:36:35 +02005834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005835 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005836 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005837 u8 ps_state;
5838 bool state;
5839 int err;
5840
Johannes Berg4c476992010-10-04 21:36:35 +02005841 if (!info->attrs[NL80211_ATTR_PS_STATE])
5842 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005843
5844 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5845
Johannes Berg4c476992010-10-04 21:36:35 +02005846 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5847 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005848
5849 wdev = dev->ieee80211_ptr;
5850
Johannes Berg4c476992010-10-04 21:36:35 +02005851 if (!rdev->ops->set_power_mgmt)
5852 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005853
5854 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5855
5856 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005857 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005858
Johannes Berg4c476992010-10-04 21:36:35 +02005859 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5860 wdev->ps_timeout);
5861 if (!err)
5862 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005863 return err;
5864}
5865
5866static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5867{
Johannes Berg4c476992010-10-04 21:36:35 +02005868 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005869 enum nl80211_ps_state ps_state;
5870 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005871 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005872 struct sk_buff *msg;
5873 void *hdr;
5874 int err;
5875
Kalle Valoffb9eb32010-02-17 17:58:10 +02005876 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 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005882 if (!msg)
5883 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005884
5885 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5886 NL80211_CMD_GET_POWER_SAVE);
5887 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005888 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005889 goto free_msg;
5890 }
5891
5892 if (wdev->ps)
5893 ps_state = NL80211_PS_ENABLED;
5894 else
5895 ps_state = NL80211_PS_DISABLED;
5896
David S. Miller9360ffd2012-03-29 04:41:26 -04005897 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5898 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005899
5900 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005901 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005902
Johannes Berg4c476992010-10-04 21:36:35 +02005903 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005904 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005905 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005906 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005907 return err;
5908}
5909
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005910static struct nla_policy
5911nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5912 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5913 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5914 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5915};
5916
5917static int nl80211_set_cqm_rssi(struct genl_info *info,
5918 s32 threshold, u32 hysteresis)
5919{
Johannes Berg4c476992010-10-04 21:36:35 +02005920 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005921 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005922 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005923
5924 if (threshold > 0)
5925 return -EINVAL;
5926
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005927 wdev = dev->ieee80211_ptr;
5928
Johannes Berg4c476992010-10-04 21:36:35 +02005929 if (!rdev->ops->set_cqm_rssi_config)
5930 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005931
Johannes Berg074ac8d2010-09-16 14:58:22 +02005932 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005933 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5934 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005935
Johannes Berg4c476992010-10-04 21:36:35 +02005936 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5937 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005938}
5939
5940static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5941{
5942 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5943 struct nlattr *cqm;
5944 int err;
5945
5946 cqm = info->attrs[NL80211_ATTR_CQM];
5947 if (!cqm) {
5948 err = -EINVAL;
5949 goto out;
5950 }
5951
5952 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5953 nl80211_attr_cqm_policy);
5954 if (err)
5955 goto out;
5956
5957 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5958 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5959 s32 threshold;
5960 u32 hysteresis;
5961 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5962 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5963 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5964 } else
5965 err = -EINVAL;
5966
5967out:
5968 return err;
5969}
5970
Johannes Berg29cbe682010-12-03 09:20:44 +01005971static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5972{
5973 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5974 struct net_device *dev = info->user_ptr[1];
5975 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005976 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005977 int err;
5978
5979 /* start with default */
5980 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005981 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005982
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005983 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005984 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005985 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005986 if (err)
5987 return err;
5988 }
5989
5990 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5991 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5992 return -EINVAL;
5993
Javier Cardonac80d5452010-12-16 17:37:49 -08005994 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5995 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5996
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08005997 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5998 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5999 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6000 return -EINVAL;
6001
Javier Cardonac80d5452010-12-16 17:37:49 -08006002 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6003 /* parse additional setup parameters if given */
6004 err = nl80211_parse_mesh_setup(info, &setup);
6005 if (err)
6006 return err;
6007 }
6008
6009 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006010}
6011
6012static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6013{
6014 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6015 struct net_device *dev = info->user_ptr[1];
6016
6017 return cfg80211_leave_mesh(rdev, dev);
6018}
6019
Johannes Bergff1b6e62011-05-04 15:37:28 +02006020static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6021{
6022 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6023 struct sk_buff *msg;
6024 void *hdr;
6025
6026 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6027 return -EOPNOTSUPP;
6028
6029 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6030 if (!msg)
6031 return -ENOMEM;
6032
6033 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6034 NL80211_CMD_GET_WOWLAN);
6035 if (!hdr)
6036 goto nla_put_failure;
6037
6038 if (rdev->wowlan) {
6039 struct nlattr *nl_wowlan;
6040
6041 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6042 if (!nl_wowlan)
6043 goto nla_put_failure;
6044
David S. Miller9360ffd2012-03-29 04:41:26 -04006045 if ((rdev->wowlan->any &&
6046 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6047 (rdev->wowlan->disconnect &&
6048 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6049 (rdev->wowlan->magic_pkt &&
6050 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6051 (rdev->wowlan->gtk_rekey_failure &&
6052 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6053 (rdev->wowlan->eap_identity_req &&
6054 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6055 (rdev->wowlan->four_way_handshake &&
6056 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6057 (rdev->wowlan->rfkill_release &&
6058 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6059 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006060 if (rdev->wowlan->n_patterns) {
6061 struct nlattr *nl_pats, *nl_pat;
6062 int i, pat_len;
6063
6064 nl_pats = nla_nest_start(msg,
6065 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6066 if (!nl_pats)
6067 goto nla_put_failure;
6068
6069 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6070 nl_pat = nla_nest_start(msg, i + 1);
6071 if (!nl_pat)
6072 goto nla_put_failure;
6073 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006074 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6075 DIV_ROUND_UP(pat_len, 8),
6076 rdev->wowlan->patterns[i].mask) ||
6077 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6078 pat_len,
6079 rdev->wowlan->patterns[i].pattern))
6080 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006081 nla_nest_end(msg, nl_pat);
6082 }
6083 nla_nest_end(msg, nl_pats);
6084 }
6085
6086 nla_nest_end(msg, nl_wowlan);
6087 }
6088
6089 genlmsg_end(msg, hdr);
6090 return genlmsg_reply(msg, info);
6091
6092nla_put_failure:
6093 nlmsg_free(msg);
6094 return -ENOBUFS;
6095}
6096
6097static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6098{
6099 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6100 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6101 struct cfg80211_wowlan no_triggers = {};
6102 struct cfg80211_wowlan new_triggers = {};
6103 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6104 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006105 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006106
6107 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6108 return -EOPNOTSUPP;
6109
6110 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6111 goto no_triggers;
6112
6113 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6114 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6115 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6116 nl80211_wowlan_policy);
6117 if (err)
6118 return err;
6119
6120 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6121 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6122 return -EINVAL;
6123 new_triggers.any = true;
6124 }
6125
6126 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6127 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6128 return -EINVAL;
6129 new_triggers.disconnect = true;
6130 }
6131
6132 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6133 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6134 return -EINVAL;
6135 new_triggers.magic_pkt = true;
6136 }
6137
Johannes Berg77dbbb12011-07-13 10:48:55 +02006138 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6139 return -EINVAL;
6140
6141 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6142 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6143 return -EINVAL;
6144 new_triggers.gtk_rekey_failure = true;
6145 }
6146
6147 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6148 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6149 return -EINVAL;
6150 new_triggers.eap_identity_req = true;
6151 }
6152
6153 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6154 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6155 return -EINVAL;
6156 new_triggers.four_way_handshake = true;
6157 }
6158
6159 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6160 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6161 return -EINVAL;
6162 new_triggers.rfkill_release = true;
6163 }
6164
Johannes Bergff1b6e62011-05-04 15:37:28 +02006165 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6166 struct nlattr *pat;
6167 int n_patterns = 0;
6168 int rem, pat_len, mask_len;
6169 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6170
6171 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6172 rem)
6173 n_patterns++;
6174 if (n_patterns > wowlan->n_patterns)
6175 return -EINVAL;
6176
6177 new_triggers.patterns = kcalloc(n_patterns,
6178 sizeof(new_triggers.patterns[0]),
6179 GFP_KERNEL);
6180 if (!new_triggers.patterns)
6181 return -ENOMEM;
6182
6183 new_triggers.n_patterns = n_patterns;
6184 i = 0;
6185
6186 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6187 rem) {
6188 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6189 nla_data(pat), nla_len(pat), NULL);
6190 err = -EINVAL;
6191 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6192 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6193 goto error;
6194 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6195 mask_len = DIV_ROUND_UP(pat_len, 8);
6196 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6197 mask_len)
6198 goto error;
6199 if (pat_len > wowlan->pattern_max_len ||
6200 pat_len < wowlan->pattern_min_len)
6201 goto error;
6202
6203 new_triggers.patterns[i].mask =
6204 kmalloc(mask_len + pat_len, GFP_KERNEL);
6205 if (!new_triggers.patterns[i].mask) {
6206 err = -ENOMEM;
6207 goto error;
6208 }
6209 new_triggers.patterns[i].pattern =
6210 new_triggers.patterns[i].mask + mask_len;
6211 memcpy(new_triggers.patterns[i].mask,
6212 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6213 mask_len);
6214 new_triggers.patterns[i].pattern_len = pat_len;
6215 memcpy(new_triggers.patterns[i].pattern,
6216 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6217 pat_len);
6218 i++;
6219 }
6220 }
6221
6222 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6223 struct cfg80211_wowlan *ntrig;
6224 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6225 GFP_KERNEL);
6226 if (!ntrig) {
6227 err = -ENOMEM;
6228 goto error;
6229 }
6230 cfg80211_rdev_free_wowlan(rdev);
6231 rdev->wowlan = ntrig;
6232 } else {
6233 no_triggers:
6234 cfg80211_rdev_free_wowlan(rdev);
6235 rdev->wowlan = NULL;
6236 }
6237
Johannes Berg6d525632012-04-04 15:05:25 +02006238 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6239 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6240
Johannes Bergff1b6e62011-05-04 15:37:28 +02006241 return 0;
6242 error:
6243 for (i = 0; i < new_triggers.n_patterns; i++)
6244 kfree(new_triggers.patterns[i].mask);
6245 kfree(new_triggers.patterns);
6246 return err;
6247}
6248
Johannes Berge5497d72011-07-05 16:35:40 +02006249static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6250{
6251 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6252 struct net_device *dev = info->user_ptr[1];
6253 struct wireless_dev *wdev = dev->ieee80211_ptr;
6254 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6255 struct cfg80211_gtk_rekey_data rekey_data;
6256 int err;
6257
6258 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6259 return -EINVAL;
6260
6261 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6262 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6263 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6264 nl80211_rekey_policy);
6265 if (err)
6266 return err;
6267
6268 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6269 return -ERANGE;
6270 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6271 return -ERANGE;
6272 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6273 return -ERANGE;
6274
6275 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6276 NL80211_KEK_LEN);
6277 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6278 NL80211_KCK_LEN);
6279 memcpy(rekey_data.replay_ctr,
6280 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6281 NL80211_REPLAY_CTR_LEN);
6282
6283 wdev_lock(wdev);
6284 if (!wdev->current_bss) {
6285 err = -ENOTCONN;
6286 goto out;
6287 }
6288
6289 if (!rdev->ops->set_rekey_data) {
6290 err = -EOPNOTSUPP;
6291 goto out;
6292 }
6293
6294 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6295 out:
6296 wdev_unlock(wdev);
6297 return err;
6298}
6299
Johannes Berg28946da2011-11-04 11:18:12 +01006300static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6301 struct genl_info *info)
6302{
6303 struct net_device *dev = info->user_ptr[1];
6304 struct wireless_dev *wdev = dev->ieee80211_ptr;
6305
6306 if (wdev->iftype != NL80211_IFTYPE_AP &&
6307 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6308 return -EINVAL;
6309
6310 if (wdev->ap_unexpected_nlpid)
6311 return -EBUSY;
6312
6313 wdev->ap_unexpected_nlpid = info->snd_pid;
6314 return 0;
6315}
6316
Johannes Berg7f6cf312011-11-04 11:18:15 +01006317static int nl80211_probe_client(struct sk_buff *skb,
6318 struct genl_info *info)
6319{
6320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6321 struct net_device *dev = info->user_ptr[1];
6322 struct wireless_dev *wdev = dev->ieee80211_ptr;
6323 struct sk_buff *msg;
6324 void *hdr;
6325 const u8 *addr;
6326 u64 cookie;
6327 int err;
6328
6329 if (wdev->iftype != NL80211_IFTYPE_AP &&
6330 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6331 return -EOPNOTSUPP;
6332
6333 if (!info->attrs[NL80211_ATTR_MAC])
6334 return -EINVAL;
6335
6336 if (!rdev->ops->probe_client)
6337 return -EOPNOTSUPP;
6338
6339 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6340 if (!msg)
6341 return -ENOMEM;
6342
6343 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6344 NL80211_CMD_PROBE_CLIENT);
6345
6346 if (IS_ERR(hdr)) {
6347 err = PTR_ERR(hdr);
6348 goto free_msg;
6349 }
6350
6351 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6352
6353 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6354 if (err)
6355 goto free_msg;
6356
David S. Miller9360ffd2012-03-29 04:41:26 -04006357 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6358 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006359
6360 genlmsg_end(msg, hdr);
6361
6362 return genlmsg_reply(msg, info);
6363
6364 nla_put_failure:
6365 err = -ENOBUFS;
6366 free_msg:
6367 nlmsg_free(msg);
6368 return err;
6369}
6370
Johannes Berg5e7602302011-11-04 11:18:17 +01006371static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6372{
6373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6374
6375 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6376 return -EOPNOTSUPP;
6377
6378 if (rdev->ap_beacons_nlpid)
6379 return -EBUSY;
6380
6381 rdev->ap_beacons_nlpid = info->snd_pid;
6382
6383 return 0;
6384}
6385
Johannes Berg4c476992010-10-04 21:36:35 +02006386#define NL80211_FLAG_NEED_WIPHY 0x01
6387#define NL80211_FLAG_NEED_NETDEV 0x02
6388#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006389#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6390#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6391 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006392
6393static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6394 struct genl_info *info)
6395{
6396 struct cfg80211_registered_device *rdev;
6397 struct net_device *dev;
6398 int err;
6399 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6400
6401 if (rtnl)
6402 rtnl_lock();
6403
6404 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6405 rdev = cfg80211_get_dev_from_info(info);
6406 if (IS_ERR(rdev)) {
6407 if (rtnl)
6408 rtnl_unlock();
6409 return PTR_ERR(rdev);
6410 }
6411 info->user_ptr[0] = rdev;
6412 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006413 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6414 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006415 if (err) {
6416 if (rtnl)
6417 rtnl_unlock();
6418 return err;
6419 }
Johannes Berg41265712010-10-04 21:14:05 +02006420 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6421 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006422 cfg80211_unlock_rdev(rdev);
6423 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006424 if (rtnl)
6425 rtnl_unlock();
6426 return -ENETDOWN;
6427 }
Johannes Berg4c476992010-10-04 21:36:35 +02006428 info->user_ptr[0] = rdev;
6429 info->user_ptr[1] = dev;
6430 }
6431
6432 return 0;
6433}
6434
6435static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6436 struct genl_info *info)
6437{
6438 if (info->user_ptr[0])
6439 cfg80211_unlock_rdev(info->user_ptr[0]);
6440 if (info->user_ptr[1])
6441 dev_put(info->user_ptr[1]);
6442 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6443 rtnl_unlock();
6444}
6445
Johannes Berg55682962007-09-20 13:09:35 -04006446static struct genl_ops nl80211_ops[] = {
6447 {
6448 .cmd = NL80211_CMD_GET_WIPHY,
6449 .doit = nl80211_get_wiphy,
6450 .dumpit = nl80211_dump_wiphy,
6451 .policy = nl80211_policy,
6452 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006453 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006454 },
6455 {
6456 .cmd = NL80211_CMD_SET_WIPHY,
6457 .doit = nl80211_set_wiphy,
6458 .policy = nl80211_policy,
6459 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006460 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006461 },
6462 {
6463 .cmd = NL80211_CMD_GET_INTERFACE,
6464 .doit = nl80211_get_interface,
6465 .dumpit = nl80211_dump_interface,
6466 .policy = nl80211_policy,
6467 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006468 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006469 },
6470 {
6471 .cmd = NL80211_CMD_SET_INTERFACE,
6472 .doit = nl80211_set_interface,
6473 .policy = nl80211_policy,
6474 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006475 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6476 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006477 },
6478 {
6479 .cmd = NL80211_CMD_NEW_INTERFACE,
6480 .doit = nl80211_new_interface,
6481 .policy = nl80211_policy,
6482 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006483 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6484 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006485 },
6486 {
6487 .cmd = NL80211_CMD_DEL_INTERFACE,
6488 .doit = nl80211_del_interface,
6489 .policy = nl80211_policy,
6490 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006491 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6492 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006493 },
Johannes Berg41ade002007-12-19 02:03:29 +01006494 {
6495 .cmd = NL80211_CMD_GET_KEY,
6496 .doit = nl80211_get_key,
6497 .policy = nl80211_policy,
6498 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006499 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006500 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006501 },
6502 {
6503 .cmd = NL80211_CMD_SET_KEY,
6504 .doit = nl80211_set_key,
6505 .policy = nl80211_policy,
6506 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006507 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006508 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006509 },
6510 {
6511 .cmd = NL80211_CMD_NEW_KEY,
6512 .doit = nl80211_new_key,
6513 .policy = nl80211_policy,
6514 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006515 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006516 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006517 },
6518 {
6519 .cmd = NL80211_CMD_DEL_KEY,
6520 .doit = nl80211_del_key,
6521 .policy = nl80211_policy,
6522 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006523 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006524 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006525 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006526 {
6527 .cmd = NL80211_CMD_SET_BEACON,
6528 .policy = nl80211_policy,
6529 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006530 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006531 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006532 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006533 },
6534 {
Johannes Berg88600202012-02-13 15:17:18 +01006535 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006536 .policy = nl80211_policy,
6537 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006538 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006539 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006540 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006541 },
6542 {
Johannes Berg88600202012-02-13 15:17:18 +01006543 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006544 .policy = nl80211_policy,
6545 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006546 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006547 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006548 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006549 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006550 {
6551 .cmd = NL80211_CMD_GET_STATION,
6552 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006553 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006554 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006555 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6556 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006557 },
6558 {
6559 .cmd = NL80211_CMD_SET_STATION,
6560 .doit = nl80211_set_station,
6561 .policy = nl80211_policy,
6562 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006563 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006564 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006565 },
6566 {
6567 .cmd = NL80211_CMD_NEW_STATION,
6568 .doit = nl80211_new_station,
6569 .policy = nl80211_policy,
6570 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006571 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006572 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006573 },
6574 {
6575 .cmd = NL80211_CMD_DEL_STATION,
6576 .doit = nl80211_del_station,
6577 .policy = nl80211_policy,
6578 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006579 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006580 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006581 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006582 {
6583 .cmd = NL80211_CMD_GET_MPATH,
6584 .doit = nl80211_get_mpath,
6585 .dumpit = nl80211_dump_mpath,
6586 .policy = nl80211_policy,
6587 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006588 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006589 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006590 },
6591 {
6592 .cmd = NL80211_CMD_SET_MPATH,
6593 .doit = nl80211_set_mpath,
6594 .policy = nl80211_policy,
6595 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006596 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006597 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006598 },
6599 {
6600 .cmd = NL80211_CMD_NEW_MPATH,
6601 .doit = nl80211_new_mpath,
6602 .policy = nl80211_policy,
6603 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006604 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006605 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006606 },
6607 {
6608 .cmd = NL80211_CMD_DEL_MPATH,
6609 .doit = nl80211_del_mpath,
6610 .policy = nl80211_policy,
6611 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006612 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006613 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006614 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006615 {
6616 .cmd = NL80211_CMD_SET_BSS,
6617 .doit = nl80211_set_bss,
6618 .policy = nl80211_policy,
6619 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006620 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006621 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006622 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006623 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006624 .cmd = NL80211_CMD_GET_REG,
6625 .doit = nl80211_get_reg,
6626 .policy = nl80211_policy,
6627 /* can be retrieved by unprivileged users */
6628 },
6629 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006630 .cmd = NL80211_CMD_SET_REG,
6631 .doit = nl80211_set_reg,
6632 .policy = nl80211_policy,
6633 .flags = GENL_ADMIN_PERM,
6634 },
6635 {
6636 .cmd = NL80211_CMD_REQ_SET_REG,
6637 .doit = nl80211_req_set_reg,
6638 .policy = nl80211_policy,
6639 .flags = GENL_ADMIN_PERM,
6640 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006641 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006642 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6643 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006644 .policy = nl80211_policy,
6645 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006646 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006647 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006648 },
6649 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006650 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6651 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006652 .policy = nl80211_policy,
6653 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006655 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006656 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006657 {
Johannes Berg2a519312009-02-10 21:25:55 +01006658 .cmd = NL80211_CMD_TRIGGER_SCAN,
6659 .doit = nl80211_trigger_scan,
6660 .policy = nl80211_policy,
6661 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006663 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006664 },
6665 {
6666 .cmd = NL80211_CMD_GET_SCAN,
6667 .policy = nl80211_policy,
6668 .dumpit = nl80211_dump_scan,
6669 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006670 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006671 .cmd = NL80211_CMD_START_SCHED_SCAN,
6672 .doit = nl80211_start_sched_scan,
6673 .policy = nl80211_policy,
6674 .flags = GENL_ADMIN_PERM,
6675 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6676 NL80211_FLAG_NEED_RTNL,
6677 },
6678 {
6679 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6680 .doit = nl80211_stop_sched_scan,
6681 .policy = nl80211_policy,
6682 .flags = GENL_ADMIN_PERM,
6683 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6684 NL80211_FLAG_NEED_RTNL,
6685 },
6686 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006687 .cmd = NL80211_CMD_AUTHENTICATE,
6688 .doit = nl80211_authenticate,
6689 .policy = nl80211_policy,
6690 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006691 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006692 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006693 },
6694 {
6695 .cmd = NL80211_CMD_ASSOCIATE,
6696 .doit = nl80211_associate,
6697 .policy = nl80211_policy,
6698 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006699 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006700 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006701 },
6702 {
6703 .cmd = NL80211_CMD_DEAUTHENTICATE,
6704 .doit = nl80211_deauthenticate,
6705 .policy = nl80211_policy,
6706 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006707 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006708 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006709 },
6710 {
6711 .cmd = NL80211_CMD_DISASSOCIATE,
6712 .doit = nl80211_disassociate,
6713 .policy = nl80211_policy,
6714 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006715 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006716 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006717 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006718 {
6719 .cmd = NL80211_CMD_JOIN_IBSS,
6720 .doit = nl80211_join_ibss,
6721 .policy = nl80211_policy,
6722 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006723 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006724 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006725 },
6726 {
6727 .cmd = NL80211_CMD_LEAVE_IBSS,
6728 .doit = nl80211_leave_ibss,
6729 .policy = nl80211_policy,
6730 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006731 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006732 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006733 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006734#ifdef CONFIG_NL80211_TESTMODE
6735 {
6736 .cmd = NL80211_CMD_TESTMODE,
6737 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006738 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006739 .policy = nl80211_policy,
6740 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006741 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6742 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006743 },
6744#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006745 {
6746 .cmd = NL80211_CMD_CONNECT,
6747 .doit = nl80211_connect,
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,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006752 },
6753 {
6754 .cmd = NL80211_CMD_DISCONNECT,
6755 .doit = nl80211_disconnect,
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,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006760 },
Johannes Berg463d0182009-07-14 00:33:35 +02006761 {
6762 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6763 .doit = nl80211_wiphy_netns,
6764 .policy = nl80211_policy,
6765 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006766 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6767 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006768 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006769 {
6770 .cmd = NL80211_CMD_GET_SURVEY,
6771 .policy = nl80211_policy,
6772 .dumpit = nl80211_dump_survey,
6773 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006774 {
6775 .cmd = NL80211_CMD_SET_PMKSA,
6776 .doit = nl80211_setdel_pmksa,
6777 .policy = nl80211_policy,
6778 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006779 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006780 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006781 },
6782 {
6783 .cmd = NL80211_CMD_DEL_PMKSA,
6784 .doit = nl80211_setdel_pmksa,
6785 .policy = nl80211_policy,
6786 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006787 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006788 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006789 },
6790 {
6791 .cmd = NL80211_CMD_FLUSH_PMKSA,
6792 .doit = nl80211_flush_pmksa,
6793 .policy = nl80211_policy,
6794 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006795 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006796 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006797 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006798 {
6799 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6800 .doit = nl80211_remain_on_channel,
6801 .policy = nl80211_policy,
6802 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006803 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006804 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006805 },
6806 {
6807 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6808 .doit = nl80211_cancel_remain_on_channel,
6809 .policy = nl80211_policy,
6810 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006811 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006812 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006813 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006814 {
6815 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6816 .doit = nl80211_set_tx_bitrate_mask,
6817 .policy = nl80211_policy,
6818 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006819 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6820 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006821 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006822 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006823 .cmd = NL80211_CMD_REGISTER_FRAME,
6824 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006825 .policy = nl80211_policy,
6826 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006827 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6828 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006829 },
6830 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006831 .cmd = NL80211_CMD_FRAME,
6832 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006833 .policy = nl80211_policy,
6834 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006835 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006836 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006837 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006838 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006839 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6840 .doit = nl80211_tx_mgmt_cancel_wait,
6841 .policy = nl80211_policy,
6842 .flags = GENL_ADMIN_PERM,
6843 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6844 NL80211_FLAG_NEED_RTNL,
6845 },
6846 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006847 .cmd = NL80211_CMD_SET_POWER_SAVE,
6848 .doit = nl80211_set_power_save,
6849 .policy = nl80211_policy,
6850 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006851 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6852 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006853 },
6854 {
6855 .cmd = NL80211_CMD_GET_POWER_SAVE,
6856 .doit = nl80211_get_power_save,
6857 .policy = nl80211_policy,
6858 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006859 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6860 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006861 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006862 {
6863 .cmd = NL80211_CMD_SET_CQM,
6864 .doit = nl80211_set_cqm,
6865 .policy = nl80211_policy,
6866 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006867 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6868 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006869 },
Johannes Bergf444de02010-05-05 15:25:02 +02006870 {
6871 .cmd = NL80211_CMD_SET_CHANNEL,
6872 .doit = nl80211_set_channel,
6873 .policy = nl80211_policy,
6874 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006875 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6876 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006877 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006878 {
6879 .cmd = NL80211_CMD_SET_WDS_PEER,
6880 .doit = nl80211_set_wds_peer,
6881 .policy = nl80211_policy,
6882 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006883 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6884 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006885 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006886 {
6887 .cmd = NL80211_CMD_JOIN_MESH,
6888 .doit = nl80211_join_mesh,
6889 .policy = nl80211_policy,
6890 .flags = GENL_ADMIN_PERM,
6891 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6892 NL80211_FLAG_NEED_RTNL,
6893 },
6894 {
6895 .cmd = NL80211_CMD_LEAVE_MESH,
6896 .doit = nl80211_leave_mesh,
6897 .policy = nl80211_policy,
6898 .flags = GENL_ADMIN_PERM,
6899 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6900 NL80211_FLAG_NEED_RTNL,
6901 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006902 {
6903 .cmd = NL80211_CMD_GET_WOWLAN,
6904 .doit = nl80211_get_wowlan,
6905 .policy = nl80211_policy,
6906 /* can be retrieved by unprivileged users */
6907 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6908 NL80211_FLAG_NEED_RTNL,
6909 },
6910 {
6911 .cmd = NL80211_CMD_SET_WOWLAN,
6912 .doit = nl80211_set_wowlan,
6913 .policy = nl80211_policy,
6914 .flags = GENL_ADMIN_PERM,
6915 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6916 NL80211_FLAG_NEED_RTNL,
6917 },
Johannes Berge5497d72011-07-05 16:35:40 +02006918 {
6919 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6920 .doit = nl80211_set_rekey_data,
6921 .policy = nl80211_policy,
6922 .flags = GENL_ADMIN_PERM,
6923 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6924 NL80211_FLAG_NEED_RTNL,
6925 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006926 {
6927 .cmd = NL80211_CMD_TDLS_MGMT,
6928 .doit = nl80211_tdls_mgmt,
6929 .policy = nl80211_policy,
6930 .flags = GENL_ADMIN_PERM,
6931 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6932 NL80211_FLAG_NEED_RTNL,
6933 },
6934 {
6935 .cmd = NL80211_CMD_TDLS_OPER,
6936 .doit = nl80211_tdls_oper,
6937 .policy = nl80211_policy,
6938 .flags = GENL_ADMIN_PERM,
6939 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6940 NL80211_FLAG_NEED_RTNL,
6941 },
Johannes Berg28946da2011-11-04 11:18:12 +01006942 {
6943 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6944 .doit = nl80211_register_unexpected_frame,
6945 .policy = nl80211_policy,
6946 .flags = GENL_ADMIN_PERM,
6947 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6948 NL80211_FLAG_NEED_RTNL,
6949 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006950 {
6951 .cmd = NL80211_CMD_PROBE_CLIENT,
6952 .doit = nl80211_probe_client,
6953 .policy = nl80211_policy,
6954 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006955 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01006956 NL80211_FLAG_NEED_RTNL,
6957 },
Johannes Berg5e7602302011-11-04 11:18:17 +01006958 {
6959 .cmd = NL80211_CMD_REGISTER_BEACONS,
6960 .doit = nl80211_register_beacons,
6961 .policy = nl80211_policy,
6962 .flags = GENL_ADMIN_PERM,
6963 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6964 NL80211_FLAG_NEED_RTNL,
6965 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006966 {
6967 .cmd = NL80211_CMD_SET_NOACK_MAP,
6968 .doit = nl80211_set_noack_map,
6969 .policy = nl80211_policy,
6970 .flags = GENL_ADMIN_PERM,
6971 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6972 NL80211_FLAG_NEED_RTNL,
6973 },
6974
Johannes Berg55682962007-09-20 13:09:35 -04006975};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006976
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006977static struct genl_multicast_group nl80211_mlme_mcgrp = {
6978 .name = "mlme",
6979};
Johannes Berg55682962007-09-20 13:09:35 -04006980
6981/* multicast groups */
6982static struct genl_multicast_group nl80211_config_mcgrp = {
6983 .name = "config",
6984};
Johannes Berg2a519312009-02-10 21:25:55 +01006985static struct genl_multicast_group nl80211_scan_mcgrp = {
6986 .name = "scan",
6987};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006988static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6989 .name = "regulatory",
6990};
Johannes Berg55682962007-09-20 13:09:35 -04006991
6992/* notification functions */
6993
6994void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6995{
6996 struct sk_buff *msg;
6997
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006998 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006999 if (!msg)
7000 return;
7001
7002 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7003 nlmsg_free(msg);
7004 return;
7005 }
7006
Johannes Berg463d0182009-07-14 00:33:35 +02007007 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7008 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007009}
7010
Johannes Berg362a4152009-05-24 16:43:15 +02007011static int nl80211_add_scan_req(struct sk_buff *msg,
7012 struct cfg80211_registered_device *rdev)
7013{
7014 struct cfg80211_scan_request *req = rdev->scan_req;
7015 struct nlattr *nest;
7016 int i;
7017
Johannes Berg667503dd2009-07-07 03:56:11 +02007018 ASSERT_RDEV_LOCK(rdev);
7019
Johannes Berg362a4152009-05-24 16:43:15 +02007020 if (WARN_ON(!req))
7021 return 0;
7022
7023 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7024 if (!nest)
7025 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007026 for (i = 0; i < req->n_ssids; i++) {
7027 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7028 goto nla_put_failure;
7029 }
Johannes Berg362a4152009-05-24 16:43:15 +02007030 nla_nest_end(msg, nest);
7031
7032 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7033 if (!nest)
7034 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007035 for (i = 0; i < req->n_channels; i++) {
7036 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7037 goto nla_put_failure;
7038 }
Johannes Berg362a4152009-05-24 16:43:15 +02007039 nla_nest_end(msg, nest);
7040
David S. Miller9360ffd2012-03-29 04:41:26 -04007041 if (req->ie &&
7042 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7043 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007044
7045 return 0;
7046 nla_put_failure:
7047 return -ENOBUFS;
7048}
7049
Johannes Berga538e2d2009-06-16 19:56:42 +02007050static int nl80211_send_scan_msg(struct sk_buff *msg,
7051 struct cfg80211_registered_device *rdev,
7052 struct net_device *netdev,
7053 u32 pid, u32 seq, int flags,
7054 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007055{
7056 void *hdr;
7057
7058 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7059 if (!hdr)
7060 return -1;
7061
David S. Miller9360ffd2012-03-29 04:41:26 -04007062 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7063 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7064 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007065
Johannes Berg362a4152009-05-24 16:43:15 +02007066 /* ignore errors and send incomplete event anyway */
7067 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007068
7069 return genlmsg_end(msg, hdr);
7070
7071 nla_put_failure:
7072 genlmsg_cancel(msg, hdr);
7073 return -EMSGSIZE;
7074}
7075
Luciano Coelho807f8a82011-05-11 17:09:35 +03007076static int
7077nl80211_send_sched_scan_msg(struct sk_buff *msg,
7078 struct cfg80211_registered_device *rdev,
7079 struct net_device *netdev,
7080 u32 pid, u32 seq, int flags, u32 cmd)
7081{
7082 void *hdr;
7083
7084 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7085 if (!hdr)
7086 return -1;
7087
David S. Miller9360ffd2012-03-29 04:41:26 -04007088 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7089 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7090 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007091
7092 return genlmsg_end(msg, hdr);
7093
7094 nla_put_failure:
7095 genlmsg_cancel(msg, hdr);
7096 return -EMSGSIZE;
7097}
7098
Johannes Berga538e2d2009-06-16 19:56:42 +02007099void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7100 struct net_device *netdev)
7101{
7102 struct sk_buff *msg;
7103
7104 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7105 if (!msg)
7106 return;
7107
7108 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7109 NL80211_CMD_TRIGGER_SCAN) < 0) {
7110 nlmsg_free(msg);
7111 return;
7112 }
7113
Johannes Berg463d0182009-07-14 00:33:35 +02007114 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7115 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007116}
7117
Johannes Berg2a519312009-02-10 21:25:55 +01007118void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7119 struct net_device *netdev)
7120{
7121 struct sk_buff *msg;
7122
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007123 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007124 if (!msg)
7125 return;
7126
Johannes Berga538e2d2009-06-16 19:56:42 +02007127 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7128 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007129 nlmsg_free(msg);
7130 return;
7131 }
7132
Johannes Berg463d0182009-07-14 00:33:35 +02007133 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7134 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007135}
7136
7137void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7138 struct net_device *netdev)
7139{
7140 struct sk_buff *msg;
7141
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007142 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007143 if (!msg)
7144 return;
7145
Johannes Berga538e2d2009-06-16 19:56:42 +02007146 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7147 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007148 nlmsg_free(msg);
7149 return;
7150 }
7151
Johannes Berg463d0182009-07-14 00:33:35 +02007152 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7153 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007154}
7155
Luciano Coelho807f8a82011-05-11 17:09:35 +03007156void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7157 struct net_device *netdev)
7158{
7159 struct sk_buff *msg;
7160
7161 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7162 if (!msg)
7163 return;
7164
7165 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7166 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7167 nlmsg_free(msg);
7168 return;
7169 }
7170
7171 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7172 nl80211_scan_mcgrp.id, GFP_KERNEL);
7173}
7174
7175void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7176 struct net_device *netdev, u32 cmd)
7177{
7178 struct sk_buff *msg;
7179
7180 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7181 if (!msg)
7182 return;
7183
7184 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7185 nlmsg_free(msg);
7186 return;
7187 }
7188
7189 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7190 nl80211_scan_mcgrp.id, GFP_KERNEL);
7191}
7192
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007193/*
7194 * This can happen on global regulatory changes or device specific settings
7195 * based on custom world regulatory domains.
7196 */
7197void nl80211_send_reg_change_event(struct regulatory_request *request)
7198{
7199 struct sk_buff *msg;
7200 void *hdr;
7201
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007203 if (!msg)
7204 return;
7205
7206 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7207 if (!hdr) {
7208 nlmsg_free(msg);
7209 return;
7210 }
7211
7212 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007213 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7214 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007215
David S. Miller9360ffd2012-03-29 04:41:26 -04007216 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7217 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7218 NL80211_REGDOM_TYPE_WORLD))
7219 goto nla_put_failure;
7220 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7221 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7222 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7223 goto nla_put_failure;
7224 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7225 request->intersect) {
7226 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7227 NL80211_REGDOM_TYPE_INTERSECTION))
7228 goto nla_put_failure;
7229 } else {
7230 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7231 NL80211_REGDOM_TYPE_COUNTRY) ||
7232 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7233 request->alpha2))
7234 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007235 }
7236
David S. Miller9360ffd2012-03-29 04:41:26 -04007237 if (wiphy_idx_valid(request->wiphy_idx) &&
7238 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7239 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007240
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007241 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007242
Johannes Bergbc43b282009-07-25 10:54:13 +02007243 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007244 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007245 GFP_ATOMIC);
7246 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007247
7248 return;
7249
7250nla_put_failure:
7251 genlmsg_cancel(msg, hdr);
7252 nlmsg_free(msg);
7253}
7254
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007255static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7256 struct net_device *netdev,
7257 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007258 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007259{
7260 struct sk_buff *msg;
7261 void *hdr;
7262
Johannes Berge6d6e342009-07-01 21:26:47 +02007263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007264 if (!msg)
7265 return;
7266
7267 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7268 if (!hdr) {
7269 nlmsg_free(msg);
7270 return;
7271 }
7272
David S. Miller9360ffd2012-03-29 04:41:26 -04007273 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7274 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7275 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7276 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007277
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007278 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007279
Johannes Berg463d0182009-07-14 00:33:35 +02007280 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7281 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007282 return;
7283
7284 nla_put_failure:
7285 genlmsg_cancel(msg, hdr);
7286 nlmsg_free(msg);
7287}
7288
7289void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007290 struct net_device *netdev, const u8 *buf,
7291 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007292{
7293 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007294 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007295}
7296
7297void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7298 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007299 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007300{
Johannes Berge6d6e342009-07-01 21:26:47 +02007301 nl80211_send_mlme_event(rdev, netdev, buf, len,
7302 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007303}
7304
Jouni Malinen53b46b82009-03-27 20:53:56 +02007305void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007306 struct net_device *netdev, const u8 *buf,
7307 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007308{
7309 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007310 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007311}
7312
Jouni Malinen53b46b82009-03-27 20:53:56 +02007313void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7314 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007315 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007316{
7317 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007318 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007319}
7320
Jouni Malinencf4e5942010-12-16 00:52:40 +02007321void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7322 struct net_device *netdev, const u8 *buf,
7323 size_t len, gfp_t gfp)
7324{
7325 nl80211_send_mlme_event(rdev, netdev, buf, len,
7326 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7327}
7328
7329void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7330 struct net_device *netdev, const u8 *buf,
7331 size_t len, gfp_t gfp)
7332{
7333 nl80211_send_mlme_event(rdev, netdev, buf, len,
7334 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7335}
7336
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007337static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7338 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007339 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007340{
7341 struct sk_buff *msg;
7342 void *hdr;
7343
Johannes Berge6d6e342009-07-01 21:26:47 +02007344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007345 if (!msg)
7346 return;
7347
7348 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7349 if (!hdr) {
7350 nlmsg_free(msg);
7351 return;
7352 }
7353
David S. Miller9360ffd2012-03-29 04:41:26 -04007354 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7355 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7356 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7357 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7358 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007359
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007360 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007361
Johannes Berg463d0182009-07-14 00:33:35 +02007362 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7363 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007364 return;
7365
7366 nla_put_failure:
7367 genlmsg_cancel(msg, hdr);
7368 nlmsg_free(msg);
7369}
7370
7371void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007372 struct net_device *netdev, const u8 *addr,
7373 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007374{
7375 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007376 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007377}
7378
7379void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007380 struct net_device *netdev, const u8 *addr,
7381 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007382{
Johannes Berge6d6e342009-07-01 21:26:47 +02007383 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7384 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007385}
7386
Samuel Ortizb23aa672009-07-01 21:26:54 +02007387void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7388 struct net_device *netdev, const u8 *bssid,
7389 const u8 *req_ie, size_t req_ie_len,
7390 const u8 *resp_ie, size_t resp_ie_len,
7391 u16 status, gfp_t gfp)
7392{
7393 struct sk_buff *msg;
7394 void *hdr;
7395
7396 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7397 if (!msg)
7398 return;
7399
7400 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7401 if (!hdr) {
7402 nlmsg_free(msg);
7403 return;
7404 }
7405
David S. Miller9360ffd2012-03-29 04:41:26 -04007406 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7407 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7408 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7409 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7410 (req_ie &&
7411 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7412 (resp_ie &&
7413 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7414 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007415
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007416 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007417
Johannes Berg463d0182009-07-14 00:33:35 +02007418 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7419 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007420 return;
7421
7422 nla_put_failure:
7423 genlmsg_cancel(msg, hdr);
7424 nlmsg_free(msg);
7425
7426}
7427
7428void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7429 struct net_device *netdev, const u8 *bssid,
7430 const u8 *req_ie, size_t req_ie_len,
7431 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7432{
7433 struct sk_buff *msg;
7434 void *hdr;
7435
7436 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7437 if (!msg)
7438 return;
7439
7440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7441 if (!hdr) {
7442 nlmsg_free(msg);
7443 return;
7444 }
7445
David S. Miller9360ffd2012-03-29 04:41:26 -04007446 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7447 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7448 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7449 (req_ie &&
7450 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7451 (resp_ie &&
7452 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7453 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007454
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007455 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007456
Johannes Berg463d0182009-07-14 00:33:35 +02007457 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7458 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007459 return;
7460
7461 nla_put_failure:
7462 genlmsg_cancel(msg, hdr);
7463 nlmsg_free(msg);
7464
7465}
7466
7467void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7468 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007469 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007470{
7471 struct sk_buff *msg;
7472 void *hdr;
7473
Johannes Berg667503dd2009-07-07 03:56:11 +02007474 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007475 if (!msg)
7476 return;
7477
7478 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7479 if (!hdr) {
7480 nlmsg_free(msg);
7481 return;
7482 }
7483
David S. Miller9360ffd2012-03-29 04:41:26 -04007484 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7485 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7486 (from_ap && reason &&
7487 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7488 (from_ap &&
7489 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7490 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7491 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007492
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007493 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007494
Johannes Berg463d0182009-07-14 00:33:35 +02007495 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7496 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007497 return;
7498
7499 nla_put_failure:
7500 genlmsg_cancel(msg, hdr);
7501 nlmsg_free(msg);
7502
7503}
7504
Johannes Berg04a773a2009-04-19 21:24:32 +02007505void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7506 struct net_device *netdev, const u8 *bssid,
7507 gfp_t gfp)
7508{
7509 struct sk_buff *msg;
7510 void *hdr;
7511
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007513 if (!msg)
7514 return;
7515
7516 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7517 if (!hdr) {
7518 nlmsg_free(msg);
7519 return;
7520 }
7521
David S. Miller9360ffd2012-03-29 04:41:26 -04007522 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7523 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7524 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7525 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007526
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007527 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007528
Johannes Berg463d0182009-07-14 00:33:35 +02007529 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7530 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007531 return;
7532
7533 nla_put_failure:
7534 genlmsg_cancel(msg, hdr);
7535 nlmsg_free(msg);
7536}
7537
Javier Cardonac93b5e72011-04-07 15:08:34 -07007538void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7539 struct net_device *netdev,
7540 const u8 *macaddr, const u8* ie, u8 ie_len,
7541 gfp_t gfp)
7542{
7543 struct sk_buff *msg;
7544 void *hdr;
7545
7546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7547 if (!msg)
7548 return;
7549
7550 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7551 if (!hdr) {
7552 nlmsg_free(msg);
7553 return;
7554 }
7555
David S. Miller9360ffd2012-03-29 04:41:26 -04007556 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7557 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7558 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7559 (ie_len && ie &&
7560 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7561 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007562
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007563 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007564
7565 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7566 nl80211_mlme_mcgrp.id, gfp);
7567 return;
7568
7569 nla_put_failure:
7570 genlmsg_cancel(msg, hdr);
7571 nlmsg_free(msg);
7572}
7573
Jouni Malinena3b8b052009-03-27 21:59:49 +02007574void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7575 struct net_device *netdev, const u8 *addr,
7576 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007577 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007578{
7579 struct sk_buff *msg;
7580 void *hdr;
7581
Johannes Berge6d6e342009-07-01 21:26:47 +02007582 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007583 if (!msg)
7584 return;
7585
7586 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7587 if (!hdr) {
7588 nlmsg_free(msg);
7589 return;
7590 }
7591
David S. Miller9360ffd2012-03-29 04:41:26 -04007592 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7593 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7594 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7595 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7596 (key_id != -1 &&
7597 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7598 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7599 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007600
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007601 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007602
Johannes Berg463d0182009-07-14 00:33:35 +02007603 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7604 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007605 return;
7606
7607 nla_put_failure:
7608 genlmsg_cancel(msg, hdr);
7609 nlmsg_free(msg);
7610}
7611
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007612void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7613 struct ieee80211_channel *channel_before,
7614 struct ieee80211_channel *channel_after)
7615{
7616 struct sk_buff *msg;
7617 void *hdr;
7618 struct nlattr *nl_freq;
7619
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007620 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007621 if (!msg)
7622 return;
7623
7624 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7625 if (!hdr) {
7626 nlmsg_free(msg);
7627 return;
7628 }
7629
7630 /*
7631 * Since we are applying the beacon hint to a wiphy we know its
7632 * wiphy_idx is valid
7633 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007634 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7635 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007636
7637 /* Before */
7638 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7639 if (!nl_freq)
7640 goto nla_put_failure;
7641 if (nl80211_msg_put_channel(msg, channel_before))
7642 goto nla_put_failure;
7643 nla_nest_end(msg, nl_freq);
7644
7645 /* After */
7646 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7647 if (!nl_freq)
7648 goto nla_put_failure;
7649 if (nl80211_msg_put_channel(msg, channel_after))
7650 goto nla_put_failure;
7651 nla_nest_end(msg, nl_freq);
7652
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007653 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007654
Johannes Berg463d0182009-07-14 00:33:35 +02007655 rcu_read_lock();
7656 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7657 GFP_ATOMIC);
7658 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007659
7660 return;
7661
7662nla_put_failure:
7663 genlmsg_cancel(msg, hdr);
7664 nlmsg_free(msg);
7665}
7666
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007667static void nl80211_send_remain_on_chan_event(
7668 int cmd, struct cfg80211_registered_device *rdev,
7669 struct net_device *netdev, u64 cookie,
7670 struct ieee80211_channel *chan,
7671 enum nl80211_channel_type channel_type,
7672 unsigned int duration, gfp_t gfp)
7673{
7674 struct sk_buff *msg;
7675 void *hdr;
7676
7677 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7678 if (!msg)
7679 return;
7680
7681 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7682 if (!hdr) {
7683 nlmsg_free(msg);
7684 return;
7685 }
7686
David S. Miller9360ffd2012-03-29 04:41:26 -04007687 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7688 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7689 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7690 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7691 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7692 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007693
David S. Miller9360ffd2012-03-29 04:41:26 -04007694 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7695 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7696 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007697
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007698 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007699
7700 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7701 nl80211_mlme_mcgrp.id, gfp);
7702 return;
7703
7704 nla_put_failure:
7705 genlmsg_cancel(msg, hdr);
7706 nlmsg_free(msg);
7707}
7708
7709void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7710 struct net_device *netdev, u64 cookie,
7711 struct ieee80211_channel *chan,
7712 enum nl80211_channel_type channel_type,
7713 unsigned int duration, gfp_t gfp)
7714{
7715 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7716 rdev, netdev, cookie, chan,
7717 channel_type, duration, gfp);
7718}
7719
7720void nl80211_send_remain_on_channel_cancel(
7721 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7722 u64 cookie, struct ieee80211_channel *chan,
7723 enum nl80211_channel_type channel_type, gfp_t gfp)
7724{
7725 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7726 rdev, netdev, cookie, chan,
7727 channel_type, 0, gfp);
7728}
7729
Johannes Berg98b62182009-12-23 13:15:44 +01007730void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7731 struct net_device *dev, const u8 *mac_addr,
7732 struct station_info *sinfo, gfp_t gfp)
7733{
7734 struct sk_buff *msg;
7735
7736 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7737 if (!msg)
7738 return;
7739
John W. Linville66266b32012-03-15 13:25:41 -04007740 if (nl80211_send_station(msg, 0, 0, 0,
7741 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007742 nlmsg_free(msg);
7743 return;
7744 }
7745
7746 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7747 nl80211_mlme_mcgrp.id, gfp);
7748}
7749
Jouni Malinenec15e682011-03-23 15:29:52 +02007750void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7751 struct net_device *dev, const u8 *mac_addr,
7752 gfp_t gfp)
7753{
7754 struct sk_buff *msg;
7755 void *hdr;
7756
7757 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7758 if (!msg)
7759 return;
7760
7761 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7762 if (!hdr) {
7763 nlmsg_free(msg);
7764 return;
7765 }
7766
David S. Miller9360ffd2012-03-29 04:41:26 -04007767 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7768 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7769 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007770
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007771 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007772
7773 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7774 nl80211_mlme_mcgrp.id, gfp);
7775 return;
7776
7777 nla_put_failure:
7778 genlmsg_cancel(msg, hdr);
7779 nlmsg_free(msg);
7780}
7781
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007782static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7783 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007784{
7785 struct wireless_dev *wdev = dev->ieee80211_ptr;
7786 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7787 struct sk_buff *msg;
7788 void *hdr;
7789 int err;
7790 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7791
7792 if (!nlpid)
7793 return false;
7794
7795 msg = nlmsg_new(100, gfp);
7796 if (!msg)
7797 return true;
7798
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007799 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007800 if (!hdr) {
7801 nlmsg_free(msg);
7802 return true;
7803 }
7804
David S. Miller9360ffd2012-03-29 04:41:26 -04007805 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7806 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7807 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7808 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007809
7810 err = genlmsg_end(msg, hdr);
7811 if (err < 0) {
7812 nlmsg_free(msg);
7813 return true;
7814 }
7815
7816 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7817 return true;
7818
7819 nla_put_failure:
7820 genlmsg_cancel(msg, hdr);
7821 nlmsg_free(msg);
7822 return true;
7823}
7824
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007825bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7826{
7827 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7828 addr, gfp);
7829}
7830
7831bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7832 const u8 *addr, gfp_t gfp)
7833{
7834 return __nl80211_unexpected_frame(dev,
7835 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7836 addr, gfp);
7837}
7838
Johannes Berg2e161f72010-08-12 15:38:38 +02007839int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7840 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007841 int freq, int sig_dbm,
7842 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007843{
7844 struct sk_buff *msg;
7845 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007846
7847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7848 if (!msg)
7849 return -ENOMEM;
7850
Johannes Berg2e161f72010-08-12 15:38:38 +02007851 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007852 if (!hdr) {
7853 nlmsg_free(msg);
7854 return -ENOMEM;
7855 }
7856
David S. Miller9360ffd2012-03-29 04:41:26 -04007857 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7858 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7859 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7860 (sig_dbm &&
7861 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7862 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7863 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007864
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007865 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007866
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007867 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007868
7869 nla_put_failure:
7870 genlmsg_cancel(msg, hdr);
7871 nlmsg_free(msg);
7872 return -ENOBUFS;
7873}
7874
Johannes Berg2e161f72010-08-12 15:38:38 +02007875void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7876 struct net_device *netdev, u64 cookie,
7877 const u8 *buf, size_t len, bool ack,
7878 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007879{
7880 struct sk_buff *msg;
7881 void *hdr;
7882
7883 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7884 if (!msg)
7885 return;
7886
Johannes Berg2e161f72010-08-12 15:38:38 +02007887 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007888 if (!hdr) {
7889 nlmsg_free(msg);
7890 return;
7891 }
7892
David S. Miller9360ffd2012-03-29 04:41:26 -04007893 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7894 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7895 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7896 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7897 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7898 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007899
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007900 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007901
7902 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7903 return;
7904
7905 nla_put_failure:
7906 genlmsg_cancel(msg, hdr);
7907 nlmsg_free(msg);
7908}
7909
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007910void
7911nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7912 struct net_device *netdev,
7913 enum nl80211_cqm_rssi_threshold_event rssi_event,
7914 gfp_t gfp)
7915{
7916 struct sk_buff *msg;
7917 struct nlattr *pinfoattr;
7918 void *hdr;
7919
7920 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7921 if (!msg)
7922 return;
7923
7924 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7925 if (!hdr) {
7926 nlmsg_free(msg);
7927 return;
7928 }
7929
David S. Miller9360ffd2012-03-29 04:41:26 -04007930 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7931 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7932 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007933
7934 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7935 if (!pinfoattr)
7936 goto nla_put_failure;
7937
David S. Miller9360ffd2012-03-29 04:41:26 -04007938 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7939 rssi_event))
7940 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007941
7942 nla_nest_end(msg, pinfoattr);
7943
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007944 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007945
7946 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7947 nl80211_mlme_mcgrp.id, gfp);
7948 return;
7949
7950 nla_put_failure:
7951 genlmsg_cancel(msg, hdr);
7952 nlmsg_free(msg);
7953}
7954
Johannes Berge5497d72011-07-05 16:35:40 +02007955void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7956 struct net_device *netdev, const u8 *bssid,
7957 const u8 *replay_ctr, gfp_t gfp)
7958{
7959 struct sk_buff *msg;
7960 struct nlattr *rekey_attr;
7961 void *hdr;
7962
7963 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7964 if (!msg)
7965 return;
7966
7967 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7968 if (!hdr) {
7969 nlmsg_free(msg);
7970 return;
7971 }
7972
David S. Miller9360ffd2012-03-29 04:41:26 -04007973 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7974 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7975 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7976 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007977
7978 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7979 if (!rekey_attr)
7980 goto nla_put_failure;
7981
David S. Miller9360ffd2012-03-29 04:41:26 -04007982 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7983 NL80211_REPLAY_CTR_LEN, replay_ctr))
7984 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007985
7986 nla_nest_end(msg, rekey_attr);
7987
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007988 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007989
7990 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7991 nl80211_mlme_mcgrp.id, gfp);
7992 return;
7993
7994 nla_put_failure:
7995 genlmsg_cancel(msg, hdr);
7996 nlmsg_free(msg);
7997}
7998
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007999void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8000 struct net_device *netdev, int index,
8001 const u8 *bssid, bool preauth, gfp_t gfp)
8002{
8003 struct sk_buff *msg;
8004 struct nlattr *attr;
8005 void *hdr;
8006
8007 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8008 if (!msg)
8009 return;
8010
8011 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8012 if (!hdr) {
8013 nlmsg_free(msg);
8014 return;
8015 }
8016
David S. Miller9360ffd2012-03-29 04:41:26 -04008017 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8018 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8019 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008020
8021 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8022 if (!attr)
8023 goto nla_put_failure;
8024
David S. Miller9360ffd2012-03-29 04:41:26 -04008025 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8026 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8027 (preauth &&
8028 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8029 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008030
8031 nla_nest_end(msg, attr);
8032
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008033 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008034
8035 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8036 nl80211_mlme_mcgrp.id, gfp);
8037 return;
8038
8039 nla_put_failure:
8040 genlmsg_cancel(msg, hdr);
8041 nlmsg_free(msg);
8042}
8043
Thomas Pedersen53145262012-04-06 13:35:47 -07008044void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8045 struct net_device *netdev, int freq,
8046 enum nl80211_channel_type type, gfp_t gfp)
8047{
8048 struct sk_buff *msg;
8049 void *hdr;
8050
8051 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8052 if (!msg)
8053 return;
8054
8055 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8056 if (!hdr) {
8057 nlmsg_free(msg);
8058 return;
8059 }
8060
John W. Linville7eab0f62012-04-12 14:25:14 -04008061 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8062 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8063 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8064 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008065
8066 genlmsg_end(msg, hdr);
8067
8068 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8069 nl80211_mlme_mcgrp.id, gfp);
8070 return;
8071
8072 nla_put_failure:
8073 genlmsg_cancel(msg, hdr);
8074 nlmsg_free(msg);
8075}
8076
Johannes Bergc063dbf2010-11-24 08:10:05 +01008077void
8078nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8079 struct net_device *netdev, const u8 *peer,
8080 u32 num_packets, gfp_t gfp)
8081{
8082 struct sk_buff *msg;
8083 struct nlattr *pinfoattr;
8084 void *hdr;
8085
8086 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8087 if (!msg)
8088 return;
8089
8090 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8091 if (!hdr) {
8092 nlmsg_free(msg);
8093 return;
8094 }
8095
David S. Miller9360ffd2012-03-29 04:41:26 -04008096 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8097 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8098 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8099 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008100
8101 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8102 if (!pinfoattr)
8103 goto nla_put_failure;
8104
David S. Miller9360ffd2012-03-29 04:41:26 -04008105 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8106 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008107
8108 nla_nest_end(msg, pinfoattr);
8109
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008110 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008111
8112 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8113 nl80211_mlme_mcgrp.id, gfp);
8114 return;
8115
8116 nla_put_failure:
8117 genlmsg_cancel(msg, hdr);
8118 nlmsg_free(msg);
8119}
8120
Johannes Berg7f6cf312011-11-04 11:18:15 +01008121void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8122 u64 cookie, bool acked, gfp_t gfp)
8123{
8124 struct wireless_dev *wdev = dev->ieee80211_ptr;
8125 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8126 struct sk_buff *msg;
8127 void *hdr;
8128 int err;
8129
8130 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8131 if (!msg)
8132 return;
8133
8134 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8135 if (!hdr) {
8136 nlmsg_free(msg);
8137 return;
8138 }
8139
David S. Miller9360ffd2012-03-29 04:41:26 -04008140 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8141 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8142 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8143 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8144 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8145 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008146
8147 err = genlmsg_end(msg, hdr);
8148 if (err < 0) {
8149 nlmsg_free(msg);
8150 return;
8151 }
8152
8153 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8154 nl80211_mlme_mcgrp.id, gfp);
8155 return;
8156
8157 nla_put_failure:
8158 genlmsg_cancel(msg, hdr);
8159 nlmsg_free(msg);
8160}
8161EXPORT_SYMBOL(cfg80211_probe_status);
8162
Johannes Berg5e7602302011-11-04 11:18:17 +01008163void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8164 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008165 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e7602302011-11-04 11:18:17 +01008166{
8167 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8168 struct sk_buff *msg;
8169 void *hdr;
8170 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8171
8172 if (!nlpid)
8173 return;
8174
8175 msg = nlmsg_new(len + 100, gfp);
8176 if (!msg)
8177 return;
8178
8179 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8180 if (!hdr) {
8181 nlmsg_free(msg);
8182 return;
8183 }
8184
David S. Miller9360ffd2012-03-29 04:41:26 -04008185 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8186 (freq &&
8187 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8188 (sig_dbm &&
8189 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8190 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8191 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01008192
8193 genlmsg_end(msg, hdr);
8194
8195 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8196 return;
8197
8198 nla_put_failure:
8199 genlmsg_cancel(msg, hdr);
8200 nlmsg_free(msg);
8201}
8202EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8203
Jouni Malinen026331c2010-02-15 12:53:10 +02008204static int nl80211_netlink_notify(struct notifier_block * nb,
8205 unsigned long state,
8206 void *_notify)
8207{
8208 struct netlink_notify *notify = _notify;
8209 struct cfg80211_registered_device *rdev;
8210 struct wireless_dev *wdev;
8211
8212 if (state != NETLINK_URELEASE)
8213 return NOTIFY_DONE;
8214
8215 rcu_read_lock();
8216
Johannes Berg5e7602302011-11-04 11:18:17 +01008217 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008218 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008219 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e7602302011-11-04 11:18:17 +01008220 if (rdev->ap_beacons_nlpid == notify->pid)
8221 rdev->ap_beacons_nlpid = 0;
8222 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008223
8224 rcu_read_unlock();
8225
8226 return NOTIFY_DONE;
8227}
8228
8229static struct notifier_block nl80211_netlink_notifier = {
8230 .notifier_call = nl80211_netlink_notify,
8231};
8232
Johannes Berg55682962007-09-20 13:09:35 -04008233/* initialisation/exit functions */
8234
8235int nl80211_init(void)
8236{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008237 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008238
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008239 err = genl_register_family_with_ops(&nl80211_fam,
8240 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008241 if (err)
8242 return err;
8243
Johannes Berg55682962007-09-20 13:09:35 -04008244 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8245 if (err)
8246 goto err_out;
8247
Johannes Berg2a519312009-02-10 21:25:55 +01008248 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8249 if (err)
8250 goto err_out;
8251
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008252 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8253 if (err)
8254 goto err_out;
8255
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008256 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8257 if (err)
8258 goto err_out;
8259
Johannes Bergaff89a92009-07-01 21:26:51 +02008260#ifdef CONFIG_NL80211_TESTMODE
8261 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8262 if (err)
8263 goto err_out;
8264#endif
8265
Jouni Malinen026331c2010-02-15 12:53:10 +02008266 err = netlink_register_notifier(&nl80211_netlink_notifier);
8267 if (err)
8268 goto err_out;
8269
Johannes Berg55682962007-09-20 13:09:35 -04008270 return 0;
8271 err_out:
8272 genl_unregister_family(&nl80211_fam);
8273 return err;
8274}
8275
8276void nl80211_exit(void)
8277{
Jouni Malinen026331c2010-02-15 12:53:10 +02008278 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008279 genl_unregister_family(&nl80211_fam);
8280}