blob: 83d71d6b63a45570dbdceea6a4fdb82c70c93d21 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530207 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530208 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400209};
210
Johannes Berge31b8212010-10-05 19:39:30 +0200211/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000212static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200213 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200214 [NL80211_KEY_IDX] = { .type = NLA_U8 },
215 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200216 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200217 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
218 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200219 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100220 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
221};
222
223/* policy for the key default flags */
224static const struct nla_policy
225nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
226 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
227 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200228};
229
Johannes Bergff1b6e62011-05-04 15:37:28 +0200230/* policy for WoWLAN attributes */
231static const struct nla_policy
232nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
233 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
235 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb132011-07-13 10:48:55 +0200237 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200241};
242
Johannes Berge5497d72011-07-05 16:35:40 +0200243/* policy for GTK rekey offload attributes */
244static const struct nla_policy
245nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
246 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
247 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
248 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249};
250
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300251static const struct nla_policy
252nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
253 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_SSID_LEN },
255};
256
Holger Schuriga0438972009-11-11 11:30:02 +0100257/* ifidx get helper */
258static int nl80211_get_ifidx(struct netlink_callback *cb)
259{
260 int res;
261
262 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
263 nl80211_fam.attrbuf, nl80211_fam.maxattr,
264 nl80211_policy);
265 if (res)
266 return res;
267
268 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
269 return -EINVAL;
270
271 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
272 if (!res)
273 return -EINVAL;
274 return res;
275}
276
Johannes Berg67748892010-10-04 21:14:06 +0200277static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
278 struct netlink_callback *cb,
279 struct cfg80211_registered_device **rdev,
280 struct net_device **dev)
281{
282 int ifidx = cb->args[0];
283 int err;
284
285 if (!ifidx)
286 ifidx = nl80211_get_ifidx(cb);
287 if (ifidx < 0)
288 return ifidx;
289
290 cb->args[0] = ifidx;
291
292 rtnl_lock();
293
294 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
295 if (!*dev) {
296 err = -ENODEV;
297 goto out_rtnl;
298 }
299
300 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100301 if (IS_ERR(*rdev)) {
302 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200303 goto out_rtnl;
304 }
305
306 return 0;
307 out_rtnl:
308 rtnl_unlock();
309 return err;
310}
311
312static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313{
314 cfg80211_unlock_rdev(rdev);
315 rtnl_unlock();
316}
317
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100318/* IE validation */
319static bool is_valid_ie_attr(const struct nlattr *attr)
320{
321 const u8 *pos;
322 int len;
323
324 if (!attr)
325 return true;
326
327 pos = nla_data(attr);
328 len = nla_len(attr);
329
330 while (len) {
331 u8 elemlen;
332
333 if (len < 2)
334 return false;
335 len -= 2;
336
337 elemlen = pos[1];
338 if (elemlen > len)
339 return false;
340
341 len -= elemlen;
342 pos += 2 + elemlen;
343 }
344
345 return true;
346}
347
Johannes Berg55682962007-09-20 13:09:35 -0400348/* message building helper */
349static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
350 int flags, u8 cmd)
351{
352 /* since there is no private header just add the generic one */
353 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354}
355
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400356static int nl80211_msg_put_channel(struct sk_buff *msg,
357 struct ieee80211_channel *chan)
358{
David S. Miller9360ffd2012-03-29 04:41:26 -0400359 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360 chan->center_freq))
361 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400362
David S. Miller9360ffd2012-03-29 04:41:26 -0400363 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
364 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
365 goto nla_put_failure;
366 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
367 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
368 goto nla_put_failure;
369 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
370 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
371 goto nla_put_failure;
372 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
373 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
374 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400375
David S. Miller9360ffd2012-03-29 04:41:26 -0400376 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
377 DBM_TO_MBM(chan->max_power)))
378 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400379
380 return 0;
381
382 nla_put_failure:
383 return -ENOBUFS;
384}
385
Johannes Berg55682962007-09-20 13:09:35 -0400386/* netlink command implementations */
387
Johannes Bergb9454e82009-07-08 13:29:08 +0200388struct key_parse {
389 struct key_params p;
390 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394};
395
396static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
397{
398 struct nlattr *tb[NL80211_KEY_MAX + 1];
399 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
400 nl80211_key_policy);
401 if (err)
402 return err;
403
404 k->def = !!tb[NL80211_KEY_DEFAULT];
405 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (k->def) {
408 k->def_uni = true;
409 k->def_multi = true;
410 }
411 if (k->defmgmt)
412 k->def_multi = true;
413
Johannes Bergb9454e82009-07-08 13:29:08 +0200414 if (tb[NL80211_KEY_IDX])
415 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
416
417 if (tb[NL80211_KEY_DATA]) {
418 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
419 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
420 }
421
422 if (tb[NL80211_KEY_SEQ]) {
423 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
424 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
425 }
426
427 if (tb[NL80211_KEY_CIPHER])
428 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
429
Johannes Berge31b8212010-10-05 19:39:30 +0200430 if (tb[NL80211_KEY_TYPE]) {
431 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
432 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
433 return -EINVAL;
434 }
435
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100436 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
437 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100438 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
439 tb[NL80211_KEY_DEFAULT_TYPES],
440 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100441 if (err)
442 return err;
443
444 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
445 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
446 }
447
Johannes Bergb9454e82009-07-08 13:29:08 +0200448 return 0;
449}
450
451static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
452{
453 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
454 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
455 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
456 }
457
458 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
459 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
460 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
461 }
462
463 if (info->attrs[NL80211_ATTR_KEY_IDX])
464 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
465
466 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
467 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
468
469 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
470 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
471
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100472 if (k->def) {
473 k->def_uni = true;
474 k->def_multi = true;
475 }
476 if (k->defmgmt)
477 k->def_multi = true;
478
Johannes Berge31b8212010-10-05 19:39:30 +0200479 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
480 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
481 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
482 return -EINVAL;
483 }
484
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100485 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
486 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
487 int err = nla_parse_nested(
488 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
489 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
490 nl80211_key_default_policy);
491 if (err)
492 return err;
493
494 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
495 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 return 0;
499}
500
501static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
502{
503 int err;
504
505 memset(k, 0, sizeof(*k));
506 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200507 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200508
509 if (info->attrs[NL80211_ATTR_KEY])
510 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
511 else
512 err = nl80211_parse_key_old(info, k);
513
514 if (err)
515 return err;
516
517 if (k->def && k->defmgmt)
518 return -EINVAL;
519
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (k->defmgmt) {
521 if (k->def_uni || !k->def_multi)
522 return -EINVAL;
523 }
524
Johannes Bergb9454e82009-07-08 13:29:08 +0200525 if (k->idx != -1) {
526 if (k->defmgmt) {
527 if (k->idx < 4 || k->idx > 5)
528 return -EINVAL;
529 } else if (k->def) {
530 if (k->idx < 0 || k->idx > 3)
531 return -EINVAL;
532 } else {
533 if (k->idx < 0 || k->idx > 5)
534 return -EINVAL;
535 }
536 }
537
538 return 0;
539}
540
Johannes Bergfffd0932009-07-08 14:22:54 +0200541static struct cfg80211_cached_keys *
542nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
543 struct nlattr *keys)
544{
545 struct key_parse parse;
546 struct nlattr *key;
547 struct cfg80211_cached_keys *result;
548 int rem, err, def = 0;
549
550 result = kzalloc(sizeof(*result), GFP_KERNEL);
551 if (!result)
552 return ERR_PTR(-ENOMEM);
553
554 result->def = -1;
555 result->defmgmt = -1;
556
557 nla_for_each_nested(key, keys, rem) {
558 memset(&parse, 0, sizeof(parse));
559 parse.idx = -1;
560
561 err = nl80211_parse_key_new(key, &parse);
562 if (err)
563 goto error;
564 err = -EINVAL;
565 if (!parse.p.key)
566 goto error;
567 if (parse.idx < 0 || parse.idx > 4)
568 goto error;
569 if (parse.def) {
570 if (def)
571 goto error;
572 def = 1;
573 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (!parse.def_uni || !parse.def_multi)
575 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 } else if (parse.defmgmt)
577 goto error;
578 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200579 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200580 if (err)
581 goto error;
582 result->params[parse.idx].cipher = parse.p.cipher;
583 result->params[parse.idx].key_len = parse.p.key_len;
584 result->params[parse.idx].key = result->data[parse.idx];
585 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
586 }
587
588 return result;
589 error:
590 kfree(result);
591 return ERR_PTR(err);
592}
593
594static int nl80211_key_allowed(struct wireless_dev *wdev)
595{
596 ASSERT_WDEV_LOCK(wdev);
597
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 switch (wdev->iftype) {
599 case NL80211_IFTYPE_AP:
600 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700602 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 break;
604 case NL80211_IFTYPE_ADHOC:
605 if (!wdev->current_bss)
606 return -ENOLINK;
607 break;
608 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200609 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200610 if (wdev->sme_state != CFG80211_SME_CONNECTED)
611 return -ENOLINK;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 return 0;
618}
619
Johannes Berg7527a782011-05-13 10:58:57 +0200620static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
621{
622 struct nlattr *nl_modes = nla_nest_start(msg, attr);
623 int i;
624
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400630 if ((ifmodes & 1) && nla_put_flag(msg, i))
631 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637 return 0;
638
639nla_put_failure:
640 return -ENOBUFS;
641}
642
643static int nl80211_put_iface_combinations(struct wiphy *wiphy,
644 struct sk_buff *msg)
645{
646 struct nlattr *nl_combis;
647 int i, j;
648
649 nl_combis = nla_nest_start(msg,
650 NL80211_ATTR_INTERFACE_COMBINATIONS);
651 if (!nl_combis)
652 goto nla_put_failure;
653
654 for (i = 0; i < wiphy->n_iface_combinations; i++) {
655 const struct ieee80211_iface_combination *c;
656 struct nlattr *nl_combi, *nl_limits;
657
658 c = &wiphy->iface_combinations[i];
659
660 nl_combi = nla_nest_start(msg, i + 1);
661 if (!nl_combi)
662 goto nla_put_failure;
663
664 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
665 if (!nl_limits)
666 goto nla_put_failure;
667
668 for (j = 0; j < c->n_limits; j++) {
669 struct nlattr *nl_limit;
670
671 nl_limit = nla_nest_start(msg, j + 1);
672 if (!nl_limit)
673 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400674 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
675 c->limits[j].max))
676 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200677 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
678 c->limits[j].types))
679 goto nla_put_failure;
680 nla_nest_end(msg, nl_limit);
681 }
682
683 nla_nest_end(msg, nl_limits);
684
David S. Miller9360ffd2012-03-29 04:41:26 -0400685 if (c->beacon_int_infra_match &&
686 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
687 goto nla_put_failure;
688 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
689 c->num_different_channels) ||
690 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
691 c->max_interfaces))
692 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200693
694 nla_nest_end(msg, nl_combi);
695 }
696
697 nla_nest_end(msg, nl_combis);
698
699 return 0;
700nla_put_failure:
701 return -ENOBUFS;
702}
703
Johannes Berg55682962007-09-20 13:09:35 -0400704static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
705 struct cfg80211_registered_device *dev)
706{
707 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100708 struct nlattr *nl_bands, *nl_band;
709 struct nlattr *nl_freqs, *nl_freq;
710 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100711 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100712 enum ieee80211_band band;
713 struct ieee80211_channel *chan;
714 struct ieee80211_rate *rate;
715 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200716 const struct ieee80211_txrx_stypes *mgmt_stypes =
717 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400718
719 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
720 if (!hdr)
721 return -1;
722
David S. Miller9360ffd2012-03-29 04:41:26 -0400723 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
724 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
725 nla_put_u32(msg, NL80211_ATTR_GENERATION,
726 cfg80211_rdev_list_generation) ||
727 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
728 dev->wiphy.retry_short) ||
729 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
730 dev->wiphy.retry_long) ||
731 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
732 dev->wiphy.frag_threshold) ||
733 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
734 dev->wiphy.rts_threshold) ||
735 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
736 dev->wiphy.coverage_class) ||
737 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
738 dev->wiphy.max_scan_ssids) ||
739 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
740 dev->wiphy.max_sched_scan_ssids) ||
741 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
742 dev->wiphy.max_scan_ie_len) ||
743 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
744 dev->wiphy.max_sched_scan_ie_len) ||
745 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
746 dev->wiphy.max_match_sets))
747 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200748
David S. Miller9360ffd2012-03-29 04:41:26 -0400749 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
750 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
751 goto nla_put_failure;
752 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
753 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
754 goto nla_put_failure;
755 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
756 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
757 goto nla_put_failure;
758 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
759 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
760 goto nla_put_failure;
761 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
762 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
763 goto nla_put_failure;
764 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
765 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
766 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200767
David S. Miller9360ffd2012-03-29 04:41:26 -0400768 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
769 sizeof(u32) * dev->wiphy.n_cipher_suites,
770 dev->wiphy.cipher_suites))
771 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100772
David S. Miller9360ffd2012-03-29 04:41:26 -0400773 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
774 dev->wiphy.max_num_pmkids))
775 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530776
David S. Miller9360ffd2012-03-29 04:41:26 -0400777 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
778 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
779 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200780
David S. Miller9360ffd2012-03-29 04:41:26 -0400781 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
782 dev->wiphy.available_antennas_tx) ||
783 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
784 dev->wiphy.available_antennas_rx))
785 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100786
David S. Miller9360ffd2012-03-29 04:41:26 -0400787 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
788 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
789 dev->wiphy.probe_resp_offload))
790 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200791
Bruno Randolf7f531e02010-12-16 11:30:22 +0900792 if ((dev->wiphy.available_antennas_tx ||
793 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900794 u32 tx_ant = 0, rx_ant = 0;
795 int res;
796 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
797 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
799 tx_ant) ||
800 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
801 rx_ant))
802 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900803 }
804 }
805
Johannes Berg7527a782011-05-13 10:58:57 +0200806 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
807 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700808 goto nla_put_failure;
809
Johannes Bergee688b002008-01-24 19:38:39 +0100810 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
811 if (!nl_bands)
812 goto nla_put_failure;
813
814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
815 if (!dev->wiphy.bands[band])
816 continue;
817
818 nl_band = nla_nest_start(msg, band);
819 if (!nl_band)
820 goto nla_put_failure;
821
Johannes Bergd51626d2008-10-09 12:20:13 +0200822 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
824 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
825 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
826 &dev->wiphy.bands[band]->ht_cap.mcs) ||
827 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
828 dev->wiphy.bands[band]->ht_cap.cap) ||
829 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
830 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
831 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
832 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
833 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200834
Johannes Bergee688b002008-01-24 19:38:39 +0100835 /* add frequencies */
836 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
837 if (!nl_freqs)
838 goto nla_put_failure;
839
840 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
841 nl_freq = nla_nest_start(msg, i);
842 if (!nl_freq)
843 goto nla_put_failure;
844
845 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100846
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400847 if (nl80211_msg_put_channel(msg, chan))
848 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200849
Johannes Bergee688b002008-01-24 19:38:39 +0100850 nla_nest_end(msg, nl_freq);
851 }
852
853 nla_nest_end(msg, nl_freqs);
854
855 /* add bitrates */
856 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
857 if (!nl_rates)
858 goto nla_put_failure;
859
860 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
861 nl_rate = nla_nest_start(msg, i);
862 if (!nl_rate)
863 goto nla_put_failure;
864
865 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
867 rate->bitrate))
868 goto nla_put_failure;
869 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
870 nla_put_flag(msg,
871 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
872 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100873
874 nla_nest_end(msg, nl_rate);
875 }
876
877 nla_nest_end(msg, nl_rates);
878
879 nla_nest_end(msg, nl_band);
880 }
881 nla_nest_end(msg, nl_bands);
882
Johannes Berg8fdc6212009-03-14 09:34:01 +0100883 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
884 if (!nl_cmds)
885 goto nla_put_failure;
886
887 i = 0;
888#define CMD(op, n) \
889 do { \
890 if (dev->ops->op) { \
891 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
893 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894 } \
895 } while (0)
896
897 CMD(add_virtual_intf, NEW_INTERFACE);
898 CMD(change_virtual_intf, SET_INTERFACE);
899 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100900 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100901 CMD(add_station, NEW_STATION);
902 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800903 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100904 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200905 CMD(auth, AUTHENTICATE);
906 CMD(assoc, ASSOCIATE);
907 CMD(deauth, DEAUTHENTICATE);
908 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200909 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100910 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100911 CMD(set_pmksa, SET_PMKSA);
912 CMD(del_pmksa, DEL_PMKSA);
913 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100914 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
915 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200916 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200917 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100918 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100919 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200920 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
922 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +0200923 }
Johannes Bergf444de02010-05-05 15:25:02 +0200924 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400925 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300926 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
927 CMD(tdls_mgmt, TDLS_MGMT);
928 CMD(tdls_oper, TDLS_OPER);
929 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300930 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
931 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100932 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100933 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100934 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
935 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
937 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +0100938 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100939
Kalle Valo4745fc02011-11-17 19:06:10 +0200940#ifdef CONFIG_NL80211_TESTMODE
941 CMD(testmode_cmd, TESTMODE);
942#endif
943
Johannes Berg8fdc6212009-03-14 09:34:01 +0100944#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200945
Johannes Berg6829c872009-07-02 09:13:27 +0200946 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200947 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400948 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
949 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200950 }
951
Johannes Berg6829c872009-07-02 09:13:27 +0200952 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200953 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400954 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
955 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200956 }
957
Johannes Berg8fdc6212009-03-14 09:34:01 +0100958 nla_nest_end(msg, nl_cmds);
959
Johannes Berg7c4ef712011-11-18 15:33:48 +0100960 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -0400961 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
962 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
963 dev->wiphy.max_remain_on_channel_duration))
964 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +0100965
David S. Miller9360ffd2012-03-29 04:41:26 -0400966 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
967 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
968 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100969
Johannes Berg2e161f72010-08-12 15:38:38 +0200970 if (mgmt_stypes) {
971 u16 stypes;
972 struct nlattr *nl_ftypes, *nl_ifs;
973 enum nl80211_iftype ift;
974
975 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
976 if (!nl_ifs)
977 goto nla_put_failure;
978
979 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
980 nl_ftypes = nla_nest_start(msg, ift);
981 if (!nl_ftypes)
982 goto nla_put_failure;
983 i = 0;
984 stypes = mgmt_stypes[ift].tx;
985 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400986 if ((stypes & 1) &&
987 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
988 (i << 4) | IEEE80211_FTYPE_MGMT))
989 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +0200990 stypes >>= 1;
991 i++;
992 }
993 nla_nest_end(msg, nl_ftypes);
994 }
995
Johannes Berg74b70a42010-08-24 12:15:53 +0200996 nla_nest_end(msg, nl_ifs);
997
Johannes Berg2e161f72010-08-12 15:38:38 +0200998 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
999 if (!nl_ifs)
1000 goto nla_put_failure;
1001
1002 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1003 nl_ftypes = nla_nest_start(msg, ift);
1004 if (!nl_ftypes)
1005 goto nla_put_failure;
1006 i = 0;
1007 stypes = mgmt_stypes[ift].rx;
1008 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001009 if ((stypes & 1) &&
1010 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1011 (i << 4) | IEEE80211_FTYPE_MGMT))
1012 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001013 stypes >>= 1;
1014 i++;
1015 }
1016 nla_nest_end(msg, nl_ftypes);
1017 }
1018 nla_nest_end(msg, nl_ifs);
1019 }
1020
Johannes Bergff1b6e62011-05-04 15:37:28 +02001021 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1022 struct nlattr *nl_wowlan;
1023
1024 nl_wowlan = nla_nest_start(msg,
1025 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1026 if (!nl_wowlan)
1027 goto nla_put_failure;
1028
David S. Miller9360ffd2012-03-29 04:41:26 -04001029 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1031 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1033 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1034 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1035 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1036 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1037 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1038 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1039 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1040 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1041 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1042 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1043 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1044 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1045 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001046 if (dev->wiphy.wowlan.n_patterns) {
1047 struct nl80211_wowlan_pattern_support pat = {
1048 .max_patterns = dev->wiphy.wowlan.n_patterns,
1049 .min_pattern_len =
1050 dev->wiphy.wowlan.pattern_min_len,
1051 .max_pattern_len =
1052 dev->wiphy.wowlan.pattern_max_len,
1053 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001054 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1055 sizeof(pat), &pat))
1056 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001057 }
1058
1059 nla_nest_end(msg, nl_wowlan);
1060 }
1061
Johannes Berg7527a782011-05-13 10:58:57 +02001062 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1063 dev->wiphy.software_iftypes))
1064 goto nla_put_failure;
1065
1066 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1067 goto nla_put_failure;
1068
David S. Miller9360ffd2012-03-29 04:41:26 -04001069 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1070 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1071 dev->wiphy.ap_sme_capa))
1072 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001073
David S. Miller9360ffd2012-03-29 04:41:26 -04001074 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1075 dev->wiphy.features))
1076 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001077
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (dev->wiphy.ht_capa_mod_mask &&
1079 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1080 sizeof(*dev->wiphy.ht_capa_mod_mask),
1081 dev->wiphy.ht_capa_mod_mask))
1082 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001083
Johannes Berg55682962007-09-20 13:09:35 -04001084 return genlmsg_end(msg, hdr);
1085
1086 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001087 genlmsg_cancel(msg, hdr);
1088 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001089}
1090
1091static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1092{
1093 int idx = 0;
1094 int start = cb->args[0];
1095 struct cfg80211_registered_device *dev;
1096
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001097 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001098 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001099 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1100 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001101 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001102 continue;
1103 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1104 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001105 dev) < 0) {
1106 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001107 break;
Julius Volzb4637272008-07-08 14:02:19 +02001108 }
Johannes Berg55682962007-09-20 13:09:35 -04001109 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001110 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001111
1112 cb->args[0] = idx;
1113
1114 return skb->len;
1115}
1116
1117static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1118{
1119 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001120 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001121
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001123 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001124 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001125
Johannes Berg4c476992010-10-04 21:36:35 +02001126 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1127 nlmsg_free(msg);
1128 return -ENOBUFS;
1129 }
Johannes Berg55682962007-09-20 13:09:35 -04001130
Johannes Berg134e6372009-07-10 09:51:34 +00001131 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001132}
1133
Jouni Malinen31888482008-10-30 16:59:24 +02001134static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1135 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1136 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1137 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1138 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1139 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1140};
1141
1142static int parse_txq_params(struct nlattr *tb[],
1143 struct ieee80211_txq_params *txq_params)
1144{
Johannes Berga3304b02012-03-28 11:04:24 +02001145 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001146 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1147 !tb[NL80211_TXQ_ATTR_AIFS])
1148 return -EINVAL;
1149
Johannes Berga3304b02012-03-28 11:04:24 +02001150 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001151 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1152 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1153 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1154 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1155
Johannes Berga3304b02012-03-28 11:04:24 +02001156 if (txq_params->ac >= NL80211_NUM_ACS)
1157 return -EINVAL;
1158
Jouni Malinen31888482008-10-30 16:59:24 +02001159 return 0;
1160}
1161
Johannes Bergf444de02010-05-05 15:25:02 +02001162static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1163{
1164 /*
1165 * You can only set the channel explicitly for AP, mesh
1166 * and WDS type interfaces; all others have their channel
1167 * managed via their respective "establish a connection"
1168 * command (connect, join, ...)
1169 *
1170 * Monitors are special as they are normally slaved to
1171 * whatever else is going on, so they behave as though
1172 * you tried setting the wiphy channel itself.
1173 */
1174 return !wdev ||
1175 wdev->iftype == NL80211_IFTYPE_AP ||
1176 wdev->iftype == NL80211_IFTYPE_WDS ||
1177 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001178 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1179 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001180}
1181
1182static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1183 struct wireless_dev *wdev,
1184 struct genl_info *info)
1185{
1186 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1187 u32 freq;
1188 int result;
1189
1190 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1191 return -EINVAL;
1192
1193 if (!nl80211_can_set_dev_channel(wdev))
1194 return -EOPNOTSUPP;
1195
1196 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1197 channel_type = nla_get_u32(info->attrs[
1198 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1199 if (channel_type != NL80211_CHAN_NO_HT &&
1200 channel_type != NL80211_CHAN_HT20 &&
1201 channel_type != NL80211_CHAN_HT40PLUS &&
1202 channel_type != NL80211_CHAN_HT40MINUS)
1203 return -EINVAL;
1204 }
1205
1206 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1207
1208 mutex_lock(&rdev->devlist_mtx);
1209 if (wdev) {
1210 wdev_lock(wdev);
1211 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1212 wdev_unlock(wdev);
1213 } else {
1214 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1215 }
1216 mutex_unlock(&rdev->devlist_mtx);
1217
1218 return result;
1219}
1220
1221static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1222{
Johannes Berg4c476992010-10-04 21:36:35 +02001223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1224 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001225
Johannes Berg4c476992010-10-04 21:36:35 +02001226 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001227}
1228
Bill Jordane8347eb2010-10-01 13:54:28 -04001229static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1230{
Johannes Berg43b19952010-10-07 13:10:30 +02001231 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1232 struct net_device *dev = info->user_ptr[1];
1233 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001234 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001235
1236 if (!info->attrs[NL80211_ATTR_MAC])
1237 return -EINVAL;
1238
Johannes Berg43b19952010-10-07 13:10:30 +02001239 if (netif_running(dev))
1240 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001241
Johannes Berg43b19952010-10-07 13:10:30 +02001242 if (!rdev->ops->set_wds_peer)
1243 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001244
Johannes Berg43b19952010-10-07 13:10:30 +02001245 if (wdev->iftype != NL80211_IFTYPE_WDS)
1246 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001247
1248 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001249 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001250}
1251
1252
Johannes Berg55682962007-09-20 13:09:35 -04001253static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1254{
1255 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001256 struct net_device *netdev = NULL;
1257 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001258 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001259 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001260 u32 changed;
1261 u8 retry_short = 0, retry_long = 0;
1262 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001263 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001264
Johannes Bergf444de02010-05-05 15:25:02 +02001265 /*
1266 * Try to find the wiphy and netdev. Normally this
1267 * function shouldn't need the netdev, but this is
1268 * done for backward compatibility -- previously
1269 * setting the channel was done per wiphy, but now
1270 * it is per netdev. Previous userland like hostapd
1271 * also passed a netdev to set_wiphy, so that it is
1272 * possible to let that go to the right netdev!
1273 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001274 mutex_lock(&cfg80211_mutex);
1275
Johannes Bergf444de02010-05-05 15:25:02 +02001276 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1277 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1278
1279 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1280 if (netdev && netdev->ieee80211_ptr) {
1281 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1282 mutex_lock(&rdev->mtx);
1283 } else
1284 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001285 }
1286
Johannes Bergf444de02010-05-05 15:25:02 +02001287 if (!netdev) {
1288 rdev = __cfg80211_rdev_from_info(info);
1289 if (IS_ERR(rdev)) {
1290 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001291 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001292 }
1293 wdev = NULL;
1294 netdev = NULL;
1295 result = 0;
1296
1297 mutex_lock(&rdev->mtx);
1298 } else if (netif_running(netdev) &&
1299 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1300 wdev = netdev->ieee80211_ptr;
1301 else
1302 wdev = NULL;
1303
1304 /*
1305 * end workaround code, by now the rdev is available
1306 * and locked, and wdev may or may not be NULL.
1307 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001308
1309 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001310 result = cfg80211_dev_rename(
1311 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001312
1313 mutex_unlock(&cfg80211_mutex);
1314
1315 if (result)
1316 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001317
Jouni Malinen31888482008-10-30 16:59:24 +02001318 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1319 struct ieee80211_txq_params txq_params;
1320 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1321
1322 if (!rdev->ops->set_txq_params) {
1323 result = -EOPNOTSUPP;
1324 goto bad_res;
1325 }
1326
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001327 if (!netdev) {
1328 result = -EINVAL;
1329 goto bad_res;
1330 }
1331
Johannes Berg133a3ff2011-11-03 14:50:13 +01001332 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1333 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1334 result = -EINVAL;
1335 goto bad_res;
1336 }
1337
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001338 if (!netif_running(netdev)) {
1339 result = -ENETDOWN;
1340 goto bad_res;
1341 }
1342
Jouni Malinen31888482008-10-30 16:59:24 +02001343 nla_for_each_nested(nl_txq_params,
1344 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1345 rem_txq_params) {
1346 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1347 nla_data(nl_txq_params),
1348 nla_len(nl_txq_params),
1349 txq_params_policy);
1350 result = parse_txq_params(tb, &txq_params);
1351 if (result)
1352 goto bad_res;
1353
1354 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001355 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001356 &txq_params);
1357 if (result)
1358 goto bad_res;
1359 }
1360 }
1361
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001362 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001363 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001364 if (result)
1365 goto bad_res;
1366 }
1367
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001368 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1369 enum nl80211_tx_power_setting type;
1370 int idx, mbm = 0;
1371
1372 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001373 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001374 goto bad_res;
1375 }
1376
1377 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1378 type = nla_get_u32(info->attrs[idx]);
1379
1380 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1381 (type != NL80211_TX_POWER_AUTOMATIC)) {
1382 result = -EINVAL;
1383 goto bad_res;
1384 }
1385
1386 if (type != NL80211_TX_POWER_AUTOMATIC) {
1387 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1388 mbm = nla_get_u32(info->attrs[idx]);
1389 }
1390
1391 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1392 if (result)
1393 goto bad_res;
1394 }
1395
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001396 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1397 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1398 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001399 if ((!rdev->wiphy.available_antennas_tx &&
1400 !rdev->wiphy.available_antennas_rx) ||
1401 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001402 result = -EOPNOTSUPP;
1403 goto bad_res;
1404 }
1405
1406 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1407 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1408
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001409 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001410 * available antenna masks, except for the "all" mask */
1411 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1412 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001413 result = -EINVAL;
1414 goto bad_res;
1415 }
1416
Bruno Randolf7f531e02010-12-16 11:30:22 +09001417 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1418 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001419
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001420 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1421 if (result)
1422 goto bad_res;
1423 }
1424
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001425 changed = 0;
1426
1427 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1428 retry_short = nla_get_u8(
1429 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1430 if (retry_short == 0) {
1431 result = -EINVAL;
1432 goto bad_res;
1433 }
1434 changed |= WIPHY_PARAM_RETRY_SHORT;
1435 }
1436
1437 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1438 retry_long = nla_get_u8(
1439 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1440 if (retry_long == 0) {
1441 result = -EINVAL;
1442 goto bad_res;
1443 }
1444 changed |= WIPHY_PARAM_RETRY_LONG;
1445 }
1446
1447 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1448 frag_threshold = nla_get_u32(
1449 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1450 if (frag_threshold < 256) {
1451 result = -EINVAL;
1452 goto bad_res;
1453 }
1454 if (frag_threshold != (u32) -1) {
1455 /*
1456 * Fragments (apart from the last one) are required to
1457 * have even length. Make the fragmentation code
1458 * simpler by stripping LSB should someone try to use
1459 * odd threshold value.
1460 */
1461 frag_threshold &= ~0x1;
1462 }
1463 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1464 }
1465
1466 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1467 rts_threshold = nla_get_u32(
1468 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1469 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1470 }
1471
Lukáš Turek81077e82009-12-21 22:50:47 +01001472 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1473 coverage_class = nla_get_u8(
1474 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1475 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1476 }
1477
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001478 if (changed) {
1479 u8 old_retry_short, old_retry_long;
1480 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001481 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001482
1483 if (!rdev->ops->set_wiphy_params) {
1484 result = -EOPNOTSUPP;
1485 goto bad_res;
1486 }
1487
1488 old_retry_short = rdev->wiphy.retry_short;
1489 old_retry_long = rdev->wiphy.retry_long;
1490 old_frag_threshold = rdev->wiphy.frag_threshold;
1491 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001492 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001493
1494 if (changed & WIPHY_PARAM_RETRY_SHORT)
1495 rdev->wiphy.retry_short = retry_short;
1496 if (changed & WIPHY_PARAM_RETRY_LONG)
1497 rdev->wiphy.retry_long = retry_long;
1498 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1499 rdev->wiphy.frag_threshold = frag_threshold;
1500 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1501 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001502 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1503 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001504
1505 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1506 if (result) {
1507 rdev->wiphy.retry_short = old_retry_short;
1508 rdev->wiphy.retry_long = old_retry_long;
1509 rdev->wiphy.frag_threshold = old_frag_threshold;
1510 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001511 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001512 }
1513 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001514
Johannes Berg306d6112008-12-08 12:39:04 +01001515 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001516 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001517 if (netdev)
1518 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001519 return result;
1520}
1521
1522
1523static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001524 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001525 struct net_device *dev)
1526{
1527 void *hdr;
1528
1529 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1530 if (!hdr)
1531 return -1;
1532
David S. Miller9360ffd2012-03-29 04:41:26 -04001533 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1534 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1535 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1536 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1537 dev->ieee80211_ptr->iftype) ||
1538 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1539 rdev->devlist_generation ^
1540 (cfg80211_rdev_list_generation << 2)))
1541 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001542
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001543 if (rdev->ops->get_channel) {
1544 struct ieee80211_channel *chan;
1545 enum nl80211_channel_type channel_type;
1546
1547 chan = rdev->ops->get_channel(&rdev->wiphy, &channel_type);
John W. Linville59ef43e2012-04-18 14:17:13 -04001548 if (chan &&
1549 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1550 chan->center_freq) ||
1551 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1552 channel_type)))
1553 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001554 }
1555
Johannes Berg55682962007-09-20 13:09:35 -04001556 return genlmsg_end(msg, hdr);
1557
1558 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001559 genlmsg_cancel(msg, hdr);
1560 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001561}
1562
1563static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1564{
1565 int wp_idx = 0;
1566 int if_idx = 0;
1567 int wp_start = cb->args[0];
1568 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001569 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001570 struct wireless_dev *wdev;
1571
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001572 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001573 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1574 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001575 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001576 if (wp_idx < wp_start) {
1577 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001578 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001579 }
Johannes Berg55682962007-09-20 13:09:35 -04001580 if_idx = 0;
1581
Johannes Bergf5ea9122009-08-07 16:17:38 +02001582 mutex_lock(&rdev->devlist_mtx);
1583 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001584 if (if_idx < if_start) {
1585 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001586 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001587 }
Johannes Berg55682962007-09-20 13:09:35 -04001588 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1589 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001590 rdev, wdev->netdev) < 0) {
1591 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001592 goto out;
1593 }
1594 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001595 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001596 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001597
1598 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001599 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001600 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001601 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001602
1603 cb->args[0] = wp_idx;
1604 cb->args[1] = if_idx;
1605
1606 return skb->len;
1607}
1608
1609static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1610{
1611 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001612 struct cfg80211_registered_device *dev = info->user_ptr[0];
1613 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001614
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001615 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001616 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001617 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001618
Johannes Bergd7264052009-04-19 16:23:20 +02001619 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001620 dev, netdev) < 0) {
1621 nlmsg_free(msg);
1622 return -ENOBUFS;
1623 }
Johannes Berg55682962007-09-20 13:09:35 -04001624
Johannes Berg134e6372009-07-10 09:51:34 +00001625 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001626}
1627
Michael Wu66f7ac52008-01-31 19:48:22 +01001628static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1629 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1630 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1631 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1632 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1633 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1634};
1635
1636static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1637{
1638 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1639 int flag;
1640
1641 *mntrflags = 0;
1642
1643 if (!nla)
1644 return -EINVAL;
1645
1646 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1647 nla, mntr_flags_policy))
1648 return -EINVAL;
1649
1650 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1651 if (flags[flag])
1652 *mntrflags |= (1<<flag);
1653
1654 return 0;
1655}
1656
Johannes Berg9bc383d2009-11-19 11:55:19 +01001657static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001658 struct net_device *netdev, u8 use_4addr,
1659 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001660{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001661 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001662 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001663 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001664 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001665 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001666
1667 switch (iftype) {
1668 case NL80211_IFTYPE_AP_VLAN:
1669 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1670 return 0;
1671 break;
1672 case NL80211_IFTYPE_STATION:
1673 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1674 return 0;
1675 break;
1676 default:
1677 break;
1678 }
1679
1680 return -EOPNOTSUPP;
1681}
1682
Johannes Berg55682962007-09-20 13:09:35 -04001683static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1684{
Johannes Berg4c476992010-10-04 21:36:35 +02001685 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001686 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001687 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001688 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001689 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001690 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001691 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001692
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001693 memset(&params, 0, sizeof(params));
1694
Johannes Berg04a773a2009-04-19 21:24:32 +02001695 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001696
Johannes Berg723b0382008-09-16 20:22:09 +02001697 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001698 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001699 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001700 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001701 if (ntype > NL80211_IFTYPE_MAX)
1702 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001703 }
1704
Johannes Berg92ffe052008-09-16 20:39:36 +02001705 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001706 struct wireless_dev *wdev = dev->ieee80211_ptr;
1707
Johannes Berg4c476992010-10-04 21:36:35 +02001708 if (ntype != NL80211_IFTYPE_MESH_POINT)
1709 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001710 if (netif_running(dev))
1711 return -EBUSY;
1712
1713 wdev_lock(wdev);
1714 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1715 IEEE80211_MAX_MESH_ID_LEN);
1716 wdev->mesh_id_up_len =
1717 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1718 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1719 wdev->mesh_id_up_len);
1720 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001721 }
1722
Felix Fietkau8b787642009-11-10 18:53:10 +01001723 if (info->attrs[NL80211_ATTR_4ADDR]) {
1724 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1725 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001726 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001727 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001728 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001729 } else {
1730 params.use_4addr = -1;
1731 }
1732
Johannes Berg92ffe052008-09-16 20:39:36 +02001733 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001734 if (ntype != NL80211_IFTYPE_MONITOR)
1735 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001736 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1737 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001738 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001739 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001740
1741 flags = &_flags;
1742 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001743 }
Johannes Berg3b858752009-03-12 09:55:09 +01001744
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001745 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001746 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001747 else
1748 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001749
Johannes Berg9bc383d2009-11-19 11:55:19 +01001750 if (!err && params.use_4addr != -1)
1751 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1752
Johannes Berg55682962007-09-20 13:09:35 -04001753 return err;
1754}
1755
1756static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1757{
Johannes Berg4c476992010-10-04 21:36:35 +02001758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001759 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001760 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001761 int err;
1762 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001763 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001764
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001765 memset(&params, 0, sizeof(params));
1766
Johannes Berg55682962007-09-20 13:09:35 -04001767 if (!info->attrs[NL80211_ATTR_IFNAME])
1768 return -EINVAL;
1769
1770 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1771 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1772 if (type > NL80211_IFTYPE_MAX)
1773 return -EINVAL;
1774 }
1775
Johannes Berg79c97e92009-07-07 03:56:12 +02001776 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001777 !(rdev->wiphy.interface_modes & (1 << type)))
1778 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001779
Johannes Berg9bc383d2009-11-19 11:55:19 +01001780 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001781 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001782 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001783 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001784 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001785 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001786
Michael Wu66f7ac52008-01-31 19:48:22 +01001787 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1788 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1789 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001790 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001791 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001792 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001793 if (IS_ERR(dev))
1794 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001795
Johannes Berg29cbe682010-12-03 09:20:44 +01001796 if (type == NL80211_IFTYPE_MESH_POINT &&
1797 info->attrs[NL80211_ATTR_MESH_ID]) {
1798 struct wireless_dev *wdev = dev->ieee80211_ptr;
1799
1800 wdev_lock(wdev);
1801 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1802 IEEE80211_MAX_MESH_ID_LEN);
1803 wdev->mesh_id_up_len =
1804 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1805 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1806 wdev->mesh_id_up_len);
1807 wdev_unlock(wdev);
1808 }
1809
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001810 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001811}
1812
1813static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1814{
Johannes Berg4c476992010-10-04 21:36:35 +02001815 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1816 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001817
Johannes Berg4c476992010-10-04 21:36:35 +02001818 if (!rdev->ops->del_virtual_intf)
1819 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001820
Johannes Berg4c476992010-10-04 21:36:35 +02001821 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001822}
1823
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001824static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1825{
1826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1827 struct net_device *dev = info->user_ptr[1];
1828 u16 noack_map;
1829
1830 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1831 return -EINVAL;
1832
1833 if (!rdev->ops->set_noack_map)
1834 return -EOPNOTSUPP;
1835
1836 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1837
1838 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1839}
1840
Johannes Berg41ade002007-12-19 02:03:29 +01001841struct get_key_cookie {
1842 struct sk_buff *msg;
1843 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001844 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001845};
1846
1847static void get_key_callback(void *c, struct key_params *params)
1848{
Johannes Bergb9454e82009-07-08 13:29:08 +02001849 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001850 struct get_key_cookie *cookie = c;
1851
David S. Miller9360ffd2012-03-29 04:41:26 -04001852 if ((params->key &&
1853 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1854 params->key_len, params->key)) ||
1855 (params->seq &&
1856 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1857 params->seq_len, params->seq)) ||
1858 (params->cipher &&
1859 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1860 params->cipher)))
1861 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001862
Johannes Bergb9454e82009-07-08 13:29:08 +02001863 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1864 if (!key)
1865 goto nla_put_failure;
1866
David S. Miller9360ffd2012-03-29 04:41:26 -04001867 if ((params->key &&
1868 nla_put(cookie->msg, NL80211_KEY_DATA,
1869 params->key_len, params->key)) ||
1870 (params->seq &&
1871 nla_put(cookie->msg, NL80211_KEY_SEQ,
1872 params->seq_len, params->seq)) ||
1873 (params->cipher &&
1874 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
1875 params->cipher)))
1876 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001877
David S. Miller9360ffd2012-03-29 04:41:26 -04001878 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
1879 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001880
1881 nla_nest_end(cookie->msg, key);
1882
Johannes Berg41ade002007-12-19 02:03:29 +01001883 return;
1884 nla_put_failure:
1885 cookie->error = 1;
1886}
1887
1888static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1889{
Johannes Berg4c476992010-10-04 21:36:35 +02001890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001891 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001892 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001893 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001894 const u8 *mac_addr = NULL;
1895 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001896 struct get_key_cookie cookie = {
1897 .error = 0,
1898 };
1899 void *hdr;
1900 struct sk_buff *msg;
1901
1902 if (info->attrs[NL80211_ATTR_KEY_IDX])
1903 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1904
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001905 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001906 return -EINVAL;
1907
1908 if (info->attrs[NL80211_ATTR_MAC])
1909 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1910
Johannes Berge31b8212010-10-05 19:39:30 +02001911 pairwise = !!mac_addr;
1912 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1913 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1914 if (kt >= NUM_NL80211_KEYTYPES)
1915 return -EINVAL;
1916 if (kt != NL80211_KEYTYPE_GROUP &&
1917 kt != NL80211_KEYTYPE_PAIRWISE)
1918 return -EINVAL;
1919 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1920 }
1921
Johannes Berg4c476992010-10-04 21:36:35 +02001922 if (!rdev->ops->get_key)
1923 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001924
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001925 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001926 if (!msg)
1927 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001928
1929 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1930 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001931 if (IS_ERR(hdr))
1932 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001933
1934 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001935 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001936
David S. Miller9360ffd2012-03-29 04:41:26 -04001937 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1938 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
1939 goto nla_put_failure;
1940 if (mac_addr &&
1941 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
1942 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001943
Johannes Berge31b8212010-10-05 19:39:30 +02001944 if (pairwise && mac_addr &&
1945 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1946 return -ENOENT;
1947
1948 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1949 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001950
1951 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001952 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001953
1954 if (cookie.error)
1955 goto nla_put_failure;
1956
1957 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001958 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001959
1960 nla_put_failure:
1961 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001962 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001963 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001964 return err;
1965}
1966
1967static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1968{
Johannes Berg4c476992010-10-04 21:36:35 +02001969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001970 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001971 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001972 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001973
Johannes Bergb9454e82009-07-08 13:29:08 +02001974 err = nl80211_parse_key(info, &key);
1975 if (err)
1976 return err;
1977
1978 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001979 return -EINVAL;
1980
Johannes Bergb9454e82009-07-08 13:29:08 +02001981 /* only support setting default key */
1982 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001983 return -EINVAL;
1984
Johannes Bergfffd0932009-07-08 14:22:54 +02001985 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001986
1987 if (key.def) {
1988 if (!rdev->ops->set_default_key) {
1989 err = -EOPNOTSUPP;
1990 goto out;
1991 }
1992
1993 err = nl80211_key_allowed(dev->ieee80211_ptr);
1994 if (err)
1995 goto out;
1996
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001997 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1998 key.def_uni, key.def_multi);
1999
2000 if (err)
2001 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002002
Johannes Berg3d23e342009-09-29 23:27:28 +02002003#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002004 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002005#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002006 } else {
2007 if (key.def_uni || !key.def_multi) {
2008 err = -EINVAL;
2009 goto out;
2010 }
2011
2012 if (!rdev->ops->set_default_mgmt_key) {
2013 err = -EOPNOTSUPP;
2014 goto out;
2015 }
2016
2017 err = nl80211_key_allowed(dev->ieee80211_ptr);
2018 if (err)
2019 goto out;
2020
2021 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2022 dev, key.idx);
2023 if (err)
2024 goto out;
2025
2026#ifdef CONFIG_CFG80211_WEXT
2027 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2028#endif
2029 }
2030
2031 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002032 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002033
Johannes Berg41ade002007-12-19 02:03:29 +01002034 return err;
2035}
2036
2037static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2038{
Johannes Berg4c476992010-10-04 21:36:35 +02002039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002040 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002041 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002042 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002043 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002044
Johannes Bergb9454e82009-07-08 13:29:08 +02002045 err = nl80211_parse_key(info, &key);
2046 if (err)
2047 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002048
Johannes Bergb9454e82009-07-08 13:29:08 +02002049 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002050 return -EINVAL;
2051
Johannes Berg41ade002007-12-19 02:03:29 +01002052 if (info->attrs[NL80211_ATTR_MAC])
2053 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2054
Johannes Berge31b8212010-10-05 19:39:30 +02002055 if (key.type == -1) {
2056 if (mac_addr)
2057 key.type = NL80211_KEYTYPE_PAIRWISE;
2058 else
2059 key.type = NL80211_KEYTYPE_GROUP;
2060 }
2061
2062 /* for now */
2063 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2064 key.type != NL80211_KEYTYPE_GROUP)
2065 return -EINVAL;
2066
Johannes Berg4c476992010-10-04 21:36:35 +02002067 if (!rdev->ops->add_key)
2068 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002069
Johannes Berge31b8212010-10-05 19:39:30 +02002070 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2071 key.type == NL80211_KEYTYPE_PAIRWISE,
2072 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002073 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002074
2075 wdev_lock(dev->ieee80211_ptr);
2076 err = nl80211_key_allowed(dev->ieee80211_ptr);
2077 if (!err)
2078 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002079 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002080 mac_addr, &key.p);
2081 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002082
Johannes Berg41ade002007-12-19 02:03:29 +01002083 return err;
2084}
2085
2086static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2087{
Johannes Berg4c476992010-10-04 21:36:35 +02002088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002089 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002090 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002091 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002092 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002093
Johannes Bergb9454e82009-07-08 13:29:08 +02002094 err = nl80211_parse_key(info, &key);
2095 if (err)
2096 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002097
2098 if (info->attrs[NL80211_ATTR_MAC])
2099 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2100
Johannes Berge31b8212010-10-05 19:39:30 +02002101 if (key.type == -1) {
2102 if (mac_addr)
2103 key.type = NL80211_KEYTYPE_PAIRWISE;
2104 else
2105 key.type = NL80211_KEYTYPE_GROUP;
2106 }
2107
2108 /* for now */
2109 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2110 key.type != NL80211_KEYTYPE_GROUP)
2111 return -EINVAL;
2112
Johannes Berg4c476992010-10-04 21:36:35 +02002113 if (!rdev->ops->del_key)
2114 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002115
Johannes Bergfffd0932009-07-08 14:22:54 +02002116 wdev_lock(dev->ieee80211_ptr);
2117 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002118
2119 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2120 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2121 err = -ENOENT;
2122
Johannes Bergfffd0932009-07-08 14:22:54 +02002123 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002124 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2125 key.type == NL80211_KEYTYPE_PAIRWISE,
2126 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002127
Johannes Berg3d23e342009-09-29 23:27:28 +02002128#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002129 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002130 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002131 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002132 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002133 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2134 }
2135#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002136 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002137
Johannes Berg41ade002007-12-19 02:03:29 +01002138 return err;
2139}
2140
Johannes Berg88600202012-02-13 15:17:18 +01002141static int nl80211_parse_beacon(struct genl_info *info,
2142 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002143{
Johannes Berg88600202012-02-13 15:17:18 +01002144 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002145
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002146 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2147 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2148 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2149 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002150 return -EINVAL;
2151
Johannes Berg88600202012-02-13 15:17:18 +01002152 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002153
Johannes Berged1b6cc2007-12-19 02:03:32 +01002154 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002155 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2156 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2157 if (!bcn->head_len)
2158 return -EINVAL;
2159 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002160 }
2161
2162 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002163 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2164 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002165 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002166 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002167 }
2168
Johannes Berg4c476992010-10-04 21:36:35 +02002169 if (!haveinfo)
2170 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002171
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002172 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002173 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2174 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002175 }
2176
2177 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002178 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002179 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002180 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002181 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2182 }
2183
2184 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002185 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002186 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002187 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002188 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2189 }
2190
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002191 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002192 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002193 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002194 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002195 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2196 }
2197
Johannes Berg88600202012-02-13 15:17:18 +01002198 return 0;
2199}
2200
2201static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2202{
2203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2204 struct net_device *dev = info->user_ptr[1];
2205 struct wireless_dev *wdev = dev->ieee80211_ptr;
2206 struct cfg80211_ap_settings params;
2207 int err;
2208
2209 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2210 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2211 return -EOPNOTSUPP;
2212
2213 if (!rdev->ops->start_ap)
2214 return -EOPNOTSUPP;
2215
2216 if (wdev->beacon_interval)
2217 return -EALREADY;
2218
2219 memset(&params, 0, sizeof(params));
2220
2221 /* these are required for START_AP */
2222 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2223 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2224 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2225 return -EINVAL;
2226
2227 err = nl80211_parse_beacon(info, &params.beacon);
2228 if (err)
2229 return err;
2230
2231 params.beacon_interval =
2232 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2233 params.dtim_period =
2234 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2235
2236 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2237 if (err)
2238 return err;
2239
2240 /*
2241 * In theory, some of these attributes should be required here
2242 * but since they were not used when the command was originally
2243 * added, keep them optional for old user space programs to let
2244 * them continue to work with drivers that do not need the
2245 * additional information -- drivers must check!
2246 */
2247 if (info->attrs[NL80211_ATTR_SSID]) {
2248 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2249 params.ssid_len =
2250 nla_len(info->attrs[NL80211_ATTR_SSID]);
2251 if (params.ssid_len == 0 ||
2252 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2253 return -EINVAL;
2254 }
2255
2256 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2257 params.hidden_ssid = nla_get_u32(
2258 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2259 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2260 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2261 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2262 return -EINVAL;
2263 }
2264
2265 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2266
2267 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2268 params.auth_type = nla_get_u32(
2269 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2270 if (!nl80211_valid_auth_type(params.auth_type))
2271 return -EINVAL;
2272 } else
2273 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2274
2275 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2276 NL80211_MAX_NR_CIPHER_SUITES);
2277 if (err)
2278 return err;
2279
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302280 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2281 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2282 return -EOPNOTSUPP;
2283 params.inactivity_timeout = nla_get_u16(
2284 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2285 }
2286
Johannes Berg88600202012-02-13 15:17:18 +01002287 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2288 if (!err)
2289 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002290 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002291}
2292
Johannes Berg88600202012-02-13 15:17:18 +01002293static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2294{
2295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2296 struct net_device *dev = info->user_ptr[1];
2297 struct wireless_dev *wdev = dev->ieee80211_ptr;
2298 struct cfg80211_beacon_data params;
2299 int err;
2300
2301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2303 return -EOPNOTSUPP;
2304
2305 if (!rdev->ops->change_beacon)
2306 return -EOPNOTSUPP;
2307
2308 if (!wdev->beacon_interval)
2309 return -EINVAL;
2310
2311 err = nl80211_parse_beacon(info, &params);
2312 if (err)
2313 return err;
2314
2315 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2316}
2317
2318static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002319{
Johannes Berg4c476992010-10-04 21:36:35 +02002320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2321 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002322 struct wireless_dev *wdev = dev->ieee80211_ptr;
2323 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002324
Johannes Berg88600202012-02-13 15:17:18 +01002325 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002326 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002327
Johannes Berg074ac8d2010-09-16 14:58:22 +02002328 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002329 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2330 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002331
Johannes Berg88600202012-02-13 15:17:18 +01002332 if (!wdev->beacon_interval)
2333 return -ENOENT;
2334
2335 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002336 if (!err)
2337 wdev->beacon_interval = 0;
2338 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002339}
2340
Johannes Berg5727ef12007-12-19 02:03:34 +01002341static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2342 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2343 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2344 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002345 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002346 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002347 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002348};
2349
Johannes Bergeccb8e82009-05-11 21:57:56 +03002350static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002351 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002352 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002353{
2354 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002355 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002356 int flag;
2357
Johannes Bergeccb8e82009-05-11 21:57:56 +03002358 /*
2359 * Try parsing the new attribute first so userspace
2360 * can specify both for older kernels.
2361 */
2362 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2363 if (nla) {
2364 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002365
Johannes Bergeccb8e82009-05-11 21:57:56 +03002366 sta_flags = nla_data(nla);
2367 params->sta_flags_mask = sta_flags->mask;
2368 params->sta_flags_set = sta_flags->set;
2369 if ((params->sta_flags_mask |
2370 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2371 return -EINVAL;
2372 return 0;
2373 }
2374
2375 /* if present, parse the old attribute */
2376
2377 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002378 if (!nla)
2379 return 0;
2380
2381 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2382 nla, sta_flags_policy))
2383 return -EINVAL;
2384
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002385 /*
2386 * Only allow certain flags for interface types so that
2387 * other attributes are silently ignored. Remember that
2388 * this is backward compatibility code with old userspace
2389 * and shouldn't be hit in other cases anyway.
2390 */
2391 switch (iftype) {
2392 case NL80211_IFTYPE_AP:
2393 case NL80211_IFTYPE_AP_VLAN:
2394 case NL80211_IFTYPE_P2P_GO:
2395 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2396 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2397 BIT(NL80211_STA_FLAG_WME) |
2398 BIT(NL80211_STA_FLAG_MFP);
2399 break;
2400 case NL80211_IFTYPE_P2P_CLIENT:
2401 case NL80211_IFTYPE_STATION:
2402 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2403 BIT(NL80211_STA_FLAG_TDLS_PEER);
2404 break;
2405 case NL80211_IFTYPE_MESH_POINT:
2406 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2407 BIT(NL80211_STA_FLAG_MFP) |
2408 BIT(NL80211_STA_FLAG_AUTHORIZED);
2409 default:
2410 return -EINVAL;
2411 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002412
2413 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2414 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002415 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002416
2417 return 0;
2418}
2419
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002420static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2421 int attr)
2422{
2423 struct nlattr *rate;
2424 u16 bitrate;
2425
2426 rate = nla_nest_start(msg, attr);
2427 if (!rate)
2428 goto nla_put_failure;
2429
2430 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2431 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002432 if ((bitrate > 0 &&
2433 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2434 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2435 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2436 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2437 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2438 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2439 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2440 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002441
2442 nla_nest_end(msg, rate);
2443 return true;
2444
2445nla_put_failure:
2446 return false;
2447}
2448
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002449static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002450 int flags,
2451 struct cfg80211_registered_device *rdev,
2452 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002453 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002454{
2455 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002456 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002457
2458 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2459 if (!hdr)
2460 return -1;
2461
David S. Miller9360ffd2012-03-29 04:41:26 -04002462 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2463 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2464 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2465 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002466
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002467 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2468 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002469 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002470 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2471 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2472 sinfo->connected_time))
2473 goto nla_put_failure;
2474 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2475 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2476 sinfo->inactive_time))
2477 goto nla_put_failure;
2478 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2479 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2480 sinfo->rx_bytes))
2481 goto nla_put_failure;
2482 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2483 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2484 sinfo->tx_bytes))
2485 goto nla_put_failure;
2486 if ((sinfo->filled & STATION_INFO_LLID) &&
2487 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2488 goto nla_put_failure;
2489 if ((sinfo->filled & STATION_INFO_PLID) &&
2490 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2491 goto nla_put_failure;
2492 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2493 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2494 sinfo->plink_state))
2495 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002496 switch (rdev->wiphy.signal_type) {
2497 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002498 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2499 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2500 sinfo->signal))
2501 goto nla_put_failure;
2502 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2503 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2504 sinfo->signal_avg))
2505 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002506 break;
2507 default:
2508 break;
2509 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002510 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002511 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2512 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002513 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002514 }
2515 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2516 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2517 NL80211_STA_INFO_RX_BITRATE))
2518 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002519 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002520 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2521 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2522 sinfo->rx_packets))
2523 goto nla_put_failure;
2524 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2525 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2526 sinfo->tx_packets))
2527 goto nla_put_failure;
2528 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2529 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2530 sinfo->tx_retries))
2531 goto nla_put_failure;
2532 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2533 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2534 sinfo->tx_failed))
2535 goto nla_put_failure;
2536 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2537 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2538 sinfo->beacon_loss_count))
2539 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002540 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2541 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2542 if (!bss_param)
2543 goto nla_put_failure;
2544
David S. Miller9360ffd2012-03-29 04:41:26 -04002545 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2546 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2547 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2548 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2549 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2550 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2551 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2552 sinfo->bss_param.dtim_period) ||
2553 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2554 sinfo->bss_param.beacon_interval))
2555 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002556
2557 nla_nest_end(msg, bss_param);
2558 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002559 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2560 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2561 sizeof(struct nl80211_sta_flag_update),
2562 &sinfo->sta_flags))
2563 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002564 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2565 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2566 sinfo->t_offset))
2567 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002568 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002569
David S. Miller9360ffd2012-03-29 04:41:26 -04002570 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2571 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2572 sinfo->assoc_req_ies))
2573 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002574
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002575 return genlmsg_end(msg, hdr);
2576
2577 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002578 genlmsg_cancel(msg, hdr);
2579 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002580}
2581
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002582static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002583 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002584{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002585 struct station_info sinfo;
2586 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002587 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002588 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002589 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002590 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002591
Johannes Berg67748892010-10-04 21:14:06 +02002592 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2593 if (err)
2594 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002595
2596 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002597 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002598 goto out_err;
2599 }
2600
Johannes Bergbba95fe2008-07-29 13:22:51 +02002601 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002602 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002603 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2604 mac_addr, &sinfo);
2605 if (err == -ENOENT)
2606 break;
2607 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002608 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002609
2610 if (nl80211_send_station(skb,
2611 NETLINK_CB(cb->skb).pid,
2612 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002613 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002614 &sinfo) < 0)
2615 goto out;
2616
2617 sta_idx++;
2618 }
2619
2620
2621 out:
2622 cb->args[1] = sta_idx;
2623 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002624 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002625 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002626
2627 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002628}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002629
Johannes Berg5727ef12007-12-19 02:03:34 +01002630static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2631{
Johannes Berg4c476992010-10-04 21:36:35 +02002632 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2633 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002634 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002635 struct sk_buff *msg;
2636 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002637 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002638
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002639 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002640
2641 if (!info->attrs[NL80211_ATTR_MAC])
2642 return -EINVAL;
2643
2644 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2645
Johannes Berg4c476992010-10-04 21:36:35 +02002646 if (!rdev->ops->get_station)
2647 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002648
Johannes Berg79c97e92009-07-07 03:56:12 +02002649 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002650 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002651 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002652
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002654 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002655 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002656
2657 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002658 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002659 nlmsg_free(msg);
2660 return -ENOBUFS;
2661 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002662
Johannes Berg4c476992010-10-04 21:36:35 +02002663 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002664}
2665
2666/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002667 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002668 */
Johannes Berg80b99892011-11-18 16:23:01 +01002669static struct net_device *get_vlan(struct genl_info *info,
2670 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002671{
Johannes Berg463d0182009-07-14 00:33:35 +02002672 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002673 struct net_device *v;
2674 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002675
Johannes Berg80b99892011-11-18 16:23:01 +01002676 if (!vlanattr)
2677 return NULL;
2678
2679 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2680 if (!v)
2681 return ERR_PTR(-ENODEV);
2682
2683 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2684 ret = -EINVAL;
2685 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002686 }
Johannes Berg80b99892011-11-18 16:23:01 +01002687
2688 if (!netif_running(v)) {
2689 ret = -ENETDOWN;
2690 goto error;
2691 }
2692
2693 return v;
2694 error:
2695 dev_put(v);
2696 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002697}
2698
2699static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2700{
Johannes Berg4c476992010-10-04 21:36:35 +02002701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002702 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002703 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002704 struct station_parameters params;
2705 u8 *mac_addr = NULL;
2706
2707 memset(&params, 0, sizeof(params));
2708
2709 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002710 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002711
2712 if (info->attrs[NL80211_ATTR_STA_AID])
2713 return -EINVAL;
2714
2715 if (!info->attrs[NL80211_ATTR_MAC])
2716 return -EINVAL;
2717
2718 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2719
2720 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2721 params.supported_rates =
2722 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2723 params.supported_rates_len =
2724 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2725 }
2726
2727 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2728 params.listen_interval =
2729 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2730
Jouni Malinen36aedc92008-08-25 11:58:58 +03002731 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2732 params.ht_capa =
2733 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2734
Johannes Bergbdd90d52011-12-14 12:20:27 +01002735 if (!rdev->ops->change_station)
2736 return -EOPNOTSUPP;
2737
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002738 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002739 return -EINVAL;
2740
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002741 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2742 params.plink_action =
2743 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2744
Javier Cardona9c3990a2011-05-03 16:57:11 -07002745 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2746 params.plink_state =
2747 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2748
Johannes Berga97f4422009-06-18 17:23:43 +02002749 switch (dev->ieee80211_ptr->iftype) {
2750 case NL80211_IFTYPE_AP:
2751 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002752 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002753 /* disallow mesh-specific things */
2754 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002755 return -EINVAL;
2756
2757 /* TDLS can't be set, ... */
2758 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2759 return -EINVAL;
2760 /*
2761 * ... but don't bother the driver with it. This works around
2762 * a hostapd/wpa_supplicant issue -- it always includes the
2763 * TLDS_PEER flag in the mask even for AP mode.
2764 */
2765 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2766
2767 /* accept only the listed bits */
2768 if (params.sta_flags_mask &
2769 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2770 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2771 BIT(NL80211_STA_FLAG_WME) |
2772 BIT(NL80211_STA_FLAG_MFP)))
2773 return -EINVAL;
2774
2775 /* must be last in here for error handling */
2776 params.vlan = get_vlan(info, rdev);
2777 if (IS_ERR(params.vlan))
2778 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002779 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002780 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002781 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002782 /*
2783 * Don't allow userspace to change the TDLS_PEER flag,
2784 * but silently ignore attempts to change it since we
2785 * don't have state here to verify that it doesn't try
2786 * to change the flag.
2787 */
2788 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002789 /* fall through */
2790 case NL80211_IFTYPE_ADHOC:
2791 /* disallow things sta doesn't support */
2792 if (params.plink_action)
2793 return -EINVAL;
2794 if (params.ht_capa)
2795 return -EINVAL;
2796 if (params.listen_interval >= 0)
2797 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002798 /* reject any changes other than AUTHORIZED */
2799 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2800 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002801 break;
2802 case NL80211_IFTYPE_MESH_POINT:
2803 /* disallow things mesh doesn't support */
2804 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002805 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002806 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002807 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002808 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002809 return -EINVAL;
2810 /*
2811 * No special handling for TDLS here -- the userspace
2812 * mesh code doesn't have this bug.
2813 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002814 if (params.sta_flags_mask &
2815 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002816 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002817 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002818 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002819 break;
2820 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002821 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002822 }
2823
Johannes Bergbdd90d52011-12-14 12:20:27 +01002824 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002825
Johannes Berg79c97e92009-07-07 03:56:12 +02002826 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002827
Johannes Berg5727ef12007-12-19 02:03:34 +01002828 if (params.vlan)
2829 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002830
Johannes Berg5727ef12007-12-19 02:03:34 +01002831 return err;
2832}
2833
Eliad Pellerc75786c2011-08-23 14:37:46 +03002834static struct nla_policy
2835nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2836 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2837 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2838};
2839
Johannes Berg5727ef12007-12-19 02:03:34 +01002840static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2841{
Johannes Berg4c476992010-10-04 21:36:35 +02002842 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002843 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002844 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002845 struct station_parameters params;
2846 u8 *mac_addr = NULL;
2847
2848 memset(&params, 0, sizeof(params));
2849
2850 if (!info->attrs[NL80211_ATTR_MAC])
2851 return -EINVAL;
2852
Johannes Berg5727ef12007-12-19 02:03:34 +01002853 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2854 return -EINVAL;
2855
2856 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2857 return -EINVAL;
2858
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002859 if (!info->attrs[NL80211_ATTR_STA_AID])
2860 return -EINVAL;
2861
Johannes Berg5727ef12007-12-19 02:03:34 +01002862 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2863 params.supported_rates =
2864 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2865 params.supported_rates_len =
2866 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2867 params.listen_interval =
2868 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002869
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002870 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2871 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2872 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002873
Jouni Malinen36aedc92008-08-25 11:58:58 +03002874 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2875 params.ht_capa =
2876 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002877
Javier Cardona96b78df2011-04-07 15:08:33 -07002878 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2879 params.plink_action =
2880 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2881
Johannes Bergbdd90d52011-12-14 12:20:27 +01002882 if (!rdev->ops->add_station)
2883 return -EOPNOTSUPP;
2884
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002885 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002886 return -EINVAL;
2887
Johannes Bergbdd90d52011-12-14 12:20:27 +01002888 switch (dev->ieee80211_ptr->iftype) {
2889 case NL80211_IFTYPE_AP:
2890 case NL80211_IFTYPE_AP_VLAN:
2891 case NL80211_IFTYPE_P2P_GO:
2892 /* parse WME attributes if sta is WME capable */
2893 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2894 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2895 info->attrs[NL80211_ATTR_STA_WME]) {
2896 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2897 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002898
Johannes Bergbdd90d52011-12-14 12:20:27 +01002899 nla = info->attrs[NL80211_ATTR_STA_WME];
2900 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2901 nl80211_sta_wme_policy);
2902 if (err)
2903 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002904
Johannes Bergbdd90d52011-12-14 12:20:27 +01002905 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2906 params.uapsd_queues =
2907 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2908 if (params.uapsd_queues &
2909 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2910 return -EINVAL;
2911
2912 if (tb[NL80211_STA_WME_MAX_SP])
2913 params.max_sp =
2914 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2915
2916 if (params.max_sp &
2917 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2918 return -EINVAL;
2919
2920 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2921 }
2922 /* TDLS peers cannot be added */
2923 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002924 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002925 /* but don't bother the driver with it */
2926 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002927
Johannes Bergbdd90d52011-12-14 12:20:27 +01002928 /* must be last in here for error handling */
2929 params.vlan = get_vlan(info, rdev);
2930 if (IS_ERR(params.vlan))
2931 return PTR_ERR(params.vlan);
2932 break;
2933 case NL80211_IFTYPE_MESH_POINT:
2934 /* TDLS peers cannot be added */
2935 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002936 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002937 break;
2938 case NL80211_IFTYPE_STATION:
2939 /* Only TDLS peers can be added */
2940 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2941 return -EINVAL;
2942 /* Can only add if TDLS ... */
2943 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2944 return -EOPNOTSUPP;
2945 /* ... with external setup is supported */
2946 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2947 return -EOPNOTSUPP;
2948 break;
2949 default:
2950 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002951 }
2952
Johannes Bergbdd90d52011-12-14 12:20:27 +01002953 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002954
Johannes Berg79c97e92009-07-07 03:56:12 +02002955 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002956
Johannes Berg5727ef12007-12-19 02:03:34 +01002957 if (params.vlan)
2958 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002959 return err;
2960}
2961
2962static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2963{
Johannes Berg4c476992010-10-04 21:36:35 +02002964 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2965 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002966 u8 *mac_addr = NULL;
2967
2968 if (info->attrs[NL80211_ATTR_MAC])
2969 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2970
Johannes Berge80cf852009-05-11 14:43:13 +02002971 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002973 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2975 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002976
Johannes Berg4c476992010-10-04 21:36:35 +02002977 if (!rdev->ops->del_station)
2978 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002979
Johannes Berg4c476992010-10-04 21:36:35 +02002980 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002981}
2982
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002983static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2984 int flags, struct net_device *dev,
2985 u8 *dst, u8 *next_hop,
2986 struct mpath_info *pinfo)
2987{
2988 void *hdr;
2989 struct nlattr *pinfoattr;
2990
2991 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2992 if (!hdr)
2993 return -1;
2994
David S. Miller9360ffd2012-03-29 04:41:26 -04002995 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2996 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
2997 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
2998 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
2999 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003000
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003001 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3002 if (!pinfoattr)
3003 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003004 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3005 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3006 pinfo->frame_qlen))
3007 goto nla_put_failure;
3008 if (((pinfo->filled & MPATH_INFO_SN) &&
3009 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3010 ((pinfo->filled & MPATH_INFO_METRIC) &&
3011 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3012 pinfo->metric)) ||
3013 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3014 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3015 pinfo->exptime)) ||
3016 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3017 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3018 pinfo->flags)) ||
3019 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3020 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3021 pinfo->discovery_timeout)) ||
3022 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3023 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3024 pinfo->discovery_retries)))
3025 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003026
3027 nla_nest_end(msg, pinfoattr);
3028
3029 return genlmsg_end(msg, hdr);
3030
3031 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003032 genlmsg_cancel(msg, hdr);
3033 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003034}
3035
3036static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003037 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003038{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003039 struct mpath_info pinfo;
3040 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003041 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003042 u8 dst[ETH_ALEN];
3043 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003044 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003045 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003046
Johannes Berg67748892010-10-04 21:14:06 +02003047 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3048 if (err)
3049 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003050
3051 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003052 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003053 goto out_err;
3054 }
3055
Jouni Malineneec60b02009-03-20 21:21:19 +02003056 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3057 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003058 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003059 }
3060
Johannes Bergbba95fe2008-07-29 13:22:51 +02003061 while (1) {
3062 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3063 dst, next_hop, &pinfo);
3064 if (err == -ENOENT)
3065 break;
3066 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003067 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003068
3069 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3070 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3071 netdev, dst, next_hop,
3072 &pinfo) < 0)
3073 goto out;
3074
3075 path_idx++;
3076 }
3077
3078
3079 out:
3080 cb->args[1] = path_idx;
3081 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003082 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003083 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003084 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003085}
3086
3087static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3088{
Johannes Berg4c476992010-10-04 21:36:35 +02003089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003090 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003091 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003092 struct mpath_info pinfo;
3093 struct sk_buff *msg;
3094 u8 *dst = NULL;
3095 u8 next_hop[ETH_ALEN];
3096
3097 memset(&pinfo, 0, sizeof(pinfo));
3098
3099 if (!info->attrs[NL80211_ATTR_MAC])
3100 return -EINVAL;
3101
3102 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3103
Johannes Berg4c476992010-10-04 21:36:35 +02003104 if (!rdev->ops->get_mpath)
3105 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003106
Johannes Berg4c476992010-10-04 21:36:35 +02003107 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3108 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003109
Johannes Berg79c97e92009-07-07 03:56:12 +02003110 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003111 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003112 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003113
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003115 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003116 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003117
3118 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003119 dev, dst, next_hop, &pinfo) < 0) {
3120 nlmsg_free(msg);
3121 return -ENOBUFS;
3122 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003123
Johannes Berg4c476992010-10-04 21:36:35 +02003124 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003125}
3126
3127static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3128{
Johannes Berg4c476992010-10-04 21:36:35 +02003129 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3130 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003131 u8 *dst = NULL;
3132 u8 *next_hop = NULL;
3133
3134 if (!info->attrs[NL80211_ATTR_MAC])
3135 return -EINVAL;
3136
3137 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3138 return -EINVAL;
3139
3140 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3141 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3142
Johannes Berg4c476992010-10-04 21:36:35 +02003143 if (!rdev->ops->change_mpath)
3144 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003145
Johannes Berg4c476992010-10-04 21:36:35 +02003146 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3147 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003148
Johannes Berg4c476992010-10-04 21:36:35 +02003149 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003150}
Johannes Berg4c476992010-10-04 21:36:35 +02003151
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003152static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3153{
Johannes Berg4c476992010-10-04 21:36:35 +02003154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3155 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003156 u8 *dst = NULL;
3157 u8 *next_hop = NULL;
3158
3159 if (!info->attrs[NL80211_ATTR_MAC])
3160 return -EINVAL;
3161
3162 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3163 return -EINVAL;
3164
3165 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3166 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3167
Johannes Berg4c476992010-10-04 21:36:35 +02003168 if (!rdev->ops->add_mpath)
3169 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003170
Johannes Berg4c476992010-10-04 21:36:35 +02003171 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3172 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003173
Johannes Berg4c476992010-10-04 21:36:35 +02003174 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003175}
3176
3177static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3178{
Johannes Berg4c476992010-10-04 21:36:35 +02003179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3180 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003181 u8 *dst = NULL;
3182
3183 if (info->attrs[NL80211_ATTR_MAC])
3184 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3185
Johannes Berg4c476992010-10-04 21:36:35 +02003186 if (!rdev->ops->del_mpath)
3187 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003188
Johannes Berg4c476992010-10-04 21:36:35 +02003189 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003190}
3191
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003192static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3193{
Johannes Berg4c476992010-10-04 21:36:35 +02003194 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3195 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003196 struct bss_parameters params;
3197
3198 memset(&params, 0, sizeof(params));
3199 /* default to not changing parameters */
3200 params.use_cts_prot = -1;
3201 params.use_short_preamble = -1;
3202 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003203 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003204 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003205
3206 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3207 params.use_cts_prot =
3208 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3209 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3210 params.use_short_preamble =
3211 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3212 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3213 params.use_short_slot_time =
3214 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003215 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3216 params.basic_rates =
3217 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3218 params.basic_rates_len =
3219 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3220 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003221 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3222 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003223 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3224 params.ht_opmode =
3225 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003226
Johannes Berg4c476992010-10-04 21:36:35 +02003227 if (!rdev->ops->change_bss)
3228 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003229
Johannes Berg074ac8d2010-09-16 14:58:22 +02003230 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003231 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3232 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003233
Johannes Berg4c476992010-10-04 21:36:35 +02003234 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003235}
3236
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003237static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003238 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3239 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3240 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3241 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3242 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3243 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3244};
3245
3246static int parse_reg_rule(struct nlattr *tb[],
3247 struct ieee80211_reg_rule *reg_rule)
3248{
3249 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3250 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3251
3252 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3253 return -EINVAL;
3254 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3255 return -EINVAL;
3256 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3257 return -EINVAL;
3258 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3259 return -EINVAL;
3260 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3261 return -EINVAL;
3262
3263 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3264
3265 freq_range->start_freq_khz =
3266 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3267 freq_range->end_freq_khz =
3268 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3269 freq_range->max_bandwidth_khz =
3270 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3271
3272 power_rule->max_eirp =
3273 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3274
3275 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3276 power_rule->max_antenna_gain =
3277 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3278
3279 return 0;
3280}
3281
3282static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3283{
3284 int r;
3285 char *data = NULL;
3286
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003287 /*
3288 * You should only get this when cfg80211 hasn't yet initialized
3289 * completely when built-in to the kernel right between the time
3290 * window between nl80211_init() and regulatory_init(), if that is
3291 * even possible.
3292 */
3293 mutex_lock(&cfg80211_mutex);
3294 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003295 mutex_unlock(&cfg80211_mutex);
3296 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003297 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003298 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003299
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003300 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3301 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003302
3303 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3304
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003305 r = regulatory_hint_user(data);
3306
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003307 return r;
3308}
3309
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003310static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003311 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003312{
Johannes Berg4c476992010-10-04 21:36:35 +02003313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003314 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003315 struct wireless_dev *wdev = dev->ieee80211_ptr;
3316 struct mesh_config cur_params;
3317 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003318 void *hdr;
3319 struct nlattr *pinfoattr;
3320 struct sk_buff *msg;
3321
Johannes Berg29cbe682010-12-03 09:20:44 +01003322 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3323 return -EOPNOTSUPP;
3324
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003325 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003326 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003327
Johannes Berg29cbe682010-12-03 09:20:44 +01003328 wdev_lock(wdev);
3329 /* If not connected, get default parameters */
3330 if (!wdev->mesh_id_len)
3331 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3332 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003333 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003334 &cur_params);
3335 wdev_unlock(wdev);
3336
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003337 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003338 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003339
3340 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003341 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003342 if (!msg)
3343 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003344 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003345 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003346 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003347 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003348 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003349 if (!pinfoattr)
3350 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003351 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3352 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3353 cur_params.dot11MeshRetryTimeout) ||
3354 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3355 cur_params.dot11MeshConfirmTimeout) ||
3356 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3357 cur_params.dot11MeshHoldingTimeout) ||
3358 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3359 cur_params.dot11MeshMaxPeerLinks) ||
3360 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3361 cur_params.dot11MeshMaxRetries) ||
3362 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3363 cur_params.dot11MeshTTL) ||
3364 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3365 cur_params.element_ttl) ||
3366 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3367 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003368 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3369 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003370 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3371 cur_params.dot11MeshHWMPmaxPREQretries) ||
3372 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3373 cur_params.path_refresh_time) ||
3374 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3375 cur_params.min_discovery_timeout) ||
3376 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3377 cur_params.dot11MeshHWMPactivePathTimeout) ||
3378 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3379 cur_params.dot11MeshHWMPpreqMinInterval) ||
3380 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3381 cur_params.dot11MeshHWMPperrMinInterval) ||
3382 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3383 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3384 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3385 cur_params.dot11MeshHWMPRootMode) ||
3386 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3387 cur_params.dot11MeshHWMPRannInterval) ||
3388 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3389 cur_params.dot11MeshGateAnnouncementProtocol) ||
3390 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3391 cur_params.dot11MeshForwarding) ||
3392 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3393 cur_params.rssi_threshold))
3394 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003395 nla_nest_end(msg, pinfoattr);
3396 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003397 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003398
Johannes Berg3b858752009-03-12 09:55:09 +01003399 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003400 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003401 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003402 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003403 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003404}
3405
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003406static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003407 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3408 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3409 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3410 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3411 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3412 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003413 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003414 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003415 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003416
3417 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3418 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3419 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3420 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3421 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003422 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003423 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003424 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003425 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003426 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003427 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003428 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003429};
3430
Javier Cardonac80d5452010-12-16 17:37:49 -08003431static const struct nla_policy
3432 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003433 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003434 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3435 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003436 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003437 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003438 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003439 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003440};
3441
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003442static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003443 struct mesh_config *cfg,
3444 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003445{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003446 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003447 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003448
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003449#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3450do {\
3451 if (table[attr_num]) {\
3452 cfg->param = nla_fn(table[attr_num]); \
3453 mask |= (1 << (attr_num - 1)); \
3454 } \
3455} while (0);\
3456
3457
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003458 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003459 return -EINVAL;
3460 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003461 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003462 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003463 return -EINVAL;
3464
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003465 /* This makes sure that there aren't more than 32 mesh config
3466 * parameters (otherwise our bitfield scheme would not work.) */
3467 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3468
3469 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003470 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3471 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3472 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3473 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3474 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3475 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3476 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3477 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3478 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3479 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3480 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3481 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003482 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3483 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003484 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3485 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
Javier Cardonad299a1f2012-03-31 11:31:33 -07003486 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
3487 mask, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3488 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003489 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3490 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3491 nla_get_u8);
3492 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3493 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3494 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3495 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3496 nla_get_u16);
3497 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3498 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3499 nla_get_u32);
3500 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3501 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3502 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003503 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3504 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3505 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003506 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3507 dot11MeshHWMPnetDiameterTraversalTime,
3508 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3509 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003510 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3511 dot11MeshHWMPRootMode, mask,
3512 NL80211_MESHCONF_HWMP_ROOTMODE,
3513 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003514 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3515 dot11MeshHWMPRannInterval, mask,
3516 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3517 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003518 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3519 dot11MeshGateAnnouncementProtocol, mask,
3520 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3521 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003522 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3523 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003524 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3525 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003526 if (mask_out)
3527 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003528
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003529 return 0;
3530
3531#undef FILL_IN_MESH_PARAM_IF_SET
3532}
3533
Javier Cardonac80d5452010-12-16 17:37:49 -08003534static int nl80211_parse_mesh_setup(struct genl_info *info,
3535 struct mesh_setup *setup)
3536{
3537 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3538
3539 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3540 return -EINVAL;
3541 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3542 info->attrs[NL80211_ATTR_MESH_SETUP],
3543 nl80211_mesh_setup_params_policy))
3544 return -EINVAL;
3545
Javier Cardonad299a1f2012-03-31 11:31:33 -07003546 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3547 setup->sync_method =
3548 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3549 IEEE80211_SYNC_METHOD_VENDOR :
3550 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3551
Javier Cardonac80d5452010-12-16 17:37:49 -08003552 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3553 setup->path_sel_proto =
3554 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3555 IEEE80211_PATH_PROTOCOL_VENDOR :
3556 IEEE80211_PATH_PROTOCOL_HWMP;
3557
3558 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3559 setup->path_metric =
3560 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3561 IEEE80211_PATH_METRIC_VENDOR :
3562 IEEE80211_PATH_METRIC_AIRTIME;
3563
Javier Cardona581a8b02011-04-07 15:08:27 -07003564
3565 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003566 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003567 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003568 if (!is_valid_ie_attr(ieattr))
3569 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003570 setup->ie = nla_data(ieattr);
3571 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003572 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003573 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3574 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003575
3576 return 0;
3577}
3578
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003579static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003580 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003581{
3582 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3583 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003584 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003585 struct mesh_config cfg;
3586 u32 mask;
3587 int err;
3588
Johannes Berg29cbe682010-12-03 09:20:44 +01003589 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3590 return -EOPNOTSUPP;
3591
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003592 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003593 return -EOPNOTSUPP;
3594
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003595 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003596 if (err)
3597 return err;
3598
Johannes Berg29cbe682010-12-03 09:20:44 +01003599 wdev_lock(wdev);
3600 if (!wdev->mesh_id_len)
3601 err = -ENOLINK;
3602
3603 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003604 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003605 mask, &cfg);
3606
3607 wdev_unlock(wdev);
3608
3609 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003610}
3611
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003612static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3613{
3614 struct sk_buff *msg;
3615 void *hdr = NULL;
3616 struct nlattr *nl_reg_rules;
3617 unsigned int i;
3618 int err = -EINVAL;
3619
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003620 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003621
3622 if (!cfg80211_regdomain)
3623 goto out;
3624
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003625 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003626 if (!msg) {
3627 err = -ENOBUFS;
3628 goto out;
3629 }
3630
3631 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3632 NL80211_CMD_GET_REG);
3633 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003634 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003635
David S. Miller9360ffd2012-03-29 04:41:26 -04003636 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3637 cfg80211_regdomain->alpha2) ||
3638 (cfg80211_regdomain->dfs_region &&
3639 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3640 cfg80211_regdomain->dfs_region)))
3641 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003642
3643 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3644 if (!nl_reg_rules)
3645 goto nla_put_failure;
3646
3647 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3648 struct nlattr *nl_reg_rule;
3649 const struct ieee80211_reg_rule *reg_rule;
3650 const struct ieee80211_freq_range *freq_range;
3651 const struct ieee80211_power_rule *power_rule;
3652
3653 reg_rule = &cfg80211_regdomain->reg_rules[i];
3654 freq_range = &reg_rule->freq_range;
3655 power_rule = &reg_rule->power_rule;
3656
3657 nl_reg_rule = nla_nest_start(msg, i);
3658 if (!nl_reg_rule)
3659 goto nla_put_failure;
3660
David S. Miller9360ffd2012-03-29 04:41:26 -04003661 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3662 reg_rule->flags) ||
3663 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3664 freq_range->start_freq_khz) ||
3665 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3666 freq_range->end_freq_khz) ||
3667 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3668 freq_range->max_bandwidth_khz) ||
3669 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3670 power_rule->max_antenna_gain) ||
3671 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3672 power_rule->max_eirp))
3673 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003674
3675 nla_nest_end(msg, nl_reg_rule);
3676 }
3677
3678 nla_nest_end(msg, nl_reg_rules);
3679
3680 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003681 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003682 goto out;
3683
3684nla_put_failure:
3685 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003686put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003687 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003688 err = -EMSGSIZE;
3689out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003690 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003691 return err;
3692}
3693
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003694static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3695{
3696 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3697 struct nlattr *nl_reg_rule;
3698 char *alpha2 = NULL;
3699 int rem_reg_rules = 0, r = 0;
3700 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003701 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003702 struct ieee80211_regdomain *rd = NULL;
3703
3704 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3705 return -EINVAL;
3706
3707 if (!info->attrs[NL80211_ATTR_REG_RULES])
3708 return -EINVAL;
3709
3710 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3711
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003712 if (info->attrs[NL80211_ATTR_DFS_REGION])
3713 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3714
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003715 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3716 rem_reg_rules) {
3717 num_rules++;
3718 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003719 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003720 }
3721
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003722 mutex_lock(&cfg80211_mutex);
3723
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003724 if (!reg_is_valid_request(alpha2)) {
3725 r = -EINVAL;
3726 goto bad_reg;
3727 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003728
3729 size_of_regd = sizeof(struct ieee80211_regdomain) +
3730 (num_rules * sizeof(struct ieee80211_reg_rule));
3731
3732 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003733 if (!rd) {
3734 r = -ENOMEM;
3735 goto bad_reg;
3736 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003737
3738 rd->n_reg_rules = num_rules;
3739 rd->alpha2[0] = alpha2[0];
3740 rd->alpha2[1] = alpha2[1];
3741
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003742 /*
3743 * Disable DFS master mode if the DFS region was
3744 * not supported or known on this kernel.
3745 */
3746 if (reg_supported_dfs_region(dfs_region))
3747 rd->dfs_region = dfs_region;
3748
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003749 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3750 rem_reg_rules) {
3751 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3752 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3753 reg_rule_policy);
3754 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3755 if (r)
3756 goto bad_reg;
3757
3758 rule_idx++;
3759
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003760 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3761 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003762 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003763 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003764 }
3765
3766 BUG_ON(rule_idx != num_rules);
3767
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003768 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003769
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003770 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003771
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003772 return r;
3773
Johannes Bergd2372b32008-10-24 20:32:20 +02003774 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003775 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003776 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003777 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003778}
3779
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003780static int validate_scan_freqs(struct nlattr *freqs)
3781{
3782 struct nlattr *attr1, *attr2;
3783 int n_channels = 0, tmp1, tmp2;
3784
3785 nla_for_each_nested(attr1, freqs, tmp1) {
3786 n_channels++;
3787 /*
3788 * Some hardware has a limited channel list for
3789 * scanning, and it is pretty much nonsensical
3790 * to scan for a channel twice, so disallow that
3791 * and don't require drivers to check that the
3792 * channel list they get isn't longer than what
3793 * they can scan, as long as they can scan all
3794 * the channels they registered at once.
3795 */
3796 nla_for_each_nested(attr2, freqs, tmp2)
3797 if (attr1 != attr2 &&
3798 nla_get_u32(attr1) == nla_get_u32(attr2))
3799 return 0;
3800 }
3801
3802 return n_channels;
3803}
3804
Johannes Berg2a519312009-02-10 21:25:55 +01003805static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3806{
Johannes Berg4c476992010-10-04 21:36:35 +02003807 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3808 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003809 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003810 struct nlattr *attr;
3811 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003812 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003813 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003814
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003815 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3816 return -EINVAL;
3817
Johannes Berg79c97e92009-07-07 03:56:12 +02003818 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003819
Johannes Berg4c476992010-10-04 21:36:35 +02003820 if (!rdev->ops->scan)
3821 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003822
Johannes Berg4c476992010-10-04 21:36:35 +02003823 if (rdev->scan_req)
3824 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003825
3826 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003827 n_channels = validate_scan_freqs(
3828 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003829 if (!n_channels)
3830 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003831 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003832 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003833 n_channels = 0;
3834
Johannes Berg2a519312009-02-10 21:25:55 +01003835 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3836 if (wiphy->bands[band])
3837 n_channels += wiphy->bands[band]->n_channels;
3838 }
3839
3840 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3841 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3842 n_ssids++;
3843
Johannes Berg4c476992010-10-04 21:36:35 +02003844 if (n_ssids > wiphy->max_scan_ssids)
3845 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003846
Jouni Malinen70692ad2009-02-16 19:39:13 +02003847 if (info->attrs[NL80211_ATTR_IE])
3848 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3849 else
3850 ie_len = 0;
3851
Johannes Berg4c476992010-10-04 21:36:35 +02003852 if (ie_len > wiphy->max_scan_ie_len)
3853 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003854
Johannes Berg2a519312009-02-10 21:25:55 +01003855 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003856 + sizeof(*request->ssids) * n_ssids
3857 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003858 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003859 if (!request)
3860 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003861
Johannes Berg2a519312009-02-10 21:25:55 +01003862 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003863 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003864 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003865 if (ie_len) {
3866 if (request->ssids)
3867 request->ie = (void *)(request->ssids + n_ssids);
3868 else
3869 request->ie = (void *)(request->channels + n_channels);
3870 }
Johannes Berg2a519312009-02-10 21:25:55 +01003871
Johannes Berg584991d2009-11-02 13:32:03 +01003872 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003873 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3874 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003875 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003876 struct ieee80211_channel *chan;
3877
3878 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3879
3880 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003881 err = -EINVAL;
3882 goto out_free;
3883 }
Johannes Berg584991d2009-11-02 13:32:03 +01003884
3885 /* ignore disabled channels */
3886 if (chan->flags & IEEE80211_CHAN_DISABLED)
3887 continue;
3888
3889 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003890 i++;
3891 }
3892 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003893 enum ieee80211_band band;
3894
Johannes Berg2a519312009-02-10 21:25:55 +01003895 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003896 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3897 int j;
3898 if (!wiphy->bands[band])
3899 continue;
3900 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003901 struct ieee80211_channel *chan;
3902
3903 chan = &wiphy->bands[band]->channels[j];
3904
3905 if (chan->flags & IEEE80211_CHAN_DISABLED)
3906 continue;
3907
3908 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003909 i++;
3910 }
3911 }
3912 }
3913
Johannes Berg584991d2009-11-02 13:32:03 +01003914 if (!i) {
3915 err = -EINVAL;
3916 goto out_free;
3917 }
3918
3919 request->n_channels = i;
3920
Johannes Berg2a519312009-02-10 21:25:55 +01003921 i = 0;
3922 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3923 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003924 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003925 err = -EINVAL;
3926 goto out_free;
3927 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003928 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003929 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003930 i++;
3931 }
3932 }
3933
Jouni Malinen70692ad2009-02-16 19:39:13 +02003934 if (info->attrs[NL80211_ATTR_IE]) {
3935 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003936 memcpy((void *)request->ie,
3937 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003938 request->ie_len);
3939 }
3940
Johannes Berg34850ab2011-07-18 18:08:35 +02003941 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003942 if (wiphy->bands[i])
3943 request->rates[i] =
3944 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003945
3946 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3947 nla_for_each_nested(attr,
3948 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3949 tmp) {
3950 enum ieee80211_band band = nla_type(attr);
3951
Dan Carpenter84404622011-07-29 11:52:18 +03003952 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003953 err = -EINVAL;
3954 goto out_free;
3955 }
3956 err = ieee80211_get_ratemask(wiphy->bands[band],
3957 nla_data(attr),
3958 nla_len(attr),
3959 &request->rates[band]);
3960 if (err)
3961 goto out_free;
3962 }
3963 }
3964
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303965 request->no_cck =
3966 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3967
Johannes Berg463d0182009-07-14 00:33:35 +02003968 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003969 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003970
Johannes Berg79c97e92009-07-07 03:56:12 +02003971 rdev->scan_req = request;
3972 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003973
Johannes Berg463d0182009-07-14 00:33:35 +02003974 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003975 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003976 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003977 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003978 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003979 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003980 kfree(request);
3981 }
Johannes Berg3b858752009-03-12 09:55:09 +01003982
Johannes Berg2a519312009-02-10 21:25:55 +01003983 return err;
3984}
3985
Luciano Coelho807f8a82011-05-11 17:09:35 +03003986static int nl80211_start_sched_scan(struct sk_buff *skb,
3987 struct genl_info *info)
3988{
3989 struct cfg80211_sched_scan_request *request;
3990 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3991 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003992 struct nlattr *attr;
3993 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003994 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003995 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003996 enum ieee80211_band band;
3997 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003998 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003999
4000 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4001 !rdev->ops->sched_scan_start)
4002 return -EOPNOTSUPP;
4003
4004 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4005 return -EINVAL;
4006
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004007 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4008 return -EINVAL;
4009
4010 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4011 if (interval == 0)
4012 return -EINVAL;
4013
Luciano Coelho807f8a82011-05-11 17:09:35 +03004014 wiphy = &rdev->wiphy;
4015
4016 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4017 n_channels = validate_scan_freqs(
4018 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4019 if (!n_channels)
4020 return -EINVAL;
4021 } else {
4022 n_channels = 0;
4023
4024 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4025 if (wiphy->bands[band])
4026 n_channels += wiphy->bands[band]->n_channels;
4027 }
4028
4029 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4030 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4031 tmp)
4032 n_ssids++;
4033
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004034 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004035 return -EINVAL;
4036
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004037 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4038 nla_for_each_nested(attr,
4039 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4040 tmp)
4041 n_match_sets++;
4042
4043 if (n_match_sets > wiphy->max_match_sets)
4044 return -EINVAL;
4045
Luciano Coelho807f8a82011-05-11 17:09:35 +03004046 if (info->attrs[NL80211_ATTR_IE])
4047 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4048 else
4049 ie_len = 0;
4050
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004051 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004052 return -EINVAL;
4053
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004054 mutex_lock(&rdev->sched_scan_mtx);
4055
4056 if (rdev->sched_scan_req) {
4057 err = -EINPROGRESS;
4058 goto out;
4059 }
4060
Luciano Coelho807f8a82011-05-11 17:09:35 +03004061 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004062 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004063 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004064 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004065 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004066 if (!request) {
4067 err = -ENOMEM;
4068 goto out;
4069 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004070
4071 if (n_ssids)
4072 request->ssids = (void *)&request->channels[n_channels];
4073 request->n_ssids = n_ssids;
4074 if (ie_len) {
4075 if (request->ssids)
4076 request->ie = (void *)(request->ssids + n_ssids);
4077 else
4078 request->ie = (void *)(request->channels + n_channels);
4079 }
4080
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004081 if (n_match_sets) {
4082 if (request->ie)
4083 request->match_sets = (void *)(request->ie + ie_len);
4084 else if (request->ssids)
4085 request->match_sets =
4086 (void *)(request->ssids + n_ssids);
4087 else
4088 request->match_sets =
4089 (void *)(request->channels + n_channels);
4090 }
4091 request->n_match_sets = n_match_sets;
4092
Luciano Coelho807f8a82011-05-11 17:09:35 +03004093 i = 0;
4094 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4095 /* user specified, bail out if channel not found */
4096 nla_for_each_nested(attr,
4097 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4098 tmp) {
4099 struct ieee80211_channel *chan;
4100
4101 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4102
4103 if (!chan) {
4104 err = -EINVAL;
4105 goto out_free;
4106 }
4107
4108 /* ignore disabled channels */
4109 if (chan->flags & IEEE80211_CHAN_DISABLED)
4110 continue;
4111
4112 request->channels[i] = chan;
4113 i++;
4114 }
4115 } else {
4116 /* all channels */
4117 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4118 int j;
4119 if (!wiphy->bands[band])
4120 continue;
4121 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4122 struct ieee80211_channel *chan;
4123
4124 chan = &wiphy->bands[band]->channels[j];
4125
4126 if (chan->flags & IEEE80211_CHAN_DISABLED)
4127 continue;
4128
4129 request->channels[i] = chan;
4130 i++;
4131 }
4132 }
4133 }
4134
4135 if (!i) {
4136 err = -EINVAL;
4137 goto out_free;
4138 }
4139
4140 request->n_channels = i;
4141
4142 i = 0;
4143 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4144 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4145 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004146 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004147 err = -EINVAL;
4148 goto out_free;
4149 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004150 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004151 memcpy(request->ssids[i].ssid, nla_data(attr),
4152 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004153 i++;
4154 }
4155 }
4156
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004157 i = 0;
4158 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4159 nla_for_each_nested(attr,
4160 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4161 tmp) {
4162 struct nlattr *ssid;
4163
4164 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4165 nla_data(attr), nla_len(attr),
4166 nl80211_match_policy);
4167 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4168 if (ssid) {
4169 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4170 err = -EINVAL;
4171 goto out_free;
4172 }
4173 memcpy(request->match_sets[i].ssid.ssid,
4174 nla_data(ssid), nla_len(ssid));
4175 request->match_sets[i].ssid.ssid_len =
4176 nla_len(ssid);
4177 }
4178 i++;
4179 }
4180 }
4181
Luciano Coelho807f8a82011-05-11 17:09:35 +03004182 if (info->attrs[NL80211_ATTR_IE]) {
4183 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4184 memcpy((void *)request->ie,
4185 nla_data(info->attrs[NL80211_ATTR_IE]),
4186 request->ie_len);
4187 }
4188
4189 request->dev = dev;
4190 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004191 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004192
4193 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4194 if (!err) {
4195 rdev->sched_scan_req = request;
4196 nl80211_send_sched_scan(rdev, dev,
4197 NL80211_CMD_START_SCHED_SCAN);
4198 goto out;
4199 }
4200
4201out_free:
4202 kfree(request);
4203out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004204 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004205 return err;
4206}
4207
4208static int nl80211_stop_sched_scan(struct sk_buff *skb,
4209 struct genl_info *info)
4210{
4211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004212 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004213
4214 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4215 !rdev->ops->sched_scan_stop)
4216 return -EOPNOTSUPP;
4217
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004218 mutex_lock(&rdev->sched_scan_mtx);
4219 err = __cfg80211_stop_sched_scan(rdev, false);
4220 mutex_unlock(&rdev->sched_scan_mtx);
4221
4222 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004223}
4224
Johannes Berg9720bb32011-06-21 09:45:33 +02004225static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4226 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004227 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004228 struct wireless_dev *wdev,
4229 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004230{
Johannes Berg48ab9052009-07-10 18:42:31 +02004231 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004232 void *hdr;
4233 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004234
4235 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004236
Johannes Berg9720bb32011-06-21 09:45:33 +02004237 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004238 NL80211_CMD_NEW_SCAN_RESULTS);
4239 if (!hdr)
4240 return -1;
4241
Johannes Berg9720bb32011-06-21 09:45:33 +02004242 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4243
David S. Miller9360ffd2012-03-29 04:41:26 -04004244 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4245 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4246 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004247
4248 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4249 if (!bss)
4250 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004251 if ((!is_zero_ether_addr(res->bssid) &&
4252 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4253 (res->information_elements && res->len_information_elements &&
4254 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4255 res->len_information_elements,
4256 res->information_elements)) ||
4257 (res->beacon_ies && res->len_beacon_ies &&
4258 res->beacon_ies != res->information_elements &&
4259 nla_put(msg, NL80211_BSS_BEACON_IES,
4260 res->len_beacon_ies, res->beacon_ies)))
4261 goto nla_put_failure;
4262 if (res->tsf &&
4263 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4264 goto nla_put_failure;
4265 if (res->beacon_interval &&
4266 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4267 goto nla_put_failure;
4268 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4269 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4270 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4271 jiffies_to_msecs(jiffies - intbss->ts)))
4272 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004273
Johannes Berg77965c92009-02-18 18:45:06 +01004274 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004275 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004276 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4277 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004278 break;
4279 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004280 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4281 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004282 break;
4283 default:
4284 break;
4285 }
4286
Johannes Berg48ab9052009-07-10 18:42:31 +02004287 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004288 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004289 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004290 if (intbss == wdev->current_bss &&
4291 nla_put_u32(msg, NL80211_BSS_STATUS,
4292 NL80211_BSS_STATUS_ASSOCIATED))
4293 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004294 break;
4295 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004296 if (intbss == wdev->current_bss &&
4297 nla_put_u32(msg, NL80211_BSS_STATUS,
4298 NL80211_BSS_STATUS_IBSS_JOINED))
4299 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004300 break;
4301 default:
4302 break;
4303 }
4304
Johannes Berg2a519312009-02-10 21:25:55 +01004305 nla_nest_end(msg, bss);
4306
4307 return genlmsg_end(msg, hdr);
4308
4309 nla_put_failure:
4310 genlmsg_cancel(msg, hdr);
4311 return -EMSGSIZE;
4312}
4313
4314static int nl80211_dump_scan(struct sk_buff *skb,
4315 struct netlink_callback *cb)
4316{
Johannes Berg48ab9052009-07-10 18:42:31 +02004317 struct cfg80211_registered_device *rdev;
4318 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004319 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004320 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004321 int start = cb->args[1], idx = 0;
4322 int err;
4323
Johannes Berg67748892010-10-04 21:14:06 +02004324 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4325 if (err)
4326 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004327
Johannes Berg48ab9052009-07-10 18:42:31 +02004328 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004329
Johannes Berg48ab9052009-07-10 18:42:31 +02004330 wdev_lock(wdev);
4331 spin_lock_bh(&rdev->bss_lock);
4332 cfg80211_bss_expire(rdev);
4333
Johannes Berg9720bb32011-06-21 09:45:33 +02004334 cb->seq = rdev->bss_generation;
4335
Johannes Berg48ab9052009-07-10 18:42:31 +02004336 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004337 if (++idx <= start)
4338 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004339 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004340 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004341 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004342 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004343 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004344 }
4345 }
4346
Johannes Berg48ab9052009-07-10 18:42:31 +02004347 spin_unlock_bh(&rdev->bss_lock);
4348 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004349
4350 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004351 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004352
Johannes Berg67748892010-10-04 21:14:06 +02004353 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004354}
4355
Holger Schurig61fa7132009-11-11 12:25:40 +01004356static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4357 int flags, struct net_device *dev,
4358 struct survey_info *survey)
4359{
4360 void *hdr;
4361 struct nlattr *infoattr;
4362
Holger Schurig61fa7132009-11-11 12:25:40 +01004363 hdr = nl80211hdr_put(msg, pid, seq, flags,
4364 NL80211_CMD_NEW_SURVEY_RESULTS);
4365 if (!hdr)
4366 return -ENOMEM;
4367
David S. Miller9360ffd2012-03-29 04:41:26 -04004368 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4369 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004370
4371 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4372 if (!infoattr)
4373 goto nla_put_failure;
4374
David S. Miller9360ffd2012-03-29 04:41:26 -04004375 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4376 survey->channel->center_freq))
4377 goto nla_put_failure;
4378
4379 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4380 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4381 goto nla_put_failure;
4382 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4383 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4384 goto nla_put_failure;
4385 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4386 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4387 survey->channel_time))
4388 goto nla_put_failure;
4389 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4390 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4391 survey->channel_time_busy))
4392 goto nla_put_failure;
4393 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4394 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4395 survey->channel_time_ext_busy))
4396 goto nla_put_failure;
4397 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4398 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4399 survey->channel_time_rx))
4400 goto nla_put_failure;
4401 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4402 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4403 survey->channel_time_tx))
4404 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004405
4406 nla_nest_end(msg, infoattr);
4407
4408 return genlmsg_end(msg, hdr);
4409
4410 nla_put_failure:
4411 genlmsg_cancel(msg, hdr);
4412 return -EMSGSIZE;
4413}
4414
4415static int nl80211_dump_survey(struct sk_buff *skb,
4416 struct netlink_callback *cb)
4417{
4418 struct survey_info survey;
4419 struct cfg80211_registered_device *dev;
4420 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004421 int survey_idx = cb->args[1];
4422 int res;
4423
Johannes Berg67748892010-10-04 21:14:06 +02004424 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4425 if (res)
4426 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004427
4428 if (!dev->ops->dump_survey) {
4429 res = -EOPNOTSUPP;
4430 goto out_err;
4431 }
4432
4433 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004434 struct ieee80211_channel *chan;
4435
Holger Schurig61fa7132009-11-11 12:25:40 +01004436 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4437 &survey);
4438 if (res == -ENOENT)
4439 break;
4440 if (res)
4441 goto out_err;
4442
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004443 /* Survey without a channel doesn't make sense */
4444 if (!survey.channel) {
4445 res = -EINVAL;
4446 goto out;
4447 }
4448
4449 chan = ieee80211_get_channel(&dev->wiphy,
4450 survey.channel->center_freq);
4451 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4452 survey_idx++;
4453 continue;
4454 }
4455
Holger Schurig61fa7132009-11-11 12:25:40 +01004456 if (nl80211_send_survey(skb,
4457 NETLINK_CB(cb->skb).pid,
4458 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4459 netdev,
4460 &survey) < 0)
4461 goto out;
4462 survey_idx++;
4463 }
4464
4465 out:
4466 cb->args[1] = survey_idx;
4467 res = skb->len;
4468 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004469 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004470 return res;
4471}
4472
Jouni Malinen255e7372009-03-20 21:21:17 +02004473static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4474{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004475 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004476}
4477
Samuel Ortizb23aa672009-07-01 21:26:54 +02004478static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4479{
4480 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4481 NL80211_WPA_VERSION_2));
4482}
4483
Jouni Malinen636a5d32009-03-19 13:39:22 +02004484static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4485{
Johannes Berg4c476992010-10-04 21:36:35 +02004486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4487 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004488 struct ieee80211_channel *chan;
4489 const u8 *bssid, *ssid, *ie = NULL;
4490 int err, ssid_len, ie_len = 0;
4491 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004492 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004493 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004494
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004495 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4496 return -EINVAL;
4497
4498 if (!info->attrs[NL80211_ATTR_MAC])
4499 return -EINVAL;
4500
Jouni Malinen17780922009-03-27 20:52:47 +02004501 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4502 return -EINVAL;
4503
Johannes Berg19957bb2009-07-02 17:20:43 +02004504 if (!info->attrs[NL80211_ATTR_SSID])
4505 return -EINVAL;
4506
4507 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4508 return -EINVAL;
4509
Johannes Bergfffd0932009-07-08 14:22:54 +02004510 err = nl80211_parse_key(info, &key);
4511 if (err)
4512 return err;
4513
4514 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004515 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4516 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004517 if (!key.p.key || !key.p.key_len)
4518 return -EINVAL;
4519 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4520 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4521 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4522 key.p.key_len != WLAN_KEY_LEN_WEP104))
4523 return -EINVAL;
4524 if (key.idx > 4)
4525 return -EINVAL;
4526 } else {
4527 key.p.key_len = 0;
4528 key.p.key = NULL;
4529 }
4530
Johannes Bergafea0b72010-08-10 09:46:42 +02004531 if (key.idx >= 0) {
4532 int i;
4533 bool ok = false;
4534 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4535 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4536 ok = true;
4537 break;
4538 }
4539 }
Johannes Berg4c476992010-10-04 21:36:35 +02004540 if (!ok)
4541 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004542 }
4543
Johannes Berg4c476992010-10-04 21:36:35 +02004544 if (!rdev->ops->auth)
4545 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004546
Johannes Berg074ac8d2010-09-16 14:58:22 +02004547 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004548 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4549 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004550
Johannes Berg19957bb2009-07-02 17:20:43 +02004551 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004552 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004553 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004554 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4555 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004556
Johannes Berg19957bb2009-07-02 17:20:43 +02004557 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4558 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4559
4560 if (info->attrs[NL80211_ATTR_IE]) {
4561 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4562 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4563 }
4564
4565 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004566 if (!nl80211_valid_auth_type(auth_type))
4567 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004568
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004569 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4570
Johannes Berg95de8172012-01-20 13:55:25 +01004571 /*
4572 * Since we no longer track auth state, ignore
4573 * requests to only change local state.
4574 */
4575 if (local_state_change)
4576 return 0;
4577
Johannes Berg4c476992010-10-04 21:36:35 +02004578 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4579 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004580 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004581}
4582
Johannes Bergc0692b82010-08-27 14:26:53 +03004583static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4584 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004585 struct cfg80211_crypto_settings *settings,
4586 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004587{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004588 memset(settings, 0, sizeof(*settings));
4589
Samuel Ortizb23aa672009-07-01 21:26:54 +02004590 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4591
Johannes Bergc0692b82010-08-27 14:26:53 +03004592 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4593 u16 proto;
4594 proto = nla_get_u16(
4595 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4596 settings->control_port_ethertype = cpu_to_be16(proto);
4597 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4598 proto != ETH_P_PAE)
4599 return -EINVAL;
4600 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4601 settings->control_port_no_encrypt = true;
4602 } else
4603 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4604
Samuel Ortizb23aa672009-07-01 21:26:54 +02004605 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4606 void *data;
4607 int len, i;
4608
4609 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4610 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4611 settings->n_ciphers_pairwise = len / sizeof(u32);
4612
4613 if (len % sizeof(u32))
4614 return -EINVAL;
4615
Johannes Berg3dc27d22009-07-02 21:36:37 +02004616 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004617 return -EINVAL;
4618
4619 memcpy(settings->ciphers_pairwise, data, len);
4620
4621 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004622 if (!cfg80211_supported_cipher_suite(
4623 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004624 settings->ciphers_pairwise[i]))
4625 return -EINVAL;
4626 }
4627
4628 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4629 settings->cipher_group =
4630 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004631 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4632 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004633 return -EINVAL;
4634 }
4635
4636 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4637 settings->wpa_versions =
4638 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4639 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4640 return -EINVAL;
4641 }
4642
4643 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4644 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004645 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004646
4647 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4648 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4649 settings->n_akm_suites = len / sizeof(u32);
4650
4651 if (len % sizeof(u32))
4652 return -EINVAL;
4653
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004654 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4655 return -EINVAL;
4656
Samuel Ortizb23aa672009-07-01 21:26:54 +02004657 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004658 }
4659
4660 return 0;
4661}
4662
Jouni Malinen636a5d32009-03-19 13:39:22 +02004663static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4664{
Johannes Berg4c476992010-10-04 21:36:35 +02004665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4666 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004667 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004668 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004669 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004670 int err, ssid_len, ie_len = 0;
4671 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004672 u32 flags = 0;
4673 struct ieee80211_ht_cap *ht_capa = NULL;
4674 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004675
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004676 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4677 return -EINVAL;
4678
4679 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004680 !info->attrs[NL80211_ATTR_SSID] ||
4681 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004682 return -EINVAL;
4683
Johannes Berg4c476992010-10-04 21:36:35 +02004684 if (!rdev->ops->assoc)
4685 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004686
Johannes Berg074ac8d2010-09-16 14:58:22 +02004687 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004688 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4689 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004690
Johannes Berg19957bb2009-07-02 17:20:43 +02004691 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004692
Johannes Berg19957bb2009-07-02 17:20:43 +02004693 chan = ieee80211_get_channel(&rdev->wiphy,
4694 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004695 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4696 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004697
Johannes Berg19957bb2009-07-02 17:20:43 +02004698 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4699 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004700
4701 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004702 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4703 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004704 }
4705
Jouni Malinendc6382c2009-05-06 22:09:37 +03004706 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004707 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004708 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004709 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004710 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004711 else if (mfp != NL80211_MFP_NO)
4712 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004713 }
4714
Johannes Berg3e5d7642009-07-07 14:37:26 +02004715 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4716 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4717
Ben Greear7e7c8922011-11-18 11:31:59 -08004718 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4719 flags |= ASSOC_REQ_DISABLE_HT;
4720
4721 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4722 ht_capa_mask =
4723 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4724
4725 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4726 if (!ht_capa_mask)
4727 return -EINVAL;
4728 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4729 }
4730
Johannes Bergc0692b82010-08-27 14:26:53 +03004731 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004732 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004733 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4734 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004735 &crypto, flags, ht_capa,
4736 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004737
Jouni Malinen636a5d32009-03-19 13:39:22 +02004738 return err;
4739}
4740
4741static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4742{
Johannes Berg4c476992010-10-04 21:36:35 +02004743 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4744 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004745 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004746 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004747 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004748 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004749
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004750 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4751 return -EINVAL;
4752
4753 if (!info->attrs[NL80211_ATTR_MAC])
4754 return -EINVAL;
4755
4756 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4757 return -EINVAL;
4758
Johannes Berg4c476992010-10-04 21:36:35 +02004759 if (!rdev->ops->deauth)
4760 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004761
Johannes Berg074ac8d2010-09-16 14:58:22 +02004762 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4764 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004765
Johannes Berg19957bb2009-07-02 17:20:43 +02004766 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004767
Johannes Berg19957bb2009-07-02 17:20:43 +02004768 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4769 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004770 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004771 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004772 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004773
4774 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004775 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4776 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004777 }
4778
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004779 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4780
Johannes Berg4c476992010-10-04 21:36:35 +02004781 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4782 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004783}
4784
4785static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4786{
Johannes Berg4c476992010-10-04 21:36:35 +02004787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4788 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004789 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004790 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004791 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004792 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004793
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004794 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4795 return -EINVAL;
4796
4797 if (!info->attrs[NL80211_ATTR_MAC])
4798 return -EINVAL;
4799
4800 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4801 return -EINVAL;
4802
Johannes Berg4c476992010-10-04 21:36:35 +02004803 if (!rdev->ops->disassoc)
4804 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004805
Johannes Berg074ac8d2010-09-16 14:58:22 +02004806 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004807 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4808 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004809
Johannes Berg19957bb2009-07-02 17:20:43 +02004810 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004811
Johannes Berg19957bb2009-07-02 17:20:43 +02004812 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4813 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004814 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004815 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004816 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004817
4818 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004819 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4820 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004821 }
4822
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004823 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4824
Johannes Berg4c476992010-10-04 21:36:35 +02004825 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4826 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004827}
4828
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004829static bool
4830nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4831 int mcast_rate[IEEE80211_NUM_BANDS],
4832 int rateval)
4833{
4834 struct wiphy *wiphy = &rdev->wiphy;
4835 bool found = false;
4836 int band, i;
4837
4838 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4839 struct ieee80211_supported_band *sband;
4840
4841 sband = wiphy->bands[band];
4842 if (!sband)
4843 continue;
4844
4845 for (i = 0; i < sband->n_bitrates; i++) {
4846 if (sband->bitrates[i].bitrate == rateval) {
4847 mcast_rate[band] = i + 1;
4848 found = true;
4849 break;
4850 }
4851 }
4852 }
4853
4854 return found;
4855}
4856
Johannes Berg04a773a2009-04-19 21:24:32 +02004857static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4858{
Johannes Berg4c476992010-10-04 21:36:35 +02004859 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4860 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004861 struct cfg80211_ibss_params ibss;
4862 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004863 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004864 int err;
4865
Johannes Berg8e30bc52009-04-22 17:45:38 +02004866 memset(&ibss, 0, sizeof(ibss));
4867
Johannes Berg04a773a2009-04-19 21:24:32 +02004868 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4869 return -EINVAL;
4870
4871 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4872 !info->attrs[NL80211_ATTR_SSID] ||
4873 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4874 return -EINVAL;
4875
Johannes Berg8e30bc52009-04-22 17:45:38 +02004876 ibss.beacon_interval = 100;
4877
4878 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4879 ibss.beacon_interval =
4880 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4881 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4882 return -EINVAL;
4883 }
4884
Johannes Berg4c476992010-10-04 21:36:35 +02004885 if (!rdev->ops->join_ibss)
4886 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004887
Johannes Berg4c476992010-10-04 21:36:35 +02004888 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4889 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004890
Johannes Berg79c97e92009-07-07 03:56:12 +02004891 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004892
Johannes Berg39193492011-09-16 13:45:25 +02004893 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004894 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004895
4896 if (!is_valid_ether_addr(ibss.bssid))
4897 return -EINVAL;
4898 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004899 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4900 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4901
4902 if (info->attrs[NL80211_ATTR_IE]) {
4903 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4904 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4905 }
4906
Alexander Simon54858ee2011-11-30 16:56:32 +01004907 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4908 enum nl80211_channel_type channel_type;
4909
4910 channel_type = nla_get_u32(
4911 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4912 if (channel_type != NL80211_CHAN_NO_HT &&
4913 channel_type != NL80211_CHAN_HT20 &&
4914 channel_type != NL80211_CHAN_HT40MINUS &&
4915 channel_type != NL80211_CHAN_HT40PLUS)
4916 return -EINVAL;
4917
4918 if (channel_type != NL80211_CHAN_NO_HT &&
4919 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4920 return -EINVAL;
4921
4922 ibss.channel_type = channel_type;
4923 } else {
4924 ibss.channel_type = NL80211_CHAN_NO_HT;
4925 }
4926
4927 ibss.channel = rdev_freq_to_chan(rdev,
4928 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4929 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004930 if (!ibss.channel ||
4931 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004932 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4933 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004934
Alexander Simon54858ee2011-11-30 16:56:32 +01004935 /* Both channels should be able to initiate communication */
4936 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4937 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4938 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4939 ibss.channel_type))
4940 return -EINVAL;
4941
Johannes Berg04a773a2009-04-19 21:24:32 +02004942 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004943 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004944
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004945 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4946 u8 *rates =
4947 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4948 int n_rates =
4949 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4950 struct ieee80211_supported_band *sband =
4951 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004952
Johannes Berg34850ab2011-07-18 18:08:35 +02004953 err = ieee80211_get_ratemask(sband, rates, n_rates,
4954 &ibss.basic_rates);
4955 if (err)
4956 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004957 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004958
4959 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4960 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4961 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4962 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004963
Johannes Berg4c476992010-10-04 21:36:35 +02004964 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4965 connkeys = nl80211_parse_connkeys(rdev,
4966 info->attrs[NL80211_ATTR_KEYS]);
4967 if (IS_ERR(connkeys))
4968 return PTR_ERR(connkeys);
4969 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004970
Antonio Quartulli267335d2012-01-31 20:25:47 +01004971 ibss.control_port =
4972 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4973
Johannes Berg4c476992010-10-04 21:36:35 +02004974 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004975 if (err)
4976 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004977 return err;
4978}
4979
4980static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4981{
Johannes Berg4c476992010-10-04 21:36:35 +02004982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4983 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004984
Johannes Berg4c476992010-10-04 21:36:35 +02004985 if (!rdev->ops->leave_ibss)
4986 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004987
Johannes Berg4c476992010-10-04 21:36:35 +02004988 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4989 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004990
Johannes Berg4c476992010-10-04 21:36:35 +02004991 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004992}
4993
Johannes Bergaff89a92009-07-01 21:26:51 +02004994#ifdef CONFIG_NL80211_TESTMODE
4995static struct genl_multicast_group nl80211_testmode_mcgrp = {
4996 .name = "testmode",
4997};
4998
4999static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5000{
Johannes Berg4c476992010-10-04 21:36:35 +02005001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005002 int err;
5003
5004 if (!info->attrs[NL80211_ATTR_TESTDATA])
5005 return -EINVAL;
5006
Johannes Bergaff89a92009-07-01 21:26:51 +02005007 err = -EOPNOTSUPP;
5008 if (rdev->ops->testmode_cmd) {
5009 rdev->testmode_info = info;
5010 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5011 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5012 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5013 rdev->testmode_info = NULL;
5014 }
5015
Johannes Bergaff89a92009-07-01 21:26:51 +02005016 return err;
5017}
5018
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005019static int nl80211_testmode_dump(struct sk_buff *skb,
5020 struct netlink_callback *cb)
5021{
Johannes Berg00918d32011-12-13 17:22:05 +01005022 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005023 int err;
5024 long phy_idx;
5025 void *data = NULL;
5026 int data_len = 0;
5027
5028 if (cb->args[0]) {
5029 /*
5030 * 0 is a valid index, but not valid for args[0],
5031 * so we need to offset by 1.
5032 */
5033 phy_idx = cb->args[0] - 1;
5034 } else {
5035 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5036 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5037 nl80211_policy);
5038 if (err)
5039 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005040 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5041 phy_idx = nla_get_u32(
5042 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5043 } else {
5044 struct net_device *netdev;
5045
5046 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5047 nl80211_fam.attrbuf,
5048 &rdev, &netdev);
5049 if (err)
5050 return err;
5051 dev_put(netdev);
5052 phy_idx = rdev->wiphy_idx;
5053 cfg80211_unlock_rdev(rdev);
5054 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005055 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5056 cb->args[1] =
5057 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5058 }
5059
5060 if (cb->args[1]) {
5061 data = nla_data((void *)cb->args[1]);
5062 data_len = nla_len((void *)cb->args[1]);
5063 }
5064
5065 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005066 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5067 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005068 mutex_unlock(&cfg80211_mutex);
5069 return -ENOENT;
5070 }
Johannes Berg00918d32011-12-13 17:22:05 +01005071 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005072 mutex_unlock(&cfg80211_mutex);
5073
Johannes Berg00918d32011-12-13 17:22:05 +01005074 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005075 err = -EOPNOTSUPP;
5076 goto out_err;
5077 }
5078
5079 while (1) {
5080 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5081 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5082 NL80211_CMD_TESTMODE);
5083 struct nlattr *tmdata;
5084
David S. Miller9360ffd2012-03-29 04:41:26 -04005085 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005086 genlmsg_cancel(skb, hdr);
5087 break;
5088 }
5089
5090 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5091 if (!tmdata) {
5092 genlmsg_cancel(skb, hdr);
5093 break;
5094 }
Johannes Berg00918d32011-12-13 17:22:05 +01005095 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5096 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005097 nla_nest_end(skb, tmdata);
5098
5099 if (err == -ENOBUFS || err == -ENOENT) {
5100 genlmsg_cancel(skb, hdr);
5101 break;
5102 } else if (err) {
5103 genlmsg_cancel(skb, hdr);
5104 goto out_err;
5105 }
5106
5107 genlmsg_end(skb, hdr);
5108 }
5109
5110 err = skb->len;
5111 /* see above */
5112 cb->args[0] = phy_idx + 1;
5113 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005114 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005115 return err;
5116}
5117
Johannes Bergaff89a92009-07-01 21:26:51 +02005118static struct sk_buff *
5119__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5120 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5121{
5122 struct sk_buff *skb;
5123 void *hdr;
5124 struct nlattr *data;
5125
5126 skb = nlmsg_new(approxlen + 100, gfp);
5127 if (!skb)
5128 return NULL;
5129
5130 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5131 if (!hdr) {
5132 kfree_skb(skb);
5133 return NULL;
5134 }
5135
David S. Miller9360ffd2012-03-29 04:41:26 -04005136 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5137 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005138 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5139
5140 ((void **)skb->cb)[0] = rdev;
5141 ((void **)skb->cb)[1] = hdr;
5142 ((void **)skb->cb)[2] = data;
5143
5144 return skb;
5145
5146 nla_put_failure:
5147 kfree_skb(skb);
5148 return NULL;
5149}
5150
5151struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5152 int approxlen)
5153{
5154 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5155
5156 if (WARN_ON(!rdev->testmode_info))
5157 return NULL;
5158
5159 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5160 rdev->testmode_info->snd_pid,
5161 rdev->testmode_info->snd_seq,
5162 GFP_KERNEL);
5163}
5164EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5165
5166int cfg80211_testmode_reply(struct sk_buff *skb)
5167{
5168 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5169 void *hdr = ((void **)skb->cb)[1];
5170 struct nlattr *data = ((void **)skb->cb)[2];
5171
5172 if (WARN_ON(!rdev->testmode_info)) {
5173 kfree_skb(skb);
5174 return -EINVAL;
5175 }
5176
5177 nla_nest_end(skb, data);
5178 genlmsg_end(skb, hdr);
5179 return genlmsg_reply(skb, rdev->testmode_info);
5180}
5181EXPORT_SYMBOL(cfg80211_testmode_reply);
5182
5183struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5184 int approxlen, gfp_t gfp)
5185{
5186 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5187
5188 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5189}
5190EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5191
5192void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5193{
5194 void *hdr = ((void **)skb->cb)[1];
5195 struct nlattr *data = ((void **)skb->cb)[2];
5196
5197 nla_nest_end(skb, data);
5198 genlmsg_end(skb, hdr);
5199 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5200}
5201EXPORT_SYMBOL(cfg80211_testmode_event);
5202#endif
5203
Samuel Ortizb23aa672009-07-01 21:26:54 +02005204static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5205{
Johannes Berg4c476992010-10-04 21:36:35 +02005206 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5207 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005208 struct cfg80211_connect_params connect;
5209 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005210 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005211 int err;
5212
5213 memset(&connect, 0, sizeof(connect));
5214
5215 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5216 return -EINVAL;
5217
5218 if (!info->attrs[NL80211_ATTR_SSID] ||
5219 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5220 return -EINVAL;
5221
5222 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5223 connect.auth_type =
5224 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5225 if (!nl80211_valid_auth_type(connect.auth_type))
5226 return -EINVAL;
5227 } else
5228 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5229
5230 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5231
Johannes Bergc0692b82010-08-27 14:26:53 +03005232 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005233 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005234 if (err)
5235 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005236
Johannes Berg074ac8d2010-09-16 14:58:22 +02005237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005238 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5239 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005240
Johannes Berg79c97e92009-07-07 03:56:12 +02005241 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005242
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305243 connect.bg_scan_period = -1;
5244 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5245 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5246 connect.bg_scan_period =
5247 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5248 }
5249
Samuel Ortizb23aa672009-07-01 21:26:54 +02005250 if (info->attrs[NL80211_ATTR_MAC])
5251 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5252 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5253 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5254
5255 if (info->attrs[NL80211_ATTR_IE]) {
5256 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5257 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5258 }
5259
5260 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5261 connect.channel =
5262 ieee80211_get_channel(wiphy,
5263 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5264 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005265 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5266 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005267 }
5268
Johannes Bergfffd0932009-07-08 14:22:54 +02005269 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5270 connkeys = nl80211_parse_connkeys(rdev,
5271 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005272 if (IS_ERR(connkeys))
5273 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005274 }
5275
Ben Greear7e7c8922011-11-18 11:31:59 -08005276 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5277 connect.flags |= ASSOC_REQ_DISABLE_HT;
5278
5279 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5280 memcpy(&connect.ht_capa_mask,
5281 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5282 sizeof(connect.ht_capa_mask));
5283
5284 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5285 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5286 return -EINVAL;
5287 memcpy(&connect.ht_capa,
5288 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5289 sizeof(connect.ht_capa));
5290 }
5291
Johannes Bergfffd0932009-07-08 14:22:54 +02005292 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005293 if (err)
5294 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005295 return err;
5296}
5297
5298static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5299{
Johannes Berg4c476992010-10-04 21:36:35 +02005300 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5301 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005302 u16 reason;
5303
5304 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5305 reason = WLAN_REASON_DEAUTH_LEAVING;
5306 else
5307 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5308
5309 if (reason == 0)
5310 return -EINVAL;
5311
Johannes Berg074ac8d2010-09-16 14:58:22 +02005312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5314 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005315
Johannes Berg4c476992010-10-04 21:36:35 +02005316 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005317}
5318
Johannes Berg463d0182009-07-14 00:33:35 +02005319static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5320{
Johannes Berg4c476992010-10-04 21:36:35 +02005321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005322 struct net *net;
5323 int err;
5324 u32 pid;
5325
5326 if (!info->attrs[NL80211_ATTR_PID])
5327 return -EINVAL;
5328
5329 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5330
Johannes Berg463d0182009-07-14 00:33:35 +02005331 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005332 if (IS_ERR(net))
5333 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005334
5335 err = 0;
5336
5337 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005338 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5339 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005340
Johannes Berg463d0182009-07-14 00:33:35 +02005341 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005342 return err;
5343}
5344
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005345static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5346{
Johannes Berg4c476992010-10-04 21:36:35 +02005347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005348 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5349 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005350 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005351 struct cfg80211_pmksa pmksa;
5352
5353 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5354
5355 if (!info->attrs[NL80211_ATTR_MAC])
5356 return -EINVAL;
5357
5358 if (!info->attrs[NL80211_ATTR_PMKID])
5359 return -EINVAL;
5360
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005361 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5362 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5363
Johannes Berg074ac8d2010-09-16 14:58:22 +02005364 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005365 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5366 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005367
5368 switch (info->genlhdr->cmd) {
5369 case NL80211_CMD_SET_PMKSA:
5370 rdev_ops = rdev->ops->set_pmksa;
5371 break;
5372 case NL80211_CMD_DEL_PMKSA:
5373 rdev_ops = rdev->ops->del_pmksa;
5374 break;
5375 default:
5376 WARN_ON(1);
5377 break;
5378 }
5379
Johannes Berg4c476992010-10-04 21:36:35 +02005380 if (!rdev_ops)
5381 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005382
Johannes Berg4c476992010-10-04 21:36:35 +02005383 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005384}
5385
5386static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5387{
Johannes Berg4c476992010-10-04 21:36:35 +02005388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5389 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005390
Johannes Berg074ac8d2010-09-16 14:58:22 +02005391 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005392 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5393 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005394
Johannes Berg4c476992010-10-04 21:36:35 +02005395 if (!rdev->ops->flush_pmksa)
5396 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005397
Johannes Berg4c476992010-10-04 21:36:35 +02005398 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005399}
5400
Arik Nemtsov109086c2011-09-28 14:12:50 +03005401static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5402{
5403 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5404 struct net_device *dev = info->user_ptr[1];
5405 u8 action_code, dialog_token;
5406 u16 status_code;
5407 u8 *peer;
5408
5409 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5410 !rdev->ops->tdls_mgmt)
5411 return -EOPNOTSUPP;
5412
5413 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5414 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5415 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5416 !info->attrs[NL80211_ATTR_IE] ||
5417 !info->attrs[NL80211_ATTR_MAC])
5418 return -EINVAL;
5419
5420 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5421 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5422 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5423 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5424
5425 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5426 dialog_token, status_code,
5427 nla_data(info->attrs[NL80211_ATTR_IE]),
5428 nla_len(info->attrs[NL80211_ATTR_IE]));
5429}
5430
5431static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5432{
5433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5434 struct net_device *dev = info->user_ptr[1];
5435 enum nl80211_tdls_operation operation;
5436 u8 *peer;
5437
5438 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5439 !rdev->ops->tdls_oper)
5440 return -EOPNOTSUPP;
5441
5442 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5443 !info->attrs[NL80211_ATTR_MAC])
5444 return -EINVAL;
5445
5446 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5447 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5448
5449 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5450}
5451
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005452static int nl80211_remain_on_channel(struct sk_buff *skb,
5453 struct genl_info *info)
5454{
Johannes Berg4c476992010-10-04 21:36:35 +02005455 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5456 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005457 struct ieee80211_channel *chan;
5458 struct sk_buff *msg;
5459 void *hdr;
5460 u64 cookie;
5461 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5462 u32 freq, duration;
5463 int err;
5464
5465 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5466 !info->attrs[NL80211_ATTR_DURATION])
5467 return -EINVAL;
5468
5469 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5470
5471 /*
5472 * We should be on that channel for at least one jiffie,
5473 * and more than 5 seconds seems excessive.
5474 */
Johannes Berga2939112010-12-14 17:54:28 +01005475 if (!duration || !msecs_to_jiffies(duration) ||
5476 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005477 return -EINVAL;
5478
Johannes Berg7c4ef712011-11-18 15:33:48 +01005479 if (!rdev->ops->remain_on_channel ||
5480 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005481 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005482
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005483 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5484 channel_type = nla_get_u32(
5485 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5486 if (channel_type != NL80211_CHAN_NO_HT &&
5487 channel_type != NL80211_CHAN_HT20 &&
5488 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005489 channel_type != NL80211_CHAN_HT40MINUS)
5490 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005491 }
5492
5493 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5494 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005495 if (chan == NULL)
5496 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005497
5498 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005499 if (!msg)
5500 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005501
5502 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5503 NL80211_CMD_REMAIN_ON_CHANNEL);
5504
5505 if (IS_ERR(hdr)) {
5506 err = PTR_ERR(hdr);
5507 goto free_msg;
5508 }
5509
5510 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5511 channel_type, duration, &cookie);
5512
5513 if (err)
5514 goto free_msg;
5515
David S. Miller9360ffd2012-03-29 04:41:26 -04005516 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5517 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005518
5519 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005520
5521 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005522
5523 nla_put_failure:
5524 err = -ENOBUFS;
5525 free_msg:
5526 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005527 return err;
5528}
5529
5530static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5531 struct genl_info *info)
5532{
Johannes Berg4c476992010-10-04 21:36:35 +02005533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5534 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005535 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005536
5537 if (!info->attrs[NL80211_ATTR_COOKIE])
5538 return -EINVAL;
5539
Johannes Berg4c476992010-10-04 21:36:35 +02005540 if (!rdev->ops->cancel_remain_on_channel)
5541 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005542
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005543 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5544
Johannes Berg4c476992010-10-04 21:36:35 +02005545 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005546}
5547
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005548static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5549 u8 *rates, u8 rates_len)
5550{
5551 u8 i;
5552 u32 mask = 0;
5553
5554 for (i = 0; i < rates_len; i++) {
5555 int rate = (rates[i] & 0x7f) * 5;
5556 int ridx;
5557 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5558 struct ieee80211_rate *srate =
5559 &sband->bitrates[ridx];
5560 if (rate == srate->bitrate) {
5561 mask |= 1 << ridx;
5562 break;
5563 }
5564 }
5565 if (ridx == sband->n_bitrates)
5566 return 0; /* rate not found */
5567 }
5568
5569 return mask;
5570}
5571
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005572static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5573 u8 *rates, u8 rates_len,
5574 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5575{
5576 u8 i;
5577
5578 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5579
5580 for (i = 0; i < rates_len; i++) {
5581 int ridx, rbit;
5582
5583 ridx = rates[i] / 8;
5584 rbit = BIT(rates[i] % 8);
5585
5586 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005587 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005588 return false;
5589
5590 /* check availability */
5591 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5592 mcs[ridx] |= rbit;
5593 else
5594 return false;
5595 }
5596
5597 return true;
5598}
5599
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005600static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005601 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5602 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005603 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5604 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005605};
5606
5607static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5608 struct genl_info *info)
5609{
5610 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005611 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005612 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005613 int rem, i;
5614 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005615 struct nlattr *tx_rates;
5616 struct ieee80211_supported_band *sband;
5617
5618 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5619 return -EINVAL;
5620
Johannes Berg4c476992010-10-04 21:36:35 +02005621 if (!rdev->ops->set_bitrate_mask)
5622 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005623
5624 memset(&mask, 0, sizeof(mask));
5625 /* Default to all rates enabled */
5626 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5627 sband = rdev->wiphy.bands[i];
5628 mask.control[i].legacy =
5629 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005630 if (sband)
5631 memcpy(mask.control[i].mcs,
5632 sband->ht_cap.mcs.rx_mask,
5633 sizeof(mask.control[i].mcs));
5634 else
5635 memset(mask.control[i].mcs, 0,
5636 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005637 }
5638
5639 /*
5640 * The nested attribute uses enum nl80211_band as the index. This maps
5641 * directly to the enum ieee80211_band values used in cfg80211.
5642 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005643 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005644 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5645 {
5646 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005647 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5648 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005649 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005650 if (sband == NULL)
5651 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005652 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5653 nla_len(tx_rates), nl80211_txattr_policy);
5654 if (tb[NL80211_TXRATE_LEGACY]) {
5655 mask.control[band].legacy = rateset_to_mask(
5656 sband,
5657 nla_data(tb[NL80211_TXRATE_LEGACY]),
5658 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005659 }
5660 if (tb[NL80211_TXRATE_MCS]) {
5661 if (!ht_rateset_to_mask(
5662 sband,
5663 nla_data(tb[NL80211_TXRATE_MCS]),
5664 nla_len(tb[NL80211_TXRATE_MCS]),
5665 mask.control[band].mcs))
5666 return -EINVAL;
5667 }
5668
5669 if (mask.control[band].legacy == 0) {
5670 /* don't allow empty legacy rates if HT
5671 * is not even supported. */
5672 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5673 return -EINVAL;
5674
5675 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5676 if (mask.control[band].mcs[i])
5677 break;
5678
5679 /* legacy and mcs rates may not be both empty */
5680 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005681 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005682 }
5683 }
5684
Johannes Berg4c476992010-10-04 21:36:35 +02005685 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005686}
5687
Johannes Berg2e161f72010-08-12 15:38:38 +02005688static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005689{
Johannes Berg4c476992010-10-04 21:36:35 +02005690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5691 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005692 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005693
5694 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5695 return -EINVAL;
5696
Johannes Berg2e161f72010-08-12 15:38:38 +02005697 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5698 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005699
Johannes Berg9d38d852010-06-09 17:20:33 +02005700 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005701 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005702 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5703 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5704 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005705 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005706 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5707 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005708
5709 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005710 if (!rdev->ops->mgmt_tx)
5711 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005712
Johannes Berg4c476992010-10-04 21:36:35 +02005713 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005714 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005715 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5716 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005717}
5718
Johannes Berg2e161f72010-08-12 15:38:38 +02005719static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005720{
Johannes Berg4c476992010-10-04 21:36:35 +02005721 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5722 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005723 struct ieee80211_channel *chan;
5724 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005725 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005726 u32 freq;
5727 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005728 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005729 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005730 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005731 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005732 bool offchan, no_cck, dont_wait_for_ack;
5733
5734 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005735
5736 if (!info->attrs[NL80211_ATTR_FRAME] ||
5737 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5738 return -EINVAL;
5739
Johannes Berg4c476992010-10-04 21:36:35 +02005740 if (!rdev->ops->mgmt_tx)
5741 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005742
Johannes Berg9d38d852010-06-09 17:20:33 +02005743 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005744 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005745 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5746 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5747 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005748 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005749 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5750 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005751
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005752 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005753 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005754 return -EINVAL;
5755 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5756 }
5757
Jouni Malinen026331c2010-02-15 12:53:10 +02005758 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5759 channel_type = nla_get_u32(
5760 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5761 if (channel_type != NL80211_CHAN_NO_HT &&
5762 channel_type != NL80211_CHAN_HT20 &&
5763 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005764 channel_type != NL80211_CHAN_HT40MINUS)
5765 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005766 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005767 }
5768
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005769 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5770
Johannes Berg7c4ef712011-11-18 15:33:48 +01005771 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5772 return -EINVAL;
5773
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305774 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5775
Jouni Malinen026331c2010-02-15 12:53:10 +02005776 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5777 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005778 if (chan == NULL)
5779 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005780
Johannes Berge247bd902011-11-04 11:18:21 +01005781 if (!dont_wait_for_ack) {
5782 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5783 if (!msg)
5784 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005785
Johannes Berge247bd902011-11-04 11:18:21 +01005786 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5787 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005788
Johannes Berge247bd902011-11-04 11:18:21 +01005789 if (IS_ERR(hdr)) {
5790 err = PTR_ERR(hdr);
5791 goto free_msg;
5792 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005793 }
Johannes Berge247bd902011-11-04 11:18:21 +01005794
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005795 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5796 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005797 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5798 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005799 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005800 if (err)
5801 goto free_msg;
5802
Johannes Berge247bd902011-11-04 11:18:21 +01005803 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005804 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5805 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005806
Johannes Berge247bd902011-11-04 11:18:21 +01005807 genlmsg_end(msg, hdr);
5808 return genlmsg_reply(msg, info);
5809 }
5810
5811 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005812
5813 nla_put_failure:
5814 err = -ENOBUFS;
5815 free_msg:
5816 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005817 return err;
5818}
5819
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005820static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5821{
5822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5823 struct net_device *dev = info->user_ptr[1];
5824 u64 cookie;
5825
5826 if (!info->attrs[NL80211_ATTR_COOKIE])
5827 return -EINVAL;
5828
5829 if (!rdev->ops->mgmt_tx_cancel_wait)
5830 return -EOPNOTSUPP;
5831
5832 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5833 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5834 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5835 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5836 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5837 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5838 return -EOPNOTSUPP;
5839
5840 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5841
5842 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5843}
5844
Kalle Valoffb9eb32010-02-17 17:58:10 +02005845static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5846{
Johannes Berg4c476992010-10-04 21:36:35 +02005847 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005848 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005849 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005850 u8 ps_state;
5851 bool state;
5852 int err;
5853
Johannes Berg4c476992010-10-04 21:36:35 +02005854 if (!info->attrs[NL80211_ATTR_PS_STATE])
5855 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005856
5857 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5858
Johannes Berg4c476992010-10-04 21:36:35 +02005859 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5860 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005861
5862 wdev = dev->ieee80211_ptr;
5863
Johannes Berg4c476992010-10-04 21:36:35 +02005864 if (!rdev->ops->set_power_mgmt)
5865 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005866
5867 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5868
5869 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005870 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005871
Johannes Berg4c476992010-10-04 21:36:35 +02005872 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5873 wdev->ps_timeout);
5874 if (!err)
5875 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005876 return err;
5877}
5878
5879static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5880{
Johannes Berg4c476992010-10-04 21:36:35 +02005881 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005882 enum nl80211_ps_state ps_state;
5883 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005884 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005885 struct sk_buff *msg;
5886 void *hdr;
5887 int err;
5888
Kalle Valoffb9eb32010-02-17 17:58:10 +02005889 wdev = dev->ieee80211_ptr;
5890
Johannes Berg4c476992010-10-04 21:36:35 +02005891 if (!rdev->ops->set_power_mgmt)
5892 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005893
5894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005895 if (!msg)
5896 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005897
5898 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5899 NL80211_CMD_GET_POWER_SAVE);
5900 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005901 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005902 goto free_msg;
5903 }
5904
5905 if (wdev->ps)
5906 ps_state = NL80211_PS_ENABLED;
5907 else
5908 ps_state = NL80211_PS_DISABLED;
5909
David S. Miller9360ffd2012-03-29 04:41:26 -04005910 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5911 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005912
5913 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005914 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005915
Johannes Berg4c476992010-10-04 21:36:35 +02005916 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005917 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005918 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005919 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005920 return err;
5921}
5922
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005923static struct nla_policy
5924nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5925 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5926 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5927 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5928};
5929
5930static int nl80211_set_cqm_rssi(struct genl_info *info,
5931 s32 threshold, u32 hysteresis)
5932{
Johannes Berg4c476992010-10-04 21:36:35 +02005933 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005934 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005935 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005936
5937 if (threshold > 0)
5938 return -EINVAL;
5939
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005940 wdev = dev->ieee80211_ptr;
5941
Johannes Berg4c476992010-10-04 21:36:35 +02005942 if (!rdev->ops->set_cqm_rssi_config)
5943 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005944
Johannes Berg074ac8d2010-09-16 14:58:22 +02005945 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005946 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5947 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005948
Johannes Berg4c476992010-10-04 21:36:35 +02005949 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5950 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005951}
5952
5953static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5954{
5955 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5956 struct nlattr *cqm;
5957 int err;
5958
5959 cqm = info->attrs[NL80211_ATTR_CQM];
5960 if (!cqm) {
5961 err = -EINVAL;
5962 goto out;
5963 }
5964
5965 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5966 nl80211_attr_cqm_policy);
5967 if (err)
5968 goto out;
5969
5970 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5971 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5972 s32 threshold;
5973 u32 hysteresis;
5974 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5975 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5976 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5977 } else
5978 err = -EINVAL;
5979
5980out:
5981 return err;
5982}
5983
Johannes Berg29cbe682010-12-03 09:20:44 +01005984static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5985{
5986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5987 struct net_device *dev = info->user_ptr[1];
5988 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005989 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005990 int err;
5991
5992 /* start with default */
5993 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005994 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005995
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005996 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005997 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005998 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005999 if (err)
6000 return err;
6001 }
6002
6003 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6004 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6005 return -EINVAL;
6006
Javier Cardonac80d5452010-12-16 17:37:49 -08006007 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6008 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6009
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006010 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6011 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6012 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6013 return -EINVAL;
6014
Javier Cardonac80d5452010-12-16 17:37:49 -08006015 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6016 /* parse additional setup parameters if given */
6017 err = nl80211_parse_mesh_setup(info, &setup);
6018 if (err)
6019 return err;
6020 }
6021
6022 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006023}
6024
6025static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6026{
6027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6028 struct net_device *dev = info->user_ptr[1];
6029
6030 return cfg80211_leave_mesh(rdev, dev);
6031}
6032
Johannes Bergff1b6e62011-05-04 15:37:28 +02006033static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6034{
6035 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6036 struct sk_buff *msg;
6037 void *hdr;
6038
6039 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6040 return -EOPNOTSUPP;
6041
6042 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6043 if (!msg)
6044 return -ENOMEM;
6045
6046 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6047 NL80211_CMD_GET_WOWLAN);
6048 if (!hdr)
6049 goto nla_put_failure;
6050
6051 if (rdev->wowlan) {
6052 struct nlattr *nl_wowlan;
6053
6054 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6055 if (!nl_wowlan)
6056 goto nla_put_failure;
6057
David S. Miller9360ffd2012-03-29 04:41:26 -04006058 if ((rdev->wowlan->any &&
6059 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6060 (rdev->wowlan->disconnect &&
6061 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6062 (rdev->wowlan->magic_pkt &&
6063 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6064 (rdev->wowlan->gtk_rekey_failure &&
6065 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6066 (rdev->wowlan->eap_identity_req &&
6067 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6068 (rdev->wowlan->four_way_handshake &&
6069 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6070 (rdev->wowlan->rfkill_release &&
6071 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6072 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006073 if (rdev->wowlan->n_patterns) {
6074 struct nlattr *nl_pats, *nl_pat;
6075 int i, pat_len;
6076
6077 nl_pats = nla_nest_start(msg,
6078 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6079 if (!nl_pats)
6080 goto nla_put_failure;
6081
6082 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6083 nl_pat = nla_nest_start(msg, i + 1);
6084 if (!nl_pat)
6085 goto nla_put_failure;
6086 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006087 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6088 DIV_ROUND_UP(pat_len, 8),
6089 rdev->wowlan->patterns[i].mask) ||
6090 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6091 pat_len,
6092 rdev->wowlan->patterns[i].pattern))
6093 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006094 nla_nest_end(msg, nl_pat);
6095 }
6096 nla_nest_end(msg, nl_pats);
6097 }
6098
6099 nla_nest_end(msg, nl_wowlan);
6100 }
6101
6102 genlmsg_end(msg, hdr);
6103 return genlmsg_reply(msg, info);
6104
6105nla_put_failure:
6106 nlmsg_free(msg);
6107 return -ENOBUFS;
6108}
6109
6110static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6111{
6112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6113 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6114 struct cfg80211_wowlan no_triggers = {};
6115 struct cfg80211_wowlan new_triggers = {};
6116 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6117 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006118 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006119
6120 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6121 return -EOPNOTSUPP;
6122
6123 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6124 goto no_triggers;
6125
6126 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6127 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6128 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6129 nl80211_wowlan_policy);
6130 if (err)
6131 return err;
6132
6133 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6134 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6135 return -EINVAL;
6136 new_triggers.any = true;
6137 }
6138
6139 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6140 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6141 return -EINVAL;
6142 new_triggers.disconnect = true;
6143 }
6144
6145 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6146 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6147 return -EINVAL;
6148 new_triggers.magic_pkt = true;
6149 }
6150
Johannes Berg77dbbb132011-07-13 10:48:55 +02006151 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6152 return -EINVAL;
6153
6154 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6155 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6156 return -EINVAL;
6157 new_triggers.gtk_rekey_failure = true;
6158 }
6159
6160 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6161 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6162 return -EINVAL;
6163 new_triggers.eap_identity_req = true;
6164 }
6165
6166 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6167 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6168 return -EINVAL;
6169 new_triggers.four_way_handshake = true;
6170 }
6171
6172 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6173 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6174 return -EINVAL;
6175 new_triggers.rfkill_release = true;
6176 }
6177
Johannes Bergff1b6e62011-05-04 15:37:28 +02006178 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6179 struct nlattr *pat;
6180 int n_patterns = 0;
6181 int rem, pat_len, mask_len;
6182 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6183
6184 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6185 rem)
6186 n_patterns++;
6187 if (n_patterns > wowlan->n_patterns)
6188 return -EINVAL;
6189
6190 new_triggers.patterns = kcalloc(n_patterns,
6191 sizeof(new_triggers.patterns[0]),
6192 GFP_KERNEL);
6193 if (!new_triggers.patterns)
6194 return -ENOMEM;
6195
6196 new_triggers.n_patterns = n_patterns;
6197 i = 0;
6198
6199 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6200 rem) {
6201 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6202 nla_data(pat), nla_len(pat), NULL);
6203 err = -EINVAL;
6204 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6205 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6206 goto error;
6207 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6208 mask_len = DIV_ROUND_UP(pat_len, 8);
6209 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6210 mask_len)
6211 goto error;
6212 if (pat_len > wowlan->pattern_max_len ||
6213 pat_len < wowlan->pattern_min_len)
6214 goto error;
6215
6216 new_triggers.patterns[i].mask =
6217 kmalloc(mask_len + pat_len, GFP_KERNEL);
6218 if (!new_triggers.patterns[i].mask) {
6219 err = -ENOMEM;
6220 goto error;
6221 }
6222 new_triggers.patterns[i].pattern =
6223 new_triggers.patterns[i].mask + mask_len;
6224 memcpy(new_triggers.patterns[i].mask,
6225 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6226 mask_len);
6227 new_triggers.patterns[i].pattern_len = pat_len;
6228 memcpy(new_triggers.patterns[i].pattern,
6229 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6230 pat_len);
6231 i++;
6232 }
6233 }
6234
6235 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6236 struct cfg80211_wowlan *ntrig;
6237 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6238 GFP_KERNEL);
6239 if (!ntrig) {
6240 err = -ENOMEM;
6241 goto error;
6242 }
6243 cfg80211_rdev_free_wowlan(rdev);
6244 rdev->wowlan = ntrig;
6245 } else {
6246 no_triggers:
6247 cfg80211_rdev_free_wowlan(rdev);
6248 rdev->wowlan = NULL;
6249 }
6250
Johannes Berg6d525632012-04-04 15:05:25 +02006251 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6252 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6253
Johannes Bergff1b6e62011-05-04 15:37:28 +02006254 return 0;
6255 error:
6256 for (i = 0; i < new_triggers.n_patterns; i++)
6257 kfree(new_triggers.patterns[i].mask);
6258 kfree(new_triggers.patterns);
6259 return err;
6260}
6261
Johannes Berge5497d72011-07-05 16:35:40 +02006262static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6263{
6264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6265 struct net_device *dev = info->user_ptr[1];
6266 struct wireless_dev *wdev = dev->ieee80211_ptr;
6267 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6268 struct cfg80211_gtk_rekey_data rekey_data;
6269 int err;
6270
6271 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6272 return -EINVAL;
6273
6274 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6275 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6276 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6277 nl80211_rekey_policy);
6278 if (err)
6279 return err;
6280
6281 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6282 return -ERANGE;
6283 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6284 return -ERANGE;
6285 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6286 return -ERANGE;
6287
6288 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6289 NL80211_KEK_LEN);
6290 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6291 NL80211_KCK_LEN);
6292 memcpy(rekey_data.replay_ctr,
6293 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6294 NL80211_REPLAY_CTR_LEN);
6295
6296 wdev_lock(wdev);
6297 if (!wdev->current_bss) {
6298 err = -ENOTCONN;
6299 goto out;
6300 }
6301
6302 if (!rdev->ops->set_rekey_data) {
6303 err = -EOPNOTSUPP;
6304 goto out;
6305 }
6306
6307 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6308 out:
6309 wdev_unlock(wdev);
6310 return err;
6311}
6312
Johannes Berg28946da2011-11-04 11:18:12 +01006313static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6314 struct genl_info *info)
6315{
6316 struct net_device *dev = info->user_ptr[1];
6317 struct wireless_dev *wdev = dev->ieee80211_ptr;
6318
6319 if (wdev->iftype != NL80211_IFTYPE_AP &&
6320 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6321 return -EINVAL;
6322
6323 if (wdev->ap_unexpected_nlpid)
6324 return -EBUSY;
6325
6326 wdev->ap_unexpected_nlpid = info->snd_pid;
6327 return 0;
6328}
6329
Johannes Berg7f6cf312011-11-04 11:18:15 +01006330static int nl80211_probe_client(struct sk_buff *skb,
6331 struct genl_info *info)
6332{
6333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6334 struct net_device *dev = info->user_ptr[1];
6335 struct wireless_dev *wdev = dev->ieee80211_ptr;
6336 struct sk_buff *msg;
6337 void *hdr;
6338 const u8 *addr;
6339 u64 cookie;
6340 int err;
6341
6342 if (wdev->iftype != NL80211_IFTYPE_AP &&
6343 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6344 return -EOPNOTSUPP;
6345
6346 if (!info->attrs[NL80211_ATTR_MAC])
6347 return -EINVAL;
6348
6349 if (!rdev->ops->probe_client)
6350 return -EOPNOTSUPP;
6351
6352 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6353 if (!msg)
6354 return -ENOMEM;
6355
6356 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6357 NL80211_CMD_PROBE_CLIENT);
6358
6359 if (IS_ERR(hdr)) {
6360 err = PTR_ERR(hdr);
6361 goto free_msg;
6362 }
6363
6364 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6365
6366 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6367 if (err)
6368 goto free_msg;
6369
David S. Miller9360ffd2012-03-29 04:41:26 -04006370 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6371 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006372
6373 genlmsg_end(msg, hdr);
6374
6375 return genlmsg_reply(msg, info);
6376
6377 nla_put_failure:
6378 err = -ENOBUFS;
6379 free_msg:
6380 nlmsg_free(msg);
6381 return err;
6382}
6383
Johannes Berg5e760232011-11-04 11:18:17 +01006384static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6385{
6386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6387
6388 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6389 return -EOPNOTSUPP;
6390
6391 if (rdev->ap_beacons_nlpid)
6392 return -EBUSY;
6393
6394 rdev->ap_beacons_nlpid = info->snd_pid;
6395
6396 return 0;
6397}
6398
Johannes Berg4c476992010-10-04 21:36:35 +02006399#define NL80211_FLAG_NEED_WIPHY 0x01
6400#define NL80211_FLAG_NEED_NETDEV 0x02
6401#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006402#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6403#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6404 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006405
6406static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6407 struct genl_info *info)
6408{
6409 struct cfg80211_registered_device *rdev;
6410 struct net_device *dev;
6411 int err;
6412 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6413
6414 if (rtnl)
6415 rtnl_lock();
6416
6417 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6418 rdev = cfg80211_get_dev_from_info(info);
6419 if (IS_ERR(rdev)) {
6420 if (rtnl)
6421 rtnl_unlock();
6422 return PTR_ERR(rdev);
6423 }
6424 info->user_ptr[0] = rdev;
6425 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006426 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6427 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006428 if (err) {
6429 if (rtnl)
6430 rtnl_unlock();
6431 return err;
6432 }
Johannes Berg41265712010-10-04 21:14:05 +02006433 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6434 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006435 cfg80211_unlock_rdev(rdev);
6436 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006437 if (rtnl)
6438 rtnl_unlock();
6439 return -ENETDOWN;
6440 }
Johannes Berg4c476992010-10-04 21:36:35 +02006441 info->user_ptr[0] = rdev;
6442 info->user_ptr[1] = dev;
6443 }
6444
6445 return 0;
6446}
6447
6448static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6449 struct genl_info *info)
6450{
6451 if (info->user_ptr[0])
6452 cfg80211_unlock_rdev(info->user_ptr[0]);
6453 if (info->user_ptr[1])
6454 dev_put(info->user_ptr[1]);
6455 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6456 rtnl_unlock();
6457}
6458
Johannes Berg55682962007-09-20 13:09:35 -04006459static struct genl_ops nl80211_ops[] = {
6460 {
6461 .cmd = NL80211_CMD_GET_WIPHY,
6462 .doit = nl80211_get_wiphy,
6463 .dumpit = nl80211_dump_wiphy,
6464 .policy = nl80211_policy,
6465 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006466 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006467 },
6468 {
6469 .cmd = NL80211_CMD_SET_WIPHY,
6470 .doit = nl80211_set_wiphy,
6471 .policy = nl80211_policy,
6472 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006473 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006474 },
6475 {
6476 .cmd = NL80211_CMD_GET_INTERFACE,
6477 .doit = nl80211_get_interface,
6478 .dumpit = nl80211_dump_interface,
6479 .policy = nl80211_policy,
6480 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006481 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006482 },
6483 {
6484 .cmd = NL80211_CMD_SET_INTERFACE,
6485 .doit = nl80211_set_interface,
6486 .policy = nl80211_policy,
6487 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006488 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6489 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006490 },
6491 {
6492 .cmd = NL80211_CMD_NEW_INTERFACE,
6493 .doit = nl80211_new_interface,
6494 .policy = nl80211_policy,
6495 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006496 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6497 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006498 },
6499 {
6500 .cmd = NL80211_CMD_DEL_INTERFACE,
6501 .doit = nl80211_del_interface,
6502 .policy = nl80211_policy,
6503 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006504 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6505 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006506 },
Johannes Berg41ade002007-12-19 02:03:29 +01006507 {
6508 .cmd = NL80211_CMD_GET_KEY,
6509 .doit = nl80211_get_key,
6510 .policy = nl80211_policy,
6511 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006513 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006514 },
6515 {
6516 .cmd = NL80211_CMD_SET_KEY,
6517 .doit = nl80211_set_key,
6518 .policy = nl80211_policy,
6519 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006521 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006522 },
6523 {
6524 .cmd = NL80211_CMD_NEW_KEY,
6525 .doit = nl80211_new_key,
6526 .policy = nl80211_policy,
6527 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006528 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006529 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006530 },
6531 {
6532 .cmd = NL80211_CMD_DEL_KEY,
6533 .doit = nl80211_del_key,
6534 .policy = nl80211_policy,
6535 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006536 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006537 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006538 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006539 {
6540 .cmd = NL80211_CMD_SET_BEACON,
6541 .policy = nl80211_policy,
6542 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006543 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006544 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006545 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006546 },
6547 {
Johannes Berg88600202012-02-13 15:17:18 +01006548 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006549 .policy = nl80211_policy,
6550 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006551 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006552 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006553 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006554 },
6555 {
Johannes Berg88600202012-02-13 15:17:18 +01006556 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006557 .policy = nl80211_policy,
6558 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006559 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006560 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006561 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006562 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006563 {
6564 .cmd = NL80211_CMD_GET_STATION,
6565 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006566 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006567 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006568 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6569 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006570 },
6571 {
6572 .cmd = NL80211_CMD_SET_STATION,
6573 .doit = nl80211_set_station,
6574 .policy = nl80211_policy,
6575 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006576 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006577 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006578 },
6579 {
6580 .cmd = NL80211_CMD_NEW_STATION,
6581 .doit = nl80211_new_station,
6582 .policy = nl80211_policy,
6583 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006584 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006585 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006586 },
6587 {
6588 .cmd = NL80211_CMD_DEL_STATION,
6589 .doit = nl80211_del_station,
6590 .policy = nl80211_policy,
6591 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006592 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006593 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006594 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006595 {
6596 .cmd = NL80211_CMD_GET_MPATH,
6597 .doit = nl80211_get_mpath,
6598 .dumpit = nl80211_dump_mpath,
6599 .policy = nl80211_policy,
6600 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006601 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006602 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006603 },
6604 {
6605 .cmd = NL80211_CMD_SET_MPATH,
6606 .doit = nl80211_set_mpath,
6607 .policy = nl80211_policy,
6608 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006609 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006610 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006611 },
6612 {
6613 .cmd = NL80211_CMD_NEW_MPATH,
6614 .doit = nl80211_new_mpath,
6615 .policy = nl80211_policy,
6616 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006617 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006618 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006619 },
6620 {
6621 .cmd = NL80211_CMD_DEL_MPATH,
6622 .doit = nl80211_del_mpath,
6623 .policy = nl80211_policy,
6624 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006626 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006627 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006628 {
6629 .cmd = NL80211_CMD_SET_BSS,
6630 .doit = nl80211_set_bss,
6631 .policy = nl80211_policy,
6632 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006634 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006635 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006636 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006637 .cmd = NL80211_CMD_GET_REG,
6638 .doit = nl80211_get_reg,
6639 .policy = nl80211_policy,
6640 /* can be retrieved by unprivileged users */
6641 },
6642 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006643 .cmd = NL80211_CMD_SET_REG,
6644 .doit = nl80211_set_reg,
6645 .policy = nl80211_policy,
6646 .flags = GENL_ADMIN_PERM,
6647 },
6648 {
6649 .cmd = NL80211_CMD_REQ_SET_REG,
6650 .doit = nl80211_req_set_reg,
6651 .policy = nl80211_policy,
6652 .flags = GENL_ADMIN_PERM,
6653 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006654 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006655 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6656 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006657 .policy = nl80211_policy,
6658 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006659 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006660 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006661 },
6662 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006663 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6664 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006665 .policy = nl80211_policy,
6666 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006667 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006668 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006669 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006670 {
Johannes Berg2a519312009-02-10 21:25:55 +01006671 .cmd = NL80211_CMD_TRIGGER_SCAN,
6672 .doit = nl80211_trigger_scan,
6673 .policy = nl80211_policy,
6674 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006675 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006676 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006677 },
6678 {
6679 .cmd = NL80211_CMD_GET_SCAN,
6680 .policy = nl80211_policy,
6681 .dumpit = nl80211_dump_scan,
6682 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006683 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006684 .cmd = NL80211_CMD_START_SCHED_SCAN,
6685 .doit = nl80211_start_sched_scan,
6686 .policy = nl80211_policy,
6687 .flags = GENL_ADMIN_PERM,
6688 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6689 NL80211_FLAG_NEED_RTNL,
6690 },
6691 {
6692 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6693 .doit = nl80211_stop_sched_scan,
6694 .policy = nl80211_policy,
6695 .flags = GENL_ADMIN_PERM,
6696 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6697 NL80211_FLAG_NEED_RTNL,
6698 },
6699 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006700 .cmd = NL80211_CMD_AUTHENTICATE,
6701 .doit = nl80211_authenticate,
6702 .policy = nl80211_policy,
6703 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006704 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006705 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006706 },
6707 {
6708 .cmd = NL80211_CMD_ASSOCIATE,
6709 .doit = nl80211_associate,
6710 .policy = nl80211_policy,
6711 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006712 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006713 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006714 },
6715 {
6716 .cmd = NL80211_CMD_DEAUTHENTICATE,
6717 .doit = nl80211_deauthenticate,
6718 .policy = nl80211_policy,
6719 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006721 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006722 },
6723 {
6724 .cmd = NL80211_CMD_DISASSOCIATE,
6725 .doit = nl80211_disassociate,
6726 .policy = nl80211_policy,
6727 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006729 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006730 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006731 {
6732 .cmd = NL80211_CMD_JOIN_IBSS,
6733 .doit = nl80211_join_ibss,
6734 .policy = nl80211_policy,
6735 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006737 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006738 },
6739 {
6740 .cmd = NL80211_CMD_LEAVE_IBSS,
6741 .doit = nl80211_leave_ibss,
6742 .policy = nl80211_policy,
6743 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006744 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006745 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006746 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006747#ifdef CONFIG_NL80211_TESTMODE
6748 {
6749 .cmd = NL80211_CMD_TESTMODE,
6750 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006751 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006752 .policy = nl80211_policy,
6753 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006754 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6755 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006756 },
6757#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006758 {
6759 .cmd = NL80211_CMD_CONNECT,
6760 .doit = nl80211_connect,
6761 .policy = nl80211_policy,
6762 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006763 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006764 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006765 },
6766 {
6767 .cmd = NL80211_CMD_DISCONNECT,
6768 .doit = nl80211_disconnect,
6769 .policy = nl80211_policy,
6770 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006771 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006772 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006773 },
Johannes Berg463d0182009-07-14 00:33:35 +02006774 {
6775 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6776 .doit = nl80211_wiphy_netns,
6777 .policy = nl80211_policy,
6778 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006779 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6780 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006781 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006782 {
6783 .cmd = NL80211_CMD_GET_SURVEY,
6784 .policy = nl80211_policy,
6785 .dumpit = nl80211_dump_survey,
6786 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006787 {
6788 .cmd = NL80211_CMD_SET_PMKSA,
6789 .doit = nl80211_setdel_pmksa,
6790 .policy = nl80211_policy,
6791 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006792 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006793 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006794 },
6795 {
6796 .cmd = NL80211_CMD_DEL_PMKSA,
6797 .doit = nl80211_setdel_pmksa,
6798 .policy = nl80211_policy,
6799 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006800 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006801 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006802 },
6803 {
6804 .cmd = NL80211_CMD_FLUSH_PMKSA,
6805 .doit = nl80211_flush_pmksa,
6806 .policy = nl80211_policy,
6807 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006808 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006809 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006810 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006811 {
6812 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6813 .doit = nl80211_remain_on_channel,
6814 .policy = nl80211_policy,
6815 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006816 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006817 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006818 },
6819 {
6820 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6821 .doit = nl80211_cancel_remain_on_channel,
6822 .policy = nl80211_policy,
6823 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006824 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006825 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006826 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006827 {
6828 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6829 .doit = nl80211_set_tx_bitrate_mask,
6830 .policy = nl80211_policy,
6831 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006832 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6833 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006834 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006835 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006836 .cmd = NL80211_CMD_REGISTER_FRAME,
6837 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006838 .policy = nl80211_policy,
6839 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006840 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6841 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006842 },
6843 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006844 .cmd = NL80211_CMD_FRAME,
6845 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006846 .policy = nl80211_policy,
6847 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006848 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006849 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006850 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006851 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006852 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6853 .doit = nl80211_tx_mgmt_cancel_wait,
6854 .policy = nl80211_policy,
6855 .flags = GENL_ADMIN_PERM,
6856 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6857 NL80211_FLAG_NEED_RTNL,
6858 },
6859 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006860 .cmd = NL80211_CMD_SET_POWER_SAVE,
6861 .doit = nl80211_set_power_save,
6862 .policy = nl80211_policy,
6863 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006864 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6865 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006866 },
6867 {
6868 .cmd = NL80211_CMD_GET_POWER_SAVE,
6869 .doit = nl80211_get_power_save,
6870 .policy = nl80211_policy,
6871 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006872 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6873 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006874 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006875 {
6876 .cmd = NL80211_CMD_SET_CQM,
6877 .doit = nl80211_set_cqm,
6878 .policy = nl80211_policy,
6879 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006880 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6881 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006882 },
Johannes Bergf444de02010-05-05 15:25:02 +02006883 {
6884 .cmd = NL80211_CMD_SET_CHANNEL,
6885 .doit = nl80211_set_channel,
6886 .policy = nl80211_policy,
6887 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006888 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6889 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006890 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006891 {
6892 .cmd = NL80211_CMD_SET_WDS_PEER,
6893 .doit = nl80211_set_wds_peer,
6894 .policy = nl80211_policy,
6895 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006896 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6897 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006898 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006899 {
6900 .cmd = NL80211_CMD_JOIN_MESH,
6901 .doit = nl80211_join_mesh,
6902 .policy = nl80211_policy,
6903 .flags = GENL_ADMIN_PERM,
6904 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6905 NL80211_FLAG_NEED_RTNL,
6906 },
6907 {
6908 .cmd = NL80211_CMD_LEAVE_MESH,
6909 .doit = nl80211_leave_mesh,
6910 .policy = nl80211_policy,
6911 .flags = GENL_ADMIN_PERM,
6912 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6913 NL80211_FLAG_NEED_RTNL,
6914 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006915 {
6916 .cmd = NL80211_CMD_GET_WOWLAN,
6917 .doit = nl80211_get_wowlan,
6918 .policy = nl80211_policy,
6919 /* can be retrieved by unprivileged users */
6920 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6921 NL80211_FLAG_NEED_RTNL,
6922 },
6923 {
6924 .cmd = NL80211_CMD_SET_WOWLAN,
6925 .doit = nl80211_set_wowlan,
6926 .policy = nl80211_policy,
6927 .flags = GENL_ADMIN_PERM,
6928 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6929 NL80211_FLAG_NEED_RTNL,
6930 },
Johannes Berge5497d72011-07-05 16:35:40 +02006931 {
6932 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6933 .doit = nl80211_set_rekey_data,
6934 .policy = nl80211_policy,
6935 .flags = GENL_ADMIN_PERM,
6936 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6937 NL80211_FLAG_NEED_RTNL,
6938 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006939 {
6940 .cmd = NL80211_CMD_TDLS_MGMT,
6941 .doit = nl80211_tdls_mgmt,
6942 .policy = nl80211_policy,
6943 .flags = GENL_ADMIN_PERM,
6944 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6945 NL80211_FLAG_NEED_RTNL,
6946 },
6947 {
6948 .cmd = NL80211_CMD_TDLS_OPER,
6949 .doit = nl80211_tdls_oper,
6950 .policy = nl80211_policy,
6951 .flags = GENL_ADMIN_PERM,
6952 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6953 NL80211_FLAG_NEED_RTNL,
6954 },
Johannes Berg28946da2011-11-04 11:18:12 +01006955 {
6956 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6957 .doit = nl80211_register_unexpected_frame,
6958 .policy = nl80211_policy,
6959 .flags = GENL_ADMIN_PERM,
6960 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6961 NL80211_FLAG_NEED_RTNL,
6962 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006963 {
6964 .cmd = NL80211_CMD_PROBE_CLIENT,
6965 .doit = nl80211_probe_client,
6966 .policy = nl80211_policy,
6967 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006968 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01006969 NL80211_FLAG_NEED_RTNL,
6970 },
Johannes Berg5e760232011-11-04 11:18:17 +01006971 {
6972 .cmd = NL80211_CMD_REGISTER_BEACONS,
6973 .doit = nl80211_register_beacons,
6974 .policy = nl80211_policy,
6975 .flags = GENL_ADMIN_PERM,
6976 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6977 NL80211_FLAG_NEED_RTNL,
6978 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006979 {
6980 .cmd = NL80211_CMD_SET_NOACK_MAP,
6981 .doit = nl80211_set_noack_map,
6982 .policy = nl80211_policy,
6983 .flags = GENL_ADMIN_PERM,
6984 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6985 NL80211_FLAG_NEED_RTNL,
6986 },
6987
Johannes Berg55682962007-09-20 13:09:35 -04006988};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006989
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006990static struct genl_multicast_group nl80211_mlme_mcgrp = {
6991 .name = "mlme",
6992};
Johannes Berg55682962007-09-20 13:09:35 -04006993
6994/* multicast groups */
6995static struct genl_multicast_group nl80211_config_mcgrp = {
6996 .name = "config",
6997};
Johannes Berg2a519312009-02-10 21:25:55 +01006998static struct genl_multicast_group nl80211_scan_mcgrp = {
6999 .name = "scan",
7000};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007001static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7002 .name = "regulatory",
7003};
Johannes Berg55682962007-09-20 13:09:35 -04007004
7005/* notification functions */
7006
7007void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7008{
7009 struct sk_buff *msg;
7010
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007012 if (!msg)
7013 return;
7014
7015 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7016 nlmsg_free(msg);
7017 return;
7018 }
7019
Johannes Berg463d0182009-07-14 00:33:35 +02007020 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7021 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007022}
7023
Johannes Berg362a4152009-05-24 16:43:15 +02007024static int nl80211_add_scan_req(struct sk_buff *msg,
7025 struct cfg80211_registered_device *rdev)
7026{
7027 struct cfg80211_scan_request *req = rdev->scan_req;
7028 struct nlattr *nest;
7029 int i;
7030
Johannes Berg667503d2009-07-07 03:56:11 +02007031 ASSERT_RDEV_LOCK(rdev);
7032
Johannes Berg362a4152009-05-24 16:43:15 +02007033 if (WARN_ON(!req))
7034 return 0;
7035
7036 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7037 if (!nest)
7038 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007039 for (i = 0; i < req->n_ssids; i++) {
7040 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7041 goto nla_put_failure;
7042 }
Johannes Berg362a4152009-05-24 16:43:15 +02007043 nla_nest_end(msg, nest);
7044
7045 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7046 if (!nest)
7047 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007048 for (i = 0; i < req->n_channels; i++) {
7049 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7050 goto nla_put_failure;
7051 }
Johannes Berg362a4152009-05-24 16:43:15 +02007052 nla_nest_end(msg, nest);
7053
David S. Miller9360ffd2012-03-29 04:41:26 -04007054 if (req->ie &&
7055 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7056 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007057
7058 return 0;
7059 nla_put_failure:
7060 return -ENOBUFS;
7061}
7062
Johannes Berga538e2d2009-06-16 19:56:42 +02007063static int nl80211_send_scan_msg(struct sk_buff *msg,
7064 struct cfg80211_registered_device *rdev,
7065 struct net_device *netdev,
7066 u32 pid, u32 seq, int flags,
7067 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007068{
7069 void *hdr;
7070
7071 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7072 if (!hdr)
7073 return -1;
7074
David S. Miller9360ffd2012-03-29 04:41:26 -04007075 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7076 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7077 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007078
Johannes Berg362a4152009-05-24 16:43:15 +02007079 /* ignore errors and send incomplete event anyway */
7080 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007081
7082 return genlmsg_end(msg, hdr);
7083
7084 nla_put_failure:
7085 genlmsg_cancel(msg, hdr);
7086 return -EMSGSIZE;
7087}
7088
Luciano Coelho807f8a82011-05-11 17:09:35 +03007089static int
7090nl80211_send_sched_scan_msg(struct sk_buff *msg,
7091 struct cfg80211_registered_device *rdev,
7092 struct net_device *netdev,
7093 u32 pid, u32 seq, int flags, u32 cmd)
7094{
7095 void *hdr;
7096
7097 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7098 if (!hdr)
7099 return -1;
7100
David S. Miller9360ffd2012-03-29 04:41:26 -04007101 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7102 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7103 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007104
7105 return genlmsg_end(msg, hdr);
7106
7107 nla_put_failure:
7108 genlmsg_cancel(msg, hdr);
7109 return -EMSGSIZE;
7110}
7111
Johannes Berga538e2d2009-06-16 19:56:42 +02007112void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7113 struct net_device *netdev)
7114{
7115 struct sk_buff *msg;
7116
7117 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7118 if (!msg)
7119 return;
7120
7121 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7122 NL80211_CMD_TRIGGER_SCAN) < 0) {
7123 nlmsg_free(msg);
7124 return;
7125 }
7126
Johannes Berg463d0182009-07-14 00:33:35 +02007127 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7128 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007129}
7130
Johannes Berg2a519312009-02-10 21:25:55 +01007131void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7132 struct net_device *netdev)
7133{
7134 struct sk_buff *msg;
7135
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007136 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007137 if (!msg)
7138 return;
7139
Johannes Berga538e2d2009-06-16 19:56:42 +02007140 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7141 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007142 nlmsg_free(msg);
7143 return;
7144 }
7145
Johannes Berg463d0182009-07-14 00:33:35 +02007146 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7147 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007148}
7149
7150void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7151 struct net_device *netdev)
7152{
7153 struct sk_buff *msg;
7154
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007156 if (!msg)
7157 return;
7158
Johannes Berga538e2d2009-06-16 19:56:42 +02007159 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7160 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007161 nlmsg_free(msg);
7162 return;
7163 }
7164
Johannes Berg463d0182009-07-14 00:33:35 +02007165 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7166 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007167}
7168
Luciano Coelho807f8a82011-05-11 17:09:35 +03007169void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7170 struct net_device *netdev)
7171{
7172 struct sk_buff *msg;
7173
7174 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7175 if (!msg)
7176 return;
7177
7178 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7179 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7180 nlmsg_free(msg);
7181 return;
7182 }
7183
7184 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7185 nl80211_scan_mcgrp.id, GFP_KERNEL);
7186}
7187
7188void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7189 struct net_device *netdev, u32 cmd)
7190{
7191 struct sk_buff *msg;
7192
7193 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7194 if (!msg)
7195 return;
7196
7197 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7198 nlmsg_free(msg);
7199 return;
7200 }
7201
7202 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7203 nl80211_scan_mcgrp.id, GFP_KERNEL);
7204}
7205
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007206/*
7207 * This can happen on global regulatory changes or device specific settings
7208 * based on custom world regulatory domains.
7209 */
7210void nl80211_send_reg_change_event(struct regulatory_request *request)
7211{
7212 struct sk_buff *msg;
7213 void *hdr;
7214
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007215 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007216 if (!msg)
7217 return;
7218
7219 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7220 if (!hdr) {
7221 nlmsg_free(msg);
7222 return;
7223 }
7224
7225 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007226 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7227 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007228
David S. Miller9360ffd2012-03-29 04:41:26 -04007229 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7230 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7231 NL80211_REGDOM_TYPE_WORLD))
7232 goto nla_put_failure;
7233 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7234 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7235 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7236 goto nla_put_failure;
7237 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7238 request->intersect) {
7239 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7240 NL80211_REGDOM_TYPE_INTERSECTION))
7241 goto nla_put_failure;
7242 } else {
7243 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7244 NL80211_REGDOM_TYPE_COUNTRY) ||
7245 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7246 request->alpha2))
7247 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007248 }
7249
David S. Miller9360ffd2012-03-29 04:41:26 -04007250 if (wiphy_idx_valid(request->wiphy_idx) &&
7251 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7252 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007253
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007254 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007255
Johannes Bergbc43b282009-07-25 10:54:13 +02007256 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007257 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007258 GFP_ATOMIC);
7259 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007260
7261 return;
7262
7263nla_put_failure:
7264 genlmsg_cancel(msg, hdr);
7265 nlmsg_free(msg);
7266}
7267
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007268static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7269 struct net_device *netdev,
7270 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007271 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007272{
7273 struct sk_buff *msg;
7274 void *hdr;
7275
Johannes Berge6d6e342009-07-01 21:26:47 +02007276 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007277 if (!msg)
7278 return;
7279
7280 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7281 if (!hdr) {
7282 nlmsg_free(msg);
7283 return;
7284 }
7285
David S. Miller9360ffd2012-03-29 04:41:26 -04007286 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7287 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7288 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7289 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007290
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007291 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007292
Johannes Berg463d0182009-07-14 00:33:35 +02007293 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7294 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007295 return;
7296
7297 nla_put_failure:
7298 genlmsg_cancel(msg, hdr);
7299 nlmsg_free(msg);
7300}
7301
7302void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007303 struct net_device *netdev, const u8 *buf,
7304 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007305{
7306 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007307 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007308}
7309
7310void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7311 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007312 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007313{
Johannes Berge6d6e342009-07-01 21:26:47 +02007314 nl80211_send_mlme_event(rdev, netdev, buf, len,
7315 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007316}
7317
Jouni Malinen53b46b82009-03-27 20:53:56 +02007318void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007319 struct net_device *netdev, const u8 *buf,
7320 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007321{
7322 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007323 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007324}
7325
Jouni Malinen53b46b82009-03-27 20:53:56 +02007326void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7327 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007328 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007329{
7330 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007331 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007332}
7333
Jouni Malinencf4e5942010-12-16 00:52:40 +02007334void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7335 struct net_device *netdev, const u8 *buf,
7336 size_t len, gfp_t gfp)
7337{
7338 nl80211_send_mlme_event(rdev, netdev, buf, len,
7339 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7340}
7341
7342void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7343 struct net_device *netdev, const u8 *buf,
7344 size_t len, gfp_t gfp)
7345{
7346 nl80211_send_mlme_event(rdev, netdev, buf, len,
7347 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7348}
7349
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007350static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7351 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007352 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007353{
7354 struct sk_buff *msg;
7355 void *hdr;
7356
Johannes Berge6d6e342009-07-01 21:26:47 +02007357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007358 if (!msg)
7359 return;
7360
7361 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7362 if (!hdr) {
7363 nlmsg_free(msg);
7364 return;
7365 }
7366
David S. Miller9360ffd2012-03-29 04:41:26 -04007367 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7368 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7369 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7370 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7371 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007372
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007373 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007374
Johannes Berg463d0182009-07-14 00:33:35 +02007375 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7376 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007377 return;
7378
7379 nla_put_failure:
7380 genlmsg_cancel(msg, hdr);
7381 nlmsg_free(msg);
7382}
7383
7384void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007385 struct net_device *netdev, const u8 *addr,
7386 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007387{
7388 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007389 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007390}
7391
7392void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007393 struct net_device *netdev, const u8 *addr,
7394 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007395{
Johannes Berge6d6e342009-07-01 21:26:47 +02007396 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7397 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007398}
7399
Samuel Ortizb23aa672009-07-01 21:26:54 +02007400void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7401 struct net_device *netdev, const u8 *bssid,
7402 const u8 *req_ie, size_t req_ie_len,
7403 const u8 *resp_ie, size_t resp_ie_len,
7404 u16 status, gfp_t gfp)
7405{
7406 struct sk_buff *msg;
7407 void *hdr;
7408
7409 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7410 if (!msg)
7411 return;
7412
7413 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7414 if (!hdr) {
7415 nlmsg_free(msg);
7416 return;
7417 }
7418
David S. Miller9360ffd2012-03-29 04:41:26 -04007419 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7420 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7421 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7422 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7423 (req_ie &&
7424 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7425 (resp_ie &&
7426 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7427 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007428
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007429 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007430
Johannes Berg463d0182009-07-14 00:33:35 +02007431 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7432 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007433 return;
7434
7435 nla_put_failure:
7436 genlmsg_cancel(msg, hdr);
7437 nlmsg_free(msg);
7438
7439}
7440
7441void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7442 struct net_device *netdev, const u8 *bssid,
7443 const u8 *req_ie, size_t req_ie_len,
7444 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7445{
7446 struct sk_buff *msg;
7447 void *hdr;
7448
7449 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7450 if (!msg)
7451 return;
7452
7453 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7454 if (!hdr) {
7455 nlmsg_free(msg);
7456 return;
7457 }
7458
David S. Miller9360ffd2012-03-29 04:41:26 -04007459 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7460 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7461 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7462 (req_ie &&
7463 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7464 (resp_ie &&
7465 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7466 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007467
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007468 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007469
Johannes Berg463d0182009-07-14 00:33:35 +02007470 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7471 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007472 return;
7473
7474 nla_put_failure:
7475 genlmsg_cancel(msg, hdr);
7476 nlmsg_free(msg);
7477
7478}
7479
7480void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7481 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007482 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007483{
7484 struct sk_buff *msg;
7485 void *hdr;
7486
Johannes Berg667503d2009-07-07 03:56:11 +02007487 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007488 if (!msg)
7489 return;
7490
7491 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7492 if (!hdr) {
7493 nlmsg_free(msg);
7494 return;
7495 }
7496
David S. Miller9360ffd2012-03-29 04:41:26 -04007497 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7498 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7499 (from_ap && reason &&
7500 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7501 (from_ap &&
7502 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7503 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7504 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007505
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007506 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007507
Johannes Berg463d0182009-07-14 00:33:35 +02007508 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7509 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007510 return;
7511
7512 nla_put_failure:
7513 genlmsg_cancel(msg, hdr);
7514 nlmsg_free(msg);
7515
7516}
7517
Johannes Berg04a773a2009-04-19 21:24:32 +02007518void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7519 struct net_device *netdev, const u8 *bssid,
7520 gfp_t gfp)
7521{
7522 struct sk_buff *msg;
7523 void *hdr;
7524
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007525 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007526 if (!msg)
7527 return;
7528
7529 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7530 if (!hdr) {
7531 nlmsg_free(msg);
7532 return;
7533 }
7534
David S. Miller9360ffd2012-03-29 04:41:26 -04007535 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7536 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7537 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7538 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007539
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007540 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007541
Johannes Berg463d0182009-07-14 00:33:35 +02007542 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7543 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007544 return;
7545
7546 nla_put_failure:
7547 genlmsg_cancel(msg, hdr);
7548 nlmsg_free(msg);
7549}
7550
Javier Cardonac93b5e72011-04-07 15:08:34 -07007551void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7552 struct net_device *netdev,
7553 const u8 *macaddr, const u8* ie, u8 ie_len,
7554 gfp_t gfp)
7555{
7556 struct sk_buff *msg;
7557 void *hdr;
7558
7559 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7560 if (!msg)
7561 return;
7562
7563 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7564 if (!hdr) {
7565 nlmsg_free(msg);
7566 return;
7567 }
7568
David S. Miller9360ffd2012-03-29 04:41:26 -04007569 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7570 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7571 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7572 (ie_len && ie &&
7573 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7574 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007575
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007576 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007577
7578 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7579 nl80211_mlme_mcgrp.id, gfp);
7580 return;
7581
7582 nla_put_failure:
7583 genlmsg_cancel(msg, hdr);
7584 nlmsg_free(msg);
7585}
7586
Jouni Malinena3b8b052009-03-27 21:59:49 +02007587void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7588 struct net_device *netdev, const u8 *addr,
7589 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007590 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007591{
7592 struct sk_buff *msg;
7593 void *hdr;
7594
Johannes Berge6d6e342009-07-01 21:26:47 +02007595 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007596 if (!msg)
7597 return;
7598
7599 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7600 if (!hdr) {
7601 nlmsg_free(msg);
7602 return;
7603 }
7604
David S. Miller9360ffd2012-03-29 04:41:26 -04007605 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7606 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7607 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7608 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7609 (key_id != -1 &&
7610 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7611 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7612 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007613
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007614 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007615
Johannes Berg463d0182009-07-14 00:33:35 +02007616 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7617 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007618 return;
7619
7620 nla_put_failure:
7621 genlmsg_cancel(msg, hdr);
7622 nlmsg_free(msg);
7623}
7624
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007625void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7626 struct ieee80211_channel *channel_before,
7627 struct ieee80211_channel *channel_after)
7628{
7629 struct sk_buff *msg;
7630 void *hdr;
7631 struct nlattr *nl_freq;
7632
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007634 if (!msg)
7635 return;
7636
7637 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7638 if (!hdr) {
7639 nlmsg_free(msg);
7640 return;
7641 }
7642
7643 /*
7644 * Since we are applying the beacon hint to a wiphy we know its
7645 * wiphy_idx is valid
7646 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007647 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7648 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007649
7650 /* Before */
7651 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7652 if (!nl_freq)
7653 goto nla_put_failure;
7654 if (nl80211_msg_put_channel(msg, channel_before))
7655 goto nla_put_failure;
7656 nla_nest_end(msg, nl_freq);
7657
7658 /* After */
7659 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7660 if (!nl_freq)
7661 goto nla_put_failure;
7662 if (nl80211_msg_put_channel(msg, channel_after))
7663 goto nla_put_failure;
7664 nla_nest_end(msg, nl_freq);
7665
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007666 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007667
Johannes Berg463d0182009-07-14 00:33:35 +02007668 rcu_read_lock();
7669 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7670 GFP_ATOMIC);
7671 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007672
7673 return;
7674
7675nla_put_failure:
7676 genlmsg_cancel(msg, hdr);
7677 nlmsg_free(msg);
7678}
7679
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007680static void nl80211_send_remain_on_chan_event(
7681 int cmd, struct cfg80211_registered_device *rdev,
7682 struct net_device *netdev, u64 cookie,
7683 struct ieee80211_channel *chan,
7684 enum nl80211_channel_type channel_type,
7685 unsigned int duration, gfp_t gfp)
7686{
7687 struct sk_buff *msg;
7688 void *hdr;
7689
7690 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7691 if (!msg)
7692 return;
7693
7694 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7695 if (!hdr) {
7696 nlmsg_free(msg);
7697 return;
7698 }
7699
David S. Miller9360ffd2012-03-29 04:41:26 -04007700 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7701 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7702 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7703 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7704 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7705 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007706
David S. Miller9360ffd2012-03-29 04:41:26 -04007707 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7708 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7709 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007710
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007711 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007712
7713 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7714 nl80211_mlme_mcgrp.id, gfp);
7715 return;
7716
7717 nla_put_failure:
7718 genlmsg_cancel(msg, hdr);
7719 nlmsg_free(msg);
7720}
7721
7722void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7723 struct net_device *netdev, u64 cookie,
7724 struct ieee80211_channel *chan,
7725 enum nl80211_channel_type channel_type,
7726 unsigned int duration, gfp_t gfp)
7727{
7728 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7729 rdev, netdev, cookie, chan,
7730 channel_type, duration, gfp);
7731}
7732
7733void nl80211_send_remain_on_channel_cancel(
7734 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7735 u64 cookie, struct ieee80211_channel *chan,
7736 enum nl80211_channel_type channel_type, gfp_t gfp)
7737{
7738 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7739 rdev, netdev, cookie, chan,
7740 channel_type, 0, gfp);
7741}
7742
Johannes Berg98b62182009-12-23 13:15:44 +01007743void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7744 struct net_device *dev, const u8 *mac_addr,
7745 struct station_info *sinfo, gfp_t gfp)
7746{
7747 struct sk_buff *msg;
7748
7749 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7750 if (!msg)
7751 return;
7752
John W. Linville66266b32012-03-15 13:25:41 -04007753 if (nl80211_send_station(msg, 0, 0, 0,
7754 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007755 nlmsg_free(msg);
7756 return;
7757 }
7758
7759 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7760 nl80211_mlme_mcgrp.id, gfp);
7761}
7762
Jouni Malinenec15e682011-03-23 15:29:52 +02007763void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7764 struct net_device *dev, const u8 *mac_addr,
7765 gfp_t gfp)
7766{
7767 struct sk_buff *msg;
7768 void *hdr;
7769
7770 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7771 if (!msg)
7772 return;
7773
7774 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7775 if (!hdr) {
7776 nlmsg_free(msg);
7777 return;
7778 }
7779
David S. Miller9360ffd2012-03-29 04:41:26 -04007780 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7781 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7782 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007783
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007784 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007785
7786 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7787 nl80211_mlme_mcgrp.id, gfp);
7788 return;
7789
7790 nla_put_failure:
7791 genlmsg_cancel(msg, hdr);
7792 nlmsg_free(msg);
7793}
7794
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007795static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7796 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007797{
7798 struct wireless_dev *wdev = dev->ieee80211_ptr;
7799 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7800 struct sk_buff *msg;
7801 void *hdr;
7802 int err;
7803 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7804
7805 if (!nlpid)
7806 return false;
7807
7808 msg = nlmsg_new(100, gfp);
7809 if (!msg)
7810 return true;
7811
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007812 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007813 if (!hdr) {
7814 nlmsg_free(msg);
7815 return true;
7816 }
7817
David S. Miller9360ffd2012-03-29 04:41:26 -04007818 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7819 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7820 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7821 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007822
7823 err = genlmsg_end(msg, hdr);
7824 if (err < 0) {
7825 nlmsg_free(msg);
7826 return true;
7827 }
7828
7829 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7830 return true;
7831
7832 nla_put_failure:
7833 genlmsg_cancel(msg, hdr);
7834 nlmsg_free(msg);
7835 return true;
7836}
7837
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007838bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7839{
7840 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7841 addr, gfp);
7842}
7843
7844bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7845 const u8 *addr, gfp_t gfp)
7846{
7847 return __nl80211_unexpected_frame(dev,
7848 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7849 addr, gfp);
7850}
7851
Johannes Berg2e161f72010-08-12 15:38:38 +02007852int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7853 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007854 int freq, int sig_dbm,
7855 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007856{
7857 struct sk_buff *msg;
7858 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007859
7860 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7861 if (!msg)
7862 return -ENOMEM;
7863
Johannes Berg2e161f72010-08-12 15:38:38 +02007864 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007865 if (!hdr) {
7866 nlmsg_free(msg);
7867 return -ENOMEM;
7868 }
7869
David S. Miller9360ffd2012-03-29 04:41:26 -04007870 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7871 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7872 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7873 (sig_dbm &&
7874 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7875 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7876 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007877
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007878 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007879
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007880 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007881
7882 nla_put_failure:
7883 genlmsg_cancel(msg, hdr);
7884 nlmsg_free(msg);
7885 return -ENOBUFS;
7886}
7887
Johannes Berg2e161f72010-08-12 15:38:38 +02007888void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7889 struct net_device *netdev, u64 cookie,
7890 const u8 *buf, size_t len, bool ack,
7891 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007892{
7893 struct sk_buff *msg;
7894 void *hdr;
7895
7896 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7897 if (!msg)
7898 return;
7899
Johannes Berg2e161f72010-08-12 15:38:38 +02007900 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007901 if (!hdr) {
7902 nlmsg_free(msg);
7903 return;
7904 }
7905
David S. Miller9360ffd2012-03-29 04:41:26 -04007906 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7907 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7908 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7909 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7910 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7911 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007912
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007913 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007914
7915 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7916 return;
7917
7918 nla_put_failure:
7919 genlmsg_cancel(msg, hdr);
7920 nlmsg_free(msg);
7921}
7922
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007923void
7924nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7925 struct net_device *netdev,
7926 enum nl80211_cqm_rssi_threshold_event rssi_event,
7927 gfp_t gfp)
7928{
7929 struct sk_buff *msg;
7930 struct nlattr *pinfoattr;
7931 void *hdr;
7932
7933 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7934 if (!msg)
7935 return;
7936
7937 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7938 if (!hdr) {
7939 nlmsg_free(msg);
7940 return;
7941 }
7942
David S. Miller9360ffd2012-03-29 04:41:26 -04007943 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7944 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7945 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007946
7947 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7948 if (!pinfoattr)
7949 goto nla_put_failure;
7950
David S. Miller9360ffd2012-03-29 04:41:26 -04007951 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7952 rssi_event))
7953 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007954
7955 nla_nest_end(msg, pinfoattr);
7956
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007957 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007958
7959 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7960 nl80211_mlme_mcgrp.id, gfp);
7961 return;
7962
7963 nla_put_failure:
7964 genlmsg_cancel(msg, hdr);
7965 nlmsg_free(msg);
7966}
7967
Johannes Berge5497d72011-07-05 16:35:40 +02007968void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7969 struct net_device *netdev, const u8 *bssid,
7970 const u8 *replay_ctr, gfp_t gfp)
7971{
7972 struct sk_buff *msg;
7973 struct nlattr *rekey_attr;
7974 void *hdr;
7975
7976 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7977 if (!msg)
7978 return;
7979
7980 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7981 if (!hdr) {
7982 nlmsg_free(msg);
7983 return;
7984 }
7985
David S. Miller9360ffd2012-03-29 04:41:26 -04007986 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7987 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7988 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7989 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007990
7991 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7992 if (!rekey_attr)
7993 goto nla_put_failure;
7994
David S. Miller9360ffd2012-03-29 04:41:26 -04007995 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7996 NL80211_REPLAY_CTR_LEN, replay_ctr))
7997 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007998
7999 nla_nest_end(msg, rekey_attr);
8000
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008001 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008002
8003 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8004 nl80211_mlme_mcgrp.id, gfp);
8005 return;
8006
8007 nla_put_failure:
8008 genlmsg_cancel(msg, hdr);
8009 nlmsg_free(msg);
8010}
8011
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008012void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8013 struct net_device *netdev, int index,
8014 const u8 *bssid, bool preauth, gfp_t gfp)
8015{
8016 struct sk_buff *msg;
8017 struct nlattr *attr;
8018 void *hdr;
8019
8020 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8021 if (!msg)
8022 return;
8023
8024 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8025 if (!hdr) {
8026 nlmsg_free(msg);
8027 return;
8028 }
8029
David S. Miller9360ffd2012-03-29 04:41:26 -04008030 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8031 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8032 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008033
8034 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8035 if (!attr)
8036 goto nla_put_failure;
8037
David S. Miller9360ffd2012-03-29 04:41:26 -04008038 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8039 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8040 (preauth &&
8041 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8042 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008043
8044 nla_nest_end(msg, attr);
8045
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008046 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008047
8048 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8049 nl80211_mlme_mcgrp.id, gfp);
8050 return;
8051
8052 nla_put_failure:
8053 genlmsg_cancel(msg, hdr);
8054 nlmsg_free(msg);
8055}
8056
Thomas Pedersen53145262012-04-06 13:35:47 -07008057void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8058 struct net_device *netdev, int freq,
8059 enum nl80211_channel_type type, gfp_t gfp)
8060{
8061 struct sk_buff *msg;
8062 void *hdr;
8063
8064 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8065 if (!msg)
8066 return;
8067
8068 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8069 if (!hdr) {
8070 nlmsg_free(msg);
8071 return;
8072 }
8073
John W. Linville7eab0f62012-04-12 14:25:14 -04008074 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8075 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8076 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8077 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008078
8079 genlmsg_end(msg, hdr);
8080
8081 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8082 nl80211_mlme_mcgrp.id, gfp);
8083 return;
8084
8085 nla_put_failure:
8086 genlmsg_cancel(msg, hdr);
8087 nlmsg_free(msg);
8088}
8089
Johannes Bergc063dbf2010-11-24 08:10:05 +01008090void
8091nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8092 struct net_device *netdev, const u8 *peer,
8093 u32 num_packets, gfp_t gfp)
8094{
8095 struct sk_buff *msg;
8096 struct nlattr *pinfoattr;
8097 void *hdr;
8098
8099 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8100 if (!msg)
8101 return;
8102
8103 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8104 if (!hdr) {
8105 nlmsg_free(msg);
8106 return;
8107 }
8108
David S. Miller9360ffd2012-03-29 04:41:26 -04008109 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8110 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8111 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8112 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008113
8114 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8115 if (!pinfoattr)
8116 goto nla_put_failure;
8117
David S. Miller9360ffd2012-03-29 04:41:26 -04008118 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8119 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008120
8121 nla_nest_end(msg, pinfoattr);
8122
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008123 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008124
8125 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8126 nl80211_mlme_mcgrp.id, gfp);
8127 return;
8128
8129 nla_put_failure:
8130 genlmsg_cancel(msg, hdr);
8131 nlmsg_free(msg);
8132}
8133
Johannes Berg7f6cf312011-11-04 11:18:15 +01008134void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8135 u64 cookie, bool acked, gfp_t gfp)
8136{
8137 struct wireless_dev *wdev = dev->ieee80211_ptr;
8138 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8139 struct sk_buff *msg;
8140 void *hdr;
8141 int err;
8142
8143 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8144 if (!msg)
8145 return;
8146
8147 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8148 if (!hdr) {
8149 nlmsg_free(msg);
8150 return;
8151 }
8152
David S. Miller9360ffd2012-03-29 04:41:26 -04008153 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8154 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8155 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8156 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8157 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8158 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008159
8160 err = genlmsg_end(msg, hdr);
8161 if (err < 0) {
8162 nlmsg_free(msg);
8163 return;
8164 }
8165
8166 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8167 nl80211_mlme_mcgrp.id, gfp);
8168 return;
8169
8170 nla_put_failure:
8171 genlmsg_cancel(msg, hdr);
8172 nlmsg_free(msg);
8173}
8174EXPORT_SYMBOL(cfg80211_probe_status);
8175
Johannes Berg5e760232011-11-04 11:18:17 +01008176void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8177 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008178 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008179{
8180 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8181 struct sk_buff *msg;
8182 void *hdr;
8183 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8184
8185 if (!nlpid)
8186 return;
8187
8188 msg = nlmsg_new(len + 100, gfp);
8189 if (!msg)
8190 return;
8191
8192 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8193 if (!hdr) {
8194 nlmsg_free(msg);
8195 return;
8196 }
8197
David S. Miller9360ffd2012-03-29 04:41:26 -04008198 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8199 (freq &&
8200 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8201 (sig_dbm &&
8202 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8203 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8204 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008205
8206 genlmsg_end(msg, hdr);
8207
8208 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8209 return;
8210
8211 nla_put_failure:
8212 genlmsg_cancel(msg, hdr);
8213 nlmsg_free(msg);
8214}
8215EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8216
Jouni Malinen026331c2010-02-15 12:53:10 +02008217static int nl80211_netlink_notify(struct notifier_block * nb,
8218 unsigned long state,
8219 void *_notify)
8220{
8221 struct netlink_notify *notify = _notify;
8222 struct cfg80211_registered_device *rdev;
8223 struct wireless_dev *wdev;
8224
8225 if (state != NETLINK_URELEASE)
8226 return NOTIFY_DONE;
8227
8228 rcu_read_lock();
8229
Johannes Berg5e760232011-11-04 11:18:17 +01008230 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008231 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008232 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008233 if (rdev->ap_beacons_nlpid == notify->pid)
8234 rdev->ap_beacons_nlpid = 0;
8235 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008236
8237 rcu_read_unlock();
8238
8239 return NOTIFY_DONE;
8240}
8241
8242static struct notifier_block nl80211_netlink_notifier = {
8243 .notifier_call = nl80211_netlink_notify,
8244};
8245
Johannes Berg55682962007-09-20 13:09:35 -04008246/* initialisation/exit functions */
8247
8248int nl80211_init(void)
8249{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008250 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008251
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008252 err = genl_register_family_with_ops(&nl80211_fam,
8253 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008254 if (err)
8255 return err;
8256
Johannes Berg55682962007-09-20 13:09:35 -04008257 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8258 if (err)
8259 goto err_out;
8260
Johannes Berg2a519312009-02-10 21:25:55 +01008261 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8262 if (err)
8263 goto err_out;
8264
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008265 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8266 if (err)
8267 goto err_out;
8268
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008269 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8270 if (err)
8271 goto err_out;
8272
Johannes Bergaff89a92009-07-01 21:26:51 +02008273#ifdef CONFIG_NL80211_TESTMODE
8274 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8275 if (err)
8276 goto err_out;
8277#endif
8278
Jouni Malinen026331c2010-02-15 12:53:10 +02008279 err = netlink_register_notifier(&nl80211_netlink_notifier);
8280 if (err)
8281 goto err_out;
8282
Johannes Berg55682962007-09-20 13:09:35 -04008283 return 0;
8284 err_out:
8285 genl_unregister_family(&nl80211_fam);
8286 return err;
8287}
8288
8289void nl80211_exit(void)
8290{
Jouni Malinen026331c2010-02-15 12:53:10 +02008291 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008292 genl_unregister_family(&nl80211_fam);
8293}