blob: 089a5204dad5aa4ce75e5853240f35340831bd71 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530207 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530208 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400209};
210
Johannes Berge31b8212010-10-05 19:39:30 +0200211/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000212static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200213 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200214 [NL80211_KEY_IDX] = { .type = NLA_U8 },
215 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200216 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200217 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
218 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200219 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100220 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
221};
222
223/* policy for the key default flags */
224static const struct nla_policy
225nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
226 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
227 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200228};
229
Johannes Bergff1b6e62011-05-04 15:37:28 +0200230/* policy for WoWLAN attributes */
231static const struct nla_policy
232nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
233 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
235 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200237 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200241};
242
Johannes Berge5497d72011-07-05 16:35:40 +0200243/* policy for GTK rekey offload attributes */
244static const struct nla_policy
245nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
246 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
247 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
248 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249};
250
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300251static const struct nla_policy
252nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
253 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_SSID_LEN },
255};
256
Holger Schuriga0438972009-11-11 11:30:02 +0100257/* ifidx get helper */
258static int nl80211_get_ifidx(struct netlink_callback *cb)
259{
260 int res;
261
262 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
263 nl80211_fam.attrbuf, nl80211_fam.maxattr,
264 nl80211_policy);
265 if (res)
266 return res;
267
268 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
269 return -EINVAL;
270
271 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
272 if (!res)
273 return -EINVAL;
274 return res;
275}
276
Johannes Berg67748892010-10-04 21:14:06 +0200277static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
278 struct netlink_callback *cb,
279 struct cfg80211_registered_device **rdev,
280 struct net_device **dev)
281{
282 int ifidx = cb->args[0];
283 int err;
284
285 if (!ifidx)
286 ifidx = nl80211_get_ifidx(cb);
287 if (ifidx < 0)
288 return ifidx;
289
290 cb->args[0] = ifidx;
291
292 rtnl_lock();
293
294 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
295 if (!*dev) {
296 err = -ENODEV;
297 goto out_rtnl;
298 }
299
300 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100301 if (IS_ERR(*rdev)) {
302 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200303 goto out_rtnl;
304 }
305
306 return 0;
307 out_rtnl:
308 rtnl_unlock();
309 return err;
310}
311
312static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313{
314 cfg80211_unlock_rdev(rdev);
315 rtnl_unlock();
316}
317
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100318/* IE validation */
319static bool is_valid_ie_attr(const struct nlattr *attr)
320{
321 const u8 *pos;
322 int len;
323
324 if (!attr)
325 return true;
326
327 pos = nla_data(attr);
328 len = nla_len(attr);
329
330 while (len) {
331 u8 elemlen;
332
333 if (len < 2)
334 return false;
335 len -= 2;
336
337 elemlen = pos[1];
338 if (elemlen > len)
339 return false;
340
341 len -= elemlen;
342 pos += 2 + elemlen;
343 }
344
345 return true;
346}
347
Johannes Berg55682962007-09-20 13:09:35 -0400348/* message building helper */
349static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
350 int flags, u8 cmd)
351{
352 /* since there is no private header just add the generic one */
353 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354}
355
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400356static int nl80211_msg_put_channel(struct sk_buff *msg,
357 struct ieee80211_channel *chan)
358{
David S. Miller9360ffd2012-03-29 04:41:26 -0400359 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360 chan->center_freq))
361 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400362
David S. Miller9360ffd2012-03-29 04:41:26 -0400363 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
364 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
365 goto nla_put_failure;
366 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
367 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
368 goto nla_put_failure;
369 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
370 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
371 goto nla_put_failure;
372 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
373 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
374 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400375
David S. Miller9360ffd2012-03-29 04:41:26 -0400376 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
377 DBM_TO_MBM(chan->max_power)))
378 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400379
380 return 0;
381
382 nla_put_failure:
383 return -ENOBUFS;
384}
385
Johannes Berg55682962007-09-20 13:09:35 -0400386/* netlink command implementations */
387
Johannes Bergb9454e82009-07-08 13:29:08 +0200388struct key_parse {
389 struct key_params p;
390 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394};
395
396static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
397{
398 struct nlattr *tb[NL80211_KEY_MAX + 1];
399 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
400 nl80211_key_policy);
401 if (err)
402 return err;
403
404 k->def = !!tb[NL80211_KEY_DEFAULT];
405 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (k->def) {
408 k->def_uni = true;
409 k->def_multi = true;
410 }
411 if (k->defmgmt)
412 k->def_multi = true;
413
Johannes Bergb9454e82009-07-08 13:29:08 +0200414 if (tb[NL80211_KEY_IDX])
415 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
416
417 if (tb[NL80211_KEY_DATA]) {
418 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
419 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
420 }
421
422 if (tb[NL80211_KEY_SEQ]) {
423 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
424 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
425 }
426
427 if (tb[NL80211_KEY_CIPHER])
428 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
429
Johannes Berge31b8212010-10-05 19:39:30 +0200430 if (tb[NL80211_KEY_TYPE]) {
431 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
432 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
433 return -EINVAL;
434 }
435
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100436 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
437 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100438 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
439 tb[NL80211_KEY_DEFAULT_TYPES],
440 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100441 if (err)
442 return err;
443
444 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
445 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
446 }
447
Johannes Bergb9454e82009-07-08 13:29:08 +0200448 return 0;
449}
450
451static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
452{
453 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
454 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
455 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
456 }
457
458 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
459 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
460 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
461 }
462
463 if (info->attrs[NL80211_ATTR_KEY_IDX])
464 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
465
466 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
467 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
468
469 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
470 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
471
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100472 if (k->def) {
473 k->def_uni = true;
474 k->def_multi = true;
475 }
476 if (k->defmgmt)
477 k->def_multi = true;
478
Johannes Berge31b8212010-10-05 19:39:30 +0200479 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
480 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
481 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
482 return -EINVAL;
483 }
484
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100485 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
486 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
487 int err = nla_parse_nested(
488 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
489 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
490 nl80211_key_default_policy);
491 if (err)
492 return err;
493
494 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
495 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 return 0;
499}
500
501static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
502{
503 int err;
504
505 memset(k, 0, sizeof(*k));
506 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200507 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200508
509 if (info->attrs[NL80211_ATTR_KEY])
510 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
511 else
512 err = nl80211_parse_key_old(info, k);
513
514 if (err)
515 return err;
516
517 if (k->def && k->defmgmt)
518 return -EINVAL;
519
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (k->defmgmt) {
521 if (k->def_uni || !k->def_multi)
522 return -EINVAL;
523 }
524
Johannes Bergb9454e82009-07-08 13:29:08 +0200525 if (k->idx != -1) {
526 if (k->defmgmt) {
527 if (k->idx < 4 || k->idx > 5)
528 return -EINVAL;
529 } else if (k->def) {
530 if (k->idx < 0 || k->idx > 3)
531 return -EINVAL;
532 } else {
533 if (k->idx < 0 || k->idx > 5)
534 return -EINVAL;
535 }
536 }
537
538 return 0;
539}
540
Johannes Bergfffd0932009-07-08 14:22:54 +0200541static struct cfg80211_cached_keys *
542nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
543 struct nlattr *keys)
544{
545 struct key_parse parse;
546 struct nlattr *key;
547 struct cfg80211_cached_keys *result;
548 int rem, err, def = 0;
549
550 result = kzalloc(sizeof(*result), GFP_KERNEL);
551 if (!result)
552 return ERR_PTR(-ENOMEM);
553
554 result->def = -1;
555 result->defmgmt = -1;
556
557 nla_for_each_nested(key, keys, rem) {
558 memset(&parse, 0, sizeof(parse));
559 parse.idx = -1;
560
561 err = nl80211_parse_key_new(key, &parse);
562 if (err)
563 goto error;
564 err = -EINVAL;
565 if (!parse.p.key)
566 goto error;
567 if (parse.idx < 0 || parse.idx > 4)
568 goto error;
569 if (parse.def) {
570 if (def)
571 goto error;
572 def = 1;
573 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (!parse.def_uni || !parse.def_multi)
575 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 } else if (parse.defmgmt)
577 goto error;
578 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200579 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200580 if (err)
581 goto error;
582 result->params[parse.idx].cipher = parse.p.cipher;
583 result->params[parse.idx].key_len = parse.p.key_len;
584 result->params[parse.idx].key = result->data[parse.idx];
585 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
586 }
587
588 return result;
589 error:
590 kfree(result);
591 return ERR_PTR(err);
592}
593
594static int nl80211_key_allowed(struct wireless_dev *wdev)
595{
596 ASSERT_WDEV_LOCK(wdev);
597
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 switch (wdev->iftype) {
599 case NL80211_IFTYPE_AP:
600 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700602 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 break;
604 case NL80211_IFTYPE_ADHOC:
605 if (!wdev->current_bss)
606 return -ENOLINK;
607 break;
608 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200609 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200610 if (wdev->sme_state != CFG80211_SME_CONNECTED)
611 return -ENOLINK;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 return 0;
618}
619
Johannes Berg7527a782011-05-13 10:58:57 +0200620static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
621{
622 struct nlattr *nl_modes = nla_nest_start(msg, attr);
623 int i;
624
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400630 if ((ifmodes & 1) && nla_put_flag(msg, i))
631 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637 return 0;
638
639nla_put_failure:
640 return -ENOBUFS;
641}
642
643static int nl80211_put_iface_combinations(struct wiphy *wiphy,
644 struct sk_buff *msg)
645{
646 struct nlattr *nl_combis;
647 int i, j;
648
649 nl_combis = nla_nest_start(msg,
650 NL80211_ATTR_INTERFACE_COMBINATIONS);
651 if (!nl_combis)
652 goto nla_put_failure;
653
654 for (i = 0; i < wiphy->n_iface_combinations; i++) {
655 const struct ieee80211_iface_combination *c;
656 struct nlattr *nl_combi, *nl_limits;
657
658 c = &wiphy->iface_combinations[i];
659
660 nl_combi = nla_nest_start(msg, i + 1);
661 if (!nl_combi)
662 goto nla_put_failure;
663
664 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
665 if (!nl_limits)
666 goto nla_put_failure;
667
668 for (j = 0; j < c->n_limits; j++) {
669 struct nlattr *nl_limit;
670
671 nl_limit = nla_nest_start(msg, j + 1);
672 if (!nl_limit)
673 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400674 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
675 c->limits[j].max))
676 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200677 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
678 c->limits[j].types))
679 goto nla_put_failure;
680 nla_nest_end(msg, nl_limit);
681 }
682
683 nla_nest_end(msg, nl_limits);
684
David S. Miller9360ffd2012-03-29 04:41:26 -0400685 if (c->beacon_int_infra_match &&
686 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
687 goto nla_put_failure;
688 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
689 c->num_different_channels) ||
690 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
691 c->max_interfaces))
692 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200693
694 nla_nest_end(msg, nl_combi);
695 }
696
697 nla_nest_end(msg, nl_combis);
698
699 return 0;
700nla_put_failure:
701 return -ENOBUFS;
702}
703
Johannes Berg55682962007-09-20 13:09:35 -0400704static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
705 struct cfg80211_registered_device *dev)
706{
707 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100708 struct nlattr *nl_bands, *nl_band;
709 struct nlattr *nl_freqs, *nl_freq;
710 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100711 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100712 enum ieee80211_band band;
713 struct ieee80211_channel *chan;
714 struct ieee80211_rate *rate;
715 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200716 const struct ieee80211_txrx_stypes *mgmt_stypes =
717 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400718
719 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
720 if (!hdr)
721 return -1;
722
David S. Miller9360ffd2012-03-29 04:41:26 -0400723 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
724 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
725 nla_put_u32(msg, NL80211_ATTR_GENERATION,
726 cfg80211_rdev_list_generation) ||
727 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
728 dev->wiphy.retry_short) ||
729 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
730 dev->wiphy.retry_long) ||
731 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
732 dev->wiphy.frag_threshold) ||
733 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
734 dev->wiphy.rts_threshold) ||
735 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
736 dev->wiphy.coverage_class) ||
737 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
738 dev->wiphy.max_scan_ssids) ||
739 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
740 dev->wiphy.max_sched_scan_ssids) ||
741 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
742 dev->wiphy.max_scan_ie_len) ||
743 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
744 dev->wiphy.max_sched_scan_ie_len) ||
745 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
746 dev->wiphy.max_match_sets))
747 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200748
David S. Miller9360ffd2012-03-29 04:41:26 -0400749 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
750 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
751 goto nla_put_failure;
752 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
753 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
754 goto nla_put_failure;
755 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
756 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
757 goto nla_put_failure;
758 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
759 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
760 goto nla_put_failure;
761 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
762 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
763 goto nla_put_failure;
764 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
765 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
766 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200767
David S. Miller9360ffd2012-03-29 04:41:26 -0400768 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
769 sizeof(u32) * dev->wiphy.n_cipher_suites,
770 dev->wiphy.cipher_suites))
771 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100772
David S. Miller9360ffd2012-03-29 04:41:26 -0400773 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
774 dev->wiphy.max_num_pmkids))
775 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530776
David S. Miller9360ffd2012-03-29 04:41:26 -0400777 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
778 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
779 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200780
David S. Miller9360ffd2012-03-29 04:41:26 -0400781 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
782 dev->wiphy.available_antennas_tx) ||
783 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
784 dev->wiphy.available_antennas_rx))
785 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100786
David S. Miller9360ffd2012-03-29 04:41:26 -0400787 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
788 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
789 dev->wiphy.probe_resp_offload))
790 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200791
Bruno Randolf7f531e02010-12-16 11:30:22 +0900792 if ((dev->wiphy.available_antennas_tx ||
793 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900794 u32 tx_ant = 0, rx_ant = 0;
795 int res;
796 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
797 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
799 tx_ant) ||
800 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
801 rx_ant))
802 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900803 }
804 }
805
Johannes Berg7527a782011-05-13 10:58:57 +0200806 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
807 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700808 goto nla_put_failure;
809
Johannes Bergee688b002008-01-24 19:38:39 +0100810 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
811 if (!nl_bands)
812 goto nla_put_failure;
813
814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
815 if (!dev->wiphy.bands[band])
816 continue;
817
818 nl_band = nla_nest_start(msg, band);
819 if (!nl_band)
820 goto nla_put_failure;
821
Johannes Bergd51626d2008-10-09 12:20:13 +0200822 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
824 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
825 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
826 &dev->wiphy.bands[band]->ht_cap.mcs) ||
827 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
828 dev->wiphy.bands[band]->ht_cap.cap) ||
829 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
830 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
831 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
832 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
833 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200834
Johannes Bergee688b002008-01-24 19:38:39 +0100835 /* add frequencies */
836 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
837 if (!nl_freqs)
838 goto nla_put_failure;
839
840 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
841 nl_freq = nla_nest_start(msg, i);
842 if (!nl_freq)
843 goto nla_put_failure;
844
845 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100846
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400847 if (nl80211_msg_put_channel(msg, chan))
848 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200849
Johannes Bergee688b002008-01-24 19:38:39 +0100850 nla_nest_end(msg, nl_freq);
851 }
852
853 nla_nest_end(msg, nl_freqs);
854
855 /* add bitrates */
856 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
857 if (!nl_rates)
858 goto nla_put_failure;
859
860 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
861 nl_rate = nla_nest_start(msg, i);
862 if (!nl_rate)
863 goto nla_put_failure;
864
865 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
867 rate->bitrate))
868 goto nla_put_failure;
869 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
870 nla_put_flag(msg,
871 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
872 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100873
874 nla_nest_end(msg, nl_rate);
875 }
876
877 nla_nest_end(msg, nl_rates);
878
879 nla_nest_end(msg, nl_band);
880 }
881 nla_nest_end(msg, nl_bands);
882
Johannes Berg8fdc6212009-03-14 09:34:01 +0100883 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
884 if (!nl_cmds)
885 goto nla_put_failure;
886
887 i = 0;
888#define CMD(op, n) \
889 do { \
890 if (dev->ops->op) { \
891 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
893 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894 } \
895 } while (0)
896
897 CMD(add_virtual_intf, NEW_INTERFACE);
898 CMD(change_virtual_intf, SET_INTERFACE);
899 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100900 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100901 CMD(add_station, NEW_STATION);
902 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800903 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100904 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200905 CMD(auth, AUTHENTICATE);
906 CMD(assoc, ASSOCIATE);
907 CMD(deauth, DEAUTHENTICATE);
908 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200909 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100910 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100911 CMD(set_pmksa, SET_PMKSA);
912 CMD(del_pmksa, DEL_PMKSA);
913 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100914 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
915 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200916 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200917 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100918 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100919 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200920 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
922 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +0200923 }
Johannes Bergaa430da2012-05-16 23:50:18 +0200924 if (dev->ops->set_channel || dev->ops->start_ap) {
925 i++;
926 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
927 goto nla_put_failure;
928 }
Bill Jordane8347eb2010-10-01 13:54:28 -0400929 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300930 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
931 CMD(tdls_mgmt, TDLS_MGMT);
932 CMD(tdls_oper, TDLS_OPER);
933 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300934 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
935 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100936 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100937 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100938 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
939 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
941 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +0100942 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100943
Kalle Valo4745fc02011-11-17 19:06:10 +0200944#ifdef CONFIG_NL80211_TESTMODE
945 CMD(testmode_cmd, TESTMODE);
946#endif
947
Johannes Berg8fdc6212009-03-14 09:34:01 +0100948#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200949
Johannes Berg6829c872009-07-02 09:13:27 +0200950 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200951 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400952 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
953 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200954 }
955
Johannes Berg6829c872009-07-02 09:13:27 +0200956 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200957 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -0400958 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
959 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200960 }
961
Johannes Berg8fdc6212009-03-14 09:34:01 +0100962 nla_nest_end(msg, nl_cmds);
963
Johannes Berg7c4ef712011-11-18 15:33:48 +0100964 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -0400965 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
966 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
967 dev->wiphy.max_remain_on_channel_duration))
968 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +0100969
David S. Miller9360ffd2012-03-29 04:41:26 -0400970 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
971 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
972 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100973
Johannes Berg2e161f72010-08-12 15:38:38 +0200974 if (mgmt_stypes) {
975 u16 stypes;
976 struct nlattr *nl_ftypes, *nl_ifs;
977 enum nl80211_iftype ift;
978
979 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
980 if (!nl_ifs)
981 goto nla_put_failure;
982
983 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
984 nl_ftypes = nla_nest_start(msg, ift);
985 if (!nl_ftypes)
986 goto nla_put_failure;
987 i = 0;
988 stypes = mgmt_stypes[ift].tx;
989 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400990 if ((stypes & 1) &&
991 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
992 (i << 4) | IEEE80211_FTYPE_MGMT))
993 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +0200994 stypes >>= 1;
995 i++;
996 }
997 nla_nest_end(msg, nl_ftypes);
998 }
999
Johannes Berg74b70a42010-08-24 12:15:53 +02001000 nla_nest_end(msg, nl_ifs);
1001
Johannes Berg2e161f72010-08-12 15:38:38 +02001002 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1003 if (!nl_ifs)
1004 goto nla_put_failure;
1005
1006 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1007 nl_ftypes = nla_nest_start(msg, ift);
1008 if (!nl_ftypes)
1009 goto nla_put_failure;
1010 i = 0;
1011 stypes = mgmt_stypes[ift].rx;
1012 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001013 if ((stypes & 1) &&
1014 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1015 (i << 4) | IEEE80211_FTYPE_MGMT))
1016 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001017 stypes >>= 1;
1018 i++;
1019 }
1020 nla_nest_end(msg, nl_ftypes);
1021 }
1022 nla_nest_end(msg, nl_ifs);
1023 }
1024
Johannes Bergff1b6e62011-05-04 15:37:28 +02001025 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1026 struct nlattr *nl_wowlan;
1027
1028 nl_wowlan = nla_nest_start(msg,
1029 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1030 if (!nl_wowlan)
1031 goto nla_put_failure;
1032
David S. Miller9360ffd2012-03-29 04:41:26 -04001033 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1034 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1035 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1036 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1037 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1038 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1039 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1040 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1041 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1042 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1043 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1044 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1045 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1046 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1047 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1048 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1049 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001050 if (dev->wiphy.wowlan.n_patterns) {
1051 struct nl80211_wowlan_pattern_support pat = {
1052 .max_patterns = dev->wiphy.wowlan.n_patterns,
1053 .min_pattern_len =
1054 dev->wiphy.wowlan.pattern_min_len,
1055 .max_pattern_len =
1056 dev->wiphy.wowlan.pattern_max_len,
1057 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001058 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1059 sizeof(pat), &pat))
1060 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001061 }
1062
1063 nla_nest_end(msg, nl_wowlan);
1064 }
1065
Johannes Berg7527a782011-05-13 10:58:57 +02001066 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1067 dev->wiphy.software_iftypes))
1068 goto nla_put_failure;
1069
1070 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1071 goto nla_put_failure;
1072
David S. Miller9360ffd2012-03-29 04:41:26 -04001073 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1074 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1075 dev->wiphy.ap_sme_capa))
1076 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001077
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1079 dev->wiphy.features))
1080 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001081
David S. Miller9360ffd2012-03-29 04:41:26 -04001082 if (dev->wiphy.ht_capa_mod_mask &&
1083 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1084 sizeof(*dev->wiphy.ht_capa_mod_mask),
1085 dev->wiphy.ht_capa_mod_mask))
1086 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001087
Johannes Berg55682962007-09-20 13:09:35 -04001088 return genlmsg_end(msg, hdr);
1089
1090 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001091 genlmsg_cancel(msg, hdr);
1092 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001093}
1094
1095static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1096{
1097 int idx = 0;
1098 int start = cb->args[0];
1099 struct cfg80211_registered_device *dev;
1100
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001101 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001102 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001103 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1104 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001105 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001106 continue;
1107 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1108 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001109 dev) < 0) {
1110 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001111 break;
Julius Volzb4637272008-07-08 14:02:19 +02001112 }
Johannes Berg55682962007-09-20 13:09:35 -04001113 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001114 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001115
1116 cb->args[0] = idx;
1117
1118 return skb->len;
1119}
1120
1121static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1122{
1123 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001124 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001125
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001126 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001127 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001128 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001129
Johannes Berg4c476992010-10-04 21:36:35 +02001130 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1131 nlmsg_free(msg);
1132 return -ENOBUFS;
1133 }
Johannes Berg55682962007-09-20 13:09:35 -04001134
Johannes Berg134e6372009-07-10 09:51:34 +00001135 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001136}
1137
Jouni Malinen31888482008-10-30 16:59:24 +02001138static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1139 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1140 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1141 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1142 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1143 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1144};
1145
1146static int parse_txq_params(struct nlattr *tb[],
1147 struct ieee80211_txq_params *txq_params)
1148{
Johannes Berga3304b02012-03-28 11:04:24 +02001149 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001150 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1151 !tb[NL80211_TXQ_ATTR_AIFS])
1152 return -EINVAL;
1153
Johannes Berga3304b02012-03-28 11:04:24 +02001154 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001155 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1156 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1157 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1158 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1159
Johannes Berga3304b02012-03-28 11:04:24 +02001160 if (txq_params->ac >= NL80211_NUM_ACS)
1161 return -EINVAL;
1162
Jouni Malinen31888482008-10-30 16:59:24 +02001163 return 0;
1164}
1165
Johannes Bergf444de02010-05-05 15:25:02 +02001166static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1167{
1168 /*
Johannes Berg685d12a2012-05-16 23:50:19 +02001169 * You can only set the channel explicitly for AP and
1170 * mesh type interfaces; all others have their channel
Johannes Bergf444de02010-05-05 15:25:02 +02001171 * managed via their respective "establish a connection"
1172 * command (connect, join, ...)
1173 *
1174 * Monitors are special as they are normally slaved to
1175 * whatever else is going on, so they behave as though
1176 * you tried setting the wiphy channel itself.
Johannes Bergaa430da2012-05-16 23:50:18 +02001177 *
1178 * For AP/GO modes, it's only for compatibility, you can
1179 * also give the channel to the start-AP command.
Johannes Bergf444de02010-05-05 15:25:02 +02001180 */
1181 return !wdev ||
1182 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001183 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001184 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1185 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001186}
1187
Johannes Bergcd6c6592012-05-10 21:27:18 +02001188static bool nl80211_valid_channel_type(struct genl_info *info,
1189 enum nl80211_channel_type *channel_type)
1190{
1191 enum nl80211_channel_type tmp;
1192
1193 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1194 return false;
1195
1196 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1197 if (tmp != NL80211_CHAN_NO_HT &&
1198 tmp != NL80211_CHAN_HT20 &&
1199 tmp != NL80211_CHAN_HT40PLUS &&
1200 tmp != NL80211_CHAN_HT40MINUS)
1201 return false;
1202
1203 if (channel_type)
1204 *channel_type = tmp;
1205
1206 return true;
1207}
1208
Johannes Bergf444de02010-05-05 15:25:02 +02001209static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1210 struct wireless_dev *wdev,
1211 struct genl_info *info)
1212{
Johannes Bergaa430da2012-05-16 23:50:18 +02001213 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001214 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1215 u32 freq;
1216 int result;
1217
1218 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1219 return -EINVAL;
1220
1221 if (!nl80211_can_set_dev_channel(wdev))
1222 return -EOPNOTSUPP;
1223
Johannes Bergcd6c6592012-05-10 21:27:18 +02001224 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1225 !nl80211_valid_channel_type(info, &channel_type))
1226 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001227
1228 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1229
1230 mutex_lock(&rdev->devlist_mtx);
Johannes Bergaa430da2012-05-16 23:50:18 +02001231 if (wdev) switch (wdev->iftype) {
1232 case NL80211_IFTYPE_AP:
1233 case NL80211_IFTYPE_P2P_GO:
1234 if (wdev->beacon_interval) {
1235 result = -EBUSY;
1236 break;
1237 }
1238 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1239 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1240 channel,
1241 channel_type)) {
1242 result = -EINVAL;
1243 break;
1244 }
1245 wdev->preset_chan = channel;
1246 wdev->preset_chantype = channel_type;
1247 result = 0;
1248 break;
1249 default:
Johannes Bergf444de02010-05-05 15:25:02 +02001250 wdev_lock(wdev);
1251 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1252 wdev_unlock(wdev);
1253 } else {
1254 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1255 }
1256 mutex_unlock(&rdev->devlist_mtx);
1257
1258 return result;
1259}
1260
1261static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1262{
Johannes Berg4c476992010-10-04 21:36:35 +02001263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1264 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001265
Johannes Berg4c476992010-10-04 21:36:35 +02001266 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001267}
1268
Bill Jordane8347eb2010-10-01 13:54:28 -04001269static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1270{
Johannes Berg43b19952010-10-07 13:10:30 +02001271 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1272 struct net_device *dev = info->user_ptr[1];
1273 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001274 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001275
1276 if (!info->attrs[NL80211_ATTR_MAC])
1277 return -EINVAL;
1278
Johannes Berg43b19952010-10-07 13:10:30 +02001279 if (netif_running(dev))
1280 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001281
Johannes Berg43b19952010-10-07 13:10:30 +02001282 if (!rdev->ops->set_wds_peer)
1283 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001284
Johannes Berg43b19952010-10-07 13:10:30 +02001285 if (wdev->iftype != NL80211_IFTYPE_WDS)
1286 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001287
1288 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001289 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001290}
1291
1292
Johannes Berg55682962007-09-20 13:09:35 -04001293static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1294{
1295 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001296 struct net_device *netdev = NULL;
1297 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001298 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001299 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001300 u32 changed;
1301 u8 retry_short = 0, retry_long = 0;
1302 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001303 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001304
Johannes Bergf444de02010-05-05 15:25:02 +02001305 /*
1306 * Try to find the wiphy and netdev. Normally this
1307 * function shouldn't need the netdev, but this is
1308 * done for backward compatibility -- previously
1309 * setting the channel was done per wiphy, but now
1310 * it is per netdev. Previous userland like hostapd
1311 * also passed a netdev to set_wiphy, so that it is
1312 * possible to let that go to the right netdev!
1313 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001314 mutex_lock(&cfg80211_mutex);
1315
Johannes Bergf444de02010-05-05 15:25:02 +02001316 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1317 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1318
1319 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1320 if (netdev && netdev->ieee80211_ptr) {
1321 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1322 mutex_lock(&rdev->mtx);
1323 } else
1324 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001325 }
1326
Johannes Bergf444de02010-05-05 15:25:02 +02001327 if (!netdev) {
1328 rdev = __cfg80211_rdev_from_info(info);
1329 if (IS_ERR(rdev)) {
1330 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001331 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001332 }
1333 wdev = NULL;
1334 netdev = NULL;
1335 result = 0;
1336
1337 mutex_lock(&rdev->mtx);
1338 } else if (netif_running(netdev) &&
1339 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1340 wdev = netdev->ieee80211_ptr;
1341 else
1342 wdev = NULL;
1343
1344 /*
1345 * end workaround code, by now the rdev is available
1346 * and locked, and wdev may or may not be NULL.
1347 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001348
1349 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001350 result = cfg80211_dev_rename(
1351 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001352
1353 mutex_unlock(&cfg80211_mutex);
1354
1355 if (result)
1356 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001357
Jouni Malinen31888482008-10-30 16:59:24 +02001358 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1359 struct ieee80211_txq_params txq_params;
1360 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1361
1362 if (!rdev->ops->set_txq_params) {
1363 result = -EOPNOTSUPP;
1364 goto bad_res;
1365 }
1366
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001367 if (!netdev) {
1368 result = -EINVAL;
1369 goto bad_res;
1370 }
1371
Johannes Berg133a3ff2011-11-03 14:50:13 +01001372 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1373 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1374 result = -EINVAL;
1375 goto bad_res;
1376 }
1377
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001378 if (!netif_running(netdev)) {
1379 result = -ENETDOWN;
1380 goto bad_res;
1381 }
1382
Jouni Malinen31888482008-10-30 16:59:24 +02001383 nla_for_each_nested(nl_txq_params,
1384 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1385 rem_txq_params) {
1386 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1387 nla_data(nl_txq_params),
1388 nla_len(nl_txq_params),
1389 txq_params_policy);
1390 result = parse_txq_params(tb, &txq_params);
1391 if (result)
1392 goto bad_res;
1393
1394 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001395 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001396 &txq_params);
1397 if (result)
1398 goto bad_res;
1399 }
1400 }
1401
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001402 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001403 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001404 if (result)
1405 goto bad_res;
1406 }
1407
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001408 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1409 enum nl80211_tx_power_setting type;
1410 int idx, mbm = 0;
1411
1412 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001413 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001414 goto bad_res;
1415 }
1416
1417 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1418 type = nla_get_u32(info->attrs[idx]);
1419
1420 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1421 (type != NL80211_TX_POWER_AUTOMATIC)) {
1422 result = -EINVAL;
1423 goto bad_res;
1424 }
1425
1426 if (type != NL80211_TX_POWER_AUTOMATIC) {
1427 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1428 mbm = nla_get_u32(info->attrs[idx]);
1429 }
1430
1431 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1432 if (result)
1433 goto bad_res;
1434 }
1435
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001436 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1437 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1438 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001439 if ((!rdev->wiphy.available_antennas_tx &&
1440 !rdev->wiphy.available_antennas_rx) ||
1441 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001442 result = -EOPNOTSUPP;
1443 goto bad_res;
1444 }
1445
1446 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1447 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1448
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001449 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001450 * available antenna masks, except for the "all" mask */
1451 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1452 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001453 result = -EINVAL;
1454 goto bad_res;
1455 }
1456
Bruno Randolf7f531e02010-12-16 11:30:22 +09001457 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1458 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001459
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001460 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1461 if (result)
1462 goto bad_res;
1463 }
1464
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001465 changed = 0;
1466
1467 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1468 retry_short = nla_get_u8(
1469 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1470 if (retry_short == 0) {
1471 result = -EINVAL;
1472 goto bad_res;
1473 }
1474 changed |= WIPHY_PARAM_RETRY_SHORT;
1475 }
1476
1477 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1478 retry_long = nla_get_u8(
1479 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1480 if (retry_long == 0) {
1481 result = -EINVAL;
1482 goto bad_res;
1483 }
1484 changed |= WIPHY_PARAM_RETRY_LONG;
1485 }
1486
1487 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1488 frag_threshold = nla_get_u32(
1489 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1490 if (frag_threshold < 256) {
1491 result = -EINVAL;
1492 goto bad_res;
1493 }
1494 if (frag_threshold != (u32) -1) {
1495 /*
1496 * Fragments (apart from the last one) are required to
1497 * have even length. Make the fragmentation code
1498 * simpler by stripping LSB should someone try to use
1499 * odd threshold value.
1500 */
1501 frag_threshold &= ~0x1;
1502 }
1503 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1504 }
1505
1506 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1507 rts_threshold = nla_get_u32(
1508 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1509 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1510 }
1511
Lukáš Turek81077e82009-12-21 22:50:47 +01001512 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1513 coverage_class = nla_get_u8(
1514 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1515 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1516 }
1517
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001518 if (changed) {
1519 u8 old_retry_short, old_retry_long;
1520 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001521 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001522
1523 if (!rdev->ops->set_wiphy_params) {
1524 result = -EOPNOTSUPP;
1525 goto bad_res;
1526 }
1527
1528 old_retry_short = rdev->wiphy.retry_short;
1529 old_retry_long = rdev->wiphy.retry_long;
1530 old_frag_threshold = rdev->wiphy.frag_threshold;
1531 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001532 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001533
1534 if (changed & WIPHY_PARAM_RETRY_SHORT)
1535 rdev->wiphy.retry_short = retry_short;
1536 if (changed & WIPHY_PARAM_RETRY_LONG)
1537 rdev->wiphy.retry_long = retry_long;
1538 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1539 rdev->wiphy.frag_threshold = frag_threshold;
1540 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1541 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001542 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1543 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001544
1545 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1546 if (result) {
1547 rdev->wiphy.retry_short = old_retry_short;
1548 rdev->wiphy.retry_long = old_retry_long;
1549 rdev->wiphy.frag_threshold = old_frag_threshold;
1550 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001551 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001552 }
1553 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001554
Johannes Berg306d6112008-12-08 12:39:04 +01001555 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001556 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001557 if (netdev)
1558 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001559 return result;
1560}
1561
1562
1563static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001564 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001565 struct net_device *dev)
1566{
1567 void *hdr;
1568
1569 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1570 if (!hdr)
1571 return -1;
1572
David S. Miller9360ffd2012-03-29 04:41:26 -04001573 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1574 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1575 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1576 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1577 dev->ieee80211_ptr->iftype) ||
1578 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1579 rdev->devlist_generation ^
1580 (cfg80211_rdev_list_generation << 2)))
1581 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001582
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001583 if (rdev->ops->get_channel) {
1584 struct ieee80211_channel *chan;
1585 enum nl80211_channel_type channel_type;
1586
1587 chan = rdev->ops->get_channel(&rdev->wiphy, &channel_type);
John W. Linville59ef43e2012-04-18 14:17:13 -04001588 if (chan &&
1589 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1590 chan->center_freq) ||
1591 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1592 channel_type)))
1593 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001594 }
1595
Johannes Berg55682962007-09-20 13:09:35 -04001596 return genlmsg_end(msg, hdr);
1597
1598 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001599 genlmsg_cancel(msg, hdr);
1600 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001601}
1602
1603static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1604{
1605 int wp_idx = 0;
1606 int if_idx = 0;
1607 int wp_start = cb->args[0];
1608 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001609 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001610 struct wireless_dev *wdev;
1611
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001612 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001613 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1614 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001615 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001616 if (wp_idx < wp_start) {
1617 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001618 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001619 }
Johannes Berg55682962007-09-20 13:09:35 -04001620 if_idx = 0;
1621
Johannes Bergf5ea9122009-08-07 16:17:38 +02001622 mutex_lock(&rdev->devlist_mtx);
1623 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001624 if (if_idx < if_start) {
1625 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001626 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001627 }
Johannes Berg55682962007-09-20 13:09:35 -04001628 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1629 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001630 rdev, wdev->netdev) < 0) {
1631 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001632 goto out;
1633 }
1634 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001635 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001636 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001637
1638 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001639 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001640 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001641 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001642
1643 cb->args[0] = wp_idx;
1644 cb->args[1] = if_idx;
1645
1646 return skb->len;
1647}
1648
1649static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1650{
1651 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001652 struct cfg80211_registered_device *dev = info->user_ptr[0];
1653 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001654
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001656 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001657 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001658
Johannes Bergd7264052009-04-19 16:23:20 +02001659 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001660 dev, netdev) < 0) {
1661 nlmsg_free(msg);
1662 return -ENOBUFS;
1663 }
Johannes Berg55682962007-09-20 13:09:35 -04001664
Johannes Berg134e6372009-07-10 09:51:34 +00001665 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001666}
1667
Michael Wu66f7ac52008-01-31 19:48:22 +01001668static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1669 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1670 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1671 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1672 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1673 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1674};
1675
1676static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1677{
1678 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1679 int flag;
1680
1681 *mntrflags = 0;
1682
1683 if (!nla)
1684 return -EINVAL;
1685
1686 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1687 nla, mntr_flags_policy))
1688 return -EINVAL;
1689
1690 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1691 if (flags[flag])
1692 *mntrflags |= (1<<flag);
1693
1694 return 0;
1695}
1696
Johannes Berg9bc383d2009-11-19 11:55:19 +01001697static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001698 struct net_device *netdev, u8 use_4addr,
1699 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001700{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001701 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001702 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001703 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001704 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001705 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001706
1707 switch (iftype) {
1708 case NL80211_IFTYPE_AP_VLAN:
1709 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1710 return 0;
1711 break;
1712 case NL80211_IFTYPE_STATION:
1713 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1714 return 0;
1715 break;
1716 default:
1717 break;
1718 }
1719
1720 return -EOPNOTSUPP;
1721}
1722
Johannes Berg55682962007-09-20 13:09:35 -04001723static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1724{
Johannes Berg4c476992010-10-04 21:36:35 +02001725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001726 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001727 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001728 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001729 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001730 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001731 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001732
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001733 memset(&params, 0, sizeof(params));
1734
Johannes Berg04a773a2009-04-19 21:24:32 +02001735 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001736
Johannes Berg723b0382008-09-16 20:22:09 +02001737 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001738 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001739 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001740 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001741 if (ntype > NL80211_IFTYPE_MAX)
1742 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001743 }
1744
Johannes Berg92ffe052008-09-16 20:39:36 +02001745 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001746 struct wireless_dev *wdev = dev->ieee80211_ptr;
1747
Johannes Berg4c476992010-10-04 21:36:35 +02001748 if (ntype != NL80211_IFTYPE_MESH_POINT)
1749 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001750 if (netif_running(dev))
1751 return -EBUSY;
1752
1753 wdev_lock(wdev);
1754 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1755 IEEE80211_MAX_MESH_ID_LEN);
1756 wdev->mesh_id_up_len =
1757 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1758 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1759 wdev->mesh_id_up_len);
1760 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001761 }
1762
Felix Fietkau8b787642009-11-10 18:53:10 +01001763 if (info->attrs[NL80211_ATTR_4ADDR]) {
1764 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1765 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001766 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001767 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001768 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001769 } else {
1770 params.use_4addr = -1;
1771 }
1772
Johannes Berg92ffe052008-09-16 20:39:36 +02001773 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001774 if (ntype != NL80211_IFTYPE_MONITOR)
1775 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001776 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1777 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001778 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001779 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001780
1781 flags = &_flags;
1782 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001783 }
Johannes Berg3b858752009-03-12 09:55:09 +01001784
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001785 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001786 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001787 else
1788 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001789
Johannes Berg9bc383d2009-11-19 11:55:19 +01001790 if (!err && params.use_4addr != -1)
1791 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1792
Johannes Berg55682962007-09-20 13:09:35 -04001793 return err;
1794}
1795
1796static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1797{
Johannes Berg4c476992010-10-04 21:36:35 +02001798 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001799 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001800 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001801 int err;
1802 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001803 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001804
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001805 memset(&params, 0, sizeof(params));
1806
Johannes Berg55682962007-09-20 13:09:35 -04001807 if (!info->attrs[NL80211_ATTR_IFNAME])
1808 return -EINVAL;
1809
1810 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1811 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1812 if (type > NL80211_IFTYPE_MAX)
1813 return -EINVAL;
1814 }
1815
Johannes Berg79c97e92009-07-07 03:56:12 +02001816 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001817 !(rdev->wiphy.interface_modes & (1 << type)))
1818 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001819
Johannes Berg9bc383d2009-11-19 11:55:19 +01001820 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001821 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001822 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001823 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001824 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001825 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001826
Michael Wu66f7ac52008-01-31 19:48:22 +01001827 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1828 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1829 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001830 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001831 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001832 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001833 if (IS_ERR(dev))
1834 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001835
Johannes Berg29cbe682010-12-03 09:20:44 +01001836 if (type == NL80211_IFTYPE_MESH_POINT &&
1837 info->attrs[NL80211_ATTR_MESH_ID]) {
1838 struct wireless_dev *wdev = dev->ieee80211_ptr;
1839
1840 wdev_lock(wdev);
1841 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1842 IEEE80211_MAX_MESH_ID_LEN);
1843 wdev->mesh_id_up_len =
1844 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1845 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1846 wdev->mesh_id_up_len);
1847 wdev_unlock(wdev);
1848 }
1849
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001850 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001851}
1852
1853static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1854{
Johannes Berg4c476992010-10-04 21:36:35 +02001855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1856 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001857
Johannes Berg4c476992010-10-04 21:36:35 +02001858 if (!rdev->ops->del_virtual_intf)
1859 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001860
Johannes Berg4c476992010-10-04 21:36:35 +02001861 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001862}
1863
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001864static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1865{
1866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1867 struct net_device *dev = info->user_ptr[1];
1868 u16 noack_map;
1869
1870 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1871 return -EINVAL;
1872
1873 if (!rdev->ops->set_noack_map)
1874 return -EOPNOTSUPP;
1875
1876 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1877
1878 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1879}
1880
Johannes Berg41ade002007-12-19 02:03:29 +01001881struct get_key_cookie {
1882 struct sk_buff *msg;
1883 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001884 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001885};
1886
1887static void get_key_callback(void *c, struct key_params *params)
1888{
Johannes Bergb9454e82009-07-08 13:29:08 +02001889 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001890 struct get_key_cookie *cookie = c;
1891
David S. Miller9360ffd2012-03-29 04:41:26 -04001892 if ((params->key &&
1893 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1894 params->key_len, params->key)) ||
1895 (params->seq &&
1896 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1897 params->seq_len, params->seq)) ||
1898 (params->cipher &&
1899 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1900 params->cipher)))
1901 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001902
Johannes Bergb9454e82009-07-08 13:29:08 +02001903 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1904 if (!key)
1905 goto nla_put_failure;
1906
David S. Miller9360ffd2012-03-29 04:41:26 -04001907 if ((params->key &&
1908 nla_put(cookie->msg, NL80211_KEY_DATA,
1909 params->key_len, params->key)) ||
1910 (params->seq &&
1911 nla_put(cookie->msg, NL80211_KEY_SEQ,
1912 params->seq_len, params->seq)) ||
1913 (params->cipher &&
1914 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
1915 params->cipher)))
1916 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001917
David S. Miller9360ffd2012-03-29 04:41:26 -04001918 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
1919 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001920
1921 nla_nest_end(cookie->msg, key);
1922
Johannes Berg41ade002007-12-19 02:03:29 +01001923 return;
1924 nla_put_failure:
1925 cookie->error = 1;
1926}
1927
1928static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1929{
Johannes Berg4c476992010-10-04 21:36:35 +02001930 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001931 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001932 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001933 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001934 const u8 *mac_addr = NULL;
1935 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001936 struct get_key_cookie cookie = {
1937 .error = 0,
1938 };
1939 void *hdr;
1940 struct sk_buff *msg;
1941
1942 if (info->attrs[NL80211_ATTR_KEY_IDX])
1943 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1944
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001945 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001946 return -EINVAL;
1947
1948 if (info->attrs[NL80211_ATTR_MAC])
1949 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1950
Johannes Berge31b8212010-10-05 19:39:30 +02001951 pairwise = !!mac_addr;
1952 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1953 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1954 if (kt >= NUM_NL80211_KEYTYPES)
1955 return -EINVAL;
1956 if (kt != NL80211_KEYTYPE_GROUP &&
1957 kt != NL80211_KEYTYPE_PAIRWISE)
1958 return -EINVAL;
1959 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1960 }
1961
Johannes Berg4c476992010-10-04 21:36:35 +02001962 if (!rdev->ops->get_key)
1963 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001964
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001965 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001966 if (!msg)
1967 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001968
1969 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1970 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001971 if (IS_ERR(hdr))
1972 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001973
1974 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001975 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001976
David S. Miller9360ffd2012-03-29 04:41:26 -04001977 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1978 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
1979 goto nla_put_failure;
1980 if (mac_addr &&
1981 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
1982 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001983
Johannes Berge31b8212010-10-05 19:39:30 +02001984 if (pairwise && mac_addr &&
1985 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1986 return -ENOENT;
1987
1988 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1989 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001990
1991 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001992 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001993
1994 if (cookie.error)
1995 goto nla_put_failure;
1996
1997 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001998 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001999
2000 nla_put_failure:
2001 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002002 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002003 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002004 return err;
2005}
2006
2007static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2008{
Johannes Berg4c476992010-10-04 21:36:35 +02002009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002010 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002011 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002012 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002013
Johannes Bergb9454e82009-07-08 13:29:08 +02002014 err = nl80211_parse_key(info, &key);
2015 if (err)
2016 return err;
2017
2018 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002019 return -EINVAL;
2020
Johannes Bergb9454e82009-07-08 13:29:08 +02002021 /* only support setting default key */
2022 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002023 return -EINVAL;
2024
Johannes Bergfffd0932009-07-08 14:22:54 +02002025 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002026
2027 if (key.def) {
2028 if (!rdev->ops->set_default_key) {
2029 err = -EOPNOTSUPP;
2030 goto out;
2031 }
2032
2033 err = nl80211_key_allowed(dev->ieee80211_ptr);
2034 if (err)
2035 goto out;
2036
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002037 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2038 key.def_uni, key.def_multi);
2039
2040 if (err)
2041 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002042
Johannes Berg3d23e342009-09-29 23:27:28 +02002043#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002044 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002045#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002046 } else {
2047 if (key.def_uni || !key.def_multi) {
2048 err = -EINVAL;
2049 goto out;
2050 }
2051
2052 if (!rdev->ops->set_default_mgmt_key) {
2053 err = -EOPNOTSUPP;
2054 goto out;
2055 }
2056
2057 err = nl80211_key_allowed(dev->ieee80211_ptr);
2058 if (err)
2059 goto out;
2060
2061 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2062 dev, key.idx);
2063 if (err)
2064 goto out;
2065
2066#ifdef CONFIG_CFG80211_WEXT
2067 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2068#endif
2069 }
2070
2071 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002072 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002073
Johannes Berg41ade002007-12-19 02:03:29 +01002074 return err;
2075}
2076
2077static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2078{
Johannes Berg4c476992010-10-04 21:36:35 +02002079 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002080 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002081 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002082 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002083 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002084
Johannes Bergb9454e82009-07-08 13:29:08 +02002085 err = nl80211_parse_key(info, &key);
2086 if (err)
2087 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002088
Johannes Bergb9454e82009-07-08 13:29:08 +02002089 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002090 return -EINVAL;
2091
Johannes Berg41ade002007-12-19 02:03:29 +01002092 if (info->attrs[NL80211_ATTR_MAC])
2093 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2094
Johannes Berge31b8212010-10-05 19:39:30 +02002095 if (key.type == -1) {
2096 if (mac_addr)
2097 key.type = NL80211_KEYTYPE_PAIRWISE;
2098 else
2099 key.type = NL80211_KEYTYPE_GROUP;
2100 }
2101
2102 /* for now */
2103 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2104 key.type != NL80211_KEYTYPE_GROUP)
2105 return -EINVAL;
2106
Johannes Berg4c476992010-10-04 21:36:35 +02002107 if (!rdev->ops->add_key)
2108 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002109
Johannes Berge31b8212010-10-05 19:39:30 +02002110 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2111 key.type == NL80211_KEYTYPE_PAIRWISE,
2112 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002113 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002114
2115 wdev_lock(dev->ieee80211_ptr);
2116 err = nl80211_key_allowed(dev->ieee80211_ptr);
2117 if (!err)
2118 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002119 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002120 mac_addr, &key.p);
2121 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002122
Johannes Berg41ade002007-12-19 02:03:29 +01002123 return err;
2124}
2125
2126static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2127{
Johannes Berg4c476992010-10-04 21:36:35 +02002128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002129 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002130 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002131 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002132 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002133
Johannes Bergb9454e82009-07-08 13:29:08 +02002134 err = nl80211_parse_key(info, &key);
2135 if (err)
2136 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002137
2138 if (info->attrs[NL80211_ATTR_MAC])
2139 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2140
Johannes Berge31b8212010-10-05 19:39:30 +02002141 if (key.type == -1) {
2142 if (mac_addr)
2143 key.type = NL80211_KEYTYPE_PAIRWISE;
2144 else
2145 key.type = NL80211_KEYTYPE_GROUP;
2146 }
2147
2148 /* for now */
2149 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2150 key.type != NL80211_KEYTYPE_GROUP)
2151 return -EINVAL;
2152
Johannes Berg4c476992010-10-04 21:36:35 +02002153 if (!rdev->ops->del_key)
2154 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002155
Johannes Bergfffd0932009-07-08 14:22:54 +02002156 wdev_lock(dev->ieee80211_ptr);
2157 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002158
2159 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2160 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2161 err = -ENOENT;
2162
Johannes Bergfffd0932009-07-08 14:22:54 +02002163 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002164 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2165 key.type == NL80211_KEYTYPE_PAIRWISE,
2166 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002167
Johannes Berg3d23e342009-09-29 23:27:28 +02002168#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002169 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002170 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002171 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002172 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002173 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2174 }
2175#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002176 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002177
Johannes Berg41ade002007-12-19 02:03:29 +01002178 return err;
2179}
2180
Johannes Berg88600202012-02-13 15:17:18 +01002181static int nl80211_parse_beacon(struct genl_info *info,
2182 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002183{
Johannes Berg88600202012-02-13 15:17:18 +01002184 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002185
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002186 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2187 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2188 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2189 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002190 return -EINVAL;
2191
Johannes Berg88600202012-02-13 15:17:18 +01002192 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002193
Johannes Berged1b6cc2007-12-19 02:03:32 +01002194 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002195 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2196 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2197 if (!bcn->head_len)
2198 return -EINVAL;
2199 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002200 }
2201
2202 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002203 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2204 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002205 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002206 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002207 }
2208
Johannes Berg4c476992010-10-04 21:36:35 +02002209 if (!haveinfo)
2210 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002211
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002212 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002213 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2214 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002215 }
2216
2217 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002218 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002219 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002220 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002221 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2222 }
2223
2224 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002225 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002226 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002227 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002228 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2229 }
2230
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002231 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002232 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002233 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002234 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002235 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2236 }
2237
Johannes Berg88600202012-02-13 15:17:18 +01002238 return 0;
2239}
2240
2241static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2242{
2243 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2244 struct net_device *dev = info->user_ptr[1];
2245 struct wireless_dev *wdev = dev->ieee80211_ptr;
2246 struct cfg80211_ap_settings params;
2247 int err;
2248
2249 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2250 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2251 return -EOPNOTSUPP;
2252
2253 if (!rdev->ops->start_ap)
2254 return -EOPNOTSUPP;
2255
2256 if (wdev->beacon_interval)
2257 return -EALREADY;
2258
2259 memset(&params, 0, sizeof(params));
2260
2261 /* these are required for START_AP */
2262 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2263 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2264 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2265 return -EINVAL;
2266
2267 err = nl80211_parse_beacon(info, &params.beacon);
2268 if (err)
2269 return err;
2270
2271 params.beacon_interval =
2272 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2273 params.dtim_period =
2274 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2275
2276 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2277 if (err)
2278 return err;
2279
2280 /*
2281 * In theory, some of these attributes should be required here
2282 * but since they were not used when the command was originally
2283 * added, keep them optional for old user space programs to let
2284 * them continue to work with drivers that do not need the
2285 * additional information -- drivers must check!
2286 */
2287 if (info->attrs[NL80211_ATTR_SSID]) {
2288 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2289 params.ssid_len =
2290 nla_len(info->attrs[NL80211_ATTR_SSID]);
2291 if (params.ssid_len == 0 ||
2292 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2293 return -EINVAL;
2294 }
2295
2296 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2297 params.hidden_ssid = nla_get_u32(
2298 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2299 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2300 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2301 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2302 return -EINVAL;
2303 }
2304
2305 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2306
2307 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2308 params.auth_type = nla_get_u32(
2309 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2310 if (!nl80211_valid_auth_type(params.auth_type))
2311 return -EINVAL;
2312 } else
2313 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2314
2315 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2316 NL80211_MAX_NR_CIPHER_SUITES);
2317 if (err)
2318 return err;
2319
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302320 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2321 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2322 return -EOPNOTSUPP;
2323 params.inactivity_timeout = nla_get_u16(
2324 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2325 }
2326
Johannes Bergaa430da2012-05-16 23:50:18 +02002327 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2328 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2329
2330 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2331 !nl80211_valid_channel_type(info, &channel_type))
2332 return -EINVAL;
2333
2334 params.channel = rdev_freq_to_chan(rdev,
2335 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2336 channel_type);
2337 if (!params.channel)
2338 return -EINVAL;
2339 params.channel_type = channel_type;
2340 } else if (wdev->preset_chan) {
2341 params.channel = wdev->preset_chan;
2342 params.channel_type = wdev->preset_chantype;
2343 } else
2344 return -EINVAL;
2345
2346 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2347 params.channel_type))
2348 return -EINVAL;
2349
Johannes Berg88600202012-02-13 15:17:18 +01002350 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2351 if (!err)
2352 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002353 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002354}
2355
Johannes Berg88600202012-02-13 15:17:18 +01002356static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2357{
2358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2359 struct net_device *dev = info->user_ptr[1];
2360 struct wireless_dev *wdev = dev->ieee80211_ptr;
2361 struct cfg80211_beacon_data params;
2362 int err;
2363
2364 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2365 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2366 return -EOPNOTSUPP;
2367
2368 if (!rdev->ops->change_beacon)
2369 return -EOPNOTSUPP;
2370
2371 if (!wdev->beacon_interval)
2372 return -EINVAL;
2373
2374 err = nl80211_parse_beacon(info, &params);
2375 if (err)
2376 return err;
2377
2378 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2379}
2380
2381static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002382{
Johannes Berg4c476992010-10-04 21:36:35 +02002383 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2384 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002385 struct wireless_dev *wdev = dev->ieee80211_ptr;
2386 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002387
Johannes Berg88600202012-02-13 15:17:18 +01002388 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002389 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002390
Johannes Berg074ac8d2010-09-16 14:58:22 +02002391 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002392 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2393 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002394
Johannes Berg88600202012-02-13 15:17:18 +01002395 if (!wdev->beacon_interval)
2396 return -ENOENT;
2397
2398 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002399 if (!err)
2400 wdev->beacon_interval = 0;
2401 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002402}
2403
Johannes Berg5727ef12007-12-19 02:03:34 +01002404static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2405 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2406 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2407 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002408 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002409 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002410 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002411};
2412
Johannes Bergeccb8e82009-05-11 21:57:56 +03002413static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002414 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002415 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002416{
2417 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002418 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002419 int flag;
2420
Johannes Bergeccb8e82009-05-11 21:57:56 +03002421 /*
2422 * Try parsing the new attribute first so userspace
2423 * can specify both for older kernels.
2424 */
2425 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2426 if (nla) {
2427 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002428
Johannes Bergeccb8e82009-05-11 21:57:56 +03002429 sta_flags = nla_data(nla);
2430 params->sta_flags_mask = sta_flags->mask;
2431 params->sta_flags_set = sta_flags->set;
2432 if ((params->sta_flags_mask |
2433 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2434 return -EINVAL;
2435 return 0;
2436 }
2437
2438 /* if present, parse the old attribute */
2439
2440 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002441 if (!nla)
2442 return 0;
2443
2444 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2445 nla, sta_flags_policy))
2446 return -EINVAL;
2447
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002448 /*
2449 * Only allow certain flags for interface types so that
2450 * other attributes are silently ignored. Remember that
2451 * this is backward compatibility code with old userspace
2452 * and shouldn't be hit in other cases anyway.
2453 */
2454 switch (iftype) {
2455 case NL80211_IFTYPE_AP:
2456 case NL80211_IFTYPE_AP_VLAN:
2457 case NL80211_IFTYPE_P2P_GO:
2458 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2459 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2460 BIT(NL80211_STA_FLAG_WME) |
2461 BIT(NL80211_STA_FLAG_MFP);
2462 break;
2463 case NL80211_IFTYPE_P2P_CLIENT:
2464 case NL80211_IFTYPE_STATION:
2465 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2466 BIT(NL80211_STA_FLAG_TDLS_PEER);
2467 break;
2468 case NL80211_IFTYPE_MESH_POINT:
2469 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2470 BIT(NL80211_STA_FLAG_MFP) |
2471 BIT(NL80211_STA_FLAG_AUTHORIZED);
2472 default:
2473 return -EINVAL;
2474 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002475
Johannes Berg3383b5a2012-05-10 20:14:43 +02002476 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2477 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002478 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002479
Johannes Berg3383b5a2012-05-10 20:14:43 +02002480 /* no longer support new API additions in old API */
2481 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2482 return -EINVAL;
2483 }
2484 }
2485
Johannes Berg5727ef12007-12-19 02:03:34 +01002486 return 0;
2487}
2488
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002489static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2490 int attr)
2491{
2492 struct nlattr *rate;
2493 u16 bitrate;
2494
2495 rate = nla_nest_start(msg, attr);
2496 if (!rate)
2497 goto nla_put_failure;
2498
2499 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2500 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002501 if ((bitrate > 0 &&
2502 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2503 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2504 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2505 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2506 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2507 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2508 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2509 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002510
2511 nla_nest_end(msg, rate);
2512 return true;
2513
2514nla_put_failure:
2515 return false;
2516}
2517
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002518static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002519 int flags,
2520 struct cfg80211_registered_device *rdev,
2521 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002522 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002523{
2524 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002525 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002526
2527 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2528 if (!hdr)
2529 return -1;
2530
David S. Miller9360ffd2012-03-29 04:41:26 -04002531 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2532 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2533 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2534 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002535
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002536 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2537 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002538 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002539 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2540 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2541 sinfo->connected_time))
2542 goto nla_put_failure;
2543 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2544 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2545 sinfo->inactive_time))
2546 goto nla_put_failure;
2547 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2548 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2549 sinfo->rx_bytes))
2550 goto nla_put_failure;
2551 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2552 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2553 sinfo->tx_bytes))
2554 goto nla_put_failure;
2555 if ((sinfo->filled & STATION_INFO_LLID) &&
2556 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2557 goto nla_put_failure;
2558 if ((sinfo->filled & STATION_INFO_PLID) &&
2559 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2560 goto nla_put_failure;
2561 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2562 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2563 sinfo->plink_state))
2564 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002565 switch (rdev->wiphy.signal_type) {
2566 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002567 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2568 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2569 sinfo->signal))
2570 goto nla_put_failure;
2571 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2572 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2573 sinfo->signal_avg))
2574 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002575 break;
2576 default:
2577 break;
2578 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002579 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002580 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2581 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002582 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002583 }
2584 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2585 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2586 NL80211_STA_INFO_RX_BITRATE))
2587 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002588 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002589 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2590 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2591 sinfo->rx_packets))
2592 goto nla_put_failure;
2593 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2594 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2595 sinfo->tx_packets))
2596 goto nla_put_failure;
2597 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2598 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2599 sinfo->tx_retries))
2600 goto nla_put_failure;
2601 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2602 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2603 sinfo->tx_failed))
2604 goto nla_put_failure;
2605 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2606 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2607 sinfo->beacon_loss_count))
2608 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002609 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2610 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2611 if (!bss_param)
2612 goto nla_put_failure;
2613
David S. Miller9360ffd2012-03-29 04:41:26 -04002614 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2615 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2616 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2617 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2618 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2619 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2620 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2621 sinfo->bss_param.dtim_period) ||
2622 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2623 sinfo->bss_param.beacon_interval))
2624 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002625
2626 nla_nest_end(msg, bss_param);
2627 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002628 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2629 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2630 sizeof(struct nl80211_sta_flag_update),
2631 &sinfo->sta_flags))
2632 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002633 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2634 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2635 sinfo->t_offset))
2636 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002637 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002638
David S. Miller9360ffd2012-03-29 04:41:26 -04002639 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2640 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2641 sinfo->assoc_req_ies))
2642 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002643
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002644 return genlmsg_end(msg, hdr);
2645
2646 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002647 genlmsg_cancel(msg, hdr);
2648 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002649}
2650
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002651static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002652 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002653{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002654 struct station_info sinfo;
2655 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002656 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002657 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002658 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002659 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002660
Johannes Berg67748892010-10-04 21:14:06 +02002661 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2662 if (err)
2663 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002664
2665 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002666 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002667 goto out_err;
2668 }
2669
Johannes Bergbba95fe2008-07-29 13:22:51 +02002670 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002671 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002672 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2673 mac_addr, &sinfo);
2674 if (err == -ENOENT)
2675 break;
2676 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002677 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002678
2679 if (nl80211_send_station(skb,
2680 NETLINK_CB(cb->skb).pid,
2681 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002682 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002683 &sinfo) < 0)
2684 goto out;
2685
2686 sta_idx++;
2687 }
2688
2689
2690 out:
2691 cb->args[1] = sta_idx;
2692 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002693 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002694 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002695
2696 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002697}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002698
Johannes Berg5727ef12007-12-19 02:03:34 +01002699static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2700{
Johannes Berg4c476992010-10-04 21:36:35 +02002701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2702 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002703 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002704 struct sk_buff *msg;
2705 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002706 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002707
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002708 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002709
2710 if (!info->attrs[NL80211_ATTR_MAC])
2711 return -EINVAL;
2712
2713 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2714
Johannes Berg4c476992010-10-04 21:36:35 +02002715 if (!rdev->ops->get_station)
2716 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002717
Johannes Berg79c97e92009-07-07 03:56:12 +02002718 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002719 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002720 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002721
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002723 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002724 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002725
2726 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002727 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002728 nlmsg_free(msg);
2729 return -ENOBUFS;
2730 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002731
Johannes Berg4c476992010-10-04 21:36:35 +02002732 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002733}
2734
2735/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002736 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002737 */
Johannes Berg80b99892011-11-18 16:23:01 +01002738static struct net_device *get_vlan(struct genl_info *info,
2739 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002740{
Johannes Berg463d0182009-07-14 00:33:35 +02002741 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002742 struct net_device *v;
2743 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002744
Johannes Berg80b99892011-11-18 16:23:01 +01002745 if (!vlanattr)
2746 return NULL;
2747
2748 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2749 if (!v)
2750 return ERR_PTR(-ENODEV);
2751
2752 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2753 ret = -EINVAL;
2754 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002755 }
Johannes Berg80b99892011-11-18 16:23:01 +01002756
2757 if (!netif_running(v)) {
2758 ret = -ENETDOWN;
2759 goto error;
2760 }
2761
2762 return v;
2763 error:
2764 dev_put(v);
2765 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002766}
2767
2768static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2769{
Johannes Berg4c476992010-10-04 21:36:35 +02002770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002771 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002772 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002773 struct station_parameters params;
2774 u8 *mac_addr = NULL;
2775
2776 memset(&params, 0, sizeof(params));
2777
2778 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002779 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002780
2781 if (info->attrs[NL80211_ATTR_STA_AID])
2782 return -EINVAL;
2783
2784 if (!info->attrs[NL80211_ATTR_MAC])
2785 return -EINVAL;
2786
2787 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2788
2789 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2790 params.supported_rates =
2791 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2792 params.supported_rates_len =
2793 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2794 }
2795
2796 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2797 params.listen_interval =
2798 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2799
Jouni Malinen36aedc92008-08-25 11:58:58 +03002800 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2801 params.ht_capa =
2802 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2803
Johannes Bergbdd90d52011-12-14 12:20:27 +01002804 if (!rdev->ops->change_station)
2805 return -EOPNOTSUPP;
2806
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002807 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002808 return -EINVAL;
2809
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002810 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2811 params.plink_action =
2812 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2813
Javier Cardona9c3990a2011-05-03 16:57:11 -07002814 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2815 params.plink_state =
2816 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2817
Johannes Berga97f4422009-06-18 17:23:43 +02002818 switch (dev->ieee80211_ptr->iftype) {
2819 case NL80211_IFTYPE_AP:
2820 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002821 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002822 /* disallow mesh-specific things */
2823 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002824 return -EINVAL;
2825
2826 /* TDLS can't be set, ... */
2827 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2828 return -EINVAL;
2829 /*
2830 * ... but don't bother the driver with it. This works around
2831 * a hostapd/wpa_supplicant issue -- it always includes the
2832 * TLDS_PEER flag in the mask even for AP mode.
2833 */
2834 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2835
2836 /* accept only the listed bits */
2837 if (params.sta_flags_mask &
2838 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2839 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2840 BIT(NL80211_STA_FLAG_WME) |
2841 BIT(NL80211_STA_FLAG_MFP)))
2842 return -EINVAL;
2843
2844 /* must be last in here for error handling */
2845 params.vlan = get_vlan(info, rdev);
2846 if (IS_ERR(params.vlan))
2847 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002848 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002849 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002850 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002851 /*
2852 * Don't allow userspace to change the TDLS_PEER flag,
2853 * but silently ignore attempts to change it since we
2854 * don't have state here to verify that it doesn't try
2855 * to change the flag.
2856 */
2857 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002858 /* fall through */
2859 case NL80211_IFTYPE_ADHOC:
2860 /* disallow things sta doesn't support */
2861 if (params.plink_action)
2862 return -EINVAL;
2863 if (params.ht_capa)
2864 return -EINVAL;
2865 if (params.listen_interval >= 0)
2866 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002867 /* reject any changes other than AUTHORIZED */
2868 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2869 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002870 break;
2871 case NL80211_IFTYPE_MESH_POINT:
2872 /* disallow things mesh doesn't support */
2873 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002874 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002875 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002876 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002877 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002878 return -EINVAL;
2879 /*
2880 * No special handling for TDLS here -- the userspace
2881 * mesh code doesn't have this bug.
2882 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002883 if (params.sta_flags_mask &
2884 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002885 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002886 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002887 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002888 break;
2889 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002890 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002891 }
2892
Johannes Bergbdd90d52011-12-14 12:20:27 +01002893 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002894
Johannes Berg79c97e92009-07-07 03:56:12 +02002895 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002896
Johannes Berg5727ef12007-12-19 02:03:34 +01002897 if (params.vlan)
2898 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002899
Johannes Berg5727ef12007-12-19 02:03:34 +01002900 return err;
2901}
2902
Eliad Pellerc75786c2011-08-23 14:37:46 +03002903static struct nla_policy
2904nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2905 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2906 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2907};
2908
Johannes Berg5727ef12007-12-19 02:03:34 +01002909static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2910{
Johannes Berg4c476992010-10-04 21:36:35 +02002911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002912 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002913 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002914 struct station_parameters params;
2915 u8 *mac_addr = NULL;
2916
2917 memset(&params, 0, sizeof(params));
2918
2919 if (!info->attrs[NL80211_ATTR_MAC])
2920 return -EINVAL;
2921
Johannes Berg5727ef12007-12-19 02:03:34 +01002922 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2923 return -EINVAL;
2924
2925 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2926 return -EINVAL;
2927
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002928 if (!info->attrs[NL80211_ATTR_STA_AID])
2929 return -EINVAL;
2930
Johannes Berg5727ef12007-12-19 02:03:34 +01002931 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2932 params.supported_rates =
2933 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2934 params.supported_rates_len =
2935 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2936 params.listen_interval =
2937 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002938
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002939 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2940 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2941 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002942
Jouni Malinen36aedc92008-08-25 11:58:58 +03002943 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2944 params.ht_capa =
2945 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002946
Javier Cardona96b78df2011-04-07 15:08:33 -07002947 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2948 params.plink_action =
2949 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2950
Johannes Bergbdd90d52011-12-14 12:20:27 +01002951 if (!rdev->ops->add_station)
2952 return -EOPNOTSUPP;
2953
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002954 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002955 return -EINVAL;
2956
Johannes Bergbdd90d52011-12-14 12:20:27 +01002957 switch (dev->ieee80211_ptr->iftype) {
2958 case NL80211_IFTYPE_AP:
2959 case NL80211_IFTYPE_AP_VLAN:
2960 case NL80211_IFTYPE_P2P_GO:
2961 /* parse WME attributes if sta is WME capable */
2962 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2963 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2964 info->attrs[NL80211_ATTR_STA_WME]) {
2965 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2966 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002967
Johannes Bergbdd90d52011-12-14 12:20:27 +01002968 nla = info->attrs[NL80211_ATTR_STA_WME];
2969 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2970 nl80211_sta_wme_policy);
2971 if (err)
2972 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002973
Johannes Bergbdd90d52011-12-14 12:20:27 +01002974 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2975 params.uapsd_queues =
2976 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2977 if (params.uapsd_queues &
2978 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2979 return -EINVAL;
2980
2981 if (tb[NL80211_STA_WME_MAX_SP])
2982 params.max_sp =
2983 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2984
2985 if (params.max_sp &
2986 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2987 return -EINVAL;
2988
2989 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2990 }
2991 /* TDLS peers cannot be added */
2992 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002993 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002994 /* but don't bother the driver with it */
2995 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002996
Johannes Bergbdd90d52011-12-14 12:20:27 +01002997 /* must be last in here for error handling */
2998 params.vlan = get_vlan(info, rdev);
2999 if (IS_ERR(params.vlan))
3000 return PTR_ERR(params.vlan);
3001 break;
3002 case NL80211_IFTYPE_MESH_POINT:
3003 /* TDLS peers cannot be added */
3004 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003005 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003006 break;
3007 case NL80211_IFTYPE_STATION:
3008 /* Only TDLS peers can be added */
3009 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3010 return -EINVAL;
3011 /* Can only add if TDLS ... */
3012 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3013 return -EOPNOTSUPP;
3014 /* ... with external setup is supported */
3015 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3016 return -EOPNOTSUPP;
3017 break;
3018 default:
3019 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003020 }
3021
Johannes Bergbdd90d52011-12-14 12:20:27 +01003022 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003023
Johannes Berg79c97e92009-07-07 03:56:12 +02003024 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003025
Johannes Berg5727ef12007-12-19 02:03:34 +01003026 if (params.vlan)
3027 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003028 return err;
3029}
3030
3031static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3032{
Johannes Berg4c476992010-10-04 21:36:35 +02003033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3034 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003035 u8 *mac_addr = NULL;
3036
3037 if (info->attrs[NL80211_ATTR_MAC])
3038 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3039
Johannes Berge80cf852009-05-11 14:43:13 +02003040 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003041 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003042 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003043 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3044 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003045
Johannes Berg4c476992010-10-04 21:36:35 +02003046 if (!rdev->ops->del_station)
3047 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003048
Johannes Berg4c476992010-10-04 21:36:35 +02003049 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003050}
3051
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003052static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3053 int flags, struct net_device *dev,
3054 u8 *dst, u8 *next_hop,
3055 struct mpath_info *pinfo)
3056{
3057 void *hdr;
3058 struct nlattr *pinfoattr;
3059
3060 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3061 if (!hdr)
3062 return -1;
3063
David S. Miller9360ffd2012-03-29 04:41:26 -04003064 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3065 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3066 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3067 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3068 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003069
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003070 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3071 if (!pinfoattr)
3072 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003073 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3074 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3075 pinfo->frame_qlen))
3076 goto nla_put_failure;
3077 if (((pinfo->filled & MPATH_INFO_SN) &&
3078 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3079 ((pinfo->filled & MPATH_INFO_METRIC) &&
3080 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3081 pinfo->metric)) ||
3082 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3083 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3084 pinfo->exptime)) ||
3085 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3086 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3087 pinfo->flags)) ||
3088 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3089 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3090 pinfo->discovery_timeout)) ||
3091 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3092 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3093 pinfo->discovery_retries)))
3094 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003095
3096 nla_nest_end(msg, pinfoattr);
3097
3098 return genlmsg_end(msg, hdr);
3099
3100 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003101 genlmsg_cancel(msg, hdr);
3102 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003103}
3104
3105static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003106 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003107{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003108 struct mpath_info pinfo;
3109 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003110 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003111 u8 dst[ETH_ALEN];
3112 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003113 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003114 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003115
Johannes Berg67748892010-10-04 21:14:06 +02003116 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3117 if (err)
3118 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003119
3120 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003121 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003122 goto out_err;
3123 }
3124
Jouni Malineneec60b02009-03-20 21:21:19 +02003125 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3126 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003127 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003128 }
3129
Johannes Bergbba95fe2008-07-29 13:22:51 +02003130 while (1) {
3131 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3132 dst, next_hop, &pinfo);
3133 if (err == -ENOENT)
3134 break;
3135 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003136 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003137
3138 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3139 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3140 netdev, dst, next_hop,
3141 &pinfo) < 0)
3142 goto out;
3143
3144 path_idx++;
3145 }
3146
3147
3148 out:
3149 cb->args[1] = path_idx;
3150 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003151 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003152 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003153 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003154}
3155
3156static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3157{
Johannes Berg4c476992010-10-04 21:36:35 +02003158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003159 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003160 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003161 struct mpath_info pinfo;
3162 struct sk_buff *msg;
3163 u8 *dst = NULL;
3164 u8 next_hop[ETH_ALEN];
3165
3166 memset(&pinfo, 0, sizeof(pinfo));
3167
3168 if (!info->attrs[NL80211_ATTR_MAC])
3169 return -EINVAL;
3170
3171 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3172
Johannes Berg4c476992010-10-04 21:36:35 +02003173 if (!rdev->ops->get_mpath)
3174 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003175
Johannes Berg4c476992010-10-04 21:36:35 +02003176 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3177 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003178
Johannes Berg79c97e92009-07-07 03:56:12 +02003179 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003180 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003181 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003182
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003184 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003185 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003186
3187 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003188 dev, dst, next_hop, &pinfo) < 0) {
3189 nlmsg_free(msg);
3190 return -ENOBUFS;
3191 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003192
Johannes Berg4c476992010-10-04 21:36:35 +02003193 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003194}
3195
3196static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3197{
Johannes Berg4c476992010-10-04 21:36:35 +02003198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3199 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003200 u8 *dst = NULL;
3201 u8 *next_hop = NULL;
3202
3203 if (!info->attrs[NL80211_ATTR_MAC])
3204 return -EINVAL;
3205
3206 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3207 return -EINVAL;
3208
3209 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3210 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3211
Johannes Berg4c476992010-10-04 21:36:35 +02003212 if (!rdev->ops->change_mpath)
3213 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003214
Johannes Berg4c476992010-10-04 21:36:35 +02003215 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3216 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003217
Johannes Berg4c476992010-10-04 21:36:35 +02003218 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003219}
Johannes Berg4c476992010-10-04 21:36:35 +02003220
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003221static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3222{
Johannes Berg4c476992010-10-04 21:36:35 +02003223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3224 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003225 u8 *dst = NULL;
3226 u8 *next_hop = NULL;
3227
3228 if (!info->attrs[NL80211_ATTR_MAC])
3229 return -EINVAL;
3230
3231 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3232 return -EINVAL;
3233
3234 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3235 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3236
Johannes Berg4c476992010-10-04 21:36:35 +02003237 if (!rdev->ops->add_mpath)
3238 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003239
Johannes Berg4c476992010-10-04 21:36:35 +02003240 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3241 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003242
Johannes Berg4c476992010-10-04 21:36:35 +02003243 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003244}
3245
3246static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3247{
Johannes Berg4c476992010-10-04 21:36:35 +02003248 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3249 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003250 u8 *dst = NULL;
3251
3252 if (info->attrs[NL80211_ATTR_MAC])
3253 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3254
Johannes Berg4c476992010-10-04 21:36:35 +02003255 if (!rdev->ops->del_mpath)
3256 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003257
Johannes Berg4c476992010-10-04 21:36:35 +02003258 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003259}
3260
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003261static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3262{
Johannes Berg4c476992010-10-04 21:36:35 +02003263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3264 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003265 struct bss_parameters params;
3266
3267 memset(&params, 0, sizeof(params));
3268 /* default to not changing parameters */
3269 params.use_cts_prot = -1;
3270 params.use_short_preamble = -1;
3271 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003272 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003273 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003274
3275 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3276 params.use_cts_prot =
3277 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3278 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3279 params.use_short_preamble =
3280 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3281 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3282 params.use_short_slot_time =
3283 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003284 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3285 params.basic_rates =
3286 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3287 params.basic_rates_len =
3288 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3289 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003290 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3291 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003292 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3293 params.ht_opmode =
3294 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003295
Johannes Berg4c476992010-10-04 21:36:35 +02003296 if (!rdev->ops->change_bss)
3297 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003298
Johannes Berg074ac8d2010-09-16 14:58:22 +02003299 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003300 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3301 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003302
Johannes Berg4c476992010-10-04 21:36:35 +02003303 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003304}
3305
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003306static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003307 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3308 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3309 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3310 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3311 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3312 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3313};
3314
3315static int parse_reg_rule(struct nlattr *tb[],
3316 struct ieee80211_reg_rule *reg_rule)
3317{
3318 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3319 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3320
3321 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3322 return -EINVAL;
3323 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3324 return -EINVAL;
3325 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3326 return -EINVAL;
3327 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3328 return -EINVAL;
3329 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3330 return -EINVAL;
3331
3332 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3333
3334 freq_range->start_freq_khz =
3335 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3336 freq_range->end_freq_khz =
3337 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3338 freq_range->max_bandwidth_khz =
3339 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3340
3341 power_rule->max_eirp =
3342 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3343
3344 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3345 power_rule->max_antenna_gain =
3346 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3347
3348 return 0;
3349}
3350
3351static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3352{
3353 int r;
3354 char *data = NULL;
3355
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003356 /*
3357 * You should only get this when cfg80211 hasn't yet initialized
3358 * completely when built-in to the kernel right between the time
3359 * window between nl80211_init() and regulatory_init(), if that is
3360 * even possible.
3361 */
3362 mutex_lock(&cfg80211_mutex);
3363 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003364 mutex_unlock(&cfg80211_mutex);
3365 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003366 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003367 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003368
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003369 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3370 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003371
3372 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3373
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003374 r = regulatory_hint_user(data);
3375
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003376 return r;
3377}
3378
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003379static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003380 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003381{
Johannes Berg4c476992010-10-04 21:36:35 +02003382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003383 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003384 struct wireless_dev *wdev = dev->ieee80211_ptr;
3385 struct mesh_config cur_params;
3386 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003387 void *hdr;
3388 struct nlattr *pinfoattr;
3389 struct sk_buff *msg;
3390
Johannes Berg29cbe682010-12-03 09:20:44 +01003391 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3392 return -EOPNOTSUPP;
3393
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003394 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003395 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003396
Johannes Berg29cbe682010-12-03 09:20:44 +01003397 wdev_lock(wdev);
3398 /* If not connected, get default parameters */
3399 if (!wdev->mesh_id_len)
3400 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3401 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003402 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003403 &cur_params);
3404 wdev_unlock(wdev);
3405
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003406 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003407 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003408
3409 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003410 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003411 if (!msg)
3412 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003413 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003414 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003415 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003416 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003417 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003418 if (!pinfoattr)
3419 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003420 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3421 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3422 cur_params.dot11MeshRetryTimeout) ||
3423 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3424 cur_params.dot11MeshConfirmTimeout) ||
3425 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3426 cur_params.dot11MeshHoldingTimeout) ||
3427 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3428 cur_params.dot11MeshMaxPeerLinks) ||
3429 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3430 cur_params.dot11MeshMaxRetries) ||
3431 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3432 cur_params.dot11MeshTTL) ||
3433 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3434 cur_params.element_ttl) ||
3435 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3436 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003437 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3438 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003439 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3440 cur_params.dot11MeshHWMPmaxPREQretries) ||
3441 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3442 cur_params.path_refresh_time) ||
3443 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3444 cur_params.min_discovery_timeout) ||
3445 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3446 cur_params.dot11MeshHWMPactivePathTimeout) ||
3447 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3448 cur_params.dot11MeshHWMPpreqMinInterval) ||
3449 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3450 cur_params.dot11MeshHWMPperrMinInterval) ||
3451 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3452 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3453 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3454 cur_params.dot11MeshHWMPRootMode) ||
3455 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3456 cur_params.dot11MeshHWMPRannInterval) ||
3457 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3458 cur_params.dot11MeshGateAnnouncementProtocol) ||
3459 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3460 cur_params.dot11MeshForwarding) ||
3461 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003462 cur_params.rssi_threshold) ||
3463 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
3464 cur_params.ht_opmode))
David S. Miller9360ffd2012-03-29 04:41:26 -04003465 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003466 nla_nest_end(msg, pinfoattr);
3467 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003468 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003469
Johannes Berg3b858752009-03-12 09:55:09 +01003470 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003471 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003472 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003473 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003474 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003475}
3476
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003477static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003478 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3479 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3480 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3481 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3482 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3483 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003484 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003485 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003486 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003487
3488 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3489 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3490 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3491 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3492 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003493 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003494 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003495 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003496 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003497 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003498 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003499 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003500 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003501};
3502
Javier Cardonac80d5452010-12-16 17:37:49 -08003503static const struct nla_policy
3504 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003505 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003506 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3507 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003508 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003509 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003510 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003511 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003512};
3513
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003514static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003515 struct mesh_config *cfg,
3516 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003517{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003518 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003519 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003520
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003521#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3522do {\
3523 if (table[attr_num]) {\
3524 cfg->param = nla_fn(table[attr_num]); \
3525 mask |= (1 << (attr_num - 1)); \
3526 } \
3527} while (0);\
3528
3529
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003530 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003531 return -EINVAL;
3532 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003533 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003534 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003535 return -EINVAL;
3536
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003537 /* This makes sure that there aren't more than 32 mesh config
3538 * parameters (otherwise our bitfield scheme would not work.) */
3539 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3540
3541 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003542 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3543 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3544 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3545 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3546 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3547 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3548 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3549 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3550 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3551 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3552 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3553 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003554 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3555 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003556 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3557 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
Javier Cardonad299a1f2012-03-31 11:31:33 -07003558 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
3559 mask, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3560 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003561 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3562 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3563 nla_get_u8);
3564 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3565 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3566 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3567 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3568 nla_get_u16);
3569 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3570 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3571 nla_get_u32);
3572 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3573 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3574 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003575 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3576 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3577 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003578 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3579 dot11MeshHWMPnetDiameterTraversalTime,
3580 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3581 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003582 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3583 dot11MeshHWMPRootMode, mask,
3584 NL80211_MESHCONF_HWMP_ROOTMODE,
3585 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003586 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3587 dot11MeshHWMPRannInterval, mask,
3588 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3589 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003590 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3591 dot11MeshGateAnnouncementProtocol, mask,
3592 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3593 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003594 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3595 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003596 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3597 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003598 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
3599 mask, NL80211_MESHCONF_HT_OPMODE, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003600 if (mask_out)
3601 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003602
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003603 return 0;
3604
3605#undef FILL_IN_MESH_PARAM_IF_SET
3606}
3607
Javier Cardonac80d5452010-12-16 17:37:49 -08003608static int nl80211_parse_mesh_setup(struct genl_info *info,
3609 struct mesh_setup *setup)
3610{
3611 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3612
3613 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3614 return -EINVAL;
3615 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3616 info->attrs[NL80211_ATTR_MESH_SETUP],
3617 nl80211_mesh_setup_params_policy))
3618 return -EINVAL;
3619
Javier Cardonad299a1f2012-03-31 11:31:33 -07003620 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3621 setup->sync_method =
3622 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3623 IEEE80211_SYNC_METHOD_VENDOR :
3624 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3625
Javier Cardonac80d5452010-12-16 17:37:49 -08003626 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3627 setup->path_sel_proto =
3628 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3629 IEEE80211_PATH_PROTOCOL_VENDOR :
3630 IEEE80211_PATH_PROTOCOL_HWMP;
3631
3632 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3633 setup->path_metric =
3634 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3635 IEEE80211_PATH_METRIC_VENDOR :
3636 IEEE80211_PATH_METRIC_AIRTIME;
3637
Javier Cardona581a8b02011-04-07 15:08:27 -07003638
3639 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003640 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003641 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003642 if (!is_valid_ie_attr(ieattr))
3643 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003644 setup->ie = nla_data(ieattr);
3645 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003646 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003647 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3648 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003649
3650 return 0;
3651}
3652
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003653static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003654 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003655{
3656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3657 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003658 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003659 struct mesh_config cfg;
3660 u32 mask;
3661 int err;
3662
Johannes Berg29cbe682010-12-03 09:20:44 +01003663 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3664 return -EOPNOTSUPP;
3665
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003666 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003667 return -EOPNOTSUPP;
3668
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003669 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003670 if (err)
3671 return err;
3672
Johannes Berg29cbe682010-12-03 09:20:44 +01003673 wdev_lock(wdev);
3674 if (!wdev->mesh_id_len)
3675 err = -ENOLINK;
3676
3677 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003678 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003679 mask, &cfg);
3680
3681 wdev_unlock(wdev);
3682
3683 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003684}
3685
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003686static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3687{
3688 struct sk_buff *msg;
3689 void *hdr = NULL;
3690 struct nlattr *nl_reg_rules;
3691 unsigned int i;
3692 int err = -EINVAL;
3693
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003694 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003695
3696 if (!cfg80211_regdomain)
3697 goto out;
3698
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003699 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003700 if (!msg) {
3701 err = -ENOBUFS;
3702 goto out;
3703 }
3704
3705 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3706 NL80211_CMD_GET_REG);
3707 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003708 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003709
David S. Miller9360ffd2012-03-29 04:41:26 -04003710 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3711 cfg80211_regdomain->alpha2) ||
3712 (cfg80211_regdomain->dfs_region &&
3713 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3714 cfg80211_regdomain->dfs_region)))
3715 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003716
3717 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3718 if (!nl_reg_rules)
3719 goto nla_put_failure;
3720
3721 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3722 struct nlattr *nl_reg_rule;
3723 const struct ieee80211_reg_rule *reg_rule;
3724 const struct ieee80211_freq_range *freq_range;
3725 const struct ieee80211_power_rule *power_rule;
3726
3727 reg_rule = &cfg80211_regdomain->reg_rules[i];
3728 freq_range = &reg_rule->freq_range;
3729 power_rule = &reg_rule->power_rule;
3730
3731 nl_reg_rule = nla_nest_start(msg, i);
3732 if (!nl_reg_rule)
3733 goto nla_put_failure;
3734
David S. Miller9360ffd2012-03-29 04:41:26 -04003735 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3736 reg_rule->flags) ||
3737 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3738 freq_range->start_freq_khz) ||
3739 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3740 freq_range->end_freq_khz) ||
3741 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3742 freq_range->max_bandwidth_khz) ||
3743 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3744 power_rule->max_antenna_gain) ||
3745 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3746 power_rule->max_eirp))
3747 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003748
3749 nla_nest_end(msg, nl_reg_rule);
3750 }
3751
3752 nla_nest_end(msg, nl_reg_rules);
3753
3754 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003755 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003756 goto out;
3757
3758nla_put_failure:
3759 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003760put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003761 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003762 err = -EMSGSIZE;
3763out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003764 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003765 return err;
3766}
3767
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003768static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3769{
3770 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3771 struct nlattr *nl_reg_rule;
3772 char *alpha2 = NULL;
3773 int rem_reg_rules = 0, r = 0;
3774 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003775 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003776 struct ieee80211_regdomain *rd = NULL;
3777
3778 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3779 return -EINVAL;
3780
3781 if (!info->attrs[NL80211_ATTR_REG_RULES])
3782 return -EINVAL;
3783
3784 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3785
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003786 if (info->attrs[NL80211_ATTR_DFS_REGION])
3787 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3788
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003789 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3790 rem_reg_rules) {
3791 num_rules++;
3792 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003793 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003794 }
3795
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003796 mutex_lock(&cfg80211_mutex);
3797
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003798 if (!reg_is_valid_request(alpha2)) {
3799 r = -EINVAL;
3800 goto bad_reg;
3801 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003802
3803 size_of_regd = sizeof(struct ieee80211_regdomain) +
3804 (num_rules * sizeof(struct ieee80211_reg_rule));
3805
3806 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003807 if (!rd) {
3808 r = -ENOMEM;
3809 goto bad_reg;
3810 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003811
3812 rd->n_reg_rules = num_rules;
3813 rd->alpha2[0] = alpha2[0];
3814 rd->alpha2[1] = alpha2[1];
3815
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003816 /*
3817 * Disable DFS master mode if the DFS region was
3818 * not supported or known on this kernel.
3819 */
3820 if (reg_supported_dfs_region(dfs_region))
3821 rd->dfs_region = dfs_region;
3822
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003823 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3824 rem_reg_rules) {
3825 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3826 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3827 reg_rule_policy);
3828 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3829 if (r)
3830 goto bad_reg;
3831
3832 rule_idx++;
3833
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003834 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3835 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003836 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003837 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003838 }
3839
3840 BUG_ON(rule_idx != num_rules);
3841
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003842 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003843
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003844 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003845
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003846 return r;
3847
Johannes Bergd2372b32008-10-24 20:32:20 +02003848 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003849 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003850 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003851 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003852}
3853
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003854static int validate_scan_freqs(struct nlattr *freqs)
3855{
3856 struct nlattr *attr1, *attr2;
3857 int n_channels = 0, tmp1, tmp2;
3858
3859 nla_for_each_nested(attr1, freqs, tmp1) {
3860 n_channels++;
3861 /*
3862 * Some hardware has a limited channel list for
3863 * scanning, and it is pretty much nonsensical
3864 * to scan for a channel twice, so disallow that
3865 * and don't require drivers to check that the
3866 * channel list they get isn't longer than what
3867 * they can scan, as long as they can scan all
3868 * the channels they registered at once.
3869 */
3870 nla_for_each_nested(attr2, freqs, tmp2)
3871 if (attr1 != attr2 &&
3872 nla_get_u32(attr1) == nla_get_u32(attr2))
3873 return 0;
3874 }
3875
3876 return n_channels;
3877}
3878
Johannes Berg2a519312009-02-10 21:25:55 +01003879static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3880{
Johannes Berg4c476992010-10-04 21:36:35 +02003881 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3882 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003883 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003884 struct nlattr *attr;
3885 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003886 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003887 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003888
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003889 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3890 return -EINVAL;
3891
Johannes Berg79c97e92009-07-07 03:56:12 +02003892 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003893
Johannes Berg4c476992010-10-04 21:36:35 +02003894 if (!rdev->ops->scan)
3895 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003896
Johannes Berg4c476992010-10-04 21:36:35 +02003897 if (rdev->scan_req)
3898 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003899
3900 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003901 n_channels = validate_scan_freqs(
3902 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003903 if (!n_channels)
3904 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003905 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003906 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003907 n_channels = 0;
3908
Johannes Berg2a519312009-02-10 21:25:55 +01003909 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3910 if (wiphy->bands[band])
3911 n_channels += wiphy->bands[band]->n_channels;
3912 }
3913
3914 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3915 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3916 n_ssids++;
3917
Johannes Berg4c476992010-10-04 21:36:35 +02003918 if (n_ssids > wiphy->max_scan_ssids)
3919 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003920
Jouni Malinen70692ad2009-02-16 19:39:13 +02003921 if (info->attrs[NL80211_ATTR_IE])
3922 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3923 else
3924 ie_len = 0;
3925
Johannes Berg4c476992010-10-04 21:36:35 +02003926 if (ie_len > wiphy->max_scan_ie_len)
3927 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003928
Johannes Berg2a519312009-02-10 21:25:55 +01003929 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003930 + sizeof(*request->ssids) * n_ssids
3931 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003932 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003933 if (!request)
3934 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003935
Johannes Berg2a519312009-02-10 21:25:55 +01003936 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003937 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003938 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003939 if (ie_len) {
3940 if (request->ssids)
3941 request->ie = (void *)(request->ssids + n_ssids);
3942 else
3943 request->ie = (void *)(request->channels + n_channels);
3944 }
Johannes Berg2a519312009-02-10 21:25:55 +01003945
Johannes Berg584991d2009-11-02 13:32:03 +01003946 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003947 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3948 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003949 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003950 struct ieee80211_channel *chan;
3951
3952 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3953
3954 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003955 err = -EINVAL;
3956 goto out_free;
3957 }
Johannes Berg584991d2009-11-02 13:32:03 +01003958
3959 /* ignore disabled channels */
3960 if (chan->flags & IEEE80211_CHAN_DISABLED)
3961 continue;
3962
3963 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003964 i++;
3965 }
3966 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003967 enum ieee80211_band band;
3968
Johannes Berg2a519312009-02-10 21:25:55 +01003969 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003970 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3971 int j;
3972 if (!wiphy->bands[band])
3973 continue;
3974 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003975 struct ieee80211_channel *chan;
3976
3977 chan = &wiphy->bands[band]->channels[j];
3978
3979 if (chan->flags & IEEE80211_CHAN_DISABLED)
3980 continue;
3981
3982 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003983 i++;
3984 }
3985 }
3986 }
3987
Johannes Berg584991d2009-11-02 13:32:03 +01003988 if (!i) {
3989 err = -EINVAL;
3990 goto out_free;
3991 }
3992
3993 request->n_channels = i;
3994
Johannes Berg2a519312009-02-10 21:25:55 +01003995 i = 0;
3996 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3997 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003998 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003999 err = -EINVAL;
4000 goto out_free;
4001 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004002 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004003 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004004 i++;
4005 }
4006 }
4007
Jouni Malinen70692ad2009-02-16 19:39:13 +02004008 if (info->attrs[NL80211_ATTR_IE]) {
4009 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004010 memcpy((void *)request->ie,
4011 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004012 request->ie_len);
4013 }
4014
Johannes Berg34850ab2011-07-18 18:08:35 +02004015 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004016 if (wiphy->bands[i])
4017 request->rates[i] =
4018 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004019
4020 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4021 nla_for_each_nested(attr,
4022 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4023 tmp) {
4024 enum ieee80211_band band = nla_type(attr);
4025
Dan Carpenter84404622011-07-29 11:52:18 +03004026 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004027 err = -EINVAL;
4028 goto out_free;
4029 }
4030 err = ieee80211_get_ratemask(wiphy->bands[band],
4031 nla_data(attr),
4032 nla_len(attr),
4033 &request->rates[band]);
4034 if (err)
4035 goto out_free;
4036 }
4037 }
4038
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304039 request->no_cck =
4040 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4041
Johannes Berg463d0182009-07-14 00:33:35 +02004042 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004043 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004044
Johannes Berg79c97e92009-07-07 03:56:12 +02004045 rdev->scan_req = request;
4046 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004047
Johannes Berg463d0182009-07-14 00:33:35 +02004048 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004049 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004050 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004051 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004052 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004053 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004054 kfree(request);
4055 }
Johannes Berg3b858752009-03-12 09:55:09 +01004056
Johannes Berg2a519312009-02-10 21:25:55 +01004057 return err;
4058}
4059
Luciano Coelho807f8a82011-05-11 17:09:35 +03004060static int nl80211_start_sched_scan(struct sk_buff *skb,
4061 struct genl_info *info)
4062{
4063 struct cfg80211_sched_scan_request *request;
4064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4065 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004066 struct nlattr *attr;
4067 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004068 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004069 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004070 enum ieee80211_band band;
4071 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004072 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004073
4074 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4075 !rdev->ops->sched_scan_start)
4076 return -EOPNOTSUPP;
4077
4078 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4079 return -EINVAL;
4080
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004081 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4082 return -EINVAL;
4083
4084 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4085 if (interval == 0)
4086 return -EINVAL;
4087
Luciano Coelho807f8a82011-05-11 17:09:35 +03004088 wiphy = &rdev->wiphy;
4089
4090 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4091 n_channels = validate_scan_freqs(
4092 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4093 if (!n_channels)
4094 return -EINVAL;
4095 } else {
4096 n_channels = 0;
4097
4098 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4099 if (wiphy->bands[band])
4100 n_channels += wiphy->bands[band]->n_channels;
4101 }
4102
4103 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4104 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4105 tmp)
4106 n_ssids++;
4107
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004108 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004109 return -EINVAL;
4110
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004111 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4112 nla_for_each_nested(attr,
4113 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4114 tmp)
4115 n_match_sets++;
4116
4117 if (n_match_sets > wiphy->max_match_sets)
4118 return -EINVAL;
4119
Luciano Coelho807f8a82011-05-11 17:09:35 +03004120 if (info->attrs[NL80211_ATTR_IE])
4121 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4122 else
4123 ie_len = 0;
4124
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004125 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004126 return -EINVAL;
4127
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004128 mutex_lock(&rdev->sched_scan_mtx);
4129
4130 if (rdev->sched_scan_req) {
4131 err = -EINPROGRESS;
4132 goto out;
4133 }
4134
Luciano Coelho807f8a82011-05-11 17:09:35 +03004135 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004136 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004137 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004138 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004139 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004140 if (!request) {
4141 err = -ENOMEM;
4142 goto out;
4143 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004144
4145 if (n_ssids)
4146 request->ssids = (void *)&request->channels[n_channels];
4147 request->n_ssids = n_ssids;
4148 if (ie_len) {
4149 if (request->ssids)
4150 request->ie = (void *)(request->ssids + n_ssids);
4151 else
4152 request->ie = (void *)(request->channels + n_channels);
4153 }
4154
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004155 if (n_match_sets) {
4156 if (request->ie)
4157 request->match_sets = (void *)(request->ie + ie_len);
4158 else if (request->ssids)
4159 request->match_sets =
4160 (void *)(request->ssids + n_ssids);
4161 else
4162 request->match_sets =
4163 (void *)(request->channels + n_channels);
4164 }
4165 request->n_match_sets = n_match_sets;
4166
Luciano Coelho807f8a82011-05-11 17:09:35 +03004167 i = 0;
4168 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4169 /* user specified, bail out if channel not found */
4170 nla_for_each_nested(attr,
4171 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4172 tmp) {
4173 struct ieee80211_channel *chan;
4174
4175 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4176
4177 if (!chan) {
4178 err = -EINVAL;
4179 goto out_free;
4180 }
4181
4182 /* ignore disabled channels */
4183 if (chan->flags & IEEE80211_CHAN_DISABLED)
4184 continue;
4185
4186 request->channels[i] = chan;
4187 i++;
4188 }
4189 } else {
4190 /* all channels */
4191 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4192 int j;
4193 if (!wiphy->bands[band])
4194 continue;
4195 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4196 struct ieee80211_channel *chan;
4197
4198 chan = &wiphy->bands[band]->channels[j];
4199
4200 if (chan->flags & IEEE80211_CHAN_DISABLED)
4201 continue;
4202
4203 request->channels[i] = chan;
4204 i++;
4205 }
4206 }
4207 }
4208
4209 if (!i) {
4210 err = -EINVAL;
4211 goto out_free;
4212 }
4213
4214 request->n_channels = i;
4215
4216 i = 0;
4217 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4218 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4219 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004220 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004221 err = -EINVAL;
4222 goto out_free;
4223 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004224 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004225 memcpy(request->ssids[i].ssid, nla_data(attr),
4226 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004227 i++;
4228 }
4229 }
4230
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004231 i = 0;
4232 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4233 nla_for_each_nested(attr,
4234 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4235 tmp) {
4236 struct nlattr *ssid;
4237
4238 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4239 nla_data(attr), nla_len(attr),
4240 nl80211_match_policy);
4241 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4242 if (ssid) {
4243 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4244 err = -EINVAL;
4245 goto out_free;
4246 }
4247 memcpy(request->match_sets[i].ssid.ssid,
4248 nla_data(ssid), nla_len(ssid));
4249 request->match_sets[i].ssid.ssid_len =
4250 nla_len(ssid);
4251 }
4252 i++;
4253 }
4254 }
4255
Luciano Coelho807f8a82011-05-11 17:09:35 +03004256 if (info->attrs[NL80211_ATTR_IE]) {
4257 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4258 memcpy((void *)request->ie,
4259 nla_data(info->attrs[NL80211_ATTR_IE]),
4260 request->ie_len);
4261 }
4262
4263 request->dev = dev;
4264 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004265 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004266
4267 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4268 if (!err) {
4269 rdev->sched_scan_req = request;
4270 nl80211_send_sched_scan(rdev, dev,
4271 NL80211_CMD_START_SCHED_SCAN);
4272 goto out;
4273 }
4274
4275out_free:
4276 kfree(request);
4277out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004278 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004279 return err;
4280}
4281
4282static int nl80211_stop_sched_scan(struct sk_buff *skb,
4283 struct genl_info *info)
4284{
4285 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004286 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004287
4288 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4289 !rdev->ops->sched_scan_stop)
4290 return -EOPNOTSUPP;
4291
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004292 mutex_lock(&rdev->sched_scan_mtx);
4293 err = __cfg80211_stop_sched_scan(rdev, false);
4294 mutex_unlock(&rdev->sched_scan_mtx);
4295
4296 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004297}
4298
Johannes Berg9720bb32011-06-21 09:45:33 +02004299static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4300 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004301 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004302 struct wireless_dev *wdev,
4303 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004304{
Johannes Berg48ab9052009-07-10 18:42:31 +02004305 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004306 void *hdr;
4307 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004308
4309 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004310
Johannes Berg9720bb32011-06-21 09:45:33 +02004311 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004312 NL80211_CMD_NEW_SCAN_RESULTS);
4313 if (!hdr)
4314 return -1;
4315
Johannes Berg9720bb32011-06-21 09:45:33 +02004316 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4317
David S. Miller9360ffd2012-03-29 04:41:26 -04004318 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4319 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4320 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004321
4322 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4323 if (!bss)
4324 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004325 if ((!is_zero_ether_addr(res->bssid) &&
4326 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4327 (res->information_elements && res->len_information_elements &&
4328 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4329 res->len_information_elements,
4330 res->information_elements)) ||
4331 (res->beacon_ies && res->len_beacon_ies &&
4332 res->beacon_ies != res->information_elements &&
4333 nla_put(msg, NL80211_BSS_BEACON_IES,
4334 res->len_beacon_ies, res->beacon_ies)))
4335 goto nla_put_failure;
4336 if (res->tsf &&
4337 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4338 goto nla_put_failure;
4339 if (res->beacon_interval &&
4340 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4341 goto nla_put_failure;
4342 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4343 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4344 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4345 jiffies_to_msecs(jiffies - intbss->ts)))
4346 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004347
Johannes Berg77965c92009-02-18 18:45:06 +01004348 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004349 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004350 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4351 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004352 break;
4353 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004354 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4355 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004356 break;
4357 default:
4358 break;
4359 }
4360
Johannes Berg48ab9052009-07-10 18:42:31 +02004361 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004362 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004363 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004364 if (intbss == wdev->current_bss &&
4365 nla_put_u32(msg, NL80211_BSS_STATUS,
4366 NL80211_BSS_STATUS_ASSOCIATED))
4367 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004368 break;
4369 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004370 if (intbss == wdev->current_bss &&
4371 nla_put_u32(msg, NL80211_BSS_STATUS,
4372 NL80211_BSS_STATUS_IBSS_JOINED))
4373 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004374 break;
4375 default:
4376 break;
4377 }
4378
Johannes Berg2a519312009-02-10 21:25:55 +01004379 nla_nest_end(msg, bss);
4380
4381 return genlmsg_end(msg, hdr);
4382
4383 nla_put_failure:
4384 genlmsg_cancel(msg, hdr);
4385 return -EMSGSIZE;
4386}
4387
4388static int nl80211_dump_scan(struct sk_buff *skb,
4389 struct netlink_callback *cb)
4390{
Johannes Berg48ab9052009-07-10 18:42:31 +02004391 struct cfg80211_registered_device *rdev;
4392 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004393 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004394 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004395 int start = cb->args[1], idx = 0;
4396 int err;
4397
Johannes Berg67748892010-10-04 21:14:06 +02004398 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4399 if (err)
4400 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004401
Johannes Berg48ab9052009-07-10 18:42:31 +02004402 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004403
Johannes Berg48ab9052009-07-10 18:42:31 +02004404 wdev_lock(wdev);
4405 spin_lock_bh(&rdev->bss_lock);
4406 cfg80211_bss_expire(rdev);
4407
Johannes Berg9720bb32011-06-21 09:45:33 +02004408 cb->seq = rdev->bss_generation;
4409
Johannes Berg48ab9052009-07-10 18:42:31 +02004410 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004411 if (++idx <= start)
4412 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004413 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004414 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004415 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004416 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004417 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004418 }
4419 }
4420
Johannes Berg48ab9052009-07-10 18:42:31 +02004421 spin_unlock_bh(&rdev->bss_lock);
4422 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004423
4424 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004425 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004426
Johannes Berg67748892010-10-04 21:14:06 +02004427 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004428}
4429
Holger Schurig61fa7132009-11-11 12:25:40 +01004430static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4431 int flags, struct net_device *dev,
4432 struct survey_info *survey)
4433{
4434 void *hdr;
4435 struct nlattr *infoattr;
4436
Holger Schurig61fa7132009-11-11 12:25:40 +01004437 hdr = nl80211hdr_put(msg, pid, seq, flags,
4438 NL80211_CMD_NEW_SURVEY_RESULTS);
4439 if (!hdr)
4440 return -ENOMEM;
4441
David S. Miller9360ffd2012-03-29 04:41:26 -04004442 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4443 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004444
4445 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4446 if (!infoattr)
4447 goto nla_put_failure;
4448
David S. Miller9360ffd2012-03-29 04:41:26 -04004449 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4450 survey->channel->center_freq))
4451 goto nla_put_failure;
4452
4453 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4454 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4455 goto nla_put_failure;
4456 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4457 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4458 goto nla_put_failure;
4459 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4460 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4461 survey->channel_time))
4462 goto nla_put_failure;
4463 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4464 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4465 survey->channel_time_busy))
4466 goto nla_put_failure;
4467 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4468 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4469 survey->channel_time_ext_busy))
4470 goto nla_put_failure;
4471 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4472 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4473 survey->channel_time_rx))
4474 goto nla_put_failure;
4475 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4476 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4477 survey->channel_time_tx))
4478 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004479
4480 nla_nest_end(msg, infoattr);
4481
4482 return genlmsg_end(msg, hdr);
4483
4484 nla_put_failure:
4485 genlmsg_cancel(msg, hdr);
4486 return -EMSGSIZE;
4487}
4488
4489static int nl80211_dump_survey(struct sk_buff *skb,
4490 struct netlink_callback *cb)
4491{
4492 struct survey_info survey;
4493 struct cfg80211_registered_device *dev;
4494 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004495 int survey_idx = cb->args[1];
4496 int res;
4497
Johannes Berg67748892010-10-04 21:14:06 +02004498 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4499 if (res)
4500 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004501
4502 if (!dev->ops->dump_survey) {
4503 res = -EOPNOTSUPP;
4504 goto out_err;
4505 }
4506
4507 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004508 struct ieee80211_channel *chan;
4509
Holger Schurig61fa7132009-11-11 12:25:40 +01004510 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4511 &survey);
4512 if (res == -ENOENT)
4513 break;
4514 if (res)
4515 goto out_err;
4516
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004517 /* Survey without a channel doesn't make sense */
4518 if (!survey.channel) {
4519 res = -EINVAL;
4520 goto out;
4521 }
4522
4523 chan = ieee80211_get_channel(&dev->wiphy,
4524 survey.channel->center_freq);
4525 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4526 survey_idx++;
4527 continue;
4528 }
4529
Holger Schurig61fa7132009-11-11 12:25:40 +01004530 if (nl80211_send_survey(skb,
4531 NETLINK_CB(cb->skb).pid,
4532 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4533 netdev,
4534 &survey) < 0)
4535 goto out;
4536 survey_idx++;
4537 }
4538
4539 out:
4540 cb->args[1] = survey_idx;
4541 res = skb->len;
4542 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004543 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004544 return res;
4545}
4546
Jouni Malinen255e7372009-03-20 21:21:17 +02004547static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4548{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004549 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004550}
4551
Samuel Ortizb23aa672009-07-01 21:26:54 +02004552static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4553{
4554 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4555 NL80211_WPA_VERSION_2));
4556}
4557
Jouni Malinen636a5d32009-03-19 13:39:22 +02004558static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4559{
Johannes Berg4c476992010-10-04 21:36:35 +02004560 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4561 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004562 struct ieee80211_channel *chan;
4563 const u8 *bssid, *ssid, *ie = NULL;
4564 int err, ssid_len, ie_len = 0;
4565 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004566 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004567 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004568
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004569 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4570 return -EINVAL;
4571
4572 if (!info->attrs[NL80211_ATTR_MAC])
4573 return -EINVAL;
4574
Jouni Malinen17780922009-03-27 20:52:47 +02004575 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4576 return -EINVAL;
4577
Johannes Berg19957bb2009-07-02 17:20:43 +02004578 if (!info->attrs[NL80211_ATTR_SSID])
4579 return -EINVAL;
4580
4581 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4582 return -EINVAL;
4583
Johannes Bergfffd0932009-07-08 14:22:54 +02004584 err = nl80211_parse_key(info, &key);
4585 if (err)
4586 return err;
4587
4588 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004589 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4590 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004591 if (!key.p.key || !key.p.key_len)
4592 return -EINVAL;
4593 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4594 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4595 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4596 key.p.key_len != WLAN_KEY_LEN_WEP104))
4597 return -EINVAL;
4598 if (key.idx > 4)
4599 return -EINVAL;
4600 } else {
4601 key.p.key_len = 0;
4602 key.p.key = NULL;
4603 }
4604
Johannes Bergafea0b72010-08-10 09:46:42 +02004605 if (key.idx >= 0) {
4606 int i;
4607 bool ok = false;
4608 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4609 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4610 ok = true;
4611 break;
4612 }
4613 }
Johannes Berg4c476992010-10-04 21:36:35 +02004614 if (!ok)
4615 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004616 }
4617
Johannes Berg4c476992010-10-04 21:36:35 +02004618 if (!rdev->ops->auth)
4619 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004620
Johannes Berg074ac8d2010-09-16 14:58:22 +02004621 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004622 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4623 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004624
Johannes Berg19957bb2009-07-02 17:20:43 +02004625 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004626 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004627 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004628 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4629 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004630
Johannes Berg19957bb2009-07-02 17:20:43 +02004631 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4632 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4633
4634 if (info->attrs[NL80211_ATTR_IE]) {
4635 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4636 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4637 }
4638
4639 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004640 if (!nl80211_valid_auth_type(auth_type))
4641 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004642
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004643 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4644
Johannes Berg95de8172012-01-20 13:55:25 +01004645 /*
4646 * Since we no longer track auth state, ignore
4647 * requests to only change local state.
4648 */
4649 if (local_state_change)
4650 return 0;
4651
Johannes Berg4c476992010-10-04 21:36:35 +02004652 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4653 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004654 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004655}
4656
Johannes Bergc0692b82010-08-27 14:26:53 +03004657static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4658 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004659 struct cfg80211_crypto_settings *settings,
4660 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004661{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004662 memset(settings, 0, sizeof(*settings));
4663
Samuel Ortizb23aa672009-07-01 21:26:54 +02004664 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4665
Johannes Bergc0692b82010-08-27 14:26:53 +03004666 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4667 u16 proto;
4668 proto = nla_get_u16(
4669 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4670 settings->control_port_ethertype = cpu_to_be16(proto);
4671 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4672 proto != ETH_P_PAE)
4673 return -EINVAL;
4674 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4675 settings->control_port_no_encrypt = true;
4676 } else
4677 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4678
Samuel Ortizb23aa672009-07-01 21:26:54 +02004679 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4680 void *data;
4681 int len, i;
4682
4683 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4684 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4685 settings->n_ciphers_pairwise = len / sizeof(u32);
4686
4687 if (len % sizeof(u32))
4688 return -EINVAL;
4689
Johannes Berg3dc27d22009-07-02 21:36:37 +02004690 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004691 return -EINVAL;
4692
4693 memcpy(settings->ciphers_pairwise, data, len);
4694
4695 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004696 if (!cfg80211_supported_cipher_suite(
4697 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004698 settings->ciphers_pairwise[i]))
4699 return -EINVAL;
4700 }
4701
4702 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4703 settings->cipher_group =
4704 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004705 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4706 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004707 return -EINVAL;
4708 }
4709
4710 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4711 settings->wpa_versions =
4712 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4713 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4714 return -EINVAL;
4715 }
4716
4717 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4718 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004719 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004720
4721 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4722 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4723 settings->n_akm_suites = len / sizeof(u32);
4724
4725 if (len % sizeof(u32))
4726 return -EINVAL;
4727
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004728 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4729 return -EINVAL;
4730
Samuel Ortizb23aa672009-07-01 21:26:54 +02004731 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004732 }
4733
4734 return 0;
4735}
4736
Jouni Malinen636a5d32009-03-19 13:39:22 +02004737static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4738{
Johannes Berg4c476992010-10-04 21:36:35 +02004739 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4740 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004741 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004742 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004743 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004744 int err, ssid_len, ie_len = 0;
4745 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004746 u32 flags = 0;
4747 struct ieee80211_ht_cap *ht_capa = NULL;
4748 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004749
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004750 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4751 return -EINVAL;
4752
4753 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004754 !info->attrs[NL80211_ATTR_SSID] ||
4755 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004756 return -EINVAL;
4757
Johannes Berg4c476992010-10-04 21:36:35 +02004758 if (!rdev->ops->assoc)
4759 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004760
Johannes Berg074ac8d2010-09-16 14:58:22 +02004761 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004762 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4763 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004764
Johannes Berg19957bb2009-07-02 17:20:43 +02004765 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004766
Johannes Berg19957bb2009-07-02 17:20:43 +02004767 chan = ieee80211_get_channel(&rdev->wiphy,
4768 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004769 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4770 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004771
Johannes Berg19957bb2009-07-02 17:20:43 +02004772 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4773 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004774
4775 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004776 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4777 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004778 }
4779
Jouni Malinendc6382c2009-05-06 22:09:37 +03004780 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004781 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004782 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004783 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004784 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004785 else if (mfp != NL80211_MFP_NO)
4786 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004787 }
4788
Johannes Berg3e5d7642009-07-07 14:37:26 +02004789 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4790 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4791
Ben Greear7e7c8922011-11-18 11:31:59 -08004792 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4793 flags |= ASSOC_REQ_DISABLE_HT;
4794
4795 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4796 ht_capa_mask =
4797 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4798
4799 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4800 if (!ht_capa_mask)
4801 return -EINVAL;
4802 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4803 }
4804
Johannes Bergc0692b82010-08-27 14:26:53 +03004805 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004806 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004807 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4808 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004809 &crypto, flags, ht_capa,
4810 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004811
Jouni Malinen636a5d32009-03-19 13:39:22 +02004812 return err;
4813}
4814
4815static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4816{
Johannes Berg4c476992010-10-04 21:36:35 +02004817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4818 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004819 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004820 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004821 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004822 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004823
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004824 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4825 return -EINVAL;
4826
4827 if (!info->attrs[NL80211_ATTR_MAC])
4828 return -EINVAL;
4829
4830 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4831 return -EINVAL;
4832
Johannes Berg4c476992010-10-04 21:36:35 +02004833 if (!rdev->ops->deauth)
4834 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004835
Johannes Berg074ac8d2010-09-16 14:58:22 +02004836 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004837 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4838 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004839
Johannes Berg19957bb2009-07-02 17:20:43 +02004840 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004841
Johannes Berg19957bb2009-07-02 17:20:43 +02004842 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4843 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004844 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004845 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004846 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004847
4848 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004849 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4850 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004851 }
4852
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004853 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4854
Johannes Berg4c476992010-10-04 21:36:35 +02004855 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4856 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004857}
4858
4859static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4860{
Johannes Berg4c476992010-10-04 21:36:35 +02004861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4862 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004863 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004864 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004865 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004866 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004867
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004868 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4869 return -EINVAL;
4870
4871 if (!info->attrs[NL80211_ATTR_MAC])
4872 return -EINVAL;
4873
4874 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4875 return -EINVAL;
4876
Johannes Berg4c476992010-10-04 21:36:35 +02004877 if (!rdev->ops->disassoc)
4878 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004879
Johannes Berg074ac8d2010-09-16 14:58:22 +02004880 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004881 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4882 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004883
Johannes Berg19957bb2009-07-02 17:20:43 +02004884 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004885
Johannes Berg19957bb2009-07-02 17:20:43 +02004886 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4887 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004888 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004889 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004890 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004891
4892 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004893 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4894 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004895 }
4896
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004897 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4898
Johannes Berg4c476992010-10-04 21:36:35 +02004899 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4900 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004901}
4902
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004903static bool
4904nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4905 int mcast_rate[IEEE80211_NUM_BANDS],
4906 int rateval)
4907{
4908 struct wiphy *wiphy = &rdev->wiphy;
4909 bool found = false;
4910 int band, i;
4911
4912 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4913 struct ieee80211_supported_band *sband;
4914
4915 sband = wiphy->bands[band];
4916 if (!sband)
4917 continue;
4918
4919 for (i = 0; i < sband->n_bitrates; i++) {
4920 if (sband->bitrates[i].bitrate == rateval) {
4921 mcast_rate[band] = i + 1;
4922 found = true;
4923 break;
4924 }
4925 }
4926 }
4927
4928 return found;
4929}
4930
Johannes Berg04a773a2009-04-19 21:24:32 +02004931static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4932{
Johannes Berg4c476992010-10-04 21:36:35 +02004933 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4934 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004935 struct cfg80211_ibss_params ibss;
4936 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004937 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004938 int err;
4939
Johannes Berg8e30bc52009-04-22 17:45:38 +02004940 memset(&ibss, 0, sizeof(ibss));
4941
Johannes Berg04a773a2009-04-19 21:24:32 +02004942 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4943 return -EINVAL;
4944
4945 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4946 !info->attrs[NL80211_ATTR_SSID] ||
4947 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4948 return -EINVAL;
4949
Johannes Berg8e30bc52009-04-22 17:45:38 +02004950 ibss.beacon_interval = 100;
4951
4952 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4953 ibss.beacon_interval =
4954 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4955 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4956 return -EINVAL;
4957 }
4958
Johannes Berg4c476992010-10-04 21:36:35 +02004959 if (!rdev->ops->join_ibss)
4960 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004961
Johannes Berg4c476992010-10-04 21:36:35 +02004962 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4963 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004964
Johannes Berg79c97e92009-07-07 03:56:12 +02004965 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004966
Johannes Berg39193492011-09-16 13:45:25 +02004967 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004968 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004969
4970 if (!is_valid_ether_addr(ibss.bssid))
4971 return -EINVAL;
4972 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004973 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4974 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4975
4976 if (info->attrs[NL80211_ATTR_IE]) {
4977 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4978 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4979 }
4980
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004981 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4982 enum nl80211_channel_type channel_type;
4983
Johannes Bergcd6c6592012-05-10 21:27:18 +02004984 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004985 return -EINVAL;
4986
4987 if (channel_type != NL80211_CHAN_NO_HT &&
4988 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4989 return -EINVAL;
4990
4991 ibss.channel_type = channel_type;
4992 } else {
4993 ibss.channel_type = NL80211_CHAN_NO_HT;
4994 }
4995
4996 ibss.channel = rdev_freq_to_chan(rdev,
4997 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4998 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004999 if (!ibss.channel ||
5000 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005001 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5002 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005003
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005004 /* Both channels should be able to initiate communication */
5005 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5006 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5007 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5008 ibss.channel_type))
5009 return -EINVAL;
5010
Johannes Berg04a773a2009-04-19 21:24:32 +02005011 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005012 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005013
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005014 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5015 u8 *rates =
5016 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5017 int n_rates =
5018 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5019 struct ieee80211_supported_band *sband =
5020 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005021
Johannes Berg34850ab2011-07-18 18:08:35 +02005022 err = ieee80211_get_ratemask(sband, rates, n_rates,
5023 &ibss.basic_rates);
5024 if (err)
5025 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005026 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005027
5028 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5029 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5030 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5031 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005032
Johannes Berg4c476992010-10-04 21:36:35 +02005033 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5034 connkeys = nl80211_parse_connkeys(rdev,
5035 info->attrs[NL80211_ATTR_KEYS]);
5036 if (IS_ERR(connkeys))
5037 return PTR_ERR(connkeys);
5038 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005039
Antonio Quartulli267335d2012-01-31 20:25:47 +01005040 ibss.control_port =
5041 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5042
Johannes Berg4c476992010-10-04 21:36:35 +02005043 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005044 if (err)
5045 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005046 return err;
5047}
5048
5049static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5050{
Johannes Berg4c476992010-10-04 21:36:35 +02005051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5052 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005053
Johannes Berg4c476992010-10-04 21:36:35 +02005054 if (!rdev->ops->leave_ibss)
5055 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005056
Johannes Berg4c476992010-10-04 21:36:35 +02005057 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5058 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005059
Johannes Berg4c476992010-10-04 21:36:35 +02005060 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005061}
5062
Johannes Bergaff89a92009-07-01 21:26:51 +02005063#ifdef CONFIG_NL80211_TESTMODE
5064static struct genl_multicast_group nl80211_testmode_mcgrp = {
5065 .name = "testmode",
5066};
5067
5068static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5069{
Johannes Berg4c476992010-10-04 21:36:35 +02005070 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005071 int err;
5072
5073 if (!info->attrs[NL80211_ATTR_TESTDATA])
5074 return -EINVAL;
5075
Johannes Bergaff89a92009-07-01 21:26:51 +02005076 err = -EOPNOTSUPP;
5077 if (rdev->ops->testmode_cmd) {
5078 rdev->testmode_info = info;
5079 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5080 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5081 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5082 rdev->testmode_info = NULL;
5083 }
5084
Johannes Bergaff89a92009-07-01 21:26:51 +02005085 return err;
5086}
5087
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005088static int nl80211_testmode_dump(struct sk_buff *skb,
5089 struct netlink_callback *cb)
5090{
Johannes Berg00918d32011-12-13 17:22:05 +01005091 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005092 int err;
5093 long phy_idx;
5094 void *data = NULL;
5095 int data_len = 0;
5096
5097 if (cb->args[0]) {
5098 /*
5099 * 0 is a valid index, but not valid for args[0],
5100 * so we need to offset by 1.
5101 */
5102 phy_idx = cb->args[0] - 1;
5103 } else {
5104 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5105 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5106 nl80211_policy);
5107 if (err)
5108 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005109 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5110 phy_idx = nla_get_u32(
5111 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5112 } else {
5113 struct net_device *netdev;
5114
5115 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5116 nl80211_fam.attrbuf,
5117 &rdev, &netdev);
5118 if (err)
5119 return err;
5120 dev_put(netdev);
5121 phy_idx = rdev->wiphy_idx;
5122 cfg80211_unlock_rdev(rdev);
5123 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005124 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5125 cb->args[1] =
5126 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5127 }
5128
5129 if (cb->args[1]) {
5130 data = nla_data((void *)cb->args[1]);
5131 data_len = nla_len((void *)cb->args[1]);
5132 }
5133
5134 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005135 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5136 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005137 mutex_unlock(&cfg80211_mutex);
5138 return -ENOENT;
5139 }
Johannes Berg00918d32011-12-13 17:22:05 +01005140 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005141 mutex_unlock(&cfg80211_mutex);
5142
Johannes Berg00918d32011-12-13 17:22:05 +01005143 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005144 err = -EOPNOTSUPP;
5145 goto out_err;
5146 }
5147
5148 while (1) {
5149 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5150 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5151 NL80211_CMD_TESTMODE);
5152 struct nlattr *tmdata;
5153
David S. Miller9360ffd2012-03-29 04:41:26 -04005154 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005155 genlmsg_cancel(skb, hdr);
5156 break;
5157 }
5158
5159 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5160 if (!tmdata) {
5161 genlmsg_cancel(skb, hdr);
5162 break;
5163 }
Johannes Berg00918d32011-12-13 17:22:05 +01005164 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5165 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005166 nla_nest_end(skb, tmdata);
5167
5168 if (err == -ENOBUFS || err == -ENOENT) {
5169 genlmsg_cancel(skb, hdr);
5170 break;
5171 } else if (err) {
5172 genlmsg_cancel(skb, hdr);
5173 goto out_err;
5174 }
5175
5176 genlmsg_end(skb, hdr);
5177 }
5178
5179 err = skb->len;
5180 /* see above */
5181 cb->args[0] = phy_idx + 1;
5182 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005183 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005184 return err;
5185}
5186
Johannes Bergaff89a92009-07-01 21:26:51 +02005187static struct sk_buff *
5188__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5189 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5190{
5191 struct sk_buff *skb;
5192 void *hdr;
5193 struct nlattr *data;
5194
5195 skb = nlmsg_new(approxlen + 100, gfp);
5196 if (!skb)
5197 return NULL;
5198
5199 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5200 if (!hdr) {
5201 kfree_skb(skb);
5202 return NULL;
5203 }
5204
David S. Miller9360ffd2012-03-29 04:41:26 -04005205 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5206 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005207 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5208
5209 ((void **)skb->cb)[0] = rdev;
5210 ((void **)skb->cb)[1] = hdr;
5211 ((void **)skb->cb)[2] = data;
5212
5213 return skb;
5214
5215 nla_put_failure:
5216 kfree_skb(skb);
5217 return NULL;
5218}
5219
5220struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5221 int approxlen)
5222{
5223 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5224
5225 if (WARN_ON(!rdev->testmode_info))
5226 return NULL;
5227
5228 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5229 rdev->testmode_info->snd_pid,
5230 rdev->testmode_info->snd_seq,
5231 GFP_KERNEL);
5232}
5233EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5234
5235int cfg80211_testmode_reply(struct sk_buff *skb)
5236{
5237 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5238 void *hdr = ((void **)skb->cb)[1];
5239 struct nlattr *data = ((void **)skb->cb)[2];
5240
5241 if (WARN_ON(!rdev->testmode_info)) {
5242 kfree_skb(skb);
5243 return -EINVAL;
5244 }
5245
5246 nla_nest_end(skb, data);
5247 genlmsg_end(skb, hdr);
5248 return genlmsg_reply(skb, rdev->testmode_info);
5249}
5250EXPORT_SYMBOL(cfg80211_testmode_reply);
5251
5252struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5253 int approxlen, gfp_t gfp)
5254{
5255 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5256
5257 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5258}
5259EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5260
5261void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5262{
5263 void *hdr = ((void **)skb->cb)[1];
5264 struct nlattr *data = ((void **)skb->cb)[2];
5265
5266 nla_nest_end(skb, data);
5267 genlmsg_end(skb, hdr);
5268 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5269}
5270EXPORT_SYMBOL(cfg80211_testmode_event);
5271#endif
5272
Samuel Ortizb23aa672009-07-01 21:26:54 +02005273static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5274{
Johannes Berg4c476992010-10-04 21:36:35 +02005275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5276 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005277 struct cfg80211_connect_params connect;
5278 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005279 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005280 int err;
5281
5282 memset(&connect, 0, sizeof(connect));
5283
5284 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5285 return -EINVAL;
5286
5287 if (!info->attrs[NL80211_ATTR_SSID] ||
5288 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5289 return -EINVAL;
5290
5291 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5292 connect.auth_type =
5293 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5294 if (!nl80211_valid_auth_type(connect.auth_type))
5295 return -EINVAL;
5296 } else
5297 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5298
5299 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5300
Johannes Bergc0692b82010-08-27 14:26:53 +03005301 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005302 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005303 if (err)
5304 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005305
Johannes Berg074ac8d2010-09-16 14:58:22 +02005306 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005307 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5308 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005309
Johannes Berg79c97e92009-07-07 03:56:12 +02005310 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005311
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305312 connect.bg_scan_period = -1;
5313 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5314 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5315 connect.bg_scan_period =
5316 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5317 }
5318
Samuel Ortizb23aa672009-07-01 21:26:54 +02005319 if (info->attrs[NL80211_ATTR_MAC])
5320 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5321 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5322 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5323
5324 if (info->attrs[NL80211_ATTR_IE]) {
5325 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5326 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5327 }
5328
5329 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5330 connect.channel =
5331 ieee80211_get_channel(wiphy,
5332 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5333 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005334 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5335 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005336 }
5337
Johannes Bergfffd0932009-07-08 14:22:54 +02005338 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5339 connkeys = nl80211_parse_connkeys(rdev,
5340 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005341 if (IS_ERR(connkeys))
5342 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005343 }
5344
Ben Greear7e7c8922011-11-18 11:31:59 -08005345 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5346 connect.flags |= ASSOC_REQ_DISABLE_HT;
5347
5348 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5349 memcpy(&connect.ht_capa_mask,
5350 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5351 sizeof(connect.ht_capa_mask));
5352
5353 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5354 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5355 return -EINVAL;
5356 memcpy(&connect.ht_capa,
5357 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5358 sizeof(connect.ht_capa));
5359 }
5360
Johannes Bergfffd0932009-07-08 14:22:54 +02005361 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005362 if (err)
5363 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005364 return err;
5365}
5366
5367static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5368{
Johannes Berg4c476992010-10-04 21:36:35 +02005369 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5370 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005371 u16 reason;
5372
5373 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5374 reason = WLAN_REASON_DEAUTH_LEAVING;
5375 else
5376 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5377
5378 if (reason == 0)
5379 return -EINVAL;
5380
Johannes Berg074ac8d2010-09-16 14:58:22 +02005381 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005382 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5383 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005384
Johannes Berg4c476992010-10-04 21:36:35 +02005385 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005386}
5387
Johannes Berg463d0182009-07-14 00:33:35 +02005388static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5389{
Johannes Berg4c476992010-10-04 21:36:35 +02005390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005391 struct net *net;
5392 int err;
5393 u32 pid;
5394
5395 if (!info->attrs[NL80211_ATTR_PID])
5396 return -EINVAL;
5397
5398 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5399
Johannes Berg463d0182009-07-14 00:33:35 +02005400 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005401 if (IS_ERR(net))
5402 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005403
5404 err = 0;
5405
5406 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005407 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5408 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005409
Johannes Berg463d0182009-07-14 00:33:35 +02005410 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005411 return err;
5412}
5413
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005414static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5415{
Johannes Berg4c476992010-10-04 21:36:35 +02005416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005417 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5418 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005419 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005420 struct cfg80211_pmksa pmksa;
5421
5422 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5423
5424 if (!info->attrs[NL80211_ATTR_MAC])
5425 return -EINVAL;
5426
5427 if (!info->attrs[NL80211_ATTR_PMKID])
5428 return -EINVAL;
5429
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005430 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5431 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5432
Johannes Berg074ac8d2010-09-16 14:58:22 +02005433 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005434 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5435 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005436
5437 switch (info->genlhdr->cmd) {
5438 case NL80211_CMD_SET_PMKSA:
5439 rdev_ops = rdev->ops->set_pmksa;
5440 break;
5441 case NL80211_CMD_DEL_PMKSA:
5442 rdev_ops = rdev->ops->del_pmksa;
5443 break;
5444 default:
5445 WARN_ON(1);
5446 break;
5447 }
5448
Johannes Berg4c476992010-10-04 21:36:35 +02005449 if (!rdev_ops)
5450 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005451
Johannes Berg4c476992010-10-04 21:36:35 +02005452 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005453}
5454
5455static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5456{
Johannes Berg4c476992010-10-04 21:36:35 +02005457 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5458 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005459
Johannes Berg074ac8d2010-09-16 14:58:22 +02005460 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005461 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5462 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005463
Johannes Berg4c476992010-10-04 21:36:35 +02005464 if (!rdev->ops->flush_pmksa)
5465 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005466
Johannes Berg4c476992010-10-04 21:36:35 +02005467 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005468}
5469
Arik Nemtsov109086c2011-09-28 14:12:50 +03005470static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5471{
5472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5473 struct net_device *dev = info->user_ptr[1];
5474 u8 action_code, dialog_token;
5475 u16 status_code;
5476 u8 *peer;
5477
5478 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5479 !rdev->ops->tdls_mgmt)
5480 return -EOPNOTSUPP;
5481
5482 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5483 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5484 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5485 !info->attrs[NL80211_ATTR_IE] ||
5486 !info->attrs[NL80211_ATTR_MAC])
5487 return -EINVAL;
5488
5489 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5490 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5491 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5492 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5493
5494 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5495 dialog_token, status_code,
5496 nla_data(info->attrs[NL80211_ATTR_IE]),
5497 nla_len(info->attrs[NL80211_ATTR_IE]));
5498}
5499
5500static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5501{
5502 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5503 struct net_device *dev = info->user_ptr[1];
5504 enum nl80211_tdls_operation operation;
5505 u8 *peer;
5506
5507 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5508 !rdev->ops->tdls_oper)
5509 return -EOPNOTSUPP;
5510
5511 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5512 !info->attrs[NL80211_ATTR_MAC])
5513 return -EINVAL;
5514
5515 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5516 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5517
5518 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5519}
5520
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005521static int nl80211_remain_on_channel(struct sk_buff *skb,
5522 struct genl_info *info)
5523{
Johannes Berg4c476992010-10-04 21:36:35 +02005524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5525 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005526 struct ieee80211_channel *chan;
5527 struct sk_buff *msg;
5528 void *hdr;
5529 u64 cookie;
5530 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5531 u32 freq, duration;
5532 int err;
5533
5534 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5535 !info->attrs[NL80211_ATTR_DURATION])
5536 return -EINVAL;
5537
5538 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5539
5540 /*
5541 * We should be on that channel for at least one jiffie,
5542 * and more than 5 seconds seems excessive.
5543 */
Johannes Berga2939112010-12-14 17:54:28 +01005544 if (!duration || !msecs_to_jiffies(duration) ||
5545 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005546 return -EINVAL;
5547
Johannes Berg7c4ef712011-11-18 15:33:48 +01005548 if (!rdev->ops->remain_on_channel ||
5549 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005550 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005551
Johannes Bergcd6c6592012-05-10 21:27:18 +02005552 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5553 !nl80211_valid_channel_type(info, &channel_type))
5554 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005555
5556 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5557 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005558 if (chan == NULL)
5559 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005560
5561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005562 if (!msg)
5563 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005564
5565 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5566 NL80211_CMD_REMAIN_ON_CHANNEL);
5567
5568 if (IS_ERR(hdr)) {
5569 err = PTR_ERR(hdr);
5570 goto free_msg;
5571 }
5572
5573 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5574 channel_type, duration, &cookie);
5575
5576 if (err)
5577 goto free_msg;
5578
David S. Miller9360ffd2012-03-29 04:41:26 -04005579 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5580 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005581
5582 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005583
5584 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005585
5586 nla_put_failure:
5587 err = -ENOBUFS;
5588 free_msg:
5589 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005590 return err;
5591}
5592
5593static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5594 struct genl_info *info)
5595{
Johannes Berg4c476992010-10-04 21:36:35 +02005596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5597 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005598 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005599
5600 if (!info->attrs[NL80211_ATTR_COOKIE])
5601 return -EINVAL;
5602
Johannes Berg4c476992010-10-04 21:36:35 +02005603 if (!rdev->ops->cancel_remain_on_channel)
5604 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005605
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005606 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5607
Johannes Berg4c476992010-10-04 21:36:35 +02005608 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005609}
5610
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005611static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5612 u8 *rates, u8 rates_len)
5613{
5614 u8 i;
5615 u32 mask = 0;
5616
5617 for (i = 0; i < rates_len; i++) {
5618 int rate = (rates[i] & 0x7f) * 5;
5619 int ridx;
5620 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5621 struct ieee80211_rate *srate =
5622 &sband->bitrates[ridx];
5623 if (rate == srate->bitrate) {
5624 mask |= 1 << ridx;
5625 break;
5626 }
5627 }
5628 if (ridx == sband->n_bitrates)
5629 return 0; /* rate not found */
5630 }
5631
5632 return mask;
5633}
5634
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005635static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5636 u8 *rates, u8 rates_len,
5637 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5638{
5639 u8 i;
5640
5641 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5642
5643 for (i = 0; i < rates_len; i++) {
5644 int ridx, rbit;
5645
5646 ridx = rates[i] / 8;
5647 rbit = BIT(rates[i] % 8);
5648
5649 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005650 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005651 return false;
5652
5653 /* check availability */
5654 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5655 mcs[ridx] |= rbit;
5656 else
5657 return false;
5658 }
5659
5660 return true;
5661}
5662
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005663static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005664 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5665 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005666 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5667 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005668};
5669
5670static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5671 struct genl_info *info)
5672{
5673 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005675 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005676 int rem, i;
5677 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005678 struct nlattr *tx_rates;
5679 struct ieee80211_supported_band *sband;
5680
5681 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5682 return -EINVAL;
5683
Johannes Berg4c476992010-10-04 21:36:35 +02005684 if (!rdev->ops->set_bitrate_mask)
5685 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005686
5687 memset(&mask, 0, sizeof(mask));
5688 /* Default to all rates enabled */
5689 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5690 sband = rdev->wiphy.bands[i];
5691 mask.control[i].legacy =
5692 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005693 if (sband)
5694 memcpy(mask.control[i].mcs,
5695 sband->ht_cap.mcs.rx_mask,
5696 sizeof(mask.control[i].mcs));
5697 else
5698 memset(mask.control[i].mcs, 0,
5699 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005700 }
5701
5702 /*
5703 * The nested attribute uses enum nl80211_band as the index. This maps
5704 * directly to the enum ieee80211_band values used in cfg80211.
5705 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005706 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005707 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5708 {
5709 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005710 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5711 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005712 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005713 if (sband == NULL)
5714 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005715 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5716 nla_len(tx_rates), nl80211_txattr_policy);
5717 if (tb[NL80211_TXRATE_LEGACY]) {
5718 mask.control[band].legacy = rateset_to_mask(
5719 sband,
5720 nla_data(tb[NL80211_TXRATE_LEGACY]),
5721 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305722 if ((mask.control[band].legacy == 0) &&
5723 nla_len(tb[NL80211_TXRATE_LEGACY]))
5724 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005725 }
5726 if (tb[NL80211_TXRATE_MCS]) {
5727 if (!ht_rateset_to_mask(
5728 sband,
5729 nla_data(tb[NL80211_TXRATE_MCS]),
5730 nla_len(tb[NL80211_TXRATE_MCS]),
5731 mask.control[band].mcs))
5732 return -EINVAL;
5733 }
5734
5735 if (mask.control[band].legacy == 0) {
5736 /* don't allow empty legacy rates if HT
5737 * is not even supported. */
5738 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5739 return -EINVAL;
5740
5741 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5742 if (mask.control[band].mcs[i])
5743 break;
5744
5745 /* legacy and mcs rates may not be both empty */
5746 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005747 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005748 }
5749 }
5750
Johannes Berg4c476992010-10-04 21:36:35 +02005751 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005752}
5753
Johannes Berg2e161f72010-08-12 15:38:38 +02005754static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005755{
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];
Johannes Berg2e161f72010-08-12 15:38:38 +02005758 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005759
5760 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5761 return -EINVAL;
5762
Johannes Berg2e161f72010-08-12 15:38:38 +02005763 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5764 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005765
Johannes Berg9d38d852010-06-09 17:20:33 +02005766 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005767 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005768 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5770 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005771 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005772 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5773 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005774
5775 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005776 if (!rdev->ops->mgmt_tx)
5777 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005778
Johannes Berg4c476992010-10-04 21:36:35 +02005779 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005780 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005781 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5782 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005783}
5784
Johannes Berg2e161f72010-08-12 15:38:38 +02005785static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005786{
Johannes Berg4c476992010-10-04 21:36:35 +02005787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5788 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005789 struct ieee80211_channel *chan;
5790 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005791 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005792 u32 freq;
5793 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005794 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005795 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005796 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005797 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005798 bool offchan, no_cck, dont_wait_for_ack;
5799
5800 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005801
5802 if (!info->attrs[NL80211_ATTR_FRAME] ||
5803 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5804 return -EINVAL;
5805
Johannes Berg4c476992010-10-04 21:36:35 +02005806 if (!rdev->ops->mgmt_tx)
5807 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005808
Johannes Berg9d38d852010-06-09 17:20:33 +02005809 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005810 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005811 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5812 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5813 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005814 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005815 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5816 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005817
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005818 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005819 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005820 return -EINVAL;
5821 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5822 }
5823
Jouni Malinen026331c2010-02-15 12:53:10 +02005824 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02005825 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02005826 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005827 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005828 }
5829
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005830 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5831
Johannes Berg7c4ef712011-11-18 15:33:48 +01005832 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5833 return -EINVAL;
5834
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305835 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5836
Jouni Malinen026331c2010-02-15 12:53:10 +02005837 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5838 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005839 if (chan == NULL)
5840 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005841
Johannes Berge247bd902011-11-04 11:18:21 +01005842 if (!dont_wait_for_ack) {
5843 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5844 if (!msg)
5845 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005846
Johannes Berge247bd902011-11-04 11:18:21 +01005847 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5848 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005849
Johannes Berge247bd902011-11-04 11:18:21 +01005850 if (IS_ERR(hdr)) {
5851 err = PTR_ERR(hdr);
5852 goto free_msg;
5853 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005854 }
Johannes Berge247bd902011-11-04 11:18:21 +01005855
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005856 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5857 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005858 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5859 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005860 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005861 if (err)
5862 goto free_msg;
5863
Johannes Berge247bd902011-11-04 11:18:21 +01005864 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005865 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5866 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005867
Johannes Berge247bd902011-11-04 11:18:21 +01005868 genlmsg_end(msg, hdr);
5869 return genlmsg_reply(msg, info);
5870 }
5871
5872 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005873
5874 nla_put_failure:
5875 err = -ENOBUFS;
5876 free_msg:
5877 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005878 return err;
5879}
5880
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005881static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5882{
5883 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5884 struct net_device *dev = info->user_ptr[1];
5885 u64 cookie;
5886
5887 if (!info->attrs[NL80211_ATTR_COOKIE])
5888 return -EINVAL;
5889
5890 if (!rdev->ops->mgmt_tx_cancel_wait)
5891 return -EOPNOTSUPP;
5892
5893 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5894 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5895 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5896 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5897 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5898 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5899 return -EOPNOTSUPP;
5900
5901 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5902
5903 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5904}
5905
Kalle Valoffb9eb32010-02-17 17:58:10 +02005906static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5907{
Johannes Berg4c476992010-10-04 21:36:35 +02005908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005909 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005910 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005911 u8 ps_state;
5912 bool state;
5913 int err;
5914
Johannes Berg4c476992010-10-04 21:36:35 +02005915 if (!info->attrs[NL80211_ATTR_PS_STATE])
5916 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005917
5918 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5919
Johannes Berg4c476992010-10-04 21:36:35 +02005920 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5921 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005922
5923 wdev = dev->ieee80211_ptr;
5924
Johannes Berg4c476992010-10-04 21:36:35 +02005925 if (!rdev->ops->set_power_mgmt)
5926 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005927
5928 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5929
5930 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005931 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005932
Johannes Berg4c476992010-10-04 21:36:35 +02005933 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5934 wdev->ps_timeout);
5935 if (!err)
5936 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005937 return err;
5938}
5939
5940static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5941{
Johannes Berg4c476992010-10-04 21:36:35 +02005942 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005943 enum nl80211_ps_state ps_state;
5944 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005945 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005946 struct sk_buff *msg;
5947 void *hdr;
5948 int err;
5949
Kalle Valoffb9eb32010-02-17 17:58:10 +02005950 wdev = dev->ieee80211_ptr;
5951
Johannes Berg4c476992010-10-04 21:36:35 +02005952 if (!rdev->ops->set_power_mgmt)
5953 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005954
5955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005956 if (!msg)
5957 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005958
5959 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5960 NL80211_CMD_GET_POWER_SAVE);
5961 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005962 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005963 goto free_msg;
5964 }
5965
5966 if (wdev->ps)
5967 ps_state = NL80211_PS_ENABLED;
5968 else
5969 ps_state = NL80211_PS_DISABLED;
5970
David S. Miller9360ffd2012-03-29 04:41:26 -04005971 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5972 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005973
5974 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005975 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005976
Johannes Berg4c476992010-10-04 21:36:35 +02005977 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005978 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005979 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005980 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005981 return err;
5982}
5983
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005984static struct nla_policy
5985nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5986 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5987 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5988 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5989};
5990
5991static int nl80211_set_cqm_rssi(struct genl_info *info,
5992 s32 threshold, u32 hysteresis)
5993{
Johannes Berg4c476992010-10-04 21:36:35 +02005994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005995 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005996 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005997
5998 if (threshold > 0)
5999 return -EINVAL;
6000
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006001 wdev = dev->ieee80211_ptr;
6002
Johannes Berg4c476992010-10-04 21:36:35 +02006003 if (!rdev->ops->set_cqm_rssi_config)
6004 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006005
Johannes Berg074ac8d2010-09-16 14:58:22 +02006006 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006007 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6008 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006009
Johannes Berg4c476992010-10-04 21:36:35 +02006010 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6011 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006012}
6013
6014static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6015{
6016 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6017 struct nlattr *cqm;
6018 int err;
6019
6020 cqm = info->attrs[NL80211_ATTR_CQM];
6021 if (!cqm) {
6022 err = -EINVAL;
6023 goto out;
6024 }
6025
6026 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6027 nl80211_attr_cqm_policy);
6028 if (err)
6029 goto out;
6030
6031 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6032 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6033 s32 threshold;
6034 u32 hysteresis;
6035 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6036 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6037 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6038 } else
6039 err = -EINVAL;
6040
6041out:
6042 return err;
6043}
6044
Johannes Berg29cbe682010-12-03 09:20:44 +01006045static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6046{
6047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6048 struct net_device *dev = info->user_ptr[1];
6049 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006050 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006051 int err;
6052
6053 /* start with default */
6054 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006055 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006056
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006057 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006058 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006059 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006060 if (err)
6061 return err;
6062 }
6063
6064 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6065 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6066 return -EINVAL;
6067
Javier Cardonac80d5452010-12-16 17:37:49 -08006068 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6069 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6070
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006071 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6072 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6073 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6074 return -EINVAL;
6075
Javier Cardonac80d5452010-12-16 17:37:49 -08006076 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6077 /* parse additional setup parameters if given */
6078 err = nl80211_parse_mesh_setup(info, &setup);
6079 if (err)
6080 return err;
6081 }
6082
6083 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006084}
6085
6086static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6087{
6088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6089 struct net_device *dev = info->user_ptr[1];
6090
6091 return cfg80211_leave_mesh(rdev, dev);
6092}
6093
Johannes Bergff1b6e62011-05-04 15:37:28 +02006094static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6095{
6096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6097 struct sk_buff *msg;
6098 void *hdr;
6099
6100 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6101 return -EOPNOTSUPP;
6102
6103 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6104 if (!msg)
6105 return -ENOMEM;
6106
6107 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6108 NL80211_CMD_GET_WOWLAN);
6109 if (!hdr)
6110 goto nla_put_failure;
6111
6112 if (rdev->wowlan) {
6113 struct nlattr *nl_wowlan;
6114
6115 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6116 if (!nl_wowlan)
6117 goto nla_put_failure;
6118
David S. Miller9360ffd2012-03-29 04:41:26 -04006119 if ((rdev->wowlan->any &&
6120 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6121 (rdev->wowlan->disconnect &&
6122 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6123 (rdev->wowlan->magic_pkt &&
6124 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6125 (rdev->wowlan->gtk_rekey_failure &&
6126 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6127 (rdev->wowlan->eap_identity_req &&
6128 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6129 (rdev->wowlan->four_way_handshake &&
6130 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6131 (rdev->wowlan->rfkill_release &&
6132 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6133 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006134 if (rdev->wowlan->n_patterns) {
6135 struct nlattr *nl_pats, *nl_pat;
6136 int i, pat_len;
6137
6138 nl_pats = nla_nest_start(msg,
6139 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6140 if (!nl_pats)
6141 goto nla_put_failure;
6142
6143 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6144 nl_pat = nla_nest_start(msg, i + 1);
6145 if (!nl_pat)
6146 goto nla_put_failure;
6147 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006148 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6149 DIV_ROUND_UP(pat_len, 8),
6150 rdev->wowlan->patterns[i].mask) ||
6151 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6152 pat_len,
6153 rdev->wowlan->patterns[i].pattern))
6154 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006155 nla_nest_end(msg, nl_pat);
6156 }
6157 nla_nest_end(msg, nl_pats);
6158 }
6159
6160 nla_nest_end(msg, nl_wowlan);
6161 }
6162
6163 genlmsg_end(msg, hdr);
6164 return genlmsg_reply(msg, info);
6165
6166nla_put_failure:
6167 nlmsg_free(msg);
6168 return -ENOBUFS;
6169}
6170
6171static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6172{
6173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6174 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6175 struct cfg80211_wowlan no_triggers = {};
6176 struct cfg80211_wowlan new_triggers = {};
6177 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6178 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006179 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006180
6181 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6182 return -EOPNOTSUPP;
6183
6184 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6185 goto no_triggers;
6186
6187 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6188 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6189 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6190 nl80211_wowlan_policy);
6191 if (err)
6192 return err;
6193
6194 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6195 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6196 return -EINVAL;
6197 new_triggers.any = true;
6198 }
6199
6200 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6201 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6202 return -EINVAL;
6203 new_triggers.disconnect = true;
6204 }
6205
6206 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6207 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6208 return -EINVAL;
6209 new_triggers.magic_pkt = true;
6210 }
6211
Johannes Berg77dbbb12011-07-13 10:48:55 +02006212 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6213 return -EINVAL;
6214
6215 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6216 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6217 return -EINVAL;
6218 new_triggers.gtk_rekey_failure = true;
6219 }
6220
6221 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6222 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6223 return -EINVAL;
6224 new_triggers.eap_identity_req = true;
6225 }
6226
6227 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6228 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6229 return -EINVAL;
6230 new_triggers.four_way_handshake = true;
6231 }
6232
6233 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6234 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6235 return -EINVAL;
6236 new_triggers.rfkill_release = true;
6237 }
6238
Johannes Bergff1b6e62011-05-04 15:37:28 +02006239 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6240 struct nlattr *pat;
6241 int n_patterns = 0;
6242 int rem, pat_len, mask_len;
6243 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6244
6245 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6246 rem)
6247 n_patterns++;
6248 if (n_patterns > wowlan->n_patterns)
6249 return -EINVAL;
6250
6251 new_triggers.patterns = kcalloc(n_patterns,
6252 sizeof(new_triggers.patterns[0]),
6253 GFP_KERNEL);
6254 if (!new_triggers.patterns)
6255 return -ENOMEM;
6256
6257 new_triggers.n_patterns = n_patterns;
6258 i = 0;
6259
6260 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6261 rem) {
6262 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6263 nla_data(pat), nla_len(pat), NULL);
6264 err = -EINVAL;
6265 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6266 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6267 goto error;
6268 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6269 mask_len = DIV_ROUND_UP(pat_len, 8);
6270 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6271 mask_len)
6272 goto error;
6273 if (pat_len > wowlan->pattern_max_len ||
6274 pat_len < wowlan->pattern_min_len)
6275 goto error;
6276
6277 new_triggers.patterns[i].mask =
6278 kmalloc(mask_len + pat_len, GFP_KERNEL);
6279 if (!new_triggers.patterns[i].mask) {
6280 err = -ENOMEM;
6281 goto error;
6282 }
6283 new_triggers.patterns[i].pattern =
6284 new_triggers.patterns[i].mask + mask_len;
6285 memcpy(new_triggers.patterns[i].mask,
6286 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6287 mask_len);
6288 new_triggers.patterns[i].pattern_len = pat_len;
6289 memcpy(new_triggers.patterns[i].pattern,
6290 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6291 pat_len);
6292 i++;
6293 }
6294 }
6295
6296 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6297 struct cfg80211_wowlan *ntrig;
6298 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6299 GFP_KERNEL);
6300 if (!ntrig) {
6301 err = -ENOMEM;
6302 goto error;
6303 }
6304 cfg80211_rdev_free_wowlan(rdev);
6305 rdev->wowlan = ntrig;
6306 } else {
6307 no_triggers:
6308 cfg80211_rdev_free_wowlan(rdev);
6309 rdev->wowlan = NULL;
6310 }
6311
Johannes Berg6d525632012-04-04 15:05:25 +02006312 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6313 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6314
Johannes Bergff1b6e62011-05-04 15:37:28 +02006315 return 0;
6316 error:
6317 for (i = 0; i < new_triggers.n_patterns; i++)
6318 kfree(new_triggers.patterns[i].mask);
6319 kfree(new_triggers.patterns);
6320 return err;
6321}
6322
Johannes Berge5497d72011-07-05 16:35:40 +02006323static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6324{
6325 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6326 struct net_device *dev = info->user_ptr[1];
6327 struct wireless_dev *wdev = dev->ieee80211_ptr;
6328 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6329 struct cfg80211_gtk_rekey_data rekey_data;
6330 int err;
6331
6332 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6333 return -EINVAL;
6334
6335 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6336 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6337 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6338 nl80211_rekey_policy);
6339 if (err)
6340 return err;
6341
6342 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6343 return -ERANGE;
6344 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6345 return -ERANGE;
6346 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6347 return -ERANGE;
6348
6349 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6350 NL80211_KEK_LEN);
6351 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6352 NL80211_KCK_LEN);
6353 memcpy(rekey_data.replay_ctr,
6354 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6355 NL80211_REPLAY_CTR_LEN);
6356
6357 wdev_lock(wdev);
6358 if (!wdev->current_bss) {
6359 err = -ENOTCONN;
6360 goto out;
6361 }
6362
6363 if (!rdev->ops->set_rekey_data) {
6364 err = -EOPNOTSUPP;
6365 goto out;
6366 }
6367
6368 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6369 out:
6370 wdev_unlock(wdev);
6371 return err;
6372}
6373
Johannes Berg28946da2011-11-04 11:18:12 +01006374static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6375 struct genl_info *info)
6376{
6377 struct net_device *dev = info->user_ptr[1];
6378 struct wireless_dev *wdev = dev->ieee80211_ptr;
6379
6380 if (wdev->iftype != NL80211_IFTYPE_AP &&
6381 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6382 return -EINVAL;
6383
6384 if (wdev->ap_unexpected_nlpid)
6385 return -EBUSY;
6386
6387 wdev->ap_unexpected_nlpid = info->snd_pid;
6388 return 0;
6389}
6390
Johannes Berg7f6cf312011-11-04 11:18:15 +01006391static int nl80211_probe_client(struct sk_buff *skb,
6392 struct genl_info *info)
6393{
6394 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6395 struct net_device *dev = info->user_ptr[1];
6396 struct wireless_dev *wdev = dev->ieee80211_ptr;
6397 struct sk_buff *msg;
6398 void *hdr;
6399 const u8 *addr;
6400 u64 cookie;
6401 int err;
6402
6403 if (wdev->iftype != NL80211_IFTYPE_AP &&
6404 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6405 return -EOPNOTSUPP;
6406
6407 if (!info->attrs[NL80211_ATTR_MAC])
6408 return -EINVAL;
6409
6410 if (!rdev->ops->probe_client)
6411 return -EOPNOTSUPP;
6412
6413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6414 if (!msg)
6415 return -ENOMEM;
6416
6417 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6418 NL80211_CMD_PROBE_CLIENT);
6419
6420 if (IS_ERR(hdr)) {
6421 err = PTR_ERR(hdr);
6422 goto free_msg;
6423 }
6424
6425 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6426
6427 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6428 if (err)
6429 goto free_msg;
6430
David S. Miller9360ffd2012-03-29 04:41:26 -04006431 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6432 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006433
6434 genlmsg_end(msg, hdr);
6435
6436 return genlmsg_reply(msg, info);
6437
6438 nla_put_failure:
6439 err = -ENOBUFS;
6440 free_msg:
6441 nlmsg_free(msg);
6442 return err;
6443}
6444
Johannes Berg5e760232011-11-04 11:18:17 +01006445static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6446{
6447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6448
6449 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6450 return -EOPNOTSUPP;
6451
6452 if (rdev->ap_beacons_nlpid)
6453 return -EBUSY;
6454
6455 rdev->ap_beacons_nlpid = info->snd_pid;
6456
6457 return 0;
6458}
6459
Johannes Berg4c476992010-10-04 21:36:35 +02006460#define NL80211_FLAG_NEED_WIPHY 0x01
6461#define NL80211_FLAG_NEED_NETDEV 0x02
6462#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006463#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6464#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6465 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006466
6467static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6468 struct genl_info *info)
6469{
6470 struct cfg80211_registered_device *rdev;
6471 struct net_device *dev;
6472 int err;
6473 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6474
6475 if (rtnl)
6476 rtnl_lock();
6477
6478 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6479 rdev = cfg80211_get_dev_from_info(info);
6480 if (IS_ERR(rdev)) {
6481 if (rtnl)
6482 rtnl_unlock();
6483 return PTR_ERR(rdev);
6484 }
6485 info->user_ptr[0] = rdev;
6486 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006487 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6488 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006489 if (err) {
6490 if (rtnl)
6491 rtnl_unlock();
6492 return err;
6493 }
Johannes Berg41265712010-10-04 21:14:05 +02006494 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6495 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006496 cfg80211_unlock_rdev(rdev);
6497 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006498 if (rtnl)
6499 rtnl_unlock();
6500 return -ENETDOWN;
6501 }
Johannes Berg4c476992010-10-04 21:36:35 +02006502 info->user_ptr[0] = rdev;
6503 info->user_ptr[1] = dev;
6504 }
6505
6506 return 0;
6507}
6508
6509static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6510 struct genl_info *info)
6511{
6512 if (info->user_ptr[0])
6513 cfg80211_unlock_rdev(info->user_ptr[0]);
6514 if (info->user_ptr[1])
6515 dev_put(info->user_ptr[1]);
6516 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6517 rtnl_unlock();
6518}
6519
Johannes Berg55682962007-09-20 13:09:35 -04006520static struct genl_ops nl80211_ops[] = {
6521 {
6522 .cmd = NL80211_CMD_GET_WIPHY,
6523 .doit = nl80211_get_wiphy,
6524 .dumpit = nl80211_dump_wiphy,
6525 .policy = nl80211_policy,
6526 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006527 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006528 },
6529 {
6530 .cmd = NL80211_CMD_SET_WIPHY,
6531 .doit = nl80211_set_wiphy,
6532 .policy = nl80211_policy,
6533 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006534 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006535 },
6536 {
6537 .cmd = NL80211_CMD_GET_INTERFACE,
6538 .doit = nl80211_get_interface,
6539 .dumpit = nl80211_dump_interface,
6540 .policy = nl80211_policy,
6541 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006542 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006543 },
6544 {
6545 .cmd = NL80211_CMD_SET_INTERFACE,
6546 .doit = nl80211_set_interface,
6547 .policy = nl80211_policy,
6548 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006549 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6550 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006551 },
6552 {
6553 .cmd = NL80211_CMD_NEW_INTERFACE,
6554 .doit = nl80211_new_interface,
6555 .policy = nl80211_policy,
6556 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006557 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6558 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006559 },
6560 {
6561 .cmd = NL80211_CMD_DEL_INTERFACE,
6562 .doit = nl80211_del_interface,
6563 .policy = nl80211_policy,
6564 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006565 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6566 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006567 },
Johannes Berg41ade002007-12-19 02:03:29 +01006568 {
6569 .cmd = NL80211_CMD_GET_KEY,
6570 .doit = nl80211_get_key,
6571 .policy = nl80211_policy,
6572 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006573 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006574 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006575 },
6576 {
6577 .cmd = NL80211_CMD_SET_KEY,
6578 .doit = nl80211_set_key,
6579 .policy = nl80211_policy,
6580 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006581 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006582 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006583 },
6584 {
6585 .cmd = NL80211_CMD_NEW_KEY,
6586 .doit = nl80211_new_key,
6587 .policy = nl80211_policy,
6588 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006589 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006590 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006591 },
6592 {
6593 .cmd = NL80211_CMD_DEL_KEY,
6594 .doit = nl80211_del_key,
6595 .policy = nl80211_policy,
6596 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006597 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006598 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006599 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006600 {
6601 .cmd = NL80211_CMD_SET_BEACON,
6602 .policy = nl80211_policy,
6603 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006604 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006605 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006606 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006607 },
6608 {
Johannes Berg88600202012-02-13 15:17:18 +01006609 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006610 .policy = nl80211_policy,
6611 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006612 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006613 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006614 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006615 },
6616 {
Johannes Berg88600202012-02-13 15:17:18 +01006617 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006618 .policy = nl80211_policy,
6619 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006620 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006621 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006622 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006623 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006624 {
6625 .cmd = NL80211_CMD_GET_STATION,
6626 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006627 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006628 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006629 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6630 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006631 },
6632 {
6633 .cmd = NL80211_CMD_SET_STATION,
6634 .doit = nl80211_set_station,
6635 .policy = nl80211_policy,
6636 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006638 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006639 },
6640 {
6641 .cmd = NL80211_CMD_NEW_STATION,
6642 .doit = nl80211_new_station,
6643 .policy = nl80211_policy,
6644 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006645 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006646 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006647 },
6648 {
6649 .cmd = NL80211_CMD_DEL_STATION,
6650 .doit = nl80211_del_station,
6651 .policy = nl80211_policy,
6652 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006653 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006654 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006655 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006656 {
6657 .cmd = NL80211_CMD_GET_MPATH,
6658 .doit = nl80211_get_mpath,
6659 .dumpit = nl80211_dump_mpath,
6660 .policy = nl80211_policy,
6661 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006663 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006664 },
6665 {
6666 .cmd = NL80211_CMD_SET_MPATH,
6667 .doit = nl80211_set_mpath,
6668 .policy = nl80211_policy,
6669 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006671 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006672 },
6673 {
6674 .cmd = NL80211_CMD_NEW_MPATH,
6675 .doit = nl80211_new_mpath,
6676 .policy = nl80211_policy,
6677 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006678 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006679 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006680 },
6681 {
6682 .cmd = NL80211_CMD_DEL_MPATH,
6683 .doit = nl80211_del_mpath,
6684 .policy = nl80211_policy,
6685 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006687 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006688 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006689 {
6690 .cmd = NL80211_CMD_SET_BSS,
6691 .doit = nl80211_set_bss,
6692 .policy = nl80211_policy,
6693 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006695 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006696 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006697 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006698 .cmd = NL80211_CMD_GET_REG,
6699 .doit = nl80211_get_reg,
6700 .policy = nl80211_policy,
6701 /* can be retrieved by unprivileged users */
6702 },
6703 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006704 .cmd = NL80211_CMD_SET_REG,
6705 .doit = nl80211_set_reg,
6706 .policy = nl80211_policy,
6707 .flags = GENL_ADMIN_PERM,
6708 },
6709 {
6710 .cmd = NL80211_CMD_REQ_SET_REG,
6711 .doit = nl80211_req_set_reg,
6712 .policy = nl80211_policy,
6713 .flags = GENL_ADMIN_PERM,
6714 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006715 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006716 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6717 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006718 .policy = nl80211_policy,
6719 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006721 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006722 },
6723 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006724 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6725 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006726 .policy = nl80211_policy,
6727 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006729 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006730 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006731 {
Johannes Berg2a519312009-02-10 21:25:55 +01006732 .cmd = NL80211_CMD_TRIGGER_SCAN,
6733 .doit = nl80211_trigger_scan,
6734 .policy = nl80211_policy,
6735 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006737 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006738 },
6739 {
6740 .cmd = NL80211_CMD_GET_SCAN,
6741 .policy = nl80211_policy,
6742 .dumpit = nl80211_dump_scan,
6743 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006744 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006745 .cmd = NL80211_CMD_START_SCHED_SCAN,
6746 .doit = nl80211_start_sched_scan,
6747 .policy = nl80211_policy,
6748 .flags = GENL_ADMIN_PERM,
6749 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6750 NL80211_FLAG_NEED_RTNL,
6751 },
6752 {
6753 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6754 .doit = nl80211_stop_sched_scan,
6755 .policy = nl80211_policy,
6756 .flags = GENL_ADMIN_PERM,
6757 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6758 NL80211_FLAG_NEED_RTNL,
6759 },
6760 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006761 .cmd = NL80211_CMD_AUTHENTICATE,
6762 .doit = nl80211_authenticate,
6763 .policy = nl80211_policy,
6764 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006765 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006766 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006767 },
6768 {
6769 .cmd = NL80211_CMD_ASSOCIATE,
6770 .doit = nl80211_associate,
6771 .policy = nl80211_policy,
6772 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006773 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006774 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006775 },
6776 {
6777 .cmd = NL80211_CMD_DEAUTHENTICATE,
6778 .doit = nl80211_deauthenticate,
6779 .policy = nl80211_policy,
6780 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006781 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006782 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006783 },
6784 {
6785 .cmd = NL80211_CMD_DISASSOCIATE,
6786 .doit = nl80211_disassociate,
6787 .policy = nl80211_policy,
6788 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006789 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006790 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006791 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006792 {
6793 .cmd = NL80211_CMD_JOIN_IBSS,
6794 .doit = nl80211_join_ibss,
6795 .policy = nl80211_policy,
6796 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006797 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006798 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006799 },
6800 {
6801 .cmd = NL80211_CMD_LEAVE_IBSS,
6802 .doit = nl80211_leave_ibss,
6803 .policy = nl80211_policy,
6804 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006805 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006806 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006807 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006808#ifdef CONFIG_NL80211_TESTMODE
6809 {
6810 .cmd = NL80211_CMD_TESTMODE,
6811 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006812 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006813 .policy = nl80211_policy,
6814 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006815 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6816 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006817 },
6818#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006819 {
6820 .cmd = NL80211_CMD_CONNECT,
6821 .doit = nl80211_connect,
6822 .policy = nl80211_policy,
6823 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006824 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006825 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006826 },
6827 {
6828 .cmd = NL80211_CMD_DISCONNECT,
6829 .doit = nl80211_disconnect,
6830 .policy = nl80211_policy,
6831 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006832 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006833 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006834 },
Johannes Berg463d0182009-07-14 00:33:35 +02006835 {
6836 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6837 .doit = nl80211_wiphy_netns,
6838 .policy = nl80211_policy,
6839 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006840 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6841 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006842 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006843 {
6844 .cmd = NL80211_CMD_GET_SURVEY,
6845 .policy = nl80211_policy,
6846 .dumpit = nl80211_dump_survey,
6847 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006848 {
6849 .cmd = NL80211_CMD_SET_PMKSA,
6850 .doit = nl80211_setdel_pmksa,
6851 .policy = nl80211_policy,
6852 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006853 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006854 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006855 },
6856 {
6857 .cmd = NL80211_CMD_DEL_PMKSA,
6858 .doit = nl80211_setdel_pmksa,
6859 .policy = nl80211_policy,
6860 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006861 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006862 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006863 },
6864 {
6865 .cmd = NL80211_CMD_FLUSH_PMKSA,
6866 .doit = nl80211_flush_pmksa,
6867 .policy = nl80211_policy,
6868 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006869 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006870 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006871 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006872 {
6873 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6874 .doit = nl80211_remain_on_channel,
6875 .policy = nl80211_policy,
6876 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006877 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006878 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006879 },
6880 {
6881 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6882 .doit = nl80211_cancel_remain_on_channel,
6883 .policy = nl80211_policy,
6884 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006885 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006886 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006887 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006888 {
6889 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6890 .doit = nl80211_set_tx_bitrate_mask,
6891 .policy = nl80211_policy,
6892 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006893 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6894 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006895 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006896 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006897 .cmd = NL80211_CMD_REGISTER_FRAME,
6898 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006899 .policy = nl80211_policy,
6900 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006901 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6902 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006903 },
6904 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006905 .cmd = NL80211_CMD_FRAME,
6906 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006907 .policy = nl80211_policy,
6908 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006909 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006910 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006911 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006912 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006913 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6914 .doit = nl80211_tx_mgmt_cancel_wait,
6915 .policy = nl80211_policy,
6916 .flags = GENL_ADMIN_PERM,
6917 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6918 NL80211_FLAG_NEED_RTNL,
6919 },
6920 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006921 .cmd = NL80211_CMD_SET_POWER_SAVE,
6922 .doit = nl80211_set_power_save,
6923 .policy = nl80211_policy,
6924 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006925 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6926 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006927 },
6928 {
6929 .cmd = NL80211_CMD_GET_POWER_SAVE,
6930 .doit = nl80211_get_power_save,
6931 .policy = nl80211_policy,
6932 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006933 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6934 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006935 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006936 {
6937 .cmd = NL80211_CMD_SET_CQM,
6938 .doit = nl80211_set_cqm,
6939 .policy = nl80211_policy,
6940 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006941 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6942 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006943 },
Johannes Bergf444de02010-05-05 15:25:02 +02006944 {
6945 .cmd = NL80211_CMD_SET_CHANNEL,
6946 .doit = nl80211_set_channel,
6947 .policy = nl80211_policy,
6948 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006949 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6950 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006951 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006952 {
6953 .cmd = NL80211_CMD_SET_WDS_PEER,
6954 .doit = nl80211_set_wds_peer,
6955 .policy = nl80211_policy,
6956 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006957 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6958 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006959 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006960 {
6961 .cmd = NL80211_CMD_JOIN_MESH,
6962 .doit = nl80211_join_mesh,
6963 .policy = nl80211_policy,
6964 .flags = GENL_ADMIN_PERM,
6965 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6966 NL80211_FLAG_NEED_RTNL,
6967 },
6968 {
6969 .cmd = NL80211_CMD_LEAVE_MESH,
6970 .doit = nl80211_leave_mesh,
6971 .policy = nl80211_policy,
6972 .flags = GENL_ADMIN_PERM,
6973 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6974 NL80211_FLAG_NEED_RTNL,
6975 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006976 {
6977 .cmd = NL80211_CMD_GET_WOWLAN,
6978 .doit = nl80211_get_wowlan,
6979 .policy = nl80211_policy,
6980 /* can be retrieved by unprivileged users */
6981 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6982 NL80211_FLAG_NEED_RTNL,
6983 },
6984 {
6985 .cmd = NL80211_CMD_SET_WOWLAN,
6986 .doit = nl80211_set_wowlan,
6987 .policy = nl80211_policy,
6988 .flags = GENL_ADMIN_PERM,
6989 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6990 NL80211_FLAG_NEED_RTNL,
6991 },
Johannes Berge5497d72011-07-05 16:35:40 +02006992 {
6993 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6994 .doit = nl80211_set_rekey_data,
6995 .policy = nl80211_policy,
6996 .flags = GENL_ADMIN_PERM,
6997 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6998 NL80211_FLAG_NEED_RTNL,
6999 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007000 {
7001 .cmd = NL80211_CMD_TDLS_MGMT,
7002 .doit = nl80211_tdls_mgmt,
7003 .policy = nl80211_policy,
7004 .flags = GENL_ADMIN_PERM,
7005 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7006 NL80211_FLAG_NEED_RTNL,
7007 },
7008 {
7009 .cmd = NL80211_CMD_TDLS_OPER,
7010 .doit = nl80211_tdls_oper,
7011 .policy = nl80211_policy,
7012 .flags = GENL_ADMIN_PERM,
7013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7014 NL80211_FLAG_NEED_RTNL,
7015 },
Johannes Berg28946da2011-11-04 11:18:12 +01007016 {
7017 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7018 .doit = nl80211_register_unexpected_frame,
7019 .policy = nl80211_policy,
7020 .flags = GENL_ADMIN_PERM,
7021 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7022 NL80211_FLAG_NEED_RTNL,
7023 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007024 {
7025 .cmd = NL80211_CMD_PROBE_CLIENT,
7026 .doit = nl80211_probe_client,
7027 .policy = nl80211_policy,
7028 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007029 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007030 NL80211_FLAG_NEED_RTNL,
7031 },
Johannes Berg5e760232011-11-04 11:18:17 +01007032 {
7033 .cmd = NL80211_CMD_REGISTER_BEACONS,
7034 .doit = nl80211_register_beacons,
7035 .policy = nl80211_policy,
7036 .flags = GENL_ADMIN_PERM,
7037 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7038 NL80211_FLAG_NEED_RTNL,
7039 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007040 {
7041 .cmd = NL80211_CMD_SET_NOACK_MAP,
7042 .doit = nl80211_set_noack_map,
7043 .policy = nl80211_policy,
7044 .flags = GENL_ADMIN_PERM,
7045 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7046 NL80211_FLAG_NEED_RTNL,
7047 },
7048
Johannes Berg55682962007-09-20 13:09:35 -04007049};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007050
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007051static struct genl_multicast_group nl80211_mlme_mcgrp = {
7052 .name = "mlme",
7053};
Johannes Berg55682962007-09-20 13:09:35 -04007054
7055/* multicast groups */
7056static struct genl_multicast_group nl80211_config_mcgrp = {
7057 .name = "config",
7058};
Johannes Berg2a519312009-02-10 21:25:55 +01007059static struct genl_multicast_group nl80211_scan_mcgrp = {
7060 .name = "scan",
7061};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007062static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7063 .name = "regulatory",
7064};
Johannes Berg55682962007-09-20 13:09:35 -04007065
7066/* notification functions */
7067
7068void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7069{
7070 struct sk_buff *msg;
7071
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007072 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007073 if (!msg)
7074 return;
7075
7076 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7077 nlmsg_free(msg);
7078 return;
7079 }
7080
Johannes Berg463d0182009-07-14 00:33:35 +02007081 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7082 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007083}
7084
Johannes Berg362a4152009-05-24 16:43:15 +02007085static int nl80211_add_scan_req(struct sk_buff *msg,
7086 struct cfg80211_registered_device *rdev)
7087{
7088 struct cfg80211_scan_request *req = rdev->scan_req;
7089 struct nlattr *nest;
7090 int i;
7091
Johannes Berg667503dd2009-07-07 03:56:11 +02007092 ASSERT_RDEV_LOCK(rdev);
7093
Johannes Berg362a4152009-05-24 16:43:15 +02007094 if (WARN_ON(!req))
7095 return 0;
7096
7097 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7098 if (!nest)
7099 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007100 for (i = 0; i < req->n_ssids; i++) {
7101 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7102 goto nla_put_failure;
7103 }
Johannes Berg362a4152009-05-24 16:43:15 +02007104 nla_nest_end(msg, nest);
7105
7106 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7107 if (!nest)
7108 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007109 for (i = 0; i < req->n_channels; i++) {
7110 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7111 goto nla_put_failure;
7112 }
Johannes Berg362a4152009-05-24 16:43:15 +02007113 nla_nest_end(msg, nest);
7114
David S. Miller9360ffd2012-03-29 04:41:26 -04007115 if (req->ie &&
7116 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7117 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007118
7119 return 0;
7120 nla_put_failure:
7121 return -ENOBUFS;
7122}
7123
Johannes Berga538e2d2009-06-16 19:56:42 +02007124static int nl80211_send_scan_msg(struct sk_buff *msg,
7125 struct cfg80211_registered_device *rdev,
7126 struct net_device *netdev,
7127 u32 pid, u32 seq, int flags,
7128 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007129{
7130 void *hdr;
7131
7132 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7133 if (!hdr)
7134 return -1;
7135
David S. Miller9360ffd2012-03-29 04:41:26 -04007136 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7137 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7138 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007139
Johannes Berg362a4152009-05-24 16:43:15 +02007140 /* ignore errors and send incomplete event anyway */
7141 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007142
7143 return genlmsg_end(msg, hdr);
7144
7145 nla_put_failure:
7146 genlmsg_cancel(msg, hdr);
7147 return -EMSGSIZE;
7148}
7149
Luciano Coelho807f8a82011-05-11 17:09:35 +03007150static int
7151nl80211_send_sched_scan_msg(struct sk_buff *msg,
7152 struct cfg80211_registered_device *rdev,
7153 struct net_device *netdev,
7154 u32 pid, u32 seq, int flags, u32 cmd)
7155{
7156 void *hdr;
7157
7158 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7159 if (!hdr)
7160 return -1;
7161
David S. Miller9360ffd2012-03-29 04:41:26 -04007162 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7163 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7164 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007165
7166 return genlmsg_end(msg, hdr);
7167
7168 nla_put_failure:
7169 genlmsg_cancel(msg, hdr);
7170 return -EMSGSIZE;
7171}
7172
Johannes Berga538e2d2009-06-16 19:56:42 +02007173void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7174 struct net_device *netdev)
7175{
7176 struct sk_buff *msg;
7177
7178 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7179 if (!msg)
7180 return;
7181
7182 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7183 NL80211_CMD_TRIGGER_SCAN) < 0) {
7184 nlmsg_free(msg);
7185 return;
7186 }
7187
Johannes Berg463d0182009-07-14 00:33:35 +02007188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7189 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007190}
7191
Johannes Berg2a519312009-02-10 21:25:55 +01007192void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7193 struct net_device *netdev)
7194{
7195 struct sk_buff *msg;
7196
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007198 if (!msg)
7199 return;
7200
Johannes Berga538e2d2009-06-16 19:56:42 +02007201 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7202 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007203 nlmsg_free(msg);
7204 return;
7205 }
7206
Johannes Berg463d0182009-07-14 00:33:35 +02007207 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7208 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007209}
7210
7211void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7212 struct net_device *netdev)
7213{
7214 struct sk_buff *msg;
7215
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007217 if (!msg)
7218 return;
7219
Johannes Berga538e2d2009-06-16 19:56:42 +02007220 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7221 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007222 nlmsg_free(msg);
7223 return;
7224 }
7225
Johannes Berg463d0182009-07-14 00:33:35 +02007226 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7227 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007228}
7229
Luciano Coelho807f8a82011-05-11 17:09:35 +03007230void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7231 struct net_device *netdev)
7232{
7233 struct sk_buff *msg;
7234
7235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7236 if (!msg)
7237 return;
7238
7239 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7240 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7241 nlmsg_free(msg);
7242 return;
7243 }
7244
7245 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7246 nl80211_scan_mcgrp.id, GFP_KERNEL);
7247}
7248
7249void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7250 struct net_device *netdev, u32 cmd)
7251{
7252 struct sk_buff *msg;
7253
7254 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7255 if (!msg)
7256 return;
7257
7258 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7259 nlmsg_free(msg);
7260 return;
7261 }
7262
7263 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7264 nl80211_scan_mcgrp.id, GFP_KERNEL);
7265}
7266
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007267/*
7268 * This can happen on global regulatory changes or device specific settings
7269 * based on custom world regulatory domains.
7270 */
7271void nl80211_send_reg_change_event(struct regulatory_request *request)
7272{
7273 struct sk_buff *msg;
7274 void *hdr;
7275
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007276 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007277 if (!msg)
7278 return;
7279
7280 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7281 if (!hdr) {
7282 nlmsg_free(msg);
7283 return;
7284 }
7285
7286 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007287 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7288 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007289
David S. Miller9360ffd2012-03-29 04:41:26 -04007290 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7291 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7292 NL80211_REGDOM_TYPE_WORLD))
7293 goto nla_put_failure;
7294 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7295 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7296 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7297 goto nla_put_failure;
7298 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7299 request->intersect) {
7300 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7301 NL80211_REGDOM_TYPE_INTERSECTION))
7302 goto nla_put_failure;
7303 } else {
7304 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7305 NL80211_REGDOM_TYPE_COUNTRY) ||
7306 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7307 request->alpha2))
7308 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007309 }
7310
David S. Miller9360ffd2012-03-29 04:41:26 -04007311 if (wiphy_idx_valid(request->wiphy_idx) &&
7312 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7313 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007314
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007315 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007316
Johannes Bergbc43b282009-07-25 10:54:13 +02007317 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007318 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007319 GFP_ATOMIC);
7320 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007321
7322 return;
7323
7324nla_put_failure:
7325 genlmsg_cancel(msg, hdr);
7326 nlmsg_free(msg);
7327}
7328
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007329static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7330 struct net_device *netdev,
7331 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007332 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007333{
7334 struct sk_buff *msg;
7335 void *hdr;
7336
Johannes Berge6d6e342009-07-01 21:26:47 +02007337 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007338 if (!msg)
7339 return;
7340
7341 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7342 if (!hdr) {
7343 nlmsg_free(msg);
7344 return;
7345 }
7346
David S. Miller9360ffd2012-03-29 04:41:26 -04007347 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7348 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7349 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7350 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007351
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007352 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007353
Johannes Berg463d0182009-07-14 00:33:35 +02007354 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7355 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007356 return;
7357
7358 nla_put_failure:
7359 genlmsg_cancel(msg, hdr);
7360 nlmsg_free(msg);
7361}
7362
7363void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007364 struct net_device *netdev, const u8 *buf,
7365 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007366{
7367 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007368 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007369}
7370
7371void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7372 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007373 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007374{
Johannes Berge6d6e342009-07-01 21:26:47 +02007375 nl80211_send_mlme_event(rdev, netdev, buf, len,
7376 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007377}
7378
Jouni Malinen53b46b82009-03-27 20:53:56 +02007379void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007380 struct net_device *netdev, const u8 *buf,
7381 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007382{
7383 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007384 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007385}
7386
Jouni Malinen53b46b82009-03-27 20:53:56 +02007387void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7388 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007389 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007390{
7391 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007392 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007393}
7394
Jouni Malinencf4e5942010-12-16 00:52:40 +02007395void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7396 struct net_device *netdev, const u8 *buf,
7397 size_t len, gfp_t gfp)
7398{
7399 nl80211_send_mlme_event(rdev, netdev, buf, len,
7400 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7401}
7402
7403void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7404 struct net_device *netdev, const u8 *buf,
7405 size_t len, gfp_t gfp)
7406{
7407 nl80211_send_mlme_event(rdev, netdev, buf, len,
7408 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7409}
7410
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007411static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7412 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007413 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007414{
7415 struct sk_buff *msg;
7416 void *hdr;
7417
Johannes Berge6d6e342009-07-01 21:26:47 +02007418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007419 if (!msg)
7420 return;
7421
7422 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7423 if (!hdr) {
7424 nlmsg_free(msg);
7425 return;
7426 }
7427
David S. Miller9360ffd2012-03-29 04:41:26 -04007428 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7429 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7430 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7431 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7432 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007433
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007434 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007435
Johannes Berg463d0182009-07-14 00:33:35 +02007436 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7437 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007438 return;
7439
7440 nla_put_failure:
7441 genlmsg_cancel(msg, hdr);
7442 nlmsg_free(msg);
7443}
7444
7445void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007446 struct net_device *netdev, const u8 *addr,
7447 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007448{
7449 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007450 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007451}
7452
7453void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007454 struct net_device *netdev, const u8 *addr,
7455 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007456{
Johannes Berge6d6e342009-07-01 21:26:47 +02007457 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7458 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007459}
7460
Samuel Ortizb23aa672009-07-01 21:26:54 +02007461void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7462 struct net_device *netdev, const u8 *bssid,
7463 const u8 *req_ie, size_t req_ie_len,
7464 const u8 *resp_ie, size_t resp_ie_len,
7465 u16 status, gfp_t gfp)
7466{
7467 struct sk_buff *msg;
7468 void *hdr;
7469
7470 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7471 if (!msg)
7472 return;
7473
7474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7475 if (!hdr) {
7476 nlmsg_free(msg);
7477 return;
7478 }
7479
David S. Miller9360ffd2012-03-29 04:41:26 -04007480 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7481 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7482 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7483 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7484 (req_ie &&
7485 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7486 (resp_ie &&
7487 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7488 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007489
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007490 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007491
Johannes Berg463d0182009-07-14 00:33:35 +02007492 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7493 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007494 return;
7495
7496 nla_put_failure:
7497 genlmsg_cancel(msg, hdr);
7498 nlmsg_free(msg);
7499
7500}
7501
7502void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7503 struct net_device *netdev, const u8 *bssid,
7504 const u8 *req_ie, size_t req_ie_len,
7505 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7506{
7507 struct sk_buff *msg;
7508 void *hdr;
7509
7510 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7511 if (!msg)
7512 return;
7513
7514 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7515 if (!hdr) {
7516 nlmsg_free(msg);
7517 return;
7518 }
7519
David S. Miller9360ffd2012-03-29 04:41:26 -04007520 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7521 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7522 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7523 (req_ie &&
7524 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7525 (resp_ie &&
7526 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7527 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007528
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007529 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007530
Johannes Berg463d0182009-07-14 00:33:35 +02007531 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7532 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007533 return;
7534
7535 nla_put_failure:
7536 genlmsg_cancel(msg, hdr);
7537 nlmsg_free(msg);
7538
7539}
7540
7541void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7542 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007543 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007544{
7545 struct sk_buff *msg;
7546 void *hdr;
7547
Johannes Berg667503dd2009-07-07 03:56:11 +02007548 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007549 if (!msg)
7550 return;
7551
7552 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7553 if (!hdr) {
7554 nlmsg_free(msg);
7555 return;
7556 }
7557
David S. Miller9360ffd2012-03-29 04:41:26 -04007558 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7559 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7560 (from_ap && reason &&
7561 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7562 (from_ap &&
7563 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7564 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7565 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007566
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007567 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007568
Johannes Berg463d0182009-07-14 00:33:35 +02007569 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7570 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007571 return;
7572
7573 nla_put_failure:
7574 genlmsg_cancel(msg, hdr);
7575 nlmsg_free(msg);
7576
7577}
7578
Johannes Berg04a773a2009-04-19 21:24:32 +02007579void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7580 struct net_device *netdev, const u8 *bssid,
7581 gfp_t gfp)
7582{
7583 struct sk_buff *msg;
7584 void *hdr;
7585
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007587 if (!msg)
7588 return;
7589
7590 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7591 if (!hdr) {
7592 nlmsg_free(msg);
7593 return;
7594 }
7595
David S. Miller9360ffd2012-03-29 04:41:26 -04007596 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7597 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7598 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7599 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007600
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007601 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007602
Johannes Berg463d0182009-07-14 00:33:35 +02007603 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7604 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007605 return;
7606
7607 nla_put_failure:
7608 genlmsg_cancel(msg, hdr);
7609 nlmsg_free(msg);
7610}
7611
Javier Cardonac93b5e72011-04-07 15:08:34 -07007612void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7613 struct net_device *netdev,
7614 const u8 *macaddr, const u8* ie, u8 ie_len,
7615 gfp_t gfp)
7616{
7617 struct sk_buff *msg;
7618 void *hdr;
7619
7620 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7621 if (!msg)
7622 return;
7623
7624 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7625 if (!hdr) {
7626 nlmsg_free(msg);
7627 return;
7628 }
7629
David S. Miller9360ffd2012-03-29 04:41:26 -04007630 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7631 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7632 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7633 (ie_len && ie &&
7634 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7635 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007636
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007637 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007638
7639 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7640 nl80211_mlme_mcgrp.id, gfp);
7641 return;
7642
7643 nla_put_failure:
7644 genlmsg_cancel(msg, hdr);
7645 nlmsg_free(msg);
7646}
7647
Jouni Malinena3b8b052009-03-27 21:59:49 +02007648void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7649 struct net_device *netdev, const u8 *addr,
7650 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007651 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007652{
7653 struct sk_buff *msg;
7654 void *hdr;
7655
Johannes Berge6d6e342009-07-01 21:26:47 +02007656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007657 if (!msg)
7658 return;
7659
7660 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7661 if (!hdr) {
7662 nlmsg_free(msg);
7663 return;
7664 }
7665
David S. Miller9360ffd2012-03-29 04:41:26 -04007666 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7667 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7668 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7669 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7670 (key_id != -1 &&
7671 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7672 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7673 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007674
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007675 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007676
Johannes Berg463d0182009-07-14 00:33:35 +02007677 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7678 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007679 return;
7680
7681 nla_put_failure:
7682 genlmsg_cancel(msg, hdr);
7683 nlmsg_free(msg);
7684}
7685
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007686void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7687 struct ieee80211_channel *channel_before,
7688 struct ieee80211_channel *channel_after)
7689{
7690 struct sk_buff *msg;
7691 void *hdr;
7692 struct nlattr *nl_freq;
7693
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007694 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007695 if (!msg)
7696 return;
7697
7698 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7699 if (!hdr) {
7700 nlmsg_free(msg);
7701 return;
7702 }
7703
7704 /*
7705 * Since we are applying the beacon hint to a wiphy we know its
7706 * wiphy_idx is valid
7707 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007708 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7709 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007710
7711 /* Before */
7712 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7713 if (!nl_freq)
7714 goto nla_put_failure;
7715 if (nl80211_msg_put_channel(msg, channel_before))
7716 goto nla_put_failure;
7717 nla_nest_end(msg, nl_freq);
7718
7719 /* After */
7720 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7721 if (!nl_freq)
7722 goto nla_put_failure;
7723 if (nl80211_msg_put_channel(msg, channel_after))
7724 goto nla_put_failure;
7725 nla_nest_end(msg, nl_freq);
7726
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007727 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007728
Johannes Berg463d0182009-07-14 00:33:35 +02007729 rcu_read_lock();
7730 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7731 GFP_ATOMIC);
7732 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007733
7734 return;
7735
7736nla_put_failure:
7737 genlmsg_cancel(msg, hdr);
7738 nlmsg_free(msg);
7739}
7740
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007741static void nl80211_send_remain_on_chan_event(
7742 int cmd, struct cfg80211_registered_device *rdev,
7743 struct net_device *netdev, u64 cookie,
7744 struct ieee80211_channel *chan,
7745 enum nl80211_channel_type channel_type,
7746 unsigned int duration, gfp_t gfp)
7747{
7748 struct sk_buff *msg;
7749 void *hdr;
7750
7751 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7752 if (!msg)
7753 return;
7754
7755 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7756 if (!hdr) {
7757 nlmsg_free(msg);
7758 return;
7759 }
7760
David S. Miller9360ffd2012-03-29 04:41:26 -04007761 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7762 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7763 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7764 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7765 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7766 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007767
David S. Miller9360ffd2012-03-29 04:41:26 -04007768 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7769 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7770 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007771
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007772 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007773
7774 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7775 nl80211_mlme_mcgrp.id, gfp);
7776 return;
7777
7778 nla_put_failure:
7779 genlmsg_cancel(msg, hdr);
7780 nlmsg_free(msg);
7781}
7782
7783void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7784 struct net_device *netdev, u64 cookie,
7785 struct ieee80211_channel *chan,
7786 enum nl80211_channel_type channel_type,
7787 unsigned int duration, gfp_t gfp)
7788{
7789 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7790 rdev, netdev, cookie, chan,
7791 channel_type, duration, gfp);
7792}
7793
7794void nl80211_send_remain_on_channel_cancel(
7795 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7796 u64 cookie, struct ieee80211_channel *chan,
7797 enum nl80211_channel_type channel_type, gfp_t gfp)
7798{
7799 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7800 rdev, netdev, cookie, chan,
7801 channel_type, 0, gfp);
7802}
7803
Johannes Berg98b62182009-12-23 13:15:44 +01007804void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7805 struct net_device *dev, const u8 *mac_addr,
7806 struct station_info *sinfo, gfp_t gfp)
7807{
7808 struct sk_buff *msg;
7809
7810 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7811 if (!msg)
7812 return;
7813
John W. Linville66266b32012-03-15 13:25:41 -04007814 if (nl80211_send_station(msg, 0, 0, 0,
7815 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007816 nlmsg_free(msg);
7817 return;
7818 }
7819
7820 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7821 nl80211_mlme_mcgrp.id, gfp);
7822}
7823
Jouni Malinenec15e682011-03-23 15:29:52 +02007824void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7825 struct net_device *dev, const u8 *mac_addr,
7826 gfp_t gfp)
7827{
7828 struct sk_buff *msg;
7829 void *hdr;
7830
7831 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7832 if (!msg)
7833 return;
7834
7835 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7836 if (!hdr) {
7837 nlmsg_free(msg);
7838 return;
7839 }
7840
David S. Miller9360ffd2012-03-29 04:41:26 -04007841 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7842 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7843 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007844
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007845 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007846
7847 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7848 nl80211_mlme_mcgrp.id, gfp);
7849 return;
7850
7851 nla_put_failure:
7852 genlmsg_cancel(msg, hdr);
7853 nlmsg_free(msg);
7854}
7855
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007856static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7857 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007858{
7859 struct wireless_dev *wdev = dev->ieee80211_ptr;
7860 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7861 struct sk_buff *msg;
7862 void *hdr;
7863 int err;
7864 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7865
7866 if (!nlpid)
7867 return false;
7868
7869 msg = nlmsg_new(100, gfp);
7870 if (!msg)
7871 return true;
7872
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007873 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007874 if (!hdr) {
7875 nlmsg_free(msg);
7876 return true;
7877 }
7878
David S. Miller9360ffd2012-03-29 04:41:26 -04007879 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7880 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7881 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7882 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007883
7884 err = genlmsg_end(msg, hdr);
7885 if (err < 0) {
7886 nlmsg_free(msg);
7887 return true;
7888 }
7889
7890 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7891 return true;
7892
7893 nla_put_failure:
7894 genlmsg_cancel(msg, hdr);
7895 nlmsg_free(msg);
7896 return true;
7897}
7898
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007899bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7900{
7901 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7902 addr, gfp);
7903}
7904
7905bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7906 const u8 *addr, gfp_t gfp)
7907{
7908 return __nl80211_unexpected_frame(dev,
7909 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7910 addr, gfp);
7911}
7912
Johannes Berg2e161f72010-08-12 15:38:38 +02007913int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7914 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007915 int freq, int sig_dbm,
7916 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007917{
7918 struct sk_buff *msg;
7919 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007920
7921 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7922 if (!msg)
7923 return -ENOMEM;
7924
Johannes Berg2e161f72010-08-12 15:38:38 +02007925 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007926 if (!hdr) {
7927 nlmsg_free(msg);
7928 return -ENOMEM;
7929 }
7930
David S. Miller9360ffd2012-03-29 04:41:26 -04007931 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7932 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7933 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7934 (sig_dbm &&
7935 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7936 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7937 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007938
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007939 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007940
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007941 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007942
7943 nla_put_failure:
7944 genlmsg_cancel(msg, hdr);
7945 nlmsg_free(msg);
7946 return -ENOBUFS;
7947}
7948
Johannes Berg2e161f72010-08-12 15:38:38 +02007949void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7950 struct net_device *netdev, u64 cookie,
7951 const u8 *buf, size_t len, bool ack,
7952 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007953{
7954 struct sk_buff *msg;
7955 void *hdr;
7956
7957 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7958 if (!msg)
7959 return;
7960
Johannes Berg2e161f72010-08-12 15:38:38 +02007961 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007962 if (!hdr) {
7963 nlmsg_free(msg);
7964 return;
7965 }
7966
David S. Miller9360ffd2012-03-29 04:41:26 -04007967 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7968 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7969 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7970 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7971 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7972 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007973
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007974 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007975
7976 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7977 return;
7978
7979 nla_put_failure:
7980 genlmsg_cancel(msg, hdr);
7981 nlmsg_free(msg);
7982}
7983
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007984void
7985nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7986 struct net_device *netdev,
7987 enum nl80211_cqm_rssi_threshold_event rssi_event,
7988 gfp_t gfp)
7989{
7990 struct sk_buff *msg;
7991 struct nlattr *pinfoattr;
7992 void *hdr;
7993
7994 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7995 if (!msg)
7996 return;
7997
7998 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7999 if (!hdr) {
8000 nlmsg_free(msg);
8001 return;
8002 }
8003
David S. Miller9360ffd2012-03-29 04:41:26 -04008004 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8005 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8006 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008007
8008 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8009 if (!pinfoattr)
8010 goto nla_put_failure;
8011
David S. Miller9360ffd2012-03-29 04:41:26 -04008012 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8013 rssi_event))
8014 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008015
8016 nla_nest_end(msg, pinfoattr);
8017
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008018 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008019
8020 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8021 nl80211_mlme_mcgrp.id, gfp);
8022 return;
8023
8024 nla_put_failure:
8025 genlmsg_cancel(msg, hdr);
8026 nlmsg_free(msg);
8027}
8028
Johannes Berge5497d72011-07-05 16:35:40 +02008029void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8030 struct net_device *netdev, const u8 *bssid,
8031 const u8 *replay_ctr, gfp_t gfp)
8032{
8033 struct sk_buff *msg;
8034 struct nlattr *rekey_attr;
8035 void *hdr;
8036
8037 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8038 if (!msg)
8039 return;
8040
8041 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8042 if (!hdr) {
8043 nlmsg_free(msg);
8044 return;
8045 }
8046
David S. Miller9360ffd2012-03-29 04:41:26 -04008047 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8048 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8049 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8050 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008051
8052 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8053 if (!rekey_attr)
8054 goto nla_put_failure;
8055
David S. Miller9360ffd2012-03-29 04:41:26 -04008056 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8057 NL80211_REPLAY_CTR_LEN, replay_ctr))
8058 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008059
8060 nla_nest_end(msg, rekey_attr);
8061
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008062 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008063
8064 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8065 nl80211_mlme_mcgrp.id, gfp);
8066 return;
8067
8068 nla_put_failure:
8069 genlmsg_cancel(msg, hdr);
8070 nlmsg_free(msg);
8071}
8072
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008073void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8074 struct net_device *netdev, int index,
8075 const u8 *bssid, bool preauth, gfp_t gfp)
8076{
8077 struct sk_buff *msg;
8078 struct nlattr *attr;
8079 void *hdr;
8080
8081 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8082 if (!msg)
8083 return;
8084
8085 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8086 if (!hdr) {
8087 nlmsg_free(msg);
8088 return;
8089 }
8090
David S. Miller9360ffd2012-03-29 04:41:26 -04008091 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8092 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8093 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008094
8095 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8096 if (!attr)
8097 goto nla_put_failure;
8098
David S. Miller9360ffd2012-03-29 04:41:26 -04008099 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8100 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8101 (preauth &&
8102 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8103 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008104
8105 nla_nest_end(msg, attr);
8106
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008107 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008108
8109 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8110 nl80211_mlme_mcgrp.id, gfp);
8111 return;
8112
8113 nla_put_failure:
8114 genlmsg_cancel(msg, hdr);
8115 nlmsg_free(msg);
8116}
8117
Thomas Pedersen53145262012-04-06 13:35:47 -07008118void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8119 struct net_device *netdev, int freq,
8120 enum nl80211_channel_type type, gfp_t gfp)
8121{
8122 struct sk_buff *msg;
8123 void *hdr;
8124
8125 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8126 if (!msg)
8127 return;
8128
8129 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8130 if (!hdr) {
8131 nlmsg_free(msg);
8132 return;
8133 }
8134
John W. Linville7eab0f62012-04-12 14:25:14 -04008135 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8136 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8137 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8138 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008139
8140 genlmsg_end(msg, hdr);
8141
8142 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8143 nl80211_mlme_mcgrp.id, gfp);
8144 return;
8145
8146 nla_put_failure:
8147 genlmsg_cancel(msg, hdr);
8148 nlmsg_free(msg);
8149}
8150
Johannes Bergc063dbf2010-11-24 08:10:05 +01008151void
8152nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8153 struct net_device *netdev, const u8 *peer,
8154 u32 num_packets, gfp_t gfp)
8155{
8156 struct sk_buff *msg;
8157 struct nlattr *pinfoattr;
8158 void *hdr;
8159
8160 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8161 if (!msg)
8162 return;
8163
8164 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8165 if (!hdr) {
8166 nlmsg_free(msg);
8167 return;
8168 }
8169
David S. Miller9360ffd2012-03-29 04:41:26 -04008170 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8171 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8172 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8173 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008174
8175 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8176 if (!pinfoattr)
8177 goto nla_put_failure;
8178
David S. Miller9360ffd2012-03-29 04:41:26 -04008179 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8180 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008181
8182 nla_nest_end(msg, pinfoattr);
8183
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008184 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008185
8186 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8187 nl80211_mlme_mcgrp.id, gfp);
8188 return;
8189
8190 nla_put_failure:
8191 genlmsg_cancel(msg, hdr);
8192 nlmsg_free(msg);
8193}
8194
Johannes Berg7f6cf312011-11-04 11:18:15 +01008195void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8196 u64 cookie, bool acked, gfp_t gfp)
8197{
8198 struct wireless_dev *wdev = dev->ieee80211_ptr;
8199 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8200 struct sk_buff *msg;
8201 void *hdr;
8202 int err;
8203
8204 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8205 if (!msg)
8206 return;
8207
8208 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8209 if (!hdr) {
8210 nlmsg_free(msg);
8211 return;
8212 }
8213
David S. Miller9360ffd2012-03-29 04:41:26 -04008214 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8215 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8216 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8217 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8218 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8219 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008220
8221 err = genlmsg_end(msg, hdr);
8222 if (err < 0) {
8223 nlmsg_free(msg);
8224 return;
8225 }
8226
8227 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8228 nl80211_mlme_mcgrp.id, gfp);
8229 return;
8230
8231 nla_put_failure:
8232 genlmsg_cancel(msg, hdr);
8233 nlmsg_free(msg);
8234}
8235EXPORT_SYMBOL(cfg80211_probe_status);
8236
Johannes Berg5e760232011-11-04 11:18:17 +01008237void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8238 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008239 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008240{
8241 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8242 struct sk_buff *msg;
8243 void *hdr;
8244 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8245
8246 if (!nlpid)
8247 return;
8248
8249 msg = nlmsg_new(len + 100, gfp);
8250 if (!msg)
8251 return;
8252
8253 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8254 if (!hdr) {
8255 nlmsg_free(msg);
8256 return;
8257 }
8258
David S. Miller9360ffd2012-03-29 04:41:26 -04008259 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8260 (freq &&
8261 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8262 (sig_dbm &&
8263 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8264 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8265 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008266
8267 genlmsg_end(msg, hdr);
8268
8269 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8270 return;
8271
8272 nla_put_failure:
8273 genlmsg_cancel(msg, hdr);
8274 nlmsg_free(msg);
8275}
8276EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8277
Jouni Malinen026331c2010-02-15 12:53:10 +02008278static int nl80211_netlink_notify(struct notifier_block * nb,
8279 unsigned long state,
8280 void *_notify)
8281{
8282 struct netlink_notify *notify = _notify;
8283 struct cfg80211_registered_device *rdev;
8284 struct wireless_dev *wdev;
8285
8286 if (state != NETLINK_URELEASE)
8287 return NOTIFY_DONE;
8288
8289 rcu_read_lock();
8290
Johannes Berg5e760232011-11-04 11:18:17 +01008291 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008292 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008293 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008294 if (rdev->ap_beacons_nlpid == notify->pid)
8295 rdev->ap_beacons_nlpid = 0;
8296 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008297
8298 rcu_read_unlock();
8299
8300 return NOTIFY_DONE;
8301}
8302
8303static struct notifier_block nl80211_netlink_notifier = {
8304 .notifier_call = nl80211_netlink_notify,
8305};
8306
Johannes Berg55682962007-09-20 13:09:35 -04008307/* initialisation/exit functions */
8308
8309int nl80211_init(void)
8310{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008311 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008312
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008313 err = genl_register_family_with_ops(&nl80211_fam,
8314 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008315 if (err)
8316 return err;
8317
Johannes Berg55682962007-09-20 13:09:35 -04008318 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8319 if (err)
8320 goto err_out;
8321
Johannes Berg2a519312009-02-10 21:25:55 +01008322 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8323 if (err)
8324 goto err_out;
8325
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008326 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8327 if (err)
8328 goto err_out;
8329
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008330 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8331 if (err)
8332 goto err_out;
8333
Johannes Bergaff89a92009-07-01 21:26:51 +02008334#ifdef CONFIG_NL80211_TESTMODE
8335 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8336 if (err)
8337 goto err_out;
8338#endif
8339
Jouni Malinen026331c2010-02-15 12:53:10 +02008340 err = netlink_register_notifier(&nl80211_netlink_notifier);
8341 if (err)
8342 goto err_out;
8343
Johannes Berg55682962007-09-20 13:09:35 -04008344 return 0;
8345 err_out:
8346 genl_unregister_family(&nl80211_fam);
8347 return err;
8348}
8349
8350void nl80211_exit(void)
8351{
Jouni Malinen026331c2010-02-15 12:53:10 +02008352 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008353 genl_unregister_family(&nl80211_fam);
8354}