blob: afeea32e04ad9ea1a0caa1adeabe83b940bc0c42 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
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];
430 int err = nla_parse_nested(kdt,
431 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
432 tb[NL80211_KEY_DEFAULT_TYPES],
433 nl80211_key_default_policy);
434 if (err)
435 return err;
436
437 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
438 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
439 }
440
Johannes Bergb9454e82009-07-08 13:29:08 +0200441 return 0;
442}
443
444static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
445{
446 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
447 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
448 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
449 }
450
451 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
452 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
453 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
454 }
455
456 if (info->attrs[NL80211_ATTR_KEY_IDX])
457 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
458
459 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
460 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
461
462 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
463 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
464
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100465 if (k->def) {
466 k->def_uni = true;
467 k->def_multi = true;
468 }
469 if (k->defmgmt)
470 k->def_multi = true;
471
Johannes Berge31b8212010-10-05 19:39:30 +0200472 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
473 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
474 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
475 return -EINVAL;
476 }
477
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100478 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
479 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
480 int err = nla_parse_nested(
481 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
482 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
483 nl80211_key_default_policy);
484 if (err)
485 return err;
486
487 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
488 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
489 }
490
Johannes Bergb9454e82009-07-08 13:29:08 +0200491 return 0;
492}
493
494static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
495{
496 int err;
497
498 memset(k, 0, sizeof(*k));
499 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200500 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200501
502 if (info->attrs[NL80211_ATTR_KEY])
503 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
504 else
505 err = nl80211_parse_key_old(info, k);
506
507 if (err)
508 return err;
509
510 if (k->def && k->defmgmt)
511 return -EINVAL;
512
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100513 if (k->defmgmt) {
514 if (k->def_uni || !k->def_multi)
515 return -EINVAL;
516 }
517
Johannes Bergb9454e82009-07-08 13:29:08 +0200518 if (k->idx != -1) {
519 if (k->defmgmt) {
520 if (k->idx < 4 || k->idx > 5)
521 return -EINVAL;
522 } else if (k->def) {
523 if (k->idx < 0 || k->idx > 3)
524 return -EINVAL;
525 } else {
526 if (k->idx < 0 || k->idx > 5)
527 return -EINVAL;
528 }
529 }
530
531 return 0;
532}
533
Johannes Bergfffd0932009-07-08 14:22:54 +0200534static struct cfg80211_cached_keys *
535nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
536 struct nlattr *keys)
537{
538 struct key_parse parse;
539 struct nlattr *key;
540 struct cfg80211_cached_keys *result;
541 int rem, err, def = 0;
542
543 result = kzalloc(sizeof(*result), GFP_KERNEL);
544 if (!result)
545 return ERR_PTR(-ENOMEM);
546
547 result->def = -1;
548 result->defmgmt = -1;
549
550 nla_for_each_nested(key, keys, rem) {
551 memset(&parse, 0, sizeof(parse));
552 parse.idx = -1;
553
554 err = nl80211_parse_key_new(key, &parse);
555 if (err)
556 goto error;
557 err = -EINVAL;
558 if (!parse.p.key)
559 goto error;
560 if (parse.idx < 0 || parse.idx > 4)
561 goto error;
562 if (parse.def) {
563 if (def)
564 goto error;
565 def = 1;
566 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100567 if (!parse.def_uni || !parse.def_multi)
568 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200569 } else if (parse.defmgmt)
570 goto error;
571 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200572 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200573 if (err)
574 goto error;
575 result->params[parse.idx].cipher = parse.p.cipher;
576 result->params[parse.idx].key_len = parse.p.key_len;
577 result->params[parse.idx].key = result->data[parse.idx];
578 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
579 }
580
581 return result;
582 error:
583 kfree(result);
584 return ERR_PTR(err);
585}
586
587static int nl80211_key_allowed(struct wireless_dev *wdev)
588{
589 ASSERT_WDEV_LOCK(wdev);
590
Johannes Bergfffd0932009-07-08 14:22:54 +0200591 switch (wdev->iftype) {
592 case NL80211_IFTYPE_AP:
593 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200594 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700595 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200596 break;
597 case NL80211_IFTYPE_ADHOC:
598 if (!wdev->current_bss)
599 return -ENOLINK;
600 break;
601 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200602 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 if (wdev->sme_state != CFG80211_SME_CONNECTED)
604 return -ENOLINK;
605 break;
606 default:
607 return -EINVAL;
608 }
609
610 return 0;
611}
612
Johannes Berg7527a782011-05-13 10:58:57 +0200613static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
614{
615 struct nlattr *nl_modes = nla_nest_start(msg, attr);
616 int i;
617
618 if (!nl_modes)
619 goto nla_put_failure;
620
621 i = 0;
622 while (ifmodes) {
623 if (ifmodes & 1)
624 NLA_PUT_FLAG(msg, i);
625 ifmodes >>= 1;
626 i++;
627 }
628
629 nla_nest_end(msg, nl_modes);
630 return 0;
631
632nla_put_failure:
633 return -ENOBUFS;
634}
635
636static int nl80211_put_iface_combinations(struct wiphy *wiphy,
637 struct sk_buff *msg)
638{
639 struct nlattr *nl_combis;
640 int i, j;
641
642 nl_combis = nla_nest_start(msg,
643 NL80211_ATTR_INTERFACE_COMBINATIONS);
644 if (!nl_combis)
645 goto nla_put_failure;
646
647 for (i = 0; i < wiphy->n_iface_combinations; i++) {
648 const struct ieee80211_iface_combination *c;
649 struct nlattr *nl_combi, *nl_limits;
650
651 c = &wiphy->iface_combinations[i];
652
653 nl_combi = nla_nest_start(msg, i + 1);
654 if (!nl_combi)
655 goto nla_put_failure;
656
657 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
658 if (!nl_limits)
659 goto nla_put_failure;
660
661 for (j = 0; j < c->n_limits; j++) {
662 struct nlattr *nl_limit;
663
664 nl_limit = nla_nest_start(msg, j + 1);
665 if (!nl_limit)
666 goto nla_put_failure;
667 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
668 c->limits[j].max);
669 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
670 c->limits[j].types))
671 goto nla_put_failure;
672 nla_nest_end(msg, nl_limit);
673 }
674
675 nla_nest_end(msg, nl_limits);
676
677 if (c->beacon_int_infra_match)
678 NLA_PUT_FLAG(msg,
679 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
680 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
681 c->num_different_channels);
682 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
683 c->max_interfaces);
684
685 nla_nest_end(msg, nl_combi);
686 }
687
688 nla_nest_end(msg, nl_combis);
689
690 return 0;
691nla_put_failure:
692 return -ENOBUFS;
693}
694
Johannes Berg55682962007-09-20 13:09:35 -0400695static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
696 struct cfg80211_registered_device *dev)
697{
698 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100699 struct nlattr *nl_bands, *nl_band;
700 struct nlattr *nl_freqs, *nl_freq;
701 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100702 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100703 enum ieee80211_band band;
704 struct ieee80211_channel *chan;
705 struct ieee80211_rate *rate;
706 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200707 const struct ieee80211_txrx_stypes *mgmt_stypes =
708 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400709
710 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
711 if (!hdr)
712 return -1;
713
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500714 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400715 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200716
Johannes Bergf5ea9122009-08-07 16:17:38 +0200717 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
718 cfg80211_rdev_list_generation);
719
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200720 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
721 dev->wiphy.retry_short);
722 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
723 dev->wiphy.retry_long);
724 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
725 dev->wiphy.frag_threshold);
726 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
727 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100728 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
729 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100730 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
731 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300732 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
733 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200734 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
735 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300736 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
737 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300738 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
739 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100740
Johannes Berge31b8212010-10-05 19:39:30 +0200741 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
742 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700743 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
744 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300745 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
746 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530747 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
748 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300749 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
750 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
751 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
752 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530753
Johannes Berg25e47c12009-04-02 20:14:06 +0200754 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
755 sizeof(u32) * dev->wiphy.n_cipher_suites,
756 dev->wiphy.cipher_suites);
757
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100758 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
759 dev->wiphy.max_num_pmkids);
760
Johannes Bergc0692b82010-08-27 14:26:53 +0300761 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
762 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
763
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900764 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
765 dev->wiphy.available_antennas_tx);
766 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
767 dev->wiphy.available_antennas_rx);
768
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200769 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
770 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
771 dev->wiphy.probe_resp_offload);
772
Bruno Randolf7f531e02010-12-16 11:30:22 +0900773 if ((dev->wiphy.available_antennas_tx ||
774 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900775 u32 tx_ant = 0, rx_ant = 0;
776 int res;
777 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
778 if (!res) {
779 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
780 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
781 }
782 }
783
Johannes Berg7527a782011-05-13 10:58:57 +0200784 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
785 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700786 goto nla_put_failure;
787
Johannes Bergee688b002008-01-24 19:38:39 +0100788 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
789 if (!nl_bands)
790 goto nla_put_failure;
791
792 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
793 if (!dev->wiphy.bands[band])
794 continue;
795
796 nl_band = nla_nest_start(msg, band);
797 if (!nl_band)
798 goto nla_put_failure;
799
Johannes Bergd51626d2008-10-09 12:20:13 +0200800 /* add HT info */
801 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
802 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
803 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
804 &dev->wiphy.bands[band]->ht_cap.mcs);
805 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
806 dev->wiphy.bands[band]->ht_cap.cap);
807 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
808 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
809 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
810 dev->wiphy.bands[band]->ht_cap.ampdu_density);
811 }
812
Johannes Bergee688b002008-01-24 19:38:39 +0100813 /* add frequencies */
814 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
815 if (!nl_freqs)
816 goto nla_put_failure;
817
818 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
819 nl_freq = nla_nest_start(msg, i);
820 if (!nl_freq)
821 goto nla_put_failure;
822
823 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100824
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400825 if (nl80211_msg_put_channel(msg, chan))
826 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200827
Johannes Bergee688b002008-01-24 19:38:39 +0100828 nla_nest_end(msg, nl_freq);
829 }
830
831 nla_nest_end(msg, nl_freqs);
832
833 /* add bitrates */
834 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
835 if (!nl_rates)
836 goto nla_put_failure;
837
838 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
839 nl_rate = nla_nest_start(msg, i);
840 if (!nl_rate)
841 goto nla_put_failure;
842
843 rate = &dev->wiphy.bands[band]->bitrates[i];
844 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
845 rate->bitrate);
846 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
847 NLA_PUT_FLAG(msg,
848 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
849
850 nla_nest_end(msg, nl_rate);
851 }
852
853 nla_nest_end(msg, nl_rates);
854
855 nla_nest_end(msg, nl_band);
856 }
857 nla_nest_end(msg, nl_bands);
858
Johannes Berg8fdc6212009-03-14 09:34:01 +0100859 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
860 if (!nl_cmds)
861 goto nla_put_failure;
862
863 i = 0;
864#define CMD(op, n) \
865 do { \
866 if (dev->ops->op) { \
867 i++; \
868 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
869 } \
870 } while (0)
871
872 CMD(add_virtual_intf, NEW_INTERFACE);
873 CMD(change_virtual_intf, SET_INTERFACE);
874 CMD(add_key, NEW_KEY);
875 CMD(add_beacon, NEW_BEACON);
876 CMD(add_station, NEW_STATION);
877 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800878 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100879 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200880 CMD(auth, AUTHENTICATE);
881 CMD(assoc, ASSOCIATE);
882 CMD(deauth, DEAUTHENTICATE);
883 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200884 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100885 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100886 CMD(set_pmksa, SET_PMKSA);
887 CMD(del_pmksa, DEL_PMKSA);
888 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100889 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
890 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200891 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200892 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100893 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100894 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200895 i++;
896 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
897 }
Johannes Bergf444de02010-05-05 15:25:02 +0200898 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400899 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300900 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
901 CMD(tdls_mgmt, TDLS_MGMT);
902 CMD(tdls_oper, TDLS_OPER);
903 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300904 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
905 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100906 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100907 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e7602302011-11-04 11:18:17 +0100908 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
909 i++;
910 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
911 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100912
Kalle Valo4745fc02011-11-17 19:06:10 +0200913#ifdef CONFIG_NL80211_TESTMODE
914 CMD(testmode_cmd, TESTMODE);
915#endif
916
Johannes Berg8fdc6212009-03-14 09:34:01 +0100917#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200918
Johannes Berg6829c872009-07-02 09:13:27 +0200919 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200920 i++;
921 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
922 }
923
Johannes Berg6829c872009-07-02 09:13:27 +0200924 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200925 i++;
926 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
927 }
928
Johannes Berg8fdc6212009-03-14 09:34:01 +0100929 nla_nest_end(msg, nl_cmds);
930
Johannes Berg7c4ef712011-11-18 15:33:48 +0100931 if (dev->ops->remain_on_channel &&
932 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100933 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
934 dev->wiphy.max_remain_on_channel_duration);
935
Johannes Berg7c4ef712011-11-18 15:33:48 +0100936 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100937 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
938
Johannes Berg2e161f72010-08-12 15:38:38 +0200939 if (mgmt_stypes) {
940 u16 stypes;
941 struct nlattr *nl_ftypes, *nl_ifs;
942 enum nl80211_iftype ift;
943
944 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
945 if (!nl_ifs)
946 goto nla_put_failure;
947
948 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
949 nl_ftypes = nla_nest_start(msg, ift);
950 if (!nl_ftypes)
951 goto nla_put_failure;
952 i = 0;
953 stypes = mgmt_stypes[ift].tx;
954 while (stypes) {
955 if (stypes & 1)
956 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
957 (i << 4) | IEEE80211_FTYPE_MGMT);
958 stypes >>= 1;
959 i++;
960 }
961 nla_nest_end(msg, nl_ftypes);
962 }
963
Johannes Berg74b70a42010-08-24 12:15:53 +0200964 nla_nest_end(msg, nl_ifs);
965
Johannes Berg2e161f72010-08-12 15:38:38 +0200966 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
967 if (!nl_ifs)
968 goto nla_put_failure;
969
970 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
971 nl_ftypes = nla_nest_start(msg, ift);
972 if (!nl_ftypes)
973 goto nla_put_failure;
974 i = 0;
975 stypes = mgmt_stypes[ift].rx;
976 while (stypes) {
977 if (stypes & 1)
978 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
979 (i << 4) | IEEE80211_FTYPE_MGMT);
980 stypes >>= 1;
981 i++;
982 }
983 nla_nest_end(msg, nl_ftypes);
984 }
985 nla_nest_end(msg, nl_ifs);
986 }
987
Johannes Bergff1b6e62011-05-04 15:37:28 +0200988 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
989 struct nlattr *nl_wowlan;
990
991 nl_wowlan = nla_nest_start(msg,
992 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
993 if (!nl_wowlan)
994 goto nla_put_failure;
995
996 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
997 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
998 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
999 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1000 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1001 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02001002 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1003 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1004 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1005 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1006 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1007 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1008 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1009 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1010 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1011 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001012 if (dev->wiphy.wowlan.n_patterns) {
1013 struct nl80211_wowlan_pattern_support pat = {
1014 .max_patterns = dev->wiphy.wowlan.n_patterns,
1015 .min_pattern_len =
1016 dev->wiphy.wowlan.pattern_min_len,
1017 .max_pattern_len =
1018 dev->wiphy.wowlan.pattern_max_len,
1019 };
1020 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1021 sizeof(pat), &pat);
1022 }
1023
1024 nla_nest_end(msg, nl_wowlan);
1025 }
1026
Johannes Berg7527a782011-05-13 10:58:57 +02001027 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1028 dev->wiphy.software_iftypes))
1029 goto nla_put_failure;
1030
1031 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1032 goto nla_put_failure;
1033
Johannes Berg562a7482011-11-07 12:39:33 +01001034 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1035 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1036 dev->wiphy.ap_sme_capa);
1037
Johannes Berg1f074bd2011-11-06 14:13:33 +01001038 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1039
Ben Greear7e7c8922011-11-18 11:31:59 -08001040 if (dev->wiphy.ht_capa_mod_mask)
1041 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1042 sizeof(*dev->wiphy.ht_capa_mod_mask),
1043 dev->wiphy.ht_capa_mod_mask);
1044
Johannes Berg55682962007-09-20 13:09:35 -04001045 return genlmsg_end(msg, hdr);
1046
1047 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001048 genlmsg_cancel(msg, hdr);
1049 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001050}
1051
1052static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1053{
1054 int idx = 0;
1055 int start = cb->args[0];
1056 struct cfg80211_registered_device *dev;
1057
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001058 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001059 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001060 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1061 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001062 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001063 continue;
1064 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1065 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001066 dev) < 0) {
1067 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001068 break;
Julius Volzb4637272008-07-08 14:02:19 +02001069 }
Johannes Berg55682962007-09-20 13:09:35 -04001070 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001071 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001072
1073 cb->args[0] = idx;
1074
1075 return skb->len;
1076}
1077
1078static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1079{
1080 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001081 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001082
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001083 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001084 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001085 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001086
Johannes Berg4c476992010-10-04 21:36:35 +02001087 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1088 nlmsg_free(msg);
1089 return -ENOBUFS;
1090 }
Johannes Berg55682962007-09-20 13:09:35 -04001091
Johannes Berg134e6372009-07-10 09:51:34 +00001092 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001093}
1094
Jouni Malinen31888482008-10-30 16:59:24 +02001095static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1096 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1097 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1098 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1099 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1100 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1101};
1102
1103static int parse_txq_params(struct nlattr *tb[],
1104 struct ieee80211_txq_params *txq_params)
1105{
1106 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1107 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1108 !tb[NL80211_TXQ_ATTR_AIFS])
1109 return -EINVAL;
1110
1111 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1112 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1113 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1114 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1115 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1116
1117 return 0;
1118}
1119
Johannes Bergf444de02010-05-05 15:25:02 +02001120static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1121{
1122 /*
1123 * You can only set the channel explicitly for AP, mesh
1124 * and WDS type interfaces; all others have their channel
1125 * managed via their respective "establish a connection"
1126 * command (connect, join, ...)
1127 *
1128 * Monitors are special as they are normally slaved to
1129 * whatever else is going on, so they behave as though
1130 * you tried setting the wiphy channel itself.
1131 */
1132 return !wdev ||
1133 wdev->iftype == NL80211_IFTYPE_AP ||
1134 wdev->iftype == NL80211_IFTYPE_WDS ||
1135 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001136 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1137 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001138}
1139
1140static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1141 struct wireless_dev *wdev,
1142 struct genl_info *info)
1143{
1144 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1145 u32 freq;
1146 int result;
1147
1148 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1149 return -EINVAL;
1150
1151 if (!nl80211_can_set_dev_channel(wdev))
1152 return -EOPNOTSUPP;
1153
1154 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1155 channel_type = nla_get_u32(info->attrs[
1156 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1157 if (channel_type != NL80211_CHAN_NO_HT &&
1158 channel_type != NL80211_CHAN_HT20 &&
1159 channel_type != NL80211_CHAN_HT40PLUS &&
1160 channel_type != NL80211_CHAN_HT40MINUS)
1161 return -EINVAL;
1162 }
1163
1164 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1165
1166 mutex_lock(&rdev->devlist_mtx);
1167 if (wdev) {
1168 wdev_lock(wdev);
1169 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1170 wdev_unlock(wdev);
1171 } else {
1172 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1173 }
1174 mutex_unlock(&rdev->devlist_mtx);
1175
1176 return result;
1177}
1178
1179static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1180{
Johannes Berg4c476992010-10-04 21:36:35 +02001181 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1182 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001183
Johannes Berg4c476992010-10-04 21:36:35 +02001184 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001185}
1186
Bill Jordane8347eb2010-10-01 13:54:28 -04001187static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1188{
Johannes Berg43b19952010-10-07 13:10:30 +02001189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1190 struct net_device *dev = info->user_ptr[1];
1191 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001192 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001193
1194 if (!info->attrs[NL80211_ATTR_MAC])
1195 return -EINVAL;
1196
Johannes Berg43b19952010-10-07 13:10:30 +02001197 if (netif_running(dev))
1198 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001199
Johannes Berg43b19952010-10-07 13:10:30 +02001200 if (!rdev->ops->set_wds_peer)
1201 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001202
Johannes Berg43b19952010-10-07 13:10:30 +02001203 if (wdev->iftype != NL80211_IFTYPE_WDS)
1204 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001205
1206 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001207 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001208}
1209
1210
Johannes Berg55682962007-09-20 13:09:35 -04001211static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1212{
1213 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001214 struct net_device *netdev = NULL;
1215 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001216 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001217 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001218 u32 changed;
1219 u8 retry_short = 0, retry_long = 0;
1220 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001221 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001222
Johannes Bergf444de02010-05-05 15:25:02 +02001223 /*
1224 * Try to find the wiphy and netdev. Normally this
1225 * function shouldn't need the netdev, but this is
1226 * done for backward compatibility -- previously
1227 * setting the channel was done per wiphy, but now
1228 * it is per netdev. Previous userland like hostapd
1229 * also passed a netdev to set_wiphy, so that it is
1230 * possible to let that go to the right netdev!
1231 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001232 mutex_lock(&cfg80211_mutex);
1233
Johannes Bergf444de02010-05-05 15:25:02 +02001234 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1235 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1236
1237 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1238 if (netdev && netdev->ieee80211_ptr) {
1239 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1240 mutex_lock(&rdev->mtx);
1241 } else
1242 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001243 }
1244
Johannes Bergf444de02010-05-05 15:25:02 +02001245 if (!netdev) {
1246 rdev = __cfg80211_rdev_from_info(info);
1247 if (IS_ERR(rdev)) {
1248 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001249 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001250 }
1251 wdev = NULL;
1252 netdev = NULL;
1253 result = 0;
1254
1255 mutex_lock(&rdev->mtx);
1256 } else if (netif_running(netdev) &&
1257 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1258 wdev = netdev->ieee80211_ptr;
1259 else
1260 wdev = NULL;
1261
1262 /*
1263 * end workaround code, by now the rdev is available
1264 * and locked, and wdev may or may not be NULL.
1265 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001266
1267 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001268 result = cfg80211_dev_rename(
1269 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001270
1271 mutex_unlock(&cfg80211_mutex);
1272
1273 if (result)
1274 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001275
Jouni Malinen31888482008-10-30 16:59:24 +02001276 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1277 struct ieee80211_txq_params txq_params;
1278 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1279
1280 if (!rdev->ops->set_txq_params) {
1281 result = -EOPNOTSUPP;
1282 goto bad_res;
1283 }
1284
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001285 if (!netdev) {
1286 result = -EINVAL;
1287 goto bad_res;
1288 }
1289
Johannes Berg133a3ff2011-11-03 14:50:13 +01001290 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1291 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1292 result = -EINVAL;
1293 goto bad_res;
1294 }
1295
Jouni Malinen31888482008-10-30 16:59:24 +02001296 nla_for_each_nested(nl_txq_params,
1297 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1298 rem_txq_params) {
1299 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1300 nla_data(nl_txq_params),
1301 nla_len(nl_txq_params),
1302 txq_params_policy);
1303 result = parse_txq_params(tb, &txq_params);
1304 if (result)
1305 goto bad_res;
1306
1307 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001308 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001309 &txq_params);
1310 if (result)
1311 goto bad_res;
1312 }
1313 }
1314
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001315 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001316 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001317 if (result)
1318 goto bad_res;
1319 }
1320
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001321 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1322 enum nl80211_tx_power_setting type;
1323 int idx, mbm = 0;
1324
1325 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001326 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001327 goto bad_res;
1328 }
1329
1330 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1331 type = nla_get_u32(info->attrs[idx]);
1332
1333 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1334 (type != NL80211_TX_POWER_AUTOMATIC)) {
1335 result = -EINVAL;
1336 goto bad_res;
1337 }
1338
1339 if (type != NL80211_TX_POWER_AUTOMATIC) {
1340 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1341 mbm = nla_get_u32(info->attrs[idx]);
1342 }
1343
1344 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1345 if (result)
1346 goto bad_res;
1347 }
1348
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001349 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1350 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1351 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001352 if ((!rdev->wiphy.available_antennas_tx &&
1353 !rdev->wiphy.available_antennas_rx) ||
1354 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001355 result = -EOPNOTSUPP;
1356 goto bad_res;
1357 }
1358
1359 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1360 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1361
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001362 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001363 * available antenna masks, except for the "all" mask */
1364 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1365 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001366 result = -EINVAL;
1367 goto bad_res;
1368 }
1369
Bruno Randolf7f531e02010-12-16 11:30:22 +09001370 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1371 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001372
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001373 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1374 if (result)
1375 goto bad_res;
1376 }
1377
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001378 changed = 0;
1379
1380 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1381 retry_short = nla_get_u8(
1382 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1383 if (retry_short == 0) {
1384 result = -EINVAL;
1385 goto bad_res;
1386 }
1387 changed |= WIPHY_PARAM_RETRY_SHORT;
1388 }
1389
1390 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1391 retry_long = nla_get_u8(
1392 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1393 if (retry_long == 0) {
1394 result = -EINVAL;
1395 goto bad_res;
1396 }
1397 changed |= WIPHY_PARAM_RETRY_LONG;
1398 }
1399
1400 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1401 frag_threshold = nla_get_u32(
1402 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1403 if (frag_threshold < 256) {
1404 result = -EINVAL;
1405 goto bad_res;
1406 }
1407 if (frag_threshold != (u32) -1) {
1408 /*
1409 * Fragments (apart from the last one) are required to
1410 * have even length. Make the fragmentation code
1411 * simpler by stripping LSB should someone try to use
1412 * odd threshold value.
1413 */
1414 frag_threshold &= ~0x1;
1415 }
1416 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1417 }
1418
1419 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1420 rts_threshold = nla_get_u32(
1421 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1422 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1423 }
1424
Lukáš Turek81077e82009-12-21 22:50:47 +01001425 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1426 coverage_class = nla_get_u8(
1427 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1428 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1429 }
1430
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001431 if (changed) {
1432 u8 old_retry_short, old_retry_long;
1433 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001434 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001435
1436 if (!rdev->ops->set_wiphy_params) {
1437 result = -EOPNOTSUPP;
1438 goto bad_res;
1439 }
1440
1441 old_retry_short = rdev->wiphy.retry_short;
1442 old_retry_long = rdev->wiphy.retry_long;
1443 old_frag_threshold = rdev->wiphy.frag_threshold;
1444 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001445 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001446
1447 if (changed & WIPHY_PARAM_RETRY_SHORT)
1448 rdev->wiphy.retry_short = retry_short;
1449 if (changed & WIPHY_PARAM_RETRY_LONG)
1450 rdev->wiphy.retry_long = retry_long;
1451 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1452 rdev->wiphy.frag_threshold = frag_threshold;
1453 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1454 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001455 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1456 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001457
1458 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1459 if (result) {
1460 rdev->wiphy.retry_short = old_retry_short;
1461 rdev->wiphy.retry_long = old_retry_long;
1462 rdev->wiphy.frag_threshold = old_frag_threshold;
1463 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001464 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001465 }
1466 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001467
Johannes Berg306d6112008-12-08 12:39:04 +01001468 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001469 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001470 if (netdev)
1471 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001472 return result;
1473}
1474
1475
1476static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001477 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001478 struct net_device *dev)
1479{
1480 void *hdr;
1481
1482 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1483 if (!hdr)
1484 return -1;
1485
1486 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001487 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001488 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001489 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001490
1491 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1492 rdev->devlist_generation ^
1493 (cfg80211_rdev_list_generation << 2));
1494
Johannes Berg55682962007-09-20 13:09:35 -04001495 return genlmsg_end(msg, hdr);
1496
1497 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001498 genlmsg_cancel(msg, hdr);
1499 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001500}
1501
1502static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1503{
1504 int wp_idx = 0;
1505 int if_idx = 0;
1506 int wp_start = cb->args[0];
1507 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001508 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001509 struct wireless_dev *wdev;
1510
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001511 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001512 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1513 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001514 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001515 if (wp_idx < wp_start) {
1516 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001517 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001518 }
Johannes Berg55682962007-09-20 13:09:35 -04001519 if_idx = 0;
1520
Johannes Bergf5ea9122009-08-07 16:17:38 +02001521 mutex_lock(&rdev->devlist_mtx);
1522 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001523 if (if_idx < if_start) {
1524 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001525 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001526 }
Johannes Berg55682962007-09-20 13:09:35 -04001527 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1528 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001529 rdev, wdev->netdev) < 0) {
1530 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001531 goto out;
1532 }
1533 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001534 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001535 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001536
1537 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001538 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001539 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001540 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001541
1542 cb->args[0] = wp_idx;
1543 cb->args[1] = if_idx;
1544
1545 return skb->len;
1546}
1547
1548static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1549{
1550 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001551 struct cfg80211_registered_device *dev = info->user_ptr[0];
1552 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001553
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001554 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001555 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001556 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001557
Johannes Bergd7264052009-04-19 16:23:20 +02001558 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001559 dev, netdev) < 0) {
1560 nlmsg_free(msg);
1561 return -ENOBUFS;
1562 }
Johannes Berg55682962007-09-20 13:09:35 -04001563
Johannes Berg134e6372009-07-10 09:51:34 +00001564 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001565}
1566
Michael Wu66f7ac52008-01-31 19:48:22 +01001567static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1568 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1569 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1570 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1571 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1572 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1573};
1574
1575static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1576{
1577 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1578 int flag;
1579
1580 *mntrflags = 0;
1581
1582 if (!nla)
1583 return -EINVAL;
1584
1585 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1586 nla, mntr_flags_policy))
1587 return -EINVAL;
1588
1589 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1590 if (flags[flag])
1591 *mntrflags |= (1<<flag);
1592
1593 return 0;
1594}
1595
Johannes Berg9bc383d2009-11-19 11:55:19 +01001596static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001597 struct net_device *netdev, u8 use_4addr,
1598 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001599{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001600 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001601 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001602 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001603 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001604 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001605
1606 switch (iftype) {
1607 case NL80211_IFTYPE_AP_VLAN:
1608 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1609 return 0;
1610 break;
1611 case NL80211_IFTYPE_STATION:
1612 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1613 return 0;
1614 break;
1615 default:
1616 break;
1617 }
1618
1619 return -EOPNOTSUPP;
1620}
1621
Johannes Berg55682962007-09-20 13:09:35 -04001622static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1623{
Johannes Berg4c476992010-10-04 21:36:35 +02001624 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001625 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001626 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001627 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001628 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001629 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001630 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001631
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001632 memset(&params, 0, sizeof(params));
1633
Johannes Berg04a773a2009-04-19 21:24:32 +02001634 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001635
Johannes Berg723b0382008-09-16 20:22:09 +02001636 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001637 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001638 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001639 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001640 if (ntype > NL80211_IFTYPE_MAX)
1641 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001642 }
1643
Johannes Berg92ffe052008-09-16 20:39:36 +02001644 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001645 struct wireless_dev *wdev = dev->ieee80211_ptr;
1646
Johannes Berg4c476992010-10-04 21:36:35 +02001647 if (ntype != NL80211_IFTYPE_MESH_POINT)
1648 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001649 if (netif_running(dev))
1650 return -EBUSY;
1651
1652 wdev_lock(wdev);
1653 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1654 IEEE80211_MAX_MESH_ID_LEN);
1655 wdev->mesh_id_up_len =
1656 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1657 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1658 wdev->mesh_id_up_len);
1659 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001660 }
1661
Felix Fietkau8b787642009-11-10 18:53:10 +01001662 if (info->attrs[NL80211_ATTR_4ADDR]) {
1663 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1664 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001665 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001666 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001667 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001668 } else {
1669 params.use_4addr = -1;
1670 }
1671
Johannes Berg92ffe052008-09-16 20:39:36 +02001672 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001673 if (ntype != NL80211_IFTYPE_MONITOR)
1674 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001675 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1676 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001677 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001678 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001679
1680 flags = &_flags;
1681 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001682 }
Johannes Berg3b858752009-03-12 09:55:09 +01001683
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001684 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001685 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001686 else
1687 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001688
Johannes Berg9bc383d2009-11-19 11:55:19 +01001689 if (!err && params.use_4addr != -1)
1690 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1691
Johannes Berg55682962007-09-20 13:09:35 -04001692 return err;
1693}
1694
1695static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1696{
Johannes Berg4c476992010-10-04 21:36:35 +02001697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001698 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001699 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001700 int err;
1701 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001702 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001703
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001704 memset(&params, 0, sizeof(params));
1705
Johannes Berg55682962007-09-20 13:09:35 -04001706 if (!info->attrs[NL80211_ATTR_IFNAME])
1707 return -EINVAL;
1708
1709 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1710 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1711 if (type > NL80211_IFTYPE_MAX)
1712 return -EINVAL;
1713 }
1714
Johannes Berg79c97e92009-07-07 03:56:12 +02001715 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001716 !(rdev->wiphy.interface_modes & (1 << type)))
1717 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001718
Johannes Berg9bc383d2009-11-19 11:55:19 +01001719 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001720 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001721 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001722 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001723 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001724 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001725
Michael Wu66f7ac52008-01-31 19:48:22 +01001726 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1727 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1728 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001729 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001730 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001731 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001732 if (IS_ERR(dev))
1733 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001734
Johannes Berg29cbe682010-12-03 09:20:44 +01001735 if (type == NL80211_IFTYPE_MESH_POINT &&
1736 info->attrs[NL80211_ATTR_MESH_ID]) {
1737 struct wireless_dev *wdev = dev->ieee80211_ptr;
1738
1739 wdev_lock(wdev);
1740 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1741 IEEE80211_MAX_MESH_ID_LEN);
1742 wdev->mesh_id_up_len =
1743 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1744 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1745 wdev->mesh_id_up_len);
1746 wdev_unlock(wdev);
1747 }
1748
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001749 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001750}
1751
1752static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1753{
Johannes Berg4c476992010-10-04 21:36:35 +02001754 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1755 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001756
Johannes Berg4c476992010-10-04 21:36:35 +02001757 if (!rdev->ops->del_virtual_intf)
1758 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001759
Johannes Berg4c476992010-10-04 21:36:35 +02001760 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001761}
1762
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001763static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1764{
1765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1766 struct net_device *dev = info->user_ptr[1];
1767 u16 noack_map;
1768
1769 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1770 return -EINVAL;
1771
1772 if (!rdev->ops->set_noack_map)
1773 return -EOPNOTSUPP;
1774
1775 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1776
1777 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1778}
1779
Johannes Berg41ade002007-12-19 02:03:29 +01001780struct get_key_cookie {
1781 struct sk_buff *msg;
1782 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001783 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001784};
1785
1786static void get_key_callback(void *c, struct key_params *params)
1787{
Johannes Bergb9454e82009-07-08 13:29:08 +02001788 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001789 struct get_key_cookie *cookie = c;
1790
1791 if (params->key)
1792 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1793 params->key_len, params->key);
1794
1795 if (params->seq)
1796 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1797 params->seq_len, params->seq);
1798
1799 if (params->cipher)
1800 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1801 params->cipher);
1802
Johannes Bergb9454e82009-07-08 13:29:08 +02001803 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1804 if (!key)
1805 goto nla_put_failure;
1806
1807 if (params->key)
1808 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1809 params->key_len, params->key);
1810
1811 if (params->seq)
1812 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1813 params->seq_len, params->seq);
1814
1815 if (params->cipher)
1816 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1817 params->cipher);
1818
1819 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1820
1821 nla_nest_end(cookie->msg, key);
1822
Johannes Berg41ade002007-12-19 02:03:29 +01001823 return;
1824 nla_put_failure:
1825 cookie->error = 1;
1826}
1827
1828static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1829{
Johannes Berg4c476992010-10-04 21:36:35 +02001830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001831 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001832 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001833 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001834 const u8 *mac_addr = NULL;
1835 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001836 struct get_key_cookie cookie = {
1837 .error = 0,
1838 };
1839 void *hdr;
1840 struct sk_buff *msg;
1841
1842 if (info->attrs[NL80211_ATTR_KEY_IDX])
1843 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1844
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001845 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001846 return -EINVAL;
1847
1848 if (info->attrs[NL80211_ATTR_MAC])
1849 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1850
Johannes Berge31b8212010-10-05 19:39:30 +02001851 pairwise = !!mac_addr;
1852 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1853 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1854 if (kt >= NUM_NL80211_KEYTYPES)
1855 return -EINVAL;
1856 if (kt != NL80211_KEYTYPE_GROUP &&
1857 kt != NL80211_KEYTYPE_PAIRWISE)
1858 return -EINVAL;
1859 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1860 }
1861
Johannes Berg4c476992010-10-04 21:36:35 +02001862 if (!rdev->ops->get_key)
1863 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001864
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001865 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001866 if (!msg)
1867 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001868
1869 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1870 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001871 if (IS_ERR(hdr))
1872 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001873
1874 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001875 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001876
1877 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1878 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1879 if (mac_addr)
1880 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1881
Johannes Berge31b8212010-10-05 19:39:30 +02001882 if (pairwise && mac_addr &&
1883 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1884 return -ENOENT;
1885
1886 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1887 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001888
1889 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001890 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001891
1892 if (cookie.error)
1893 goto nla_put_failure;
1894
1895 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001896 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001897
1898 nla_put_failure:
1899 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001900 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001901 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001902 return err;
1903}
1904
1905static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1906{
Johannes Berg4c476992010-10-04 21:36:35 +02001907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001908 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001909 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001910 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001911
Johannes Bergb9454e82009-07-08 13:29:08 +02001912 err = nl80211_parse_key(info, &key);
1913 if (err)
1914 return err;
1915
1916 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001917 return -EINVAL;
1918
Johannes Bergb9454e82009-07-08 13:29:08 +02001919 /* only support setting default key */
1920 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001921 return -EINVAL;
1922
Johannes Bergfffd0932009-07-08 14:22:54 +02001923 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001924
1925 if (key.def) {
1926 if (!rdev->ops->set_default_key) {
1927 err = -EOPNOTSUPP;
1928 goto out;
1929 }
1930
1931 err = nl80211_key_allowed(dev->ieee80211_ptr);
1932 if (err)
1933 goto out;
1934
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001935 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1936 key.def_uni, key.def_multi);
1937
1938 if (err)
1939 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001940
Johannes Berg3d23e342009-09-29 23:27:28 +02001941#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001942 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001943#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001944 } else {
1945 if (key.def_uni || !key.def_multi) {
1946 err = -EINVAL;
1947 goto out;
1948 }
1949
1950 if (!rdev->ops->set_default_mgmt_key) {
1951 err = -EOPNOTSUPP;
1952 goto out;
1953 }
1954
1955 err = nl80211_key_allowed(dev->ieee80211_ptr);
1956 if (err)
1957 goto out;
1958
1959 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1960 dev, key.idx);
1961 if (err)
1962 goto out;
1963
1964#ifdef CONFIG_CFG80211_WEXT
1965 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1966#endif
1967 }
1968
1969 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001970 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001971
Johannes Berg41ade002007-12-19 02:03:29 +01001972 return err;
1973}
1974
1975static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1976{
Johannes Berg4c476992010-10-04 21:36:35 +02001977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001978 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001979 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001980 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001981 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001982
Johannes Bergb9454e82009-07-08 13:29:08 +02001983 err = nl80211_parse_key(info, &key);
1984 if (err)
1985 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001986
Johannes Bergb9454e82009-07-08 13:29:08 +02001987 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001988 return -EINVAL;
1989
Johannes Berg41ade002007-12-19 02:03:29 +01001990 if (info->attrs[NL80211_ATTR_MAC])
1991 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1992
Johannes Berge31b8212010-10-05 19:39:30 +02001993 if (key.type == -1) {
1994 if (mac_addr)
1995 key.type = NL80211_KEYTYPE_PAIRWISE;
1996 else
1997 key.type = NL80211_KEYTYPE_GROUP;
1998 }
1999
2000 /* for now */
2001 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2002 key.type != NL80211_KEYTYPE_GROUP)
2003 return -EINVAL;
2004
Johannes Berg4c476992010-10-04 21:36:35 +02002005 if (!rdev->ops->add_key)
2006 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002007
Johannes Berge31b8212010-10-05 19:39:30 +02002008 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2009 key.type == NL80211_KEYTYPE_PAIRWISE,
2010 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002011 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002012
2013 wdev_lock(dev->ieee80211_ptr);
2014 err = nl80211_key_allowed(dev->ieee80211_ptr);
2015 if (!err)
2016 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002017 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002018 mac_addr, &key.p);
2019 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002020
Johannes Berg41ade002007-12-19 02:03:29 +01002021 return err;
2022}
2023
2024static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2025{
Johannes Berg4c476992010-10-04 21:36:35 +02002026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002027 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002028 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002029 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002030 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002031
Johannes Bergb9454e82009-07-08 13:29:08 +02002032 err = nl80211_parse_key(info, &key);
2033 if (err)
2034 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002035
2036 if (info->attrs[NL80211_ATTR_MAC])
2037 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2038
Johannes Berge31b8212010-10-05 19:39:30 +02002039 if (key.type == -1) {
2040 if (mac_addr)
2041 key.type = NL80211_KEYTYPE_PAIRWISE;
2042 else
2043 key.type = NL80211_KEYTYPE_GROUP;
2044 }
2045
2046 /* for now */
2047 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2048 key.type != NL80211_KEYTYPE_GROUP)
2049 return -EINVAL;
2050
Johannes Berg4c476992010-10-04 21:36:35 +02002051 if (!rdev->ops->del_key)
2052 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002053
Johannes Bergfffd0932009-07-08 14:22:54 +02002054 wdev_lock(dev->ieee80211_ptr);
2055 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002056
2057 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2058 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2059 err = -ENOENT;
2060
Johannes Bergfffd0932009-07-08 14:22:54 +02002061 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002062 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2063 key.type == NL80211_KEYTYPE_PAIRWISE,
2064 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002065
Johannes Berg3d23e342009-09-29 23:27:28 +02002066#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002067 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002068 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002069 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002070 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002071 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2072 }
2073#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002074 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002075
Johannes Berg41ade002007-12-19 02:03:29 +01002076 return err;
2077}
2078
Johannes Berged1b6cc2007-12-19 02:03:32 +01002079static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2080{
2081 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2082 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2084 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002085 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002086 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002087 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002088
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002089 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2090 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2091 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2092 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002093 return -EINVAL;
2094
Johannes Berg074ac8d2010-09-16 14:58:22 +02002095 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002096 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2097 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002098
Johannes Berg56d18932011-05-09 18:41:15 +02002099 memset(&params, 0, sizeof(params));
2100
Johannes Berged1b6cc2007-12-19 02:03:32 +01002101 switch (info->genlhdr->cmd) {
2102 case NL80211_CMD_NEW_BEACON:
2103 /* these are required for NEW_BEACON */
2104 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2105 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002106 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2107 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002108
Johannes Berg56d18932011-05-09 18:41:15 +02002109 params.interval =
2110 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2111 params.dtim_period =
2112 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2113
2114 err = cfg80211_validate_beacon_int(rdev, params.interval);
2115 if (err)
2116 return err;
2117
Jouni Malinen32e9de82011-08-10 23:53:31 +03002118 /*
2119 * In theory, some of these attributes could be required for
2120 * NEW_BEACON, but since they were not used when the command was
2121 * originally added, keep them optional for old user space
2122 * programs to work with drivers that do not need the additional
2123 * information.
2124 */
2125 if (info->attrs[NL80211_ATTR_SSID]) {
2126 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2127 params.ssid_len =
2128 nla_len(info->attrs[NL80211_ATTR_SSID]);
2129 if (params.ssid_len == 0 ||
2130 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2131 return -EINVAL;
2132 }
2133
2134 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2135 params.hidden_ssid = nla_get_u32(
2136 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2137 if (params.hidden_ssid !=
2138 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2139 params.hidden_ssid !=
2140 NL80211_HIDDEN_SSID_ZERO_LEN &&
2141 params.hidden_ssid !=
2142 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2143 return -EINVAL;
2144 }
2145
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002146 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2147
2148 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2149 params.auth_type = nla_get_u32(
2150 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2151 if (!nl80211_valid_auth_type(params.auth_type))
2152 return -EINVAL;
2153 } else
2154 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2155
2156 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2157 NL80211_MAX_NR_CIPHER_SUITES);
2158 if (err)
2159 return err;
2160
Johannes Berg79c97e92009-07-07 03:56:12 +02002161 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002162 break;
2163 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002164 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002165 break;
2166 default:
2167 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002168 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002169 }
2170
Johannes Berg4c476992010-10-04 21:36:35 +02002171 if (!call)
2172 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002173
Johannes Berged1b6cc2007-12-19 02:03:32 +01002174 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2175 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2176 params.head_len =
2177 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2178 haveinfo = 1;
2179 }
2180
2181 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2182 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2183 params.tail_len =
2184 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2185 haveinfo = 1;
2186 }
2187
Johannes Berg4c476992010-10-04 21:36:35 +02002188 if (!haveinfo)
2189 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002190
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002191 if (info->attrs[NL80211_ATTR_IE]) {
2192 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2193 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2194 }
2195
2196 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2197 params.proberesp_ies =
2198 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2199 params.proberesp_ies_len =
2200 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2201 }
2202
2203 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2204 params.assocresp_ies =
2205 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2206 params.assocresp_ies_len =
2207 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2208 }
2209
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002210 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2211 params.probe_resp =
2212 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2213 params.probe_resp_len =
2214 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2215 }
2216
Johannes Berg56d18932011-05-09 18:41:15 +02002217 err = call(&rdev->wiphy, dev, &params);
2218 if (!err && params.interval)
2219 wdev->beacon_interval = params.interval;
2220 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002221}
2222
2223static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2224{
Johannes Berg4c476992010-10-04 21:36:35 +02002225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2226 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002227 struct wireless_dev *wdev = dev->ieee80211_ptr;
2228 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002229
Johannes Berg4c476992010-10-04 21:36:35 +02002230 if (!rdev->ops->del_beacon)
2231 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002232
Johannes Berg074ac8d2010-09-16 14:58:22 +02002233 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002234 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2235 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002236
Johannes Berg56d18932011-05-09 18:41:15 +02002237 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2238 if (!err)
2239 wdev->beacon_interval = 0;
2240 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002241}
2242
Johannes Berg5727ef12007-12-19 02:03:34 +01002243static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2244 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2245 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2246 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002247 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002248 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002249 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002250};
2251
Johannes Bergeccb8e82009-05-11 21:57:56 +03002252static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002253 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002254 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002255{
2256 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002257 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002258 int flag;
2259
Johannes Bergeccb8e82009-05-11 21:57:56 +03002260 /*
2261 * Try parsing the new attribute first so userspace
2262 * can specify both for older kernels.
2263 */
2264 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2265 if (nla) {
2266 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002267
Johannes Bergeccb8e82009-05-11 21:57:56 +03002268 sta_flags = nla_data(nla);
2269 params->sta_flags_mask = sta_flags->mask;
2270 params->sta_flags_set = sta_flags->set;
2271 if ((params->sta_flags_mask |
2272 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2273 return -EINVAL;
2274 return 0;
2275 }
2276
2277 /* if present, parse the old attribute */
2278
2279 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002280 if (!nla)
2281 return 0;
2282
2283 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2284 nla, sta_flags_policy))
2285 return -EINVAL;
2286
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002287 /*
2288 * Only allow certain flags for interface types so that
2289 * other attributes are silently ignored. Remember that
2290 * this is backward compatibility code with old userspace
2291 * and shouldn't be hit in other cases anyway.
2292 */
2293 switch (iftype) {
2294 case NL80211_IFTYPE_AP:
2295 case NL80211_IFTYPE_AP_VLAN:
2296 case NL80211_IFTYPE_P2P_GO:
2297 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2298 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2299 BIT(NL80211_STA_FLAG_WME) |
2300 BIT(NL80211_STA_FLAG_MFP);
2301 break;
2302 case NL80211_IFTYPE_P2P_CLIENT:
2303 case NL80211_IFTYPE_STATION:
2304 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2305 BIT(NL80211_STA_FLAG_TDLS_PEER);
2306 break;
2307 case NL80211_IFTYPE_MESH_POINT:
2308 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2309 BIT(NL80211_STA_FLAG_MFP) |
2310 BIT(NL80211_STA_FLAG_AUTHORIZED);
2311 default:
2312 return -EINVAL;
2313 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002314
2315 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2316 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002317 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002318
2319 return 0;
2320}
2321
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002322static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2323 int attr)
2324{
2325 struct nlattr *rate;
2326 u16 bitrate;
2327
2328 rate = nla_nest_start(msg, attr);
2329 if (!rate)
2330 goto nla_put_failure;
2331
2332 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2333 bitrate = cfg80211_calculate_bitrate(info);
2334 if (bitrate > 0)
2335 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2336
2337 if (info->flags & RATE_INFO_FLAGS_MCS)
2338 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2339 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2340 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2341 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2342 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2343
2344 nla_nest_end(msg, rate);
2345 return true;
2346
2347nla_put_failure:
2348 return false;
2349}
2350
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002351static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2352 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002353 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002354{
2355 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002356 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002357
2358 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2359 if (!hdr)
2360 return -1;
2361
2362 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2363 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2364
Johannes Bergf5ea9122009-08-07 16:17:38 +02002365 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2366
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002367 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2368 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002369 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302370 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2371 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2372 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002373 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2374 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2375 sinfo->inactive_time);
2376 if (sinfo->filled & STATION_INFO_RX_BYTES)
2377 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2378 sinfo->rx_bytes);
2379 if (sinfo->filled & STATION_INFO_TX_BYTES)
2380 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2381 sinfo->tx_bytes);
2382 if (sinfo->filled & STATION_INFO_LLID)
2383 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2384 sinfo->llid);
2385 if (sinfo->filled & STATION_INFO_PLID)
2386 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2387 sinfo->plid);
2388 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2389 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2390 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002391 if (sinfo->filled & STATION_INFO_SIGNAL)
2392 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2393 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002394 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2395 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2396 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002397 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002398 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2399 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002400 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002401 }
2402 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2403 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2404 NL80211_STA_INFO_RX_BITRATE))
2405 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002406 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002407 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2408 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2409 sinfo->rx_packets);
2410 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2411 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2412 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002413 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2414 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2415 sinfo->tx_retries);
2416 if (sinfo->filled & STATION_INFO_TX_FAILED)
2417 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2418 sinfo->tx_failed);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002419 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2420 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2421 sinfo->beacon_loss_count);
Paul Stewartf4263c92011-03-31 09:25:41 -07002422 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2423 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2424 if (!bss_param)
2425 goto nla_put_failure;
2426
2427 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2428 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2429 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2430 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2431 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2432 NLA_PUT_FLAG(msg,
2433 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2434 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2435 sinfo->bss_param.dtim_period);
2436 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2437 sinfo->bss_param.beacon_interval);
2438
2439 nla_nest_end(msg, bss_param);
2440 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002441 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2442 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2443 sizeof(struct nl80211_sta_flag_update),
2444 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002445 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002446
Felix Fietkau040bdf72011-08-10 19:00:33 -06002447 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002448 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2449 sinfo->assoc_req_ies);
2450
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002451 return genlmsg_end(msg, hdr);
2452
2453 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002454 genlmsg_cancel(msg, hdr);
2455 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002456}
2457
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002458static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002459 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002460{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002461 struct station_info sinfo;
2462 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002463 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002464 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002465 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002466 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002467
Johannes Berg67748892010-10-04 21:14:06 +02002468 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2469 if (err)
2470 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002471
2472 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002473 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002474 goto out_err;
2475 }
2476
Johannes Bergbba95fe2008-07-29 13:22:51 +02002477 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002478 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002479 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2480 mac_addr, &sinfo);
2481 if (err == -ENOENT)
2482 break;
2483 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002484 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002485
2486 if (nl80211_send_station(skb,
2487 NETLINK_CB(cb->skb).pid,
2488 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2489 netdev, mac_addr,
2490 &sinfo) < 0)
2491 goto out;
2492
2493 sta_idx++;
2494 }
2495
2496
2497 out:
2498 cb->args[1] = sta_idx;
2499 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002500 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002501 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002502
2503 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002504}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002505
Johannes Berg5727ef12007-12-19 02:03:34 +01002506static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2507{
Johannes Berg4c476992010-10-04 21:36:35 +02002508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2509 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002510 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002511 struct sk_buff *msg;
2512 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002513 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002514
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002515 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002516
2517 if (!info->attrs[NL80211_ATTR_MAC])
2518 return -EINVAL;
2519
2520 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2521
Johannes Berg4c476992010-10-04 21:36:35 +02002522 if (!rdev->ops->get_station)
2523 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002524
Johannes Berg79c97e92009-07-07 03:56:12 +02002525 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002526 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002527 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002528
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002529 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002530 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002531 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002532
2533 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002534 dev, mac_addr, &sinfo) < 0) {
2535 nlmsg_free(msg);
2536 return -ENOBUFS;
2537 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002538
Johannes Berg4c476992010-10-04 21:36:35 +02002539 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002540}
2541
2542/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002543 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002544 */
Johannes Berg80b99892011-11-18 16:23:01 +01002545static struct net_device *get_vlan(struct genl_info *info,
2546 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002547{
Johannes Berg463d0182009-07-14 00:33:35 +02002548 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002549 struct net_device *v;
2550 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002551
Johannes Berg80b99892011-11-18 16:23:01 +01002552 if (!vlanattr)
2553 return NULL;
2554
2555 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2556 if (!v)
2557 return ERR_PTR(-ENODEV);
2558
2559 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2560 ret = -EINVAL;
2561 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002562 }
Johannes Berg80b99892011-11-18 16:23:01 +01002563
2564 if (!netif_running(v)) {
2565 ret = -ENETDOWN;
2566 goto error;
2567 }
2568
2569 return v;
2570 error:
2571 dev_put(v);
2572 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002573}
2574
2575static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2576{
Johannes Berg4c476992010-10-04 21:36:35 +02002577 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002578 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002579 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002580 struct station_parameters params;
2581 u8 *mac_addr = NULL;
2582
2583 memset(&params, 0, sizeof(params));
2584
2585 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002586 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002587
2588 if (info->attrs[NL80211_ATTR_STA_AID])
2589 return -EINVAL;
2590
2591 if (!info->attrs[NL80211_ATTR_MAC])
2592 return -EINVAL;
2593
2594 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2595
2596 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2597 params.supported_rates =
2598 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2599 params.supported_rates_len =
2600 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2601 }
2602
2603 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2604 params.listen_interval =
2605 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2606
Jouni Malinen36aedc902008-08-25 11:58:58 +03002607 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2608 params.ht_capa =
2609 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2610
Johannes Bergbdd90d52011-12-14 12:20:27 +01002611 if (!rdev->ops->change_station)
2612 return -EOPNOTSUPP;
2613
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002614 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002615 return -EINVAL;
2616
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002617 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2618 params.plink_action =
2619 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2620
Javier Cardona9c3990a2011-05-03 16:57:11 -07002621 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2622 params.plink_state =
2623 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2624
Johannes Berga97f4422009-06-18 17:23:43 +02002625 switch (dev->ieee80211_ptr->iftype) {
2626 case NL80211_IFTYPE_AP:
2627 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002628 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002629 /* disallow mesh-specific things */
2630 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002631 return -EINVAL;
2632
2633 /* TDLS can't be set, ... */
2634 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2635 return -EINVAL;
2636 /*
2637 * ... but don't bother the driver with it. This works around
2638 * a hostapd/wpa_supplicant issue -- it always includes the
2639 * TLDS_PEER flag in the mask even for AP mode.
2640 */
2641 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2642
2643 /* accept only the listed bits */
2644 if (params.sta_flags_mask &
2645 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2646 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2647 BIT(NL80211_STA_FLAG_WME) |
2648 BIT(NL80211_STA_FLAG_MFP)))
2649 return -EINVAL;
2650
2651 /* must be last in here for error handling */
2652 params.vlan = get_vlan(info, rdev);
2653 if (IS_ERR(params.vlan))
2654 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002655 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002656 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002657 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002658 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002659 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002660 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002661 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002662 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002663 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002664 return -EINVAL;
2665 /*
2666 * Don't allow userspace to change the TDLS_PEER flag,
2667 * but silently ignore attempts to change it since we
2668 * don't have state here to verify that it doesn't try
2669 * to change the flag.
2670 */
2671 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2672
2673 /* reject any changes other than AUTHORIZED */
2674 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2675 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002676 break;
2677 case NL80211_IFTYPE_MESH_POINT:
2678 /* disallow things mesh doesn't support */
2679 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002680 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002681 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002682 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002683 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002684 return -EINVAL;
2685 /*
2686 * No special handling for TDLS here -- the userspace
2687 * mesh code doesn't have this bug.
2688 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002689 if (params.sta_flags_mask &
2690 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002691 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002692 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002693 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002694 break;
2695 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002696 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002697 }
2698
Johannes Bergbdd90d52011-12-14 12:20:27 +01002699 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002700
Johannes Berg79c97e92009-07-07 03:56:12 +02002701 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002702
Johannes Berg5727ef12007-12-19 02:03:34 +01002703 if (params.vlan)
2704 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002705
Johannes Berg5727ef12007-12-19 02:03:34 +01002706 return err;
2707}
2708
Eliad Pellerc75786c2011-08-23 14:37:46 +03002709static struct nla_policy
2710nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2711 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2712 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2713};
2714
Johannes Berg5727ef12007-12-19 02:03:34 +01002715static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2716{
Johannes Berg4c476992010-10-04 21:36:35 +02002717 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002718 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002719 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002720 struct station_parameters params;
2721 u8 *mac_addr = NULL;
2722
2723 memset(&params, 0, sizeof(params));
2724
2725 if (!info->attrs[NL80211_ATTR_MAC])
2726 return -EINVAL;
2727
Johannes Berg5727ef12007-12-19 02:03:34 +01002728 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2729 return -EINVAL;
2730
2731 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2732 return -EINVAL;
2733
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002734 if (!info->attrs[NL80211_ATTR_STA_AID])
2735 return -EINVAL;
2736
Johannes Berg5727ef12007-12-19 02:03:34 +01002737 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2738 params.supported_rates =
2739 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2740 params.supported_rates_len =
2741 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2742 params.listen_interval =
2743 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002744
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002745 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2746 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2747 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002748
Jouni Malinen36aedc902008-08-25 11:58:58 +03002749 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2750 params.ht_capa =
2751 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002752
Javier Cardona96b78df2011-04-07 15:08:33 -07002753 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2754 params.plink_action =
2755 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2756
Johannes Bergbdd90d52011-12-14 12:20:27 +01002757 if (!rdev->ops->add_station)
2758 return -EOPNOTSUPP;
2759
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002760 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002761 return -EINVAL;
2762
Johannes Bergbdd90d52011-12-14 12:20:27 +01002763 switch (dev->ieee80211_ptr->iftype) {
2764 case NL80211_IFTYPE_AP:
2765 case NL80211_IFTYPE_AP_VLAN:
2766 case NL80211_IFTYPE_P2P_GO:
2767 /* parse WME attributes if sta is WME capable */
2768 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2769 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2770 info->attrs[NL80211_ATTR_STA_WME]) {
2771 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2772 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002773
Johannes Bergbdd90d52011-12-14 12:20:27 +01002774 nla = info->attrs[NL80211_ATTR_STA_WME];
2775 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2776 nl80211_sta_wme_policy);
2777 if (err)
2778 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002779
Johannes Bergbdd90d52011-12-14 12:20:27 +01002780 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2781 params.uapsd_queues =
2782 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2783 if (params.uapsd_queues &
2784 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2785 return -EINVAL;
2786
2787 if (tb[NL80211_STA_WME_MAX_SP])
2788 params.max_sp =
2789 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2790
2791 if (params.max_sp &
2792 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2793 return -EINVAL;
2794
2795 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2796 }
2797 /* TDLS peers cannot be added */
2798 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002799 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002800 /* but don't bother the driver with it */
2801 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002802
Johannes Bergbdd90d52011-12-14 12:20:27 +01002803 /* must be last in here for error handling */
2804 params.vlan = get_vlan(info, rdev);
2805 if (IS_ERR(params.vlan))
2806 return PTR_ERR(params.vlan);
2807 break;
2808 case NL80211_IFTYPE_MESH_POINT:
2809 /* TDLS peers cannot be added */
2810 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002811 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002812 break;
2813 case NL80211_IFTYPE_STATION:
2814 /* Only TDLS peers can be added */
2815 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2816 return -EINVAL;
2817 /* Can only add if TDLS ... */
2818 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2819 return -EOPNOTSUPP;
2820 /* ... with external setup is supported */
2821 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2822 return -EOPNOTSUPP;
2823 break;
2824 default:
2825 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002826 }
2827
Johannes Bergbdd90d52011-12-14 12:20:27 +01002828 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002829
Johannes Berg79c97e92009-07-07 03:56:12 +02002830 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002831
Johannes Berg5727ef12007-12-19 02:03:34 +01002832 if (params.vlan)
2833 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002834 return err;
2835}
2836
2837static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2838{
Johannes Berg4c476992010-10-04 21:36:35 +02002839 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2840 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002841 u8 *mac_addr = NULL;
2842
2843 if (info->attrs[NL80211_ATTR_MAC])
2844 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2845
Johannes Berge80cf852009-05-11 14:43:13 +02002846 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002847 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002848 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002849 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2850 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002851
Johannes Berg4c476992010-10-04 21:36:35 +02002852 if (!rdev->ops->del_station)
2853 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002854
Johannes Berg4c476992010-10-04 21:36:35 +02002855 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002856}
2857
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002858static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2859 int flags, struct net_device *dev,
2860 u8 *dst, u8 *next_hop,
2861 struct mpath_info *pinfo)
2862{
2863 void *hdr;
2864 struct nlattr *pinfoattr;
2865
2866 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2867 if (!hdr)
2868 return -1;
2869
2870 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2871 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2872 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2873
Johannes Bergf5ea9122009-08-07 16:17:38 +02002874 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2875
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002876 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2877 if (!pinfoattr)
2878 goto nla_put_failure;
2879 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2880 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2881 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002882 if (pinfo->filled & MPATH_INFO_SN)
2883 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2884 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002885 if (pinfo->filled & MPATH_INFO_METRIC)
2886 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2887 pinfo->metric);
2888 if (pinfo->filled & MPATH_INFO_EXPTIME)
2889 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2890 pinfo->exptime);
2891 if (pinfo->filled & MPATH_INFO_FLAGS)
2892 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2893 pinfo->flags);
2894 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2895 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2896 pinfo->discovery_timeout);
2897 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2898 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2899 pinfo->discovery_retries);
2900
2901 nla_nest_end(msg, pinfoattr);
2902
2903 return genlmsg_end(msg, hdr);
2904
2905 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002906 genlmsg_cancel(msg, hdr);
2907 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002908}
2909
2910static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002911 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002912{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002913 struct mpath_info pinfo;
2914 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002915 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002916 u8 dst[ETH_ALEN];
2917 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002918 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002919 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002920
Johannes Berg67748892010-10-04 21:14:06 +02002921 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2922 if (err)
2923 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002924
2925 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002926 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002927 goto out_err;
2928 }
2929
Jouni Malineneec60b02009-03-20 21:21:19 +02002930 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2931 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002932 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002933 }
2934
Johannes Bergbba95fe2008-07-29 13:22:51 +02002935 while (1) {
2936 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2937 dst, next_hop, &pinfo);
2938 if (err == -ENOENT)
2939 break;
2940 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002941 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002942
2943 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2944 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2945 netdev, dst, next_hop,
2946 &pinfo) < 0)
2947 goto out;
2948
2949 path_idx++;
2950 }
2951
2952
2953 out:
2954 cb->args[1] = path_idx;
2955 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002956 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002957 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002958 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002959}
2960
2961static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2962{
Johannes Berg4c476992010-10-04 21:36:35 +02002963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002964 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002965 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002966 struct mpath_info pinfo;
2967 struct sk_buff *msg;
2968 u8 *dst = NULL;
2969 u8 next_hop[ETH_ALEN];
2970
2971 memset(&pinfo, 0, sizeof(pinfo));
2972
2973 if (!info->attrs[NL80211_ATTR_MAC])
2974 return -EINVAL;
2975
2976 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2977
Johannes Berg4c476992010-10-04 21:36:35 +02002978 if (!rdev->ops->get_mpath)
2979 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002980
Johannes Berg4c476992010-10-04 21:36:35 +02002981 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2982 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002983
Johannes Berg79c97e92009-07-07 03:56:12 +02002984 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002985 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002986 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002987
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002988 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002989 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002990 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002991
2992 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002993 dev, dst, next_hop, &pinfo) < 0) {
2994 nlmsg_free(msg);
2995 return -ENOBUFS;
2996 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002997
Johannes Berg4c476992010-10-04 21:36:35 +02002998 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002999}
3000
3001static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3002{
Johannes Berg4c476992010-10-04 21:36:35 +02003003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3004 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003005 u8 *dst = NULL;
3006 u8 *next_hop = NULL;
3007
3008 if (!info->attrs[NL80211_ATTR_MAC])
3009 return -EINVAL;
3010
3011 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3012 return -EINVAL;
3013
3014 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3015 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3016
Johannes Berg4c476992010-10-04 21:36:35 +02003017 if (!rdev->ops->change_mpath)
3018 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003019
Johannes Berg4c476992010-10-04 21:36:35 +02003020 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3021 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003022
Johannes Berg4c476992010-10-04 21:36:35 +02003023 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003024}
Johannes Berg4c476992010-10-04 21:36:35 +02003025
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003026static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3027{
Johannes Berg4c476992010-10-04 21:36:35 +02003028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3029 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003030 u8 *dst = NULL;
3031 u8 *next_hop = NULL;
3032
3033 if (!info->attrs[NL80211_ATTR_MAC])
3034 return -EINVAL;
3035
3036 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3037 return -EINVAL;
3038
3039 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3040 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3041
Johannes Berg4c476992010-10-04 21:36:35 +02003042 if (!rdev->ops->add_mpath)
3043 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003044
Johannes Berg4c476992010-10-04 21:36:35 +02003045 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3046 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003047
Johannes Berg4c476992010-10-04 21:36:35 +02003048 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003049}
3050
3051static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3052{
Johannes Berg4c476992010-10-04 21:36:35 +02003053 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3054 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003055 u8 *dst = NULL;
3056
3057 if (info->attrs[NL80211_ATTR_MAC])
3058 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3059
Johannes Berg4c476992010-10-04 21:36:35 +02003060 if (!rdev->ops->del_mpath)
3061 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003062
Johannes Berg4c476992010-10-04 21:36:35 +02003063 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003064}
3065
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003066static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3067{
Johannes Berg4c476992010-10-04 21:36:35 +02003068 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3069 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003070 struct bss_parameters params;
3071
3072 memset(&params, 0, sizeof(params));
3073 /* default to not changing parameters */
3074 params.use_cts_prot = -1;
3075 params.use_short_preamble = -1;
3076 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003077 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003078 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003079
3080 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3081 params.use_cts_prot =
3082 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3083 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3084 params.use_short_preamble =
3085 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3086 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3087 params.use_short_slot_time =
3088 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003089 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3090 params.basic_rates =
3091 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3092 params.basic_rates_len =
3093 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3094 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003095 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3096 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003097 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3098 params.ht_opmode =
3099 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003100
Johannes Berg4c476992010-10-04 21:36:35 +02003101 if (!rdev->ops->change_bss)
3102 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003103
Johannes Berg074ac8d2010-09-16 14:58:22 +02003104 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003105 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3106 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003107
Johannes Berg4c476992010-10-04 21:36:35 +02003108 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003109}
3110
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003111static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003112 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3113 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3114 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3115 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3116 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3117 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3118};
3119
3120static int parse_reg_rule(struct nlattr *tb[],
3121 struct ieee80211_reg_rule *reg_rule)
3122{
3123 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3124 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3125
3126 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3127 return -EINVAL;
3128 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3129 return -EINVAL;
3130 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3131 return -EINVAL;
3132 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3133 return -EINVAL;
3134 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3135 return -EINVAL;
3136
3137 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3138
3139 freq_range->start_freq_khz =
3140 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3141 freq_range->end_freq_khz =
3142 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3143 freq_range->max_bandwidth_khz =
3144 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3145
3146 power_rule->max_eirp =
3147 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3148
3149 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3150 power_rule->max_antenna_gain =
3151 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3152
3153 return 0;
3154}
3155
3156static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3157{
3158 int r;
3159 char *data = NULL;
3160
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003161 /*
3162 * You should only get this when cfg80211 hasn't yet initialized
3163 * completely when built-in to the kernel right between the time
3164 * window between nl80211_init() and regulatory_init(), if that is
3165 * even possible.
3166 */
3167 mutex_lock(&cfg80211_mutex);
3168 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003169 mutex_unlock(&cfg80211_mutex);
3170 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003171 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003172 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003173
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003174 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3175 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003176
3177 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3178
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003179 r = regulatory_hint_user(data);
3180
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003181 return r;
3182}
3183
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003184static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003185 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003186{
Johannes Berg4c476992010-10-04 21:36:35 +02003187 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003188 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003189 struct wireless_dev *wdev = dev->ieee80211_ptr;
3190 struct mesh_config cur_params;
3191 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003192 void *hdr;
3193 struct nlattr *pinfoattr;
3194 struct sk_buff *msg;
3195
Johannes Berg29cbe682010-12-03 09:20:44 +01003196 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3197 return -EOPNOTSUPP;
3198
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003199 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003200 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003201
Johannes Berg29cbe682010-12-03 09:20:44 +01003202 wdev_lock(wdev);
3203 /* If not connected, get default parameters */
3204 if (!wdev->mesh_id_len)
3205 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3206 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003207 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003208 &cur_params);
3209 wdev_unlock(wdev);
3210
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003211 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003212 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003213
3214 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003215 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003216 if (!msg)
3217 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003218 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003219 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003220 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003221 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003222 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003223 if (!pinfoattr)
3224 goto nla_put_failure;
3225 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3226 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3227 cur_params.dot11MeshRetryTimeout);
3228 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3229 cur_params.dot11MeshConfirmTimeout);
3230 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3231 cur_params.dot11MeshHoldingTimeout);
3232 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3233 cur_params.dot11MeshMaxPeerLinks);
3234 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3235 cur_params.dot11MeshMaxRetries);
3236 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3237 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003238 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3239 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003240 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3241 cur_params.auto_open_plinks);
3242 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3243 cur_params.dot11MeshHWMPmaxPREQretries);
3244 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3245 cur_params.path_refresh_time);
3246 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3247 cur_params.min_discovery_timeout);
3248 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3249 cur_params.dot11MeshHWMPactivePathTimeout);
3250 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3251 cur_params.dot11MeshHWMPpreqMinInterval);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003252 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3253 cur_params.dot11MeshHWMPperrMinInterval);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003254 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3255 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003256 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3257 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003258 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3259 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003260 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3261 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003262 nla_nest_end(msg, pinfoattr);
3263 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003264 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003265
Johannes Berg3b858752009-03-12 09:55:09 +01003266 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003267 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003268 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003269 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003270 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003271}
3272
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003273static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003274 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3275 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3276 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3277 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3278 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3279 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003280 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003281 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3282
3283 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3284 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3285 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3286 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3287 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003288 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003289 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003290 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003291 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003292 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003293};
3294
Javier Cardonac80d5452010-12-16 17:37:49 -08003295static const struct nla_policy
3296 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3297 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3298 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003299 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003300 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003301 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003302 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003303};
3304
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003305static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003306 struct mesh_config *cfg,
3307 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003308{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003309 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003310 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003311
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003312#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3313do {\
3314 if (table[attr_num]) {\
3315 cfg->param = nla_fn(table[attr_num]); \
3316 mask |= (1 << (attr_num - 1)); \
3317 } \
3318} while (0);\
3319
3320
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003321 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003322 return -EINVAL;
3323 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003324 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003325 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003326 return -EINVAL;
3327
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003328 /* This makes sure that there aren't more than 32 mesh config
3329 * parameters (otherwise our bitfield scheme would not work.) */
3330 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3331
3332 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003333 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3334 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3335 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3336 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3337 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3338 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3339 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3340 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3341 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3342 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3343 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3344 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003345 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3346 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003347 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3348 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3349 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3350 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3351 nla_get_u8);
3352 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3353 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3355 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3356 nla_get_u16);
3357 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3358 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3359 nla_get_u32);
3360 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3361 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3362 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003363 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3364 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3365 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003366 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3367 dot11MeshHWMPnetDiameterTraversalTime,
3368 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3369 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003370 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3371 dot11MeshHWMPRootMode, mask,
3372 NL80211_MESHCONF_HWMP_ROOTMODE,
3373 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003374 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3375 dot11MeshHWMPRannInterval, mask,
3376 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3377 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003378 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3379 dot11MeshGateAnnouncementProtocol, mask,
3380 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3381 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003382 if (mask_out)
3383 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003384
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003385 return 0;
3386
3387#undef FILL_IN_MESH_PARAM_IF_SET
3388}
3389
Javier Cardonac80d5452010-12-16 17:37:49 -08003390static int nl80211_parse_mesh_setup(struct genl_info *info,
3391 struct mesh_setup *setup)
3392{
3393 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3394
3395 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3396 return -EINVAL;
3397 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3398 info->attrs[NL80211_ATTR_MESH_SETUP],
3399 nl80211_mesh_setup_params_policy))
3400 return -EINVAL;
3401
3402 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3403 setup->path_sel_proto =
3404 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3405 IEEE80211_PATH_PROTOCOL_VENDOR :
3406 IEEE80211_PATH_PROTOCOL_HWMP;
3407
3408 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3409 setup->path_metric =
3410 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3411 IEEE80211_PATH_METRIC_VENDOR :
3412 IEEE80211_PATH_METRIC_AIRTIME;
3413
Javier Cardona581a8b02011-04-07 15:08:27 -07003414
3415 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003416 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003417 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003418 if (!is_valid_ie_attr(ieattr))
3419 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003420 setup->ie = nla_data(ieattr);
3421 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003422 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003423 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3424 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003425
3426 return 0;
3427}
3428
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003429static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003430 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003431{
3432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3433 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003434 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003435 struct mesh_config cfg;
3436 u32 mask;
3437 int err;
3438
Johannes Berg29cbe682010-12-03 09:20:44 +01003439 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3440 return -EOPNOTSUPP;
3441
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003442 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003443 return -EOPNOTSUPP;
3444
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003445 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003446 if (err)
3447 return err;
3448
Johannes Berg29cbe682010-12-03 09:20:44 +01003449 wdev_lock(wdev);
3450 if (!wdev->mesh_id_len)
3451 err = -ENOLINK;
3452
3453 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003454 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003455 mask, &cfg);
3456
3457 wdev_unlock(wdev);
3458
3459 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003460}
3461
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003462static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3463{
3464 struct sk_buff *msg;
3465 void *hdr = NULL;
3466 struct nlattr *nl_reg_rules;
3467 unsigned int i;
3468 int err = -EINVAL;
3469
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003470 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003471
3472 if (!cfg80211_regdomain)
3473 goto out;
3474
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003476 if (!msg) {
3477 err = -ENOBUFS;
3478 goto out;
3479 }
3480
3481 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3482 NL80211_CMD_GET_REG);
3483 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003484 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003485
3486 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3487 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003488 if (cfg80211_regdomain->dfs_region)
3489 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3490 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003491
3492 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3493 if (!nl_reg_rules)
3494 goto nla_put_failure;
3495
3496 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3497 struct nlattr *nl_reg_rule;
3498 const struct ieee80211_reg_rule *reg_rule;
3499 const struct ieee80211_freq_range *freq_range;
3500 const struct ieee80211_power_rule *power_rule;
3501
3502 reg_rule = &cfg80211_regdomain->reg_rules[i];
3503 freq_range = &reg_rule->freq_range;
3504 power_rule = &reg_rule->power_rule;
3505
3506 nl_reg_rule = nla_nest_start(msg, i);
3507 if (!nl_reg_rule)
3508 goto nla_put_failure;
3509
3510 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3511 reg_rule->flags);
3512 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3513 freq_range->start_freq_khz);
3514 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3515 freq_range->end_freq_khz);
3516 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3517 freq_range->max_bandwidth_khz);
3518 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3519 power_rule->max_antenna_gain);
3520 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3521 power_rule->max_eirp);
3522
3523 nla_nest_end(msg, nl_reg_rule);
3524 }
3525
3526 nla_nest_end(msg, nl_reg_rules);
3527
3528 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003529 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003530 goto out;
3531
3532nla_put_failure:
3533 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003534put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003535 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003536 err = -EMSGSIZE;
3537out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003538 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003539 return err;
3540}
3541
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003542static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3543{
3544 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3545 struct nlattr *nl_reg_rule;
3546 char *alpha2 = NULL;
3547 int rem_reg_rules = 0, r = 0;
3548 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003549 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003550 struct ieee80211_regdomain *rd = NULL;
3551
3552 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3553 return -EINVAL;
3554
3555 if (!info->attrs[NL80211_ATTR_REG_RULES])
3556 return -EINVAL;
3557
3558 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3559
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003560 if (info->attrs[NL80211_ATTR_DFS_REGION])
3561 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3562
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003563 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3564 rem_reg_rules) {
3565 num_rules++;
3566 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003567 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003568 }
3569
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003570 mutex_lock(&cfg80211_mutex);
3571
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003572 if (!reg_is_valid_request(alpha2)) {
3573 r = -EINVAL;
3574 goto bad_reg;
3575 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003576
3577 size_of_regd = sizeof(struct ieee80211_regdomain) +
3578 (num_rules * sizeof(struct ieee80211_reg_rule));
3579
3580 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003581 if (!rd) {
3582 r = -ENOMEM;
3583 goto bad_reg;
3584 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003585
3586 rd->n_reg_rules = num_rules;
3587 rd->alpha2[0] = alpha2[0];
3588 rd->alpha2[1] = alpha2[1];
3589
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003590 /*
3591 * Disable DFS master mode if the DFS region was
3592 * not supported or known on this kernel.
3593 */
3594 if (reg_supported_dfs_region(dfs_region))
3595 rd->dfs_region = dfs_region;
3596
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003597 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3598 rem_reg_rules) {
3599 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3600 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3601 reg_rule_policy);
3602 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3603 if (r)
3604 goto bad_reg;
3605
3606 rule_idx++;
3607
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003608 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3609 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003610 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003611 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003612 }
3613
3614 BUG_ON(rule_idx != num_rules);
3615
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003616 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003617
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003618 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003619
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003620 return r;
3621
Johannes Bergd2372b32008-10-24 20:32:20 +02003622 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003623 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003624 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003625 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003626}
3627
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003628static int validate_scan_freqs(struct nlattr *freqs)
3629{
3630 struct nlattr *attr1, *attr2;
3631 int n_channels = 0, tmp1, tmp2;
3632
3633 nla_for_each_nested(attr1, freqs, tmp1) {
3634 n_channels++;
3635 /*
3636 * Some hardware has a limited channel list for
3637 * scanning, and it is pretty much nonsensical
3638 * to scan for a channel twice, so disallow that
3639 * and don't require drivers to check that the
3640 * channel list they get isn't longer than what
3641 * they can scan, as long as they can scan all
3642 * the channels they registered at once.
3643 */
3644 nla_for_each_nested(attr2, freqs, tmp2)
3645 if (attr1 != attr2 &&
3646 nla_get_u32(attr1) == nla_get_u32(attr2))
3647 return 0;
3648 }
3649
3650 return n_channels;
3651}
3652
Johannes Berg2a519312009-02-10 21:25:55 +01003653static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3654{
Johannes Berg4c476992010-10-04 21:36:35 +02003655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3656 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003657 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003658 struct nlattr *attr;
3659 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003660 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003661 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003662
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003663 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3664 return -EINVAL;
3665
Johannes Berg79c97e92009-07-07 03:56:12 +02003666 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003667
Johannes Berg4c476992010-10-04 21:36:35 +02003668 if (!rdev->ops->scan)
3669 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003670
Johannes Berg4c476992010-10-04 21:36:35 +02003671 if (rdev->scan_req)
3672 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003673
3674 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003675 n_channels = validate_scan_freqs(
3676 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003677 if (!n_channels)
3678 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003679 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003680 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003681 n_channels = 0;
3682
Johannes Berg2a519312009-02-10 21:25:55 +01003683 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3684 if (wiphy->bands[band])
3685 n_channels += wiphy->bands[band]->n_channels;
3686 }
3687
3688 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3689 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3690 n_ssids++;
3691
Johannes Berg4c476992010-10-04 21:36:35 +02003692 if (n_ssids > wiphy->max_scan_ssids)
3693 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003694
Jouni Malinen70692ad2009-02-16 19:39:13 +02003695 if (info->attrs[NL80211_ATTR_IE])
3696 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3697 else
3698 ie_len = 0;
3699
Johannes Berg4c476992010-10-04 21:36:35 +02003700 if (ie_len > wiphy->max_scan_ie_len)
3701 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003702
Johannes Berg2a519312009-02-10 21:25:55 +01003703 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003704 + sizeof(*request->ssids) * n_ssids
3705 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003706 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003707 if (!request)
3708 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003709
Johannes Berg2a519312009-02-10 21:25:55 +01003710 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003711 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003712 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003713 if (ie_len) {
3714 if (request->ssids)
3715 request->ie = (void *)(request->ssids + n_ssids);
3716 else
3717 request->ie = (void *)(request->channels + n_channels);
3718 }
Johannes Berg2a519312009-02-10 21:25:55 +01003719
Johannes Berg584991d2009-11-02 13:32:03 +01003720 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003721 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3722 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003723 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003724 struct ieee80211_channel *chan;
3725
3726 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3727
3728 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003729 err = -EINVAL;
3730 goto out_free;
3731 }
Johannes Berg584991d2009-11-02 13:32:03 +01003732
3733 /* ignore disabled channels */
3734 if (chan->flags & IEEE80211_CHAN_DISABLED)
3735 continue;
3736
3737 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003738 i++;
3739 }
3740 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003741 enum ieee80211_band band;
3742
Johannes Berg2a519312009-02-10 21:25:55 +01003743 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003744 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3745 int j;
3746 if (!wiphy->bands[band])
3747 continue;
3748 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003749 struct ieee80211_channel *chan;
3750
3751 chan = &wiphy->bands[band]->channels[j];
3752
3753 if (chan->flags & IEEE80211_CHAN_DISABLED)
3754 continue;
3755
3756 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003757 i++;
3758 }
3759 }
3760 }
3761
Johannes Berg584991d2009-11-02 13:32:03 +01003762 if (!i) {
3763 err = -EINVAL;
3764 goto out_free;
3765 }
3766
3767 request->n_channels = i;
3768
Johannes Berg2a519312009-02-10 21:25:55 +01003769 i = 0;
3770 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3771 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003772 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003773 err = -EINVAL;
3774 goto out_free;
3775 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003776 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003777 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003778 i++;
3779 }
3780 }
3781
Jouni Malinen70692ad2009-02-16 19:39:13 +02003782 if (info->attrs[NL80211_ATTR_IE]) {
3783 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003784 memcpy((void *)request->ie,
3785 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003786 request->ie_len);
3787 }
3788
Johannes Berg34850ab2011-07-18 18:08:35 +02003789 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003790 if (wiphy->bands[i])
3791 request->rates[i] =
3792 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003793
3794 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3795 nla_for_each_nested(attr,
3796 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3797 tmp) {
3798 enum ieee80211_band band = nla_type(attr);
3799
Dan Carpenter84404622011-07-29 11:52:18 +03003800 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003801 err = -EINVAL;
3802 goto out_free;
3803 }
3804 err = ieee80211_get_ratemask(wiphy->bands[band],
3805 nla_data(attr),
3806 nla_len(attr),
3807 &request->rates[band]);
3808 if (err)
3809 goto out_free;
3810 }
3811 }
3812
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303813 request->no_cck =
3814 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3815
Johannes Berg463d0182009-07-14 00:33:35 +02003816 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003817 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003818
Johannes Berg79c97e92009-07-07 03:56:12 +02003819 rdev->scan_req = request;
3820 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003821
Johannes Berg463d0182009-07-14 00:33:35 +02003822 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003823 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003824 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003825 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003826 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003827 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003828 kfree(request);
3829 }
Johannes Berg3b858752009-03-12 09:55:09 +01003830
Johannes Berg2a519312009-02-10 21:25:55 +01003831 return err;
3832}
3833
Luciano Coelho807f8a82011-05-11 17:09:35 +03003834static int nl80211_start_sched_scan(struct sk_buff *skb,
3835 struct genl_info *info)
3836{
3837 struct cfg80211_sched_scan_request *request;
3838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3839 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003840 struct nlattr *attr;
3841 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003842 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003843 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003844 enum ieee80211_band band;
3845 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003846 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003847
3848 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3849 !rdev->ops->sched_scan_start)
3850 return -EOPNOTSUPP;
3851
3852 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3853 return -EINVAL;
3854
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003855 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3856 return -EINVAL;
3857
3858 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3859 if (interval == 0)
3860 return -EINVAL;
3861
Luciano Coelho807f8a82011-05-11 17:09:35 +03003862 wiphy = &rdev->wiphy;
3863
3864 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3865 n_channels = validate_scan_freqs(
3866 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3867 if (!n_channels)
3868 return -EINVAL;
3869 } else {
3870 n_channels = 0;
3871
3872 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3873 if (wiphy->bands[band])
3874 n_channels += wiphy->bands[band]->n_channels;
3875 }
3876
3877 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3878 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3879 tmp)
3880 n_ssids++;
3881
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003882 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003883 return -EINVAL;
3884
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003885 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3886 nla_for_each_nested(attr,
3887 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3888 tmp)
3889 n_match_sets++;
3890
3891 if (n_match_sets > wiphy->max_match_sets)
3892 return -EINVAL;
3893
Luciano Coelho807f8a82011-05-11 17:09:35 +03003894 if (info->attrs[NL80211_ATTR_IE])
3895 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3896 else
3897 ie_len = 0;
3898
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003899 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003900 return -EINVAL;
3901
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003902 mutex_lock(&rdev->sched_scan_mtx);
3903
3904 if (rdev->sched_scan_req) {
3905 err = -EINPROGRESS;
3906 goto out;
3907 }
3908
Luciano Coelho807f8a82011-05-11 17:09:35 +03003909 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003910 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003911 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003912 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003913 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003914 if (!request) {
3915 err = -ENOMEM;
3916 goto out;
3917 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003918
3919 if (n_ssids)
3920 request->ssids = (void *)&request->channels[n_channels];
3921 request->n_ssids = n_ssids;
3922 if (ie_len) {
3923 if (request->ssids)
3924 request->ie = (void *)(request->ssids + n_ssids);
3925 else
3926 request->ie = (void *)(request->channels + n_channels);
3927 }
3928
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003929 if (n_match_sets) {
3930 if (request->ie)
3931 request->match_sets = (void *)(request->ie + ie_len);
3932 else if (request->ssids)
3933 request->match_sets =
3934 (void *)(request->ssids + n_ssids);
3935 else
3936 request->match_sets =
3937 (void *)(request->channels + n_channels);
3938 }
3939 request->n_match_sets = n_match_sets;
3940
Luciano Coelho807f8a82011-05-11 17:09:35 +03003941 i = 0;
3942 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3943 /* user specified, bail out if channel not found */
3944 nla_for_each_nested(attr,
3945 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3946 tmp) {
3947 struct ieee80211_channel *chan;
3948
3949 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3950
3951 if (!chan) {
3952 err = -EINVAL;
3953 goto out_free;
3954 }
3955
3956 /* ignore disabled channels */
3957 if (chan->flags & IEEE80211_CHAN_DISABLED)
3958 continue;
3959
3960 request->channels[i] = chan;
3961 i++;
3962 }
3963 } else {
3964 /* all channels */
3965 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3966 int j;
3967 if (!wiphy->bands[band])
3968 continue;
3969 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3970 struct ieee80211_channel *chan;
3971
3972 chan = &wiphy->bands[band]->channels[j];
3973
3974 if (chan->flags & IEEE80211_CHAN_DISABLED)
3975 continue;
3976
3977 request->channels[i] = chan;
3978 i++;
3979 }
3980 }
3981 }
3982
3983 if (!i) {
3984 err = -EINVAL;
3985 goto out_free;
3986 }
3987
3988 request->n_channels = i;
3989
3990 i = 0;
3991 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3992 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3993 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003994 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003995 err = -EINVAL;
3996 goto out_free;
3997 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003998 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003999 memcpy(request->ssids[i].ssid, nla_data(attr),
4000 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004001 i++;
4002 }
4003 }
4004
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004005 i = 0;
4006 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4007 nla_for_each_nested(attr,
4008 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4009 tmp) {
4010 struct nlattr *ssid;
4011
4012 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4013 nla_data(attr), nla_len(attr),
4014 nl80211_match_policy);
4015 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4016 if (ssid) {
4017 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4018 err = -EINVAL;
4019 goto out_free;
4020 }
4021 memcpy(request->match_sets[i].ssid.ssid,
4022 nla_data(ssid), nla_len(ssid));
4023 request->match_sets[i].ssid.ssid_len =
4024 nla_len(ssid);
4025 }
4026 i++;
4027 }
4028 }
4029
Luciano Coelho807f8a82011-05-11 17:09:35 +03004030 if (info->attrs[NL80211_ATTR_IE]) {
4031 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4032 memcpy((void *)request->ie,
4033 nla_data(info->attrs[NL80211_ATTR_IE]),
4034 request->ie_len);
4035 }
4036
4037 request->dev = dev;
4038 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004039 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004040
4041 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4042 if (!err) {
4043 rdev->sched_scan_req = request;
4044 nl80211_send_sched_scan(rdev, dev,
4045 NL80211_CMD_START_SCHED_SCAN);
4046 goto out;
4047 }
4048
4049out_free:
4050 kfree(request);
4051out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004052 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004053 return err;
4054}
4055
4056static int nl80211_stop_sched_scan(struct sk_buff *skb,
4057 struct genl_info *info)
4058{
4059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004060 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004061
4062 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4063 !rdev->ops->sched_scan_stop)
4064 return -EOPNOTSUPP;
4065
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004066 mutex_lock(&rdev->sched_scan_mtx);
4067 err = __cfg80211_stop_sched_scan(rdev, false);
4068 mutex_unlock(&rdev->sched_scan_mtx);
4069
4070 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004071}
4072
Johannes Berg9720bb32011-06-21 09:45:33 +02004073static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4074 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004075 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004076 struct wireless_dev *wdev,
4077 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004078{
Johannes Berg48ab9052009-07-10 18:42:31 +02004079 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004080 void *hdr;
4081 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004082 int i;
4083
4084 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004085
Johannes Berg9720bb32011-06-21 09:45:33 +02004086 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004087 NL80211_CMD_NEW_SCAN_RESULTS);
4088 if (!hdr)
4089 return -1;
4090
Johannes Berg9720bb32011-06-21 09:45:33 +02004091 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4092
Johannes Bergf5ea9122009-08-07 16:17:38 +02004093 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02004094 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01004095
4096 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4097 if (!bss)
4098 goto nla_put_failure;
4099 if (!is_zero_ether_addr(res->bssid))
4100 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4101 if (res->information_elements && res->len_information_elements)
4102 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4103 res->len_information_elements,
4104 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004105 if (res->beacon_ies && res->len_beacon_ies &&
4106 res->beacon_ies != res->information_elements)
4107 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4108 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004109 if (res->tsf)
4110 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4111 if (res->beacon_interval)
4112 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4113 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4114 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004115 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4116 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004117
Johannes Berg77965c92009-02-18 18:45:06 +01004118 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004119 case CFG80211_SIGNAL_TYPE_MBM:
4120 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4121 break;
4122 case CFG80211_SIGNAL_TYPE_UNSPEC:
4123 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4124 break;
4125 default:
4126 break;
4127 }
4128
Johannes Berg48ab9052009-07-10 18:42:31 +02004129 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004130 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004131 case NL80211_IFTYPE_STATION:
4132 if (intbss == wdev->current_bss)
4133 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4134 NL80211_BSS_STATUS_ASSOCIATED);
4135 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4136 if (intbss != wdev->auth_bsses[i])
4137 continue;
4138 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4139 NL80211_BSS_STATUS_AUTHENTICATED);
4140 break;
4141 }
4142 break;
4143 case NL80211_IFTYPE_ADHOC:
4144 if (intbss == wdev->current_bss)
4145 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4146 NL80211_BSS_STATUS_IBSS_JOINED);
4147 break;
4148 default:
4149 break;
4150 }
4151
Johannes Berg2a519312009-02-10 21:25:55 +01004152 nla_nest_end(msg, bss);
4153
4154 return genlmsg_end(msg, hdr);
4155
4156 nla_put_failure:
4157 genlmsg_cancel(msg, hdr);
4158 return -EMSGSIZE;
4159}
4160
4161static int nl80211_dump_scan(struct sk_buff *skb,
4162 struct netlink_callback *cb)
4163{
Johannes Berg48ab9052009-07-10 18:42:31 +02004164 struct cfg80211_registered_device *rdev;
4165 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004166 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004167 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004168 int start = cb->args[1], idx = 0;
4169 int err;
4170
Johannes Berg67748892010-10-04 21:14:06 +02004171 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4172 if (err)
4173 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004174
Johannes Berg48ab9052009-07-10 18:42:31 +02004175 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004176
Johannes Berg48ab9052009-07-10 18:42:31 +02004177 wdev_lock(wdev);
4178 spin_lock_bh(&rdev->bss_lock);
4179 cfg80211_bss_expire(rdev);
4180
Johannes Berg9720bb32011-06-21 09:45:33 +02004181 cb->seq = rdev->bss_generation;
4182
Johannes Berg48ab9052009-07-10 18:42:31 +02004183 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004184 if (++idx <= start)
4185 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004186 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004187 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004188 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004189 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004190 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004191 }
4192 }
4193
Johannes Berg48ab9052009-07-10 18:42:31 +02004194 spin_unlock_bh(&rdev->bss_lock);
4195 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004196
4197 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004198 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004199
Johannes Berg67748892010-10-04 21:14:06 +02004200 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004201}
4202
Holger Schurig61fa7132009-11-11 12:25:40 +01004203static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4204 int flags, struct net_device *dev,
4205 struct survey_info *survey)
4206{
4207 void *hdr;
4208 struct nlattr *infoattr;
4209
Holger Schurig61fa7132009-11-11 12:25:40 +01004210 hdr = nl80211hdr_put(msg, pid, seq, flags,
4211 NL80211_CMD_NEW_SURVEY_RESULTS);
4212 if (!hdr)
4213 return -ENOMEM;
4214
4215 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4216
4217 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4218 if (!infoattr)
4219 goto nla_put_failure;
4220
4221 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4222 survey->channel->center_freq);
4223 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4224 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4225 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004226 if (survey->filled & SURVEY_INFO_IN_USE)
4227 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004228 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4229 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4230 survey->channel_time);
4231 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4232 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4233 survey->channel_time_busy);
4234 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4235 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4236 survey->channel_time_ext_busy);
4237 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4238 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4239 survey->channel_time_rx);
4240 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4241 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4242 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004243
4244 nla_nest_end(msg, infoattr);
4245
4246 return genlmsg_end(msg, hdr);
4247
4248 nla_put_failure:
4249 genlmsg_cancel(msg, hdr);
4250 return -EMSGSIZE;
4251}
4252
4253static int nl80211_dump_survey(struct sk_buff *skb,
4254 struct netlink_callback *cb)
4255{
4256 struct survey_info survey;
4257 struct cfg80211_registered_device *dev;
4258 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004259 int survey_idx = cb->args[1];
4260 int res;
4261
Johannes Berg67748892010-10-04 21:14:06 +02004262 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4263 if (res)
4264 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004265
4266 if (!dev->ops->dump_survey) {
4267 res = -EOPNOTSUPP;
4268 goto out_err;
4269 }
4270
4271 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004272 struct ieee80211_channel *chan;
4273
Holger Schurig61fa7132009-11-11 12:25:40 +01004274 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4275 &survey);
4276 if (res == -ENOENT)
4277 break;
4278 if (res)
4279 goto out_err;
4280
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004281 /* Survey without a channel doesn't make sense */
4282 if (!survey.channel) {
4283 res = -EINVAL;
4284 goto out;
4285 }
4286
4287 chan = ieee80211_get_channel(&dev->wiphy,
4288 survey.channel->center_freq);
4289 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4290 survey_idx++;
4291 continue;
4292 }
4293
Holger Schurig61fa7132009-11-11 12:25:40 +01004294 if (nl80211_send_survey(skb,
4295 NETLINK_CB(cb->skb).pid,
4296 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4297 netdev,
4298 &survey) < 0)
4299 goto out;
4300 survey_idx++;
4301 }
4302
4303 out:
4304 cb->args[1] = survey_idx;
4305 res = skb->len;
4306 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004307 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004308 return res;
4309}
4310
Jouni Malinen255e7372009-03-20 21:21:17 +02004311static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4312{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004313 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004314}
4315
Samuel Ortizb23aa672009-07-01 21:26:54 +02004316static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4317{
4318 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4319 NL80211_WPA_VERSION_2));
4320}
4321
Jouni Malinen636a5d32009-03-19 13:39:22 +02004322static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4323{
Johannes Berg4c476992010-10-04 21:36:35 +02004324 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4325 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004326 struct ieee80211_channel *chan;
4327 const u8 *bssid, *ssid, *ie = NULL;
4328 int err, ssid_len, ie_len = 0;
4329 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004330 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004331 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004332
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004333 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4334 return -EINVAL;
4335
4336 if (!info->attrs[NL80211_ATTR_MAC])
4337 return -EINVAL;
4338
Jouni Malinen17780922009-03-27 20:52:47 +02004339 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4340 return -EINVAL;
4341
Johannes Berg19957bb2009-07-02 17:20:43 +02004342 if (!info->attrs[NL80211_ATTR_SSID])
4343 return -EINVAL;
4344
4345 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4346 return -EINVAL;
4347
Johannes Bergfffd0932009-07-08 14:22:54 +02004348 err = nl80211_parse_key(info, &key);
4349 if (err)
4350 return err;
4351
4352 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004353 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4354 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004355 if (!key.p.key || !key.p.key_len)
4356 return -EINVAL;
4357 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4358 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4359 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4360 key.p.key_len != WLAN_KEY_LEN_WEP104))
4361 return -EINVAL;
4362 if (key.idx > 4)
4363 return -EINVAL;
4364 } else {
4365 key.p.key_len = 0;
4366 key.p.key = NULL;
4367 }
4368
Johannes Bergafea0b72010-08-10 09:46:42 +02004369 if (key.idx >= 0) {
4370 int i;
4371 bool ok = false;
4372 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4373 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4374 ok = true;
4375 break;
4376 }
4377 }
Johannes Berg4c476992010-10-04 21:36:35 +02004378 if (!ok)
4379 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004380 }
4381
Johannes Berg4c476992010-10-04 21:36:35 +02004382 if (!rdev->ops->auth)
4383 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004384
Johannes Berg074ac8d2010-09-16 14:58:22 +02004385 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004386 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4387 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004388
Johannes Berg19957bb2009-07-02 17:20:43 +02004389 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004390 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004391 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004392 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4393 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004394
Johannes Berg19957bb2009-07-02 17:20:43 +02004395 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4396 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4397
4398 if (info->attrs[NL80211_ATTR_IE]) {
4399 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4400 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4401 }
4402
4403 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004404 if (!nl80211_valid_auth_type(auth_type))
4405 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004406
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004407 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4408
Johannes Berg4c476992010-10-04 21:36:35 +02004409 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4410 ssid, ssid_len, ie, ie_len,
4411 key.p.key, key.p.key_len, key.idx,
4412 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004413}
4414
Johannes Bergc0692b82010-08-27 14:26:53 +03004415static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4416 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004417 struct cfg80211_crypto_settings *settings,
4418 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004419{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004420 memset(settings, 0, sizeof(*settings));
4421
Samuel Ortizb23aa672009-07-01 21:26:54 +02004422 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4423
Johannes Bergc0692b82010-08-27 14:26:53 +03004424 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4425 u16 proto;
4426 proto = nla_get_u16(
4427 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4428 settings->control_port_ethertype = cpu_to_be16(proto);
4429 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4430 proto != ETH_P_PAE)
4431 return -EINVAL;
4432 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4433 settings->control_port_no_encrypt = true;
4434 } else
4435 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4436
Samuel Ortizb23aa672009-07-01 21:26:54 +02004437 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4438 void *data;
4439 int len, i;
4440
4441 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4442 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4443 settings->n_ciphers_pairwise = len / sizeof(u32);
4444
4445 if (len % sizeof(u32))
4446 return -EINVAL;
4447
Johannes Berg3dc27d22009-07-02 21:36:37 +02004448 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004449 return -EINVAL;
4450
4451 memcpy(settings->ciphers_pairwise, data, len);
4452
4453 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004454 if (!cfg80211_supported_cipher_suite(
4455 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004456 settings->ciphers_pairwise[i]))
4457 return -EINVAL;
4458 }
4459
4460 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4461 settings->cipher_group =
4462 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004463 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4464 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004465 return -EINVAL;
4466 }
4467
4468 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4469 settings->wpa_versions =
4470 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4471 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4472 return -EINVAL;
4473 }
4474
4475 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4476 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004477 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004478
4479 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4480 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4481 settings->n_akm_suites = len / sizeof(u32);
4482
4483 if (len % sizeof(u32))
4484 return -EINVAL;
4485
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004486 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4487 return -EINVAL;
4488
Samuel Ortizb23aa672009-07-01 21:26:54 +02004489 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004490 }
4491
4492 return 0;
4493}
4494
Jouni Malinen636a5d32009-03-19 13:39:22 +02004495static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4496{
Johannes Berg4c476992010-10-04 21:36:35 +02004497 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4498 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004499 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004500 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004501 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004502 int err, ssid_len, ie_len = 0;
4503 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004504 u32 flags = 0;
4505 struct ieee80211_ht_cap *ht_capa = NULL;
4506 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004507
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004508 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4509 return -EINVAL;
4510
4511 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004512 !info->attrs[NL80211_ATTR_SSID] ||
4513 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004514 return -EINVAL;
4515
Johannes Berg4c476992010-10-04 21:36:35 +02004516 if (!rdev->ops->assoc)
4517 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004518
Johannes Berg074ac8d2010-09-16 14:58:22 +02004519 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004520 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4521 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004522
Johannes Berg19957bb2009-07-02 17:20:43 +02004523 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004524
Johannes Berg19957bb2009-07-02 17:20:43 +02004525 chan = ieee80211_get_channel(&rdev->wiphy,
4526 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004527 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4528 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004529
Johannes Berg19957bb2009-07-02 17:20:43 +02004530 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4531 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004532
4533 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004534 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4535 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004536 }
4537
Jouni Malinendc6382c2009-05-06 22:09:37 +03004538 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004539 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004540 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004541 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004542 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004543 else if (mfp != NL80211_MFP_NO)
4544 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004545 }
4546
Johannes Berg3e5d7642009-07-07 14:37:26 +02004547 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4548 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4549
Ben Greear7e7c8922011-11-18 11:31:59 -08004550 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4551 flags |= ASSOC_REQ_DISABLE_HT;
4552
4553 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4554 ht_capa_mask =
4555 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4556
4557 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4558 if (!ht_capa_mask)
4559 return -EINVAL;
4560 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4561 }
4562
Johannes Bergc0692b82010-08-27 14:26:53 +03004563 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004564 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004565 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4566 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004567 &crypto, flags, ht_capa,
4568 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004569
Jouni Malinen636a5d32009-03-19 13:39:22 +02004570 return err;
4571}
4572
4573static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4574{
Johannes Berg4c476992010-10-04 21:36:35 +02004575 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4576 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004577 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004578 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004579 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004580 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004581
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004582 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4583 return -EINVAL;
4584
4585 if (!info->attrs[NL80211_ATTR_MAC])
4586 return -EINVAL;
4587
4588 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4589 return -EINVAL;
4590
Johannes Berg4c476992010-10-04 21:36:35 +02004591 if (!rdev->ops->deauth)
4592 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004593
Johannes Berg074ac8d2010-09-16 14:58:22 +02004594 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004595 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4596 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004597
Johannes Berg19957bb2009-07-02 17:20:43 +02004598 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004599
Johannes Berg19957bb2009-07-02 17:20:43 +02004600 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4601 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004602 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004603 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004604 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004605
4606 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004607 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4608 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004609 }
4610
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004611 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4612
Johannes Berg4c476992010-10-04 21:36:35 +02004613 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4614 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004615}
4616
4617static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4618{
Johannes Berg4c476992010-10-04 21:36:35 +02004619 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4620 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004621 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004622 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004623 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004624 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004625
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004626 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4627 return -EINVAL;
4628
4629 if (!info->attrs[NL80211_ATTR_MAC])
4630 return -EINVAL;
4631
4632 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4633 return -EINVAL;
4634
Johannes Berg4c476992010-10-04 21:36:35 +02004635 if (!rdev->ops->disassoc)
4636 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004637
Johannes Berg074ac8d2010-09-16 14:58:22 +02004638 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004639 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4640 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004641
Johannes Berg19957bb2009-07-02 17:20:43 +02004642 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004643
Johannes Berg19957bb2009-07-02 17:20:43 +02004644 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4645 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004646 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004647 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004648 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004649
4650 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004651 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4652 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004653 }
4654
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004655 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4656
Johannes Berg4c476992010-10-04 21:36:35 +02004657 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4658 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004659}
4660
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004661static bool
4662nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4663 int mcast_rate[IEEE80211_NUM_BANDS],
4664 int rateval)
4665{
4666 struct wiphy *wiphy = &rdev->wiphy;
4667 bool found = false;
4668 int band, i;
4669
4670 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4671 struct ieee80211_supported_band *sband;
4672
4673 sband = wiphy->bands[band];
4674 if (!sband)
4675 continue;
4676
4677 for (i = 0; i < sband->n_bitrates; i++) {
4678 if (sband->bitrates[i].bitrate == rateval) {
4679 mcast_rate[band] = i + 1;
4680 found = true;
4681 break;
4682 }
4683 }
4684 }
4685
4686 return found;
4687}
4688
Johannes Berg04a773a2009-04-19 21:24:32 +02004689static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4690{
Johannes Berg4c476992010-10-04 21:36:35 +02004691 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4692 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004693 struct cfg80211_ibss_params ibss;
4694 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004695 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004696 int err;
4697
Johannes Berg8e30bc52009-04-22 17:45:38 +02004698 memset(&ibss, 0, sizeof(ibss));
4699
Johannes Berg04a773a2009-04-19 21:24:32 +02004700 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4701 return -EINVAL;
4702
4703 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4704 !info->attrs[NL80211_ATTR_SSID] ||
4705 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4706 return -EINVAL;
4707
Johannes Berg8e30bc52009-04-22 17:45:38 +02004708 ibss.beacon_interval = 100;
4709
4710 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4711 ibss.beacon_interval =
4712 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4713 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4714 return -EINVAL;
4715 }
4716
Johannes Berg4c476992010-10-04 21:36:35 +02004717 if (!rdev->ops->join_ibss)
4718 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004719
Johannes Berg4c476992010-10-04 21:36:35 +02004720 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4721 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004722
Johannes Berg79c97e92009-07-07 03:56:12 +02004723 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004724
Johannes Berg39193492011-09-16 13:45:25 +02004725 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004726 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004727
4728 if (!is_valid_ether_addr(ibss.bssid))
4729 return -EINVAL;
4730 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004731 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4732 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4733
4734 if (info->attrs[NL80211_ATTR_IE]) {
4735 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4736 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4737 }
4738
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004739 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4740 enum nl80211_channel_type channel_type;
4741
4742 channel_type = nla_get_u32(
4743 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4744 if (channel_type != NL80211_CHAN_NO_HT &&
4745 channel_type != NL80211_CHAN_HT20 &&
4746 channel_type != NL80211_CHAN_HT40MINUS &&
4747 channel_type != NL80211_CHAN_HT40PLUS)
4748 return -EINVAL;
4749
4750 if (channel_type != NL80211_CHAN_NO_HT &&
4751 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4752 return -EINVAL;
4753
4754 ibss.channel_type = channel_type;
4755 } else {
4756 ibss.channel_type = NL80211_CHAN_NO_HT;
4757 }
4758
4759 ibss.channel = rdev_freq_to_chan(rdev,
4760 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4761 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004762 if (!ibss.channel ||
4763 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004764 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4765 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004766
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004767 /* Both channels should be able to initiate communication */
4768 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4769 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4770 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4771 ibss.channel_type))
4772 return -EINVAL;
4773
Johannes Berg04a773a2009-04-19 21:24:32 +02004774 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004775 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004776
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004777 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4778 u8 *rates =
4779 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4780 int n_rates =
4781 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4782 struct ieee80211_supported_band *sband =
4783 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004784 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004785
Johannes Berg34850ab2011-07-18 18:08:35 +02004786 err = ieee80211_get_ratemask(sband, rates, n_rates,
4787 &ibss.basic_rates);
4788 if (err)
4789 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004790 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004791
4792 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4793 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4794 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4795 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004796
Johannes Berg4c476992010-10-04 21:36:35 +02004797 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4798 connkeys = nl80211_parse_connkeys(rdev,
4799 info->attrs[NL80211_ATTR_KEYS]);
4800 if (IS_ERR(connkeys))
4801 return PTR_ERR(connkeys);
4802 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004803
Johannes Berg4c476992010-10-04 21:36:35 +02004804 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004805 if (err)
4806 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004807 return err;
4808}
4809
4810static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4811{
Johannes Berg4c476992010-10-04 21:36:35 +02004812 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4813 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004814
Johannes Berg4c476992010-10-04 21:36:35 +02004815 if (!rdev->ops->leave_ibss)
4816 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004817
Johannes Berg4c476992010-10-04 21:36:35 +02004818 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4819 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004820
Johannes Berg4c476992010-10-04 21:36:35 +02004821 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004822}
4823
Johannes Bergaff89a92009-07-01 21:26:51 +02004824#ifdef CONFIG_NL80211_TESTMODE
4825static struct genl_multicast_group nl80211_testmode_mcgrp = {
4826 .name = "testmode",
4827};
4828
4829static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4830{
Johannes Berg4c476992010-10-04 21:36:35 +02004831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004832 int err;
4833
4834 if (!info->attrs[NL80211_ATTR_TESTDATA])
4835 return -EINVAL;
4836
Johannes Bergaff89a92009-07-01 21:26:51 +02004837 err = -EOPNOTSUPP;
4838 if (rdev->ops->testmode_cmd) {
4839 rdev->testmode_info = info;
4840 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4841 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4842 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4843 rdev->testmode_info = NULL;
4844 }
4845
Johannes Bergaff89a92009-07-01 21:26:51 +02004846 return err;
4847}
4848
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004849static int nl80211_testmode_dump(struct sk_buff *skb,
4850 struct netlink_callback *cb)
4851{
Johannes Berg00918d32011-12-13 17:22:05 +01004852 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004853 int err;
4854 long phy_idx;
4855 void *data = NULL;
4856 int data_len = 0;
4857
4858 if (cb->args[0]) {
4859 /*
4860 * 0 is a valid index, but not valid for args[0],
4861 * so we need to offset by 1.
4862 */
4863 phy_idx = cb->args[0] - 1;
4864 } else {
4865 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4866 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4867 nl80211_policy);
4868 if (err)
4869 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01004870 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
4871 phy_idx = nla_get_u32(
4872 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4873 } else {
4874 struct net_device *netdev;
4875
4876 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
4877 nl80211_fam.attrbuf,
4878 &rdev, &netdev);
4879 if (err)
4880 return err;
4881 dev_put(netdev);
4882 phy_idx = rdev->wiphy_idx;
4883 cfg80211_unlock_rdev(rdev);
4884 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004885 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4886 cb->args[1] =
4887 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4888 }
4889
4890 if (cb->args[1]) {
4891 data = nla_data((void *)cb->args[1]);
4892 data_len = nla_len((void *)cb->args[1]);
4893 }
4894
4895 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01004896 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4897 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004898 mutex_unlock(&cfg80211_mutex);
4899 return -ENOENT;
4900 }
Johannes Berg00918d32011-12-13 17:22:05 +01004901 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004902 mutex_unlock(&cfg80211_mutex);
4903
Johannes Berg00918d32011-12-13 17:22:05 +01004904 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004905 err = -EOPNOTSUPP;
4906 goto out_err;
4907 }
4908
4909 while (1) {
4910 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4911 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4912 NL80211_CMD_TESTMODE);
4913 struct nlattr *tmdata;
4914
Johannes Berg00918d32011-12-13 17:22:05 +01004915 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004916 genlmsg_cancel(skb, hdr);
4917 break;
4918 }
4919
4920 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4921 if (!tmdata) {
4922 genlmsg_cancel(skb, hdr);
4923 break;
4924 }
Johannes Berg00918d32011-12-13 17:22:05 +01004925 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
4926 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004927 nla_nest_end(skb, tmdata);
4928
4929 if (err == -ENOBUFS || err == -ENOENT) {
4930 genlmsg_cancel(skb, hdr);
4931 break;
4932 } else if (err) {
4933 genlmsg_cancel(skb, hdr);
4934 goto out_err;
4935 }
4936
4937 genlmsg_end(skb, hdr);
4938 }
4939
4940 err = skb->len;
4941 /* see above */
4942 cb->args[0] = phy_idx + 1;
4943 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01004944 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004945 return err;
4946}
4947
Johannes Bergaff89a92009-07-01 21:26:51 +02004948static struct sk_buff *
4949__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4950 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4951{
4952 struct sk_buff *skb;
4953 void *hdr;
4954 struct nlattr *data;
4955
4956 skb = nlmsg_new(approxlen + 100, gfp);
4957 if (!skb)
4958 return NULL;
4959
4960 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4961 if (!hdr) {
4962 kfree_skb(skb);
4963 return NULL;
4964 }
4965
4966 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4967 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4968
4969 ((void **)skb->cb)[0] = rdev;
4970 ((void **)skb->cb)[1] = hdr;
4971 ((void **)skb->cb)[2] = data;
4972
4973 return skb;
4974
4975 nla_put_failure:
4976 kfree_skb(skb);
4977 return NULL;
4978}
4979
4980struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4981 int approxlen)
4982{
4983 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4984
4985 if (WARN_ON(!rdev->testmode_info))
4986 return NULL;
4987
4988 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4989 rdev->testmode_info->snd_pid,
4990 rdev->testmode_info->snd_seq,
4991 GFP_KERNEL);
4992}
4993EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4994
4995int cfg80211_testmode_reply(struct sk_buff *skb)
4996{
4997 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4998 void *hdr = ((void **)skb->cb)[1];
4999 struct nlattr *data = ((void **)skb->cb)[2];
5000
5001 if (WARN_ON(!rdev->testmode_info)) {
5002 kfree_skb(skb);
5003 return -EINVAL;
5004 }
5005
5006 nla_nest_end(skb, data);
5007 genlmsg_end(skb, hdr);
5008 return genlmsg_reply(skb, rdev->testmode_info);
5009}
5010EXPORT_SYMBOL(cfg80211_testmode_reply);
5011
5012struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5013 int approxlen, gfp_t gfp)
5014{
5015 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5016
5017 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5018}
5019EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5020
5021void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5022{
5023 void *hdr = ((void **)skb->cb)[1];
5024 struct nlattr *data = ((void **)skb->cb)[2];
5025
5026 nla_nest_end(skb, data);
5027 genlmsg_end(skb, hdr);
5028 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5029}
5030EXPORT_SYMBOL(cfg80211_testmode_event);
5031#endif
5032
Samuel Ortizb23aa672009-07-01 21:26:54 +02005033static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5034{
Johannes Berg4c476992010-10-04 21:36:35 +02005035 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5036 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005037 struct cfg80211_connect_params connect;
5038 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005039 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005040 int err;
5041
5042 memset(&connect, 0, sizeof(connect));
5043
5044 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5045 return -EINVAL;
5046
5047 if (!info->attrs[NL80211_ATTR_SSID] ||
5048 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5049 return -EINVAL;
5050
5051 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5052 connect.auth_type =
5053 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5054 if (!nl80211_valid_auth_type(connect.auth_type))
5055 return -EINVAL;
5056 } else
5057 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5058
5059 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5060
Johannes Bergc0692b82010-08-27 14:26:53 +03005061 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005062 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005063 if (err)
5064 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005065
Johannes Berg074ac8d2010-09-16 14:58:22 +02005066 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005067 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5068 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005069
Johannes Berg79c97e92009-07-07 03:56:12 +02005070 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005071
Samuel Ortizb23aa672009-07-01 21:26:54 +02005072 if (info->attrs[NL80211_ATTR_MAC])
5073 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5074 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5075 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5076
5077 if (info->attrs[NL80211_ATTR_IE]) {
5078 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5079 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5080 }
5081
5082 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5083 connect.channel =
5084 ieee80211_get_channel(wiphy,
5085 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5086 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005087 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5088 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005089 }
5090
Johannes Bergfffd0932009-07-08 14:22:54 +02005091 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5092 connkeys = nl80211_parse_connkeys(rdev,
5093 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005094 if (IS_ERR(connkeys))
5095 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005096 }
5097
Ben Greear7e7c8922011-11-18 11:31:59 -08005098 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5099 connect.flags |= ASSOC_REQ_DISABLE_HT;
5100
5101 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5102 memcpy(&connect.ht_capa_mask,
5103 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5104 sizeof(connect.ht_capa_mask));
5105
5106 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5107 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5108 return -EINVAL;
5109 memcpy(&connect.ht_capa,
5110 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5111 sizeof(connect.ht_capa));
5112 }
5113
Johannes Bergfffd0932009-07-08 14:22:54 +02005114 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005115 if (err)
5116 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005117 return err;
5118}
5119
5120static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5121{
Johannes Berg4c476992010-10-04 21:36:35 +02005122 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5123 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005124 u16 reason;
5125
5126 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5127 reason = WLAN_REASON_DEAUTH_LEAVING;
5128 else
5129 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5130
5131 if (reason == 0)
5132 return -EINVAL;
5133
Johannes Berg074ac8d2010-09-16 14:58:22 +02005134 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005135 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5136 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005137
Johannes Berg4c476992010-10-04 21:36:35 +02005138 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005139}
5140
Johannes Berg463d0182009-07-14 00:33:35 +02005141static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5142{
Johannes Berg4c476992010-10-04 21:36:35 +02005143 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005144 struct net *net;
5145 int err;
5146 u32 pid;
5147
5148 if (!info->attrs[NL80211_ATTR_PID])
5149 return -EINVAL;
5150
5151 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5152
Johannes Berg463d0182009-07-14 00:33:35 +02005153 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005154 if (IS_ERR(net))
5155 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005156
5157 err = 0;
5158
5159 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005160 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5161 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005162
Johannes Berg463d0182009-07-14 00:33:35 +02005163 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005164 return err;
5165}
5166
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005167static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5168{
Johannes Berg4c476992010-10-04 21:36:35 +02005169 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005170 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5171 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005172 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005173 struct cfg80211_pmksa pmksa;
5174
5175 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5176
5177 if (!info->attrs[NL80211_ATTR_MAC])
5178 return -EINVAL;
5179
5180 if (!info->attrs[NL80211_ATTR_PMKID])
5181 return -EINVAL;
5182
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005183 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5184 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5185
Johannes Berg074ac8d2010-09-16 14:58:22 +02005186 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005187 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5188 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005189
5190 switch (info->genlhdr->cmd) {
5191 case NL80211_CMD_SET_PMKSA:
5192 rdev_ops = rdev->ops->set_pmksa;
5193 break;
5194 case NL80211_CMD_DEL_PMKSA:
5195 rdev_ops = rdev->ops->del_pmksa;
5196 break;
5197 default:
5198 WARN_ON(1);
5199 break;
5200 }
5201
Johannes Berg4c476992010-10-04 21:36:35 +02005202 if (!rdev_ops)
5203 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005204
Johannes Berg4c476992010-10-04 21:36:35 +02005205 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005206}
5207
5208static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5209{
Johannes Berg4c476992010-10-04 21:36:35 +02005210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5211 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005212
Johannes Berg074ac8d2010-09-16 14:58:22 +02005213 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005214 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5215 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005216
Johannes Berg4c476992010-10-04 21:36:35 +02005217 if (!rdev->ops->flush_pmksa)
5218 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005219
Johannes Berg4c476992010-10-04 21:36:35 +02005220 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005221}
5222
Arik Nemtsov109086c2011-09-28 14:12:50 +03005223static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5224{
5225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5226 struct net_device *dev = info->user_ptr[1];
5227 u8 action_code, dialog_token;
5228 u16 status_code;
5229 u8 *peer;
5230
5231 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5232 !rdev->ops->tdls_mgmt)
5233 return -EOPNOTSUPP;
5234
5235 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5236 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5237 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5238 !info->attrs[NL80211_ATTR_IE] ||
5239 !info->attrs[NL80211_ATTR_MAC])
5240 return -EINVAL;
5241
5242 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5243 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5244 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5245 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5246
5247 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5248 dialog_token, status_code,
5249 nla_data(info->attrs[NL80211_ATTR_IE]),
5250 nla_len(info->attrs[NL80211_ATTR_IE]));
5251}
5252
5253static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5254{
5255 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5256 struct net_device *dev = info->user_ptr[1];
5257 enum nl80211_tdls_operation operation;
5258 u8 *peer;
5259
5260 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5261 !rdev->ops->tdls_oper)
5262 return -EOPNOTSUPP;
5263
5264 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5265 !info->attrs[NL80211_ATTR_MAC])
5266 return -EINVAL;
5267
5268 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5269 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5270
5271 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5272}
5273
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005274static int nl80211_remain_on_channel(struct sk_buff *skb,
5275 struct genl_info *info)
5276{
Johannes Berg4c476992010-10-04 21:36:35 +02005277 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5278 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005279 struct ieee80211_channel *chan;
5280 struct sk_buff *msg;
5281 void *hdr;
5282 u64 cookie;
5283 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5284 u32 freq, duration;
5285 int err;
5286
5287 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5288 !info->attrs[NL80211_ATTR_DURATION])
5289 return -EINVAL;
5290
5291 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5292
5293 /*
5294 * We should be on that channel for at least one jiffie,
5295 * and more than 5 seconds seems excessive.
5296 */
Johannes Berga2939112010-12-14 17:54:28 +01005297 if (!duration || !msecs_to_jiffies(duration) ||
5298 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005299 return -EINVAL;
5300
Johannes Berg7c4ef712011-11-18 15:33:48 +01005301 if (!rdev->ops->remain_on_channel ||
5302 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005303 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005304
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005305 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5306 channel_type = nla_get_u32(
5307 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5308 if (channel_type != NL80211_CHAN_NO_HT &&
5309 channel_type != NL80211_CHAN_HT20 &&
5310 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005311 channel_type != NL80211_CHAN_HT40MINUS)
5312 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005313 }
5314
5315 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5316 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005317 if (chan == NULL)
5318 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005319
5320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005321 if (!msg)
5322 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005323
5324 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5325 NL80211_CMD_REMAIN_ON_CHANNEL);
5326
5327 if (IS_ERR(hdr)) {
5328 err = PTR_ERR(hdr);
5329 goto free_msg;
5330 }
5331
5332 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5333 channel_type, duration, &cookie);
5334
5335 if (err)
5336 goto free_msg;
5337
5338 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5339
5340 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005341
5342 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005343
5344 nla_put_failure:
5345 err = -ENOBUFS;
5346 free_msg:
5347 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005348 return err;
5349}
5350
5351static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5352 struct genl_info *info)
5353{
Johannes Berg4c476992010-10-04 21:36:35 +02005354 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5355 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005356 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005357
5358 if (!info->attrs[NL80211_ATTR_COOKIE])
5359 return -EINVAL;
5360
Johannes Berg4c476992010-10-04 21:36:35 +02005361 if (!rdev->ops->cancel_remain_on_channel)
5362 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005363
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005364 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5365
Johannes Berg4c476992010-10-04 21:36:35 +02005366 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005367}
5368
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005369static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5370 u8 *rates, u8 rates_len)
5371{
5372 u8 i;
5373 u32 mask = 0;
5374
5375 for (i = 0; i < rates_len; i++) {
5376 int rate = (rates[i] & 0x7f) * 5;
5377 int ridx;
5378 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5379 struct ieee80211_rate *srate =
5380 &sband->bitrates[ridx];
5381 if (rate == srate->bitrate) {
5382 mask |= 1 << ridx;
5383 break;
5384 }
5385 }
5386 if (ridx == sband->n_bitrates)
5387 return 0; /* rate not found */
5388 }
5389
5390 return mask;
5391}
5392
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005393static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005394 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5395 .len = NL80211_MAX_SUPP_RATES },
5396};
5397
5398static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5399 struct genl_info *info)
5400{
5401 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005403 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005404 int rem, i;
5405 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005406 struct nlattr *tx_rates;
5407 struct ieee80211_supported_band *sband;
5408
5409 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5410 return -EINVAL;
5411
Johannes Berg4c476992010-10-04 21:36:35 +02005412 if (!rdev->ops->set_bitrate_mask)
5413 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005414
5415 memset(&mask, 0, sizeof(mask));
5416 /* Default to all rates enabled */
5417 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5418 sband = rdev->wiphy.bands[i];
5419 mask.control[i].legacy =
5420 sband ? (1 << sband->n_bitrates) - 1 : 0;
5421 }
5422
5423 /*
5424 * The nested attribute uses enum nl80211_band as the index. This maps
5425 * directly to the enum ieee80211_band values used in cfg80211.
5426 */
5427 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5428 {
5429 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005430 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5431 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005432 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005433 if (sband == NULL)
5434 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005435 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5436 nla_len(tx_rates), nl80211_txattr_policy);
5437 if (tb[NL80211_TXRATE_LEGACY]) {
5438 mask.control[band].legacy = rateset_to_mask(
5439 sband,
5440 nla_data(tb[NL80211_TXRATE_LEGACY]),
5441 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005442 if (mask.control[band].legacy == 0)
5443 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005444 }
5445 }
5446
Johannes Berg4c476992010-10-04 21:36:35 +02005447 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005448}
5449
Johannes Berg2e161f72010-08-12 15:38:38 +02005450static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005451{
Johannes Berg4c476992010-10-04 21:36:35 +02005452 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5453 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005454 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005455
5456 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5457 return -EINVAL;
5458
Johannes Berg2e161f72010-08-12 15:38:38 +02005459 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5460 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005461
Johannes Berg9d38d852010-06-09 17:20:33 +02005462 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005463 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005464 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5465 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5466 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005467 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005468 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5469 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005470
5471 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005472 if (!rdev->ops->mgmt_tx)
5473 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005474
Johannes Berg4c476992010-10-04 21:36:35 +02005475 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005476 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005477 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5478 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005479}
5480
Johannes Berg2e161f72010-08-12 15:38:38 +02005481static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005482{
Johannes Berg4c476992010-10-04 21:36:35 +02005483 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5484 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005485 struct ieee80211_channel *chan;
5486 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005487 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005488 u32 freq;
5489 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005490 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005491 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005492 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005493 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005494 bool offchan, no_cck, dont_wait_for_ack;
5495
5496 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005497
5498 if (!info->attrs[NL80211_ATTR_FRAME] ||
5499 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5500 return -EINVAL;
5501
Johannes Berg4c476992010-10-04 21:36:35 +02005502 if (!rdev->ops->mgmt_tx)
5503 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005504
Johannes Berg9d38d852010-06-09 17:20:33 +02005505 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005506 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005507 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5508 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5509 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005510 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005511 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5512 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005513
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005514 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005515 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005516 return -EINVAL;
5517 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5518 }
5519
Jouni Malinen026331c2010-02-15 12:53:10 +02005520 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5521 channel_type = nla_get_u32(
5522 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5523 if (channel_type != NL80211_CHAN_NO_HT &&
5524 channel_type != NL80211_CHAN_HT20 &&
5525 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005526 channel_type != NL80211_CHAN_HT40MINUS)
5527 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005528 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005529 }
5530
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005531 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5532
Johannes Berg7c4ef712011-11-18 15:33:48 +01005533 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5534 return -EINVAL;
5535
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305536 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5537
Jouni Malinen026331c2010-02-15 12:53:10 +02005538 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5539 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005540 if (chan == NULL)
5541 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005542
Johannes Berge247bd902011-11-04 11:18:21 +01005543 if (!dont_wait_for_ack) {
5544 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5545 if (!msg)
5546 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005547
Johannes Berge247bd902011-11-04 11:18:21 +01005548 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5549 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005550
Johannes Berge247bd902011-11-04 11:18:21 +01005551 if (IS_ERR(hdr)) {
5552 err = PTR_ERR(hdr);
5553 goto free_msg;
5554 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005555 }
Johannes Berge247bd902011-11-04 11:18:21 +01005556
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005557 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5558 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005559 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5560 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005561 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005562 if (err)
5563 goto free_msg;
5564
Johannes Berge247bd902011-11-04 11:18:21 +01005565 if (msg) {
5566 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005567
Johannes Berge247bd902011-11-04 11:18:21 +01005568 genlmsg_end(msg, hdr);
5569 return genlmsg_reply(msg, info);
5570 }
5571
5572 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005573
5574 nla_put_failure:
5575 err = -ENOBUFS;
5576 free_msg:
5577 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005578 return err;
5579}
5580
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005581static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5582{
5583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5584 struct net_device *dev = info->user_ptr[1];
5585 u64 cookie;
5586
5587 if (!info->attrs[NL80211_ATTR_COOKIE])
5588 return -EINVAL;
5589
5590 if (!rdev->ops->mgmt_tx_cancel_wait)
5591 return -EOPNOTSUPP;
5592
5593 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5594 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5595 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5596 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5597 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5598 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5599 return -EOPNOTSUPP;
5600
5601 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5602
5603 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5604}
5605
Kalle Valoffb9eb32010-02-17 17:58:10 +02005606static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5607{
Johannes Berg4c476992010-10-04 21:36:35 +02005608 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005609 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005610 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005611 u8 ps_state;
5612 bool state;
5613 int err;
5614
Johannes Berg4c476992010-10-04 21:36:35 +02005615 if (!info->attrs[NL80211_ATTR_PS_STATE])
5616 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005617
5618 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5619
Johannes Berg4c476992010-10-04 21:36:35 +02005620 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5621 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005622
5623 wdev = dev->ieee80211_ptr;
5624
Johannes Berg4c476992010-10-04 21:36:35 +02005625 if (!rdev->ops->set_power_mgmt)
5626 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005627
5628 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5629
5630 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005631 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005632
Johannes Berg4c476992010-10-04 21:36:35 +02005633 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5634 wdev->ps_timeout);
5635 if (!err)
5636 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005637 return err;
5638}
5639
5640static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5641{
Johannes Berg4c476992010-10-04 21:36:35 +02005642 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005643 enum nl80211_ps_state ps_state;
5644 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005645 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005646 struct sk_buff *msg;
5647 void *hdr;
5648 int err;
5649
Kalle Valoffb9eb32010-02-17 17:58:10 +02005650 wdev = dev->ieee80211_ptr;
5651
Johannes Berg4c476992010-10-04 21:36:35 +02005652 if (!rdev->ops->set_power_mgmt)
5653 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005654
5655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005656 if (!msg)
5657 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005658
5659 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5660 NL80211_CMD_GET_POWER_SAVE);
5661 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005662 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005663 goto free_msg;
5664 }
5665
5666 if (wdev->ps)
5667 ps_state = NL80211_PS_ENABLED;
5668 else
5669 ps_state = NL80211_PS_DISABLED;
5670
5671 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5672
5673 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005674 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005675
Johannes Berg4c476992010-10-04 21:36:35 +02005676 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005677 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005678 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005679 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005680 return err;
5681}
5682
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005683static struct nla_policy
5684nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5685 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5686 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5687 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5688};
5689
5690static int nl80211_set_cqm_rssi(struct genl_info *info,
5691 s32 threshold, u32 hysteresis)
5692{
Johannes Berg4c476992010-10-04 21:36:35 +02005693 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005694 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005695 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005696
5697 if (threshold > 0)
5698 return -EINVAL;
5699
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005700 wdev = dev->ieee80211_ptr;
5701
Johannes Berg4c476992010-10-04 21:36:35 +02005702 if (!rdev->ops->set_cqm_rssi_config)
5703 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005704
Johannes Berg074ac8d2010-09-16 14:58:22 +02005705 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005706 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5707 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005708
Johannes Berg4c476992010-10-04 21:36:35 +02005709 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5710 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005711}
5712
5713static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5714{
5715 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5716 struct nlattr *cqm;
5717 int err;
5718
5719 cqm = info->attrs[NL80211_ATTR_CQM];
5720 if (!cqm) {
5721 err = -EINVAL;
5722 goto out;
5723 }
5724
5725 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5726 nl80211_attr_cqm_policy);
5727 if (err)
5728 goto out;
5729
5730 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5731 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5732 s32 threshold;
5733 u32 hysteresis;
5734 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5735 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5736 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5737 } else
5738 err = -EINVAL;
5739
5740out:
5741 return err;
5742}
5743
Johannes Berg29cbe682010-12-03 09:20:44 +01005744static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5745{
5746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5747 struct net_device *dev = info->user_ptr[1];
5748 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005749 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005750 int err;
5751
5752 /* start with default */
5753 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005754 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005755
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005756 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005757 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005758 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005759 if (err)
5760 return err;
5761 }
5762
5763 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5764 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5765 return -EINVAL;
5766
Javier Cardonac80d5452010-12-16 17:37:49 -08005767 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5768 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5769
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08005770 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5771 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5772 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5773 return -EINVAL;
5774
Javier Cardonac80d5452010-12-16 17:37:49 -08005775 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5776 /* parse additional setup parameters if given */
5777 err = nl80211_parse_mesh_setup(info, &setup);
5778 if (err)
5779 return err;
5780 }
5781
5782 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005783}
5784
5785static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5786{
5787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5788 struct net_device *dev = info->user_ptr[1];
5789
5790 return cfg80211_leave_mesh(rdev, dev);
5791}
5792
Johannes Bergff1b6e62011-05-04 15:37:28 +02005793static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5794{
5795 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5796 struct sk_buff *msg;
5797 void *hdr;
5798
5799 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5800 return -EOPNOTSUPP;
5801
5802 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5803 if (!msg)
5804 return -ENOMEM;
5805
5806 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5807 NL80211_CMD_GET_WOWLAN);
5808 if (!hdr)
5809 goto nla_put_failure;
5810
5811 if (rdev->wowlan) {
5812 struct nlattr *nl_wowlan;
5813
5814 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5815 if (!nl_wowlan)
5816 goto nla_put_failure;
5817
5818 if (rdev->wowlan->any)
5819 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5820 if (rdev->wowlan->disconnect)
5821 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5822 if (rdev->wowlan->magic_pkt)
5823 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005824 if (rdev->wowlan->gtk_rekey_failure)
5825 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5826 if (rdev->wowlan->eap_identity_req)
5827 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5828 if (rdev->wowlan->four_way_handshake)
5829 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5830 if (rdev->wowlan->rfkill_release)
5831 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005832 if (rdev->wowlan->n_patterns) {
5833 struct nlattr *nl_pats, *nl_pat;
5834 int i, pat_len;
5835
5836 nl_pats = nla_nest_start(msg,
5837 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5838 if (!nl_pats)
5839 goto nla_put_failure;
5840
5841 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5842 nl_pat = nla_nest_start(msg, i + 1);
5843 if (!nl_pat)
5844 goto nla_put_failure;
5845 pat_len = rdev->wowlan->patterns[i].pattern_len;
5846 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5847 DIV_ROUND_UP(pat_len, 8),
5848 rdev->wowlan->patterns[i].mask);
5849 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5850 pat_len,
5851 rdev->wowlan->patterns[i].pattern);
5852 nla_nest_end(msg, nl_pat);
5853 }
5854 nla_nest_end(msg, nl_pats);
5855 }
5856
5857 nla_nest_end(msg, nl_wowlan);
5858 }
5859
5860 genlmsg_end(msg, hdr);
5861 return genlmsg_reply(msg, info);
5862
5863nla_put_failure:
5864 nlmsg_free(msg);
5865 return -ENOBUFS;
5866}
5867
5868static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5869{
5870 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5871 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5872 struct cfg80211_wowlan no_triggers = {};
5873 struct cfg80211_wowlan new_triggers = {};
5874 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5875 int err, i;
5876
5877 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5878 return -EOPNOTSUPP;
5879
5880 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5881 goto no_triggers;
5882
5883 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5884 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5885 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5886 nl80211_wowlan_policy);
5887 if (err)
5888 return err;
5889
5890 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5891 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5892 return -EINVAL;
5893 new_triggers.any = true;
5894 }
5895
5896 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5897 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5898 return -EINVAL;
5899 new_triggers.disconnect = true;
5900 }
5901
5902 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5903 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5904 return -EINVAL;
5905 new_triggers.magic_pkt = true;
5906 }
5907
Johannes Berg77dbbb12011-07-13 10:48:55 +02005908 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5909 return -EINVAL;
5910
5911 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5912 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5913 return -EINVAL;
5914 new_triggers.gtk_rekey_failure = true;
5915 }
5916
5917 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5918 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5919 return -EINVAL;
5920 new_triggers.eap_identity_req = true;
5921 }
5922
5923 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5924 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5925 return -EINVAL;
5926 new_triggers.four_way_handshake = true;
5927 }
5928
5929 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5930 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5931 return -EINVAL;
5932 new_triggers.rfkill_release = true;
5933 }
5934
Johannes Bergff1b6e62011-05-04 15:37:28 +02005935 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5936 struct nlattr *pat;
5937 int n_patterns = 0;
5938 int rem, pat_len, mask_len;
5939 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5940
5941 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5942 rem)
5943 n_patterns++;
5944 if (n_patterns > wowlan->n_patterns)
5945 return -EINVAL;
5946
5947 new_triggers.patterns = kcalloc(n_patterns,
5948 sizeof(new_triggers.patterns[0]),
5949 GFP_KERNEL);
5950 if (!new_triggers.patterns)
5951 return -ENOMEM;
5952
5953 new_triggers.n_patterns = n_patterns;
5954 i = 0;
5955
5956 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5957 rem) {
5958 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5959 nla_data(pat), nla_len(pat), NULL);
5960 err = -EINVAL;
5961 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5962 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5963 goto error;
5964 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5965 mask_len = DIV_ROUND_UP(pat_len, 8);
5966 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5967 mask_len)
5968 goto error;
5969 if (pat_len > wowlan->pattern_max_len ||
5970 pat_len < wowlan->pattern_min_len)
5971 goto error;
5972
5973 new_triggers.patterns[i].mask =
5974 kmalloc(mask_len + pat_len, GFP_KERNEL);
5975 if (!new_triggers.patterns[i].mask) {
5976 err = -ENOMEM;
5977 goto error;
5978 }
5979 new_triggers.patterns[i].pattern =
5980 new_triggers.patterns[i].mask + mask_len;
5981 memcpy(new_triggers.patterns[i].mask,
5982 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5983 mask_len);
5984 new_triggers.patterns[i].pattern_len = pat_len;
5985 memcpy(new_triggers.patterns[i].pattern,
5986 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5987 pat_len);
5988 i++;
5989 }
5990 }
5991
5992 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5993 struct cfg80211_wowlan *ntrig;
5994 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5995 GFP_KERNEL);
5996 if (!ntrig) {
5997 err = -ENOMEM;
5998 goto error;
5999 }
6000 cfg80211_rdev_free_wowlan(rdev);
6001 rdev->wowlan = ntrig;
6002 } else {
6003 no_triggers:
6004 cfg80211_rdev_free_wowlan(rdev);
6005 rdev->wowlan = NULL;
6006 }
6007
6008 return 0;
6009 error:
6010 for (i = 0; i < new_triggers.n_patterns; i++)
6011 kfree(new_triggers.patterns[i].mask);
6012 kfree(new_triggers.patterns);
6013 return err;
6014}
6015
Johannes Berge5497d72011-07-05 16:35:40 +02006016static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6017{
6018 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6019 struct net_device *dev = info->user_ptr[1];
6020 struct wireless_dev *wdev = dev->ieee80211_ptr;
6021 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6022 struct cfg80211_gtk_rekey_data rekey_data;
6023 int err;
6024
6025 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6026 return -EINVAL;
6027
6028 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6029 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6030 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6031 nl80211_rekey_policy);
6032 if (err)
6033 return err;
6034
6035 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6036 return -ERANGE;
6037 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6038 return -ERANGE;
6039 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6040 return -ERANGE;
6041
6042 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6043 NL80211_KEK_LEN);
6044 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6045 NL80211_KCK_LEN);
6046 memcpy(rekey_data.replay_ctr,
6047 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6048 NL80211_REPLAY_CTR_LEN);
6049
6050 wdev_lock(wdev);
6051 if (!wdev->current_bss) {
6052 err = -ENOTCONN;
6053 goto out;
6054 }
6055
6056 if (!rdev->ops->set_rekey_data) {
6057 err = -EOPNOTSUPP;
6058 goto out;
6059 }
6060
6061 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6062 out:
6063 wdev_unlock(wdev);
6064 return err;
6065}
6066
Johannes Berg28946da2011-11-04 11:18:12 +01006067static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6068 struct genl_info *info)
6069{
6070 struct net_device *dev = info->user_ptr[1];
6071 struct wireless_dev *wdev = dev->ieee80211_ptr;
6072
6073 if (wdev->iftype != NL80211_IFTYPE_AP &&
6074 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6075 return -EINVAL;
6076
6077 if (wdev->ap_unexpected_nlpid)
6078 return -EBUSY;
6079
6080 wdev->ap_unexpected_nlpid = info->snd_pid;
6081 return 0;
6082}
6083
Johannes Berg7f6cf312011-11-04 11:18:15 +01006084static int nl80211_probe_client(struct sk_buff *skb,
6085 struct genl_info *info)
6086{
6087 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6088 struct net_device *dev = info->user_ptr[1];
6089 struct wireless_dev *wdev = dev->ieee80211_ptr;
6090 struct sk_buff *msg;
6091 void *hdr;
6092 const u8 *addr;
6093 u64 cookie;
6094 int err;
6095
6096 if (wdev->iftype != NL80211_IFTYPE_AP &&
6097 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6098 return -EOPNOTSUPP;
6099
6100 if (!info->attrs[NL80211_ATTR_MAC])
6101 return -EINVAL;
6102
6103 if (!rdev->ops->probe_client)
6104 return -EOPNOTSUPP;
6105
6106 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6107 if (!msg)
6108 return -ENOMEM;
6109
6110 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6111 NL80211_CMD_PROBE_CLIENT);
6112
6113 if (IS_ERR(hdr)) {
6114 err = PTR_ERR(hdr);
6115 goto free_msg;
6116 }
6117
6118 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6119
6120 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6121 if (err)
6122 goto free_msg;
6123
6124 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6125
6126 genlmsg_end(msg, hdr);
6127
6128 return genlmsg_reply(msg, info);
6129
6130 nla_put_failure:
6131 err = -ENOBUFS;
6132 free_msg:
6133 nlmsg_free(msg);
6134 return err;
6135}
6136
Johannes Berg5e7602302011-11-04 11:18:17 +01006137static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6138{
6139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6140
6141 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6142 return -EOPNOTSUPP;
6143
6144 if (rdev->ap_beacons_nlpid)
6145 return -EBUSY;
6146
6147 rdev->ap_beacons_nlpid = info->snd_pid;
6148
6149 return 0;
6150}
6151
Johannes Berg4c476992010-10-04 21:36:35 +02006152#define NL80211_FLAG_NEED_WIPHY 0x01
6153#define NL80211_FLAG_NEED_NETDEV 0x02
6154#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006155#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6156#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6157 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006158
6159static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6160 struct genl_info *info)
6161{
6162 struct cfg80211_registered_device *rdev;
6163 struct net_device *dev;
6164 int err;
6165 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6166
6167 if (rtnl)
6168 rtnl_lock();
6169
6170 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6171 rdev = cfg80211_get_dev_from_info(info);
6172 if (IS_ERR(rdev)) {
6173 if (rtnl)
6174 rtnl_unlock();
6175 return PTR_ERR(rdev);
6176 }
6177 info->user_ptr[0] = rdev;
6178 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006179 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6180 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006181 if (err) {
6182 if (rtnl)
6183 rtnl_unlock();
6184 return err;
6185 }
Johannes Berg41265712010-10-04 21:14:05 +02006186 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6187 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006188 cfg80211_unlock_rdev(rdev);
6189 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006190 if (rtnl)
6191 rtnl_unlock();
6192 return -ENETDOWN;
6193 }
Johannes Berg4c476992010-10-04 21:36:35 +02006194 info->user_ptr[0] = rdev;
6195 info->user_ptr[1] = dev;
6196 }
6197
6198 return 0;
6199}
6200
6201static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6202 struct genl_info *info)
6203{
6204 if (info->user_ptr[0])
6205 cfg80211_unlock_rdev(info->user_ptr[0]);
6206 if (info->user_ptr[1])
6207 dev_put(info->user_ptr[1]);
6208 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6209 rtnl_unlock();
6210}
6211
Johannes Berg55682962007-09-20 13:09:35 -04006212static struct genl_ops nl80211_ops[] = {
6213 {
6214 .cmd = NL80211_CMD_GET_WIPHY,
6215 .doit = nl80211_get_wiphy,
6216 .dumpit = nl80211_dump_wiphy,
6217 .policy = nl80211_policy,
6218 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006219 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006220 },
6221 {
6222 .cmd = NL80211_CMD_SET_WIPHY,
6223 .doit = nl80211_set_wiphy,
6224 .policy = nl80211_policy,
6225 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006226 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006227 },
6228 {
6229 .cmd = NL80211_CMD_GET_INTERFACE,
6230 .doit = nl80211_get_interface,
6231 .dumpit = nl80211_dump_interface,
6232 .policy = nl80211_policy,
6233 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006234 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006235 },
6236 {
6237 .cmd = NL80211_CMD_SET_INTERFACE,
6238 .doit = nl80211_set_interface,
6239 .policy = nl80211_policy,
6240 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006241 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6242 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006243 },
6244 {
6245 .cmd = NL80211_CMD_NEW_INTERFACE,
6246 .doit = nl80211_new_interface,
6247 .policy = nl80211_policy,
6248 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006249 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6250 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006251 },
6252 {
6253 .cmd = NL80211_CMD_DEL_INTERFACE,
6254 .doit = nl80211_del_interface,
6255 .policy = nl80211_policy,
6256 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006257 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6258 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006259 },
Johannes Berg41ade002007-12-19 02:03:29 +01006260 {
6261 .cmd = NL80211_CMD_GET_KEY,
6262 .doit = nl80211_get_key,
6263 .policy = nl80211_policy,
6264 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006265 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6266 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006267 },
6268 {
6269 .cmd = NL80211_CMD_SET_KEY,
6270 .doit = nl80211_set_key,
6271 .policy = nl80211_policy,
6272 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006273 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006274 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006275 },
6276 {
6277 .cmd = NL80211_CMD_NEW_KEY,
6278 .doit = nl80211_new_key,
6279 .policy = nl80211_policy,
6280 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006281 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006282 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006283 },
6284 {
6285 .cmd = NL80211_CMD_DEL_KEY,
6286 .doit = nl80211_del_key,
6287 .policy = nl80211_policy,
6288 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006289 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006290 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006291 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006292 {
6293 .cmd = NL80211_CMD_SET_BEACON,
6294 .policy = nl80211_policy,
6295 .flags = GENL_ADMIN_PERM,
6296 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006297 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6298 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006299 },
6300 {
6301 .cmd = NL80211_CMD_NEW_BEACON,
6302 .policy = nl80211_policy,
6303 .flags = GENL_ADMIN_PERM,
6304 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006305 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6306 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006307 },
6308 {
6309 .cmd = NL80211_CMD_DEL_BEACON,
6310 .policy = nl80211_policy,
6311 .flags = GENL_ADMIN_PERM,
6312 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006313 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6314 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006315 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006316 {
6317 .cmd = NL80211_CMD_GET_STATION,
6318 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006319 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006320 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006321 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6322 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006323 },
6324 {
6325 .cmd = NL80211_CMD_SET_STATION,
6326 .doit = nl80211_set_station,
6327 .policy = nl80211_policy,
6328 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006329 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6330 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006331 },
6332 {
6333 .cmd = NL80211_CMD_NEW_STATION,
6334 .doit = nl80211_new_station,
6335 .policy = nl80211_policy,
6336 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006337 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006338 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006339 },
6340 {
6341 .cmd = NL80211_CMD_DEL_STATION,
6342 .doit = nl80211_del_station,
6343 .policy = nl80211_policy,
6344 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006345 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6346 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006347 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006348 {
6349 .cmd = NL80211_CMD_GET_MPATH,
6350 .doit = nl80211_get_mpath,
6351 .dumpit = nl80211_dump_mpath,
6352 .policy = nl80211_policy,
6353 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006354 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006355 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006356 },
6357 {
6358 .cmd = NL80211_CMD_SET_MPATH,
6359 .doit = nl80211_set_mpath,
6360 .policy = nl80211_policy,
6361 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006362 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006363 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006364 },
6365 {
6366 .cmd = NL80211_CMD_NEW_MPATH,
6367 .doit = nl80211_new_mpath,
6368 .policy = nl80211_policy,
6369 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006370 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006371 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006372 },
6373 {
6374 .cmd = NL80211_CMD_DEL_MPATH,
6375 .doit = nl80211_del_mpath,
6376 .policy = nl80211_policy,
6377 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006378 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6379 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006380 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006381 {
6382 .cmd = NL80211_CMD_SET_BSS,
6383 .doit = nl80211_set_bss,
6384 .policy = nl80211_policy,
6385 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006386 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6387 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006388 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006389 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006390 .cmd = NL80211_CMD_GET_REG,
6391 .doit = nl80211_get_reg,
6392 .policy = nl80211_policy,
6393 /* can be retrieved by unprivileged users */
6394 },
6395 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006396 .cmd = NL80211_CMD_SET_REG,
6397 .doit = nl80211_set_reg,
6398 .policy = nl80211_policy,
6399 .flags = GENL_ADMIN_PERM,
6400 },
6401 {
6402 .cmd = NL80211_CMD_REQ_SET_REG,
6403 .doit = nl80211_req_set_reg,
6404 .policy = nl80211_policy,
6405 .flags = GENL_ADMIN_PERM,
6406 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006407 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006408 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6409 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006410 .policy = nl80211_policy,
6411 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006412 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6413 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006414 },
6415 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006416 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6417 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006418 .policy = nl80211_policy,
6419 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006420 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006421 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006422 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006423 {
Johannes Berg2a519312009-02-10 21:25:55 +01006424 .cmd = NL80211_CMD_TRIGGER_SCAN,
6425 .doit = nl80211_trigger_scan,
6426 .policy = nl80211_policy,
6427 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006428 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006429 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006430 },
6431 {
6432 .cmd = NL80211_CMD_GET_SCAN,
6433 .policy = nl80211_policy,
6434 .dumpit = nl80211_dump_scan,
6435 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006436 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006437 .cmd = NL80211_CMD_START_SCHED_SCAN,
6438 .doit = nl80211_start_sched_scan,
6439 .policy = nl80211_policy,
6440 .flags = GENL_ADMIN_PERM,
6441 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6442 NL80211_FLAG_NEED_RTNL,
6443 },
6444 {
6445 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6446 .doit = nl80211_stop_sched_scan,
6447 .policy = nl80211_policy,
6448 .flags = GENL_ADMIN_PERM,
6449 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6450 NL80211_FLAG_NEED_RTNL,
6451 },
6452 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006453 .cmd = NL80211_CMD_AUTHENTICATE,
6454 .doit = nl80211_authenticate,
6455 .policy = nl80211_policy,
6456 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006457 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006458 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006459 },
6460 {
6461 .cmd = NL80211_CMD_ASSOCIATE,
6462 .doit = nl80211_associate,
6463 .policy = nl80211_policy,
6464 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006465 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006466 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006467 },
6468 {
6469 .cmd = NL80211_CMD_DEAUTHENTICATE,
6470 .doit = nl80211_deauthenticate,
6471 .policy = nl80211_policy,
6472 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006473 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006474 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006475 },
6476 {
6477 .cmd = NL80211_CMD_DISASSOCIATE,
6478 .doit = nl80211_disassociate,
6479 .policy = nl80211_policy,
6480 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006481 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006482 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006483 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006484 {
6485 .cmd = NL80211_CMD_JOIN_IBSS,
6486 .doit = nl80211_join_ibss,
6487 .policy = nl80211_policy,
6488 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006489 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006490 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006491 },
6492 {
6493 .cmd = NL80211_CMD_LEAVE_IBSS,
6494 .doit = nl80211_leave_ibss,
6495 .policy = nl80211_policy,
6496 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006497 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006498 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006499 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006500#ifdef CONFIG_NL80211_TESTMODE
6501 {
6502 .cmd = NL80211_CMD_TESTMODE,
6503 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006504 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006505 .policy = nl80211_policy,
6506 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006507 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6508 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006509 },
6510#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006511 {
6512 .cmd = NL80211_CMD_CONNECT,
6513 .doit = nl80211_connect,
6514 .policy = nl80211_policy,
6515 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006516 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006517 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006518 },
6519 {
6520 .cmd = NL80211_CMD_DISCONNECT,
6521 .doit = nl80211_disconnect,
6522 .policy = nl80211_policy,
6523 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006524 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006525 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006526 },
Johannes Berg463d0182009-07-14 00:33:35 +02006527 {
6528 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6529 .doit = nl80211_wiphy_netns,
6530 .policy = nl80211_policy,
6531 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006532 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6533 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006534 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006535 {
6536 .cmd = NL80211_CMD_GET_SURVEY,
6537 .policy = nl80211_policy,
6538 .dumpit = nl80211_dump_survey,
6539 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006540 {
6541 .cmd = NL80211_CMD_SET_PMKSA,
6542 .doit = nl80211_setdel_pmksa,
6543 .policy = nl80211_policy,
6544 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006545 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6546 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006547 },
6548 {
6549 .cmd = NL80211_CMD_DEL_PMKSA,
6550 .doit = nl80211_setdel_pmksa,
6551 .policy = nl80211_policy,
6552 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006553 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6554 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006555 },
6556 {
6557 .cmd = NL80211_CMD_FLUSH_PMKSA,
6558 .doit = nl80211_flush_pmksa,
6559 .policy = nl80211_policy,
6560 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006561 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6562 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006563 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006564 {
6565 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6566 .doit = nl80211_remain_on_channel,
6567 .policy = nl80211_policy,
6568 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006569 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006570 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006571 },
6572 {
6573 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6574 .doit = nl80211_cancel_remain_on_channel,
6575 .policy = nl80211_policy,
6576 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006577 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006578 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006579 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006580 {
6581 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6582 .doit = nl80211_set_tx_bitrate_mask,
6583 .policy = nl80211_policy,
6584 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006585 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6586 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006587 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006588 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006589 .cmd = NL80211_CMD_REGISTER_FRAME,
6590 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006591 .policy = nl80211_policy,
6592 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006593 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6594 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006595 },
6596 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006597 .cmd = NL80211_CMD_FRAME,
6598 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006599 .policy = nl80211_policy,
6600 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006601 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006602 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006603 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006604 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006605 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6606 .doit = nl80211_tx_mgmt_cancel_wait,
6607 .policy = nl80211_policy,
6608 .flags = GENL_ADMIN_PERM,
6609 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6610 NL80211_FLAG_NEED_RTNL,
6611 },
6612 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006613 .cmd = NL80211_CMD_SET_POWER_SAVE,
6614 .doit = nl80211_set_power_save,
6615 .policy = nl80211_policy,
6616 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006617 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6618 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006619 },
6620 {
6621 .cmd = NL80211_CMD_GET_POWER_SAVE,
6622 .doit = nl80211_get_power_save,
6623 .policy = nl80211_policy,
6624 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006625 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6626 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006627 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006628 {
6629 .cmd = NL80211_CMD_SET_CQM,
6630 .doit = nl80211_set_cqm,
6631 .policy = nl80211_policy,
6632 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006633 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6634 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006635 },
Johannes Bergf444de02010-05-05 15:25:02 +02006636 {
6637 .cmd = NL80211_CMD_SET_CHANNEL,
6638 .doit = nl80211_set_channel,
6639 .policy = nl80211_policy,
6640 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006641 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6642 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006643 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006644 {
6645 .cmd = NL80211_CMD_SET_WDS_PEER,
6646 .doit = nl80211_set_wds_peer,
6647 .policy = nl80211_policy,
6648 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006649 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6650 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006651 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006652 {
6653 .cmd = NL80211_CMD_JOIN_MESH,
6654 .doit = nl80211_join_mesh,
6655 .policy = nl80211_policy,
6656 .flags = GENL_ADMIN_PERM,
6657 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6658 NL80211_FLAG_NEED_RTNL,
6659 },
6660 {
6661 .cmd = NL80211_CMD_LEAVE_MESH,
6662 .doit = nl80211_leave_mesh,
6663 .policy = nl80211_policy,
6664 .flags = GENL_ADMIN_PERM,
6665 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6666 NL80211_FLAG_NEED_RTNL,
6667 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006668 {
6669 .cmd = NL80211_CMD_GET_WOWLAN,
6670 .doit = nl80211_get_wowlan,
6671 .policy = nl80211_policy,
6672 /* can be retrieved by unprivileged users */
6673 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6674 NL80211_FLAG_NEED_RTNL,
6675 },
6676 {
6677 .cmd = NL80211_CMD_SET_WOWLAN,
6678 .doit = nl80211_set_wowlan,
6679 .policy = nl80211_policy,
6680 .flags = GENL_ADMIN_PERM,
6681 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6682 NL80211_FLAG_NEED_RTNL,
6683 },
Johannes Berge5497d72011-07-05 16:35:40 +02006684 {
6685 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6686 .doit = nl80211_set_rekey_data,
6687 .policy = nl80211_policy,
6688 .flags = GENL_ADMIN_PERM,
6689 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6690 NL80211_FLAG_NEED_RTNL,
6691 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006692 {
6693 .cmd = NL80211_CMD_TDLS_MGMT,
6694 .doit = nl80211_tdls_mgmt,
6695 .policy = nl80211_policy,
6696 .flags = GENL_ADMIN_PERM,
6697 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6698 NL80211_FLAG_NEED_RTNL,
6699 },
6700 {
6701 .cmd = NL80211_CMD_TDLS_OPER,
6702 .doit = nl80211_tdls_oper,
6703 .policy = nl80211_policy,
6704 .flags = GENL_ADMIN_PERM,
6705 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6706 NL80211_FLAG_NEED_RTNL,
6707 },
Johannes Berg28946da2011-11-04 11:18:12 +01006708 {
6709 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6710 .doit = nl80211_register_unexpected_frame,
6711 .policy = nl80211_policy,
6712 .flags = GENL_ADMIN_PERM,
6713 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6714 NL80211_FLAG_NEED_RTNL,
6715 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006716 {
6717 .cmd = NL80211_CMD_PROBE_CLIENT,
6718 .doit = nl80211_probe_client,
6719 .policy = nl80211_policy,
6720 .flags = GENL_ADMIN_PERM,
6721 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6722 NL80211_FLAG_NEED_RTNL,
6723 },
Johannes Berg5e7602302011-11-04 11:18:17 +01006724 {
6725 .cmd = NL80211_CMD_REGISTER_BEACONS,
6726 .doit = nl80211_register_beacons,
6727 .policy = nl80211_policy,
6728 .flags = GENL_ADMIN_PERM,
6729 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6730 NL80211_FLAG_NEED_RTNL,
6731 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006732 {
6733 .cmd = NL80211_CMD_SET_NOACK_MAP,
6734 .doit = nl80211_set_noack_map,
6735 .policy = nl80211_policy,
6736 .flags = GENL_ADMIN_PERM,
6737 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6738 NL80211_FLAG_NEED_RTNL,
6739 },
6740
Johannes Berg55682962007-09-20 13:09:35 -04006741};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006742
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006743static struct genl_multicast_group nl80211_mlme_mcgrp = {
6744 .name = "mlme",
6745};
Johannes Berg55682962007-09-20 13:09:35 -04006746
6747/* multicast groups */
6748static struct genl_multicast_group nl80211_config_mcgrp = {
6749 .name = "config",
6750};
Johannes Berg2a519312009-02-10 21:25:55 +01006751static struct genl_multicast_group nl80211_scan_mcgrp = {
6752 .name = "scan",
6753};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006754static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6755 .name = "regulatory",
6756};
Johannes Berg55682962007-09-20 13:09:35 -04006757
6758/* notification functions */
6759
6760void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6761{
6762 struct sk_buff *msg;
6763
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006764 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006765 if (!msg)
6766 return;
6767
6768 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6769 nlmsg_free(msg);
6770 return;
6771 }
6772
Johannes Berg463d0182009-07-14 00:33:35 +02006773 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6774 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006775}
6776
Johannes Berg362a4152009-05-24 16:43:15 +02006777static int nl80211_add_scan_req(struct sk_buff *msg,
6778 struct cfg80211_registered_device *rdev)
6779{
6780 struct cfg80211_scan_request *req = rdev->scan_req;
6781 struct nlattr *nest;
6782 int i;
6783
Johannes Berg667503dd2009-07-07 03:56:11 +02006784 ASSERT_RDEV_LOCK(rdev);
6785
Johannes Berg362a4152009-05-24 16:43:15 +02006786 if (WARN_ON(!req))
6787 return 0;
6788
6789 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6790 if (!nest)
6791 goto nla_put_failure;
6792 for (i = 0; i < req->n_ssids; i++)
6793 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6794 nla_nest_end(msg, nest);
6795
6796 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6797 if (!nest)
6798 goto nla_put_failure;
6799 for (i = 0; i < req->n_channels; i++)
6800 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6801 nla_nest_end(msg, nest);
6802
6803 if (req->ie)
6804 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6805
6806 return 0;
6807 nla_put_failure:
6808 return -ENOBUFS;
6809}
6810
Johannes Berga538e2d2009-06-16 19:56:42 +02006811static int nl80211_send_scan_msg(struct sk_buff *msg,
6812 struct cfg80211_registered_device *rdev,
6813 struct net_device *netdev,
6814 u32 pid, u32 seq, int flags,
6815 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006816{
6817 void *hdr;
6818
6819 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6820 if (!hdr)
6821 return -1;
6822
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006823 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006824 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6825
Johannes Berg362a4152009-05-24 16:43:15 +02006826 /* ignore errors and send incomplete event anyway */
6827 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006828
6829 return genlmsg_end(msg, hdr);
6830
6831 nla_put_failure:
6832 genlmsg_cancel(msg, hdr);
6833 return -EMSGSIZE;
6834}
6835
Luciano Coelho807f8a82011-05-11 17:09:35 +03006836static int
6837nl80211_send_sched_scan_msg(struct sk_buff *msg,
6838 struct cfg80211_registered_device *rdev,
6839 struct net_device *netdev,
6840 u32 pid, u32 seq, int flags, u32 cmd)
6841{
6842 void *hdr;
6843
6844 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6845 if (!hdr)
6846 return -1;
6847
6848 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6849 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6850
6851 return genlmsg_end(msg, hdr);
6852
6853 nla_put_failure:
6854 genlmsg_cancel(msg, hdr);
6855 return -EMSGSIZE;
6856}
6857
Johannes Berga538e2d2009-06-16 19:56:42 +02006858void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6859 struct net_device *netdev)
6860{
6861 struct sk_buff *msg;
6862
6863 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6864 if (!msg)
6865 return;
6866
6867 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6868 NL80211_CMD_TRIGGER_SCAN) < 0) {
6869 nlmsg_free(msg);
6870 return;
6871 }
6872
Johannes Berg463d0182009-07-14 00:33:35 +02006873 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6874 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006875}
6876
Johannes Berg2a519312009-02-10 21:25:55 +01006877void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6878 struct net_device *netdev)
6879{
6880 struct sk_buff *msg;
6881
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006883 if (!msg)
6884 return;
6885
Johannes Berga538e2d2009-06-16 19:56:42 +02006886 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6887 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006888 nlmsg_free(msg);
6889 return;
6890 }
6891
Johannes Berg463d0182009-07-14 00:33:35 +02006892 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6893 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006894}
6895
6896void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6897 struct net_device *netdev)
6898{
6899 struct sk_buff *msg;
6900
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006901 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006902 if (!msg)
6903 return;
6904
Johannes Berga538e2d2009-06-16 19:56:42 +02006905 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6906 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006907 nlmsg_free(msg);
6908 return;
6909 }
6910
Johannes Berg463d0182009-07-14 00:33:35 +02006911 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6912 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006913}
6914
Luciano Coelho807f8a82011-05-11 17:09:35 +03006915void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6916 struct net_device *netdev)
6917{
6918 struct sk_buff *msg;
6919
6920 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6921 if (!msg)
6922 return;
6923
6924 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6925 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6926 nlmsg_free(msg);
6927 return;
6928 }
6929
6930 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6931 nl80211_scan_mcgrp.id, GFP_KERNEL);
6932}
6933
6934void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6935 struct net_device *netdev, u32 cmd)
6936{
6937 struct sk_buff *msg;
6938
6939 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6940 if (!msg)
6941 return;
6942
6943 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6944 nlmsg_free(msg);
6945 return;
6946 }
6947
6948 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6949 nl80211_scan_mcgrp.id, GFP_KERNEL);
6950}
6951
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006952/*
6953 * This can happen on global regulatory changes or device specific settings
6954 * based on custom world regulatory domains.
6955 */
6956void nl80211_send_reg_change_event(struct regulatory_request *request)
6957{
6958 struct sk_buff *msg;
6959 void *hdr;
6960
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006962 if (!msg)
6963 return;
6964
6965 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6966 if (!hdr) {
6967 nlmsg_free(msg);
6968 return;
6969 }
6970
6971 /* Userspace can always count this one always being set */
6972 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6973
6974 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6975 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6976 NL80211_REGDOM_TYPE_WORLD);
6977 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6978 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6979 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6980 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6981 request->intersect)
6982 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6983 NL80211_REGDOM_TYPE_INTERSECTION);
6984 else {
6985 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6986 NL80211_REGDOM_TYPE_COUNTRY);
6987 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6988 }
6989
6990 if (wiphy_idx_valid(request->wiphy_idx))
6991 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6992
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006993 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006994
Johannes Bergbc43b282009-07-25 10:54:13 +02006995 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006996 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006997 GFP_ATOMIC);
6998 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006999
7000 return;
7001
7002nla_put_failure:
7003 genlmsg_cancel(msg, hdr);
7004 nlmsg_free(msg);
7005}
7006
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007007static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7008 struct net_device *netdev,
7009 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007010 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007011{
7012 struct sk_buff *msg;
7013 void *hdr;
7014
Johannes Berge6d6e342009-07-01 21:26:47 +02007015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007016 if (!msg)
7017 return;
7018
7019 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7020 if (!hdr) {
7021 nlmsg_free(msg);
7022 return;
7023 }
7024
7025 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7026 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7027 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7028
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007029 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007030
Johannes Berg463d0182009-07-14 00:33:35 +02007031 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7032 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007033 return;
7034
7035 nla_put_failure:
7036 genlmsg_cancel(msg, hdr);
7037 nlmsg_free(msg);
7038}
7039
7040void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007041 struct net_device *netdev, const u8 *buf,
7042 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007043{
7044 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007045 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007046}
7047
7048void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7049 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007050 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007051{
Johannes Berge6d6e342009-07-01 21:26:47 +02007052 nl80211_send_mlme_event(rdev, netdev, buf, len,
7053 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007054}
7055
Jouni Malinen53b46b82009-03-27 20:53:56 +02007056void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007057 struct net_device *netdev, const u8 *buf,
7058 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007059{
7060 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007061 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007062}
7063
Jouni Malinen53b46b82009-03-27 20:53:56 +02007064void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7065 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007066 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007067{
7068 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007069 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007070}
7071
Jouni Malinencf4e5942010-12-16 00:52:40 +02007072void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7073 struct net_device *netdev, const u8 *buf,
7074 size_t len, gfp_t gfp)
7075{
7076 nl80211_send_mlme_event(rdev, netdev, buf, len,
7077 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7078}
7079
7080void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7081 struct net_device *netdev, const u8 *buf,
7082 size_t len, gfp_t gfp)
7083{
7084 nl80211_send_mlme_event(rdev, netdev, buf, len,
7085 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7086}
7087
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007088static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7089 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007090 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007091{
7092 struct sk_buff *msg;
7093 void *hdr;
7094
Johannes Berge6d6e342009-07-01 21:26:47 +02007095 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007096 if (!msg)
7097 return;
7098
7099 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7100 if (!hdr) {
7101 nlmsg_free(msg);
7102 return;
7103 }
7104
7105 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7106 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7107 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7108 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7109
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007110 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007111
Johannes Berg463d0182009-07-14 00:33:35 +02007112 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7113 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007114 return;
7115
7116 nla_put_failure:
7117 genlmsg_cancel(msg, hdr);
7118 nlmsg_free(msg);
7119}
7120
7121void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007122 struct net_device *netdev, const u8 *addr,
7123 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007124{
7125 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007126 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007127}
7128
7129void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007130 struct net_device *netdev, const u8 *addr,
7131 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007132{
Johannes Berge6d6e342009-07-01 21:26:47 +02007133 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7134 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007135}
7136
Samuel Ortizb23aa672009-07-01 21:26:54 +02007137void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7138 struct net_device *netdev, const u8 *bssid,
7139 const u8 *req_ie, size_t req_ie_len,
7140 const u8 *resp_ie, size_t resp_ie_len,
7141 u16 status, gfp_t gfp)
7142{
7143 struct sk_buff *msg;
7144 void *hdr;
7145
7146 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7147 if (!msg)
7148 return;
7149
7150 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7151 if (!hdr) {
7152 nlmsg_free(msg);
7153 return;
7154 }
7155
7156 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7157 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7158 if (bssid)
7159 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7160 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7161 if (req_ie)
7162 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7163 if (resp_ie)
7164 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7165
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007166 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007167
Johannes Berg463d0182009-07-14 00:33:35 +02007168 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7169 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007170 return;
7171
7172 nla_put_failure:
7173 genlmsg_cancel(msg, hdr);
7174 nlmsg_free(msg);
7175
7176}
7177
7178void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7179 struct net_device *netdev, const u8 *bssid,
7180 const u8 *req_ie, size_t req_ie_len,
7181 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7182{
7183 struct sk_buff *msg;
7184 void *hdr;
7185
7186 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7187 if (!msg)
7188 return;
7189
7190 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7191 if (!hdr) {
7192 nlmsg_free(msg);
7193 return;
7194 }
7195
7196 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7197 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7198 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7199 if (req_ie)
7200 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7201 if (resp_ie)
7202 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7203
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007204 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007205
Johannes Berg463d0182009-07-14 00:33:35 +02007206 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7207 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007208 return;
7209
7210 nla_put_failure:
7211 genlmsg_cancel(msg, hdr);
7212 nlmsg_free(msg);
7213
7214}
7215
7216void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7217 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007218 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007219{
7220 struct sk_buff *msg;
7221 void *hdr;
7222
Johannes Berg667503dd2009-07-07 03:56:11 +02007223 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007224 if (!msg)
7225 return;
7226
7227 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7228 if (!hdr) {
7229 nlmsg_free(msg);
7230 return;
7231 }
7232
7233 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7235 if (from_ap && reason)
7236 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7237 if (from_ap)
7238 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7239 if (ie)
7240 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7241
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007242 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007243
Johannes Berg463d0182009-07-14 00:33:35 +02007244 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7245 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007246 return;
7247
7248 nla_put_failure:
7249 genlmsg_cancel(msg, hdr);
7250 nlmsg_free(msg);
7251
7252}
7253
Johannes Berg04a773a2009-04-19 21:24:32 +02007254void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7255 struct net_device *netdev, const u8 *bssid,
7256 gfp_t gfp)
7257{
7258 struct sk_buff *msg;
7259 void *hdr;
7260
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007262 if (!msg)
7263 return;
7264
7265 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7266 if (!hdr) {
7267 nlmsg_free(msg);
7268 return;
7269 }
7270
7271 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7272 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7273 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7274
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007275 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007276
Johannes Berg463d0182009-07-14 00:33:35 +02007277 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7278 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007279 return;
7280
7281 nla_put_failure:
7282 genlmsg_cancel(msg, hdr);
7283 nlmsg_free(msg);
7284}
7285
Javier Cardonac93b5e72011-04-07 15:08:34 -07007286void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7287 struct net_device *netdev,
7288 const u8 *macaddr, const u8* ie, u8 ie_len,
7289 gfp_t gfp)
7290{
7291 struct sk_buff *msg;
7292 void *hdr;
7293
7294 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7295 if (!msg)
7296 return;
7297
7298 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7299 if (!hdr) {
7300 nlmsg_free(msg);
7301 return;
7302 }
7303
7304 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7305 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7306 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7307 if (ie_len && ie)
7308 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7309
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007310 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007311
7312 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7313 nl80211_mlme_mcgrp.id, gfp);
7314 return;
7315
7316 nla_put_failure:
7317 genlmsg_cancel(msg, hdr);
7318 nlmsg_free(msg);
7319}
7320
Jouni Malinena3b8b052009-03-27 21:59:49 +02007321void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7322 struct net_device *netdev, const u8 *addr,
7323 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007324 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007325{
7326 struct sk_buff *msg;
7327 void *hdr;
7328
Johannes Berge6d6e342009-07-01 21:26:47 +02007329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007330 if (!msg)
7331 return;
7332
7333 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7334 if (!hdr) {
7335 nlmsg_free(msg);
7336 return;
7337 }
7338
7339 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7340 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7341 if (addr)
7342 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7343 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007344 if (key_id != -1)
7345 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007346 if (tsc)
7347 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7348
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007349 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007350
Johannes Berg463d0182009-07-14 00:33:35 +02007351 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7352 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007353 return;
7354
7355 nla_put_failure:
7356 genlmsg_cancel(msg, hdr);
7357 nlmsg_free(msg);
7358}
7359
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007360void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7361 struct ieee80211_channel *channel_before,
7362 struct ieee80211_channel *channel_after)
7363{
7364 struct sk_buff *msg;
7365 void *hdr;
7366 struct nlattr *nl_freq;
7367
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007369 if (!msg)
7370 return;
7371
7372 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7373 if (!hdr) {
7374 nlmsg_free(msg);
7375 return;
7376 }
7377
7378 /*
7379 * Since we are applying the beacon hint to a wiphy we know its
7380 * wiphy_idx is valid
7381 */
7382 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7383
7384 /* Before */
7385 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7386 if (!nl_freq)
7387 goto nla_put_failure;
7388 if (nl80211_msg_put_channel(msg, channel_before))
7389 goto nla_put_failure;
7390 nla_nest_end(msg, nl_freq);
7391
7392 /* After */
7393 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7394 if (!nl_freq)
7395 goto nla_put_failure;
7396 if (nl80211_msg_put_channel(msg, channel_after))
7397 goto nla_put_failure;
7398 nla_nest_end(msg, nl_freq);
7399
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007400 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007401
Johannes Berg463d0182009-07-14 00:33:35 +02007402 rcu_read_lock();
7403 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7404 GFP_ATOMIC);
7405 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007406
7407 return;
7408
7409nla_put_failure:
7410 genlmsg_cancel(msg, hdr);
7411 nlmsg_free(msg);
7412}
7413
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007414static void nl80211_send_remain_on_chan_event(
7415 int cmd, struct cfg80211_registered_device *rdev,
7416 struct net_device *netdev, u64 cookie,
7417 struct ieee80211_channel *chan,
7418 enum nl80211_channel_type channel_type,
7419 unsigned int duration, gfp_t gfp)
7420{
7421 struct sk_buff *msg;
7422 void *hdr;
7423
7424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7425 if (!msg)
7426 return;
7427
7428 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7429 if (!hdr) {
7430 nlmsg_free(msg);
7431 return;
7432 }
7433
7434 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7435 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7436 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7437 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7438 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7439
7440 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7441 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7442
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007443 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007444
7445 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7446 nl80211_mlme_mcgrp.id, gfp);
7447 return;
7448
7449 nla_put_failure:
7450 genlmsg_cancel(msg, hdr);
7451 nlmsg_free(msg);
7452}
7453
7454void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7455 struct net_device *netdev, u64 cookie,
7456 struct ieee80211_channel *chan,
7457 enum nl80211_channel_type channel_type,
7458 unsigned int duration, gfp_t gfp)
7459{
7460 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7461 rdev, netdev, cookie, chan,
7462 channel_type, duration, gfp);
7463}
7464
7465void nl80211_send_remain_on_channel_cancel(
7466 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7467 u64 cookie, struct ieee80211_channel *chan,
7468 enum nl80211_channel_type channel_type, gfp_t gfp)
7469{
7470 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7471 rdev, netdev, cookie, chan,
7472 channel_type, 0, gfp);
7473}
7474
Johannes Berg98b62182009-12-23 13:15:44 +01007475void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7476 struct net_device *dev, const u8 *mac_addr,
7477 struct station_info *sinfo, gfp_t gfp)
7478{
7479 struct sk_buff *msg;
7480
7481 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7482 if (!msg)
7483 return;
7484
7485 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7486 nlmsg_free(msg);
7487 return;
7488 }
7489
7490 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7491 nl80211_mlme_mcgrp.id, gfp);
7492}
7493
Jouni Malinenec15e682011-03-23 15:29:52 +02007494void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7495 struct net_device *dev, const u8 *mac_addr,
7496 gfp_t gfp)
7497{
7498 struct sk_buff *msg;
7499 void *hdr;
7500
7501 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7502 if (!msg)
7503 return;
7504
7505 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7506 if (!hdr) {
7507 nlmsg_free(msg);
7508 return;
7509 }
7510
7511 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7512 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7513
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007514 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007515
7516 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7517 nl80211_mlme_mcgrp.id, gfp);
7518 return;
7519
7520 nla_put_failure:
7521 genlmsg_cancel(msg, hdr);
7522 nlmsg_free(msg);
7523}
7524
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007525static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7526 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007527{
7528 struct wireless_dev *wdev = dev->ieee80211_ptr;
7529 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7530 struct sk_buff *msg;
7531 void *hdr;
7532 int err;
7533 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7534
7535 if (!nlpid)
7536 return false;
7537
7538 msg = nlmsg_new(100, gfp);
7539 if (!msg)
7540 return true;
7541
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007542 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007543 if (!hdr) {
7544 nlmsg_free(msg);
7545 return true;
7546 }
7547
7548 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7549 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7550 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7551
7552 err = genlmsg_end(msg, hdr);
7553 if (err < 0) {
7554 nlmsg_free(msg);
7555 return true;
7556 }
7557
7558 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7559 return true;
7560
7561 nla_put_failure:
7562 genlmsg_cancel(msg, hdr);
7563 nlmsg_free(msg);
7564 return true;
7565}
7566
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007567bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7568{
7569 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7570 addr, gfp);
7571}
7572
7573bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7574 const u8 *addr, gfp_t gfp)
7575{
7576 return __nl80211_unexpected_frame(dev,
7577 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7578 addr, gfp);
7579}
7580
Johannes Berg2e161f72010-08-12 15:38:38 +02007581int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7582 struct net_device *netdev, u32 nlpid,
7583 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007584{
7585 struct sk_buff *msg;
7586 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007587
7588 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7589 if (!msg)
7590 return -ENOMEM;
7591
Johannes Berg2e161f72010-08-12 15:38:38 +02007592 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007593 if (!hdr) {
7594 nlmsg_free(msg);
7595 return -ENOMEM;
7596 }
7597
7598 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7599 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7600 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7601 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7602
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007603 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007604
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007605 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007606
7607 nla_put_failure:
7608 genlmsg_cancel(msg, hdr);
7609 nlmsg_free(msg);
7610 return -ENOBUFS;
7611}
7612
Johannes Berg2e161f72010-08-12 15:38:38 +02007613void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7614 struct net_device *netdev, u64 cookie,
7615 const u8 *buf, size_t len, bool ack,
7616 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007617{
7618 struct sk_buff *msg;
7619 void *hdr;
7620
7621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7622 if (!msg)
7623 return;
7624
Johannes Berg2e161f72010-08-12 15:38:38 +02007625 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007626 if (!hdr) {
7627 nlmsg_free(msg);
7628 return;
7629 }
7630
7631 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7632 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7633 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7634 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7635 if (ack)
7636 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7637
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007638 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007639
7640 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7641 return;
7642
7643 nla_put_failure:
7644 genlmsg_cancel(msg, hdr);
7645 nlmsg_free(msg);
7646}
7647
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007648void
7649nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7650 struct net_device *netdev,
7651 enum nl80211_cqm_rssi_threshold_event rssi_event,
7652 gfp_t gfp)
7653{
7654 struct sk_buff *msg;
7655 struct nlattr *pinfoattr;
7656 void *hdr;
7657
7658 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7659 if (!msg)
7660 return;
7661
7662 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7663 if (!hdr) {
7664 nlmsg_free(msg);
7665 return;
7666 }
7667
7668 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7669 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7670
7671 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7672 if (!pinfoattr)
7673 goto nla_put_failure;
7674
7675 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7676 rssi_event);
7677
7678 nla_nest_end(msg, pinfoattr);
7679
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007680 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007681
7682 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7683 nl80211_mlme_mcgrp.id, gfp);
7684 return;
7685
7686 nla_put_failure:
7687 genlmsg_cancel(msg, hdr);
7688 nlmsg_free(msg);
7689}
7690
Johannes Berge5497d72011-07-05 16:35:40 +02007691void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7692 struct net_device *netdev, const u8 *bssid,
7693 const u8 *replay_ctr, gfp_t gfp)
7694{
7695 struct sk_buff *msg;
7696 struct nlattr *rekey_attr;
7697 void *hdr;
7698
7699 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7700 if (!msg)
7701 return;
7702
7703 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7704 if (!hdr) {
7705 nlmsg_free(msg);
7706 return;
7707 }
7708
7709 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7710 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7711 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7712
7713 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7714 if (!rekey_attr)
7715 goto nla_put_failure;
7716
7717 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7718 NL80211_REPLAY_CTR_LEN, replay_ctr);
7719
7720 nla_nest_end(msg, rekey_attr);
7721
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007722 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007723
7724 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7725 nl80211_mlme_mcgrp.id, gfp);
7726 return;
7727
7728 nla_put_failure:
7729 genlmsg_cancel(msg, hdr);
7730 nlmsg_free(msg);
7731}
7732
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007733void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7734 struct net_device *netdev, int index,
7735 const u8 *bssid, bool preauth, gfp_t gfp)
7736{
7737 struct sk_buff *msg;
7738 struct nlattr *attr;
7739 void *hdr;
7740
7741 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7742 if (!msg)
7743 return;
7744
7745 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7746 if (!hdr) {
7747 nlmsg_free(msg);
7748 return;
7749 }
7750
7751 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7752 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7753
7754 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7755 if (!attr)
7756 goto nla_put_failure;
7757
7758 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7759 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7760 if (preauth)
7761 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7762
7763 nla_nest_end(msg, attr);
7764
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007765 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007766
7767 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7768 nl80211_mlme_mcgrp.id, gfp);
7769 return;
7770
7771 nla_put_failure:
7772 genlmsg_cancel(msg, hdr);
7773 nlmsg_free(msg);
7774}
7775
Johannes Bergc063dbf2010-11-24 08:10:05 +01007776void
7777nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7778 struct net_device *netdev, const u8 *peer,
7779 u32 num_packets, gfp_t gfp)
7780{
7781 struct sk_buff *msg;
7782 struct nlattr *pinfoattr;
7783 void *hdr;
7784
7785 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7786 if (!msg)
7787 return;
7788
7789 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7790 if (!hdr) {
7791 nlmsg_free(msg);
7792 return;
7793 }
7794
7795 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7796 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7797 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7798
7799 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7800 if (!pinfoattr)
7801 goto nla_put_failure;
7802
7803 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7804
7805 nla_nest_end(msg, pinfoattr);
7806
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007807 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007808
7809 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7810 nl80211_mlme_mcgrp.id, gfp);
7811 return;
7812
7813 nla_put_failure:
7814 genlmsg_cancel(msg, hdr);
7815 nlmsg_free(msg);
7816}
7817
Johannes Berg7f6cf312011-11-04 11:18:15 +01007818void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7819 u64 cookie, bool acked, gfp_t gfp)
7820{
7821 struct wireless_dev *wdev = dev->ieee80211_ptr;
7822 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7823 struct sk_buff *msg;
7824 void *hdr;
7825 int err;
7826
7827 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7828 if (!msg)
7829 return;
7830
7831 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7832 if (!hdr) {
7833 nlmsg_free(msg);
7834 return;
7835 }
7836
7837 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7838 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7839 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7840 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7841 if (acked)
7842 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7843
7844 err = genlmsg_end(msg, hdr);
7845 if (err < 0) {
7846 nlmsg_free(msg);
7847 return;
7848 }
7849
7850 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7851 nl80211_mlme_mcgrp.id, gfp);
7852 return;
7853
7854 nla_put_failure:
7855 genlmsg_cancel(msg, hdr);
7856 nlmsg_free(msg);
7857}
7858EXPORT_SYMBOL(cfg80211_probe_status);
7859
Johannes Berg5e7602302011-11-04 11:18:17 +01007860void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7861 const u8 *frame, size_t len,
7862 int freq, gfp_t gfp)
7863{
7864 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7865 struct sk_buff *msg;
7866 void *hdr;
7867 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7868
7869 if (!nlpid)
7870 return;
7871
7872 msg = nlmsg_new(len + 100, gfp);
7873 if (!msg)
7874 return;
7875
7876 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7877 if (!hdr) {
7878 nlmsg_free(msg);
7879 return;
7880 }
7881
7882 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7883 if (freq)
7884 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7885 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7886
7887 genlmsg_end(msg, hdr);
7888
7889 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7890 return;
7891
7892 nla_put_failure:
7893 genlmsg_cancel(msg, hdr);
7894 nlmsg_free(msg);
7895}
7896EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7897
Jouni Malinen026331c2010-02-15 12:53:10 +02007898static int nl80211_netlink_notify(struct notifier_block * nb,
7899 unsigned long state,
7900 void *_notify)
7901{
7902 struct netlink_notify *notify = _notify;
7903 struct cfg80211_registered_device *rdev;
7904 struct wireless_dev *wdev;
7905
7906 if (state != NETLINK_URELEASE)
7907 return NOTIFY_DONE;
7908
7909 rcu_read_lock();
7910
Johannes Berg5e7602302011-11-04 11:18:17 +01007911 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007912 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007913 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e7602302011-11-04 11:18:17 +01007914 if (rdev->ap_beacons_nlpid == notify->pid)
7915 rdev->ap_beacons_nlpid = 0;
7916 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007917
7918 rcu_read_unlock();
7919
7920 return NOTIFY_DONE;
7921}
7922
7923static struct notifier_block nl80211_netlink_notifier = {
7924 .notifier_call = nl80211_netlink_notify,
7925};
7926
Johannes Berg55682962007-09-20 13:09:35 -04007927/* initialisation/exit functions */
7928
7929int nl80211_init(void)
7930{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007931 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007932
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007933 err = genl_register_family_with_ops(&nl80211_fam,
7934 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007935 if (err)
7936 return err;
7937
Johannes Berg55682962007-09-20 13:09:35 -04007938 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7939 if (err)
7940 goto err_out;
7941
Johannes Berg2a519312009-02-10 21:25:55 +01007942 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7943 if (err)
7944 goto err_out;
7945
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007946 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7947 if (err)
7948 goto err_out;
7949
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007950 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7951 if (err)
7952 goto err_out;
7953
Johannes Bergaff89a92009-07-01 21:26:51 +02007954#ifdef CONFIG_NL80211_TESTMODE
7955 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7956 if (err)
7957 goto err_out;
7958#endif
7959
Jouni Malinen026331c2010-02-15 12:53:10 +02007960 err = netlink_register_notifier(&nl80211_netlink_notifier);
7961 if (err)
7962 goto err_out;
7963
Johannes Berg55682962007-09-20 13:09:35 -04007964 return 0;
7965 err_out:
7966 genl_unregister_family(&nl80211_fam);
7967 return err;
7968}
7969
7970void nl80211_exit(void)
7971{
Jouni Malinen026331c2010-02-15 12:53:10 +02007972 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007973 genl_unregister_family(&nl80211_fam);
7974}