blob: 067c9fe02a7f507013b2a382d19d9f0482a6e787 [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
Johannes Berga9455402012-06-15 13:32:49 +020073static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +020074__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +020075{
Johannes Berg7fee4772012-06-15 14:09:58 +020076 struct cfg80211_registered_device *rdev = NULL, *tmp;
77 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +020078
79 assert_cfg80211_lock();
80
Johannes Berg878d9ec2012-06-15 14:18:32 +020081 if (!attrs[NL80211_ATTR_WIPHY] &&
82 !attrs[NL80211_ATTR_IFINDEX])
Johannes Berg7fee4772012-06-15 14:09:58 +020083 return ERR_PTR(-EINVAL);
84
Johannes Berg878d9ec2012-06-15 14:18:32 +020085 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +020086 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +020087 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +020088
Johannes Berg878d9ec2012-06-15 14:18:32 +020089 if (attrs[NL80211_ATTR_IFINDEX]) {
90 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +020091 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +020092 if (netdev) {
93 if (netdev->ieee80211_ptr)
94 tmp = wiphy_to_dev(
95 netdev->ieee80211_ptr->wiphy);
96 else
97 tmp = NULL;
98
99 dev_put(netdev);
100
101 /* not wireless device -- return error */
102 if (!tmp)
103 return ERR_PTR(-EINVAL);
104
105 /* mismatch -- return error */
106 if (rdev && tmp != rdev)
107 return ERR_PTR(-EINVAL);
108
109 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200110 }
Johannes Berga9455402012-06-15 13:32:49 +0200111 }
112
Johannes Berg4f7eff12012-06-15 14:14:22 +0200113 if (!rdev)
114 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200115
Johannes Berg4f7eff12012-06-15 14:14:22 +0200116 if (netns != wiphy_net(&rdev->wiphy))
117 return ERR_PTR(-ENODEV);
118
119 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200120}
121
122/*
123 * This function returns a pointer to the driver
124 * that the genl_info item that is passed refers to.
125 * If successful, it returns non-NULL and also locks
126 * the driver's mutex!
127 *
128 * This means that you need to call cfg80211_unlock_rdev()
129 * before being allowed to acquire &cfg80211_mutex!
130 *
131 * This is necessary because we need to lock the global
132 * mutex to get an item off the list safely, and then
133 * we lock the rdev mutex so it doesn't go away under us.
134 *
135 * We don't want to keep cfg80211_mutex locked
136 * for all the time in order to allow requests on
137 * other interfaces to go through at the same time.
138 *
139 * The result of this can be a PTR_ERR and hence must
140 * be checked with IS_ERR() for errors.
141 */
142static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200143cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200144{
145 struct cfg80211_registered_device *rdev;
146
147 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200149
150 /* if it is not an error we grab the lock on
151 * it to assure it won't be going away while
152 * we operate on it */
153 if (!IS_ERR(rdev))
154 mutex_lock(&rdev->mtx);
155
156 mutex_unlock(&cfg80211_mutex);
157
158 return rdev;
159}
160
Johannes Berg55682962007-09-20 13:09:35 -0400161/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000162static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400163 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
164 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700165 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200166 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200167 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530168 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200169 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
170 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
171 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
172 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100173 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400174
175 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
176 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
177 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100178
Eliad Pellere007b852011-11-24 18:13:56 +0200179 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
180 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100181
Johannes Bergb9454e82009-07-08 13:29:08 +0200182 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100183 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
184 .len = WLAN_MAX_KEY_LEN },
185 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
186 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
187 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200188 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200189 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100190
191 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
192 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
193 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
194 .len = IEEE80211_MAX_DATA_LEN },
195 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
196 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100197 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
198 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
199 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
200 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
201 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100202 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100203 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200204 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100205 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800206 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100207 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300208
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700209 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
210 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
211
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300212 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
213 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
214 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200215 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
216 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100217 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300218
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800219 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700220 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700221
Johannes Berg6c739412011-11-03 09:27:01 +0100222 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200223
224 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
225 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
226 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100227 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
228 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200229
230 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
231 .len = IEEE80211_MAX_SSID_LEN },
232 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
233 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200234 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300235 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300236 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300237 [NL80211_ATTR_STA_FLAGS2] = {
238 .len = sizeof(struct nl80211_sta_flag_update),
239 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300240 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300241 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
242 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200243 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
244 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
245 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200246 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100247 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100248 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
249 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100250 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
251 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200252 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200253 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_DATA_LEN },
255 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200256 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200257 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300258 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200259 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300260 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
261 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200262 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900263 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
264 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100265 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100266 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100267 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200268 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700269 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300270 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200271 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200272 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300273 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300274 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
275 .len = IEEE80211_MAX_DATA_LEN },
276 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
277 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530278 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300279 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530280 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300281 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
282 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
283 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
284 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
285 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100286 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200287 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
288 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700289 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800290 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
291 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
292 .len = NL80211_HT_CAPABILITY_LEN
293 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100294 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530295 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530296 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400297};
298
Johannes Berge31b8212010-10-05 19:39:30 +0200299/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000300static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200301 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200302 [NL80211_KEY_IDX] = { .type = NLA_U8 },
303 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200304 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200305 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
306 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200307 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100308 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
309};
310
311/* policy for the key default flags */
312static const struct nla_policy
313nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
314 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
315 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200316};
317
Johannes Bergff1b6e62011-05-04 15:37:28 +0200318/* policy for WoWLAN attributes */
319static const struct nla_policy
320nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
321 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
322 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
323 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
324 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200325 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
326 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
327 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
328 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200329};
330
Johannes Berge5497d72011-07-05 16:35:40 +0200331/* policy for GTK rekey offload attributes */
332static const struct nla_policy
333nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
334 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
335 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
336 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
337};
338
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300339static const struct nla_policy
340nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200341 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300342 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700343 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344};
345
Holger Schuriga0438972009-11-11 11:30:02 +0100346/* ifidx get helper */
347static int nl80211_get_ifidx(struct netlink_callback *cb)
348{
349 int res;
350
351 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
352 nl80211_fam.attrbuf, nl80211_fam.maxattr,
353 nl80211_policy);
354 if (res)
355 return res;
356
357 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
358 return -EINVAL;
359
360 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
361 if (!res)
362 return -EINVAL;
363 return res;
364}
365
Johannes Berg67748892010-10-04 21:14:06 +0200366static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
367 struct netlink_callback *cb,
368 struct cfg80211_registered_device **rdev,
369 struct net_device **dev)
370{
371 int ifidx = cb->args[0];
372 int err;
373
374 if (!ifidx)
375 ifidx = nl80211_get_ifidx(cb);
376 if (ifidx < 0)
377 return ifidx;
378
379 cb->args[0] = ifidx;
380
381 rtnl_lock();
382
383 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
384 if (!*dev) {
385 err = -ENODEV;
386 goto out_rtnl;
387 }
388
389 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100390 if (IS_ERR(*rdev)) {
391 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200392 goto out_rtnl;
393 }
394
395 return 0;
396 out_rtnl:
397 rtnl_unlock();
398 return err;
399}
400
401static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
402{
403 cfg80211_unlock_rdev(rdev);
404 rtnl_unlock();
405}
406
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100407/* IE validation */
408static bool is_valid_ie_attr(const struct nlattr *attr)
409{
410 const u8 *pos;
411 int len;
412
413 if (!attr)
414 return true;
415
416 pos = nla_data(attr);
417 len = nla_len(attr);
418
419 while (len) {
420 u8 elemlen;
421
422 if (len < 2)
423 return false;
424 len -= 2;
425
426 elemlen = pos[1];
427 if (elemlen > len)
428 return false;
429
430 len -= elemlen;
431 pos += 2 + elemlen;
432 }
433
434 return true;
435}
436
Johannes Berg55682962007-09-20 13:09:35 -0400437/* message building helper */
438static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
439 int flags, u8 cmd)
440{
441 /* since there is no private header just add the generic one */
442 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
443}
444
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400445static int nl80211_msg_put_channel(struct sk_buff *msg,
446 struct ieee80211_channel *chan)
447{
David S. Miller9360ffd2012-03-29 04:41:26 -0400448 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
449 chan->center_freq))
450 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400451
David S. Miller9360ffd2012-03-29 04:41:26 -0400452 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
453 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
454 goto nla_put_failure;
455 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
456 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
457 goto nla_put_failure;
458 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
459 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
460 goto nla_put_failure;
461 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
462 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
463 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400464
David S. Miller9360ffd2012-03-29 04:41:26 -0400465 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
466 DBM_TO_MBM(chan->max_power)))
467 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400468
469 return 0;
470
471 nla_put_failure:
472 return -ENOBUFS;
473}
474
Johannes Berg55682962007-09-20 13:09:35 -0400475/* netlink command implementations */
476
Johannes Bergb9454e82009-07-08 13:29:08 +0200477struct key_parse {
478 struct key_params p;
479 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200480 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200481 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100482 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200483};
484
485static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
486{
487 struct nlattr *tb[NL80211_KEY_MAX + 1];
488 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
489 nl80211_key_policy);
490 if (err)
491 return err;
492
493 k->def = !!tb[NL80211_KEY_DEFAULT];
494 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
495
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100496 if (k->def) {
497 k->def_uni = true;
498 k->def_multi = true;
499 }
500 if (k->defmgmt)
501 k->def_multi = true;
502
Johannes Bergb9454e82009-07-08 13:29:08 +0200503 if (tb[NL80211_KEY_IDX])
504 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
505
506 if (tb[NL80211_KEY_DATA]) {
507 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
508 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
509 }
510
511 if (tb[NL80211_KEY_SEQ]) {
512 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
513 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
514 }
515
516 if (tb[NL80211_KEY_CIPHER])
517 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
518
Johannes Berge31b8212010-10-05 19:39:30 +0200519 if (tb[NL80211_KEY_TYPE]) {
520 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
521 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
522 return -EINVAL;
523 }
524
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100525 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
526 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100527 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
528 tb[NL80211_KEY_DEFAULT_TYPES],
529 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100530 if (err)
531 return err;
532
533 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
534 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
535 }
536
Johannes Bergb9454e82009-07-08 13:29:08 +0200537 return 0;
538}
539
540static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
541{
542 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
543 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
544 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
545 }
546
547 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
548 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
549 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
550 }
551
552 if (info->attrs[NL80211_ATTR_KEY_IDX])
553 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
554
555 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
556 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
557
558 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
559 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
560
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100561 if (k->def) {
562 k->def_uni = true;
563 k->def_multi = true;
564 }
565 if (k->defmgmt)
566 k->def_multi = true;
567
Johannes Berge31b8212010-10-05 19:39:30 +0200568 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
569 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
570 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
571 return -EINVAL;
572 }
573
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
575 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
576 int err = nla_parse_nested(
577 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
578 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
579 nl80211_key_default_policy);
580 if (err)
581 return err;
582
583 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
584 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
585 }
586
Johannes Bergb9454e82009-07-08 13:29:08 +0200587 return 0;
588}
589
590static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
591{
592 int err;
593
594 memset(k, 0, sizeof(*k));
595 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200596 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200597
598 if (info->attrs[NL80211_ATTR_KEY])
599 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
600 else
601 err = nl80211_parse_key_old(info, k);
602
603 if (err)
604 return err;
605
606 if (k->def && k->defmgmt)
607 return -EINVAL;
608
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100609 if (k->defmgmt) {
610 if (k->def_uni || !k->def_multi)
611 return -EINVAL;
612 }
613
Johannes Bergb9454e82009-07-08 13:29:08 +0200614 if (k->idx != -1) {
615 if (k->defmgmt) {
616 if (k->idx < 4 || k->idx > 5)
617 return -EINVAL;
618 } else if (k->def) {
619 if (k->idx < 0 || k->idx > 3)
620 return -EINVAL;
621 } else {
622 if (k->idx < 0 || k->idx > 5)
623 return -EINVAL;
624 }
625 }
626
627 return 0;
628}
629
Johannes Bergfffd0932009-07-08 14:22:54 +0200630static struct cfg80211_cached_keys *
631nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
632 struct nlattr *keys)
633{
634 struct key_parse parse;
635 struct nlattr *key;
636 struct cfg80211_cached_keys *result;
637 int rem, err, def = 0;
638
639 result = kzalloc(sizeof(*result), GFP_KERNEL);
640 if (!result)
641 return ERR_PTR(-ENOMEM);
642
643 result->def = -1;
644 result->defmgmt = -1;
645
646 nla_for_each_nested(key, keys, rem) {
647 memset(&parse, 0, sizeof(parse));
648 parse.idx = -1;
649
650 err = nl80211_parse_key_new(key, &parse);
651 if (err)
652 goto error;
653 err = -EINVAL;
654 if (!parse.p.key)
655 goto error;
656 if (parse.idx < 0 || parse.idx > 4)
657 goto error;
658 if (parse.def) {
659 if (def)
660 goto error;
661 def = 1;
662 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100663 if (!parse.def_uni || !parse.def_multi)
664 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200665 } else if (parse.defmgmt)
666 goto error;
667 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200668 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200669 if (err)
670 goto error;
671 result->params[parse.idx].cipher = parse.p.cipher;
672 result->params[parse.idx].key_len = parse.p.key_len;
673 result->params[parse.idx].key = result->data[parse.idx];
674 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
675 }
676
677 return result;
678 error:
679 kfree(result);
680 return ERR_PTR(err);
681}
682
683static int nl80211_key_allowed(struct wireless_dev *wdev)
684{
685 ASSERT_WDEV_LOCK(wdev);
686
Johannes Bergfffd0932009-07-08 14:22:54 +0200687 switch (wdev->iftype) {
688 case NL80211_IFTYPE_AP:
689 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200690 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700691 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200692 break;
693 case NL80211_IFTYPE_ADHOC:
694 if (!wdev->current_bss)
695 return -ENOLINK;
696 break;
697 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200698 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200699 if (wdev->sme_state != CFG80211_SME_CONNECTED)
700 return -ENOLINK;
701 break;
702 default:
703 return -EINVAL;
704 }
705
706 return 0;
707}
708
Johannes Berg7527a782011-05-13 10:58:57 +0200709static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
710{
711 struct nlattr *nl_modes = nla_nest_start(msg, attr);
712 int i;
713
714 if (!nl_modes)
715 goto nla_put_failure;
716
717 i = 0;
718 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400719 if ((ifmodes & 1) && nla_put_flag(msg, i))
720 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200721 ifmodes >>= 1;
722 i++;
723 }
724
725 nla_nest_end(msg, nl_modes);
726 return 0;
727
728nla_put_failure:
729 return -ENOBUFS;
730}
731
732static int nl80211_put_iface_combinations(struct wiphy *wiphy,
733 struct sk_buff *msg)
734{
735 struct nlattr *nl_combis;
736 int i, j;
737
738 nl_combis = nla_nest_start(msg,
739 NL80211_ATTR_INTERFACE_COMBINATIONS);
740 if (!nl_combis)
741 goto nla_put_failure;
742
743 for (i = 0; i < wiphy->n_iface_combinations; i++) {
744 const struct ieee80211_iface_combination *c;
745 struct nlattr *nl_combi, *nl_limits;
746
747 c = &wiphy->iface_combinations[i];
748
749 nl_combi = nla_nest_start(msg, i + 1);
750 if (!nl_combi)
751 goto nla_put_failure;
752
753 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
754 if (!nl_limits)
755 goto nla_put_failure;
756
757 for (j = 0; j < c->n_limits; j++) {
758 struct nlattr *nl_limit;
759
760 nl_limit = nla_nest_start(msg, j + 1);
761 if (!nl_limit)
762 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400763 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
764 c->limits[j].max))
765 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200766 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
767 c->limits[j].types))
768 goto nla_put_failure;
769 nla_nest_end(msg, nl_limit);
770 }
771
772 nla_nest_end(msg, nl_limits);
773
David S. Miller9360ffd2012-03-29 04:41:26 -0400774 if (c->beacon_int_infra_match &&
775 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
776 goto nla_put_failure;
777 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
778 c->num_different_channels) ||
779 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
780 c->max_interfaces))
781 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200782
783 nla_nest_end(msg, nl_combi);
784 }
785
786 nla_nest_end(msg, nl_combis);
787
788 return 0;
789nla_put_failure:
790 return -ENOBUFS;
791}
792
Johannes Berg55682962007-09-20 13:09:35 -0400793static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
794 struct cfg80211_registered_device *dev)
795{
796 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100797 struct nlattr *nl_bands, *nl_band;
798 struct nlattr *nl_freqs, *nl_freq;
799 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100800 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100801 enum ieee80211_band band;
802 struct ieee80211_channel *chan;
803 struct ieee80211_rate *rate;
804 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200805 const struct ieee80211_txrx_stypes *mgmt_stypes =
806 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400807
808 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
809 if (!hdr)
810 return -1;
811
David S. Miller9360ffd2012-03-29 04:41:26 -0400812 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
813 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
814 nla_put_u32(msg, NL80211_ATTR_GENERATION,
815 cfg80211_rdev_list_generation) ||
816 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
817 dev->wiphy.retry_short) ||
818 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
819 dev->wiphy.retry_long) ||
820 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
821 dev->wiphy.frag_threshold) ||
822 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
823 dev->wiphy.rts_threshold) ||
824 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
825 dev->wiphy.coverage_class) ||
826 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
827 dev->wiphy.max_scan_ssids) ||
828 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
829 dev->wiphy.max_sched_scan_ssids) ||
830 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
831 dev->wiphy.max_scan_ie_len) ||
832 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
833 dev->wiphy.max_sched_scan_ie_len) ||
834 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
835 dev->wiphy.max_match_sets))
836 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200837
David S. Miller9360ffd2012-03-29 04:41:26 -0400838 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
839 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
840 goto nla_put_failure;
841 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
842 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
843 goto nla_put_failure;
844 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
845 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
846 goto nla_put_failure;
847 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
848 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
849 goto nla_put_failure;
850 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
851 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
852 goto nla_put_failure;
853 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
854 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
855 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200856
David S. Miller9360ffd2012-03-29 04:41:26 -0400857 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
858 sizeof(u32) * dev->wiphy.n_cipher_suites,
859 dev->wiphy.cipher_suites))
860 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100861
David S. Miller9360ffd2012-03-29 04:41:26 -0400862 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
863 dev->wiphy.max_num_pmkids))
864 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530865
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
867 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
868 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200869
David S. Miller9360ffd2012-03-29 04:41:26 -0400870 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
871 dev->wiphy.available_antennas_tx) ||
872 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
873 dev->wiphy.available_antennas_rx))
874 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100875
David S. Miller9360ffd2012-03-29 04:41:26 -0400876 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
877 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
878 dev->wiphy.probe_resp_offload))
879 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200880
Bruno Randolf7f531e02010-12-16 11:30:22 +0900881 if ((dev->wiphy.available_antennas_tx ||
882 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900883 u32 tx_ant = 0, rx_ant = 0;
884 int res;
885 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
886 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400887 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
888 tx_ant) ||
889 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
890 rx_ant))
891 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900892 }
893 }
894
Johannes Berg7527a782011-05-13 10:58:57 +0200895 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
896 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700897 goto nla_put_failure;
898
Johannes Bergee688b002008-01-24 19:38:39 +0100899 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
900 if (!nl_bands)
901 goto nla_put_failure;
902
903 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
904 if (!dev->wiphy.bands[band])
905 continue;
906
907 nl_band = nla_nest_start(msg, band);
908 if (!nl_band)
909 goto nla_put_failure;
910
Johannes Bergd51626d2008-10-09 12:20:13 +0200911 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400912 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
913 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
914 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
915 &dev->wiphy.bands[band]->ht_cap.mcs) ||
916 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
917 dev->wiphy.bands[band]->ht_cap.cap) ||
918 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
919 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
920 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
921 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
922 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200923
Johannes Bergee688b002008-01-24 19:38:39 +0100924 /* add frequencies */
925 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
926 if (!nl_freqs)
927 goto nla_put_failure;
928
929 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
930 nl_freq = nla_nest_start(msg, i);
931 if (!nl_freq)
932 goto nla_put_failure;
933
934 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100935
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400936 if (nl80211_msg_put_channel(msg, chan))
937 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200938
Johannes Bergee688b002008-01-24 19:38:39 +0100939 nla_nest_end(msg, nl_freq);
940 }
941
942 nla_nest_end(msg, nl_freqs);
943
944 /* add bitrates */
945 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
946 if (!nl_rates)
947 goto nla_put_failure;
948
949 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
950 nl_rate = nla_nest_start(msg, i);
951 if (!nl_rate)
952 goto nla_put_failure;
953
954 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -0400955 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
956 rate->bitrate))
957 goto nla_put_failure;
958 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
959 nla_put_flag(msg,
960 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
961 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100962
963 nla_nest_end(msg, nl_rate);
964 }
965
966 nla_nest_end(msg, nl_rates);
967
968 nla_nest_end(msg, nl_band);
969 }
970 nla_nest_end(msg, nl_bands);
971
Johannes Berg8fdc6212009-03-14 09:34:01 +0100972 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
973 if (!nl_cmds)
974 goto nla_put_failure;
975
976 i = 0;
977#define CMD(op, n) \
978 do { \
979 if (dev->ops->op) { \
980 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -0400981 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
982 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +0100983 } \
984 } while (0)
985
986 CMD(add_virtual_intf, NEW_INTERFACE);
987 CMD(change_virtual_intf, SET_INTERFACE);
988 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100989 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100990 CMD(add_station, NEW_STATION);
991 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800992 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100993 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200994 CMD(auth, AUTHENTICATE);
995 CMD(assoc, ASSOCIATE);
996 CMD(deauth, DEAUTHENTICATE);
997 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200998 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100999 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001000 CMD(set_pmksa, SET_PMKSA);
1001 CMD(del_pmksa, DEL_PMKSA);
1002 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001003 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1004 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001005 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001006 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001007 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001008 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001009 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001010 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1011 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001012 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001013 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001014 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001015 i++;
1016 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1017 goto nla_put_failure;
1018 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001019 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001020 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1021 CMD(tdls_mgmt, TDLS_MGMT);
1022 CMD(tdls_oper, TDLS_OPER);
1023 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001024 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1025 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001026 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001027 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001028 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1029 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001030 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1031 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001032 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001033
Kalle Valo4745fc02011-11-17 19:06:10 +02001034#ifdef CONFIG_NL80211_TESTMODE
1035 CMD(testmode_cmd, TESTMODE);
1036#endif
1037
Johannes Berg8fdc6212009-03-14 09:34:01 +01001038#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001039
Johannes Berg6829c872009-07-02 09:13:27 +02001040 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001041 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001042 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1043 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001044 }
1045
Johannes Berg6829c872009-07-02 09:13:27 +02001046 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001047 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001048 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1049 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001050 }
1051
Johannes Berg8fdc6212009-03-14 09:34:01 +01001052 nla_nest_end(msg, nl_cmds);
1053
Johannes Berg7c4ef712011-11-18 15:33:48 +01001054 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001055 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1056 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1057 dev->wiphy.max_remain_on_channel_duration))
1058 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001059
David S. Miller9360ffd2012-03-29 04:41:26 -04001060 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1061 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1062 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001063
Johannes Berg2e161f72010-08-12 15:38:38 +02001064 if (mgmt_stypes) {
1065 u16 stypes;
1066 struct nlattr *nl_ftypes, *nl_ifs;
1067 enum nl80211_iftype ift;
1068
1069 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1070 if (!nl_ifs)
1071 goto nla_put_failure;
1072
1073 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1074 nl_ftypes = nla_nest_start(msg, ift);
1075 if (!nl_ftypes)
1076 goto nla_put_failure;
1077 i = 0;
1078 stypes = mgmt_stypes[ift].tx;
1079 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001080 if ((stypes & 1) &&
1081 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1082 (i << 4) | IEEE80211_FTYPE_MGMT))
1083 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001084 stypes >>= 1;
1085 i++;
1086 }
1087 nla_nest_end(msg, nl_ftypes);
1088 }
1089
Johannes Berg74b70a42010-08-24 12:15:53 +02001090 nla_nest_end(msg, nl_ifs);
1091
Johannes Berg2e161f72010-08-12 15:38:38 +02001092 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1093 if (!nl_ifs)
1094 goto nla_put_failure;
1095
1096 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1097 nl_ftypes = nla_nest_start(msg, ift);
1098 if (!nl_ftypes)
1099 goto nla_put_failure;
1100 i = 0;
1101 stypes = mgmt_stypes[ift].rx;
1102 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001103 if ((stypes & 1) &&
1104 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1105 (i << 4) | IEEE80211_FTYPE_MGMT))
1106 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001107 stypes >>= 1;
1108 i++;
1109 }
1110 nla_nest_end(msg, nl_ftypes);
1111 }
1112 nla_nest_end(msg, nl_ifs);
1113 }
1114
Johannes Bergdfb89c52012-06-27 09:23:48 +02001115#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001116 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1117 struct nlattr *nl_wowlan;
1118
1119 nl_wowlan = nla_nest_start(msg,
1120 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1121 if (!nl_wowlan)
1122 goto nla_put_failure;
1123
David S. Miller9360ffd2012-03-29 04:41:26 -04001124 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1125 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1126 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1127 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1128 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1129 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1130 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1131 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1132 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1133 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1134 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1135 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1136 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1137 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1138 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1139 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1140 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001141 if (dev->wiphy.wowlan.n_patterns) {
1142 struct nl80211_wowlan_pattern_support pat = {
1143 .max_patterns = dev->wiphy.wowlan.n_patterns,
1144 .min_pattern_len =
1145 dev->wiphy.wowlan.pattern_min_len,
1146 .max_pattern_len =
1147 dev->wiphy.wowlan.pattern_max_len,
1148 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001149 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1150 sizeof(pat), &pat))
1151 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001152 }
1153
1154 nla_nest_end(msg, nl_wowlan);
1155 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001156#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001157
Johannes Berg7527a782011-05-13 10:58:57 +02001158 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1159 dev->wiphy.software_iftypes))
1160 goto nla_put_failure;
1161
1162 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1163 goto nla_put_failure;
1164
David S. Miller9360ffd2012-03-29 04:41:26 -04001165 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1166 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1167 dev->wiphy.ap_sme_capa))
1168 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001169
David S. Miller9360ffd2012-03-29 04:41:26 -04001170 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1171 dev->wiphy.features))
1172 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001173
David S. Miller9360ffd2012-03-29 04:41:26 -04001174 if (dev->wiphy.ht_capa_mod_mask &&
1175 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1176 sizeof(*dev->wiphy.ht_capa_mod_mask),
1177 dev->wiphy.ht_capa_mod_mask))
1178 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001179
Johannes Berg55682962007-09-20 13:09:35 -04001180 return genlmsg_end(msg, hdr);
1181
1182 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001183 genlmsg_cancel(msg, hdr);
1184 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001185}
1186
1187static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1188{
1189 int idx = 0;
1190 int start = cb->args[0];
1191 struct cfg80211_registered_device *dev;
1192
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001193 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001194 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001195 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1196 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001197 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001198 continue;
1199 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1200 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001201 dev) < 0) {
1202 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001203 break;
Julius Volzb4637272008-07-08 14:02:19 +02001204 }
Johannes Berg55682962007-09-20 13:09:35 -04001205 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001206 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001207
1208 cb->args[0] = idx;
1209
1210 return skb->len;
1211}
1212
1213static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1214{
1215 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001216 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001217
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001219 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001220 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001221
Johannes Berg4c476992010-10-04 21:36:35 +02001222 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1223 nlmsg_free(msg);
1224 return -ENOBUFS;
1225 }
Johannes Berg55682962007-09-20 13:09:35 -04001226
Johannes Berg134e6372009-07-10 09:51:34 +00001227 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001228}
1229
Jouni Malinen31888482008-10-30 16:59:24 +02001230static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1231 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1232 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1233 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1234 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1235 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1236};
1237
1238static int parse_txq_params(struct nlattr *tb[],
1239 struct ieee80211_txq_params *txq_params)
1240{
Johannes Berga3304b02012-03-28 11:04:24 +02001241 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001242 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1243 !tb[NL80211_TXQ_ATTR_AIFS])
1244 return -EINVAL;
1245
Johannes Berga3304b02012-03-28 11:04:24 +02001246 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001247 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1248 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1249 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1250 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1251
Johannes Berga3304b02012-03-28 11:04:24 +02001252 if (txq_params->ac >= NL80211_NUM_ACS)
1253 return -EINVAL;
1254
Jouni Malinen31888482008-10-30 16:59:24 +02001255 return 0;
1256}
1257
Johannes Bergf444de02010-05-05 15:25:02 +02001258static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1259{
1260 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001261 * You can only set the channel explicitly for WDS interfaces,
1262 * all others have their channel managed via their respective
1263 * "establish a connection" command (connect, join, ...)
1264 *
1265 * For AP/GO and mesh mode, the channel can be set with the
1266 * channel userspace API, but is only stored and passed to the
1267 * low-level driver when the AP starts or the mesh is joined.
1268 * This is for backward compatibility, userspace can also give
1269 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001270 *
1271 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001272 * whatever else is going on, so they have their own special
1273 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001274 */
1275 return !wdev ||
1276 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001277 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001278 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1279 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001280}
1281
Johannes Bergcd6c6592012-05-10 21:27:18 +02001282static bool nl80211_valid_channel_type(struct genl_info *info,
1283 enum nl80211_channel_type *channel_type)
1284{
1285 enum nl80211_channel_type tmp;
1286
1287 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1288 return false;
1289
1290 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1291 if (tmp != NL80211_CHAN_NO_HT &&
1292 tmp != NL80211_CHAN_HT20 &&
1293 tmp != NL80211_CHAN_HT40PLUS &&
1294 tmp != NL80211_CHAN_HT40MINUS)
1295 return false;
1296
1297 if (channel_type)
1298 *channel_type = tmp;
1299
1300 return true;
1301}
1302
Johannes Bergf444de02010-05-05 15:25:02 +02001303static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1304 struct wireless_dev *wdev,
1305 struct genl_info *info)
1306{
Johannes Bergaa430da2012-05-16 23:50:18 +02001307 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001308 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1309 u32 freq;
1310 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001311 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1312
1313 if (wdev)
1314 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001315
1316 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1317 return -EINVAL;
1318
1319 if (!nl80211_can_set_dev_channel(wdev))
1320 return -EOPNOTSUPP;
1321
Johannes Bergcd6c6592012-05-10 21:27:18 +02001322 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1323 !nl80211_valid_channel_type(info, &channel_type))
1324 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001325
1326 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1327
1328 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001329 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001330 case NL80211_IFTYPE_AP:
1331 case NL80211_IFTYPE_P2P_GO:
1332 if (wdev->beacon_interval) {
1333 result = -EBUSY;
1334 break;
1335 }
1336 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1337 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1338 channel,
1339 channel_type)) {
1340 result = -EINVAL;
1341 break;
1342 }
1343 wdev->preset_chan = channel;
1344 wdev->preset_chantype = channel_type;
1345 result = 0;
1346 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001347 case NL80211_IFTYPE_MESH_POINT:
1348 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1349 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001350 case NL80211_IFTYPE_MONITOR:
1351 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1352 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001353 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001354 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001355 }
1356 mutex_unlock(&rdev->devlist_mtx);
1357
1358 return result;
1359}
1360
1361static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1362{
Johannes Berg4c476992010-10-04 21:36:35 +02001363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1364 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001365
Johannes Berg4c476992010-10-04 21:36:35 +02001366 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001367}
1368
Bill Jordane8347eb2010-10-01 13:54:28 -04001369static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1370{
Johannes Berg43b19952010-10-07 13:10:30 +02001371 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1372 struct net_device *dev = info->user_ptr[1];
1373 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001374 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001375
1376 if (!info->attrs[NL80211_ATTR_MAC])
1377 return -EINVAL;
1378
Johannes Berg43b19952010-10-07 13:10:30 +02001379 if (netif_running(dev))
1380 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001381
Johannes Berg43b19952010-10-07 13:10:30 +02001382 if (!rdev->ops->set_wds_peer)
1383 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001384
Johannes Berg43b19952010-10-07 13:10:30 +02001385 if (wdev->iftype != NL80211_IFTYPE_WDS)
1386 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001387
1388 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001389 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001390}
1391
1392
Johannes Berg55682962007-09-20 13:09:35 -04001393static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1394{
1395 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001396 struct net_device *netdev = NULL;
1397 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001398 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001399 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001400 u32 changed;
1401 u8 retry_short = 0, retry_long = 0;
1402 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001403 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001404
Johannes Bergf444de02010-05-05 15:25:02 +02001405 /*
1406 * Try to find the wiphy and netdev. Normally this
1407 * function shouldn't need the netdev, but this is
1408 * done for backward compatibility -- previously
1409 * setting the channel was done per wiphy, but now
1410 * it is per netdev. Previous userland like hostapd
1411 * also passed a netdev to set_wiphy, so that it is
1412 * possible to let that go to the right netdev!
1413 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001414 mutex_lock(&cfg80211_mutex);
1415
Johannes Bergf444de02010-05-05 15:25:02 +02001416 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1417 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1418
1419 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1420 if (netdev && netdev->ieee80211_ptr) {
1421 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1422 mutex_lock(&rdev->mtx);
1423 } else
1424 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001425 }
1426
Johannes Bergf444de02010-05-05 15:25:02 +02001427 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001428 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1429 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001430 if (IS_ERR(rdev)) {
1431 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001432 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001433 }
1434 wdev = NULL;
1435 netdev = NULL;
1436 result = 0;
1437
1438 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001439 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001440 wdev = netdev->ieee80211_ptr;
1441 else
1442 wdev = NULL;
1443
1444 /*
1445 * end workaround code, by now the rdev is available
1446 * and locked, and wdev may or may not be NULL.
1447 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001448
1449 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001450 result = cfg80211_dev_rename(
1451 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001452
1453 mutex_unlock(&cfg80211_mutex);
1454
1455 if (result)
1456 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001457
Jouni Malinen31888482008-10-30 16:59:24 +02001458 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1459 struct ieee80211_txq_params txq_params;
1460 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1461
1462 if (!rdev->ops->set_txq_params) {
1463 result = -EOPNOTSUPP;
1464 goto bad_res;
1465 }
1466
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001467 if (!netdev) {
1468 result = -EINVAL;
1469 goto bad_res;
1470 }
1471
Johannes Berg133a3ff2011-11-03 14:50:13 +01001472 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1473 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1474 result = -EINVAL;
1475 goto bad_res;
1476 }
1477
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001478 if (!netif_running(netdev)) {
1479 result = -ENETDOWN;
1480 goto bad_res;
1481 }
1482
Jouni Malinen31888482008-10-30 16:59:24 +02001483 nla_for_each_nested(nl_txq_params,
1484 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1485 rem_txq_params) {
1486 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1487 nla_data(nl_txq_params),
1488 nla_len(nl_txq_params),
1489 txq_params_policy);
1490 result = parse_txq_params(tb, &txq_params);
1491 if (result)
1492 goto bad_res;
1493
1494 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001495 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001496 &txq_params);
1497 if (result)
1498 goto bad_res;
1499 }
1500 }
1501
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001502 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001503 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001504 if (result)
1505 goto bad_res;
1506 }
1507
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001508 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1509 enum nl80211_tx_power_setting type;
1510 int idx, mbm = 0;
1511
1512 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001513 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001514 goto bad_res;
1515 }
1516
1517 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1518 type = nla_get_u32(info->attrs[idx]);
1519
1520 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1521 (type != NL80211_TX_POWER_AUTOMATIC)) {
1522 result = -EINVAL;
1523 goto bad_res;
1524 }
1525
1526 if (type != NL80211_TX_POWER_AUTOMATIC) {
1527 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1528 mbm = nla_get_u32(info->attrs[idx]);
1529 }
1530
1531 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1532 if (result)
1533 goto bad_res;
1534 }
1535
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001536 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1537 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1538 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001539 if ((!rdev->wiphy.available_antennas_tx &&
1540 !rdev->wiphy.available_antennas_rx) ||
1541 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001542 result = -EOPNOTSUPP;
1543 goto bad_res;
1544 }
1545
1546 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1547 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1548
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001549 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001550 * available antenna masks, except for the "all" mask */
1551 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1552 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001553 result = -EINVAL;
1554 goto bad_res;
1555 }
1556
Bruno Randolf7f531e02010-12-16 11:30:22 +09001557 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1558 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001559
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001560 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1561 if (result)
1562 goto bad_res;
1563 }
1564
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001565 changed = 0;
1566
1567 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1568 retry_short = nla_get_u8(
1569 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1570 if (retry_short == 0) {
1571 result = -EINVAL;
1572 goto bad_res;
1573 }
1574 changed |= WIPHY_PARAM_RETRY_SHORT;
1575 }
1576
1577 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1578 retry_long = nla_get_u8(
1579 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1580 if (retry_long == 0) {
1581 result = -EINVAL;
1582 goto bad_res;
1583 }
1584 changed |= WIPHY_PARAM_RETRY_LONG;
1585 }
1586
1587 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1588 frag_threshold = nla_get_u32(
1589 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1590 if (frag_threshold < 256) {
1591 result = -EINVAL;
1592 goto bad_res;
1593 }
1594 if (frag_threshold != (u32) -1) {
1595 /*
1596 * Fragments (apart from the last one) are required to
1597 * have even length. Make the fragmentation code
1598 * simpler by stripping LSB should someone try to use
1599 * odd threshold value.
1600 */
1601 frag_threshold &= ~0x1;
1602 }
1603 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1604 }
1605
1606 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1607 rts_threshold = nla_get_u32(
1608 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1609 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1610 }
1611
Lukáš Turek81077e82009-12-21 22:50:47 +01001612 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1613 coverage_class = nla_get_u8(
1614 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1615 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1616 }
1617
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001618 if (changed) {
1619 u8 old_retry_short, old_retry_long;
1620 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001621 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001622
1623 if (!rdev->ops->set_wiphy_params) {
1624 result = -EOPNOTSUPP;
1625 goto bad_res;
1626 }
1627
1628 old_retry_short = rdev->wiphy.retry_short;
1629 old_retry_long = rdev->wiphy.retry_long;
1630 old_frag_threshold = rdev->wiphy.frag_threshold;
1631 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001632 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001633
1634 if (changed & WIPHY_PARAM_RETRY_SHORT)
1635 rdev->wiphy.retry_short = retry_short;
1636 if (changed & WIPHY_PARAM_RETRY_LONG)
1637 rdev->wiphy.retry_long = retry_long;
1638 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1639 rdev->wiphy.frag_threshold = frag_threshold;
1640 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1641 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001642 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1643 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001644
1645 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1646 if (result) {
1647 rdev->wiphy.retry_short = old_retry_short;
1648 rdev->wiphy.retry_long = old_retry_long;
1649 rdev->wiphy.frag_threshold = old_frag_threshold;
1650 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001651 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001652 }
1653 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001654
Johannes Berg306d6112008-12-08 12:39:04 +01001655 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001656 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001657 if (netdev)
1658 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001659 return result;
1660}
1661
1662
1663static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001664 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001665 struct net_device *dev)
1666{
1667 void *hdr;
1668
1669 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1670 if (!hdr)
1671 return -1;
1672
David S. Miller9360ffd2012-03-29 04:41:26 -04001673 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1674 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1675 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1676 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1677 dev->ieee80211_ptr->iftype) ||
1678 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1679 rdev->devlist_generation ^
1680 (cfg80211_rdev_list_generation << 2)))
1681 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001682
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001683 if (rdev->ops->get_channel) {
1684 struct ieee80211_channel *chan;
1685 enum nl80211_channel_type channel_type;
1686
1687 chan = rdev->ops->get_channel(&rdev->wiphy, &channel_type);
John W. Linville59ef43e2012-04-18 14:17:13 -04001688 if (chan &&
1689 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1690 chan->center_freq) ||
1691 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1692 channel_type)))
1693 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001694 }
1695
Johannes Berg55682962007-09-20 13:09:35 -04001696 return genlmsg_end(msg, hdr);
1697
1698 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001699 genlmsg_cancel(msg, hdr);
1700 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001701}
1702
1703static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1704{
1705 int wp_idx = 0;
1706 int if_idx = 0;
1707 int wp_start = cb->args[0];
1708 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001709 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001710 struct wireless_dev *wdev;
1711
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001712 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001713 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1714 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001715 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001716 if (wp_idx < wp_start) {
1717 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001718 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001719 }
Johannes Berg55682962007-09-20 13:09:35 -04001720 if_idx = 0;
1721
Johannes Bergf5ea9122009-08-07 16:17:38 +02001722 mutex_lock(&rdev->devlist_mtx);
1723 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001724 if (if_idx < if_start) {
1725 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001726 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001727 }
Johannes Berg55682962007-09-20 13:09:35 -04001728 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1729 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001730 rdev, wdev->netdev) < 0) {
1731 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001732 goto out;
1733 }
1734 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001735 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001736 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001737
1738 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001739 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001740 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001741 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001742
1743 cb->args[0] = wp_idx;
1744 cb->args[1] = if_idx;
1745
1746 return skb->len;
1747}
1748
1749static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1750{
1751 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001752 struct cfg80211_registered_device *dev = info->user_ptr[0];
1753 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001754
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001755 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001756 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001757 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001758
Johannes Bergd7264052009-04-19 16:23:20 +02001759 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001760 dev, netdev) < 0) {
1761 nlmsg_free(msg);
1762 return -ENOBUFS;
1763 }
Johannes Berg55682962007-09-20 13:09:35 -04001764
Johannes Berg134e6372009-07-10 09:51:34 +00001765 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001766}
1767
Michael Wu66f7ac52008-01-31 19:48:22 +01001768static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1769 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1770 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1771 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1772 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1773 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1774};
1775
1776static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1777{
1778 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1779 int flag;
1780
1781 *mntrflags = 0;
1782
1783 if (!nla)
1784 return -EINVAL;
1785
1786 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1787 nla, mntr_flags_policy))
1788 return -EINVAL;
1789
1790 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1791 if (flags[flag])
1792 *mntrflags |= (1<<flag);
1793
1794 return 0;
1795}
1796
Johannes Berg9bc383d2009-11-19 11:55:19 +01001797static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001798 struct net_device *netdev, u8 use_4addr,
1799 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001800{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001801 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001802 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001803 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001804 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001805 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001806
1807 switch (iftype) {
1808 case NL80211_IFTYPE_AP_VLAN:
1809 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1810 return 0;
1811 break;
1812 case NL80211_IFTYPE_STATION:
1813 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1814 return 0;
1815 break;
1816 default:
1817 break;
1818 }
1819
1820 return -EOPNOTSUPP;
1821}
1822
Johannes Berg55682962007-09-20 13:09:35 -04001823static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1824{
Johannes Berg4c476992010-10-04 21:36:35 +02001825 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001826 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001827 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001828 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001829 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001830 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001831 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001832
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001833 memset(&params, 0, sizeof(params));
1834
Johannes Berg04a773a2009-04-19 21:24:32 +02001835 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001836
Johannes Berg723b0382008-09-16 20:22:09 +02001837 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001838 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001839 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001840 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001841 if (ntype > NL80211_IFTYPE_MAX)
1842 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001843 }
1844
Johannes Berg92ffe052008-09-16 20:39:36 +02001845 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001846 struct wireless_dev *wdev = dev->ieee80211_ptr;
1847
Johannes Berg4c476992010-10-04 21:36:35 +02001848 if (ntype != NL80211_IFTYPE_MESH_POINT)
1849 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001850 if (netif_running(dev))
1851 return -EBUSY;
1852
1853 wdev_lock(wdev);
1854 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1855 IEEE80211_MAX_MESH_ID_LEN);
1856 wdev->mesh_id_up_len =
1857 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1858 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1859 wdev->mesh_id_up_len);
1860 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001861 }
1862
Felix Fietkau8b787642009-11-10 18:53:10 +01001863 if (info->attrs[NL80211_ATTR_4ADDR]) {
1864 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1865 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001866 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001867 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001868 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001869 } else {
1870 params.use_4addr = -1;
1871 }
1872
Johannes Berg92ffe052008-09-16 20:39:36 +02001873 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001874 if (ntype != NL80211_IFTYPE_MONITOR)
1875 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001876 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1877 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001878 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001879 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001880
1881 flags = &_flags;
1882 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001883 }
Johannes Berg3b858752009-03-12 09:55:09 +01001884
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001885 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001886 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001887 else
1888 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001889
Johannes Berg9bc383d2009-11-19 11:55:19 +01001890 if (!err && params.use_4addr != -1)
1891 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1892
Johannes Berg55682962007-09-20 13:09:35 -04001893 return err;
1894}
1895
1896static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1897{
Johannes Berg4c476992010-10-04 21:36:35 +02001898 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001899 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001900 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001901 int err;
1902 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001903 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001904
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001905 memset(&params, 0, sizeof(params));
1906
Johannes Berg55682962007-09-20 13:09:35 -04001907 if (!info->attrs[NL80211_ATTR_IFNAME])
1908 return -EINVAL;
1909
1910 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1911 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1912 if (type > NL80211_IFTYPE_MAX)
1913 return -EINVAL;
1914 }
1915
Johannes Berg79c97e92009-07-07 03:56:12 +02001916 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001917 !(rdev->wiphy.interface_modes & (1 << type)))
1918 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001919
Johannes Berg9bc383d2009-11-19 11:55:19 +01001920 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001921 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001922 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001923 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001924 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001925 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001926
Michael Wu66f7ac52008-01-31 19:48:22 +01001927 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1928 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1929 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001930 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001931 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001932 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001933 if (IS_ERR(dev))
1934 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001935
Johannes Berg29cbe682010-12-03 09:20:44 +01001936 if (type == NL80211_IFTYPE_MESH_POINT &&
1937 info->attrs[NL80211_ATTR_MESH_ID]) {
1938 struct wireless_dev *wdev = dev->ieee80211_ptr;
1939
1940 wdev_lock(wdev);
1941 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1942 IEEE80211_MAX_MESH_ID_LEN);
1943 wdev->mesh_id_up_len =
1944 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1945 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1946 wdev->mesh_id_up_len);
1947 wdev_unlock(wdev);
1948 }
1949
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001950 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001951}
1952
1953static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1954{
Johannes Berg4c476992010-10-04 21:36:35 +02001955 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1956 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001957
Johannes Berg4c476992010-10-04 21:36:35 +02001958 if (!rdev->ops->del_virtual_intf)
1959 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001960
Johannes Berg4c476992010-10-04 21:36:35 +02001961 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001962}
1963
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001964static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1965{
1966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1967 struct net_device *dev = info->user_ptr[1];
1968 u16 noack_map;
1969
1970 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1971 return -EINVAL;
1972
1973 if (!rdev->ops->set_noack_map)
1974 return -EOPNOTSUPP;
1975
1976 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1977
1978 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1979}
1980
Johannes Berg41ade002007-12-19 02:03:29 +01001981struct get_key_cookie {
1982 struct sk_buff *msg;
1983 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001984 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001985};
1986
1987static void get_key_callback(void *c, struct key_params *params)
1988{
Johannes Bergb9454e82009-07-08 13:29:08 +02001989 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001990 struct get_key_cookie *cookie = c;
1991
David S. Miller9360ffd2012-03-29 04:41:26 -04001992 if ((params->key &&
1993 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1994 params->key_len, params->key)) ||
1995 (params->seq &&
1996 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1997 params->seq_len, params->seq)) ||
1998 (params->cipher &&
1999 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2000 params->cipher)))
2001 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002002
Johannes Bergb9454e82009-07-08 13:29:08 +02002003 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2004 if (!key)
2005 goto nla_put_failure;
2006
David S. Miller9360ffd2012-03-29 04:41:26 -04002007 if ((params->key &&
2008 nla_put(cookie->msg, NL80211_KEY_DATA,
2009 params->key_len, params->key)) ||
2010 (params->seq &&
2011 nla_put(cookie->msg, NL80211_KEY_SEQ,
2012 params->seq_len, params->seq)) ||
2013 (params->cipher &&
2014 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2015 params->cipher)))
2016 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002017
David S. Miller9360ffd2012-03-29 04:41:26 -04002018 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2019 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002020
2021 nla_nest_end(cookie->msg, key);
2022
Johannes Berg41ade002007-12-19 02:03:29 +01002023 return;
2024 nla_put_failure:
2025 cookie->error = 1;
2026}
2027
2028static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2029{
Johannes Berg4c476992010-10-04 21:36:35 +02002030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002031 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002032 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002033 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002034 const u8 *mac_addr = NULL;
2035 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002036 struct get_key_cookie cookie = {
2037 .error = 0,
2038 };
2039 void *hdr;
2040 struct sk_buff *msg;
2041
2042 if (info->attrs[NL80211_ATTR_KEY_IDX])
2043 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2044
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002045 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002046 return -EINVAL;
2047
2048 if (info->attrs[NL80211_ATTR_MAC])
2049 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2050
Johannes Berge31b8212010-10-05 19:39:30 +02002051 pairwise = !!mac_addr;
2052 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2053 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2054 if (kt >= NUM_NL80211_KEYTYPES)
2055 return -EINVAL;
2056 if (kt != NL80211_KEYTYPE_GROUP &&
2057 kt != NL80211_KEYTYPE_PAIRWISE)
2058 return -EINVAL;
2059 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2060 }
2061
Johannes Berg4c476992010-10-04 21:36:35 +02002062 if (!rdev->ops->get_key)
2063 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002064
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002065 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002066 if (!msg)
2067 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002068
2069 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2070 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002071 if (IS_ERR(hdr))
2072 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002073
2074 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002075 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002076
David S. Miller9360ffd2012-03-29 04:41:26 -04002077 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2078 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2079 goto nla_put_failure;
2080 if (mac_addr &&
2081 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2082 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002083
Johannes Berge31b8212010-10-05 19:39:30 +02002084 if (pairwise && mac_addr &&
2085 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2086 return -ENOENT;
2087
2088 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2089 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002090
2091 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002092 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002093
2094 if (cookie.error)
2095 goto nla_put_failure;
2096
2097 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002098 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002099
2100 nla_put_failure:
2101 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002102 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002103 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002104 return err;
2105}
2106
2107static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2108{
Johannes Berg4c476992010-10-04 21:36:35 +02002109 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002110 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002111 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002112 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002113
Johannes Bergb9454e82009-07-08 13:29:08 +02002114 err = nl80211_parse_key(info, &key);
2115 if (err)
2116 return err;
2117
2118 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002119 return -EINVAL;
2120
Johannes Bergb9454e82009-07-08 13:29:08 +02002121 /* only support setting default key */
2122 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002123 return -EINVAL;
2124
Johannes Bergfffd0932009-07-08 14:22:54 +02002125 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002126
2127 if (key.def) {
2128 if (!rdev->ops->set_default_key) {
2129 err = -EOPNOTSUPP;
2130 goto out;
2131 }
2132
2133 err = nl80211_key_allowed(dev->ieee80211_ptr);
2134 if (err)
2135 goto out;
2136
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002137 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2138 key.def_uni, key.def_multi);
2139
2140 if (err)
2141 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002142
Johannes Berg3d23e342009-09-29 23:27:28 +02002143#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002144 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002145#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002146 } else {
2147 if (key.def_uni || !key.def_multi) {
2148 err = -EINVAL;
2149 goto out;
2150 }
2151
2152 if (!rdev->ops->set_default_mgmt_key) {
2153 err = -EOPNOTSUPP;
2154 goto out;
2155 }
2156
2157 err = nl80211_key_allowed(dev->ieee80211_ptr);
2158 if (err)
2159 goto out;
2160
2161 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2162 dev, key.idx);
2163 if (err)
2164 goto out;
2165
2166#ifdef CONFIG_CFG80211_WEXT
2167 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2168#endif
2169 }
2170
2171 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002172 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002173
Johannes Berg41ade002007-12-19 02:03:29 +01002174 return err;
2175}
2176
2177static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2178{
Johannes Berg4c476992010-10-04 21:36:35 +02002179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002180 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002181 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002182 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002183 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002184
Johannes Bergb9454e82009-07-08 13:29:08 +02002185 err = nl80211_parse_key(info, &key);
2186 if (err)
2187 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002188
Johannes Bergb9454e82009-07-08 13:29:08 +02002189 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002190 return -EINVAL;
2191
Johannes Berg41ade002007-12-19 02:03:29 +01002192 if (info->attrs[NL80211_ATTR_MAC])
2193 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2194
Johannes Berge31b8212010-10-05 19:39:30 +02002195 if (key.type == -1) {
2196 if (mac_addr)
2197 key.type = NL80211_KEYTYPE_PAIRWISE;
2198 else
2199 key.type = NL80211_KEYTYPE_GROUP;
2200 }
2201
2202 /* for now */
2203 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2204 key.type != NL80211_KEYTYPE_GROUP)
2205 return -EINVAL;
2206
Johannes Berg4c476992010-10-04 21:36:35 +02002207 if (!rdev->ops->add_key)
2208 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002209
Johannes Berge31b8212010-10-05 19:39:30 +02002210 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2211 key.type == NL80211_KEYTYPE_PAIRWISE,
2212 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002213 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002214
2215 wdev_lock(dev->ieee80211_ptr);
2216 err = nl80211_key_allowed(dev->ieee80211_ptr);
2217 if (!err)
2218 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002219 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002220 mac_addr, &key.p);
2221 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002222
Johannes Berg41ade002007-12-19 02:03:29 +01002223 return err;
2224}
2225
2226static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2227{
Johannes Berg4c476992010-10-04 21:36:35 +02002228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002229 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002230 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002231 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002232 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002233
Johannes Bergb9454e82009-07-08 13:29:08 +02002234 err = nl80211_parse_key(info, &key);
2235 if (err)
2236 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002237
2238 if (info->attrs[NL80211_ATTR_MAC])
2239 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2240
Johannes Berge31b8212010-10-05 19:39:30 +02002241 if (key.type == -1) {
2242 if (mac_addr)
2243 key.type = NL80211_KEYTYPE_PAIRWISE;
2244 else
2245 key.type = NL80211_KEYTYPE_GROUP;
2246 }
2247
2248 /* for now */
2249 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2250 key.type != NL80211_KEYTYPE_GROUP)
2251 return -EINVAL;
2252
Johannes Berg4c476992010-10-04 21:36:35 +02002253 if (!rdev->ops->del_key)
2254 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002255
Johannes Bergfffd0932009-07-08 14:22:54 +02002256 wdev_lock(dev->ieee80211_ptr);
2257 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002258
2259 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2260 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2261 err = -ENOENT;
2262
Johannes Bergfffd0932009-07-08 14:22:54 +02002263 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002264 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2265 key.type == NL80211_KEYTYPE_PAIRWISE,
2266 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002267
Johannes Berg3d23e342009-09-29 23:27:28 +02002268#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002269 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002270 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002271 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002272 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002273 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2274 }
2275#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002276 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002277
Johannes Berg41ade002007-12-19 02:03:29 +01002278 return err;
2279}
2280
Johannes Berg88600202012-02-13 15:17:18 +01002281static int nl80211_parse_beacon(struct genl_info *info,
2282 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002283{
Johannes Berg88600202012-02-13 15:17:18 +01002284 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002285
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002286 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2287 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2288 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2289 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002290 return -EINVAL;
2291
Johannes Berg88600202012-02-13 15:17:18 +01002292 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002293
Johannes Berged1b6cc2007-12-19 02:03:32 +01002294 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002295 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2296 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2297 if (!bcn->head_len)
2298 return -EINVAL;
2299 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002300 }
2301
2302 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002303 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2304 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002305 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002306 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002307 }
2308
Johannes Berg4c476992010-10-04 21:36:35 +02002309 if (!haveinfo)
2310 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002311
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002312 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002313 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2314 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002315 }
2316
2317 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002318 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002319 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002320 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002321 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2322 }
2323
2324 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002325 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002326 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002327 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002328 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2329 }
2330
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002331 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002332 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002333 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002334 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002335 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2336 }
2337
Johannes Berg88600202012-02-13 15:17:18 +01002338 return 0;
2339}
2340
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002341static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2342 struct cfg80211_ap_settings *params)
2343{
2344 struct wireless_dev *wdev;
2345 bool ret = false;
2346
2347 mutex_lock(&rdev->devlist_mtx);
2348
2349 list_for_each_entry(wdev, &rdev->netdev_list, list) {
2350 if (wdev->iftype != NL80211_IFTYPE_AP &&
2351 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2352 continue;
2353
2354 if (!wdev->preset_chan)
2355 continue;
2356
2357 params->channel = wdev->preset_chan;
2358 params->channel_type = wdev->preset_chantype;
2359 ret = true;
2360 break;
2361 }
2362
2363 mutex_unlock(&rdev->devlist_mtx);
2364
2365 return ret;
2366}
2367
Johannes Berg88600202012-02-13 15:17:18 +01002368static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2369{
2370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2371 struct net_device *dev = info->user_ptr[1];
2372 struct wireless_dev *wdev = dev->ieee80211_ptr;
2373 struct cfg80211_ap_settings params;
2374 int err;
2375
2376 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2377 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2378 return -EOPNOTSUPP;
2379
2380 if (!rdev->ops->start_ap)
2381 return -EOPNOTSUPP;
2382
2383 if (wdev->beacon_interval)
2384 return -EALREADY;
2385
2386 memset(&params, 0, sizeof(params));
2387
2388 /* these are required for START_AP */
2389 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2390 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2391 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2392 return -EINVAL;
2393
2394 err = nl80211_parse_beacon(info, &params.beacon);
2395 if (err)
2396 return err;
2397
2398 params.beacon_interval =
2399 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2400 params.dtim_period =
2401 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2402
2403 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2404 if (err)
2405 return err;
2406
2407 /*
2408 * In theory, some of these attributes should be required here
2409 * but since they were not used when the command was originally
2410 * added, keep them optional for old user space programs to let
2411 * them continue to work with drivers that do not need the
2412 * additional information -- drivers must check!
2413 */
2414 if (info->attrs[NL80211_ATTR_SSID]) {
2415 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2416 params.ssid_len =
2417 nla_len(info->attrs[NL80211_ATTR_SSID]);
2418 if (params.ssid_len == 0 ||
2419 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2420 return -EINVAL;
2421 }
2422
2423 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2424 params.hidden_ssid = nla_get_u32(
2425 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2426 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2427 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2428 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2429 return -EINVAL;
2430 }
2431
2432 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2433
2434 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2435 params.auth_type = nla_get_u32(
2436 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2437 if (!nl80211_valid_auth_type(params.auth_type))
2438 return -EINVAL;
2439 } else
2440 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2441
2442 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2443 NL80211_MAX_NR_CIPHER_SUITES);
2444 if (err)
2445 return err;
2446
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302447 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2448 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2449 return -EOPNOTSUPP;
2450 params.inactivity_timeout = nla_get_u16(
2451 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2452 }
2453
Johannes Bergaa430da2012-05-16 23:50:18 +02002454 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2455 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2456
2457 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2458 !nl80211_valid_channel_type(info, &channel_type))
2459 return -EINVAL;
2460
2461 params.channel = rdev_freq_to_chan(rdev,
2462 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2463 channel_type);
2464 if (!params.channel)
2465 return -EINVAL;
2466 params.channel_type = channel_type;
2467 } else if (wdev->preset_chan) {
2468 params.channel = wdev->preset_chan;
2469 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002470 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002471 return -EINVAL;
2472
2473 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2474 params.channel_type))
2475 return -EINVAL;
2476
Johannes Berg88600202012-02-13 15:17:18 +01002477 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002478 if (!err) {
2479 wdev->preset_chan = params.channel;
2480 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002481 wdev->beacon_interval = params.beacon_interval;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002482 }
Johannes Berg56d18932011-05-09 18:41:15 +02002483 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002484}
2485
Johannes Berg88600202012-02-13 15:17:18 +01002486static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2487{
2488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2489 struct net_device *dev = info->user_ptr[1];
2490 struct wireless_dev *wdev = dev->ieee80211_ptr;
2491 struct cfg80211_beacon_data params;
2492 int err;
2493
2494 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2495 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2496 return -EOPNOTSUPP;
2497
2498 if (!rdev->ops->change_beacon)
2499 return -EOPNOTSUPP;
2500
2501 if (!wdev->beacon_interval)
2502 return -EINVAL;
2503
2504 err = nl80211_parse_beacon(info, &params);
2505 if (err)
2506 return err;
2507
2508 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2509}
2510
2511static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002512{
Johannes Berg4c476992010-10-04 21:36:35 +02002513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2514 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002515 struct wireless_dev *wdev = dev->ieee80211_ptr;
2516 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002517
Johannes Berg88600202012-02-13 15:17:18 +01002518 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002519 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002520
Johannes Berg074ac8d2010-09-16 14:58:22 +02002521 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002522 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2523 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002524
Johannes Berg88600202012-02-13 15:17:18 +01002525 if (!wdev->beacon_interval)
2526 return -ENOENT;
2527
2528 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002529 if (!err)
2530 wdev->beacon_interval = 0;
2531 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002532}
2533
Johannes Berg5727ef12007-12-19 02:03:34 +01002534static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2535 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2536 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2537 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002538 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002539 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002540 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002541};
2542
Johannes Bergeccb8e82009-05-11 21:57:56 +03002543static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002544 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002545 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002546{
2547 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002548 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002549 int flag;
2550
Johannes Bergeccb8e82009-05-11 21:57:56 +03002551 /*
2552 * Try parsing the new attribute first so userspace
2553 * can specify both for older kernels.
2554 */
2555 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2556 if (nla) {
2557 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002558
Johannes Bergeccb8e82009-05-11 21:57:56 +03002559 sta_flags = nla_data(nla);
2560 params->sta_flags_mask = sta_flags->mask;
2561 params->sta_flags_set = sta_flags->set;
2562 if ((params->sta_flags_mask |
2563 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2564 return -EINVAL;
2565 return 0;
2566 }
2567
2568 /* if present, parse the old attribute */
2569
2570 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002571 if (!nla)
2572 return 0;
2573
2574 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2575 nla, sta_flags_policy))
2576 return -EINVAL;
2577
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002578 /*
2579 * Only allow certain flags for interface types so that
2580 * other attributes are silently ignored. Remember that
2581 * this is backward compatibility code with old userspace
2582 * and shouldn't be hit in other cases anyway.
2583 */
2584 switch (iftype) {
2585 case NL80211_IFTYPE_AP:
2586 case NL80211_IFTYPE_AP_VLAN:
2587 case NL80211_IFTYPE_P2P_GO:
2588 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2589 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2590 BIT(NL80211_STA_FLAG_WME) |
2591 BIT(NL80211_STA_FLAG_MFP);
2592 break;
2593 case NL80211_IFTYPE_P2P_CLIENT:
2594 case NL80211_IFTYPE_STATION:
2595 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2596 BIT(NL80211_STA_FLAG_TDLS_PEER);
2597 break;
2598 case NL80211_IFTYPE_MESH_POINT:
2599 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2600 BIT(NL80211_STA_FLAG_MFP) |
2601 BIT(NL80211_STA_FLAG_AUTHORIZED);
2602 default:
2603 return -EINVAL;
2604 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002605
Johannes Berg3383b5a2012-05-10 20:14:43 +02002606 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2607 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002608 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002609
Johannes Berg3383b5a2012-05-10 20:14:43 +02002610 /* no longer support new API additions in old API */
2611 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2612 return -EINVAL;
2613 }
2614 }
2615
Johannes Berg5727ef12007-12-19 02:03:34 +01002616 return 0;
2617}
2618
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002619static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2620 int attr)
2621{
2622 struct nlattr *rate;
2623 u16 bitrate;
2624
2625 rate = nla_nest_start(msg, attr);
2626 if (!rate)
2627 goto nla_put_failure;
2628
2629 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2630 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002631 if ((bitrate > 0 &&
2632 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2633 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2634 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2635 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2636 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2637 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2638 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2639 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002640
2641 nla_nest_end(msg, rate);
2642 return true;
2643
2644nla_put_failure:
2645 return false;
2646}
2647
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002648static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002649 int flags,
2650 struct cfg80211_registered_device *rdev,
2651 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002652 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002653{
2654 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002655 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002656
2657 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2658 if (!hdr)
2659 return -1;
2660
David S. Miller9360ffd2012-03-29 04:41:26 -04002661 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2662 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2663 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2664 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002665
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002666 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2667 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002668 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002669 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2670 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2671 sinfo->connected_time))
2672 goto nla_put_failure;
2673 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2674 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2675 sinfo->inactive_time))
2676 goto nla_put_failure;
2677 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2678 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2679 sinfo->rx_bytes))
2680 goto nla_put_failure;
2681 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2682 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2683 sinfo->tx_bytes))
2684 goto nla_put_failure;
2685 if ((sinfo->filled & STATION_INFO_LLID) &&
2686 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2687 goto nla_put_failure;
2688 if ((sinfo->filled & STATION_INFO_PLID) &&
2689 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2690 goto nla_put_failure;
2691 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2692 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2693 sinfo->plink_state))
2694 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002695 switch (rdev->wiphy.signal_type) {
2696 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002697 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2698 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2699 sinfo->signal))
2700 goto nla_put_failure;
2701 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2702 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2703 sinfo->signal_avg))
2704 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002705 break;
2706 default:
2707 break;
2708 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002709 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002710 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2711 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002712 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002713 }
2714 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2715 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2716 NL80211_STA_INFO_RX_BITRATE))
2717 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002718 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002719 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2720 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2721 sinfo->rx_packets))
2722 goto nla_put_failure;
2723 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2724 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2725 sinfo->tx_packets))
2726 goto nla_put_failure;
2727 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2728 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2729 sinfo->tx_retries))
2730 goto nla_put_failure;
2731 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2732 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2733 sinfo->tx_failed))
2734 goto nla_put_failure;
2735 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2736 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2737 sinfo->beacon_loss_count))
2738 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002739 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2740 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2741 if (!bss_param)
2742 goto nla_put_failure;
2743
David S. Miller9360ffd2012-03-29 04:41:26 -04002744 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2745 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2746 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2747 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2748 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2749 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2750 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2751 sinfo->bss_param.dtim_period) ||
2752 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2753 sinfo->bss_param.beacon_interval))
2754 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002755
2756 nla_nest_end(msg, bss_param);
2757 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002758 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2759 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2760 sizeof(struct nl80211_sta_flag_update),
2761 &sinfo->sta_flags))
2762 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002763 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2764 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2765 sinfo->t_offset))
2766 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002767 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002768
David S. Miller9360ffd2012-03-29 04:41:26 -04002769 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2770 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2771 sinfo->assoc_req_ies))
2772 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002773
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002774 return genlmsg_end(msg, hdr);
2775
2776 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002777 genlmsg_cancel(msg, hdr);
2778 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002779}
2780
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002781static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002782 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002783{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002784 struct station_info sinfo;
2785 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002786 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002787 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002788 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002789 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002790
Johannes Berg67748892010-10-04 21:14:06 +02002791 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2792 if (err)
2793 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002794
2795 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002796 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002797 goto out_err;
2798 }
2799
Johannes Bergbba95fe2008-07-29 13:22:51 +02002800 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002801 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002802 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2803 mac_addr, &sinfo);
2804 if (err == -ENOENT)
2805 break;
2806 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002807 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002808
2809 if (nl80211_send_station(skb,
2810 NETLINK_CB(cb->skb).pid,
2811 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002812 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002813 &sinfo) < 0)
2814 goto out;
2815
2816 sta_idx++;
2817 }
2818
2819
2820 out:
2821 cb->args[1] = sta_idx;
2822 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002823 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002824 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002825
2826 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002827}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002828
Johannes Berg5727ef12007-12-19 02:03:34 +01002829static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2830{
Johannes Berg4c476992010-10-04 21:36:35 +02002831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2832 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002833 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002834 struct sk_buff *msg;
2835 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002836 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002837
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002838 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002839
2840 if (!info->attrs[NL80211_ATTR_MAC])
2841 return -EINVAL;
2842
2843 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2844
Johannes Berg4c476992010-10-04 21:36:35 +02002845 if (!rdev->ops->get_station)
2846 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002847
Johannes Berg79c97e92009-07-07 03:56:12 +02002848 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002849 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002850 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002851
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002852 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002853 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002854 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002855
2856 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002857 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002858 nlmsg_free(msg);
2859 return -ENOBUFS;
2860 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002861
Johannes Berg4c476992010-10-04 21:36:35 +02002862 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002863}
2864
2865/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002866 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002867 */
Johannes Berg80b99892011-11-18 16:23:01 +01002868static struct net_device *get_vlan(struct genl_info *info,
2869 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002870{
Johannes Berg463d0182009-07-14 00:33:35 +02002871 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002872 struct net_device *v;
2873 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002874
Johannes Berg80b99892011-11-18 16:23:01 +01002875 if (!vlanattr)
2876 return NULL;
2877
2878 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2879 if (!v)
2880 return ERR_PTR(-ENODEV);
2881
2882 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2883 ret = -EINVAL;
2884 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002885 }
Johannes Berg80b99892011-11-18 16:23:01 +01002886
2887 if (!netif_running(v)) {
2888 ret = -ENETDOWN;
2889 goto error;
2890 }
2891
2892 return v;
2893 error:
2894 dev_put(v);
2895 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002896}
2897
2898static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2899{
Johannes Berg4c476992010-10-04 21:36:35 +02002900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002901 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002902 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002903 struct station_parameters params;
2904 u8 *mac_addr = NULL;
2905
2906 memset(&params, 0, sizeof(params));
2907
2908 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002909 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002910
2911 if (info->attrs[NL80211_ATTR_STA_AID])
2912 return -EINVAL;
2913
2914 if (!info->attrs[NL80211_ATTR_MAC])
2915 return -EINVAL;
2916
2917 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2918
2919 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2920 params.supported_rates =
2921 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2922 params.supported_rates_len =
2923 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2924 }
2925
2926 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2927 params.listen_interval =
2928 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2929
Jouni Malinen36aedc92008-08-25 11:58:58 +03002930 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2931 params.ht_capa =
2932 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2933
Johannes Bergbdd90d52011-12-14 12:20:27 +01002934 if (!rdev->ops->change_station)
2935 return -EOPNOTSUPP;
2936
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002937 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002938 return -EINVAL;
2939
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002940 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2941 params.plink_action =
2942 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2943
Javier Cardona9c3990a2011-05-03 16:57:11 -07002944 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2945 params.plink_state =
2946 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2947
Johannes Berga97f4422009-06-18 17:23:43 +02002948 switch (dev->ieee80211_ptr->iftype) {
2949 case NL80211_IFTYPE_AP:
2950 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002951 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002952 /* disallow mesh-specific things */
2953 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002954 return -EINVAL;
2955
2956 /* TDLS can't be set, ... */
2957 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2958 return -EINVAL;
2959 /*
2960 * ... but don't bother the driver with it. This works around
2961 * a hostapd/wpa_supplicant issue -- it always includes the
2962 * TLDS_PEER flag in the mask even for AP mode.
2963 */
2964 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2965
2966 /* accept only the listed bits */
2967 if (params.sta_flags_mask &
2968 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2969 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2970 BIT(NL80211_STA_FLAG_WME) |
2971 BIT(NL80211_STA_FLAG_MFP)))
2972 return -EINVAL;
2973
2974 /* must be last in here for error handling */
2975 params.vlan = get_vlan(info, rdev);
2976 if (IS_ERR(params.vlan))
2977 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002978 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002979 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002980 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002981 /*
2982 * Don't allow userspace to change the TDLS_PEER flag,
2983 * but silently ignore attempts to change it since we
2984 * don't have state here to verify that it doesn't try
2985 * to change the flag.
2986 */
2987 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002988 /* fall through */
2989 case NL80211_IFTYPE_ADHOC:
2990 /* disallow things sta doesn't support */
2991 if (params.plink_action)
2992 return -EINVAL;
2993 if (params.ht_capa)
2994 return -EINVAL;
2995 if (params.listen_interval >= 0)
2996 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002997 /* reject any changes other than AUTHORIZED */
2998 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2999 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003000 break;
3001 case NL80211_IFTYPE_MESH_POINT:
3002 /* disallow things mesh doesn't support */
3003 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003004 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003005 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003006 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003007 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003008 return -EINVAL;
3009 /*
3010 * No special handling for TDLS here -- the userspace
3011 * mesh code doesn't have this bug.
3012 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003013 if (params.sta_flags_mask &
3014 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003015 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003016 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003017 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003018 break;
3019 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003020 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003021 }
3022
Johannes Bergbdd90d52011-12-14 12:20:27 +01003023 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003024
Johannes Berg79c97e92009-07-07 03:56:12 +02003025 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003026
Johannes Berg5727ef12007-12-19 02:03:34 +01003027 if (params.vlan)
3028 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003029
Johannes Berg5727ef12007-12-19 02:03:34 +01003030 return err;
3031}
3032
Eliad Pellerc75786c2011-08-23 14:37:46 +03003033static struct nla_policy
3034nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3035 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3036 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3037};
3038
Johannes Berg5727ef12007-12-19 02:03:34 +01003039static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3040{
Johannes Berg4c476992010-10-04 21:36:35 +02003041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003042 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003043 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003044 struct station_parameters params;
3045 u8 *mac_addr = NULL;
3046
3047 memset(&params, 0, sizeof(params));
3048
3049 if (!info->attrs[NL80211_ATTR_MAC])
3050 return -EINVAL;
3051
Johannes Berg5727ef12007-12-19 02:03:34 +01003052 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3053 return -EINVAL;
3054
3055 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3056 return -EINVAL;
3057
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003058 if (!info->attrs[NL80211_ATTR_STA_AID])
3059 return -EINVAL;
3060
Johannes Berg5727ef12007-12-19 02:03:34 +01003061 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3062 params.supported_rates =
3063 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3064 params.supported_rates_len =
3065 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3066 params.listen_interval =
3067 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003068
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003069 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3070 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3071 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003072
Jouni Malinen36aedc92008-08-25 11:58:58 +03003073 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3074 params.ht_capa =
3075 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003076
Javier Cardona96b78df2011-04-07 15:08:33 -07003077 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3078 params.plink_action =
3079 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3080
Johannes Bergbdd90d52011-12-14 12:20:27 +01003081 if (!rdev->ops->add_station)
3082 return -EOPNOTSUPP;
3083
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003084 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003085 return -EINVAL;
3086
Johannes Bergbdd90d52011-12-14 12:20:27 +01003087 switch (dev->ieee80211_ptr->iftype) {
3088 case NL80211_IFTYPE_AP:
3089 case NL80211_IFTYPE_AP_VLAN:
3090 case NL80211_IFTYPE_P2P_GO:
3091 /* parse WME attributes if sta is WME capable */
3092 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3093 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3094 info->attrs[NL80211_ATTR_STA_WME]) {
3095 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3096 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003097
Johannes Bergbdd90d52011-12-14 12:20:27 +01003098 nla = info->attrs[NL80211_ATTR_STA_WME];
3099 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3100 nl80211_sta_wme_policy);
3101 if (err)
3102 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003103
Johannes Bergbdd90d52011-12-14 12:20:27 +01003104 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3105 params.uapsd_queues =
3106 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3107 if (params.uapsd_queues &
3108 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3109 return -EINVAL;
3110
3111 if (tb[NL80211_STA_WME_MAX_SP])
3112 params.max_sp =
3113 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3114
3115 if (params.max_sp &
3116 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3117 return -EINVAL;
3118
3119 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3120 }
3121 /* TDLS peers cannot be added */
3122 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003123 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003124 /* but don't bother the driver with it */
3125 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003126
Johannes Bergbdd90d52011-12-14 12:20:27 +01003127 /* must be last in here for error handling */
3128 params.vlan = get_vlan(info, rdev);
3129 if (IS_ERR(params.vlan))
3130 return PTR_ERR(params.vlan);
3131 break;
3132 case NL80211_IFTYPE_MESH_POINT:
3133 /* TDLS peers cannot be added */
3134 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003135 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003136 break;
3137 case NL80211_IFTYPE_STATION:
3138 /* Only TDLS peers can be added */
3139 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3140 return -EINVAL;
3141 /* Can only add if TDLS ... */
3142 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3143 return -EOPNOTSUPP;
3144 /* ... with external setup is supported */
3145 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3146 return -EOPNOTSUPP;
3147 break;
3148 default:
3149 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003150 }
3151
Johannes Bergbdd90d52011-12-14 12:20:27 +01003152 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003153
Johannes Berg79c97e92009-07-07 03:56:12 +02003154 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003155
Johannes Berg5727ef12007-12-19 02:03:34 +01003156 if (params.vlan)
3157 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003158 return err;
3159}
3160
3161static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3162{
Johannes Berg4c476992010-10-04 21:36:35 +02003163 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3164 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003165 u8 *mac_addr = NULL;
3166
3167 if (info->attrs[NL80211_ATTR_MAC])
3168 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3169
Johannes Berge80cf852009-05-11 14:43:13 +02003170 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003171 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003172 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003173 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3174 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003175
Johannes Berg4c476992010-10-04 21:36:35 +02003176 if (!rdev->ops->del_station)
3177 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003178
Johannes Berg4c476992010-10-04 21:36:35 +02003179 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003180}
3181
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003182static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3183 int flags, struct net_device *dev,
3184 u8 *dst, u8 *next_hop,
3185 struct mpath_info *pinfo)
3186{
3187 void *hdr;
3188 struct nlattr *pinfoattr;
3189
3190 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3191 if (!hdr)
3192 return -1;
3193
David S. Miller9360ffd2012-03-29 04:41:26 -04003194 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3195 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3196 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3197 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3198 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003199
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003200 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3201 if (!pinfoattr)
3202 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003203 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3204 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3205 pinfo->frame_qlen))
3206 goto nla_put_failure;
3207 if (((pinfo->filled & MPATH_INFO_SN) &&
3208 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3209 ((pinfo->filled & MPATH_INFO_METRIC) &&
3210 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3211 pinfo->metric)) ||
3212 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3213 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3214 pinfo->exptime)) ||
3215 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3216 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3217 pinfo->flags)) ||
3218 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3219 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3220 pinfo->discovery_timeout)) ||
3221 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3222 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3223 pinfo->discovery_retries)))
3224 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003225
3226 nla_nest_end(msg, pinfoattr);
3227
3228 return genlmsg_end(msg, hdr);
3229
3230 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003231 genlmsg_cancel(msg, hdr);
3232 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003233}
3234
3235static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003236 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003237{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003238 struct mpath_info pinfo;
3239 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003240 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003241 u8 dst[ETH_ALEN];
3242 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003243 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003244 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003245
Johannes Berg67748892010-10-04 21:14:06 +02003246 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3247 if (err)
3248 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003249
3250 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003251 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003252 goto out_err;
3253 }
3254
Jouni Malineneec60b02009-03-20 21:21:19 +02003255 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3256 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003257 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003258 }
3259
Johannes Bergbba95fe2008-07-29 13:22:51 +02003260 while (1) {
3261 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3262 dst, next_hop, &pinfo);
3263 if (err == -ENOENT)
3264 break;
3265 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003266 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003267
3268 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3270 netdev, dst, next_hop,
3271 &pinfo) < 0)
3272 goto out;
3273
3274 path_idx++;
3275 }
3276
3277
3278 out:
3279 cb->args[1] = path_idx;
3280 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003281 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003282 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003283 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003284}
3285
3286static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3287{
Johannes Berg4c476992010-10-04 21:36:35 +02003288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003289 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003290 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003291 struct mpath_info pinfo;
3292 struct sk_buff *msg;
3293 u8 *dst = NULL;
3294 u8 next_hop[ETH_ALEN];
3295
3296 memset(&pinfo, 0, sizeof(pinfo));
3297
3298 if (!info->attrs[NL80211_ATTR_MAC])
3299 return -EINVAL;
3300
3301 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3302
Johannes Berg4c476992010-10-04 21:36:35 +02003303 if (!rdev->ops->get_mpath)
3304 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003305
Johannes Berg4c476992010-10-04 21:36:35 +02003306 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3307 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003308
Johannes Berg79c97e92009-07-07 03:56:12 +02003309 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003310 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003311 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003312
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003313 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003314 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003315 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003316
3317 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003318 dev, dst, next_hop, &pinfo) < 0) {
3319 nlmsg_free(msg);
3320 return -ENOBUFS;
3321 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003322
Johannes Berg4c476992010-10-04 21:36:35 +02003323 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003324}
3325
3326static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3327{
Johannes Berg4c476992010-10-04 21:36:35 +02003328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3329 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003330 u8 *dst = NULL;
3331 u8 *next_hop = NULL;
3332
3333 if (!info->attrs[NL80211_ATTR_MAC])
3334 return -EINVAL;
3335
3336 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3337 return -EINVAL;
3338
3339 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3340 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3341
Johannes Berg4c476992010-10-04 21:36:35 +02003342 if (!rdev->ops->change_mpath)
3343 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003344
Johannes Berg4c476992010-10-04 21:36:35 +02003345 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3346 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003347
Johannes Berg4c476992010-10-04 21:36:35 +02003348 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003349}
Johannes Berg4c476992010-10-04 21:36:35 +02003350
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003351static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3352{
Johannes Berg4c476992010-10-04 21:36:35 +02003353 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3354 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003355 u8 *dst = NULL;
3356 u8 *next_hop = NULL;
3357
3358 if (!info->attrs[NL80211_ATTR_MAC])
3359 return -EINVAL;
3360
3361 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3362 return -EINVAL;
3363
3364 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3365 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3366
Johannes Berg4c476992010-10-04 21:36:35 +02003367 if (!rdev->ops->add_mpath)
3368 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003369
Johannes Berg4c476992010-10-04 21:36:35 +02003370 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3371 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003372
Johannes Berg4c476992010-10-04 21:36:35 +02003373 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003374}
3375
3376static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3377{
Johannes Berg4c476992010-10-04 21:36:35 +02003378 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3379 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003380 u8 *dst = NULL;
3381
3382 if (info->attrs[NL80211_ATTR_MAC])
3383 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3384
Johannes Berg4c476992010-10-04 21:36:35 +02003385 if (!rdev->ops->del_mpath)
3386 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003387
Johannes Berg4c476992010-10-04 21:36:35 +02003388 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003389}
3390
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003391static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3392{
Johannes Berg4c476992010-10-04 21:36:35 +02003393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3394 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003395 struct bss_parameters params;
3396
3397 memset(&params, 0, sizeof(params));
3398 /* default to not changing parameters */
3399 params.use_cts_prot = -1;
3400 params.use_short_preamble = -1;
3401 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003402 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003403 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003404
3405 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3406 params.use_cts_prot =
3407 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3408 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3409 params.use_short_preamble =
3410 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3411 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3412 params.use_short_slot_time =
3413 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003414 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3415 params.basic_rates =
3416 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3417 params.basic_rates_len =
3418 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3419 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003420 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3421 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003422 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3423 params.ht_opmode =
3424 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003425
Johannes Berg4c476992010-10-04 21:36:35 +02003426 if (!rdev->ops->change_bss)
3427 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003428
Johannes Berg074ac8d2010-09-16 14:58:22 +02003429 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003430 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3431 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003432
Johannes Berg4c476992010-10-04 21:36:35 +02003433 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003434}
3435
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003436static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003437 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3438 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3439 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3440 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3441 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3442 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3443};
3444
3445static int parse_reg_rule(struct nlattr *tb[],
3446 struct ieee80211_reg_rule *reg_rule)
3447{
3448 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3449 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3450
3451 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3452 return -EINVAL;
3453 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3454 return -EINVAL;
3455 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3456 return -EINVAL;
3457 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3458 return -EINVAL;
3459 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3460 return -EINVAL;
3461
3462 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3463
3464 freq_range->start_freq_khz =
3465 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3466 freq_range->end_freq_khz =
3467 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3468 freq_range->max_bandwidth_khz =
3469 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3470
3471 power_rule->max_eirp =
3472 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3473
3474 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3475 power_rule->max_antenna_gain =
3476 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3477
3478 return 0;
3479}
3480
3481static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3482{
3483 int r;
3484 char *data = NULL;
3485
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003486 /*
3487 * You should only get this when cfg80211 hasn't yet initialized
3488 * completely when built-in to the kernel right between the time
3489 * window between nl80211_init() and regulatory_init(), if that is
3490 * even possible.
3491 */
3492 mutex_lock(&cfg80211_mutex);
3493 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003494 mutex_unlock(&cfg80211_mutex);
3495 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003496 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003497 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003498
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003499 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3500 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003501
3502 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3503
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003504 r = regulatory_hint_user(data);
3505
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003506 return r;
3507}
3508
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003509static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003510 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003511{
Johannes Berg4c476992010-10-04 21:36:35 +02003512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003513 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003514 struct wireless_dev *wdev = dev->ieee80211_ptr;
3515 struct mesh_config cur_params;
3516 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003517 void *hdr;
3518 struct nlattr *pinfoattr;
3519 struct sk_buff *msg;
3520
Johannes Berg29cbe682010-12-03 09:20:44 +01003521 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3522 return -EOPNOTSUPP;
3523
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003524 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003525 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003526
Johannes Berg29cbe682010-12-03 09:20:44 +01003527 wdev_lock(wdev);
3528 /* If not connected, get default parameters */
3529 if (!wdev->mesh_id_len)
3530 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3531 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003532 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003533 &cur_params);
3534 wdev_unlock(wdev);
3535
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003536 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003537 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003538
3539 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003540 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003541 if (!msg)
3542 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003543 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003544 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003545 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003546 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003547 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003548 if (!pinfoattr)
3549 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003550 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3551 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3552 cur_params.dot11MeshRetryTimeout) ||
3553 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3554 cur_params.dot11MeshConfirmTimeout) ||
3555 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3556 cur_params.dot11MeshHoldingTimeout) ||
3557 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3558 cur_params.dot11MeshMaxPeerLinks) ||
3559 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3560 cur_params.dot11MeshMaxRetries) ||
3561 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3562 cur_params.dot11MeshTTL) ||
3563 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3564 cur_params.element_ttl) ||
3565 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3566 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003567 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3568 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003569 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3570 cur_params.dot11MeshHWMPmaxPREQretries) ||
3571 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3572 cur_params.path_refresh_time) ||
3573 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3574 cur_params.min_discovery_timeout) ||
3575 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3576 cur_params.dot11MeshHWMPactivePathTimeout) ||
3577 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3578 cur_params.dot11MeshHWMPpreqMinInterval) ||
3579 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3580 cur_params.dot11MeshHWMPperrMinInterval) ||
3581 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3582 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3583 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3584 cur_params.dot11MeshHWMPRootMode) ||
3585 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3586 cur_params.dot11MeshHWMPRannInterval) ||
3587 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3588 cur_params.dot11MeshGateAnnouncementProtocol) ||
3589 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3590 cur_params.dot11MeshForwarding) ||
3591 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003592 cur_params.rssi_threshold) ||
3593 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003594 cur_params.ht_opmode) ||
3595 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3596 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3597 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003598 cur_params.dot11MeshHWMProotInterval) ||
3599 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3600 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003601 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003602 nla_nest_end(msg, pinfoattr);
3603 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003604 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003605
Johannes Berg3b858752009-03-12 09:55:09 +01003606 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003607 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003608 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003609 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003610 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003611}
3612
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003613static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003614 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3615 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3616 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3617 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3618 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3619 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003620 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003621 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003622 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003623 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3624 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3625 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3626 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3627 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003628 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003629 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003630 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003631 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003632 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003633 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003634 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3635 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003636 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3637 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003638 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003639};
3640
Javier Cardonac80d5452010-12-16 17:37:49 -08003641static const struct nla_policy
3642 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003643 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003644 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3645 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003646 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003647 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003648 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003649 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003650};
3651
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003652static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003653 struct mesh_config *cfg,
3654 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003655{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003656 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003657 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003658
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003659#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3660do {\
3661 if (table[attr_num]) {\
3662 cfg->param = nla_fn(table[attr_num]); \
3663 mask |= (1 << (attr_num - 1)); \
3664 } \
3665} while (0);\
3666
3667
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003668 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003669 return -EINVAL;
3670 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003671 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003672 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003673 return -EINVAL;
3674
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003675 /* This makes sure that there aren't more than 32 mesh config
3676 * parameters (otherwise our bitfield scheme would not work.) */
3677 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3678
3679 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003680 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003681 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3682 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003683 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003684 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3685 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003686 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003687 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3688 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003689 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003690 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3691 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003692 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003693 mask, NL80211_MESHCONF_MAX_RETRIES,
3694 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003695 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003696 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003697 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003698 mask, NL80211_MESHCONF_ELEMENT_TTL,
3699 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003700 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003701 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3702 nla_get_u8);
3703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3704 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3705 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003706 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003707 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3708 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003710 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3711 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003712 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003713 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3714 nla_get_u16);
3715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3716 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3717 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003718 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003719 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3720 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003721 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003722 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3723 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003724 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003725 dot11MeshHWMPnetDiameterTraversalTime, mask,
3726 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3727 nla_get_u16);
3728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3729 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3730 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3731 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3732 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003733 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003734 dot11MeshGateAnnouncementProtocol, mask,
3735 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3736 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003737 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003738 mask, NL80211_MESHCONF_FORWARDING,
3739 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003740 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003741 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3742 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003743 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003744 mask, NL80211_MESHCONF_HT_OPMODE,
3745 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003746 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3747 mask,
3748 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3749 nla_get_u32);
3750 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3751 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3752 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003753 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3754 dot11MeshHWMPconfirmationInterval, mask,
3755 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3756 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003757 if (mask_out)
3758 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003759
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003760 return 0;
3761
3762#undef FILL_IN_MESH_PARAM_IF_SET
3763}
3764
Javier Cardonac80d5452010-12-16 17:37:49 -08003765static int nl80211_parse_mesh_setup(struct genl_info *info,
3766 struct mesh_setup *setup)
3767{
3768 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3769
3770 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3771 return -EINVAL;
3772 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3773 info->attrs[NL80211_ATTR_MESH_SETUP],
3774 nl80211_mesh_setup_params_policy))
3775 return -EINVAL;
3776
Javier Cardonad299a1f2012-03-31 11:31:33 -07003777 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3778 setup->sync_method =
3779 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3780 IEEE80211_SYNC_METHOD_VENDOR :
3781 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3782
Javier Cardonac80d5452010-12-16 17:37:49 -08003783 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3784 setup->path_sel_proto =
3785 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3786 IEEE80211_PATH_PROTOCOL_VENDOR :
3787 IEEE80211_PATH_PROTOCOL_HWMP;
3788
3789 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3790 setup->path_metric =
3791 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3792 IEEE80211_PATH_METRIC_VENDOR :
3793 IEEE80211_PATH_METRIC_AIRTIME;
3794
Javier Cardona581a8b02011-04-07 15:08:27 -07003795
3796 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003797 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003798 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003799 if (!is_valid_ie_attr(ieattr))
3800 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003801 setup->ie = nla_data(ieattr);
3802 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003803 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003804 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3805 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003806
3807 return 0;
3808}
3809
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003810static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003811 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003812{
3813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3814 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003815 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003816 struct mesh_config cfg;
3817 u32 mask;
3818 int err;
3819
Johannes Berg29cbe682010-12-03 09:20:44 +01003820 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3821 return -EOPNOTSUPP;
3822
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003823 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003824 return -EOPNOTSUPP;
3825
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003826 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003827 if (err)
3828 return err;
3829
Johannes Berg29cbe682010-12-03 09:20:44 +01003830 wdev_lock(wdev);
3831 if (!wdev->mesh_id_len)
3832 err = -ENOLINK;
3833
3834 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003835 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003836 mask, &cfg);
3837
3838 wdev_unlock(wdev);
3839
3840 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003841}
3842
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003843static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3844{
3845 struct sk_buff *msg;
3846 void *hdr = NULL;
3847 struct nlattr *nl_reg_rules;
3848 unsigned int i;
3849 int err = -EINVAL;
3850
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003851 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003852
3853 if (!cfg80211_regdomain)
3854 goto out;
3855
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003856 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003857 if (!msg) {
3858 err = -ENOBUFS;
3859 goto out;
3860 }
3861
3862 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3863 NL80211_CMD_GET_REG);
3864 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003865 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003866
David S. Miller9360ffd2012-03-29 04:41:26 -04003867 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3868 cfg80211_regdomain->alpha2) ||
3869 (cfg80211_regdomain->dfs_region &&
3870 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3871 cfg80211_regdomain->dfs_region)))
3872 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003873
3874 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3875 if (!nl_reg_rules)
3876 goto nla_put_failure;
3877
3878 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3879 struct nlattr *nl_reg_rule;
3880 const struct ieee80211_reg_rule *reg_rule;
3881 const struct ieee80211_freq_range *freq_range;
3882 const struct ieee80211_power_rule *power_rule;
3883
3884 reg_rule = &cfg80211_regdomain->reg_rules[i];
3885 freq_range = &reg_rule->freq_range;
3886 power_rule = &reg_rule->power_rule;
3887
3888 nl_reg_rule = nla_nest_start(msg, i);
3889 if (!nl_reg_rule)
3890 goto nla_put_failure;
3891
David S. Miller9360ffd2012-03-29 04:41:26 -04003892 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3893 reg_rule->flags) ||
3894 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3895 freq_range->start_freq_khz) ||
3896 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3897 freq_range->end_freq_khz) ||
3898 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3899 freq_range->max_bandwidth_khz) ||
3900 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3901 power_rule->max_antenna_gain) ||
3902 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3903 power_rule->max_eirp))
3904 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003905
3906 nla_nest_end(msg, nl_reg_rule);
3907 }
3908
3909 nla_nest_end(msg, nl_reg_rules);
3910
3911 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003912 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003913 goto out;
3914
3915nla_put_failure:
3916 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003917put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003918 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003919 err = -EMSGSIZE;
3920out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003921 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003922 return err;
3923}
3924
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003925static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3926{
3927 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3928 struct nlattr *nl_reg_rule;
3929 char *alpha2 = NULL;
3930 int rem_reg_rules = 0, r = 0;
3931 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003932 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003933 struct ieee80211_regdomain *rd = NULL;
3934
3935 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3936 return -EINVAL;
3937
3938 if (!info->attrs[NL80211_ATTR_REG_RULES])
3939 return -EINVAL;
3940
3941 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3942
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003943 if (info->attrs[NL80211_ATTR_DFS_REGION])
3944 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3945
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003946 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3947 rem_reg_rules) {
3948 num_rules++;
3949 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003950 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003951 }
3952
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003953 mutex_lock(&cfg80211_mutex);
3954
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003955 if (!reg_is_valid_request(alpha2)) {
3956 r = -EINVAL;
3957 goto bad_reg;
3958 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003959
3960 size_of_regd = sizeof(struct ieee80211_regdomain) +
3961 (num_rules * sizeof(struct ieee80211_reg_rule));
3962
3963 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003964 if (!rd) {
3965 r = -ENOMEM;
3966 goto bad_reg;
3967 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003968
3969 rd->n_reg_rules = num_rules;
3970 rd->alpha2[0] = alpha2[0];
3971 rd->alpha2[1] = alpha2[1];
3972
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003973 /*
3974 * Disable DFS master mode if the DFS region was
3975 * not supported or known on this kernel.
3976 */
3977 if (reg_supported_dfs_region(dfs_region))
3978 rd->dfs_region = dfs_region;
3979
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003980 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3981 rem_reg_rules) {
3982 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3983 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3984 reg_rule_policy);
3985 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3986 if (r)
3987 goto bad_reg;
3988
3989 rule_idx++;
3990
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003991 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3992 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003993 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003994 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003995 }
3996
3997 BUG_ON(rule_idx != num_rules);
3998
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003999 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004000
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004001 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004002
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004003 return r;
4004
Johannes Bergd2372b32008-10-24 20:32:20 +02004005 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004006 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004007 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004008 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004009}
4010
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004011static int validate_scan_freqs(struct nlattr *freqs)
4012{
4013 struct nlattr *attr1, *attr2;
4014 int n_channels = 0, tmp1, tmp2;
4015
4016 nla_for_each_nested(attr1, freqs, tmp1) {
4017 n_channels++;
4018 /*
4019 * Some hardware has a limited channel list for
4020 * scanning, and it is pretty much nonsensical
4021 * to scan for a channel twice, so disallow that
4022 * and don't require drivers to check that the
4023 * channel list they get isn't longer than what
4024 * they can scan, as long as they can scan all
4025 * the channels they registered at once.
4026 */
4027 nla_for_each_nested(attr2, freqs, tmp2)
4028 if (attr1 != attr2 &&
4029 nla_get_u32(attr1) == nla_get_u32(attr2))
4030 return 0;
4031 }
4032
4033 return n_channels;
4034}
4035
Johannes Berg2a519312009-02-10 21:25:55 +01004036static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4037{
Johannes Berg4c476992010-10-04 21:36:35 +02004038 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4039 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004040 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004041 struct nlattr *attr;
4042 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004043 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004044 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004045
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004046 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4047 return -EINVAL;
4048
Johannes Berg79c97e92009-07-07 03:56:12 +02004049 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004050
Johannes Berg4c476992010-10-04 21:36:35 +02004051 if (!rdev->ops->scan)
4052 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004053
Johannes Berg4c476992010-10-04 21:36:35 +02004054 if (rdev->scan_req)
4055 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004056
4057 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004058 n_channels = validate_scan_freqs(
4059 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004060 if (!n_channels)
4061 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004062 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004063 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004064 n_channels = 0;
4065
Johannes Berg2a519312009-02-10 21:25:55 +01004066 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4067 if (wiphy->bands[band])
4068 n_channels += wiphy->bands[band]->n_channels;
4069 }
4070
4071 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4072 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4073 n_ssids++;
4074
Johannes Berg4c476992010-10-04 21:36:35 +02004075 if (n_ssids > wiphy->max_scan_ssids)
4076 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004077
Jouni Malinen70692ad2009-02-16 19:39:13 +02004078 if (info->attrs[NL80211_ATTR_IE])
4079 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4080 else
4081 ie_len = 0;
4082
Johannes Berg4c476992010-10-04 21:36:35 +02004083 if (ie_len > wiphy->max_scan_ie_len)
4084 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004085
Johannes Berg2a519312009-02-10 21:25:55 +01004086 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004087 + sizeof(*request->ssids) * n_ssids
4088 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004089 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004090 if (!request)
4091 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004092
Johannes Berg2a519312009-02-10 21:25:55 +01004093 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004094 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004095 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004096 if (ie_len) {
4097 if (request->ssids)
4098 request->ie = (void *)(request->ssids + n_ssids);
4099 else
4100 request->ie = (void *)(request->channels + n_channels);
4101 }
Johannes Berg2a519312009-02-10 21:25:55 +01004102
Johannes Berg584991d2009-11-02 13:32:03 +01004103 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004104 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4105 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004106 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004107 struct ieee80211_channel *chan;
4108
4109 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4110
4111 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004112 err = -EINVAL;
4113 goto out_free;
4114 }
Johannes Berg584991d2009-11-02 13:32:03 +01004115
4116 /* ignore disabled channels */
4117 if (chan->flags & IEEE80211_CHAN_DISABLED)
4118 continue;
4119
4120 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004121 i++;
4122 }
4123 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004124 enum ieee80211_band band;
4125
Johannes Berg2a519312009-02-10 21:25:55 +01004126 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004127 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4128 int j;
4129 if (!wiphy->bands[band])
4130 continue;
4131 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004132 struct ieee80211_channel *chan;
4133
4134 chan = &wiphy->bands[band]->channels[j];
4135
4136 if (chan->flags & IEEE80211_CHAN_DISABLED)
4137 continue;
4138
4139 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004140 i++;
4141 }
4142 }
4143 }
4144
Johannes Berg584991d2009-11-02 13:32:03 +01004145 if (!i) {
4146 err = -EINVAL;
4147 goto out_free;
4148 }
4149
4150 request->n_channels = i;
4151
Johannes Berg2a519312009-02-10 21:25:55 +01004152 i = 0;
4153 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4154 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004155 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004156 err = -EINVAL;
4157 goto out_free;
4158 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004159 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004160 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004161 i++;
4162 }
4163 }
4164
Jouni Malinen70692ad2009-02-16 19:39:13 +02004165 if (info->attrs[NL80211_ATTR_IE]) {
4166 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004167 memcpy((void *)request->ie,
4168 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004169 request->ie_len);
4170 }
4171
Johannes Berg34850ab2011-07-18 18:08:35 +02004172 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004173 if (wiphy->bands[i])
4174 request->rates[i] =
4175 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004176
4177 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4178 nla_for_each_nested(attr,
4179 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4180 tmp) {
4181 enum ieee80211_band band = nla_type(attr);
4182
Dan Carpenter84404622011-07-29 11:52:18 +03004183 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004184 err = -EINVAL;
4185 goto out_free;
4186 }
4187 err = ieee80211_get_ratemask(wiphy->bands[band],
4188 nla_data(attr),
4189 nla_len(attr),
4190 &request->rates[band]);
4191 if (err)
4192 goto out_free;
4193 }
4194 }
4195
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304196 request->no_cck =
4197 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4198
Johannes Berg463d0182009-07-14 00:33:35 +02004199 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004200 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004201
Johannes Berg79c97e92009-07-07 03:56:12 +02004202 rdev->scan_req = request;
4203 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004204
Johannes Berg463d0182009-07-14 00:33:35 +02004205 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004206 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004207 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004208 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004209 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004210 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004211 kfree(request);
4212 }
Johannes Berg3b858752009-03-12 09:55:09 +01004213
Johannes Berg2a519312009-02-10 21:25:55 +01004214 return err;
4215}
4216
Luciano Coelho807f8a82011-05-11 17:09:35 +03004217static int nl80211_start_sched_scan(struct sk_buff *skb,
4218 struct genl_info *info)
4219{
4220 struct cfg80211_sched_scan_request *request;
4221 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4222 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004223 struct nlattr *attr;
4224 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004225 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004226 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004227 enum ieee80211_band band;
4228 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004229 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004230
4231 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4232 !rdev->ops->sched_scan_start)
4233 return -EOPNOTSUPP;
4234
4235 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4236 return -EINVAL;
4237
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004238 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4239 return -EINVAL;
4240
4241 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4242 if (interval == 0)
4243 return -EINVAL;
4244
Luciano Coelho807f8a82011-05-11 17:09:35 +03004245 wiphy = &rdev->wiphy;
4246
4247 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4248 n_channels = validate_scan_freqs(
4249 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4250 if (!n_channels)
4251 return -EINVAL;
4252 } else {
4253 n_channels = 0;
4254
4255 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4256 if (wiphy->bands[band])
4257 n_channels += wiphy->bands[band]->n_channels;
4258 }
4259
4260 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4261 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4262 tmp)
4263 n_ssids++;
4264
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004265 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004266 return -EINVAL;
4267
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004268 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4269 nla_for_each_nested(attr,
4270 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4271 tmp)
4272 n_match_sets++;
4273
4274 if (n_match_sets > wiphy->max_match_sets)
4275 return -EINVAL;
4276
Luciano Coelho807f8a82011-05-11 17:09:35 +03004277 if (info->attrs[NL80211_ATTR_IE])
4278 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4279 else
4280 ie_len = 0;
4281
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004282 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004283 return -EINVAL;
4284
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004285 mutex_lock(&rdev->sched_scan_mtx);
4286
4287 if (rdev->sched_scan_req) {
4288 err = -EINPROGRESS;
4289 goto out;
4290 }
4291
Luciano Coelho807f8a82011-05-11 17:09:35 +03004292 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004293 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004294 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004295 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004296 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004297 if (!request) {
4298 err = -ENOMEM;
4299 goto out;
4300 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004301
4302 if (n_ssids)
4303 request->ssids = (void *)&request->channels[n_channels];
4304 request->n_ssids = n_ssids;
4305 if (ie_len) {
4306 if (request->ssids)
4307 request->ie = (void *)(request->ssids + n_ssids);
4308 else
4309 request->ie = (void *)(request->channels + n_channels);
4310 }
4311
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004312 if (n_match_sets) {
4313 if (request->ie)
4314 request->match_sets = (void *)(request->ie + ie_len);
4315 else if (request->ssids)
4316 request->match_sets =
4317 (void *)(request->ssids + n_ssids);
4318 else
4319 request->match_sets =
4320 (void *)(request->channels + n_channels);
4321 }
4322 request->n_match_sets = n_match_sets;
4323
Luciano Coelho807f8a82011-05-11 17:09:35 +03004324 i = 0;
4325 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4326 /* user specified, bail out if channel not found */
4327 nla_for_each_nested(attr,
4328 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4329 tmp) {
4330 struct ieee80211_channel *chan;
4331
4332 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4333
4334 if (!chan) {
4335 err = -EINVAL;
4336 goto out_free;
4337 }
4338
4339 /* ignore disabled channels */
4340 if (chan->flags & IEEE80211_CHAN_DISABLED)
4341 continue;
4342
4343 request->channels[i] = chan;
4344 i++;
4345 }
4346 } else {
4347 /* all channels */
4348 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4349 int j;
4350 if (!wiphy->bands[band])
4351 continue;
4352 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4353 struct ieee80211_channel *chan;
4354
4355 chan = &wiphy->bands[band]->channels[j];
4356
4357 if (chan->flags & IEEE80211_CHAN_DISABLED)
4358 continue;
4359
4360 request->channels[i] = chan;
4361 i++;
4362 }
4363 }
4364 }
4365
4366 if (!i) {
4367 err = -EINVAL;
4368 goto out_free;
4369 }
4370
4371 request->n_channels = i;
4372
4373 i = 0;
4374 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4375 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4376 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004377 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004378 err = -EINVAL;
4379 goto out_free;
4380 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004381 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004382 memcpy(request->ssids[i].ssid, nla_data(attr),
4383 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004384 i++;
4385 }
4386 }
4387
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004388 i = 0;
4389 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4390 nla_for_each_nested(attr,
4391 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4392 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004393 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004394
4395 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4396 nla_data(attr), nla_len(attr),
4397 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004398 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004399 if (ssid) {
4400 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4401 err = -EINVAL;
4402 goto out_free;
4403 }
4404 memcpy(request->match_sets[i].ssid.ssid,
4405 nla_data(ssid), nla_len(ssid));
4406 request->match_sets[i].ssid.ssid_len =
4407 nla_len(ssid);
4408 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004409 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4410 if (rssi)
4411 request->rssi_thold = nla_get_u32(rssi);
4412 else
4413 request->rssi_thold =
4414 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004415 i++;
4416 }
4417 }
4418
Luciano Coelho807f8a82011-05-11 17:09:35 +03004419 if (info->attrs[NL80211_ATTR_IE]) {
4420 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4421 memcpy((void *)request->ie,
4422 nla_data(info->attrs[NL80211_ATTR_IE]),
4423 request->ie_len);
4424 }
4425
4426 request->dev = dev;
4427 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004428 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004429
4430 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4431 if (!err) {
4432 rdev->sched_scan_req = request;
4433 nl80211_send_sched_scan(rdev, dev,
4434 NL80211_CMD_START_SCHED_SCAN);
4435 goto out;
4436 }
4437
4438out_free:
4439 kfree(request);
4440out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004441 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004442 return err;
4443}
4444
4445static int nl80211_stop_sched_scan(struct sk_buff *skb,
4446 struct genl_info *info)
4447{
4448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004449 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004450
4451 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4452 !rdev->ops->sched_scan_stop)
4453 return -EOPNOTSUPP;
4454
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004455 mutex_lock(&rdev->sched_scan_mtx);
4456 err = __cfg80211_stop_sched_scan(rdev, false);
4457 mutex_unlock(&rdev->sched_scan_mtx);
4458
4459 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004460}
4461
Johannes Berg9720bb32011-06-21 09:45:33 +02004462static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4463 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004464 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004465 struct wireless_dev *wdev,
4466 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004467{
Johannes Berg48ab9052009-07-10 18:42:31 +02004468 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004469 void *hdr;
4470 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004471
4472 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004473
Johannes Berg9720bb32011-06-21 09:45:33 +02004474 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004475 NL80211_CMD_NEW_SCAN_RESULTS);
4476 if (!hdr)
4477 return -1;
4478
Johannes Berg9720bb32011-06-21 09:45:33 +02004479 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4480
David S. Miller9360ffd2012-03-29 04:41:26 -04004481 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4482 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4483 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004484
4485 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4486 if (!bss)
4487 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004488 if ((!is_zero_ether_addr(res->bssid) &&
4489 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4490 (res->information_elements && res->len_information_elements &&
4491 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4492 res->len_information_elements,
4493 res->information_elements)) ||
4494 (res->beacon_ies && res->len_beacon_ies &&
4495 res->beacon_ies != res->information_elements &&
4496 nla_put(msg, NL80211_BSS_BEACON_IES,
4497 res->len_beacon_ies, res->beacon_ies)))
4498 goto nla_put_failure;
4499 if (res->tsf &&
4500 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4501 goto nla_put_failure;
4502 if (res->beacon_interval &&
4503 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4504 goto nla_put_failure;
4505 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4506 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4507 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4508 jiffies_to_msecs(jiffies - intbss->ts)))
4509 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004510
Johannes Berg77965c92009-02-18 18:45:06 +01004511 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004512 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004513 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4514 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004515 break;
4516 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004517 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4518 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004519 break;
4520 default:
4521 break;
4522 }
4523
Johannes Berg48ab9052009-07-10 18:42:31 +02004524 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004525 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004526 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004527 if (intbss == wdev->current_bss &&
4528 nla_put_u32(msg, NL80211_BSS_STATUS,
4529 NL80211_BSS_STATUS_ASSOCIATED))
4530 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004531 break;
4532 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004533 if (intbss == wdev->current_bss &&
4534 nla_put_u32(msg, NL80211_BSS_STATUS,
4535 NL80211_BSS_STATUS_IBSS_JOINED))
4536 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004537 break;
4538 default:
4539 break;
4540 }
4541
Johannes Berg2a519312009-02-10 21:25:55 +01004542 nla_nest_end(msg, bss);
4543
4544 return genlmsg_end(msg, hdr);
4545
4546 nla_put_failure:
4547 genlmsg_cancel(msg, hdr);
4548 return -EMSGSIZE;
4549}
4550
4551static int nl80211_dump_scan(struct sk_buff *skb,
4552 struct netlink_callback *cb)
4553{
Johannes Berg48ab9052009-07-10 18:42:31 +02004554 struct cfg80211_registered_device *rdev;
4555 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004556 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004557 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004558 int start = cb->args[1], idx = 0;
4559 int err;
4560
Johannes Berg67748892010-10-04 21:14:06 +02004561 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4562 if (err)
4563 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004564
Johannes Berg48ab9052009-07-10 18:42:31 +02004565 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004566
Johannes Berg48ab9052009-07-10 18:42:31 +02004567 wdev_lock(wdev);
4568 spin_lock_bh(&rdev->bss_lock);
4569 cfg80211_bss_expire(rdev);
4570
Johannes Berg9720bb32011-06-21 09:45:33 +02004571 cb->seq = rdev->bss_generation;
4572
Johannes Berg48ab9052009-07-10 18:42:31 +02004573 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004574 if (++idx <= start)
4575 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004576 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004577 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004578 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004579 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004580 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004581 }
4582 }
4583
Johannes Berg48ab9052009-07-10 18:42:31 +02004584 spin_unlock_bh(&rdev->bss_lock);
4585 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004586
4587 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004588 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004589
Johannes Berg67748892010-10-04 21:14:06 +02004590 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004591}
4592
Holger Schurig61fa7132009-11-11 12:25:40 +01004593static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4594 int flags, struct net_device *dev,
4595 struct survey_info *survey)
4596{
4597 void *hdr;
4598 struct nlattr *infoattr;
4599
Holger Schurig61fa7132009-11-11 12:25:40 +01004600 hdr = nl80211hdr_put(msg, pid, seq, flags,
4601 NL80211_CMD_NEW_SURVEY_RESULTS);
4602 if (!hdr)
4603 return -ENOMEM;
4604
David S. Miller9360ffd2012-03-29 04:41:26 -04004605 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4606 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004607
4608 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4609 if (!infoattr)
4610 goto nla_put_failure;
4611
David S. Miller9360ffd2012-03-29 04:41:26 -04004612 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4613 survey->channel->center_freq))
4614 goto nla_put_failure;
4615
4616 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4617 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4618 goto nla_put_failure;
4619 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4620 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4621 goto nla_put_failure;
4622 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4623 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4624 survey->channel_time))
4625 goto nla_put_failure;
4626 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4627 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4628 survey->channel_time_busy))
4629 goto nla_put_failure;
4630 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4631 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4632 survey->channel_time_ext_busy))
4633 goto nla_put_failure;
4634 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4635 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4636 survey->channel_time_rx))
4637 goto nla_put_failure;
4638 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4639 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4640 survey->channel_time_tx))
4641 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004642
4643 nla_nest_end(msg, infoattr);
4644
4645 return genlmsg_end(msg, hdr);
4646
4647 nla_put_failure:
4648 genlmsg_cancel(msg, hdr);
4649 return -EMSGSIZE;
4650}
4651
4652static int nl80211_dump_survey(struct sk_buff *skb,
4653 struct netlink_callback *cb)
4654{
4655 struct survey_info survey;
4656 struct cfg80211_registered_device *dev;
4657 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004658 int survey_idx = cb->args[1];
4659 int res;
4660
Johannes Berg67748892010-10-04 21:14:06 +02004661 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4662 if (res)
4663 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004664
4665 if (!dev->ops->dump_survey) {
4666 res = -EOPNOTSUPP;
4667 goto out_err;
4668 }
4669
4670 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004671 struct ieee80211_channel *chan;
4672
Holger Schurig61fa7132009-11-11 12:25:40 +01004673 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4674 &survey);
4675 if (res == -ENOENT)
4676 break;
4677 if (res)
4678 goto out_err;
4679
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004680 /* Survey without a channel doesn't make sense */
4681 if (!survey.channel) {
4682 res = -EINVAL;
4683 goto out;
4684 }
4685
4686 chan = ieee80211_get_channel(&dev->wiphy,
4687 survey.channel->center_freq);
4688 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4689 survey_idx++;
4690 continue;
4691 }
4692
Holger Schurig61fa7132009-11-11 12:25:40 +01004693 if (nl80211_send_survey(skb,
4694 NETLINK_CB(cb->skb).pid,
4695 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4696 netdev,
4697 &survey) < 0)
4698 goto out;
4699 survey_idx++;
4700 }
4701
4702 out:
4703 cb->args[1] = survey_idx;
4704 res = skb->len;
4705 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004706 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004707 return res;
4708}
4709
Jouni Malinen255e7372009-03-20 21:21:17 +02004710static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4711{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004712 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004713}
4714
Samuel Ortizb23aa672009-07-01 21:26:54 +02004715static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4716{
4717 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4718 NL80211_WPA_VERSION_2));
4719}
4720
Jouni Malinen636a5d32009-03-19 13:39:22 +02004721static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4722{
Johannes Berg4c476992010-10-04 21:36:35 +02004723 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4724 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004725 struct ieee80211_channel *chan;
4726 const u8 *bssid, *ssid, *ie = NULL;
4727 int err, ssid_len, ie_len = 0;
4728 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004729 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004730 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004731
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004732 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4733 return -EINVAL;
4734
4735 if (!info->attrs[NL80211_ATTR_MAC])
4736 return -EINVAL;
4737
Jouni Malinen17780922009-03-27 20:52:47 +02004738 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4739 return -EINVAL;
4740
Johannes Berg19957bb2009-07-02 17:20:43 +02004741 if (!info->attrs[NL80211_ATTR_SSID])
4742 return -EINVAL;
4743
4744 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4745 return -EINVAL;
4746
Johannes Bergfffd0932009-07-08 14:22:54 +02004747 err = nl80211_parse_key(info, &key);
4748 if (err)
4749 return err;
4750
4751 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004752 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4753 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004754 if (!key.p.key || !key.p.key_len)
4755 return -EINVAL;
4756 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4757 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4758 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4759 key.p.key_len != WLAN_KEY_LEN_WEP104))
4760 return -EINVAL;
4761 if (key.idx > 4)
4762 return -EINVAL;
4763 } else {
4764 key.p.key_len = 0;
4765 key.p.key = NULL;
4766 }
4767
Johannes Bergafea0b72010-08-10 09:46:42 +02004768 if (key.idx >= 0) {
4769 int i;
4770 bool ok = false;
4771 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4772 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4773 ok = true;
4774 break;
4775 }
4776 }
Johannes Berg4c476992010-10-04 21:36:35 +02004777 if (!ok)
4778 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004779 }
4780
Johannes Berg4c476992010-10-04 21:36:35 +02004781 if (!rdev->ops->auth)
4782 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004783
Johannes Berg074ac8d2010-09-16 14:58:22 +02004784 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004785 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4786 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004787
Johannes Berg19957bb2009-07-02 17:20:43 +02004788 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004789 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004790 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004791 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4792 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004793
Johannes Berg19957bb2009-07-02 17:20:43 +02004794 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4795 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4796
4797 if (info->attrs[NL80211_ATTR_IE]) {
4798 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4799 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4800 }
4801
4802 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004803 if (!nl80211_valid_auth_type(auth_type))
4804 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004805
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004806 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4807
Johannes Berg95de8172012-01-20 13:55:25 +01004808 /*
4809 * Since we no longer track auth state, ignore
4810 * requests to only change local state.
4811 */
4812 if (local_state_change)
4813 return 0;
4814
Johannes Berg4c476992010-10-04 21:36:35 +02004815 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4816 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004817 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004818}
4819
Johannes Bergc0692b82010-08-27 14:26:53 +03004820static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4821 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004822 struct cfg80211_crypto_settings *settings,
4823 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004824{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004825 memset(settings, 0, sizeof(*settings));
4826
Samuel Ortizb23aa672009-07-01 21:26:54 +02004827 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4828
Johannes Bergc0692b82010-08-27 14:26:53 +03004829 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4830 u16 proto;
4831 proto = nla_get_u16(
4832 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4833 settings->control_port_ethertype = cpu_to_be16(proto);
4834 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4835 proto != ETH_P_PAE)
4836 return -EINVAL;
4837 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4838 settings->control_port_no_encrypt = true;
4839 } else
4840 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4841
Samuel Ortizb23aa672009-07-01 21:26:54 +02004842 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4843 void *data;
4844 int len, i;
4845
4846 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4847 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4848 settings->n_ciphers_pairwise = len / sizeof(u32);
4849
4850 if (len % sizeof(u32))
4851 return -EINVAL;
4852
Johannes Berg3dc27d22009-07-02 21:36:37 +02004853 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004854 return -EINVAL;
4855
4856 memcpy(settings->ciphers_pairwise, data, len);
4857
4858 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004859 if (!cfg80211_supported_cipher_suite(
4860 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004861 settings->ciphers_pairwise[i]))
4862 return -EINVAL;
4863 }
4864
4865 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4866 settings->cipher_group =
4867 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004868 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4869 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004870 return -EINVAL;
4871 }
4872
4873 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4874 settings->wpa_versions =
4875 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4876 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4877 return -EINVAL;
4878 }
4879
4880 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4881 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004882 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004883
4884 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4885 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4886 settings->n_akm_suites = len / sizeof(u32);
4887
4888 if (len % sizeof(u32))
4889 return -EINVAL;
4890
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004891 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4892 return -EINVAL;
4893
Samuel Ortizb23aa672009-07-01 21:26:54 +02004894 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004895 }
4896
4897 return 0;
4898}
4899
Jouni Malinen636a5d32009-03-19 13:39:22 +02004900static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4901{
Johannes Berg4c476992010-10-04 21:36:35 +02004902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4903 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004904 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004905 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004906 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004907 int err, ssid_len, ie_len = 0;
4908 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004909 u32 flags = 0;
4910 struct ieee80211_ht_cap *ht_capa = NULL;
4911 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004912
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004913 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4914 return -EINVAL;
4915
4916 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004917 !info->attrs[NL80211_ATTR_SSID] ||
4918 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004919 return -EINVAL;
4920
Johannes Berg4c476992010-10-04 21:36:35 +02004921 if (!rdev->ops->assoc)
4922 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004923
Johannes Berg074ac8d2010-09-16 14:58:22 +02004924 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004925 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4926 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004927
Johannes Berg19957bb2009-07-02 17:20:43 +02004928 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004929
Johannes Berg19957bb2009-07-02 17:20:43 +02004930 chan = ieee80211_get_channel(&rdev->wiphy,
4931 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004932 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4933 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004934
Johannes Berg19957bb2009-07-02 17:20:43 +02004935 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4936 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004937
4938 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004939 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4940 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004941 }
4942
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004943 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004944 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004945 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004946 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004947 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004948 else if (mfp != NL80211_MFP_NO)
4949 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004950 }
4951
Johannes Berg3e5d7642009-07-07 14:37:26 +02004952 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4953 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4954
Ben Greear7e7c8922011-11-18 11:31:59 -08004955 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4956 flags |= ASSOC_REQ_DISABLE_HT;
4957
4958 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4959 ht_capa_mask =
4960 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4961
4962 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4963 if (!ht_capa_mask)
4964 return -EINVAL;
4965 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4966 }
4967
Johannes Bergc0692b82010-08-27 14:26:53 +03004968 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004969 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004970 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4971 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004972 &crypto, flags, ht_capa,
4973 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004974
Jouni Malinen636a5d32009-03-19 13:39:22 +02004975 return err;
4976}
4977
4978static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4979{
Johannes Berg4c476992010-10-04 21:36:35 +02004980 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4981 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004982 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004983 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004984 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004985 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004986
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004987 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4988 return -EINVAL;
4989
4990 if (!info->attrs[NL80211_ATTR_MAC])
4991 return -EINVAL;
4992
4993 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4994 return -EINVAL;
4995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 if (!rdev->ops->deauth)
4997 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004998
Johannes Berg074ac8d2010-09-16 14:58:22 +02004999 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005000 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5001 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005002
Johannes Berg19957bb2009-07-02 17:20:43 +02005003 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005004
Johannes Berg19957bb2009-07-02 17:20:43 +02005005 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5006 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005007 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005008 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005009 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005010
5011 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005012 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5013 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005014 }
5015
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005016 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5017
Johannes Berg4c476992010-10-04 21:36:35 +02005018 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5019 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005020}
5021
5022static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5023{
Johannes Berg4c476992010-10-04 21:36:35 +02005024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5025 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005026 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005027 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005028 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005029 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005030
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005031 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5032 return -EINVAL;
5033
5034 if (!info->attrs[NL80211_ATTR_MAC])
5035 return -EINVAL;
5036
5037 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5038 return -EINVAL;
5039
Johannes Berg4c476992010-10-04 21:36:35 +02005040 if (!rdev->ops->disassoc)
5041 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005042
Johannes Berg074ac8d2010-09-16 14:58:22 +02005043 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005044 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5045 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005046
Johannes Berg19957bb2009-07-02 17:20:43 +02005047 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005048
Johannes Berg19957bb2009-07-02 17:20:43 +02005049 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5050 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005051 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005052 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005053 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005054
5055 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005056 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5057 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005058 }
5059
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005060 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5061
Johannes Berg4c476992010-10-04 21:36:35 +02005062 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5063 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005064}
5065
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005066static bool
5067nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5068 int mcast_rate[IEEE80211_NUM_BANDS],
5069 int rateval)
5070{
5071 struct wiphy *wiphy = &rdev->wiphy;
5072 bool found = false;
5073 int band, i;
5074
5075 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5076 struct ieee80211_supported_band *sband;
5077
5078 sband = wiphy->bands[band];
5079 if (!sband)
5080 continue;
5081
5082 for (i = 0; i < sband->n_bitrates; i++) {
5083 if (sband->bitrates[i].bitrate == rateval) {
5084 mcast_rate[band] = i + 1;
5085 found = true;
5086 break;
5087 }
5088 }
5089 }
5090
5091 return found;
5092}
5093
Johannes Berg04a773a2009-04-19 21:24:32 +02005094static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5095{
Johannes Berg4c476992010-10-04 21:36:35 +02005096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5097 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005098 struct cfg80211_ibss_params ibss;
5099 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005100 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005101 int err;
5102
Johannes Berg8e30bc52009-04-22 17:45:38 +02005103 memset(&ibss, 0, sizeof(ibss));
5104
Johannes Berg04a773a2009-04-19 21:24:32 +02005105 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5106 return -EINVAL;
5107
5108 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5109 !info->attrs[NL80211_ATTR_SSID] ||
5110 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5111 return -EINVAL;
5112
Johannes Berg8e30bc52009-04-22 17:45:38 +02005113 ibss.beacon_interval = 100;
5114
5115 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5116 ibss.beacon_interval =
5117 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5118 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5119 return -EINVAL;
5120 }
5121
Johannes Berg4c476992010-10-04 21:36:35 +02005122 if (!rdev->ops->join_ibss)
5123 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005124
Johannes Berg4c476992010-10-04 21:36:35 +02005125 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5126 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005127
Johannes Berg79c97e92009-07-07 03:56:12 +02005128 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005129
Johannes Berg39193492011-09-16 13:45:25 +02005130 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005131 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005132
5133 if (!is_valid_ether_addr(ibss.bssid))
5134 return -EINVAL;
5135 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005136 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5137 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5138
5139 if (info->attrs[NL80211_ATTR_IE]) {
5140 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5141 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5142 }
5143
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005144 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5145 enum nl80211_channel_type channel_type;
5146
Johannes Bergcd6c6592012-05-10 21:27:18 +02005147 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005148 return -EINVAL;
5149
5150 if (channel_type != NL80211_CHAN_NO_HT &&
5151 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5152 return -EINVAL;
5153
5154 ibss.channel_type = channel_type;
5155 } else {
5156 ibss.channel_type = NL80211_CHAN_NO_HT;
5157 }
5158
5159 ibss.channel = rdev_freq_to_chan(rdev,
5160 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5161 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005162 if (!ibss.channel ||
5163 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005164 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5165 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005166
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005167 /* Both channels should be able to initiate communication */
5168 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5169 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5170 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5171 ibss.channel_type))
5172 return -EINVAL;
5173
Johannes Berg04a773a2009-04-19 21:24:32 +02005174 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005175 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005176
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005177 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5178 u8 *rates =
5179 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5180 int n_rates =
5181 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5182 struct ieee80211_supported_band *sband =
5183 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005184
Johannes Berg34850ab2011-07-18 18:08:35 +02005185 err = ieee80211_get_ratemask(sband, rates, n_rates,
5186 &ibss.basic_rates);
5187 if (err)
5188 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005189 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005190
5191 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5192 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5193 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5194 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005195
Johannes Berg4c476992010-10-04 21:36:35 +02005196 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5197 connkeys = nl80211_parse_connkeys(rdev,
5198 info->attrs[NL80211_ATTR_KEYS]);
5199 if (IS_ERR(connkeys))
5200 return PTR_ERR(connkeys);
5201 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005202
Antonio Quartulli267335d2012-01-31 20:25:47 +01005203 ibss.control_port =
5204 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5205
Johannes Berg4c476992010-10-04 21:36:35 +02005206 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005207 if (err)
5208 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005209 return err;
5210}
5211
5212static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5213{
Johannes Berg4c476992010-10-04 21:36:35 +02005214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5215 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005216
Johannes Berg4c476992010-10-04 21:36:35 +02005217 if (!rdev->ops->leave_ibss)
5218 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005219
Johannes Berg4c476992010-10-04 21:36:35 +02005220 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5221 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005222
Johannes Berg4c476992010-10-04 21:36:35 +02005223 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005224}
5225
Johannes Bergaff89a92009-07-01 21:26:51 +02005226#ifdef CONFIG_NL80211_TESTMODE
5227static struct genl_multicast_group nl80211_testmode_mcgrp = {
5228 .name = "testmode",
5229};
5230
5231static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5232{
Johannes Berg4c476992010-10-04 21:36:35 +02005233 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005234 int err;
5235
5236 if (!info->attrs[NL80211_ATTR_TESTDATA])
5237 return -EINVAL;
5238
Johannes Bergaff89a92009-07-01 21:26:51 +02005239 err = -EOPNOTSUPP;
5240 if (rdev->ops->testmode_cmd) {
5241 rdev->testmode_info = info;
5242 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5243 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5244 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5245 rdev->testmode_info = NULL;
5246 }
5247
Johannes Bergaff89a92009-07-01 21:26:51 +02005248 return err;
5249}
5250
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005251static int nl80211_testmode_dump(struct sk_buff *skb,
5252 struct netlink_callback *cb)
5253{
Johannes Berg00918d32011-12-13 17:22:05 +01005254 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005255 int err;
5256 long phy_idx;
5257 void *data = NULL;
5258 int data_len = 0;
5259
5260 if (cb->args[0]) {
5261 /*
5262 * 0 is a valid index, but not valid for args[0],
5263 * so we need to offset by 1.
5264 */
5265 phy_idx = cb->args[0] - 1;
5266 } else {
5267 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5268 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5269 nl80211_policy);
5270 if (err)
5271 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005272
Johannes Berg2bd7e352012-06-15 14:23:16 +02005273 mutex_lock(&cfg80211_mutex);
5274 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5275 nl80211_fam.attrbuf);
5276 if (IS_ERR(rdev)) {
5277 mutex_unlock(&cfg80211_mutex);
5278 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005279 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005280 phy_idx = rdev->wiphy_idx;
5281 rdev = NULL;
5282 mutex_unlock(&cfg80211_mutex);
5283
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005284 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5285 cb->args[1] =
5286 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5287 }
5288
5289 if (cb->args[1]) {
5290 data = nla_data((void *)cb->args[1]);
5291 data_len = nla_len((void *)cb->args[1]);
5292 }
5293
5294 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005295 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5296 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005297 mutex_unlock(&cfg80211_mutex);
5298 return -ENOENT;
5299 }
Johannes Berg00918d32011-12-13 17:22:05 +01005300 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005301 mutex_unlock(&cfg80211_mutex);
5302
Johannes Berg00918d32011-12-13 17:22:05 +01005303 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005304 err = -EOPNOTSUPP;
5305 goto out_err;
5306 }
5307
5308 while (1) {
5309 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5310 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5311 NL80211_CMD_TESTMODE);
5312 struct nlattr *tmdata;
5313
David S. Miller9360ffd2012-03-29 04:41:26 -04005314 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005315 genlmsg_cancel(skb, hdr);
5316 break;
5317 }
5318
5319 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5320 if (!tmdata) {
5321 genlmsg_cancel(skb, hdr);
5322 break;
5323 }
Johannes Berg00918d32011-12-13 17:22:05 +01005324 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5325 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005326 nla_nest_end(skb, tmdata);
5327
5328 if (err == -ENOBUFS || err == -ENOENT) {
5329 genlmsg_cancel(skb, hdr);
5330 break;
5331 } else if (err) {
5332 genlmsg_cancel(skb, hdr);
5333 goto out_err;
5334 }
5335
5336 genlmsg_end(skb, hdr);
5337 }
5338
5339 err = skb->len;
5340 /* see above */
5341 cb->args[0] = phy_idx + 1;
5342 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005343 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005344 return err;
5345}
5346
Johannes Bergaff89a92009-07-01 21:26:51 +02005347static struct sk_buff *
5348__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5349 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5350{
5351 struct sk_buff *skb;
5352 void *hdr;
5353 struct nlattr *data;
5354
5355 skb = nlmsg_new(approxlen + 100, gfp);
5356 if (!skb)
5357 return NULL;
5358
5359 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5360 if (!hdr) {
5361 kfree_skb(skb);
5362 return NULL;
5363 }
5364
David S. Miller9360ffd2012-03-29 04:41:26 -04005365 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5366 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005367 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5368
5369 ((void **)skb->cb)[0] = rdev;
5370 ((void **)skb->cb)[1] = hdr;
5371 ((void **)skb->cb)[2] = data;
5372
5373 return skb;
5374
5375 nla_put_failure:
5376 kfree_skb(skb);
5377 return NULL;
5378}
5379
5380struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5381 int approxlen)
5382{
5383 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5384
5385 if (WARN_ON(!rdev->testmode_info))
5386 return NULL;
5387
5388 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5389 rdev->testmode_info->snd_pid,
5390 rdev->testmode_info->snd_seq,
5391 GFP_KERNEL);
5392}
5393EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5394
5395int cfg80211_testmode_reply(struct sk_buff *skb)
5396{
5397 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5398 void *hdr = ((void **)skb->cb)[1];
5399 struct nlattr *data = ((void **)skb->cb)[2];
5400
5401 if (WARN_ON(!rdev->testmode_info)) {
5402 kfree_skb(skb);
5403 return -EINVAL;
5404 }
5405
5406 nla_nest_end(skb, data);
5407 genlmsg_end(skb, hdr);
5408 return genlmsg_reply(skb, rdev->testmode_info);
5409}
5410EXPORT_SYMBOL(cfg80211_testmode_reply);
5411
5412struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5413 int approxlen, gfp_t gfp)
5414{
5415 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5416
5417 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5418}
5419EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5420
5421void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5422{
5423 void *hdr = ((void **)skb->cb)[1];
5424 struct nlattr *data = ((void **)skb->cb)[2];
5425
5426 nla_nest_end(skb, data);
5427 genlmsg_end(skb, hdr);
5428 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5429}
5430EXPORT_SYMBOL(cfg80211_testmode_event);
5431#endif
5432
Samuel Ortizb23aa672009-07-01 21:26:54 +02005433static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5434{
Johannes Berg4c476992010-10-04 21:36:35 +02005435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5436 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005437 struct cfg80211_connect_params connect;
5438 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005439 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005440 int err;
5441
5442 memset(&connect, 0, sizeof(connect));
5443
5444 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5445 return -EINVAL;
5446
5447 if (!info->attrs[NL80211_ATTR_SSID] ||
5448 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5449 return -EINVAL;
5450
5451 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5452 connect.auth_type =
5453 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5454 if (!nl80211_valid_auth_type(connect.auth_type))
5455 return -EINVAL;
5456 } else
5457 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5458
5459 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5460
Johannes Bergc0692b82010-08-27 14:26:53 +03005461 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005462 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005463 if (err)
5464 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005465
Johannes Berg074ac8d2010-09-16 14:58:22 +02005466 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005467 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5468 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005469
Johannes Berg79c97e92009-07-07 03:56:12 +02005470 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005471
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305472 connect.bg_scan_period = -1;
5473 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5474 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5475 connect.bg_scan_period =
5476 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5477 }
5478
Samuel Ortizb23aa672009-07-01 21:26:54 +02005479 if (info->attrs[NL80211_ATTR_MAC])
5480 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5481 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5482 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5483
5484 if (info->attrs[NL80211_ATTR_IE]) {
5485 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5486 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5487 }
5488
5489 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5490 connect.channel =
5491 ieee80211_get_channel(wiphy,
5492 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5493 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005494 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5495 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005496 }
5497
Johannes Bergfffd0932009-07-08 14:22:54 +02005498 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5499 connkeys = nl80211_parse_connkeys(rdev,
5500 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005501 if (IS_ERR(connkeys))
5502 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005503 }
5504
Ben Greear7e7c8922011-11-18 11:31:59 -08005505 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5506 connect.flags |= ASSOC_REQ_DISABLE_HT;
5507
5508 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5509 memcpy(&connect.ht_capa_mask,
5510 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5511 sizeof(connect.ht_capa_mask));
5512
5513 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5514 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5515 return -EINVAL;
5516 memcpy(&connect.ht_capa,
5517 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5518 sizeof(connect.ht_capa));
5519 }
5520
Johannes Bergfffd0932009-07-08 14:22:54 +02005521 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005522 if (err)
5523 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005524 return err;
5525}
5526
5527static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5528{
Johannes Berg4c476992010-10-04 21:36:35 +02005529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5530 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005531 u16 reason;
5532
5533 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5534 reason = WLAN_REASON_DEAUTH_LEAVING;
5535 else
5536 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5537
5538 if (reason == 0)
5539 return -EINVAL;
5540
Johannes Berg074ac8d2010-09-16 14:58:22 +02005541 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005542 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5543 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005544
Johannes Berg4c476992010-10-04 21:36:35 +02005545 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005546}
5547
Johannes Berg463d0182009-07-14 00:33:35 +02005548static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5549{
Johannes Berg4c476992010-10-04 21:36:35 +02005550 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005551 struct net *net;
5552 int err;
5553 u32 pid;
5554
5555 if (!info->attrs[NL80211_ATTR_PID])
5556 return -EINVAL;
5557
5558 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5559
Johannes Berg463d0182009-07-14 00:33:35 +02005560 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005561 if (IS_ERR(net))
5562 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005563
5564 err = 0;
5565
5566 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005567 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5568 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005569
Johannes Berg463d0182009-07-14 00:33:35 +02005570 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005571 return err;
5572}
5573
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005574static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5575{
Johannes Berg4c476992010-10-04 21:36:35 +02005576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005577 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5578 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005579 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005580 struct cfg80211_pmksa pmksa;
5581
5582 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5583
5584 if (!info->attrs[NL80211_ATTR_MAC])
5585 return -EINVAL;
5586
5587 if (!info->attrs[NL80211_ATTR_PMKID])
5588 return -EINVAL;
5589
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005590 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5591 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5592
Johannes Berg074ac8d2010-09-16 14:58:22 +02005593 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005594 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5595 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005596
5597 switch (info->genlhdr->cmd) {
5598 case NL80211_CMD_SET_PMKSA:
5599 rdev_ops = rdev->ops->set_pmksa;
5600 break;
5601 case NL80211_CMD_DEL_PMKSA:
5602 rdev_ops = rdev->ops->del_pmksa;
5603 break;
5604 default:
5605 WARN_ON(1);
5606 break;
5607 }
5608
Johannes Berg4c476992010-10-04 21:36:35 +02005609 if (!rdev_ops)
5610 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005611
Johannes Berg4c476992010-10-04 21:36:35 +02005612 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005613}
5614
5615static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5616{
Johannes Berg4c476992010-10-04 21:36:35 +02005617 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5618 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005619
Johannes Berg074ac8d2010-09-16 14:58:22 +02005620 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005621 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5622 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005623
Johannes Berg4c476992010-10-04 21:36:35 +02005624 if (!rdev->ops->flush_pmksa)
5625 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005626
Johannes Berg4c476992010-10-04 21:36:35 +02005627 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005628}
5629
Arik Nemtsov109086c2011-09-28 14:12:50 +03005630static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5631{
5632 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5633 struct net_device *dev = info->user_ptr[1];
5634 u8 action_code, dialog_token;
5635 u16 status_code;
5636 u8 *peer;
5637
5638 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5639 !rdev->ops->tdls_mgmt)
5640 return -EOPNOTSUPP;
5641
5642 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5643 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5644 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5645 !info->attrs[NL80211_ATTR_IE] ||
5646 !info->attrs[NL80211_ATTR_MAC])
5647 return -EINVAL;
5648
5649 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5650 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5651 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5652 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5653
5654 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5655 dialog_token, status_code,
5656 nla_data(info->attrs[NL80211_ATTR_IE]),
5657 nla_len(info->attrs[NL80211_ATTR_IE]));
5658}
5659
5660static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5661{
5662 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5663 struct net_device *dev = info->user_ptr[1];
5664 enum nl80211_tdls_operation operation;
5665 u8 *peer;
5666
5667 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5668 !rdev->ops->tdls_oper)
5669 return -EOPNOTSUPP;
5670
5671 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5672 !info->attrs[NL80211_ATTR_MAC])
5673 return -EINVAL;
5674
5675 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5676 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5677
5678 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5679}
5680
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005681static int nl80211_remain_on_channel(struct sk_buff *skb,
5682 struct genl_info *info)
5683{
Johannes Berg4c476992010-10-04 21:36:35 +02005684 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5685 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005686 struct ieee80211_channel *chan;
5687 struct sk_buff *msg;
5688 void *hdr;
5689 u64 cookie;
5690 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5691 u32 freq, duration;
5692 int err;
5693
5694 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5695 !info->attrs[NL80211_ATTR_DURATION])
5696 return -EINVAL;
5697
5698 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5699
Johannes Berg7c4ef712011-11-18 15:33:48 +01005700 if (!rdev->ops->remain_on_channel ||
5701 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005702 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005703
Johannes Bergebf348f2012-06-01 12:50:54 +02005704 /*
5705 * We should be on that channel for at least a minimum amount of
5706 * time (10ms) but no longer than the driver supports.
5707 */
5708 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5709 duration > rdev->wiphy.max_remain_on_channel_duration)
5710 return -EINVAL;
5711
Johannes Bergcd6c6592012-05-10 21:27:18 +02005712 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5713 !nl80211_valid_channel_type(info, &channel_type))
5714 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005715
5716 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5717 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005718 if (chan == NULL)
5719 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005720
5721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005722 if (!msg)
5723 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005724
5725 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5726 NL80211_CMD_REMAIN_ON_CHANNEL);
5727
5728 if (IS_ERR(hdr)) {
5729 err = PTR_ERR(hdr);
5730 goto free_msg;
5731 }
5732
5733 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5734 channel_type, duration, &cookie);
5735
5736 if (err)
5737 goto free_msg;
5738
David S. Miller9360ffd2012-03-29 04:41:26 -04005739 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5740 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005741
5742 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005743
5744 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005745
5746 nla_put_failure:
5747 err = -ENOBUFS;
5748 free_msg:
5749 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005750 return err;
5751}
5752
5753static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5754 struct genl_info *info)
5755{
Johannes Berg4c476992010-10-04 21:36:35 +02005756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5757 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005758 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005759
5760 if (!info->attrs[NL80211_ATTR_COOKIE])
5761 return -EINVAL;
5762
Johannes Berg4c476992010-10-04 21:36:35 +02005763 if (!rdev->ops->cancel_remain_on_channel)
5764 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005765
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005766 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5767
Johannes Berg4c476992010-10-04 21:36:35 +02005768 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005769}
5770
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005771static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5772 u8 *rates, u8 rates_len)
5773{
5774 u8 i;
5775 u32 mask = 0;
5776
5777 for (i = 0; i < rates_len; i++) {
5778 int rate = (rates[i] & 0x7f) * 5;
5779 int ridx;
5780 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5781 struct ieee80211_rate *srate =
5782 &sband->bitrates[ridx];
5783 if (rate == srate->bitrate) {
5784 mask |= 1 << ridx;
5785 break;
5786 }
5787 }
5788 if (ridx == sband->n_bitrates)
5789 return 0; /* rate not found */
5790 }
5791
5792 return mask;
5793}
5794
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005795static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5796 u8 *rates, u8 rates_len,
5797 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5798{
5799 u8 i;
5800
5801 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5802
5803 for (i = 0; i < rates_len; i++) {
5804 int ridx, rbit;
5805
5806 ridx = rates[i] / 8;
5807 rbit = BIT(rates[i] % 8);
5808
5809 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005810 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005811 return false;
5812
5813 /* check availability */
5814 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5815 mcs[ridx] |= rbit;
5816 else
5817 return false;
5818 }
5819
5820 return true;
5821}
5822
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005823static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005824 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5825 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005826 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5827 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005828};
5829
5830static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5831 struct genl_info *info)
5832{
5833 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005835 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005836 int rem, i;
5837 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005838 struct nlattr *tx_rates;
5839 struct ieee80211_supported_band *sband;
5840
5841 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5842 return -EINVAL;
5843
Johannes Berg4c476992010-10-04 21:36:35 +02005844 if (!rdev->ops->set_bitrate_mask)
5845 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005846
5847 memset(&mask, 0, sizeof(mask));
5848 /* Default to all rates enabled */
5849 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5850 sband = rdev->wiphy.bands[i];
5851 mask.control[i].legacy =
5852 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005853 if (sband)
5854 memcpy(mask.control[i].mcs,
5855 sband->ht_cap.mcs.rx_mask,
5856 sizeof(mask.control[i].mcs));
5857 else
5858 memset(mask.control[i].mcs, 0,
5859 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005860 }
5861
5862 /*
5863 * The nested attribute uses enum nl80211_band as the index. This maps
5864 * directly to the enum ieee80211_band values used in cfg80211.
5865 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005866 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005867 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5868 {
5869 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005870 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5871 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005872 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005873 if (sband == NULL)
5874 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005875 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5876 nla_len(tx_rates), nl80211_txattr_policy);
5877 if (tb[NL80211_TXRATE_LEGACY]) {
5878 mask.control[band].legacy = rateset_to_mask(
5879 sband,
5880 nla_data(tb[NL80211_TXRATE_LEGACY]),
5881 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305882 if ((mask.control[band].legacy == 0) &&
5883 nla_len(tb[NL80211_TXRATE_LEGACY]))
5884 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005885 }
5886 if (tb[NL80211_TXRATE_MCS]) {
5887 if (!ht_rateset_to_mask(
5888 sband,
5889 nla_data(tb[NL80211_TXRATE_MCS]),
5890 nla_len(tb[NL80211_TXRATE_MCS]),
5891 mask.control[band].mcs))
5892 return -EINVAL;
5893 }
5894
5895 if (mask.control[band].legacy == 0) {
5896 /* don't allow empty legacy rates if HT
5897 * is not even supported. */
5898 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5899 return -EINVAL;
5900
5901 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5902 if (mask.control[band].mcs[i])
5903 break;
5904
5905 /* legacy and mcs rates may not be both empty */
5906 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005907 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005908 }
5909 }
5910
Johannes Berg4c476992010-10-04 21:36:35 +02005911 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005912}
5913
Johannes Berg2e161f72010-08-12 15:38:38 +02005914static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005915{
Johannes Berg4c476992010-10-04 21:36:35 +02005916 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5917 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005918 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005919
5920 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5921 return -EINVAL;
5922
Johannes Berg2e161f72010-08-12 15:38:38 +02005923 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5924 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005925
Johannes Berg9d38d852010-06-09 17:20:33 +02005926 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005927 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005928 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5929 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5930 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005931 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005932 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5933 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005934
5935 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005936 if (!rdev->ops->mgmt_tx)
5937 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005938
Johannes Berg4c476992010-10-04 21:36:35 +02005939 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005940 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005941 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5942 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005943}
5944
Johannes Berg2e161f72010-08-12 15:38:38 +02005945static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005946{
Johannes Berg4c476992010-10-04 21:36:35 +02005947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5948 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005949 struct ieee80211_channel *chan;
5950 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005951 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005952 u32 freq;
5953 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005954 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005955 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005956 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005957 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005958 bool offchan, no_cck, dont_wait_for_ack;
5959
5960 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005961
5962 if (!info->attrs[NL80211_ATTR_FRAME] ||
5963 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5964 return -EINVAL;
5965
Johannes Berg4c476992010-10-04 21:36:35 +02005966 if (!rdev->ops->mgmt_tx)
5967 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005968
Johannes Berg9d38d852010-06-09 17:20:33 +02005969 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005970 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005971 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5973 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005975 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5976 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005977
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005978 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005979 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005980 return -EINVAL;
5981 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02005982
5983 /*
5984 * We should wait on the channel for at least a minimum amount
5985 * of time (10ms) but no longer than the driver supports.
5986 */
5987 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5988 wait > rdev->wiphy.max_remain_on_channel_duration)
5989 return -EINVAL;
5990
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005991 }
5992
Jouni Malinen026331c2010-02-15 12:53:10 +02005993 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02005994 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02005995 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005996 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005997 }
5998
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005999 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6000
Johannes Berg7c4ef712011-11-18 15:33:48 +01006001 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6002 return -EINVAL;
6003
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306004 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6005
Jouni Malinen026331c2010-02-15 12:53:10 +02006006 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6007 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006008 if (chan == NULL)
6009 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006010
Johannes Berge247bd902011-11-04 11:18:21 +01006011 if (!dont_wait_for_ack) {
6012 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6013 if (!msg)
6014 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006015
Johannes Berge247bd902011-11-04 11:18:21 +01006016 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6017 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006018
Johannes Berge247bd902011-11-04 11:18:21 +01006019 if (IS_ERR(hdr)) {
6020 err = PTR_ERR(hdr);
6021 goto free_msg;
6022 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006023 }
Johannes Berge247bd902011-11-04 11:18:21 +01006024
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006025 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
6026 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006027 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6028 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006029 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006030 if (err)
6031 goto free_msg;
6032
Johannes Berge247bd902011-11-04 11:18:21 +01006033 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006034 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6035 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006036
Johannes Berge247bd902011-11-04 11:18:21 +01006037 genlmsg_end(msg, hdr);
6038 return genlmsg_reply(msg, info);
6039 }
6040
6041 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006042
6043 nla_put_failure:
6044 err = -ENOBUFS;
6045 free_msg:
6046 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006047 return err;
6048}
6049
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006050static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6051{
6052 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6053 struct net_device *dev = info->user_ptr[1];
6054 u64 cookie;
6055
6056 if (!info->attrs[NL80211_ATTR_COOKIE])
6057 return -EINVAL;
6058
6059 if (!rdev->ops->mgmt_tx_cancel_wait)
6060 return -EOPNOTSUPP;
6061
6062 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6063 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6064 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
6065 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6066 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6067 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6068 return -EOPNOTSUPP;
6069
6070 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6071
6072 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
6073}
6074
Kalle Valoffb9eb32010-02-17 17:58:10 +02006075static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6076{
Johannes Berg4c476992010-10-04 21:36:35 +02006077 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006078 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006079 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006080 u8 ps_state;
6081 bool state;
6082 int err;
6083
Johannes Berg4c476992010-10-04 21:36:35 +02006084 if (!info->attrs[NL80211_ATTR_PS_STATE])
6085 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006086
6087 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6088
Johannes Berg4c476992010-10-04 21:36:35 +02006089 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6090 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006091
6092 wdev = dev->ieee80211_ptr;
6093
Johannes Berg4c476992010-10-04 21:36:35 +02006094 if (!rdev->ops->set_power_mgmt)
6095 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006096
6097 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6098
6099 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006100 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006101
Johannes Berg4c476992010-10-04 21:36:35 +02006102 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6103 wdev->ps_timeout);
6104 if (!err)
6105 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006106 return err;
6107}
6108
6109static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6110{
Johannes Berg4c476992010-10-04 21:36:35 +02006111 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006112 enum nl80211_ps_state ps_state;
6113 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006114 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006115 struct sk_buff *msg;
6116 void *hdr;
6117 int err;
6118
Kalle Valoffb9eb32010-02-17 17:58:10 +02006119 wdev = dev->ieee80211_ptr;
6120
Johannes Berg4c476992010-10-04 21:36:35 +02006121 if (!rdev->ops->set_power_mgmt)
6122 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006123
6124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006125 if (!msg)
6126 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006127
6128 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6129 NL80211_CMD_GET_POWER_SAVE);
6130 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006131 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006132 goto free_msg;
6133 }
6134
6135 if (wdev->ps)
6136 ps_state = NL80211_PS_ENABLED;
6137 else
6138 ps_state = NL80211_PS_DISABLED;
6139
David S. Miller9360ffd2012-03-29 04:41:26 -04006140 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6141 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006142
6143 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006144 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006145
Johannes Berg4c476992010-10-04 21:36:35 +02006146 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006147 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006148 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006149 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006150 return err;
6151}
6152
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006153static struct nla_policy
6154nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6155 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6156 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6157 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
6158};
6159
6160static int nl80211_set_cqm_rssi(struct genl_info *info,
6161 s32 threshold, u32 hysteresis)
6162{
Johannes Berg4c476992010-10-04 21:36:35 +02006163 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006164 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006165 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006166
6167 if (threshold > 0)
6168 return -EINVAL;
6169
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006170 wdev = dev->ieee80211_ptr;
6171
Johannes Berg4c476992010-10-04 21:36:35 +02006172 if (!rdev->ops->set_cqm_rssi_config)
6173 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006174
Johannes Berg074ac8d2010-09-16 14:58:22 +02006175 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006176 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6177 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006178
Johannes Berg4c476992010-10-04 21:36:35 +02006179 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6180 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006181}
6182
6183static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6184{
6185 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6186 struct nlattr *cqm;
6187 int err;
6188
6189 cqm = info->attrs[NL80211_ATTR_CQM];
6190 if (!cqm) {
6191 err = -EINVAL;
6192 goto out;
6193 }
6194
6195 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6196 nl80211_attr_cqm_policy);
6197 if (err)
6198 goto out;
6199
6200 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6201 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6202 s32 threshold;
6203 u32 hysteresis;
6204 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6205 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6206 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6207 } else
6208 err = -EINVAL;
6209
6210out:
6211 return err;
6212}
6213
Johannes Berg29cbe682010-12-03 09:20:44 +01006214static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6215{
6216 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6217 struct net_device *dev = info->user_ptr[1];
6218 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006219 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006220 int err;
6221
6222 /* start with default */
6223 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006224 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006225
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006226 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006227 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006228 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006229 if (err)
6230 return err;
6231 }
6232
6233 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6234 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6235 return -EINVAL;
6236
Javier Cardonac80d5452010-12-16 17:37:49 -08006237 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6238 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6239
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006240 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6241 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6242 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6243 return -EINVAL;
6244
Javier Cardonac80d5452010-12-16 17:37:49 -08006245 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6246 /* parse additional setup parameters if given */
6247 err = nl80211_parse_mesh_setup(info, &setup);
6248 if (err)
6249 return err;
6250 }
6251
Johannes Bergcc1d2802012-05-16 23:50:20 +02006252 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6253 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6254
6255 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6256 !nl80211_valid_channel_type(info, &channel_type))
6257 return -EINVAL;
6258
6259 setup.channel = rdev_freq_to_chan(rdev,
6260 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6261 channel_type);
6262 if (!setup.channel)
6263 return -EINVAL;
6264 setup.channel_type = channel_type;
6265 } else {
6266 /* cfg80211_join_mesh() will sort it out */
6267 setup.channel = NULL;
6268 }
6269
Javier Cardonac80d5452010-12-16 17:37:49 -08006270 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006271}
6272
6273static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6274{
6275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6276 struct net_device *dev = info->user_ptr[1];
6277
6278 return cfg80211_leave_mesh(rdev, dev);
6279}
6280
Johannes Bergdfb89c52012-06-27 09:23:48 +02006281#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006282static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6283{
6284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6285 struct sk_buff *msg;
6286 void *hdr;
6287
6288 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6289 return -EOPNOTSUPP;
6290
6291 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6292 if (!msg)
6293 return -ENOMEM;
6294
6295 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6296 NL80211_CMD_GET_WOWLAN);
6297 if (!hdr)
6298 goto nla_put_failure;
6299
6300 if (rdev->wowlan) {
6301 struct nlattr *nl_wowlan;
6302
6303 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6304 if (!nl_wowlan)
6305 goto nla_put_failure;
6306
David S. Miller9360ffd2012-03-29 04:41:26 -04006307 if ((rdev->wowlan->any &&
6308 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6309 (rdev->wowlan->disconnect &&
6310 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6311 (rdev->wowlan->magic_pkt &&
6312 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6313 (rdev->wowlan->gtk_rekey_failure &&
6314 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6315 (rdev->wowlan->eap_identity_req &&
6316 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6317 (rdev->wowlan->four_way_handshake &&
6318 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6319 (rdev->wowlan->rfkill_release &&
6320 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6321 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006322 if (rdev->wowlan->n_patterns) {
6323 struct nlattr *nl_pats, *nl_pat;
6324 int i, pat_len;
6325
6326 nl_pats = nla_nest_start(msg,
6327 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6328 if (!nl_pats)
6329 goto nla_put_failure;
6330
6331 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6332 nl_pat = nla_nest_start(msg, i + 1);
6333 if (!nl_pat)
6334 goto nla_put_failure;
6335 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006336 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6337 DIV_ROUND_UP(pat_len, 8),
6338 rdev->wowlan->patterns[i].mask) ||
6339 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6340 pat_len,
6341 rdev->wowlan->patterns[i].pattern))
6342 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006343 nla_nest_end(msg, nl_pat);
6344 }
6345 nla_nest_end(msg, nl_pats);
6346 }
6347
6348 nla_nest_end(msg, nl_wowlan);
6349 }
6350
6351 genlmsg_end(msg, hdr);
6352 return genlmsg_reply(msg, info);
6353
6354nla_put_failure:
6355 nlmsg_free(msg);
6356 return -ENOBUFS;
6357}
6358
6359static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6360{
6361 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6362 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6363 struct cfg80211_wowlan no_triggers = {};
6364 struct cfg80211_wowlan new_triggers = {};
6365 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6366 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006367 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006368
6369 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6370 return -EOPNOTSUPP;
6371
6372 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6373 goto no_triggers;
6374
6375 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6376 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6377 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6378 nl80211_wowlan_policy);
6379 if (err)
6380 return err;
6381
6382 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6383 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6384 return -EINVAL;
6385 new_triggers.any = true;
6386 }
6387
6388 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6389 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6390 return -EINVAL;
6391 new_triggers.disconnect = true;
6392 }
6393
6394 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6395 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6396 return -EINVAL;
6397 new_triggers.magic_pkt = true;
6398 }
6399
Johannes Berg77dbbb12011-07-13 10:48:55 +02006400 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6401 return -EINVAL;
6402
6403 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6404 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6405 return -EINVAL;
6406 new_triggers.gtk_rekey_failure = true;
6407 }
6408
6409 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6410 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6411 return -EINVAL;
6412 new_triggers.eap_identity_req = true;
6413 }
6414
6415 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6416 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6417 return -EINVAL;
6418 new_triggers.four_way_handshake = true;
6419 }
6420
6421 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6422 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6423 return -EINVAL;
6424 new_triggers.rfkill_release = true;
6425 }
6426
Johannes Bergff1b6e62011-05-04 15:37:28 +02006427 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6428 struct nlattr *pat;
6429 int n_patterns = 0;
6430 int rem, pat_len, mask_len;
6431 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6432
6433 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6434 rem)
6435 n_patterns++;
6436 if (n_patterns > wowlan->n_patterns)
6437 return -EINVAL;
6438
6439 new_triggers.patterns = kcalloc(n_patterns,
6440 sizeof(new_triggers.patterns[0]),
6441 GFP_KERNEL);
6442 if (!new_triggers.patterns)
6443 return -ENOMEM;
6444
6445 new_triggers.n_patterns = n_patterns;
6446 i = 0;
6447
6448 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6449 rem) {
6450 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6451 nla_data(pat), nla_len(pat), NULL);
6452 err = -EINVAL;
6453 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6454 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6455 goto error;
6456 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6457 mask_len = DIV_ROUND_UP(pat_len, 8);
6458 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6459 mask_len)
6460 goto error;
6461 if (pat_len > wowlan->pattern_max_len ||
6462 pat_len < wowlan->pattern_min_len)
6463 goto error;
6464
6465 new_triggers.patterns[i].mask =
6466 kmalloc(mask_len + pat_len, GFP_KERNEL);
6467 if (!new_triggers.patterns[i].mask) {
6468 err = -ENOMEM;
6469 goto error;
6470 }
6471 new_triggers.patterns[i].pattern =
6472 new_triggers.patterns[i].mask + mask_len;
6473 memcpy(new_triggers.patterns[i].mask,
6474 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6475 mask_len);
6476 new_triggers.patterns[i].pattern_len = pat_len;
6477 memcpy(new_triggers.patterns[i].pattern,
6478 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6479 pat_len);
6480 i++;
6481 }
6482 }
6483
6484 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6485 struct cfg80211_wowlan *ntrig;
6486 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6487 GFP_KERNEL);
6488 if (!ntrig) {
6489 err = -ENOMEM;
6490 goto error;
6491 }
6492 cfg80211_rdev_free_wowlan(rdev);
6493 rdev->wowlan = ntrig;
6494 } else {
6495 no_triggers:
6496 cfg80211_rdev_free_wowlan(rdev);
6497 rdev->wowlan = NULL;
6498 }
6499
Johannes Berg6d525632012-04-04 15:05:25 +02006500 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6501 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6502
Johannes Bergff1b6e62011-05-04 15:37:28 +02006503 return 0;
6504 error:
6505 for (i = 0; i < new_triggers.n_patterns; i++)
6506 kfree(new_triggers.patterns[i].mask);
6507 kfree(new_triggers.patterns);
6508 return err;
6509}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006510#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006511
Johannes Berge5497d72011-07-05 16:35:40 +02006512static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6513{
6514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6515 struct net_device *dev = info->user_ptr[1];
6516 struct wireless_dev *wdev = dev->ieee80211_ptr;
6517 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6518 struct cfg80211_gtk_rekey_data rekey_data;
6519 int err;
6520
6521 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6522 return -EINVAL;
6523
6524 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6525 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6526 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6527 nl80211_rekey_policy);
6528 if (err)
6529 return err;
6530
6531 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6532 return -ERANGE;
6533 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6534 return -ERANGE;
6535 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6536 return -ERANGE;
6537
6538 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6539 NL80211_KEK_LEN);
6540 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6541 NL80211_KCK_LEN);
6542 memcpy(rekey_data.replay_ctr,
6543 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6544 NL80211_REPLAY_CTR_LEN);
6545
6546 wdev_lock(wdev);
6547 if (!wdev->current_bss) {
6548 err = -ENOTCONN;
6549 goto out;
6550 }
6551
6552 if (!rdev->ops->set_rekey_data) {
6553 err = -EOPNOTSUPP;
6554 goto out;
6555 }
6556
6557 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6558 out:
6559 wdev_unlock(wdev);
6560 return err;
6561}
6562
Johannes Berg28946da2011-11-04 11:18:12 +01006563static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6564 struct genl_info *info)
6565{
6566 struct net_device *dev = info->user_ptr[1];
6567 struct wireless_dev *wdev = dev->ieee80211_ptr;
6568
6569 if (wdev->iftype != NL80211_IFTYPE_AP &&
6570 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6571 return -EINVAL;
6572
6573 if (wdev->ap_unexpected_nlpid)
6574 return -EBUSY;
6575
6576 wdev->ap_unexpected_nlpid = info->snd_pid;
6577 return 0;
6578}
6579
Johannes Berg7f6cf312011-11-04 11:18:15 +01006580static int nl80211_probe_client(struct sk_buff *skb,
6581 struct genl_info *info)
6582{
6583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6584 struct net_device *dev = info->user_ptr[1];
6585 struct wireless_dev *wdev = dev->ieee80211_ptr;
6586 struct sk_buff *msg;
6587 void *hdr;
6588 const u8 *addr;
6589 u64 cookie;
6590 int err;
6591
6592 if (wdev->iftype != NL80211_IFTYPE_AP &&
6593 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6594 return -EOPNOTSUPP;
6595
6596 if (!info->attrs[NL80211_ATTR_MAC])
6597 return -EINVAL;
6598
6599 if (!rdev->ops->probe_client)
6600 return -EOPNOTSUPP;
6601
6602 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6603 if (!msg)
6604 return -ENOMEM;
6605
6606 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6607 NL80211_CMD_PROBE_CLIENT);
6608
6609 if (IS_ERR(hdr)) {
6610 err = PTR_ERR(hdr);
6611 goto free_msg;
6612 }
6613
6614 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6615
6616 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6617 if (err)
6618 goto free_msg;
6619
David S. Miller9360ffd2012-03-29 04:41:26 -04006620 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6621 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006622
6623 genlmsg_end(msg, hdr);
6624
6625 return genlmsg_reply(msg, info);
6626
6627 nla_put_failure:
6628 err = -ENOBUFS;
6629 free_msg:
6630 nlmsg_free(msg);
6631 return err;
6632}
6633
Johannes Berg5e760232011-11-04 11:18:17 +01006634static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6635{
6636 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6637
6638 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6639 return -EOPNOTSUPP;
6640
6641 if (rdev->ap_beacons_nlpid)
6642 return -EBUSY;
6643
6644 rdev->ap_beacons_nlpid = info->snd_pid;
6645
6646 return 0;
6647}
6648
Johannes Berg4c476992010-10-04 21:36:35 +02006649#define NL80211_FLAG_NEED_WIPHY 0x01
6650#define NL80211_FLAG_NEED_NETDEV 0x02
6651#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006652#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6653#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6654 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006655
6656static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6657 struct genl_info *info)
6658{
6659 struct cfg80211_registered_device *rdev;
6660 struct net_device *dev;
6661 int err;
6662 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6663
6664 if (rtnl)
6665 rtnl_lock();
6666
6667 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006668 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006669 if (IS_ERR(rdev)) {
6670 if (rtnl)
6671 rtnl_unlock();
6672 return PTR_ERR(rdev);
6673 }
6674 info->user_ptr[0] = rdev;
6675 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006676 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6677 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006678 if (err) {
6679 if (rtnl)
6680 rtnl_unlock();
6681 return err;
6682 }
Johannes Berg41265712010-10-04 21:14:05 +02006683 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6684 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006685 cfg80211_unlock_rdev(rdev);
6686 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006687 if (rtnl)
6688 rtnl_unlock();
6689 return -ENETDOWN;
6690 }
Johannes Berg4c476992010-10-04 21:36:35 +02006691 info->user_ptr[0] = rdev;
6692 info->user_ptr[1] = dev;
6693 }
6694
6695 return 0;
6696}
6697
6698static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6699 struct genl_info *info)
6700{
6701 if (info->user_ptr[0])
6702 cfg80211_unlock_rdev(info->user_ptr[0]);
6703 if (info->user_ptr[1])
6704 dev_put(info->user_ptr[1]);
6705 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6706 rtnl_unlock();
6707}
6708
Johannes Berg55682962007-09-20 13:09:35 -04006709static struct genl_ops nl80211_ops[] = {
6710 {
6711 .cmd = NL80211_CMD_GET_WIPHY,
6712 .doit = nl80211_get_wiphy,
6713 .dumpit = nl80211_dump_wiphy,
6714 .policy = nl80211_policy,
6715 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006716 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006717 },
6718 {
6719 .cmd = NL80211_CMD_SET_WIPHY,
6720 .doit = nl80211_set_wiphy,
6721 .policy = nl80211_policy,
6722 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006723 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006724 },
6725 {
6726 .cmd = NL80211_CMD_GET_INTERFACE,
6727 .doit = nl80211_get_interface,
6728 .dumpit = nl80211_dump_interface,
6729 .policy = nl80211_policy,
6730 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006731 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006732 },
6733 {
6734 .cmd = NL80211_CMD_SET_INTERFACE,
6735 .doit = nl80211_set_interface,
6736 .policy = nl80211_policy,
6737 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006738 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6739 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006740 },
6741 {
6742 .cmd = NL80211_CMD_NEW_INTERFACE,
6743 .doit = nl80211_new_interface,
6744 .policy = nl80211_policy,
6745 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006746 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6747 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006748 },
6749 {
6750 .cmd = NL80211_CMD_DEL_INTERFACE,
6751 .doit = nl80211_del_interface,
6752 .policy = nl80211_policy,
6753 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006754 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6755 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006756 },
Johannes Berg41ade002007-12-19 02:03:29 +01006757 {
6758 .cmd = NL80211_CMD_GET_KEY,
6759 .doit = nl80211_get_key,
6760 .policy = nl80211_policy,
6761 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006762 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006763 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006764 },
6765 {
6766 .cmd = NL80211_CMD_SET_KEY,
6767 .doit = nl80211_set_key,
6768 .policy = nl80211_policy,
6769 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006770 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006771 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006772 },
6773 {
6774 .cmd = NL80211_CMD_NEW_KEY,
6775 .doit = nl80211_new_key,
6776 .policy = nl80211_policy,
6777 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006778 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006779 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006780 },
6781 {
6782 .cmd = NL80211_CMD_DEL_KEY,
6783 .doit = nl80211_del_key,
6784 .policy = nl80211_policy,
6785 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006786 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006787 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006788 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006789 {
6790 .cmd = NL80211_CMD_SET_BEACON,
6791 .policy = nl80211_policy,
6792 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006793 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006794 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006795 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006796 },
6797 {
Johannes Berg88600202012-02-13 15:17:18 +01006798 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006799 .policy = nl80211_policy,
6800 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006801 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006802 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006803 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006804 },
6805 {
Johannes Berg88600202012-02-13 15:17:18 +01006806 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006807 .policy = nl80211_policy,
6808 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006809 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006810 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006811 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006812 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006813 {
6814 .cmd = NL80211_CMD_GET_STATION,
6815 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006816 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006817 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006818 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6819 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006820 },
6821 {
6822 .cmd = NL80211_CMD_SET_STATION,
6823 .doit = nl80211_set_station,
6824 .policy = nl80211_policy,
6825 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006826 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006827 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006828 },
6829 {
6830 .cmd = NL80211_CMD_NEW_STATION,
6831 .doit = nl80211_new_station,
6832 .policy = nl80211_policy,
6833 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006834 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006835 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006836 },
6837 {
6838 .cmd = NL80211_CMD_DEL_STATION,
6839 .doit = nl80211_del_station,
6840 .policy = nl80211_policy,
6841 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006842 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006843 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006844 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006845 {
6846 .cmd = NL80211_CMD_GET_MPATH,
6847 .doit = nl80211_get_mpath,
6848 .dumpit = nl80211_dump_mpath,
6849 .policy = nl80211_policy,
6850 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006851 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006852 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006853 },
6854 {
6855 .cmd = NL80211_CMD_SET_MPATH,
6856 .doit = nl80211_set_mpath,
6857 .policy = nl80211_policy,
6858 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006859 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006860 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006861 },
6862 {
6863 .cmd = NL80211_CMD_NEW_MPATH,
6864 .doit = nl80211_new_mpath,
6865 .policy = nl80211_policy,
6866 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006867 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006868 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006869 },
6870 {
6871 .cmd = NL80211_CMD_DEL_MPATH,
6872 .doit = nl80211_del_mpath,
6873 .policy = nl80211_policy,
6874 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006875 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006876 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006877 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006878 {
6879 .cmd = NL80211_CMD_SET_BSS,
6880 .doit = nl80211_set_bss,
6881 .policy = nl80211_policy,
6882 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006883 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006884 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006885 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006886 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006887 .cmd = NL80211_CMD_GET_REG,
6888 .doit = nl80211_get_reg,
6889 .policy = nl80211_policy,
6890 /* can be retrieved by unprivileged users */
6891 },
6892 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006893 .cmd = NL80211_CMD_SET_REG,
6894 .doit = nl80211_set_reg,
6895 .policy = nl80211_policy,
6896 .flags = GENL_ADMIN_PERM,
6897 },
6898 {
6899 .cmd = NL80211_CMD_REQ_SET_REG,
6900 .doit = nl80211_req_set_reg,
6901 .policy = nl80211_policy,
6902 .flags = GENL_ADMIN_PERM,
6903 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006904 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006905 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6906 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006907 .policy = nl80211_policy,
6908 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006909 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006910 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006911 },
6912 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006913 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6914 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006915 .policy = nl80211_policy,
6916 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006917 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006918 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006919 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006920 {
Johannes Berg2a519312009-02-10 21:25:55 +01006921 .cmd = NL80211_CMD_TRIGGER_SCAN,
6922 .doit = nl80211_trigger_scan,
6923 .policy = nl80211_policy,
6924 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006925 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006926 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006927 },
6928 {
6929 .cmd = NL80211_CMD_GET_SCAN,
6930 .policy = nl80211_policy,
6931 .dumpit = nl80211_dump_scan,
6932 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006933 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006934 .cmd = NL80211_CMD_START_SCHED_SCAN,
6935 .doit = nl80211_start_sched_scan,
6936 .policy = nl80211_policy,
6937 .flags = GENL_ADMIN_PERM,
6938 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6939 NL80211_FLAG_NEED_RTNL,
6940 },
6941 {
6942 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6943 .doit = nl80211_stop_sched_scan,
6944 .policy = nl80211_policy,
6945 .flags = GENL_ADMIN_PERM,
6946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6947 NL80211_FLAG_NEED_RTNL,
6948 },
6949 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006950 .cmd = NL80211_CMD_AUTHENTICATE,
6951 .doit = nl80211_authenticate,
6952 .policy = nl80211_policy,
6953 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006955 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006956 },
6957 {
6958 .cmd = NL80211_CMD_ASSOCIATE,
6959 .doit = nl80211_associate,
6960 .policy = nl80211_policy,
6961 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006962 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006963 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006964 },
6965 {
6966 .cmd = NL80211_CMD_DEAUTHENTICATE,
6967 .doit = nl80211_deauthenticate,
6968 .policy = nl80211_policy,
6969 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006970 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006971 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006972 },
6973 {
6974 .cmd = NL80211_CMD_DISASSOCIATE,
6975 .doit = nl80211_disassociate,
6976 .policy = nl80211_policy,
6977 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006978 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006979 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006980 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006981 {
6982 .cmd = NL80211_CMD_JOIN_IBSS,
6983 .doit = nl80211_join_ibss,
6984 .policy = nl80211_policy,
6985 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006986 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006987 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006988 },
6989 {
6990 .cmd = NL80211_CMD_LEAVE_IBSS,
6991 .doit = nl80211_leave_ibss,
6992 .policy = nl80211_policy,
6993 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006994 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006995 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006996 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006997#ifdef CONFIG_NL80211_TESTMODE
6998 {
6999 .cmd = NL80211_CMD_TESTMODE,
7000 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007001 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007002 .policy = nl80211_policy,
7003 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007004 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7005 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007006 },
7007#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007008 {
7009 .cmd = NL80211_CMD_CONNECT,
7010 .doit = nl80211_connect,
7011 .policy = nl80211_policy,
7012 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007014 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007015 },
7016 {
7017 .cmd = NL80211_CMD_DISCONNECT,
7018 .doit = nl80211_disconnect,
7019 .policy = nl80211_policy,
7020 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007021 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007022 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007023 },
Johannes Berg463d0182009-07-14 00:33:35 +02007024 {
7025 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7026 .doit = nl80211_wiphy_netns,
7027 .policy = nl80211_policy,
7028 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007029 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7030 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007031 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007032 {
7033 .cmd = NL80211_CMD_GET_SURVEY,
7034 .policy = nl80211_policy,
7035 .dumpit = nl80211_dump_survey,
7036 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007037 {
7038 .cmd = NL80211_CMD_SET_PMKSA,
7039 .doit = nl80211_setdel_pmksa,
7040 .policy = nl80211_policy,
7041 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007042 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007043 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007044 },
7045 {
7046 .cmd = NL80211_CMD_DEL_PMKSA,
7047 .doit = nl80211_setdel_pmksa,
7048 .policy = nl80211_policy,
7049 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007050 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007051 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007052 },
7053 {
7054 .cmd = NL80211_CMD_FLUSH_PMKSA,
7055 .doit = nl80211_flush_pmksa,
7056 .policy = nl80211_policy,
7057 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007058 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007059 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007060 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007061 {
7062 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7063 .doit = nl80211_remain_on_channel,
7064 .policy = nl80211_policy,
7065 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007066 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007067 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007068 },
7069 {
7070 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7071 .doit = nl80211_cancel_remain_on_channel,
7072 .policy = nl80211_policy,
7073 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007074 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007075 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007076 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007077 {
7078 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7079 .doit = nl80211_set_tx_bitrate_mask,
7080 .policy = nl80211_policy,
7081 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007082 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7083 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007084 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007085 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007086 .cmd = NL80211_CMD_REGISTER_FRAME,
7087 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007088 .policy = nl80211_policy,
7089 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007090 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7091 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007092 },
7093 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007094 .cmd = NL80211_CMD_FRAME,
7095 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007096 .policy = nl80211_policy,
7097 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007098 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007099 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007100 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007101 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007102 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7103 .doit = nl80211_tx_mgmt_cancel_wait,
7104 .policy = nl80211_policy,
7105 .flags = GENL_ADMIN_PERM,
7106 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7107 NL80211_FLAG_NEED_RTNL,
7108 },
7109 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007110 .cmd = NL80211_CMD_SET_POWER_SAVE,
7111 .doit = nl80211_set_power_save,
7112 .policy = nl80211_policy,
7113 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007114 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7115 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007116 },
7117 {
7118 .cmd = NL80211_CMD_GET_POWER_SAVE,
7119 .doit = nl80211_get_power_save,
7120 .policy = nl80211_policy,
7121 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007122 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7123 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007124 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007125 {
7126 .cmd = NL80211_CMD_SET_CQM,
7127 .doit = nl80211_set_cqm,
7128 .policy = nl80211_policy,
7129 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007130 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7131 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007132 },
Johannes Bergf444de02010-05-05 15:25:02 +02007133 {
7134 .cmd = NL80211_CMD_SET_CHANNEL,
7135 .doit = nl80211_set_channel,
7136 .policy = nl80211_policy,
7137 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007138 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7139 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007140 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007141 {
7142 .cmd = NL80211_CMD_SET_WDS_PEER,
7143 .doit = nl80211_set_wds_peer,
7144 .policy = nl80211_policy,
7145 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007146 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7147 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007148 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007149 {
7150 .cmd = NL80211_CMD_JOIN_MESH,
7151 .doit = nl80211_join_mesh,
7152 .policy = nl80211_policy,
7153 .flags = GENL_ADMIN_PERM,
7154 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7155 NL80211_FLAG_NEED_RTNL,
7156 },
7157 {
7158 .cmd = NL80211_CMD_LEAVE_MESH,
7159 .doit = nl80211_leave_mesh,
7160 .policy = nl80211_policy,
7161 .flags = GENL_ADMIN_PERM,
7162 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7163 NL80211_FLAG_NEED_RTNL,
7164 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007165#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007166 {
7167 .cmd = NL80211_CMD_GET_WOWLAN,
7168 .doit = nl80211_get_wowlan,
7169 .policy = nl80211_policy,
7170 /* can be retrieved by unprivileged users */
7171 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7172 NL80211_FLAG_NEED_RTNL,
7173 },
7174 {
7175 .cmd = NL80211_CMD_SET_WOWLAN,
7176 .doit = nl80211_set_wowlan,
7177 .policy = nl80211_policy,
7178 .flags = GENL_ADMIN_PERM,
7179 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7180 NL80211_FLAG_NEED_RTNL,
7181 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007182#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007183 {
7184 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7185 .doit = nl80211_set_rekey_data,
7186 .policy = nl80211_policy,
7187 .flags = GENL_ADMIN_PERM,
7188 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7189 NL80211_FLAG_NEED_RTNL,
7190 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007191 {
7192 .cmd = NL80211_CMD_TDLS_MGMT,
7193 .doit = nl80211_tdls_mgmt,
7194 .policy = nl80211_policy,
7195 .flags = GENL_ADMIN_PERM,
7196 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7197 NL80211_FLAG_NEED_RTNL,
7198 },
7199 {
7200 .cmd = NL80211_CMD_TDLS_OPER,
7201 .doit = nl80211_tdls_oper,
7202 .policy = nl80211_policy,
7203 .flags = GENL_ADMIN_PERM,
7204 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7205 NL80211_FLAG_NEED_RTNL,
7206 },
Johannes Berg28946da2011-11-04 11:18:12 +01007207 {
7208 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7209 .doit = nl80211_register_unexpected_frame,
7210 .policy = nl80211_policy,
7211 .flags = GENL_ADMIN_PERM,
7212 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7213 NL80211_FLAG_NEED_RTNL,
7214 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007215 {
7216 .cmd = NL80211_CMD_PROBE_CLIENT,
7217 .doit = nl80211_probe_client,
7218 .policy = nl80211_policy,
7219 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007220 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007221 NL80211_FLAG_NEED_RTNL,
7222 },
Johannes Berg5e760232011-11-04 11:18:17 +01007223 {
7224 .cmd = NL80211_CMD_REGISTER_BEACONS,
7225 .doit = nl80211_register_beacons,
7226 .policy = nl80211_policy,
7227 .flags = GENL_ADMIN_PERM,
7228 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7229 NL80211_FLAG_NEED_RTNL,
7230 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007231 {
7232 .cmd = NL80211_CMD_SET_NOACK_MAP,
7233 .doit = nl80211_set_noack_map,
7234 .policy = nl80211_policy,
7235 .flags = GENL_ADMIN_PERM,
7236 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7237 NL80211_FLAG_NEED_RTNL,
7238 },
7239
Johannes Berg55682962007-09-20 13:09:35 -04007240};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007241
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007242static struct genl_multicast_group nl80211_mlme_mcgrp = {
7243 .name = "mlme",
7244};
Johannes Berg55682962007-09-20 13:09:35 -04007245
7246/* multicast groups */
7247static struct genl_multicast_group nl80211_config_mcgrp = {
7248 .name = "config",
7249};
Johannes Berg2a519312009-02-10 21:25:55 +01007250static struct genl_multicast_group nl80211_scan_mcgrp = {
7251 .name = "scan",
7252};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007253static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7254 .name = "regulatory",
7255};
Johannes Berg55682962007-09-20 13:09:35 -04007256
7257/* notification functions */
7258
7259void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7260{
7261 struct sk_buff *msg;
7262
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007264 if (!msg)
7265 return;
7266
7267 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7268 nlmsg_free(msg);
7269 return;
7270 }
7271
Johannes Berg463d0182009-07-14 00:33:35 +02007272 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7273 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007274}
7275
Johannes Berg362a4152009-05-24 16:43:15 +02007276static int nl80211_add_scan_req(struct sk_buff *msg,
7277 struct cfg80211_registered_device *rdev)
7278{
7279 struct cfg80211_scan_request *req = rdev->scan_req;
7280 struct nlattr *nest;
7281 int i;
7282
Johannes Berg667503d2009-07-07 03:56:11 +02007283 ASSERT_RDEV_LOCK(rdev);
7284
Johannes Berg362a4152009-05-24 16:43:15 +02007285 if (WARN_ON(!req))
7286 return 0;
7287
7288 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7289 if (!nest)
7290 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007291 for (i = 0; i < req->n_ssids; i++) {
7292 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7293 goto nla_put_failure;
7294 }
Johannes Berg362a4152009-05-24 16:43:15 +02007295 nla_nest_end(msg, nest);
7296
7297 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7298 if (!nest)
7299 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007300 for (i = 0; i < req->n_channels; i++) {
7301 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7302 goto nla_put_failure;
7303 }
Johannes Berg362a4152009-05-24 16:43:15 +02007304 nla_nest_end(msg, nest);
7305
David S. Miller9360ffd2012-03-29 04:41:26 -04007306 if (req->ie &&
7307 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7308 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007309
7310 return 0;
7311 nla_put_failure:
7312 return -ENOBUFS;
7313}
7314
Johannes Berga538e2d2009-06-16 19:56:42 +02007315static int nl80211_send_scan_msg(struct sk_buff *msg,
7316 struct cfg80211_registered_device *rdev,
7317 struct net_device *netdev,
7318 u32 pid, u32 seq, int flags,
7319 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007320{
7321 void *hdr;
7322
7323 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7324 if (!hdr)
7325 return -1;
7326
David S. Miller9360ffd2012-03-29 04:41:26 -04007327 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7328 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7329 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007330
Johannes Berg362a4152009-05-24 16:43:15 +02007331 /* ignore errors and send incomplete event anyway */
7332 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007333
7334 return genlmsg_end(msg, hdr);
7335
7336 nla_put_failure:
7337 genlmsg_cancel(msg, hdr);
7338 return -EMSGSIZE;
7339}
7340
Luciano Coelho807f8a82011-05-11 17:09:35 +03007341static int
7342nl80211_send_sched_scan_msg(struct sk_buff *msg,
7343 struct cfg80211_registered_device *rdev,
7344 struct net_device *netdev,
7345 u32 pid, u32 seq, int flags, u32 cmd)
7346{
7347 void *hdr;
7348
7349 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7350 if (!hdr)
7351 return -1;
7352
David S. Miller9360ffd2012-03-29 04:41:26 -04007353 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7354 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7355 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007356
7357 return genlmsg_end(msg, hdr);
7358
7359 nla_put_failure:
7360 genlmsg_cancel(msg, hdr);
7361 return -EMSGSIZE;
7362}
7363
Johannes Berga538e2d2009-06-16 19:56:42 +02007364void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7365 struct net_device *netdev)
7366{
7367 struct sk_buff *msg;
7368
7369 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7370 if (!msg)
7371 return;
7372
7373 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7374 NL80211_CMD_TRIGGER_SCAN) < 0) {
7375 nlmsg_free(msg);
7376 return;
7377 }
7378
Johannes Berg463d0182009-07-14 00:33:35 +02007379 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7380 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007381}
7382
Johannes Berg2a519312009-02-10 21:25:55 +01007383void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7384 struct net_device *netdev)
7385{
7386 struct sk_buff *msg;
7387
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007389 if (!msg)
7390 return;
7391
Johannes Berga538e2d2009-06-16 19:56:42 +02007392 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7393 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007394 nlmsg_free(msg);
7395 return;
7396 }
7397
Johannes Berg463d0182009-07-14 00:33:35 +02007398 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7399 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007400}
7401
7402void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7403 struct net_device *netdev)
7404{
7405 struct sk_buff *msg;
7406
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007407 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007408 if (!msg)
7409 return;
7410
Johannes Berga538e2d2009-06-16 19:56:42 +02007411 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7412 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007413 nlmsg_free(msg);
7414 return;
7415 }
7416
Johannes Berg463d0182009-07-14 00:33:35 +02007417 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7418 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007419}
7420
Luciano Coelho807f8a82011-05-11 17:09:35 +03007421void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7422 struct net_device *netdev)
7423{
7424 struct sk_buff *msg;
7425
7426 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7427 if (!msg)
7428 return;
7429
7430 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7431 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7432 nlmsg_free(msg);
7433 return;
7434 }
7435
7436 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7437 nl80211_scan_mcgrp.id, GFP_KERNEL);
7438}
7439
7440void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7441 struct net_device *netdev, u32 cmd)
7442{
7443 struct sk_buff *msg;
7444
7445 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7446 if (!msg)
7447 return;
7448
7449 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7450 nlmsg_free(msg);
7451 return;
7452 }
7453
7454 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7455 nl80211_scan_mcgrp.id, GFP_KERNEL);
7456}
7457
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007458/*
7459 * This can happen on global regulatory changes or device specific settings
7460 * based on custom world regulatory domains.
7461 */
7462void nl80211_send_reg_change_event(struct regulatory_request *request)
7463{
7464 struct sk_buff *msg;
7465 void *hdr;
7466
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007467 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007468 if (!msg)
7469 return;
7470
7471 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7472 if (!hdr) {
7473 nlmsg_free(msg);
7474 return;
7475 }
7476
7477 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007478 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7479 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007480
David S. Miller9360ffd2012-03-29 04:41:26 -04007481 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7482 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7483 NL80211_REGDOM_TYPE_WORLD))
7484 goto nla_put_failure;
7485 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7486 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7487 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7488 goto nla_put_failure;
7489 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7490 request->intersect) {
7491 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7492 NL80211_REGDOM_TYPE_INTERSECTION))
7493 goto nla_put_failure;
7494 } else {
7495 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7496 NL80211_REGDOM_TYPE_COUNTRY) ||
7497 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7498 request->alpha2))
7499 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007500 }
7501
David S. Miller9360ffd2012-03-29 04:41:26 -04007502 if (wiphy_idx_valid(request->wiphy_idx) &&
7503 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7504 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007505
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007506 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007507
Johannes Bergbc43b282009-07-25 10:54:13 +02007508 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007509 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007510 GFP_ATOMIC);
7511 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007512
7513 return;
7514
7515nla_put_failure:
7516 genlmsg_cancel(msg, hdr);
7517 nlmsg_free(msg);
7518}
7519
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007520static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7521 struct net_device *netdev,
7522 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007523 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007524{
7525 struct sk_buff *msg;
7526 void *hdr;
7527
Johannes Berge6d6e342009-07-01 21:26:47 +02007528 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007529 if (!msg)
7530 return;
7531
7532 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7533 if (!hdr) {
7534 nlmsg_free(msg);
7535 return;
7536 }
7537
David S. Miller9360ffd2012-03-29 04:41:26 -04007538 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7539 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7540 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7541 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007542
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007543 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007544
Johannes Berg463d0182009-07-14 00:33:35 +02007545 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7546 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007547 return;
7548
7549 nla_put_failure:
7550 genlmsg_cancel(msg, hdr);
7551 nlmsg_free(msg);
7552}
7553
7554void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007555 struct net_device *netdev, const u8 *buf,
7556 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007557{
7558 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007559 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007560}
7561
7562void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7563 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007564 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007565{
Johannes Berge6d6e342009-07-01 21:26:47 +02007566 nl80211_send_mlme_event(rdev, netdev, buf, len,
7567 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007568}
7569
Jouni Malinen53b46b82009-03-27 20:53:56 +02007570void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007571 struct net_device *netdev, const u8 *buf,
7572 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007573{
7574 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007575 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007576}
7577
Jouni Malinen53b46b82009-03-27 20:53:56 +02007578void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7579 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007580 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007581{
7582 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007583 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007584}
7585
Jouni Malinencf4e5942010-12-16 00:52:40 +02007586void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7587 struct net_device *netdev, const u8 *buf,
7588 size_t len, gfp_t gfp)
7589{
7590 nl80211_send_mlme_event(rdev, netdev, buf, len,
7591 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7592}
7593
7594void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7595 struct net_device *netdev, const u8 *buf,
7596 size_t len, gfp_t gfp)
7597{
7598 nl80211_send_mlme_event(rdev, netdev, buf, len,
7599 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7600}
7601
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007602static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7603 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007604 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007605{
7606 struct sk_buff *msg;
7607 void *hdr;
7608
Johannes Berge6d6e342009-07-01 21:26:47 +02007609 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007610 if (!msg)
7611 return;
7612
7613 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7614 if (!hdr) {
7615 nlmsg_free(msg);
7616 return;
7617 }
7618
David S. Miller9360ffd2012-03-29 04:41:26 -04007619 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7620 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7621 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7622 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7623 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007624
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007625 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007626
Johannes Berg463d0182009-07-14 00:33:35 +02007627 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7628 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007629 return;
7630
7631 nla_put_failure:
7632 genlmsg_cancel(msg, hdr);
7633 nlmsg_free(msg);
7634}
7635
7636void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007637 struct net_device *netdev, const u8 *addr,
7638 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007639{
7640 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007641 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007642}
7643
7644void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007645 struct net_device *netdev, const u8 *addr,
7646 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007647{
Johannes Berge6d6e342009-07-01 21:26:47 +02007648 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7649 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007650}
7651
Samuel Ortizb23aa672009-07-01 21:26:54 +02007652void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7653 struct net_device *netdev, const u8 *bssid,
7654 const u8 *req_ie, size_t req_ie_len,
7655 const u8 *resp_ie, size_t resp_ie_len,
7656 u16 status, gfp_t gfp)
7657{
7658 struct sk_buff *msg;
7659 void *hdr;
7660
7661 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7662 if (!msg)
7663 return;
7664
7665 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7666 if (!hdr) {
7667 nlmsg_free(msg);
7668 return;
7669 }
7670
David S. Miller9360ffd2012-03-29 04:41:26 -04007671 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7672 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7673 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7674 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7675 (req_ie &&
7676 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7677 (resp_ie &&
7678 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7679 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007680
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007681 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007682
Johannes Berg463d0182009-07-14 00:33:35 +02007683 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7684 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007685 return;
7686
7687 nla_put_failure:
7688 genlmsg_cancel(msg, hdr);
7689 nlmsg_free(msg);
7690
7691}
7692
7693void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7694 struct net_device *netdev, const u8 *bssid,
7695 const u8 *req_ie, size_t req_ie_len,
7696 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7697{
7698 struct sk_buff *msg;
7699 void *hdr;
7700
7701 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7702 if (!msg)
7703 return;
7704
7705 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7706 if (!hdr) {
7707 nlmsg_free(msg);
7708 return;
7709 }
7710
David S. Miller9360ffd2012-03-29 04:41:26 -04007711 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7712 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7713 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7714 (req_ie &&
7715 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7716 (resp_ie &&
7717 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7718 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007719
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007720 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007721
Johannes Berg463d0182009-07-14 00:33:35 +02007722 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7723 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007724 return;
7725
7726 nla_put_failure:
7727 genlmsg_cancel(msg, hdr);
7728 nlmsg_free(msg);
7729
7730}
7731
7732void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7733 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007734 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007735{
7736 struct sk_buff *msg;
7737 void *hdr;
7738
Johannes Berg667503d2009-07-07 03:56:11 +02007739 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007740 if (!msg)
7741 return;
7742
7743 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7744 if (!hdr) {
7745 nlmsg_free(msg);
7746 return;
7747 }
7748
David S. Miller9360ffd2012-03-29 04:41:26 -04007749 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7750 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7751 (from_ap && reason &&
7752 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7753 (from_ap &&
7754 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7755 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7756 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007757
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007758 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007759
Johannes Berg463d0182009-07-14 00:33:35 +02007760 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7761 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007762 return;
7763
7764 nla_put_failure:
7765 genlmsg_cancel(msg, hdr);
7766 nlmsg_free(msg);
7767
7768}
7769
Johannes Berg04a773a2009-04-19 21:24:32 +02007770void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7771 struct net_device *netdev, const u8 *bssid,
7772 gfp_t gfp)
7773{
7774 struct sk_buff *msg;
7775 void *hdr;
7776
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007777 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007778 if (!msg)
7779 return;
7780
7781 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7782 if (!hdr) {
7783 nlmsg_free(msg);
7784 return;
7785 }
7786
David S. Miller9360ffd2012-03-29 04:41:26 -04007787 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7788 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7789 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7790 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007791
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007792 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007793
Johannes Berg463d0182009-07-14 00:33:35 +02007794 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7795 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007796 return;
7797
7798 nla_put_failure:
7799 genlmsg_cancel(msg, hdr);
7800 nlmsg_free(msg);
7801}
7802
Javier Cardonac93b5e72011-04-07 15:08:34 -07007803void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7804 struct net_device *netdev,
7805 const u8 *macaddr, const u8* ie, u8 ie_len,
7806 gfp_t gfp)
7807{
7808 struct sk_buff *msg;
7809 void *hdr;
7810
7811 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7812 if (!msg)
7813 return;
7814
7815 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7816 if (!hdr) {
7817 nlmsg_free(msg);
7818 return;
7819 }
7820
David S. Miller9360ffd2012-03-29 04:41:26 -04007821 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7822 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7823 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7824 (ie_len && ie &&
7825 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7826 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007827
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007828 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007829
7830 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7831 nl80211_mlme_mcgrp.id, gfp);
7832 return;
7833
7834 nla_put_failure:
7835 genlmsg_cancel(msg, hdr);
7836 nlmsg_free(msg);
7837}
7838
Jouni Malinena3b8b052009-03-27 21:59:49 +02007839void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7840 struct net_device *netdev, const u8 *addr,
7841 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007842 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007843{
7844 struct sk_buff *msg;
7845 void *hdr;
7846
Johannes Berge6d6e342009-07-01 21:26:47 +02007847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007848 if (!msg)
7849 return;
7850
7851 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7852 if (!hdr) {
7853 nlmsg_free(msg);
7854 return;
7855 }
7856
David S. Miller9360ffd2012-03-29 04:41:26 -04007857 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7858 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7859 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7860 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7861 (key_id != -1 &&
7862 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7863 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7864 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007865
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007866 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007867
Johannes Berg463d0182009-07-14 00:33:35 +02007868 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7869 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007870 return;
7871
7872 nla_put_failure:
7873 genlmsg_cancel(msg, hdr);
7874 nlmsg_free(msg);
7875}
7876
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007877void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7878 struct ieee80211_channel *channel_before,
7879 struct ieee80211_channel *channel_after)
7880{
7881 struct sk_buff *msg;
7882 void *hdr;
7883 struct nlattr *nl_freq;
7884
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007886 if (!msg)
7887 return;
7888
7889 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7890 if (!hdr) {
7891 nlmsg_free(msg);
7892 return;
7893 }
7894
7895 /*
7896 * Since we are applying the beacon hint to a wiphy we know its
7897 * wiphy_idx is valid
7898 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007899 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7900 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007901
7902 /* Before */
7903 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7904 if (!nl_freq)
7905 goto nla_put_failure;
7906 if (nl80211_msg_put_channel(msg, channel_before))
7907 goto nla_put_failure;
7908 nla_nest_end(msg, nl_freq);
7909
7910 /* After */
7911 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7912 if (!nl_freq)
7913 goto nla_put_failure;
7914 if (nl80211_msg_put_channel(msg, channel_after))
7915 goto nla_put_failure;
7916 nla_nest_end(msg, nl_freq);
7917
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007918 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007919
Johannes Berg463d0182009-07-14 00:33:35 +02007920 rcu_read_lock();
7921 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7922 GFP_ATOMIC);
7923 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007924
7925 return;
7926
7927nla_put_failure:
7928 genlmsg_cancel(msg, hdr);
7929 nlmsg_free(msg);
7930}
7931
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007932static void nl80211_send_remain_on_chan_event(
7933 int cmd, struct cfg80211_registered_device *rdev,
7934 struct net_device *netdev, u64 cookie,
7935 struct ieee80211_channel *chan,
7936 enum nl80211_channel_type channel_type,
7937 unsigned int duration, gfp_t gfp)
7938{
7939 struct sk_buff *msg;
7940 void *hdr;
7941
7942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7943 if (!msg)
7944 return;
7945
7946 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7947 if (!hdr) {
7948 nlmsg_free(msg);
7949 return;
7950 }
7951
David S. Miller9360ffd2012-03-29 04:41:26 -04007952 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7953 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7954 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7955 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7956 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7957 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007958
David S. Miller9360ffd2012-03-29 04:41:26 -04007959 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7960 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7961 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007962
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007963 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007964
7965 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7966 nl80211_mlme_mcgrp.id, gfp);
7967 return;
7968
7969 nla_put_failure:
7970 genlmsg_cancel(msg, hdr);
7971 nlmsg_free(msg);
7972}
7973
7974void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7975 struct net_device *netdev, u64 cookie,
7976 struct ieee80211_channel *chan,
7977 enum nl80211_channel_type channel_type,
7978 unsigned int duration, gfp_t gfp)
7979{
7980 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7981 rdev, netdev, cookie, chan,
7982 channel_type, duration, gfp);
7983}
7984
7985void nl80211_send_remain_on_channel_cancel(
7986 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7987 u64 cookie, struct ieee80211_channel *chan,
7988 enum nl80211_channel_type channel_type, gfp_t gfp)
7989{
7990 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7991 rdev, netdev, cookie, chan,
7992 channel_type, 0, gfp);
7993}
7994
Johannes Berg98b62182009-12-23 13:15:44 +01007995void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7996 struct net_device *dev, const u8 *mac_addr,
7997 struct station_info *sinfo, gfp_t gfp)
7998{
7999 struct sk_buff *msg;
8000
8001 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8002 if (!msg)
8003 return;
8004
John W. Linville66266b32012-03-15 13:25:41 -04008005 if (nl80211_send_station(msg, 0, 0, 0,
8006 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008007 nlmsg_free(msg);
8008 return;
8009 }
8010
8011 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8012 nl80211_mlme_mcgrp.id, gfp);
8013}
8014
Jouni Malinenec15e682011-03-23 15:29:52 +02008015void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8016 struct net_device *dev, const u8 *mac_addr,
8017 gfp_t gfp)
8018{
8019 struct sk_buff *msg;
8020 void *hdr;
8021
8022 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8023 if (!msg)
8024 return;
8025
8026 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8027 if (!hdr) {
8028 nlmsg_free(msg);
8029 return;
8030 }
8031
David S. Miller9360ffd2012-03-29 04:41:26 -04008032 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8033 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8034 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008035
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008036 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008037
8038 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8039 nl80211_mlme_mcgrp.id, gfp);
8040 return;
8041
8042 nla_put_failure:
8043 genlmsg_cancel(msg, hdr);
8044 nlmsg_free(msg);
8045}
8046
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008047static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8048 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008049{
8050 struct wireless_dev *wdev = dev->ieee80211_ptr;
8051 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8052 struct sk_buff *msg;
8053 void *hdr;
8054 int err;
8055 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8056
8057 if (!nlpid)
8058 return false;
8059
8060 msg = nlmsg_new(100, gfp);
8061 if (!msg)
8062 return true;
8063
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008064 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008065 if (!hdr) {
8066 nlmsg_free(msg);
8067 return true;
8068 }
8069
David S. Miller9360ffd2012-03-29 04:41:26 -04008070 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8071 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8072 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8073 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008074
8075 err = genlmsg_end(msg, hdr);
8076 if (err < 0) {
8077 nlmsg_free(msg);
8078 return true;
8079 }
8080
8081 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8082 return true;
8083
8084 nla_put_failure:
8085 genlmsg_cancel(msg, hdr);
8086 nlmsg_free(msg);
8087 return true;
8088}
8089
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008090bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8091{
8092 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8093 addr, gfp);
8094}
8095
8096bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8097 const u8 *addr, gfp_t gfp)
8098{
8099 return __nl80211_unexpected_frame(dev,
8100 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8101 addr, gfp);
8102}
8103
Johannes Berg2e161f72010-08-12 15:38:38 +02008104int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
8105 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01008106 int freq, int sig_dbm,
8107 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008108{
8109 struct sk_buff *msg;
8110 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008111
8112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8113 if (!msg)
8114 return -ENOMEM;
8115
Johannes Berg2e161f72010-08-12 15:38:38 +02008116 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008117 if (!hdr) {
8118 nlmsg_free(msg);
8119 return -ENOMEM;
8120 }
8121
David S. Miller9360ffd2012-03-29 04:41:26 -04008122 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8123 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8124 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8125 (sig_dbm &&
8126 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8127 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8128 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008129
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008130 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008131
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008132 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008133
8134 nla_put_failure:
8135 genlmsg_cancel(msg, hdr);
8136 nlmsg_free(msg);
8137 return -ENOBUFS;
8138}
8139
Johannes Berg2e161f72010-08-12 15:38:38 +02008140void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
8141 struct net_device *netdev, u64 cookie,
8142 const u8 *buf, size_t len, bool ack,
8143 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008144{
8145 struct sk_buff *msg;
8146 void *hdr;
8147
8148 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8149 if (!msg)
8150 return;
8151
Johannes Berg2e161f72010-08-12 15:38:38 +02008152 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008153 if (!hdr) {
8154 nlmsg_free(msg);
8155 return;
8156 }
8157
David S. Miller9360ffd2012-03-29 04:41:26 -04008158 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8159 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8160 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8161 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8162 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8163 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008164
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008165 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008166
8167 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8168 return;
8169
8170 nla_put_failure:
8171 genlmsg_cancel(msg, hdr);
8172 nlmsg_free(msg);
8173}
8174
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008175void
8176nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8177 struct net_device *netdev,
8178 enum nl80211_cqm_rssi_threshold_event rssi_event,
8179 gfp_t gfp)
8180{
8181 struct sk_buff *msg;
8182 struct nlattr *pinfoattr;
8183 void *hdr;
8184
8185 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8186 if (!msg)
8187 return;
8188
8189 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8190 if (!hdr) {
8191 nlmsg_free(msg);
8192 return;
8193 }
8194
David S. Miller9360ffd2012-03-29 04:41:26 -04008195 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8196 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8197 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008198
8199 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8200 if (!pinfoattr)
8201 goto nla_put_failure;
8202
David S. Miller9360ffd2012-03-29 04:41:26 -04008203 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8204 rssi_event))
8205 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008206
8207 nla_nest_end(msg, pinfoattr);
8208
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008209 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008210
8211 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8212 nl80211_mlme_mcgrp.id, gfp);
8213 return;
8214
8215 nla_put_failure:
8216 genlmsg_cancel(msg, hdr);
8217 nlmsg_free(msg);
8218}
8219
Johannes Berge5497d72011-07-05 16:35:40 +02008220void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8221 struct net_device *netdev, const u8 *bssid,
8222 const u8 *replay_ctr, gfp_t gfp)
8223{
8224 struct sk_buff *msg;
8225 struct nlattr *rekey_attr;
8226 void *hdr;
8227
8228 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8229 if (!msg)
8230 return;
8231
8232 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8233 if (!hdr) {
8234 nlmsg_free(msg);
8235 return;
8236 }
8237
David S. Miller9360ffd2012-03-29 04:41:26 -04008238 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8239 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8240 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8241 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008242
8243 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8244 if (!rekey_attr)
8245 goto nla_put_failure;
8246
David S. Miller9360ffd2012-03-29 04:41:26 -04008247 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8248 NL80211_REPLAY_CTR_LEN, replay_ctr))
8249 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008250
8251 nla_nest_end(msg, rekey_attr);
8252
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008253 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008254
8255 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8256 nl80211_mlme_mcgrp.id, gfp);
8257 return;
8258
8259 nla_put_failure:
8260 genlmsg_cancel(msg, hdr);
8261 nlmsg_free(msg);
8262}
8263
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008264void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8265 struct net_device *netdev, int index,
8266 const u8 *bssid, bool preauth, gfp_t gfp)
8267{
8268 struct sk_buff *msg;
8269 struct nlattr *attr;
8270 void *hdr;
8271
8272 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8273 if (!msg)
8274 return;
8275
8276 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8277 if (!hdr) {
8278 nlmsg_free(msg);
8279 return;
8280 }
8281
David S. Miller9360ffd2012-03-29 04:41:26 -04008282 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8283 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8284 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008285
8286 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8287 if (!attr)
8288 goto nla_put_failure;
8289
David S. Miller9360ffd2012-03-29 04:41:26 -04008290 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8291 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8292 (preauth &&
8293 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8294 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008295
8296 nla_nest_end(msg, attr);
8297
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008298 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008299
8300 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8301 nl80211_mlme_mcgrp.id, gfp);
8302 return;
8303
8304 nla_put_failure:
8305 genlmsg_cancel(msg, hdr);
8306 nlmsg_free(msg);
8307}
8308
Thomas Pedersen53145262012-04-06 13:35:47 -07008309void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8310 struct net_device *netdev, int freq,
8311 enum nl80211_channel_type type, gfp_t gfp)
8312{
8313 struct sk_buff *msg;
8314 void *hdr;
8315
8316 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8317 if (!msg)
8318 return;
8319
8320 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8321 if (!hdr) {
8322 nlmsg_free(msg);
8323 return;
8324 }
8325
John W. Linville7eab0f62012-04-12 14:25:14 -04008326 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8327 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8328 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8329 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008330
8331 genlmsg_end(msg, hdr);
8332
8333 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8334 nl80211_mlme_mcgrp.id, gfp);
8335 return;
8336
8337 nla_put_failure:
8338 genlmsg_cancel(msg, hdr);
8339 nlmsg_free(msg);
8340}
8341
Johannes Bergc063dbf2010-11-24 08:10:05 +01008342void
8343nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8344 struct net_device *netdev, const u8 *peer,
8345 u32 num_packets, gfp_t gfp)
8346{
8347 struct sk_buff *msg;
8348 struct nlattr *pinfoattr;
8349 void *hdr;
8350
8351 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8352 if (!msg)
8353 return;
8354
8355 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8356 if (!hdr) {
8357 nlmsg_free(msg);
8358 return;
8359 }
8360
David S. Miller9360ffd2012-03-29 04:41:26 -04008361 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8362 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8363 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8364 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008365
8366 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8367 if (!pinfoattr)
8368 goto nla_put_failure;
8369
David S. Miller9360ffd2012-03-29 04:41:26 -04008370 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8371 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008372
8373 nla_nest_end(msg, pinfoattr);
8374
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008375 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008376
8377 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8378 nl80211_mlme_mcgrp.id, gfp);
8379 return;
8380
8381 nla_put_failure:
8382 genlmsg_cancel(msg, hdr);
8383 nlmsg_free(msg);
8384}
8385
Johannes Berg7f6cf312011-11-04 11:18:15 +01008386void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8387 u64 cookie, bool acked, gfp_t gfp)
8388{
8389 struct wireless_dev *wdev = dev->ieee80211_ptr;
8390 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8391 struct sk_buff *msg;
8392 void *hdr;
8393 int err;
8394
8395 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8396 if (!msg)
8397 return;
8398
8399 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8400 if (!hdr) {
8401 nlmsg_free(msg);
8402 return;
8403 }
8404
David S. Miller9360ffd2012-03-29 04:41:26 -04008405 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8406 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8407 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8408 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8409 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8410 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008411
8412 err = genlmsg_end(msg, hdr);
8413 if (err < 0) {
8414 nlmsg_free(msg);
8415 return;
8416 }
8417
8418 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8419 nl80211_mlme_mcgrp.id, gfp);
8420 return;
8421
8422 nla_put_failure:
8423 genlmsg_cancel(msg, hdr);
8424 nlmsg_free(msg);
8425}
8426EXPORT_SYMBOL(cfg80211_probe_status);
8427
Johannes Berg5e760232011-11-04 11:18:17 +01008428void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8429 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008430 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008431{
8432 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8433 struct sk_buff *msg;
8434 void *hdr;
8435 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8436
8437 if (!nlpid)
8438 return;
8439
8440 msg = nlmsg_new(len + 100, gfp);
8441 if (!msg)
8442 return;
8443
8444 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8445 if (!hdr) {
8446 nlmsg_free(msg);
8447 return;
8448 }
8449
David S. Miller9360ffd2012-03-29 04:41:26 -04008450 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8451 (freq &&
8452 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8453 (sig_dbm &&
8454 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8455 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8456 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008457
8458 genlmsg_end(msg, hdr);
8459
8460 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8461 return;
8462
8463 nla_put_failure:
8464 genlmsg_cancel(msg, hdr);
8465 nlmsg_free(msg);
8466}
8467EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8468
Jouni Malinen026331c2010-02-15 12:53:10 +02008469static int nl80211_netlink_notify(struct notifier_block * nb,
8470 unsigned long state,
8471 void *_notify)
8472{
8473 struct netlink_notify *notify = _notify;
8474 struct cfg80211_registered_device *rdev;
8475 struct wireless_dev *wdev;
8476
8477 if (state != NETLINK_URELEASE)
8478 return NOTIFY_DONE;
8479
8480 rcu_read_lock();
8481
Johannes Berg5e760232011-11-04 11:18:17 +01008482 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008483 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008484 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008485 if (rdev->ap_beacons_nlpid == notify->pid)
8486 rdev->ap_beacons_nlpid = 0;
8487 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008488
8489 rcu_read_unlock();
8490
8491 return NOTIFY_DONE;
8492}
8493
8494static struct notifier_block nl80211_netlink_notifier = {
8495 .notifier_call = nl80211_netlink_notify,
8496};
8497
Johannes Berg55682962007-09-20 13:09:35 -04008498/* initialisation/exit functions */
8499
8500int nl80211_init(void)
8501{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008502 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008503
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008504 err = genl_register_family_with_ops(&nl80211_fam,
8505 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008506 if (err)
8507 return err;
8508
Johannes Berg55682962007-09-20 13:09:35 -04008509 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8510 if (err)
8511 goto err_out;
8512
Johannes Berg2a519312009-02-10 21:25:55 +01008513 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8514 if (err)
8515 goto err_out;
8516
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008517 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8518 if (err)
8519 goto err_out;
8520
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008521 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8522 if (err)
8523 goto err_out;
8524
Johannes Bergaff89a92009-07-01 21:26:51 +02008525#ifdef CONFIG_NL80211_TESTMODE
8526 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8527 if (err)
8528 goto err_out;
8529#endif
8530
Jouni Malinen026331c2010-02-15 12:53:10 +02008531 err = netlink_register_notifier(&nl80211_netlink_notifier);
8532 if (err)
8533 goto err_out;
8534
Johannes Berg55682962007-09-20 13:09:35 -04008535 return 0;
8536 err_out:
8537 genl_unregister_family(&nl80211_fam);
8538 return err;
8539}
8540
8541void nl80211_exit(void)
8542{
Jouni Malinen026331c2010-02-15 12:53:10 +02008543 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008544 genl_unregister_family(&nl80211_fam);
8545}