blob: a4aab1d362852a9c8960f9b290d0bf290b8ad4cc [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530207 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530208 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400209};
210
Johannes Berge31b8212010-10-05 19:39:30 +0200211/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000212static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200213 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200214 [NL80211_KEY_IDX] = { .type = NLA_U8 },
215 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200216 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200217 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
218 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200219 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100220 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
221};
222
223/* policy for the key default flags */
224static const struct nla_policy
225nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
226 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
227 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200228};
229
Johannes Bergff1b6e62011-05-04 15:37:28 +0200230/* policy for WoWLAN attributes */
231static const struct nla_policy
232nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
233 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
235 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb132011-07-13 10:48:55 +0200237 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200241};
242
Johannes Berge5497d72011-07-05 16:35:40 +0200243/* policy for GTK rekey offload attributes */
244static const struct nla_policy
245nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
246 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
247 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
248 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249};
250
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300251static const struct nla_policy
252nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
253 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_SSID_LEN },
255};
256
Holger Schuriga0438972009-11-11 11:30:02 +0100257/* ifidx get helper */
258static int nl80211_get_ifidx(struct netlink_callback *cb)
259{
260 int res;
261
262 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
263 nl80211_fam.attrbuf, nl80211_fam.maxattr,
264 nl80211_policy);
265 if (res)
266 return res;
267
268 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
269 return -EINVAL;
270
271 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
272 if (!res)
273 return -EINVAL;
274 return res;
275}
276
Johannes Berg67748892010-10-04 21:14:06 +0200277static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
278 struct netlink_callback *cb,
279 struct cfg80211_registered_device **rdev,
280 struct net_device **dev)
281{
282 int ifidx = cb->args[0];
283 int err;
284
285 if (!ifidx)
286 ifidx = nl80211_get_ifidx(cb);
287 if (ifidx < 0)
288 return ifidx;
289
290 cb->args[0] = ifidx;
291
292 rtnl_lock();
293
294 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
295 if (!*dev) {
296 err = -ENODEV;
297 goto out_rtnl;
298 }
299
300 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100301 if (IS_ERR(*rdev)) {
302 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200303 goto out_rtnl;
304 }
305
306 return 0;
307 out_rtnl:
308 rtnl_unlock();
309 return err;
310}
311
312static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313{
314 cfg80211_unlock_rdev(rdev);
315 rtnl_unlock();
316}
317
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100318/* IE validation */
319static bool is_valid_ie_attr(const struct nlattr *attr)
320{
321 const u8 *pos;
322 int len;
323
324 if (!attr)
325 return true;
326
327 pos = nla_data(attr);
328 len = nla_len(attr);
329
330 while (len) {
331 u8 elemlen;
332
333 if (len < 2)
334 return false;
335 len -= 2;
336
337 elemlen = pos[1];
338 if (elemlen > len)
339 return false;
340
341 len -= elemlen;
342 pos += 2 + elemlen;
343 }
344
345 return true;
346}
347
Johannes Berg55682962007-09-20 13:09:35 -0400348/* message building helper */
349static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
350 int flags, u8 cmd)
351{
352 /* since there is no private header just add the generic one */
353 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354}
355
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400356static int nl80211_msg_put_channel(struct sk_buff *msg,
357 struct ieee80211_channel *chan)
358{
David S. Miller9360ffd2012-03-29 04:41:26 -0400359 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360 chan->center_freq))
361 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400362
David S. Miller9360ffd2012-03-29 04:41:26 -0400363 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
364 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
365 goto nla_put_failure;
366 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
367 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
368 goto nla_put_failure;
369 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
370 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
371 goto nla_put_failure;
372 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
373 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
374 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400375
David S. Miller9360ffd2012-03-29 04:41:26 -0400376 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
377 DBM_TO_MBM(chan->max_power)))
378 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400379
380 return 0;
381
382 nla_put_failure:
383 return -ENOBUFS;
384}
385
Johannes Berg55682962007-09-20 13:09:35 -0400386/* netlink command implementations */
387
Johannes Bergb9454e82009-07-08 13:29:08 +0200388struct key_parse {
389 struct key_params p;
390 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394};
395
396static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
397{
398 struct nlattr *tb[NL80211_KEY_MAX + 1];
399 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
400 nl80211_key_policy);
401 if (err)
402 return err;
403
404 k->def = !!tb[NL80211_KEY_DEFAULT];
405 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (k->def) {
408 k->def_uni = true;
409 k->def_multi = true;
410 }
411 if (k->defmgmt)
412 k->def_multi = true;
413
Johannes Bergb9454e82009-07-08 13:29:08 +0200414 if (tb[NL80211_KEY_IDX])
415 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
416
417 if (tb[NL80211_KEY_DATA]) {
418 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
419 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
420 }
421
422 if (tb[NL80211_KEY_SEQ]) {
423 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
424 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
425 }
426
427 if (tb[NL80211_KEY_CIPHER])
428 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
429
Johannes Berge31b8212010-10-05 19:39:30 +0200430 if (tb[NL80211_KEY_TYPE]) {
431 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
432 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
433 return -EINVAL;
434 }
435
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100436 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
437 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100438 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
439 tb[NL80211_KEY_DEFAULT_TYPES],
440 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100441 if (err)
442 return err;
443
444 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
445 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
446 }
447
Johannes Bergb9454e82009-07-08 13:29:08 +0200448 return 0;
449}
450
451static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
452{
453 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
454 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
455 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
456 }
457
458 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
459 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
460 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
461 }
462
463 if (info->attrs[NL80211_ATTR_KEY_IDX])
464 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
465
466 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
467 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
468
469 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
470 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
471
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100472 if (k->def) {
473 k->def_uni = true;
474 k->def_multi = true;
475 }
476 if (k->defmgmt)
477 k->def_multi = true;
478
Johannes Berge31b8212010-10-05 19:39:30 +0200479 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
480 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
481 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
482 return -EINVAL;
483 }
484
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100485 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
486 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
487 int err = nla_parse_nested(
488 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
489 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
490 nl80211_key_default_policy);
491 if (err)
492 return err;
493
494 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
495 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 return 0;
499}
500
501static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
502{
503 int err;
504
505 memset(k, 0, sizeof(*k));
506 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200507 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200508
509 if (info->attrs[NL80211_ATTR_KEY])
510 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
511 else
512 err = nl80211_parse_key_old(info, k);
513
514 if (err)
515 return err;
516
517 if (k->def && k->defmgmt)
518 return -EINVAL;
519
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (k->defmgmt) {
521 if (k->def_uni || !k->def_multi)
522 return -EINVAL;
523 }
524
Johannes Bergb9454e82009-07-08 13:29:08 +0200525 if (k->idx != -1) {
526 if (k->defmgmt) {
527 if (k->idx < 4 || k->idx > 5)
528 return -EINVAL;
529 } else if (k->def) {
530 if (k->idx < 0 || k->idx > 3)
531 return -EINVAL;
532 } else {
533 if (k->idx < 0 || k->idx > 5)
534 return -EINVAL;
535 }
536 }
537
538 return 0;
539}
540
Johannes Bergfffd0932009-07-08 14:22:54 +0200541static struct cfg80211_cached_keys *
542nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
543 struct nlattr *keys)
544{
545 struct key_parse parse;
546 struct nlattr *key;
547 struct cfg80211_cached_keys *result;
548 int rem, err, def = 0;
549
550 result = kzalloc(sizeof(*result), GFP_KERNEL);
551 if (!result)
552 return ERR_PTR(-ENOMEM);
553
554 result->def = -1;
555 result->defmgmt = -1;
556
557 nla_for_each_nested(key, keys, rem) {
558 memset(&parse, 0, sizeof(parse));
559 parse.idx = -1;
560
561 err = nl80211_parse_key_new(key, &parse);
562 if (err)
563 goto error;
564 err = -EINVAL;
565 if (!parse.p.key)
566 goto error;
567 if (parse.idx < 0 || parse.idx > 4)
568 goto error;
569 if (parse.def) {
570 if (def)
571 goto error;
572 def = 1;
573 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (!parse.def_uni || !parse.def_multi)
575 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 } else if (parse.defmgmt)
577 goto error;
578 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200579 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200580 if (err)
581 goto error;
582 result->params[parse.idx].cipher = parse.p.cipher;
583 result->params[parse.idx].key_len = parse.p.key_len;
584 result->params[parse.idx].key = result->data[parse.idx];
585 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
586 }
587
588 return result;
589 error:
590 kfree(result);
591 return ERR_PTR(err);
592}
593
594static int nl80211_key_allowed(struct wireless_dev *wdev)
595{
596 ASSERT_WDEV_LOCK(wdev);
597
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 switch (wdev->iftype) {
599 case NL80211_IFTYPE_AP:
600 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700602 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 break;
604 case NL80211_IFTYPE_ADHOC:
605 if (!wdev->current_bss)
606 return -ENOLINK;
607 break;
608 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200609 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200610 if (wdev->sme_state != CFG80211_SME_CONNECTED)
611 return -ENOLINK;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 return 0;
618}
619
Johannes Berg7527a782011-05-13 10:58:57 +0200620static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
621{
622 struct nlattr *nl_modes = nla_nest_start(msg, attr);
623 int i;
624
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400630 if ((ifmodes & 1) && nla_put_flag(msg, i))
631 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637 return 0;
638
639nla_put_failure:
640 return -ENOBUFS;
641}
642
643static int nl80211_put_iface_combinations(struct wiphy *wiphy,
644 struct sk_buff *msg)
645{
646 struct nlattr *nl_combis;
647 int i, j;
648
649 nl_combis = nla_nest_start(msg,
650 NL80211_ATTR_INTERFACE_COMBINATIONS);
651 if (!nl_combis)
652 goto nla_put_failure;
653
654 for (i = 0; i < wiphy->n_iface_combinations; i++) {
655 const struct ieee80211_iface_combination *c;
656 struct nlattr *nl_combi, *nl_limits;
657
658 c = &wiphy->iface_combinations[i];
659
660 nl_combi = nla_nest_start(msg, i + 1);
661 if (!nl_combi)
662 goto nla_put_failure;
663
664 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
665 if (!nl_limits)
666 goto nla_put_failure;
667
668 for (j = 0; j < c->n_limits; j++) {
669 struct nlattr *nl_limit;
670
671 nl_limit = nla_nest_start(msg, j + 1);
672 if (!nl_limit)
673 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400674 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
675 c->limits[j].max))
676 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200677 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
678 c->limits[j].types))
679 goto nla_put_failure;
680 nla_nest_end(msg, nl_limit);
681 }
682
683 nla_nest_end(msg, nl_limits);
684
David S. Miller9360ffd2012-03-29 04:41:26 -0400685 if (c->beacon_int_infra_match &&
686 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
687 goto nla_put_failure;
688 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
689 c->num_different_channels) ||
690 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
691 c->max_interfaces))
692 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200693
694 nla_nest_end(msg, nl_combi);
695 }
696
697 nla_nest_end(msg, nl_combis);
698
699 return 0;
700nla_put_failure:
701 return -ENOBUFS;
702}
703
Johannes Berg55682962007-09-20 13:09:35 -0400704static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
705 struct cfg80211_registered_device *dev)
706{
707 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100708 struct nlattr *nl_bands, *nl_band;
709 struct nlattr *nl_freqs, *nl_freq;
710 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100711 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100712 enum ieee80211_band band;
713 struct ieee80211_channel *chan;
714 struct ieee80211_rate *rate;
715 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200716 const struct ieee80211_txrx_stypes *mgmt_stypes =
717 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400718
719 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
720 if (!hdr)
721 return -1;
722
David S. Miller9360ffd2012-03-29 04:41:26 -0400723 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
724 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
725 nla_put_u32(msg, NL80211_ATTR_GENERATION,
726 cfg80211_rdev_list_generation) ||
727 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
728 dev->wiphy.retry_short) ||
729 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
730 dev->wiphy.retry_long) ||
731 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
732 dev->wiphy.frag_threshold) ||
733 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
734 dev->wiphy.rts_threshold) ||
735 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
736 dev->wiphy.coverage_class) ||
737 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
738 dev->wiphy.max_scan_ssids) ||
739 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
740 dev->wiphy.max_sched_scan_ssids) ||
741 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
742 dev->wiphy.max_scan_ie_len) ||
743 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
744 dev->wiphy.max_sched_scan_ie_len) ||
745 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
746 dev->wiphy.max_match_sets))
747 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200748
David S. Miller9360ffd2012-03-29 04:41:26 -0400749 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
750 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
751 goto nla_put_failure;
752 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
753 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
754 goto nla_put_failure;
755 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
756 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
757 goto nla_put_failure;
758 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
759 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
760 goto nla_put_failure;
761 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
762 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
763 goto nla_put_failure;
764 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
765 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
766 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200767
David S. Miller9360ffd2012-03-29 04:41:26 -0400768 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
769 sizeof(u32) * dev->wiphy.n_cipher_suites,
770 dev->wiphy.cipher_suites))
771 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100772
David S. Miller9360ffd2012-03-29 04:41:26 -0400773 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
774 dev->wiphy.max_num_pmkids))
775 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530776
David S. Miller9360ffd2012-03-29 04:41:26 -0400777 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
778 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
779 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200780
David S. Miller9360ffd2012-03-29 04:41:26 -0400781 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
782 dev->wiphy.available_antennas_tx) ||
783 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
784 dev->wiphy.available_antennas_rx))
785 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100786
David S. Miller9360ffd2012-03-29 04:41:26 -0400787 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
788 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
789 dev->wiphy.probe_resp_offload))
790 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200791
Bruno Randolf7f531e02010-12-16 11:30:22 +0900792 if ((dev->wiphy.available_antennas_tx ||
793 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900794 u32 tx_ant = 0, rx_ant = 0;
795 int res;
796 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
797 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
799 tx_ant) ||
800 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
801 rx_ant))
802 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900803 }
804 }
805
Johannes Berg7527a782011-05-13 10:58:57 +0200806 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
807 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700808 goto nla_put_failure;
809
Johannes Bergee688b002008-01-24 19:38:39 +0100810 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
811 if (!nl_bands)
812 goto nla_put_failure;
813
814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
815 if (!dev->wiphy.bands[band])
816 continue;
817
818 nl_band = nla_nest_start(msg, band);
819 if (!nl_band)
820 goto nla_put_failure;
821
Johannes Bergd51626d2008-10-09 12:20:13 +0200822 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
824 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
825 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
826 &dev->wiphy.bands[band]->ht_cap.mcs) ||
827 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
828 dev->wiphy.bands[band]->ht_cap.cap) ||
829 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
830 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
831 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
832 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
833 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200834
Johannes Bergee688b002008-01-24 19:38:39 +0100835 /* add frequencies */
836 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
837 if (!nl_freqs)
838 goto nla_put_failure;
839
840 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
841 nl_freq = nla_nest_start(msg, i);
842 if (!nl_freq)
843 goto nla_put_failure;
844
845 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100846
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400847 if (nl80211_msg_put_channel(msg, chan))
848 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200849
Johannes Bergee688b002008-01-24 19:38:39 +0100850 nla_nest_end(msg, nl_freq);
851 }
852
853 nla_nest_end(msg, nl_freqs);
854
855 /* add bitrates */
856 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
857 if (!nl_rates)
858 goto nla_put_failure;
859
860 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
861 nl_rate = nla_nest_start(msg, i);
862 if (!nl_rate)
863 goto nla_put_failure;
864
865 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
867 rate->bitrate))
868 goto nla_put_failure;
869 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
870 nla_put_flag(msg,
871 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
872 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100873
874 nla_nest_end(msg, nl_rate);
875 }
876
877 nla_nest_end(msg, nl_rates);
878
879 nla_nest_end(msg, nl_band);
880 }
881 nla_nest_end(msg, nl_bands);
882
Johannes Berg8fdc6212009-03-14 09:34:01 +0100883 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
884 if (!nl_cmds)
885 goto nla_put_failure;
886
887 i = 0;
888#define CMD(op, n) \
889 do { \
890 if (dev->ops->op) { \
891 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
893 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894 } \
895 } while (0)
896
897 CMD(add_virtual_intf, NEW_INTERFACE);
898 CMD(change_virtual_intf, SET_INTERFACE);
899 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100900 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100901 CMD(add_station, NEW_STATION);
902 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800903 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100904 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200905 CMD(auth, AUTHENTICATE);
906 CMD(assoc, ASSOCIATE);
907 CMD(deauth, DEAUTHENTICATE);
908 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200909 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100910 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100911 CMD(set_pmksa, SET_PMKSA);
912 CMD(del_pmksa, DEL_PMKSA);
913 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100914 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
915 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200916 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200917 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100918 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100919 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200920 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
922 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +0200923 }
Johannes Bergf444de02010-05-05 15:25:02 +0200924 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400925 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300926 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
927 CMD(tdls_mgmt, TDLS_MGMT);
928 CMD(tdls_oper, TDLS_OPER);
929 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300930 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
931 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100932 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100933 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100934 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
935 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
937 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +0100938 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100939
Kalle Valo4745fc02011-11-17 19:06:10 +0200940#ifdef CONFIG_NL80211_TESTMODE
941 CMD(testmode_cmd, TESTMODE);
942#endif
943
Johannes Berg8fdc6212009-03-14 09:34:01 +0100944#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200945
Johannes Berg6829c872009-07-02 09:13:27 +0200946 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200947 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400948 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
949 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200950 }
951
Johannes Berg6829c872009-07-02 09:13:27 +0200952 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200953 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400954 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
955 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200956 }
957
Johannes Berg8fdc6212009-03-14 09:34:01 +0100958 nla_nest_end(msg, nl_cmds);
959
Johannes Berg7c4ef712011-11-18 15:33:48 +0100960 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -0400961 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
962 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
963 dev->wiphy.max_remain_on_channel_duration))
964 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +0100965
David S. Miller9360ffd2012-03-29 04:41:26 -0400966 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
967 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
968 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100969
Johannes Berg2e161f72010-08-12 15:38:38 +0200970 if (mgmt_stypes) {
971 u16 stypes;
972 struct nlattr *nl_ftypes, *nl_ifs;
973 enum nl80211_iftype ift;
974
975 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
976 if (!nl_ifs)
977 goto nla_put_failure;
978
979 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
980 nl_ftypes = nla_nest_start(msg, ift);
981 if (!nl_ftypes)
982 goto nla_put_failure;
983 i = 0;
984 stypes = mgmt_stypes[ift].tx;
985 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400986 if ((stypes & 1) &&
987 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
988 (i << 4) | IEEE80211_FTYPE_MGMT))
989 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +0200990 stypes >>= 1;
991 i++;
992 }
993 nla_nest_end(msg, nl_ftypes);
994 }
995
Johannes Berg74b70a42010-08-24 12:15:53 +0200996 nla_nest_end(msg, nl_ifs);
997
Johannes Berg2e161f72010-08-12 15:38:38 +0200998 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
999 if (!nl_ifs)
1000 goto nla_put_failure;
1001
1002 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1003 nl_ftypes = nla_nest_start(msg, ift);
1004 if (!nl_ftypes)
1005 goto nla_put_failure;
1006 i = 0;
1007 stypes = mgmt_stypes[ift].rx;
1008 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001009 if ((stypes & 1) &&
1010 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1011 (i << 4) | IEEE80211_FTYPE_MGMT))
1012 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001013 stypes >>= 1;
1014 i++;
1015 }
1016 nla_nest_end(msg, nl_ftypes);
1017 }
1018 nla_nest_end(msg, nl_ifs);
1019 }
1020
Johannes Bergff1b6e62011-05-04 15:37:28 +02001021 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1022 struct nlattr *nl_wowlan;
1023
1024 nl_wowlan = nla_nest_start(msg,
1025 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1026 if (!nl_wowlan)
1027 goto nla_put_failure;
1028
David S. Miller9360ffd2012-03-29 04:41:26 -04001029 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1031 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1033 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1034 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1035 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1036 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1037 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1038 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1039 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1040 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1041 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1042 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1043 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1044 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1045 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001046 if (dev->wiphy.wowlan.n_patterns) {
1047 struct nl80211_wowlan_pattern_support pat = {
1048 .max_patterns = dev->wiphy.wowlan.n_patterns,
1049 .min_pattern_len =
1050 dev->wiphy.wowlan.pattern_min_len,
1051 .max_pattern_len =
1052 dev->wiphy.wowlan.pattern_max_len,
1053 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001054 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1055 sizeof(pat), &pat))
1056 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001057 }
1058
1059 nla_nest_end(msg, nl_wowlan);
1060 }
1061
Johannes Berg7527a782011-05-13 10:58:57 +02001062 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1063 dev->wiphy.software_iftypes))
1064 goto nla_put_failure;
1065
1066 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1067 goto nla_put_failure;
1068
David S. Miller9360ffd2012-03-29 04:41:26 -04001069 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1070 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1071 dev->wiphy.ap_sme_capa))
1072 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001073
David S. Miller9360ffd2012-03-29 04:41:26 -04001074 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1075 dev->wiphy.features))
1076 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001077
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (dev->wiphy.ht_capa_mod_mask &&
1079 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1080 sizeof(*dev->wiphy.ht_capa_mod_mask),
1081 dev->wiphy.ht_capa_mod_mask))
1082 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001083
Johannes Berg55682962007-09-20 13:09:35 -04001084 return genlmsg_end(msg, hdr);
1085
1086 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001087 genlmsg_cancel(msg, hdr);
1088 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001089}
1090
1091static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1092{
1093 int idx = 0;
1094 int start = cb->args[0];
1095 struct cfg80211_registered_device *dev;
1096
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001097 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001098 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001099 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1100 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001101 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001102 continue;
1103 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1104 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001105 dev) < 0) {
1106 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001107 break;
Julius Volzb4637272008-07-08 14:02:19 +02001108 }
Johannes Berg55682962007-09-20 13:09:35 -04001109 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001110 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001111
1112 cb->args[0] = idx;
1113
1114 return skb->len;
1115}
1116
1117static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1118{
1119 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001120 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001121
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001123 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001124 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001125
Johannes Berg4c476992010-10-04 21:36:35 +02001126 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1127 nlmsg_free(msg);
1128 return -ENOBUFS;
1129 }
Johannes Berg55682962007-09-20 13:09:35 -04001130
Johannes Berg134e6372009-07-10 09:51:34 +00001131 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001132}
1133
Jouni Malinen31888482008-10-30 16:59:24 +02001134static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1135 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1136 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1137 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1138 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1139 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1140};
1141
1142static int parse_txq_params(struct nlattr *tb[],
1143 struct ieee80211_txq_params *txq_params)
1144{
1145 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1146 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1147 !tb[NL80211_TXQ_ATTR_AIFS])
1148 return -EINVAL;
1149
1150 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1151 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
1156 return 0;
1157}
1158
Johannes Bergf444de02010-05-05 15:25:02 +02001159static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1160{
1161 /*
1162 * You can only set the channel explicitly for AP, mesh
1163 * and WDS type interfaces; all others have their channel
1164 * managed via their respective "establish a connection"
1165 * command (connect, join, ...)
1166 *
1167 * Monitors are special as they are normally slaved to
1168 * whatever else is going on, so they behave as though
1169 * you tried setting the wiphy channel itself.
1170 */
1171 return !wdev ||
1172 wdev->iftype == NL80211_IFTYPE_AP ||
1173 wdev->iftype == NL80211_IFTYPE_WDS ||
1174 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001175 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1176 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001177}
1178
1179static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1180 struct wireless_dev *wdev,
1181 struct genl_info *info)
1182{
1183 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1184 u32 freq;
1185 int result;
1186
1187 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1188 return -EINVAL;
1189
1190 if (!nl80211_can_set_dev_channel(wdev))
1191 return -EOPNOTSUPP;
1192
1193 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1194 channel_type = nla_get_u32(info->attrs[
1195 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1196 if (channel_type != NL80211_CHAN_NO_HT &&
1197 channel_type != NL80211_CHAN_HT20 &&
1198 channel_type != NL80211_CHAN_HT40PLUS &&
1199 channel_type != NL80211_CHAN_HT40MINUS)
1200 return -EINVAL;
1201 }
1202
1203 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1204
1205 mutex_lock(&rdev->devlist_mtx);
1206 if (wdev) {
1207 wdev_lock(wdev);
1208 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1209 wdev_unlock(wdev);
1210 } else {
1211 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1212 }
1213 mutex_unlock(&rdev->devlist_mtx);
1214
1215 return result;
1216}
1217
1218static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1219{
Johannes Berg4c476992010-10-04 21:36:35 +02001220 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1221 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001222
Johannes Berg4c476992010-10-04 21:36:35 +02001223 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001224}
1225
Bill Jordane8347eb2010-10-01 13:54:28 -04001226static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1227{
Johannes Berg43b19952010-10-07 13:10:30 +02001228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1229 struct net_device *dev = info->user_ptr[1];
1230 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001231 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001232
1233 if (!info->attrs[NL80211_ATTR_MAC])
1234 return -EINVAL;
1235
Johannes Berg43b19952010-10-07 13:10:30 +02001236 if (netif_running(dev))
1237 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001238
Johannes Berg43b19952010-10-07 13:10:30 +02001239 if (!rdev->ops->set_wds_peer)
1240 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001241
Johannes Berg43b19952010-10-07 13:10:30 +02001242 if (wdev->iftype != NL80211_IFTYPE_WDS)
1243 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001244
1245 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001246 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001247}
1248
1249
Johannes Berg55682962007-09-20 13:09:35 -04001250static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1251{
1252 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001253 struct net_device *netdev = NULL;
1254 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001255 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001256 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001257 u32 changed;
1258 u8 retry_short = 0, retry_long = 0;
1259 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001260 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001261
Johannes Bergf444de02010-05-05 15:25:02 +02001262 /*
1263 * Try to find the wiphy and netdev. Normally this
1264 * function shouldn't need the netdev, but this is
1265 * done for backward compatibility -- previously
1266 * setting the channel was done per wiphy, but now
1267 * it is per netdev. Previous userland like hostapd
1268 * also passed a netdev to set_wiphy, so that it is
1269 * possible to let that go to the right netdev!
1270 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001271 mutex_lock(&cfg80211_mutex);
1272
Johannes Bergf444de02010-05-05 15:25:02 +02001273 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1274 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1275
1276 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1277 if (netdev && netdev->ieee80211_ptr) {
1278 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1279 mutex_lock(&rdev->mtx);
1280 } else
1281 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001282 }
1283
Johannes Bergf444de02010-05-05 15:25:02 +02001284 if (!netdev) {
1285 rdev = __cfg80211_rdev_from_info(info);
1286 if (IS_ERR(rdev)) {
1287 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001288 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001289 }
1290 wdev = NULL;
1291 netdev = NULL;
1292 result = 0;
1293
1294 mutex_lock(&rdev->mtx);
1295 } else if (netif_running(netdev) &&
1296 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1297 wdev = netdev->ieee80211_ptr;
1298 else
1299 wdev = NULL;
1300
1301 /*
1302 * end workaround code, by now the rdev is available
1303 * and locked, and wdev may or may not be NULL.
1304 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001305
1306 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001307 result = cfg80211_dev_rename(
1308 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001309
1310 mutex_unlock(&cfg80211_mutex);
1311
1312 if (result)
1313 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001314
Jouni Malinen31888482008-10-30 16:59:24 +02001315 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1316 struct ieee80211_txq_params txq_params;
1317 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1318
1319 if (!rdev->ops->set_txq_params) {
1320 result = -EOPNOTSUPP;
1321 goto bad_res;
1322 }
1323
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001324 if (!netdev) {
1325 result = -EINVAL;
1326 goto bad_res;
1327 }
1328
Johannes Berg133a3ff2011-11-03 14:50:13 +01001329 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1330 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1331 result = -EINVAL;
1332 goto bad_res;
1333 }
1334
Jouni Malinen31888482008-10-30 16:59:24 +02001335 nla_for_each_nested(nl_txq_params,
1336 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1337 rem_txq_params) {
1338 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1339 nla_data(nl_txq_params),
1340 nla_len(nl_txq_params),
1341 txq_params_policy);
1342 result = parse_txq_params(tb, &txq_params);
1343 if (result)
1344 goto bad_res;
1345
1346 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001347 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001348 &txq_params);
1349 if (result)
1350 goto bad_res;
1351 }
1352 }
1353
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001354 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001355 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001356 if (result)
1357 goto bad_res;
1358 }
1359
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001360 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1361 enum nl80211_tx_power_setting type;
1362 int idx, mbm = 0;
1363
1364 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001365 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001366 goto bad_res;
1367 }
1368
1369 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1370 type = nla_get_u32(info->attrs[idx]);
1371
1372 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1373 (type != NL80211_TX_POWER_AUTOMATIC)) {
1374 result = -EINVAL;
1375 goto bad_res;
1376 }
1377
1378 if (type != NL80211_TX_POWER_AUTOMATIC) {
1379 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1380 mbm = nla_get_u32(info->attrs[idx]);
1381 }
1382
1383 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1384 if (result)
1385 goto bad_res;
1386 }
1387
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001388 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1389 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1390 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001391 if ((!rdev->wiphy.available_antennas_tx &&
1392 !rdev->wiphy.available_antennas_rx) ||
1393 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001394 result = -EOPNOTSUPP;
1395 goto bad_res;
1396 }
1397
1398 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1399 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1400
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001401 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001402 * available antenna masks, except for the "all" mask */
1403 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1404 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001405 result = -EINVAL;
1406 goto bad_res;
1407 }
1408
Bruno Randolf7f531e02010-12-16 11:30:22 +09001409 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1410 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001411
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001412 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1413 if (result)
1414 goto bad_res;
1415 }
1416
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001417 changed = 0;
1418
1419 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1420 retry_short = nla_get_u8(
1421 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1422 if (retry_short == 0) {
1423 result = -EINVAL;
1424 goto bad_res;
1425 }
1426 changed |= WIPHY_PARAM_RETRY_SHORT;
1427 }
1428
1429 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1430 retry_long = nla_get_u8(
1431 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1432 if (retry_long == 0) {
1433 result = -EINVAL;
1434 goto bad_res;
1435 }
1436 changed |= WIPHY_PARAM_RETRY_LONG;
1437 }
1438
1439 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1440 frag_threshold = nla_get_u32(
1441 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1442 if (frag_threshold < 256) {
1443 result = -EINVAL;
1444 goto bad_res;
1445 }
1446 if (frag_threshold != (u32) -1) {
1447 /*
1448 * Fragments (apart from the last one) are required to
1449 * have even length. Make the fragmentation code
1450 * simpler by stripping LSB should someone try to use
1451 * odd threshold value.
1452 */
1453 frag_threshold &= ~0x1;
1454 }
1455 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1456 }
1457
1458 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1459 rts_threshold = nla_get_u32(
1460 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1461 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1462 }
1463
Lukáš Turek81077e82009-12-21 22:50:47 +01001464 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1465 coverage_class = nla_get_u8(
1466 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1467 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1468 }
1469
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001470 if (changed) {
1471 u8 old_retry_short, old_retry_long;
1472 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001473 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001474
1475 if (!rdev->ops->set_wiphy_params) {
1476 result = -EOPNOTSUPP;
1477 goto bad_res;
1478 }
1479
1480 old_retry_short = rdev->wiphy.retry_short;
1481 old_retry_long = rdev->wiphy.retry_long;
1482 old_frag_threshold = rdev->wiphy.frag_threshold;
1483 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001484 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001485
1486 if (changed & WIPHY_PARAM_RETRY_SHORT)
1487 rdev->wiphy.retry_short = retry_short;
1488 if (changed & WIPHY_PARAM_RETRY_LONG)
1489 rdev->wiphy.retry_long = retry_long;
1490 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1491 rdev->wiphy.frag_threshold = frag_threshold;
1492 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1493 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001494 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1495 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001496
1497 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1498 if (result) {
1499 rdev->wiphy.retry_short = old_retry_short;
1500 rdev->wiphy.retry_long = old_retry_long;
1501 rdev->wiphy.frag_threshold = old_frag_threshold;
1502 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001503 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001504 }
1505 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001506
Johannes Berg306d6112008-12-08 12:39:04 +01001507 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001508 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001509 if (netdev)
1510 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001511 return result;
1512}
1513
1514
1515static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001516 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001517 struct net_device *dev)
1518{
1519 void *hdr;
1520
1521 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1522 if (!hdr)
1523 return -1;
1524
David S. Miller9360ffd2012-03-29 04:41:26 -04001525 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1526 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1527 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1528 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1529 dev->ieee80211_ptr->iftype) ||
1530 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1531 rdev->devlist_generation ^
1532 (cfg80211_rdev_list_generation << 2)))
1533 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001534
Johannes Berg55682962007-09-20 13:09:35 -04001535 return genlmsg_end(msg, hdr);
1536
1537 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001538 genlmsg_cancel(msg, hdr);
1539 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001540}
1541
1542static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1543{
1544 int wp_idx = 0;
1545 int if_idx = 0;
1546 int wp_start = cb->args[0];
1547 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001548 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001549 struct wireless_dev *wdev;
1550
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001551 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001552 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1553 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001554 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001555 if (wp_idx < wp_start) {
1556 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001557 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001558 }
Johannes Berg55682962007-09-20 13:09:35 -04001559 if_idx = 0;
1560
Johannes Bergf5ea9122009-08-07 16:17:38 +02001561 mutex_lock(&rdev->devlist_mtx);
1562 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001563 if (if_idx < if_start) {
1564 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001565 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001566 }
Johannes Berg55682962007-09-20 13:09:35 -04001567 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1568 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001569 rdev, wdev->netdev) < 0) {
1570 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001571 goto out;
1572 }
1573 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001574 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001575 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001576
1577 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001578 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001579 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001580 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001581
1582 cb->args[0] = wp_idx;
1583 cb->args[1] = if_idx;
1584
1585 return skb->len;
1586}
1587
1588static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1589{
1590 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001591 struct cfg80211_registered_device *dev = info->user_ptr[0];
1592 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001593
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001594 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001595 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001596 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001597
Johannes Bergd7264052009-04-19 16:23:20 +02001598 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001599 dev, netdev) < 0) {
1600 nlmsg_free(msg);
1601 return -ENOBUFS;
1602 }
Johannes Berg55682962007-09-20 13:09:35 -04001603
Johannes Berg134e6372009-07-10 09:51:34 +00001604 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001605}
1606
Michael Wu66f7ac52008-01-31 19:48:22 +01001607static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1608 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1609 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1610 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1611 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1612 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1613};
1614
1615static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1616{
1617 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1618 int flag;
1619
1620 *mntrflags = 0;
1621
1622 if (!nla)
1623 return -EINVAL;
1624
1625 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1626 nla, mntr_flags_policy))
1627 return -EINVAL;
1628
1629 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1630 if (flags[flag])
1631 *mntrflags |= (1<<flag);
1632
1633 return 0;
1634}
1635
Johannes Berg9bc383d2009-11-19 11:55:19 +01001636static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001637 struct net_device *netdev, u8 use_4addr,
1638 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001639{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001640 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001641 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001642 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001643 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001644 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001645
1646 switch (iftype) {
1647 case NL80211_IFTYPE_AP_VLAN:
1648 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1649 return 0;
1650 break;
1651 case NL80211_IFTYPE_STATION:
1652 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1653 return 0;
1654 break;
1655 default:
1656 break;
1657 }
1658
1659 return -EOPNOTSUPP;
1660}
1661
Johannes Berg55682962007-09-20 13:09:35 -04001662static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1663{
Johannes Berg4c476992010-10-04 21:36:35 +02001664 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001665 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001666 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001667 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001668 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001669 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001670 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001671
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001672 memset(&params, 0, sizeof(params));
1673
Johannes Berg04a773a2009-04-19 21:24:32 +02001674 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001675
Johannes Berg723b0382008-09-16 20:22:09 +02001676 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001677 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001678 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001679 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001680 if (ntype > NL80211_IFTYPE_MAX)
1681 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001682 }
1683
Johannes Berg92ffe052008-09-16 20:39:36 +02001684 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001685 struct wireless_dev *wdev = dev->ieee80211_ptr;
1686
Johannes Berg4c476992010-10-04 21:36:35 +02001687 if (ntype != NL80211_IFTYPE_MESH_POINT)
1688 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001689 if (netif_running(dev))
1690 return -EBUSY;
1691
1692 wdev_lock(wdev);
1693 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1694 IEEE80211_MAX_MESH_ID_LEN);
1695 wdev->mesh_id_up_len =
1696 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1697 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1698 wdev->mesh_id_up_len);
1699 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001700 }
1701
Felix Fietkau8b787642009-11-10 18:53:10 +01001702 if (info->attrs[NL80211_ATTR_4ADDR]) {
1703 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1704 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001705 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001706 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001707 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001708 } else {
1709 params.use_4addr = -1;
1710 }
1711
Johannes Berg92ffe052008-09-16 20:39:36 +02001712 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001713 if (ntype != NL80211_IFTYPE_MONITOR)
1714 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001715 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1716 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001717 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001718 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001719
1720 flags = &_flags;
1721 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001722 }
Johannes Berg3b858752009-03-12 09:55:09 +01001723
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001724 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001725 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001726 else
1727 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001728
Johannes Berg9bc383d2009-11-19 11:55:19 +01001729 if (!err && params.use_4addr != -1)
1730 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1731
Johannes Berg55682962007-09-20 13:09:35 -04001732 return err;
1733}
1734
1735static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1736{
Johannes Berg4c476992010-10-04 21:36:35 +02001737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001738 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001739 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001740 int err;
1741 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001742 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001743
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001744 memset(&params, 0, sizeof(params));
1745
Johannes Berg55682962007-09-20 13:09:35 -04001746 if (!info->attrs[NL80211_ATTR_IFNAME])
1747 return -EINVAL;
1748
1749 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1750 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1751 if (type > NL80211_IFTYPE_MAX)
1752 return -EINVAL;
1753 }
1754
Johannes Berg79c97e92009-07-07 03:56:12 +02001755 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001756 !(rdev->wiphy.interface_modes & (1 << type)))
1757 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001758
Johannes Berg9bc383d2009-11-19 11:55:19 +01001759 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001760 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001761 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001762 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001763 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001764 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001765
Michael Wu66f7ac52008-01-31 19:48:22 +01001766 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1767 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1768 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001769 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001770 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001771 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001772 if (IS_ERR(dev))
1773 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001774
Johannes Berg29cbe682010-12-03 09:20:44 +01001775 if (type == NL80211_IFTYPE_MESH_POINT &&
1776 info->attrs[NL80211_ATTR_MESH_ID]) {
1777 struct wireless_dev *wdev = dev->ieee80211_ptr;
1778
1779 wdev_lock(wdev);
1780 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1781 IEEE80211_MAX_MESH_ID_LEN);
1782 wdev->mesh_id_up_len =
1783 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1784 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1785 wdev->mesh_id_up_len);
1786 wdev_unlock(wdev);
1787 }
1788
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001789 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001790}
1791
1792static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1793{
Johannes Berg4c476992010-10-04 21:36:35 +02001794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1795 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001796
Johannes Berg4c476992010-10-04 21:36:35 +02001797 if (!rdev->ops->del_virtual_intf)
1798 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001799
Johannes Berg4c476992010-10-04 21:36:35 +02001800 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001801}
1802
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001803static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1804{
1805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1806 struct net_device *dev = info->user_ptr[1];
1807 u16 noack_map;
1808
1809 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1810 return -EINVAL;
1811
1812 if (!rdev->ops->set_noack_map)
1813 return -EOPNOTSUPP;
1814
1815 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1816
1817 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1818}
1819
Johannes Berg41ade002007-12-19 02:03:29 +01001820struct get_key_cookie {
1821 struct sk_buff *msg;
1822 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001823 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001824};
1825
1826static void get_key_callback(void *c, struct key_params *params)
1827{
Johannes Bergb9454e82009-07-08 13:29:08 +02001828 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001829 struct get_key_cookie *cookie = c;
1830
David S. Miller9360ffd2012-03-29 04:41:26 -04001831 if ((params->key &&
1832 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1833 params->key_len, params->key)) ||
1834 (params->seq &&
1835 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1836 params->seq_len, params->seq)) ||
1837 (params->cipher &&
1838 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1839 params->cipher)))
1840 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001841
Johannes Bergb9454e82009-07-08 13:29:08 +02001842 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1843 if (!key)
1844 goto nla_put_failure;
1845
David S. Miller9360ffd2012-03-29 04:41:26 -04001846 if ((params->key &&
1847 nla_put(cookie->msg, NL80211_KEY_DATA,
1848 params->key_len, params->key)) ||
1849 (params->seq &&
1850 nla_put(cookie->msg, NL80211_KEY_SEQ,
1851 params->seq_len, params->seq)) ||
1852 (params->cipher &&
1853 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
1854 params->cipher)))
1855 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001856
David S. Miller9360ffd2012-03-29 04:41:26 -04001857 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
1858 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001859
1860 nla_nest_end(cookie->msg, key);
1861
Johannes Berg41ade002007-12-19 02:03:29 +01001862 return;
1863 nla_put_failure:
1864 cookie->error = 1;
1865}
1866
1867static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1868{
Johannes Berg4c476992010-10-04 21:36:35 +02001869 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001870 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001871 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001872 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001873 const u8 *mac_addr = NULL;
1874 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001875 struct get_key_cookie cookie = {
1876 .error = 0,
1877 };
1878 void *hdr;
1879 struct sk_buff *msg;
1880
1881 if (info->attrs[NL80211_ATTR_KEY_IDX])
1882 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1883
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001884 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001885 return -EINVAL;
1886
1887 if (info->attrs[NL80211_ATTR_MAC])
1888 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1889
Johannes Berge31b8212010-10-05 19:39:30 +02001890 pairwise = !!mac_addr;
1891 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1892 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1893 if (kt >= NUM_NL80211_KEYTYPES)
1894 return -EINVAL;
1895 if (kt != NL80211_KEYTYPE_GROUP &&
1896 kt != NL80211_KEYTYPE_PAIRWISE)
1897 return -EINVAL;
1898 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1899 }
1900
Johannes Berg4c476992010-10-04 21:36:35 +02001901 if (!rdev->ops->get_key)
1902 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001903
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001905 if (!msg)
1906 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001907
1908 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1909 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001910 if (IS_ERR(hdr))
1911 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001912
1913 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001914 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001915
David S. Miller9360ffd2012-03-29 04:41:26 -04001916 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1917 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
1918 goto nla_put_failure;
1919 if (mac_addr &&
1920 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
1921 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001922
Johannes Berge31b8212010-10-05 19:39:30 +02001923 if (pairwise && mac_addr &&
1924 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1925 return -ENOENT;
1926
1927 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1928 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001929
1930 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001931 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001932
1933 if (cookie.error)
1934 goto nla_put_failure;
1935
1936 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001937 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001938
1939 nla_put_failure:
1940 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001941 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001942 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001943 return err;
1944}
1945
1946static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1947{
Johannes Berg4c476992010-10-04 21:36:35 +02001948 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001949 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001950 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001951 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001952
Johannes Bergb9454e82009-07-08 13:29:08 +02001953 err = nl80211_parse_key(info, &key);
1954 if (err)
1955 return err;
1956
1957 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001958 return -EINVAL;
1959
Johannes Bergb9454e82009-07-08 13:29:08 +02001960 /* only support setting default key */
1961 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001962 return -EINVAL;
1963
Johannes Bergfffd0932009-07-08 14:22:54 +02001964 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001965
1966 if (key.def) {
1967 if (!rdev->ops->set_default_key) {
1968 err = -EOPNOTSUPP;
1969 goto out;
1970 }
1971
1972 err = nl80211_key_allowed(dev->ieee80211_ptr);
1973 if (err)
1974 goto out;
1975
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001976 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1977 key.def_uni, key.def_multi);
1978
1979 if (err)
1980 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001981
Johannes Berg3d23e342009-09-29 23:27:28 +02001982#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001983 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001984#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001985 } else {
1986 if (key.def_uni || !key.def_multi) {
1987 err = -EINVAL;
1988 goto out;
1989 }
1990
1991 if (!rdev->ops->set_default_mgmt_key) {
1992 err = -EOPNOTSUPP;
1993 goto out;
1994 }
1995
1996 err = nl80211_key_allowed(dev->ieee80211_ptr);
1997 if (err)
1998 goto out;
1999
2000 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2001 dev, key.idx);
2002 if (err)
2003 goto out;
2004
2005#ifdef CONFIG_CFG80211_WEXT
2006 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2007#endif
2008 }
2009
2010 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002011 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002012
Johannes Berg41ade002007-12-19 02:03:29 +01002013 return err;
2014}
2015
2016static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2017{
Johannes Berg4c476992010-10-04 21:36:35 +02002018 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002019 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002020 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002021 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002022 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002023
Johannes Bergb9454e82009-07-08 13:29:08 +02002024 err = nl80211_parse_key(info, &key);
2025 if (err)
2026 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002027
Johannes Bergb9454e82009-07-08 13:29:08 +02002028 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002029 return -EINVAL;
2030
Johannes Berg41ade002007-12-19 02:03:29 +01002031 if (info->attrs[NL80211_ATTR_MAC])
2032 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2033
Johannes Berge31b8212010-10-05 19:39:30 +02002034 if (key.type == -1) {
2035 if (mac_addr)
2036 key.type = NL80211_KEYTYPE_PAIRWISE;
2037 else
2038 key.type = NL80211_KEYTYPE_GROUP;
2039 }
2040
2041 /* for now */
2042 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2043 key.type != NL80211_KEYTYPE_GROUP)
2044 return -EINVAL;
2045
Johannes Berg4c476992010-10-04 21:36:35 +02002046 if (!rdev->ops->add_key)
2047 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002048
Johannes Berge31b8212010-10-05 19:39:30 +02002049 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2050 key.type == NL80211_KEYTYPE_PAIRWISE,
2051 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002052 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002053
2054 wdev_lock(dev->ieee80211_ptr);
2055 err = nl80211_key_allowed(dev->ieee80211_ptr);
2056 if (!err)
2057 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002058 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002059 mac_addr, &key.p);
2060 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002061
Johannes Berg41ade002007-12-19 02:03:29 +01002062 return err;
2063}
2064
2065static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2066{
Johannes Berg4c476992010-10-04 21:36:35 +02002067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002068 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002069 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002070 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002071 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002072
Johannes Bergb9454e82009-07-08 13:29:08 +02002073 err = nl80211_parse_key(info, &key);
2074 if (err)
2075 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002076
2077 if (info->attrs[NL80211_ATTR_MAC])
2078 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2079
Johannes Berge31b8212010-10-05 19:39:30 +02002080 if (key.type == -1) {
2081 if (mac_addr)
2082 key.type = NL80211_KEYTYPE_PAIRWISE;
2083 else
2084 key.type = NL80211_KEYTYPE_GROUP;
2085 }
2086
2087 /* for now */
2088 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2089 key.type != NL80211_KEYTYPE_GROUP)
2090 return -EINVAL;
2091
Johannes Berg4c476992010-10-04 21:36:35 +02002092 if (!rdev->ops->del_key)
2093 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002094
Johannes Bergfffd0932009-07-08 14:22:54 +02002095 wdev_lock(dev->ieee80211_ptr);
2096 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002097
2098 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2099 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2100 err = -ENOENT;
2101
Johannes Bergfffd0932009-07-08 14:22:54 +02002102 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002103 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2104 key.type == NL80211_KEYTYPE_PAIRWISE,
2105 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002106
Johannes Berg3d23e342009-09-29 23:27:28 +02002107#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002108 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002109 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002110 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002111 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002112 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2113 }
2114#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002115 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002116
Johannes Berg41ade002007-12-19 02:03:29 +01002117 return err;
2118}
2119
Johannes Berg88600202012-02-13 15:17:18 +01002120static int nl80211_parse_beacon(struct genl_info *info,
2121 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002122{
Johannes Berg88600202012-02-13 15:17:18 +01002123 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002124
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002125 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2126 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2127 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2128 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002129 return -EINVAL;
2130
Johannes Berg88600202012-02-13 15:17:18 +01002131 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002132
Johannes Berged1b6cc2007-12-19 02:03:32 +01002133 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002134 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2135 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2136 if (!bcn->head_len)
2137 return -EINVAL;
2138 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002139 }
2140
2141 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002142 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2143 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002144 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002145 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002146 }
2147
Johannes Berg4c476992010-10-04 21:36:35 +02002148 if (!haveinfo)
2149 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002150
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002151 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002152 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2153 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002154 }
2155
2156 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002157 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002158 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002159 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002160 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2161 }
2162
2163 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002164 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002165 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002166 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002167 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2168 }
2169
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002170 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002171 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002172 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002173 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002174 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2175 }
2176
Johannes Berg88600202012-02-13 15:17:18 +01002177 return 0;
2178}
2179
2180static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2181{
2182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2183 struct net_device *dev = info->user_ptr[1];
2184 struct wireless_dev *wdev = dev->ieee80211_ptr;
2185 struct cfg80211_ap_settings params;
2186 int err;
2187
2188 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2189 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2190 return -EOPNOTSUPP;
2191
2192 if (!rdev->ops->start_ap)
2193 return -EOPNOTSUPP;
2194
2195 if (wdev->beacon_interval)
2196 return -EALREADY;
2197
2198 memset(&params, 0, sizeof(params));
2199
2200 /* these are required for START_AP */
2201 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2202 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2203 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2204 return -EINVAL;
2205
2206 err = nl80211_parse_beacon(info, &params.beacon);
2207 if (err)
2208 return err;
2209
2210 params.beacon_interval =
2211 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2212 params.dtim_period =
2213 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2214
2215 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2216 if (err)
2217 return err;
2218
2219 /*
2220 * In theory, some of these attributes should be required here
2221 * but since they were not used when the command was originally
2222 * added, keep them optional for old user space programs to let
2223 * them continue to work with drivers that do not need the
2224 * additional information -- drivers must check!
2225 */
2226 if (info->attrs[NL80211_ATTR_SSID]) {
2227 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2228 params.ssid_len =
2229 nla_len(info->attrs[NL80211_ATTR_SSID]);
2230 if (params.ssid_len == 0 ||
2231 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2232 return -EINVAL;
2233 }
2234
2235 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2236 params.hidden_ssid = nla_get_u32(
2237 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2238 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2239 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2240 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2241 return -EINVAL;
2242 }
2243
2244 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2245
2246 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2247 params.auth_type = nla_get_u32(
2248 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2249 if (!nl80211_valid_auth_type(params.auth_type))
2250 return -EINVAL;
2251 } else
2252 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2253
2254 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2255 NL80211_MAX_NR_CIPHER_SUITES);
2256 if (err)
2257 return err;
2258
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302259 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2260 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2261 return -EOPNOTSUPP;
2262 params.inactivity_timeout = nla_get_u16(
2263 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2264 }
2265
Johannes Berg88600202012-02-13 15:17:18 +01002266 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2267 if (!err)
2268 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002269 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002270}
2271
Johannes Berg88600202012-02-13 15:17:18 +01002272static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2273{
2274 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2275 struct net_device *dev = info->user_ptr[1];
2276 struct wireless_dev *wdev = dev->ieee80211_ptr;
2277 struct cfg80211_beacon_data params;
2278 int err;
2279
2280 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2281 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2282 return -EOPNOTSUPP;
2283
2284 if (!rdev->ops->change_beacon)
2285 return -EOPNOTSUPP;
2286
2287 if (!wdev->beacon_interval)
2288 return -EINVAL;
2289
2290 err = nl80211_parse_beacon(info, &params);
2291 if (err)
2292 return err;
2293
2294 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2295}
2296
2297static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002298{
Johannes Berg4c476992010-10-04 21:36:35 +02002299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2300 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002301 struct wireless_dev *wdev = dev->ieee80211_ptr;
2302 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002303
Johannes Berg88600202012-02-13 15:17:18 +01002304 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002305 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002306
Johannes Berg074ac8d2010-09-16 14:58:22 +02002307 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002308 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2309 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002310
Johannes Berg88600202012-02-13 15:17:18 +01002311 if (!wdev->beacon_interval)
2312 return -ENOENT;
2313
2314 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002315 if (!err)
2316 wdev->beacon_interval = 0;
2317 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002318}
2319
Johannes Berg5727ef12007-12-19 02:03:34 +01002320static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2321 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2322 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2323 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002324 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002325 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002326 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002327};
2328
Johannes Bergeccb8e82009-05-11 21:57:56 +03002329static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002330 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002331 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002332{
2333 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002334 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002335 int flag;
2336
Johannes Bergeccb8e82009-05-11 21:57:56 +03002337 /*
2338 * Try parsing the new attribute first so userspace
2339 * can specify both for older kernels.
2340 */
2341 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2342 if (nla) {
2343 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002344
Johannes Bergeccb8e82009-05-11 21:57:56 +03002345 sta_flags = nla_data(nla);
2346 params->sta_flags_mask = sta_flags->mask;
2347 params->sta_flags_set = sta_flags->set;
2348 if ((params->sta_flags_mask |
2349 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2350 return -EINVAL;
2351 return 0;
2352 }
2353
2354 /* if present, parse the old attribute */
2355
2356 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002357 if (!nla)
2358 return 0;
2359
2360 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2361 nla, sta_flags_policy))
2362 return -EINVAL;
2363
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002364 /*
2365 * Only allow certain flags for interface types so that
2366 * other attributes are silently ignored. Remember that
2367 * this is backward compatibility code with old userspace
2368 * and shouldn't be hit in other cases anyway.
2369 */
2370 switch (iftype) {
2371 case NL80211_IFTYPE_AP:
2372 case NL80211_IFTYPE_AP_VLAN:
2373 case NL80211_IFTYPE_P2P_GO:
2374 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2375 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2376 BIT(NL80211_STA_FLAG_WME) |
2377 BIT(NL80211_STA_FLAG_MFP);
2378 break;
2379 case NL80211_IFTYPE_P2P_CLIENT:
2380 case NL80211_IFTYPE_STATION:
2381 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2382 BIT(NL80211_STA_FLAG_TDLS_PEER);
2383 break;
2384 case NL80211_IFTYPE_MESH_POINT:
2385 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2386 BIT(NL80211_STA_FLAG_MFP) |
2387 BIT(NL80211_STA_FLAG_AUTHORIZED);
2388 default:
2389 return -EINVAL;
2390 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002391
2392 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2393 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002394 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002395
2396 return 0;
2397}
2398
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002399static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2400 int attr)
2401{
2402 struct nlattr *rate;
2403 u16 bitrate;
2404
2405 rate = nla_nest_start(msg, attr);
2406 if (!rate)
2407 goto nla_put_failure;
2408
2409 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2410 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002411 if ((bitrate > 0 &&
2412 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2413 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2414 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2415 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2416 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2417 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2418 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2419 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002420
2421 nla_nest_end(msg, rate);
2422 return true;
2423
2424nla_put_failure:
2425 return false;
2426}
2427
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002428static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002429 int flags,
2430 struct cfg80211_registered_device *rdev,
2431 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002432 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002433{
2434 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002435 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002436
2437 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2438 if (!hdr)
2439 return -1;
2440
David S. Miller9360ffd2012-03-29 04:41:26 -04002441 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2442 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2443 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2444 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002445
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002446 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2447 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002448 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002449 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2450 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2451 sinfo->connected_time))
2452 goto nla_put_failure;
2453 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2454 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2455 sinfo->inactive_time))
2456 goto nla_put_failure;
2457 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2458 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2459 sinfo->rx_bytes))
2460 goto nla_put_failure;
2461 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2462 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2463 sinfo->tx_bytes))
2464 goto nla_put_failure;
2465 if ((sinfo->filled & STATION_INFO_LLID) &&
2466 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2467 goto nla_put_failure;
2468 if ((sinfo->filled & STATION_INFO_PLID) &&
2469 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2470 goto nla_put_failure;
2471 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2472 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2473 sinfo->plink_state))
2474 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002475 switch (rdev->wiphy.signal_type) {
2476 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002477 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2478 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2479 sinfo->signal))
2480 goto nla_put_failure;
2481 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2482 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2483 sinfo->signal_avg))
2484 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002485 break;
2486 default:
2487 break;
2488 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002489 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002490 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2491 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002492 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002493 }
2494 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2495 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2496 NL80211_STA_INFO_RX_BITRATE))
2497 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002498 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002499 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2500 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2501 sinfo->rx_packets))
2502 goto nla_put_failure;
2503 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2504 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2505 sinfo->tx_packets))
2506 goto nla_put_failure;
2507 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2508 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2509 sinfo->tx_retries))
2510 goto nla_put_failure;
2511 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2512 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2513 sinfo->tx_failed))
2514 goto nla_put_failure;
2515 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2516 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2517 sinfo->beacon_loss_count))
2518 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002519 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2520 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2521 if (!bss_param)
2522 goto nla_put_failure;
2523
David S. Miller9360ffd2012-03-29 04:41:26 -04002524 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2525 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2526 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2527 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2528 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2529 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2530 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2531 sinfo->bss_param.dtim_period) ||
2532 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2533 sinfo->bss_param.beacon_interval))
2534 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002535
2536 nla_nest_end(msg, bss_param);
2537 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002538 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2539 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2540 sizeof(struct nl80211_sta_flag_update),
2541 &sinfo->sta_flags))
2542 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002543 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002544
David S. Miller9360ffd2012-03-29 04:41:26 -04002545 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2546 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2547 sinfo->assoc_req_ies))
2548 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002549
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002550 return genlmsg_end(msg, hdr);
2551
2552 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002553 genlmsg_cancel(msg, hdr);
2554 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002555}
2556
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002557static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002558 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002559{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002560 struct station_info sinfo;
2561 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002562 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002563 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002564 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002565 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002566
Johannes Berg67748892010-10-04 21:14:06 +02002567 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2568 if (err)
2569 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002570
2571 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002572 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002573 goto out_err;
2574 }
2575
Johannes Bergbba95fe2008-07-29 13:22:51 +02002576 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002577 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002578 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2579 mac_addr, &sinfo);
2580 if (err == -ENOENT)
2581 break;
2582 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002583 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002584
2585 if (nl80211_send_station(skb,
2586 NETLINK_CB(cb->skb).pid,
2587 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002588 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002589 &sinfo) < 0)
2590 goto out;
2591
2592 sta_idx++;
2593 }
2594
2595
2596 out:
2597 cb->args[1] = sta_idx;
2598 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002599 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002600 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002601
2602 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002603}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002604
Johannes Berg5727ef12007-12-19 02:03:34 +01002605static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2606{
Johannes Berg4c476992010-10-04 21:36:35 +02002607 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2608 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002609 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002610 struct sk_buff *msg;
2611 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002612 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002613
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002614 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002615
2616 if (!info->attrs[NL80211_ATTR_MAC])
2617 return -EINVAL;
2618
2619 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2620
Johannes Berg4c476992010-10-04 21:36:35 +02002621 if (!rdev->ops->get_station)
2622 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002623
Johannes Berg79c97e92009-07-07 03:56:12 +02002624 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002625 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002626 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002627
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002629 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002630 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002631
2632 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002633 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002634 nlmsg_free(msg);
2635 return -ENOBUFS;
2636 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002637
Johannes Berg4c476992010-10-04 21:36:35 +02002638 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002639}
2640
2641/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002642 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002643 */
Johannes Berg80b99892011-11-18 16:23:01 +01002644static struct net_device *get_vlan(struct genl_info *info,
2645 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002646{
Johannes Berg463d0182009-07-14 00:33:35 +02002647 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002648 struct net_device *v;
2649 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002650
Johannes Berg80b99892011-11-18 16:23:01 +01002651 if (!vlanattr)
2652 return NULL;
2653
2654 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2655 if (!v)
2656 return ERR_PTR(-ENODEV);
2657
2658 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2659 ret = -EINVAL;
2660 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002661 }
Johannes Berg80b99892011-11-18 16:23:01 +01002662
2663 if (!netif_running(v)) {
2664 ret = -ENETDOWN;
2665 goto error;
2666 }
2667
2668 return v;
2669 error:
2670 dev_put(v);
2671 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002672}
2673
2674static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2675{
Johannes Berg4c476992010-10-04 21:36:35 +02002676 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002677 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002678 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002679 struct station_parameters params;
2680 u8 *mac_addr = NULL;
2681
2682 memset(&params, 0, sizeof(params));
2683
2684 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002685 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002686
2687 if (info->attrs[NL80211_ATTR_STA_AID])
2688 return -EINVAL;
2689
2690 if (!info->attrs[NL80211_ATTR_MAC])
2691 return -EINVAL;
2692
2693 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2694
2695 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2696 params.supported_rates =
2697 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2698 params.supported_rates_len =
2699 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2700 }
2701
2702 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2703 params.listen_interval =
2704 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2705
Jouni Malinen36aedc92008-08-25 11:58:58 +03002706 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2707 params.ht_capa =
2708 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2709
Johannes Bergbdd90d52011-12-14 12:20:27 +01002710 if (!rdev->ops->change_station)
2711 return -EOPNOTSUPP;
2712
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002713 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002714 return -EINVAL;
2715
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002716 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2717 params.plink_action =
2718 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2719
Javier Cardona9c3990a2011-05-03 16:57:11 -07002720 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2721 params.plink_state =
2722 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2723
Johannes Berga97f4422009-06-18 17:23:43 +02002724 switch (dev->ieee80211_ptr->iftype) {
2725 case NL80211_IFTYPE_AP:
2726 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002727 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002728 /* disallow mesh-specific things */
2729 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002730 return -EINVAL;
2731
2732 /* TDLS can't be set, ... */
2733 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2734 return -EINVAL;
2735 /*
2736 * ... but don't bother the driver with it. This works around
2737 * a hostapd/wpa_supplicant issue -- it always includes the
2738 * TLDS_PEER flag in the mask even for AP mode.
2739 */
2740 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2741
2742 /* accept only the listed bits */
2743 if (params.sta_flags_mask &
2744 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2745 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2746 BIT(NL80211_STA_FLAG_WME) |
2747 BIT(NL80211_STA_FLAG_MFP)))
2748 return -EINVAL;
2749
2750 /* must be last in here for error handling */
2751 params.vlan = get_vlan(info, rdev);
2752 if (IS_ERR(params.vlan))
2753 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002754 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002755 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002756 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002757 /*
2758 * Don't allow userspace to change the TDLS_PEER flag,
2759 * but silently ignore attempts to change it since we
2760 * don't have state here to verify that it doesn't try
2761 * to change the flag.
2762 */
2763 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002764 /* fall through */
2765 case NL80211_IFTYPE_ADHOC:
2766 /* disallow things sta doesn't support */
2767 if (params.plink_action)
2768 return -EINVAL;
2769 if (params.ht_capa)
2770 return -EINVAL;
2771 if (params.listen_interval >= 0)
2772 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002773 /* reject any changes other than AUTHORIZED */
2774 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2775 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002776 break;
2777 case NL80211_IFTYPE_MESH_POINT:
2778 /* disallow things mesh doesn't support */
2779 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002780 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002781 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002782 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002783 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002784 return -EINVAL;
2785 /*
2786 * No special handling for TDLS here -- the userspace
2787 * mesh code doesn't have this bug.
2788 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002789 if (params.sta_flags_mask &
2790 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002791 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002792 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002793 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002794 break;
2795 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002796 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002797 }
2798
Johannes Bergbdd90d52011-12-14 12:20:27 +01002799 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002800
Johannes Berg79c97e92009-07-07 03:56:12 +02002801 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002802
Johannes Berg5727ef12007-12-19 02:03:34 +01002803 if (params.vlan)
2804 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002805
Johannes Berg5727ef12007-12-19 02:03:34 +01002806 return err;
2807}
2808
Eliad Pellerc75786c2011-08-23 14:37:46 +03002809static struct nla_policy
2810nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2811 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2812 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2813};
2814
Johannes Berg5727ef12007-12-19 02:03:34 +01002815static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2816{
Johannes Berg4c476992010-10-04 21:36:35 +02002817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002818 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002819 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002820 struct station_parameters params;
2821 u8 *mac_addr = NULL;
2822
2823 memset(&params, 0, sizeof(params));
2824
2825 if (!info->attrs[NL80211_ATTR_MAC])
2826 return -EINVAL;
2827
Johannes Berg5727ef12007-12-19 02:03:34 +01002828 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2829 return -EINVAL;
2830
2831 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2832 return -EINVAL;
2833
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002834 if (!info->attrs[NL80211_ATTR_STA_AID])
2835 return -EINVAL;
2836
Johannes Berg5727ef12007-12-19 02:03:34 +01002837 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2838 params.supported_rates =
2839 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2840 params.supported_rates_len =
2841 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2842 params.listen_interval =
2843 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002844
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002845 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2846 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2847 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002848
Jouni Malinen36aedc92008-08-25 11:58:58 +03002849 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2850 params.ht_capa =
2851 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002852
Javier Cardona96b78df2011-04-07 15:08:33 -07002853 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2854 params.plink_action =
2855 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2856
Johannes Bergbdd90d52011-12-14 12:20:27 +01002857 if (!rdev->ops->add_station)
2858 return -EOPNOTSUPP;
2859
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002860 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002861 return -EINVAL;
2862
Johannes Bergbdd90d52011-12-14 12:20:27 +01002863 switch (dev->ieee80211_ptr->iftype) {
2864 case NL80211_IFTYPE_AP:
2865 case NL80211_IFTYPE_AP_VLAN:
2866 case NL80211_IFTYPE_P2P_GO:
2867 /* parse WME attributes if sta is WME capable */
2868 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2869 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2870 info->attrs[NL80211_ATTR_STA_WME]) {
2871 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2872 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002873
Johannes Bergbdd90d52011-12-14 12:20:27 +01002874 nla = info->attrs[NL80211_ATTR_STA_WME];
2875 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2876 nl80211_sta_wme_policy);
2877 if (err)
2878 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002879
Johannes Bergbdd90d52011-12-14 12:20:27 +01002880 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2881 params.uapsd_queues =
2882 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2883 if (params.uapsd_queues &
2884 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2885 return -EINVAL;
2886
2887 if (tb[NL80211_STA_WME_MAX_SP])
2888 params.max_sp =
2889 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2890
2891 if (params.max_sp &
2892 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2893 return -EINVAL;
2894
2895 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2896 }
2897 /* TDLS peers cannot be added */
2898 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002899 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002900 /* but don't bother the driver with it */
2901 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002902
Johannes Bergbdd90d52011-12-14 12:20:27 +01002903 /* must be last in here for error handling */
2904 params.vlan = get_vlan(info, rdev);
2905 if (IS_ERR(params.vlan))
2906 return PTR_ERR(params.vlan);
2907 break;
2908 case NL80211_IFTYPE_MESH_POINT:
2909 /* TDLS peers cannot be added */
2910 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002911 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002912 break;
2913 case NL80211_IFTYPE_STATION:
2914 /* Only TDLS peers can be added */
2915 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2916 return -EINVAL;
2917 /* Can only add if TDLS ... */
2918 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2919 return -EOPNOTSUPP;
2920 /* ... with external setup is supported */
2921 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2922 return -EOPNOTSUPP;
2923 break;
2924 default:
2925 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002926 }
2927
Johannes Bergbdd90d52011-12-14 12:20:27 +01002928 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002929
Johannes Berg79c97e92009-07-07 03:56:12 +02002930 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002931
Johannes Berg5727ef12007-12-19 02:03:34 +01002932 if (params.vlan)
2933 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002934 return err;
2935}
2936
2937static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2938{
Johannes Berg4c476992010-10-04 21:36:35 +02002939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2940 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002941 u8 *mac_addr = NULL;
2942
2943 if (info->attrs[NL80211_ATTR_MAC])
2944 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2945
Johannes Berge80cf852009-05-11 14:43:13 +02002946 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002947 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002948 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002949 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2950 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002951
Johannes Berg4c476992010-10-04 21:36:35 +02002952 if (!rdev->ops->del_station)
2953 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002954
Johannes Berg4c476992010-10-04 21:36:35 +02002955 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002956}
2957
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002958static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2959 int flags, struct net_device *dev,
2960 u8 *dst, u8 *next_hop,
2961 struct mpath_info *pinfo)
2962{
2963 void *hdr;
2964 struct nlattr *pinfoattr;
2965
2966 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2967 if (!hdr)
2968 return -1;
2969
David S. Miller9360ffd2012-03-29 04:41:26 -04002970 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2971 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
2972 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
2973 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
2974 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002975
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002976 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2977 if (!pinfoattr)
2978 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002979 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
2980 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2981 pinfo->frame_qlen))
2982 goto nla_put_failure;
2983 if (((pinfo->filled & MPATH_INFO_SN) &&
2984 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
2985 ((pinfo->filled & MPATH_INFO_METRIC) &&
2986 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
2987 pinfo->metric)) ||
2988 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
2989 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
2990 pinfo->exptime)) ||
2991 ((pinfo->filled & MPATH_INFO_FLAGS) &&
2992 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
2993 pinfo->flags)) ||
2994 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
2995 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2996 pinfo->discovery_timeout)) ||
2997 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
2998 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2999 pinfo->discovery_retries)))
3000 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003001
3002 nla_nest_end(msg, pinfoattr);
3003
3004 return genlmsg_end(msg, hdr);
3005
3006 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003007 genlmsg_cancel(msg, hdr);
3008 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003009}
3010
3011static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003012 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003013{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003014 struct mpath_info pinfo;
3015 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003016 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003017 u8 dst[ETH_ALEN];
3018 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003019 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003020 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003021
Johannes Berg67748892010-10-04 21:14:06 +02003022 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3023 if (err)
3024 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003025
3026 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003027 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003028 goto out_err;
3029 }
3030
Jouni Malineneec60b02009-03-20 21:21:19 +02003031 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3032 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003033 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003034 }
3035
Johannes Bergbba95fe2008-07-29 13:22:51 +02003036 while (1) {
3037 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3038 dst, next_hop, &pinfo);
3039 if (err == -ENOENT)
3040 break;
3041 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003042 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003043
3044 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3045 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3046 netdev, dst, next_hop,
3047 &pinfo) < 0)
3048 goto out;
3049
3050 path_idx++;
3051 }
3052
3053
3054 out:
3055 cb->args[1] = path_idx;
3056 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003057 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003058 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003059 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003060}
3061
3062static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3063{
Johannes Berg4c476992010-10-04 21:36:35 +02003064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003065 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003066 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003067 struct mpath_info pinfo;
3068 struct sk_buff *msg;
3069 u8 *dst = NULL;
3070 u8 next_hop[ETH_ALEN];
3071
3072 memset(&pinfo, 0, sizeof(pinfo));
3073
3074 if (!info->attrs[NL80211_ATTR_MAC])
3075 return -EINVAL;
3076
3077 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3078
Johannes Berg4c476992010-10-04 21:36:35 +02003079 if (!rdev->ops->get_mpath)
3080 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003081
Johannes Berg4c476992010-10-04 21:36:35 +02003082 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3083 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003084
Johannes Berg79c97e92009-07-07 03:56:12 +02003085 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003086 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003087 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003088
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003090 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003091 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003092
3093 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003094 dev, dst, next_hop, &pinfo) < 0) {
3095 nlmsg_free(msg);
3096 return -ENOBUFS;
3097 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003098
Johannes Berg4c476992010-10-04 21:36:35 +02003099 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003100}
3101
3102static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3103{
Johannes Berg4c476992010-10-04 21:36:35 +02003104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3105 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003106 u8 *dst = NULL;
3107 u8 *next_hop = NULL;
3108
3109 if (!info->attrs[NL80211_ATTR_MAC])
3110 return -EINVAL;
3111
3112 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3113 return -EINVAL;
3114
3115 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3116 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3117
Johannes Berg4c476992010-10-04 21:36:35 +02003118 if (!rdev->ops->change_mpath)
3119 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003120
Johannes Berg4c476992010-10-04 21:36:35 +02003121 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3122 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003123
Johannes Berg4c476992010-10-04 21:36:35 +02003124 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003125}
Johannes Berg4c476992010-10-04 21:36:35 +02003126
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003127static int nl80211_new_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->add_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->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003150}
3151
3152static int nl80211_del_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
3158 if (info->attrs[NL80211_ATTR_MAC])
3159 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3160
Johannes Berg4c476992010-10-04 21:36:35 +02003161 if (!rdev->ops->del_mpath)
3162 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003163
Johannes Berg4c476992010-10-04 21:36:35 +02003164 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003165}
3166
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003167static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3168{
Johannes Berg4c476992010-10-04 21:36:35 +02003169 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3170 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003171 struct bss_parameters params;
3172
3173 memset(&params, 0, sizeof(params));
3174 /* default to not changing parameters */
3175 params.use_cts_prot = -1;
3176 params.use_short_preamble = -1;
3177 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003178 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003179 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003180
3181 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3182 params.use_cts_prot =
3183 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3184 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3185 params.use_short_preamble =
3186 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3187 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3188 params.use_short_slot_time =
3189 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003190 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3191 params.basic_rates =
3192 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3193 params.basic_rates_len =
3194 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3195 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003196 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3197 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003198 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3199 params.ht_opmode =
3200 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003201
Johannes Berg4c476992010-10-04 21:36:35 +02003202 if (!rdev->ops->change_bss)
3203 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003204
Johannes Berg074ac8d2010-09-16 14:58:22 +02003205 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003206 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3207 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003208
Johannes Berg4c476992010-10-04 21:36:35 +02003209 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003210}
3211
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003212static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003213 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3214 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3215 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3216 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3217 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3218 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3219};
3220
3221static int parse_reg_rule(struct nlattr *tb[],
3222 struct ieee80211_reg_rule *reg_rule)
3223{
3224 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3225 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3226
3227 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3228 return -EINVAL;
3229 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3230 return -EINVAL;
3231 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3232 return -EINVAL;
3233 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3234 return -EINVAL;
3235 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3236 return -EINVAL;
3237
3238 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3239
3240 freq_range->start_freq_khz =
3241 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3242 freq_range->end_freq_khz =
3243 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3244 freq_range->max_bandwidth_khz =
3245 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3246
3247 power_rule->max_eirp =
3248 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3249
3250 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3251 power_rule->max_antenna_gain =
3252 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3253
3254 return 0;
3255}
3256
3257static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3258{
3259 int r;
3260 char *data = NULL;
3261
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003262 /*
3263 * You should only get this when cfg80211 hasn't yet initialized
3264 * completely when built-in to the kernel right between the time
3265 * window between nl80211_init() and regulatory_init(), if that is
3266 * even possible.
3267 */
3268 mutex_lock(&cfg80211_mutex);
3269 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003270 mutex_unlock(&cfg80211_mutex);
3271 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003272 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003273 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003274
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003275 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3276 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003277
3278 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3279
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003280 r = regulatory_hint_user(data);
3281
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003282 return r;
3283}
3284
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003285static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003286 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003287{
Johannes Berg4c476992010-10-04 21:36:35 +02003288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003289 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003290 struct wireless_dev *wdev = dev->ieee80211_ptr;
3291 struct mesh_config cur_params;
3292 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003293 void *hdr;
3294 struct nlattr *pinfoattr;
3295 struct sk_buff *msg;
3296
Johannes Berg29cbe682010-12-03 09:20:44 +01003297 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3298 return -EOPNOTSUPP;
3299
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003300 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003301 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003302
Johannes Berg29cbe682010-12-03 09:20:44 +01003303 wdev_lock(wdev);
3304 /* If not connected, get default parameters */
3305 if (!wdev->mesh_id_len)
3306 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3307 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003308 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003309 &cur_params);
3310 wdev_unlock(wdev);
3311
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003312 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003313 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003314
3315 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003316 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003317 if (!msg)
3318 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003319 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003320 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003321 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003322 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003323 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003324 if (!pinfoattr)
3325 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003326 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3327 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3328 cur_params.dot11MeshRetryTimeout) ||
3329 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3330 cur_params.dot11MeshConfirmTimeout) ||
3331 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3332 cur_params.dot11MeshHoldingTimeout) ||
3333 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3334 cur_params.dot11MeshMaxPeerLinks) ||
3335 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3336 cur_params.dot11MeshMaxRetries) ||
3337 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3338 cur_params.dot11MeshTTL) ||
3339 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3340 cur_params.element_ttl) ||
3341 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3342 cur_params.auto_open_plinks) ||
3343 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3344 cur_params.dot11MeshHWMPmaxPREQretries) ||
3345 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3346 cur_params.path_refresh_time) ||
3347 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3348 cur_params.min_discovery_timeout) ||
3349 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3350 cur_params.dot11MeshHWMPactivePathTimeout) ||
3351 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3352 cur_params.dot11MeshHWMPpreqMinInterval) ||
3353 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3354 cur_params.dot11MeshHWMPperrMinInterval) ||
3355 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3356 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3357 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3358 cur_params.dot11MeshHWMPRootMode) ||
3359 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3360 cur_params.dot11MeshHWMPRannInterval) ||
3361 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3362 cur_params.dot11MeshGateAnnouncementProtocol) ||
3363 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3364 cur_params.dot11MeshForwarding) ||
3365 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3366 cur_params.rssi_threshold))
3367 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003368 nla_nest_end(msg, pinfoattr);
3369 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003370 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003371
Johannes Berg3b858752009-03-12 09:55:09 +01003372 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003373 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003374 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003375 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003376 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003377}
3378
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003379static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003380 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3381 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3382 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3383 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3384 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3385 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003386 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003387 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3388
3389 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3390 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3391 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3392 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3393 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003394 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003395 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003396 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003397 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003398 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003399 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003400 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003401};
3402
Javier Cardonac80d5452010-12-16 17:37:49 -08003403static const struct nla_policy
3404 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3405 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3406 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003407 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003408 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003409 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003410 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003411};
3412
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003413static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003414 struct mesh_config *cfg,
3415 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003416{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003417 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003418 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003419
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003420#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3421do {\
3422 if (table[attr_num]) {\
3423 cfg->param = nla_fn(table[attr_num]); \
3424 mask |= (1 << (attr_num - 1)); \
3425 } \
3426} while (0);\
3427
3428
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003429 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003430 return -EINVAL;
3431 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003432 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003433 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003434 return -EINVAL;
3435
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003436 /* This makes sure that there aren't more than 32 mesh config
3437 * parameters (otherwise our bitfield scheme would not work.) */
3438 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3439
3440 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003441 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3442 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3443 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3444 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3445 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3446 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3447 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3448 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3449 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3450 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3451 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3452 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003453 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3454 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003455 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3456 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3457 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3458 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3459 nla_get_u8);
3460 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3461 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3462 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3463 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3464 nla_get_u16);
3465 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3466 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3467 nla_get_u32);
3468 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3469 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3470 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003471 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3472 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3473 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003474 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3475 dot11MeshHWMPnetDiameterTraversalTime,
3476 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3477 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003478 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3479 dot11MeshHWMPRootMode, mask,
3480 NL80211_MESHCONF_HWMP_ROOTMODE,
3481 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003482 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3483 dot11MeshHWMPRannInterval, mask,
3484 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3485 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003486 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3487 dot11MeshGateAnnouncementProtocol, mask,
3488 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3489 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003490 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3491 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003492 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3493 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003494 if (mask_out)
3495 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003496
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003497 return 0;
3498
3499#undef FILL_IN_MESH_PARAM_IF_SET
3500}
3501
Javier Cardonac80d5452010-12-16 17:37:49 -08003502static int nl80211_parse_mesh_setup(struct genl_info *info,
3503 struct mesh_setup *setup)
3504{
3505 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3506
3507 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3508 return -EINVAL;
3509 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3510 info->attrs[NL80211_ATTR_MESH_SETUP],
3511 nl80211_mesh_setup_params_policy))
3512 return -EINVAL;
3513
3514 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3515 setup->path_sel_proto =
3516 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3517 IEEE80211_PATH_PROTOCOL_VENDOR :
3518 IEEE80211_PATH_PROTOCOL_HWMP;
3519
3520 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3521 setup->path_metric =
3522 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3523 IEEE80211_PATH_METRIC_VENDOR :
3524 IEEE80211_PATH_METRIC_AIRTIME;
3525
Javier Cardona581a8b02011-04-07 15:08:27 -07003526
3527 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003528 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003529 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003530 if (!is_valid_ie_attr(ieattr))
3531 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003532 setup->ie = nla_data(ieattr);
3533 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003534 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003535 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3536 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003537
3538 return 0;
3539}
3540
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003541static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003542 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003543{
3544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3545 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003546 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003547 struct mesh_config cfg;
3548 u32 mask;
3549 int err;
3550
Johannes Berg29cbe682010-12-03 09:20:44 +01003551 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3552 return -EOPNOTSUPP;
3553
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003554 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003555 return -EOPNOTSUPP;
3556
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003557 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003558 if (err)
3559 return err;
3560
Johannes Berg29cbe682010-12-03 09:20:44 +01003561 wdev_lock(wdev);
3562 if (!wdev->mesh_id_len)
3563 err = -ENOLINK;
3564
3565 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003566 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003567 mask, &cfg);
3568
3569 wdev_unlock(wdev);
3570
3571 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003572}
3573
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003574static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3575{
3576 struct sk_buff *msg;
3577 void *hdr = NULL;
3578 struct nlattr *nl_reg_rules;
3579 unsigned int i;
3580 int err = -EINVAL;
3581
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003582 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003583
3584 if (!cfg80211_regdomain)
3585 goto out;
3586
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003587 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003588 if (!msg) {
3589 err = -ENOBUFS;
3590 goto out;
3591 }
3592
3593 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3594 NL80211_CMD_GET_REG);
3595 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003596 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003597
David S. Miller9360ffd2012-03-29 04:41:26 -04003598 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3599 cfg80211_regdomain->alpha2) ||
3600 (cfg80211_regdomain->dfs_region &&
3601 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3602 cfg80211_regdomain->dfs_region)))
3603 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003604
3605 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3606 if (!nl_reg_rules)
3607 goto nla_put_failure;
3608
3609 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3610 struct nlattr *nl_reg_rule;
3611 const struct ieee80211_reg_rule *reg_rule;
3612 const struct ieee80211_freq_range *freq_range;
3613 const struct ieee80211_power_rule *power_rule;
3614
3615 reg_rule = &cfg80211_regdomain->reg_rules[i];
3616 freq_range = &reg_rule->freq_range;
3617 power_rule = &reg_rule->power_rule;
3618
3619 nl_reg_rule = nla_nest_start(msg, i);
3620 if (!nl_reg_rule)
3621 goto nla_put_failure;
3622
David S. Miller9360ffd2012-03-29 04:41:26 -04003623 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3624 reg_rule->flags) ||
3625 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3626 freq_range->start_freq_khz) ||
3627 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3628 freq_range->end_freq_khz) ||
3629 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3630 freq_range->max_bandwidth_khz) ||
3631 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3632 power_rule->max_antenna_gain) ||
3633 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3634 power_rule->max_eirp))
3635 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003636
3637 nla_nest_end(msg, nl_reg_rule);
3638 }
3639
3640 nla_nest_end(msg, nl_reg_rules);
3641
3642 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003643 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003644 goto out;
3645
3646nla_put_failure:
3647 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003648put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003649 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003650 err = -EMSGSIZE;
3651out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003652 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003653 return err;
3654}
3655
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003656static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3657{
3658 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3659 struct nlattr *nl_reg_rule;
3660 char *alpha2 = NULL;
3661 int rem_reg_rules = 0, r = 0;
3662 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003663 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003664 struct ieee80211_regdomain *rd = NULL;
3665
3666 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3667 return -EINVAL;
3668
3669 if (!info->attrs[NL80211_ATTR_REG_RULES])
3670 return -EINVAL;
3671
3672 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3673
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003674 if (info->attrs[NL80211_ATTR_DFS_REGION])
3675 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3676
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003677 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3678 rem_reg_rules) {
3679 num_rules++;
3680 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003681 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003682 }
3683
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003684 mutex_lock(&cfg80211_mutex);
3685
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003686 if (!reg_is_valid_request(alpha2)) {
3687 r = -EINVAL;
3688 goto bad_reg;
3689 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003690
3691 size_of_regd = sizeof(struct ieee80211_regdomain) +
3692 (num_rules * sizeof(struct ieee80211_reg_rule));
3693
3694 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003695 if (!rd) {
3696 r = -ENOMEM;
3697 goto bad_reg;
3698 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003699
3700 rd->n_reg_rules = num_rules;
3701 rd->alpha2[0] = alpha2[0];
3702 rd->alpha2[1] = alpha2[1];
3703
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003704 /*
3705 * Disable DFS master mode if the DFS region was
3706 * not supported or known on this kernel.
3707 */
3708 if (reg_supported_dfs_region(dfs_region))
3709 rd->dfs_region = dfs_region;
3710
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003711 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3712 rem_reg_rules) {
3713 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3714 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3715 reg_rule_policy);
3716 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3717 if (r)
3718 goto bad_reg;
3719
3720 rule_idx++;
3721
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003722 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3723 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003724 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003725 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003726 }
3727
3728 BUG_ON(rule_idx != num_rules);
3729
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003730 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003731
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003732 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003733
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003734 return r;
3735
Johannes Bergd2372b32008-10-24 20:32:20 +02003736 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003737 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003738 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003739 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003740}
3741
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003742static int validate_scan_freqs(struct nlattr *freqs)
3743{
3744 struct nlattr *attr1, *attr2;
3745 int n_channels = 0, tmp1, tmp2;
3746
3747 nla_for_each_nested(attr1, freqs, tmp1) {
3748 n_channels++;
3749 /*
3750 * Some hardware has a limited channel list for
3751 * scanning, and it is pretty much nonsensical
3752 * to scan for a channel twice, so disallow that
3753 * and don't require drivers to check that the
3754 * channel list they get isn't longer than what
3755 * they can scan, as long as they can scan all
3756 * the channels they registered at once.
3757 */
3758 nla_for_each_nested(attr2, freqs, tmp2)
3759 if (attr1 != attr2 &&
3760 nla_get_u32(attr1) == nla_get_u32(attr2))
3761 return 0;
3762 }
3763
3764 return n_channels;
3765}
3766
Johannes Berg2a519312009-02-10 21:25:55 +01003767static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3768{
Johannes Berg4c476992010-10-04 21:36:35 +02003769 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3770 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003771 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003772 struct nlattr *attr;
3773 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003774 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003775 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003776
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003777 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3778 return -EINVAL;
3779
Johannes Berg79c97e92009-07-07 03:56:12 +02003780 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003781
Johannes Berg4c476992010-10-04 21:36:35 +02003782 if (!rdev->ops->scan)
3783 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003784
Johannes Berg4c476992010-10-04 21:36:35 +02003785 if (rdev->scan_req)
3786 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003787
3788 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003789 n_channels = validate_scan_freqs(
3790 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003791 if (!n_channels)
3792 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003793 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003794 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003795 n_channels = 0;
3796
Johannes Berg2a519312009-02-10 21:25:55 +01003797 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3798 if (wiphy->bands[band])
3799 n_channels += wiphy->bands[band]->n_channels;
3800 }
3801
3802 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3803 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3804 n_ssids++;
3805
Johannes Berg4c476992010-10-04 21:36:35 +02003806 if (n_ssids > wiphy->max_scan_ssids)
3807 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003808
Jouni Malinen70692ad2009-02-16 19:39:13 +02003809 if (info->attrs[NL80211_ATTR_IE])
3810 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3811 else
3812 ie_len = 0;
3813
Johannes Berg4c476992010-10-04 21:36:35 +02003814 if (ie_len > wiphy->max_scan_ie_len)
3815 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003816
Johannes Berg2a519312009-02-10 21:25:55 +01003817 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003818 + sizeof(*request->ssids) * n_ssids
3819 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003820 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003821 if (!request)
3822 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003823
Johannes Berg2a519312009-02-10 21:25:55 +01003824 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003825 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003826 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003827 if (ie_len) {
3828 if (request->ssids)
3829 request->ie = (void *)(request->ssids + n_ssids);
3830 else
3831 request->ie = (void *)(request->channels + n_channels);
3832 }
Johannes Berg2a519312009-02-10 21:25:55 +01003833
Johannes Berg584991d2009-11-02 13:32:03 +01003834 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003835 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3836 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003837 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003838 struct ieee80211_channel *chan;
3839
3840 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3841
3842 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003843 err = -EINVAL;
3844 goto out_free;
3845 }
Johannes Berg584991d2009-11-02 13:32:03 +01003846
3847 /* ignore disabled channels */
3848 if (chan->flags & IEEE80211_CHAN_DISABLED)
3849 continue;
3850
3851 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003852 i++;
3853 }
3854 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003855 enum ieee80211_band band;
3856
Johannes Berg2a519312009-02-10 21:25:55 +01003857 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003858 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3859 int j;
3860 if (!wiphy->bands[band])
3861 continue;
3862 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003863 struct ieee80211_channel *chan;
3864
3865 chan = &wiphy->bands[band]->channels[j];
3866
3867 if (chan->flags & IEEE80211_CHAN_DISABLED)
3868 continue;
3869
3870 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003871 i++;
3872 }
3873 }
3874 }
3875
Johannes Berg584991d2009-11-02 13:32:03 +01003876 if (!i) {
3877 err = -EINVAL;
3878 goto out_free;
3879 }
3880
3881 request->n_channels = i;
3882
Johannes Berg2a519312009-02-10 21:25:55 +01003883 i = 0;
3884 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3885 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003886 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003887 err = -EINVAL;
3888 goto out_free;
3889 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003890 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003891 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003892 i++;
3893 }
3894 }
3895
Jouni Malinen70692ad2009-02-16 19:39:13 +02003896 if (info->attrs[NL80211_ATTR_IE]) {
3897 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003898 memcpy((void *)request->ie,
3899 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003900 request->ie_len);
3901 }
3902
Johannes Berg34850ab2011-07-18 18:08:35 +02003903 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003904 if (wiphy->bands[i])
3905 request->rates[i] =
3906 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003907
3908 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3909 nla_for_each_nested(attr,
3910 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3911 tmp) {
3912 enum ieee80211_band band = nla_type(attr);
3913
Dan Carpenter84404622011-07-29 11:52:18 +03003914 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003915 err = -EINVAL;
3916 goto out_free;
3917 }
3918 err = ieee80211_get_ratemask(wiphy->bands[band],
3919 nla_data(attr),
3920 nla_len(attr),
3921 &request->rates[band]);
3922 if (err)
3923 goto out_free;
3924 }
3925 }
3926
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303927 request->no_cck =
3928 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3929
Johannes Berg463d0182009-07-14 00:33:35 +02003930 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003931 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003932
Johannes Berg79c97e92009-07-07 03:56:12 +02003933 rdev->scan_req = request;
3934 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003935
Johannes Berg463d0182009-07-14 00:33:35 +02003936 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003937 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003938 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003939 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003940 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003941 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003942 kfree(request);
3943 }
Johannes Berg3b858752009-03-12 09:55:09 +01003944
Johannes Berg2a519312009-02-10 21:25:55 +01003945 return err;
3946}
3947
Luciano Coelho807f8a82011-05-11 17:09:35 +03003948static int nl80211_start_sched_scan(struct sk_buff *skb,
3949 struct genl_info *info)
3950{
3951 struct cfg80211_sched_scan_request *request;
3952 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3953 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003954 struct nlattr *attr;
3955 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003956 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003957 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003958 enum ieee80211_band band;
3959 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003960 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003961
3962 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3963 !rdev->ops->sched_scan_start)
3964 return -EOPNOTSUPP;
3965
3966 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3967 return -EINVAL;
3968
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003969 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3970 return -EINVAL;
3971
3972 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3973 if (interval == 0)
3974 return -EINVAL;
3975
Luciano Coelho807f8a82011-05-11 17:09:35 +03003976 wiphy = &rdev->wiphy;
3977
3978 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3979 n_channels = validate_scan_freqs(
3980 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3981 if (!n_channels)
3982 return -EINVAL;
3983 } else {
3984 n_channels = 0;
3985
3986 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3987 if (wiphy->bands[band])
3988 n_channels += wiphy->bands[band]->n_channels;
3989 }
3990
3991 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3992 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3993 tmp)
3994 n_ssids++;
3995
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003996 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003997 return -EINVAL;
3998
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003999 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4000 nla_for_each_nested(attr,
4001 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4002 tmp)
4003 n_match_sets++;
4004
4005 if (n_match_sets > wiphy->max_match_sets)
4006 return -EINVAL;
4007
Luciano Coelho807f8a82011-05-11 17:09:35 +03004008 if (info->attrs[NL80211_ATTR_IE])
4009 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4010 else
4011 ie_len = 0;
4012
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004013 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004014 return -EINVAL;
4015
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004016 mutex_lock(&rdev->sched_scan_mtx);
4017
4018 if (rdev->sched_scan_req) {
4019 err = -EINPROGRESS;
4020 goto out;
4021 }
4022
Luciano Coelho807f8a82011-05-11 17:09:35 +03004023 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004024 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004025 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004026 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004027 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004028 if (!request) {
4029 err = -ENOMEM;
4030 goto out;
4031 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004032
4033 if (n_ssids)
4034 request->ssids = (void *)&request->channels[n_channels];
4035 request->n_ssids = n_ssids;
4036 if (ie_len) {
4037 if (request->ssids)
4038 request->ie = (void *)(request->ssids + n_ssids);
4039 else
4040 request->ie = (void *)(request->channels + n_channels);
4041 }
4042
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004043 if (n_match_sets) {
4044 if (request->ie)
4045 request->match_sets = (void *)(request->ie + ie_len);
4046 else if (request->ssids)
4047 request->match_sets =
4048 (void *)(request->ssids + n_ssids);
4049 else
4050 request->match_sets =
4051 (void *)(request->channels + n_channels);
4052 }
4053 request->n_match_sets = n_match_sets;
4054
Luciano Coelho807f8a82011-05-11 17:09:35 +03004055 i = 0;
4056 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4057 /* user specified, bail out if channel not found */
4058 nla_for_each_nested(attr,
4059 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4060 tmp) {
4061 struct ieee80211_channel *chan;
4062
4063 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4064
4065 if (!chan) {
4066 err = -EINVAL;
4067 goto out_free;
4068 }
4069
4070 /* ignore disabled channels */
4071 if (chan->flags & IEEE80211_CHAN_DISABLED)
4072 continue;
4073
4074 request->channels[i] = chan;
4075 i++;
4076 }
4077 } else {
4078 /* all channels */
4079 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4080 int j;
4081 if (!wiphy->bands[band])
4082 continue;
4083 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4084 struct ieee80211_channel *chan;
4085
4086 chan = &wiphy->bands[band]->channels[j];
4087
4088 if (chan->flags & IEEE80211_CHAN_DISABLED)
4089 continue;
4090
4091 request->channels[i] = chan;
4092 i++;
4093 }
4094 }
4095 }
4096
4097 if (!i) {
4098 err = -EINVAL;
4099 goto out_free;
4100 }
4101
4102 request->n_channels = i;
4103
4104 i = 0;
4105 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4106 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4107 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004108 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004109 err = -EINVAL;
4110 goto out_free;
4111 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004112 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004113 memcpy(request->ssids[i].ssid, nla_data(attr),
4114 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004115 i++;
4116 }
4117 }
4118
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004119 i = 0;
4120 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4121 nla_for_each_nested(attr,
4122 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4123 tmp) {
4124 struct nlattr *ssid;
4125
4126 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4127 nla_data(attr), nla_len(attr),
4128 nl80211_match_policy);
4129 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4130 if (ssid) {
4131 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4132 err = -EINVAL;
4133 goto out_free;
4134 }
4135 memcpy(request->match_sets[i].ssid.ssid,
4136 nla_data(ssid), nla_len(ssid));
4137 request->match_sets[i].ssid.ssid_len =
4138 nla_len(ssid);
4139 }
4140 i++;
4141 }
4142 }
4143
Luciano Coelho807f8a82011-05-11 17:09:35 +03004144 if (info->attrs[NL80211_ATTR_IE]) {
4145 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4146 memcpy((void *)request->ie,
4147 nla_data(info->attrs[NL80211_ATTR_IE]),
4148 request->ie_len);
4149 }
4150
4151 request->dev = dev;
4152 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004153 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004154
4155 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4156 if (!err) {
4157 rdev->sched_scan_req = request;
4158 nl80211_send_sched_scan(rdev, dev,
4159 NL80211_CMD_START_SCHED_SCAN);
4160 goto out;
4161 }
4162
4163out_free:
4164 kfree(request);
4165out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004166 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004167 return err;
4168}
4169
4170static int nl80211_stop_sched_scan(struct sk_buff *skb,
4171 struct genl_info *info)
4172{
4173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004174 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004175
4176 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4177 !rdev->ops->sched_scan_stop)
4178 return -EOPNOTSUPP;
4179
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004180 mutex_lock(&rdev->sched_scan_mtx);
4181 err = __cfg80211_stop_sched_scan(rdev, false);
4182 mutex_unlock(&rdev->sched_scan_mtx);
4183
4184 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004185}
4186
Johannes Berg9720bb32011-06-21 09:45:33 +02004187static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4188 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004189 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004190 struct wireless_dev *wdev,
4191 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004192{
Johannes Berg48ab9052009-07-10 18:42:31 +02004193 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004194 void *hdr;
4195 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004196
4197 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004198
Johannes Berg9720bb32011-06-21 09:45:33 +02004199 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004200 NL80211_CMD_NEW_SCAN_RESULTS);
4201 if (!hdr)
4202 return -1;
4203
Johannes Berg9720bb32011-06-21 09:45:33 +02004204 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4205
David S. Miller9360ffd2012-03-29 04:41:26 -04004206 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4207 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4208 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004209
4210 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4211 if (!bss)
4212 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004213 if ((!is_zero_ether_addr(res->bssid) &&
4214 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4215 (res->information_elements && res->len_information_elements &&
4216 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4217 res->len_information_elements,
4218 res->information_elements)) ||
4219 (res->beacon_ies && res->len_beacon_ies &&
4220 res->beacon_ies != res->information_elements &&
4221 nla_put(msg, NL80211_BSS_BEACON_IES,
4222 res->len_beacon_ies, res->beacon_ies)))
4223 goto nla_put_failure;
4224 if (res->tsf &&
4225 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4226 goto nla_put_failure;
4227 if (res->beacon_interval &&
4228 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4229 goto nla_put_failure;
4230 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4231 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4232 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4233 jiffies_to_msecs(jiffies - intbss->ts)))
4234 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004235
Johannes Berg77965c92009-02-18 18:45:06 +01004236 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004237 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004238 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4239 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004240 break;
4241 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004242 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4243 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004244 break;
4245 default:
4246 break;
4247 }
4248
Johannes Berg48ab9052009-07-10 18:42:31 +02004249 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004250 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004251 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004252 if (intbss == wdev->current_bss &&
4253 nla_put_u32(msg, NL80211_BSS_STATUS,
4254 NL80211_BSS_STATUS_ASSOCIATED))
4255 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004256 break;
4257 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004258 if (intbss == wdev->current_bss &&
4259 nla_put_u32(msg, NL80211_BSS_STATUS,
4260 NL80211_BSS_STATUS_IBSS_JOINED))
4261 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004262 break;
4263 default:
4264 break;
4265 }
4266
Johannes Berg2a519312009-02-10 21:25:55 +01004267 nla_nest_end(msg, bss);
4268
4269 return genlmsg_end(msg, hdr);
4270
4271 nla_put_failure:
4272 genlmsg_cancel(msg, hdr);
4273 return -EMSGSIZE;
4274}
4275
4276static int nl80211_dump_scan(struct sk_buff *skb,
4277 struct netlink_callback *cb)
4278{
Johannes Berg48ab9052009-07-10 18:42:31 +02004279 struct cfg80211_registered_device *rdev;
4280 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004281 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004282 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004283 int start = cb->args[1], idx = 0;
4284 int err;
4285
Johannes Berg67748892010-10-04 21:14:06 +02004286 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4287 if (err)
4288 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004289
Johannes Berg48ab9052009-07-10 18:42:31 +02004290 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004291
Johannes Berg48ab9052009-07-10 18:42:31 +02004292 wdev_lock(wdev);
4293 spin_lock_bh(&rdev->bss_lock);
4294 cfg80211_bss_expire(rdev);
4295
Johannes Berg9720bb32011-06-21 09:45:33 +02004296 cb->seq = rdev->bss_generation;
4297
Johannes Berg48ab9052009-07-10 18:42:31 +02004298 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004299 if (++idx <= start)
4300 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004301 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004302 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004303 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004304 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004305 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004306 }
4307 }
4308
Johannes Berg48ab9052009-07-10 18:42:31 +02004309 spin_unlock_bh(&rdev->bss_lock);
4310 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004311
4312 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004313 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004314
Johannes Berg67748892010-10-04 21:14:06 +02004315 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004316}
4317
Holger Schurig61fa7132009-11-11 12:25:40 +01004318static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4319 int flags, struct net_device *dev,
4320 struct survey_info *survey)
4321{
4322 void *hdr;
4323 struct nlattr *infoattr;
4324
Holger Schurig61fa7132009-11-11 12:25:40 +01004325 hdr = nl80211hdr_put(msg, pid, seq, flags,
4326 NL80211_CMD_NEW_SURVEY_RESULTS);
4327 if (!hdr)
4328 return -ENOMEM;
4329
David S. Miller9360ffd2012-03-29 04:41:26 -04004330 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4331 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004332
4333 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4334 if (!infoattr)
4335 goto nla_put_failure;
4336
David S. Miller9360ffd2012-03-29 04:41:26 -04004337 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4338 survey->channel->center_freq))
4339 goto nla_put_failure;
4340
4341 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4342 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4343 goto nla_put_failure;
4344 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4345 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4346 goto nla_put_failure;
4347 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4348 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4349 survey->channel_time))
4350 goto nla_put_failure;
4351 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4352 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4353 survey->channel_time_busy))
4354 goto nla_put_failure;
4355 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4356 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4357 survey->channel_time_ext_busy))
4358 goto nla_put_failure;
4359 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4360 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4361 survey->channel_time_rx))
4362 goto nla_put_failure;
4363 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4364 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4365 survey->channel_time_tx))
4366 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004367
4368 nla_nest_end(msg, infoattr);
4369
4370 return genlmsg_end(msg, hdr);
4371
4372 nla_put_failure:
4373 genlmsg_cancel(msg, hdr);
4374 return -EMSGSIZE;
4375}
4376
4377static int nl80211_dump_survey(struct sk_buff *skb,
4378 struct netlink_callback *cb)
4379{
4380 struct survey_info survey;
4381 struct cfg80211_registered_device *dev;
4382 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004383 int survey_idx = cb->args[1];
4384 int res;
4385
Johannes Berg67748892010-10-04 21:14:06 +02004386 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4387 if (res)
4388 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004389
4390 if (!dev->ops->dump_survey) {
4391 res = -EOPNOTSUPP;
4392 goto out_err;
4393 }
4394
4395 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004396 struct ieee80211_channel *chan;
4397
Holger Schurig61fa7132009-11-11 12:25:40 +01004398 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4399 &survey);
4400 if (res == -ENOENT)
4401 break;
4402 if (res)
4403 goto out_err;
4404
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004405 /* Survey without a channel doesn't make sense */
4406 if (!survey.channel) {
4407 res = -EINVAL;
4408 goto out;
4409 }
4410
4411 chan = ieee80211_get_channel(&dev->wiphy,
4412 survey.channel->center_freq);
4413 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4414 survey_idx++;
4415 continue;
4416 }
4417
Holger Schurig61fa7132009-11-11 12:25:40 +01004418 if (nl80211_send_survey(skb,
4419 NETLINK_CB(cb->skb).pid,
4420 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4421 netdev,
4422 &survey) < 0)
4423 goto out;
4424 survey_idx++;
4425 }
4426
4427 out:
4428 cb->args[1] = survey_idx;
4429 res = skb->len;
4430 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004431 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004432 return res;
4433}
4434
Jouni Malinen255e7372009-03-20 21:21:17 +02004435static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4436{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004437 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004438}
4439
Samuel Ortizb23aa672009-07-01 21:26:54 +02004440static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4441{
4442 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4443 NL80211_WPA_VERSION_2));
4444}
4445
Jouni Malinen636a5d32009-03-19 13:39:22 +02004446static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4447{
Johannes Berg4c476992010-10-04 21:36:35 +02004448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4449 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004450 struct ieee80211_channel *chan;
4451 const u8 *bssid, *ssid, *ie = NULL;
4452 int err, ssid_len, ie_len = 0;
4453 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004454 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004455 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004456
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004457 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4458 return -EINVAL;
4459
4460 if (!info->attrs[NL80211_ATTR_MAC])
4461 return -EINVAL;
4462
Jouni Malinen17780922009-03-27 20:52:47 +02004463 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4464 return -EINVAL;
4465
Johannes Berg19957bb2009-07-02 17:20:43 +02004466 if (!info->attrs[NL80211_ATTR_SSID])
4467 return -EINVAL;
4468
4469 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4470 return -EINVAL;
4471
Johannes Bergfffd0932009-07-08 14:22:54 +02004472 err = nl80211_parse_key(info, &key);
4473 if (err)
4474 return err;
4475
4476 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004477 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4478 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004479 if (!key.p.key || !key.p.key_len)
4480 return -EINVAL;
4481 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4482 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4483 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4484 key.p.key_len != WLAN_KEY_LEN_WEP104))
4485 return -EINVAL;
4486 if (key.idx > 4)
4487 return -EINVAL;
4488 } else {
4489 key.p.key_len = 0;
4490 key.p.key = NULL;
4491 }
4492
Johannes Bergafea0b72010-08-10 09:46:42 +02004493 if (key.idx >= 0) {
4494 int i;
4495 bool ok = false;
4496 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4497 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4498 ok = true;
4499 break;
4500 }
4501 }
Johannes Berg4c476992010-10-04 21:36:35 +02004502 if (!ok)
4503 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004504 }
4505
Johannes Berg4c476992010-10-04 21:36:35 +02004506 if (!rdev->ops->auth)
4507 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004508
Johannes Berg074ac8d2010-09-16 14:58:22 +02004509 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004510 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4511 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004512
Johannes Berg19957bb2009-07-02 17:20:43 +02004513 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004514 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004515 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004516 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4517 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004518
Johannes Berg19957bb2009-07-02 17:20:43 +02004519 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4520 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4521
4522 if (info->attrs[NL80211_ATTR_IE]) {
4523 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4524 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4525 }
4526
4527 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (!nl80211_valid_auth_type(auth_type))
4529 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004530
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004531 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4532
Johannes Berg95de8172012-01-20 13:55:25 +01004533 /*
4534 * Since we no longer track auth state, ignore
4535 * requests to only change local state.
4536 */
4537 if (local_state_change)
4538 return 0;
4539
Johannes Berg4c476992010-10-04 21:36:35 +02004540 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4541 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004542 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004543}
4544
Johannes Bergc0692b82010-08-27 14:26:53 +03004545static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4546 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004547 struct cfg80211_crypto_settings *settings,
4548 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004549{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004550 memset(settings, 0, sizeof(*settings));
4551
Samuel Ortizb23aa672009-07-01 21:26:54 +02004552 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4553
Johannes Bergc0692b82010-08-27 14:26:53 +03004554 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4555 u16 proto;
4556 proto = nla_get_u16(
4557 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4558 settings->control_port_ethertype = cpu_to_be16(proto);
4559 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4560 proto != ETH_P_PAE)
4561 return -EINVAL;
4562 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4563 settings->control_port_no_encrypt = true;
4564 } else
4565 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4566
Samuel Ortizb23aa672009-07-01 21:26:54 +02004567 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4568 void *data;
4569 int len, i;
4570
4571 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4572 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4573 settings->n_ciphers_pairwise = len / sizeof(u32);
4574
4575 if (len % sizeof(u32))
4576 return -EINVAL;
4577
Johannes Berg3dc27d22009-07-02 21:36:37 +02004578 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004579 return -EINVAL;
4580
4581 memcpy(settings->ciphers_pairwise, data, len);
4582
4583 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004584 if (!cfg80211_supported_cipher_suite(
4585 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004586 settings->ciphers_pairwise[i]))
4587 return -EINVAL;
4588 }
4589
4590 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4591 settings->cipher_group =
4592 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004593 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4594 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004595 return -EINVAL;
4596 }
4597
4598 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4599 settings->wpa_versions =
4600 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4601 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4602 return -EINVAL;
4603 }
4604
4605 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4606 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004607 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004608
4609 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4610 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4611 settings->n_akm_suites = len / sizeof(u32);
4612
4613 if (len % sizeof(u32))
4614 return -EINVAL;
4615
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004616 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4617 return -EINVAL;
4618
Samuel Ortizb23aa672009-07-01 21:26:54 +02004619 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004620 }
4621
4622 return 0;
4623}
4624
Jouni Malinen636a5d32009-03-19 13:39:22 +02004625static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4626{
Johannes Berg4c476992010-10-04 21:36:35 +02004627 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4628 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004629 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004630 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004631 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004632 int err, ssid_len, ie_len = 0;
4633 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004634 u32 flags = 0;
4635 struct ieee80211_ht_cap *ht_capa = NULL;
4636 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004637
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004638 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4639 return -EINVAL;
4640
4641 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004642 !info->attrs[NL80211_ATTR_SSID] ||
4643 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004644 return -EINVAL;
4645
Johannes Berg4c476992010-10-04 21:36:35 +02004646 if (!rdev->ops->assoc)
4647 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004648
Johannes Berg074ac8d2010-09-16 14:58:22 +02004649 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004650 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4651 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004652
Johannes Berg19957bb2009-07-02 17:20:43 +02004653 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004654
Johannes Berg19957bb2009-07-02 17:20:43 +02004655 chan = ieee80211_get_channel(&rdev->wiphy,
4656 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004657 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4658 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004659
Johannes Berg19957bb2009-07-02 17:20:43 +02004660 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4661 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004662
4663 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004664 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4665 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004666 }
4667
Jouni Malinendc6382c2009-05-06 22:09:37 +03004668 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004669 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004670 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004671 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004672 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004673 else if (mfp != NL80211_MFP_NO)
4674 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004675 }
4676
Johannes Berg3e5d7642009-07-07 14:37:26 +02004677 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4678 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4679
Ben Greear7e7c8922011-11-18 11:31:59 -08004680 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4681 flags |= ASSOC_REQ_DISABLE_HT;
4682
4683 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4684 ht_capa_mask =
4685 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4686
4687 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4688 if (!ht_capa_mask)
4689 return -EINVAL;
4690 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4691 }
4692
Johannes Bergc0692b82010-08-27 14:26:53 +03004693 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004694 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004695 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4696 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004697 &crypto, flags, ht_capa,
4698 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004699
Jouni Malinen636a5d32009-03-19 13:39:22 +02004700 return err;
4701}
4702
4703static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4704{
Johannes Berg4c476992010-10-04 21:36:35 +02004705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4706 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004707 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004708 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004709 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004710 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004711
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004712 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4713 return -EINVAL;
4714
4715 if (!info->attrs[NL80211_ATTR_MAC])
4716 return -EINVAL;
4717
4718 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4719 return -EINVAL;
4720
Johannes Berg4c476992010-10-04 21:36:35 +02004721 if (!rdev->ops->deauth)
4722 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004723
Johannes Berg074ac8d2010-09-16 14:58:22 +02004724 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004725 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4726 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004727
Johannes Berg19957bb2009-07-02 17:20:43 +02004728 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004729
Johannes Berg19957bb2009-07-02 17:20:43 +02004730 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4731 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004732 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004733 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004734 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004735
4736 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004737 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4738 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004739 }
4740
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004741 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4742
Johannes Berg4c476992010-10-04 21:36:35 +02004743 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4744 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004745}
4746
4747static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4748{
Johannes Berg4c476992010-10-04 21:36:35 +02004749 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4750 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004751 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004752 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004753 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004754 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004755
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004756 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4757 return -EINVAL;
4758
4759 if (!info->attrs[NL80211_ATTR_MAC])
4760 return -EINVAL;
4761
4762 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4763 return -EINVAL;
4764
Johannes Berg4c476992010-10-04 21:36:35 +02004765 if (!rdev->ops->disassoc)
4766 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004767
Johannes Berg074ac8d2010-09-16 14:58:22 +02004768 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4770 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004771
Johannes Berg19957bb2009-07-02 17:20:43 +02004772 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004773
Johannes Berg19957bb2009-07-02 17:20:43 +02004774 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4775 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004776 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004777 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004778 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004779
4780 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004781 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4782 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004783 }
4784
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004785 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4786
Johannes Berg4c476992010-10-04 21:36:35 +02004787 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4788 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004789}
4790
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004791static bool
4792nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4793 int mcast_rate[IEEE80211_NUM_BANDS],
4794 int rateval)
4795{
4796 struct wiphy *wiphy = &rdev->wiphy;
4797 bool found = false;
4798 int band, i;
4799
4800 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4801 struct ieee80211_supported_band *sband;
4802
4803 sband = wiphy->bands[band];
4804 if (!sband)
4805 continue;
4806
4807 for (i = 0; i < sband->n_bitrates; i++) {
4808 if (sband->bitrates[i].bitrate == rateval) {
4809 mcast_rate[band] = i + 1;
4810 found = true;
4811 break;
4812 }
4813 }
4814 }
4815
4816 return found;
4817}
4818
Johannes Berg04a773a2009-04-19 21:24:32 +02004819static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4820{
Johannes Berg4c476992010-10-04 21:36:35 +02004821 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4822 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004823 struct cfg80211_ibss_params ibss;
4824 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004825 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004826 int err;
4827
Johannes Berg8e30bc52009-04-22 17:45:38 +02004828 memset(&ibss, 0, sizeof(ibss));
4829
Johannes Berg04a773a2009-04-19 21:24:32 +02004830 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4831 return -EINVAL;
4832
4833 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4834 !info->attrs[NL80211_ATTR_SSID] ||
4835 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4836 return -EINVAL;
4837
Johannes Berg8e30bc52009-04-22 17:45:38 +02004838 ibss.beacon_interval = 100;
4839
4840 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4841 ibss.beacon_interval =
4842 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4843 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4844 return -EINVAL;
4845 }
4846
Johannes Berg4c476992010-10-04 21:36:35 +02004847 if (!rdev->ops->join_ibss)
4848 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004849
Johannes Berg4c476992010-10-04 21:36:35 +02004850 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4851 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004852
Johannes Berg79c97e92009-07-07 03:56:12 +02004853 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004854
Johannes Berg39193492011-09-16 13:45:25 +02004855 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004856 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004857
4858 if (!is_valid_ether_addr(ibss.bssid))
4859 return -EINVAL;
4860 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004861 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4862 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4863
4864 if (info->attrs[NL80211_ATTR_IE]) {
4865 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4866 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4867 }
4868
Alexander Simon54858ee2011-11-30 16:56:32 +01004869 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4870 enum nl80211_channel_type channel_type;
4871
4872 channel_type = nla_get_u32(
4873 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4874 if (channel_type != NL80211_CHAN_NO_HT &&
4875 channel_type != NL80211_CHAN_HT20 &&
4876 channel_type != NL80211_CHAN_HT40MINUS &&
4877 channel_type != NL80211_CHAN_HT40PLUS)
4878 return -EINVAL;
4879
4880 if (channel_type != NL80211_CHAN_NO_HT &&
4881 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4882 return -EINVAL;
4883
4884 ibss.channel_type = channel_type;
4885 } else {
4886 ibss.channel_type = NL80211_CHAN_NO_HT;
4887 }
4888
4889 ibss.channel = rdev_freq_to_chan(rdev,
4890 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4891 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004892 if (!ibss.channel ||
4893 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004894 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4895 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004896
Alexander Simon54858ee2011-11-30 16:56:32 +01004897 /* Both channels should be able to initiate communication */
4898 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4899 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4900 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4901 ibss.channel_type))
4902 return -EINVAL;
4903
Johannes Berg04a773a2009-04-19 21:24:32 +02004904 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004905 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004906
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004907 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4908 u8 *rates =
4909 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4910 int n_rates =
4911 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4912 struct ieee80211_supported_band *sband =
4913 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004914
Johannes Berg34850ab2011-07-18 18:08:35 +02004915 err = ieee80211_get_ratemask(sband, rates, n_rates,
4916 &ibss.basic_rates);
4917 if (err)
4918 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004919 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004920
4921 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4922 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4923 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4924 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004925
Johannes Berg4c476992010-10-04 21:36:35 +02004926 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4927 connkeys = nl80211_parse_connkeys(rdev,
4928 info->attrs[NL80211_ATTR_KEYS]);
4929 if (IS_ERR(connkeys))
4930 return PTR_ERR(connkeys);
4931 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004932
Antonio Quartulli267335d2012-01-31 20:25:47 +01004933 ibss.control_port =
4934 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4935
Johannes Berg4c476992010-10-04 21:36:35 +02004936 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004937 if (err)
4938 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004939 return err;
4940}
4941
4942static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4943{
Johannes Berg4c476992010-10-04 21:36:35 +02004944 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4945 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004946
Johannes Berg4c476992010-10-04 21:36:35 +02004947 if (!rdev->ops->leave_ibss)
4948 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004949
Johannes Berg4c476992010-10-04 21:36:35 +02004950 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4951 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004952
Johannes Berg4c476992010-10-04 21:36:35 +02004953 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004954}
4955
Johannes Bergaff89a92009-07-01 21:26:51 +02004956#ifdef CONFIG_NL80211_TESTMODE
4957static struct genl_multicast_group nl80211_testmode_mcgrp = {
4958 .name = "testmode",
4959};
4960
4961static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4962{
Johannes Berg4c476992010-10-04 21:36:35 +02004963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004964 int err;
4965
4966 if (!info->attrs[NL80211_ATTR_TESTDATA])
4967 return -EINVAL;
4968
Johannes Bergaff89a92009-07-01 21:26:51 +02004969 err = -EOPNOTSUPP;
4970 if (rdev->ops->testmode_cmd) {
4971 rdev->testmode_info = info;
4972 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4973 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4974 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4975 rdev->testmode_info = NULL;
4976 }
4977
Johannes Bergaff89a92009-07-01 21:26:51 +02004978 return err;
4979}
4980
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004981static int nl80211_testmode_dump(struct sk_buff *skb,
4982 struct netlink_callback *cb)
4983{
Johannes Berg00918d32011-12-13 17:22:05 +01004984 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004985 int err;
4986 long phy_idx;
4987 void *data = NULL;
4988 int data_len = 0;
4989
4990 if (cb->args[0]) {
4991 /*
4992 * 0 is a valid index, but not valid for args[0],
4993 * so we need to offset by 1.
4994 */
4995 phy_idx = cb->args[0] - 1;
4996 } else {
4997 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4998 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4999 nl80211_policy);
5000 if (err)
5001 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005002 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5003 phy_idx = nla_get_u32(
5004 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5005 } else {
5006 struct net_device *netdev;
5007
5008 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5009 nl80211_fam.attrbuf,
5010 &rdev, &netdev);
5011 if (err)
5012 return err;
5013 dev_put(netdev);
5014 phy_idx = rdev->wiphy_idx;
5015 cfg80211_unlock_rdev(rdev);
5016 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005017 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5018 cb->args[1] =
5019 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5020 }
5021
5022 if (cb->args[1]) {
5023 data = nla_data((void *)cb->args[1]);
5024 data_len = nla_len((void *)cb->args[1]);
5025 }
5026
5027 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005028 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5029 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005030 mutex_unlock(&cfg80211_mutex);
5031 return -ENOENT;
5032 }
Johannes Berg00918d32011-12-13 17:22:05 +01005033 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005034 mutex_unlock(&cfg80211_mutex);
5035
Johannes Berg00918d32011-12-13 17:22:05 +01005036 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005037 err = -EOPNOTSUPP;
5038 goto out_err;
5039 }
5040
5041 while (1) {
5042 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5043 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5044 NL80211_CMD_TESTMODE);
5045 struct nlattr *tmdata;
5046
David S. Miller9360ffd2012-03-29 04:41:26 -04005047 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005048 genlmsg_cancel(skb, hdr);
5049 break;
5050 }
5051
5052 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5053 if (!tmdata) {
5054 genlmsg_cancel(skb, hdr);
5055 break;
5056 }
Johannes Berg00918d32011-12-13 17:22:05 +01005057 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5058 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005059 nla_nest_end(skb, tmdata);
5060
5061 if (err == -ENOBUFS || err == -ENOENT) {
5062 genlmsg_cancel(skb, hdr);
5063 break;
5064 } else if (err) {
5065 genlmsg_cancel(skb, hdr);
5066 goto out_err;
5067 }
5068
5069 genlmsg_end(skb, hdr);
5070 }
5071
5072 err = skb->len;
5073 /* see above */
5074 cb->args[0] = phy_idx + 1;
5075 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005076 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005077 return err;
5078}
5079
Johannes Bergaff89a92009-07-01 21:26:51 +02005080static struct sk_buff *
5081__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5082 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5083{
5084 struct sk_buff *skb;
5085 void *hdr;
5086 struct nlattr *data;
5087
5088 skb = nlmsg_new(approxlen + 100, gfp);
5089 if (!skb)
5090 return NULL;
5091
5092 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5093 if (!hdr) {
5094 kfree_skb(skb);
5095 return NULL;
5096 }
5097
David S. Miller9360ffd2012-03-29 04:41:26 -04005098 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5099 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005100 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5101
5102 ((void **)skb->cb)[0] = rdev;
5103 ((void **)skb->cb)[1] = hdr;
5104 ((void **)skb->cb)[2] = data;
5105
5106 return skb;
5107
5108 nla_put_failure:
5109 kfree_skb(skb);
5110 return NULL;
5111}
5112
5113struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5114 int approxlen)
5115{
5116 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5117
5118 if (WARN_ON(!rdev->testmode_info))
5119 return NULL;
5120
5121 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5122 rdev->testmode_info->snd_pid,
5123 rdev->testmode_info->snd_seq,
5124 GFP_KERNEL);
5125}
5126EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5127
5128int cfg80211_testmode_reply(struct sk_buff *skb)
5129{
5130 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5131 void *hdr = ((void **)skb->cb)[1];
5132 struct nlattr *data = ((void **)skb->cb)[2];
5133
5134 if (WARN_ON(!rdev->testmode_info)) {
5135 kfree_skb(skb);
5136 return -EINVAL;
5137 }
5138
5139 nla_nest_end(skb, data);
5140 genlmsg_end(skb, hdr);
5141 return genlmsg_reply(skb, rdev->testmode_info);
5142}
5143EXPORT_SYMBOL(cfg80211_testmode_reply);
5144
5145struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5146 int approxlen, gfp_t gfp)
5147{
5148 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5149
5150 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5151}
5152EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5153
5154void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5155{
5156 void *hdr = ((void **)skb->cb)[1];
5157 struct nlattr *data = ((void **)skb->cb)[2];
5158
5159 nla_nest_end(skb, data);
5160 genlmsg_end(skb, hdr);
5161 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5162}
5163EXPORT_SYMBOL(cfg80211_testmode_event);
5164#endif
5165
Samuel Ortizb23aa672009-07-01 21:26:54 +02005166static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5167{
Johannes Berg4c476992010-10-04 21:36:35 +02005168 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5169 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005170 struct cfg80211_connect_params connect;
5171 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005172 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005173 int err;
5174
5175 memset(&connect, 0, sizeof(connect));
5176
5177 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5178 return -EINVAL;
5179
5180 if (!info->attrs[NL80211_ATTR_SSID] ||
5181 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5182 return -EINVAL;
5183
5184 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5185 connect.auth_type =
5186 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5187 if (!nl80211_valid_auth_type(connect.auth_type))
5188 return -EINVAL;
5189 } else
5190 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5191
5192 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5193
Johannes Bergc0692b82010-08-27 14:26:53 +03005194 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005195 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005196 if (err)
5197 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005198
Johannes Berg074ac8d2010-09-16 14:58:22 +02005199 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005200 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5201 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005202
Johannes Berg79c97e92009-07-07 03:56:12 +02005203 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005204
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305205 connect.bg_scan_period = -1;
5206 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5207 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5208 connect.bg_scan_period =
5209 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5210 }
5211
Samuel Ortizb23aa672009-07-01 21:26:54 +02005212 if (info->attrs[NL80211_ATTR_MAC])
5213 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5214 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5215 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5216
5217 if (info->attrs[NL80211_ATTR_IE]) {
5218 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5219 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5220 }
5221
5222 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5223 connect.channel =
5224 ieee80211_get_channel(wiphy,
5225 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5226 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005227 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5228 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005229 }
5230
Johannes Bergfffd0932009-07-08 14:22:54 +02005231 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5232 connkeys = nl80211_parse_connkeys(rdev,
5233 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005234 if (IS_ERR(connkeys))
5235 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005236 }
5237
Ben Greear7e7c8922011-11-18 11:31:59 -08005238 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5239 connect.flags |= ASSOC_REQ_DISABLE_HT;
5240
5241 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5242 memcpy(&connect.ht_capa_mask,
5243 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5244 sizeof(connect.ht_capa_mask));
5245
5246 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5247 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5248 return -EINVAL;
5249 memcpy(&connect.ht_capa,
5250 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5251 sizeof(connect.ht_capa));
5252 }
5253
Johannes Bergfffd0932009-07-08 14:22:54 +02005254 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005255 if (err)
5256 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005257 return err;
5258}
5259
5260static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5261{
Johannes Berg4c476992010-10-04 21:36:35 +02005262 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5263 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005264 u16 reason;
5265
5266 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5267 reason = WLAN_REASON_DEAUTH_LEAVING;
5268 else
5269 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5270
5271 if (reason == 0)
5272 return -EINVAL;
5273
Johannes Berg074ac8d2010-09-16 14:58:22 +02005274 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005275 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5276 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005277
Johannes Berg4c476992010-10-04 21:36:35 +02005278 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005279}
5280
Johannes Berg463d0182009-07-14 00:33:35 +02005281static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5282{
Johannes Berg4c476992010-10-04 21:36:35 +02005283 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005284 struct net *net;
5285 int err;
5286 u32 pid;
5287
5288 if (!info->attrs[NL80211_ATTR_PID])
5289 return -EINVAL;
5290
5291 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5292
Johannes Berg463d0182009-07-14 00:33:35 +02005293 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005294 if (IS_ERR(net))
5295 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005296
5297 err = 0;
5298
5299 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005300 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5301 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005302
Johannes Berg463d0182009-07-14 00:33:35 +02005303 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005304 return err;
5305}
5306
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005307static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5308{
Johannes Berg4c476992010-10-04 21:36:35 +02005309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005310 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5311 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005312 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005313 struct cfg80211_pmksa pmksa;
5314
5315 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5316
5317 if (!info->attrs[NL80211_ATTR_MAC])
5318 return -EINVAL;
5319
5320 if (!info->attrs[NL80211_ATTR_PMKID])
5321 return -EINVAL;
5322
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005323 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5324 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5325
Johannes Berg074ac8d2010-09-16 14:58:22 +02005326 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005327 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5328 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005329
5330 switch (info->genlhdr->cmd) {
5331 case NL80211_CMD_SET_PMKSA:
5332 rdev_ops = rdev->ops->set_pmksa;
5333 break;
5334 case NL80211_CMD_DEL_PMKSA:
5335 rdev_ops = rdev->ops->del_pmksa;
5336 break;
5337 default:
5338 WARN_ON(1);
5339 break;
5340 }
5341
Johannes Berg4c476992010-10-04 21:36:35 +02005342 if (!rdev_ops)
5343 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005344
Johannes Berg4c476992010-10-04 21:36:35 +02005345 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005346}
5347
5348static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5349{
Johannes Berg4c476992010-10-04 21:36:35 +02005350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5351 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005352
Johannes Berg074ac8d2010-09-16 14:58:22 +02005353 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005354 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5355 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005356
Johannes Berg4c476992010-10-04 21:36:35 +02005357 if (!rdev->ops->flush_pmksa)
5358 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005359
Johannes Berg4c476992010-10-04 21:36:35 +02005360 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005361}
5362
Arik Nemtsov109086c2011-09-28 14:12:50 +03005363static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5364{
5365 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5366 struct net_device *dev = info->user_ptr[1];
5367 u8 action_code, dialog_token;
5368 u16 status_code;
5369 u8 *peer;
5370
5371 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5372 !rdev->ops->tdls_mgmt)
5373 return -EOPNOTSUPP;
5374
5375 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5376 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5377 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5378 !info->attrs[NL80211_ATTR_IE] ||
5379 !info->attrs[NL80211_ATTR_MAC])
5380 return -EINVAL;
5381
5382 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5383 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5384 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5385 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5386
5387 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5388 dialog_token, status_code,
5389 nla_data(info->attrs[NL80211_ATTR_IE]),
5390 nla_len(info->attrs[NL80211_ATTR_IE]));
5391}
5392
5393static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5394{
5395 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5396 struct net_device *dev = info->user_ptr[1];
5397 enum nl80211_tdls_operation operation;
5398 u8 *peer;
5399
5400 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5401 !rdev->ops->tdls_oper)
5402 return -EOPNOTSUPP;
5403
5404 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5405 !info->attrs[NL80211_ATTR_MAC])
5406 return -EINVAL;
5407
5408 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5409 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5410
5411 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5412}
5413
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005414static int nl80211_remain_on_channel(struct sk_buff *skb,
5415 struct genl_info *info)
5416{
Johannes Berg4c476992010-10-04 21:36:35 +02005417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5418 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005419 struct ieee80211_channel *chan;
5420 struct sk_buff *msg;
5421 void *hdr;
5422 u64 cookie;
5423 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5424 u32 freq, duration;
5425 int err;
5426
5427 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5428 !info->attrs[NL80211_ATTR_DURATION])
5429 return -EINVAL;
5430
5431 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5432
5433 /*
5434 * We should be on that channel for at least one jiffie,
5435 * and more than 5 seconds seems excessive.
5436 */
Johannes Berga2939112010-12-14 17:54:28 +01005437 if (!duration || !msecs_to_jiffies(duration) ||
5438 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005439 return -EINVAL;
5440
Johannes Berg7c4ef712011-11-18 15:33:48 +01005441 if (!rdev->ops->remain_on_channel ||
5442 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005443 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005444
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005445 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5446 channel_type = nla_get_u32(
5447 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5448 if (channel_type != NL80211_CHAN_NO_HT &&
5449 channel_type != NL80211_CHAN_HT20 &&
5450 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005451 channel_type != NL80211_CHAN_HT40MINUS)
5452 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005453 }
5454
5455 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5456 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005457 if (chan == NULL)
5458 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005459
5460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005461 if (!msg)
5462 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005463
5464 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5465 NL80211_CMD_REMAIN_ON_CHANNEL);
5466
5467 if (IS_ERR(hdr)) {
5468 err = PTR_ERR(hdr);
5469 goto free_msg;
5470 }
5471
5472 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5473 channel_type, duration, &cookie);
5474
5475 if (err)
5476 goto free_msg;
5477
David S. Miller9360ffd2012-03-29 04:41:26 -04005478 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5479 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005480
5481 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005482
5483 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005484
5485 nla_put_failure:
5486 err = -ENOBUFS;
5487 free_msg:
5488 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005489 return err;
5490}
5491
5492static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5493 struct genl_info *info)
5494{
Johannes Berg4c476992010-10-04 21:36:35 +02005495 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5496 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005497 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005498
5499 if (!info->attrs[NL80211_ATTR_COOKIE])
5500 return -EINVAL;
5501
Johannes Berg4c476992010-10-04 21:36:35 +02005502 if (!rdev->ops->cancel_remain_on_channel)
5503 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005504
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005505 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5506
Johannes Berg4c476992010-10-04 21:36:35 +02005507 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005508}
5509
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005510static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5511 u8 *rates, u8 rates_len)
5512{
5513 u8 i;
5514 u32 mask = 0;
5515
5516 for (i = 0; i < rates_len; i++) {
5517 int rate = (rates[i] & 0x7f) * 5;
5518 int ridx;
5519 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5520 struct ieee80211_rate *srate =
5521 &sband->bitrates[ridx];
5522 if (rate == srate->bitrate) {
5523 mask |= 1 << ridx;
5524 break;
5525 }
5526 }
5527 if (ridx == sband->n_bitrates)
5528 return 0; /* rate not found */
5529 }
5530
5531 return mask;
5532}
5533
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005534static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5535 u8 *rates, u8 rates_len,
5536 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5537{
5538 u8 i;
5539
5540 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5541
5542 for (i = 0; i < rates_len; i++) {
5543 int ridx, rbit;
5544
5545 ridx = rates[i] / 8;
5546 rbit = BIT(rates[i] % 8);
5547
5548 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005549 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005550 return false;
5551
5552 /* check availability */
5553 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5554 mcs[ridx] |= rbit;
5555 else
5556 return false;
5557 }
5558
5559 return true;
5560}
5561
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005562static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005563 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5564 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005565 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5566 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005567};
5568
5569static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5570 struct genl_info *info)
5571{
5572 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005573 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005574 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005575 int rem, i;
5576 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005577 struct nlattr *tx_rates;
5578 struct ieee80211_supported_band *sband;
5579
5580 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5581 return -EINVAL;
5582
Johannes Berg4c476992010-10-04 21:36:35 +02005583 if (!rdev->ops->set_bitrate_mask)
5584 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005585
5586 memset(&mask, 0, sizeof(mask));
5587 /* Default to all rates enabled */
5588 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5589 sband = rdev->wiphy.bands[i];
5590 mask.control[i].legacy =
5591 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005592 if (sband)
5593 memcpy(mask.control[i].mcs,
5594 sband->ht_cap.mcs.rx_mask,
5595 sizeof(mask.control[i].mcs));
5596 else
5597 memset(mask.control[i].mcs, 0,
5598 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005599 }
5600
5601 /*
5602 * The nested attribute uses enum nl80211_band as the index. This maps
5603 * directly to the enum ieee80211_band values used in cfg80211.
5604 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005605 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005606 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5607 {
5608 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005609 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5610 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005611 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005612 if (sband == NULL)
5613 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005614 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5615 nla_len(tx_rates), nl80211_txattr_policy);
5616 if (tb[NL80211_TXRATE_LEGACY]) {
5617 mask.control[band].legacy = rateset_to_mask(
5618 sband,
5619 nla_data(tb[NL80211_TXRATE_LEGACY]),
5620 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005621 }
5622 if (tb[NL80211_TXRATE_MCS]) {
5623 if (!ht_rateset_to_mask(
5624 sband,
5625 nla_data(tb[NL80211_TXRATE_MCS]),
5626 nla_len(tb[NL80211_TXRATE_MCS]),
5627 mask.control[band].mcs))
5628 return -EINVAL;
5629 }
5630
5631 if (mask.control[band].legacy == 0) {
5632 /* don't allow empty legacy rates if HT
5633 * is not even supported. */
5634 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5635 return -EINVAL;
5636
5637 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5638 if (mask.control[band].mcs[i])
5639 break;
5640
5641 /* legacy and mcs rates may not be both empty */
5642 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005643 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005644 }
5645 }
5646
Johannes Berg4c476992010-10-04 21:36:35 +02005647 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005648}
5649
Johannes Berg2e161f72010-08-12 15:38:38 +02005650static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005651{
Johannes Berg4c476992010-10-04 21:36:35 +02005652 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5653 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005654 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005655
5656 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5657 return -EINVAL;
5658
Johannes Berg2e161f72010-08-12 15:38:38 +02005659 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5660 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005661
Johannes Berg9d38d852010-06-09 17:20:33 +02005662 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005663 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005664 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5665 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5666 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005668 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5669 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005670
5671 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005672 if (!rdev->ops->mgmt_tx)
5673 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005674
Johannes Berg4c476992010-10-04 21:36:35 +02005675 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005676 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005677 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5678 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005679}
5680
Johannes Berg2e161f72010-08-12 15:38:38 +02005681static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005682{
Johannes Berg4c476992010-10-04 21:36:35 +02005683 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5684 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005685 struct ieee80211_channel *chan;
5686 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005687 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005688 u32 freq;
5689 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005690 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005691 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005692 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005693 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005694 bool offchan, no_cck, dont_wait_for_ack;
5695
5696 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005697
5698 if (!info->attrs[NL80211_ATTR_FRAME] ||
5699 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5700 return -EINVAL;
5701
Johannes Berg4c476992010-10-04 21:36:35 +02005702 if (!rdev->ops->mgmt_tx)
5703 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005704
Johannes Berg9d38d852010-06-09 17:20:33 +02005705 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005706 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005707 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5708 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5709 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005710 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005711 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5712 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005713
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005714 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005715 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005716 return -EINVAL;
5717 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5718 }
5719
Jouni Malinen026331c2010-02-15 12:53:10 +02005720 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5721 channel_type = nla_get_u32(
5722 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5723 if (channel_type != NL80211_CHAN_NO_HT &&
5724 channel_type != NL80211_CHAN_HT20 &&
5725 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005726 channel_type != NL80211_CHAN_HT40MINUS)
5727 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005728 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005729 }
5730
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005731 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5732
Johannes Berg7c4ef712011-11-18 15:33:48 +01005733 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5734 return -EINVAL;
5735
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305736 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5737
Jouni Malinen026331c2010-02-15 12:53:10 +02005738 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5739 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005740 if (chan == NULL)
5741 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005742
Johannes Berge247bd902011-11-04 11:18:21 +01005743 if (!dont_wait_for_ack) {
5744 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5745 if (!msg)
5746 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005747
Johannes Berge247bd902011-11-04 11:18:21 +01005748 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5749 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005750
Johannes Berge247bd902011-11-04 11:18:21 +01005751 if (IS_ERR(hdr)) {
5752 err = PTR_ERR(hdr);
5753 goto free_msg;
5754 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005755 }
Johannes Berge247bd902011-11-04 11:18:21 +01005756
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005757 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5758 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005759 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5760 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005761 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005762 if (err)
5763 goto free_msg;
5764
Johannes Berge247bd902011-11-04 11:18:21 +01005765 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005766 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5767 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005768
Johannes Berge247bd902011-11-04 11:18:21 +01005769 genlmsg_end(msg, hdr);
5770 return genlmsg_reply(msg, info);
5771 }
5772
5773 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005774
5775 nla_put_failure:
5776 err = -ENOBUFS;
5777 free_msg:
5778 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005779 return err;
5780}
5781
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005782static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5783{
5784 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5785 struct net_device *dev = info->user_ptr[1];
5786 u64 cookie;
5787
5788 if (!info->attrs[NL80211_ATTR_COOKIE])
5789 return -EINVAL;
5790
5791 if (!rdev->ops->mgmt_tx_cancel_wait)
5792 return -EOPNOTSUPP;
5793
5794 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5795 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5796 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5797 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5798 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5799 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5800 return -EOPNOTSUPP;
5801
5802 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5803
5804 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5805}
5806
Kalle Valoffb9eb32010-02-17 17:58:10 +02005807static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5808{
Johannes Berg4c476992010-10-04 21:36:35 +02005809 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005810 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005811 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005812 u8 ps_state;
5813 bool state;
5814 int err;
5815
Johannes Berg4c476992010-10-04 21:36:35 +02005816 if (!info->attrs[NL80211_ATTR_PS_STATE])
5817 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005818
5819 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5820
Johannes Berg4c476992010-10-04 21:36:35 +02005821 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5822 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005823
5824 wdev = dev->ieee80211_ptr;
5825
Johannes Berg4c476992010-10-04 21:36:35 +02005826 if (!rdev->ops->set_power_mgmt)
5827 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005828
5829 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5830
5831 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005832 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005833
Johannes Berg4c476992010-10-04 21:36:35 +02005834 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5835 wdev->ps_timeout);
5836 if (!err)
5837 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005838 return err;
5839}
5840
5841static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5842{
Johannes Berg4c476992010-10-04 21:36:35 +02005843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005844 enum nl80211_ps_state ps_state;
5845 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005846 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005847 struct sk_buff *msg;
5848 void *hdr;
5849 int err;
5850
Kalle Valoffb9eb32010-02-17 17:58:10 +02005851 wdev = dev->ieee80211_ptr;
5852
Johannes Berg4c476992010-10-04 21:36:35 +02005853 if (!rdev->ops->set_power_mgmt)
5854 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005855
5856 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005857 if (!msg)
5858 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005859
5860 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5861 NL80211_CMD_GET_POWER_SAVE);
5862 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005863 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005864 goto free_msg;
5865 }
5866
5867 if (wdev->ps)
5868 ps_state = NL80211_PS_ENABLED;
5869 else
5870 ps_state = NL80211_PS_DISABLED;
5871
David S. Miller9360ffd2012-03-29 04:41:26 -04005872 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5873 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005874
5875 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005876 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005877
Johannes Berg4c476992010-10-04 21:36:35 +02005878 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005879 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005880 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005881 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005882 return err;
5883}
5884
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005885static struct nla_policy
5886nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5887 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5888 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5889 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5890};
5891
5892static int nl80211_set_cqm_rssi(struct genl_info *info,
5893 s32 threshold, u32 hysteresis)
5894{
Johannes Berg4c476992010-10-04 21:36:35 +02005895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005896 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005897 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005898
5899 if (threshold > 0)
5900 return -EINVAL;
5901
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005902 wdev = dev->ieee80211_ptr;
5903
Johannes Berg4c476992010-10-04 21:36:35 +02005904 if (!rdev->ops->set_cqm_rssi_config)
5905 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005906
Johannes Berg074ac8d2010-09-16 14:58:22 +02005907 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005908 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5909 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005910
Johannes Berg4c476992010-10-04 21:36:35 +02005911 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5912 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005913}
5914
5915static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5916{
5917 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5918 struct nlattr *cqm;
5919 int err;
5920
5921 cqm = info->attrs[NL80211_ATTR_CQM];
5922 if (!cqm) {
5923 err = -EINVAL;
5924 goto out;
5925 }
5926
5927 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5928 nl80211_attr_cqm_policy);
5929 if (err)
5930 goto out;
5931
5932 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5933 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5934 s32 threshold;
5935 u32 hysteresis;
5936 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5937 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5938 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5939 } else
5940 err = -EINVAL;
5941
5942out:
5943 return err;
5944}
5945
Johannes Berg29cbe682010-12-03 09:20:44 +01005946static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5947{
5948 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5949 struct net_device *dev = info->user_ptr[1];
5950 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005951 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005952 int err;
5953
5954 /* start with default */
5955 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005956 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005957
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005958 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005959 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005960 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005961 if (err)
5962 return err;
5963 }
5964
5965 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5966 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5967 return -EINVAL;
5968
Javier Cardonac80d5452010-12-16 17:37:49 -08005969 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5970 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5971
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08005972 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5973 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5974 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5975 return -EINVAL;
5976
Javier Cardonac80d5452010-12-16 17:37:49 -08005977 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5978 /* parse additional setup parameters if given */
5979 err = nl80211_parse_mesh_setup(info, &setup);
5980 if (err)
5981 return err;
5982 }
5983
5984 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005985}
5986
5987static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5988{
5989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5990 struct net_device *dev = info->user_ptr[1];
5991
5992 return cfg80211_leave_mesh(rdev, dev);
5993}
5994
Johannes Bergff1b6e62011-05-04 15:37:28 +02005995static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5996{
5997 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5998 struct sk_buff *msg;
5999 void *hdr;
6000
6001 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6002 return -EOPNOTSUPP;
6003
6004 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6005 if (!msg)
6006 return -ENOMEM;
6007
6008 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6009 NL80211_CMD_GET_WOWLAN);
6010 if (!hdr)
6011 goto nla_put_failure;
6012
6013 if (rdev->wowlan) {
6014 struct nlattr *nl_wowlan;
6015
6016 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6017 if (!nl_wowlan)
6018 goto nla_put_failure;
6019
David S. Miller9360ffd2012-03-29 04:41:26 -04006020 if ((rdev->wowlan->any &&
6021 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6022 (rdev->wowlan->disconnect &&
6023 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6024 (rdev->wowlan->magic_pkt &&
6025 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6026 (rdev->wowlan->gtk_rekey_failure &&
6027 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6028 (rdev->wowlan->eap_identity_req &&
6029 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6030 (rdev->wowlan->four_way_handshake &&
6031 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6032 (rdev->wowlan->rfkill_release &&
6033 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6034 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006035 if (rdev->wowlan->n_patterns) {
6036 struct nlattr *nl_pats, *nl_pat;
6037 int i, pat_len;
6038
6039 nl_pats = nla_nest_start(msg,
6040 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6041 if (!nl_pats)
6042 goto nla_put_failure;
6043
6044 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6045 nl_pat = nla_nest_start(msg, i + 1);
6046 if (!nl_pat)
6047 goto nla_put_failure;
6048 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006049 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6050 DIV_ROUND_UP(pat_len, 8),
6051 rdev->wowlan->patterns[i].mask) ||
6052 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6053 pat_len,
6054 rdev->wowlan->patterns[i].pattern))
6055 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006056 nla_nest_end(msg, nl_pat);
6057 }
6058 nla_nest_end(msg, nl_pats);
6059 }
6060
6061 nla_nest_end(msg, nl_wowlan);
6062 }
6063
6064 genlmsg_end(msg, hdr);
6065 return genlmsg_reply(msg, info);
6066
6067nla_put_failure:
6068 nlmsg_free(msg);
6069 return -ENOBUFS;
6070}
6071
6072static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6073{
6074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6075 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6076 struct cfg80211_wowlan no_triggers = {};
6077 struct cfg80211_wowlan new_triggers = {};
6078 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6079 int err, i;
6080
6081 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6082 return -EOPNOTSUPP;
6083
6084 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6085 goto no_triggers;
6086
6087 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6088 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6089 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6090 nl80211_wowlan_policy);
6091 if (err)
6092 return err;
6093
6094 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6095 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6096 return -EINVAL;
6097 new_triggers.any = true;
6098 }
6099
6100 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6101 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6102 return -EINVAL;
6103 new_triggers.disconnect = true;
6104 }
6105
6106 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6107 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6108 return -EINVAL;
6109 new_triggers.magic_pkt = true;
6110 }
6111
Johannes Berg77dbbb132011-07-13 10:48:55 +02006112 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6113 return -EINVAL;
6114
6115 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6116 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6117 return -EINVAL;
6118 new_triggers.gtk_rekey_failure = true;
6119 }
6120
6121 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6122 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6123 return -EINVAL;
6124 new_triggers.eap_identity_req = true;
6125 }
6126
6127 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6128 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6129 return -EINVAL;
6130 new_triggers.four_way_handshake = true;
6131 }
6132
6133 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6134 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6135 return -EINVAL;
6136 new_triggers.rfkill_release = true;
6137 }
6138
Johannes Bergff1b6e62011-05-04 15:37:28 +02006139 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6140 struct nlattr *pat;
6141 int n_patterns = 0;
6142 int rem, pat_len, mask_len;
6143 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6144
6145 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6146 rem)
6147 n_patterns++;
6148 if (n_patterns > wowlan->n_patterns)
6149 return -EINVAL;
6150
6151 new_triggers.patterns = kcalloc(n_patterns,
6152 sizeof(new_triggers.patterns[0]),
6153 GFP_KERNEL);
6154 if (!new_triggers.patterns)
6155 return -ENOMEM;
6156
6157 new_triggers.n_patterns = n_patterns;
6158 i = 0;
6159
6160 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6161 rem) {
6162 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6163 nla_data(pat), nla_len(pat), NULL);
6164 err = -EINVAL;
6165 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6166 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6167 goto error;
6168 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6169 mask_len = DIV_ROUND_UP(pat_len, 8);
6170 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6171 mask_len)
6172 goto error;
6173 if (pat_len > wowlan->pattern_max_len ||
6174 pat_len < wowlan->pattern_min_len)
6175 goto error;
6176
6177 new_triggers.patterns[i].mask =
6178 kmalloc(mask_len + pat_len, GFP_KERNEL);
6179 if (!new_triggers.patterns[i].mask) {
6180 err = -ENOMEM;
6181 goto error;
6182 }
6183 new_triggers.patterns[i].pattern =
6184 new_triggers.patterns[i].mask + mask_len;
6185 memcpy(new_triggers.patterns[i].mask,
6186 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6187 mask_len);
6188 new_triggers.patterns[i].pattern_len = pat_len;
6189 memcpy(new_triggers.patterns[i].pattern,
6190 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6191 pat_len);
6192 i++;
6193 }
6194 }
6195
6196 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6197 struct cfg80211_wowlan *ntrig;
6198 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6199 GFP_KERNEL);
6200 if (!ntrig) {
6201 err = -ENOMEM;
6202 goto error;
6203 }
6204 cfg80211_rdev_free_wowlan(rdev);
6205 rdev->wowlan = ntrig;
6206 } else {
6207 no_triggers:
6208 cfg80211_rdev_free_wowlan(rdev);
6209 rdev->wowlan = NULL;
6210 }
6211
6212 return 0;
6213 error:
6214 for (i = 0; i < new_triggers.n_patterns; i++)
6215 kfree(new_triggers.patterns[i].mask);
6216 kfree(new_triggers.patterns);
6217 return err;
6218}
6219
Johannes Berge5497d72011-07-05 16:35:40 +02006220static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6221{
6222 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6223 struct net_device *dev = info->user_ptr[1];
6224 struct wireless_dev *wdev = dev->ieee80211_ptr;
6225 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6226 struct cfg80211_gtk_rekey_data rekey_data;
6227 int err;
6228
6229 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6230 return -EINVAL;
6231
6232 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6233 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6234 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6235 nl80211_rekey_policy);
6236 if (err)
6237 return err;
6238
6239 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6240 return -ERANGE;
6241 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6242 return -ERANGE;
6243 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6244 return -ERANGE;
6245
6246 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6247 NL80211_KEK_LEN);
6248 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6249 NL80211_KCK_LEN);
6250 memcpy(rekey_data.replay_ctr,
6251 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6252 NL80211_REPLAY_CTR_LEN);
6253
6254 wdev_lock(wdev);
6255 if (!wdev->current_bss) {
6256 err = -ENOTCONN;
6257 goto out;
6258 }
6259
6260 if (!rdev->ops->set_rekey_data) {
6261 err = -EOPNOTSUPP;
6262 goto out;
6263 }
6264
6265 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6266 out:
6267 wdev_unlock(wdev);
6268 return err;
6269}
6270
Johannes Berg28946da2011-11-04 11:18:12 +01006271static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6272 struct genl_info *info)
6273{
6274 struct net_device *dev = info->user_ptr[1];
6275 struct wireless_dev *wdev = dev->ieee80211_ptr;
6276
6277 if (wdev->iftype != NL80211_IFTYPE_AP &&
6278 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6279 return -EINVAL;
6280
6281 if (wdev->ap_unexpected_nlpid)
6282 return -EBUSY;
6283
6284 wdev->ap_unexpected_nlpid = info->snd_pid;
6285 return 0;
6286}
6287
Johannes Berg7f6cf312011-11-04 11:18:15 +01006288static int nl80211_probe_client(struct sk_buff *skb,
6289 struct genl_info *info)
6290{
6291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6292 struct net_device *dev = info->user_ptr[1];
6293 struct wireless_dev *wdev = dev->ieee80211_ptr;
6294 struct sk_buff *msg;
6295 void *hdr;
6296 const u8 *addr;
6297 u64 cookie;
6298 int err;
6299
6300 if (wdev->iftype != NL80211_IFTYPE_AP &&
6301 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6302 return -EOPNOTSUPP;
6303
6304 if (!info->attrs[NL80211_ATTR_MAC])
6305 return -EINVAL;
6306
6307 if (!rdev->ops->probe_client)
6308 return -EOPNOTSUPP;
6309
6310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6311 if (!msg)
6312 return -ENOMEM;
6313
6314 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6315 NL80211_CMD_PROBE_CLIENT);
6316
6317 if (IS_ERR(hdr)) {
6318 err = PTR_ERR(hdr);
6319 goto free_msg;
6320 }
6321
6322 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6323
6324 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6325 if (err)
6326 goto free_msg;
6327
David S. Miller9360ffd2012-03-29 04:41:26 -04006328 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6329 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006330
6331 genlmsg_end(msg, hdr);
6332
6333 return genlmsg_reply(msg, info);
6334
6335 nla_put_failure:
6336 err = -ENOBUFS;
6337 free_msg:
6338 nlmsg_free(msg);
6339 return err;
6340}
6341
Johannes Berg5e760232011-11-04 11:18:17 +01006342static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6343{
6344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6345
6346 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6347 return -EOPNOTSUPP;
6348
6349 if (rdev->ap_beacons_nlpid)
6350 return -EBUSY;
6351
6352 rdev->ap_beacons_nlpid = info->snd_pid;
6353
6354 return 0;
6355}
6356
Johannes Berg4c476992010-10-04 21:36:35 +02006357#define NL80211_FLAG_NEED_WIPHY 0x01
6358#define NL80211_FLAG_NEED_NETDEV 0x02
6359#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006360#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6361#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6362 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006363
6364static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6365 struct genl_info *info)
6366{
6367 struct cfg80211_registered_device *rdev;
6368 struct net_device *dev;
6369 int err;
6370 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6371
6372 if (rtnl)
6373 rtnl_lock();
6374
6375 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6376 rdev = cfg80211_get_dev_from_info(info);
6377 if (IS_ERR(rdev)) {
6378 if (rtnl)
6379 rtnl_unlock();
6380 return PTR_ERR(rdev);
6381 }
6382 info->user_ptr[0] = rdev;
6383 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006384 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6385 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006386 if (err) {
6387 if (rtnl)
6388 rtnl_unlock();
6389 return err;
6390 }
Johannes Berg41265712010-10-04 21:14:05 +02006391 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6392 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006393 cfg80211_unlock_rdev(rdev);
6394 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006395 if (rtnl)
6396 rtnl_unlock();
6397 return -ENETDOWN;
6398 }
Johannes Berg4c476992010-10-04 21:36:35 +02006399 info->user_ptr[0] = rdev;
6400 info->user_ptr[1] = dev;
6401 }
6402
6403 return 0;
6404}
6405
6406static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6407 struct genl_info *info)
6408{
6409 if (info->user_ptr[0])
6410 cfg80211_unlock_rdev(info->user_ptr[0]);
6411 if (info->user_ptr[1])
6412 dev_put(info->user_ptr[1]);
6413 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6414 rtnl_unlock();
6415}
6416
Johannes Berg55682962007-09-20 13:09:35 -04006417static struct genl_ops nl80211_ops[] = {
6418 {
6419 .cmd = NL80211_CMD_GET_WIPHY,
6420 .doit = nl80211_get_wiphy,
6421 .dumpit = nl80211_dump_wiphy,
6422 .policy = nl80211_policy,
6423 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006424 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006425 },
6426 {
6427 .cmd = NL80211_CMD_SET_WIPHY,
6428 .doit = nl80211_set_wiphy,
6429 .policy = nl80211_policy,
6430 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006431 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006432 },
6433 {
6434 .cmd = NL80211_CMD_GET_INTERFACE,
6435 .doit = nl80211_get_interface,
6436 .dumpit = nl80211_dump_interface,
6437 .policy = nl80211_policy,
6438 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006439 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006440 },
6441 {
6442 .cmd = NL80211_CMD_SET_INTERFACE,
6443 .doit = nl80211_set_interface,
6444 .policy = nl80211_policy,
6445 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006446 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6447 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006448 },
6449 {
6450 .cmd = NL80211_CMD_NEW_INTERFACE,
6451 .doit = nl80211_new_interface,
6452 .policy = nl80211_policy,
6453 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006454 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6455 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006456 },
6457 {
6458 .cmd = NL80211_CMD_DEL_INTERFACE,
6459 .doit = nl80211_del_interface,
6460 .policy = nl80211_policy,
6461 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006462 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6463 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006464 },
Johannes Berg41ade002007-12-19 02:03:29 +01006465 {
6466 .cmd = NL80211_CMD_GET_KEY,
6467 .doit = nl80211_get_key,
6468 .policy = nl80211_policy,
6469 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006470 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6471 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006472 },
6473 {
6474 .cmd = NL80211_CMD_SET_KEY,
6475 .doit = nl80211_set_key,
6476 .policy = nl80211_policy,
6477 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006479 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006480 },
6481 {
6482 .cmd = NL80211_CMD_NEW_KEY,
6483 .doit = nl80211_new_key,
6484 .policy = nl80211_policy,
6485 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006487 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006488 },
6489 {
6490 .cmd = NL80211_CMD_DEL_KEY,
6491 .doit = nl80211_del_key,
6492 .policy = nl80211_policy,
6493 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006495 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006496 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006497 {
6498 .cmd = NL80211_CMD_SET_BEACON,
6499 .policy = nl80211_policy,
6500 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006501 .doit = nl80211_set_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006502 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6503 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006504 },
6505 {
Johannes Berg88600202012-02-13 15:17:18 +01006506 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006507 .policy = nl80211_policy,
6508 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006509 .doit = nl80211_start_ap,
Johannes Berg4c476992010-10-04 21:36:35 +02006510 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6511 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006512 },
6513 {
Johannes Berg88600202012-02-13 15:17:18 +01006514 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006515 .policy = nl80211_policy,
6516 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006517 .doit = nl80211_stop_ap,
Johannes Berg4c476992010-10-04 21:36:35 +02006518 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6519 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006520 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006521 {
6522 .cmd = NL80211_CMD_GET_STATION,
6523 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006524 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006525 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006526 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6527 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006528 },
6529 {
6530 .cmd = NL80211_CMD_SET_STATION,
6531 .doit = nl80211_set_station,
6532 .policy = nl80211_policy,
6533 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006534 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6535 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006536 },
6537 {
6538 .cmd = NL80211_CMD_NEW_STATION,
6539 .doit = nl80211_new_station,
6540 .policy = nl80211_policy,
6541 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006543 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006544 },
6545 {
6546 .cmd = NL80211_CMD_DEL_STATION,
6547 .doit = nl80211_del_station,
6548 .policy = nl80211_policy,
6549 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006550 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6551 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006552 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006553 {
6554 .cmd = NL80211_CMD_GET_MPATH,
6555 .doit = nl80211_get_mpath,
6556 .dumpit = nl80211_dump_mpath,
6557 .policy = nl80211_policy,
6558 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006559 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006560 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006561 },
6562 {
6563 .cmd = NL80211_CMD_SET_MPATH,
6564 .doit = nl80211_set_mpath,
6565 .policy = nl80211_policy,
6566 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006567 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006568 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006569 },
6570 {
6571 .cmd = NL80211_CMD_NEW_MPATH,
6572 .doit = nl80211_new_mpath,
6573 .policy = nl80211_policy,
6574 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006575 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006576 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006577 },
6578 {
6579 .cmd = NL80211_CMD_DEL_MPATH,
6580 .doit = nl80211_del_mpath,
6581 .policy = nl80211_policy,
6582 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006583 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6584 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006585 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006586 {
6587 .cmd = NL80211_CMD_SET_BSS,
6588 .doit = nl80211_set_bss,
6589 .policy = nl80211_policy,
6590 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006591 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6592 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006593 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006594 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006595 .cmd = NL80211_CMD_GET_REG,
6596 .doit = nl80211_get_reg,
6597 .policy = nl80211_policy,
6598 /* can be retrieved by unprivileged users */
6599 },
6600 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006601 .cmd = NL80211_CMD_SET_REG,
6602 .doit = nl80211_set_reg,
6603 .policy = nl80211_policy,
6604 .flags = GENL_ADMIN_PERM,
6605 },
6606 {
6607 .cmd = NL80211_CMD_REQ_SET_REG,
6608 .doit = nl80211_req_set_reg,
6609 .policy = nl80211_policy,
6610 .flags = GENL_ADMIN_PERM,
6611 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006612 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006613 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6614 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006615 .policy = nl80211_policy,
6616 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006617 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6618 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006619 },
6620 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006621 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6622 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006623 .policy = nl80211_policy,
6624 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006626 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006627 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006628 {
Johannes Berg2a519312009-02-10 21:25:55 +01006629 .cmd = NL80211_CMD_TRIGGER_SCAN,
6630 .doit = nl80211_trigger_scan,
6631 .policy = nl80211_policy,
6632 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006634 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006635 },
6636 {
6637 .cmd = NL80211_CMD_GET_SCAN,
6638 .policy = nl80211_policy,
6639 .dumpit = nl80211_dump_scan,
6640 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006641 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006642 .cmd = NL80211_CMD_START_SCHED_SCAN,
6643 .doit = nl80211_start_sched_scan,
6644 .policy = nl80211_policy,
6645 .flags = GENL_ADMIN_PERM,
6646 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6647 NL80211_FLAG_NEED_RTNL,
6648 },
6649 {
6650 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6651 .doit = nl80211_stop_sched_scan,
6652 .policy = nl80211_policy,
6653 .flags = GENL_ADMIN_PERM,
6654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6655 NL80211_FLAG_NEED_RTNL,
6656 },
6657 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006658 .cmd = NL80211_CMD_AUTHENTICATE,
6659 .doit = nl80211_authenticate,
6660 .policy = nl80211_policy,
6661 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006663 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006664 },
6665 {
6666 .cmd = NL80211_CMD_ASSOCIATE,
6667 .doit = nl80211_associate,
6668 .policy = nl80211_policy,
6669 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006671 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006672 },
6673 {
6674 .cmd = NL80211_CMD_DEAUTHENTICATE,
6675 .doit = nl80211_deauthenticate,
6676 .policy = nl80211_policy,
6677 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006678 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006679 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006680 },
6681 {
6682 .cmd = NL80211_CMD_DISASSOCIATE,
6683 .doit = nl80211_disassociate,
6684 .policy = nl80211_policy,
6685 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006687 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006688 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006689 {
6690 .cmd = NL80211_CMD_JOIN_IBSS,
6691 .doit = nl80211_join_ibss,
6692 .policy = nl80211_policy,
6693 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006695 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006696 },
6697 {
6698 .cmd = NL80211_CMD_LEAVE_IBSS,
6699 .doit = nl80211_leave_ibss,
6700 .policy = nl80211_policy,
6701 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006703 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006704 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006705#ifdef CONFIG_NL80211_TESTMODE
6706 {
6707 .cmd = NL80211_CMD_TESTMODE,
6708 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006709 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006710 .policy = nl80211_policy,
6711 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006712 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6713 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006714 },
6715#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006716 {
6717 .cmd = NL80211_CMD_CONNECT,
6718 .doit = nl80211_connect,
6719 .policy = nl80211_policy,
6720 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006721 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006722 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006723 },
6724 {
6725 .cmd = NL80211_CMD_DISCONNECT,
6726 .doit = nl80211_disconnect,
6727 .policy = nl80211_policy,
6728 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006729 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006730 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006731 },
Johannes Berg463d0182009-07-14 00:33:35 +02006732 {
6733 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6734 .doit = nl80211_wiphy_netns,
6735 .policy = nl80211_policy,
6736 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006737 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6738 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006739 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006740 {
6741 .cmd = NL80211_CMD_GET_SURVEY,
6742 .policy = nl80211_policy,
6743 .dumpit = nl80211_dump_survey,
6744 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006745 {
6746 .cmd = NL80211_CMD_SET_PMKSA,
6747 .doit = nl80211_setdel_pmksa,
6748 .policy = nl80211_policy,
6749 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006750 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6751 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006752 },
6753 {
6754 .cmd = NL80211_CMD_DEL_PMKSA,
6755 .doit = nl80211_setdel_pmksa,
6756 .policy = nl80211_policy,
6757 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006758 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6759 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006760 },
6761 {
6762 .cmd = NL80211_CMD_FLUSH_PMKSA,
6763 .doit = nl80211_flush_pmksa,
6764 .policy = nl80211_policy,
6765 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006766 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6767 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006768 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006769 {
6770 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6771 .doit = nl80211_remain_on_channel,
6772 .policy = nl80211_policy,
6773 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006775 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006776 },
6777 {
6778 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6779 .doit = nl80211_cancel_remain_on_channel,
6780 .policy = nl80211_policy,
6781 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006782 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006783 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006784 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006785 {
6786 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6787 .doit = nl80211_set_tx_bitrate_mask,
6788 .policy = nl80211_policy,
6789 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006790 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6791 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006792 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006793 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006794 .cmd = NL80211_CMD_REGISTER_FRAME,
6795 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006796 .policy = nl80211_policy,
6797 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006798 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6799 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006800 },
6801 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006802 .cmd = NL80211_CMD_FRAME,
6803 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006804 .policy = nl80211_policy,
6805 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006806 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006807 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006808 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006809 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006810 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6811 .doit = nl80211_tx_mgmt_cancel_wait,
6812 .policy = nl80211_policy,
6813 .flags = GENL_ADMIN_PERM,
6814 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6815 NL80211_FLAG_NEED_RTNL,
6816 },
6817 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006818 .cmd = NL80211_CMD_SET_POWER_SAVE,
6819 .doit = nl80211_set_power_save,
6820 .policy = nl80211_policy,
6821 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006822 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6823 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006824 },
6825 {
6826 .cmd = NL80211_CMD_GET_POWER_SAVE,
6827 .doit = nl80211_get_power_save,
6828 .policy = nl80211_policy,
6829 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006830 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6831 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006832 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006833 {
6834 .cmd = NL80211_CMD_SET_CQM,
6835 .doit = nl80211_set_cqm,
6836 .policy = nl80211_policy,
6837 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006838 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6839 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006840 },
Johannes Bergf444de02010-05-05 15:25:02 +02006841 {
6842 .cmd = NL80211_CMD_SET_CHANNEL,
6843 .doit = nl80211_set_channel,
6844 .policy = nl80211_policy,
6845 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006846 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6847 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006848 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006849 {
6850 .cmd = NL80211_CMD_SET_WDS_PEER,
6851 .doit = nl80211_set_wds_peer,
6852 .policy = nl80211_policy,
6853 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006854 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6855 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006856 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006857 {
6858 .cmd = NL80211_CMD_JOIN_MESH,
6859 .doit = nl80211_join_mesh,
6860 .policy = nl80211_policy,
6861 .flags = GENL_ADMIN_PERM,
6862 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6863 NL80211_FLAG_NEED_RTNL,
6864 },
6865 {
6866 .cmd = NL80211_CMD_LEAVE_MESH,
6867 .doit = nl80211_leave_mesh,
6868 .policy = nl80211_policy,
6869 .flags = GENL_ADMIN_PERM,
6870 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6871 NL80211_FLAG_NEED_RTNL,
6872 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006873 {
6874 .cmd = NL80211_CMD_GET_WOWLAN,
6875 .doit = nl80211_get_wowlan,
6876 .policy = nl80211_policy,
6877 /* can be retrieved by unprivileged users */
6878 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6879 NL80211_FLAG_NEED_RTNL,
6880 },
6881 {
6882 .cmd = NL80211_CMD_SET_WOWLAN,
6883 .doit = nl80211_set_wowlan,
6884 .policy = nl80211_policy,
6885 .flags = GENL_ADMIN_PERM,
6886 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6887 NL80211_FLAG_NEED_RTNL,
6888 },
Johannes Berge5497d72011-07-05 16:35:40 +02006889 {
6890 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6891 .doit = nl80211_set_rekey_data,
6892 .policy = nl80211_policy,
6893 .flags = GENL_ADMIN_PERM,
6894 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6895 NL80211_FLAG_NEED_RTNL,
6896 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006897 {
6898 .cmd = NL80211_CMD_TDLS_MGMT,
6899 .doit = nl80211_tdls_mgmt,
6900 .policy = nl80211_policy,
6901 .flags = GENL_ADMIN_PERM,
6902 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6903 NL80211_FLAG_NEED_RTNL,
6904 },
6905 {
6906 .cmd = NL80211_CMD_TDLS_OPER,
6907 .doit = nl80211_tdls_oper,
6908 .policy = nl80211_policy,
6909 .flags = GENL_ADMIN_PERM,
6910 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6911 NL80211_FLAG_NEED_RTNL,
6912 },
Johannes Berg28946da2011-11-04 11:18:12 +01006913 {
6914 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6915 .doit = nl80211_register_unexpected_frame,
6916 .policy = nl80211_policy,
6917 .flags = GENL_ADMIN_PERM,
6918 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6919 NL80211_FLAG_NEED_RTNL,
6920 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006921 {
6922 .cmd = NL80211_CMD_PROBE_CLIENT,
6923 .doit = nl80211_probe_client,
6924 .policy = nl80211_policy,
6925 .flags = GENL_ADMIN_PERM,
6926 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6927 NL80211_FLAG_NEED_RTNL,
6928 },
Johannes Berg5e760232011-11-04 11:18:17 +01006929 {
6930 .cmd = NL80211_CMD_REGISTER_BEACONS,
6931 .doit = nl80211_register_beacons,
6932 .policy = nl80211_policy,
6933 .flags = GENL_ADMIN_PERM,
6934 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6935 NL80211_FLAG_NEED_RTNL,
6936 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006937 {
6938 .cmd = NL80211_CMD_SET_NOACK_MAP,
6939 .doit = nl80211_set_noack_map,
6940 .policy = nl80211_policy,
6941 .flags = GENL_ADMIN_PERM,
6942 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6943 NL80211_FLAG_NEED_RTNL,
6944 },
6945
Johannes Berg55682962007-09-20 13:09:35 -04006946};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006947
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006948static struct genl_multicast_group nl80211_mlme_mcgrp = {
6949 .name = "mlme",
6950};
Johannes Berg55682962007-09-20 13:09:35 -04006951
6952/* multicast groups */
6953static struct genl_multicast_group nl80211_config_mcgrp = {
6954 .name = "config",
6955};
Johannes Berg2a519312009-02-10 21:25:55 +01006956static struct genl_multicast_group nl80211_scan_mcgrp = {
6957 .name = "scan",
6958};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006959static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6960 .name = "regulatory",
6961};
Johannes Berg55682962007-09-20 13:09:35 -04006962
6963/* notification functions */
6964
6965void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6966{
6967 struct sk_buff *msg;
6968
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006969 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006970 if (!msg)
6971 return;
6972
6973 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6974 nlmsg_free(msg);
6975 return;
6976 }
6977
Johannes Berg463d0182009-07-14 00:33:35 +02006978 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6979 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006980}
6981
Johannes Berg362a4152009-05-24 16:43:15 +02006982static int nl80211_add_scan_req(struct sk_buff *msg,
6983 struct cfg80211_registered_device *rdev)
6984{
6985 struct cfg80211_scan_request *req = rdev->scan_req;
6986 struct nlattr *nest;
6987 int i;
6988
Johannes Berg667503d2009-07-07 03:56:11 +02006989 ASSERT_RDEV_LOCK(rdev);
6990
Johannes Berg362a4152009-05-24 16:43:15 +02006991 if (WARN_ON(!req))
6992 return 0;
6993
6994 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6995 if (!nest)
6996 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006997 for (i = 0; i < req->n_ssids; i++) {
6998 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
6999 goto nla_put_failure;
7000 }
Johannes Berg362a4152009-05-24 16:43:15 +02007001 nla_nest_end(msg, nest);
7002
7003 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7004 if (!nest)
7005 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007006 for (i = 0; i < req->n_channels; i++) {
7007 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7008 goto nla_put_failure;
7009 }
Johannes Berg362a4152009-05-24 16:43:15 +02007010 nla_nest_end(msg, nest);
7011
David S. Miller9360ffd2012-03-29 04:41:26 -04007012 if (req->ie &&
7013 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7014 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007015
7016 return 0;
7017 nla_put_failure:
7018 return -ENOBUFS;
7019}
7020
Johannes Berga538e2d2009-06-16 19:56:42 +02007021static int nl80211_send_scan_msg(struct sk_buff *msg,
7022 struct cfg80211_registered_device *rdev,
7023 struct net_device *netdev,
7024 u32 pid, u32 seq, int flags,
7025 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007026{
7027 void *hdr;
7028
7029 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7030 if (!hdr)
7031 return -1;
7032
David S. Miller9360ffd2012-03-29 04:41:26 -04007033 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7034 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7035 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007036
Johannes Berg362a4152009-05-24 16:43:15 +02007037 /* ignore errors and send incomplete event anyway */
7038 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007039
7040 return genlmsg_end(msg, hdr);
7041
7042 nla_put_failure:
7043 genlmsg_cancel(msg, hdr);
7044 return -EMSGSIZE;
7045}
7046
Luciano Coelho807f8a82011-05-11 17:09:35 +03007047static int
7048nl80211_send_sched_scan_msg(struct sk_buff *msg,
7049 struct cfg80211_registered_device *rdev,
7050 struct net_device *netdev,
7051 u32 pid, u32 seq, int flags, u32 cmd)
7052{
7053 void *hdr;
7054
7055 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7056 if (!hdr)
7057 return -1;
7058
David S. Miller9360ffd2012-03-29 04:41:26 -04007059 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7060 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7061 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007062
7063 return genlmsg_end(msg, hdr);
7064
7065 nla_put_failure:
7066 genlmsg_cancel(msg, hdr);
7067 return -EMSGSIZE;
7068}
7069
Johannes Berga538e2d2009-06-16 19:56:42 +02007070void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7071 struct net_device *netdev)
7072{
7073 struct sk_buff *msg;
7074
7075 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7076 if (!msg)
7077 return;
7078
7079 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7080 NL80211_CMD_TRIGGER_SCAN) < 0) {
7081 nlmsg_free(msg);
7082 return;
7083 }
7084
Johannes Berg463d0182009-07-14 00:33:35 +02007085 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7086 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007087}
7088
Johannes Berg2a519312009-02-10 21:25:55 +01007089void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7090 struct net_device *netdev)
7091{
7092 struct sk_buff *msg;
7093
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007095 if (!msg)
7096 return;
7097
Johannes Berga538e2d2009-06-16 19:56:42 +02007098 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7099 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007100 nlmsg_free(msg);
7101 return;
7102 }
7103
Johannes Berg463d0182009-07-14 00:33:35 +02007104 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7105 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007106}
7107
7108void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7109 struct net_device *netdev)
7110{
7111 struct sk_buff *msg;
7112
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007114 if (!msg)
7115 return;
7116
Johannes Berga538e2d2009-06-16 19:56:42 +02007117 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7118 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007119 nlmsg_free(msg);
7120 return;
7121 }
7122
Johannes Berg463d0182009-07-14 00:33:35 +02007123 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7124 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007125}
7126
Luciano Coelho807f8a82011-05-11 17:09:35 +03007127void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7128 struct net_device *netdev)
7129{
7130 struct sk_buff *msg;
7131
7132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7133 if (!msg)
7134 return;
7135
7136 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7137 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7138 nlmsg_free(msg);
7139 return;
7140 }
7141
7142 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7143 nl80211_scan_mcgrp.id, GFP_KERNEL);
7144}
7145
7146void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7147 struct net_device *netdev, u32 cmd)
7148{
7149 struct sk_buff *msg;
7150
7151 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7152 if (!msg)
7153 return;
7154
7155 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7156 nlmsg_free(msg);
7157 return;
7158 }
7159
7160 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7161 nl80211_scan_mcgrp.id, GFP_KERNEL);
7162}
7163
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007164/*
7165 * This can happen on global regulatory changes or device specific settings
7166 * based on custom world regulatory domains.
7167 */
7168void nl80211_send_reg_change_event(struct regulatory_request *request)
7169{
7170 struct sk_buff *msg;
7171 void *hdr;
7172
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007173 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007174 if (!msg)
7175 return;
7176
7177 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7178 if (!hdr) {
7179 nlmsg_free(msg);
7180 return;
7181 }
7182
7183 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007184 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7185 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007186
David S. Miller9360ffd2012-03-29 04:41:26 -04007187 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7188 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7189 NL80211_REGDOM_TYPE_WORLD))
7190 goto nla_put_failure;
7191 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7192 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7193 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7194 goto nla_put_failure;
7195 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7196 request->intersect) {
7197 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7198 NL80211_REGDOM_TYPE_INTERSECTION))
7199 goto nla_put_failure;
7200 } else {
7201 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7202 NL80211_REGDOM_TYPE_COUNTRY) ||
7203 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7204 request->alpha2))
7205 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007206 }
7207
David S. Miller9360ffd2012-03-29 04:41:26 -04007208 if (wiphy_idx_valid(request->wiphy_idx) &&
7209 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7210 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007211
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007212 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007213
Johannes Bergbc43b282009-07-25 10:54:13 +02007214 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007215 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007216 GFP_ATOMIC);
7217 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007218
7219 return;
7220
7221nla_put_failure:
7222 genlmsg_cancel(msg, hdr);
7223 nlmsg_free(msg);
7224}
7225
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007226static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7227 struct net_device *netdev,
7228 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007229 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007230{
7231 struct sk_buff *msg;
7232 void *hdr;
7233
Johannes Berge6d6e342009-07-01 21:26:47 +02007234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007235 if (!msg)
7236 return;
7237
7238 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7239 if (!hdr) {
7240 nlmsg_free(msg);
7241 return;
7242 }
7243
David S. Miller9360ffd2012-03-29 04:41:26 -04007244 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7245 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7246 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7247 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007248
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007249 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007250
Johannes Berg463d0182009-07-14 00:33:35 +02007251 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7252 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007253 return;
7254
7255 nla_put_failure:
7256 genlmsg_cancel(msg, hdr);
7257 nlmsg_free(msg);
7258}
7259
7260void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007261 struct net_device *netdev, const u8 *buf,
7262 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007263{
7264 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007265 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007266}
7267
7268void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7269 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007270 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007271{
Johannes Berge6d6e342009-07-01 21:26:47 +02007272 nl80211_send_mlme_event(rdev, netdev, buf, len,
7273 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007274}
7275
Jouni Malinen53b46b82009-03-27 20:53:56 +02007276void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007277 struct net_device *netdev, const u8 *buf,
7278 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007279{
7280 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007281 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007282}
7283
Jouni Malinen53b46b82009-03-27 20:53:56 +02007284void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7285 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007286 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007287{
7288 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007289 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007290}
7291
Jouni Malinencf4e5942010-12-16 00:52:40 +02007292void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7293 struct net_device *netdev, const u8 *buf,
7294 size_t len, gfp_t gfp)
7295{
7296 nl80211_send_mlme_event(rdev, netdev, buf, len,
7297 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7298}
7299
7300void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7301 struct net_device *netdev, const u8 *buf,
7302 size_t len, gfp_t gfp)
7303{
7304 nl80211_send_mlme_event(rdev, netdev, buf, len,
7305 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7306}
7307
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007308static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7309 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007310 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007311{
7312 struct sk_buff *msg;
7313 void *hdr;
7314
Johannes Berge6d6e342009-07-01 21:26:47 +02007315 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007316 if (!msg)
7317 return;
7318
7319 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7320 if (!hdr) {
7321 nlmsg_free(msg);
7322 return;
7323 }
7324
David S. Miller9360ffd2012-03-29 04:41:26 -04007325 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7326 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7327 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7328 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7329 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007330
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007331 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007332
Johannes Berg463d0182009-07-14 00:33:35 +02007333 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7334 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007335 return;
7336
7337 nla_put_failure:
7338 genlmsg_cancel(msg, hdr);
7339 nlmsg_free(msg);
7340}
7341
7342void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007343 struct net_device *netdev, const u8 *addr,
7344 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007345{
7346 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007347 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007348}
7349
7350void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007351 struct net_device *netdev, const u8 *addr,
7352 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007353{
Johannes Berge6d6e342009-07-01 21:26:47 +02007354 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7355 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007356}
7357
Samuel Ortizb23aa672009-07-01 21:26:54 +02007358void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7359 struct net_device *netdev, const u8 *bssid,
7360 const u8 *req_ie, size_t req_ie_len,
7361 const u8 *resp_ie, size_t resp_ie_len,
7362 u16 status, gfp_t gfp)
7363{
7364 struct sk_buff *msg;
7365 void *hdr;
7366
7367 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7368 if (!msg)
7369 return;
7370
7371 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7372 if (!hdr) {
7373 nlmsg_free(msg);
7374 return;
7375 }
7376
David S. Miller9360ffd2012-03-29 04:41:26 -04007377 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7378 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7379 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7380 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7381 (req_ie &&
7382 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7383 (resp_ie &&
7384 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7385 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007386
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007387 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007388
Johannes Berg463d0182009-07-14 00:33:35 +02007389 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7390 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007391 return;
7392
7393 nla_put_failure:
7394 genlmsg_cancel(msg, hdr);
7395 nlmsg_free(msg);
7396
7397}
7398
7399void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7400 struct net_device *netdev, const u8 *bssid,
7401 const u8 *req_ie, size_t req_ie_len,
7402 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7403{
7404 struct sk_buff *msg;
7405 void *hdr;
7406
7407 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7408 if (!msg)
7409 return;
7410
7411 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7412 if (!hdr) {
7413 nlmsg_free(msg);
7414 return;
7415 }
7416
David S. Miller9360ffd2012-03-29 04:41:26 -04007417 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7418 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7419 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7420 (req_ie &&
7421 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7422 (resp_ie &&
7423 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7424 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007425
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007426 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007427
Johannes Berg463d0182009-07-14 00:33:35 +02007428 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7429 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007430 return;
7431
7432 nla_put_failure:
7433 genlmsg_cancel(msg, hdr);
7434 nlmsg_free(msg);
7435
7436}
7437
7438void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7439 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007440 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007441{
7442 struct sk_buff *msg;
7443 void *hdr;
7444
Johannes Berg667503d2009-07-07 03:56:11 +02007445 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007446 if (!msg)
7447 return;
7448
7449 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7450 if (!hdr) {
7451 nlmsg_free(msg);
7452 return;
7453 }
7454
David S. Miller9360ffd2012-03-29 04:41:26 -04007455 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7456 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7457 (from_ap && reason &&
7458 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7459 (from_ap &&
7460 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7461 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7462 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007463
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007464 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007465
Johannes Berg463d0182009-07-14 00:33:35 +02007466 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7467 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007468 return;
7469
7470 nla_put_failure:
7471 genlmsg_cancel(msg, hdr);
7472 nlmsg_free(msg);
7473
7474}
7475
Johannes Berg04a773a2009-04-19 21:24:32 +02007476void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7477 struct net_device *netdev, const u8 *bssid,
7478 gfp_t gfp)
7479{
7480 struct sk_buff *msg;
7481 void *hdr;
7482
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007484 if (!msg)
7485 return;
7486
7487 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7488 if (!hdr) {
7489 nlmsg_free(msg);
7490 return;
7491 }
7492
David S. Miller9360ffd2012-03-29 04:41:26 -04007493 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7494 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7495 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7496 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007497
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007498 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007499
Johannes Berg463d0182009-07-14 00:33:35 +02007500 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7501 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007502 return;
7503
7504 nla_put_failure:
7505 genlmsg_cancel(msg, hdr);
7506 nlmsg_free(msg);
7507}
7508
Javier Cardonac93b5e72011-04-07 15:08:34 -07007509void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7510 struct net_device *netdev,
7511 const u8 *macaddr, const u8* ie, u8 ie_len,
7512 gfp_t gfp)
7513{
7514 struct sk_buff *msg;
7515 void *hdr;
7516
7517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7518 if (!msg)
7519 return;
7520
7521 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7522 if (!hdr) {
7523 nlmsg_free(msg);
7524 return;
7525 }
7526
David S. Miller9360ffd2012-03-29 04:41:26 -04007527 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7528 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7529 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7530 (ie_len && ie &&
7531 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7532 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007533
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007534 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007535
7536 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7537 nl80211_mlme_mcgrp.id, gfp);
7538 return;
7539
7540 nla_put_failure:
7541 genlmsg_cancel(msg, hdr);
7542 nlmsg_free(msg);
7543}
7544
Jouni Malinena3b8b052009-03-27 21:59:49 +02007545void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7546 struct net_device *netdev, const u8 *addr,
7547 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007548 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007549{
7550 struct sk_buff *msg;
7551 void *hdr;
7552
Johannes Berge6d6e342009-07-01 21:26:47 +02007553 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007554 if (!msg)
7555 return;
7556
7557 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7558 if (!hdr) {
7559 nlmsg_free(msg);
7560 return;
7561 }
7562
David S. Miller9360ffd2012-03-29 04:41:26 -04007563 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7564 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7565 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7566 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7567 (key_id != -1 &&
7568 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7569 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7570 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007571
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007572 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007573
Johannes Berg463d0182009-07-14 00:33:35 +02007574 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7575 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007576 return;
7577
7578 nla_put_failure:
7579 genlmsg_cancel(msg, hdr);
7580 nlmsg_free(msg);
7581}
7582
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007583void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7584 struct ieee80211_channel *channel_before,
7585 struct ieee80211_channel *channel_after)
7586{
7587 struct sk_buff *msg;
7588 void *hdr;
7589 struct nlattr *nl_freq;
7590
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007591 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007592 if (!msg)
7593 return;
7594
7595 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7596 if (!hdr) {
7597 nlmsg_free(msg);
7598 return;
7599 }
7600
7601 /*
7602 * Since we are applying the beacon hint to a wiphy we know its
7603 * wiphy_idx is valid
7604 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007605 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7606 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007607
7608 /* Before */
7609 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7610 if (!nl_freq)
7611 goto nla_put_failure;
7612 if (nl80211_msg_put_channel(msg, channel_before))
7613 goto nla_put_failure;
7614 nla_nest_end(msg, nl_freq);
7615
7616 /* After */
7617 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7618 if (!nl_freq)
7619 goto nla_put_failure;
7620 if (nl80211_msg_put_channel(msg, channel_after))
7621 goto nla_put_failure;
7622 nla_nest_end(msg, nl_freq);
7623
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007624 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007625
Johannes Berg463d0182009-07-14 00:33:35 +02007626 rcu_read_lock();
7627 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7628 GFP_ATOMIC);
7629 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007630
7631 return;
7632
7633nla_put_failure:
7634 genlmsg_cancel(msg, hdr);
7635 nlmsg_free(msg);
7636}
7637
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007638static void nl80211_send_remain_on_chan_event(
7639 int cmd, struct cfg80211_registered_device *rdev,
7640 struct net_device *netdev, u64 cookie,
7641 struct ieee80211_channel *chan,
7642 enum nl80211_channel_type channel_type,
7643 unsigned int duration, gfp_t gfp)
7644{
7645 struct sk_buff *msg;
7646 void *hdr;
7647
7648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7649 if (!msg)
7650 return;
7651
7652 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7653 if (!hdr) {
7654 nlmsg_free(msg);
7655 return;
7656 }
7657
David S. Miller9360ffd2012-03-29 04:41:26 -04007658 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7659 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7660 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7661 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7662 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7663 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007664
David S. Miller9360ffd2012-03-29 04:41:26 -04007665 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7666 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7667 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007668
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007669 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007670
7671 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7672 nl80211_mlme_mcgrp.id, gfp);
7673 return;
7674
7675 nla_put_failure:
7676 genlmsg_cancel(msg, hdr);
7677 nlmsg_free(msg);
7678}
7679
7680void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7681 struct net_device *netdev, u64 cookie,
7682 struct ieee80211_channel *chan,
7683 enum nl80211_channel_type channel_type,
7684 unsigned int duration, gfp_t gfp)
7685{
7686 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7687 rdev, netdev, cookie, chan,
7688 channel_type, duration, gfp);
7689}
7690
7691void nl80211_send_remain_on_channel_cancel(
7692 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7693 u64 cookie, struct ieee80211_channel *chan,
7694 enum nl80211_channel_type channel_type, gfp_t gfp)
7695{
7696 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7697 rdev, netdev, cookie, chan,
7698 channel_type, 0, gfp);
7699}
7700
Johannes Berg98b62182009-12-23 13:15:44 +01007701void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7702 struct net_device *dev, const u8 *mac_addr,
7703 struct station_info *sinfo, gfp_t gfp)
7704{
7705 struct sk_buff *msg;
7706
7707 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7708 if (!msg)
7709 return;
7710
John W. Linville66266b32012-03-15 13:25:41 -04007711 if (nl80211_send_station(msg, 0, 0, 0,
7712 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007713 nlmsg_free(msg);
7714 return;
7715 }
7716
7717 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7718 nl80211_mlme_mcgrp.id, gfp);
7719}
7720
Jouni Malinenec15e682011-03-23 15:29:52 +02007721void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7722 struct net_device *dev, const u8 *mac_addr,
7723 gfp_t gfp)
7724{
7725 struct sk_buff *msg;
7726 void *hdr;
7727
7728 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7729 if (!msg)
7730 return;
7731
7732 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7733 if (!hdr) {
7734 nlmsg_free(msg);
7735 return;
7736 }
7737
David S. Miller9360ffd2012-03-29 04:41:26 -04007738 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7739 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7740 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007741
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007742 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007743
7744 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7745 nl80211_mlme_mcgrp.id, gfp);
7746 return;
7747
7748 nla_put_failure:
7749 genlmsg_cancel(msg, hdr);
7750 nlmsg_free(msg);
7751}
7752
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007753static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7754 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007755{
7756 struct wireless_dev *wdev = dev->ieee80211_ptr;
7757 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7758 struct sk_buff *msg;
7759 void *hdr;
7760 int err;
7761 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7762
7763 if (!nlpid)
7764 return false;
7765
7766 msg = nlmsg_new(100, gfp);
7767 if (!msg)
7768 return true;
7769
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007770 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007771 if (!hdr) {
7772 nlmsg_free(msg);
7773 return true;
7774 }
7775
David S. Miller9360ffd2012-03-29 04:41:26 -04007776 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7777 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7778 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7779 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007780
7781 err = genlmsg_end(msg, hdr);
7782 if (err < 0) {
7783 nlmsg_free(msg);
7784 return true;
7785 }
7786
7787 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7788 return true;
7789
7790 nla_put_failure:
7791 genlmsg_cancel(msg, hdr);
7792 nlmsg_free(msg);
7793 return true;
7794}
7795
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007796bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7797{
7798 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7799 addr, gfp);
7800}
7801
7802bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7803 const u8 *addr, gfp_t gfp)
7804{
7805 return __nl80211_unexpected_frame(dev,
7806 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7807 addr, gfp);
7808}
7809
Johannes Berg2e161f72010-08-12 15:38:38 +02007810int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7811 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007812 int freq, int sig_dbm,
7813 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007814{
7815 struct sk_buff *msg;
7816 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007817
7818 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7819 if (!msg)
7820 return -ENOMEM;
7821
Johannes Berg2e161f72010-08-12 15:38:38 +02007822 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007823 if (!hdr) {
7824 nlmsg_free(msg);
7825 return -ENOMEM;
7826 }
7827
David S. Miller9360ffd2012-03-29 04:41:26 -04007828 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7829 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7830 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7831 (sig_dbm &&
7832 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7833 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7834 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007835
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007836 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007837
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007838 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007839
7840 nla_put_failure:
7841 genlmsg_cancel(msg, hdr);
7842 nlmsg_free(msg);
7843 return -ENOBUFS;
7844}
7845
Johannes Berg2e161f72010-08-12 15:38:38 +02007846void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7847 struct net_device *netdev, u64 cookie,
7848 const u8 *buf, size_t len, bool ack,
7849 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007850{
7851 struct sk_buff *msg;
7852 void *hdr;
7853
7854 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7855 if (!msg)
7856 return;
7857
Johannes Berg2e161f72010-08-12 15:38:38 +02007858 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007859 if (!hdr) {
7860 nlmsg_free(msg);
7861 return;
7862 }
7863
David S. Miller9360ffd2012-03-29 04:41:26 -04007864 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7865 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7866 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7867 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7868 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7869 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007870
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007871 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007872
7873 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7874 return;
7875
7876 nla_put_failure:
7877 genlmsg_cancel(msg, hdr);
7878 nlmsg_free(msg);
7879}
7880
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007881void
7882nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7883 struct net_device *netdev,
7884 enum nl80211_cqm_rssi_threshold_event rssi_event,
7885 gfp_t gfp)
7886{
7887 struct sk_buff *msg;
7888 struct nlattr *pinfoattr;
7889 void *hdr;
7890
7891 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7892 if (!msg)
7893 return;
7894
7895 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7896 if (!hdr) {
7897 nlmsg_free(msg);
7898 return;
7899 }
7900
David S. Miller9360ffd2012-03-29 04:41:26 -04007901 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7902 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7903 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007904
7905 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7906 if (!pinfoattr)
7907 goto nla_put_failure;
7908
David S. Miller9360ffd2012-03-29 04:41:26 -04007909 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7910 rssi_event))
7911 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007912
7913 nla_nest_end(msg, pinfoattr);
7914
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007915 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007916
7917 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7918 nl80211_mlme_mcgrp.id, gfp);
7919 return;
7920
7921 nla_put_failure:
7922 genlmsg_cancel(msg, hdr);
7923 nlmsg_free(msg);
7924}
7925
Johannes Berge5497d72011-07-05 16:35:40 +02007926void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7927 struct net_device *netdev, const u8 *bssid,
7928 const u8 *replay_ctr, gfp_t gfp)
7929{
7930 struct sk_buff *msg;
7931 struct nlattr *rekey_attr;
7932 void *hdr;
7933
7934 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7935 if (!msg)
7936 return;
7937
7938 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7939 if (!hdr) {
7940 nlmsg_free(msg);
7941 return;
7942 }
7943
David S. Miller9360ffd2012-03-29 04:41:26 -04007944 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7945 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7946 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7947 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007948
7949 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7950 if (!rekey_attr)
7951 goto nla_put_failure;
7952
David S. Miller9360ffd2012-03-29 04:41:26 -04007953 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7954 NL80211_REPLAY_CTR_LEN, replay_ctr))
7955 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02007956
7957 nla_nest_end(msg, rekey_attr);
7958
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007959 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007960
7961 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7962 nl80211_mlme_mcgrp.id, gfp);
7963 return;
7964
7965 nla_put_failure:
7966 genlmsg_cancel(msg, hdr);
7967 nlmsg_free(msg);
7968}
7969
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007970void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7971 struct net_device *netdev, int index,
7972 const u8 *bssid, bool preauth, gfp_t gfp)
7973{
7974 struct sk_buff *msg;
7975 struct nlattr *attr;
7976 void *hdr;
7977
7978 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7979 if (!msg)
7980 return;
7981
7982 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7983 if (!hdr) {
7984 nlmsg_free(msg);
7985 return;
7986 }
7987
David S. Miller9360ffd2012-03-29 04:41:26 -04007988 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7989 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7990 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007991
7992 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7993 if (!attr)
7994 goto nla_put_failure;
7995
David S. Miller9360ffd2012-03-29 04:41:26 -04007996 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
7997 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
7998 (preauth &&
7999 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8000 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008001
8002 nla_nest_end(msg, attr);
8003
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008004 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008005
8006 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8007 nl80211_mlme_mcgrp.id, gfp);
8008 return;
8009
8010 nla_put_failure:
8011 genlmsg_cancel(msg, hdr);
8012 nlmsg_free(msg);
8013}
8014
Johannes Bergc063dbf2010-11-24 08:10:05 +01008015void
8016nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8017 struct net_device *netdev, const u8 *peer,
8018 u32 num_packets, gfp_t gfp)
8019{
8020 struct sk_buff *msg;
8021 struct nlattr *pinfoattr;
8022 void *hdr;
8023
8024 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8025 if (!msg)
8026 return;
8027
8028 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8029 if (!hdr) {
8030 nlmsg_free(msg);
8031 return;
8032 }
8033
David S. Miller9360ffd2012-03-29 04:41:26 -04008034 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8035 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8036 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8037 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008038
8039 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8040 if (!pinfoattr)
8041 goto nla_put_failure;
8042
David S. Miller9360ffd2012-03-29 04:41:26 -04008043 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8044 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008045
8046 nla_nest_end(msg, pinfoattr);
8047
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008048 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008049
8050 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8051 nl80211_mlme_mcgrp.id, gfp);
8052 return;
8053
8054 nla_put_failure:
8055 genlmsg_cancel(msg, hdr);
8056 nlmsg_free(msg);
8057}
8058
Johannes Berg7f6cf312011-11-04 11:18:15 +01008059void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8060 u64 cookie, bool acked, gfp_t gfp)
8061{
8062 struct wireless_dev *wdev = dev->ieee80211_ptr;
8063 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8064 struct sk_buff *msg;
8065 void *hdr;
8066 int err;
8067
8068 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8069 if (!msg)
8070 return;
8071
8072 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8073 if (!hdr) {
8074 nlmsg_free(msg);
8075 return;
8076 }
8077
David S. Miller9360ffd2012-03-29 04:41:26 -04008078 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8079 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8080 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8081 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8082 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8083 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008084
8085 err = genlmsg_end(msg, hdr);
8086 if (err < 0) {
8087 nlmsg_free(msg);
8088 return;
8089 }
8090
8091 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8092 nl80211_mlme_mcgrp.id, gfp);
8093 return;
8094
8095 nla_put_failure:
8096 genlmsg_cancel(msg, hdr);
8097 nlmsg_free(msg);
8098}
8099EXPORT_SYMBOL(cfg80211_probe_status);
8100
Johannes Berg5e760232011-11-04 11:18:17 +01008101void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8102 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008103 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008104{
8105 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8106 struct sk_buff *msg;
8107 void *hdr;
8108 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8109
8110 if (!nlpid)
8111 return;
8112
8113 msg = nlmsg_new(len + 100, gfp);
8114 if (!msg)
8115 return;
8116
8117 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8118 if (!hdr) {
8119 nlmsg_free(msg);
8120 return;
8121 }
8122
David S. Miller9360ffd2012-03-29 04:41:26 -04008123 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8124 (freq &&
8125 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8126 (sig_dbm &&
8127 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8128 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8129 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008130
8131 genlmsg_end(msg, hdr);
8132
8133 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8134 return;
8135
8136 nla_put_failure:
8137 genlmsg_cancel(msg, hdr);
8138 nlmsg_free(msg);
8139}
8140EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8141
Jouni Malinen026331c2010-02-15 12:53:10 +02008142static int nl80211_netlink_notify(struct notifier_block * nb,
8143 unsigned long state,
8144 void *_notify)
8145{
8146 struct netlink_notify *notify = _notify;
8147 struct cfg80211_registered_device *rdev;
8148 struct wireless_dev *wdev;
8149
8150 if (state != NETLINK_URELEASE)
8151 return NOTIFY_DONE;
8152
8153 rcu_read_lock();
8154
Johannes Berg5e760232011-11-04 11:18:17 +01008155 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008156 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008157 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008158 if (rdev->ap_beacons_nlpid == notify->pid)
8159 rdev->ap_beacons_nlpid = 0;
8160 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008161
8162 rcu_read_unlock();
8163
8164 return NOTIFY_DONE;
8165}
8166
8167static struct notifier_block nl80211_netlink_notifier = {
8168 .notifier_call = nl80211_netlink_notify,
8169};
8170
Johannes Berg55682962007-09-20 13:09:35 -04008171/* initialisation/exit functions */
8172
8173int nl80211_init(void)
8174{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008175 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008176
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008177 err = genl_register_family_with_ops(&nl80211_fam,
8178 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008179 if (err)
8180 return err;
8181
Johannes Berg55682962007-09-20 13:09:35 -04008182 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8183 if (err)
8184 goto err_out;
8185
Johannes Berg2a519312009-02-10 21:25:55 +01008186 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8187 if (err)
8188 goto err_out;
8189
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008190 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8191 if (err)
8192 goto err_out;
8193
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008194 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8195 if (err)
8196 goto err_out;
8197
Johannes Bergaff89a92009-07-01 21:26:51 +02008198#ifdef CONFIG_NL80211_TESTMODE
8199 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8200 if (err)
8201 goto err_out;
8202#endif
8203
Jouni Malinen026331c2010-02-15 12:53:10 +02008204 err = netlink_register_notifier(&nl80211_netlink_notifier);
8205 if (err)
8206 goto err_out;
8207
Johannes Berg55682962007-09-20 13:09:35 -04008208 return 0;
8209 err_out:
8210 genl_unregister_family(&nl80211_fam);
8211 return err;
8212}
8213
8214void nl80211_exit(void)
8215{
Jouni Malinen026331c2010-02-15 12:53:10 +02008216 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008217 genl_unregister_family(&nl80211_fam);
8218}