blob: e1fd1bf90729cd8ca6e0c8b0f884748b65e62733 [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 },
Johannes Berg55682962007-09-20 13:09:35 -0400207};
208
Johannes Berge31b8212010-10-05 19:39:30 +0200209/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000210static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200211 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200212 [NL80211_KEY_IDX] = { .type = NLA_U8 },
213 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200214 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200215 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
216 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200217 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100218 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
219};
220
221/* policy for the key default flags */
222static const struct nla_policy
223nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
224 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
225 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200226};
227
Johannes Bergff1b6e62011-05-04 15:37:28 +0200228/* policy for WoWLAN attributes */
229static const struct nla_policy
230nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
231 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
232 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
233 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200235 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
237 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200239};
240
Johannes Berge5497d72011-07-05 16:35:40 +0200241/* policy for GTK rekey offload attributes */
242static const struct nla_policy
243nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
244 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
245 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
246 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
247};
248
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300249static const struct nla_policy
250nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
251 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_SSID_LEN },
253};
254
Holger Schuriga0438972009-11-11 11:30:02 +0100255/* ifidx get helper */
256static int nl80211_get_ifidx(struct netlink_callback *cb)
257{
258 int res;
259
260 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
261 nl80211_fam.attrbuf, nl80211_fam.maxattr,
262 nl80211_policy);
263 if (res)
264 return res;
265
266 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
267 return -EINVAL;
268
269 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
270 if (!res)
271 return -EINVAL;
272 return res;
273}
274
Johannes Berg67748892010-10-04 21:14:06 +0200275static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
276 struct netlink_callback *cb,
277 struct cfg80211_registered_device **rdev,
278 struct net_device **dev)
279{
280 int ifidx = cb->args[0];
281 int err;
282
283 if (!ifidx)
284 ifidx = nl80211_get_ifidx(cb);
285 if (ifidx < 0)
286 return ifidx;
287
288 cb->args[0] = ifidx;
289
290 rtnl_lock();
291
292 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
293 if (!*dev) {
294 err = -ENODEV;
295 goto out_rtnl;
296 }
297
298 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100299 if (IS_ERR(*rdev)) {
300 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200301 goto out_rtnl;
302 }
303
304 return 0;
305 out_rtnl:
306 rtnl_unlock();
307 return err;
308}
309
310static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
311{
312 cfg80211_unlock_rdev(rdev);
313 rtnl_unlock();
314}
315
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100316/* IE validation */
317static bool is_valid_ie_attr(const struct nlattr *attr)
318{
319 const u8 *pos;
320 int len;
321
322 if (!attr)
323 return true;
324
325 pos = nla_data(attr);
326 len = nla_len(attr);
327
328 while (len) {
329 u8 elemlen;
330
331 if (len < 2)
332 return false;
333 len -= 2;
334
335 elemlen = pos[1];
336 if (elemlen > len)
337 return false;
338
339 len -= elemlen;
340 pos += 2 + elemlen;
341 }
342
343 return true;
344}
345
Johannes Berg55682962007-09-20 13:09:35 -0400346/* message building helper */
347static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
348 int flags, u8 cmd)
349{
350 /* since there is no private header just add the generic one */
351 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
352}
353
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400354static int nl80211_msg_put_channel(struct sk_buff *msg,
355 struct ieee80211_channel *chan)
356{
357 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
358 chan->center_freq);
359
360 if (chan->flags & IEEE80211_CHAN_DISABLED)
361 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
362 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
363 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
364 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
365 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
366 if (chan->flags & IEEE80211_CHAN_RADAR)
367 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
368
369 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
370 DBM_TO_MBM(chan->max_power));
371
372 return 0;
373
374 nla_put_failure:
375 return -ENOBUFS;
376}
377
Johannes Berg55682962007-09-20 13:09:35 -0400378/* netlink command implementations */
379
Johannes Bergb9454e82009-07-08 13:29:08 +0200380struct key_parse {
381 struct key_params p;
382 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200383 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200384 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100385 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200386};
387
388static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
389{
390 struct nlattr *tb[NL80211_KEY_MAX + 1];
391 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
392 nl80211_key_policy);
393 if (err)
394 return err;
395
396 k->def = !!tb[NL80211_KEY_DEFAULT];
397 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
398
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100399 if (k->def) {
400 k->def_uni = true;
401 k->def_multi = true;
402 }
403 if (k->defmgmt)
404 k->def_multi = true;
405
Johannes Bergb9454e82009-07-08 13:29:08 +0200406 if (tb[NL80211_KEY_IDX])
407 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
408
409 if (tb[NL80211_KEY_DATA]) {
410 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
411 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
412 }
413
414 if (tb[NL80211_KEY_SEQ]) {
415 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
416 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
417 }
418
419 if (tb[NL80211_KEY_CIPHER])
420 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
421
Johannes Berge31b8212010-10-05 19:39:30 +0200422 if (tb[NL80211_KEY_TYPE]) {
423 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
424 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
425 return -EINVAL;
426 }
427
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100428 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
429 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100430 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
431 tb[NL80211_KEY_DEFAULT_TYPES],
432 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100433 if (err)
434 return err;
435
436 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
437 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
438 }
439
Johannes Bergb9454e82009-07-08 13:29:08 +0200440 return 0;
441}
442
443static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
444{
445 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
446 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
447 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
448 }
449
450 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
451 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
452 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
453 }
454
455 if (info->attrs[NL80211_ATTR_KEY_IDX])
456 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
457
458 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
459 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
460
461 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
462 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
463
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100464 if (k->def) {
465 k->def_uni = true;
466 k->def_multi = true;
467 }
468 if (k->defmgmt)
469 k->def_multi = true;
470
Johannes Berge31b8212010-10-05 19:39:30 +0200471 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
472 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
473 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
474 return -EINVAL;
475 }
476
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100477 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
478 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
479 int err = nla_parse_nested(
480 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
481 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
482 nl80211_key_default_policy);
483 if (err)
484 return err;
485
486 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
487 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
488 }
489
Johannes Bergb9454e82009-07-08 13:29:08 +0200490 return 0;
491}
492
493static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
494{
495 int err;
496
497 memset(k, 0, sizeof(*k));
498 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200499 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200500
501 if (info->attrs[NL80211_ATTR_KEY])
502 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
503 else
504 err = nl80211_parse_key_old(info, k);
505
506 if (err)
507 return err;
508
509 if (k->def && k->defmgmt)
510 return -EINVAL;
511
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100512 if (k->defmgmt) {
513 if (k->def_uni || !k->def_multi)
514 return -EINVAL;
515 }
516
Johannes Bergb9454e82009-07-08 13:29:08 +0200517 if (k->idx != -1) {
518 if (k->defmgmt) {
519 if (k->idx < 4 || k->idx > 5)
520 return -EINVAL;
521 } else if (k->def) {
522 if (k->idx < 0 || k->idx > 3)
523 return -EINVAL;
524 } else {
525 if (k->idx < 0 || k->idx > 5)
526 return -EINVAL;
527 }
528 }
529
530 return 0;
531}
532
Johannes Bergfffd0932009-07-08 14:22:54 +0200533static struct cfg80211_cached_keys *
534nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
535 struct nlattr *keys)
536{
537 struct key_parse parse;
538 struct nlattr *key;
539 struct cfg80211_cached_keys *result;
540 int rem, err, def = 0;
541
542 result = kzalloc(sizeof(*result), GFP_KERNEL);
543 if (!result)
544 return ERR_PTR(-ENOMEM);
545
546 result->def = -1;
547 result->defmgmt = -1;
548
549 nla_for_each_nested(key, keys, rem) {
550 memset(&parse, 0, sizeof(parse));
551 parse.idx = -1;
552
553 err = nl80211_parse_key_new(key, &parse);
554 if (err)
555 goto error;
556 err = -EINVAL;
557 if (!parse.p.key)
558 goto error;
559 if (parse.idx < 0 || parse.idx > 4)
560 goto error;
561 if (parse.def) {
562 if (def)
563 goto error;
564 def = 1;
565 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100566 if (!parse.def_uni || !parse.def_multi)
567 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200568 } else if (parse.defmgmt)
569 goto error;
570 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200571 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200572 if (err)
573 goto error;
574 result->params[parse.idx].cipher = parse.p.cipher;
575 result->params[parse.idx].key_len = parse.p.key_len;
576 result->params[parse.idx].key = result->data[parse.idx];
577 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
578 }
579
580 return result;
581 error:
582 kfree(result);
583 return ERR_PTR(err);
584}
585
586static int nl80211_key_allowed(struct wireless_dev *wdev)
587{
588 ASSERT_WDEV_LOCK(wdev);
589
Johannes Bergfffd0932009-07-08 14:22:54 +0200590 switch (wdev->iftype) {
591 case NL80211_IFTYPE_AP:
592 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200593 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700594 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200595 break;
596 case NL80211_IFTYPE_ADHOC:
597 if (!wdev->current_bss)
598 return -ENOLINK;
599 break;
600 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200602 if (wdev->sme_state != CFG80211_SME_CONNECTED)
603 return -ENOLINK;
604 break;
605 default:
606 return -EINVAL;
607 }
608
609 return 0;
610}
611
Johannes Berg7527a782011-05-13 10:58:57 +0200612static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
613{
614 struct nlattr *nl_modes = nla_nest_start(msg, attr);
615 int i;
616
617 if (!nl_modes)
618 goto nla_put_failure;
619
620 i = 0;
621 while (ifmodes) {
622 if (ifmodes & 1)
623 NLA_PUT_FLAG(msg, i);
624 ifmodes >>= 1;
625 i++;
626 }
627
628 nla_nest_end(msg, nl_modes);
629 return 0;
630
631nla_put_failure:
632 return -ENOBUFS;
633}
634
635static int nl80211_put_iface_combinations(struct wiphy *wiphy,
636 struct sk_buff *msg)
637{
638 struct nlattr *nl_combis;
639 int i, j;
640
641 nl_combis = nla_nest_start(msg,
642 NL80211_ATTR_INTERFACE_COMBINATIONS);
643 if (!nl_combis)
644 goto nla_put_failure;
645
646 for (i = 0; i < wiphy->n_iface_combinations; i++) {
647 const struct ieee80211_iface_combination *c;
648 struct nlattr *nl_combi, *nl_limits;
649
650 c = &wiphy->iface_combinations[i];
651
652 nl_combi = nla_nest_start(msg, i + 1);
653 if (!nl_combi)
654 goto nla_put_failure;
655
656 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
657 if (!nl_limits)
658 goto nla_put_failure;
659
660 for (j = 0; j < c->n_limits; j++) {
661 struct nlattr *nl_limit;
662
663 nl_limit = nla_nest_start(msg, j + 1);
664 if (!nl_limit)
665 goto nla_put_failure;
666 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
667 c->limits[j].max);
668 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
669 c->limits[j].types))
670 goto nla_put_failure;
671 nla_nest_end(msg, nl_limit);
672 }
673
674 nla_nest_end(msg, nl_limits);
675
676 if (c->beacon_int_infra_match)
677 NLA_PUT_FLAG(msg,
678 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
679 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
680 c->num_different_channels);
681 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
682 c->max_interfaces);
683
684 nla_nest_end(msg, nl_combi);
685 }
686
687 nla_nest_end(msg, nl_combis);
688
689 return 0;
690nla_put_failure:
691 return -ENOBUFS;
692}
693
Johannes Berg55682962007-09-20 13:09:35 -0400694static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
695 struct cfg80211_registered_device *dev)
696{
697 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100698 struct nlattr *nl_bands, *nl_band;
699 struct nlattr *nl_freqs, *nl_freq;
700 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100701 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100702 enum ieee80211_band band;
703 struct ieee80211_channel *chan;
704 struct ieee80211_rate *rate;
705 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200706 const struct ieee80211_txrx_stypes *mgmt_stypes =
707 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400708
709 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
710 if (!hdr)
711 return -1;
712
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500713 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400714 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200715
Johannes Bergf5ea9122009-08-07 16:17:38 +0200716 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
717 cfg80211_rdev_list_generation);
718
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200719 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
720 dev->wiphy.retry_short);
721 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
722 dev->wiphy.retry_long);
723 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
724 dev->wiphy.frag_threshold);
725 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
726 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100727 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
728 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100729 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
730 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300731 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
732 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200733 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
734 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300735 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
736 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300737 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
738 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100739
Johannes Berge31b8212010-10-05 19:39:30 +0200740 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
741 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700742 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
743 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300744 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
745 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530746 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
747 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300748 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
749 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
750 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
751 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530752
Johannes Berg25e47c12009-04-02 20:14:06 +0200753 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
754 sizeof(u32) * dev->wiphy.n_cipher_suites,
755 dev->wiphy.cipher_suites);
756
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100757 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
758 dev->wiphy.max_num_pmkids);
759
Johannes Bergc0692b82010-08-27 14:26:53 +0300760 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
761 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
762
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900763 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
764 dev->wiphy.available_antennas_tx);
765 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
766 dev->wiphy.available_antennas_rx);
767
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200768 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
769 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
770 dev->wiphy.probe_resp_offload);
771
Bruno Randolf7f531e02010-12-16 11:30:22 +0900772 if ((dev->wiphy.available_antennas_tx ||
773 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900774 u32 tx_ant = 0, rx_ant = 0;
775 int res;
776 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
777 if (!res) {
778 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
779 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
780 }
781 }
782
Johannes Berg7527a782011-05-13 10:58:57 +0200783 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
784 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700785 goto nla_put_failure;
786
Johannes Bergee688b002008-01-24 19:38:39 +0100787 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
788 if (!nl_bands)
789 goto nla_put_failure;
790
791 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
792 if (!dev->wiphy.bands[band])
793 continue;
794
795 nl_band = nla_nest_start(msg, band);
796 if (!nl_band)
797 goto nla_put_failure;
798
Johannes Bergd51626d2008-10-09 12:20:13 +0200799 /* add HT info */
800 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
801 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
802 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
803 &dev->wiphy.bands[band]->ht_cap.mcs);
804 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
805 dev->wiphy.bands[band]->ht_cap.cap);
806 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
807 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
808 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
809 dev->wiphy.bands[band]->ht_cap.ampdu_density);
810 }
811
Johannes Bergee688b002008-01-24 19:38:39 +0100812 /* add frequencies */
813 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
814 if (!nl_freqs)
815 goto nla_put_failure;
816
817 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
818 nl_freq = nla_nest_start(msg, i);
819 if (!nl_freq)
820 goto nla_put_failure;
821
822 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100823
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400824 if (nl80211_msg_put_channel(msg, chan))
825 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200826
Johannes Bergee688b002008-01-24 19:38:39 +0100827 nla_nest_end(msg, nl_freq);
828 }
829
830 nla_nest_end(msg, nl_freqs);
831
832 /* add bitrates */
833 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
834 if (!nl_rates)
835 goto nla_put_failure;
836
837 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
838 nl_rate = nla_nest_start(msg, i);
839 if (!nl_rate)
840 goto nla_put_failure;
841
842 rate = &dev->wiphy.bands[band]->bitrates[i];
843 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
844 rate->bitrate);
845 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
846 NLA_PUT_FLAG(msg,
847 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
848
849 nla_nest_end(msg, nl_rate);
850 }
851
852 nla_nest_end(msg, nl_rates);
853
854 nla_nest_end(msg, nl_band);
855 }
856 nla_nest_end(msg, nl_bands);
857
Johannes Berg8fdc6212009-03-14 09:34:01 +0100858 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
859 if (!nl_cmds)
860 goto nla_put_failure;
861
862 i = 0;
863#define CMD(op, n) \
864 do { \
865 if (dev->ops->op) { \
866 i++; \
867 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
868 } \
869 } while (0)
870
871 CMD(add_virtual_intf, NEW_INTERFACE);
872 CMD(change_virtual_intf, SET_INTERFACE);
873 CMD(add_key, NEW_KEY);
874 CMD(add_beacon, NEW_BEACON);
875 CMD(add_station, NEW_STATION);
876 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800877 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100878 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200879 CMD(auth, AUTHENTICATE);
880 CMD(assoc, ASSOCIATE);
881 CMD(deauth, DEAUTHENTICATE);
882 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200883 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100884 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100885 CMD(set_pmksa, SET_PMKSA);
886 CMD(del_pmksa, DEL_PMKSA);
887 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100888 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
889 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200890 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200891 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100892 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100893 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200894 i++;
895 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
896 }
Johannes Bergf444de02010-05-05 15:25:02 +0200897 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400898 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300899 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
900 CMD(tdls_mgmt, TDLS_MGMT);
901 CMD(tdls_oper, TDLS_OPER);
902 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300903 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
904 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100905 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100906 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100907 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
908 i++;
909 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
910 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100911
Kalle Valo4745fc02011-11-17 19:06:10 +0200912#ifdef CONFIG_NL80211_TESTMODE
913 CMD(testmode_cmd, TESTMODE);
914#endif
915
Johannes Berg8fdc6212009-03-14 09:34:01 +0100916#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200917
Johannes Berg6829c872009-07-02 09:13:27 +0200918 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919 i++;
920 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
921 }
922
Johannes Berg6829c872009-07-02 09:13:27 +0200923 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200924 i++;
925 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
926 }
927
Johannes Berg8fdc6212009-03-14 09:34:01 +0100928 nla_nest_end(msg, nl_cmds);
929
Johannes Berg7c4ef712011-11-18 15:33:48 +0100930 if (dev->ops->remain_on_channel &&
931 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100932 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
933 dev->wiphy.max_remain_on_channel_duration);
934
Johannes Berg7c4ef712011-11-18 15:33:48 +0100935 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100936 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
937
Johannes Berg2e161f72010-08-12 15:38:38 +0200938 if (mgmt_stypes) {
939 u16 stypes;
940 struct nlattr *nl_ftypes, *nl_ifs;
941 enum nl80211_iftype ift;
942
943 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
944 if (!nl_ifs)
945 goto nla_put_failure;
946
947 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
948 nl_ftypes = nla_nest_start(msg, ift);
949 if (!nl_ftypes)
950 goto nla_put_failure;
951 i = 0;
952 stypes = mgmt_stypes[ift].tx;
953 while (stypes) {
954 if (stypes & 1)
955 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
956 (i << 4) | IEEE80211_FTYPE_MGMT);
957 stypes >>= 1;
958 i++;
959 }
960 nla_nest_end(msg, nl_ftypes);
961 }
962
Johannes Berg74b70a42010-08-24 12:15:53 +0200963 nla_nest_end(msg, nl_ifs);
964
Johannes Berg2e161f72010-08-12 15:38:38 +0200965 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
966 if (!nl_ifs)
967 goto nla_put_failure;
968
969 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
970 nl_ftypes = nla_nest_start(msg, ift);
971 if (!nl_ftypes)
972 goto nla_put_failure;
973 i = 0;
974 stypes = mgmt_stypes[ift].rx;
975 while (stypes) {
976 if (stypes & 1)
977 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
978 (i << 4) | IEEE80211_FTYPE_MGMT);
979 stypes >>= 1;
980 i++;
981 }
982 nla_nest_end(msg, nl_ftypes);
983 }
984 nla_nest_end(msg, nl_ifs);
985 }
986
Johannes Bergff1b6e62011-05-04 15:37:28 +0200987 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
988 struct nlattr *nl_wowlan;
989
990 nl_wowlan = nla_nest_start(msg,
991 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
992 if (!nl_wowlan)
993 goto nla_put_failure;
994
995 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
996 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
997 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
998 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
999 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1000 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02001001 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1002 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1003 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1004 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1005 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1006 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1007 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1008 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1009 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1010 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001011 if (dev->wiphy.wowlan.n_patterns) {
1012 struct nl80211_wowlan_pattern_support pat = {
1013 .max_patterns = dev->wiphy.wowlan.n_patterns,
1014 .min_pattern_len =
1015 dev->wiphy.wowlan.pattern_min_len,
1016 .max_pattern_len =
1017 dev->wiphy.wowlan.pattern_max_len,
1018 };
1019 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1020 sizeof(pat), &pat);
1021 }
1022
1023 nla_nest_end(msg, nl_wowlan);
1024 }
1025
Johannes Berg7527a782011-05-13 10:58:57 +02001026 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1027 dev->wiphy.software_iftypes))
1028 goto nla_put_failure;
1029
1030 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1031 goto nla_put_failure;
1032
Johannes Berg562a7482011-11-07 12:39:33 +01001033 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1034 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1035 dev->wiphy.ap_sme_capa);
1036
Johannes Berg1f074bd2011-11-06 14:13:33 +01001037 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1038
Ben Greear7e7c8922011-11-18 11:31:59 -08001039 if (dev->wiphy.ht_capa_mod_mask)
1040 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1041 sizeof(*dev->wiphy.ht_capa_mod_mask),
1042 dev->wiphy.ht_capa_mod_mask);
1043
Johannes Berg55682962007-09-20 13:09:35 -04001044 return genlmsg_end(msg, hdr);
1045
1046 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001047 genlmsg_cancel(msg, hdr);
1048 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001049}
1050
1051static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1052{
1053 int idx = 0;
1054 int start = cb->args[0];
1055 struct cfg80211_registered_device *dev;
1056
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001057 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001058 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001059 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1060 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001061 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001062 continue;
1063 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1064 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001065 dev) < 0) {
1066 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001067 break;
Julius Volzb4637272008-07-08 14:02:19 +02001068 }
Johannes Berg55682962007-09-20 13:09:35 -04001069 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001070 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001071
1072 cb->args[0] = idx;
1073
1074 return skb->len;
1075}
1076
1077static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1078{
1079 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001080 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001081
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001082 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001083 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001084 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001085
Johannes Berg4c476992010-10-04 21:36:35 +02001086 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1087 nlmsg_free(msg);
1088 return -ENOBUFS;
1089 }
Johannes Berg55682962007-09-20 13:09:35 -04001090
Johannes Berg134e6372009-07-10 09:51:34 +00001091 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001092}
1093
Jouni Malinen31888482008-10-30 16:59:24 +02001094static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1095 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1096 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1097 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1098 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1099 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1100};
1101
1102static int parse_txq_params(struct nlattr *tb[],
1103 struct ieee80211_txq_params *txq_params)
1104{
1105 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1106 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1107 !tb[NL80211_TXQ_ATTR_AIFS])
1108 return -EINVAL;
1109
1110 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1111 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1112 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1113 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1114 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1115
1116 return 0;
1117}
1118
Johannes Bergf444de02010-05-05 15:25:02 +02001119static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1120{
1121 /*
1122 * You can only set the channel explicitly for AP, mesh
1123 * and WDS type interfaces; all others have their channel
1124 * managed via their respective "establish a connection"
1125 * command (connect, join, ...)
1126 *
1127 * Monitors are special as they are normally slaved to
1128 * whatever else is going on, so they behave as though
1129 * you tried setting the wiphy channel itself.
1130 */
1131 return !wdev ||
1132 wdev->iftype == NL80211_IFTYPE_AP ||
1133 wdev->iftype == NL80211_IFTYPE_WDS ||
1134 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001135 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1136 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001137}
1138
1139static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1140 struct wireless_dev *wdev,
1141 struct genl_info *info)
1142{
1143 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1144 u32 freq;
1145 int result;
1146
1147 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1148 return -EINVAL;
1149
1150 if (!nl80211_can_set_dev_channel(wdev))
1151 return -EOPNOTSUPP;
1152
1153 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1154 channel_type = nla_get_u32(info->attrs[
1155 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1156 if (channel_type != NL80211_CHAN_NO_HT &&
1157 channel_type != NL80211_CHAN_HT20 &&
1158 channel_type != NL80211_CHAN_HT40PLUS &&
1159 channel_type != NL80211_CHAN_HT40MINUS)
1160 return -EINVAL;
1161 }
1162
1163 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1164
1165 mutex_lock(&rdev->devlist_mtx);
1166 if (wdev) {
1167 wdev_lock(wdev);
1168 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1169 wdev_unlock(wdev);
1170 } else {
1171 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1172 }
1173 mutex_unlock(&rdev->devlist_mtx);
1174
1175 return result;
1176}
1177
1178static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1179{
Johannes Berg4c476992010-10-04 21:36:35 +02001180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1181 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001182
Johannes Berg4c476992010-10-04 21:36:35 +02001183 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001184}
1185
Bill Jordane8347eb2010-10-01 13:54:28 -04001186static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1187{
Johannes Berg43b19952010-10-07 13:10:30 +02001188 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1189 struct net_device *dev = info->user_ptr[1];
1190 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001191 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001192
1193 if (!info->attrs[NL80211_ATTR_MAC])
1194 return -EINVAL;
1195
Johannes Berg43b19952010-10-07 13:10:30 +02001196 if (netif_running(dev))
1197 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001198
Johannes Berg43b19952010-10-07 13:10:30 +02001199 if (!rdev->ops->set_wds_peer)
1200 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001201
Johannes Berg43b19952010-10-07 13:10:30 +02001202 if (wdev->iftype != NL80211_IFTYPE_WDS)
1203 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001204
1205 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001206 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001207}
1208
1209
Johannes Berg55682962007-09-20 13:09:35 -04001210static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1211{
1212 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001213 struct net_device *netdev = NULL;
1214 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001215 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001216 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001217 u32 changed;
1218 u8 retry_short = 0, retry_long = 0;
1219 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001220 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001221
Johannes Bergf444de02010-05-05 15:25:02 +02001222 /*
1223 * Try to find the wiphy and netdev. Normally this
1224 * function shouldn't need the netdev, but this is
1225 * done for backward compatibility -- previously
1226 * setting the channel was done per wiphy, but now
1227 * it is per netdev. Previous userland like hostapd
1228 * also passed a netdev to set_wiphy, so that it is
1229 * possible to let that go to the right netdev!
1230 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001231 mutex_lock(&cfg80211_mutex);
1232
Johannes Bergf444de02010-05-05 15:25:02 +02001233 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1234 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1235
1236 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1237 if (netdev && netdev->ieee80211_ptr) {
1238 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1239 mutex_lock(&rdev->mtx);
1240 } else
1241 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001242 }
1243
Johannes Bergf444de02010-05-05 15:25:02 +02001244 if (!netdev) {
1245 rdev = __cfg80211_rdev_from_info(info);
1246 if (IS_ERR(rdev)) {
1247 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001248 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001249 }
1250 wdev = NULL;
1251 netdev = NULL;
1252 result = 0;
1253
1254 mutex_lock(&rdev->mtx);
1255 } else if (netif_running(netdev) &&
1256 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1257 wdev = netdev->ieee80211_ptr;
1258 else
1259 wdev = NULL;
1260
1261 /*
1262 * end workaround code, by now the rdev is available
1263 * and locked, and wdev may or may not be NULL.
1264 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001265
1266 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001267 result = cfg80211_dev_rename(
1268 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001269
1270 mutex_unlock(&cfg80211_mutex);
1271
1272 if (result)
1273 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001274
Jouni Malinen31888482008-10-30 16:59:24 +02001275 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1276 struct ieee80211_txq_params txq_params;
1277 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1278
1279 if (!rdev->ops->set_txq_params) {
1280 result = -EOPNOTSUPP;
1281 goto bad_res;
1282 }
1283
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001284 if (!netdev) {
1285 result = -EINVAL;
1286 goto bad_res;
1287 }
1288
Johannes Berg133a3ff2011-11-03 14:50:13 +01001289 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1290 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1291 result = -EINVAL;
1292 goto bad_res;
1293 }
1294
Jouni Malinen31888482008-10-30 16:59:24 +02001295 nla_for_each_nested(nl_txq_params,
1296 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1297 rem_txq_params) {
1298 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1299 nla_data(nl_txq_params),
1300 nla_len(nl_txq_params),
1301 txq_params_policy);
1302 result = parse_txq_params(tb, &txq_params);
1303 if (result)
1304 goto bad_res;
1305
1306 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001307 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001308 &txq_params);
1309 if (result)
1310 goto bad_res;
1311 }
1312 }
1313
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001314 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001315 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001316 if (result)
1317 goto bad_res;
1318 }
1319
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001320 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1321 enum nl80211_tx_power_setting type;
1322 int idx, mbm = 0;
1323
1324 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001325 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001326 goto bad_res;
1327 }
1328
1329 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1330 type = nla_get_u32(info->attrs[idx]);
1331
1332 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1333 (type != NL80211_TX_POWER_AUTOMATIC)) {
1334 result = -EINVAL;
1335 goto bad_res;
1336 }
1337
1338 if (type != NL80211_TX_POWER_AUTOMATIC) {
1339 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1340 mbm = nla_get_u32(info->attrs[idx]);
1341 }
1342
1343 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1344 if (result)
1345 goto bad_res;
1346 }
1347
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001348 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1349 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1350 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001351 if ((!rdev->wiphy.available_antennas_tx &&
1352 !rdev->wiphy.available_antennas_rx) ||
1353 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001354 result = -EOPNOTSUPP;
1355 goto bad_res;
1356 }
1357
1358 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1359 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1360
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001361 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001362 * available antenna masks, except for the "all" mask */
1363 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1364 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001365 result = -EINVAL;
1366 goto bad_res;
1367 }
1368
Bruno Randolf7f531e02010-12-16 11:30:22 +09001369 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1370 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001371
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001372 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1373 if (result)
1374 goto bad_res;
1375 }
1376
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001377 changed = 0;
1378
1379 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1380 retry_short = nla_get_u8(
1381 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1382 if (retry_short == 0) {
1383 result = -EINVAL;
1384 goto bad_res;
1385 }
1386 changed |= WIPHY_PARAM_RETRY_SHORT;
1387 }
1388
1389 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1390 retry_long = nla_get_u8(
1391 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1392 if (retry_long == 0) {
1393 result = -EINVAL;
1394 goto bad_res;
1395 }
1396 changed |= WIPHY_PARAM_RETRY_LONG;
1397 }
1398
1399 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1400 frag_threshold = nla_get_u32(
1401 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1402 if (frag_threshold < 256) {
1403 result = -EINVAL;
1404 goto bad_res;
1405 }
1406 if (frag_threshold != (u32) -1) {
1407 /*
1408 * Fragments (apart from the last one) are required to
1409 * have even length. Make the fragmentation code
1410 * simpler by stripping LSB should someone try to use
1411 * odd threshold value.
1412 */
1413 frag_threshold &= ~0x1;
1414 }
1415 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1416 }
1417
1418 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1419 rts_threshold = nla_get_u32(
1420 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1421 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1422 }
1423
Lukáš Turek81077e82009-12-21 22:50:47 +01001424 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1425 coverage_class = nla_get_u8(
1426 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1427 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1428 }
1429
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001430 if (changed) {
1431 u8 old_retry_short, old_retry_long;
1432 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001433 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001434
1435 if (!rdev->ops->set_wiphy_params) {
1436 result = -EOPNOTSUPP;
1437 goto bad_res;
1438 }
1439
1440 old_retry_short = rdev->wiphy.retry_short;
1441 old_retry_long = rdev->wiphy.retry_long;
1442 old_frag_threshold = rdev->wiphy.frag_threshold;
1443 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001444 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001445
1446 if (changed & WIPHY_PARAM_RETRY_SHORT)
1447 rdev->wiphy.retry_short = retry_short;
1448 if (changed & WIPHY_PARAM_RETRY_LONG)
1449 rdev->wiphy.retry_long = retry_long;
1450 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1451 rdev->wiphy.frag_threshold = frag_threshold;
1452 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1453 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001454 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1455 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001456
1457 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1458 if (result) {
1459 rdev->wiphy.retry_short = old_retry_short;
1460 rdev->wiphy.retry_long = old_retry_long;
1461 rdev->wiphy.frag_threshold = old_frag_threshold;
1462 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001463 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001464 }
1465 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001466
Johannes Berg306d6112008-12-08 12:39:04 +01001467 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001468 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001469 if (netdev)
1470 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001471 return result;
1472}
1473
1474
1475static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001476 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001477 struct net_device *dev)
1478{
1479 void *hdr;
1480
1481 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1482 if (!hdr)
1483 return -1;
1484
1485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001486 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001487 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001488 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001489
1490 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1491 rdev->devlist_generation ^
1492 (cfg80211_rdev_list_generation << 2));
1493
Johannes Berg55682962007-09-20 13:09:35 -04001494 return genlmsg_end(msg, hdr);
1495
1496 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001497 genlmsg_cancel(msg, hdr);
1498 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001499}
1500
1501static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1502{
1503 int wp_idx = 0;
1504 int if_idx = 0;
1505 int wp_start = cb->args[0];
1506 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001507 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001508 struct wireless_dev *wdev;
1509
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001510 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001511 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1512 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001513 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001514 if (wp_idx < wp_start) {
1515 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001516 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001517 }
Johannes Berg55682962007-09-20 13:09:35 -04001518 if_idx = 0;
1519
Johannes Bergf5ea9122009-08-07 16:17:38 +02001520 mutex_lock(&rdev->devlist_mtx);
1521 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001522 if (if_idx < if_start) {
1523 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001524 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001525 }
Johannes Berg55682962007-09-20 13:09:35 -04001526 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1527 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001528 rdev, wdev->netdev) < 0) {
1529 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001530 goto out;
1531 }
1532 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001533 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001534 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001535
1536 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001537 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001538 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001539 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001540
1541 cb->args[0] = wp_idx;
1542 cb->args[1] = if_idx;
1543
1544 return skb->len;
1545}
1546
1547static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1548{
1549 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001550 struct cfg80211_registered_device *dev = info->user_ptr[0];
1551 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001552
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001553 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001554 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001555 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001556
Johannes Bergd7264052009-04-19 16:23:20 +02001557 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001558 dev, netdev) < 0) {
1559 nlmsg_free(msg);
1560 return -ENOBUFS;
1561 }
Johannes Berg55682962007-09-20 13:09:35 -04001562
Johannes Berg134e6372009-07-10 09:51:34 +00001563 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001564}
1565
Michael Wu66f7ac52008-01-31 19:48:22 +01001566static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1567 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1568 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1569 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1570 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1571 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1572};
1573
1574static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1575{
1576 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1577 int flag;
1578
1579 *mntrflags = 0;
1580
1581 if (!nla)
1582 return -EINVAL;
1583
1584 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1585 nla, mntr_flags_policy))
1586 return -EINVAL;
1587
1588 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1589 if (flags[flag])
1590 *mntrflags |= (1<<flag);
1591
1592 return 0;
1593}
1594
Johannes Berg9bc383d2009-11-19 11:55:19 +01001595static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001596 struct net_device *netdev, u8 use_4addr,
1597 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001598{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001599 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001600 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001601 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001602 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001603 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001604
1605 switch (iftype) {
1606 case NL80211_IFTYPE_AP_VLAN:
1607 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1608 return 0;
1609 break;
1610 case NL80211_IFTYPE_STATION:
1611 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1612 return 0;
1613 break;
1614 default:
1615 break;
1616 }
1617
1618 return -EOPNOTSUPP;
1619}
1620
Johannes Berg55682962007-09-20 13:09:35 -04001621static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1622{
Johannes Berg4c476992010-10-04 21:36:35 +02001623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001624 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001625 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001626 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001627 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001628 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001629 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001630
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001631 memset(&params, 0, sizeof(params));
1632
Johannes Berg04a773a2009-04-19 21:24:32 +02001633 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001634
Johannes Berg723b0382008-09-16 20:22:09 +02001635 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001636 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001637 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001638 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001639 if (ntype > NL80211_IFTYPE_MAX)
1640 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001641 }
1642
Johannes Berg92ffe052008-09-16 20:39:36 +02001643 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001644 struct wireless_dev *wdev = dev->ieee80211_ptr;
1645
Johannes Berg4c476992010-10-04 21:36:35 +02001646 if (ntype != NL80211_IFTYPE_MESH_POINT)
1647 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001648 if (netif_running(dev))
1649 return -EBUSY;
1650
1651 wdev_lock(wdev);
1652 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1653 IEEE80211_MAX_MESH_ID_LEN);
1654 wdev->mesh_id_up_len =
1655 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1656 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1657 wdev->mesh_id_up_len);
1658 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001659 }
1660
Felix Fietkau8b787642009-11-10 18:53:10 +01001661 if (info->attrs[NL80211_ATTR_4ADDR]) {
1662 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1663 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001664 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001665 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001666 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001667 } else {
1668 params.use_4addr = -1;
1669 }
1670
Johannes Berg92ffe052008-09-16 20:39:36 +02001671 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001672 if (ntype != NL80211_IFTYPE_MONITOR)
1673 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001674 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1675 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001676 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001677 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001678
1679 flags = &_flags;
1680 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001681 }
Johannes Berg3b858752009-03-12 09:55:09 +01001682
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001683 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001684 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001685 else
1686 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001687
Johannes Berg9bc383d2009-11-19 11:55:19 +01001688 if (!err && params.use_4addr != -1)
1689 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1690
Johannes Berg55682962007-09-20 13:09:35 -04001691 return err;
1692}
1693
1694static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1695{
Johannes Berg4c476992010-10-04 21:36:35 +02001696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001697 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001698 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001699 int err;
1700 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001701 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001702
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001703 memset(&params, 0, sizeof(params));
1704
Johannes Berg55682962007-09-20 13:09:35 -04001705 if (!info->attrs[NL80211_ATTR_IFNAME])
1706 return -EINVAL;
1707
1708 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1709 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1710 if (type > NL80211_IFTYPE_MAX)
1711 return -EINVAL;
1712 }
1713
Johannes Berg79c97e92009-07-07 03:56:12 +02001714 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001715 !(rdev->wiphy.interface_modes & (1 << type)))
1716 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001717
Johannes Berg9bc383d2009-11-19 11:55:19 +01001718 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001719 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001720 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001721 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001722 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001723 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001724
Michael Wu66f7ac52008-01-31 19:48:22 +01001725 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1726 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1727 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001728 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001729 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001730 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001731 if (IS_ERR(dev))
1732 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001733
Johannes Berg29cbe682010-12-03 09:20:44 +01001734 if (type == NL80211_IFTYPE_MESH_POINT &&
1735 info->attrs[NL80211_ATTR_MESH_ID]) {
1736 struct wireless_dev *wdev = dev->ieee80211_ptr;
1737
1738 wdev_lock(wdev);
1739 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1740 IEEE80211_MAX_MESH_ID_LEN);
1741 wdev->mesh_id_up_len =
1742 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1743 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1744 wdev->mesh_id_up_len);
1745 wdev_unlock(wdev);
1746 }
1747
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001748 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001749}
1750
1751static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1752{
Johannes Berg4c476992010-10-04 21:36:35 +02001753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1754 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001755
Johannes Berg4c476992010-10-04 21:36:35 +02001756 if (!rdev->ops->del_virtual_intf)
1757 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001758
Johannes Berg4c476992010-10-04 21:36:35 +02001759 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001760}
1761
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001762static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1763{
1764 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1765 struct net_device *dev = info->user_ptr[1];
1766 u16 noack_map;
1767
1768 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1769 return -EINVAL;
1770
1771 if (!rdev->ops->set_noack_map)
1772 return -EOPNOTSUPP;
1773
1774 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1775
1776 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1777}
1778
Johannes Berg41ade002007-12-19 02:03:29 +01001779struct get_key_cookie {
1780 struct sk_buff *msg;
1781 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001782 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001783};
1784
1785static void get_key_callback(void *c, struct key_params *params)
1786{
Johannes Bergb9454e82009-07-08 13:29:08 +02001787 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001788 struct get_key_cookie *cookie = c;
1789
1790 if (params->key)
1791 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1792 params->key_len, params->key);
1793
1794 if (params->seq)
1795 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1796 params->seq_len, params->seq);
1797
1798 if (params->cipher)
1799 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1800 params->cipher);
1801
Johannes Bergb9454e82009-07-08 13:29:08 +02001802 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1803 if (!key)
1804 goto nla_put_failure;
1805
1806 if (params->key)
1807 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1808 params->key_len, params->key);
1809
1810 if (params->seq)
1811 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1812 params->seq_len, params->seq);
1813
1814 if (params->cipher)
1815 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1816 params->cipher);
1817
1818 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1819
1820 nla_nest_end(cookie->msg, key);
1821
Johannes Berg41ade002007-12-19 02:03:29 +01001822 return;
1823 nla_put_failure:
1824 cookie->error = 1;
1825}
1826
1827static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1828{
Johannes Berg4c476992010-10-04 21:36:35 +02001829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001830 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001831 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001832 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001833 const u8 *mac_addr = NULL;
1834 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001835 struct get_key_cookie cookie = {
1836 .error = 0,
1837 };
1838 void *hdr;
1839 struct sk_buff *msg;
1840
1841 if (info->attrs[NL80211_ATTR_KEY_IDX])
1842 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1843
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001844 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001845 return -EINVAL;
1846
1847 if (info->attrs[NL80211_ATTR_MAC])
1848 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1849
Johannes Berge31b8212010-10-05 19:39:30 +02001850 pairwise = !!mac_addr;
1851 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1852 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1853 if (kt >= NUM_NL80211_KEYTYPES)
1854 return -EINVAL;
1855 if (kt != NL80211_KEYTYPE_GROUP &&
1856 kt != NL80211_KEYTYPE_PAIRWISE)
1857 return -EINVAL;
1858 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1859 }
1860
Johannes Berg4c476992010-10-04 21:36:35 +02001861 if (!rdev->ops->get_key)
1862 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001863
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001864 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001865 if (!msg)
1866 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001867
1868 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1869 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001870 if (IS_ERR(hdr))
1871 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001872
1873 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001874 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001875
1876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1877 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1878 if (mac_addr)
1879 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1880
Johannes Berge31b8212010-10-05 19:39:30 +02001881 if (pairwise && mac_addr &&
1882 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1883 return -ENOENT;
1884
1885 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1886 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001887
1888 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001889 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001890
1891 if (cookie.error)
1892 goto nla_put_failure;
1893
1894 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001895 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001896
1897 nla_put_failure:
1898 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001899 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001900 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001901 return err;
1902}
1903
1904static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1905{
Johannes Berg4c476992010-10-04 21:36:35 +02001906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001907 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001908 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001909 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001910
Johannes Bergb9454e82009-07-08 13:29:08 +02001911 err = nl80211_parse_key(info, &key);
1912 if (err)
1913 return err;
1914
1915 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001916 return -EINVAL;
1917
Johannes Bergb9454e82009-07-08 13:29:08 +02001918 /* only support setting default key */
1919 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001920 return -EINVAL;
1921
Johannes Bergfffd0932009-07-08 14:22:54 +02001922 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001923
1924 if (key.def) {
1925 if (!rdev->ops->set_default_key) {
1926 err = -EOPNOTSUPP;
1927 goto out;
1928 }
1929
1930 err = nl80211_key_allowed(dev->ieee80211_ptr);
1931 if (err)
1932 goto out;
1933
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001934 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1935 key.def_uni, key.def_multi);
1936
1937 if (err)
1938 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001939
Johannes Berg3d23e342009-09-29 23:27:28 +02001940#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001941 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001942#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001943 } else {
1944 if (key.def_uni || !key.def_multi) {
1945 err = -EINVAL;
1946 goto out;
1947 }
1948
1949 if (!rdev->ops->set_default_mgmt_key) {
1950 err = -EOPNOTSUPP;
1951 goto out;
1952 }
1953
1954 err = nl80211_key_allowed(dev->ieee80211_ptr);
1955 if (err)
1956 goto out;
1957
1958 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1959 dev, key.idx);
1960 if (err)
1961 goto out;
1962
1963#ifdef CONFIG_CFG80211_WEXT
1964 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1965#endif
1966 }
1967
1968 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001969 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001970
Johannes Berg41ade002007-12-19 02:03:29 +01001971 return err;
1972}
1973
1974static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1975{
Johannes Berg4c476992010-10-04 21:36:35 +02001976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001977 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001978 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001979 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001980 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001981
Johannes Bergb9454e82009-07-08 13:29:08 +02001982 err = nl80211_parse_key(info, &key);
1983 if (err)
1984 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001985
Johannes Bergb9454e82009-07-08 13:29:08 +02001986 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001987 return -EINVAL;
1988
Johannes Berg41ade002007-12-19 02:03:29 +01001989 if (info->attrs[NL80211_ATTR_MAC])
1990 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1991
Johannes Berge31b8212010-10-05 19:39:30 +02001992 if (key.type == -1) {
1993 if (mac_addr)
1994 key.type = NL80211_KEYTYPE_PAIRWISE;
1995 else
1996 key.type = NL80211_KEYTYPE_GROUP;
1997 }
1998
1999 /* for now */
2000 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2001 key.type != NL80211_KEYTYPE_GROUP)
2002 return -EINVAL;
2003
Johannes Berg4c476992010-10-04 21:36:35 +02002004 if (!rdev->ops->add_key)
2005 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002006
Johannes Berge31b8212010-10-05 19:39:30 +02002007 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2008 key.type == NL80211_KEYTYPE_PAIRWISE,
2009 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002010 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002011
2012 wdev_lock(dev->ieee80211_ptr);
2013 err = nl80211_key_allowed(dev->ieee80211_ptr);
2014 if (!err)
2015 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002016 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002017 mac_addr, &key.p);
2018 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002019
Johannes Berg41ade002007-12-19 02:03:29 +01002020 return err;
2021}
2022
2023static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2024{
Johannes Berg4c476992010-10-04 21:36:35 +02002025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002026 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002027 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002028 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002029 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002030
Johannes Bergb9454e82009-07-08 13:29:08 +02002031 err = nl80211_parse_key(info, &key);
2032 if (err)
2033 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002034
2035 if (info->attrs[NL80211_ATTR_MAC])
2036 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2037
Johannes Berge31b8212010-10-05 19:39:30 +02002038 if (key.type == -1) {
2039 if (mac_addr)
2040 key.type = NL80211_KEYTYPE_PAIRWISE;
2041 else
2042 key.type = NL80211_KEYTYPE_GROUP;
2043 }
2044
2045 /* for now */
2046 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2047 key.type != NL80211_KEYTYPE_GROUP)
2048 return -EINVAL;
2049
Johannes Berg4c476992010-10-04 21:36:35 +02002050 if (!rdev->ops->del_key)
2051 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002052
Johannes Bergfffd0932009-07-08 14:22:54 +02002053 wdev_lock(dev->ieee80211_ptr);
2054 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002055
2056 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2057 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2058 err = -ENOENT;
2059
Johannes Bergfffd0932009-07-08 14:22:54 +02002060 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002061 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2062 key.type == NL80211_KEYTYPE_PAIRWISE,
2063 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002064
Johannes Berg3d23e342009-09-29 23:27:28 +02002065#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002066 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002067 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002068 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002069 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002070 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2071 }
2072#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002073 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002074
Johannes Berg41ade002007-12-19 02:03:29 +01002075 return err;
2076}
2077
Johannes Berged1b6cc2007-12-19 02:03:32 +01002078static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2079{
2080 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2081 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002082 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2083 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002084 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002085 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002086 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002087
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002088 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2089 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2090 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2091 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002092 return -EINVAL;
2093
Johannes Berg074ac8d2010-09-16 14:58:22 +02002094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002095 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2096 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002097
Johannes Berg56d18932011-05-09 18:41:15 +02002098 memset(&params, 0, sizeof(params));
2099
Johannes Berged1b6cc2007-12-19 02:03:32 +01002100 switch (info->genlhdr->cmd) {
2101 case NL80211_CMD_NEW_BEACON:
2102 /* these are required for NEW_BEACON */
2103 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2104 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002105 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2106 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002107
Johannes Berg56d18932011-05-09 18:41:15 +02002108 params.interval =
2109 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2110 params.dtim_period =
2111 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2112
2113 err = cfg80211_validate_beacon_int(rdev, params.interval);
2114 if (err)
2115 return err;
2116
Jouni Malinen32e9de82011-08-10 23:53:31 +03002117 /*
2118 * In theory, some of these attributes could be required for
2119 * NEW_BEACON, but since they were not used when the command was
2120 * originally added, keep them optional for old user space
2121 * programs to work with drivers that do not need the additional
2122 * information.
2123 */
2124 if (info->attrs[NL80211_ATTR_SSID]) {
2125 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2126 params.ssid_len =
2127 nla_len(info->attrs[NL80211_ATTR_SSID]);
2128 if (params.ssid_len == 0 ||
2129 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2130 return -EINVAL;
2131 }
2132
2133 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2134 params.hidden_ssid = nla_get_u32(
2135 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2136 if (params.hidden_ssid !=
2137 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2138 params.hidden_ssid !=
2139 NL80211_HIDDEN_SSID_ZERO_LEN &&
2140 params.hidden_ssid !=
2141 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2142 return -EINVAL;
2143 }
2144
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002145 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2146
2147 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2148 params.auth_type = nla_get_u32(
2149 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2150 if (!nl80211_valid_auth_type(params.auth_type))
2151 return -EINVAL;
2152 } else
2153 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2154
2155 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2156 NL80211_MAX_NR_CIPHER_SUITES);
2157 if (err)
2158 return err;
2159
Johannes Berg79c97e92009-07-07 03:56:12 +02002160 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002161 break;
2162 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002163 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002164 break;
2165 default:
2166 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002167 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002168 }
2169
Johannes Berg4c476992010-10-04 21:36:35 +02002170 if (!call)
2171 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002172
Johannes Berged1b6cc2007-12-19 02:03:32 +01002173 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2174 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2175 params.head_len =
2176 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2177 haveinfo = 1;
2178 }
2179
2180 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2181 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2182 params.tail_len =
2183 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2184 haveinfo = 1;
2185 }
2186
Johannes Berg4c476992010-10-04 21:36:35 +02002187 if (!haveinfo)
2188 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002189
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002190 if (info->attrs[NL80211_ATTR_IE]) {
2191 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2192 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2193 }
2194
2195 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2196 params.proberesp_ies =
2197 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2198 params.proberesp_ies_len =
2199 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2200 }
2201
2202 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2203 params.assocresp_ies =
2204 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2205 params.assocresp_ies_len =
2206 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2207 }
2208
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002209 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2210 params.probe_resp =
2211 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2212 params.probe_resp_len =
2213 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2214 }
2215
Johannes Berg56d18932011-05-09 18:41:15 +02002216 err = call(&rdev->wiphy, dev, &params);
2217 if (!err && params.interval)
2218 wdev->beacon_interval = params.interval;
2219 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002220}
2221
2222static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2223{
Johannes Berg4c476992010-10-04 21:36:35 +02002224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2225 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002226 struct wireless_dev *wdev = dev->ieee80211_ptr;
2227 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002228
Johannes Berg4c476992010-10-04 21:36:35 +02002229 if (!rdev->ops->del_beacon)
2230 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002231
Johannes Berg074ac8d2010-09-16 14:58:22 +02002232 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002233 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2234 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002235
Johannes Berg56d18932011-05-09 18:41:15 +02002236 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2237 if (!err)
2238 wdev->beacon_interval = 0;
2239 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002240}
2241
Johannes Berg5727ef12007-12-19 02:03:34 +01002242static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2243 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2244 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2245 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002246 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002247 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002248 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002249};
2250
Johannes Bergeccb8e82009-05-11 21:57:56 +03002251static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002252 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002253 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002254{
2255 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002256 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002257 int flag;
2258
Johannes Bergeccb8e82009-05-11 21:57:56 +03002259 /*
2260 * Try parsing the new attribute first so userspace
2261 * can specify both for older kernels.
2262 */
2263 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2264 if (nla) {
2265 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002266
Johannes Bergeccb8e82009-05-11 21:57:56 +03002267 sta_flags = nla_data(nla);
2268 params->sta_flags_mask = sta_flags->mask;
2269 params->sta_flags_set = sta_flags->set;
2270 if ((params->sta_flags_mask |
2271 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2272 return -EINVAL;
2273 return 0;
2274 }
2275
2276 /* if present, parse the old attribute */
2277
2278 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002279 if (!nla)
2280 return 0;
2281
2282 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2283 nla, sta_flags_policy))
2284 return -EINVAL;
2285
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002286 /*
2287 * Only allow certain flags for interface types so that
2288 * other attributes are silently ignored. Remember that
2289 * this is backward compatibility code with old userspace
2290 * and shouldn't be hit in other cases anyway.
2291 */
2292 switch (iftype) {
2293 case NL80211_IFTYPE_AP:
2294 case NL80211_IFTYPE_AP_VLAN:
2295 case NL80211_IFTYPE_P2P_GO:
2296 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2297 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2298 BIT(NL80211_STA_FLAG_WME) |
2299 BIT(NL80211_STA_FLAG_MFP);
2300 break;
2301 case NL80211_IFTYPE_P2P_CLIENT:
2302 case NL80211_IFTYPE_STATION:
2303 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2304 BIT(NL80211_STA_FLAG_TDLS_PEER);
2305 break;
2306 case NL80211_IFTYPE_MESH_POINT:
2307 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2308 BIT(NL80211_STA_FLAG_MFP) |
2309 BIT(NL80211_STA_FLAG_AUTHORIZED);
2310 default:
2311 return -EINVAL;
2312 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002313
2314 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2315 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002316 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002317
2318 return 0;
2319}
2320
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002321static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2322 int attr)
2323{
2324 struct nlattr *rate;
2325 u16 bitrate;
2326
2327 rate = nla_nest_start(msg, attr);
2328 if (!rate)
2329 goto nla_put_failure;
2330
2331 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2332 bitrate = cfg80211_calculate_bitrate(info);
2333 if (bitrate > 0)
2334 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2335
2336 if (info->flags & RATE_INFO_FLAGS_MCS)
2337 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2338 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2339 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2340 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2341 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2342
2343 nla_nest_end(msg, rate);
2344 return true;
2345
2346nla_put_failure:
2347 return false;
2348}
2349
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002350static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2351 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002352 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002353{
2354 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002355 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002356
2357 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2358 if (!hdr)
2359 return -1;
2360
2361 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2362 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2363
Johannes Bergf5ea9122009-08-07 16:17:38 +02002364 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2365
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2367 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002368 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302369 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2370 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2371 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2373 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2374 sinfo->inactive_time);
2375 if (sinfo->filled & STATION_INFO_RX_BYTES)
2376 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2377 sinfo->rx_bytes);
2378 if (sinfo->filled & STATION_INFO_TX_BYTES)
2379 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2380 sinfo->tx_bytes);
2381 if (sinfo->filled & STATION_INFO_LLID)
2382 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2383 sinfo->llid);
2384 if (sinfo->filled & STATION_INFO_PLID)
2385 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2386 sinfo->plid);
2387 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2388 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2389 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002390 if (sinfo->filled & STATION_INFO_SIGNAL)
2391 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2392 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002393 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2394 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2395 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002396 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002397 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2398 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002399 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002400 }
2401 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2402 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2403 NL80211_STA_INFO_RX_BITRATE))
2404 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002405 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002406 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2407 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2408 sinfo->rx_packets);
2409 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2410 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2411 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002412 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2413 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2414 sinfo->tx_retries);
2415 if (sinfo->filled & STATION_INFO_TX_FAILED)
2416 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2417 sinfo->tx_failed);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002418 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2419 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2420 sinfo->beacon_loss_count);
Paul Stewartf4263c92011-03-31 09:25:41 -07002421 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2422 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2423 if (!bss_param)
2424 goto nla_put_failure;
2425
2426 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2427 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2428 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2429 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2430 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2431 NLA_PUT_FLAG(msg,
2432 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2433 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2434 sinfo->bss_param.dtim_period);
2435 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2436 sinfo->bss_param.beacon_interval);
2437
2438 nla_nest_end(msg, bss_param);
2439 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002440 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2441 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2442 sizeof(struct nl80211_sta_flag_update),
2443 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002444 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002445
Felix Fietkau040bdf72011-08-10 19:00:33 -06002446 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002447 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2448 sinfo->assoc_req_ies);
2449
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002450 return genlmsg_end(msg, hdr);
2451
2452 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002453 genlmsg_cancel(msg, hdr);
2454 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002455}
2456
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002457static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002458 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002459{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002460 struct station_info sinfo;
2461 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002462 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002463 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002464 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002465 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002466
Johannes Berg67748892010-10-04 21:14:06 +02002467 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2468 if (err)
2469 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002470
2471 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002472 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002473 goto out_err;
2474 }
2475
Johannes Bergbba95fe2008-07-29 13:22:51 +02002476 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002477 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002478 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2479 mac_addr, &sinfo);
2480 if (err == -ENOENT)
2481 break;
2482 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002483 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002484
2485 if (nl80211_send_station(skb,
2486 NETLINK_CB(cb->skb).pid,
2487 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2488 netdev, mac_addr,
2489 &sinfo) < 0)
2490 goto out;
2491
2492 sta_idx++;
2493 }
2494
2495
2496 out:
2497 cb->args[1] = sta_idx;
2498 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002499 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002500 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002501
2502 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002504
Johannes Berg5727ef12007-12-19 02:03:34 +01002505static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2506{
Johannes Berg4c476992010-10-04 21:36:35 +02002507 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2508 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002509 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002510 struct sk_buff *msg;
2511 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002512 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002513
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002514 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002515
2516 if (!info->attrs[NL80211_ATTR_MAC])
2517 return -EINVAL;
2518
2519 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2520
Johannes Berg4c476992010-10-04 21:36:35 +02002521 if (!rdev->ops->get_station)
2522 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002523
Johannes Berg79c97e92009-07-07 03:56:12 +02002524 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002525 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002526 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002527
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002528 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002529 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002530 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002531
2532 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002533 dev, mac_addr, &sinfo) < 0) {
2534 nlmsg_free(msg);
2535 return -ENOBUFS;
2536 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002537
Johannes Berg4c476992010-10-04 21:36:35 +02002538 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002539}
2540
2541/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002542 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002543 */
Johannes Berg80b99892011-11-18 16:23:01 +01002544static struct net_device *get_vlan(struct genl_info *info,
2545 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002546{
Johannes Berg463d0182009-07-14 00:33:35 +02002547 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002548 struct net_device *v;
2549 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002550
Johannes Berg80b99892011-11-18 16:23:01 +01002551 if (!vlanattr)
2552 return NULL;
2553
2554 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2555 if (!v)
2556 return ERR_PTR(-ENODEV);
2557
2558 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2559 ret = -EINVAL;
2560 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002561 }
Johannes Berg80b99892011-11-18 16:23:01 +01002562
2563 if (!netif_running(v)) {
2564 ret = -ENETDOWN;
2565 goto error;
2566 }
2567
2568 return v;
2569 error:
2570 dev_put(v);
2571 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002572}
2573
2574static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2575{
Johannes Berg4c476992010-10-04 21:36:35 +02002576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002577 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002578 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002579 struct station_parameters params;
2580 u8 *mac_addr = NULL;
2581
2582 memset(&params, 0, sizeof(params));
2583
2584 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002585 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002586
2587 if (info->attrs[NL80211_ATTR_STA_AID])
2588 return -EINVAL;
2589
2590 if (!info->attrs[NL80211_ATTR_MAC])
2591 return -EINVAL;
2592
2593 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2594
2595 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2596 params.supported_rates =
2597 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2598 params.supported_rates_len =
2599 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2600 }
2601
2602 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2603 params.listen_interval =
2604 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2605
Jouni Malinen36aedc92008-08-25 11:58:58 +03002606 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2607 params.ht_capa =
2608 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2609
Johannes Bergbdd90d52011-12-14 12:20:27 +01002610 if (!rdev->ops->change_station)
2611 return -EOPNOTSUPP;
2612
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002613 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002614 return -EINVAL;
2615
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002616 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2617 params.plink_action =
2618 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2619
Javier Cardona9c3990a2011-05-03 16:57:11 -07002620 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2621 params.plink_state =
2622 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2623
Johannes Berga97f4422009-06-18 17:23:43 +02002624 switch (dev->ieee80211_ptr->iftype) {
2625 case NL80211_IFTYPE_AP:
2626 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002627 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002628 /* disallow mesh-specific things */
2629 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002630 return -EINVAL;
2631
2632 /* TDLS can't be set, ... */
2633 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2634 return -EINVAL;
2635 /*
2636 * ... but don't bother the driver with it. This works around
2637 * a hostapd/wpa_supplicant issue -- it always includes the
2638 * TLDS_PEER flag in the mask even for AP mode.
2639 */
2640 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2641
2642 /* accept only the listed bits */
2643 if (params.sta_flags_mask &
2644 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2645 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2646 BIT(NL80211_STA_FLAG_WME) |
2647 BIT(NL80211_STA_FLAG_MFP)))
2648 return -EINVAL;
2649
2650 /* must be last in here for error handling */
2651 params.vlan = get_vlan(info, rdev);
2652 if (IS_ERR(params.vlan))
2653 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002654 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002655 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002656 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002657 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002658 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002659 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002660 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002661 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002662 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002663 return -EINVAL;
2664 /*
2665 * Don't allow userspace to change the TDLS_PEER flag,
2666 * but silently ignore attempts to change it since we
2667 * don't have state here to verify that it doesn't try
2668 * to change the flag.
2669 */
2670 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2671
2672 /* reject any changes other than AUTHORIZED */
2673 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2674 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002675 break;
2676 case NL80211_IFTYPE_MESH_POINT:
2677 /* disallow things mesh doesn't support */
2678 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002679 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002680 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002681 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002682 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002683 return -EINVAL;
2684 /*
2685 * No special handling for TDLS here -- the userspace
2686 * mesh code doesn't have this bug.
2687 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002688 if (params.sta_flags_mask &
2689 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002690 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002691 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002692 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002693 break;
2694 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002695 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002696 }
2697
Johannes Bergbdd90d52011-12-14 12:20:27 +01002698 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002699
Johannes Berg79c97e92009-07-07 03:56:12 +02002700 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002701
Johannes Berg5727ef12007-12-19 02:03:34 +01002702 if (params.vlan)
2703 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002704
Johannes Berg5727ef12007-12-19 02:03:34 +01002705 return err;
2706}
2707
Eliad Pellerc75786c2011-08-23 14:37:46 +03002708static struct nla_policy
2709nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2710 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2711 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2712};
2713
Johannes Berg5727ef12007-12-19 02:03:34 +01002714static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2715{
Johannes Berg4c476992010-10-04 21:36:35 +02002716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002717 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002718 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002719 struct station_parameters params;
2720 u8 *mac_addr = NULL;
2721
2722 memset(&params, 0, sizeof(params));
2723
2724 if (!info->attrs[NL80211_ATTR_MAC])
2725 return -EINVAL;
2726
Johannes Berg5727ef12007-12-19 02:03:34 +01002727 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2728 return -EINVAL;
2729
2730 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2731 return -EINVAL;
2732
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002733 if (!info->attrs[NL80211_ATTR_STA_AID])
2734 return -EINVAL;
2735
Johannes Berg5727ef12007-12-19 02:03:34 +01002736 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2737 params.supported_rates =
2738 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2739 params.supported_rates_len =
2740 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2741 params.listen_interval =
2742 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002743
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002744 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2745 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2746 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002747
Jouni Malinen36aedc92008-08-25 11:58:58 +03002748 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2749 params.ht_capa =
2750 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002751
Javier Cardona96b78df2011-04-07 15:08:33 -07002752 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2753 params.plink_action =
2754 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2755
Johannes Bergbdd90d52011-12-14 12:20:27 +01002756 if (!rdev->ops->add_station)
2757 return -EOPNOTSUPP;
2758
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002759 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002760 return -EINVAL;
2761
Johannes Bergbdd90d52011-12-14 12:20:27 +01002762 switch (dev->ieee80211_ptr->iftype) {
2763 case NL80211_IFTYPE_AP:
2764 case NL80211_IFTYPE_AP_VLAN:
2765 case NL80211_IFTYPE_P2P_GO:
2766 /* parse WME attributes if sta is WME capable */
2767 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2768 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2769 info->attrs[NL80211_ATTR_STA_WME]) {
2770 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2771 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002772
Johannes Bergbdd90d52011-12-14 12:20:27 +01002773 nla = info->attrs[NL80211_ATTR_STA_WME];
2774 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2775 nl80211_sta_wme_policy);
2776 if (err)
2777 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002778
Johannes Bergbdd90d52011-12-14 12:20:27 +01002779 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2780 params.uapsd_queues =
2781 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2782 if (params.uapsd_queues &
2783 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2784 return -EINVAL;
2785
2786 if (tb[NL80211_STA_WME_MAX_SP])
2787 params.max_sp =
2788 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2789
2790 if (params.max_sp &
2791 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2792 return -EINVAL;
2793
2794 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2795 }
2796 /* TDLS peers cannot be added */
2797 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002798 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002799 /* but don't bother the driver with it */
2800 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002801
Johannes Bergbdd90d52011-12-14 12:20:27 +01002802 /* must be last in here for error handling */
2803 params.vlan = get_vlan(info, rdev);
2804 if (IS_ERR(params.vlan))
2805 return PTR_ERR(params.vlan);
2806 break;
2807 case NL80211_IFTYPE_MESH_POINT:
2808 /* TDLS peers cannot be added */
2809 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002810 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002811 break;
2812 case NL80211_IFTYPE_STATION:
2813 /* Only TDLS peers can be added */
2814 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2815 return -EINVAL;
2816 /* Can only add if TDLS ... */
2817 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2818 return -EOPNOTSUPP;
2819 /* ... with external setup is supported */
2820 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2821 return -EOPNOTSUPP;
2822 break;
2823 default:
2824 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002825 }
2826
Johannes Bergbdd90d52011-12-14 12:20:27 +01002827 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002828
Johannes Berg79c97e92009-07-07 03:56:12 +02002829 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002830
Johannes Berg5727ef12007-12-19 02:03:34 +01002831 if (params.vlan)
2832 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002833 return err;
2834}
2835
2836static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2837{
Johannes Berg4c476992010-10-04 21:36:35 +02002838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2839 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002840 u8 *mac_addr = NULL;
2841
2842 if (info->attrs[NL80211_ATTR_MAC])
2843 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2844
Johannes Berge80cf852009-05-11 14:43:13 +02002845 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002846 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002847 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002848 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2849 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002850
Johannes Berg4c476992010-10-04 21:36:35 +02002851 if (!rdev->ops->del_station)
2852 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002853
Johannes Berg4c476992010-10-04 21:36:35 +02002854 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002855}
2856
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002857static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2858 int flags, struct net_device *dev,
2859 u8 *dst, u8 *next_hop,
2860 struct mpath_info *pinfo)
2861{
2862 void *hdr;
2863 struct nlattr *pinfoattr;
2864
2865 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2866 if (!hdr)
2867 return -1;
2868
2869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2870 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2871 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2872
Johannes Bergf5ea9122009-08-07 16:17:38 +02002873 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2874
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002875 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2876 if (!pinfoattr)
2877 goto nla_put_failure;
2878 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2879 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2880 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002881 if (pinfo->filled & MPATH_INFO_SN)
2882 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2883 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002884 if (pinfo->filled & MPATH_INFO_METRIC)
2885 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2886 pinfo->metric);
2887 if (pinfo->filled & MPATH_INFO_EXPTIME)
2888 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2889 pinfo->exptime);
2890 if (pinfo->filled & MPATH_INFO_FLAGS)
2891 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2892 pinfo->flags);
2893 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2894 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2895 pinfo->discovery_timeout);
2896 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2897 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2898 pinfo->discovery_retries);
2899
2900 nla_nest_end(msg, pinfoattr);
2901
2902 return genlmsg_end(msg, hdr);
2903
2904 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002905 genlmsg_cancel(msg, hdr);
2906 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002907}
2908
2909static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002910 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002911{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002912 struct mpath_info pinfo;
2913 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002914 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002915 u8 dst[ETH_ALEN];
2916 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002917 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002918 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002919
Johannes Berg67748892010-10-04 21:14:06 +02002920 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2921 if (err)
2922 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002923
2924 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002925 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002926 goto out_err;
2927 }
2928
Jouni Malineneec60b02009-03-20 21:21:19 +02002929 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2930 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002931 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002932 }
2933
Johannes Bergbba95fe2008-07-29 13:22:51 +02002934 while (1) {
2935 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2936 dst, next_hop, &pinfo);
2937 if (err == -ENOENT)
2938 break;
2939 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002940 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002941
2942 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2943 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2944 netdev, dst, next_hop,
2945 &pinfo) < 0)
2946 goto out;
2947
2948 path_idx++;
2949 }
2950
2951
2952 out:
2953 cb->args[1] = path_idx;
2954 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002955 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002956 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002957 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002958}
2959
2960static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2961{
Johannes Berg4c476992010-10-04 21:36:35 +02002962 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002963 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002964 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002965 struct mpath_info pinfo;
2966 struct sk_buff *msg;
2967 u8 *dst = NULL;
2968 u8 next_hop[ETH_ALEN];
2969
2970 memset(&pinfo, 0, sizeof(pinfo));
2971
2972 if (!info->attrs[NL80211_ATTR_MAC])
2973 return -EINVAL;
2974
2975 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2976
Johannes Berg4c476992010-10-04 21:36:35 +02002977 if (!rdev->ops->get_mpath)
2978 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002979
Johannes Berg4c476992010-10-04 21:36:35 +02002980 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2981 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002982
Johannes Berg79c97e92009-07-07 03:56:12 +02002983 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002984 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002985 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002986
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002987 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002988 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002989 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002990
2991 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002992 dev, dst, next_hop, &pinfo) < 0) {
2993 nlmsg_free(msg);
2994 return -ENOBUFS;
2995 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002996
Johannes Berg4c476992010-10-04 21:36:35 +02002997 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002998}
2999
3000static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3001{
Johannes Berg4c476992010-10-04 21:36:35 +02003002 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3003 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003004 u8 *dst = NULL;
3005 u8 *next_hop = NULL;
3006
3007 if (!info->attrs[NL80211_ATTR_MAC])
3008 return -EINVAL;
3009
3010 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3011 return -EINVAL;
3012
3013 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3014 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3015
Johannes Berg4c476992010-10-04 21:36:35 +02003016 if (!rdev->ops->change_mpath)
3017 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003018
Johannes Berg4c476992010-10-04 21:36:35 +02003019 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3020 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003021
Johannes Berg4c476992010-10-04 21:36:35 +02003022 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003023}
Johannes Berg4c476992010-10-04 21:36:35 +02003024
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003025static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3026{
Johannes Berg4c476992010-10-04 21:36:35 +02003027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3028 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003029 u8 *dst = NULL;
3030 u8 *next_hop = NULL;
3031
3032 if (!info->attrs[NL80211_ATTR_MAC])
3033 return -EINVAL;
3034
3035 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3036 return -EINVAL;
3037
3038 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3039 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3040
Johannes Berg4c476992010-10-04 21:36:35 +02003041 if (!rdev->ops->add_mpath)
3042 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003043
Johannes Berg4c476992010-10-04 21:36:35 +02003044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3045 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003046
Johannes Berg4c476992010-10-04 21:36:35 +02003047 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003048}
3049
3050static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3051{
Johannes Berg4c476992010-10-04 21:36:35 +02003052 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3053 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003054 u8 *dst = NULL;
3055
3056 if (info->attrs[NL80211_ATTR_MAC])
3057 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3058
Johannes Berg4c476992010-10-04 21:36:35 +02003059 if (!rdev->ops->del_mpath)
3060 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003061
Johannes Berg4c476992010-10-04 21:36:35 +02003062 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003063}
3064
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003065static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3066{
Johannes Berg4c476992010-10-04 21:36:35 +02003067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3068 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003069 struct bss_parameters params;
3070
3071 memset(&params, 0, sizeof(params));
3072 /* default to not changing parameters */
3073 params.use_cts_prot = -1;
3074 params.use_short_preamble = -1;
3075 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003076 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003077 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003078
3079 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3080 params.use_cts_prot =
3081 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3082 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3083 params.use_short_preamble =
3084 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3085 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3086 params.use_short_slot_time =
3087 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003088 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3089 params.basic_rates =
3090 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3091 params.basic_rates_len =
3092 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3093 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003094 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3095 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003096 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3097 params.ht_opmode =
3098 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003099
Johannes Berg4c476992010-10-04 21:36:35 +02003100 if (!rdev->ops->change_bss)
3101 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003102
Johannes Berg074ac8d2010-09-16 14:58:22 +02003103 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003104 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3105 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003106
Johannes Berg4c476992010-10-04 21:36:35 +02003107 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003108}
3109
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003110static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003111 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3112 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3113 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3114 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3115 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3116 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3117};
3118
3119static int parse_reg_rule(struct nlattr *tb[],
3120 struct ieee80211_reg_rule *reg_rule)
3121{
3122 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3123 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3124
3125 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3126 return -EINVAL;
3127 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3128 return -EINVAL;
3129 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3130 return -EINVAL;
3131 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3132 return -EINVAL;
3133 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3134 return -EINVAL;
3135
3136 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3137
3138 freq_range->start_freq_khz =
3139 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3140 freq_range->end_freq_khz =
3141 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3142 freq_range->max_bandwidth_khz =
3143 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3144
3145 power_rule->max_eirp =
3146 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3147
3148 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3149 power_rule->max_antenna_gain =
3150 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3151
3152 return 0;
3153}
3154
3155static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3156{
3157 int r;
3158 char *data = NULL;
3159
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003160 /*
3161 * You should only get this when cfg80211 hasn't yet initialized
3162 * completely when built-in to the kernel right between the time
3163 * window between nl80211_init() and regulatory_init(), if that is
3164 * even possible.
3165 */
3166 mutex_lock(&cfg80211_mutex);
3167 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003168 mutex_unlock(&cfg80211_mutex);
3169 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003170 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003171 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003172
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003173 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3174 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003175
3176 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3177
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003178 r = regulatory_hint_user(data);
3179
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003180 return r;
3181}
3182
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003183static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003184 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003185{
Johannes Berg4c476992010-10-04 21:36:35 +02003186 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003187 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003188 struct wireless_dev *wdev = dev->ieee80211_ptr;
3189 struct mesh_config cur_params;
3190 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003191 void *hdr;
3192 struct nlattr *pinfoattr;
3193 struct sk_buff *msg;
3194
Johannes Berg29cbe682010-12-03 09:20:44 +01003195 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3196 return -EOPNOTSUPP;
3197
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003198 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003199 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003200
Johannes Berg29cbe682010-12-03 09:20:44 +01003201 wdev_lock(wdev);
3202 /* If not connected, get default parameters */
3203 if (!wdev->mesh_id_len)
3204 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3205 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003206 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003207 &cur_params);
3208 wdev_unlock(wdev);
3209
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003210 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003211 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003212
3213 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003214 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003215 if (!msg)
3216 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003217 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003218 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003219 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003220 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003221 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003222 if (!pinfoattr)
3223 goto nla_put_failure;
3224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3225 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3226 cur_params.dot11MeshRetryTimeout);
3227 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3228 cur_params.dot11MeshConfirmTimeout);
3229 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3230 cur_params.dot11MeshHoldingTimeout);
3231 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3232 cur_params.dot11MeshMaxPeerLinks);
3233 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3234 cur_params.dot11MeshMaxRetries);
3235 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3236 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003237 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3238 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003239 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3240 cur_params.auto_open_plinks);
3241 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3242 cur_params.dot11MeshHWMPmaxPREQretries);
3243 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3244 cur_params.path_refresh_time);
3245 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3246 cur_params.min_discovery_timeout);
3247 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3248 cur_params.dot11MeshHWMPactivePathTimeout);
3249 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3250 cur_params.dot11MeshHWMPpreqMinInterval);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003251 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3252 cur_params.dot11MeshHWMPperrMinInterval);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003253 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3254 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003255 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3256 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003257 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3258 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003259 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3260 cur_params.dot11MeshGateAnnouncementProtocol);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003261 NLA_PUT_U8(msg, NL80211_MESHCONF_FORWARDING,
3262 cur_params.dot11MeshForwarding);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003263 nla_nest_end(msg, pinfoattr);
3264 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003265 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003266
Johannes Berg3b858752009-03-12 09:55:09 +01003267 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003268 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003269 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003270 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003271 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003272}
3273
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003274static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003275 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3276 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3277 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3278 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3279 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3280 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003281 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003282 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3283
3284 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3285 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3286 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3287 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3288 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003289 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003290 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003291 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003292 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003293 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003294 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003295};
3296
Javier Cardonac80d5452010-12-16 17:37:49 -08003297static const struct nla_policy
3298 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3299 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3300 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003301 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003302 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003303 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003304 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003305};
3306
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003307static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003308 struct mesh_config *cfg,
3309 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003310{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003311 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003312 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003313
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003314#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3315do {\
3316 if (table[attr_num]) {\
3317 cfg->param = nla_fn(table[attr_num]); \
3318 mask |= (1 << (attr_num - 1)); \
3319 } \
3320} while (0);\
3321
3322
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003323 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003324 return -EINVAL;
3325 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003326 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003327 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003328 return -EINVAL;
3329
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003330 /* This makes sure that there aren't more than 32 mesh config
3331 * parameters (otherwise our bitfield scheme would not work.) */
3332 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3333
3334 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003335 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3336 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3337 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3338 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3339 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3340 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3341 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3342 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3343 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3344 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3345 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3346 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003347 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3348 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003349 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3350 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3351 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3352 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3353 nla_get_u8);
3354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3355 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3356 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3357 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3358 nla_get_u16);
3359 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3360 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3361 nla_get_u32);
3362 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3363 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3364 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003365 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3366 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3367 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003368 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3369 dot11MeshHWMPnetDiameterTraversalTime,
3370 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3371 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003372 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3373 dot11MeshHWMPRootMode, mask,
3374 NL80211_MESHCONF_HWMP_ROOTMODE,
3375 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003376 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3377 dot11MeshHWMPRannInterval, mask,
3378 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3379 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003380 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3381 dot11MeshGateAnnouncementProtocol, mask,
3382 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3383 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003384 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3385 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003386 if (mask_out)
3387 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003388
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003389 return 0;
3390
3391#undef FILL_IN_MESH_PARAM_IF_SET
3392}
3393
Javier Cardonac80d5452010-12-16 17:37:49 -08003394static int nl80211_parse_mesh_setup(struct genl_info *info,
3395 struct mesh_setup *setup)
3396{
3397 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3398
3399 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3400 return -EINVAL;
3401 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3402 info->attrs[NL80211_ATTR_MESH_SETUP],
3403 nl80211_mesh_setup_params_policy))
3404 return -EINVAL;
3405
3406 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3407 setup->path_sel_proto =
3408 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3409 IEEE80211_PATH_PROTOCOL_VENDOR :
3410 IEEE80211_PATH_PROTOCOL_HWMP;
3411
3412 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3413 setup->path_metric =
3414 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3415 IEEE80211_PATH_METRIC_VENDOR :
3416 IEEE80211_PATH_METRIC_AIRTIME;
3417
Javier Cardona581a8b02011-04-07 15:08:27 -07003418
3419 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003420 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003421 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003422 if (!is_valid_ie_attr(ieattr))
3423 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003424 setup->ie = nla_data(ieattr);
3425 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003426 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003427 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3428 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003429
3430 return 0;
3431}
3432
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003433static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003434 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003435{
3436 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3437 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003438 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003439 struct mesh_config cfg;
3440 u32 mask;
3441 int err;
3442
Johannes Berg29cbe682010-12-03 09:20:44 +01003443 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3444 return -EOPNOTSUPP;
3445
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003446 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003447 return -EOPNOTSUPP;
3448
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003449 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003450 if (err)
3451 return err;
3452
Johannes Berg29cbe682010-12-03 09:20:44 +01003453 wdev_lock(wdev);
3454 if (!wdev->mesh_id_len)
3455 err = -ENOLINK;
3456
3457 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003458 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003459 mask, &cfg);
3460
3461 wdev_unlock(wdev);
3462
3463 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003464}
3465
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003466static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3467{
3468 struct sk_buff *msg;
3469 void *hdr = NULL;
3470 struct nlattr *nl_reg_rules;
3471 unsigned int i;
3472 int err = -EINVAL;
3473
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003474 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003475
3476 if (!cfg80211_regdomain)
3477 goto out;
3478
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003479 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003480 if (!msg) {
3481 err = -ENOBUFS;
3482 goto out;
3483 }
3484
3485 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3486 NL80211_CMD_GET_REG);
3487 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003488 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003489
3490 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3491 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003492 if (cfg80211_regdomain->dfs_region)
3493 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3494 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003495
3496 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3497 if (!nl_reg_rules)
3498 goto nla_put_failure;
3499
3500 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3501 struct nlattr *nl_reg_rule;
3502 const struct ieee80211_reg_rule *reg_rule;
3503 const struct ieee80211_freq_range *freq_range;
3504 const struct ieee80211_power_rule *power_rule;
3505
3506 reg_rule = &cfg80211_regdomain->reg_rules[i];
3507 freq_range = &reg_rule->freq_range;
3508 power_rule = &reg_rule->power_rule;
3509
3510 nl_reg_rule = nla_nest_start(msg, i);
3511 if (!nl_reg_rule)
3512 goto nla_put_failure;
3513
3514 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3515 reg_rule->flags);
3516 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3517 freq_range->start_freq_khz);
3518 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3519 freq_range->end_freq_khz);
3520 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3521 freq_range->max_bandwidth_khz);
3522 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3523 power_rule->max_antenna_gain);
3524 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3525 power_rule->max_eirp);
3526
3527 nla_nest_end(msg, nl_reg_rule);
3528 }
3529
3530 nla_nest_end(msg, nl_reg_rules);
3531
3532 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003533 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003534 goto out;
3535
3536nla_put_failure:
3537 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003538put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003539 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003540 err = -EMSGSIZE;
3541out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003542 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003543 return err;
3544}
3545
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003546static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3547{
3548 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3549 struct nlattr *nl_reg_rule;
3550 char *alpha2 = NULL;
3551 int rem_reg_rules = 0, r = 0;
3552 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003553 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003554 struct ieee80211_regdomain *rd = NULL;
3555
3556 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3557 return -EINVAL;
3558
3559 if (!info->attrs[NL80211_ATTR_REG_RULES])
3560 return -EINVAL;
3561
3562 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3563
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003564 if (info->attrs[NL80211_ATTR_DFS_REGION])
3565 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3566
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003567 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3568 rem_reg_rules) {
3569 num_rules++;
3570 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003571 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003572 }
3573
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003574 mutex_lock(&cfg80211_mutex);
3575
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003576 if (!reg_is_valid_request(alpha2)) {
3577 r = -EINVAL;
3578 goto bad_reg;
3579 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003580
3581 size_of_regd = sizeof(struct ieee80211_regdomain) +
3582 (num_rules * sizeof(struct ieee80211_reg_rule));
3583
3584 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003585 if (!rd) {
3586 r = -ENOMEM;
3587 goto bad_reg;
3588 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003589
3590 rd->n_reg_rules = num_rules;
3591 rd->alpha2[0] = alpha2[0];
3592 rd->alpha2[1] = alpha2[1];
3593
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003594 /*
3595 * Disable DFS master mode if the DFS region was
3596 * not supported or known on this kernel.
3597 */
3598 if (reg_supported_dfs_region(dfs_region))
3599 rd->dfs_region = dfs_region;
3600
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003601 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3602 rem_reg_rules) {
3603 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3604 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3605 reg_rule_policy);
3606 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3607 if (r)
3608 goto bad_reg;
3609
3610 rule_idx++;
3611
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003612 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3613 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003614 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003615 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003616 }
3617
3618 BUG_ON(rule_idx != num_rules);
3619
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003620 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003621
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003622 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003623
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003624 return r;
3625
Johannes Bergd2372b32008-10-24 20:32:20 +02003626 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003627 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003628 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003629 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003630}
3631
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003632static int validate_scan_freqs(struct nlattr *freqs)
3633{
3634 struct nlattr *attr1, *attr2;
3635 int n_channels = 0, tmp1, tmp2;
3636
3637 nla_for_each_nested(attr1, freqs, tmp1) {
3638 n_channels++;
3639 /*
3640 * Some hardware has a limited channel list for
3641 * scanning, and it is pretty much nonsensical
3642 * to scan for a channel twice, so disallow that
3643 * and don't require drivers to check that the
3644 * channel list they get isn't longer than what
3645 * they can scan, as long as they can scan all
3646 * the channels they registered at once.
3647 */
3648 nla_for_each_nested(attr2, freqs, tmp2)
3649 if (attr1 != attr2 &&
3650 nla_get_u32(attr1) == nla_get_u32(attr2))
3651 return 0;
3652 }
3653
3654 return n_channels;
3655}
3656
Johannes Berg2a519312009-02-10 21:25:55 +01003657static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3658{
Johannes Berg4c476992010-10-04 21:36:35 +02003659 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3660 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003661 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003662 struct nlattr *attr;
3663 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003664 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003665 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003666
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003667 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3668 return -EINVAL;
3669
Johannes Berg79c97e92009-07-07 03:56:12 +02003670 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003671
Johannes Berg4c476992010-10-04 21:36:35 +02003672 if (!rdev->ops->scan)
3673 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003674
Johannes Berg4c476992010-10-04 21:36:35 +02003675 if (rdev->scan_req)
3676 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003677
3678 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003679 n_channels = validate_scan_freqs(
3680 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003681 if (!n_channels)
3682 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003683 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003684 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003685 n_channels = 0;
3686
Johannes Berg2a519312009-02-10 21:25:55 +01003687 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3688 if (wiphy->bands[band])
3689 n_channels += wiphy->bands[band]->n_channels;
3690 }
3691
3692 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3693 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3694 n_ssids++;
3695
Johannes Berg4c476992010-10-04 21:36:35 +02003696 if (n_ssids > wiphy->max_scan_ssids)
3697 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003698
Jouni Malinen70692ad2009-02-16 19:39:13 +02003699 if (info->attrs[NL80211_ATTR_IE])
3700 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3701 else
3702 ie_len = 0;
3703
Johannes Berg4c476992010-10-04 21:36:35 +02003704 if (ie_len > wiphy->max_scan_ie_len)
3705 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003706
Johannes Berg2a519312009-02-10 21:25:55 +01003707 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003708 + sizeof(*request->ssids) * n_ssids
3709 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003710 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003711 if (!request)
3712 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003713
Johannes Berg2a519312009-02-10 21:25:55 +01003714 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003715 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003716 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003717 if (ie_len) {
3718 if (request->ssids)
3719 request->ie = (void *)(request->ssids + n_ssids);
3720 else
3721 request->ie = (void *)(request->channels + n_channels);
3722 }
Johannes Berg2a519312009-02-10 21:25:55 +01003723
Johannes Berg584991d2009-11-02 13:32:03 +01003724 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003725 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3726 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003727 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003728 struct ieee80211_channel *chan;
3729
3730 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3731
3732 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003733 err = -EINVAL;
3734 goto out_free;
3735 }
Johannes Berg584991d2009-11-02 13:32:03 +01003736
3737 /* ignore disabled channels */
3738 if (chan->flags & IEEE80211_CHAN_DISABLED)
3739 continue;
3740
3741 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003742 i++;
3743 }
3744 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003745 enum ieee80211_band band;
3746
Johannes Berg2a519312009-02-10 21:25:55 +01003747 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003748 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3749 int j;
3750 if (!wiphy->bands[band])
3751 continue;
3752 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003753 struct ieee80211_channel *chan;
3754
3755 chan = &wiphy->bands[band]->channels[j];
3756
3757 if (chan->flags & IEEE80211_CHAN_DISABLED)
3758 continue;
3759
3760 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003761 i++;
3762 }
3763 }
3764 }
3765
Johannes Berg584991d2009-11-02 13:32:03 +01003766 if (!i) {
3767 err = -EINVAL;
3768 goto out_free;
3769 }
3770
3771 request->n_channels = i;
3772
Johannes Berg2a519312009-02-10 21:25:55 +01003773 i = 0;
3774 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3775 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003776 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003777 err = -EINVAL;
3778 goto out_free;
3779 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003780 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003781 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003782 i++;
3783 }
3784 }
3785
Jouni Malinen70692ad2009-02-16 19:39:13 +02003786 if (info->attrs[NL80211_ATTR_IE]) {
3787 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003788 memcpy((void *)request->ie,
3789 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003790 request->ie_len);
3791 }
3792
Johannes Berg34850ab2011-07-18 18:08:35 +02003793 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003794 if (wiphy->bands[i])
3795 request->rates[i] =
3796 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003797
3798 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3799 nla_for_each_nested(attr,
3800 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3801 tmp) {
3802 enum ieee80211_band band = nla_type(attr);
3803
Dan Carpenter84404622011-07-29 11:52:18 +03003804 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003805 err = -EINVAL;
3806 goto out_free;
3807 }
3808 err = ieee80211_get_ratemask(wiphy->bands[band],
3809 nla_data(attr),
3810 nla_len(attr),
3811 &request->rates[band]);
3812 if (err)
3813 goto out_free;
3814 }
3815 }
3816
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303817 request->no_cck =
3818 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3819
Johannes Berg463d0182009-07-14 00:33:35 +02003820 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003821 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003822
Johannes Berg79c97e92009-07-07 03:56:12 +02003823 rdev->scan_req = request;
3824 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003825
Johannes Berg463d0182009-07-14 00:33:35 +02003826 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003827 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003828 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003829 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003830 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003831 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003832 kfree(request);
3833 }
Johannes Berg3b858752009-03-12 09:55:09 +01003834
Johannes Berg2a519312009-02-10 21:25:55 +01003835 return err;
3836}
3837
Luciano Coelho807f8a82011-05-11 17:09:35 +03003838static int nl80211_start_sched_scan(struct sk_buff *skb,
3839 struct genl_info *info)
3840{
3841 struct cfg80211_sched_scan_request *request;
3842 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3843 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003844 struct nlattr *attr;
3845 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003846 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003847 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003848 enum ieee80211_band band;
3849 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003850 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003851
3852 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3853 !rdev->ops->sched_scan_start)
3854 return -EOPNOTSUPP;
3855
3856 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3857 return -EINVAL;
3858
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003859 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3860 return -EINVAL;
3861
3862 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3863 if (interval == 0)
3864 return -EINVAL;
3865
Luciano Coelho807f8a82011-05-11 17:09:35 +03003866 wiphy = &rdev->wiphy;
3867
3868 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3869 n_channels = validate_scan_freqs(
3870 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3871 if (!n_channels)
3872 return -EINVAL;
3873 } else {
3874 n_channels = 0;
3875
3876 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3877 if (wiphy->bands[band])
3878 n_channels += wiphy->bands[band]->n_channels;
3879 }
3880
3881 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3882 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3883 tmp)
3884 n_ssids++;
3885
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003886 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003887 return -EINVAL;
3888
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003889 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3890 nla_for_each_nested(attr,
3891 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3892 tmp)
3893 n_match_sets++;
3894
3895 if (n_match_sets > wiphy->max_match_sets)
3896 return -EINVAL;
3897
Luciano Coelho807f8a82011-05-11 17:09:35 +03003898 if (info->attrs[NL80211_ATTR_IE])
3899 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3900 else
3901 ie_len = 0;
3902
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003903 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003904 return -EINVAL;
3905
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003906 mutex_lock(&rdev->sched_scan_mtx);
3907
3908 if (rdev->sched_scan_req) {
3909 err = -EINPROGRESS;
3910 goto out;
3911 }
3912
Luciano Coelho807f8a82011-05-11 17:09:35 +03003913 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003914 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003915 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003916 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003917 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003918 if (!request) {
3919 err = -ENOMEM;
3920 goto out;
3921 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003922
3923 if (n_ssids)
3924 request->ssids = (void *)&request->channels[n_channels];
3925 request->n_ssids = n_ssids;
3926 if (ie_len) {
3927 if (request->ssids)
3928 request->ie = (void *)(request->ssids + n_ssids);
3929 else
3930 request->ie = (void *)(request->channels + n_channels);
3931 }
3932
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003933 if (n_match_sets) {
3934 if (request->ie)
3935 request->match_sets = (void *)(request->ie + ie_len);
3936 else if (request->ssids)
3937 request->match_sets =
3938 (void *)(request->ssids + n_ssids);
3939 else
3940 request->match_sets =
3941 (void *)(request->channels + n_channels);
3942 }
3943 request->n_match_sets = n_match_sets;
3944
Luciano Coelho807f8a82011-05-11 17:09:35 +03003945 i = 0;
3946 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3947 /* user specified, bail out if channel not found */
3948 nla_for_each_nested(attr,
3949 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3950 tmp) {
3951 struct ieee80211_channel *chan;
3952
3953 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3954
3955 if (!chan) {
3956 err = -EINVAL;
3957 goto out_free;
3958 }
3959
3960 /* ignore disabled channels */
3961 if (chan->flags & IEEE80211_CHAN_DISABLED)
3962 continue;
3963
3964 request->channels[i] = chan;
3965 i++;
3966 }
3967 } else {
3968 /* all channels */
3969 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3970 int j;
3971 if (!wiphy->bands[band])
3972 continue;
3973 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3974 struct ieee80211_channel *chan;
3975
3976 chan = &wiphy->bands[band]->channels[j];
3977
3978 if (chan->flags & IEEE80211_CHAN_DISABLED)
3979 continue;
3980
3981 request->channels[i] = chan;
3982 i++;
3983 }
3984 }
3985 }
3986
3987 if (!i) {
3988 err = -EINVAL;
3989 goto out_free;
3990 }
3991
3992 request->n_channels = i;
3993
3994 i = 0;
3995 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3996 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3997 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003998 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003999 err = -EINVAL;
4000 goto out_free;
4001 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004002 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004003 memcpy(request->ssids[i].ssid, nla_data(attr),
4004 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004005 i++;
4006 }
4007 }
4008
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004009 i = 0;
4010 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4011 nla_for_each_nested(attr,
4012 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4013 tmp) {
4014 struct nlattr *ssid;
4015
4016 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4017 nla_data(attr), nla_len(attr),
4018 nl80211_match_policy);
4019 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4020 if (ssid) {
4021 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4022 err = -EINVAL;
4023 goto out_free;
4024 }
4025 memcpy(request->match_sets[i].ssid.ssid,
4026 nla_data(ssid), nla_len(ssid));
4027 request->match_sets[i].ssid.ssid_len =
4028 nla_len(ssid);
4029 }
4030 i++;
4031 }
4032 }
4033
Luciano Coelho807f8a82011-05-11 17:09:35 +03004034 if (info->attrs[NL80211_ATTR_IE]) {
4035 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4036 memcpy((void *)request->ie,
4037 nla_data(info->attrs[NL80211_ATTR_IE]),
4038 request->ie_len);
4039 }
4040
4041 request->dev = dev;
4042 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004043 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004044
4045 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4046 if (!err) {
4047 rdev->sched_scan_req = request;
4048 nl80211_send_sched_scan(rdev, dev,
4049 NL80211_CMD_START_SCHED_SCAN);
4050 goto out;
4051 }
4052
4053out_free:
4054 kfree(request);
4055out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004056 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004057 return err;
4058}
4059
4060static int nl80211_stop_sched_scan(struct sk_buff *skb,
4061 struct genl_info *info)
4062{
4063 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004064 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004065
4066 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4067 !rdev->ops->sched_scan_stop)
4068 return -EOPNOTSUPP;
4069
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004070 mutex_lock(&rdev->sched_scan_mtx);
4071 err = __cfg80211_stop_sched_scan(rdev, false);
4072 mutex_unlock(&rdev->sched_scan_mtx);
4073
4074 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004075}
4076
Johannes Berg9720bb32011-06-21 09:45:33 +02004077static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4078 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004079 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004080 struct wireless_dev *wdev,
4081 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004082{
Johannes Berg48ab9052009-07-10 18:42:31 +02004083 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004084 void *hdr;
4085 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004086
4087 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004088
Johannes Berg9720bb32011-06-21 09:45:33 +02004089 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004090 NL80211_CMD_NEW_SCAN_RESULTS);
4091 if (!hdr)
4092 return -1;
4093
Johannes Berg9720bb32011-06-21 09:45:33 +02004094 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4095
Johannes Bergf5ea9122009-08-07 16:17:38 +02004096 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02004097 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01004098
4099 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4100 if (!bss)
4101 goto nla_put_failure;
4102 if (!is_zero_ether_addr(res->bssid))
4103 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4104 if (res->information_elements && res->len_information_elements)
4105 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4106 res->len_information_elements,
4107 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004108 if (res->beacon_ies && res->len_beacon_ies &&
4109 res->beacon_ies != res->information_elements)
4110 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4111 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004112 if (res->tsf)
4113 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4114 if (res->beacon_interval)
4115 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4116 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4117 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004118 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4119 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004120
Johannes Berg77965c92009-02-18 18:45:06 +01004121 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004122 case CFG80211_SIGNAL_TYPE_MBM:
4123 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4124 break;
4125 case CFG80211_SIGNAL_TYPE_UNSPEC:
4126 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4127 break;
4128 default:
4129 break;
4130 }
4131
Johannes Berg48ab9052009-07-10 18:42:31 +02004132 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004133 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004134 case NL80211_IFTYPE_STATION:
4135 if (intbss == wdev->current_bss)
4136 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4137 NL80211_BSS_STATUS_ASSOCIATED);
Johannes Berg48ab9052009-07-10 18:42:31 +02004138 break;
4139 case NL80211_IFTYPE_ADHOC:
4140 if (intbss == wdev->current_bss)
4141 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4142 NL80211_BSS_STATUS_IBSS_JOINED);
4143 break;
4144 default:
4145 break;
4146 }
4147
Johannes Berg2a519312009-02-10 21:25:55 +01004148 nla_nest_end(msg, bss);
4149
4150 return genlmsg_end(msg, hdr);
4151
4152 nla_put_failure:
4153 genlmsg_cancel(msg, hdr);
4154 return -EMSGSIZE;
4155}
4156
4157static int nl80211_dump_scan(struct sk_buff *skb,
4158 struct netlink_callback *cb)
4159{
Johannes Berg48ab9052009-07-10 18:42:31 +02004160 struct cfg80211_registered_device *rdev;
4161 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004162 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004163 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004164 int start = cb->args[1], idx = 0;
4165 int err;
4166
Johannes Berg67748892010-10-04 21:14:06 +02004167 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4168 if (err)
4169 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004170
Johannes Berg48ab9052009-07-10 18:42:31 +02004171 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004172
Johannes Berg48ab9052009-07-10 18:42:31 +02004173 wdev_lock(wdev);
4174 spin_lock_bh(&rdev->bss_lock);
4175 cfg80211_bss_expire(rdev);
4176
Johannes Berg9720bb32011-06-21 09:45:33 +02004177 cb->seq = rdev->bss_generation;
4178
Johannes Berg48ab9052009-07-10 18:42:31 +02004179 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004180 if (++idx <= start)
4181 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004182 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004183 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004184 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004185 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004186 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004187 }
4188 }
4189
Johannes Berg48ab9052009-07-10 18:42:31 +02004190 spin_unlock_bh(&rdev->bss_lock);
4191 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004192
4193 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004194 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004195
Johannes Berg67748892010-10-04 21:14:06 +02004196 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004197}
4198
Holger Schurig61fa7132009-11-11 12:25:40 +01004199static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4200 int flags, struct net_device *dev,
4201 struct survey_info *survey)
4202{
4203 void *hdr;
4204 struct nlattr *infoattr;
4205
Holger Schurig61fa7132009-11-11 12:25:40 +01004206 hdr = nl80211hdr_put(msg, pid, seq, flags,
4207 NL80211_CMD_NEW_SURVEY_RESULTS);
4208 if (!hdr)
4209 return -ENOMEM;
4210
4211 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4212
4213 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4214 if (!infoattr)
4215 goto nla_put_failure;
4216
4217 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4218 survey->channel->center_freq);
4219 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4220 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4221 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004222 if (survey->filled & SURVEY_INFO_IN_USE)
4223 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004224 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4225 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4226 survey->channel_time);
4227 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4228 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4229 survey->channel_time_busy);
4230 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4231 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4232 survey->channel_time_ext_busy);
4233 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4234 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4235 survey->channel_time_rx);
4236 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4237 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4238 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004239
4240 nla_nest_end(msg, infoattr);
4241
4242 return genlmsg_end(msg, hdr);
4243
4244 nla_put_failure:
4245 genlmsg_cancel(msg, hdr);
4246 return -EMSGSIZE;
4247}
4248
4249static int nl80211_dump_survey(struct sk_buff *skb,
4250 struct netlink_callback *cb)
4251{
4252 struct survey_info survey;
4253 struct cfg80211_registered_device *dev;
4254 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004255 int survey_idx = cb->args[1];
4256 int res;
4257
Johannes Berg67748892010-10-04 21:14:06 +02004258 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4259 if (res)
4260 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004261
4262 if (!dev->ops->dump_survey) {
4263 res = -EOPNOTSUPP;
4264 goto out_err;
4265 }
4266
4267 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004268 struct ieee80211_channel *chan;
4269
Holger Schurig61fa7132009-11-11 12:25:40 +01004270 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4271 &survey);
4272 if (res == -ENOENT)
4273 break;
4274 if (res)
4275 goto out_err;
4276
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004277 /* Survey without a channel doesn't make sense */
4278 if (!survey.channel) {
4279 res = -EINVAL;
4280 goto out;
4281 }
4282
4283 chan = ieee80211_get_channel(&dev->wiphy,
4284 survey.channel->center_freq);
4285 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4286 survey_idx++;
4287 continue;
4288 }
4289
Holger Schurig61fa7132009-11-11 12:25:40 +01004290 if (nl80211_send_survey(skb,
4291 NETLINK_CB(cb->skb).pid,
4292 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4293 netdev,
4294 &survey) < 0)
4295 goto out;
4296 survey_idx++;
4297 }
4298
4299 out:
4300 cb->args[1] = survey_idx;
4301 res = skb->len;
4302 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004303 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004304 return res;
4305}
4306
Jouni Malinen255e7372009-03-20 21:21:17 +02004307static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4308{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004309 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004310}
4311
Samuel Ortizb23aa672009-07-01 21:26:54 +02004312static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4313{
4314 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4315 NL80211_WPA_VERSION_2));
4316}
4317
Jouni Malinen636a5d32009-03-19 13:39:22 +02004318static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4319{
Johannes Berg4c476992010-10-04 21:36:35 +02004320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4321 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004322 struct ieee80211_channel *chan;
4323 const u8 *bssid, *ssid, *ie = NULL;
4324 int err, ssid_len, ie_len = 0;
4325 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004326 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004327 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004328
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004329 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4330 return -EINVAL;
4331
4332 if (!info->attrs[NL80211_ATTR_MAC])
4333 return -EINVAL;
4334
Jouni Malinen17780922009-03-27 20:52:47 +02004335 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4336 return -EINVAL;
4337
Johannes Berg19957bb2009-07-02 17:20:43 +02004338 if (!info->attrs[NL80211_ATTR_SSID])
4339 return -EINVAL;
4340
4341 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4342 return -EINVAL;
4343
Johannes Bergfffd0932009-07-08 14:22:54 +02004344 err = nl80211_parse_key(info, &key);
4345 if (err)
4346 return err;
4347
4348 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004349 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4350 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004351 if (!key.p.key || !key.p.key_len)
4352 return -EINVAL;
4353 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4354 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4355 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4356 key.p.key_len != WLAN_KEY_LEN_WEP104))
4357 return -EINVAL;
4358 if (key.idx > 4)
4359 return -EINVAL;
4360 } else {
4361 key.p.key_len = 0;
4362 key.p.key = NULL;
4363 }
4364
Johannes Bergafea0b72010-08-10 09:46:42 +02004365 if (key.idx >= 0) {
4366 int i;
4367 bool ok = false;
4368 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4369 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4370 ok = true;
4371 break;
4372 }
4373 }
Johannes Berg4c476992010-10-04 21:36:35 +02004374 if (!ok)
4375 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004376 }
4377
Johannes Berg4c476992010-10-04 21:36:35 +02004378 if (!rdev->ops->auth)
4379 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004380
Johannes Berg074ac8d2010-09-16 14:58:22 +02004381 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004382 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4383 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004384
Johannes Berg19957bb2009-07-02 17:20:43 +02004385 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004386 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004387 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004388 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4389 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004390
Johannes Berg19957bb2009-07-02 17:20:43 +02004391 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4392 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4393
4394 if (info->attrs[NL80211_ATTR_IE]) {
4395 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4396 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4397 }
4398
4399 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004400 if (!nl80211_valid_auth_type(auth_type))
4401 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004402
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004403 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4404
Johannes Berg95de8172012-01-20 13:55:25 +01004405 /*
4406 * Since we no longer track auth state, ignore
4407 * requests to only change local state.
4408 */
4409 if (local_state_change)
4410 return 0;
4411
Johannes Berg4c476992010-10-04 21:36:35 +02004412 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4413 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004414 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004415}
4416
Johannes Bergc0692b82010-08-27 14:26:53 +03004417static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4418 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004419 struct cfg80211_crypto_settings *settings,
4420 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004421{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004422 memset(settings, 0, sizeof(*settings));
4423
Samuel Ortizb23aa672009-07-01 21:26:54 +02004424 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4425
Johannes Bergc0692b82010-08-27 14:26:53 +03004426 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4427 u16 proto;
4428 proto = nla_get_u16(
4429 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4430 settings->control_port_ethertype = cpu_to_be16(proto);
4431 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4432 proto != ETH_P_PAE)
4433 return -EINVAL;
4434 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4435 settings->control_port_no_encrypt = true;
4436 } else
4437 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4438
Samuel Ortizb23aa672009-07-01 21:26:54 +02004439 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4440 void *data;
4441 int len, i;
4442
4443 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4444 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4445 settings->n_ciphers_pairwise = len / sizeof(u32);
4446
4447 if (len % sizeof(u32))
4448 return -EINVAL;
4449
Johannes Berg3dc27d22009-07-02 21:36:37 +02004450 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004451 return -EINVAL;
4452
4453 memcpy(settings->ciphers_pairwise, data, len);
4454
4455 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004456 if (!cfg80211_supported_cipher_suite(
4457 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004458 settings->ciphers_pairwise[i]))
4459 return -EINVAL;
4460 }
4461
4462 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4463 settings->cipher_group =
4464 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004465 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4466 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004467 return -EINVAL;
4468 }
4469
4470 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4471 settings->wpa_versions =
4472 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4473 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4474 return -EINVAL;
4475 }
4476
4477 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4478 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004479 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004480
4481 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4482 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4483 settings->n_akm_suites = len / sizeof(u32);
4484
4485 if (len % sizeof(u32))
4486 return -EINVAL;
4487
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004488 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4489 return -EINVAL;
4490
Samuel Ortizb23aa672009-07-01 21:26:54 +02004491 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004492 }
4493
4494 return 0;
4495}
4496
Jouni Malinen636a5d32009-03-19 13:39:22 +02004497static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4498{
Johannes Berg4c476992010-10-04 21:36:35 +02004499 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4500 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004501 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004502 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004503 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004504 int err, ssid_len, ie_len = 0;
4505 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004506 u32 flags = 0;
4507 struct ieee80211_ht_cap *ht_capa = NULL;
4508 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004509
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004510 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4511 return -EINVAL;
4512
4513 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004514 !info->attrs[NL80211_ATTR_SSID] ||
4515 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004516 return -EINVAL;
4517
Johannes Berg4c476992010-10-04 21:36:35 +02004518 if (!rdev->ops->assoc)
4519 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004520
Johannes Berg074ac8d2010-09-16 14:58:22 +02004521 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004522 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4523 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004524
Johannes Berg19957bb2009-07-02 17:20:43 +02004525 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004526
Johannes Berg19957bb2009-07-02 17:20:43 +02004527 chan = ieee80211_get_channel(&rdev->wiphy,
4528 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004529 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4530 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004531
Johannes Berg19957bb2009-07-02 17:20:43 +02004532 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4533 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004534
4535 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004536 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4537 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004538 }
4539
Jouni Malinendc6382c2009-05-06 22:09:37 +03004540 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004541 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004542 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004543 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004544 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004545 else if (mfp != NL80211_MFP_NO)
4546 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004547 }
4548
Johannes Berg3e5d7642009-07-07 14:37:26 +02004549 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4550 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4551
Ben Greear7e7c8922011-11-18 11:31:59 -08004552 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4553 flags |= ASSOC_REQ_DISABLE_HT;
4554
4555 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4556 ht_capa_mask =
4557 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4558
4559 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4560 if (!ht_capa_mask)
4561 return -EINVAL;
4562 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4563 }
4564
Johannes Bergc0692b82010-08-27 14:26:53 +03004565 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004566 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004567 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4568 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004569 &crypto, flags, ht_capa,
4570 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004571
Jouni Malinen636a5d32009-03-19 13:39:22 +02004572 return err;
4573}
4574
4575static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4576{
Johannes Berg4c476992010-10-04 21:36:35 +02004577 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4578 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004579 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004580 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004581 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004582 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004583
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004584 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4585 return -EINVAL;
4586
4587 if (!info->attrs[NL80211_ATTR_MAC])
4588 return -EINVAL;
4589
4590 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4591 return -EINVAL;
4592
Johannes Berg4c476992010-10-04 21:36:35 +02004593 if (!rdev->ops->deauth)
4594 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004595
Johannes Berg074ac8d2010-09-16 14:58:22 +02004596 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004597 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4598 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004599
Johannes Berg19957bb2009-07-02 17:20:43 +02004600 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004601
Johannes Berg19957bb2009-07-02 17:20:43 +02004602 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4603 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004604 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004605 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004606 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004607
4608 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004609 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4610 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004611 }
4612
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004613 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4614
Johannes Berg4c476992010-10-04 21:36:35 +02004615 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4616 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004617}
4618
4619static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4620{
Johannes Berg4c476992010-10-04 21:36:35 +02004621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4622 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004623 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004624 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004625 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004626 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004627
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004628 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4629 return -EINVAL;
4630
4631 if (!info->attrs[NL80211_ATTR_MAC])
4632 return -EINVAL;
4633
4634 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4635 return -EINVAL;
4636
Johannes Berg4c476992010-10-04 21:36:35 +02004637 if (!rdev->ops->disassoc)
4638 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004639
Johannes Berg074ac8d2010-09-16 14:58:22 +02004640 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004641 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4642 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004643
Johannes Berg19957bb2009-07-02 17:20:43 +02004644 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004645
Johannes Berg19957bb2009-07-02 17:20:43 +02004646 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4647 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004648 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004649 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004650 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004651
4652 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004653 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4654 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004655 }
4656
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004657 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4658
Johannes Berg4c476992010-10-04 21:36:35 +02004659 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4660 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004661}
4662
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004663static bool
4664nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4665 int mcast_rate[IEEE80211_NUM_BANDS],
4666 int rateval)
4667{
4668 struct wiphy *wiphy = &rdev->wiphy;
4669 bool found = false;
4670 int band, i;
4671
4672 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4673 struct ieee80211_supported_band *sband;
4674
4675 sband = wiphy->bands[band];
4676 if (!sband)
4677 continue;
4678
4679 for (i = 0; i < sband->n_bitrates; i++) {
4680 if (sband->bitrates[i].bitrate == rateval) {
4681 mcast_rate[band] = i + 1;
4682 found = true;
4683 break;
4684 }
4685 }
4686 }
4687
4688 return found;
4689}
4690
Johannes Berg04a773a2009-04-19 21:24:32 +02004691static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4692{
Johannes Berg4c476992010-10-04 21:36:35 +02004693 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4694 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004695 struct cfg80211_ibss_params ibss;
4696 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004697 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004698 int err;
4699
Johannes Berg8e30bc52009-04-22 17:45:38 +02004700 memset(&ibss, 0, sizeof(ibss));
4701
Johannes Berg04a773a2009-04-19 21:24:32 +02004702 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4703 return -EINVAL;
4704
4705 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4706 !info->attrs[NL80211_ATTR_SSID] ||
4707 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4708 return -EINVAL;
4709
Johannes Berg8e30bc52009-04-22 17:45:38 +02004710 ibss.beacon_interval = 100;
4711
4712 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4713 ibss.beacon_interval =
4714 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4715 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4716 return -EINVAL;
4717 }
4718
Johannes Berg4c476992010-10-04 21:36:35 +02004719 if (!rdev->ops->join_ibss)
4720 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004721
Johannes Berg4c476992010-10-04 21:36:35 +02004722 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4723 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004724
Johannes Berg79c97e92009-07-07 03:56:12 +02004725 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004726
Johannes Berg39193492011-09-16 13:45:25 +02004727 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004728 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004729
4730 if (!is_valid_ether_addr(ibss.bssid))
4731 return -EINVAL;
4732 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004733 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4734 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4735
4736 if (info->attrs[NL80211_ATTR_IE]) {
4737 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4738 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4739 }
4740
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004741 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4742 enum nl80211_channel_type channel_type;
4743
4744 channel_type = nla_get_u32(
4745 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4746 if (channel_type != NL80211_CHAN_NO_HT &&
4747 channel_type != NL80211_CHAN_HT20 &&
4748 channel_type != NL80211_CHAN_HT40MINUS &&
4749 channel_type != NL80211_CHAN_HT40PLUS)
4750 return -EINVAL;
4751
4752 if (channel_type != NL80211_CHAN_NO_HT &&
4753 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4754 return -EINVAL;
4755
4756 ibss.channel_type = channel_type;
4757 } else {
4758 ibss.channel_type = NL80211_CHAN_NO_HT;
4759 }
4760
4761 ibss.channel = rdev_freq_to_chan(rdev,
4762 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4763 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004764 if (!ibss.channel ||
4765 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004766 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4767 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004768
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004769 /* Both channels should be able to initiate communication */
4770 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4771 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4772 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4773 ibss.channel_type))
4774 return -EINVAL;
4775
Johannes Berg04a773a2009-04-19 21:24:32 +02004776 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004777 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004778
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004779 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4780 u8 *rates =
4781 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4782 int n_rates =
4783 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4784 struct ieee80211_supported_band *sband =
4785 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004786
Johannes Berg34850ab2011-07-18 18:08:35 +02004787 err = ieee80211_get_ratemask(sband, rates, n_rates,
4788 &ibss.basic_rates);
4789 if (err)
4790 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004791 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004792
4793 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4794 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4795 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4796 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004797
Johannes Berg4c476992010-10-04 21:36:35 +02004798 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4799 connkeys = nl80211_parse_connkeys(rdev,
4800 info->attrs[NL80211_ATTR_KEYS]);
4801 if (IS_ERR(connkeys))
4802 return PTR_ERR(connkeys);
4803 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004804
Johannes Berg4c476992010-10-04 21:36:35 +02004805 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004806 if (err)
4807 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004808 return err;
4809}
4810
4811static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4812{
Johannes Berg4c476992010-10-04 21:36:35 +02004813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4814 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004815
Johannes Berg4c476992010-10-04 21:36:35 +02004816 if (!rdev->ops->leave_ibss)
4817 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004818
Johannes Berg4c476992010-10-04 21:36:35 +02004819 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4820 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004821
Johannes Berg4c476992010-10-04 21:36:35 +02004822 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004823}
4824
Johannes Bergaff89a92009-07-01 21:26:51 +02004825#ifdef CONFIG_NL80211_TESTMODE
4826static struct genl_multicast_group nl80211_testmode_mcgrp = {
4827 .name = "testmode",
4828};
4829
4830static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4831{
Johannes Berg4c476992010-10-04 21:36:35 +02004832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004833 int err;
4834
4835 if (!info->attrs[NL80211_ATTR_TESTDATA])
4836 return -EINVAL;
4837
Johannes Bergaff89a92009-07-01 21:26:51 +02004838 err = -EOPNOTSUPP;
4839 if (rdev->ops->testmode_cmd) {
4840 rdev->testmode_info = info;
4841 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4842 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4843 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4844 rdev->testmode_info = NULL;
4845 }
4846
Johannes Bergaff89a92009-07-01 21:26:51 +02004847 return err;
4848}
4849
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004850static int nl80211_testmode_dump(struct sk_buff *skb,
4851 struct netlink_callback *cb)
4852{
Johannes Berg00918d32011-12-13 17:22:05 +01004853 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004854 int err;
4855 long phy_idx;
4856 void *data = NULL;
4857 int data_len = 0;
4858
4859 if (cb->args[0]) {
4860 /*
4861 * 0 is a valid index, but not valid for args[0],
4862 * so we need to offset by 1.
4863 */
4864 phy_idx = cb->args[0] - 1;
4865 } else {
4866 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4867 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4868 nl80211_policy);
4869 if (err)
4870 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01004871 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
4872 phy_idx = nla_get_u32(
4873 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4874 } else {
4875 struct net_device *netdev;
4876
4877 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
4878 nl80211_fam.attrbuf,
4879 &rdev, &netdev);
4880 if (err)
4881 return err;
4882 dev_put(netdev);
4883 phy_idx = rdev->wiphy_idx;
4884 cfg80211_unlock_rdev(rdev);
4885 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004886 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4887 cb->args[1] =
4888 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4889 }
4890
4891 if (cb->args[1]) {
4892 data = nla_data((void *)cb->args[1]);
4893 data_len = nla_len((void *)cb->args[1]);
4894 }
4895
4896 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01004897 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4898 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004899 mutex_unlock(&cfg80211_mutex);
4900 return -ENOENT;
4901 }
Johannes Berg00918d32011-12-13 17:22:05 +01004902 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004903 mutex_unlock(&cfg80211_mutex);
4904
Johannes Berg00918d32011-12-13 17:22:05 +01004905 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004906 err = -EOPNOTSUPP;
4907 goto out_err;
4908 }
4909
4910 while (1) {
4911 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4912 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4913 NL80211_CMD_TESTMODE);
4914 struct nlattr *tmdata;
4915
Johannes Berg00918d32011-12-13 17:22:05 +01004916 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004917 genlmsg_cancel(skb, hdr);
4918 break;
4919 }
4920
4921 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4922 if (!tmdata) {
4923 genlmsg_cancel(skb, hdr);
4924 break;
4925 }
Johannes Berg00918d32011-12-13 17:22:05 +01004926 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
4927 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004928 nla_nest_end(skb, tmdata);
4929
4930 if (err == -ENOBUFS || err == -ENOENT) {
4931 genlmsg_cancel(skb, hdr);
4932 break;
4933 } else if (err) {
4934 genlmsg_cancel(skb, hdr);
4935 goto out_err;
4936 }
4937
4938 genlmsg_end(skb, hdr);
4939 }
4940
4941 err = skb->len;
4942 /* see above */
4943 cb->args[0] = phy_idx + 1;
4944 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01004945 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004946 return err;
4947}
4948
Johannes Bergaff89a92009-07-01 21:26:51 +02004949static struct sk_buff *
4950__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4951 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4952{
4953 struct sk_buff *skb;
4954 void *hdr;
4955 struct nlattr *data;
4956
4957 skb = nlmsg_new(approxlen + 100, gfp);
4958 if (!skb)
4959 return NULL;
4960
4961 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4962 if (!hdr) {
4963 kfree_skb(skb);
4964 return NULL;
4965 }
4966
4967 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4968 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4969
4970 ((void **)skb->cb)[0] = rdev;
4971 ((void **)skb->cb)[1] = hdr;
4972 ((void **)skb->cb)[2] = data;
4973
4974 return skb;
4975
4976 nla_put_failure:
4977 kfree_skb(skb);
4978 return NULL;
4979}
4980
4981struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4982 int approxlen)
4983{
4984 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4985
4986 if (WARN_ON(!rdev->testmode_info))
4987 return NULL;
4988
4989 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4990 rdev->testmode_info->snd_pid,
4991 rdev->testmode_info->snd_seq,
4992 GFP_KERNEL);
4993}
4994EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4995
4996int cfg80211_testmode_reply(struct sk_buff *skb)
4997{
4998 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4999 void *hdr = ((void **)skb->cb)[1];
5000 struct nlattr *data = ((void **)skb->cb)[2];
5001
5002 if (WARN_ON(!rdev->testmode_info)) {
5003 kfree_skb(skb);
5004 return -EINVAL;
5005 }
5006
5007 nla_nest_end(skb, data);
5008 genlmsg_end(skb, hdr);
5009 return genlmsg_reply(skb, rdev->testmode_info);
5010}
5011EXPORT_SYMBOL(cfg80211_testmode_reply);
5012
5013struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5014 int approxlen, gfp_t gfp)
5015{
5016 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5017
5018 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5019}
5020EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5021
5022void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5023{
5024 void *hdr = ((void **)skb->cb)[1];
5025 struct nlattr *data = ((void **)skb->cb)[2];
5026
5027 nla_nest_end(skb, data);
5028 genlmsg_end(skb, hdr);
5029 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5030}
5031EXPORT_SYMBOL(cfg80211_testmode_event);
5032#endif
5033
Samuel Ortizb23aa672009-07-01 21:26:54 +02005034static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5035{
Johannes Berg4c476992010-10-04 21:36:35 +02005036 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5037 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005038 struct cfg80211_connect_params connect;
5039 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005040 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005041 int err;
5042
5043 memset(&connect, 0, sizeof(connect));
5044
5045 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5046 return -EINVAL;
5047
5048 if (!info->attrs[NL80211_ATTR_SSID] ||
5049 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5050 return -EINVAL;
5051
5052 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5053 connect.auth_type =
5054 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5055 if (!nl80211_valid_auth_type(connect.auth_type))
5056 return -EINVAL;
5057 } else
5058 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5059
5060 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5061
Johannes Bergc0692b82010-08-27 14:26:53 +03005062 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005063 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005064 if (err)
5065 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005066
Johannes Berg074ac8d2010-09-16 14:58:22 +02005067 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005068 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5069 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005070
Johannes Berg79c97e92009-07-07 03:56:12 +02005071 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005072
Samuel Ortizb23aa672009-07-01 21:26:54 +02005073 if (info->attrs[NL80211_ATTR_MAC])
5074 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5075 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5076 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5077
5078 if (info->attrs[NL80211_ATTR_IE]) {
5079 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5080 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5081 }
5082
5083 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5084 connect.channel =
5085 ieee80211_get_channel(wiphy,
5086 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5087 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005088 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5089 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005090 }
5091
Johannes Bergfffd0932009-07-08 14:22:54 +02005092 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5093 connkeys = nl80211_parse_connkeys(rdev,
5094 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005095 if (IS_ERR(connkeys))
5096 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005097 }
5098
Ben Greear7e7c8922011-11-18 11:31:59 -08005099 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5100 connect.flags |= ASSOC_REQ_DISABLE_HT;
5101
5102 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5103 memcpy(&connect.ht_capa_mask,
5104 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5105 sizeof(connect.ht_capa_mask));
5106
5107 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5108 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5109 return -EINVAL;
5110 memcpy(&connect.ht_capa,
5111 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5112 sizeof(connect.ht_capa));
5113 }
5114
Johannes Bergfffd0932009-07-08 14:22:54 +02005115 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005116 if (err)
5117 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005118 return err;
5119}
5120
5121static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5122{
Johannes Berg4c476992010-10-04 21:36:35 +02005123 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5124 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005125 u16 reason;
5126
5127 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5128 reason = WLAN_REASON_DEAUTH_LEAVING;
5129 else
5130 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5131
5132 if (reason == 0)
5133 return -EINVAL;
5134
Johannes Berg074ac8d2010-09-16 14:58:22 +02005135 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005136 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5137 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005138
Johannes Berg4c476992010-10-04 21:36:35 +02005139 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005140}
5141
Johannes Berg463d0182009-07-14 00:33:35 +02005142static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5143{
Johannes Berg4c476992010-10-04 21:36:35 +02005144 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005145 struct net *net;
5146 int err;
5147 u32 pid;
5148
5149 if (!info->attrs[NL80211_ATTR_PID])
5150 return -EINVAL;
5151
5152 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5153
Johannes Berg463d0182009-07-14 00:33:35 +02005154 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005155 if (IS_ERR(net))
5156 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005157
5158 err = 0;
5159
5160 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005161 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5162 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005163
Johannes Berg463d0182009-07-14 00:33:35 +02005164 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005165 return err;
5166}
5167
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005168static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5169{
Johannes Berg4c476992010-10-04 21:36:35 +02005170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005171 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5172 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005173 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005174 struct cfg80211_pmksa pmksa;
5175
5176 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5177
5178 if (!info->attrs[NL80211_ATTR_MAC])
5179 return -EINVAL;
5180
5181 if (!info->attrs[NL80211_ATTR_PMKID])
5182 return -EINVAL;
5183
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005184 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5185 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5186
Johannes Berg074ac8d2010-09-16 14:58:22 +02005187 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005188 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5189 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005190
5191 switch (info->genlhdr->cmd) {
5192 case NL80211_CMD_SET_PMKSA:
5193 rdev_ops = rdev->ops->set_pmksa;
5194 break;
5195 case NL80211_CMD_DEL_PMKSA:
5196 rdev_ops = rdev->ops->del_pmksa;
5197 break;
5198 default:
5199 WARN_ON(1);
5200 break;
5201 }
5202
Johannes Berg4c476992010-10-04 21:36:35 +02005203 if (!rdev_ops)
5204 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005205
Johannes Berg4c476992010-10-04 21:36:35 +02005206 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005207}
5208
5209static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5210{
Johannes Berg4c476992010-10-04 21:36:35 +02005211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5212 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005213
Johannes Berg074ac8d2010-09-16 14:58:22 +02005214 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005215 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5216 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005217
Johannes Berg4c476992010-10-04 21:36:35 +02005218 if (!rdev->ops->flush_pmksa)
5219 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005220
Johannes Berg4c476992010-10-04 21:36:35 +02005221 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005222}
5223
Arik Nemtsov109086c2011-09-28 14:12:50 +03005224static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5225{
5226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5227 struct net_device *dev = info->user_ptr[1];
5228 u8 action_code, dialog_token;
5229 u16 status_code;
5230 u8 *peer;
5231
5232 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5233 !rdev->ops->tdls_mgmt)
5234 return -EOPNOTSUPP;
5235
5236 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5237 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5238 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5239 !info->attrs[NL80211_ATTR_IE] ||
5240 !info->attrs[NL80211_ATTR_MAC])
5241 return -EINVAL;
5242
5243 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5244 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5245 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5246 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5247
5248 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5249 dialog_token, status_code,
5250 nla_data(info->attrs[NL80211_ATTR_IE]),
5251 nla_len(info->attrs[NL80211_ATTR_IE]));
5252}
5253
5254static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5255{
5256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5257 struct net_device *dev = info->user_ptr[1];
5258 enum nl80211_tdls_operation operation;
5259 u8 *peer;
5260
5261 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5262 !rdev->ops->tdls_oper)
5263 return -EOPNOTSUPP;
5264
5265 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5266 !info->attrs[NL80211_ATTR_MAC])
5267 return -EINVAL;
5268
5269 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5270 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5271
5272 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5273}
5274
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005275static int nl80211_remain_on_channel(struct sk_buff *skb,
5276 struct genl_info *info)
5277{
Johannes Berg4c476992010-10-04 21:36:35 +02005278 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5279 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005280 struct ieee80211_channel *chan;
5281 struct sk_buff *msg;
5282 void *hdr;
5283 u64 cookie;
5284 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5285 u32 freq, duration;
5286 int err;
5287
5288 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5289 !info->attrs[NL80211_ATTR_DURATION])
5290 return -EINVAL;
5291
5292 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5293
5294 /*
5295 * We should be on that channel for at least one jiffie,
5296 * and more than 5 seconds seems excessive.
5297 */
Johannes Berga2939112010-12-14 17:54:28 +01005298 if (!duration || !msecs_to_jiffies(duration) ||
5299 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005300 return -EINVAL;
5301
Johannes Berg7c4ef712011-11-18 15:33:48 +01005302 if (!rdev->ops->remain_on_channel ||
5303 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005304 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005305
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005306 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5307 channel_type = nla_get_u32(
5308 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5309 if (channel_type != NL80211_CHAN_NO_HT &&
5310 channel_type != NL80211_CHAN_HT20 &&
5311 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005312 channel_type != NL80211_CHAN_HT40MINUS)
5313 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005314 }
5315
5316 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5317 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005318 if (chan == NULL)
5319 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005320
5321 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005322 if (!msg)
5323 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005324
5325 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5326 NL80211_CMD_REMAIN_ON_CHANNEL);
5327
5328 if (IS_ERR(hdr)) {
5329 err = PTR_ERR(hdr);
5330 goto free_msg;
5331 }
5332
5333 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5334 channel_type, duration, &cookie);
5335
5336 if (err)
5337 goto free_msg;
5338
5339 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5340
5341 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005342
5343 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005344
5345 nla_put_failure:
5346 err = -ENOBUFS;
5347 free_msg:
5348 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005349 return err;
5350}
5351
5352static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5353 struct genl_info *info)
5354{
Johannes Berg4c476992010-10-04 21:36:35 +02005355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5356 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005357 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005358
5359 if (!info->attrs[NL80211_ATTR_COOKIE])
5360 return -EINVAL;
5361
Johannes Berg4c476992010-10-04 21:36:35 +02005362 if (!rdev->ops->cancel_remain_on_channel)
5363 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005364
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005365 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5366
Johannes Berg4c476992010-10-04 21:36:35 +02005367 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005368}
5369
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005370static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5371 u8 *rates, u8 rates_len)
5372{
5373 u8 i;
5374 u32 mask = 0;
5375
5376 for (i = 0; i < rates_len; i++) {
5377 int rate = (rates[i] & 0x7f) * 5;
5378 int ridx;
5379 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5380 struct ieee80211_rate *srate =
5381 &sband->bitrates[ridx];
5382 if (rate == srate->bitrate) {
5383 mask |= 1 << ridx;
5384 break;
5385 }
5386 }
5387 if (ridx == sband->n_bitrates)
5388 return 0; /* rate not found */
5389 }
5390
5391 return mask;
5392}
5393
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005394static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5395 u8 *rates, u8 rates_len,
5396 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5397{
5398 u8 i;
5399
5400 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5401
5402 for (i = 0; i < rates_len; i++) {
5403 int ridx, rbit;
5404
5405 ridx = rates[i] / 8;
5406 rbit = BIT(rates[i] % 8);
5407
5408 /* check validity */
5409 if ((ridx < 0) || (ridx > IEEE80211_HT_MCS_MASK_LEN))
5410 return false;
5411
5412 /* check availability */
5413 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5414 mcs[ridx] |= rbit;
5415 else
5416 return false;
5417 }
5418
5419 return true;
5420}
5421
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005422static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005423 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5424 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005425 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5426 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005427};
5428
5429static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5430 struct genl_info *info)
5431{
5432 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005434 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005435 int rem, i;
5436 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005437 struct nlattr *tx_rates;
5438 struct ieee80211_supported_band *sband;
5439
5440 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5441 return -EINVAL;
5442
Johannes Berg4c476992010-10-04 21:36:35 +02005443 if (!rdev->ops->set_bitrate_mask)
5444 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005445
5446 memset(&mask, 0, sizeof(mask));
5447 /* Default to all rates enabled */
5448 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5449 sband = rdev->wiphy.bands[i];
5450 mask.control[i].legacy =
5451 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005452 if (sband)
5453 memcpy(mask.control[i].mcs,
5454 sband->ht_cap.mcs.rx_mask,
5455 sizeof(mask.control[i].mcs));
5456 else
5457 memset(mask.control[i].mcs, 0,
5458 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005459 }
5460
5461 /*
5462 * The nested attribute uses enum nl80211_band as the index. This maps
5463 * directly to the enum ieee80211_band values used in cfg80211.
5464 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005465 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005466 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5467 {
5468 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005469 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5470 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005471 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005472 if (sband == NULL)
5473 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005474 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5475 nla_len(tx_rates), nl80211_txattr_policy);
5476 if (tb[NL80211_TXRATE_LEGACY]) {
5477 mask.control[band].legacy = rateset_to_mask(
5478 sband,
5479 nla_data(tb[NL80211_TXRATE_LEGACY]),
5480 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005481 }
5482 if (tb[NL80211_TXRATE_MCS]) {
5483 if (!ht_rateset_to_mask(
5484 sband,
5485 nla_data(tb[NL80211_TXRATE_MCS]),
5486 nla_len(tb[NL80211_TXRATE_MCS]),
5487 mask.control[band].mcs))
5488 return -EINVAL;
5489 }
5490
5491 if (mask.control[band].legacy == 0) {
5492 /* don't allow empty legacy rates if HT
5493 * is not even supported. */
5494 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5495 return -EINVAL;
5496
5497 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5498 if (mask.control[band].mcs[i])
5499 break;
5500
5501 /* legacy and mcs rates may not be both empty */
5502 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005503 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005504 }
5505 }
5506
Johannes Berg4c476992010-10-04 21:36:35 +02005507 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005508}
5509
Johannes Berg2e161f72010-08-12 15:38:38 +02005510static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005511{
Johannes Berg4c476992010-10-04 21:36:35 +02005512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5513 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005514 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005515
5516 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5517 return -EINVAL;
5518
Johannes Berg2e161f72010-08-12 15:38:38 +02005519 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5520 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005521
Johannes Berg9d38d852010-06-09 17:20:33 +02005522 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005523 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005524 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5525 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5526 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005527 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005528 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5529 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005530
5531 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005532 if (!rdev->ops->mgmt_tx)
5533 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005534
Johannes Berg4c476992010-10-04 21:36:35 +02005535 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005536 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005537 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5538 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005539}
5540
Johannes Berg2e161f72010-08-12 15:38:38 +02005541static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005542{
Johannes Berg4c476992010-10-04 21:36:35 +02005543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5544 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005545 struct ieee80211_channel *chan;
5546 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005547 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005548 u32 freq;
5549 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005550 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005551 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005552 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005553 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005554 bool offchan, no_cck, dont_wait_for_ack;
5555
5556 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005557
5558 if (!info->attrs[NL80211_ATTR_FRAME] ||
5559 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5560 return -EINVAL;
5561
Johannes Berg4c476992010-10-04 21:36:35 +02005562 if (!rdev->ops->mgmt_tx)
5563 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005564
Johannes Berg9d38d852010-06-09 17:20:33 +02005565 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005566 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005567 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5568 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5569 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005570 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005571 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5572 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005573
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005574 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005575 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005576 return -EINVAL;
5577 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5578 }
5579
Jouni Malinen026331c2010-02-15 12:53:10 +02005580 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5581 channel_type = nla_get_u32(
5582 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5583 if (channel_type != NL80211_CHAN_NO_HT &&
5584 channel_type != NL80211_CHAN_HT20 &&
5585 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005586 channel_type != NL80211_CHAN_HT40MINUS)
5587 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005588 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005589 }
5590
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005591 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5592
Johannes Berg7c4ef712011-11-18 15:33:48 +01005593 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5594 return -EINVAL;
5595
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305596 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5597
Jouni Malinen026331c2010-02-15 12:53:10 +02005598 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5599 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005600 if (chan == NULL)
5601 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005602
Johannes Berge247bd902011-11-04 11:18:21 +01005603 if (!dont_wait_for_ack) {
5604 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5605 if (!msg)
5606 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005607
Johannes Berge247bd902011-11-04 11:18:21 +01005608 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5609 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005610
Johannes Berge247bd902011-11-04 11:18:21 +01005611 if (IS_ERR(hdr)) {
5612 err = PTR_ERR(hdr);
5613 goto free_msg;
5614 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005615 }
Johannes Berge247bd902011-11-04 11:18:21 +01005616
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005617 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5618 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005619 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5620 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005621 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005622 if (err)
5623 goto free_msg;
5624
Johannes Berge247bd902011-11-04 11:18:21 +01005625 if (msg) {
5626 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005627
Johannes Berge247bd902011-11-04 11:18:21 +01005628 genlmsg_end(msg, hdr);
5629 return genlmsg_reply(msg, info);
5630 }
5631
5632 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005633
5634 nla_put_failure:
5635 err = -ENOBUFS;
5636 free_msg:
5637 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005638 return err;
5639}
5640
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005641static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5642{
5643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5644 struct net_device *dev = info->user_ptr[1];
5645 u64 cookie;
5646
5647 if (!info->attrs[NL80211_ATTR_COOKIE])
5648 return -EINVAL;
5649
5650 if (!rdev->ops->mgmt_tx_cancel_wait)
5651 return -EOPNOTSUPP;
5652
5653 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5654 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5655 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5656 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5657 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5658 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5659 return -EOPNOTSUPP;
5660
5661 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5662
5663 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5664}
5665
Kalle Valoffb9eb32010-02-17 17:58:10 +02005666static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5667{
Johannes Berg4c476992010-10-04 21:36:35 +02005668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005669 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005670 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005671 u8 ps_state;
5672 bool state;
5673 int err;
5674
Johannes Berg4c476992010-10-04 21:36:35 +02005675 if (!info->attrs[NL80211_ATTR_PS_STATE])
5676 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005677
5678 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5679
Johannes Berg4c476992010-10-04 21:36:35 +02005680 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5681 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005682
5683 wdev = dev->ieee80211_ptr;
5684
Johannes Berg4c476992010-10-04 21:36:35 +02005685 if (!rdev->ops->set_power_mgmt)
5686 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005687
5688 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5689
5690 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005691 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005692
Johannes Berg4c476992010-10-04 21:36:35 +02005693 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5694 wdev->ps_timeout);
5695 if (!err)
5696 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005697 return err;
5698}
5699
5700static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5701{
Johannes Berg4c476992010-10-04 21:36:35 +02005702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005703 enum nl80211_ps_state ps_state;
5704 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005705 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005706 struct sk_buff *msg;
5707 void *hdr;
5708 int err;
5709
Kalle Valoffb9eb32010-02-17 17:58:10 +02005710 wdev = dev->ieee80211_ptr;
5711
Johannes Berg4c476992010-10-04 21:36:35 +02005712 if (!rdev->ops->set_power_mgmt)
5713 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005714
5715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005716 if (!msg)
5717 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005718
5719 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5720 NL80211_CMD_GET_POWER_SAVE);
5721 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005722 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005723 goto free_msg;
5724 }
5725
5726 if (wdev->ps)
5727 ps_state = NL80211_PS_ENABLED;
5728 else
5729 ps_state = NL80211_PS_DISABLED;
5730
5731 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5732
5733 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005734 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005735
Johannes Berg4c476992010-10-04 21:36:35 +02005736 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005737 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005738 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005739 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005740 return err;
5741}
5742
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005743static struct nla_policy
5744nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5745 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5746 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5747 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5748};
5749
5750static int nl80211_set_cqm_rssi(struct genl_info *info,
5751 s32 threshold, u32 hysteresis)
5752{
Johannes Berg4c476992010-10-04 21:36:35 +02005753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005754 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005755 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005756
5757 if (threshold > 0)
5758 return -EINVAL;
5759
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005760 wdev = dev->ieee80211_ptr;
5761
Johannes Berg4c476992010-10-04 21:36:35 +02005762 if (!rdev->ops->set_cqm_rssi_config)
5763 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005764
Johannes Berg074ac8d2010-09-16 14:58:22 +02005765 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005766 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5767 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005768
Johannes Berg4c476992010-10-04 21:36:35 +02005769 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5770 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005771}
5772
5773static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5774{
5775 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5776 struct nlattr *cqm;
5777 int err;
5778
5779 cqm = info->attrs[NL80211_ATTR_CQM];
5780 if (!cqm) {
5781 err = -EINVAL;
5782 goto out;
5783 }
5784
5785 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5786 nl80211_attr_cqm_policy);
5787 if (err)
5788 goto out;
5789
5790 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5791 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5792 s32 threshold;
5793 u32 hysteresis;
5794 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5795 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5796 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5797 } else
5798 err = -EINVAL;
5799
5800out:
5801 return err;
5802}
5803
Johannes Berg29cbe682010-12-03 09:20:44 +01005804static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5805{
5806 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5807 struct net_device *dev = info->user_ptr[1];
5808 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005809 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005810 int err;
5811
5812 /* start with default */
5813 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005814 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005815
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005816 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005817 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005818 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005819 if (err)
5820 return err;
5821 }
5822
5823 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5824 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5825 return -EINVAL;
5826
Javier Cardonac80d5452010-12-16 17:37:49 -08005827 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5828 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5829
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08005830 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5831 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5832 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5833 return -EINVAL;
5834
Javier Cardonac80d5452010-12-16 17:37:49 -08005835 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5836 /* parse additional setup parameters if given */
5837 err = nl80211_parse_mesh_setup(info, &setup);
5838 if (err)
5839 return err;
5840 }
5841
5842 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005843}
5844
5845static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5846{
5847 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5848 struct net_device *dev = info->user_ptr[1];
5849
5850 return cfg80211_leave_mesh(rdev, dev);
5851}
5852
Johannes Bergff1b6e62011-05-04 15:37:28 +02005853static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5854{
5855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5856 struct sk_buff *msg;
5857 void *hdr;
5858
5859 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5860 return -EOPNOTSUPP;
5861
5862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5863 if (!msg)
5864 return -ENOMEM;
5865
5866 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5867 NL80211_CMD_GET_WOWLAN);
5868 if (!hdr)
5869 goto nla_put_failure;
5870
5871 if (rdev->wowlan) {
5872 struct nlattr *nl_wowlan;
5873
5874 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5875 if (!nl_wowlan)
5876 goto nla_put_failure;
5877
5878 if (rdev->wowlan->any)
5879 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5880 if (rdev->wowlan->disconnect)
5881 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5882 if (rdev->wowlan->magic_pkt)
5883 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005884 if (rdev->wowlan->gtk_rekey_failure)
5885 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5886 if (rdev->wowlan->eap_identity_req)
5887 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5888 if (rdev->wowlan->four_way_handshake)
5889 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5890 if (rdev->wowlan->rfkill_release)
5891 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005892 if (rdev->wowlan->n_patterns) {
5893 struct nlattr *nl_pats, *nl_pat;
5894 int i, pat_len;
5895
5896 nl_pats = nla_nest_start(msg,
5897 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5898 if (!nl_pats)
5899 goto nla_put_failure;
5900
5901 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5902 nl_pat = nla_nest_start(msg, i + 1);
5903 if (!nl_pat)
5904 goto nla_put_failure;
5905 pat_len = rdev->wowlan->patterns[i].pattern_len;
5906 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5907 DIV_ROUND_UP(pat_len, 8),
5908 rdev->wowlan->patterns[i].mask);
5909 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5910 pat_len,
5911 rdev->wowlan->patterns[i].pattern);
5912 nla_nest_end(msg, nl_pat);
5913 }
5914 nla_nest_end(msg, nl_pats);
5915 }
5916
5917 nla_nest_end(msg, nl_wowlan);
5918 }
5919
5920 genlmsg_end(msg, hdr);
5921 return genlmsg_reply(msg, info);
5922
5923nla_put_failure:
5924 nlmsg_free(msg);
5925 return -ENOBUFS;
5926}
5927
5928static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5929{
5930 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5931 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5932 struct cfg80211_wowlan no_triggers = {};
5933 struct cfg80211_wowlan new_triggers = {};
5934 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5935 int err, i;
5936
5937 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5938 return -EOPNOTSUPP;
5939
5940 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5941 goto no_triggers;
5942
5943 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5944 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5945 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5946 nl80211_wowlan_policy);
5947 if (err)
5948 return err;
5949
5950 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5951 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5952 return -EINVAL;
5953 new_triggers.any = true;
5954 }
5955
5956 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5957 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5958 return -EINVAL;
5959 new_triggers.disconnect = true;
5960 }
5961
5962 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5963 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5964 return -EINVAL;
5965 new_triggers.magic_pkt = true;
5966 }
5967
Johannes Berg77dbbb12011-07-13 10:48:55 +02005968 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5969 return -EINVAL;
5970
5971 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5972 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5973 return -EINVAL;
5974 new_triggers.gtk_rekey_failure = true;
5975 }
5976
5977 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5978 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5979 return -EINVAL;
5980 new_triggers.eap_identity_req = true;
5981 }
5982
5983 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5984 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5985 return -EINVAL;
5986 new_triggers.four_way_handshake = true;
5987 }
5988
5989 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5990 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5991 return -EINVAL;
5992 new_triggers.rfkill_release = true;
5993 }
5994
Johannes Bergff1b6e62011-05-04 15:37:28 +02005995 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5996 struct nlattr *pat;
5997 int n_patterns = 0;
5998 int rem, pat_len, mask_len;
5999 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6000
6001 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6002 rem)
6003 n_patterns++;
6004 if (n_patterns > wowlan->n_patterns)
6005 return -EINVAL;
6006
6007 new_triggers.patterns = kcalloc(n_patterns,
6008 sizeof(new_triggers.patterns[0]),
6009 GFP_KERNEL);
6010 if (!new_triggers.patterns)
6011 return -ENOMEM;
6012
6013 new_triggers.n_patterns = n_patterns;
6014 i = 0;
6015
6016 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6017 rem) {
6018 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6019 nla_data(pat), nla_len(pat), NULL);
6020 err = -EINVAL;
6021 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6022 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6023 goto error;
6024 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6025 mask_len = DIV_ROUND_UP(pat_len, 8);
6026 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6027 mask_len)
6028 goto error;
6029 if (pat_len > wowlan->pattern_max_len ||
6030 pat_len < wowlan->pattern_min_len)
6031 goto error;
6032
6033 new_triggers.patterns[i].mask =
6034 kmalloc(mask_len + pat_len, GFP_KERNEL);
6035 if (!new_triggers.patterns[i].mask) {
6036 err = -ENOMEM;
6037 goto error;
6038 }
6039 new_triggers.patterns[i].pattern =
6040 new_triggers.patterns[i].mask + mask_len;
6041 memcpy(new_triggers.patterns[i].mask,
6042 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6043 mask_len);
6044 new_triggers.patterns[i].pattern_len = pat_len;
6045 memcpy(new_triggers.patterns[i].pattern,
6046 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6047 pat_len);
6048 i++;
6049 }
6050 }
6051
6052 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6053 struct cfg80211_wowlan *ntrig;
6054 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6055 GFP_KERNEL);
6056 if (!ntrig) {
6057 err = -ENOMEM;
6058 goto error;
6059 }
6060 cfg80211_rdev_free_wowlan(rdev);
6061 rdev->wowlan = ntrig;
6062 } else {
6063 no_triggers:
6064 cfg80211_rdev_free_wowlan(rdev);
6065 rdev->wowlan = NULL;
6066 }
6067
6068 return 0;
6069 error:
6070 for (i = 0; i < new_triggers.n_patterns; i++)
6071 kfree(new_triggers.patterns[i].mask);
6072 kfree(new_triggers.patterns);
6073 return err;
6074}
6075
Johannes Berge5497d72011-07-05 16:35:40 +02006076static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6077{
6078 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6079 struct net_device *dev = info->user_ptr[1];
6080 struct wireless_dev *wdev = dev->ieee80211_ptr;
6081 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6082 struct cfg80211_gtk_rekey_data rekey_data;
6083 int err;
6084
6085 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6086 return -EINVAL;
6087
6088 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6089 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6090 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6091 nl80211_rekey_policy);
6092 if (err)
6093 return err;
6094
6095 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6096 return -ERANGE;
6097 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6098 return -ERANGE;
6099 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6100 return -ERANGE;
6101
6102 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6103 NL80211_KEK_LEN);
6104 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6105 NL80211_KCK_LEN);
6106 memcpy(rekey_data.replay_ctr,
6107 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6108 NL80211_REPLAY_CTR_LEN);
6109
6110 wdev_lock(wdev);
6111 if (!wdev->current_bss) {
6112 err = -ENOTCONN;
6113 goto out;
6114 }
6115
6116 if (!rdev->ops->set_rekey_data) {
6117 err = -EOPNOTSUPP;
6118 goto out;
6119 }
6120
6121 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6122 out:
6123 wdev_unlock(wdev);
6124 return err;
6125}
6126
Johannes Berg28946da2011-11-04 11:18:12 +01006127static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6128 struct genl_info *info)
6129{
6130 struct net_device *dev = info->user_ptr[1];
6131 struct wireless_dev *wdev = dev->ieee80211_ptr;
6132
6133 if (wdev->iftype != NL80211_IFTYPE_AP &&
6134 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6135 return -EINVAL;
6136
6137 if (wdev->ap_unexpected_nlpid)
6138 return -EBUSY;
6139
6140 wdev->ap_unexpected_nlpid = info->snd_pid;
6141 return 0;
6142}
6143
Johannes Berg7f6cf312011-11-04 11:18:15 +01006144static int nl80211_probe_client(struct sk_buff *skb,
6145 struct genl_info *info)
6146{
6147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6148 struct net_device *dev = info->user_ptr[1];
6149 struct wireless_dev *wdev = dev->ieee80211_ptr;
6150 struct sk_buff *msg;
6151 void *hdr;
6152 const u8 *addr;
6153 u64 cookie;
6154 int err;
6155
6156 if (wdev->iftype != NL80211_IFTYPE_AP &&
6157 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6158 return -EOPNOTSUPP;
6159
6160 if (!info->attrs[NL80211_ATTR_MAC])
6161 return -EINVAL;
6162
6163 if (!rdev->ops->probe_client)
6164 return -EOPNOTSUPP;
6165
6166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6167 if (!msg)
6168 return -ENOMEM;
6169
6170 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6171 NL80211_CMD_PROBE_CLIENT);
6172
6173 if (IS_ERR(hdr)) {
6174 err = PTR_ERR(hdr);
6175 goto free_msg;
6176 }
6177
6178 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6179
6180 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6181 if (err)
6182 goto free_msg;
6183
6184 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6185
6186 genlmsg_end(msg, hdr);
6187
6188 return genlmsg_reply(msg, info);
6189
6190 nla_put_failure:
6191 err = -ENOBUFS;
6192 free_msg:
6193 nlmsg_free(msg);
6194 return err;
6195}
6196
Johannes Berg5e760232011-11-04 11:18:17 +01006197static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6198{
6199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6200
6201 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6202 return -EOPNOTSUPP;
6203
6204 if (rdev->ap_beacons_nlpid)
6205 return -EBUSY;
6206
6207 rdev->ap_beacons_nlpid = info->snd_pid;
6208
6209 return 0;
6210}
6211
Johannes Berg4c476992010-10-04 21:36:35 +02006212#define NL80211_FLAG_NEED_WIPHY 0x01
6213#define NL80211_FLAG_NEED_NETDEV 0x02
6214#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006215#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6216#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6217 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006218
6219static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6220 struct genl_info *info)
6221{
6222 struct cfg80211_registered_device *rdev;
6223 struct net_device *dev;
6224 int err;
6225 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6226
6227 if (rtnl)
6228 rtnl_lock();
6229
6230 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6231 rdev = cfg80211_get_dev_from_info(info);
6232 if (IS_ERR(rdev)) {
6233 if (rtnl)
6234 rtnl_unlock();
6235 return PTR_ERR(rdev);
6236 }
6237 info->user_ptr[0] = rdev;
6238 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006239 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6240 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006241 if (err) {
6242 if (rtnl)
6243 rtnl_unlock();
6244 return err;
6245 }
Johannes Berg41265712010-10-04 21:14:05 +02006246 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6247 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006248 cfg80211_unlock_rdev(rdev);
6249 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006250 if (rtnl)
6251 rtnl_unlock();
6252 return -ENETDOWN;
6253 }
Johannes Berg4c476992010-10-04 21:36:35 +02006254 info->user_ptr[0] = rdev;
6255 info->user_ptr[1] = dev;
6256 }
6257
6258 return 0;
6259}
6260
6261static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6262 struct genl_info *info)
6263{
6264 if (info->user_ptr[0])
6265 cfg80211_unlock_rdev(info->user_ptr[0]);
6266 if (info->user_ptr[1])
6267 dev_put(info->user_ptr[1]);
6268 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6269 rtnl_unlock();
6270}
6271
Johannes Berg55682962007-09-20 13:09:35 -04006272static struct genl_ops nl80211_ops[] = {
6273 {
6274 .cmd = NL80211_CMD_GET_WIPHY,
6275 .doit = nl80211_get_wiphy,
6276 .dumpit = nl80211_dump_wiphy,
6277 .policy = nl80211_policy,
6278 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006279 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006280 },
6281 {
6282 .cmd = NL80211_CMD_SET_WIPHY,
6283 .doit = nl80211_set_wiphy,
6284 .policy = nl80211_policy,
6285 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006286 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006287 },
6288 {
6289 .cmd = NL80211_CMD_GET_INTERFACE,
6290 .doit = nl80211_get_interface,
6291 .dumpit = nl80211_dump_interface,
6292 .policy = nl80211_policy,
6293 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006294 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006295 },
6296 {
6297 .cmd = NL80211_CMD_SET_INTERFACE,
6298 .doit = nl80211_set_interface,
6299 .policy = nl80211_policy,
6300 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006301 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6302 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006303 },
6304 {
6305 .cmd = NL80211_CMD_NEW_INTERFACE,
6306 .doit = nl80211_new_interface,
6307 .policy = nl80211_policy,
6308 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006309 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6310 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006311 },
6312 {
6313 .cmd = NL80211_CMD_DEL_INTERFACE,
6314 .doit = nl80211_del_interface,
6315 .policy = nl80211_policy,
6316 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006317 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6318 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006319 },
Johannes Berg41ade002007-12-19 02:03:29 +01006320 {
6321 .cmd = NL80211_CMD_GET_KEY,
6322 .doit = nl80211_get_key,
6323 .policy = nl80211_policy,
6324 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006325 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6326 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006327 },
6328 {
6329 .cmd = NL80211_CMD_SET_KEY,
6330 .doit = nl80211_set_key,
6331 .policy = nl80211_policy,
6332 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006333 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006334 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006335 },
6336 {
6337 .cmd = NL80211_CMD_NEW_KEY,
6338 .doit = nl80211_new_key,
6339 .policy = nl80211_policy,
6340 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006342 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006343 },
6344 {
6345 .cmd = NL80211_CMD_DEL_KEY,
6346 .doit = nl80211_del_key,
6347 .policy = nl80211_policy,
6348 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006349 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006350 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006351 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006352 {
6353 .cmd = NL80211_CMD_SET_BEACON,
6354 .policy = nl80211_policy,
6355 .flags = GENL_ADMIN_PERM,
6356 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006357 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6358 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006359 },
6360 {
6361 .cmd = NL80211_CMD_NEW_BEACON,
6362 .policy = nl80211_policy,
6363 .flags = GENL_ADMIN_PERM,
6364 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006365 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6366 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006367 },
6368 {
6369 .cmd = NL80211_CMD_DEL_BEACON,
6370 .policy = nl80211_policy,
6371 .flags = GENL_ADMIN_PERM,
6372 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006373 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6374 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006375 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006376 {
6377 .cmd = NL80211_CMD_GET_STATION,
6378 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006379 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006380 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006381 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6382 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006383 },
6384 {
6385 .cmd = NL80211_CMD_SET_STATION,
6386 .doit = nl80211_set_station,
6387 .policy = nl80211_policy,
6388 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006389 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6390 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006391 },
6392 {
6393 .cmd = NL80211_CMD_NEW_STATION,
6394 .doit = nl80211_new_station,
6395 .policy = nl80211_policy,
6396 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006397 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006398 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006399 },
6400 {
6401 .cmd = NL80211_CMD_DEL_STATION,
6402 .doit = nl80211_del_station,
6403 .policy = nl80211_policy,
6404 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006405 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6406 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006407 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006408 {
6409 .cmd = NL80211_CMD_GET_MPATH,
6410 .doit = nl80211_get_mpath,
6411 .dumpit = nl80211_dump_mpath,
6412 .policy = nl80211_policy,
6413 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006414 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006415 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006416 },
6417 {
6418 .cmd = NL80211_CMD_SET_MPATH,
6419 .doit = nl80211_set_mpath,
6420 .policy = nl80211_policy,
6421 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006422 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006423 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006424 },
6425 {
6426 .cmd = NL80211_CMD_NEW_MPATH,
6427 .doit = nl80211_new_mpath,
6428 .policy = nl80211_policy,
6429 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006430 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006431 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006432 },
6433 {
6434 .cmd = NL80211_CMD_DEL_MPATH,
6435 .doit = nl80211_del_mpath,
6436 .policy = nl80211_policy,
6437 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006438 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6439 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006440 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006441 {
6442 .cmd = NL80211_CMD_SET_BSS,
6443 .doit = nl80211_set_bss,
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,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006448 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006449 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006450 .cmd = NL80211_CMD_GET_REG,
6451 .doit = nl80211_get_reg,
6452 .policy = nl80211_policy,
6453 /* can be retrieved by unprivileged users */
6454 },
6455 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006456 .cmd = NL80211_CMD_SET_REG,
6457 .doit = nl80211_set_reg,
6458 .policy = nl80211_policy,
6459 .flags = GENL_ADMIN_PERM,
6460 },
6461 {
6462 .cmd = NL80211_CMD_REQ_SET_REG,
6463 .doit = nl80211_req_set_reg,
6464 .policy = nl80211_policy,
6465 .flags = GENL_ADMIN_PERM,
6466 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006467 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006468 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6469 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006470 .policy = nl80211_policy,
6471 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006472 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6473 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006474 },
6475 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006476 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6477 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006478 .policy = nl80211_policy,
6479 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006480 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006481 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006482 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006483 {
Johannes Berg2a519312009-02-10 21:25:55 +01006484 .cmd = NL80211_CMD_TRIGGER_SCAN,
6485 .doit = nl80211_trigger_scan,
6486 .policy = nl80211_policy,
6487 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006488 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006489 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006490 },
6491 {
6492 .cmd = NL80211_CMD_GET_SCAN,
6493 .policy = nl80211_policy,
6494 .dumpit = nl80211_dump_scan,
6495 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006496 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006497 .cmd = NL80211_CMD_START_SCHED_SCAN,
6498 .doit = nl80211_start_sched_scan,
6499 .policy = nl80211_policy,
6500 .flags = GENL_ADMIN_PERM,
6501 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6502 NL80211_FLAG_NEED_RTNL,
6503 },
6504 {
6505 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6506 .doit = nl80211_stop_sched_scan,
6507 .policy = nl80211_policy,
6508 .flags = GENL_ADMIN_PERM,
6509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6510 NL80211_FLAG_NEED_RTNL,
6511 },
6512 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006513 .cmd = NL80211_CMD_AUTHENTICATE,
6514 .doit = nl80211_authenticate,
6515 .policy = nl80211_policy,
6516 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006517 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006518 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006519 },
6520 {
6521 .cmd = NL80211_CMD_ASSOCIATE,
6522 .doit = nl80211_associate,
6523 .policy = nl80211_policy,
6524 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006526 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006527 },
6528 {
6529 .cmd = NL80211_CMD_DEAUTHENTICATE,
6530 .doit = nl80211_deauthenticate,
6531 .policy = nl80211_policy,
6532 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006533 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006534 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006535 },
6536 {
6537 .cmd = NL80211_CMD_DISASSOCIATE,
6538 .doit = nl80211_disassociate,
6539 .policy = nl80211_policy,
6540 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006541 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006542 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006543 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006544 {
6545 .cmd = NL80211_CMD_JOIN_IBSS,
6546 .doit = nl80211_join_ibss,
6547 .policy = nl80211_policy,
6548 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006549 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006550 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006551 },
6552 {
6553 .cmd = NL80211_CMD_LEAVE_IBSS,
6554 .doit = nl80211_leave_ibss,
6555 .policy = nl80211_policy,
6556 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006557 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006558 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006559 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006560#ifdef CONFIG_NL80211_TESTMODE
6561 {
6562 .cmd = NL80211_CMD_TESTMODE,
6563 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006564 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006565 .policy = nl80211_policy,
6566 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006567 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6568 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006569 },
6570#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006571 {
6572 .cmd = NL80211_CMD_CONNECT,
6573 .doit = nl80211_connect,
6574 .policy = nl80211_policy,
6575 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006576 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006577 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006578 },
6579 {
6580 .cmd = NL80211_CMD_DISCONNECT,
6581 .doit = nl80211_disconnect,
6582 .policy = nl80211_policy,
6583 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006584 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006585 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006586 },
Johannes Berg463d0182009-07-14 00:33:35 +02006587 {
6588 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6589 .doit = nl80211_wiphy_netns,
6590 .policy = nl80211_policy,
6591 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006592 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6593 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006594 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006595 {
6596 .cmd = NL80211_CMD_GET_SURVEY,
6597 .policy = nl80211_policy,
6598 .dumpit = nl80211_dump_survey,
6599 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006600 {
6601 .cmd = NL80211_CMD_SET_PMKSA,
6602 .doit = nl80211_setdel_pmksa,
6603 .policy = nl80211_policy,
6604 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006605 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6606 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006607 },
6608 {
6609 .cmd = NL80211_CMD_DEL_PMKSA,
6610 .doit = nl80211_setdel_pmksa,
6611 .policy = nl80211_policy,
6612 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006613 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6614 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006615 },
6616 {
6617 .cmd = NL80211_CMD_FLUSH_PMKSA,
6618 .doit = nl80211_flush_pmksa,
6619 .policy = nl80211_policy,
6620 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006621 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6622 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006623 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006624 {
6625 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6626 .doit = nl80211_remain_on_channel,
6627 .policy = nl80211_policy,
6628 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006629 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006630 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006631 },
6632 {
6633 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6634 .doit = nl80211_cancel_remain_on_channel,
6635 .policy = nl80211_policy,
6636 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006638 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006639 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006640 {
6641 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6642 .doit = nl80211_set_tx_bitrate_mask,
6643 .policy = nl80211_policy,
6644 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006645 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6646 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006647 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006648 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006649 .cmd = NL80211_CMD_REGISTER_FRAME,
6650 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006651 .policy = nl80211_policy,
6652 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006653 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6654 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006655 },
6656 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006657 .cmd = NL80211_CMD_FRAME,
6658 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006659 .policy = nl80211_policy,
6660 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006661 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006662 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006663 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006664 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006665 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6666 .doit = nl80211_tx_mgmt_cancel_wait,
6667 .policy = nl80211_policy,
6668 .flags = GENL_ADMIN_PERM,
6669 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6670 NL80211_FLAG_NEED_RTNL,
6671 },
6672 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006673 .cmd = NL80211_CMD_SET_POWER_SAVE,
6674 .doit = nl80211_set_power_save,
6675 .policy = nl80211_policy,
6676 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006677 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6678 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006679 },
6680 {
6681 .cmd = NL80211_CMD_GET_POWER_SAVE,
6682 .doit = nl80211_get_power_save,
6683 .policy = nl80211_policy,
6684 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006685 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6686 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006687 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006688 {
6689 .cmd = NL80211_CMD_SET_CQM,
6690 .doit = nl80211_set_cqm,
6691 .policy = nl80211_policy,
6692 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006693 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6694 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006695 },
Johannes Bergf444de02010-05-05 15:25:02 +02006696 {
6697 .cmd = NL80211_CMD_SET_CHANNEL,
6698 .doit = nl80211_set_channel,
6699 .policy = nl80211_policy,
6700 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006701 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6702 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006703 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006704 {
6705 .cmd = NL80211_CMD_SET_WDS_PEER,
6706 .doit = nl80211_set_wds_peer,
6707 .policy = nl80211_policy,
6708 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006709 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6710 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006711 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006712 {
6713 .cmd = NL80211_CMD_JOIN_MESH,
6714 .doit = nl80211_join_mesh,
6715 .policy = nl80211_policy,
6716 .flags = GENL_ADMIN_PERM,
6717 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6718 NL80211_FLAG_NEED_RTNL,
6719 },
6720 {
6721 .cmd = NL80211_CMD_LEAVE_MESH,
6722 .doit = nl80211_leave_mesh,
6723 .policy = nl80211_policy,
6724 .flags = GENL_ADMIN_PERM,
6725 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6726 NL80211_FLAG_NEED_RTNL,
6727 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006728 {
6729 .cmd = NL80211_CMD_GET_WOWLAN,
6730 .doit = nl80211_get_wowlan,
6731 .policy = nl80211_policy,
6732 /* can be retrieved by unprivileged users */
6733 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6734 NL80211_FLAG_NEED_RTNL,
6735 },
6736 {
6737 .cmd = NL80211_CMD_SET_WOWLAN,
6738 .doit = nl80211_set_wowlan,
6739 .policy = nl80211_policy,
6740 .flags = GENL_ADMIN_PERM,
6741 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6742 NL80211_FLAG_NEED_RTNL,
6743 },
Johannes Berge5497d72011-07-05 16:35:40 +02006744 {
6745 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6746 .doit = nl80211_set_rekey_data,
6747 .policy = nl80211_policy,
6748 .flags = GENL_ADMIN_PERM,
6749 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6750 NL80211_FLAG_NEED_RTNL,
6751 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006752 {
6753 .cmd = NL80211_CMD_TDLS_MGMT,
6754 .doit = nl80211_tdls_mgmt,
6755 .policy = nl80211_policy,
6756 .flags = GENL_ADMIN_PERM,
6757 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6758 NL80211_FLAG_NEED_RTNL,
6759 },
6760 {
6761 .cmd = NL80211_CMD_TDLS_OPER,
6762 .doit = nl80211_tdls_oper,
6763 .policy = nl80211_policy,
6764 .flags = GENL_ADMIN_PERM,
6765 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6766 NL80211_FLAG_NEED_RTNL,
6767 },
Johannes Berg28946da2011-11-04 11:18:12 +01006768 {
6769 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6770 .doit = nl80211_register_unexpected_frame,
6771 .policy = nl80211_policy,
6772 .flags = GENL_ADMIN_PERM,
6773 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6774 NL80211_FLAG_NEED_RTNL,
6775 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006776 {
6777 .cmd = NL80211_CMD_PROBE_CLIENT,
6778 .doit = nl80211_probe_client,
6779 .policy = nl80211_policy,
6780 .flags = GENL_ADMIN_PERM,
6781 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6782 NL80211_FLAG_NEED_RTNL,
6783 },
Johannes Berg5e760232011-11-04 11:18:17 +01006784 {
6785 .cmd = NL80211_CMD_REGISTER_BEACONS,
6786 .doit = nl80211_register_beacons,
6787 .policy = nl80211_policy,
6788 .flags = GENL_ADMIN_PERM,
6789 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6790 NL80211_FLAG_NEED_RTNL,
6791 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006792 {
6793 .cmd = NL80211_CMD_SET_NOACK_MAP,
6794 .doit = nl80211_set_noack_map,
6795 .policy = nl80211_policy,
6796 .flags = GENL_ADMIN_PERM,
6797 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6798 NL80211_FLAG_NEED_RTNL,
6799 },
6800
Johannes Berg55682962007-09-20 13:09:35 -04006801};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006802
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006803static struct genl_multicast_group nl80211_mlme_mcgrp = {
6804 .name = "mlme",
6805};
Johannes Berg55682962007-09-20 13:09:35 -04006806
6807/* multicast groups */
6808static struct genl_multicast_group nl80211_config_mcgrp = {
6809 .name = "config",
6810};
Johannes Berg2a519312009-02-10 21:25:55 +01006811static struct genl_multicast_group nl80211_scan_mcgrp = {
6812 .name = "scan",
6813};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006814static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6815 .name = "regulatory",
6816};
Johannes Berg55682962007-09-20 13:09:35 -04006817
6818/* notification functions */
6819
6820void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6821{
6822 struct sk_buff *msg;
6823
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006824 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006825 if (!msg)
6826 return;
6827
6828 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6829 nlmsg_free(msg);
6830 return;
6831 }
6832
Johannes Berg463d0182009-07-14 00:33:35 +02006833 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6834 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006835}
6836
Johannes Berg362a4152009-05-24 16:43:15 +02006837static int nl80211_add_scan_req(struct sk_buff *msg,
6838 struct cfg80211_registered_device *rdev)
6839{
6840 struct cfg80211_scan_request *req = rdev->scan_req;
6841 struct nlattr *nest;
6842 int i;
6843
Johannes Berg667503dd2009-07-07 03:56:11 +02006844 ASSERT_RDEV_LOCK(rdev);
6845
Johannes Berg362a4152009-05-24 16:43:15 +02006846 if (WARN_ON(!req))
6847 return 0;
6848
6849 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6850 if (!nest)
6851 goto nla_put_failure;
6852 for (i = 0; i < req->n_ssids; i++)
6853 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6854 nla_nest_end(msg, nest);
6855
6856 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6857 if (!nest)
6858 goto nla_put_failure;
6859 for (i = 0; i < req->n_channels; i++)
6860 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6861 nla_nest_end(msg, nest);
6862
6863 if (req->ie)
6864 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6865
6866 return 0;
6867 nla_put_failure:
6868 return -ENOBUFS;
6869}
6870
Johannes Berga538e2d2009-06-16 19:56:42 +02006871static int nl80211_send_scan_msg(struct sk_buff *msg,
6872 struct cfg80211_registered_device *rdev,
6873 struct net_device *netdev,
6874 u32 pid, u32 seq, int flags,
6875 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006876{
6877 void *hdr;
6878
6879 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6880 if (!hdr)
6881 return -1;
6882
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006883 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006884 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6885
Johannes Berg362a4152009-05-24 16:43:15 +02006886 /* ignore errors and send incomplete event anyway */
6887 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006888
6889 return genlmsg_end(msg, hdr);
6890
6891 nla_put_failure:
6892 genlmsg_cancel(msg, hdr);
6893 return -EMSGSIZE;
6894}
6895
Luciano Coelho807f8a82011-05-11 17:09:35 +03006896static int
6897nl80211_send_sched_scan_msg(struct sk_buff *msg,
6898 struct cfg80211_registered_device *rdev,
6899 struct net_device *netdev,
6900 u32 pid, u32 seq, int flags, u32 cmd)
6901{
6902 void *hdr;
6903
6904 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6905 if (!hdr)
6906 return -1;
6907
6908 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6909 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6910
6911 return genlmsg_end(msg, hdr);
6912
6913 nla_put_failure:
6914 genlmsg_cancel(msg, hdr);
6915 return -EMSGSIZE;
6916}
6917
Johannes Berga538e2d2009-06-16 19:56:42 +02006918void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6919 struct net_device *netdev)
6920{
6921 struct sk_buff *msg;
6922
6923 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6924 if (!msg)
6925 return;
6926
6927 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6928 NL80211_CMD_TRIGGER_SCAN) < 0) {
6929 nlmsg_free(msg);
6930 return;
6931 }
6932
Johannes Berg463d0182009-07-14 00:33:35 +02006933 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6934 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006935}
6936
Johannes Berg2a519312009-02-10 21:25:55 +01006937void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6938 struct net_device *netdev)
6939{
6940 struct sk_buff *msg;
6941
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006943 if (!msg)
6944 return;
6945
Johannes Berga538e2d2009-06-16 19:56:42 +02006946 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6947 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006948 nlmsg_free(msg);
6949 return;
6950 }
6951
Johannes Berg463d0182009-07-14 00:33:35 +02006952 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6953 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006954}
6955
6956void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6957 struct net_device *netdev)
6958{
6959 struct sk_buff *msg;
6960
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006962 if (!msg)
6963 return;
6964
Johannes Berga538e2d2009-06-16 19:56:42 +02006965 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6966 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006967 nlmsg_free(msg);
6968 return;
6969 }
6970
Johannes Berg463d0182009-07-14 00:33:35 +02006971 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6972 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006973}
6974
Luciano Coelho807f8a82011-05-11 17:09:35 +03006975void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6976 struct net_device *netdev)
6977{
6978 struct sk_buff *msg;
6979
6980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6981 if (!msg)
6982 return;
6983
6984 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6985 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6986 nlmsg_free(msg);
6987 return;
6988 }
6989
6990 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6991 nl80211_scan_mcgrp.id, GFP_KERNEL);
6992}
6993
6994void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6995 struct net_device *netdev, u32 cmd)
6996{
6997 struct sk_buff *msg;
6998
6999 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7000 if (!msg)
7001 return;
7002
7003 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7004 nlmsg_free(msg);
7005 return;
7006 }
7007
7008 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7009 nl80211_scan_mcgrp.id, GFP_KERNEL);
7010}
7011
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007012/*
7013 * This can happen on global regulatory changes or device specific settings
7014 * based on custom world regulatory domains.
7015 */
7016void nl80211_send_reg_change_event(struct regulatory_request *request)
7017{
7018 struct sk_buff *msg;
7019 void *hdr;
7020
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007021 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007022 if (!msg)
7023 return;
7024
7025 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7026 if (!hdr) {
7027 nlmsg_free(msg);
7028 return;
7029 }
7030
7031 /* Userspace can always count this one always being set */
7032 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
7033
7034 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
7035 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7036 NL80211_REGDOM_TYPE_WORLD);
7037 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
7038 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7039 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
7040 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7041 request->intersect)
7042 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7043 NL80211_REGDOM_TYPE_INTERSECTION);
7044 else {
7045 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7046 NL80211_REGDOM_TYPE_COUNTRY);
7047 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
7048 }
7049
7050 if (wiphy_idx_valid(request->wiphy_idx))
7051 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
7052
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007053 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007054
Johannes Bergbc43b282009-07-25 10:54:13 +02007055 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007056 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007057 GFP_ATOMIC);
7058 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007059
7060 return;
7061
7062nla_put_failure:
7063 genlmsg_cancel(msg, hdr);
7064 nlmsg_free(msg);
7065}
7066
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007067static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7068 struct net_device *netdev,
7069 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007070 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007071{
7072 struct sk_buff *msg;
7073 void *hdr;
7074
Johannes Berge6d6e342009-07-01 21:26:47 +02007075 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007076 if (!msg)
7077 return;
7078
7079 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7080 if (!hdr) {
7081 nlmsg_free(msg);
7082 return;
7083 }
7084
7085 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7086 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7087 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7088
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007089 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007090
Johannes Berg463d0182009-07-14 00:33:35 +02007091 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7092 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007093 return;
7094
7095 nla_put_failure:
7096 genlmsg_cancel(msg, hdr);
7097 nlmsg_free(msg);
7098}
7099
7100void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007101 struct net_device *netdev, const u8 *buf,
7102 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007103{
7104 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007105 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007106}
7107
7108void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7109 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007110 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007111{
Johannes Berge6d6e342009-07-01 21:26:47 +02007112 nl80211_send_mlme_event(rdev, netdev, buf, len,
7113 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007114}
7115
Jouni Malinen53b46b82009-03-27 20:53:56 +02007116void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007117 struct net_device *netdev, const u8 *buf,
7118 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007119{
7120 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007121 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007122}
7123
Jouni Malinen53b46b82009-03-27 20:53:56 +02007124void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7125 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007126 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007127{
7128 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007129 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007130}
7131
Jouni Malinencf4e5942010-12-16 00:52:40 +02007132void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7133 struct net_device *netdev, const u8 *buf,
7134 size_t len, gfp_t gfp)
7135{
7136 nl80211_send_mlme_event(rdev, netdev, buf, len,
7137 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7138}
7139
7140void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7141 struct net_device *netdev, const u8 *buf,
7142 size_t len, gfp_t gfp)
7143{
7144 nl80211_send_mlme_event(rdev, netdev, buf, len,
7145 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7146}
7147
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007148static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7149 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007150 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007151{
7152 struct sk_buff *msg;
7153 void *hdr;
7154
Johannes Berge6d6e342009-07-01 21:26:47 +02007155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007156 if (!msg)
7157 return;
7158
7159 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7160 if (!hdr) {
7161 nlmsg_free(msg);
7162 return;
7163 }
7164
7165 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7167 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7168 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7169
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007170 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007171
Johannes Berg463d0182009-07-14 00:33:35 +02007172 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7173 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007174 return;
7175
7176 nla_put_failure:
7177 genlmsg_cancel(msg, hdr);
7178 nlmsg_free(msg);
7179}
7180
7181void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007182 struct net_device *netdev, const u8 *addr,
7183 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007184{
7185 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007186 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007187}
7188
7189void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007190 struct net_device *netdev, const u8 *addr,
7191 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007192{
Johannes Berge6d6e342009-07-01 21:26:47 +02007193 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7194 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007195}
7196
Samuel Ortizb23aa672009-07-01 21:26:54 +02007197void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7198 struct net_device *netdev, const u8 *bssid,
7199 const u8 *req_ie, size_t req_ie_len,
7200 const u8 *resp_ie, size_t resp_ie_len,
7201 u16 status, gfp_t gfp)
7202{
7203 struct sk_buff *msg;
7204 void *hdr;
7205
7206 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7207 if (!msg)
7208 return;
7209
7210 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7211 if (!hdr) {
7212 nlmsg_free(msg);
7213 return;
7214 }
7215
7216 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7217 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7218 if (bssid)
7219 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7220 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7221 if (req_ie)
7222 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7223 if (resp_ie)
7224 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7225
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007226 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007227
Johannes Berg463d0182009-07-14 00:33:35 +02007228 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7229 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007230 return;
7231
7232 nla_put_failure:
7233 genlmsg_cancel(msg, hdr);
7234 nlmsg_free(msg);
7235
7236}
7237
7238void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7239 struct net_device *netdev, const u8 *bssid,
7240 const u8 *req_ie, size_t req_ie_len,
7241 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7242{
7243 struct sk_buff *msg;
7244 void *hdr;
7245
7246 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7247 if (!msg)
7248 return;
7249
7250 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7251 if (!hdr) {
7252 nlmsg_free(msg);
7253 return;
7254 }
7255
7256 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7257 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7258 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7259 if (req_ie)
7260 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7261 if (resp_ie)
7262 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7263
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007264 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007265
Johannes Berg463d0182009-07-14 00:33:35 +02007266 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7267 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007268 return;
7269
7270 nla_put_failure:
7271 genlmsg_cancel(msg, hdr);
7272 nlmsg_free(msg);
7273
7274}
7275
7276void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7277 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007278 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007279{
7280 struct sk_buff *msg;
7281 void *hdr;
7282
Johannes Berg667503dd2009-07-07 03:56:11 +02007283 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007284 if (!msg)
7285 return;
7286
7287 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7288 if (!hdr) {
7289 nlmsg_free(msg);
7290 return;
7291 }
7292
7293 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7294 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7295 if (from_ap && reason)
7296 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7297 if (from_ap)
7298 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7299 if (ie)
7300 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7301
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007302 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007303
Johannes Berg463d0182009-07-14 00:33:35 +02007304 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7305 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007306 return;
7307
7308 nla_put_failure:
7309 genlmsg_cancel(msg, hdr);
7310 nlmsg_free(msg);
7311
7312}
7313
Johannes Berg04a773a2009-04-19 21:24:32 +02007314void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7315 struct net_device *netdev, const u8 *bssid,
7316 gfp_t gfp)
7317{
7318 struct sk_buff *msg;
7319 void *hdr;
7320
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007321 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007322 if (!msg)
7323 return;
7324
7325 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7326 if (!hdr) {
7327 nlmsg_free(msg);
7328 return;
7329 }
7330
7331 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7332 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7333 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7334
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007335 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007336
Johannes Berg463d0182009-07-14 00:33:35 +02007337 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7338 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007339 return;
7340
7341 nla_put_failure:
7342 genlmsg_cancel(msg, hdr);
7343 nlmsg_free(msg);
7344}
7345
Javier Cardonac93b5e72011-04-07 15:08:34 -07007346void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7347 struct net_device *netdev,
7348 const u8 *macaddr, const u8* ie, u8 ie_len,
7349 gfp_t gfp)
7350{
7351 struct sk_buff *msg;
7352 void *hdr;
7353
7354 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7355 if (!msg)
7356 return;
7357
7358 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7359 if (!hdr) {
7360 nlmsg_free(msg);
7361 return;
7362 }
7363
7364 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7365 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7366 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7367 if (ie_len && ie)
7368 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7369
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007370 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007371
7372 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7373 nl80211_mlme_mcgrp.id, gfp);
7374 return;
7375
7376 nla_put_failure:
7377 genlmsg_cancel(msg, hdr);
7378 nlmsg_free(msg);
7379}
7380
Jouni Malinena3b8b052009-03-27 21:59:49 +02007381void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7382 struct net_device *netdev, const u8 *addr,
7383 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007384 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007385{
7386 struct sk_buff *msg;
7387 void *hdr;
7388
Johannes Berge6d6e342009-07-01 21:26:47 +02007389 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007390 if (!msg)
7391 return;
7392
7393 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7394 if (!hdr) {
7395 nlmsg_free(msg);
7396 return;
7397 }
7398
7399 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7400 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7401 if (addr)
7402 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7403 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007404 if (key_id != -1)
7405 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007406 if (tsc)
7407 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7408
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007409 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007410
Johannes Berg463d0182009-07-14 00:33:35 +02007411 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7412 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007413 return;
7414
7415 nla_put_failure:
7416 genlmsg_cancel(msg, hdr);
7417 nlmsg_free(msg);
7418}
7419
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007420void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7421 struct ieee80211_channel *channel_before,
7422 struct ieee80211_channel *channel_after)
7423{
7424 struct sk_buff *msg;
7425 void *hdr;
7426 struct nlattr *nl_freq;
7427
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007429 if (!msg)
7430 return;
7431
7432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7433 if (!hdr) {
7434 nlmsg_free(msg);
7435 return;
7436 }
7437
7438 /*
7439 * Since we are applying the beacon hint to a wiphy we know its
7440 * wiphy_idx is valid
7441 */
7442 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7443
7444 /* Before */
7445 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7446 if (!nl_freq)
7447 goto nla_put_failure;
7448 if (nl80211_msg_put_channel(msg, channel_before))
7449 goto nla_put_failure;
7450 nla_nest_end(msg, nl_freq);
7451
7452 /* After */
7453 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7454 if (!nl_freq)
7455 goto nla_put_failure;
7456 if (nl80211_msg_put_channel(msg, channel_after))
7457 goto nla_put_failure;
7458 nla_nest_end(msg, nl_freq);
7459
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007460 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007461
Johannes Berg463d0182009-07-14 00:33:35 +02007462 rcu_read_lock();
7463 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7464 GFP_ATOMIC);
7465 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007466
7467 return;
7468
7469nla_put_failure:
7470 genlmsg_cancel(msg, hdr);
7471 nlmsg_free(msg);
7472}
7473
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007474static void nl80211_send_remain_on_chan_event(
7475 int cmd, struct cfg80211_registered_device *rdev,
7476 struct net_device *netdev, u64 cookie,
7477 struct ieee80211_channel *chan,
7478 enum nl80211_channel_type channel_type,
7479 unsigned int duration, gfp_t gfp)
7480{
7481 struct sk_buff *msg;
7482 void *hdr;
7483
7484 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7485 if (!msg)
7486 return;
7487
7488 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7489 if (!hdr) {
7490 nlmsg_free(msg);
7491 return;
7492 }
7493
7494 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7495 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7496 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7497 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7498 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7499
7500 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7501 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7502
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007503 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007504
7505 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7506 nl80211_mlme_mcgrp.id, gfp);
7507 return;
7508
7509 nla_put_failure:
7510 genlmsg_cancel(msg, hdr);
7511 nlmsg_free(msg);
7512}
7513
7514void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7515 struct net_device *netdev, u64 cookie,
7516 struct ieee80211_channel *chan,
7517 enum nl80211_channel_type channel_type,
7518 unsigned int duration, gfp_t gfp)
7519{
7520 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7521 rdev, netdev, cookie, chan,
7522 channel_type, duration, gfp);
7523}
7524
7525void nl80211_send_remain_on_channel_cancel(
7526 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7527 u64 cookie, struct ieee80211_channel *chan,
7528 enum nl80211_channel_type channel_type, gfp_t gfp)
7529{
7530 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7531 rdev, netdev, cookie, chan,
7532 channel_type, 0, gfp);
7533}
7534
Johannes Berg98b62182009-12-23 13:15:44 +01007535void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7536 struct net_device *dev, const u8 *mac_addr,
7537 struct station_info *sinfo, gfp_t gfp)
7538{
7539 struct sk_buff *msg;
7540
7541 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7542 if (!msg)
7543 return;
7544
7545 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7546 nlmsg_free(msg);
7547 return;
7548 }
7549
7550 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7551 nl80211_mlme_mcgrp.id, gfp);
7552}
7553
Jouni Malinenec15e682011-03-23 15:29:52 +02007554void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7555 struct net_device *dev, const u8 *mac_addr,
7556 gfp_t gfp)
7557{
7558 struct sk_buff *msg;
7559 void *hdr;
7560
7561 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7562 if (!msg)
7563 return;
7564
7565 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7566 if (!hdr) {
7567 nlmsg_free(msg);
7568 return;
7569 }
7570
7571 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7572 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7573
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007574 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007575
7576 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7577 nl80211_mlme_mcgrp.id, gfp);
7578 return;
7579
7580 nla_put_failure:
7581 genlmsg_cancel(msg, hdr);
7582 nlmsg_free(msg);
7583}
7584
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007585static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7586 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007587{
7588 struct wireless_dev *wdev = dev->ieee80211_ptr;
7589 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7590 struct sk_buff *msg;
7591 void *hdr;
7592 int err;
7593 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7594
7595 if (!nlpid)
7596 return false;
7597
7598 msg = nlmsg_new(100, gfp);
7599 if (!msg)
7600 return true;
7601
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007602 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007603 if (!hdr) {
7604 nlmsg_free(msg);
7605 return true;
7606 }
7607
7608 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7609 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7610 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7611
7612 err = genlmsg_end(msg, hdr);
7613 if (err < 0) {
7614 nlmsg_free(msg);
7615 return true;
7616 }
7617
7618 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7619 return true;
7620
7621 nla_put_failure:
7622 genlmsg_cancel(msg, hdr);
7623 nlmsg_free(msg);
7624 return true;
7625}
7626
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007627bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7628{
7629 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7630 addr, gfp);
7631}
7632
7633bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7634 const u8 *addr, gfp_t gfp)
7635{
7636 return __nl80211_unexpected_frame(dev,
7637 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7638 addr, gfp);
7639}
7640
Johannes Berg2e161f72010-08-12 15:38:38 +02007641int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7642 struct net_device *netdev, u32 nlpid,
7643 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007644{
7645 struct sk_buff *msg;
7646 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007647
7648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7649 if (!msg)
7650 return -ENOMEM;
7651
Johannes Berg2e161f72010-08-12 15:38:38 +02007652 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007653 if (!hdr) {
7654 nlmsg_free(msg);
7655 return -ENOMEM;
7656 }
7657
7658 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, freq);
7661 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7662
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007663 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007664
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007665 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007666
7667 nla_put_failure:
7668 genlmsg_cancel(msg, hdr);
7669 nlmsg_free(msg);
7670 return -ENOBUFS;
7671}
7672
Johannes Berg2e161f72010-08-12 15:38:38 +02007673void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7674 struct net_device *netdev, u64 cookie,
7675 const u8 *buf, size_t len, bool ack,
7676 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007677{
7678 struct sk_buff *msg;
7679 void *hdr;
7680
7681 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7682 if (!msg)
7683 return;
7684
Johannes Berg2e161f72010-08-12 15:38:38 +02007685 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007686 if (!hdr) {
7687 nlmsg_free(msg);
7688 return;
7689 }
7690
7691 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7692 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7693 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7694 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7695 if (ack)
7696 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7697
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007698 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007699
7700 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7701 return;
7702
7703 nla_put_failure:
7704 genlmsg_cancel(msg, hdr);
7705 nlmsg_free(msg);
7706}
7707
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007708void
7709nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7710 struct net_device *netdev,
7711 enum nl80211_cqm_rssi_threshold_event rssi_event,
7712 gfp_t gfp)
7713{
7714 struct sk_buff *msg;
7715 struct nlattr *pinfoattr;
7716 void *hdr;
7717
7718 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7719 if (!msg)
7720 return;
7721
7722 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7723 if (!hdr) {
7724 nlmsg_free(msg);
7725 return;
7726 }
7727
7728 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7729 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7730
7731 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7732 if (!pinfoattr)
7733 goto nla_put_failure;
7734
7735 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7736 rssi_event);
7737
7738 nla_nest_end(msg, pinfoattr);
7739
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007740 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007741
7742 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7743 nl80211_mlme_mcgrp.id, gfp);
7744 return;
7745
7746 nla_put_failure:
7747 genlmsg_cancel(msg, hdr);
7748 nlmsg_free(msg);
7749}
7750
Johannes Berge5497d72011-07-05 16:35:40 +02007751void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7752 struct net_device *netdev, const u8 *bssid,
7753 const u8 *replay_ctr, gfp_t gfp)
7754{
7755 struct sk_buff *msg;
7756 struct nlattr *rekey_attr;
7757 void *hdr;
7758
7759 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7760 if (!msg)
7761 return;
7762
7763 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7764 if (!hdr) {
7765 nlmsg_free(msg);
7766 return;
7767 }
7768
7769 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7770 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7771 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7772
7773 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7774 if (!rekey_attr)
7775 goto nla_put_failure;
7776
7777 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7778 NL80211_REPLAY_CTR_LEN, replay_ctr);
7779
7780 nla_nest_end(msg, rekey_attr);
7781
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007782 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007783
7784 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7785 nl80211_mlme_mcgrp.id, gfp);
7786 return;
7787
7788 nla_put_failure:
7789 genlmsg_cancel(msg, hdr);
7790 nlmsg_free(msg);
7791}
7792
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007793void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7794 struct net_device *netdev, int index,
7795 const u8 *bssid, bool preauth, gfp_t gfp)
7796{
7797 struct sk_buff *msg;
7798 struct nlattr *attr;
7799 void *hdr;
7800
7801 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7802 if (!msg)
7803 return;
7804
7805 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7806 if (!hdr) {
7807 nlmsg_free(msg);
7808 return;
7809 }
7810
7811 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7812 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7813
7814 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7815 if (!attr)
7816 goto nla_put_failure;
7817
7818 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7819 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7820 if (preauth)
7821 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7822
7823 nla_nest_end(msg, attr);
7824
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007825 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007826
7827 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7828 nl80211_mlme_mcgrp.id, gfp);
7829 return;
7830
7831 nla_put_failure:
7832 genlmsg_cancel(msg, hdr);
7833 nlmsg_free(msg);
7834}
7835
Johannes Bergc063dbf2010-11-24 08:10:05 +01007836void
7837nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7838 struct net_device *netdev, const u8 *peer,
7839 u32 num_packets, gfp_t gfp)
7840{
7841 struct sk_buff *msg;
7842 struct nlattr *pinfoattr;
7843 void *hdr;
7844
7845 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7846 if (!msg)
7847 return;
7848
7849 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7850 if (!hdr) {
7851 nlmsg_free(msg);
7852 return;
7853 }
7854
7855 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7857 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7858
7859 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7860 if (!pinfoattr)
7861 goto nla_put_failure;
7862
7863 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7864
7865 nla_nest_end(msg, pinfoattr);
7866
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007867 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007868
7869 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7870 nl80211_mlme_mcgrp.id, gfp);
7871 return;
7872
7873 nla_put_failure:
7874 genlmsg_cancel(msg, hdr);
7875 nlmsg_free(msg);
7876}
7877
Johannes Berg7f6cf312011-11-04 11:18:15 +01007878void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7879 u64 cookie, bool acked, gfp_t gfp)
7880{
7881 struct wireless_dev *wdev = dev->ieee80211_ptr;
7882 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7883 struct sk_buff *msg;
7884 void *hdr;
7885 int err;
7886
7887 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7888 if (!msg)
7889 return;
7890
7891 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7892 if (!hdr) {
7893 nlmsg_free(msg);
7894 return;
7895 }
7896
7897 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7898 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7899 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7900 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7901 if (acked)
7902 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7903
7904 err = genlmsg_end(msg, hdr);
7905 if (err < 0) {
7906 nlmsg_free(msg);
7907 return;
7908 }
7909
7910 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7911 nl80211_mlme_mcgrp.id, gfp);
7912 return;
7913
7914 nla_put_failure:
7915 genlmsg_cancel(msg, hdr);
7916 nlmsg_free(msg);
7917}
7918EXPORT_SYMBOL(cfg80211_probe_status);
7919
Johannes Berg5e760232011-11-04 11:18:17 +01007920void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7921 const u8 *frame, size_t len,
7922 int freq, gfp_t gfp)
7923{
7924 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7925 struct sk_buff *msg;
7926 void *hdr;
7927 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7928
7929 if (!nlpid)
7930 return;
7931
7932 msg = nlmsg_new(len + 100, gfp);
7933 if (!msg)
7934 return;
7935
7936 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7937 if (!hdr) {
7938 nlmsg_free(msg);
7939 return;
7940 }
7941
7942 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7943 if (freq)
7944 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7945 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7946
7947 genlmsg_end(msg, hdr);
7948
7949 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7950 return;
7951
7952 nla_put_failure:
7953 genlmsg_cancel(msg, hdr);
7954 nlmsg_free(msg);
7955}
7956EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7957
Jouni Malinen026331c2010-02-15 12:53:10 +02007958static int nl80211_netlink_notify(struct notifier_block * nb,
7959 unsigned long state,
7960 void *_notify)
7961{
7962 struct netlink_notify *notify = _notify;
7963 struct cfg80211_registered_device *rdev;
7964 struct wireless_dev *wdev;
7965
7966 if (state != NETLINK_URELEASE)
7967 return NOTIFY_DONE;
7968
7969 rcu_read_lock();
7970
Johannes Berg5e760232011-11-04 11:18:17 +01007971 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007972 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007973 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01007974 if (rdev->ap_beacons_nlpid == notify->pid)
7975 rdev->ap_beacons_nlpid = 0;
7976 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007977
7978 rcu_read_unlock();
7979
7980 return NOTIFY_DONE;
7981}
7982
7983static struct notifier_block nl80211_netlink_notifier = {
7984 .notifier_call = nl80211_netlink_notify,
7985};
7986
Johannes Berg55682962007-09-20 13:09:35 -04007987/* initialisation/exit functions */
7988
7989int nl80211_init(void)
7990{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007991 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007992
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007993 err = genl_register_family_with_ops(&nl80211_fam,
7994 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007995 if (err)
7996 return err;
7997
Johannes Berg55682962007-09-20 13:09:35 -04007998 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7999 if (err)
8000 goto err_out;
8001
Johannes Berg2a519312009-02-10 21:25:55 +01008002 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8003 if (err)
8004 goto err_out;
8005
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008006 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8007 if (err)
8008 goto err_out;
8009
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008010 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8011 if (err)
8012 goto err_out;
8013
Johannes Bergaff89a92009-07-01 21:26:51 +02008014#ifdef CONFIG_NL80211_TESTMODE
8015 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8016 if (err)
8017 goto err_out;
8018#endif
8019
Jouni Malinen026331c2010-02-15 12:53:10 +02008020 err = netlink_register_notifier(&nl80211_netlink_notifier);
8021 if (err)
8022 goto err_out;
8023
Johannes Berg55682962007-09-20 13:09:35 -04008024 return 0;
8025 err_out:
8026 genl_unregister_family(&nl80211_fam);
8027 return err;
8028}
8029
8030void nl80211_exit(void)
8031{
Jouni Malinen026331c2010-02-15 12:53:10 +02008032 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008033 genl_unregister_family(&nl80211_fam);
8034}