blob: 74f4a8f939356e52c97432dbfb512f7fd35e583c [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
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 Berg5e7602302011-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 Berg5e7602302011-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 /*
1169 * You can only set the channel explicitly for AP, mesh
1170 * and WDS type interfaces; all others have their channel
1171 * 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 ||
1183 wdev->iftype == NL80211_IFTYPE_WDS ||
1184 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001185 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1186 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001187}
1188
Johannes Bergcd6c6592012-05-10 21:27:18 +02001189static bool nl80211_valid_channel_type(struct genl_info *info,
1190 enum nl80211_channel_type *channel_type)
1191{
1192 enum nl80211_channel_type tmp;
1193
1194 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1195 return false;
1196
1197 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1198 if (tmp != NL80211_CHAN_NO_HT &&
1199 tmp != NL80211_CHAN_HT20 &&
1200 tmp != NL80211_CHAN_HT40PLUS &&
1201 tmp != NL80211_CHAN_HT40MINUS)
1202 return false;
1203
1204 if (channel_type)
1205 *channel_type = tmp;
1206
1207 return true;
1208}
1209
Johannes Bergf444de02010-05-05 15:25:02 +02001210static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1211 struct wireless_dev *wdev,
1212 struct genl_info *info)
1213{
Johannes Bergaa430da2012-05-16 23:50:18 +02001214 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001215 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1216 u32 freq;
1217 int result;
1218
1219 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1220 return -EINVAL;
1221
1222 if (!nl80211_can_set_dev_channel(wdev))
1223 return -EOPNOTSUPP;
1224
Johannes Bergcd6c6592012-05-10 21:27:18 +02001225 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1226 !nl80211_valid_channel_type(info, &channel_type))
1227 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001228
1229 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1230
1231 mutex_lock(&rdev->devlist_mtx);
Johannes Bergaa430da2012-05-16 23:50:18 +02001232 if (wdev) switch (wdev->iftype) {
1233 case NL80211_IFTYPE_AP:
1234 case NL80211_IFTYPE_P2P_GO:
1235 if (wdev->beacon_interval) {
1236 result = -EBUSY;
1237 break;
1238 }
1239 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1240 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1241 channel,
1242 channel_type)) {
1243 result = -EINVAL;
1244 break;
1245 }
1246 wdev->preset_chan = channel;
1247 wdev->preset_chantype = channel_type;
1248 result = 0;
1249 break;
1250 default:
Johannes Bergf444de02010-05-05 15:25:02 +02001251 wdev_lock(wdev);
1252 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1253 wdev_unlock(wdev);
1254 } else {
1255 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1256 }
1257 mutex_unlock(&rdev->devlist_mtx);
1258
1259 return result;
1260}
1261
1262static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1263{
Johannes Berg4c476992010-10-04 21:36:35 +02001264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1265 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001266
Johannes Berg4c476992010-10-04 21:36:35 +02001267 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001268}
1269
Bill Jordane8347eb2010-10-01 13:54:28 -04001270static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1271{
Johannes Berg43b19952010-10-07 13:10:30 +02001272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1273 struct net_device *dev = info->user_ptr[1];
1274 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001275 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001276
1277 if (!info->attrs[NL80211_ATTR_MAC])
1278 return -EINVAL;
1279
Johannes Berg43b19952010-10-07 13:10:30 +02001280 if (netif_running(dev))
1281 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001282
Johannes Berg43b19952010-10-07 13:10:30 +02001283 if (!rdev->ops->set_wds_peer)
1284 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001285
Johannes Berg43b19952010-10-07 13:10:30 +02001286 if (wdev->iftype != NL80211_IFTYPE_WDS)
1287 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001288
1289 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001290 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001291}
1292
1293
Johannes Berg55682962007-09-20 13:09:35 -04001294static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1295{
1296 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001297 struct net_device *netdev = NULL;
1298 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001299 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001300 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001301 u32 changed;
1302 u8 retry_short = 0, retry_long = 0;
1303 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001304 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001305
Johannes Bergf444de02010-05-05 15:25:02 +02001306 /*
1307 * Try to find the wiphy and netdev. Normally this
1308 * function shouldn't need the netdev, but this is
1309 * done for backward compatibility -- previously
1310 * setting the channel was done per wiphy, but now
1311 * it is per netdev. Previous userland like hostapd
1312 * also passed a netdev to set_wiphy, so that it is
1313 * possible to let that go to the right netdev!
1314 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001315 mutex_lock(&cfg80211_mutex);
1316
Johannes Bergf444de02010-05-05 15:25:02 +02001317 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1318 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1319
1320 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1321 if (netdev && netdev->ieee80211_ptr) {
1322 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1323 mutex_lock(&rdev->mtx);
1324 } else
1325 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001326 }
1327
Johannes Bergf444de02010-05-05 15:25:02 +02001328 if (!netdev) {
1329 rdev = __cfg80211_rdev_from_info(info);
1330 if (IS_ERR(rdev)) {
1331 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001332 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001333 }
1334 wdev = NULL;
1335 netdev = NULL;
1336 result = 0;
1337
1338 mutex_lock(&rdev->mtx);
1339 } else if (netif_running(netdev) &&
1340 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1341 wdev = netdev->ieee80211_ptr;
1342 else
1343 wdev = NULL;
1344
1345 /*
1346 * end workaround code, by now the rdev is available
1347 * and locked, and wdev may or may not be NULL.
1348 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001349
1350 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001351 result = cfg80211_dev_rename(
1352 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001353
1354 mutex_unlock(&cfg80211_mutex);
1355
1356 if (result)
1357 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001358
Jouni Malinen31888482008-10-30 16:59:24 +02001359 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1360 struct ieee80211_txq_params txq_params;
1361 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1362
1363 if (!rdev->ops->set_txq_params) {
1364 result = -EOPNOTSUPP;
1365 goto bad_res;
1366 }
1367
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001368 if (!netdev) {
1369 result = -EINVAL;
1370 goto bad_res;
1371 }
1372
Johannes Berg133a3ff2011-11-03 14:50:13 +01001373 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1374 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1375 result = -EINVAL;
1376 goto bad_res;
1377 }
1378
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001379 if (!netif_running(netdev)) {
1380 result = -ENETDOWN;
1381 goto bad_res;
1382 }
1383
Jouni Malinen31888482008-10-30 16:59:24 +02001384 nla_for_each_nested(nl_txq_params,
1385 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1386 rem_txq_params) {
1387 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1388 nla_data(nl_txq_params),
1389 nla_len(nl_txq_params),
1390 txq_params_policy);
1391 result = parse_txq_params(tb, &txq_params);
1392 if (result)
1393 goto bad_res;
1394
1395 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001396 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001397 &txq_params);
1398 if (result)
1399 goto bad_res;
1400 }
1401 }
1402
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001403 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001404 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001405 if (result)
1406 goto bad_res;
1407 }
1408
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001409 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1410 enum nl80211_tx_power_setting type;
1411 int idx, mbm = 0;
1412
1413 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001414 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001415 goto bad_res;
1416 }
1417
1418 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1419 type = nla_get_u32(info->attrs[idx]);
1420
1421 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1422 (type != NL80211_TX_POWER_AUTOMATIC)) {
1423 result = -EINVAL;
1424 goto bad_res;
1425 }
1426
1427 if (type != NL80211_TX_POWER_AUTOMATIC) {
1428 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1429 mbm = nla_get_u32(info->attrs[idx]);
1430 }
1431
1432 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1433 if (result)
1434 goto bad_res;
1435 }
1436
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001437 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1438 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1439 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001440 if ((!rdev->wiphy.available_antennas_tx &&
1441 !rdev->wiphy.available_antennas_rx) ||
1442 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001443 result = -EOPNOTSUPP;
1444 goto bad_res;
1445 }
1446
1447 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1448 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1449
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001450 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001451 * available antenna masks, except for the "all" mask */
1452 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1453 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001454 result = -EINVAL;
1455 goto bad_res;
1456 }
1457
Bruno Randolf7f531e02010-12-16 11:30:22 +09001458 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1459 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001460
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001461 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1462 if (result)
1463 goto bad_res;
1464 }
1465
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001466 changed = 0;
1467
1468 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1469 retry_short = nla_get_u8(
1470 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1471 if (retry_short == 0) {
1472 result = -EINVAL;
1473 goto bad_res;
1474 }
1475 changed |= WIPHY_PARAM_RETRY_SHORT;
1476 }
1477
1478 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1479 retry_long = nla_get_u8(
1480 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1481 if (retry_long == 0) {
1482 result = -EINVAL;
1483 goto bad_res;
1484 }
1485 changed |= WIPHY_PARAM_RETRY_LONG;
1486 }
1487
1488 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1489 frag_threshold = nla_get_u32(
1490 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1491 if (frag_threshold < 256) {
1492 result = -EINVAL;
1493 goto bad_res;
1494 }
1495 if (frag_threshold != (u32) -1) {
1496 /*
1497 * Fragments (apart from the last one) are required to
1498 * have even length. Make the fragmentation code
1499 * simpler by stripping LSB should someone try to use
1500 * odd threshold value.
1501 */
1502 frag_threshold &= ~0x1;
1503 }
1504 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1505 }
1506
1507 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1508 rts_threshold = nla_get_u32(
1509 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1510 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1511 }
1512
Lukáš Turek81077e82009-12-21 22:50:47 +01001513 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1514 coverage_class = nla_get_u8(
1515 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1516 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1517 }
1518
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001519 if (changed) {
1520 u8 old_retry_short, old_retry_long;
1521 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001522 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001523
1524 if (!rdev->ops->set_wiphy_params) {
1525 result = -EOPNOTSUPP;
1526 goto bad_res;
1527 }
1528
1529 old_retry_short = rdev->wiphy.retry_short;
1530 old_retry_long = rdev->wiphy.retry_long;
1531 old_frag_threshold = rdev->wiphy.frag_threshold;
1532 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001533 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001534
1535 if (changed & WIPHY_PARAM_RETRY_SHORT)
1536 rdev->wiphy.retry_short = retry_short;
1537 if (changed & WIPHY_PARAM_RETRY_LONG)
1538 rdev->wiphy.retry_long = retry_long;
1539 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1540 rdev->wiphy.frag_threshold = frag_threshold;
1541 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1542 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001543 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1544 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001545
1546 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1547 if (result) {
1548 rdev->wiphy.retry_short = old_retry_short;
1549 rdev->wiphy.retry_long = old_retry_long;
1550 rdev->wiphy.frag_threshold = old_frag_threshold;
1551 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001552 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001553 }
1554 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001555
Johannes Berg306d6112008-12-08 12:39:04 +01001556 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001557 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001558 if (netdev)
1559 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001560 return result;
1561}
1562
1563
1564static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001565 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001566 struct net_device *dev)
1567{
1568 void *hdr;
1569
1570 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1571 if (!hdr)
1572 return -1;
1573
David S. Miller9360ffd2012-03-29 04:41:26 -04001574 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1575 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1576 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1577 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1578 dev->ieee80211_ptr->iftype) ||
1579 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1580 rdev->devlist_generation ^
1581 (cfg80211_rdev_list_generation << 2)))
1582 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001583
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001584 if (rdev->ops->get_channel) {
1585 struct ieee80211_channel *chan;
1586 enum nl80211_channel_type channel_type;
1587
1588 chan = rdev->ops->get_channel(&rdev->wiphy, &channel_type);
John W. Linville59ef43e2012-04-18 14:17:13 -04001589 if (chan &&
1590 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1591 chan->center_freq) ||
1592 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1593 channel_type)))
1594 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001595 }
1596
Johannes Berg55682962007-09-20 13:09:35 -04001597 return genlmsg_end(msg, hdr);
1598
1599 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001600 genlmsg_cancel(msg, hdr);
1601 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001602}
1603
1604static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1605{
1606 int wp_idx = 0;
1607 int if_idx = 0;
1608 int wp_start = cb->args[0];
1609 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001610 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001611 struct wireless_dev *wdev;
1612
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001613 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001614 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1615 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001616 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001617 if (wp_idx < wp_start) {
1618 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001619 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001620 }
Johannes Berg55682962007-09-20 13:09:35 -04001621 if_idx = 0;
1622
Johannes Bergf5ea9122009-08-07 16:17:38 +02001623 mutex_lock(&rdev->devlist_mtx);
1624 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001625 if (if_idx < if_start) {
1626 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001627 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001628 }
Johannes Berg55682962007-09-20 13:09:35 -04001629 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1630 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001631 rdev, wdev->netdev) < 0) {
1632 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001633 goto out;
1634 }
1635 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001636 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001637 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001638
1639 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001640 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001641 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001642 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001643
1644 cb->args[0] = wp_idx;
1645 cb->args[1] = if_idx;
1646
1647 return skb->len;
1648}
1649
1650static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1651{
1652 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001653 struct cfg80211_registered_device *dev = info->user_ptr[0];
1654 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001655
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001657 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001658 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001659
Johannes Bergd7264052009-04-19 16:23:20 +02001660 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001661 dev, netdev) < 0) {
1662 nlmsg_free(msg);
1663 return -ENOBUFS;
1664 }
Johannes Berg55682962007-09-20 13:09:35 -04001665
Johannes Berg134e6372009-07-10 09:51:34 +00001666 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001667}
1668
Michael Wu66f7ac52008-01-31 19:48:22 +01001669static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1670 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1671 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1672 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1673 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1674 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1675};
1676
1677static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1678{
1679 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1680 int flag;
1681
1682 *mntrflags = 0;
1683
1684 if (!nla)
1685 return -EINVAL;
1686
1687 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1688 nla, mntr_flags_policy))
1689 return -EINVAL;
1690
1691 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1692 if (flags[flag])
1693 *mntrflags |= (1<<flag);
1694
1695 return 0;
1696}
1697
Johannes Berg9bc383d2009-11-19 11:55:19 +01001698static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001699 struct net_device *netdev, u8 use_4addr,
1700 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001701{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001702 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001703 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001704 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001705 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001706 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001707
1708 switch (iftype) {
1709 case NL80211_IFTYPE_AP_VLAN:
1710 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1711 return 0;
1712 break;
1713 case NL80211_IFTYPE_STATION:
1714 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1715 return 0;
1716 break;
1717 default:
1718 break;
1719 }
1720
1721 return -EOPNOTSUPP;
1722}
1723
Johannes Berg55682962007-09-20 13:09:35 -04001724static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1725{
Johannes Berg4c476992010-10-04 21:36:35 +02001726 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001727 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001728 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001729 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001730 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001731 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001732 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001733
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001734 memset(&params, 0, sizeof(params));
1735
Johannes Berg04a773a2009-04-19 21:24:32 +02001736 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001737
Johannes Berg723b0382008-09-16 20:22:09 +02001738 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001739 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001740 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001741 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001742 if (ntype > NL80211_IFTYPE_MAX)
1743 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001744 }
1745
Johannes Berg92ffe052008-09-16 20:39:36 +02001746 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001747 struct wireless_dev *wdev = dev->ieee80211_ptr;
1748
Johannes Berg4c476992010-10-04 21:36:35 +02001749 if (ntype != NL80211_IFTYPE_MESH_POINT)
1750 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001751 if (netif_running(dev))
1752 return -EBUSY;
1753
1754 wdev_lock(wdev);
1755 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1756 IEEE80211_MAX_MESH_ID_LEN);
1757 wdev->mesh_id_up_len =
1758 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1759 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1760 wdev->mesh_id_up_len);
1761 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001762 }
1763
Felix Fietkau8b787642009-11-10 18:53:10 +01001764 if (info->attrs[NL80211_ATTR_4ADDR]) {
1765 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1766 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001767 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001768 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001769 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001770 } else {
1771 params.use_4addr = -1;
1772 }
1773
Johannes Berg92ffe052008-09-16 20:39:36 +02001774 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001775 if (ntype != NL80211_IFTYPE_MONITOR)
1776 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001777 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1778 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001779 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001780 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001781
1782 flags = &_flags;
1783 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001784 }
Johannes Berg3b858752009-03-12 09:55:09 +01001785
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001786 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001787 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001788 else
1789 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001790
Johannes Berg9bc383d2009-11-19 11:55:19 +01001791 if (!err && params.use_4addr != -1)
1792 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1793
Johannes Berg55682962007-09-20 13:09:35 -04001794 return err;
1795}
1796
1797static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1798{
Johannes Berg4c476992010-10-04 21:36:35 +02001799 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001800 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001801 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001802 int err;
1803 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001804 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001805
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001806 memset(&params, 0, sizeof(params));
1807
Johannes Berg55682962007-09-20 13:09:35 -04001808 if (!info->attrs[NL80211_ATTR_IFNAME])
1809 return -EINVAL;
1810
1811 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1812 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1813 if (type > NL80211_IFTYPE_MAX)
1814 return -EINVAL;
1815 }
1816
Johannes Berg79c97e92009-07-07 03:56:12 +02001817 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001818 !(rdev->wiphy.interface_modes & (1 << type)))
1819 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001820
Johannes Berg9bc383d2009-11-19 11:55:19 +01001821 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001822 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001823 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001824 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001825 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001826 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001827
Michael Wu66f7ac52008-01-31 19:48:22 +01001828 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1829 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1830 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001831 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001832 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001833 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001834 if (IS_ERR(dev))
1835 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001836
Johannes Berg29cbe682010-12-03 09:20:44 +01001837 if (type == NL80211_IFTYPE_MESH_POINT &&
1838 info->attrs[NL80211_ATTR_MESH_ID]) {
1839 struct wireless_dev *wdev = dev->ieee80211_ptr;
1840
1841 wdev_lock(wdev);
1842 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1843 IEEE80211_MAX_MESH_ID_LEN);
1844 wdev->mesh_id_up_len =
1845 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1846 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1847 wdev->mesh_id_up_len);
1848 wdev_unlock(wdev);
1849 }
1850
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001851 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001852}
1853
1854static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1855{
Johannes Berg4c476992010-10-04 21:36:35 +02001856 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1857 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001858
Johannes Berg4c476992010-10-04 21:36:35 +02001859 if (!rdev->ops->del_virtual_intf)
1860 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001861
Johannes Berg4c476992010-10-04 21:36:35 +02001862 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001863}
1864
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001865static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1866{
1867 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1868 struct net_device *dev = info->user_ptr[1];
1869 u16 noack_map;
1870
1871 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1872 return -EINVAL;
1873
1874 if (!rdev->ops->set_noack_map)
1875 return -EOPNOTSUPP;
1876
1877 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1878
1879 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1880}
1881
Johannes Berg41ade002007-12-19 02:03:29 +01001882struct get_key_cookie {
1883 struct sk_buff *msg;
1884 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001885 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001886};
1887
1888static void get_key_callback(void *c, struct key_params *params)
1889{
Johannes Bergb9454e82009-07-08 13:29:08 +02001890 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001891 struct get_key_cookie *cookie = c;
1892
David S. Miller9360ffd2012-03-29 04:41:26 -04001893 if ((params->key &&
1894 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
1895 params->key_len, params->key)) ||
1896 (params->seq &&
1897 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
1898 params->seq_len, params->seq)) ||
1899 (params->cipher &&
1900 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1901 params->cipher)))
1902 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001903
Johannes Bergb9454e82009-07-08 13:29:08 +02001904 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1905 if (!key)
1906 goto nla_put_failure;
1907
David S. Miller9360ffd2012-03-29 04:41:26 -04001908 if ((params->key &&
1909 nla_put(cookie->msg, NL80211_KEY_DATA,
1910 params->key_len, params->key)) ||
1911 (params->seq &&
1912 nla_put(cookie->msg, NL80211_KEY_SEQ,
1913 params->seq_len, params->seq)) ||
1914 (params->cipher &&
1915 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
1916 params->cipher)))
1917 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001918
David S. Miller9360ffd2012-03-29 04:41:26 -04001919 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
1920 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02001921
1922 nla_nest_end(cookie->msg, key);
1923
Johannes Berg41ade002007-12-19 02:03:29 +01001924 return;
1925 nla_put_failure:
1926 cookie->error = 1;
1927}
1928
1929static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1930{
Johannes Berg4c476992010-10-04 21:36:35 +02001931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001932 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001933 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001934 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001935 const u8 *mac_addr = NULL;
1936 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001937 struct get_key_cookie cookie = {
1938 .error = 0,
1939 };
1940 void *hdr;
1941 struct sk_buff *msg;
1942
1943 if (info->attrs[NL80211_ATTR_KEY_IDX])
1944 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1945
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001946 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001947 return -EINVAL;
1948
1949 if (info->attrs[NL80211_ATTR_MAC])
1950 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1951
Johannes Berge31b8212010-10-05 19:39:30 +02001952 pairwise = !!mac_addr;
1953 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1954 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1955 if (kt >= NUM_NL80211_KEYTYPES)
1956 return -EINVAL;
1957 if (kt != NL80211_KEYTYPE_GROUP &&
1958 kt != NL80211_KEYTYPE_PAIRWISE)
1959 return -EINVAL;
1960 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1961 }
1962
Johannes Berg4c476992010-10-04 21:36:35 +02001963 if (!rdev->ops->get_key)
1964 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001965
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001966 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001967 if (!msg)
1968 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001969
1970 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1971 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001972 if (IS_ERR(hdr))
1973 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001974
1975 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001976 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001977
David S. Miller9360ffd2012-03-29 04:41:26 -04001978 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1979 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
1980 goto nla_put_failure;
1981 if (mac_addr &&
1982 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
1983 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01001984
Johannes Berge31b8212010-10-05 19:39:30 +02001985 if (pairwise && mac_addr &&
1986 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1987 return -ENOENT;
1988
1989 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1990 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001991
1992 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001993 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001994
1995 if (cookie.error)
1996 goto nla_put_failure;
1997
1998 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001999 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002000
2001 nla_put_failure:
2002 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002003 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002004 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002005 return err;
2006}
2007
2008static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2009{
Johannes Berg4c476992010-10-04 21:36:35 +02002010 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002011 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002012 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002013 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002014
Johannes Bergb9454e82009-07-08 13:29:08 +02002015 err = nl80211_parse_key(info, &key);
2016 if (err)
2017 return err;
2018
2019 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002020 return -EINVAL;
2021
Johannes Bergb9454e82009-07-08 13:29:08 +02002022 /* only support setting default key */
2023 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002024 return -EINVAL;
2025
Johannes Bergfffd0932009-07-08 14:22:54 +02002026 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002027
2028 if (key.def) {
2029 if (!rdev->ops->set_default_key) {
2030 err = -EOPNOTSUPP;
2031 goto out;
2032 }
2033
2034 err = nl80211_key_allowed(dev->ieee80211_ptr);
2035 if (err)
2036 goto out;
2037
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002038 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2039 key.def_uni, key.def_multi);
2040
2041 if (err)
2042 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002043
Johannes Berg3d23e342009-09-29 23:27:28 +02002044#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002045 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002046#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002047 } else {
2048 if (key.def_uni || !key.def_multi) {
2049 err = -EINVAL;
2050 goto out;
2051 }
2052
2053 if (!rdev->ops->set_default_mgmt_key) {
2054 err = -EOPNOTSUPP;
2055 goto out;
2056 }
2057
2058 err = nl80211_key_allowed(dev->ieee80211_ptr);
2059 if (err)
2060 goto out;
2061
2062 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2063 dev, key.idx);
2064 if (err)
2065 goto out;
2066
2067#ifdef CONFIG_CFG80211_WEXT
2068 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2069#endif
2070 }
2071
2072 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002073 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002074
Johannes Berg41ade002007-12-19 02:03:29 +01002075 return err;
2076}
2077
2078static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2079{
Johannes Berg4c476992010-10-04 21:36:35 +02002080 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002081 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002082 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002083 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002084 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002085
Johannes Bergb9454e82009-07-08 13:29:08 +02002086 err = nl80211_parse_key(info, &key);
2087 if (err)
2088 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002089
Johannes Bergb9454e82009-07-08 13:29:08 +02002090 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002091 return -EINVAL;
2092
Johannes Berg41ade002007-12-19 02:03:29 +01002093 if (info->attrs[NL80211_ATTR_MAC])
2094 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2095
Johannes Berge31b8212010-10-05 19:39:30 +02002096 if (key.type == -1) {
2097 if (mac_addr)
2098 key.type = NL80211_KEYTYPE_PAIRWISE;
2099 else
2100 key.type = NL80211_KEYTYPE_GROUP;
2101 }
2102
2103 /* for now */
2104 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2105 key.type != NL80211_KEYTYPE_GROUP)
2106 return -EINVAL;
2107
Johannes Berg4c476992010-10-04 21:36:35 +02002108 if (!rdev->ops->add_key)
2109 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002110
Johannes Berge31b8212010-10-05 19:39:30 +02002111 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2112 key.type == NL80211_KEYTYPE_PAIRWISE,
2113 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002114 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002115
2116 wdev_lock(dev->ieee80211_ptr);
2117 err = nl80211_key_allowed(dev->ieee80211_ptr);
2118 if (!err)
2119 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002120 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002121 mac_addr, &key.p);
2122 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002123
Johannes Berg41ade002007-12-19 02:03:29 +01002124 return err;
2125}
2126
2127static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2128{
Johannes Berg4c476992010-10-04 21:36:35 +02002129 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002130 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002131 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002132 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002133 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002134
Johannes Bergb9454e82009-07-08 13:29:08 +02002135 err = nl80211_parse_key(info, &key);
2136 if (err)
2137 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002138
2139 if (info->attrs[NL80211_ATTR_MAC])
2140 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2141
Johannes Berge31b8212010-10-05 19:39:30 +02002142 if (key.type == -1) {
2143 if (mac_addr)
2144 key.type = NL80211_KEYTYPE_PAIRWISE;
2145 else
2146 key.type = NL80211_KEYTYPE_GROUP;
2147 }
2148
2149 /* for now */
2150 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2151 key.type != NL80211_KEYTYPE_GROUP)
2152 return -EINVAL;
2153
Johannes Berg4c476992010-10-04 21:36:35 +02002154 if (!rdev->ops->del_key)
2155 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002156
Johannes Bergfffd0932009-07-08 14:22:54 +02002157 wdev_lock(dev->ieee80211_ptr);
2158 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002159
2160 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2161 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2162 err = -ENOENT;
2163
Johannes Bergfffd0932009-07-08 14:22:54 +02002164 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002165 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2166 key.type == NL80211_KEYTYPE_PAIRWISE,
2167 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002168
Johannes Berg3d23e342009-09-29 23:27:28 +02002169#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002170 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002171 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002172 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002173 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002174 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2175 }
2176#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002177 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002178
Johannes Berg41ade002007-12-19 02:03:29 +01002179 return err;
2180}
2181
Johannes Berg88600202012-02-13 15:17:18 +01002182static int nl80211_parse_beacon(struct genl_info *info,
2183 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002184{
Johannes Berg88600202012-02-13 15:17:18 +01002185 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002186
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002187 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2188 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2189 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2190 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002191 return -EINVAL;
2192
Johannes Berg88600202012-02-13 15:17:18 +01002193 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002194
Johannes Berged1b6cc2007-12-19 02:03:32 +01002195 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002196 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2197 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2198 if (!bcn->head_len)
2199 return -EINVAL;
2200 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002201 }
2202
2203 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002204 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2205 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002206 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002207 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002208 }
2209
Johannes Berg4c476992010-10-04 21:36:35 +02002210 if (!haveinfo)
2211 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002212
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002213 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002214 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2215 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002216 }
2217
2218 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002219 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002220 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002221 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002222 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2223 }
2224
2225 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002226 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002227 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002228 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002229 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2230 }
2231
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002232 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002233 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002234 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002235 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002236 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2237 }
2238
Johannes Berg88600202012-02-13 15:17:18 +01002239 return 0;
2240}
2241
2242static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2243{
2244 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2245 struct net_device *dev = info->user_ptr[1];
2246 struct wireless_dev *wdev = dev->ieee80211_ptr;
2247 struct cfg80211_ap_settings params;
2248 int err;
2249
2250 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2251 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2252 return -EOPNOTSUPP;
2253
2254 if (!rdev->ops->start_ap)
2255 return -EOPNOTSUPP;
2256
2257 if (wdev->beacon_interval)
2258 return -EALREADY;
2259
2260 memset(&params, 0, sizeof(params));
2261
2262 /* these are required for START_AP */
2263 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2264 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2265 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2266 return -EINVAL;
2267
2268 err = nl80211_parse_beacon(info, &params.beacon);
2269 if (err)
2270 return err;
2271
2272 params.beacon_interval =
2273 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2274 params.dtim_period =
2275 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2276
2277 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2278 if (err)
2279 return err;
2280
2281 /*
2282 * In theory, some of these attributes should be required here
2283 * but since they were not used when the command was originally
2284 * added, keep them optional for old user space programs to let
2285 * them continue to work with drivers that do not need the
2286 * additional information -- drivers must check!
2287 */
2288 if (info->attrs[NL80211_ATTR_SSID]) {
2289 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2290 params.ssid_len =
2291 nla_len(info->attrs[NL80211_ATTR_SSID]);
2292 if (params.ssid_len == 0 ||
2293 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2294 return -EINVAL;
2295 }
2296
2297 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2298 params.hidden_ssid = nla_get_u32(
2299 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2300 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2301 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2302 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2303 return -EINVAL;
2304 }
2305
2306 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2307
2308 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2309 params.auth_type = nla_get_u32(
2310 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2311 if (!nl80211_valid_auth_type(params.auth_type))
2312 return -EINVAL;
2313 } else
2314 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2315
2316 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2317 NL80211_MAX_NR_CIPHER_SUITES);
2318 if (err)
2319 return err;
2320
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302321 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2322 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2323 return -EOPNOTSUPP;
2324 params.inactivity_timeout = nla_get_u16(
2325 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2326 }
2327
Johannes Bergaa430da2012-05-16 23:50:18 +02002328 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2329 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2330
2331 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2332 !nl80211_valid_channel_type(info, &channel_type))
2333 return -EINVAL;
2334
2335 params.channel = rdev_freq_to_chan(rdev,
2336 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2337 channel_type);
2338 if (!params.channel)
2339 return -EINVAL;
2340 params.channel_type = channel_type;
2341 } else if (wdev->preset_chan) {
2342 params.channel = wdev->preset_chan;
2343 params.channel_type = wdev->preset_chantype;
2344 } else
2345 return -EINVAL;
2346
2347 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2348 params.channel_type))
2349 return -EINVAL;
2350
Johannes Berg88600202012-02-13 15:17:18 +01002351 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2352 if (!err)
2353 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002354 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002355}
2356
Johannes Berg88600202012-02-13 15:17:18 +01002357static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2358{
2359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2360 struct net_device *dev = info->user_ptr[1];
2361 struct wireless_dev *wdev = dev->ieee80211_ptr;
2362 struct cfg80211_beacon_data params;
2363 int err;
2364
2365 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2366 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2367 return -EOPNOTSUPP;
2368
2369 if (!rdev->ops->change_beacon)
2370 return -EOPNOTSUPP;
2371
2372 if (!wdev->beacon_interval)
2373 return -EINVAL;
2374
2375 err = nl80211_parse_beacon(info, &params);
2376 if (err)
2377 return err;
2378
2379 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2380}
2381
2382static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002383{
Johannes Berg4c476992010-10-04 21:36:35 +02002384 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2385 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002386 struct wireless_dev *wdev = dev->ieee80211_ptr;
2387 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002388
Johannes Berg88600202012-02-13 15:17:18 +01002389 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002390 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002391
Johannes Berg074ac8d2010-09-16 14:58:22 +02002392 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002393 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2394 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002395
Johannes Berg88600202012-02-13 15:17:18 +01002396 if (!wdev->beacon_interval)
2397 return -ENOENT;
2398
2399 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002400 if (!err)
2401 wdev->beacon_interval = 0;
2402 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002403}
2404
Johannes Berg5727ef12007-12-19 02:03:34 +01002405static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2406 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2407 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2408 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002409 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002410 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002411 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002412};
2413
Johannes Bergeccb8e82009-05-11 21:57:56 +03002414static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002415 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002416 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002417{
2418 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002419 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002420 int flag;
2421
Johannes Bergeccb8e82009-05-11 21:57:56 +03002422 /*
2423 * Try parsing the new attribute first so userspace
2424 * can specify both for older kernels.
2425 */
2426 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2427 if (nla) {
2428 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002429
Johannes Bergeccb8e82009-05-11 21:57:56 +03002430 sta_flags = nla_data(nla);
2431 params->sta_flags_mask = sta_flags->mask;
2432 params->sta_flags_set = sta_flags->set;
2433 if ((params->sta_flags_mask |
2434 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2435 return -EINVAL;
2436 return 0;
2437 }
2438
2439 /* if present, parse the old attribute */
2440
2441 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002442 if (!nla)
2443 return 0;
2444
2445 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2446 nla, sta_flags_policy))
2447 return -EINVAL;
2448
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002449 /*
2450 * Only allow certain flags for interface types so that
2451 * other attributes are silently ignored. Remember that
2452 * this is backward compatibility code with old userspace
2453 * and shouldn't be hit in other cases anyway.
2454 */
2455 switch (iftype) {
2456 case NL80211_IFTYPE_AP:
2457 case NL80211_IFTYPE_AP_VLAN:
2458 case NL80211_IFTYPE_P2P_GO:
2459 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2460 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2461 BIT(NL80211_STA_FLAG_WME) |
2462 BIT(NL80211_STA_FLAG_MFP);
2463 break;
2464 case NL80211_IFTYPE_P2P_CLIENT:
2465 case NL80211_IFTYPE_STATION:
2466 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2467 BIT(NL80211_STA_FLAG_TDLS_PEER);
2468 break;
2469 case NL80211_IFTYPE_MESH_POINT:
2470 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2471 BIT(NL80211_STA_FLAG_MFP) |
2472 BIT(NL80211_STA_FLAG_AUTHORIZED);
2473 default:
2474 return -EINVAL;
2475 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002476
Johannes Berg3383b5a2012-05-10 20:14:43 +02002477 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2478 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002479 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002480
Johannes Berg3383b5a2012-05-10 20:14:43 +02002481 /* no longer support new API additions in old API */
2482 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2483 return -EINVAL;
2484 }
2485 }
2486
Johannes Berg5727ef12007-12-19 02:03:34 +01002487 return 0;
2488}
2489
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002490static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2491 int attr)
2492{
2493 struct nlattr *rate;
2494 u16 bitrate;
2495
2496 rate = nla_nest_start(msg, attr);
2497 if (!rate)
2498 goto nla_put_failure;
2499
2500 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2501 bitrate = cfg80211_calculate_bitrate(info);
David S. Miller9360ffd2012-03-29 04:41:26 -04002502 if ((bitrate > 0 &&
2503 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2504 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2505 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2506 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2507 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2508 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2509 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2510 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002511
2512 nla_nest_end(msg, rate);
2513 return true;
2514
2515nla_put_failure:
2516 return false;
2517}
2518
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002519static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002520 int flags,
2521 struct cfg80211_registered_device *rdev,
2522 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002523 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002524{
2525 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002526 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002527
2528 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2529 if (!hdr)
2530 return -1;
2531
David S. Miller9360ffd2012-03-29 04:41:26 -04002532 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2533 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2534 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2535 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002536
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002537 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2538 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002539 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002540 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2541 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2542 sinfo->connected_time))
2543 goto nla_put_failure;
2544 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2545 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2546 sinfo->inactive_time))
2547 goto nla_put_failure;
2548 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2549 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2550 sinfo->rx_bytes))
2551 goto nla_put_failure;
2552 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2553 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2554 sinfo->tx_bytes))
2555 goto nla_put_failure;
2556 if ((sinfo->filled & STATION_INFO_LLID) &&
2557 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2558 goto nla_put_failure;
2559 if ((sinfo->filled & STATION_INFO_PLID) &&
2560 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2561 goto nla_put_failure;
2562 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2563 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2564 sinfo->plink_state))
2565 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002566 switch (rdev->wiphy.signal_type) {
2567 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002568 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2569 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2570 sinfo->signal))
2571 goto nla_put_failure;
2572 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2573 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2574 sinfo->signal_avg))
2575 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002576 break;
2577 default:
2578 break;
2579 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002580 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002581 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2582 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002583 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002584 }
2585 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2586 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2587 NL80211_STA_INFO_RX_BITRATE))
2588 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002589 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002590 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2591 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2592 sinfo->rx_packets))
2593 goto nla_put_failure;
2594 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2595 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2596 sinfo->tx_packets))
2597 goto nla_put_failure;
2598 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2599 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2600 sinfo->tx_retries))
2601 goto nla_put_failure;
2602 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2603 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2604 sinfo->tx_failed))
2605 goto nla_put_failure;
2606 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2607 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2608 sinfo->beacon_loss_count))
2609 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002610 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2611 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2612 if (!bss_param)
2613 goto nla_put_failure;
2614
David S. Miller9360ffd2012-03-29 04:41:26 -04002615 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2616 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2617 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2618 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2619 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2620 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2621 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2622 sinfo->bss_param.dtim_period) ||
2623 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2624 sinfo->bss_param.beacon_interval))
2625 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002626
2627 nla_nest_end(msg, bss_param);
2628 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002629 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2630 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2631 sizeof(struct nl80211_sta_flag_update),
2632 &sinfo->sta_flags))
2633 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002634 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2635 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2636 sinfo->t_offset))
2637 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002638 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002639
David S. Miller9360ffd2012-03-29 04:41:26 -04002640 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2641 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2642 sinfo->assoc_req_ies))
2643 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002644
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002645 return genlmsg_end(msg, hdr);
2646
2647 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002648 genlmsg_cancel(msg, hdr);
2649 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002650}
2651
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002652static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002653 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002654{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002655 struct station_info sinfo;
2656 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002657 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002658 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002659 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002660 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002661
Johannes Berg67748892010-10-04 21:14:06 +02002662 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2663 if (err)
2664 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002665
2666 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002667 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002668 goto out_err;
2669 }
2670
Johannes Bergbba95fe2008-07-29 13:22:51 +02002671 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002672 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002673 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2674 mac_addr, &sinfo);
2675 if (err == -ENOENT)
2676 break;
2677 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002678 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002679
2680 if (nl80211_send_station(skb,
2681 NETLINK_CB(cb->skb).pid,
2682 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002683 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002684 &sinfo) < 0)
2685 goto out;
2686
2687 sta_idx++;
2688 }
2689
2690
2691 out:
2692 cb->args[1] = sta_idx;
2693 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002694 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002695 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002696
2697 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002698}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002699
Johannes Berg5727ef12007-12-19 02:03:34 +01002700static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2701{
Johannes Berg4c476992010-10-04 21:36:35 +02002702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2703 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002704 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002705 struct sk_buff *msg;
2706 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002707 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002708
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002709 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002710
2711 if (!info->attrs[NL80211_ATTR_MAC])
2712 return -EINVAL;
2713
2714 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2715
Johannes Berg4c476992010-10-04 21:36:35 +02002716 if (!rdev->ops->get_station)
2717 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002718
Johannes Berg79c97e92009-07-07 03:56:12 +02002719 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002720 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002721 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002722
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002724 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002725 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002726
2727 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002728 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002729 nlmsg_free(msg);
2730 return -ENOBUFS;
2731 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002732
Johannes Berg4c476992010-10-04 21:36:35 +02002733 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002734}
2735
2736/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002737 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002738 */
Johannes Berg80b99892011-11-18 16:23:01 +01002739static struct net_device *get_vlan(struct genl_info *info,
2740 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002741{
Johannes Berg463d0182009-07-14 00:33:35 +02002742 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002743 struct net_device *v;
2744 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002745
Johannes Berg80b99892011-11-18 16:23:01 +01002746 if (!vlanattr)
2747 return NULL;
2748
2749 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2750 if (!v)
2751 return ERR_PTR(-ENODEV);
2752
2753 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2754 ret = -EINVAL;
2755 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002756 }
Johannes Berg80b99892011-11-18 16:23:01 +01002757
2758 if (!netif_running(v)) {
2759 ret = -ENETDOWN;
2760 goto error;
2761 }
2762
2763 return v;
2764 error:
2765 dev_put(v);
2766 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002767}
2768
2769static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2770{
Johannes Berg4c476992010-10-04 21:36:35 +02002771 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002772 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002773 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002774 struct station_parameters params;
2775 u8 *mac_addr = NULL;
2776
2777 memset(&params, 0, sizeof(params));
2778
2779 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002780 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002781
2782 if (info->attrs[NL80211_ATTR_STA_AID])
2783 return -EINVAL;
2784
2785 if (!info->attrs[NL80211_ATTR_MAC])
2786 return -EINVAL;
2787
2788 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2789
2790 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2791 params.supported_rates =
2792 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2793 params.supported_rates_len =
2794 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2795 }
2796
2797 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2798 params.listen_interval =
2799 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2800
Jouni Malinen36aedc902008-08-25 11:58:58 +03002801 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2802 params.ht_capa =
2803 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2804
Johannes Bergbdd90d52011-12-14 12:20:27 +01002805 if (!rdev->ops->change_station)
2806 return -EOPNOTSUPP;
2807
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002808 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002809 return -EINVAL;
2810
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002811 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2812 params.plink_action =
2813 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2814
Javier Cardona9c3990a2011-05-03 16:57:11 -07002815 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2816 params.plink_state =
2817 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2818
Johannes Berga97f4422009-06-18 17:23:43 +02002819 switch (dev->ieee80211_ptr->iftype) {
2820 case NL80211_IFTYPE_AP:
2821 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002822 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002823 /* disallow mesh-specific things */
2824 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002825 return -EINVAL;
2826
2827 /* TDLS can't be set, ... */
2828 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2829 return -EINVAL;
2830 /*
2831 * ... but don't bother the driver with it. This works around
2832 * a hostapd/wpa_supplicant issue -- it always includes the
2833 * TLDS_PEER flag in the mask even for AP mode.
2834 */
2835 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2836
2837 /* accept only the listed bits */
2838 if (params.sta_flags_mask &
2839 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2840 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2841 BIT(NL80211_STA_FLAG_WME) |
2842 BIT(NL80211_STA_FLAG_MFP)))
2843 return -EINVAL;
2844
2845 /* must be last in here for error handling */
2846 params.vlan = get_vlan(info, rdev);
2847 if (IS_ERR(params.vlan))
2848 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002849 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002850 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002851 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002852 /*
2853 * Don't allow userspace to change the TDLS_PEER flag,
2854 * but silently ignore attempts to change it since we
2855 * don't have state here to verify that it doesn't try
2856 * to change the flag.
2857 */
2858 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002859 /* fall through */
2860 case NL80211_IFTYPE_ADHOC:
2861 /* disallow things sta doesn't support */
2862 if (params.plink_action)
2863 return -EINVAL;
2864 if (params.ht_capa)
2865 return -EINVAL;
2866 if (params.listen_interval >= 0)
2867 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002868 /* reject any changes other than AUTHORIZED */
2869 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2870 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002871 break;
2872 case NL80211_IFTYPE_MESH_POINT:
2873 /* disallow things mesh doesn't support */
2874 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002875 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002876 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002877 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002878 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002879 return -EINVAL;
2880 /*
2881 * No special handling for TDLS here -- the userspace
2882 * mesh code doesn't have this bug.
2883 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002884 if (params.sta_flags_mask &
2885 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002886 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002887 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002888 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002889 break;
2890 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002891 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002892 }
2893
Johannes Bergbdd90d52011-12-14 12:20:27 +01002894 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002895
Johannes Berg79c97e92009-07-07 03:56:12 +02002896 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002897
Johannes Berg5727ef12007-12-19 02:03:34 +01002898 if (params.vlan)
2899 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002900
Johannes Berg5727ef12007-12-19 02:03:34 +01002901 return err;
2902}
2903
Eliad Pellerc75786c2011-08-23 14:37:46 +03002904static struct nla_policy
2905nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2906 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2907 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2908};
2909
Johannes Berg5727ef12007-12-19 02:03:34 +01002910static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2911{
Johannes Berg4c476992010-10-04 21:36:35 +02002912 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002913 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002914 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002915 struct station_parameters params;
2916 u8 *mac_addr = NULL;
2917
2918 memset(&params, 0, sizeof(params));
2919
2920 if (!info->attrs[NL80211_ATTR_MAC])
2921 return -EINVAL;
2922
Johannes Berg5727ef12007-12-19 02:03:34 +01002923 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2924 return -EINVAL;
2925
2926 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2927 return -EINVAL;
2928
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002929 if (!info->attrs[NL80211_ATTR_STA_AID])
2930 return -EINVAL;
2931
Johannes Berg5727ef12007-12-19 02:03:34 +01002932 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2933 params.supported_rates =
2934 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2935 params.supported_rates_len =
2936 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2937 params.listen_interval =
2938 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002939
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002940 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2941 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2942 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002943
Jouni Malinen36aedc902008-08-25 11:58:58 +03002944 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2945 params.ht_capa =
2946 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002947
Javier Cardona96b78df2011-04-07 15:08:33 -07002948 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2949 params.plink_action =
2950 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2951
Johannes Bergbdd90d52011-12-14 12:20:27 +01002952 if (!rdev->ops->add_station)
2953 return -EOPNOTSUPP;
2954
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002955 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002956 return -EINVAL;
2957
Johannes Bergbdd90d52011-12-14 12:20:27 +01002958 switch (dev->ieee80211_ptr->iftype) {
2959 case NL80211_IFTYPE_AP:
2960 case NL80211_IFTYPE_AP_VLAN:
2961 case NL80211_IFTYPE_P2P_GO:
2962 /* parse WME attributes if sta is WME capable */
2963 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2964 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2965 info->attrs[NL80211_ATTR_STA_WME]) {
2966 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2967 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002968
Johannes Bergbdd90d52011-12-14 12:20:27 +01002969 nla = info->attrs[NL80211_ATTR_STA_WME];
2970 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2971 nl80211_sta_wme_policy);
2972 if (err)
2973 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002974
Johannes Bergbdd90d52011-12-14 12:20:27 +01002975 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2976 params.uapsd_queues =
2977 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2978 if (params.uapsd_queues &
2979 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2980 return -EINVAL;
2981
2982 if (tb[NL80211_STA_WME_MAX_SP])
2983 params.max_sp =
2984 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2985
2986 if (params.max_sp &
2987 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2988 return -EINVAL;
2989
2990 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2991 }
2992 /* TDLS peers cannot be added */
2993 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002994 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002995 /* but don't bother the driver with it */
2996 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002997
Johannes Bergbdd90d52011-12-14 12:20:27 +01002998 /* must be last in here for error handling */
2999 params.vlan = get_vlan(info, rdev);
3000 if (IS_ERR(params.vlan))
3001 return PTR_ERR(params.vlan);
3002 break;
3003 case NL80211_IFTYPE_MESH_POINT:
3004 /* TDLS peers cannot be added */
3005 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003006 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003007 break;
3008 case NL80211_IFTYPE_STATION:
3009 /* Only TDLS peers can be added */
3010 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3011 return -EINVAL;
3012 /* Can only add if TDLS ... */
3013 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3014 return -EOPNOTSUPP;
3015 /* ... with external setup is supported */
3016 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3017 return -EOPNOTSUPP;
3018 break;
3019 default:
3020 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003021 }
3022
Johannes Bergbdd90d52011-12-14 12:20:27 +01003023 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003024
Johannes Berg79c97e92009-07-07 03:56:12 +02003025 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003026
Johannes Berg5727ef12007-12-19 02:03:34 +01003027 if (params.vlan)
3028 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003029 return err;
3030}
3031
3032static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3033{
Johannes Berg4c476992010-10-04 21:36:35 +02003034 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3035 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003036 u8 *mac_addr = NULL;
3037
3038 if (info->attrs[NL80211_ATTR_MAC])
3039 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3040
Johannes Berge80cf852009-05-11 14:43:13 +02003041 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003042 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003043 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003044 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3045 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003046
Johannes Berg4c476992010-10-04 21:36:35 +02003047 if (!rdev->ops->del_station)
3048 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003049
Johannes Berg4c476992010-10-04 21:36:35 +02003050 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003051}
3052
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003053static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3054 int flags, struct net_device *dev,
3055 u8 *dst, u8 *next_hop,
3056 struct mpath_info *pinfo)
3057{
3058 void *hdr;
3059 struct nlattr *pinfoattr;
3060
3061 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3062 if (!hdr)
3063 return -1;
3064
David S. Miller9360ffd2012-03-29 04:41:26 -04003065 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3066 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3067 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3068 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3069 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003070
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003071 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3072 if (!pinfoattr)
3073 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003074 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3075 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3076 pinfo->frame_qlen))
3077 goto nla_put_failure;
3078 if (((pinfo->filled & MPATH_INFO_SN) &&
3079 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3080 ((pinfo->filled & MPATH_INFO_METRIC) &&
3081 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3082 pinfo->metric)) ||
3083 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3084 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3085 pinfo->exptime)) ||
3086 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3087 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3088 pinfo->flags)) ||
3089 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3090 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3091 pinfo->discovery_timeout)) ||
3092 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3093 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3094 pinfo->discovery_retries)))
3095 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003096
3097 nla_nest_end(msg, pinfoattr);
3098
3099 return genlmsg_end(msg, hdr);
3100
3101 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003102 genlmsg_cancel(msg, hdr);
3103 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003104}
3105
3106static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003107 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003108{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003109 struct mpath_info pinfo;
3110 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003111 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003112 u8 dst[ETH_ALEN];
3113 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003114 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003115 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003116
Johannes Berg67748892010-10-04 21:14:06 +02003117 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3118 if (err)
3119 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003120
3121 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003122 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003123 goto out_err;
3124 }
3125
Jouni Malineneec60b02009-03-20 21:21:19 +02003126 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3127 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003128 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003129 }
3130
Johannes Bergbba95fe2008-07-29 13:22:51 +02003131 while (1) {
3132 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3133 dst, next_hop, &pinfo);
3134 if (err == -ENOENT)
3135 break;
3136 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003137 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003138
3139 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3140 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3141 netdev, dst, next_hop,
3142 &pinfo) < 0)
3143 goto out;
3144
3145 path_idx++;
3146 }
3147
3148
3149 out:
3150 cb->args[1] = path_idx;
3151 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003152 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003153 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003154 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003155}
3156
3157static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3158{
Johannes Berg4c476992010-10-04 21:36:35 +02003159 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003160 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003161 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003162 struct mpath_info pinfo;
3163 struct sk_buff *msg;
3164 u8 *dst = NULL;
3165 u8 next_hop[ETH_ALEN];
3166
3167 memset(&pinfo, 0, sizeof(pinfo));
3168
3169 if (!info->attrs[NL80211_ATTR_MAC])
3170 return -EINVAL;
3171
3172 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3173
Johannes Berg4c476992010-10-04 21:36:35 +02003174 if (!rdev->ops->get_mpath)
3175 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003176
Johannes Berg4c476992010-10-04 21:36:35 +02003177 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3178 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003179
Johannes Berg79c97e92009-07-07 03:56:12 +02003180 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003181 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003182 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003183
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003184 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003185 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003186 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003187
3188 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003189 dev, dst, next_hop, &pinfo) < 0) {
3190 nlmsg_free(msg);
3191 return -ENOBUFS;
3192 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003193
Johannes Berg4c476992010-10-04 21:36:35 +02003194 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003195}
3196
3197static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3198{
Johannes Berg4c476992010-10-04 21:36:35 +02003199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3200 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003201 u8 *dst = NULL;
3202 u8 *next_hop = NULL;
3203
3204 if (!info->attrs[NL80211_ATTR_MAC])
3205 return -EINVAL;
3206
3207 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3208 return -EINVAL;
3209
3210 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3211 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3212
Johannes Berg4c476992010-10-04 21:36:35 +02003213 if (!rdev->ops->change_mpath)
3214 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003215
Johannes Berg4c476992010-10-04 21:36:35 +02003216 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3217 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003218
Johannes Berg4c476992010-10-04 21:36:35 +02003219 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003220}
Johannes Berg4c476992010-10-04 21:36:35 +02003221
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003222static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3223{
Johannes Berg4c476992010-10-04 21:36:35 +02003224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3225 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003226 u8 *dst = NULL;
3227 u8 *next_hop = NULL;
3228
3229 if (!info->attrs[NL80211_ATTR_MAC])
3230 return -EINVAL;
3231
3232 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3233 return -EINVAL;
3234
3235 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3236 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3237
Johannes Berg4c476992010-10-04 21:36:35 +02003238 if (!rdev->ops->add_mpath)
3239 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003240
Johannes Berg4c476992010-10-04 21:36:35 +02003241 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3242 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003243
Johannes Berg4c476992010-10-04 21:36:35 +02003244 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003245}
3246
3247static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3248{
Johannes Berg4c476992010-10-04 21:36:35 +02003249 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3250 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003251 u8 *dst = NULL;
3252
3253 if (info->attrs[NL80211_ATTR_MAC])
3254 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3255
Johannes Berg4c476992010-10-04 21:36:35 +02003256 if (!rdev->ops->del_mpath)
3257 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003258
Johannes Berg4c476992010-10-04 21:36:35 +02003259 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003260}
3261
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003262static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3263{
Johannes Berg4c476992010-10-04 21:36:35 +02003264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3265 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003266 struct bss_parameters params;
3267
3268 memset(&params, 0, sizeof(params));
3269 /* default to not changing parameters */
3270 params.use_cts_prot = -1;
3271 params.use_short_preamble = -1;
3272 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003273 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003274 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003275
3276 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3277 params.use_cts_prot =
3278 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3279 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3280 params.use_short_preamble =
3281 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3282 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3283 params.use_short_slot_time =
3284 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003285 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3286 params.basic_rates =
3287 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3288 params.basic_rates_len =
3289 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3290 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003291 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3292 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003293 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3294 params.ht_opmode =
3295 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003296
Johannes Berg4c476992010-10-04 21:36:35 +02003297 if (!rdev->ops->change_bss)
3298 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003299
Johannes Berg074ac8d2010-09-16 14:58:22 +02003300 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003301 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3302 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003303
Johannes Berg4c476992010-10-04 21:36:35 +02003304 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003305}
3306
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003307static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003308 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3309 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3310 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3311 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3312 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3313 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3314};
3315
3316static int parse_reg_rule(struct nlattr *tb[],
3317 struct ieee80211_reg_rule *reg_rule)
3318{
3319 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3320 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3321
3322 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3323 return -EINVAL;
3324 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3325 return -EINVAL;
3326 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3327 return -EINVAL;
3328 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3329 return -EINVAL;
3330 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3331 return -EINVAL;
3332
3333 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3334
3335 freq_range->start_freq_khz =
3336 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3337 freq_range->end_freq_khz =
3338 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3339 freq_range->max_bandwidth_khz =
3340 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3341
3342 power_rule->max_eirp =
3343 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3344
3345 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3346 power_rule->max_antenna_gain =
3347 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3348
3349 return 0;
3350}
3351
3352static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3353{
3354 int r;
3355 char *data = NULL;
3356
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003357 /*
3358 * You should only get this when cfg80211 hasn't yet initialized
3359 * completely when built-in to the kernel right between the time
3360 * window between nl80211_init() and regulatory_init(), if that is
3361 * even possible.
3362 */
3363 mutex_lock(&cfg80211_mutex);
3364 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003365 mutex_unlock(&cfg80211_mutex);
3366 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003367 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003368 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003369
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003370 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3371 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003372
3373 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3374
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003375 r = regulatory_hint_user(data);
3376
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003377 return r;
3378}
3379
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003380static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003381 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003382{
Johannes Berg4c476992010-10-04 21:36:35 +02003383 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003384 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003385 struct wireless_dev *wdev = dev->ieee80211_ptr;
3386 struct mesh_config cur_params;
3387 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003388 void *hdr;
3389 struct nlattr *pinfoattr;
3390 struct sk_buff *msg;
3391
Johannes Berg29cbe682010-12-03 09:20:44 +01003392 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3393 return -EOPNOTSUPP;
3394
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003395 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003396 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003397
Johannes Berg29cbe682010-12-03 09:20:44 +01003398 wdev_lock(wdev);
3399 /* If not connected, get default parameters */
3400 if (!wdev->mesh_id_len)
3401 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3402 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003403 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003404 &cur_params);
3405 wdev_unlock(wdev);
3406
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003407 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003408 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003409
3410 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003411 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003412 if (!msg)
3413 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003414 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003415 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003416 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003417 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003418 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003419 if (!pinfoattr)
3420 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003421 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3422 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3423 cur_params.dot11MeshRetryTimeout) ||
3424 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3425 cur_params.dot11MeshConfirmTimeout) ||
3426 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3427 cur_params.dot11MeshHoldingTimeout) ||
3428 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3429 cur_params.dot11MeshMaxPeerLinks) ||
3430 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3431 cur_params.dot11MeshMaxRetries) ||
3432 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3433 cur_params.dot11MeshTTL) ||
3434 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3435 cur_params.element_ttl) ||
3436 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3437 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003438 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3439 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003440 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3441 cur_params.dot11MeshHWMPmaxPREQretries) ||
3442 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3443 cur_params.path_refresh_time) ||
3444 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3445 cur_params.min_discovery_timeout) ||
3446 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3447 cur_params.dot11MeshHWMPactivePathTimeout) ||
3448 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3449 cur_params.dot11MeshHWMPpreqMinInterval) ||
3450 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3451 cur_params.dot11MeshHWMPperrMinInterval) ||
3452 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3453 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3454 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3455 cur_params.dot11MeshHWMPRootMode) ||
3456 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3457 cur_params.dot11MeshHWMPRannInterval) ||
3458 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3459 cur_params.dot11MeshGateAnnouncementProtocol) ||
3460 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3461 cur_params.dot11MeshForwarding) ||
3462 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003463 cur_params.rssi_threshold) ||
3464 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
3465 cur_params.ht_opmode))
David S. Miller9360ffd2012-03-29 04:41:26 -04003466 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003467 nla_nest_end(msg, pinfoattr);
3468 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003469 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003470
Johannes Berg3b858752009-03-12 09:55:09 +01003471 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003472 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003473 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003474 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003475 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003476}
3477
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003478static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003479 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3480 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3481 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3482 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3483 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3484 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003485 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003486 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003487 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003488
3489 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3490 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3491 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3492 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3493 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003494 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003495 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003496 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003497 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003498 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003499 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003500 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003501 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003502};
3503
Javier Cardonac80d5452010-12-16 17:37:49 -08003504static const struct nla_policy
3505 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003506 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003507 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3508 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003509 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003510 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003511 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003512 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003513};
3514
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003515static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003516 struct mesh_config *cfg,
3517 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003518{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003519 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003520 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003521
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003522#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3523do {\
3524 if (table[attr_num]) {\
3525 cfg->param = nla_fn(table[attr_num]); \
3526 mask |= (1 << (attr_num - 1)); \
3527 } \
3528} while (0);\
3529
3530
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003531 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003532 return -EINVAL;
3533 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003534 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003535 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003536 return -EINVAL;
3537
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003538 /* This makes sure that there aren't more than 32 mesh config
3539 * parameters (otherwise our bitfield scheme would not work.) */
3540 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3541
3542 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003543 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3544 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3545 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3546 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3547 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3548 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3549 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3550 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3551 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3552 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3553 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3554 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003555 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3556 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003557 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3558 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
Javier Cardonad299a1f2012-03-31 11:31:33 -07003559 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
3560 mask, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3561 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003562 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3563 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3564 nla_get_u8);
3565 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3566 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3567 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3568 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3569 nla_get_u16);
3570 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3571 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3572 nla_get_u32);
3573 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3574 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3575 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003576 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3577 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3578 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003579 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3580 dot11MeshHWMPnetDiameterTraversalTime,
3581 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3582 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003583 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3584 dot11MeshHWMPRootMode, mask,
3585 NL80211_MESHCONF_HWMP_ROOTMODE,
3586 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003587 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3588 dot11MeshHWMPRannInterval, mask,
3589 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3590 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003591 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3592 dot11MeshGateAnnouncementProtocol, mask,
3593 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3594 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003595 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3596 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003597 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3598 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003599 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
3600 mask, NL80211_MESHCONF_HT_OPMODE, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003601 if (mask_out)
3602 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003603
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003604 return 0;
3605
3606#undef FILL_IN_MESH_PARAM_IF_SET
3607}
3608
Javier Cardonac80d5452010-12-16 17:37:49 -08003609static int nl80211_parse_mesh_setup(struct genl_info *info,
3610 struct mesh_setup *setup)
3611{
3612 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3613
3614 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3615 return -EINVAL;
3616 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3617 info->attrs[NL80211_ATTR_MESH_SETUP],
3618 nl80211_mesh_setup_params_policy))
3619 return -EINVAL;
3620
Javier Cardonad299a1f2012-03-31 11:31:33 -07003621 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3622 setup->sync_method =
3623 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3624 IEEE80211_SYNC_METHOD_VENDOR :
3625 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3626
Javier Cardonac80d5452010-12-16 17:37:49 -08003627 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3628 setup->path_sel_proto =
3629 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3630 IEEE80211_PATH_PROTOCOL_VENDOR :
3631 IEEE80211_PATH_PROTOCOL_HWMP;
3632
3633 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3634 setup->path_metric =
3635 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3636 IEEE80211_PATH_METRIC_VENDOR :
3637 IEEE80211_PATH_METRIC_AIRTIME;
3638
Javier Cardona581a8b02011-04-07 15:08:27 -07003639
3640 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003641 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003642 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003643 if (!is_valid_ie_attr(ieattr))
3644 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003645 setup->ie = nla_data(ieattr);
3646 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003647 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003648 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3649 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003650
3651 return 0;
3652}
3653
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003654static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003655 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003656{
3657 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3658 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003659 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003660 struct mesh_config cfg;
3661 u32 mask;
3662 int err;
3663
Johannes Berg29cbe682010-12-03 09:20:44 +01003664 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3665 return -EOPNOTSUPP;
3666
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003667 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003668 return -EOPNOTSUPP;
3669
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003670 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003671 if (err)
3672 return err;
3673
Johannes Berg29cbe682010-12-03 09:20:44 +01003674 wdev_lock(wdev);
3675 if (!wdev->mesh_id_len)
3676 err = -ENOLINK;
3677
3678 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003679 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003680 mask, &cfg);
3681
3682 wdev_unlock(wdev);
3683
3684 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003685}
3686
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003687static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3688{
3689 struct sk_buff *msg;
3690 void *hdr = NULL;
3691 struct nlattr *nl_reg_rules;
3692 unsigned int i;
3693 int err = -EINVAL;
3694
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003695 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003696
3697 if (!cfg80211_regdomain)
3698 goto out;
3699
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003701 if (!msg) {
3702 err = -ENOBUFS;
3703 goto out;
3704 }
3705
3706 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3707 NL80211_CMD_GET_REG);
3708 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003709 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003710
David S. Miller9360ffd2012-03-29 04:41:26 -04003711 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3712 cfg80211_regdomain->alpha2) ||
3713 (cfg80211_regdomain->dfs_region &&
3714 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3715 cfg80211_regdomain->dfs_region)))
3716 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003717
3718 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3719 if (!nl_reg_rules)
3720 goto nla_put_failure;
3721
3722 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3723 struct nlattr *nl_reg_rule;
3724 const struct ieee80211_reg_rule *reg_rule;
3725 const struct ieee80211_freq_range *freq_range;
3726 const struct ieee80211_power_rule *power_rule;
3727
3728 reg_rule = &cfg80211_regdomain->reg_rules[i];
3729 freq_range = &reg_rule->freq_range;
3730 power_rule = &reg_rule->power_rule;
3731
3732 nl_reg_rule = nla_nest_start(msg, i);
3733 if (!nl_reg_rule)
3734 goto nla_put_failure;
3735
David S. Miller9360ffd2012-03-29 04:41:26 -04003736 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3737 reg_rule->flags) ||
3738 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3739 freq_range->start_freq_khz) ||
3740 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3741 freq_range->end_freq_khz) ||
3742 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3743 freq_range->max_bandwidth_khz) ||
3744 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3745 power_rule->max_antenna_gain) ||
3746 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3747 power_rule->max_eirp))
3748 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003749
3750 nla_nest_end(msg, nl_reg_rule);
3751 }
3752
3753 nla_nest_end(msg, nl_reg_rules);
3754
3755 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003756 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003757 goto out;
3758
3759nla_put_failure:
3760 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003761put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003762 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003763 err = -EMSGSIZE;
3764out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003765 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003766 return err;
3767}
3768
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003769static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3770{
3771 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3772 struct nlattr *nl_reg_rule;
3773 char *alpha2 = NULL;
3774 int rem_reg_rules = 0, r = 0;
3775 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003776 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003777 struct ieee80211_regdomain *rd = NULL;
3778
3779 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3780 return -EINVAL;
3781
3782 if (!info->attrs[NL80211_ATTR_REG_RULES])
3783 return -EINVAL;
3784
3785 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3786
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003787 if (info->attrs[NL80211_ATTR_DFS_REGION])
3788 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3789
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003790 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3791 rem_reg_rules) {
3792 num_rules++;
3793 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003794 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003795 }
3796
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003797 mutex_lock(&cfg80211_mutex);
3798
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003799 if (!reg_is_valid_request(alpha2)) {
3800 r = -EINVAL;
3801 goto bad_reg;
3802 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003803
3804 size_of_regd = sizeof(struct ieee80211_regdomain) +
3805 (num_rules * sizeof(struct ieee80211_reg_rule));
3806
3807 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003808 if (!rd) {
3809 r = -ENOMEM;
3810 goto bad_reg;
3811 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003812
3813 rd->n_reg_rules = num_rules;
3814 rd->alpha2[0] = alpha2[0];
3815 rd->alpha2[1] = alpha2[1];
3816
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003817 /*
3818 * Disable DFS master mode if the DFS region was
3819 * not supported or known on this kernel.
3820 */
3821 if (reg_supported_dfs_region(dfs_region))
3822 rd->dfs_region = dfs_region;
3823
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003824 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3825 rem_reg_rules) {
3826 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3827 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3828 reg_rule_policy);
3829 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3830 if (r)
3831 goto bad_reg;
3832
3833 rule_idx++;
3834
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003835 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3836 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003837 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003838 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003839 }
3840
3841 BUG_ON(rule_idx != num_rules);
3842
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003843 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003844
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003845 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003846
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003847 return r;
3848
Johannes Bergd2372b32008-10-24 20:32:20 +02003849 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003850 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003851 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003852 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003853}
3854
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003855static int validate_scan_freqs(struct nlattr *freqs)
3856{
3857 struct nlattr *attr1, *attr2;
3858 int n_channels = 0, tmp1, tmp2;
3859
3860 nla_for_each_nested(attr1, freqs, tmp1) {
3861 n_channels++;
3862 /*
3863 * Some hardware has a limited channel list for
3864 * scanning, and it is pretty much nonsensical
3865 * to scan for a channel twice, so disallow that
3866 * and don't require drivers to check that the
3867 * channel list they get isn't longer than what
3868 * they can scan, as long as they can scan all
3869 * the channels they registered at once.
3870 */
3871 nla_for_each_nested(attr2, freqs, tmp2)
3872 if (attr1 != attr2 &&
3873 nla_get_u32(attr1) == nla_get_u32(attr2))
3874 return 0;
3875 }
3876
3877 return n_channels;
3878}
3879
Johannes Berg2a519312009-02-10 21:25:55 +01003880static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3881{
Johannes Berg4c476992010-10-04 21:36:35 +02003882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3883 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003884 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003885 struct nlattr *attr;
3886 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003887 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003888 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003889
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003890 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3891 return -EINVAL;
3892
Johannes Berg79c97e92009-07-07 03:56:12 +02003893 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003894
Johannes Berg4c476992010-10-04 21:36:35 +02003895 if (!rdev->ops->scan)
3896 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003897
Johannes Berg4c476992010-10-04 21:36:35 +02003898 if (rdev->scan_req)
3899 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003900
3901 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003902 n_channels = validate_scan_freqs(
3903 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003904 if (!n_channels)
3905 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003906 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003907 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003908 n_channels = 0;
3909
Johannes Berg2a519312009-02-10 21:25:55 +01003910 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3911 if (wiphy->bands[band])
3912 n_channels += wiphy->bands[band]->n_channels;
3913 }
3914
3915 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3916 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3917 n_ssids++;
3918
Johannes Berg4c476992010-10-04 21:36:35 +02003919 if (n_ssids > wiphy->max_scan_ssids)
3920 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003921
Jouni Malinen70692ad2009-02-16 19:39:13 +02003922 if (info->attrs[NL80211_ATTR_IE])
3923 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3924 else
3925 ie_len = 0;
3926
Johannes Berg4c476992010-10-04 21:36:35 +02003927 if (ie_len > wiphy->max_scan_ie_len)
3928 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003929
Johannes Berg2a519312009-02-10 21:25:55 +01003930 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003931 + sizeof(*request->ssids) * n_ssids
3932 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003933 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003934 if (!request)
3935 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003936
Johannes Berg2a519312009-02-10 21:25:55 +01003937 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003938 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003939 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003940 if (ie_len) {
3941 if (request->ssids)
3942 request->ie = (void *)(request->ssids + n_ssids);
3943 else
3944 request->ie = (void *)(request->channels + n_channels);
3945 }
Johannes Berg2a519312009-02-10 21:25:55 +01003946
Johannes Berg584991d2009-11-02 13:32:03 +01003947 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003948 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3949 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003950 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003951 struct ieee80211_channel *chan;
3952
3953 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3954
3955 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003956 err = -EINVAL;
3957 goto out_free;
3958 }
Johannes Berg584991d2009-11-02 13:32:03 +01003959
3960 /* ignore disabled channels */
3961 if (chan->flags & IEEE80211_CHAN_DISABLED)
3962 continue;
3963
3964 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003965 i++;
3966 }
3967 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003968 enum ieee80211_band band;
3969
Johannes Berg2a519312009-02-10 21:25:55 +01003970 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003971 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3972 int j;
3973 if (!wiphy->bands[band])
3974 continue;
3975 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003976 struct ieee80211_channel *chan;
3977
3978 chan = &wiphy->bands[band]->channels[j];
3979
3980 if (chan->flags & IEEE80211_CHAN_DISABLED)
3981 continue;
3982
3983 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003984 i++;
3985 }
3986 }
3987 }
3988
Johannes Berg584991d2009-11-02 13:32:03 +01003989 if (!i) {
3990 err = -EINVAL;
3991 goto out_free;
3992 }
3993
3994 request->n_channels = i;
3995
Johannes Berg2a519312009-02-10 21:25:55 +01003996 i = 0;
3997 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3998 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003999 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004000 err = -EINVAL;
4001 goto out_free;
4002 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004003 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004004 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004005 i++;
4006 }
4007 }
4008
Jouni Malinen70692ad2009-02-16 19:39:13 +02004009 if (info->attrs[NL80211_ATTR_IE]) {
4010 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004011 memcpy((void *)request->ie,
4012 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004013 request->ie_len);
4014 }
4015
Johannes Berg34850ab2011-07-18 18:08:35 +02004016 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004017 if (wiphy->bands[i])
4018 request->rates[i] =
4019 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004020
4021 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4022 nla_for_each_nested(attr,
4023 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4024 tmp) {
4025 enum ieee80211_band band = nla_type(attr);
4026
Dan Carpenter84404622011-07-29 11:52:18 +03004027 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004028 err = -EINVAL;
4029 goto out_free;
4030 }
4031 err = ieee80211_get_ratemask(wiphy->bands[band],
4032 nla_data(attr),
4033 nla_len(attr),
4034 &request->rates[band]);
4035 if (err)
4036 goto out_free;
4037 }
4038 }
4039
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304040 request->no_cck =
4041 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4042
Johannes Berg463d0182009-07-14 00:33:35 +02004043 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004044 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004045
Johannes Berg79c97e92009-07-07 03:56:12 +02004046 rdev->scan_req = request;
4047 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004048
Johannes Berg463d0182009-07-14 00:33:35 +02004049 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004050 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004051 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004052 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004053 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004054 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004055 kfree(request);
4056 }
Johannes Berg3b858752009-03-12 09:55:09 +01004057
Johannes Berg2a519312009-02-10 21:25:55 +01004058 return err;
4059}
4060
Luciano Coelho807f8a82011-05-11 17:09:35 +03004061static int nl80211_start_sched_scan(struct sk_buff *skb,
4062 struct genl_info *info)
4063{
4064 struct cfg80211_sched_scan_request *request;
4065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4066 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004067 struct nlattr *attr;
4068 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004069 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004070 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004071 enum ieee80211_band band;
4072 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004073 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004074
4075 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4076 !rdev->ops->sched_scan_start)
4077 return -EOPNOTSUPP;
4078
4079 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4080 return -EINVAL;
4081
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004082 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4083 return -EINVAL;
4084
4085 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4086 if (interval == 0)
4087 return -EINVAL;
4088
Luciano Coelho807f8a82011-05-11 17:09:35 +03004089 wiphy = &rdev->wiphy;
4090
4091 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4092 n_channels = validate_scan_freqs(
4093 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4094 if (!n_channels)
4095 return -EINVAL;
4096 } else {
4097 n_channels = 0;
4098
4099 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4100 if (wiphy->bands[band])
4101 n_channels += wiphy->bands[band]->n_channels;
4102 }
4103
4104 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4105 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4106 tmp)
4107 n_ssids++;
4108
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004109 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004110 return -EINVAL;
4111
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004112 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4113 nla_for_each_nested(attr,
4114 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4115 tmp)
4116 n_match_sets++;
4117
4118 if (n_match_sets > wiphy->max_match_sets)
4119 return -EINVAL;
4120
Luciano Coelho807f8a82011-05-11 17:09:35 +03004121 if (info->attrs[NL80211_ATTR_IE])
4122 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4123 else
4124 ie_len = 0;
4125
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004126 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004127 return -EINVAL;
4128
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004129 mutex_lock(&rdev->sched_scan_mtx);
4130
4131 if (rdev->sched_scan_req) {
4132 err = -EINPROGRESS;
4133 goto out;
4134 }
4135
Luciano Coelho807f8a82011-05-11 17:09:35 +03004136 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004137 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004138 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004139 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004140 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004141 if (!request) {
4142 err = -ENOMEM;
4143 goto out;
4144 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004145
4146 if (n_ssids)
4147 request->ssids = (void *)&request->channels[n_channels];
4148 request->n_ssids = n_ssids;
4149 if (ie_len) {
4150 if (request->ssids)
4151 request->ie = (void *)(request->ssids + n_ssids);
4152 else
4153 request->ie = (void *)(request->channels + n_channels);
4154 }
4155
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004156 if (n_match_sets) {
4157 if (request->ie)
4158 request->match_sets = (void *)(request->ie + ie_len);
4159 else if (request->ssids)
4160 request->match_sets =
4161 (void *)(request->ssids + n_ssids);
4162 else
4163 request->match_sets =
4164 (void *)(request->channels + n_channels);
4165 }
4166 request->n_match_sets = n_match_sets;
4167
Luciano Coelho807f8a82011-05-11 17:09:35 +03004168 i = 0;
4169 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4170 /* user specified, bail out if channel not found */
4171 nla_for_each_nested(attr,
4172 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4173 tmp) {
4174 struct ieee80211_channel *chan;
4175
4176 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4177
4178 if (!chan) {
4179 err = -EINVAL;
4180 goto out_free;
4181 }
4182
4183 /* ignore disabled channels */
4184 if (chan->flags & IEEE80211_CHAN_DISABLED)
4185 continue;
4186
4187 request->channels[i] = chan;
4188 i++;
4189 }
4190 } else {
4191 /* all channels */
4192 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4193 int j;
4194 if (!wiphy->bands[band])
4195 continue;
4196 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4197 struct ieee80211_channel *chan;
4198
4199 chan = &wiphy->bands[band]->channels[j];
4200
4201 if (chan->flags & IEEE80211_CHAN_DISABLED)
4202 continue;
4203
4204 request->channels[i] = chan;
4205 i++;
4206 }
4207 }
4208 }
4209
4210 if (!i) {
4211 err = -EINVAL;
4212 goto out_free;
4213 }
4214
4215 request->n_channels = i;
4216
4217 i = 0;
4218 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4219 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4220 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004221 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004222 err = -EINVAL;
4223 goto out_free;
4224 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004225 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004226 memcpy(request->ssids[i].ssid, nla_data(attr),
4227 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004228 i++;
4229 }
4230 }
4231
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004232 i = 0;
4233 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4234 nla_for_each_nested(attr,
4235 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4236 tmp) {
4237 struct nlattr *ssid;
4238
4239 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4240 nla_data(attr), nla_len(attr),
4241 nl80211_match_policy);
4242 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4243 if (ssid) {
4244 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4245 err = -EINVAL;
4246 goto out_free;
4247 }
4248 memcpy(request->match_sets[i].ssid.ssid,
4249 nla_data(ssid), nla_len(ssid));
4250 request->match_sets[i].ssid.ssid_len =
4251 nla_len(ssid);
4252 }
4253 i++;
4254 }
4255 }
4256
Luciano Coelho807f8a82011-05-11 17:09:35 +03004257 if (info->attrs[NL80211_ATTR_IE]) {
4258 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4259 memcpy((void *)request->ie,
4260 nla_data(info->attrs[NL80211_ATTR_IE]),
4261 request->ie_len);
4262 }
4263
4264 request->dev = dev;
4265 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004266 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004267
4268 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4269 if (!err) {
4270 rdev->sched_scan_req = request;
4271 nl80211_send_sched_scan(rdev, dev,
4272 NL80211_CMD_START_SCHED_SCAN);
4273 goto out;
4274 }
4275
4276out_free:
4277 kfree(request);
4278out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004279 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004280 return err;
4281}
4282
4283static int nl80211_stop_sched_scan(struct sk_buff *skb,
4284 struct genl_info *info)
4285{
4286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004287 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004288
4289 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4290 !rdev->ops->sched_scan_stop)
4291 return -EOPNOTSUPP;
4292
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004293 mutex_lock(&rdev->sched_scan_mtx);
4294 err = __cfg80211_stop_sched_scan(rdev, false);
4295 mutex_unlock(&rdev->sched_scan_mtx);
4296
4297 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004298}
4299
Johannes Berg9720bb32011-06-21 09:45:33 +02004300static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4301 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004302 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004303 struct wireless_dev *wdev,
4304 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004305{
Johannes Berg48ab9052009-07-10 18:42:31 +02004306 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004307 void *hdr;
4308 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004309
4310 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004311
Johannes Berg9720bb32011-06-21 09:45:33 +02004312 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004313 NL80211_CMD_NEW_SCAN_RESULTS);
4314 if (!hdr)
4315 return -1;
4316
Johannes Berg9720bb32011-06-21 09:45:33 +02004317 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4318
David S. Miller9360ffd2012-03-29 04:41:26 -04004319 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4320 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4321 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004322
4323 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4324 if (!bss)
4325 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004326 if ((!is_zero_ether_addr(res->bssid) &&
4327 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4328 (res->information_elements && res->len_information_elements &&
4329 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4330 res->len_information_elements,
4331 res->information_elements)) ||
4332 (res->beacon_ies && res->len_beacon_ies &&
4333 res->beacon_ies != res->information_elements &&
4334 nla_put(msg, NL80211_BSS_BEACON_IES,
4335 res->len_beacon_ies, res->beacon_ies)))
4336 goto nla_put_failure;
4337 if (res->tsf &&
4338 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4339 goto nla_put_failure;
4340 if (res->beacon_interval &&
4341 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4342 goto nla_put_failure;
4343 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4344 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4345 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4346 jiffies_to_msecs(jiffies - intbss->ts)))
4347 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004348
Johannes Berg77965c92009-02-18 18:45:06 +01004349 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004350 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004351 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4352 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004353 break;
4354 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004355 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4356 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004357 break;
4358 default:
4359 break;
4360 }
4361
Johannes Berg48ab9052009-07-10 18:42:31 +02004362 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004363 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004364 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004365 if (intbss == wdev->current_bss &&
4366 nla_put_u32(msg, NL80211_BSS_STATUS,
4367 NL80211_BSS_STATUS_ASSOCIATED))
4368 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004369 break;
4370 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004371 if (intbss == wdev->current_bss &&
4372 nla_put_u32(msg, NL80211_BSS_STATUS,
4373 NL80211_BSS_STATUS_IBSS_JOINED))
4374 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004375 break;
4376 default:
4377 break;
4378 }
4379
Johannes Berg2a519312009-02-10 21:25:55 +01004380 nla_nest_end(msg, bss);
4381
4382 return genlmsg_end(msg, hdr);
4383
4384 nla_put_failure:
4385 genlmsg_cancel(msg, hdr);
4386 return -EMSGSIZE;
4387}
4388
4389static int nl80211_dump_scan(struct sk_buff *skb,
4390 struct netlink_callback *cb)
4391{
Johannes Berg48ab9052009-07-10 18:42:31 +02004392 struct cfg80211_registered_device *rdev;
4393 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004394 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004395 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004396 int start = cb->args[1], idx = 0;
4397 int err;
4398
Johannes Berg67748892010-10-04 21:14:06 +02004399 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4400 if (err)
4401 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004402
Johannes Berg48ab9052009-07-10 18:42:31 +02004403 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004404
Johannes Berg48ab9052009-07-10 18:42:31 +02004405 wdev_lock(wdev);
4406 spin_lock_bh(&rdev->bss_lock);
4407 cfg80211_bss_expire(rdev);
4408
Johannes Berg9720bb32011-06-21 09:45:33 +02004409 cb->seq = rdev->bss_generation;
4410
Johannes Berg48ab9052009-07-10 18:42:31 +02004411 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004412 if (++idx <= start)
4413 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004414 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004415 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004416 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004417 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004418 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004419 }
4420 }
4421
Johannes Berg48ab9052009-07-10 18:42:31 +02004422 spin_unlock_bh(&rdev->bss_lock);
4423 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004424
4425 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004426 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004427
Johannes Berg67748892010-10-04 21:14:06 +02004428 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004429}
4430
Holger Schurig61fa7132009-11-11 12:25:40 +01004431static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4432 int flags, struct net_device *dev,
4433 struct survey_info *survey)
4434{
4435 void *hdr;
4436 struct nlattr *infoattr;
4437
Holger Schurig61fa7132009-11-11 12:25:40 +01004438 hdr = nl80211hdr_put(msg, pid, seq, flags,
4439 NL80211_CMD_NEW_SURVEY_RESULTS);
4440 if (!hdr)
4441 return -ENOMEM;
4442
David S. Miller9360ffd2012-03-29 04:41:26 -04004443 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4444 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004445
4446 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4447 if (!infoattr)
4448 goto nla_put_failure;
4449
David S. Miller9360ffd2012-03-29 04:41:26 -04004450 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4451 survey->channel->center_freq))
4452 goto nla_put_failure;
4453
4454 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4455 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4456 goto nla_put_failure;
4457 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4458 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4459 goto nla_put_failure;
4460 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4461 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4462 survey->channel_time))
4463 goto nla_put_failure;
4464 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4465 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4466 survey->channel_time_busy))
4467 goto nla_put_failure;
4468 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4469 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4470 survey->channel_time_ext_busy))
4471 goto nla_put_failure;
4472 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4473 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4474 survey->channel_time_rx))
4475 goto nla_put_failure;
4476 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4477 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4478 survey->channel_time_tx))
4479 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004480
4481 nla_nest_end(msg, infoattr);
4482
4483 return genlmsg_end(msg, hdr);
4484
4485 nla_put_failure:
4486 genlmsg_cancel(msg, hdr);
4487 return -EMSGSIZE;
4488}
4489
4490static int nl80211_dump_survey(struct sk_buff *skb,
4491 struct netlink_callback *cb)
4492{
4493 struct survey_info survey;
4494 struct cfg80211_registered_device *dev;
4495 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004496 int survey_idx = cb->args[1];
4497 int res;
4498
Johannes Berg67748892010-10-04 21:14:06 +02004499 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4500 if (res)
4501 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004502
4503 if (!dev->ops->dump_survey) {
4504 res = -EOPNOTSUPP;
4505 goto out_err;
4506 }
4507
4508 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004509 struct ieee80211_channel *chan;
4510
Holger Schurig61fa7132009-11-11 12:25:40 +01004511 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4512 &survey);
4513 if (res == -ENOENT)
4514 break;
4515 if (res)
4516 goto out_err;
4517
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004518 /* Survey without a channel doesn't make sense */
4519 if (!survey.channel) {
4520 res = -EINVAL;
4521 goto out;
4522 }
4523
4524 chan = ieee80211_get_channel(&dev->wiphy,
4525 survey.channel->center_freq);
4526 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4527 survey_idx++;
4528 continue;
4529 }
4530
Holger Schurig61fa7132009-11-11 12:25:40 +01004531 if (nl80211_send_survey(skb,
4532 NETLINK_CB(cb->skb).pid,
4533 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4534 netdev,
4535 &survey) < 0)
4536 goto out;
4537 survey_idx++;
4538 }
4539
4540 out:
4541 cb->args[1] = survey_idx;
4542 res = skb->len;
4543 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004544 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004545 return res;
4546}
4547
Jouni Malinen255e7372009-03-20 21:21:17 +02004548static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4549{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004550 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004551}
4552
Samuel Ortizb23aa672009-07-01 21:26:54 +02004553static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4554{
4555 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4556 NL80211_WPA_VERSION_2));
4557}
4558
Jouni Malinen636a5d32009-03-19 13:39:22 +02004559static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4560{
Johannes Berg4c476992010-10-04 21:36:35 +02004561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4562 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004563 struct ieee80211_channel *chan;
4564 const u8 *bssid, *ssid, *ie = NULL;
4565 int err, ssid_len, ie_len = 0;
4566 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004567 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004568 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004569
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004570 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4571 return -EINVAL;
4572
4573 if (!info->attrs[NL80211_ATTR_MAC])
4574 return -EINVAL;
4575
Jouni Malinen17780922009-03-27 20:52:47 +02004576 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4577 return -EINVAL;
4578
Johannes Berg19957bb2009-07-02 17:20:43 +02004579 if (!info->attrs[NL80211_ATTR_SSID])
4580 return -EINVAL;
4581
4582 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4583 return -EINVAL;
4584
Johannes Bergfffd0932009-07-08 14:22:54 +02004585 err = nl80211_parse_key(info, &key);
4586 if (err)
4587 return err;
4588
4589 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004590 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4591 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004592 if (!key.p.key || !key.p.key_len)
4593 return -EINVAL;
4594 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4595 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4596 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4597 key.p.key_len != WLAN_KEY_LEN_WEP104))
4598 return -EINVAL;
4599 if (key.idx > 4)
4600 return -EINVAL;
4601 } else {
4602 key.p.key_len = 0;
4603 key.p.key = NULL;
4604 }
4605
Johannes Bergafea0b72010-08-10 09:46:42 +02004606 if (key.idx >= 0) {
4607 int i;
4608 bool ok = false;
4609 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4610 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4611 ok = true;
4612 break;
4613 }
4614 }
Johannes Berg4c476992010-10-04 21:36:35 +02004615 if (!ok)
4616 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004617 }
4618
Johannes Berg4c476992010-10-04 21:36:35 +02004619 if (!rdev->ops->auth)
4620 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004621
Johannes Berg074ac8d2010-09-16 14:58:22 +02004622 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004623 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4624 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004625
Johannes Berg19957bb2009-07-02 17:20:43 +02004626 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004627 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004628 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004629 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4630 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004631
Johannes Berg19957bb2009-07-02 17:20:43 +02004632 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4633 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4634
4635 if (info->attrs[NL80211_ATTR_IE]) {
4636 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4637 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4638 }
4639
4640 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004641 if (!nl80211_valid_auth_type(auth_type))
4642 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004643
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004644 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4645
Johannes Berg95de8172012-01-20 13:55:25 +01004646 /*
4647 * Since we no longer track auth state, ignore
4648 * requests to only change local state.
4649 */
4650 if (local_state_change)
4651 return 0;
4652
Johannes Berg4c476992010-10-04 21:36:35 +02004653 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4654 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004655 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004656}
4657
Johannes Bergc0692b82010-08-27 14:26:53 +03004658static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4659 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004660 struct cfg80211_crypto_settings *settings,
4661 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004662{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004663 memset(settings, 0, sizeof(*settings));
4664
Samuel Ortizb23aa672009-07-01 21:26:54 +02004665 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4666
Johannes Bergc0692b82010-08-27 14:26:53 +03004667 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4668 u16 proto;
4669 proto = nla_get_u16(
4670 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4671 settings->control_port_ethertype = cpu_to_be16(proto);
4672 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4673 proto != ETH_P_PAE)
4674 return -EINVAL;
4675 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4676 settings->control_port_no_encrypt = true;
4677 } else
4678 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4679
Samuel Ortizb23aa672009-07-01 21:26:54 +02004680 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4681 void *data;
4682 int len, i;
4683
4684 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4685 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4686 settings->n_ciphers_pairwise = len / sizeof(u32);
4687
4688 if (len % sizeof(u32))
4689 return -EINVAL;
4690
Johannes Berg3dc27d22009-07-02 21:36:37 +02004691 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004692 return -EINVAL;
4693
4694 memcpy(settings->ciphers_pairwise, data, len);
4695
4696 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004697 if (!cfg80211_supported_cipher_suite(
4698 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004699 settings->ciphers_pairwise[i]))
4700 return -EINVAL;
4701 }
4702
4703 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4704 settings->cipher_group =
4705 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004706 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4707 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004708 return -EINVAL;
4709 }
4710
4711 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4712 settings->wpa_versions =
4713 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4714 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4715 return -EINVAL;
4716 }
4717
4718 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4719 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004720 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004721
4722 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4723 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4724 settings->n_akm_suites = len / sizeof(u32);
4725
4726 if (len % sizeof(u32))
4727 return -EINVAL;
4728
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004729 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4730 return -EINVAL;
4731
Samuel Ortizb23aa672009-07-01 21:26:54 +02004732 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004733 }
4734
4735 return 0;
4736}
4737
Jouni Malinen636a5d32009-03-19 13:39:22 +02004738static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4739{
Johannes Berg4c476992010-10-04 21:36:35 +02004740 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4741 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004742 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004743 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004744 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004745 int err, ssid_len, ie_len = 0;
4746 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004747 u32 flags = 0;
4748 struct ieee80211_ht_cap *ht_capa = NULL;
4749 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004750
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004751 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4752 return -EINVAL;
4753
4754 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004755 !info->attrs[NL80211_ATTR_SSID] ||
4756 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004757 return -EINVAL;
4758
Johannes Berg4c476992010-10-04 21:36:35 +02004759 if (!rdev->ops->assoc)
4760 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004761
Johannes Berg074ac8d2010-09-16 14:58:22 +02004762 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4764 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004765
Johannes Berg19957bb2009-07-02 17:20:43 +02004766 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004767
Johannes Berg19957bb2009-07-02 17:20:43 +02004768 chan = ieee80211_get_channel(&rdev->wiphy,
4769 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004770 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4771 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004772
Johannes Berg19957bb2009-07-02 17:20:43 +02004773 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4774 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004775
4776 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004777 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4778 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004779 }
4780
Jouni Malinendc6382c2009-05-06 22:09:37 +03004781 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004782 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004783 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004784 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004785 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004786 else if (mfp != NL80211_MFP_NO)
4787 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004788 }
4789
Johannes Berg3e5d7642009-07-07 14:37:26 +02004790 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4791 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4792
Ben Greear7e7c8922011-11-18 11:31:59 -08004793 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4794 flags |= ASSOC_REQ_DISABLE_HT;
4795
4796 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4797 ht_capa_mask =
4798 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4799
4800 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4801 if (!ht_capa_mask)
4802 return -EINVAL;
4803 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4804 }
4805
Johannes Bergc0692b82010-08-27 14:26:53 +03004806 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004807 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004808 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4809 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004810 &crypto, flags, ht_capa,
4811 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004812
Jouni Malinen636a5d32009-03-19 13:39:22 +02004813 return err;
4814}
4815
4816static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4817{
Johannes Berg4c476992010-10-04 21:36:35 +02004818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4819 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004820 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004821 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004822 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004823 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004824
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004825 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4826 return -EINVAL;
4827
4828 if (!info->attrs[NL80211_ATTR_MAC])
4829 return -EINVAL;
4830
4831 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4832 return -EINVAL;
4833
Johannes Berg4c476992010-10-04 21:36:35 +02004834 if (!rdev->ops->deauth)
4835 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004836
Johannes Berg074ac8d2010-09-16 14:58:22 +02004837 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004838 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4839 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004840
Johannes Berg19957bb2009-07-02 17:20:43 +02004841 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004842
Johannes Berg19957bb2009-07-02 17:20:43 +02004843 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4844 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004845 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004846 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004847 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004848
4849 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004850 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4851 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004852 }
4853
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004854 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4855
Johannes Berg4c476992010-10-04 21:36:35 +02004856 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4857 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004858}
4859
4860static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4861{
Johannes Berg4c476992010-10-04 21:36:35 +02004862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4863 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004864 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004865 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004866 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004867 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004868
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004869 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4870 return -EINVAL;
4871
4872 if (!info->attrs[NL80211_ATTR_MAC])
4873 return -EINVAL;
4874
4875 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4876 return -EINVAL;
4877
Johannes Berg4c476992010-10-04 21:36:35 +02004878 if (!rdev->ops->disassoc)
4879 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004880
Johannes Berg074ac8d2010-09-16 14:58:22 +02004881 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004882 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4883 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004884
Johannes Berg19957bb2009-07-02 17:20:43 +02004885 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004886
Johannes Berg19957bb2009-07-02 17:20:43 +02004887 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4888 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004889 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004890 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004891 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004892
4893 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004894 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4895 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004896 }
4897
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004898 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4899
Johannes Berg4c476992010-10-04 21:36:35 +02004900 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4901 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004902}
4903
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004904static bool
4905nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4906 int mcast_rate[IEEE80211_NUM_BANDS],
4907 int rateval)
4908{
4909 struct wiphy *wiphy = &rdev->wiphy;
4910 bool found = false;
4911 int band, i;
4912
4913 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4914 struct ieee80211_supported_band *sband;
4915
4916 sband = wiphy->bands[band];
4917 if (!sband)
4918 continue;
4919
4920 for (i = 0; i < sband->n_bitrates; i++) {
4921 if (sband->bitrates[i].bitrate == rateval) {
4922 mcast_rate[band] = i + 1;
4923 found = true;
4924 break;
4925 }
4926 }
4927 }
4928
4929 return found;
4930}
4931
Johannes Berg04a773a2009-04-19 21:24:32 +02004932static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4933{
Johannes Berg4c476992010-10-04 21:36:35 +02004934 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4935 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004936 struct cfg80211_ibss_params ibss;
4937 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004938 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004939 int err;
4940
Johannes Berg8e30bc52009-04-22 17:45:38 +02004941 memset(&ibss, 0, sizeof(ibss));
4942
Johannes Berg04a773a2009-04-19 21:24:32 +02004943 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4944 return -EINVAL;
4945
4946 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4947 !info->attrs[NL80211_ATTR_SSID] ||
4948 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4949 return -EINVAL;
4950
Johannes Berg8e30bc52009-04-22 17:45:38 +02004951 ibss.beacon_interval = 100;
4952
4953 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4954 ibss.beacon_interval =
4955 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4956 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4957 return -EINVAL;
4958 }
4959
Johannes Berg4c476992010-10-04 21:36:35 +02004960 if (!rdev->ops->join_ibss)
4961 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004962
Johannes Berg4c476992010-10-04 21:36:35 +02004963 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4964 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004965
Johannes Berg79c97e92009-07-07 03:56:12 +02004966 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004967
Johannes Berg39193492011-09-16 13:45:25 +02004968 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004969 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004970
4971 if (!is_valid_ether_addr(ibss.bssid))
4972 return -EINVAL;
4973 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004974 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4975 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4976
4977 if (info->attrs[NL80211_ATTR_IE]) {
4978 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4979 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4980 }
4981
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004982 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4983 enum nl80211_channel_type channel_type;
4984
Johannes Bergcd6c6592012-05-10 21:27:18 +02004985 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004986 return -EINVAL;
4987
4988 if (channel_type != NL80211_CHAN_NO_HT &&
4989 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4990 return -EINVAL;
4991
4992 ibss.channel_type = channel_type;
4993 } else {
4994 ibss.channel_type = NL80211_CHAN_NO_HT;
4995 }
4996
4997 ibss.channel = rdev_freq_to_chan(rdev,
4998 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4999 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005000 if (!ibss.channel ||
5001 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005002 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5003 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005004
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005005 /* Both channels should be able to initiate communication */
5006 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5007 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5008 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5009 ibss.channel_type))
5010 return -EINVAL;
5011
Johannes Berg04a773a2009-04-19 21:24:32 +02005012 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005013 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005014
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005015 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5016 u8 *rates =
5017 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5018 int n_rates =
5019 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5020 struct ieee80211_supported_band *sband =
5021 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005022
Johannes Berg34850ab2011-07-18 18:08:35 +02005023 err = ieee80211_get_ratemask(sband, rates, n_rates,
5024 &ibss.basic_rates);
5025 if (err)
5026 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005027 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005028
5029 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5030 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5031 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5032 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005033
Johannes Berg4c476992010-10-04 21:36:35 +02005034 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5035 connkeys = nl80211_parse_connkeys(rdev,
5036 info->attrs[NL80211_ATTR_KEYS]);
5037 if (IS_ERR(connkeys))
5038 return PTR_ERR(connkeys);
5039 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005040
Antonio Quartulli267335d2012-01-31 20:25:47 +01005041 ibss.control_port =
5042 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5043
Johannes Berg4c476992010-10-04 21:36:35 +02005044 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005045 if (err)
5046 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005047 return err;
5048}
5049
5050static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5051{
Johannes Berg4c476992010-10-04 21:36:35 +02005052 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5053 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005054
Johannes Berg4c476992010-10-04 21:36:35 +02005055 if (!rdev->ops->leave_ibss)
5056 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005057
Johannes Berg4c476992010-10-04 21:36:35 +02005058 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5059 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005060
Johannes Berg4c476992010-10-04 21:36:35 +02005061 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005062}
5063
Johannes Bergaff89a92009-07-01 21:26:51 +02005064#ifdef CONFIG_NL80211_TESTMODE
5065static struct genl_multicast_group nl80211_testmode_mcgrp = {
5066 .name = "testmode",
5067};
5068
5069static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5070{
Johannes Berg4c476992010-10-04 21:36:35 +02005071 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005072 int err;
5073
5074 if (!info->attrs[NL80211_ATTR_TESTDATA])
5075 return -EINVAL;
5076
Johannes Bergaff89a92009-07-01 21:26:51 +02005077 err = -EOPNOTSUPP;
5078 if (rdev->ops->testmode_cmd) {
5079 rdev->testmode_info = info;
5080 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5081 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5082 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5083 rdev->testmode_info = NULL;
5084 }
5085
Johannes Bergaff89a92009-07-01 21:26:51 +02005086 return err;
5087}
5088
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005089static int nl80211_testmode_dump(struct sk_buff *skb,
5090 struct netlink_callback *cb)
5091{
Johannes Berg00918d32011-12-13 17:22:05 +01005092 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005093 int err;
5094 long phy_idx;
5095 void *data = NULL;
5096 int data_len = 0;
5097
5098 if (cb->args[0]) {
5099 /*
5100 * 0 is a valid index, but not valid for args[0],
5101 * so we need to offset by 1.
5102 */
5103 phy_idx = cb->args[0] - 1;
5104 } else {
5105 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5106 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5107 nl80211_policy);
5108 if (err)
5109 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005110 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5111 phy_idx = nla_get_u32(
5112 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5113 } else {
5114 struct net_device *netdev;
5115
5116 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5117 nl80211_fam.attrbuf,
5118 &rdev, &netdev);
5119 if (err)
5120 return err;
5121 dev_put(netdev);
5122 phy_idx = rdev->wiphy_idx;
5123 cfg80211_unlock_rdev(rdev);
5124 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005125 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5126 cb->args[1] =
5127 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5128 }
5129
5130 if (cb->args[1]) {
5131 data = nla_data((void *)cb->args[1]);
5132 data_len = nla_len((void *)cb->args[1]);
5133 }
5134
5135 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005136 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5137 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005138 mutex_unlock(&cfg80211_mutex);
5139 return -ENOENT;
5140 }
Johannes Berg00918d32011-12-13 17:22:05 +01005141 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005142 mutex_unlock(&cfg80211_mutex);
5143
Johannes Berg00918d32011-12-13 17:22:05 +01005144 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005145 err = -EOPNOTSUPP;
5146 goto out_err;
5147 }
5148
5149 while (1) {
5150 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5151 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5152 NL80211_CMD_TESTMODE);
5153 struct nlattr *tmdata;
5154
David S. Miller9360ffd2012-03-29 04:41:26 -04005155 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005156 genlmsg_cancel(skb, hdr);
5157 break;
5158 }
5159
5160 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5161 if (!tmdata) {
5162 genlmsg_cancel(skb, hdr);
5163 break;
5164 }
Johannes Berg00918d32011-12-13 17:22:05 +01005165 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5166 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005167 nla_nest_end(skb, tmdata);
5168
5169 if (err == -ENOBUFS || err == -ENOENT) {
5170 genlmsg_cancel(skb, hdr);
5171 break;
5172 } else if (err) {
5173 genlmsg_cancel(skb, hdr);
5174 goto out_err;
5175 }
5176
5177 genlmsg_end(skb, hdr);
5178 }
5179
5180 err = skb->len;
5181 /* see above */
5182 cb->args[0] = phy_idx + 1;
5183 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005184 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005185 return err;
5186}
5187
Johannes Bergaff89a92009-07-01 21:26:51 +02005188static struct sk_buff *
5189__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5190 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5191{
5192 struct sk_buff *skb;
5193 void *hdr;
5194 struct nlattr *data;
5195
5196 skb = nlmsg_new(approxlen + 100, gfp);
5197 if (!skb)
5198 return NULL;
5199
5200 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5201 if (!hdr) {
5202 kfree_skb(skb);
5203 return NULL;
5204 }
5205
David S. Miller9360ffd2012-03-29 04:41:26 -04005206 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5207 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005208 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5209
5210 ((void **)skb->cb)[0] = rdev;
5211 ((void **)skb->cb)[1] = hdr;
5212 ((void **)skb->cb)[2] = data;
5213
5214 return skb;
5215
5216 nla_put_failure:
5217 kfree_skb(skb);
5218 return NULL;
5219}
5220
5221struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5222 int approxlen)
5223{
5224 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5225
5226 if (WARN_ON(!rdev->testmode_info))
5227 return NULL;
5228
5229 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5230 rdev->testmode_info->snd_pid,
5231 rdev->testmode_info->snd_seq,
5232 GFP_KERNEL);
5233}
5234EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5235
5236int cfg80211_testmode_reply(struct sk_buff *skb)
5237{
5238 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5239 void *hdr = ((void **)skb->cb)[1];
5240 struct nlattr *data = ((void **)skb->cb)[2];
5241
5242 if (WARN_ON(!rdev->testmode_info)) {
5243 kfree_skb(skb);
5244 return -EINVAL;
5245 }
5246
5247 nla_nest_end(skb, data);
5248 genlmsg_end(skb, hdr);
5249 return genlmsg_reply(skb, rdev->testmode_info);
5250}
5251EXPORT_SYMBOL(cfg80211_testmode_reply);
5252
5253struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5254 int approxlen, gfp_t gfp)
5255{
5256 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5257
5258 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5259}
5260EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5261
5262void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5263{
5264 void *hdr = ((void **)skb->cb)[1];
5265 struct nlattr *data = ((void **)skb->cb)[2];
5266
5267 nla_nest_end(skb, data);
5268 genlmsg_end(skb, hdr);
5269 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5270}
5271EXPORT_SYMBOL(cfg80211_testmode_event);
5272#endif
5273
Samuel Ortizb23aa672009-07-01 21:26:54 +02005274static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5275{
Johannes Berg4c476992010-10-04 21:36:35 +02005276 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5277 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005278 struct cfg80211_connect_params connect;
5279 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005280 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005281 int err;
5282
5283 memset(&connect, 0, sizeof(connect));
5284
5285 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5286 return -EINVAL;
5287
5288 if (!info->attrs[NL80211_ATTR_SSID] ||
5289 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5290 return -EINVAL;
5291
5292 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5293 connect.auth_type =
5294 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5295 if (!nl80211_valid_auth_type(connect.auth_type))
5296 return -EINVAL;
5297 } else
5298 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5299
5300 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5301
Johannes Bergc0692b82010-08-27 14:26:53 +03005302 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005303 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005304 if (err)
5305 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005306
Johannes Berg074ac8d2010-09-16 14:58:22 +02005307 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005308 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5309 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005310
Johannes Berg79c97e92009-07-07 03:56:12 +02005311 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005312
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305313 connect.bg_scan_period = -1;
5314 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5315 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5316 connect.bg_scan_period =
5317 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5318 }
5319
Samuel Ortizb23aa672009-07-01 21:26:54 +02005320 if (info->attrs[NL80211_ATTR_MAC])
5321 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5322 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5323 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5324
5325 if (info->attrs[NL80211_ATTR_IE]) {
5326 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5327 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5328 }
5329
5330 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5331 connect.channel =
5332 ieee80211_get_channel(wiphy,
5333 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5334 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005335 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5336 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005337 }
5338
Johannes Bergfffd0932009-07-08 14:22:54 +02005339 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5340 connkeys = nl80211_parse_connkeys(rdev,
5341 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005342 if (IS_ERR(connkeys))
5343 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005344 }
5345
Ben Greear7e7c8922011-11-18 11:31:59 -08005346 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5347 connect.flags |= ASSOC_REQ_DISABLE_HT;
5348
5349 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5350 memcpy(&connect.ht_capa_mask,
5351 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5352 sizeof(connect.ht_capa_mask));
5353
5354 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5355 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5356 return -EINVAL;
5357 memcpy(&connect.ht_capa,
5358 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5359 sizeof(connect.ht_capa));
5360 }
5361
Johannes Bergfffd0932009-07-08 14:22:54 +02005362 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005363 if (err)
5364 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005365 return err;
5366}
5367
5368static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5369{
Johannes Berg4c476992010-10-04 21:36:35 +02005370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5371 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005372 u16 reason;
5373
5374 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5375 reason = WLAN_REASON_DEAUTH_LEAVING;
5376 else
5377 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5378
5379 if (reason == 0)
5380 return -EINVAL;
5381
Johannes Berg074ac8d2010-09-16 14:58:22 +02005382 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005383 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5384 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005385
Johannes Berg4c476992010-10-04 21:36:35 +02005386 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005387}
5388
Johannes Berg463d0182009-07-14 00:33:35 +02005389static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5390{
Johannes Berg4c476992010-10-04 21:36:35 +02005391 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005392 struct net *net;
5393 int err;
5394 u32 pid;
5395
5396 if (!info->attrs[NL80211_ATTR_PID])
5397 return -EINVAL;
5398
5399 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5400
Johannes Berg463d0182009-07-14 00:33:35 +02005401 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005402 if (IS_ERR(net))
5403 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005404
5405 err = 0;
5406
5407 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005408 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5409 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005410
Johannes Berg463d0182009-07-14 00:33:35 +02005411 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005412 return err;
5413}
5414
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005415static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5416{
Johannes Berg4c476992010-10-04 21:36:35 +02005417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005418 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5419 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005420 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005421 struct cfg80211_pmksa pmksa;
5422
5423 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5424
5425 if (!info->attrs[NL80211_ATTR_MAC])
5426 return -EINVAL;
5427
5428 if (!info->attrs[NL80211_ATTR_PMKID])
5429 return -EINVAL;
5430
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005431 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5432 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5433
Johannes Berg074ac8d2010-09-16 14:58:22 +02005434 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005435 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5436 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005437
5438 switch (info->genlhdr->cmd) {
5439 case NL80211_CMD_SET_PMKSA:
5440 rdev_ops = rdev->ops->set_pmksa;
5441 break;
5442 case NL80211_CMD_DEL_PMKSA:
5443 rdev_ops = rdev->ops->del_pmksa;
5444 break;
5445 default:
5446 WARN_ON(1);
5447 break;
5448 }
5449
Johannes Berg4c476992010-10-04 21:36:35 +02005450 if (!rdev_ops)
5451 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005452
Johannes Berg4c476992010-10-04 21:36:35 +02005453 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005454}
5455
5456static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5457{
Johannes Berg4c476992010-10-04 21:36:35 +02005458 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5459 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005460
Johannes Berg074ac8d2010-09-16 14:58:22 +02005461 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005462 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5463 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005464
Johannes Berg4c476992010-10-04 21:36:35 +02005465 if (!rdev->ops->flush_pmksa)
5466 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005467
Johannes Berg4c476992010-10-04 21:36:35 +02005468 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005469}
5470
Arik Nemtsov109086c2011-09-28 14:12:50 +03005471static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5472{
5473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5474 struct net_device *dev = info->user_ptr[1];
5475 u8 action_code, dialog_token;
5476 u16 status_code;
5477 u8 *peer;
5478
5479 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5480 !rdev->ops->tdls_mgmt)
5481 return -EOPNOTSUPP;
5482
5483 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5484 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5485 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5486 !info->attrs[NL80211_ATTR_IE] ||
5487 !info->attrs[NL80211_ATTR_MAC])
5488 return -EINVAL;
5489
5490 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5491 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5492 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5493 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5494
5495 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5496 dialog_token, status_code,
5497 nla_data(info->attrs[NL80211_ATTR_IE]),
5498 nla_len(info->attrs[NL80211_ATTR_IE]));
5499}
5500
5501static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5502{
5503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5504 struct net_device *dev = info->user_ptr[1];
5505 enum nl80211_tdls_operation operation;
5506 u8 *peer;
5507
5508 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5509 !rdev->ops->tdls_oper)
5510 return -EOPNOTSUPP;
5511
5512 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5513 !info->attrs[NL80211_ATTR_MAC])
5514 return -EINVAL;
5515
5516 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5517 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5518
5519 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5520}
5521
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005522static int nl80211_remain_on_channel(struct sk_buff *skb,
5523 struct genl_info *info)
5524{
Johannes Berg4c476992010-10-04 21:36:35 +02005525 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5526 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005527 struct ieee80211_channel *chan;
5528 struct sk_buff *msg;
5529 void *hdr;
5530 u64 cookie;
5531 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5532 u32 freq, duration;
5533 int err;
5534
5535 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5536 !info->attrs[NL80211_ATTR_DURATION])
5537 return -EINVAL;
5538
5539 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5540
5541 /*
5542 * We should be on that channel for at least one jiffie,
5543 * and more than 5 seconds seems excessive.
5544 */
Johannes Berga2939112010-12-14 17:54:28 +01005545 if (!duration || !msecs_to_jiffies(duration) ||
5546 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005547 return -EINVAL;
5548
Johannes Berg7c4ef712011-11-18 15:33:48 +01005549 if (!rdev->ops->remain_on_channel ||
5550 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005551 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005552
Johannes Bergcd6c6592012-05-10 21:27:18 +02005553 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5554 !nl80211_valid_channel_type(info, &channel_type))
5555 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005556
5557 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5558 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005559 if (chan == NULL)
5560 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005561
5562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005563 if (!msg)
5564 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005565
5566 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5567 NL80211_CMD_REMAIN_ON_CHANNEL);
5568
5569 if (IS_ERR(hdr)) {
5570 err = PTR_ERR(hdr);
5571 goto free_msg;
5572 }
5573
5574 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5575 channel_type, duration, &cookie);
5576
5577 if (err)
5578 goto free_msg;
5579
David S. Miller9360ffd2012-03-29 04:41:26 -04005580 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5581 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005582
5583 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005584
5585 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005586
5587 nla_put_failure:
5588 err = -ENOBUFS;
5589 free_msg:
5590 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005591 return err;
5592}
5593
5594static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5595 struct genl_info *info)
5596{
Johannes Berg4c476992010-10-04 21:36:35 +02005597 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5598 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005599 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005600
5601 if (!info->attrs[NL80211_ATTR_COOKIE])
5602 return -EINVAL;
5603
Johannes Berg4c476992010-10-04 21:36:35 +02005604 if (!rdev->ops->cancel_remain_on_channel)
5605 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005606
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005607 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5608
Johannes Berg4c476992010-10-04 21:36:35 +02005609 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005610}
5611
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005612static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5613 u8 *rates, u8 rates_len)
5614{
5615 u8 i;
5616 u32 mask = 0;
5617
5618 for (i = 0; i < rates_len; i++) {
5619 int rate = (rates[i] & 0x7f) * 5;
5620 int ridx;
5621 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5622 struct ieee80211_rate *srate =
5623 &sband->bitrates[ridx];
5624 if (rate == srate->bitrate) {
5625 mask |= 1 << ridx;
5626 break;
5627 }
5628 }
5629 if (ridx == sband->n_bitrates)
5630 return 0; /* rate not found */
5631 }
5632
5633 return mask;
5634}
5635
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005636static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5637 u8 *rates, u8 rates_len,
5638 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5639{
5640 u8 i;
5641
5642 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5643
5644 for (i = 0; i < rates_len; i++) {
5645 int ridx, rbit;
5646
5647 ridx = rates[i] / 8;
5648 rbit = BIT(rates[i] % 8);
5649
5650 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005651 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005652 return false;
5653
5654 /* check availability */
5655 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5656 mcs[ridx] |= rbit;
5657 else
5658 return false;
5659 }
5660
5661 return true;
5662}
5663
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005664static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005665 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5666 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005667 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5668 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005669};
5670
5671static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5672 struct genl_info *info)
5673{
5674 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005676 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005677 int rem, i;
5678 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005679 struct nlattr *tx_rates;
5680 struct ieee80211_supported_band *sband;
5681
5682 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5683 return -EINVAL;
5684
Johannes Berg4c476992010-10-04 21:36:35 +02005685 if (!rdev->ops->set_bitrate_mask)
5686 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005687
5688 memset(&mask, 0, sizeof(mask));
5689 /* Default to all rates enabled */
5690 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5691 sband = rdev->wiphy.bands[i];
5692 mask.control[i].legacy =
5693 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005694 if (sband)
5695 memcpy(mask.control[i].mcs,
5696 sband->ht_cap.mcs.rx_mask,
5697 sizeof(mask.control[i].mcs));
5698 else
5699 memset(mask.control[i].mcs, 0,
5700 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005701 }
5702
5703 /*
5704 * The nested attribute uses enum nl80211_band as the index. This maps
5705 * directly to the enum ieee80211_band values used in cfg80211.
5706 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005707 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005708 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5709 {
5710 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005711 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5712 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005713 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005714 if (sband == NULL)
5715 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005716 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5717 nla_len(tx_rates), nl80211_txattr_policy);
5718 if (tb[NL80211_TXRATE_LEGACY]) {
5719 mask.control[band].legacy = rateset_to_mask(
5720 sband,
5721 nla_data(tb[NL80211_TXRATE_LEGACY]),
5722 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305723 if ((mask.control[band].legacy == 0) &&
5724 nla_len(tb[NL80211_TXRATE_LEGACY]))
5725 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005726 }
5727 if (tb[NL80211_TXRATE_MCS]) {
5728 if (!ht_rateset_to_mask(
5729 sband,
5730 nla_data(tb[NL80211_TXRATE_MCS]),
5731 nla_len(tb[NL80211_TXRATE_MCS]),
5732 mask.control[band].mcs))
5733 return -EINVAL;
5734 }
5735
5736 if (mask.control[band].legacy == 0) {
5737 /* don't allow empty legacy rates if HT
5738 * is not even supported. */
5739 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5740 return -EINVAL;
5741
5742 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5743 if (mask.control[band].mcs[i])
5744 break;
5745
5746 /* legacy and mcs rates may not be both empty */
5747 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005748 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005749 }
5750 }
5751
Johannes Berg4c476992010-10-04 21:36:35 +02005752 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005753}
5754
Johannes Berg2e161f72010-08-12 15:38:38 +02005755static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005756{
Johannes Berg4c476992010-10-04 21:36:35 +02005757 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5758 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005759 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005760
5761 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5762 return -EINVAL;
5763
Johannes Berg2e161f72010-08-12 15:38:38 +02005764 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5765 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005766
Johannes Berg9d38d852010-06-09 17:20:33 +02005767 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005768 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5770 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5771 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005772 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005773 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5774 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005775
5776 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005777 if (!rdev->ops->mgmt_tx)
5778 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005779
Johannes Berg4c476992010-10-04 21:36:35 +02005780 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005781 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005782 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5783 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005784}
5785
Johannes Berg2e161f72010-08-12 15:38:38 +02005786static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005787{
Johannes Berg4c476992010-10-04 21:36:35 +02005788 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5789 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005790 struct ieee80211_channel *chan;
5791 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005792 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005793 u32 freq;
5794 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005795 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005796 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005797 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005798 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005799 bool offchan, no_cck, dont_wait_for_ack;
5800
5801 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005802
5803 if (!info->attrs[NL80211_ATTR_FRAME] ||
5804 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5805 return -EINVAL;
5806
Johannes Berg4c476992010-10-04 21:36:35 +02005807 if (!rdev->ops->mgmt_tx)
5808 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005809
Johannes Berg9d38d852010-06-09 17:20:33 +02005810 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005811 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005812 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5813 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5814 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005815 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005816 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5817 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005818
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005819 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005820 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005821 return -EINVAL;
5822 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5823 }
5824
Jouni Malinen026331c2010-02-15 12:53:10 +02005825 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02005826 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02005827 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005828 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005829 }
5830
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005831 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5832
Johannes Berg7c4ef712011-11-18 15:33:48 +01005833 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5834 return -EINVAL;
5835
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305836 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5837
Jouni Malinen026331c2010-02-15 12:53:10 +02005838 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5839 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005840 if (chan == NULL)
5841 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005842
Johannes Berge247bd902011-11-04 11:18:21 +01005843 if (!dont_wait_for_ack) {
5844 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5845 if (!msg)
5846 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005847
Johannes Berge247bd902011-11-04 11:18:21 +01005848 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5849 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005850
Johannes Berge247bd902011-11-04 11:18:21 +01005851 if (IS_ERR(hdr)) {
5852 err = PTR_ERR(hdr);
5853 goto free_msg;
5854 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005855 }
Johannes Berge247bd902011-11-04 11:18:21 +01005856
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005857 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5858 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005859 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5860 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005861 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005862 if (err)
5863 goto free_msg;
5864
Johannes Berge247bd902011-11-04 11:18:21 +01005865 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04005866 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5867 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02005868
Johannes Berge247bd902011-11-04 11:18:21 +01005869 genlmsg_end(msg, hdr);
5870 return genlmsg_reply(msg, info);
5871 }
5872
5873 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005874
5875 nla_put_failure:
5876 err = -ENOBUFS;
5877 free_msg:
5878 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005879 return err;
5880}
5881
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005882static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5883{
5884 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5885 struct net_device *dev = info->user_ptr[1];
5886 u64 cookie;
5887
5888 if (!info->attrs[NL80211_ATTR_COOKIE])
5889 return -EINVAL;
5890
5891 if (!rdev->ops->mgmt_tx_cancel_wait)
5892 return -EOPNOTSUPP;
5893
5894 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5895 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5896 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5897 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5898 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5899 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5900 return -EOPNOTSUPP;
5901
5902 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5903
5904 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5905}
5906
Kalle Valoffb9eb32010-02-17 17:58:10 +02005907static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5908{
Johannes Berg4c476992010-10-04 21:36:35 +02005909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005910 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005911 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005912 u8 ps_state;
5913 bool state;
5914 int err;
5915
Johannes Berg4c476992010-10-04 21:36:35 +02005916 if (!info->attrs[NL80211_ATTR_PS_STATE])
5917 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005918
5919 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5920
Johannes Berg4c476992010-10-04 21:36:35 +02005921 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5922 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005923
5924 wdev = dev->ieee80211_ptr;
5925
Johannes Berg4c476992010-10-04 21:36:35 +02005926 if (!rdev->ops->set_power_mgmt)
5927 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005928
5929 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5930
5931 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005932 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005933
Johannes Berg4c476992010-10-04 21:36:35 +02005934 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5935 wdev->ps_timeout);
5936 if (!err)
5937 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005938 return err;
5939}
5940
5941static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5942{
Johannes Berg4c476992010-10-04 21:36:35 +02005943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005944 enum nl80211_ps_state ps_state;
5945 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005946 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005947 struct sk_buff *msg;
5948 void *hdr;
5949 int err;
5950
Kalle Valoffb9eb32010-02-17 17:58:10 +02005951 wdev = dev->ieee80211_ptr;
5952
Johannes Berg4c476992010-10-04 21:36:35 +02005953 if (!rdev->ops->set_power_mgmt)
5954 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005955
5956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005957 if (!msg)
5958 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005959
5960 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5961 NL80211_CMD_GET_POWER_SAVE);
5962 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005963 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005964 goto free_msg;
5965 }
5966
5967 if (wdev->ps)
5968 ps_state = NL80211_PS_ENABLED;
5969 else
5970 ps_state = NL80211_PS_DISABLED;
5971
David S. Miller9360ffd2012-03-29 04:41:26 -04005972 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
5973 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005974
5975 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005976 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005977
Johannes Berg4c476992010-10-04 21:36:35 +02005978 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005979 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005980 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005981 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005982 return err;
5983}
5984
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005985static struct nla_policy
5986nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5987 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5988 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5989 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5990};
5991
5992static int nl80211_set_cqm_rssi(struct genl_info *info,
5993 s32 threshold, u32 hysteresis)
5994{
Johannes Berg4c476992010-10-04 21:36:35 +02005995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005996 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005997 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005998
5999 if (threshold > 0)
6000 return -EINVAL;
6001
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006002 wdev = dev->ieee80211_ptr;
6003
Johannes Berg4c476992010-10-04 21:36:35 +02006004 if (!rdev->ops->set_cqm_rssi_config)
6005 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006006
Johannes Berg074ac8d2010-09-16 14:58:22 +02006007 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006008 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6009 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006010
Johannes Berg4c476992010-10-04 21:36:35 +02006011 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6012 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006013}
6014
6015static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6016{
6017 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6018 struct nlattr *cqm;
6019 int err;
6020
6021 cqm = info->attrs[NL80211_ATTR_CQM];
6022 if (!cqm) {
6023 err = -EINVAL;
6024 goto out;
6025 }
6026
6027 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6028 nl80211_attr_cqm_policy);
6029 if (err)
6030 goto out;
6031
6032 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6033 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6034 s32 threshold;
6035 u32 hysteresis;
6036 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6037 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6038 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6039 } else
6040 err = -EINVAL;
6041
6042out:
6043 return err;
6044}
6045
Johannes Berg29cbe682010-12-03 09:20:44 +01006046static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6047{
6048 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6049 struct net_device *dev = info->user_ptr[1];
6050 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006051 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006052 int err;
6053
6054 /* start with default */
6055 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006056 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006057
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006058 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006059 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006060 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006061 if (err)
6062 return err;
6063 }
6064
6065 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6066 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6067 return -EINVAL;
6068
Javier Cardonac80d5452010-12-16 17:37:49 -08006069 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6070 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6071
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006072 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6073 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6074 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6075 return -EINVAL;
6076
Javier Cardonac80d5452010-12-16 17:37:49 -08006077 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6078 /* parse additional setup parameters if given */
6079 err = nl80211_parse_mesh_setup(info, &setup);
6080 if (err)
6081 return err;
6082 }
6083
6084 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006085}
6086
6087static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6088{
6089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6090 struct net_device *dev = info->user_ptr[1];
6091
6092 return cfg80211_leave_mesh(rdev, dev);
6093}
6094
Johannes Bergff1b6e62011-05-04 15:37:28 +02006095static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6096{
6097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6098 struct sk_buff *msg;
6099 void *hdr;
6100
6101 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6102 return -EOPNOTSUPP;
6103
6104 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6105 if (!msg)
6106 return -ENOMEM;
6107
6108 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6109 NL80211_CMD_GET_WOWLAN);
6110 if (!hdr)
6111 goto nla_put_failure;
6112
6113 if (rdev->wowlan) {
6114 struct nlattr *nl_wowlan;
6115
6116 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6117 if (!nl_wowlan)
6118 goto nla_put_failure;
6119
David S. Miller9360ffd2012-03-29 04:41:26 -04006120 if ((rdev->wowlan->any &&
6121 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6122 (rdev->wowlan->disconnect &&
6123 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6124 (rdev->wowlan->magic_pkt &&
6125 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6126 (rdev->wowlan->gtk_rekey_failure &&
6127 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6128 (rdev->wowlan->eap_identity_req &&
6129 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6130 (rdev->wowlan->four_way_handshake &&
6131 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6132 (rdev->wowlan->rfkill_release &&
6133 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6134 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006135 if (rdev->wowlan->n_patterns) {
6136 struct nlattr *nl_pats, *nl_pat;
6137 int i, pat_len;
6138
6139 nl_pats = nla_nest_start(msg,
6140 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6141 if (!nl_pats)
6142 goto nla_put_failure;
6143
6144 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6145 nl_pat = nla_nest_start(msg, i + 1);
6146 if (!nl_pat)
6147 goto nla_put_failure;
6148 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006149 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6150 DIV_ROUND_UP(pat_len, 8),
6151 rdev->wowlan->patterns[i].mask) ||
6152 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6153 pat_len,
6154 rdev->wowlan->patterns[i].pattern))
6155 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006156 nla_nest_end(msg, nl_pat);
6157 }
6158 nla_nest_end(msg, nl_pats);
6159 }
6160
6161 nla_nest_end(msg, nl_wowlan);
6162 }
6163
6164 genlmsg_end(msg, hdr);
6165 return genlmsg_reply(msg, info);
6166
6167nla_put_failure:
6168 nlmsg_free(msg);
6169 return -ENOBUFS;
6170}
6171
6172static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6173{
6174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6175 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6176 struct cfg80211_wowlan no_triggers = {};
6177 struct cfg80211_wowlan new_triggers = {};
6178 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6179 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006180 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006181
6182 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6183 return -EOPNOTSUPP;
6184
6185 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6186 goto no_triggers;
6187
6188 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6189 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6190 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6191 nl80211_wowlan_policy);
6192 if (err)
6193 return err;
6194
6195 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6196 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6197 return -EINVAL;
6198 new_triggers.any = true;
6199 }
6200
6201 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6202 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6203 return -EINVAL;
6204 new_triggers.disconnect = true;
6205 }
6206
6207 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6208 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6209 return -EINVAL;
6210 new_triggers.magic_pkt = true;
6211 }
6212
Johannes Berg77dbbb12011-07-13 10:48:55 +02006213 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6214 return -EINVAL;
6215
6216 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6217 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6218 return -EINVAL;
6219 new_triggers.gtk_rekey_failure = true;
6220 }
6221
6222 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6223 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6224 return -EINVAL;
6225 new_triggers.eap_identity_req = true;
6226 }
6227
6228 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6229 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6230 return -EINVAL;
6231 new_triggers.four_way_handshake = true;
6232 }
6233
6234 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6235 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6236 return -EINVAL;
6237 new_triggers.rfkill_release = true;
6238 }
6239
Johannes Bergff1b6e62011-05-04 15:37:28 +02006240 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6241 struct nlattr *pat;
6242 int n_patterns = 0;
6243 int rem, pat_len, mask_len;
6244 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6245
6246 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6247 rem)
6248 n_patterns++;
6249 if (n_patterns > wowlan->n_patterns)
6250 return -EINVAL;
6251
6252 new_triggers.patterns = kcalloc(n_patterns,
6253 sizeof(new_triggers.patterns[0]),
6254 GFP_KERNEL);
6255 if (!new_triggers.patterns)
6256 return -ENOMEM;
6257
6258 new_triggers.n_patterns = n_patterns;
6259 i = 0;
6260
6261 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6262 rem) {
6263 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6264 nla_data(pat), nla_len(pat), NULL);
6265 err = -EINVAL;
6266 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6267 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6268 goto error;
6269 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6270 mask_len = DIV_ROUND_UP(pat_len, 8);
6271 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6272 mask_len)
6273 goto error;
6274 if (pat_len > wowlan->pattern_max_len ||
6275 pat_len < wowlan->pattern_min_len)
6276 goto error;
6277
6278 new_triggers.patterns[i].mask =
6279 kmalloc(mask_len + pat_len, GFP_KERNEL);
6280 if (!new_triggers.patterns[i].mask) {
6281 err = -ENOMEM;
6282 goto error;
6283 }
6284 new_triggers.patterns[i].pattern =
6285 new_triggers.patterns[i].mask + mask_len;
6286 memcpy(new_triggers.patterns[i].mask,
6287 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6288 mask_len);
6289 new_triggers.patterns[i].pattern_len = pat_len;
6290 memcpy(new_triggers.patterns[i].pattern,
6291 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6292 pat_len);
6293 i++;
6294 }
6295 }
6296
6297 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6298 struct cfg80211_wowlan *ntrig;
6299 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6300 GFP_KERNEL);
6301 if (!ntrig) {
6302 err = -ENOMEM;
6303 goto error;
6304 }
6305 cfg80211_rdev_free_wowlan(rdev);
6306 rdev->wowlan = ntrig;
6307 } else {
6308 no_triggers:
6309 cfg80211_rdev_free_wowlan(rdev);
6310 rdev->wowlan = NULL;
6311 }
6312
Johannes Berg6d525632012-04-04 15:05:25 +02006313 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6314 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6315
Johannes Bergff1b6e62011-05-04 15:37:28 +02006316 return 0;
6317 error:
6318 for (i = 0; i < new_triggers.n_patterns; i++)
6319 kfree(new_triggers.patterns[i].mask);
6320 kfree(new_triggers.patterns);
6321 return err;
6322}
6323
Johannes Berge5497d72011-07-05 16:35:40 +02006324static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6325{
6326 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6327 struct net_device *dev = info->user_ptr[1];
6328 struct wireless_dev *wdev = dev->ieee80211_ptr;
6329 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6330 struct cfg80211_gtk_rekey_data rekey_data;
6331 int err;
6332
6333 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6334 return -EINVAL;
6335
6336 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6337 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6338 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6339 nl80211_rekey_policy);
6340 if (err)
6341 return err;
6342
6343 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6344 return -ERANGE;
6345 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6346 return -ERANGE;
6347 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6348 return -ERANGE;
6349
6350 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6351 NL80211_KEK_LEN);
6352 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6353 NL80211_KCK_LEN);
6354 memcpy(rekey_data.replay_ctr,
6355 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6356 NL80211_REPLAY_CTR_LEN);
6357
6358 wdev_lock(wdev);
6359 if (!wdev->current_bss) {
6360 err = -ENOTCONN;
6361 goto out;
6362 }
6363
6364 if (!rdev->ops->set_rekey_data) {
6365 err = -EOPNOTSUPP;
6366 goto out;
6367 }
6368
6369 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6370 out:
6371 wdev_unlock(wdev);
6372 return err;
6373}
6374
Johannes Berg28946da2011-11-04 11:18:12 +01006375static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6376 struct genl_info *info)
6377{
6378 struct net_device *dev = info->user_ptr[1];
6379 struct wireless_dev *wdev = dev->ieee80211_ptr;
6380
6381 if (wdev->iftype != NL80211_IFTYPE_AP &&
6382 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6383 return -EINVAL;
6384
6385 if (wdev->ap_unexpected_nlpid)
6386 return -EBUSY;
6387
6388 wdev->ap_unexpected_nlpid = info->snd_pid;
6389 return 0;
6390}
6391
Johannes Berg7f6cf312011-11-04 11:18:15 +01006392static int nl80211_probe_client(struct sk_buff *skb,
6393 struct genl_info *info)
6394{
6395 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6396 struct net_device *dev = info->user_ptr[1];
6397 struct wireless_dev *wdev = dev->ieee80211_ptr;
6398 struct sk_buff *msg;
6399 void *hdr;
6400 const u8 *addr;
6401 u64 cookie;
6402 int err;
6403
6404 if (wdev->iftype != NL80211_IFTYPE_AP &&
6405 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6406 return -EOPNOTSUPP;
6407
6408 if (!info->attrs[NL80211_ATTR_MAC])
6409 return -EINVAL;
6410
6411 if (!rdev->ops->probe_client)
6412 return -EOPNOTSUPP;
6413
6414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6415 if (!msg)
6416 return -ENOMEM;
6417
6418 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6419 NL80211_CMD_PROBE_CLIENT);
6420
6421 if (IS_ERR(hdr)) {
6422 err = PTR_ERR(hdr);
6423 goto free_msg;
6424 }
6425
6426 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6427
6428 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6429 if (err)
6430 goto free_msg;
6431
David S. Miller9360ffd2012-03-29 04:41:26 -04006432 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6433 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006434
6435 genlmsg_end(msg, hdr);
6436
6437 return genlmsg_reply(msg, info);
6438
6439 nla_put_failure:
6440 err = -ENOBUFS;
6441 free_msg:
6442 nlmsg_free(msg);
6443 return err;
6444}
6445
Johannes Berg5e7602302011-11-04 11:18:17 +01006446static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6447{
6448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6449
6450 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6451 return -EOPNOTSUPP;
6452
6453 if (rdev->ap_beacons_nlpid)
6454 return -EBUSY;
6455
6456 rdev->ap_beacons_nlpid = info->snd_pid;
6457
6458 return 0;
6459}
6460
Johannes Berg4c476992010-10-04 21:36:35 +02006461#define NL80211_FLAG_NEED_WIPHY 0x01
6462#define NL80211_FLAG_NEED_NETDEV 0x02
6463#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006464#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6465#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6466 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006467
6468static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6469 struct genl_info *info)
6470{
6471 struct cfg80211_registered_device *rdev;
6472 struct net_device *dev;
6473 int err;
6474 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6475
6476 if (rtnl)
6477 rtnl_lock();
6478
6479 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6480 rdev = cfg80211_get_dev_from_info(info);
6481 if (IS_ERR(rdev)) {
6482 if (rtnl)
6483 rtnl_unlock();
6484 return PTR_ERR(rdev);
6485 }
6486 info->user_ptr[0] = rdev;
6487 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006488 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6489 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006490 if (err) {
6491 if (rtnl)
6492 rtnl_unlock();
6493 return err;
6494 }
Johannes Berg41265712010-10-04 21:14:05 +02006495 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6496 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006497 cfg80211_unlock_rdev(rdev);
6498 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006499 if (rtnl)
6500 rtnl_unlock();
6501 return -ENETDOWN;
6502 }
Johannes Berg4c476992010-10-04 21:36:35 +02006503 info->user_ptr[0] = rdev;
6504 info->user_ptr[1] = dev;
6505 }
6506
6507 return 0;
6508}
6509
6510static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6511 struct genl_info *info)
6512{
6513 if (info->user_ptr[0])
6514 cfg80211_unlock_rdev(info->user_ptr[0]);
6515 if (info->user_ptr[1])
6516 dev_put(info->user_ptr[1]);
6517 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6518 rtnl_unlock();
6519}
6520
Johannes Berg55682962007-09-20 13:09:35 -04006521static struct genl_ops nl80211_ops[] = {
6522 {
6523 .cmd = NL80211_CMD_GET_WIPHY,
6524 .doit = nl80211_get_wiphy,
6525 .dumpit = nl80211_dump_wiphy,
6526 .policy = nl80211_policy,
6527 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006528 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006529 },
6530 {
6531 .cmd = NL80211_CMD_SET_WIPHY,
6532 .doit = nl80211_set_wiphy,
6533 .policy = nl80211_policy,
6534 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006535 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006536 },
6537 {
6538 .cmd = NL80211_CMD_GET_INTERFACE,
6539 .doit = nl80211_get_interface,
6540 .dumpit = nl80211_dump_interface,
6541 .policy = nl80211_policy,
6542 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006543 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006544 },
6545 {
6546 .cmd = NL80211_CMD_SET_INTERFACE,
6547 .doit = nl80211_set_interface,
6548 .policy = nl80211_policy,
6549 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006550 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6551 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006552 },
6553 {
6554 .cmd = NL80211_CMD_NEW_INTERFACE,
6555 .doit = nl80211_new_interface,
6556 .policy = nl80211_policy,
6557 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006558 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6559 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006560 },
6561 {
6562 .cmd = NL80211_CMD_DEL_INTERFACE,
6563 .doit = nl80211_del_interface,
6564 .policy = nl80211_policy,
6565 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006566 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6567 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006568 },
Johannes Berg41ade002007-12-19 02:03:29 +01006569 {
6570 .cmd = NL80211_CMD_GET_KEY,
6571 .doit = nl80211_get_key,
6572 .policy = nl80211_policy,
6573 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006574 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006575 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006576 },
6577 {
6578 .cmd = NL80211_CMD_SET_KEY,
6579 .doit = nl80211_set_key,
6580 .policy = nl80211_policy,
6581 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006582 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006583 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006584 },
6585 {
6586 .cmd = NL80211_CMD_NEW_KEY,
6587 .doit = nl80211_new_key,
6588 .policy = nl80211_policy,
6589 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006590 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006591 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006592 },
6593 {
6594 .cmd = NL80211_CMD_DEL_KEY,
6595 .doit = nl80211_del_key,
6596 .policy = nl80211_policy,
6597 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006599 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006600 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006601 {
6602 .cmd = NL80211_CMD_SET_BEACON,
6603 .policy = nl80211_policy,
6604 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006605 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006607 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006608 },
6609 {
Johannes Berg88600202012-02-13 15:17:18 +01006610 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006611 .policy = nl80211_policy,
6612 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006613 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006614 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006615 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006616 },
6617 {
Johannes Berg88600202012-02-13 15:17:18 +01006618 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006619 .policy = nl80211_policy,
6620 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006621 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006622 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006623 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006624 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006625 {
6626 .cmd = NL80211_CMD_GET_STATION,
6627 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006628 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006629 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006630 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6631 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006632 },
6633 {
6634 .cmd = NL80211_CMD_SET_STATION,
6635 .doit = nl80211_set_station,
6636 .policy = nl80211_policy,
6637 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006638 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006639 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006640 },
6641 {
6642 .cmd = NL80211_CMD_NEW_STATION,
6643 .doit = nl80211_new_station,
6644 .policy = nl80211_policy,
6645 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006646 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006647 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006648 },
6649 {
6650 .cmd = NL80211_CMD_DEL_STATION,
6651 .doit = nl80211_del_station,
6652 .policy = nl80211_policy,
6653 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006655 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006656 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006657 {
6658 .cmd = NL80211_CMD_GET_MPATH,
6659 .doit = nl80211_get_mpath,
6660 .dumpit = nl80211_dump_mpath,
6661 .policy = nl80211_policy,
6662 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006663 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006664 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006665 },
6666 {
6667 .cmd = NL80211_CMD_SET_MPATH,
6668 .doit = nl80211_set_mpath,
6669 .policy = nl80211_policy,
6670 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006671 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006672 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006673 },
6674 {
6675 .cmd = NL80211_CMD_NEW_MPATH,
6676 .doit = nl80211_new_mpath,
6677 .policy = nl80211_policy,
6678 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006679 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006680 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006681 },
6682 {
6683 .cmd = NL80211_CMD_DEL_MPATH,
6684 .doit = nl80211_del_mpath,
6685 .policy = nl80211_policy,
6686 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006687 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006688 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006689 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006690 {
6691 .cmd = NL80211_CMD_SET_BSS,
6692 .doit = nl80211_set_bss,
6693 .policy = nl80211_policy,
6694 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006695 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006696 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006697 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006698 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006699 .cmd = NL80211_CMD_GET_REG,
6700 .doit = nl80211_get_reg,
6701 .policy = nl80211_policy,
6702 /* can be retrieved by unprivileged users */
6703 },
6704 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006705 .cmd = NL80211_CMD_SET_REG,
6706 .doit = nl80211_set_reg,
6707 .policy = nl80211_policy,
6708 .flags = GENL_ADMIN_PERM,
6709 },
6710 {
6711 .cmd = NL80211_CMD_REQ_SET_REG,
6712 .doit = nl80211_req_set_reg,
6713 .policy = nl80211_policy,
6714 .flags = GENL_ADMIN_PERM,
6715 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006716 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006717 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6718 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006719 .policy = nl80211_policy,
6720 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006721 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006722 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006723 },
6724 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006725 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6726 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006727 .policy = nl80211_policy,
6728 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006729 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006730 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006731 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006732 {
Johannes Berg2a519312009-02-10 21:25:55 +01006733 .cmd = NL80211_CMD_TRIGGER_SCAN,
6734 .doit = nl80211_trigger_scan,
6735 .policy = nl80211_policy,
6736 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006737 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006738 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006739 },
6740 {
6741 .cmd = NL80211_CMD_GET_SCAN,
6742 .policy = nl80211_policy,
6743 .dumpit = nl80211_dump_scan,
6744 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006745 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006746 .cmd = NL80211_CMD_START_SCHED_SCAN,
6747 .doit = nl80211_start_sched_scan,
6748 .policy = nl80211_policy,
6749 .flags = GENL_ADMIN_PERM,
6750 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6751 NL80211_FLAG_NEED_RTNL,
6752 },
6753 {
6754 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6755 .doit = nl80211_stop_sched_scan,
6756 .policy = nl80211_policy,
6757 .flags = GENL_ADMIN_PERM,
6758 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6759 NL80211_FLAG_NEED_RTNL,
6760 },
6761 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006762 .cmd = NL80211_CMD_AUTHENTICATE,
6763 .doit = nl80211_authenticate,
6764 .policy = nl80211_policy,
6765 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006766 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006767 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006768 },
6769 {
6770 .cmd = NL80211_CMD_ASSOCIATE,
6771 .doit = nl80211_associate,
6772 .policy = nl80211_policy,
6773 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006775 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006776 },
6777 {
6778 .cmd = NL80211_CMD_DEAUTHENTICATE,
6779 .doit = nl80211_deauthenticate,
6780 .policy = nl80211_policy,
6781 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006782 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006783 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006784 },
6785 {
6786 .cmd = NL80211_CMD_DISASSOCIATE,
6787 .doit = nl80211_disassociate,
6788 .policy = nl80211_policy,
6789 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006790 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006791 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006792 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006793 {
6794 .cmd = NL80211_CMD_JOIN_IBSS,
6795 .doit = nl80211_join_ibss,
6796 .policy = nl80211_policy,
6797 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006798 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006799 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006800 },
6801 {
6802 .cmd = NL80211_CMD_LEAVE_IBSS,
6803 .doit = nl80211_leave_ibss,
6804 .policy = nl80211_policy,
6805 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006806 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006807 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006808 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006809#ifdef CONFIG_NL80211_TESTMODE
6810 {
6811 .cmd = NL80211_CMD_TESTMODE,
6812 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006813 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006814 .policy = nl80211_policy,
6815 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006816 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6817 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006818 },
6819#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006820 {
6821 .cmd = NL80211_CMD_CONNECT,
6822 .doit = nl80211_connect,
6823 .policy = nl80211_policy,
6824 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006825 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006826 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006827 },
6828 {
6829 .cmd = NL80211_CMD_DISCONNECT,
6830 .doit = nl80211_disconnect,
6831 .policy = nl80211_policy,
6832 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006833 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006834 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006835 },
Johannes Berg463d0182009-07-14 00:33:35 +02006836 {
6837 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6838 .doit = nl80211_wiphy_netns,
6839 .policy = nl80211_policy,
6840 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006841 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6842 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006843 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006844 {
6845 .cmd = NL80211_CMD_GET_SURVEY,
6846 .policy = nl80211_policy,
6847 .dumpit = nl80211_dump_survey,
6848 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006849 {
6850 .cmd = NL80211_CMD_SET_PMKSA,
6851 .doit = nl80211_setdel_pmksa,
6852 .policy = nl80211_policy,
6853 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006854 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006855 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006856 },
6857 {
6858 .cmd = NL80211_CMD_DEL_PMKSA,
6859 .doit = nl80211_setdel_pmksa,
6860 .policy = nl80211_policy,
6861 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006862 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006863 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006864 },
6865 {
6866 .cmd = NL80211_CMD_FLUSH_PMKSA,
6867 .doit = nl80211_flush_pmksa,
6868 .policy = nl80211_policy,
6869 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006870 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006871 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006872 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006873 {
6874 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6875 .doit = nl80211_remain_on_channel,
6876 .policy = nl80211_policy,
6877 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006879 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006880 },
6881 {
6882 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6883 .doit = nl80211_cancel_remain_on_channel,
6884 .policy = nl80211_policy,
6885 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006886 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006887 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006888 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006889 {
6890 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6891 .doit = nl80211_set_tx_bitrate_mask,
6892 .policy = nl80211_policy,
6893 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006894 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6895 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006896 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006897 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006898 .cmd = NL80211_CMD_REGISTER_FRAME,
6899 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006900 .policy = nl80211_policy,
6901 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006902 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6903 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006904 },
6905 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006906 .cmd = NL80211_CMD_FRAME,
6907 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006908 .policy = nl80211_policy,
6909 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006910 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006911 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006912 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006913 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006914 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6915 .doit = nl80211_tx_mgmt_cancel_wait,
6916 .policy = nl80211_policy,
6917 .flags = GENL_ADMIN_PERM,
6918 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6919 NL80211_FLAG_NEED_RTNL,
6920 },
6921 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006922 .cmd = NL80211_CMD_SET_POWER_SAVE,
6923 .doit = nl80211_set_power_save,
6924 .policy = nl80211_policy,
6925 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006926 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6927 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006928 },
6929 {
6930 .cmd = NL80211_CMD_GET_POWER_SAVE,
6931 .doit = nl80211_get_power_save,
6932 .policy = nl80211_policy,
6933 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006934 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6935 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006936 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006937 {
6938 .cmd = NL80211_CMD_SET_CQM,
6939 .doit = nl80211_set_cqm,
6940 .policy = nl80211_policy,
6941 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006942 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6943 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006944 },
Johannes Bergf444de02010-05-05 15:25:02 +02006945 {
6946 .cmd = NL80211_CMD_SET_CHANNEL,
6947 .doit = nl80211_set_channel,
6948 .policy = nl80211_policy,
6949 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006950 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6951 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006952 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006953 {
6954 .cmd = NL80211_CMD_SET_WDS_PEER,
6955 .doit = nl80211_set_wds_peer,
6956 .policy = nl80211_policy,
6957 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006958 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6959 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006960 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006961 {
6962 .cmd = NL80211_CMD_JOIN_MESH,
6963 .doit = nl80211_join_mesh,
6964 .policy = nl80211_policy,
6965 .flags = GENL_ADMIN_PERM,
6966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6967 NL80211_FLAG_NEED_RTNL,
6968 },
6969 {
6970 .cmd = NL80211_CMD_LEAVE_MESH,
6971 .doit = nl80211_leave_mesh,
6972 .policy = nl80211_policy,
6973 .flags = GENL_ADMIN_PERM,
6974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6975 NL80211_FLAG_NEED_RTNL,
6976 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006977 {
6978 .cmd = NL80211_CMD_GET_WOWLAN,
6979 .doit = nl80211_get_wowlan,
6980 .policy = nl80211_policy,
6981 /* can be retrieved by unprivileged users */
6982 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6983 NL80211_FLAG_NEED_RTNL,
6984 },
6985 {
6986 .cmd = NL80211_CMD_SET_WOWLAN,
6987 .doit = nl80211_set_wowlan,
6988 .policy = nl80211_policy,
6989 .flags = GENL_ADMIN_PERM,
6990 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6991 NL80211_FLAG_NEED_RTNL,
6992 },
Johannes Berge5497d72011-07-05 16:35:40 +02006993 {
6994 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6995 .doit = nl80211_set_rekey_data,
6996 .policy = nl80211_policy,
6997 .flags = GENL_ADMIN_PERM,
6998 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6999 NL80211_FLAG_NEED_RTNL,
7000 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007001 {
7002 .cmd = NL80211_CMD_TDLS_MGMT,
7003 .doit = nl80211_tdls_mgmt,
7004 .policy = nl80211_policy,
7005 .flags = GENL_ADMIN_PERM,
7006 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7007 NL80211_FLAG_NEED_RTNL,
7008 },
7009 {
7010 .cmd = NL80211_CMD_TDLS_OPER,
7011 .doit = nl80211_tdls_oper,
7012 .policy = nl80211_policy,
7013 .flags = GENL_ADMIN_PERM,
7014 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7015 NL80211_FLAG_NEED_RTNL,
7016 },
Johannes Berg28946da2011-11-04 11:18:12 +01007017 {
7018 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7019 .doit = nl80211_register_unexpected_frame,
7020 .policy = nl80211_policy,
7021 .flags = GENL_ADMIN_PERM,
7022 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7023 NL80211_FLAG_NEED_RTNL,
7024 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007025 {
7026 .cmd = NL80211_CMD_PROBE_CLIENT,
7027 .doit = nl80211_probe_client,
7028 .policy = nl80211_policy,
7029 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007030 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007031 NL80211_FLAG_NEED_RTNL,
7032 },
Johannes Berg5e7602302011-11-04 11:18:17 +01007033 {
7034 .cmd = NL80211_CMD_REGISTER_BEACONS,
7035 .doit = nl80211_register_beacons,
7036 .policy = nl80211_policy,
7037 .flags = GENL_ADMIN_PERM,
7038 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7039 NL80211_FLAG_NEED_RTNL,
7040 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007041 {
7042 .cmd = NL80211_CMD_SET_NOACK_MAP,
7043 .doit = nl80211_set_noack_map,
7044 .policy = nl80211_policy,
7045 .flags = GENL_ADMIN_PERM,
7046 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7047 NL80211_FLAG_NEED_RTNL,
7048 },
7049
Johannes Berg55682962007-09-20 13:09:35 -04007050};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007051
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007052static struct genl_multicast_group nl80211_mlme_mcgrp = {
7053 .name = "mlme",
7054};
Johannes Berg55682962007-09-20 13:09:35 -04007055
7056/* multicast groups */
7057static struct genl_multicast_group nl80211_config_mcgrp = {
7058 .name = "config",
7059};
Johannes Berg2a519312009-02-10 21:25:55 +01007060static struct genl_multicast_group nl80211_scan_mcgrp = {
7061 .name = "scan",
7062};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007063static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7064 .name = "regulatory",
7065};
Johannes Berg55682962007-09-20 13:09:35 -04007066
7067/* notification functions */
7068
7069void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7070{
7071 struct sk_buff *msg;
7072
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007073 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007074 if (!msg)
7075 return;
7076
7077 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7078 nlmsg_free(msg);
7079 return;
7080 }
7081
Johannes Berg463d0182009-07-14 00:33:35 +02007082 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7083 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007084}
7085
Johannes Berg362a4152009-05-24 16:43:15 +02007086static int nl80211_add_scan_req(struct sk_buff *msg,
7087 struct cfg80211_registered_device *rdev)
7088{
7089 struct cfg80211_scan_request *req = rdev->scan_req;
7090 struct nlattr *nest;
7091 int i;
7092
Johannes Berg667503dd2009-07-07 03:56:11 +02007093 ASSERT_RDEV_LOCK(rdev);
7094
Johannes Berg362a4152009-05-24 16:43:15 +02007095 if (WARN_ON(!req))
7096 return 0;
7097
7098 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7099 if (!nest)
7100 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007101 for (i = 0; i < req->n_ssids; i++) {
7102 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7103 goto nla_put_failure;
7104 }
Johannes Berg362a4152009-05-24 16:43:15 +02007105 nla_nest_end(msg, nest);
7106
7107 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7108 if (!nest)
7109 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007110 for (i = 0; i < req->n_channels; i++) {
7111 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7112 goto nla_put_failure;
7113 }
Johannes Berg362a4152009-05-24 16:43:15 +02007114 nla_nest_end(msg, nest);
7115
David S. Miller9360ffd2012-03-29 04:41:26 -04007116 if (req->ie &&
7117 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7118 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007119
7120 return 0;
7121 nla_put_failure:
7122 return -ENOBUFS;
7123}
7124
Johannes Berga538e2d2009-06-16 19:56:42 +02007125static int nl80211_send_scan_msg(struct sk_buff *msg,
7126 struct cfg80211_registered_device *rdev,
7127 struct net_device *netdev,
7128 u32 pid, u32 seq, int flags,
7129 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007130{
7131 void *hdr;
7132
7133 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7134 if (!hdr)
7135 return -1;
7136
David S. Miller9360ffd2012-03-29 04:41:26 -04007137 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7138 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7139 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007140
Johannes Berg362a4152009-05-24 16:43:15 +02007141 /* ignore errors and send incomplete event anyway */
7142 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007143
7144 return genlmsg_end(msg, hdr);
7145
7146 nla_put_failure:
7147 genlmsg_cancel(msg, hdr);
7148 return -EMSGSIZE;
7149}
7150
Luciano Coelho807f8a82011-05-11 17:09:35 +03007151static int
7152nl80211_send_sched_scan_msg(struct sk_buff *msg,
7153 struct cfg80211_registered_device *rdev,
7154 struct net_device *netdev,
7155 u32 pid, u32 seq, int flags, u32 cmd)
7156{
7157 void *hdr;
7158
7159 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7160 if (!hdr)
7161 return -1;
7162
David S. Miller9360ffd2012-03-29 04:41:26 -04007163 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7164 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7165 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007166
7167 return genlmsg_end(msg, hdr);
7168
7169 nla_put_failure:
7170 genlmsg_cancel(msg, hdr);
7171 return -EMSGSIZE;
7172}
7173
Johannes Berga538e2d2009-06-16 19:56:42 +02007174void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7175 struct net_device *netdev)
7176{
7177 struct sk_buff *msg;
7178
7179 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7180 if (!msg)
7181 return;
7182
7183 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7184 NL80211_CMD_TRIGGER_SCAN) < 0) {
7185 nlmsg_free(msg);
7186 return;
7187 }
7188
Johannes Berg463d0182009-07-14 00:33:35 +02007189 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7190 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007191}
7192
Johannes Berg2a519312009-02-10 21:25:55 +01007193void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7194 struct net_device *netdev)
7195{
7196 struct sk_buff *msg;
7197
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007198 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007199 if (!msg)
7200 return;
7201
Johannes Berga538e2d2009-06-16 19:56:42 +02007202 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7203 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007204 nlmsg_free(msg);
7205 return;
7206 }
7207
Johannes Berg463d0182009-07-14 00:33:35 +02007208 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7209 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007210}
7211
7212void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7213 struct net_device *netdev)
7214{
7215 struct sk_buff *msg;
7216
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007217 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007218 if (!msg)
7219 return;
7220
Johannes Berga538e2d2009-06-16 19:56:42 +02007221 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7222 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007223 nlmsg_free(msg);
7224 return;
7225 }
7226
Johannes Berg463d0182009-07-14 00:33:35 +02007227 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7228 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007229}
7230
Luciano Coelho807f8a82011-05-11 17:09:35 +03007231void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7232 struct net_device *netdev)
7233{
7234 struct sk_buff *msg;
7235
7236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7237 if (!msg)
7238 return;
7239
7240 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7241 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7242 nlmsg_free(msg);
7243 return;
7244 }
7245
7246 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7247 nl80211_scan_mcgrp.id, GFP_KERNEL);
7248}
7249
7250void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7251 struct net_device *netdev, u32 cmd)
7252{
7253 struct sk_buff *msg;
7254
7255 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7256 if (!msg)
7257 return;
7258
7259 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7260 nlmsg_free(msg);
7261 return;
7262 }
7263
7264 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7265 nl80211_scan_mcgrp.id, GFP_KERNEL);
7266}
7267
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007268/*
7269 * This can happen on global regulatory changes or device specific settings
7270 * based on custom world regulatory domains.
7271 */
7272void nl80211_send_reg_change_event(struct regulatory_request *request)
7273{
7274 struct sk_buff *msg;
7275 void *hdr;
7276
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007277 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007278 if (!msg)
7279 return;
7280
7281 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7282 if (!hdr) {
7283 nlmsg_free(msg);
7284 return;
7285 }
7286
7287 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007288 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7289 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007290
David S. Miller9360ffd2012-03-29 04:41:26 -04007291 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7292 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7293 NL80211_REGDOM_TYPE_WORLD))
7294 goto nla_put_failure;
7295 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7296 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7297 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7298 goto nla_put_failure;
7299 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7300 request->intersect) {
7301 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7302 NL80211_REGDOM_TYPE_INTERSECTION))
7303 goto nla_put_failure;
7304 } else {
7305 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7306 NL80211_REGDOM_TYPE_COUNTRY) ||
7307 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7308 request->alpha2))
7309 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007310 }
7311
David S. Miller9360ffd2012-03-29 04:41:26 -04007312 if (wiphy_idx_valid(request->wiphy_idx) &&
7313 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7314 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007315
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007316 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007317
Johannes Bergbc43b282009-07-25 10:54:13 +02007318 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007319 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007320 GFP_ATOMIC);
7321 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007322
7323 return;
7324
7325nla_put_failure:
7326 genlmsg_cancel(msg, hdr);
7327 nlmsg_free(msg);
7328}
7329
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007330static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7331 struct net_device *netdev,
7332 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007333 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007334{
7335 struct sk_buff *msg;
7336 void *hdr;
7337
Johannes Berge6d6e342009-07-01 21:26:47 +02007338 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007339 if (!msg)
7340 return;
7341
7342 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7343 if (!hdr) {
7344 nlmsg_free(msg);
7345 return;
7346 }
7347
David S. Miller9360ffd2012-03-29 04:41:26 -04007348 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7349 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7350 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7351 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007352
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007353 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007354
Johannes Berg463d0182009-07-14 00:33:35 +02007355 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7356 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007357 return;
7358
7359 nla_put_failure:
7360 genlmsg_cancel(msg, hdr);
7361 nlmsg_free(msg);
7362}
7363
7364void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007365 struct net_device *netdev, const u8 *buf,
7366 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007367{
7368 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007369 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007370}
7371
7372void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7373 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007374 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007375{
Johannes Berge6d6e342009-07-01 21:26:47 +02007376 nl80211_send_mlme_event(rdev, netdev, buf, len,
7377 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007378}
7379
Jouni Malinen53b46b82009-03-27 20:53:56 +02007380void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007381 struct net_device *netdev, const u8 *buf,
7382 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007383{
7384 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007385 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007386}
7387
Jouni Malinen53b46b82009-03-27 20:53:56 +02007388void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7389 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007390 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007391{
7392 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007393 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007394}
7395
Jouni Malinencf4e5942010-12-16 00:52:40 +02007396void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7397 struct net_device *netdev, const u8 *buf,
7398 size_t len, gfp_t gfp)
7399{
7400 nl80211_send_mlme_event(rdev, netdev, buf, len,
7401 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7402}
7403
7404void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7405 struct net_device *netdev, const u8 *buf,
7406 size_t len, gfp_t gfp)
7407{
7408 nl80211_send_mlme_event(rdev, netdev, buf, len,
7409 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7410}
7411
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007412static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7413 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007414 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007415{
7416 struct sk_buff *msg;
7417 void *hdr;
7418
Johannes Berge6d6e342009-07-01 21:26:47 +02007419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007420 if (!msg)
7421 return;
7422
7423 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7424 if (!hdr) {
7425 nlmsg_free(msg);
7426 return;
7427 }
7428
David S. Miller9360ffd2012-03-29 04:41:26 -04007429 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7430 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7431 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7432 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7433 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007434
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007435 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007436
Johannes Berg463d0182009-07-14 00:33:35 +02007437 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7438 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007439 return;
7440
7441 nla_put_failure:
7442 genlmsg_cancel(msg, hdr);
7443 nlmsg_free(msg);
7444}
7445
7446void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007447 struct net_device *netdev, const u8 *addr,
7448 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007449{
7450 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007451 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007452}
7453
7454void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007455 struct net_device *netdev, const u8 *addr,
7456 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007457{
Johannes Berge6d6e342009-07-01 21:26:47 +02007458 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7459 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007460}
7461
Samuel Ortizb23aa672009-07-01 21:26:54 +02007462void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7463 struct net_device *netdev, const u8 *bssid,
7464 const u8 *req_ie, size_t req_ie_len,
7465 const u8 *resp_ie, size_t resp_ie_len,
7466 u16 status, gfp_t gfp)
7467{
7468 struct sk_buff *msg;
7469 void *hdr;
7470
7471 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7472 if (!msg)
7473 return;
7474
7475 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7476 if (!hdr) {
7477 nlmsg_free(msg);
7478 return;
7479 }
7480
David S. Miller9360ffd2012-03-29 04:41:26 -04007481 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7482 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7483 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7484 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7485 (req_ie &&
7486 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7487 (resp_ie &&
7488 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7489 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007490
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007491 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007492
Johannes Berg463d0182009-07-14 00:33:35 +02007493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7494 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007495 return;
7496
7497 nla_put_failure:
7498 genlmsg_cancel(msg, hdr);
7499 nlmsg_free(msg);
7500
7501}
7502
7503void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7504 struct net_device *netdev, const u8 *bssid,
7505 const u8 *req_ie, size_t req_ie_len,
7506 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7507{
7508 struct sk_buff *msg;
7509 void *hdr;
7510
7511 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7512 if (!msg)
7513 return;
7514
7515 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7516 if (!hdr) {
7517 nlmsg_free(msg);
7518 return;
7519 }
7520
David S. Miller9360ffd2012-03-29 04:41:26 -04007521 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7522 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7523 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7524 (req_ie &&
7525 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7526 (resp_ie &&
7527 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7528 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007529
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007530 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007531
Johannes Berg463d0182009-07-14 00:33:35 +02007532 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7533 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007534 return;
7535
7536 nla_put_failure:
7537 genlmsg_cancel(msg, hdr);
7538 nlmsg_free(msg);
7539
7540}
7541
7542void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7543 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007544 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007545{
7546 struct sk_buff *msg;
7547 void *hdr;
7548
Johannes Berg667503dd2009-07-07 03:56:11 +02007549 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007550 if (!msg)
7551 return;
7552
7553 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7554 if (!hdr) {
7555 nlmsg_free(msg);
7556 return;
7557 }
7558
David S. Miller9360ffd2012-03-29 04:41:26 -04007559 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7560 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7561 (from_ap && reason &&
7562 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7563 (from_ap &&
7564 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7565 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7566 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007567
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007568 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007569
Johannes Berg463d0182009-07-14 00:33:35 +02007570 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7571 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007572 return;
7573
7574 nla_put_failure:
7575 genlmsg_cancel(msg, hdr);
7576 nlmsg_free(msg);
7577
7578}
7579
Johannes Berg04a773a2009-04-19 21:24:32 +02007580void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7581 struct net_device *netdev, const u8 *bssid,
7582 gfp_t gfp)
7583{
7584 struct sk_buff *msg;
7585 void *hdr;
7586
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007587 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007588 if (!msg)
7589 return;
7590
7591 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7592 if (!hdr) {
7593 nlmsg_free(msg);
7594 return;
7595 }
7596
David S. Miller9360ffd2012-03-29 04:41:26 -04007597 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7598 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7599 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7600 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007601
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007602 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007603
Johannes Berg463d0182009-07-14 00:33:35 +02007604 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7605 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007606 return;
7607
7608 nla_put_failure:
7609 genlmsg_cancel(msg, hdr);
7610 nlmsg_free(msg);
7611}
7612
Javier Cardonac93b5e72011-04-07 15:08:34 -07007613void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7614 struct net_device *netdev,
7615 const u8 *macaddr, const u8* ie, u8 ie_len,
7616 gfp_t gfp)
7617{
7618 struct sk_buff *msg;
7619 void *hdr;
7620
7621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7622 if (!msg)
7623 return;
7624
7625 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7626 if (!hdr) {
7627 nlmsg_free(msg);
7628 return;
7629 }
7630
David S. Miller9360ffd2012-03-29 04:41:26 -04007631 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7632 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7633 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7634 (ie_len && ie &&
7635 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7636 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007637
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007638 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007639
7640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7641 nl80211_mlme_mcgrp.id, gfp);
7642 return;
7643
7644 nla_put_failure:
7645 genlmsg_cancel(msg, hdr);
7646 nlmsg_free(msg);
7647}
7648
Jouni Malinena3b8b052009-03-27 21:59:49 +02007649void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7650 struct net_device *netdev, const u8 *addr,
7651 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007652 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007653{
7654 struct sk_buff *msg;
7655 void *hdr;
7656
Johannes Berge6d6e342009-07-01 21:26:47 +02007657 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007658 if (!msg)
7659 return;
7660
7661 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7662 if (!hdr) {
7663 nlmsg_free(msg);
7664 return;
7665 }
7666
David S. Miller9360ffd2012-03-29 04:41:26 -04007667 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7668 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7669 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7670 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7671 (key_id != -1 &&
7672 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7673 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7674 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007675
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007676 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007677
Johannes Berg463d0182009-07-14 00:33:35 +02007678 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7679 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007680 return;
7681
7682 nla_put_failure:
7683 genlmsg_cancel(msg, hdr);
7684 nlmsg_free(msg);
7685}
7686
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007687void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7688 struct ieee80211_channel *channel_before,
7689 struct ieee80211_channel *channel_after)
7690{
7691 struct sk_buff *msg;
7692 void *hdr;
7693 struct nlattr *nl_freq;
7694
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007695 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007696 if (!msg)
7697 return;
7698
7699 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7700 if (!hdr) {
7701 nlmsg_free(msg);
7702 return;
7703 }
7704
7705 /*
7706 * Since we are applying the beacon hint to a wiphy we know its
7707 * wiphy_idx is valid
7708 */
David S. Miller9360ffd2012-03-29 04:41:26 -04007709 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7710 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007711
7712 /* Before */
7713 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7714 if (!nl_freq)
7715 goto nla_put_failure;
7716 if (nl80211_msg_put_channel(msg, channel_before))
7717 goto nla_put_failure;
7718 nla_nest_end(msg, nl_freq);
7719
7720 /* After */
7721 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7722 if (!nl_freq)
7723 goto nla_put_failure;
7724 if (nl80211_msg_put_channel(msg, channel_after))
7725 goto nla_put_failure;
7726 nla_nest_end(msg, nl_freq);
7727
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007728 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007729
Johannes Berg463d0182009-07-14 00:33:35 +02007730 rcu_read_lock();
7731 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7732 GFP_ATOMIC);
7733 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007734
7735 return;
7736
7737nla_put_failure:
7738 genlmsg_cancel(msg, hdr);
7739 nlmsg_free(msg);
7740}
7741
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007742static void nl80211_send_remain_on_chan_event(
7743 int cmd, struct cfg80211_registered_device *rdev,
7744 struct net_device *netdev, u64 cookie,
7745 struct ieee80211_channel *chan,
7746 enum nl80211_channel_type channel_type,
7747 unsigned int duration, gfp_t gfp)
7748{
7749 struct sk_buff *msg;
7750 void *hdr;
7751
7752 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7753 if (!msg)
7754 return;
7755
7756 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7757 if (!hdr) {
7758 nlmsg_free(msg);
7759 return;
7760 }
7761
David S. Miller9360ffd2012-03-29 04:41:26 -04007762 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7763 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7764 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
7765 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
7766 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7767 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007768
David S. Miller9360ffd2012-03-29 04:41:26 -04007769 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
7770 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
7771 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007772
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007773 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007774
7775 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7776 nl80211_mlme_mcgrp.id, gfp);
7777 return;
7778
7779 nla_put_failure:
7780 genlmsg_cancel(msg, hdr);
7781 nlmsg_free(msg);
7782}
7783
7784void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7785 struct net_device *netdev, u64 cookie,
7786 struct ieee80211_channel *chan,
7787 enum nl80211_channel_type channel_type,
7788 unsigned int duration, gfp_t gfp)
7789{
7790 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7791 rdev, netdev, cookie, chan,
7792 channel_type, duration, gfp);
7793}
7794
7795void nl80211_send_remain_on_channel_cancel(
7796 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7797 u64 cookie, struct ieee80211_channel *chan,
7798 enum nl80211_channel_type channel_type, gfp_t gfp)
7799{
7800 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7801 rdev, netdev, cookie, chan,
7802 channel_type, 0, gfp);
7803}
7804
Johannes Berg98b62182009-12-23 13:15:44 +01007805void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7806 struct net_device *dev, const u8 *mac_addr,
7807 struct station_info *sinfo, gfp_t gfp)
7808{
7809 struct sk_buff *msg;
7810
7811 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7812 if (!msg)
7813 return;
7814
John W. Linville66266b32012-03-15 13:25:41 -04007815 if (nl80211_send_station(msg, 0, 0, 0,
7816 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007817 nlmsg_free(msg);
7818 return;
7819 }
7820
7821 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7822 nl80211_mlme_mcgrp.id, gfp);
7823}
7824
Jouni Malinenec15e682011-03-23 15:29:52 +02007825void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7826 struct net_device *dev, const u8 *mac_addr,
7827 gfp_t gfp)
7828{
7829 struct sk_buff *msg;
7830 void *hdr;
7831
7832 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7833 if (!msg)
7834 return;
7835
7836 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7837 if (!hdr) {
7838 nlmsg_free(msg);
7839 return;
7840 }
7841
David S. Miller9360ffd2012-03-29 04:41:26 -04007842 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7843 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
7844 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02007845
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007846 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007847
7848 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7849 nl80211_mlme_mcgrp.id, gfp);
7850 return;
7851
7852 nla_put_failure:
7853 genlmsg_cancel(msg, hdr);
7854 nlmsg_free(msg);
7855}
7856
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007857static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7858 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007859{
7860 struct wireless_dev *wdev = dev->ieee80211_ptr;
7861 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7862 struct sk_buff *msg;
7863 void *hdr;
7864 int err;
7865 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7866
7867 if (!nlpid)
7868 return false;
7869
7870 msg = nlmsg_new(100, gfp);
7871 if (!msg)
7872 return true;
7873
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007874 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007875 if (!hdr) {
7876 nlmsg_free(msg);
7877 return true;
7878 }
7879
David S. Miller9360ffd2012-03-29 04:41:26 -04007880 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7881 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7882 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7883 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01007884
7885 err = genlmsg_end(msg, hdr);
7886 if (err < 0) {
7887 nlmsg_free(msg);
7888 return true;
7889 }
7890
7891 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7892 return true;
7893
7894 nla_put_failure:
7895 genlmsg_cancel(msg, hdr);
7896 nlmsg_free(msg);
7897 return true;
7898}
7899
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007900bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7901{
7902 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7903 addr, gfp);
7904}
7905
7906bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7907 const u8 *addr, gfp_t gfp)
7908{
7909 return __nl80211_unexpected_frame(dev,
7910 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7911 addr, gfp);
7912}
7913
Johannes Berg2e161f72010-08-12 15:38:38 +02007914int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7915 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007916 int freq, int sig_dbm,
7917 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007918{
7919 struct sk_buff *msg;
7920 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007921
7922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7923 if (!msg)
7924 return -ENOMEM;
7925
Johannes Berg2e161f72010-08-12 15:38:38 +02007926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007927 if (!hdr) {
7928 nlmsg_free(msg);
7929 return -ENOMEM;
7930 }
7931
David S. Miller9360ffd2012-03-29 04:41:26 -04007932 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7933 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7934 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7935 (sig_dbm &&
7936 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
7937 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7938 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007939
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007940 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007941
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007942 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007943
7944 nla_put_failure:
7945 genlmsg_cancel(msg, hdr);
7946 nlmsg_free(msg);
7947 return -ENOBUFS;
7948}
7949
Johannes Berg2e161f72010-08-12 15:38:38 +02007950void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7951 struct net_device *netdev, u64 cookie,
7952 const u8 *buf, size_t len, bool ack,
7953 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007954{
7955 struct sk_buff *msg;
7956 void *hdr;
7957
7958 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7959 if (!msg)
7960 return;
7961
Johannes Berg2e161f72010-08-12 15:38:38 +02007962 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007963 if (!hdr) {
7964 nlmsg_free(msg);
7965 return;
7966 }
7967
David S. Miller9360ffd2012-03-29 04:41:26 -04007968 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7969 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7970 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
7971 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
7972 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
7973 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007974
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007975 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007976
7977 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7978 return;
7979
7980 nla_put_failure:
7981 genlmsg_cancel(msg, hdr);
7982 nlmsg_free(msg);
7983}
7984
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007985void
7986nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7987 struct net_device *netdev,
7988 enum nl80211_cqm_rssi_threshold_event rssi_event,
7989 gfp_t gfp)
7990{
7991 struct sk_buff *msg;
7992 struct nlattr *pinfoattr;
7993 void *hdr;
7994
7995 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7996 if (!msg)
7997 return;
7998
7999 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8000 if (!hdr) {
8001 nlmsg_free(msg);
8002 return;
8003 }
8004
David S. Miller9360ffd2012-03-29 04:41:26 -04008005 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8006 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8007 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008008
8009 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8010 if (!pinfoattr)
8011 goto nla_put_failure;
8012
David S. Miller9360ffd2012-03-29 04:41:26 -04008013 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8014 rssi_event))
8015 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008016
8017 nla_nest_end(msg, pinfoattr);
8018
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008019 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008020
8021 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8022 nl80211_mlme_mcgrp.id, gfp);
8023 return;
8024
8025 nla_put_failure:
8026 genlmsg_cancel(msg, hdr);
8027 nlmsg_free(msg);
8028}
8029
Johannes Berge5497d72011-07-05 16:35:40 +02008030void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8031 struct net_device *netdev, const u8 *bssid,
8032 const u8 *replay_ctr, gfp_t gfp)
8033{
8034 struct sk_buff *msg;
8035 struct nlattr *rekey_attr;
8036 void *hdr;
8037
8038 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8039 if (!msg)
8040 return;
8041
8042 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8043 if (!hdr) {
8044 nlmsg_free(msg);
8045 return;
8046 }
8047
David S. Miller9360ffd2012-03-29 04:41:26 -04008048 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8049 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8050 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8051 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008052
8053 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8054 if (!rekey_attr)
8055 goto nla_put_failure;
8056
David S. Miller9360ffd2012-03-29 04:41:26 -04008057 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8058 NL80211_REPLAY_CTR_LEN, replay_ctr))
8059 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008060
8061 nla_nest_end(msg, rekey_attr);
8062
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008063 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008064
8065 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8066 nl80211_mlme_mcgrp.id, gfp);
8067 return;
8068
8069 nla_put_failure:
8070 genlmsg_cancel(msg, hdr);
8071 nlmsg_free(msg);
8072}
8073
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008074void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8075 struct net_device *netdev, int index,
8076 const u8 *bssid, bool preauth, gfp_t gfp)
8077{
8078 struct sk_buff *msg;
8079 struct nlattr *attr;
8080 void *hdr;
8081
8082 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8083 if (!msg)
8084 return;
8085
8086 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8087 if (!hdr) {
8088 nlmsg_free(msg);
8089 return;
8090 }
8091
David S. Miller9360ffd2012-03-29 04:41:26 -04008092 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8093 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8094 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008095
8096 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8097 if (!attr)
8098 goto nla_put_failure;
8099
David S. Miller9360ffd2012-03-29 04:41:26 -04008100 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8101 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8102 (preauth &&
8103 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8104 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008105
8106 nla_nest_end(msg, attr);
8107
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008108 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008109
8110 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8111 nl80211_mlme_mcgrp.id, gfp);
8112 return;
8113
8114 nla_put_failure:
8115 genlmsg_cancel(msg, hdr);
8116 nlmsg_free(msg);
8117}
8118
Thomas Pedersen53145262012-04-06 13:35:47 -07008119void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8120 struct net_device *netdev, int freq,
8121 enum nl80211_channel_type type, gfp_t gfp)
8122{
8123 struct sk_buff *msg;
8124 void *hdr;
8125
8126 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8127 if (!msg)
8128 return;
8129
8130 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8131 if (!hdr) {
8132 nlmsg_free(msg);
8133 return;
8134 }
8135
John W. Linville7eab0f62012-04-12 14:25:14 -04008136 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8137 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8138 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8139 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008140
8141 genlmsg_end(msg, hdr);
8142
8143 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8144 nl80211_mlme_mcgrp.id, gfp);
8145 return;
8146
8147 nla_put_failure:
8148 genlmsg_cancel(msg, hdr);
8149 nlmsg_free(msg);
8150}
8151
Johannes Bergc063dbf2010-11-24 08:10:05 +01008152void
8153nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8154 struct net_device *netdev, const u8 *peer,
8155 u32 num_packets, gfp_t gfp)
8156{
8157 struct sk_buff *msg;
8158 struct nlattr *pinfoattr;
8159 void *hdr;
8160
8161 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8162 if (!msg)
8163 return;
8164
8165 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8166 if (!hdr) {
8167 nlmsg_free(msg);
8168 return;
8169 }
8170
David S. Miller9360ffd2012-03-29 04:41:26 -04008171 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8172 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8173 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8174 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008175
8176 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8177 if (!pinfoattr)
8178 goto nla_put_failure;
8179
David S. Miller9360ffd2012-03-29 04:41:26 -04008180 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8181 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008182
8183 nla_nest_end(msg, pinfoattr);
8184
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008185 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008186
8187 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8188 nl80211_mlme_mcgrp.id, gfp);
8189 return;
8190
8191 nla_put_failure:
8192 genlmsg_cancel(msg, hdr);
8193 nlmsg_free(msg);
8194}
8195
Johannes Berg7f6cf312011-11-04 11:18:15 +01008196void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8197 u64 cookie, bool acked, gfp_t gfp)
8198{
8199 struct wireless_dev *wdev = dev->ieee80211_ptr;
8200 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8201 struct sk_buff *msg;
8202 void *hdr;
8203 int err;
8204
8205 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8206 if (!msg)
8207 return;
8208
8209 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8210 if (!hdr) {
8211 nlmsg_free(msg);
8212 return;
8213 }
8214
David S. Miller9360ffd2012-03-29 04:41:26 -04008215 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8216 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8217 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8218 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8219 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8220 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008221
8222 err = genlmsg_end(msg, hdr);
8223 if (err < 0) {
8224 nlmsg_free(msg);
8225 return;
8226 }
8227
8228 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8229 nl80211_mlme_mcgrp.id, gfp);
8230 return;
8231
8232 nla_put_failure:
8233 genlmsg_cancel(msg, hdr);
8234 nlmsg_free(msg);
8235}
8236EXPORT_SYMBOL(cfg80211_probe_status);
8237
Johannes Berg5e7602302011-11-04 11:18:17 +01008238void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8239 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008240 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e7602302011-11-04 11:18:17 +01008241{
8242 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8243 struct sk_buff *msg;
8244 void *hdr;
8245 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8246
8247 if (!nlpid)
8248 return;
8249
8250 msg = nlmsg_new(len + 100, gfp);
8251 if (!msg)
8252 return;
8253
8254 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8255 if (!hdr) {
8256 nlmsg_free(msg);
8257 return;
8258 }
8259
David S. Miller9360ffd2012-03-29 04:41:26 -04008260 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8261 (freq &&
8262 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8263 (sig_dbm &&
8264 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8265 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8266 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01008267
8268 genlmsg_end(msg, hdr);
8269
8270 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8271 return;
8272
8273 nla_put_failure:
8274 genlmsg_cancel(msg, hdr);
8275 nlmsg_free(msg);
8276}
8277EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8278
Jouni Malinen026331c2010-02-15 12:53:10 +02008279static int nl80211_netlink_notify(struct notifier_block * nb,
8280 unsigned long state,
8281 void *_notify)
8282{
8283 struct netlink_notify *notify = _notify;
8284 struct cfg80211_registered_device *rdev;
8285 struct wireless_dev *wdev;
8286
8287 if (state != NETLINK_URELEASE)
8288 return NOTIFY_DONE;
8289
8290 rcu_read_lock();
8291
Johannes Berg5e7602302011-11-04 11:18:17 +01008292 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008293 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008294 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e7602302011-11-04 11:18:17 +01008295 if (rdev->ap_beacons_nlpid == notify->pid)
8296 rdev->ap_beacons_nlpid = 0;
8297 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008298
8299 rcu_read_unlock();
8300
8301 return NOTIFY_DONE;
8302}
8303
8304static struct notifier_block nl80211_netlink_notifier = {
8305 .notifier_call = nl80211_netlink_notify,
8306};
8307
Johannes Berg55682962007-09-20 13:09:35 -04008308/* initialisation/exit functions */
8309
8310int nl80211_init(void)
8311{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008312 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008313
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008314 err = genl_register_family_with_ops(&nl80211_fam,
8315 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008316 if (err)
8317 return err;
8318
Johannes Berg55682962007-09-20 13:09:35 -04008319 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8320 if (err)
8321 goto err_out;
8322
Johannes Berg2a519312009-02-10 21:25:55 +01008323 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8324 if (err)
8325 goto err_out;
8326
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008327 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8328 if (err)
8329 goto err_out;
8330
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008331 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8332 if (err)
8333 goto err_out;
8334
Johannes Bergaff89a92009-07-01 21:26:51 +02008335#ifdef CONFIG_NL80211_TESTMODE
8336 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8337 if (err)
8338 goto err_out;
8339#endif
8340
Jouni Malinen026331c2010-02-15 12:53:10 +02008341 err = netlink_register_notifier(&nl80211_netlink_notifier);
8342 if (err)
8343 goto err_out;
8344
Johannes Berg55682962007-09-20 13:09:35 -04008345 return 0;
8346 err_out:
8347 genl_unregister_family(&nl80211_fam);
8348 return err;
8349}
8350
8351void nl80211_exit(void)
8352{
Jouni Malinen026331c2010-02-15 12:53:10 +02008353 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008354 genl_unregister_family(&nl80211_fam);
8355}