blob: b67b1114e25a95a6f9bf0c8ce68eaff380765a85 [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 Berg77dbbb12011-07-13 10:48:55 +0200237 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200241};
242
Johannes Berge5497d72011-07-05 16:35:40 +0200243/* policy for GTK rekey offload attributes */
244static const struct nla_policy
245nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
246 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
247 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
248 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249};
250
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300251static const struct nla_policy
252nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
253 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_SSID_LEN },
255};
256
Holger Schuriga0438972009-11-11 11:30:02 +0100257/* ifidx get helper */
258static int nl80211_get_ifidx(struct netlink_callback *cb)
259{
260 int res;
261
262 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
263 nl80211_fam.attrbuf, nl80211_fam.maxattr,
264 nl80211_policy);
265 if (res)
266 return res;
267
268 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
269 return -EINVAL;
270
271 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
272 if (!res)
273 return -EINVAL;
274 return res;
275}
276
Johannes Berg67748892010-10-04 21:14:06 +0200277static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
278 struct netlink_callback *cb,
279 struct cfg80211_registered_device **rdev,
280 struct net_device **dev)
281{
282 int ifidx = cb->args[0];
283 int err;
284
285 if (!ifidx)
286 ifidx = nl80211_get_ifidx(cb);
287 if (ifidx < 0)
288 return ifidx;
289
290 cb->args[0] = ifidx;
291
292 rtnl_lock();
293
294 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
295 if (!*dev) {
296 err = -ENODEV;
297 goto out_rtnl;
298 }
299
300 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100301 if (IS_ERR(*rdev)) {
302 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200303 goto out_rtnl;
304 }
305
306 return 0;
307 out_rtnl:
308 rtnl_unlock();
309 return err;
310}
311
312static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313{
314 cfg80211_unlock_rdev(rdev);
315 rtnl_unlock();
316}
317
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100318/* IE validation */
319static bool is_valid_ie_attr(const struct nlattr *attr)
320{
321 const u8 *pos;
322 int len;
323
324 if (!attr)
325 return true;
326
327 pos = nla_data(attr);
328 len = nla_len(attr);
329
330 while (len) {
331 u8 elemlen;
332
333 if (len < 2)
334 return false;
335 len -= 2;
336
337 elemlen = pos[1];
338 if (elemlen > len)
339 return false;
340
341 len -= elemlen;
342 pos += 2 + elemlen;
343 }
344
345 return true;
346}
347
Johannes Berg55682962007-09-20 13:09:35 -0400348/* message building helper */
349static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
350 int flags, u8 cmd)
351{
352 /* since there is no private header just add the generic one */
353 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354}
355
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400356static int nl80211_msg_put_channel(struct sk_buff *msg,
357 struct ieee80211_channel *chan)
358{
David S. Miller9360ffd2012-03-29 04:41:26 -0400359 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360 chan->center_freq))
361 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400362
David S. Miller9360ffd2012-03-29 04:41:26 -0400363 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
364 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
365 goto nla_put_failure;
366 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
367 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
368 goto nla_put_failure;
369 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
370 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
371 goto nla_put_failure;
372 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
373 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
374 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400375
David S. Miller9360ffd2012-03-29 04:41:26 -0400376 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
377 DBM_TO_MBM(chan->max_power)))
378 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400379
380 return 0;
381
382 nla_put_failure:
383 return -ENOBUFS;
384}
385
Johannes Berg55682962007-09-20 13:09:35 -0400386/* netlink command implementations */
387
Johannes Bergb9454e82009-07-08 13:29:08 +0200388struct key_parse {
389 struct key_params p;
390 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394};
395
396static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
397{
398 struct nlattr *tb[NL80211_KEY_MAX + 1];
399 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
400 nl80211_key_policy);
401 if (err)
402 return err;
403
404 k->def = !!tb[NL80211_KEY_DEFAULT];
405 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (k->def) {
408 k->def_uni = true;
409 k->def_multi = true;
410 }
411 if (k->defmgmt)
412 k->def_multi = true;
413
Johannes Bergb9454e82009-07-08 13:29:08 +0200414 if (tb[NL80211_KEY_IDX])
415 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
416
417 if (tb[NL80211_KEY_DATA]) {
418 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
419 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
420 }
421
422 if (tb[NL80211_KEY_SEQ]) {
423 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
424 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
425 }
426
427 if (tb[NL80211_KEY_CIPHER])
428 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
429
Johannes Berge31b8212010-10-05 19:39:30 +0200430 if (tb[NL80211_KEY_TYPE]) {
431 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
432 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
433 return -EINVAL;
434 }
435
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100436 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
437 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100438 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
439 tb[NL80211_KEY_DEFAULT_TYPES],
440 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100441 if (err)
442 return err;
443
444 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
445 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
446 }
447
Johannes Bergb9454e82009-07-08 13:29:08 +0200448 return 0;
449}
450
451static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
452{
453 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
454 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
455 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
456 }
457
458 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
459 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
460 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
461 }
462
463 if (info->attrs[NL80211_ATTR_KEY_IDX])
464 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
465
466 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
467 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
468
469 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
470 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
471
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100472 if (k->def) {
473 k->def_uni = true;
474 k->def_multi = true;
475 }
476 if (k->defmgmt)
477 k->def_multi = true;
478
Johannes Berge31b8212010-10-05 19:39:30 +0200479 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
480 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
481 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
482 return -EINVAL;
483 }
484
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100485 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
486 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
487 int err = nla_parse_nested(
488 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
489 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
490 nl80211_key_default_policy);
491 if (err)
492 return err;
493
494 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
495 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 return 0;
499}
500
501static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
502{
503 int err;
504
505 memset(k, 0, sizeof(*k));
506 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200507 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200508
509 if (info->attrs[NL80211_ATTR_KEY])
510 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
511 else
512 err = nl80211_parse_key_old(info, k);
513
514 if (err)
515 return err;
516
517 if (k->def && k->defmgmt)
518 return -EINVAL;
519
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (k->defmgmt) {
521 if (k->def_uni || !k->def_multi)
522 return -EINVAL;
523 }
524
Johannes Bergb9454e82009-07-08 13:29:08 +0200525 if (k->idx != -1) {
526 if (k->defmgmt) {
527 if (k->idx < 4 || k->idx > 5)
528 return -EINVAL;
529 } else if (k->def) {
530 if (k->idx < 0 || k->idx > 3)
531 return -EINVAL;
532 } else {
533 if (k->idx < 0 || k->idx > 5)
534 return -EINVAL;
535 }
536 }
537
538 return 0;
539}
540
Johannes Bergfffd0932009-07-08 14:22:54 +0200541static struct cfg80211_cached_keys *
542nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
543 struct nlattr *keys)
544{
545 struct key_parse parse;
546 struct nlattr *key;
547 struct cfg80211_cached_keys *result;
548 int rem, err, def = 0;
549
550 result = kzalloc(sizeof(*result), GFP_KERNEL);
551 if (!result)
552 return ERR_PTR(-ENOMEM);
553
554 result->def = -1;
555 result->defmgmt = -1;
556
557 nla_for_each_nested(key, keys, rem) {
558 memset(&parse, 0, sizeof(parse));
559 parse.idx = -1;
560
561 err = nl80211_parse_key_new(key, &parse);
562 if (err)
563 goto error;
564 err = -EINVAL;
565 if (!parse.p.key)
566 goto error;
567 if (parse.idx < 0 || parse.idx > 4)
568 goto error;
569 if (parse.def) {
570 if (def)
571 goto error;
572 def = 1;
573 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (!parse.def_uni || !parse.def_multi)
575 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 } else if (parse.defmgmt)
577 goto error;
578 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200579 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200580 if (err)
581 goto error;
582 result->params[parse.idx].cipher = parse.p.cipher;
583 result->params[parse.idx].key_len = parse.p.key_len;
584 result->params[parse.idx].key = result->data[parse.idx];
585 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
586 }
587
588 return result;
589 error:
590 kfree(result);
591 return ERR_PTR(err);
592}
593
594static int nl80211_key_allowed(struct wireless_dev *wdev)
595{
596 ASSERT_WDEV_LOCK(wdev);
597
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 switch (wdev->iftype) {
599 case NL80211_IFTYPE_AP:
600 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700602 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 break;
604 case NL80211_IFTYPE_ADHOC:
605 if (!wdev->current_bss)
606 return -ENOLINK;
607 break;
608 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200609 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200610 if (wdev->sme_state != CFG80211_SME_CONNECTED)
611 return -ENOLINK;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 return 0;
618}
619
Johannes Berg7527a782011-05-13 10:58:57 +0200620static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
621{
622 struct nlattr *nl_modes = nla_nest_start(msg, attr);
623 int i;
624
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400630 if ((ifmodes & 1) && nla_put_flag(msg, i))
631 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637 return 0;
638
639nla_put_failure:
640 return -ENOBUFS;
641}
642
643static int nl80211_put_iface_combinations(struct wiphy *wiphy,
644 struct sk_buff *msg)
645{
646 struct nlattr *nl_combis;
647 int i, j;
648
649 nl_combis = nla_nest_start(msg,
650 NL80211_ATTR_INTERFACE_COMBINATIONS);
651 if (!nl_combis)
652 goto nla_put_failure;
653
654 for (i = 0; i < wiphy->n_iface_combinations; i++) {
655 const struct ieee80211_iface_combination *c;
656 struct nlattr *nl_combi, *nl_limits;
657
658 c = &wiphy->iface_combinations[i];
659
660 nl_combi = nla_nest_start(msg, i + 1);
661 if (!nl_combi)
662 goto nla_put_failure;
663
664 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
665 if (!nl_limits)
666 goto nla_put_failure;
667
668 for (j = 0; j < c->n_limits; j++) {
669 struct nlattr *nl_limit;
670
671 nl_limit = nla_nest_start(msg, j + 1);
672 if (!nl_limit)
673 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400674 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
675 c->limits[j].max))
676 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200677 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
678 c->limits[j].types))
679 goto nla_put_failure;
680 nla_nest_end(msg, nl_limit);
681 }
682
683 nla_nest_end(msg, nl_limits);
684
David S. Miller9360ffd2012-03-29 04:41:26 -0400685 if (c->beacon_int_infra_match &&
686 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
687 goto nla_put_failure;
688 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
689 c->num_different_channels) ||
690 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
691 c->max_interfaces))
692 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200693
694 nla_nest_end(msg, nl_combi);
695 }
696
697 nla_nest_end(msg, nl_combis);
698
699 return 0;
700nla_put_failure:
701 return -ENOBUFS;
702}
703
Johannes Berg55682962007-09-20 13:09:35 -0400704static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
705 struct cfg80211_registered_device *dev)
706{
707 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100708 struct nlattr *nl_bands, *nl_band;
709 struct nlattr *nl_freqs, *nl_freq;
710 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100711 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100712 enum ieee80211_band band;
713 struct ieee80211_channel *chan;
714 struct ieee80211_rate *rate;
715 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200716 const struct ieee80211_txrx_stypes *mgmt_stypes =
717 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400718
719 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
720 if (!hdr)
721 return -1;
722
David S. Miller9360ffd2012-03-29 04:41:26 -0400723 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
724 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
725 nla_put_u32(msg, NL80211_ATTR_GENERATION,
726 cfg80211_rdev_list_generation) ||
727 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
728 dev->wiphy.retry_short) ||
729 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
730 dev->wiphy.retry_long) ||
731 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
732 dev->wiphy.frag_threshold) ||
733 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
734 dev->wiphy.rts_threshold) ||
735 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
736 dev->wiphy.coverage_class) ||
737 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
738 dev->wiphy.max_scan_ssids) ||
739 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
740 dev->wiphy.max_sched_scan_ssids) ||
741 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
742 dev->wiphy.max_scan_ie_len) ||
743 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
744 dev->wiphy.max_sched_scan_ie_len) ||
745 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
746 dev->wiphy.max_match_sets))
747 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200748
David S. Miller9360ffd2012-03-29 04:41:26 -0400749 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
750 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
751 goto nla_put_failure;
752 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
753 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
754 goto nla_put_failure;
755 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
756 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
757 goto nla_put_failure;
758 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
759 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
760 goto nla_put_failure;
761 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
762 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
763 goto nla_put_failure;
764 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
765 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
766 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200767
David S. Miller9360ffd2012-03-29 04:41:26 -0400768 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
769 sizeof(u32) * dev->wiphy.n_cipher_suites,
770 dev->wiphy.cipher_suites))
771 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100772
David S. Miller9360ffd2012-03-29 04:41:26 -0400773 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
774 dev->wiphy.max_num_pmkids))
775 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530776
David S. Miller9360ffd2012-03-29 04:41:26 -0400777 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
778 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
779 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200780
David S. Miller9360ffd2012-03-29 04:41:26 -0400781 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
782 dev->wiphy.available_antennas_tx) ||
783 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
784 dev->wiphy.available_antennas_rx))
785 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100786
David S. Miller9360ffd2012-03-29 04:41:26 -0400787 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
788 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
789 dev->wiphy.probe_resp_offload))
790 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200791
Bruno Randolf7f531e02010-12-16 11:30:22 +0900792 if ((dev->wiphy.available_antennas_tx ||
793 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900794 u32 tx_ant = 0, rx_ant = 0;
795 int res;
796 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
797 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
799 tx_ant) ||
800 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
801 rx_ant))
802 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900803 }
804 }
805
Johannes Berg7527a782011-05-13 10:58:57 +0200806 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
807 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700808 goto nla_put_failure;
809
Johannes Bergee688b002008-01-24 19:38:39 +0100810 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
811 if (!nl_bands)
812 goto nla_put_failure;
813
814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
815 if (!dev->wiphy.bands[band])
816 continue;
817
818 nl_band = nla_nest_start(msg, band);
819 if (!nl_band)
820 goto nla_put_failure;
821
Johannes Bergd51626d2008-10-09 12:20:13 +0200822 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
824 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
825 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
826 &dev->wiphy.bands[band]->ht_cap.mcs) ||
827 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
828 dev->wiphy.bands[band]->ht_cap.cap) ||
829 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
830 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
831 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
832 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
833 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200834
Johannes Bergee688b002008-01-24 19:38:39 +0100835 /* add frequencies */
836 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
837 if (!nl_freqs)
838 goto nla_put_failure;
839
840 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
841 nl_freq = nla_nest_start(msg, i);
842 if (!nl_freq)
843 goto nla_put_failure;
844
845 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100846
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400847 if (nl80211_msg_put_channel(msg, chan))
848 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200849
Johannes Bergee688b002008-01-24 19:38:39 +0100850 nla_nest_end(msg, nl_freq);
851 }
852
853 nla_nest_end(msg, nl_freqs);
854
855 /* add bitrates */
856 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
857 if (!nl_rates)
858 goto nla_put_failure;
859
860 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
861 nl_rate = nla_nest_start(msg, i);
862 if (!nl_rate)
863 goto nla_put_failure;
864
865 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
867 rate->bitrate))
868 goto nla_put_failure;
869 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
870 nla_put_flag(msg,
871 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
872 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100873
874 nla_nest_end(msg, nl_rate);
875 }
876
877 nla_nest_end(msg, nl_rates);
878
879 nla_nest_end(msg, nl_band);
880 }
881 nla_nest_end(msg, nl_bands);
882
Johannes Berg8fdc6212009-03-14 09:34:01 +0100883 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
884 if (!nl_cmds)
885 goto nla_put_failure;
886
887 i = 0;
888#define CMD(op, n) \
889 do { \
890 if (dev->ops->op) { \
891 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
893 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894 } \
895 } while (0)
896
897 CMD(add_virtual_intf, NEW_INTERFACE);
898 CMD(change_virtual_intf, SET_INTERFACE);
899 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100900 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100901 CMD(add_station, NEW_STATION);
902 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800903 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100904 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200905 CMD(auth, AUTHENTICATE);
906 CMD(assoc, ASSOCIATE);
907 CMD(deauth, DEAUTHENTICATE);
908 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200909 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100910 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100911 CMD(set_pmksa, SET_PMKSA);
912 CMD(del_pmksa, DEL_PMKSA);
913 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100914 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
915 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200916 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200917 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100918 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100919 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200920 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
922 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +0200923 }
Johannes Bergf444de02010-05-05 15:25:02 +0200924 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400925 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300926 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
927 CMD(tdls_mgmt, TDLS_MGMT);
928 CMD(tdls_oper, TDLS_OPER);
929 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300930 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
931 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100932 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100933 CMD(set_noack_map, SET_NOACK_MAP);
Johannes 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,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003393 cur_params.rssi_threshold) ||
3394 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
3395 cur_params.ht_opmode))
David S. Miller9360ffd2012-03-29 04:41:26 -04003396 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003397 nla_nest_end(msg, pinfoattr);
3398 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003399 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003400
Johannes Berg3b858752009-03-12 09:55:09 +01003401 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003402 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003403 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003404 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003405 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003406}
3407
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003408static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003409 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3410 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3411 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3412 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3413 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3414 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003415 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003416 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003417 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003418
3419 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3420 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3421 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3422 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3423 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003424 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003425 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003426 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003427 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003428 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003429 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003430 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003431 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003432};
3433
Javier Cardonac80d5452010-12-16 17:37:49 -08003434static const struct nla_policy
3435 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003436 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003437 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3438 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003439 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003440 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003441 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003442 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003443};
3444
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003445static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003446 struct mesh_config *cfg,
3447 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003448{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003449 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003450 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003451
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003452#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3453do {\
3454 if (table[attr_num]) {\
3455 cfg->param = nla_fn(table[attr_num]); \
3456 mask |= (1 << (attr_num - 1)); \
3457 } \
3458} while (0);\
3459
3460
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003461 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003462 return -EINVAL;
3463 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003464 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003465 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003466 return -EINVAL;
3467
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003468 /* This makes sure that there aren't more than 32 mesh config
3469 * parameters (otherwise our bitfield scheme would not work.) */
3470 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3471
3472 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003473 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3474 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3475 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3476 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3477 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3478 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3479 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3480 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3481 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3482 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3483 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3484 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003485 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3486 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003487 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3488 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
Javier Cardonad299a1f2012-03-31 11:31:33 -07003489 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
3490 mask, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3491 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003492 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3493 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3494 nla_get_u8);
3495 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3496 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3497 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3498 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3499 nla_get_u16);
3500 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3501 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3502 nla_get_u32);
3503 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3504 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3505 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003506 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3507 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3508 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003509 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3510 dot11MeshHWMPnetDiameterTraversalTime,
3511 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3512 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003513 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3514 dot11MeshHWMPRootMode, mask,
3515 NL80211_MESHCONF_HWMP_ROOTMODE,
3516 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003517 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3518 dot11MeshHWMPRannInterval, mask,
3519 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3520 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003521 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3522 dot11MeshGateAnnouncementProtocol, mask,
3523 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3524 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003525 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3526 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003527 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3528 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003529 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
3530 mask, NL80211_MESHCONF_HT_OPMODE, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003531 if (mask_out)
3532 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003533
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003534 return 0;
3535
3536#undef FILL_IN_MESH_PARAM_IF_SET
3537}
3538
Javier Cardonac80d5452010-12-16 17:37:49 -08003539static int nl80211_parse_mesh_setup(struct genl_info *info,
3540 struct mesh_setup *setup)
3541{
3542 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3543
3544 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3545 return -EINVAL;
3546 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3547 info->attrs[NL80211_ATTR_MESH_SETUP],
3548 nl80211_mesh_setup_params_policy))
3549 return -EINVAL;
3550
Javier Cardonad299a1f2012-03-31 11:31:33 -07003551 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3552 setup->sync_method =
3553 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3554 IEEE80211_SYNC_METHOD_VENDOR :
3555 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3556
Javier Cardonac80d5452010-12-16 17:37:49 -08003557 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3558 setup->path_sel_proto =
3559 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3560 IEEE80211_PATH_PROTOCOL_VENDOR :
3561 IEEE80211_PATH_PROTOCOL_HWMP;
3562
3563 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3564 setup->path_metric =
3565 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3566 IEEE80211_PATH_METRIC_VENDOR :
3567 IEEE80211_PATH_METRIC_AIRTIME;
3568
Javier Cardona581a8b02011-04-07 15:08:27 -07003569
3570 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003571 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003572 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003573 if (!is_valid_ie_attr(ieattr))
3574 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003575 setup->ie = nla_data(ieattr);
3576 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003577 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003578 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3579 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003580
3581 return 0;
3582}
3583
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003584static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003585 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003586{
3587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3588 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003589 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003590 struct mesh_config cfg;
3591 u32 mask;
3592 int err;
3593
Johannes Berg29cbe682010-12-03 09:20:44 +01003594 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3595 return -EOPNOTSUPP;
3596
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003597 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003598 return -EOPNOTSUPP;
3599
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003600 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003601 if (err)
3602 return err;
3603
Johannes Berg29cbe682010-12-03 09:20:44 +01003604 wdev_lock(wdev);
3605 if (!wdev->mesh_id_len)
3606 err = -ENOLINK;
3607
3608 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003609 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003610 mask, &cfg);
3611
3612 wdev_unlock(wdev);
3613
3614 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003615}
3616
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003617static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3618{
3619 struct sk_buff *msg;
3620 void *hdr = NULL;
3621 struct nlattr *nl_reg_rules;
3622 unsigned int i;
3623 int err = -EINVAL;
3624
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003625 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003626
3627 if (!cfg80211_regdomain)
3628 goto out;
3629
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003631 if (!msg) {
3632 err = -ENOBUFS;
3633 goto out;
3634 }
3635
3636 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3637 NL80211_CMD_GET_REG);
3638 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003639 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003640
David S. Miller9360ffd2012-03-29 04:41:26 -04003641 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3642 cfg80211_regdomain->alpha2) ||
3643 (cfg80211_regdomain->dfs_region &&
3644 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3645 cfg80211_regdomain->dfs_region)))
3646 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003647
3648 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3649 if (!nl_reg_rules)
3650 goto nla_put_failure;
3651
3652 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3653 struct nlattr *nl_reg_rule;
3654 const struct ieee80211_reg_rule *reg_rule;
3655 const struct ieee80211_freq_range *freq_range;
3656 const struct ieee80211_power_rule *power_rule;
3657
3658 reg_rule = &cfg80211_regdomain->reg_rules[i];
3659 freq_range = &reg_rule->freq_range;
3660 power_rule = &reg_rule->power_rule;
3661
3662 nl_reg_rule = nla_nest_start(msg, i);
3663 if (!nl_reg_rule)
3664 goto nla_put_failure;
3665
David S. Miller9360ffd2012-03-29 04:41:26 -04003666 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3667 reg_rule->flags) ||
3668 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3669 freq_range->start_freq_khz) ||
3670 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3671 freq_range->end_freq_khz) ||
3672 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3673 freq_range->max_bandwidth_khz) ||
3674 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3675 power_rule->max_antenna_gain) ||
3676 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3677 power_rule->max_eirp))
3678 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003679
3680 nla_nest_end(msg, nl_reg_rule);
3681 }
3682
3683 nla_nest_end(msg, nl_reg_rules);
3684
3685 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003686 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003687 goto out;
3688
3689nla_put_failure:
3690 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003691put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003692 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003693 err = -EMSGSIZE;
3694out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003695 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003696 return err;
3697}
3698
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003699static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3700{
3701 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3702 struct nlattr *nl_reg_rule;
3703 char *alpha2 = NULL;
3704 int rem_reg_rules = 0, r = 0;
3705 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003706 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003707 struct ieee80211_regdomain *rd = NULL;
3708
3709 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3710 return -EINVAL;
3711
3712 if (!info->attrs[NL80211_ATTR_REG_RULES])
3713 return -EINVAL;
3714
3715 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3716
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003717 if (info->attrs[NL80211_ATTR_DFS_REGION])
3718 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3719
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003720 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3721 rem_reg_rules) {
3722 num_rules++;
3723 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003724 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003725 }
3726
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003727 mutex_lock(&cfg80211_mutex);
3728
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003729 if (!reg_is_valid_request(alpha2)) {
3730 r = -EINVAL;
3731 goto bad_reg;
3732 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003733
3734 size_of_regd = sizeof(struct ieee80211_regdomain) +
3735 (num_rules * sizeof(struct ieee80211_reg_rule));
3736
3737 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003738 if (!rd) {
3739 r = -ENOMEM;
3740 goto bad_reg;
3741 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003742
3743 rd->n_reg_rules = num_rules;
3744 rd->alpha2[0] = alpha2[0];
3745 rd->alpha2[1] = alpha2[1];
3746
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003747 /*
3748 * Disable DFS master mode if the DFS region was
3749 * not supported or known on this kernel.
3750 */
3751 if (reg_supported_dfs_region(dfs_region))
3752 rd->dfs_region = dfs_region;
3753
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003754 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3755 rem_reg_rules) {
3756 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3757 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3758 reg_rule_policy);
3759 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3760 if (r)
3761 goto bad_reg;
3762
3763 rule_idx++;
3764
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003765 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3766 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003767 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003768 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003769 }
3770
3771 BUG_ON(rule_idx != num_rules);
3772
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003773 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003774
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003775 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003776
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003777 return r;
3778
Johannes Bergd2372b32008-10-24 20:32:20 +02003779 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003780 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003781 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003782 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003783}
3784
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003785static int validate_scan_freqs(struct nlattr *freqs)
3786{
3787 struct nlattr *attr1, *attr2;
3788 int n_channels = 0, tmp1, tmp2;
3789
3790 nla_for_each_nested(attr1, freqs, tmp1) {
3791 n_channels++;
3792 /*
3793 * Some hardware has a limited channel list for
3794 * scanning, and it is pretty much nonsensical
3795 * to scan for a channel twice, so disallow that
3796 * and don't require drivers to check that the
3797 * channel list they get isn't longer than what
3798 * they can scan, as long as they can scan all
3799 * the channels they registered at once.
3800 */
3801 nla_for_each_nested(attr2, freqs, tmp2)
3802 if (attr1 != attr2 &&
3803 nla_get_u32(attr1) == nla_get_u32(attr2))
3804 return 0;
3805 }
3806
3807 return n_channels;
3808}
3809
Johannes Berg2a519312009-02-10 21:25:55 +01003810static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3811{
Johannes Berg4c476992010-10-04 21:36:35 +02003812 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3813 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003814 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003815 struct nlattr *attr;
3816 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003817 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003818 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003819
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003820 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3821 return -EINVAL;
3822
Johannes Berg79c97e92009-07-07 03:56:12 +02003823 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003824
Johannes Berg4c476992010-10-04 21:36:35 +02003825 if (!rdev->ops->scan)
3826 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003827
Johannes Berg4c476992010-10-04 21:36:35 +02003828 if (rdev->scan_req)
3829 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003830
3831 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003832 n_channels = validate_scan_freqs(
3833 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003834 if (!n_channels)
3835 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003836 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003837 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003838 n_channels = 0;
3839
Johannes Berg2a519312009-02-10 21:25:55 +01003840 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3841 if (wiphy->bands[band])
3842 n_channels += wiphy->bands[band]->n_channels;
3843 }
3844
3845 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3846 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3847 n_ssids++;
3848
Johannes Berg4c476992010-10-04 21:36:35 +02003849 if (n_ssids > wiphy->max_scan_ssids)
3850 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003851
Jouni Malinen70692ad2009-02-16 19:39:13 +02003852 if (info->attrs[NL80211_ATTR_IE])
3853 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3854 else
3855 ie_len = 0;
3856
Johannes Berg4c476992010-10-04 21:36:35 +02003857 if (ie_len > wiphy->max_scan_ie_len)
3858 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003859
Johannes Berg2a519312009-02-10 21:25:55 +01003860 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003861 + sizeof(*request->ssids) * n_ssids
3862 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003863 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003864 if (!request)
3865 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003866
Johannes Berg2a519312009-02-10 21:25:55 +01003867 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003868 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003869 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003870 if (ie_len) {
3871 if (request->ssids)
3872 request->ie = (void *)(request->ssids + n_ssids);
3873 else
3874 request->ie = (void *)(request->channels + n_channels);
3875 }
Johannes Berg2a519312009-02-10 21:25:55 +01003876
Johannes Berg584991d2009-11-02 13:32:03 +01003877 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003878 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3879 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003880 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003881 struct ieee80211_channel *chan;
3882
3883 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3884
3885 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003886 err = -EINVAL;
3887 goto out_free;
3888 }
Johannes Berg584991d2009-11-02 13:32:03 +01003889
3890 /* ignore disabled channels */
3891 if (chan->flags & IEEE80211_CHAN_DISABLED)
3892 continue;
3893
3894 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003895 i++;
3896 }
3897 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003898 enum ieee80211_band band;
3899
Johannes Berg2a519312009-02-10 21:25:55 +01003900 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003901 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3902 int j;
3903 if (!wiphy->bands[band])
3904 continue;
3905 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003906 struct ieee80211_channel *chan;
3907
3908 chan = &wiphy->bands[band]->channels[j];
3909
3910 if (chan->flags & IEEE80211_CHAN_DISABLED)
3911 continue;
3912
3913 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003914 i++;
3915 }
3916 }
3917 }
3918
Johannes Berg584991d2009-11-02 13:32:03 +01003919 if (!i) {
3920 err = -EINVAL;
3921 goto out_free;
3922 }
3923
3924 request->n_channels = i;
3925
Johannes Berg2a519312009-02-10 21:25:55 +01003926 i = 0;
3927 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3928 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003929 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003930 err = -EINVAL;
3931 goto out_free;
3932 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003933 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003934 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003935 i++;
3936 }
3937 }
3938
Jouni Malinen70692ad2009-02-16 19:39:13 +02003939 if (info->attrs[NL80211_ATTR_IE]) {
3940 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003941 memcpy((void *)request->ie,
3942 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003943 request->ie_len);
3944 }
3945
Johannes Berg34850ab2011-07-18 18:08:35 +02003946 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003947 if (wiphy->bands[i])
3948 request->rates[i] =
3949 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003950
3951 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3952 nla_for_each_nested(attr,
3953 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3954 tmp) {
3955 enum ieee80211_band band = nla_type(attr);
3956
Dan Carpenter84404622011-07-29 11:52:18 +03003957 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003958 err = -EINVAL;
3959 goto out_free;
3960 }
3961 err = ieee80211_get_ratemask(wiphy->bands[band],
3962 nla_data(attr),
3963 nla_len(attr),
3964 &request->rates[band]);
3965 if (err)
3966 goto out_free;
3967 }
3968 }
3969
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303970 request->no_cck =
3971 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3972
Johannes Berg463d0182009-07-14 00:33:35 +02003973 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003974 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003975
Johannes Berg79c97e92009-07-07 03:56:12 +02003976 rdev->scan_req = request;
3977 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003978
Johannes Berg463d0182009-07-14 00:33:35 +02003979 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003980 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003981 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003982 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003983 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003984 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003985 kfree(request);
3986 }
Johannes Berg3b858752009-03-12 09:55:09 +01003987
Johannes Berg2a519312009-02-10 21:25:55 +01003988 return err;
3989}
3990
Luciano Coelho807f8a82011-05-11 17:09:35 +03003991static int nl80211_start_sched_scan(struct sk_buff *skb,
3992 struct genl_info *info)
3993{
3994 struct cfg80211_sched_scan_request *request;
3995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3996 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003997 struct nlattr *attr;
3998 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003999 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004000 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004001 enum ieee80211_band band;
4002 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004003 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004004
4005 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4006 !rdev->ops->sched_scan_start)
4007 return -EOPNOTSUPP;
4008
4009 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4010 return -EINVAL;
4011
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004012 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4013 return -EINVAL;
4014
4015 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4016 if (interval == 0)
4017 return -EINVAL;
4018
Luciano Coelho807f8a82011-05-11 17:09:35 +03004019 wiphy = &rdev->wiphy;
4020
4021 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4022 n_channels = validate_scan_freqs(
4023 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4024 if (!n_channels)
4025 return -EINVAL;
4026 } else {
4027 n_channels = 0;
4028
4029 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4030 if (wiphy->bands[band])
4031 n_channels += wiphy->bands[band]->n_channels;
4032 }
4033
4034 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4035 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4036 tmp)
4037 n_ssids++;
4038
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004039 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004040 return -EINVAL;
4041
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004042 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4043 nla_for_each_nested(attr,
4044 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4045 tmp)
4046 n_match_sets++;
4047
4048 if (n_match_sets > wiphy->max_match_sets)
4049 return -EINVAL;
4050
Luciano Coelho807f8a82011-05-11 17:09:35 +03004051 if (info->attrs[NL80211_ATTR_IE])
4052 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4053 else
4054 ie_len = 0;
4055
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004056 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004057 return -EINVAL;
4058
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004059 mutex_lock(&rdev->sched_scan_mtx);
4060
4061 if (rdev->sched_scan_req) {
4062 err = -EINPROGRESS;
4063 goto out;
4064 }
4065
Luciano Coelho807f8a82011-05-11 17:09:35 +03004066 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004067 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004068 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004069 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004070 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004071 if (!request) {
4072 err = -ENOMEM;
4073 goto out;
4074 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004075
4076 if (n_ssids)
4077 request->ssids = (void *)&request->channels[n_channels];
4078 request->n_ssids = n_ssids;
4079 if (ie_len) {
4080 if (request->ssids)
4081 request->ie = (void *)(request->ssids + n_ssids);
4082 else
4083 request->ie = (void *)(request->channels + n_channels);
4084 }
4085
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004086 if (n_match_sets) {
4087 if (request->ie)
4088 request->match_sets = (void *)(request->ie + ie_len);
4089 else if (request->ssids)
4090 request->match_sets =
4091 (void *)(request->ssids + n_ssids);
4092 else
4093 request->match_sets =
4094 (void *)(request->channels + n_channels);
4095 }
4096 request->n_match_sets = n_match_sets;
4097
Luciano Coelho807f8a82011-05-11 17:09:35 +03004098 i = 0;
4099 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4100 /* user specified, bail out if channel not found */
4101 nla_for_each_nested(attr,
4102 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4103 tmp) {
4104 struct ieee80211_channel *chan;
4105
4106 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4107
4108 if (!chan) {
4109 err = -EINVAL;
4110 goto out_free;
4111 }
4112
4113 /* ignore disabled channels */
4114 if (chan->flags & IEEE80211_CHAN_DISABLED)
4115 continue;
4116
4117 request->channels[i] = chan;
4118 i++;
4119 }
4120 } else {
4121 /* all channels */
4122 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4123 int j;
4124 if (!wiphy->bands[band])
4125 continue;
4126 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4127 struct ieee80211_channel *chan;
4128
4129 chan = &wiphy->bands[band]->channels[j];
4130
4131 if (chan->flags & IEEE80211_CHAN_DISABLED)
4132 continue;
4133
4134 request->channels[i] = chan;
4135 i++;
4136 }
4137 }
4138 }
4139
4140 if (!i) {
4141 err = -EINVAL;
4142 goto out_free;
4143 }
4144
4145 request->n_channels = i;
4146
4147 i = 0;
4148 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4149 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4150 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004151 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004152 err = -EINVAL;
4153 goto out_free;
4154 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004155 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004156 memcpy(request->ssids[i].ssid, nla_data(attr),
4157 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004158 i++;
4159 }
4160 }
4161
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004162 i = 0;
4163 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4164 nla_for_each_nested(attr,
4165 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4166 tmp) {
4167 struct nlattr *ssid;
4168
4169 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4170 nla_data(attr), nla_len(attr),
4171 nl80211_match_policy);
4172 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4173 if (ssid) {
4174 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4175 err = -EINVAL;
4176 goto out_free;
4177 }
4178 memcpy(request->match_sets[i].ssid.ssid,
4179 nla_data(ssid), nla_len(ssid));
4180 request->match_sets[i].ssid.ssid_len =
4181 nla_len(ssid);
4182 }
4183 i++;
4184 }
4185 }
4186
Luciano Coelho807f8a82011-05-11 17:09:35 +03004187 if (info->attrs[NL80211_ATTR_IE]) {
4188 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4189 memcpy((void *)request->ie,
4190 nla_data(info->attrs[NL80211_ATTR_IE]),
4191 request->ie_len);
4192 }
4193
4194 request->dev = dev;
4195 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004196 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004197
4198 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4199 if (!err) {
4200 rdev->sched_scan_req = request;
4201 nl80211_send_sched_scan(rdev, dev,
4202 NL80211_CMD_START_SCHED_SCAN);
4203 goto out;
4204 }
4205
4206out_free:
4207 kfree(request);
4208out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004209 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004210 return err;
4211}
4212
4213static int nl80211_stop_sched_scan(struct sk_buff *skb,
4214 struct genl_info *info)
4215{
4216 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004217 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004218
4219 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4220 !rdev->ops->sched_scan_stop)
4221 return -EOPNOTSUPP;
4222
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004223 mutex_lock(&rdev->sched_scan_mtx);
4224 err = __cfg80211_stop_sched_scan(rdev, false);
4225 mutex_unlock(&rdev->sched_scan_mtx);
4226
4227 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004228}
4229
Johannes Berg9720bb32011-06-21 09:45:33 +02004230static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4231 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004232 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004233 struct wireless_dev *wdev,
4234 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004235{
Johannes Berg48ab9052009-07-10 18:42:31 +02004236 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004237 void *hdr;
4238 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004239
4240 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004241
Johannes Berg9720bb32011-06-21 09:45:33 +02004242 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004243 NL80211_CMD_NEW_SCAN_RESULTS);
4244 if (!hdr)
4245 return -1;
4246
Johannes Berg9720bb32011-06-21 09:45:33 +02004247 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4248
David S. Miller9360ffd2012-03-29 04:41:26 -04004249 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4250 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4251 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004252
4253 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4254 if (!bss)
4255 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004256 if ((!is_zero_ether_addr(res->bssid) &&
4257 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4258 (res->information_elements && res->len_information_elements &&
4259 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4260 res->len_information_elements,
4261 res->information_elements)) ||
4262 (res->beacon_ies && res->len_beacon_ies &&
4263 res->beacon_ies != res->information_elements &&
4264 nla_put(msg, NL80211_BSS_BEACON_IES,
4265 res->len_beacon_ies, res->beacon_ies)))
4266 goto nla_put_failure;
4267 if (res->tsf &&
4268 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4269 goto nla_put_failure;
4270 if (res->beacon_interval &&
4271 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4272 goto nla_put_failure;
4273 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4274 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4275 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4276 jiffies_to_msecs(jiffies - intbss->ts)))
4277 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004278
Johannes Berg77965c92009-02-18 18:45:06 +01004279 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004280 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004281 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4282 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004283 break;
4284 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004285 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4286 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004287 break;
4288 default:
4289 break;
4290 }
4291
Johannes Berg48ab9052009-07-10 18:42:31 +02004292 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004293 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004294 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004295 if (intbss == wdev->current_bss &&
4296 nla_put_u32(msg, NL80211_BSS_STATUS,
4297 NL80211_BSS_STATUS_ASSOCIATED))
4298 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004299 break;
4300 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004301 if (intbss == wdev->current_bss &&
4302 nla_put_u32(msg, NL80211_BSS_STATUS,
4303 NL80211_BSS_STATUS_IBSS_JOINED))
4304 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004305 break;
4306 default:
4307 break;
4308 }
4309
Johannes Berg2a519312009-02-10 21:25:55 +01004310 nla_nest_end(msg, bss);
4311
4312 return genlmsg_end(msg, hdr);
4313
4314 nla_put_failure:
4315 genlmsg_cancel(msg, hdr);
4316 return -EMSGSIZE;
4317}
4318
4319static int nl80211_dump_scan(struct sk_buff *skb,
4320 struct netlink_callback *cb)
4321{
Johannes Berg48ab9052009-07-10 18:42:31 +02004322 struct cfg80211_registered_device *rdev;
4323 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004324 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004325 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004326 int start = cb->args[1], idx = 0;
4327 int err;
4328
Johannes Berg67748892010-10-04 21:14:06 +02004329 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4330 if (err)
4331 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004332
Johannes Berg48ab9052009-07-10 18:42:31 +02004333 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004334
Johannes Berg48ab9052009-07-10 18:42:31 +02004335 wdev_lock(wdev);
4336 spin_lock_bh(&rdev->bss_lock);
4337 cfg80211_bss_expire(rdev);
4338
Johannes Berg9720bb32011-06-21 09:45:33 +02004339 cb->seq = rdev->bss_generation;
4340
Johannes Berg48ab9052009-07-10 18:42:31 +02004341 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004342 if (++idx <= start)
4343 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004344 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004345 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004346 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004347 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004348 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004349 }
4350 }
4351
Johannes Berg48ab9052009-07-10 18:42:31 +02004352 spin_unlock_bh(&rdev->bss_lock);
4353 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004354
4355 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004356 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004357
Johannes Berg67748892010-10-04 21:14:06 +02004358 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004359}
4360
Holger Schurig61fa7132009-11-11 12:25:40 +01004361static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4362 int flags, struct net_device *dev,
4363 struct survey_info *survey)
4364{
4365 void *hdr;
4366 struct nlattr *infoattr;
4367
Holger Schurig61fa7132009-11-11 12:25:40 +01004368 hdr = nl80211hdr_put(msg, pid, seq, flags,
4369 NL80211_CMD_NEW_SURVEY_RESULTS);
4370 if (!hdr)
4371 return -ENOMEM;
4372
David S. Miller9360ffd2012-03-29 04:41:26 -04004373 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4374 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004375
4376 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4377 if (!infoattr)
4378 goto nla_put_failure;
4379
David S. Miller9360ffd2012-03-29 04:41:26 -04004380 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4381 survey->channel->center_freq))
4382 goto nla_put_failure;
4383
4384 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4385 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4386 goto nla_put_failure;
4387 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4388 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4389 goto nla_put_failure;
4390 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4391 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4392 survey->channel_time))
4393 goto nla_put_failure;
4394 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4395 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4396 survey->channel_time_busy))
4397 goto nla_put_failure;
4398 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4399 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4400 survey->channel_time_ext_busy))
4401 goto nla_put_failure;
4402 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4403 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4404 survey->channel_time_rx))
4405 goto nla_put_failure;
4406 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4407 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4408 survey->channel_time_tx))
4409 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004410
4411 nla_nest_end(msg, infoattr);
4412
4413 return genlmsg_end(msg, hdr);
4414
4415 nla_put_failure:
4416 genlmsg_cancel(msg, hdr);
4417 return -EMSGSIZE;
4418}
4419
4420static int nl80211_dump_survey(struct sk_buff *skb,
4421 struct netlink_callback *cb)
4422{
4423 struct survey_info survey;
4424 struct cfg80211_registered_device *dev;
4425 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004426 int survey_idx = cb->args[1];
4427 int res;
4428
Johannes Berg67748892010-10-04 21:14:06 +02004429 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4430 if (res)
4431 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004432
4433 if (!dev->ops->dump_survey) {
4434 res = -EOPNOTSUPP;
4435 goto out_err;
4436 }
4437
4438 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004439 struct ieee80211_channel *chan;
4440
Holger Schurig61fa7132009-11-11 12:25:40 +01004441 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4442 &survey);
4443 if (res == -ENOENT)
4444 break;
4445 if (res)
4446 goto out_err;
4447
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004448 /* Survey without a channel doesn't make sense */
4449 if (!survey.channel) {
4450 res = -EINVAL;
4451 goto out;
4452 }
4453
4454 chan = ieee80211_get_channel(&dev->wiphy,
4455 survey.channel->center_freq);
4456 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4457 survey_idx++;
4458 continue;
4459 }
4460
Holger Schurig61fa7132009-11-11 12:25:40 +01004461 if (nl80211_send_survey(skb,
4462 NETLINK_CB(cb->skb).pid,
4463 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4464 netdev,
4465 &survey) < 0)
4466 goto out;
4467 survey_idx++;
4468 }
4469
4470 out:
4471 cb->args[1] = survey_idx;
4472 res = skb->len;
4473 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004474 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004475 return res;
4476}
4477
Jouni Malinen255e7372009-03-20 21:21:17 +02004478static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4479{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004480 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004481}
4482
Samuel Ortizb23aa672009-07-01 21:26:54 +02004483static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4484{
4485 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4486 NL80211_WPA_VERSION_2));
4487}
4488
Jouni Malinen636a5d32009-03-19 13:39:22 +02004489static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4490{
Johannes Berg4c476992010-10-04 21:36:35 +02004491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4492 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004493 struct ieee80211_channel *chan;
4494 const u8 *bssid, *ssid, *ie = NULL;
4495 int err, ssid_len, ie_len = 0;
4496 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004497 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004498 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004499
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004500 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4501 return -EINVAL;
4502
4503 if (!info->attrs[NL80211_ATTR_MAC])
4504 return -EINVAL;
4505
Jouni Malinen17780922009-03-27 20:52:47 +02004506 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4507 return -EINVAL;
4508
Johannes Berg19957bb2009-07-02 17:20:43 +02004509 if (!info->attrs[NL80211_ATTR_SSID])
4510 return -EINVAL;
4511
4512 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4513 return -EINVAL;
4514
Johannes Bergfffd0932009-07-08 14:22:54 +02004515 err = nl80211_parse_key(info, &key);
4516 if (err)
4517 return err;
4518
4519 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004520 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4521 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004522 if (!key.p.key || !key.p.key_len)
4523 return -EINVAL;
4524 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4525 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4526 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4527 key.p.key_len != WLAN_KEY_LEN_WEP104))
4528 return -EINVAL;
4529 if (key.idx > 4)
4530 return -EINVAL;
4531 } else {
4532 key.p.key_len = 0;
4533 key.p.key = NULL;
4534 }
4535
Johannes Bergafea0b72010-08-10 09:46:42 +02004536 if (key.idx >= 0) {
4537 int i;
4538 bool ok = false;
4539 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4540 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4541 ok = true;
4542 break;
4543 }
4544 }
Johannes Berg4c476992010-10-04 21:36:35 +02004545 if (!ok)
4546 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004547 }
4548
Johannes Berg4c476992010-10-04 21:36:35 +02004549 if (!rdev->ops->auth)
4550 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004551
Johannes Berg074ac8d2010-09-16 14:58:22 +02004552 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004553 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4554 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004555
Johannes Berg19957bb2009-07-02 17:20:43 +02004556 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004557 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004558 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004559 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4560 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004561
Johannes Berg19957bb2009-07-02 17:20:43 +02004562 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4563 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4564
4565 if (info->attrs[NL80211_ATTR_IE]) {
4566 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4567 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4568 }
4569
4570 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004571 if (!nl80211_valid_auth_type(auth_type))
4572 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004573
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004574 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4575
Johannes Berg95de8172012-01-20 13:55:25 +01004576 /*
4577 * Since we no longer track auth state, ignore
4578 * requests to only change local state.
4579 */
4580 if (local_state_change)
4581 return 0;
4582
Johannes Berg4c476992010-10-04 21:36:35 +02004583 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4584 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004585 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004586}
4587
Johannes Bergc0692b82010-08-27 14:26:53 +03004588static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4589 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004590 struct cfg80211_crypto_settings *settings,
4591 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004592{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004593 memset(settings, 0, sizeof(*settings));
4594
Samuel Ortizb23aa672009-07-01 21:26:54 +02004595 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4596
Johannes Bergc0692b82010-08-27 14:26:53 +03004597 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4598 u16 proto;
4599 proto = nla_get_u16(
4600 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4601 settings->control_port_ethertype = cpu_to_be16(proto);
4602 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4603 proto != ETH_P_PAE)
4604 return -EINVAL;
4605 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4606 settings->control_port_no_encrypt = true;
4607 } else
4608 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4609
Samuel Ortizb23aa672009-07-01 21:26:54 +02004610 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4611 void *data;
4612 int len, i;
4613
4614 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4615 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4616 settings->n_ciphers_pairwise = len / sizeof(u32);
4617
4618 if (len % sizeof(u32))
4619 return -EINVAL;
4620
Johannes Berg3dc27d22009-07-02 21:36:37 +02004621 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004622 return -EINVAL;
4623
4624 memcpy(settings->ciphers_pairwise, data, len);
4625
4626 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004627 if (!cfg80211_supported_cipher_suite(
4628 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004629 settings->ciphers_pairwise[i]))
4630 return -EINVAL;
4631 }
4632
4633 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4634 settings->cipher_group =
4635 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004636 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4637 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004638 return -EINVAL;
4639 }
4640
4641 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4642 settings->wpa_versions =
4643 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4644 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4645 return -EINVAL;
4646 }
4647
4648 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4649 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004650 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004651
4652 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4653 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4654 settings->n_akm_suites = len / sizeof(u32);
4655
4656 if (len % sizeof(u32))
4657 return -EINVAL;
4658
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004659 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4660 return -EINVAL;
4661
Samuel Ortizb23aa672009-07-01 21:26:54 +02004662 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004663 }
4664
4665 return 0;
4666}
4667
Jouni Malinen636a5d32009-03-19 13:39:22 +02004668static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4669{
Johannes Berg4c476992010-10-04 21:36:35 +02004670 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4671 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004672 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004673 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004674 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004675 int err, ssid_len, ie_len = 0;
4676 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004677 u32 flags = 0;
4678 struct ieee80211_ht_cap *ht_capa = NULL;
4679 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004680
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004681 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4682 return -EINVAL;
4683
4684 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004685 !info->attrs[NL80211_ATTR_SSID] ||
4686 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004687 return -EINVAL;
4688
Johannes Berg4c476992010-10-04 21:36:35 +02004689 if (!rdev->ops->assoc)
4690 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004691
Johannes Berg074ac8d2010-09-16 14:58:22 +02004692 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004693 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4694 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004695
Johannes Berg19957bb2009-07-02 17:20:43 +02004696 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004697
Johannes Berg19957bb2009-07-02 17:20:43 +02004698 chan = ieee80211_get_channel(&rdev->wiphy,
4699 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004700 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4701 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004702
Johannes Berg19957bb2009-07-02 17:20:43 +02004703 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4704 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004705
4706 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004707 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4708 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004709 }
4710
Jouni Malinendc6382c2009-05-06 22:09:37 +03004711 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004712 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004713 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004714 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004715 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004716 else if (mfp != NL80211_MFP_NO)
4717 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004718 }
4719
Johannes Berg3e5d7642009-07-07 14:37:26 +02004720 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4721 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4722
Ben Greear7e7c8922011-11-18 11:31:59 -08004723 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4724 flags |= ASSOC_REQ_DISABLE_HT;
4725
4726 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4727 ht_capa_mask =
4728 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4729
4730 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4731 if (!ht_capa_mask)
4732 return -EINVAL;
4733 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4734 }
4735
Johannes Bergc0692b82010-08-27 14:26:53 +03004736 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004737 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004738 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4739 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004740 &crypto, flags, ht_capa,
4741 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004742
Jouni Malinen636a5d32009-03-19 13:39:22 +02004743 return err;
4744}
4745
4746static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4747{
Johannes Berg4c476992010-10-04 21:36:35 +02004748 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4749 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004750 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004751 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004752 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004753 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004754
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004755 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4756 return -EINVAL;
4757
4758 if (!info->attrs[NL80211_ATTR_MAC])
4759 return -EINVAL;
4760
4761 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4762 return -EINVAL;
4763
Johannes Berg4c476992010-10-04 21:36:35 +02004764 if (!rdev->ops->deauth)
4765 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004766
Johannes Berg074ac8d2010-09-16 14:58:22 +02004767 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004768 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4769 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004770
Johannes Berg19957bb2009-07-02 17:20:43 +02004771 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004772
Johannes Berg19957bb2009-07-02 17:20:43 +02004773 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4774 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004775 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004776 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004777 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004778
4779 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004780 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4781 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004782 }
4783
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004784 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4785
Johannes Berg4c476992010-10-04 21:36:35 +02004786 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4787 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004788}
4789
4790static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4791{
Johannes Berg4c476992010-10-04 21:36:35 +02004792 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4793 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004794 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004795 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004796 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004797 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004798
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004799 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4800 return -EINVAL;
4801
4802 if (!info->attrs[NL80211_ATTR_MAC])
4803 return -EINVAL;
4804
4805 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4806 return -EINVAL;
4807
Johannes Berg4c476992010-10-04 21:36:35 +02004808 if (!rdev->ops->disassoc)
4809 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004810
Johannes Berg074ac8d2010-09-16 14:58:22 +02004811 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004812 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4813 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004814
Johannes Berg19957bb2009-07-02 17:20:43 +02004815 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004816
Johannes Berg19957bb2009-07-02 17:20:43 +02004817 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4818 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004819 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004820 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004821 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004822
4823 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004824 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4825 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004826 }
4827
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004828 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4829
Johannes Berg4c476992010-10-04 21:36:35 +02004830 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4831 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004832}
4833
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004834static bool
4835nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4836 int mcast_rate[IEEE80211_NUM_BANDS],
4837 int rateval)
4838{
4839 struct wiphy *wiphy = &rdev->wiphy;
4840 bool found = false;
4841 int band, i;
4842
4843 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4844 struct ieee80211_supported_band *sband;
4845
4846 sband = wiphy->bands[band];
4847 if (!sband)
4848 continue;
4849
4850 for (i = 0; i < sband->n_bitrates; i++) {
4851 if (sband->bitrates[i].bitrate == rateval) {
4852 mcast_rate[band] = i + 1;
4853 found = true;
4854 break;
4855 }
4856 }
4857 }
4858
4859 return found;
4860}
4861
Johannes Berg04a773a2009-04-19 21:24:32 +02004862static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4863{
Johannes Berg4c476992010-10-04 21:36:35 +02004864 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4865 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004866 struct cfg80211_ibss_params ibss;
4867 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004868 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004869 int err;
4870
Johannes Berg8e30bc52009-04-22 17:45:38 +02004871 memset(&ibss, 0, sizeof(ibss));
4872
Johannes Berg04a773a2009-04-19 21:24:32 +02004873 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4874 return -EINVAL;
4875
4876 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4877 !info->attrs[NL80211_ATTR_SSID] ||
4878 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4879 return -EINVAL;
4880
Johannes Berg8e30bc52009-04-22 17:45:38 +02004881 ibss.beacon_interval = 100;
4882
4883 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4884 ibss.beacon_interval =
4885 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4886 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4887 return -EINVAL;
4888 }
4889
Johannes Berg4c476992010-10-04 21:36:35 +02004890 if (!rdev->ops->join_ibss)
4891 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004892
Johannes Berg4c476992010-10-04 21:36:35 +02004893 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4894 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004895
Johannes Berg79c97e92009-07-07 03:56:12 +02004896 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004897
Johannes Berg39193492011-09-16 13:45:25 +02004898 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004899 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004900
4901 if (!is_valid_ether_addr(ibss.bssid))
4902 return -EINVAL;
4903 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004904 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4905 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4906
4907 if (info->attrs[NL80211_ATTR_IE]) {
4908 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4909 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4910 }
4911
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004912 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4913 enum nl80211_channel_type channel_type;
4914
4915 channel_type = nla_get_u32(
4916 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4917 if (channel_type != NL80211_CHAN_NO_HT &&
4918 channel_type != NL80211_CHAN_HT20 &&
4919 channel_type != NL80211_CHAN_HT40MINUS &&
4920 channel_type != NL80211_CHAN_HT40PLUS)
4921 return -EINVAL;
4922
4923 if (channel_type != NL80211_CHAN_NO_HT &&
4924 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4925 return -EINVAL;
4926
4927 ibss.channel_type = channel_type;
4928 } else {
4929 ibss.channel_type = NL80211_CHAN_NO_HT;
4930 }
4931
4932 ibss.channel = rdev_freq_to_chan(rdev,
4933 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4934 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004935 if (!ibss.channel ||
4936 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004937 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4938 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004939
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004940 /* Both channels should be able to initiate communication */
4941 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4942 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4943 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4944 ibss.channel_type))
4945 return -EINVAL;
4946
Johannes Berg04a773a2009-04-19 21:24:32 +02004947 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004948 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004949
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004950 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4951 u8 *rates =
4952 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4953 int n_rates =
4954 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4955 struct ieee80211_supported_band *sband =
4956 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004957
Johannes Berg34850ab2011-07-18 18:08:35 +02004958 err = ieee80211_get_ratemask(sband, rates, n_rates,
4959 &ibss.basic_rates);
4960 if (err)
4961 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004962 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004963
4964 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4965 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4966 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4967 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004968
Johannes Berg4c476992010-10-04 21:36:35 +02004969 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4970 connkeys = nl80211_parse_connkeys(rdev,
4971 info->attrs[NL80211_ATTR_KEYS]);
4972 if (IS_ERR(connkeys))
4973 return PTR_ERR(connkeys);
4974 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004975
Antonio Quartulli267335d2012-01-31 20:25:47 +01004976 ibss.control_port =
4977 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4978
Johannes Berg4c476992010-10-04 21:36:35 +02004979 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004980 if (err)
4981 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004982 return err;
4983}
4984
4985static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4986{
Johannes Berg4c476992010-10-04 21:36:35 +02004987 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4988 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004989
Johannes Berg4c476992010-10-04 21:36:35 +02004990 if (!rdev->ops->leave_ibss)
4991 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004992
Johannes Berg4c476992010-10-04 21:36:35 +02004993 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4994 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004997}
4998
Johannes Bergaff89a92009-07-01 21:26:51 +02004999#ifdef CONFIG_NL80211_TESTMODE
5000static struct genl_multicast_group nl80211_testmode_mcgrp = {
5001 .name = "testmode",
5002};
5003
5004static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5005{
Johannes Berg4c476992010-10-04 21:36:35 +02005006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005007 int err;
5008
5009 if (!info->attrs[NL80211_ATTR_TESTDATA])
5010 return -EINVAL;
5011
Johannes Bergaff89a92009-07-01 21:26:51 +02005012 err = -EOPNOTSUPP;
5013 if (rdev->ops->testmode_cmd) {
5014 rdev->testmode_info = info;
5015 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5016 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5017 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5018 rdev->testmode_info = NULL;
5019 }
5020
Johannes Bergaff89a92009-07-01 21:26:51 +02005021 return err;
5022}
5023
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005024static int nl80211_testmode_dump(struct sk_buff *skb,
5025 struct netlink_callback *cb)
5026{
Johannes Berg00918d32011-12-13 17:22:05 +01005027 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005028 int err;
5029 long phy_idx;
5030 void *data = NULL;
5031 int data_len = 0;
5032
5033 if (cb->args[0]) {
5034 /*
5035 * 0 is a valid index, but not valid for args[0],
5036 * so we need to offset by 1.
5037 */
5038 phy_idx = cb->args[0] - 1;
5039 } else {
5040 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5041 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5042 nl80211_policy);
5043 if (err)
5044 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005045 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5046 phy_idx = nla_get_u32(
5047 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5048 } else {
5049 struct net_device *netdev;
5050
5051 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5052 nl80211_fam.attrbuf,
5053 &rdev, &netdev);
5054 if (err)
5055 return err;
5056 dev_put(netdev);
5057 phy_idx = rdev->wiphy_idx;
5058 cfg80211_unlock_rdev(rdev);
5059 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005060 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5061 cb->args[1] =
5062 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5063 }
5064
5065 if (cb->args[1]) {
5066 data = nla_data((void *)cb->args[1]);
5067 data_len = nla_len((void *)cb->args[1]);
5068 }
5069
5070 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005071 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5072 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005073 mutex_unlock(&cfg80211_mutex);
5074 return -ENOENT;
5075 }
Johannes Berg00918d32011-12-13 17:22:05 +01005076 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005077 mutex_unlock(&cfg80211_mutex);
5078
Johannes Berg00918d32011-12-13 17:22:05 +01005079 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005080 err = -EOPNOTSUPP;
5081 goto out_err;
5082 }
5083
5084 while (1) {
5085 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5086 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5087 NL80211_CMD_TESTMODE);
5088 struct nlattr *tmdata;
5089
David S. Miller9360ffd2012-03-29 04:41:26 -04005090 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005091 genlmsg_cancel(skb, hdr);
5092 break;
5093 }
5094
5095 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5096 if (!tmdata) {
5097 genlmsg_cancel(skb, hdr);
5098 break;
5099 }
Johannes Berg00918d32011-12-13 17:22:05 +01005100 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5101 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005102 nla_nest_end(skb, tmdata);
5103
5104 if (err == -ENOBUFS || err == -ENOENT) {
5105 genlmsg_cancel(skb, hdr);
5106 break;
5107 } else if (err) {
5108 genlmsg_cancel(skb, hdr);
5109 goto out_err;
5110 }
5111
5112 genlmsg_end(skb, hdr);
5113 }
5114
5115 err = skb->len;
5116 /* see above */
5117 cb->args[0] = phy_idx + 1;
5118 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005119 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005120 return err;
5121}
5122
Johannes Bergaff89a92009-07-01 21:26:51 +02005123static struct sk_buff *
5124__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5125 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5126{
5127 struct sk_buff *skb;
5128 void *hdr;
5129 struct nlattr *data;
5130
5131 skb = nlmsg_new(approxlen + 100, gfp);
5132 if (!skb)
5133 return NULL;
5134
5135 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5136 if (!hdr) {
5137 kfree_skb(skb);
5138 return NULL;
5139 }
5140
David S. Miller9360ffd2012-03-29 04:41:26 -04005141 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5142 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005143 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5144
5145 ((void **)skb->cb)[0] = rdev;
5146 ((void **)skb->cb)[1] = hdr;
5147 ((void **)skb->cb)[2] = data;
5148
5149 return skb;
5150
5151 nla_put_failure:
5152 kfree_skb(skb);
5153 return NULL;
5154}
5155
5156struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5157 int approxlen)
5158{
5159 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5160
5161 if (WARN_ON(!rdev->testmode_info))
5162 return NULL;
5163
5164 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5165 rdev->testmode_info->snd_pid,
5166 rdev->testmode_info->snd_seq,
5167 GFP_KERNEL);
5168}
5169EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5170
5171int cfg80211_testmode_reply(struct sk_buff *skb)
5172{
5173 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5174 void *hdr = ((void **)skb->cb)[1];
5175 struct nlattr *data = ((void **)skb->cb)[2];
5176
5177 if (WARN_ON(!rdev->testmode_info)) {
5178 kfree_skb(skb);
5179 return -EINVAL;
5180 }
5181
5182 nla_nest_end(skb, data);
5183 genlmsg_end(skb, hdr);
5184 return genlmsg_reply(skb, rdev->testmode_info);
5185}
5186EXPORT_SYMBOL(cfg80211_testmode_reply);
5187
5188struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5189 int approxlen, gfp_t gfp)
5190{
5191 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5192
5193 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5194}
5195EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5196
5197void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5198{
5199 void *hdr = ((void **)skb->cb)[1];
5200 struct nlattr *data = ((void **)skb->cb)[2];
5201
5202 nla_nest_end(skb, data);
5203 genlmsg_end(skb, hdr);
5204 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5205}
5206EXPORT_SYMBOL(cfg80211_testmode_event);
5207#endif
5208
Samuel Ortizb23aa672009-07-01 21:26:54 +02005209static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5210{
Johannes Berg4c476992010-10-04 21:36:35 +02005211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5212 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005213 struct cfg80211_connect_params connect;
5214 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005215 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005216 int err;
5217
5218 memset(&connect, 0, sizeof(connect));
5219
5220 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5221 return -EINVAL;
5222
5223 if (!info->attrs[NL80211_ATTR_SSID] ||
5224 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5225 return -EINVAL;
5226
5227 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5228 connect.auth_type =
5229 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5230 if (!nl80211_valid_auth_type(connect.auth_type))
5231 return -EINVAL;
5232 } else
5233 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5234
5235 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5236
Johannes Bergc0692b82010-08-27 14:26:53 +03005237 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005238 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005239 if (err)
5240 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005241
Johannes Berg074ac8d2010-09-16 14:58:22 +02005242 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005243 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5244 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005245
Johannes Berg79c97e92009-07-07 03:56:12 +02005246 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005247
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305248 connect.bg_scan_period = -1;
5249 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5250 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5251 connect.bg_scan_period =
5252 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5253 }
5254
Samuel Ortizb23aa672009-07-01 21:26:54 +02005255 if (info->attrs[NL80211_ATTR_MAC])
5256 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5257 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5258 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5259
5260 if (info->attrs[NL80211_ATTR_IE]) {
5261 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5262 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5263 }
5264
5265 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5266 connect.channel =
5267 ieee80211_get_channel(wiphy,
5268 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5269 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005270 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5271 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005272 }
5273
Johannes Bergfffd0932009-07-08 14:22:54 +02005274 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5275 connkeys = nl80211_parse_connkeys(rdev,
5276 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005277 if (IS_ERR(connkeys))
5278 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005279 }
5280
Ben Greear7e7c8922011-11-18 11:31:59 -08005281 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5282 connect.flags |= ASSOC_REQ_DISABLE_HT;
5283
5284 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5285 memcpy(&connect.ht_capa_mask,
5286 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5287 sizeof(connect.ht_capa_mask));
5288
5289 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5290 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5291 return -EINVAL;
5292 memcpy(&connect.ht_capa,
5293 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5294 sizeof(connect.ht_capa));
5295 }
5296
Johannes Bergfffd0932009-07-08 14:22:54 +02005297 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005298 if (err)
5299 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005300 return err;
5301}
5302
5303static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5304{
Johannes Berg4c476992010-10-04 21:36:35 +02005305 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5306 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005307 u16 reason;
5308
5309 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5310 reason = WLAN_REASON_DEAUTH_LEAVING;
5311 else
5312 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5313
5314 if (reason == 0)
5315 return -EINVAL;
5316
Johannes Berg074ac8d2010-09-16 14:58:22 +02005317 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005318 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5319 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005320
Johannes Berg4c476992010-10-04 21:36:35 +02005321 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005322}
5323
Johannes Berg463d0182009-07-14 00:33:35 +02005324static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5325{
Johannes Berg4c476992010-10-04 21:36:35 +02005326 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005327 struct net *net;
5328 int err;
5329 u32 pid;
5330
5331 if (!info->attrs[NL80211_ATTR_PID])
5332 return -EINVAL;
5333
5334 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5335
Johannes Berg463d0182009-07-14 00:33:35 +02005336 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005337 if (IS_ERR(net))
5338 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005339
5340 err = 0;
5341
5342 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005343 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5344 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005345
Johannes Berg463d0182009-07-14 00:33:35 +02005346 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005347 return err;
5348}
5349
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005350static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5351{
Johannes Berg4c476992010-10-04 21:36:35 +02005352 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005353 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5354 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005355 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005356 struct cfg80211_pmksa pmksa;
5357
5358 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5359
5360 if (!info->attrs[NL80211_ATTR_MAC])
5361 return -EINVAL;
5362
5363 if (!info->attrs[NL80211_ATTR_PMKID])
5364 return -EINVAL;
5365
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005366 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5367 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5368
Johannes Berg074ac8d2010-09-16 14:58:22 +02005369 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005370 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5371 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005372
5373 switch (info->genlhdr->cmd) {
5374 case NL80211_CMD_SET_PMKSA:
5375 rdev_ops = rdev->ops->set_pmksa;
5376 break;
5377 case NL80211_CMD_DEL_PMKSA:
5378 rdev_ops = rdev->ops->del_pmksa;
5379 break;
5380 default:
5381 WARN_ON(1);
5382 break;
5383 }
5384
Johannes Berg4c476992010-10-04 21:36:35 +02005385 if (!rdev_ops)
5386 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005387
Johannes Berg4c476992010-10-04 21:36:35 +02005388 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005389}
5390
5391static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5392{
Johannes Berg4c476992010-10-04 21:36:35 +02005393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5394 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005395
Johannes Berg074ac8d2010-09-16 14:58:22 +02005396 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005397 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5398 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005399
Johannes Berg4c476992010-10-04 21:36:35 +02005400 if (!rdev->ops->flush_pmksa)
5401 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005402
Johannes Berg4c476992010-10-04 21:36:35 +02005403 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005404}
5405
Arik Nemtsov109086c2011-09-28 14:12:50 +03005406static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5407{
5408 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5409 struct net_device *dev = info->user_ptr[1];
5410 u8 action_code, dialog_token;
5411 u16 status_code;
5412 u8 *peer;
5413
5414 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5415 !rdev->ops->tdls_mgmt)
5416 return -EOPNOTSUPP;
5417
5418 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5419 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5420 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5421 !info->attrs[NL80211_ATTR_IE] ||
5422 !info->attrs[NL80211_ATTR_MAC])
5423 return -EINVAL;
5424
5425 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5426 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5427 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5428 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5429
5430 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5431 dialog_token, status_code,
5432 nla_data(info->attrs[NL80211_ATTR_IE]),
5433 nla_len(info->attrs[NL80211_ATTR_IE]));
5434}
5435
5436static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5437{
5438 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5439 struct net_device *dev = info->user_ptr[1];
5440 enum nl80211_tdls_operation operation;
5441 u8 *peer;
5442
5443 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5444 !rdev->ops->tdls_oper)
5445 return -EOPNOTSUPP;
5446
5447 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5448 !info->attrs[NL80211_ATTR_MAC])
5449 return -EINVAL;
5450
5451 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5452 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5453
5454 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5455}
5456
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005457static int nl80211_remain_on_channel(struct sk_buff *skb,
5458 struct genl_info *info)
5459{
Johannes Berg4c476992010-10-04 21:36:35 +02005460 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5461 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005462 struct ieee80211_channel *chan;
5463 struct sk_buff *msg;
5464 void *hdr;
5465 u64 cookie;
5466 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5467 u32 freq, duration;
5468 int err;
5469
5470 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5471 !info->attrs[NL80211_ATTR_DURATION])
5472 return -EINVAL;
5473
5474 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5475
5476 /*
5477 * We should be on that channel for at least one jiffie,
5478 * and more than 5 seconds seems excessive.
5479 */
Johannes Berga2939112010-12-14 17:54:28 +01005480 if (!duration || !msecs_to_jiffies(duration) ||
5481 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005482 return -EINVAL;
5483
Johannes Berg7c4ef712011-11-18 15:33:48 +01005484 if (!rdev->ops->remain_on_channel ||
5485 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005486 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005487
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005488 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5489 channel_type = nla_get_u32(
5490 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5491 if (channel_type != NL80211_CHAN_NO_HT &&
5492 channel_type != NL80211_CHAN_HT20 &&
5493 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005494 channel_type != NL80211_CHAN_HT40MINUS)
5495 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005496 }
5497
5498 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5499 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005500 if (chan == NULL)
5501 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005502
5503 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005504 if (!msg)
5505 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005506
5507 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5508 NL80211_CMD_REMAIN_ON_CHANNEL);
5509
5510 if (IS_ERR(hdr)) {
5511 err = PTR_ERR(hdr);
5512 goto free_msg;
5513 }
5514
5515 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5516 channel_type, duration, &cookie);
5517
5518 if (err)
5519 goto free_msg;
5520
David S. Miller9360ffd2012-03-29 04:41:26 -04005521 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5522 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005523
5524 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005525
5526 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005527
5528 nla_put_failure:
5529 err = -ENOBUFS;
5530 free_msg:
5531 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005532 return err;
5533}
5534
5535static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5536 struct genl_info *info)
5537{
Johannes Berg4c476992010-10-04 21:36:35 +02005538 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5539 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005540 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005541
5542 if (!info->attrs[NL80211_ATTR_COOKIE])
5543 return -EINVAL;
5544
Johannes Berg4c476992010-10-04 21:36:35 +02005545 if (!rdev->ops->cancel_remain_on_channel)
5546 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005547
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005548 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5549
Johannes Berg4c476992010-10-04 21:36:35 +02005550 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005551}
5552
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005553static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5554 u8 *rates, u8 rates_len)
5555{
5556 u8 i;
5557 u32 mask = 0;
5558
5559 for (i = 0; i < rates_len; i++) {
5560 int rate = (rates[i] & 0x7f) * 5;
5561 int ridx;
5562 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5563 struct ieee80211_rate *srate =
5564 &sband->bitrates[ridx];
5565 if (rate == srate->bitrate) {
5566 mask |= 1 << ridx;
5567 break;
5568 }
5569 }
5570 if (ridx == sband->n_bitrates)
5571 return 0; /* rate not found */
5572 }
5573
5574 return mask;
5575}
5576
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005577static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5578 u8 *rates, u8 rates_len,
5579 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5580{
5581 u8 i;
5582
5583 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5584
5585 for (i = 0; i < rates_len; i++) {
5586 int ridx, rbit;
5587
5588 ridx = rates[i] / 8;
5589 rbit = BIT(rates[i] % 8);
5590
5591 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005592 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005593 return false;
5594
5595 /* check availability */
5596 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5597 mcs[ridx] |= rbit;
5598 else
5599 return false;
5600 }
5601
5602 return true;
5603}
5604
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005605static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005606 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5607 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005608 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5609 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005610};
5611
5612static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5613 struct genl_info *info)
5614{
5615 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005616 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005617 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005618 int rem, i;
5619 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005620 struct nlattr *tx_rates;
5621 struct ieee80211_supported_band *sband;
5622
5623 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5624 return -EINVAL;
5625
Johannes Berg4c476992010-10-04 21:36:35 +02005626 if (!rdev->ops->set_bitrate_mask)
5627 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005628
5629 memset(&mask, 0, sizeof(mask));
5630 /* Default to all rates enabled */
5631 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5632 sband = rdev->wiphy.bands[i];
5633 mask.control[i].legacy =
5634 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005635 if (sband)
5636 memcpy(mask.control[i].mcs,
5637 sband->ht_cap.mcs.rx_mask,
5638 sizeof(mask.control[i].mcs));
5639 else
5640 memset(mask.control[i].mcs, 0,
5641 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005642 }
5643
5644 /*
5645 * The nested attribute uses enum nl80211_band as the index. This maps
5646 * directly to the enum ieee80211_band values used in cfg80211.
5647 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005648 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005649 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5650 {
5651 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005652 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5653 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005654 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005655 if (sband == NULL)
5656 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005657 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5658 nla_len(tx_rates), nl80211_txattr_policy);
5659 if (tb[NL80211_TXRATE_LEGACY]) {
5660 mask.control[band].legacy = rateset_to_mask(
5661 sband,
5662 nla_data(tb[NL80211_TXRATE_LEGACY]),
5663 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305664 if ((mask.control[band].legacy == 0) &&
5665 nla_len(tb[NL80211_TXRATE_LEGACY]))
5666 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005667 }
5668 if (tb[NL80211_TXRATE_MCS]) {
5669 if (!ht_rateset_to_mask(
5670 sband,
5671 nla_data(tb[NL80211_TXRATE_MCS]),
5672 nla_len(tb[NL80211_TXRATE_MCS]),
5673 mask.control[band].mcs))
5674 return -EINVAL;
5675 }
5676
5677 if (mask.control[band].legacy == 0) {
5678 /* don't allow empty legacy rates if HT
5679 * is not even supported. */
5680 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5681 return -EINVAL;
5682
5683 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5684 if (mask.control[band].mcs[i])
5685 break;
5686
5687 /* legacy and mcs rates may not be both empty */
5688 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005689 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005690 }
5691 }
5692
Johannes Berg4c476992010-10-04 21:36:35 +02005693 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005694}
5695
Johannes Berg2e161f72010-08-12 15:38:38 +02005696static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005697{
Johannes Berg4c476992010-10-04 21:36:35 +02005698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5699 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005700 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005701
5702 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5703 return -EINVAL;
5704
Johannes Berg2e161f72010-08-12 15:38:38 +02005705 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5706 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005707
Johannes Berg9d38d852010-06-09 17:20:33 +02005708 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005709 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005710 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5711 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5712 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005713 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005714 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5715 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005716
5717 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005718 if (!rdev->ops->mgmt_tx)
5719 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005720
Johannes Berg4c476992010-10-04 21:36:35 +02005721 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005722 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005723 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5724 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005725}
5726
Johannes Berg2e161f72010-08-12 15:38:38 +02005727static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005728{
Johannes Berg4c476992010-10-04 21:36:35 +02005729 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5730 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005731 struct ieee80211_channel *chan;
5732 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005733 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005734 u32 freq;
5735 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005736 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005737 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005738 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005739 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005740 bool offchan, no_cck, dont_wait_for_ack;
5741
5742 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005743
5744 if (!info->attrs[NL80211_ATTR_FRAME] ||
5745 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5746 return -EINVAL;
5747
Johannes Berg4c476992010-10-04 21:36:35 +02005748 if (!rdev->ops->mgmt_tx)
5749 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005750
Johannes Berg9d38d852010-06-09 17:20:33 +02005751 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005752 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005753 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5754 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5755 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005756 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005757 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5758 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005759
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005760 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005761 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005762 return -EINVAL;
5763 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5764 }
5765
Jouni Malinen026331c2010-02-15 12:53:10 +02005766 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5767 channel_type = nla_get_u32(
5768 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5769 if (channel_type != NL80211_CHAN_NO_HT &&
5770 channel_type != NL80211_CHAN_HT20 &&
5771 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005772 channel_type != NL80211_CHAN_HT40MINUS)
5773 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005774 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005775 }
5776
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005777 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5778
Johannes Berg7c4ef712011-11-18 15:33:48 +01005779 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5780 return -EINVAL;
5781
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305782 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5783
Jouni Malinen026331c2010-02-15 12:53:10 +02005784 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5785 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005786 if (chan == NULL)
5787 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005788
Johannes Berge247bd902011-11-04 11:18:21 +01005789 if (!dont_wait_for_ack) {
5790 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5791 if (!msg)
5792 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005793
Johannes Berge247bd902011-11-04 11:18:21 +01005794 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5795 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005796
Johannes Berge247bd902011-11-04 11:18:21 +01005797 if (IS_ERR(hdr)) {
5798 err = PTR_ERR(hdr);
5799 goto free_msg;
5800 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005801 }
Johannes Berge247bd902011-11-04 11:18:21 +01005802
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005803 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5804 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005805 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5806 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005807 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005808 if (err)
5809 goto free_msg;
5810
Johannes Berge247bd902011-11-04 11:18:21 +01005811 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005812 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5813 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005814
Johannes Berge247bd902011-11-04 11:18:21 +01005815 genlmsg_end(msg, hdr);
5816 return genlmsg_reply(msg, info);
5817 }
5818
5819 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005820
5821 nla_put_failure:
5822 err = -ENOBUFS;
5823 free_msg:
5824 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005825 return err;
5826}
5827
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005828static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5829{
5830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5831 struct net_device *dev = info->user_ptr[1];
5832 u64 cookie;
5833
5834 if (!info->attrs[NL80211_ATTR_COOKIE])
5835 return -EINVAL;
5836
5837 if (!rdev->ops->mgmt_tx_cancel_wait)
5838 return -EOPNOTSUPP;
5839
5840 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5841 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5842 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5843 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5844 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5845 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5846 return -EOPNOTSUPP;
5847
5848 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5849
5850 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5851}
5852
Kalle Valoffb9eb32010-02-17 17:58:10 +02005853static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5854{
Johannes Berg4c476992010-10-04 21:36:35 +02005855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005856 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005857 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005858 u8 ps_state;
5859 bool state;
5860 int err;
5861
Johannes Berg4c476992010-10-04 21:36:35 +02005862 if (!info->attrs[NL80211_ATTR_PS_STATE])
5863 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005864
5865 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5866
Johannes Berg4c476992010-10-04 21:36:35 +02005867 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5868 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005869
5870 wdev = dev->ieee80211_ptr;
5871
Johannes Berg4c476992010-10-04 21:36:35 +02005872 if (!rdev->ops->set_power_mgmt)
5873 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005874
5875 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5876
5877 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005878 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005879
Johannes Berg4c476992010-10-04 21:36:35 +02005880 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5881 wdev->ps_timeout);
5882 if (!err)
5883 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005884 return err;
5885}
5886
5887static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5888{
Johannes Berg4c476992010-10-04 21:36:35 +02005889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005890 enum nl80211_ps_state ps_state;
5891 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005892 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005893 struct sk_buff *msg;
5894 void *hdr;
5895 int err;
5896
Kalle Valoffb9eb32010-02-17 17:58:10 +02005897 wdev = dev->ieee80211_ptr;
5898
Johannes Berg4c476992010-10-04 21:36:35 +02005899 if (!rdev->ops->set_power_mgmt)
5900 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005901
5902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005903 if (!msg)
5904 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005905
5906 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5907 NL80211_CMD_GET_POWER_SAVE);
5908 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005909 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005910 goto free_msg;
5911 }
5912
5913 if (wdev->ps)
5914 ps_state = NL80211_PS_ENABLED;
5915 else
5916 ps_state = NL80211_PS_DISABLED;
5917
David S. Miller9360ffd2012-03-29 04:41:26 -04005918 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5919 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005920
5921 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005922 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005923
Johannes Berg4c476992010-10-04 21:36:35 +02005924 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005925 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005926 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005927 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005928 return err;
5929}
5930
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005931static struct nla_policy
5932nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5933 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5934 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5935 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5936};
5937
5938static int nl80211_set_cqm_rssi(struct genl_info *info,
5939 s32 threshold, u32 hysteresis)
5940{
Johannes Berg4c476992010-10-04 21:36:35 +02005941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005942 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005943 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005944
5945 if (threshold > 0)
5946 return -EINVAL;
5947
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005948 wdev = dev->ieee80211_ptr;
5949
Johannes Berg4c476992010-10-04 21:36:35 +02005950 if (!rdev->ops->set_cqm_rssi_config)
5951 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005952
Johannes Berg074ac8d2010-09-16 14:58:22 +02005953 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005954 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5955 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005956
Johannes Berg4c476992010-10-04 21:36:35 +02005957 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5958 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005959}
5960
5961static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5962{
5963 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5964 struct nlattr *cqm;
5965 int err;
5966
5967 cqm = info->attrs[NL80211_ATTR_CQM];
5968 if (!cqm) {
5969 err = -EINVAL;
5970 goto out;
5971 }
5972
5973 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5974 nl80211_attr_cqm_policy);
5975 if (err)
5976 goto out;
5977
5978 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5979 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5980 s32 threshold;
5981 u32 hysteresis;
5982 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5983 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5984 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5985 } else
5986 err = -EINVAL;
5987
5988out:
5989 return err;
5990}
5991
Johannes Berg29cbe682010-12-03 09:20:44 +01005992static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5993{
5994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5995 struct net_device *dev = info->user_ptr[1];
5996 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005997 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005998 int err;
5999
6000 /* start with default */
6001 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006002 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006003
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006004 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006005 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006006 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006007 if (err)
6008 return err;
6009 }
6010
6011 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6012 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6013 return -EINVAL;
6014
Javier Cardonac80d5452010-12-16 17:37:49 -08006015 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6016 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6017
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006018 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6019 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6020 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6021 return -EINVAL;
6022
Javier Cardonac80d5452010-12-16 17:37:49 -08006023 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6024 /* parse additional setup parameters if given */
6025 err = nl80211_parse_mesh_setup(info, &setup);
6026 if (err)
6027 return err;
6028 }
6029
6030 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006031}
6032
6033static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6034{
6035 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6036 struct net_device *dev = info->user_ptr[1];
6037
6038 return cfg80211_leave_mesh(rdev, dev);
6039}
6040
Johannes Bergff1b6e62011-05-04 15:37:28 +02006041static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6042{
6043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6044 struct sk_buff *msg;
6045 void *hdr;
6046
6047 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6048 return -EOPNOTSUPP;
6049
6050 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6051 if (!msg)
6052 return -ENOMEM;
6053
6054 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6055 NL80211_CMD_GET_WOWLAN);
6056 if (!hdr)
6057 goto nla_put_failure;
6058
6059 if (rdev->wowlan) {
6060 struct nlattr *nl_wowlan;
6061
6062 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6063 if (!nl_wowlan)
6064 goto nla_put_failure;
6065
David S. Miller9360ffd2012-03-29 04:41:26 -04006066 if ((rdev->wowlan->any &&
6067 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6068 (rdev->wowlan->disconnect &&
6069 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6070 (rdev->wowlan->magic_pkt &&
6071 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6072 (rdev->wowlan->gtk_rekey_failure &&
6073 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6074 (rdev->wowlan->eap_identity_req &&
6075 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6076 (rdev->wowlan->four_way_handshake &&
6077 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6078 (rdev->wowlan->rfkill_release &&
6079 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6080 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006081 if (rdev->wowlan->n_patterns) {
6082 struct nlattr *nl_pats, *nl_pat;
6083 int i, pat_len;
6084
6085 nl_pats = nla_nest_start(msg,
6086 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6087 if (!nl_pats)
6088 goto nla_put_failure;
6089
6090 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6091 nl_pat = nla_nest_start(msg, i + 1);
6092 if (!nl_pat)
6093 goto nla_put_failure;
6094 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006095 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6096 DIV_ROUND_UP(pat_len, 8),
6097 rdev->wowlan->patterns[i].mask) ||
6098 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6099 pat_len,
6100 rdev->wowlan->patterns[i].pattern))
6101 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006102 nla_nest_end(msg, nl_pat);
6103 }
6104 nla_nest_end(msg, nl_pats);
6105 }
6106
6107 nla_nest_end(msg, nl_wowlan);
6108 }
6109
6110 genlmsg_end(msg, hdr);
6111 return genlmsg_reply(msg, info);
6112
6113nla_put_failure:
6114 nlmsg_free(msg);
6115 return -ENOBUFS;
6116}
6117
6118static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6119{
6120 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6121 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6122 struct cfg80211_wowlan no_triggers = {};
6123 struct cfg80211_wowlan new_triggers = {};
6124 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6125 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006126 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006127
6128 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6129 return -EOPNOTSUPP;
6130
6131 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6132 goto no_triggers;
6133
6134 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6135 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6136 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6137 nl80211_wowlan_policy);
6138 if (err)
6139 return err;
6140
6141 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6142 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6143 return -EINVAL;
6144 new_triggers.any = true;
6145 }
6146
6147 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6148 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6149 return -EINVAL;
6150 new_triggers.disconnect = true;
6151 }
6152
6153 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6154 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6155 return -EINVAL;
6156 new_triggers.magic_pkt = true;
6157 }
6158
Johannes Berg77dbbb12011-07-13 10:48:55 +02006159 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6160 return -EINVAL;
6161
6162 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6163 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6164 return -EINVAL;
6165 new_triggers.gtk_rekey_failure = true;
6166 }
6167
6168 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6169 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6170 return -EINVAL;
6171 new_triggers.eap_identity_req = true;
6172 }
6173
6174 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6175 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6176 return -EINVAL;
6177 new_triggers.four_way_handshake = true;
6178 }
6179
6180 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6181 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6182 return -EINVAL;
6183 new_triggers.rfkill_release = true;
6184 }
6185
Johannes Bergff1b6e62011-05-04 15:37:28 +02006186 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6187 struct nlattr *pat;
6188 int n_patterns = 0;
6189 int rem, pat_len, mask_len;
6190 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6191
6192 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6193 rem)
6194 n_patterns++;
6195 if (n_patterns > wowlan->n_patterns)
6196 return -EINVAL;
6197
6198 new_triggers.patterns = kcalloc(n_patterns,
6199 sizeof(new_triggers.patterns[0]),
6200 GFP_KERNEL);
6201 if (!new_triggers.patterns)
6202 return -ENOMEM;
6203
6204 new_triggers.n_patterns = n_patterns;
6205 i = 0;
6206
6207 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6208 rem) {
6209 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6210 nla_data(pat), nla_len(pat), NULL);
6211 err = -EINVAL;
6212 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6213 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6214 goto error;
6215 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6216 mask_len = DIV_ROUND_UP(pat_len, 8);
6217 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6218 mask_len)
6219 goto error;
6220 if (pat_len > wowlan->pattern_max_len ||
6221 pat_len < wowlan->pattern_min_len)
6222 goto error;
6223
6224 new_triggers.patterns[i].mask =
6225 kmalloc(mask_len + pat_len, GFP_KERNEL);
6226 if (!new_triggers.patterns[i].mask) {
6227 err = -ENOMEM;
6228 goto error;
6229 }
6230 new_triggers.patterns[i].pattern =
6231 new_triggers.patterns[i].mask + mask_len;
6232 memcpy(new_triggers.patterns[i].mask,
6233 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6234 mask_len);
6235 new_triggers.patterns[i].pattern_len = pat_len;
6236 memcpy(new_triggers.patterns[i].pattern,
6237 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6238 pat_len);
6239 i++;
6240 }
6241 }
6242
6243 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6244 struct cfg80211_wowlan *ntrig;
6245 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6246 GFP_KERNEL);
6247 if (!ntrig) {
6248 err = -ENOMEM;
6249 goto error;
6250 }
6251 cfg80211_rdev_free_wowlan(rdev);
6252 rdev->wowlan = ntrig;
6253 } else {
6254 no_triggers:
6255 cfg80211_rdev_free_wowlan(rdev);
6256 rdev->wowlan = NULL;
6257 }
6258
Johannes Berg6d525632012-04-04 15:05:25 +02006259 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6260 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6261
Johannes Bergff1b6e62011-05-04 15:37:28 +02006262 return 0;
6263 error:
6264 for (i = 0; i < new_triggers.n_patterns; i++)
6265 kfree(new_triggers.patterns[i].mask);
6266 kfree(new_triggers.patterns);
6267 return err;
6268}
6269
Johannes Berge5497d72011-07-05 16:35:40 +02006270static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6271{
6272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6273 struct net_device *dev = info->user_ptr[1];
6274 struct wireless_dev *wdev = dev->ieee80211_ptr;
6275 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6276 struct cfg80211_gtk_rekey_data rekey_data;
6277 int err;
6278
6279 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6280 return -EINVAL;
6281
6282 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6283 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6284 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6285 nl80211_rekey_policy);
6286 if (err)
6287 return err;
6288
6289 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6290 return -ERANGE;
6291 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6292 return -ERANGE;
6293 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6294 return -ERANGE;
6295
6296 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6297 NL80211_KEK_LEN);
6298 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6299 NL80211_KCK_LEN);
6300 memcpy(rekey_data.replay_ctr,
6301 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6302 NL80211_REPLAY_CTR_LEN);
6303
6304 wdev_lock(wdev);
6305 if (!wdev->current_bss) {
6306 err = -ENOTCONN;
6307 goto out;
6308 }
6309
6310 if (!rdev->ops->set_rekey_data) {
6311 err = -EOPNOTSUPP;
6312 goto out;
6313 }
6314
6315 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6316 out:
6317 wdev_unlock(wdev);
6318 return err;
6319}
6320
Johannes Berg28946da2011-11-04 11:18:12 +01006321static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6322 struct genl_info *info)
6323{
6324 struct net_device *dev = info->user_ptr[1];
6325 struct wireless_dev *wdev = dev->ieee80211_ptr;
6326
6327 if (wdev->iftype != NL80211_IFTYPE_AP &&
6328 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6329 return -EINVAL;
6330
6331 if (wdev->ap_unexpected_nlpid)
6332 return -EBUSY;
6333
6334 wdev->ap_unexpected_nlpid = info->snd_pid;
6335 return 0;
6336}
6337
Johannes Berg7f6cf312011-11-04 11:18:15 +01006338static int nl80211_probe_client(struct sk_buff *skb,
6339 struct genl_info *info)
6340{
6341 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6342 struct net_device *dev = info->user_ptr[1];
6343 struct wireless_dev *wdev = dev->ieee80211_ptr;
6344 struct sk_buff *msg;
6345 void *hdr;
6346 const u8 *addr;
6347 u64 cookie;
6348 int err;
6349
6350 if (wdev->iftype != NL80211_IFTYPE_AP &&
6351 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6352 return -EOPNOTSUPP;
6353
6354 if (!info->attrs[NL80211_ATTR_MAC])
6355 return -EINVAL;
6356
6357 if (!rdev->ops->probe_client)
6358 return -EOPNOTSUPP;
6359
6360 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6361 if (!msg)
6362 return -ENOMEM;
6363
6364 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6365 NL80211_CMD_PROBE_CLIENT);
6366
6367 if (IS_ERR(hdr)) {
6368 err = PTR_ERR(hdr);
6369 goto free_msg;
6370 }
6371
6372 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6373
6374 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6375 if (err)
6376 goto free_msg;
6377
David S. Miller9360ffd2012-03-29 04:41:26 -04006378 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6379 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006380
6381 genlmsg_end(msg, hdr);
6382
6383 return genlmsg_reply(msg, info);
6384
6385 nla_put_failure:
6386 err = -ENOBUFS;
6387 free_msg:
6388 nlmsg_free(msg);
6389 return err;
6390}
6391
Johannes Berg5e760232011-11-04 11:18:17 +01006392static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6393{
6394 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6395
6396 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6397 return -EOPNOTSUPP;
6398
6399 if (rdev->ap_beacons_nlpid)
6400 return -EBUSY;
6401
6402 rdev->ap_beacons_nlpid = info->snd_pid;
6403
6404 return 0;
6405}
6406
Johannes Berg4c476992010-10-04 21:36:35 +02006407#define NL80211_FLAG_NEED_WIPHY 0x01
6408#define NL80211_FLAG_NEED_NETDEV 0x02
6409#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006410#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6411#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6412 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006413
6414static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6415 struct genl_info *info)
6416{
6417 struct cfg80211_registered_device *rdev;
6418 struct net_device *dev;
6419 int err;
6420 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6421
6422 if (rtnl)
6423 rtnl_lock();
6424
6425 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6426 rdev = cfg80211_get_dev_from_info(info);
6427 if (IS_ERR(rdev)) {
6428 if (rtnl)
6429 rtnl_unlock();
6430 return PTR_ERR(rdev);
6431 }
6432 info->user_ptr[0] = rdev;
6433 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006434 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6435 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006436 if (err) {
6437 if (rtnl)
6438 rtnl_unlock();
6439 return err;
6440 }
Johannes Berg41265712010-10-04 21:14:05 +02006441 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6442 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006443 cfg80211_unlock_rdev(rdev);
6444 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006445 if (rtnl)
6446 rtnl_unlock();
6447 return -ENETDOWN;
6448 }
Johannes Berg4c476992010-10-04 21:36:35 +02006449 info->user_ptr[0] = rdev;
6450 info->user_ptr[1] = dev;
6451 }
6452
6453 return 0;
6454}
6455
6456static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6457 struct genl_info *info)
6458{
6459 if (info->user_ptr[0])
6460 cfg80211_unlock_rdev(info->user_ptr[0]);
6461 if (info->user_ptr[1])
6462 dev_put(info->user_ptr[1]);
6463 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6464 rtnl_unlock();
6465}
6466
Johannes Berg55682962007-09-20 13:09:35 -04006467static struct genl_ops nl80211_ops[] = {
6468 {
6469 .cmd = NL80211_CMD_GET_WIPHY,
6470 .doit = nl80211_get_wiphy,
6471 .dumpit = nl80211_dump_wiphy,
6472 .policy = nl80211_policy,
6473 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006474 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006475 },
6476 {
6477 .cmd = NL80211_CMD_SET_WIPHY,
6478 .doit = nl80211_set_wiphy,
6479 .policy = nl80211_policy,
6480 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006481 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006482 },
6483 {
6484 .cmd = NL80211_CMD_GET_INTERFACE,
6485 .doit = nl80211_get_interface,
6486 .dumpit = nl80211_dump_interface,
6487 .policy = nl80211_policy,
6488 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006489 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006490 },
6491 {
6492 .cmd = NL80211_CMD_SET_INTERFACE,
6493 .doit = nl80211_set_interface,
6494 .policy = nl80211_policy,
6495 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006496 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6497 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006498 },
6499 {
6500 .cmd = NL80211_CMD_NEW_INTERFACE,
6501 .doit = nl80211_new_interface,
6502 .policy = nl80211_policy,
6503 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006504 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6505 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006506 },
6507 {
6508 .cmd = NL80211_CMD_DEL_INTERFACE,
6509 .doit = nl80211_del_interface,
6510 .policy = nl80211_policy,
6511 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006512 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6513 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006514 },
Johannes Berg41ade002007-12-19 02:03:29 +01006515 {
6516 .cmd = NL80211_CMD_GET_KEY,
6517 .doit = nl80211_get_key,
6518 .policy = nl80211_policy,
6519 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +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_SET_KEY,
6525 .doit = nl80211_set_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_NEW_KEY,
6533 .doit = nl80211_new_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 },
6539 {
6540 .cmd = NL80211_CMD_DEL_KEY,
6541 .doit = nl80211_del_key,
6542 .policy = nl80211_policy,
6543 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006544 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006545 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006546 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006547 {
6548 .cmd = NL80211_CMD_SET_BEACON,
6549 .policy = nl80211_policy,
6550 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006551 .doit = nl80211_set_beacon,
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_START_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_start_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 },
6563 {
Johannes Berg88600202012-02-13 15:17:18 +01006564 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006565 .policy = nl80211_policy,
6566 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006567 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006568 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006569 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006570 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006571 {
6572 .cmd = NL80211_CMD_GET_STATION,
6573 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006574 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006575 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006576 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6577 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006578 },
6579 {
6580 .cmd = NL80211_CMD_SET_STATION,
6581 .doit = nl80211_set_station,
6582 .policy = nl80211_policy,
6583 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +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_NEW_STATION,
6589 .doit = nl80211_new_station,
6590 .policy = nl80211_policy,
6591 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +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 },
6595 {
6596 .cmd = NL80211_CMD_DEL_STATION,
6597 .doit = nl80211_del_station,
6598 .policy = nl80211_policy,
6599 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006600 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006601 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006602 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006603 {
6604 .cmd = NL80211_CMD_GET_MPATH,
6605 .doit = nl80211_get_mpath,
6606 .dumpit = nl80211_dump_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_SET_MPATH,
6614 .doit = nl80211_set_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_NEW_MPATH,
6622 .doit = nl80211_new_mpath,
6623 .policy = nl80211_policy,
6624 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +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 },
6628 {
6629 .cmd = NL80211_CMD_DEL_MPATH,
6630 .doit = nl80211_del_mpath,
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,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006635 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006636 {
6637 .cmd = NL80211_CMD_SET_BSS,
6638 .doit = nl80211_set_bss,
6639 .policy = nl80211_policy,
6640 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006641 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006642 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006643 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006644 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006645 .cmd = NL80211_CMD_GET_REG,
6646 .doit = nl80211_get_reg,
6647 .policy = nl80211_policy,
6648 /* can be retrieved by unprivileged users */
6649 },
6650 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006651 .cmd = NL80211_CMD_SET_REG,
6652 .doit = nl80211_set_reg,
6653 .policy = nl80211_policy,
6654 .flags = GENL_ADMIN_PERM,
6655 },
6656 {
6657 .cmd = NL80211_CMD_REQ_SET_REG,
6658 .doit = nl80211_req_set_reg,
6659 .policy = nl80211_policy,
6660 .flags = GENL_ADMIN_PERM,
6661 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006662 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006663 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6664 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006665 .policy = nl80211_policy,
6666 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006667 .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 },
6670 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006671 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6672 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006673 .policy = nl80211_policy,
6674 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006675 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006676 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006677 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006678 {
Johannes Berg2a519312009-02-10 21:25:55 +01006679 .cmd = NL80211_CMD_TRIGGER_SCAN,
6680 .doit = nl80211_trigger_scan,
6681 .policy = nl80211_policy,
6682 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006683 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006684 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006685 },
6686 {
6687 .cmd = NL80211_CMD_GET_SCAN,
6688 .policy = nl80211_policy,
6689 .dumpit = nl80211_dump_scan,
6690 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006691 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006692 .cmd = NL80211_CMD_START_SCHED_SCAN,
6693 .doit = nl80211_start_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 {
6700 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6701 .doit = nl80211_stop_sched_scan,
6702 .policy = nl80211_policy,
6703 .flags = GENL_ADMIN_PERM,
6704 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6705 NL80211_FLAG_NEED_RTNL,
6706 },
6707 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006708 .cmd = NL80211_CMD_AUTHENTICATE,
6709 .doit = nl80211_authenticate,
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_ASSOCIATE,
6717 .doit = nl80211_associate,
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_DEAUTHENTICATE,
6725 .doit = nl80211_deauthenticate,
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 },
6731 {
6732 .cmd = NL80211_CMD_DISASSOCIATE,
6733 .doit = nl80211_disassociate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006738 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006739 {
6740 .cmd = NL80211_CMD_JOIN_IBSS,
6741 .doit = nl80211_join_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 },
6747 {
6748 .cmd = NL80211_CMD_LEAVE_IBSS,
6749 .doit = nl80211_leave_ibss,
6750 .policy = nl80211_policy,
6751 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006752 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006753 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006754 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006755#ifdef CONFIG_NL80211_TESTMODE
6756 {
6757 .cmd = NL80211_CMD_TESTMODE,
6758 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006759 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006760 .policy = nl80211_policy,
6761 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006762 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6763 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006764 },
6765#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006766 {
6767 .cmd = NL80211_CMD_CONNECT,
6768 .doit = nl80211_connect,
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 },
6774 {
6775 .cmd = NL80211_CMD_DISCONNECT,
6776 .doit = nl80211_disconnect,
6777 .policy = nl80211_policy,
6778 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006779 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006780 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006781 },
Johannes Berg463d0182009-07-14 00:33:35 +02006782 {
6783 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6784 .doit = nl80211_wiphy_netns,
6785 .policy = nl80211_policy,
6786 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006787 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6788 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006789 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006790 {
6791 .cmd = NL80211_CMD_GET_SURVEY,
6792 .policy = nl80211_policy,
6793 .dumpit = nl80211_dump_survey,
6794 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006795 {
6796 .cmd = NL80211_CMD_SET_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_DEL_PMKSA,
6805 .doit = nl80211_setdel_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 },
6811 {
6812 .cmd = NL80211_CMD_FLUSH_PMKSA,
6813 .doit = nl80211_flush_pmksa,
6814 .policy = nl80211_policy,
6815 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006816 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006817 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006818 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006819 {
6820 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6821 .doit = nl80211_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 },
6827 {
6828 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6829 .doit = nl80211_cancel_remain_on_channel,
6830 .policy = nl80211_policy,
6831 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006832 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006833 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006834 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006835 {
6836 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6837 .doit = nl80211_set_tx_bitrate_mask,
6838 .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 Malinen13ae75b2009-12-29 12:59:45 +02006842 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006843 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006844 .cmd = NL80211_CMD_REGISTER_FRAME,
6845 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006846 .policy = nl80211_policy,
6847 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006848 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6849 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006850 },
6851 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006852 .cmd = NL80211_CMD_FRAME,
6853 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006854 .policy = nl80211_policy,
6855 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006856 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006857 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006858 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006859 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006860 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6861 .doit = nl80211_tx_mgmt_cancel_wait,
6862 .policy = nl80211_policy,
6863 .flags = GENL_ADMIN_PERM,
6864 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6865 NL80211_FLAG_NEED_RTNL,
6866 },
6867 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006868 .cmd = NL80211_CMD_SET_POWER_SAVE,
6869 .doit = nl80211_set_power_save,
6870 .policy = nl80211_policy,
6871 .flags = GENL_ADMIN_PERM,
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 },
6875 {
6876 .cmd = NL80211_CMD_GET_POWER_SAVE,
6877 .doit = nl80211_get_power_save,
6878 .policy = nl80211_policy,
6879 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006880 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6881 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006882 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006883 {
6884 .cmd = NL80211_CMD_SET_CQM,
6885 .doit = nl80211_set_cqm,
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,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006890 },
Johannes Bergf444de02010-05-05 15:25:02 +02006891 {
6892 .cmd = NL80211_CMD_SET_CHANNEL,
6893 .doit = nl80211_set_channel,
6894 .policy = nl80211_policy,
6895 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006896 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6897 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006898 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006899 {
6900 .cmd = NL80211_CMD_SET_WDS_PEER,
6901 .doit = nl80211_set_wds_peer,
6902 .policy = nl80211_policy,
6903 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006904 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6905 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006906 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006907 {
6908 .cmd = NL80211_CMD_JOIN_MESH,
6909 .doit = nl80211_join_mesh,
6910 .policy = nl80211_policy,
6911 .flags = GENL_ADMIN_PERM,
6912 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6913 NL80211_FLAG_NEED_RTNL,
6914 },
6915 {
6916 .cmd = NL80211_CMD_LEAVE_MESH,
6917 .doit = nl80211_leave_mesh,
6918 .policy = nl80211_policy,
6919 .flags = GENL_ADMIN_PERM,
6920 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6921 NL80211_FLAG_NEED_RTNL,
6922 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006923 {
6924 .cmd = NL80211_CMD_GET_WOWLAN,
6925 .doit = nl80211_get_wowlan,
6926 .policy = nl80211_policy,
6927 /* can be retrieved by unprivileged users */
6928 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6929 NL80211_FLAG_NEED_RTNL,
6930 },
6931 {
6932 .cmd = NL80211_CMD_SET_WOWLAN,
6933 .doit = nl80211_set_wowlan,
6934 .policy = nl80211_policy,
6935 .flags = GENL_ADMIN_PERM,
6936 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6937 NL80211_FLAG_NEED_RTNL,
6938 },
Johannes Berge5497d72011-07-05 16:35:40 +02006939 {
6940 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6941 .doit = nl80211_set_rekey_data,
6942 .policy = nl80211_policy,
6943 .flags = GENL_ADMIN_PERM,
6944 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6945 NL80211_FLAG_NEED_RTNL,
6946 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006947 {
6948 .cmd = NL80211_CMD_TDLS_MGMT,
6949 .doit = nl80211_tdls_mgmt,
6950 .policy = nl80211_policy,
6951 .flags = GENL_ADMIN_PERM,
6952 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6953 NL80211_FLAG_NEED_RTNL,
6954 },
6955 {
6956 .cmd = NL80211_CMD_TDLS_OPER,
6957 .doit = nl80211_tdls_oper,
6958 .policy = nl80211_policy,
6959 .flags = GENL_ADMIN_PERM,
6960 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6961 NL80211_FLAG_NEED_RTNL,
6962 },
Johannes Berg28946da2011-11-04 11:18:12 +01006963 {
6964 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6965 .doit = nl80211_register_unexpected_frame,
6966 .policy = nl80211_policy,
6967 .flags = GENL_ADMIN_PERM,
6968 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6969 NL80211_FLAG_NEED_RTNL,
6970 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006971 {
6972 .cmd = NL80211_CMD_PROBE_CLIENT,
6973 .doit = nl80211_probe_client,
6974 .policy = nl80211_policy,
6975 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006976 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01006977 NL80211_FLAG_NEED_RTNL,
6978 },
Johannes Berg5e760232011-11-04 11:18:17 +01006979 {
6980 .cmd = NL80211_CMD_REGISTER_BEACONS,
6981 .doit = nl80211_register_beacons,
6982 .policy = nl80211_policy,
6983 .flags = GENL_ADMIN_PERM,
6984 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6985 NL80211_FLAG_NEED_RTNL,
6986 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006987 {
6988 .cmd = NL80211_CMD_SET_NOACK_MAP,
6989 .doit = nl80211_set_noack_map,
6990 .policy = nl80211_policy,
6991 .flags = GENL_ADMIN_PERM,
6992 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6993 NL80211_FLAG_NEED_RTNL,
6994 },
6995
Johannes Berg55682962007-09-20 13:09:35 -04006996};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006997
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006998static struct genl_multicast_group nl80211_mlme_mcgrp = {
6999 .name = "mlme",
7000};
Johannes Berg55682962007-09-20 13:09:35 -04007001
7002/* multicast groups */
7003static struct genl_multicast_group nl80211_config_mcgrp = {
7004 .name = "config",
7005};
Johannes Berg2a519312009-02-10 21:25:55 +01007006static struct genl_multicast_group nl80211_scan_mcgrp = {
7007 .name = "scan",
7008};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007009static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7010 .name = "regulatory",
7011};
Johannes Berg55682962007-09-20 13:09:35 -04007012
7013/* notification functions */
7014
7015void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7016{
7017 struct sk_buff *msg;
7018
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007019 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007020 if (!msg)
7021 return;
7022
7023 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7024 nlmsg_free(msg);
7025 return;
7026 }
7027
Johannes Berg463d0182009-07-14 00:33:35 +02007028 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7029 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007030}
7031
Johannes Berg362a4152009-05-24 16:43:15 +02007032static int nl80211_add_scan_req(struct sk_buff *msg,
7033 struct cfg80211_registered_device *rdev)
7034{
7035 struct cfg80211_scan_request *req = rdev->scan_req;
7036 struct nlattr *nest;
7037 int i;
7038
Johannes Berg667503dd2009-07-07 03:56:11 +02007039 ASSERT_RDEV_LOCK(rdev);
7040
Johannes Berg362a4152009-05-24 16:43:15 +02007041 if (WARN_ON(!req))
7042 return 0;
7043
7044 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7045 if (!nest)
7046 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007047 for (i = 0; i < req->n_ssids; i++) {
7048 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7049 goto nla_put_failure;
7050 }
Johannes Berg362a4152009-05-24 16:43:15 +02007051 nla_nest_end(msg, nest);
7052
7053 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7054 if (!nest)
7055 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007056 for (i = 0; i < req->n_channels; i++) {
7057 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7058 goto nla_put_failure;
7059 }
Johannes Berg362a4152009-05-24 16:43:15 +02007060 nla_nest_end(msg, nest);
7061
David S. Miller9360ffd2012-03-29 04:41:26 -04007062 if (req->ie &&
7063 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7064 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007065
7066 return 0;
7067 nla_put_failure:
7068 return -ENOBUFS;
7069}
7070
Johannes Berga538e2d2009-06-16 19:56:42 +02007071static int nl80211_send_scan_msg(struct sk_buff *msg,
7072 struct cfg80211_registered_device *rdev,
7073 struct net_device *netdev,
7074 u32 pid, u32 seq, int flags,
7075 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007076{
7077 void *hdr;
7078
7079 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7080 if (!hdr)
7081 return -1;
7082
David S. Miller9360ffd2012-03-29 04:41:26 -04007083 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7084 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7085 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007086
Johannes Berg362a4152009-05-24 16:43:15 +02007087 /* ignore errors and send incomplete event anyway */
7088 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007089
7090 return genlmsg_end(msg, hdr);
7091
7092 nla_put_failure:
7093 genlmsg_cancel(msg, hdr);
7094 return -EMSGSIZE;
7095}
7096
Luciano Coelho807f8a82011-05-11 17:09:35 +03007097static int
7098nl80211_send_sched_scan_msg(struct sk_buff *msg,
7099 struct cfg80211_registered_device *rdev,
7100 struct net_device *netdev,
7101 u32 pid, u32 seq, int flags, u32 cmd)
7102{
7103 void *hdr;
7104
7105 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7106 if (!hdr)
7107 return -1;
7108
David S. Miller9360ffd2012-03-29 04:41:26 -04007109 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7110 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7111 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007112
7113 return genlmsg_end(msg, hdr);
7114
7115 nla_put_failure:
7116 genlmsg_cancel(msg, hdr);
7117 return -EMSGSIZE;
7118}
7119
Johannes Berga538e2d2009-06-16 19:56:42 +02007120void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7121 struct net_device *netdev)
7122{
7123 struct sk_buff *msg;
7124
7125 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7126 if (!msg)
7127 return;
7128
7129 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7130 NL80211_CMD_TRIGGER_SCAN) < 0) {
7131 nlmsg_free(msg);
7132 return;
7133 }
7134
Johannes Berg463d0182009-07-14 00:33:35 +02007135 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7136 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007137}
7138
Johannes Berg2a519312009-02-10 21:25:55 +01007139void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7140 struct net_device *netdev)
7141{
7142 struct sk_buff *msg;
7143
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007144 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007145 if (!msg)
7146 return;
7147
Johannes Berga538e2d2009-06-16 19:56:42 +02007148 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7149 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007150 nlmsg_free(msg);
7151 return;
7152 }
7153
Johannes Berg463d0182009-07-14 00:33:35 +02007154 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7155 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007156}
7157
7158void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7159 struct net_device *netdev)
7160{
7161 struct sk_buff *msg;
7162
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007164 if (!msg)
7165 return;
7166
Johannes Berga538e2d2009-06-16 19:56:42 +02007167 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7168 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007169 nlmsg_free(msg);
7170 return;
7171 }
7172
Johannes Berg463d0182009-07-14 00:33:35 +02007173 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7174 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007175}
7176
Luciano Coelho807f8a82011-05-11 17:09:35 +03007177void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7178 struct net_device *netdev)
7179{
7180 struct sk_buff *msg;
7181
7182 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7183 if (!msg)
7184 return;
7185
7186 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7187 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7188 nlmsg_free(msg);
7189 return;
7190 }
7191
7192 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7193 nl80211_scan_mcgrp.id, GFP_KERNEL);
7194}
7195
7196void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7197 struct net_device *netdev, u32 cmd)
7198{
7199 struct sk_buff *msg;
7200
7201 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7202 if (!msg)
7203 return;
7204
7205 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7206 nlmsg_free(msg);
7207 return;
7208 }
7209
7210 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7211 nl80211_scan_mcgrp.id, GFP_KERNEL);
7212}
7213
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007214/*
7215 * This can happen on global regulatory changes or device specific settings
7216 * based on custom world regulatory domains.
7217 */
7218void nl80211_send_reg_change_event(struct regulatory_request *request)
7219{
7220 struct sk_buff *msg;
7221 void *hdr;
7222
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007223 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007224 if (!msg)
7225 return;
7226
7227 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7228 if (!hdr) {
7229 nlmsg_free(msg);
7230 return;
7231 }
7232
7233 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007234 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7235 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007236
David S. Miller9360ffd2012-03-29 04:41:26 -04007237 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7238 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7239 NL80211_REGDOM_TYPE_WORLD))
7240 goto nla_put_failure;
7241 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7242 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7243 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7244 goto nla_put_failure;
7245 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7246 request->intersect) {
7247 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7248 NL80211_REGDOM_TYPE_INTERSECTION))
7249 goto nla_put_failure;
7250 } else {
7251 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7252 NL80211_REGDOM_TYPE_COUNTRY) ||
7253 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7254 request->alpha2))
7255 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007256 }
7257
David S. Miller9360ffd2012-03-29 04:41:26 -04007258 if (wiphy_idx_valid(request->wiphy_idx) &&
7259 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7260 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007261
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007262 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007263
Johannes Bergbc43b282009-07-25 10:54:13 +02007264 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007265 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007266 GFP_ATOMIC);
7267 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007268
7269 return;
7270
7271nla_put_failure:
7272 genlmsg_cancel(msg, hdr);
7273 nlmsg_free(msg);
7274}
7275
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007276static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7277 struct net_device *netdev,
7278 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007279 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007280{
7281 struct sk_buff *msg;
7282 void *hdr;
7283
Johannes Berge6d6e342009-07-01 21:26:47 +02007284 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007285 if (!msg)
7286 return;
7287
7288 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7289 if (!hdr) {
7290 nlmsg_free(msg);
7291 return;
7292 }
7293
David S. Miller9360ffd2012-03-29 04:41:26 -04007294 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7295 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7296 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7297 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007298
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007299 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007300
Johannes Berg463d0182009-07-14 00:33:35 +02007301 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7302 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007303 return;
7304
7305 nla_put_failure:
7306 genlmsg_cancel(msg, hdr);
7307 nlmsg_free(msg);
7308}
7309
7310void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007311 struct net_device *netdev, const u8 *buf,
7312 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007313{
7314 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007315 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007316}
7317
7318void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7319 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007320 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007321{
Johannes Berge6d6e342009-07-01 21:26:47 +02007322 nl80211_send_mlme_event(rdev, netdev, buf, len,
7323 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007324}
7325
Jouni Malinen53b46b82009-03-27 20:53:56 +02007326void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007327 struct net_device *netdev, const u8 *buf,
7328 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_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007332}
7333
Jouni Malinen53b46b82009-03-27 20:53:56 +02007334void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7335 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007336 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007337{
7338 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007339 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007340}
7341
Jouni Malinencf4e5942010-12-16 00:52:40 +02007342void nl80211_send_unprot_deauth(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_DEAUTHENTICATE, gfp);
7348}
7349
7350void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7351 struct net_device *netdev, const u8 *buf,
7352 size_t len, gfp_t gfp)
7353{
7354 nl80211_send_mlme_event(rdev, netdev, buf, len,
7355 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7356}
7357
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007358static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7359 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007360 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007361{
7362 struct sk_buff *msg;
7363 void *hdr;
7364
Johannes Berge6d6e342009-07-01 21:26:47 +02007365 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007366 if (!msg)
7367 return;
7368
7369 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7370 if (!hdr) {
7371 nlmsg_free(msg);
7372 return;
7373 }
7374
David S. Miller9360ffd2012-03-29 04:41:26 -04007375 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7376 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7377 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7378 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7379 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007380
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007381 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007382
Johannes Berg463d0182009-07-14 00:33:35 +02007383 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7384 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007385 return;
7386
7387 nla_put_failure:
7388 genlmsg_cancel(msg, hdr);
7389 nlmsg_free(msg);
7390}
7391
7392void nl80211_send_auth_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{
7396 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007397 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007398}
7399
7400void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007401 struct net_device *netdev, const u8 *addr,
7402 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007403{
Johannes Berge6d6e342009-07-01 21:26:47 +02007404 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7405 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007406}
7407
Samuel Ortizb23aa672009-07-01 21:26:54 +02007408void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7409 struct net_device *netdev, const u8 *bssid,
7410 const u8 *req_ie, size_t req_ie_len,
7411 const u8 *resp_ie, size_t resp_ie_len,
7412 u16 status, gfp_t gfp)
7413{
7414 struct sk_buff *msg;
7415 void *hdr;
7416
7417 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7418 if (!msg)
7419 return;
7420
7421 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7422 if (!hdr) {
7423 nlmsg_free(msg);
7424 return;
7425 }
7426
David S. Miller9360ffd2012-03-29 04:41:26 -04007427 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7428 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7429 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7430 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7431 (req_ie &&
7432 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7433 (resp_ie &&
7434 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7435 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007436
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007437 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007438
Johannes Berg463d0182009-07-14 00:33:35 +02007439 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7440 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007441 return;
7442
7443 nla_put_failure:
7444 genlmsg_cancel(msg, hdr);
7445 nlmsg_free(msg);
7446
7447}
7448
7449void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7450 struct net_device *netdev, const u8 *bssid,
7451 const u8 *req_ie, size_t req_ie_len,
7452 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7453{
7454 struct sk_buff *msg;
7455 void *hdr;
7456
7457 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7458 if (!msg)
7459 return;
7460
7461 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7462 if (!hdr) {
7463 nlmsg_free(msg);
7464 return;
7465 }
7466
David S. Miller9360ffd2012-03-29 04:41:26 -04007467 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7468 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7469 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7470 (req_ie &&
7471 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7472 (resp_ie &&
7473 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7474 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007475
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007476 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007477
Johannes Berg463d0182009-07-14 00:33:35 +02007478 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7479 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007480 return;
7481
7482 nla_put_failure:
7483 genlmsg_cancel(msg, hdr);
7484 nlmsg_free(msg);
7485
7486}
7487
7488void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7489 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007490 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007491{
7492 struct sk_buff *msg;
7493 void *hdr;
7494
Johannes Berg667503dd2009-07-07 03:56:11 +02007495 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007496 if (!msg)
7497 return;
7498
7499 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7500 if (!hdr) {
7501 nlmsg_free(msg);
7502 return;
7503 }
7504
David S. Miller9360ffd2012-03-29 04:41:26 -04007505 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7506 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7507 (from_ap && reason &&
7508 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7509 (from_ap &&
7510 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7511 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7512 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007513
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007514 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007515
Johannes Berg463d0182009-07-14 00:33:35 +02007516 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7517 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007518 return;
7519
7520 nla_put_failure:
7521 genlmsg_cancel(msg, hdr);
7522 nlmsg_free(msg);
7523
7524}
7525
Johannes Berg04a773a2009-04-19 21:24:32 +02007526void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7527 struct net_device *netdev, const u8 *bssid,
7528 gfp_t gfp)
7529{
7530 struct sk_buff *msg;
7531 void *hdr;
7532
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007534 if (!msg)
7535 return;
7536
7537 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7538 if (!hdr) {
7539 nlmsg_free(msg);
7540 return;
7541 }
7542
David S. Miller9360ffd2012-03-29 04:41:26 -04007543 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7544 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7545 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7546 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007547
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007548 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007549
Johannes Berg463d0182009-07-14 00:33:35 +02007550 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7551 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007552 return;
7553
7554 nla_put_failure:
7555 genlmsg_cancel(msg, hdr);
7556 nlmsg_free(msg);
7557}
7558
Javier Cardonac93b5e72011-04-07 15:08:34 -07007559void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7560 struct net_device *netdev,
7561 const u8 *macaddr, const u8* ie, u8 ie_len,
7562 gfp_t gfp)
7563{
7564 struct sk_buff *msg;
7565 void *hdr;
7566
7567 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7568 if (!msg)
7569 return;
7570
7571 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7572 if (!hdr) {
7573 nlmsg_free(msg);
7574 return;
7575 }
7576
David S. Miller9360ffd2012-03-29 04:41:26 -04007577 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7578 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7579 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7580 (ie_len && ie &&
7581 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7582 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007583
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007584 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007585
7586 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7587 nl80211_mlme_mcgrp.id, gfp);
7588 return;
7589
7590 nla_put_failure:
7591 genlmsg_cancel(msg, hdr);
7592 nlmsg_free(msg);
7593}
7594
Jouni Malinena3b8b052009-03-27 21:59:49 +02007595void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7596 struct net_device *netdev, const u8 *addr,
7597 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007598 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007599{
7600 struct sk_buff *msg;
7601 void *hdr;
7602
Johannes Berge6d6e342009-07-01 21:26:47 +02007603 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007604 if (!msg)
7605 return;
7606
7607 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7608 if (!hdr) {
7609 nlmsg_free(msg);
7610 return;
7611 }
7612
David S. Miller9360ffd2012-03-29 04:41:26 -04007613 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7614 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7615 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7616 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7617 (key_id != -1 &&
7618 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7619 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7620 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007621
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007622 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007623
Johannes Berg463d0182009-07-14 00:33:35 +02007624 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7625 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007626 return;
7627
7628 nla_put_failure:
7629 genlmsg_cancel(msg, hdr);
7630 nlmsg_free(msg);
7631}
7632
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007633void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7634 struct ieee80211_channel *channel_before,
7635 struct ieee80211_channel *channel_after)
7636{
7637 struct sk_buff *msg;
7638 void *hdr;
7639 struct nlattr *nl_freq;
7640
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007641 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007642 if (!msg)
7643 return;
7644
7645 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7646 if (!hdr) {
7647 nlmsg_free(msg);
7648 return;
7649 }
7650
7651 /*
7652 * Since we are applying the beacon hint to a wiphy we know its
7653 * wiphy_idx is valid
7654 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007655 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7656 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007657
7658 /* Before */
7659 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7660 if (!nl_freq)
7661 goto nla_put_failure;
7662 if (nl80211_msg_put_channel(msg, channel_before))
7663 goto nla_put_failure;
7664 nla_nest_end(msg, nl_freq);
7665
7666 /* After */
7667 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7668 if (!nl_freq)
7669 goto nla_put_failure;
7670 if (nl80211_msg_put_channel(msg, channel_after))
7671 goto nla_put_failure;
7672 nla_nest_end(msg, nl_freq);
7673
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007674 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007675
Johannes Berg463d0182009-07-14 00:33:35 +02007676 rcu_read_lock();
7677 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7678 GFP_ATOMIC);
7679 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007680
7681 return;
7682
7683nla_put_failure:
7684 genlmsg_cancel(msg, hdr);
7685 nlmsg_free(msg);
7686}
7687
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007688static void nl80211_send_remain_on_chan_event(
7689 int cmd, struct cfg80211_registered_device *rdev,
7690 struct net_device *netdev, u64 cookie,
7691 struct ieee80211_channel *chan,
7692 enum nl80211_channel_type channel_type,
7693 unsigned int duration, gfp_t gfp)
7694{
7695 struct sk_buff *msg;
7696 void *hdr;
7697
7698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7699 if (!msg)
7700 return;
7701
7702 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7703 if (!hdr) {
7704 nlmsg_free(msg);
7705 return;
7706 }
7707
David S. Miller9360ffd2012-03-29 04:41:26 -04007708 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7709 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7710 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7711 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7712 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7713 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007714
David S. Miller9360ffd2012-03-29 04:41:26 -04007715 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7716 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7717 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007718
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007719 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007720
7721 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7722 nl80211_mlme_mcgrp.id, gfp);
7723 return;
7724
7725 nla_put_failure:
7726 genlmsg_cancel(msg, hdr);
7727 nlmsg_free(msg);
7728}
7729
7730void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7731 struct net_device *netdev, u64 cookie,
7732 struct ieee80211_channel *chan,
7733 enum nl80211_channel_type channel_type,
7734 unsigned int duration, gfp_t gfp)
7735{
7736 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7737 rdev, netdev, cookie, chan,
7738 channel_type, duration, gfp);
7739}
7740
7741void nl80211_send_remain_on_channel_cancel(
7742 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7743 u64 cookie, struct ieee80211_channel *chan,
7744 enum nl80211_channel_type channel_type, gfp_t gfp)
7745{
7746 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7747 rdev, netdev, cookie, chan,
7748 channel_type, 0, gfp);
7749}
7750
Johannes Berg98b62182009-12-23 13:15:44 +01007751void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7752 struct net_device *dev, const u8 *mac_addr,
7753 struct station_info *sinfo, gfp_t gfp)
7754{
7755 struct sk_buff *msg;
7756
7757 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7758 if (!msg)
7759 return;
7760
John W. Linville66266b32012-03-15 13:25:41 -04007761 if (nl80211_send_station(msg, 0, 0, 0,
7762 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007763 nlmsg_free(msg);
7764 return;
7765 }
7766
7767 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7768 nl80211_mlme_mcgrp.id, gfp);
7769}
7770
Jouni Malinenec15e682011-03-23 15:29:52 +02007771void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7772 struct net_device *dev, const u8 *mac_addr,
7773 gfp_t gfp)
7774{
7775 struct sk_buff *msg;
7776 void *hdr;
7777
7778 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7779 if (!msg)
7780 return;
7781
7782 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7783 if (!hdr) {
7784 nlmsg_free(msg);
7785 return;
7786 }
7787
David S. Miller9360ffd2012-03-29 04:41:26 -04007788 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7789 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7790 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007791
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007792 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007793
7794 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7795 nl80211_mlme_mcgrp.id, gfp);
7796 return;
7797
7798 nla_put_failure:
7799 genlmsg_cancel(msg, hdr);
7800 nlmsg_free(msg);
7801}
7802
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007803static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7804 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007805{
7806 struct wireless_dev *wdev = dev->ieee80211_ptr;
7807 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7808 struct sk_buff *msg;
7809 void *hdr;
7810 int err;
7811 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7812
7813 if (!nlpid)
7814 return false;
7815
7816 msg = nlmsg_new(100, gfp);
7817 if (!msg)
7818 return true;
7819
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007820 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007821 if (!hdr) {
7822 nlmsg_free(msg);
7823 return true;
7824 }
7825
David S. Miller9360ffd2012-03-29 04:41:26 -04007826 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7827 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7828 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7829 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007830
7831 err = genlmsg_end(msg, hdr);
7832 if (err < 0) {
7833 nlmsg_free(msg);
7834 return true;
7835 }
7836
7837 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7838 return true;
7839
7840 nla_put_failure:
7841 genlmsg_cancel(msg, hdr);
7842 nlmsg_free(msg);
7843 return true;
7844}
7845
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007846bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7847{
7848 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7849 addr, gfp);
7850}
7851
7852bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7853 const u8 *addr, gfp_t gfp)
7854{
7855 return __nl80211_unexpected_frame(dev,
7856 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7857 addr, gfp);
7858}
7859
Johannes Berg2e161f72010-08-12 15:38:38 +02007860int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7861 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007862 int freq, int sig_dbm,
7863 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007864{
7865 struct sk_buff *msg;
7866 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007867
7868 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7869 if (!msg)
7870 return -ENOMEM;
7871
Johannes Berg2e161f72010-08-12 15:38:38 +02007872 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007873 if (!hdr) {
7874 nlmsg_free(msg);
7875 return -ENOMEM;
7876 }
7877
David S. Miller9360ffd2012-03-29 04:41:26 -04007878 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7879 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7880 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7881 (sig_dbm &&
7882 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7883 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7884 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007885
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007886 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007887
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007888 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007889
7890 nla_put_failure:
7891 genlmsg_cancel(msg, hdr);
7892 nlmsg_free(msg);
7893 return -ENOBUFS;
7894}
7895
Johannes Berg2e161f72010-08-12 15:38:38 +02007896void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7897 struct net_device *netdev, u64 cookie,
7898 const u8 *buf, size_t len, bool ack,
7899 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007900{
7901 struct sk_buff *msg;
7902 void *hdr;
7903
7904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7905 if (!msg)
7906 return;
7907
Johannes Berg2e161f72010-08-12 15:38:38 +02007908 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007909 if (!hdr) {
7910 nlmsg_free(msg);
7911 return;
7912 }
7913
David S. Miller9360ffd2012-03-29 04:41:26 -04007914 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7915 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7916 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7917 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7918 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7919 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007920
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007921 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007922
7923 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7924 return;
7925
7926 nla_put_failure:
7927 genlmsg_cancel(msg, hdr);
7928 nlmsg_free(msg);
7929}
7930
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007931void
7932nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7933 struct net_device *netdev,
7934 enum nl80211_cqm_rssi_threshold_event rssi_event,
7935 gfp_t gfp)
7936{
7937 struct sk_buff *msg;
7938 struct nlattr *pinfoattr;
7939 void *hdr;
7940
7941 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7942 if (!msg)
7943 return;
7944
7945 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7946 if (!hdr) {
7947 nlmsg_free(msg);
7948 return;
7949 }
7950
David S. Miller9360ffd2012-03-29 04:41:26 -04007951 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7952 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7953 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007954
7955 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7956 if (!pinfoattr)
7957 goto nla_put_failure;
7958
David S. Miller9360ffd2012-03-29 04:41:26 -04007959 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7960 rssi_event))
7961 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007962
7963 nla_nest_end(msg, pinfoattr);
7964
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007965 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007966
7967 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7968 nl80211_mlme_mcgrp.id, gfp);
7969 return;
7970
7971 nla_put_failure:
7972 genlmsg_cancel(msg, hdr);
7973 nlmsg_free(msg);
7974}
7975
Johannes Berge5497d72011-07-05 16:35:40 +02007976void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7977 struct net_device *netdev, const u8 *bssid,
7978 const u8 *replay_ctr, gfp_t gfp)
7979{
7980 struct sk_buff *msg;
7981 struct nlattr *rekey_attr;
7982 void *hdr;
7983
7984 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7985 if (!msg)
7986 return;
7987
7988 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7989 if (!hdr) {
7990 nlmsg_free(msg);
7991 return;
7992 }
7993
David S. Miller9360ffd2012-03-29 04:41:26 -04007994 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7995 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7996 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7997 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007998
7999 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8000 if (!rekey_attr)
8001 goto nla_put_failure;
8002
David S. Miller9360ffd2012-03-29 04:41:26 -04008003 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8004 NL80211_REPLAY_CTR_LEN, replay_ctr))
8005 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008006
8007 nla_nest_end(msg, rekey_attr);
8008
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008009 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008010
8011 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8012 nl80211_mlme_mcgrp.id, gfp);
8013 return;
8014
8015 nla_put_failure:
8016 genlmsg_cancel(msg, hdr);
8017 nlmsg_free(msg);
8018}
8019
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008020void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8021 struct net_device *netdev, int index,
8022 const u8 *bssid, bool preauth, gfp_t gfp)
8023{
8024 struct sk_buff *msg;
8025 struct nlattr *attr;
8026 void *hdr;
8027
8028 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8029 if (!msg)
8030 return;
8031
8032 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8033 if (!hdr) {
8034 nlmsg_free(msg);
8035 return;
8036 }
8037
David S. Miller9360ffd2012-03-29 04:41:26 -04008038 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8039 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8040 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008041
8042 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8043 if (!attr)
8044 goto nla_put_failure;
8045
David S. Miller9360ffd2012-03-29 04:41:26 -04008046 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8047 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8048 (preauth &&
8049 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8050 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008051
8052 nla_nest_end(msg, attr);
8053
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008054 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008055
8056 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8057 nl80211_mlme_mcgrp.id, gfp);
8058 return;
8059
8060 nla_put_failure:
8061 genlmsg_cancel(msg, hdr);
8062 nlmsg_free(msg);
8063}
8064
Thomas Pedersen53145262012-04-06 13:35:47 -07008065void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8066 struct net_device *netdev, int freq,
8067 enum nl80211_channel_type type, gfp_t gfp)
8068{
8069 struct sk_buff *msg;
8070 void *hdr;
8071
8072 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8073 if (!msg)
8074 return;
8075
8076 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8077 if (!hdr) {
8078 nlmsg_free(msg);
8079 return;
8080 }
8081
John W. Linville7eab0f62012-04-12 14:25:14 -04008082 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8083 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8084 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8085 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008086
8087 genlmsg_end(msg, hdr);
8088
8089 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8090 nl80211_mlme_mcgrp.id, gfp);
8091 return;
8092
8093 nla_put_failure:
8094 genlmsg_cancel(msg, hdr);
8095 nlmsg_free(msg);
8096}
8097
Johannes Bergc063dbf2010-11-24 08:10:05 +01008098void
8099nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8100 struct net_device *netdev, const u8 *peer,
8101 u32 num_packets, gfp_t gfp)
8102{
8103 struct sk_buff *msg;
8104 struct nlattr *pinfoattr;
8105 void *hdr;
8106
8107 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8108 if (!msg)
8109 return;
8110
8111 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8112 if (!hdr) {
8113 nlmsg_free(msg);
8114 return;
8115 }
8116
David S. Miller9360ffd2012-03-29 04:41:26 -04008117 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8118 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8119 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8120 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008121
8122 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8123 if (!pinfoattr)
8124 goto nla_put_failure;
8125
David S. Miller9360ffd2012-03-29 04:41:26 -04008126 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8127 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008128
8129 nla_nest_end(msg, pinfoattr);
8130
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008131 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008132
8133 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8134 nl80211_mlme_mcgrp.id, gfp);
8135 return;
8136
8137 nla_put_failure:
8138 genlmsg_cancel(msg, hdr);
8139 nlmsg_free(msg);
8140}
8141
Johannes Berg7f6cf312011-11-04 11:18:15 +01008142void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8143 u64 cookie, bool acked, gfp_t gfp)
8144{
8145 struct wireless_dev *wdev = dev->ieee80211_ptr;
8146 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8147 struct sk_buff *msg;
8148 void *hdr;
8149 int err;
8150
8151 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8152 if (!msg)
8153 return;
8154
8155 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8156 if (!hdr) {
8157 nlmsg_free(msg);
8158 return;
8159 }
8160
David S. Miller9360ffd2012-03-29 04:41:26 -04008161 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8162 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8163 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8164 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8165 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8166 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008167
8168 err = genlmsg_end(msg, hdr);
8169 if (err < 0) {
8170 nlmsg_free(msg);
8171 return;
8172 }
8173
8174 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8175 nl80211_mlme_mcgrp.id, gfp);
8176 return;
8177
8178 nla_put_failure:
8179 genlmsg_cancel(msg, hdr);
8180 nlmsg_free(msg);
8181}
8182EXPORT_SYMBOL(cfg80211_probe_status);
8183
Johannes Berg5e760232011-11-04 11:18:17 +01008184void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8185 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008186 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008187{
8188 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8189 struct sk_buff *msg;
8190 void *hdr;
8191 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8192
8193 if (!nlpid)
8194 return;
8195
8196 msg = nlmsg_new(len + 100, gfp);
8197 if (!msg)
8198 return;
8199
8200 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8201 if (!hdr) {
8202 nlmsg_free(msg);
8203 return;
8204 }
8205
David S. Miller9360ffd2012-03-29 04:41:26 -04008206 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8207 (freq &&
8208 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8209 (sig_dbm &&
8210 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8211 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8212 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008213
8214 genlmsg_end(msg, hdr);
8215
8216 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8217 return;
8218
8219 nla_put_failure:
8220 genlmsg_cancel(msg, hdr);
8221 nlmsg_free(msg);
8222}
8223EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8224
Jouni Malinen026331c2010-02-15 12:53:10 +02008225static int nl80211_netlink_notify(struct notifier_block * nb,
8226 unsigned long state,
8227 void *_notify)
8228{
8229 struct netlink_notify *notify = _notify;
8230 struct cfg80211_registered_device *rdev;
8231 struct wireless_dev *wdev;
8232
8233 if (state != NETLINK_URELEASE)
8234 return NOTIFY_DONE;
8235
8236 rcu_read_lock();
8237
Johannes Berg5e760232011-11-04 11:18:17 +01008238 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008239 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008240 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008241 if (rdev->ap_beacons_nlpid == notify->pid)
8242 rdev->ap_beacons_nlpid = 0;
8243 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008244
8245 rcu_read_unlock();
8246
8247 return NOTIFY_DONE;
8248}
8249
8250static struct notifier_block nl80211_netlink_notifier = {
8251 .notifier_call = nl80211_netlink_notify,
8252};
8253
Johannes Berg55682962007-09-20 13:09:35 -04008254/* initialisation/exit functions */
8255
8256int nl80211_init(void)
8257{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008258 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008259
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008260 err = genl_register_family_with_ops(&nl80211_fam,
8261 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008262 if (err)
8263 return err;
8264
Johannes Berg55682962007-09-20 13:09:35 -04008265 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8266 if (err)
8267 goto err_out;
8268
Johannes Berg2a519312009-02-10 21:25:55 +01008269 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8270 if (err)
8271 goto err_out;
8272
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008273 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8274 if (err)
8275 goto err_out;
8276
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008277 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8278 if (err)
8279 goto err_out;
8280
Johannes Bergaff89a92009-07-01 21:26:51 +02008281#ifdef CONFIG_NL80211_TESTMODE
8282 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8283 if (err)
8284 goto err_out;
8285#endif
8286
Jouni Malinen026331c2010-02-15 12:53:10 +02008287 err = netlink_register_notifier(&nl80211_netlink_notifier);
8288 if (err)
8289 goto err_out;
8290
Johannes Berg55682962007-09-20 13:09:35 -04008291 return 0;
8292 err_out:
8293 genl_unregister_family(&nl80211_fam);
8294 return err;
8295}
8296
8297void nl80211_exit(void)
8298{
Jouni Malinen026331c2010-02-15 12:53:10 +02008299 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008300 genl_unregister_family(&nl80211_fam);
8301}