blob: 25a470abd21d63d75b5832895f7f6cf45b1282fa [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400207};
208
Johannes Berge31b8212010-10-05 19:39:30 +0200209/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000210static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200211 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200212 [NL80211_KEY_IDX] = { .type = NLA_U8 },
213 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200214 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200215 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
216 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200217 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100218 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
219};
220
221/* policy for the key default flags */
222static const struct nla_policy
223nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
224 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
225 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200226};
227
Johannes Bergff1b6e62011-05-04 15:37:28 +0200228/* policy for WoWLAN attributes */
229static const struct nla_policy
230nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
231 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
232 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
233 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200235 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
237 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200239};
240
Johannes Berge5497d72011-07-05 16:35:40 +0200241/* policy for GTK rekey offload attributes */
242static const struct nla_policy
243nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
244 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
245 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
246 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
247};
248
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300249static const struct nla_policy
250nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
251 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_SSID_LEN },
253};
254
Holger Schuriga0438972009-11-11 11:30:02 +0100255/* ifidx get helper */
256static int nl80211_get_ifidx(struct netlink_callback *cb)
257{
258 int res;
259
260 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
261 nl80211_fam.attrbuf, nl80211_fam.maxattr,
262 nl80211_policy);
263 if (res)
264 return res;
265
266 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
267 return -EINVAL;
268
269 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
270 if (!res)
271 return -EINVAL;
272 return res;
273}
274
Johannes Berg67748892010-10-04 21:14:06 +0200275static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
276 struct netlink_callback *cb,
277 struct cfg80211_registered_device **rdev,
278 struct net_device **dev)
279{
280 int ifidx = cb->args[0];
281 int err;
282
283 if (!ifidx)
284 ifidx = nl80211_get_ifidx(cb);
285 if (ifidx < 0)
286 return ifidx;
287
288 cb->args[0] = ifidx;
289
290 rtnl_lock();
291
292 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
293 if (!*dev) {
294 err = -ENODEV;
295 goto out_rtnl;
296 }
297
298 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100299 if (IS_ERR(*rdev)) {
300 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200301 goto out_rtnl;
302 }
303
304 return 0;
305 out_rtnl:
306 rtnl_unlock();
307 return err;
308}
309
310static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
311{
312 cfg80211_unlock_rdev(rdev);
313 rtnl_unlock();
314}
315
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100316/* IE validation */
317static bool is_valid_ie_attr(const struct nlattr *attr)
318{
319 const u8 *pos;
320 int len;
321
322 if (!attr)
323 return true;
324
325 pos = nla_data(attr);
326 len = nla_len(attr);
327
328 while (len) {
329 u8 elemlen;
330
331 if (len < 2)
332 return false;
333 len -= 2;
334
335 elemlen = pos[1];
336 if (elemlen > len)
337 return false;
338
339 len -= elemlen;
340 pos += 2 + elemlen;
341 }
342
343 return true;
344}
345
Johannes Berg55682962007-09-20 13:09:35 -0400346/* message building helper */
347static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
348 int flags, u8 cmd)
349{
350 /* since there is no private header just add the generic one */
351 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
352}
353
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400354static int nl80211_msg_put_channel(struct sk_buff *msg,
355 struct ieee80211_channel *chan)
356{
357 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
358 chan->center_freq);
359
360 if (chan->flags & IEEE80211_CHAN_DISABLED)
361 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
362 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
363 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
364 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
365 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
366 if (chan->flags & IEEE80211_CHAN_RADAR)
367 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
368
369 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
370 DBM_TO_MBM(chan->max_power));
371
372 return 0;
373
374 nla_put_failure:
375 return -ENOBUFS;
376}
377
Johannes Berg55682962007-09-20 13:09:35 -0400378/* netlink command implementations */
379
Johannes Bergb9454e82009-07-08 13:29:08 +0200380struct key_parse {
381 struct key_params p;
382 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200383 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200384 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100385 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200386};
387
388static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
389{
390 struct nlattr *tb[NL80211_KEY_MAX + 1];
391 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
392 nl80211_key_policy);
393 if (err)
394 return err;
395
396 k->def = !!tb[NL80211_KEY_DEFAULT];
397 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
398
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100399 if (k->def) {
400 k->def_uni = true;
401 k->def_multi = true;
402 }
403 if (k->defmgmt)
404 k->def_multi = true;
405
Johannes Bergb9454e82009-07-08 13:29:08 +0200406 if (tb[NL80211_KEY_IDX])
407 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
408
409 if (tb[NL80211_KEY_DATA]) {
410 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
411 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
412 }
413
414 if (tb[NL80211_KEY_SEQ]) {
415 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
416 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
417 }
418
419 if (tb[NL80211_KEY_CIPHER])
420 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
421
Johannes Berge31b8212010-10-05 19:39:30 +0200422 if (tb[NL80211_KEY_TYPE]) {
423 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
424 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
425 return -EINVAL;
426 }
427
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100428 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
429 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100430 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
431 tb[NL80211_KEY_DEFAULT_TYPES],
432 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100433 if (err)
434 return err;
435
436 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
437 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
438 }
439
Johannes Bergb9454e82009-07-08 13:29:08 +0200440 return 0;
441}
442
443static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
444{
445 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
446 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
447 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
448 }
449
450 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
451 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
452 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
453 }
454
455 if (info->attrs[NL80211_ATTR_KEY_IDX])
456 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
457
458 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
459 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
460
461 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
462 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
463
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100464 if (k->def) {
465 k->def_uni = true;
466 k->def_multi = true;
467 }
468 if (k->defmgmt)
469 k->def_multi = true;
470
Johannes Berge31b8212010-10-05 19:39:30 +0200471 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
472 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
473 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
474 return -EINVAL;
475 }
476
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100477 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
478 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
479 int err = nla_parse_nested(
480 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
481 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
482 nl80211_key_default_policy);
483 if (err)
484 return err;
485
486 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
487 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
488 }
489
Johannes Bergb9454e82009-07-08 13:29:08 +0200490 return 0;
491}
492
493static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
494{
495 int err;
496
497 memset(k, 0, sizeof(*k));
498 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200499 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200500
501 if (info->attrs[NL80211_ATTR_KEY])
502 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
503 else
504 err = nl80211_parse_key_old(info, k);
505
506 if (err)
507 return err;
508
509 if (k->def && k->defmgmt)
510 return -EINVAL;
511
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100512 if (k->defmgmt) {
513 if (k->def_uni || !k->def_multi)
514 return -EINVAL;
515 }
516
Johannes Bergb9454e82009-07-08 13:29:08 +0200517 if (k->idx != -1) {
518 if (k->defmgmt) {
519 if (k->idx < 4 || k->idx > 5)
520 return -EINVAL;
521 } else if (k->def) {
522 if (k->idx < 0 || k->idx > 3)
523 return -EINVAL;
524 } else {
525 if (k->idx < 0 || k->idx > 5)
526 return -EINVAL;
527 }
528 }
529
530 return 0;
531}
532
Johannes Bergfffd0932009-07-08 14:22:54 +0200533static struct cfg80211_cached_keys *
534nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
535 struct nlattr *keys)
536{
537 struct key_parse parse;
538 struct nlattr *key;
539 struct cfg80211_cached_keys *result;
540 int rem, err, def = 0;
541
542 result = kzalloc(sizeof(*result), GFP_KERNEL);
543 if (!result)
544 return ERR_PTR(-ENOMEM);
545
546 result->def = -1;
547 result->defmgmt = -1;
548
549 nla_for_each_nested(key, keys, rem) {
550 memset(&parse, 0, sizeof(parse));
551 parse.idx = -1;
552
553 err = nl80211_parse_key_new(key, &parse);
554 if (err)
555 goto error;
556 err = -EINVAL;
557 if (!parse.p.key)
558 goto error;
559 if (parse.idx < 0 || parse.idx > 4)
560 goto error;
561 if (parse.def) {
562 if (def)
563 goto error;
564 def = 1;
565 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100566 if (!parse.def_uni || !parse.def_multi)
567 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200568 } else if (parse.defmgmt)
569 goto error;
570 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200571 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200572 if (err)
573 goto error;
574 result->params[parse.idx].cipher = parse.p.cipher;
575 result->params[parse.idx].key_len = parse.p.key_len;
576 result->params[parse.idx].key = result->data[parse.idx];
577 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
578 }
579
580 return result;
581 error:
582 kfree(result);
583 return ERR_PTR(err);
584}
585
586static int nl80211_key_allowed(struct wireless_dev *wdev)
587{
588 ASSERT_WDEV_LOCK(wdev);
589
Johannes Bergfffd0932009-07-08 14:22:54 +0200590 switch (wdev->iftype) {
591 case NL80211_IFTYPE_AP:
592 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200593 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700594 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200595 break;
596 case NL80211_IFTYPE_ADHOC:
597 if (!wdev->current_bss)
598 return -ENOLINK;
599 break;
600 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200602 if (wdev->sme_state != CFG80211_SME_CONNECTED)
603 return -ENOLINK;
604 break;
605 default:
606 return -EINVAL;
607 }
608
609 return 0;
610}
611
Johannes Berg7527a782011-05-13 10:58:57 +0200612static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
613{
614 struct nlattr *nl_modes = nla_nest_start(msg, attr);
615 int i;
616
617 if (!nl_modes)
618 goto nla_put_failure;
619
620 i = 0;
621 while (ifmodes) {
622 if (ifmodes & 1)
623 NLA_PUT_FLAG(msg, i);
624 ifmodes >>= 1;
625 i++;
626 }
627
628 nla_nest_end(msg, nl_modes);
629 return 0;
630
631nla_put_failure:
632 return -ENOBUFS;
633}
634
635static int nl80211_put_iface_combinations(struct wiphy *wiphy,
636 struct sk_buff *msg)
637{
638 struct nlattr *nl_combis;
639 int i, j;
640
641 nl_combis = nla_nest_start(msg,
642 NL80211_ATTR_INTERFACE_COMBINATIONS);
643 if (!nl_combis)
644 goto nla_put_failure;
645
646 for (i = 0; i < wiphy->n_iface_combinations; i++) {
647 const struct ieee80211_iface_combination *c;
648 struct nlattr *nl_combi, *nl_limits;
649
650 c = &wiphy->iface_combinations[i];
651
652 nl_combi = nla_nest_start(msg, i + 1);
653 if (!nl_combi)
654 goto nla_put_failure;
655
656 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
657 if (!nl_limits)
658 goto nla_put_failure;
659
660 for (j = 0; j < c->n_limits; j++) {
661 struct nlattr *nl_limit;
662
663 nl_limit = nla_nest_start(msg, j + 1);
664 if (!nl_limit)
665 goto nla_put_failure;
666 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
667 c->limits[j].max);
668 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
669 c->limits[j].types))
670 goto nla_put_failure;
671 nla_nest_end(msg, nl_limit);
672 }
673
674 nla_nest_end(msg, nl_limits);
675
676 if (c->beacon_int_infra_match)
677 NLA_PUT_FLAG(msg,
678 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
679 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
680 c->num_different_channels);
681 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
682 c->max_interfaces);
683
684 nla_nest_end(msg, nl_combi);
685 }
686
687 nla_nest_end(msg, nl_combis);
688
689 return 0;
690nla_put_failure:
691 return -ENOBUFS;
692}
693
Johannes Berg55682962007-09-20 13:09:35 -0400694static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
695 struct cfg80211_registered_device *dev)
696{
697 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100698 struct nlattr *nl_bands, *nl_band;
699 struct nlattr *nl_freqs, *nl_freq;
700 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100701 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100702 enum ieee80211_band band;
703 struct ieee80211_channel *chan;
704 struct ieee80211_rate *rate;
705 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200706 const struct ieee80211_txrx_stypes *mgmt_stypes =
707 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400708
709 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
710 if (!hdr)
711 return -1;
712
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500713 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400714 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200715
Johannes Bergf5ea9122009-08-07 16:17:38 +0200716 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
717 cfg80211_rdev_list_generation);
718
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200719 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
720 dev->wiphy.retry_short);
721 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
722 dev->wiphy.retry_long);
723 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
724 dev->wiphy.frag_threshold);
725 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
726 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100727 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
728 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100729 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
730 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300731 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
732 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200733 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
734 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300735 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
736 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300737 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
738 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100739
Johannes Berge31b8212010-10-05 19:39:30 +0200740 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
741 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700742 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
743 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300744 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
745 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530746 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
747 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300748 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
749 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
750 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
751 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530752
Johannes Berg25e47c182009-04-02 20:14:06 +0200753 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
754 sizeof(u32) * dev->wiphy.n_cipher_suites,
755 dev->wiphy.cipher_suites);
756
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100757 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
758 dev->wiphy.max_num_pmkids);
759
Johannes Bergc0692b82010-08-27 14:26:53 +0300760 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
761 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
762
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900763 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
764 dev->wiphy.available_antennas_tx);
765 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
766 dev->wiphy.available_antennas_rx);
767
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200768 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
769 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
770 dev->wiphy.probe_resp_offload);
771
Bruno Randolf7f531e02010-12-16 11:30:22 +0900772 if ((dev->wiphy.available_antennas_tx ||
773 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900774 u32 tx_ant = 0, rx_ant = 0;
775 int res;
776 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
777 if (!res) {
778 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
779 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
780 }
781 }
782
Johannes Berg7527a782011-05-13 10:58:57 +0200783 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
784 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700785 goto nla_put_failure;
786
Johannes Bergee688b002008-01-24 19:38:39 +0100787 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
788 if (!nl_bands)
789 goto nla_put_failure;
790
791 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
792 if (!dev->wiphy.bands[band])
793 continue;
794
795 nl_band = nla_nest_start(msg, band);
796 if (!nl_band)
797 goto nla_put_failure;
798
Johannes Bergd51626d2008-10-09 12:20:13 +0200799 /* add HT info */
800 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
801 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
802 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
803 &dev->wiphy.bands[band]->ht_cap.mcs);
804 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
805 dev->wiphy.bands[band]->ht_cap.cap);
806 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
807 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
808 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
809 dev->wiphy.bands[band]->ht_cap.ampdu_density);
810 }
811
Johannes Bergee688b002008-01-24 19:38:39 +0100812 /* add frequencies */
813 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
814 if (!nl_freqs)
815 goto nla_put_failure;
816
817 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
818 nl_freq = nla_nest_start(msg, i);
819 if (!nl_freq)
820 goto nla_put_failure;
821
822 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100823
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400824 if (nl80211_msg_put_channel(msg, chan))
825 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200826
Johannes Bergee688b002008-01-24 19:38:39 +0100827 nla_nest_end(msg, nl_freq);
828 }
829
830 nla_nest_end(msg, nl_freqs);
831
832 /* add bitrates */
833 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
834 if (!nl_rates)
835 goto nla_put_failure;
836
837 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
838 nl_rate = nla_nest_start(msg, i);
839 if (!nl_rate)
840 goto nla_put_failure;
841
842 rate = &dev->wiphy.bands[band]->bitrates[i];
843 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
844 rate->bitrate);
845 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
846 NLA_PUT_FLAG(msg,
847 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
848
849 nla_nest_end(msg, nl_rate);
850 }
851
852 nla_nest_end(msg, nl_rates);
853
854 nla_nest_end(msg, nl_band);
855 }
856 nla_nest_end(msg, nl_bands);
857
Johannes Berg8fdc6212009-03-14 09:34:01 +0100858 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
859 if (!nl_cmds)
860 goto nla_put_failure;
861
862 i = 0;
863#define CMD(op, n) \
864 do { \
865 if (dev->ops->op) { \
866 i++; \
867 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
868 } \
869 } while (0)
870
871 CMD(add_virtual_intf, NEW_INTERFACE);
872 CMD(change_virtual_intf, SET_INTERFACE);
873 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100874 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100875 CMD(add_station, NEW_STATION);
876 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800877 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100878 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200879 CMD(auth, AUTHENTICATE);
880 CMD(assoc, ASSOCIATE);
881 CMD(deauth, DEAUTHENTICATE);
882 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200883 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100884 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100885 CMD(set_pmksa, SET_PMKSA);
886 CMD(del_pmksa, DEL_PMKSA);
887 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100888 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
889 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200890 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200891 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100892 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100893 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200894 i++;
895 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
896 }
Johannes Bergf444de02010-05-05 15:25:02 +0200897 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400898 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300899 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
900 CMD(tdls_mgmt, TDLS_MGMT);
901 CMD(tdls_oper, TDLS_OPER);
902 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300903 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
904 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100905 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100906 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100907 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
908 i++;
909 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
910 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100911
Kalle Valo4745fc02011-11-17 19:06:10 +0200912#ifdef CONFIG_NL80211_TESTMODE
913 CMD(testmode_cmd, TESTMODE);
914#endif
915
Johannes Berg8fdc6212009-03-14 09:34:01 +0100916#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200917
Johannes Berg6829c872009-07-02 09:13:27 +0200918 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919 i++;
920 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
921 }
922
Johannes Berg6829c872009-07-02 09:13:27 +0200923 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200924 i++;
925 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
926 }
927
Johannes Berg8fdc6212009-03-14 09:34:01 +0100928 nla_nest_end(msg, nl_cmds);
929
Johannes Berg7c4ef712011-11-18 15:33:48 +0100930 if (dev->ops->remain_on_channel &&
931 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100932 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
933 dev->wiphy.max_remain_on_channel_duration);
934
Johannes Berg7c4ef712011-11-18 15:33:48 +0100935 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100936 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
937
Johannes Berg2e161f72010-08-12 15:38:38 +0200938 if (mgmt_stypes) {
939 u16 stypes;
940 struct nlattr *nl_ftypes, *nl_ifs;
941 enum nl80211_iftype ift;
942
943 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
944 if (!nl_ifs)
945 goto nla_put_failure;
946
947 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
948 nl_ftypes = nla_nest_start(msg, ift);
949 if (!nl_ftypes)
950 goto nla_put_failure;
951 i = 0;
952 stypes = mgmt_stypes[ift].tx;
953 while (stypes) {
954 if (stypes & 1)
955 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
956 (i << 4) | IEEE80211_FTYPE_MGMT);
957 stypes >>= 1;
958 i++;
959 }
960 nla_nest_end(msg, nl_ftypes);
961 }
962
Johannes Berg74b70a42010-08-24 12:15:53 +0200963 nla_nest_end(msg, nl_ifs);
964
Johannes Berg2e161f72010-08-12 15:38:38 +0200965 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
966 if (!nl_ifs)
967 goto nla_put_failure;
968
969 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
970 nl_ftypes = nla_nest_start(msg, ift);
971 if (!nl_ftypes)
972 goto nla_put_failure;
973 i = 0;
974 stypes = mgmt_stypes[ift].rx;
975 while (stypes) {
976 if (stypes & 1)
977 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
978 (i << 4) | IEEE80211_FTYPE_MGMT);
979 stypes >>= 1;
980 i++;
981 }
982 nla_nest_end(msg, nl_ftypes);
983 }
984 nla_nest_end(msg, nl_ifs);
985 }
986
Johannes Bergff1b6e62011-05-04 15:37:28 +0200987 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
988 struct nlattr *nl_wowlan;
989
990 nl_wowlan = nla_nest_start(msg,
991 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
992 if (!nl_wowlan)
993 goto nla_put_failure;
994
995 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
996 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
997 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
998 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
999 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1000 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02001001 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1002 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1003 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1004 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1005 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1006 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1007 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1008 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1009 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1010 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001011 if (dev->wiphy.wowlan.n_patterns) {
1012 struct nl80211_wowlan_pattern_support pat = {
1013 .max_patterns = dev->wiphy.wowlan.n_patterns,
1014 .min_pattern_len =
1015 dev->wiphy.wowlan.pattern_min_len,
1016 .max_pattern_len =
1017 dev->wiphy.wowlan.pattern_max_len,
1018 };
1019 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1020 sizeof(pat), &pat);
1021 }
1022
1023 nla_nest_end(msg, nl_wowlan);
1024 }
1025
Johannes Berg7527a782011-05-13 10:58:57 +02001026 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1027 dev->wiphy.software_iftypes))
1028 goto nla_put_failure;
1029
1030 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1031 goto nla_put_failure;
1032
Johannes Berg562a7482011-11-07 12:39:33 +01001033 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1034 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1035 dev->wiphy.ap_sme_capa);
1036
Johannes Berg1f074bd2011-11-06 14:13:33 +01001037 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1038
Ben Greear7e7c8922011-11-18 11:31:59 -08001039 if (dev->wiphy.ht_capa_mod_mask)
1040 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1041 sizeof(*dev->wiphy.ht_capa_mod_mask),
1042 dev->wiphy.ht_capa_mod_mask);
1043
Johannes Berg55682962007-09-20 13:09:35 -04001044 return genlmsg_end(msg, hdr);
1045
1046 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001047 genlmsg_cancel(msg, hdr);
1048 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001049}
1050
1051static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1052{
1053 int idx = 0;
1054 int start = cb->args[0];
1055 struct cfg80211_registered_device *dev;
1056
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001057 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001058 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001059 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1060 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001061 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001062 continue;
1063 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1064 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001065 dev) < 0) {
1066 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001067 break;
Julius Volzb4637272008-07-08 14:02:19 +02001068 }
Johannes Berg55682962007-09-20 13:09:35 -04001069 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001070 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001071
1072 cb->args[0] = idx;
1073
1074 return skb->len;
1075}
1076
1077static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1078{
1079 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001080 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001081
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001082 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001083 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001084 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001085
Johannes Berg4c476992010-10-04 21:36:35 +02001086 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1087 nlmsg_free(msg);
1088 return -ENOBUFS;
1089 }
Johannes Berg55682962007-09-20 13:09:35 -04001090
Johannes Berg134e6372009-07-10 09:51:34 +00001091 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001092}
1093
Jouni Malinen31888482008-10-30 16:59:24 +02001094static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1095 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1096 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1097 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1098 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1099 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1100};
1101
1102static int parse_txq_params(struct nlattr *tb[],
1103 struct ieee80211_txq_params *txq_params)
1104{
1105 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1106 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1107 !tb[NL80211_TXQ_ATTR_AIFS])
1108 return -EINVAL;
1109
1110 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1111 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1112 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1113 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1114 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1115
1116 return 0;
1117}
1118
Johannes Bergf444de02010-05-05 15:25:02 +02001119static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1120{
1121 /*
1122 * You can only set the channel explicitly for AP, mesh
1123 * and WDS type interfaces; all others have their channel
1124 * managed via their respective "establish a connection"
1125 * command (connect, join, ...)
1126 *
1127 * Monitors are special as they are normally slaved to
1128 * whatever else is going on, so they behave as though
1129 * you tried setting the wiphy channel itself.
1130 */
1131 return !wdev ||
1132 wdev->iftype == NL80211_IFTYPE_AP ||
1133 wdev->iftype == NL80211_IFTYPE_WDS ||
1134 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001135 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1136 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001137}
1138
1139static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1140 struct wireless_dev *wdev,
1141 struct genl_info *info)
1142{
1143 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1144 u32 freq;
1145 int result;
1146
1147 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1148 return -EINVAL;
1149
1150 if (!nl80211_can_set_dev_channel(wdev))
1151 return -EOPNOTSUPP;
1152
1153 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1154 channel_type = nla_get_u32(info->attrs[
1155 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1156 if (channel_type != NL80211_CHAN_NO_HT &&
1157 channel_type != NL80211_CHAN_HT20 &&
1158 channel_type != NL80211_CHAN_HT40PLUS &&
1159 channel_type != NL80211_CHAN_HT40MINUS)
1160 return -EINVAL;
1161 }
1162
1163 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1164
1165 mutex_lock(&rdev->devlist_mtx);
1166 if (wdev) {
1167 wdev_lock(wdev);
1168 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1169 wdev_unlock(wdev);
1170 } else {
1171 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1172 }
1173 mutex_unlock(&rdev->devlist_mtx);
1174
1175 return result;
1176}
1177
1178static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1179{
Johannes Berg4c476992010-10-04 21:36:35 +02001180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1181 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001182
Johannes Berg4c476992010-10-04 21:36:35 +02001183 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001184}
1185
Bill Jordane8347eb2010-10-01 13:54:28 -04001186static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1187{
Johannes Berg43b19952010-10-07 13:10:30 +02001188 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1189 struct net_device *dev = info->user_ptr[1];
1190 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001191 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001192
1193 if (!info->attrs[NL80211_ATTR_MAC])
1194 return -EINVAL;
1195
Johannes Berg43b19952010-10-07 13:10:30 +02001196 if (netif_running(dev))
1197 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001198
Johannes Berg43b19952010-10-07 13:10:30 +02001199 if (!rdev->ops->set_wds_peer)
1200 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001201
Johannes Berg43b19952010-10-07 13:10:30 +02001202 if (wdev->iftype != NL80211_IFTYPE_WDS)
1203 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001204
1205 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001206 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001207}
1208
1209
Johannes Berg55682962007-09-20 13:09:35 -04001210static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1211{
1212 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001213 struct net_device *netdev = NULL;
1214 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001215 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001216 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001217 u32 changed;
1218 u8 retry_short = 0, retry_long = 0;
1219 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001220 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001221
Johannes Bergf444de02010-05-05 15:25:02 +02001222 /*
1223 * Try to find the wiphy and netdev. Normally this
1224 * function shouldn't need the netdev, but this is
1225 * done for backward compatibility -- previously
1226 * setting the channel was done per wiphy, but now
1227 * it is per netdev. Previous userland like hostapd
1228 * also passed a netdev to set_wiphy, so that it is
1229 * possible to let that go to the right netdev!
1230 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001231 mutex_lock(&cfg80211_mutex);
1232
Johannes Bergf444de02010-05-05 15:25:02 +02001233 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1234 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1235
1236 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1237 if (netdev && netdev->ieee80211_ptr) {
1238 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1239 mutex_lock(&rdev->mtx);
1240 } else
1241 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001242 }
1243
Johannes Bergf444de02010-05-05 15:25:02 +02001244 if (!netdev) {
1245 rdev = __cfg80211_rdev_from_info(info);
1246 if (IS_ERR(rdev)) {
1247 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001248 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001249 }
1250 wdev = NULL;
1251 netdev = NULL;
1252 result = 0;
1253
1254 mutex_lock(&rdev->mtx);
1255 } else if (netif_running(netdev) &&
1256 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1257 wdev = netdev->ieee80211_ptr;
1258 else
1259 wdev = NULL;
1260
1261 /*
1262 * end workaround code, by now the rdev is available
1263 * and locked, and wdev may or may not be NULL.
1264 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001265
1266 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001267 result = cfg80211_dev_rename(
1268 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001269
1270 mutex_unlock(&cfg80211_mutex);
1271
1272 if (result)
1273 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001274
Jouni Malinen31888482008-10-30 16:59:24 +02001275 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1276 struct ieee80211_txq_params txq_params;
1277 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1278
1279 if (!rdev->ops->set_txq_params) {
1280 result = -EOPNOTSUPP;
1281 goto bad_res;
1282 }
1283
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001284 if (!netdev) {
1285 result = -EINVAL;
1286 goto bad_res;
1287 }
1288
Johannes Berg133a3ff2011-11-03 14:50:13 +01001289 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1290 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1291 result = -EINVAL;
1292 goto bad_res;
1293 }
1294
Jouni Malinen31888482008-10-30 16:59:24 +02001295 nla_for_each_nested(nl_txq_params,
1296 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1297 rem_txq_params) {
1298 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1299 nla_data(nl_txq_params),
1300 nla_len(nl_txq_params),
1301 txq_params_policy);
1302 result = parse_txq_params(tb, &txq_params);
1303 if (result)
1304 goto bad_res;
1305
1306 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001307 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001308 &txq_params);
1309 if (result)
1310 goto bad_res;
1311 }
1312 }
1313
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001314 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001315 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001316 if (result)
1317 goto bad_res;
1318 }
1319
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001320 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1321 enum nl80211_tx_power_setting type;
1322 int idx, mbm = 0;
1323
1324 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001325 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001326 goto bad_res;
1327 }
1328
1329 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1330 type = nla_get_u32(info->attrs[idx]);
1331
1332 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1333 (type != NL80211_TX_POWER_AUTOMATIC)) {
1334 result = -EINVAL;
1335 goto bad_res;
1336 }
1337
1338 if (type != NL80211_TX_POWER_AUTOMATIC) {
1339 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1340 mbm = nla_get_u32(info->attrs[idx]);
1341 }
1342
1343 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1344 if (result)
1345 goto bad_res;
1346 }
1347
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001348 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1349 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1350 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001351 if ((!rdev->wiphy.available_antennas_tx &&
1352 !rdev->wiphy.available_antennas_rx) ||
1353 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001354 result = -EOPNOTSUPP;
1355 goto bad_res;
1356 }
1357
1358 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1359 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1360
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001361 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001362 * available antenna masks, except for the "all" mask */
1363 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1364 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001365 result = -EINVAL;
1366 goto bad_res;
1367 }
1368
Bruno Randolf7f531e02010-12-16 11:30:22 +09001369 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1370 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001371
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001372 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1373 if (result)
1374 goto bad_res;
1375 }
1376
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001377 changed = 0;
1378
1379 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1380 retry_short = nla_get_u8(
1381 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1382 if (retry_short == 0) {
1383 result = -EINVAL;
1384 goto bad_res;
1385 }
1386 changed |= WIPHY_PARAM_RETRY_SHORT;
1387 }
1388
1389 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1390 retry_long = nla_get_u8(
1391 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1392 if (retry_long == 0) {
1393 result = -EINVAL;
1394 goto bad_res;
1395 }
1396 changed |= WIPHY_PARAM_RETRY_LONG;
1397 }
1398
1399 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1400 frag_threshold = nla_get_u32(
1401 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1402 if (frag_threshold < 256) {
1403 result = -EINVAL;
1404 goto bad_res;
1405 }
1406 if (frag_threshold != (u32) -1) {
1407 /*
1408 * Fragments (apart from the last one) are required to
1409 * have even length. Make the fragmentation code
1410 * simpler by stripping LSB should someone try to use
1411 * odd threshold value.
1412 */
1413 frag_threshold &= ~0x1;
1414 }
1415 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1416 }
1417
1418 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1419 rts_threshold = nla_get_u32(
1420 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1421 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1422 }
1423
Lukáš Turek81077e82009-12-21 22:50:47 +01001424 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1425 coverage_class = nla_get_u8(
1426 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1427 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1428 }
1429
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001430 if (changed) {
1431 u8 old_retry_short, old_retry_long;
1432 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001433 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001434
1435 if (!rdev->ops->set_wiphy_params) {
1436 result = -EOPNOTSUPP;
1437 goto bad_res;
1438 }
1439
1440 old_retry_short = rdev->wiphy.retry_short;
1441 old_retry_long = rdev->wiphy.retry_long;
1442 old_frag_threshold = rdev->wiphy.frag_threshold;
1443 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001444 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001445
1446 if (changed & WIPHY_PARAM_RETRY_SHORT)
1447 rdev->wiphy.retry_short = retry_short;
1448 if (changed & WIPHY_PARAM_RETRY_LONG)
1449 rdev->wiphy.retry_long = retry_long;
1450 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1451 rdev->wiphy.frag_threshold = frag_threshold;
1452 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1453 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001454 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1455 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001456
1457 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1458 if (result) {
1459 rdev->wiphy.retry_short = old_retry_short;
1460 rdev->wiphy.retry_long = old_retry_long;
1461 rdev->wiphy.frag_threshold = old_frag_threshold;
1462 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001463 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001464 }
1465 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001466
Johannes Berg306d6112008-12-08 12:39:04 +01001467 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001468 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001469 if (netdev)
1470 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001471 return result;
1472}
1473
1474
1475static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001476 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001477 struct net_device *dev)
1478{
1479 void *hdr;
1480
1481 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1482 if (!hdr)
1483 return -1;
1484
1485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001486 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001487 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001488 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001489
1490 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1491 rdev->devlist_generation ^
1492 (cfg80211_rdev_list_generation << 2));
1493
Johannes Berg55682962007-09-20 13:09:35 -04001494 return genlmsg_end(msg, hdr);
1495
1496 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001497 genlmsg_cancel(msg, hdr);
1498 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001499}
1500
1501static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1502{
1503 int wp_idx = 0;
1504 int if_idx = 0;
1505 int wp_start = cb->args[0];
1506 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001507 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001508 struct wireless_dev *wdev;
1509
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001510 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001511 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1512 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001513 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001514 if (wp_idx < wp_start) {
1515 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001516 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001517 }
Johannes Berg55682962007-09-20 13:09:35 -04001518 if_idx = 0;
1519
Johannes Bergf5ea9122009-08-07 16:17:38 +02001520 mutex_lock(&rdev->devlist_mtx);
1521 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001522 if (if_idx < if_start) {
1523 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001524 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001525 }
Johannes Berg55682962007-09-20 13:09:35 -04001526 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1527 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001528 rdev, wdev->netdev) < 0) {
1529 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001530 goto out;
1531 }
1532 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001533 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001534 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001535
1536 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001537 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001538 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001539 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001540
1541 cb->args[0] = wp_idx;
1542 cb->args[1] = if_idx;
1543
1544 return skb->len;
1545}
1546
1547static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1548{
1549 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001550 struct cfg80211_registered_device *dev = info->user_ptr[0];
1551 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001552
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001553 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001554 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001555 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001556
Johannes Bergd7264052009-04-19 16:23:20 +02001557 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001558 dev, netdev) < 0) {
1559 nlmsg_free(msg);
1560 return -ENOBUFS;
1561 }
Johannes Berg55682962007-09-20 13:09:35 -04001562
Johannes Berg134e6372009-07-10 09:51:34 +00001563 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001564}
1565
Michael Wu66f7ac52008-01-31 19:48:22 +01001566static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1567 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1568 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1569 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1570 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1571 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1572};
1573
1574static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1575{
1576 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1577 int flag;
1578
1579 *mntrflags = 0;
1580
1581 if (!nla)
1582 return -EINVAL;
1583
1584 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1585 nla, mntr_flags_policy))
1586 return -EINVAL;
1587
1588 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1589 if (flags[flag])
1590 *mntrflags |= (1<<flag);
1591
1592 return 0;
1593}
1594
Johannes Berg9bc383d2009-11-19 11:55:19 +01001595static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001596 struct net_device *netdev, u8 use_4addr,
1597 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001598{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001599 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001600 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001601 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001602 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001603 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001604
1605 switch (iftype) {
1606 case NL80211_IFTYPE_AP_VLAN:
1607 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1608 return 0;
1609 break;
1610 case NL80211_IFTYPE_STATION:
1611 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1612 return 0;
1613 break;
1614 default:
1615 break;
1616 }
1617
1618 return -EOPNOTSUPP;
1619}
1620
Johannes Berg55682962007-09-20 13:09:35 -04001621static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1622{
Johannes Berg4c476992010-10-04 21:36:35 +02001623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001624 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001625 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001626 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001627 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001628 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001629 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001630
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001631 memset(&params, 0, sizeof(params));
1632
Johannes Berg04a773a2009-04-19 21:24:32 +02001633 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001634
Johannes Berg723b0382008-09-16 20:22:09 +02001635 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001636 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001637 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001638 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001639 if (ntype > NL80211_IFTYPE_MAX)
1640 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001641 }
1642
Johannes Berg92ffe052008-09-16 20:39:36 +02001643 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001644 struct wireless_dev *wdev = dev->ieee80211_ptr;
1645
Johannes Berg4c476992010-10-04 21:36:35 +02001646 if (ntype != NL80211_IFTYPE_MESH_POINT)
1647 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001648 if (netif_running(dev))
1649 return -EBUSY;
1650
1651 wdev_lock(wdev);
1652 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1653 IEEE80211_MAX_MESH_ID_LEN);
1654 wdev->mesh_id_up_len =
1655 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1656 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1657 wdev->mesh_id_up_len);
1658 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001659 }
1660
Felix Fietkau8b787642009-11-10 18:53:10 +01001661 if (info->attrs[NL80211_ATTR_4ADDR]) {
1662 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1663 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001664 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001665 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001666 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001667 } else {
1668 params.use_4addr = -1;
1669 }
1670
Johannes Berg92ffe052008-09-16 20:39:36 +02001671 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001672 if (ntype != NL80211_IFTYPE_MONITOR)
1673 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001674 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1675 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001676 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001677 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001678
1679 flags = &_flags;
1680 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001681 }
Johannes Berg3b858752009-03-12 09:55:09 +01001682
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001683 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001684 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001685 else
1686 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001687
Johannes Berg9bc383d2009-11-19 11:55:19 +01001688 if (!err && params.use_4addr != -1)
1689 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1690
Johannes Berg55682962007-09-20 13:09:35 -04001691 return err;
1692}
1693
1694static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1695{
Johannes Berg4c476992010-10-04 21:36:35 +02001696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001697 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001698 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001699 int err;
1700 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001701 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001702
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001703 memset(&params, 0, sizeof(params));
1704
Johannes Berg55682962007-09-20 13:09:35 -04001705 if (!info->attrs[NL80211_ATTR_IFNAME])
1706 return -EINVAL;
1707
1708 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1709 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1710 if (type > NL80211_IFTYPE_MAX)
1711 return -EINVAL;
1712 }
1713
Johannes Berg79c97e92009-07-07 03:56:12 +02001714 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001715 !(rdev->wiphy.interface_modes & (1 << type)))
1716 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001717
Johannes Berg9bc383d2009-11-19 11:55:19 +01001718 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001719 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001720 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001721 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001722 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001723 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001724
Michael Wu66f7ac52008-01-31 19:48:22 +01001725 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1726 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1727 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001728 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001729 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001730 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001731 if (IS_ERR(dev))
1732 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001733
Johannes Berg29cbe682010-12-03 09:20:44 +01001734 if (type == NL80211_IFTYPE_MESH_POINT &&
1735 info->attrs[NL80211_ATTR_MESH_ID]) {
1736 struct wireless_dev *wdev = dev->ieee80211_ptr;
1737
1738 wdev_lock(wdev);
1739 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1740 IEEE80211_MAX_MESH_ID_LEN);
1741 wdev->mesh_id_up_len =
1742 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1743 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1744 wdev->mesh_id_up_len);
1745 wdev_unlock(wdev);
1746 }
1747
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001748 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001749}
1750
1751static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1752{
Johannes Berg4c476992010-10-04 21:36:35 +02001753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1754 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001755
Johannes Berg4c476992010-10-04 21:36:35 +02001756 if (!rdev->ops->del_virtual_intf)
1757 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001758
Johannes Berg4c476992010-10-04 21:36:35 +02001759 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001760}
1761
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001762static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1763{
1764 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1765 struct net_device *dev = info->user_ptr[1];
1766 u16 noack_map;
1767
1768 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1769 return -EINVAL;
1770
1771 if (!rdev->ops->set_noack_map)
1772 return -EOPNOTSUPP;
1773
1774 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1775
1776 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1777}
1778
Johannes Berg41ade002007-12-19 02:03:29 +01001779struct get_key_cookie {
1780 struct sk_buff *msg;
1781 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001782 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001783};
1784
1785static void get_key_callback(void *c, struct key_params *params)
1786{
Johannes Bergb9454e82009-07-08 13:29:08 +02001787 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001788 struct get_key_cookie *cookie = c;
1789
1790 if (params->key)
1791 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1792 params->key_len, params->key);
1793
1794 if (params->seq)
1795 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1796 params->seq_len, params->seq);
1797
1798 if (params->cipher)
1799 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1800 params->cipher);
1801
Johannes Bergb9454e82009-07-08 13:29:08 +02001802 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1803 if (!key)
1804 goto nla_put_failure;
1805
1806 if (params->key)
1807 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1808 params->key_len, params->key);
1809
1810 if (params->seq)
1811 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1812 params->seq_len, params->seq);
1813
1814 if (params->cipher)
1815 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1816 params->cipher);
1817
1818 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1819
1820 nla_nest_end(cookie->msg, key);
1821
Johannes Berg41ade002007-12-19 02:03:29 +01001822 return;
1823 nla_put_failure:
1824 cookie->error = 1;
1825}
1826
1827static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1828{
Johannes Berg4c476992010-10-04 21:36:35 +02001829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001830 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001831 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001832 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001833 const u8 *mac_addr = NULL;
1834 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001835 struct get_key_cookie cookie = {
1836 .error = 0,
1837 };
1838 void *hdr;
1839 struct sk_buff *msg;
1840
1841 if (info->attrs[NL80211_ATTR_KEY_IDX])
1842 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1843
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001844 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001845 return -EINVAL;
1846
1847 if (info->attrs[NL80211_ATTR_MAC])
1848 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1849
Johannes Berge31b8212010-10-05 19:39:30 +02001850 pairwise = !!mac_addr;
1851 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1852 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1853 if (kt >= NUM_NL80211_KEYTYPES)
1854 return -EINVAL;
1855 if (kt != NL80211_KEYTYPE_GROUP &&
1856 kt != NL80211_KEYTYPE_PAIRWISE)
1857 return -EINVAL;
1858 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1859 }
1860
Johannes Berg4c476992010-10-04 21:36:35 +02001861 if (!rdev->ops->get_key)
1862 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001863
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001864 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001865 if (!msg)
1866 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001867
1868 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1869 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001870 if (IS_ERR(hdr))
1871 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001872
1873 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001874 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001875
1876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1877 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1878 if (mac_addr)
1879 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1880
Johannes Berge31b8212010-10-05 19:39:30 +02001881 if (pairwise && mac_addr &&
1882 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1883 return -ENOENT;
1884
1885 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1886 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001887
1888 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001889 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001890
1891 if (cookie.error)
1892 goto nla_put_failure;
1893
1894 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001895 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001896
1897 nla_put_failure:
1898 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001899 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001900 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001901 return err;
1902}
1903
1904static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1905{
Johannes Berg4c476992010-10-04 21:36:35 +02001906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001907 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001908 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001909 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001910
Johannes Bergb9454e82009-07-08 13:29:08 +02001911 err = nl80211_parse_key(info, &key);
1912 if (err)
1913 return err;
1914
1915 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001916 return -EINVAL;
1917
Johannes Bergb9454e82009-07-08 13:29:08 +02001918 /* only support setting default key */
1919 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001920 return -EINVAL;
1921
Johannes Bergfffd0932009-07-08 14:22:54 +02001922 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001923
1924 if (key.def) {
1925 if (!rdev->ops->set_default_key) {
1926 err = -EOPNOTSUPP;
1927 goto out;
1928 }
1929
1930 err = nl80211_key_allowed(dev->ieee80211_ptr);
1931 if (err)
1932 goto out;
1933
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001934 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1935 key.def_uni, key.def_multi);
1936
1937 if (err)
1938 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001939
Johannes Berg3d23e342009-09-29 23:27:28 +02001940#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001941 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001942#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001943 } else {
1944 if (key.def_uni || !key.def_multi) {
1945 err = -EINVAL;
1946 goto out;
1947 }
1948
1949 if (!rdev->ops->set_default_mgmt_key) {
1950 err = -EOPNOTSUPP;
1951 goto out;
1952 }
1953
1954 err = nl80211_key_allowed(dev->ieee80211_ptr);
1955 if (err)
1956 goto out;
1957
1958 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1959 dev, key.idx);
1960 if (err)
1961 goto out;
1962
1963#ifdef CONFIG_CFG80211_WEXT
1964 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1965#endif
1966 }
1967
1968 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001969 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001970
Johannes Berg41ade002007-12-19 02:03:29 +01001971 return err;
1972}
1973
1974static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1975{
Johannes Berg4c476992010-10-04 21:36:35 +02001976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001977 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001978 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001979 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001980 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001981
Johannes Bergb9454e82009-07-08 13:29:08 +02001982 err = nl80211_parse_key(info, &key);
1983 if (err)
1984 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001985
Johannes Bergb9454e82009-07-08 13:29:08 +02001986 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001987 return -EINVAL;
1988
Johannes Berg41ade002007-12-19 02:03:29 +01001989 if (info->attrs[NL80211_ATTR_MAC])
1990 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1991
Johannes Berge31b8212010-10-05 19:39:30 +02001992 if (key.type == -1) {
1993 if (mac_addr)
1994 key.type = NL80211_KEYTYPE_PAIRWISE;
1995 else
1996 key.type = NL80211_KEYTYPE_GROUP;
1997 }
1998
1999 /* for now */
2000 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2001 key.type != NL80211_KEYTYPE_GROUP)
2002 return -EINVAL;
2003
Johannes Berg4c476992010-10-04 21:36:35 +02002004 if (!rdev->ops->add_key)
2005 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002006
Johannes Berge31b8212010-10-05 19:39:30 +02002007 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2008 key.type == NL80211_KEYTYPE_PAIRWISE,
2009 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002010 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002011
2012 wdev_lock(dev->ieee80211_ptr);
2013 err = nl80211_key_allowed(dev->ieee80211_ptr);
2014 if (!err)
2015 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002016 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002017 mac_addr, &key.p);
2018 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002019
Johannes Berg41ade002007-12-19 02:03:29 +01002020 return err;
2021}
2022
2023static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2024{
Johannes Berg4c476992010-10-04 21:36:35 +02002025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002026 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002027 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002028 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002029 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002030
Johannes Bergb9454e82009-07-08 13:29:08 +02002031 err = nl80211_parse_key(info, &key);
2032 if (err)
2033 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002034
2035 if (info->attrs[NL80211_ATTR_MAC])
2036 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2037
Johannes Berge31b8212010-10-05 19:39:30 +02002038 if (key.type == -1) {
2039 if (mac_addr)
2040 key.type = NL80211_KEYTYPE_PAIRWISE;
2041 else
2042 key.type = NL80211_KEYTYPE_GROUP;
2043 }
2044
2045 /* for now */
2046 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2047 key.type != NL80211_KEYTYPE_GROUP)
2048 return -EINVAL;
2049
Johannes Berg4c476992010-10-04 21:36:35 +02002050 if (!rdev->ops->del_key)
2051 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002052
Johannes Bergfffd0932009-07-08 14:22:54 +02002053 wdev_lock(dev->ieee80211_ptr);
2054 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002055
2056 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2057 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2058 err = -ENOENT;
2059
Johannes Bergfffd0932009-07-08 14:22:54 +02002060 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002061 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2062 key.type == NL80211_KEYTYPE_PAIRWISE,
2063 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002064
Johannes Berg3d23e342009-09-29 23:27:28 +02002065#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002066 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002067 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002068 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002069 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002070 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2071 }
2072#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002073 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002074
Johannes Berg41ade002007-12-19 02:03:29 +01002075 return err;
2076}
2077
Johannes Berg88600202012-02-13 15:17:18 +01002078static int nl80211_parse_beacon(struct genl_info *info,
2079 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002080{
Johannes Berg88600202012-02-13 15:17:18 +01002081 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002082
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002083 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2084 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2085 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2086 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002087 return -EINVAL;
2088
Johannes Berg88600202012-02-13 15:17:18 +01002089 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002090
Johannes Berged1b6cc2007-12-19 02:03:32 +01002091 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002092 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2093 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2094 if (!bcn->head_len)
2095 return -EINVAL;
2096 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002097 }
2098
2099 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002100 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2101 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002102 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002103 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002104 }
2105
Johannes Berg4c476992010-10-04 21:36:35 +02002106 if (!haveinfo)
2107 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002108
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002109 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002110 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2111 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002112 }
2113
2114 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002115 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002116 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002117 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002118 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2119 }
2120
2121 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002122 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002123 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002124 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002125 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2126 }
2127
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002128 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002129 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002130 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002131 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002132 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2133 }
2134
Johannes Berg88600202012-02-13 15:17:18 +01002135 return 0;
2136}
2137
2138static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2139{
2140 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2141 struct net_device *dev = info->user_ptr[1];
2142 struct wireless_dev *wdev = dev->ieee80211_ptr;
2143 struct cfg80211_ap_settings params;
2144 int err;
2145
2146 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2147 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2148 return -EOPNOTSUPP;
2149
2150 if (!rdev->ops->start_ap)
2151 return -EOPNOTSUPP;
2152
2153 if (wdev->beacon_interval)
2154 return -EALREADY;
2155
2156 memset(&params, 0, sizeof(params));
2157
2158 /* these are required for START_AP */
2159 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2160 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2161 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2162 return -EINVAL;
2163
2164 err = nl80211_parse_beacon(info, &params.beacon);
2165 if (err)
2166 return err;
2167
2168 params.beacon_interval =
2169 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2170 params.dtim_period =
2171 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2172
2173 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2174 if (err)
2175 return err;
2176
2177 /*
2178 * In theory, some of these attributes should be required here
2179 * but since they were not used when the command was originally
2180 * added, keep them optional for old user space programs to let
2181 * them continue to work with drivers that do not need the
2182 * additional information -- drivers must check!
2183 */
2184 if (info->attrs[NL80211_ATTR_SSID]) {
2185 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2186 params.ssid_len =
2187 nla_len(info->attrs[NL80211_ATTR_SSID]);
2188 if (params.ssid_len == 0 ||
2189 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2190 return -EINVAL;
2191 }
2192
2193 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2194 params.hidden_ssid = nla_get_u32(
2195 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2196 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2197 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2198 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2199 return -EINVAL;
2200 }
2201
2202 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2203
2204 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2205 params.auth_type = nla_get_u32(
2206 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2207 if (!nl80211_valid_auth_type(params.auth_type))
2208 return -EINVAL;
2209 } else
2210 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2211
2212 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2213 NL80211_MAX_NR_CIPHER_SUITES);
2214 if (err)
2215 return err;
2216
2217 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2218 if (!err)
2219 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002220 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002221}
2222
Johannes Berg88600202012-02-13 15:17:18 +01002223static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2224{
2225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2226 struct net_device *dev = info->user_ptr[1];
2227 struct wireless_dev *wdev = dev->ieee80211_ptr;
2228 struct cfg80211_beacon_data params;
2229 int err;
2230
2231 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2232 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2233 return -EOPNOTSUPP;
2234
2235 if (!rdev->ops->change_beacon)
2236 return -EOPNOTSUPP;
2237
2238 if (!wdev->beacon_interval)
2239 return -EINVAL;
2240
2241 err = nl80211_parse_beacon(info, &params);
2242 if (err)
2243 return err;
2244
2245 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2246}
2247
2248static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002249{
Johannes Berg4c476992010-10-04 21:36:35 +02002250 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2251 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002252 struct wireless_dev *wdev = dev->ieee80211_ptr;
2253 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002254
Johannes Berg88600202012-02-13 15:17:18 +01002255 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002256 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002257
Johannes Berg074ac8d2010-09-16 14:58:22 +02002258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002259 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2260 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002261
Johannes Berg88600202012-02-13 15:17:18 +01002262 if (!wdev->beacon_interval)
2263 return -ENOENT;
2264
2265 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002266 if (!err)
2267 wdev->beacon_interval = 0;
2268 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002269}
2270
Johannes Berg5727ef12007-12-19 02:03:34 +01002271static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2272 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2273 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2274 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002275 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002276 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002277 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002278};
2279
Johannes Bergeccb8e82009-05-11 21:57:56 +03002280static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002281 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002282 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002283{
2284 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002285 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002286 int flag;
2287
Johannes Bergeccb8e82009-05-11 21:57:56 +03002288 /*
2289 * Try parsing the new attribute first so userspace
2290 * can specify both for older kernels.
2291 */
2292 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2293 if (nla) {
2294 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002295
Johannes Bergeccb8e82009-05-11 21:57:56 +03002296 sta_flags = nla_data(nla);
2297 params->sta_flags_mask = sta_flags->mask;
2298 params->sta_flags_set = sta_flags->set;
2299 if ((params->sta_flags_mask |
2300 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2301 return -EINVAL;
2302 return 0;
2303 }
2304
2305 /* if present, parse the old attribute */
2306
2307 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002308 if (!nla)
2309 return 0;
2310
2311 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2312 nla, sta_flags_policy))
2313 return -EINVAL;
2314
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002315 /*
2316 * Only allow certain flags for interface types so that
2317 * other attributes are silently ignored. Remember that
2318 * this is backward compatibility code with old userspace
2319 * and shouldn't be hit in other cases anyway.
2320 */
2321 switch (iftype) {
2322 case NL80211_IFTYPE_AP:
2323 case NL80211_IFTYPE_AP_VLAN:
2324 case NL80211_IFTYPE_P2P_GO:
2325 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2326 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2327 BIT(NL80211_STA_FLAG_WME) |
2328 BIT(NL80211_STA_FLAG_MFP);
2329 break;
2330 case NL80211_IFTYPE_P2P_CLIENT:
2331 case NL80211_IFTYPE_STATION:
2332 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2333 BIT(NL80211_STA_FLAG_TDLS_PEER);
2334 break;
2335 case NL80211_IFTYPE_MESH_POINT:
2336 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2337 BIT(NL80211_STA_FLAG_MFP) |
2338 BIT(NL80211_STA_FLAG_AUTHORIZED);
2339 default:
2340 return -EINVAL;
2341 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002342
2343 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2344 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002345 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002346
2347 return 0;
2348}
2349
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002350static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2351 int attr)
2352{
2353 struct nlattr *rate;
2354 u16 bitrate;
2355
2356 rate = nla_nest_start(msg, attr);
2357 if (!rate)
2358 goto nla_put_failure;
2359
2360 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2361 bitrate = cfg80211_calculate_bitrate(info);
2362 if (bitrate > 0)
2363 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2364
2365 if (info->flags & RATE_INFO_FLAGS_MCS)
2366 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2367 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2368 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2369 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2370 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2371
2372 nla_nest_end(msg, rate);
2373 return true;
2374
2375nla_put_failure:
2376 return false;
2377}
2378
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002379static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2380 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002381 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002382{
2383 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002384 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002385
2386 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2387 if (!hdr)
2388 return -1;
2389
2390 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2391 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2392
Johannes Bergf5ea9122009-08-07 16:17:38 +02002393 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2394
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002395 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2396 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002397 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302398 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2399 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2400 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002401 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2402 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2403 sinfo->inactive_time);
2404 if (sinfo->filled & STATION_INFO_RX_BYTES)
2405 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2406 sinfo->rx_bytes);
2407 if (sinfo->filled & STATION_INFO_TX_BYTES)
2408 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2409 sinfo->tx_bytes);
2410 if (sinfo->filled & STATION_INFO_LLID)
2411 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2412 sinfo->llid);
2413 if (sinfo->filled & STATION_INFO_PLID)
2414 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2415 sinfo->plid);
2416 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2417 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2418 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002419 if (sinfo->filled & STATION_INFO_SIGNAL)
2420 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2421 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002422 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2423 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2424 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002425 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002426 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2427 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002428 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002429 }
2430 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2431 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2432 NL80211_STA_INFO_RX_BITRATE))
2433 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002434 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002435 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2436 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2437 sinfo->rx_packets);
2438 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2439 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2440 sinfo->tx_packets);
Bruno Randolfb206b4ef2010-10-06 18:34:12 +09002441 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2442 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2443 sinfo->tx_retries);
2444 if (sinfo->filled & STATION_INFO_TX_FAILED)
2445 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2446 sinfo->tx_failed);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002447 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2448 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2449 sinfo->beacon_loss_count);
Paul Stewartf4263c92011-03-31 09:25:41 -07002450 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2451 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2452 if (!bss_param)
2453 goto nla_put_failure;
2454
2455 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2456 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2457 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2458 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2459 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2460 NLA_PUT_FLAG(msg,
2461 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2462 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2463 sinfo->bss_param.dtim_period);
2464 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2465 sinfo->bss_param.beacon_interval);
2466
2467 nla_nest_end(msg, bss_param);
2468 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002469 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2470 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2471 sizeof(struct nl80211_sta_flag_update),
2472 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002473 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002474
Felix Fietkau040bdf72011-08-10 19:00:33 -06002475 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002476 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2477 sinfo->assoc_req_ies);
2478
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002479 return genlmsg_end(msg, hdr);
2480
2481 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002482 genlmsg_cancel(msg, hdr);
2483 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002484}
2485
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002486static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002487 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002488{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002489 struct station_info sinfo;
2490 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002491 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002492 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002493 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002494 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002495
Johannes Berg67748892010-10-04 21:14:06 +02002496 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2497 if (err)
2498 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002499
2500 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002501 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002502 goto out_err;
2503 }
2504
Johannes Bergbba95fe2008-07-29 13:22:51 +02002505 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002506 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002507 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2508 mac_addr, &sinfo);
2509 if (err == -ENOENT)
2510 break;
2511 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002512 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002513
2514 if (nl80211_send_station(skb,
2515 NETLINK_CB(cb->skb).pid,
2516 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2517 netdev, mac_addr,
2518 &sinfo) < 0)
2519 goto out;
2520
2521 sta_idx++;
2522 }
2523
2524
2525 out:
2526 cb->args[1] = sta_idx;
2527 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002528 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002529 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002530
2531 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002532}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002533
Johannes Berg5727ef12007-12-19 02:03:34 +01002534static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2535{
Johannes Berg4c476992010-10-04 21:36:35 +02002536 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2537 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002538 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002539 struct sk_buff *msg;
2540 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002541 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002542
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002543 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002544
2545 if (!info->attrs[NL80211_ATTR_MAC])
2546 return -EINVAL;
2547
2548 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2549
Johannes Berg4c476992010-10-04 21:36:35 +02002550 if (!rdev->ops->get_station)
2551 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002552
Johannes Berg79c97e92009-07-07 03:56:12 +02002553 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002554 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002555 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002556
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002557 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002558 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002559 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002560
2561 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002562 dev, mac_addr, &sinfo) < 0) {
2563 nlmsg_free(msg);
2564 return -ENOBUFS;
2565 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002566
Johannes Berg4c476992010-10-04 21:36:35 +02002567 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002568}
2569
2570/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002571 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002572 */
Johannes Berg80b99892011-11-18 16:23:01 +01002573static struct net_device *get_vlan(struct genl_info *info,
2574 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002575{
Johannes Berg463d0182009-07-14 00:33:35 +02002576 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002577 struct net_device *v;
2578 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002579
Johannes Berg80b99892011-11-18 16:23:01 +01002580 if (!vlanattr)
2581 return NULL;
2582
2583 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2584 if (!v)
2585 return ERR_PTR(-ENODEV);
2586
2587 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2588 ret = -EINVAL;
2589 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002590 }
Johannes Berg80b99892011-11-18 16:23:01 +01002591
2592 if (!netif_running(v)) {
2593 ret = -ENETDOWN;
2594 goto error;
2595 }
2596
2597 return v;
2598 error:
2599 dev_put(v);
2600 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002601}
2602
2603static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2604{
Johannes Berg4c476992010-10-04 21:36:35 +02002605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002606 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002607 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002608 struct station_parameters params;
2609 u8 *mac_addr = NULL;
2610
2611 memset(&params, 0, sizeof(params));
2612
2613 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002614 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002615
2616 if (info->attrs[NL80211_ATTR_STA_AID])
2617 return -EINVAL;
2618
2619 if (!info->attrs[NL80211_ATTR_MAC])
2620 return -EINVAL;
2621
2622 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2623
2624 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2625 params.supported_rates =
2626 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2627 params.supported_rates_len =
2628 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2629 }
2630
2631 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2632 params.listen_interval =
2633 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2634
Jouni Malinen36aedc92008-08-25 11:58:58 +03002635 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2636 params.ht_capa =
2637 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2638
Johannes Bergbdd90d52011-12-14 12:20:27 +01002639 if (!rdev->ops->change_station)
2640 return -EOPNOTSUPP;
2641
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002642 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002643 return -EINVAL;
2644
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002645 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2646 params.plink_action =
2647 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2648
Javier Cardona9c3990a2011-05-03 16:57:11 -07002649 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2650 params.plink_state =
2651 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2652
Johannes Berga97f4422009-06-18 17:23:43 +02002653 switch (dev->ieee80211_ptr->iftype) {
2654 case NL80211_IFTYPE_AP:
2655 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002656 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002657 /* disallow mesh-specific things */
2658 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002659 return -EINVAL;
2660
2661 /* TDLS can't be set, ... */
2662 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2663 return -EINVAL;
2664 /*
2665 * ... but don't bother the driver with it. This works around
2666 * a hostapd/wpa_supplicant issue -- it always includes the
2667 * TLDS_PEER flag in the mask even for AP mode.
2668 */
2669 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2670
2671 /* accept only the listed bits */
2672 if (params.sta_flags_mask &
2673 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2674 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2675 BIT(NL80211_STA_FLAG_WME) |
2676 BIT(NL80211_STA_FLAG_MFP)))
2677 return -EINVAL;
2678
2679 /* must be last in here for error handling */
2680 params.vlan = get_vlan(info, rdev);
2681 if (IS_ERR(params.vlan))
2682 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002683 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002684 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002685 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002686 /*
2687 * Don't allow userspace to change the TDLS_PEER flag,
2688 * but silently ignore attempts to change it since we
2689 * don't have state here to verify that it doesn't try
2690 * to change the flag.
2691 */
2692 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01002693 /* fall through */
2694 case NL80211_IFTYPE_ADHOC:
2695 /* disallow things sta doesn't support */
2696 if (params.plink_action)
2697 return -EINVAL;
2698 if (params.ht_capa)
2699 return -EINVAL;
2700 if (params.listen_interval >= 0)
2701 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002702 /* reject any changes other than AUTHORIZED */
2703 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2704 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002705 break;
2706 case NL80211_IFTYPE_MESH_POINT:
2707 /* disallow things mesh doesn't support */
2708 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002709 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002710 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002711 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002712 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002713 return -EINVAL;
2714 /*
2715 * No special handling for TDLS here -- the userspace
2716 * mesh code doesn't have this bug.
2717 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002718 if (params.sta_flags_mask &
2719 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002720 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002721 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002722 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002723 break;
2724 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002725 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002726 }
2727
Johannes Bergbdd90d52011-12-14 12:20:27 +01002728 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002729
Johannes Berg79c97e92009-07-07 03:56:12 +02002730 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002731
Johannes Berg5727ef12007-12-19 02:03:34 +01002732 if (params.vlan)
2733 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002734
Johannes Berg5727ef12007-12-19 02:03:34 +01002735 return err;
2736}
2737
Eliad Pellerc75786c2011-08-23 14:37:46 +03002738static struct nla_policy
2739nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2740 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2741 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2742};
2743
Johannes Berg5727ef12007-12-19 02:03:34 +01002744static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2745{
Johannes Berg4c476992010-10-04 21:36:35 +02002746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002747 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002748 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002749 struct station_parameters params;
2750 u8 *mac_addr = NULL;
2751
2752 memset(&params, 0, sizeof(params));
2753
2754 if (!info->attrs[NL80211_ATTR_MAC])
2755 return -EINVAL;
2756
Johannes Berg5727ef12007-12-19 02:03:34 +01002757 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2758 return -EINVAL;
2759
2760 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2761 return -EINVAL;
2762
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002763 if (!info->attrs[NL80211_ATTR_STA_AID])
2764 return -EINVAL;
2765
Johannes Berg5727ef12007-12-19 02:03:34 +01002766 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2767 params.supported_rates =
2768 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2769 params.supported_rates_len =
2770 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2771 params.listen_interval =
2772 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002773
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002774 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2775 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2776 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002777
Jouni Malinen36aedc92008-08-25 11:58:58 +03002778 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2779 params.ht_capa =
2780 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002781
Javier Cardona96b78df2011-04-07 15:08:33 -07002782 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2783 params.plink_action =
2784 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2785
Johannes Bergbdd90d52011-12-14 12:20:27 +01002786 if (!rdev->ops->add_station)
2787 return -EOPNOTSUPP;
2788
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002789 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002790 return -EINVAL;
2791
Johannes Bergbdd90d52011-12-14 12:20:27 +01002792 switch (dev->ieee80211_ptr->iftype) {
2793 case NL80211_IFTYPE_AP:
2794 case NL80211_IFTYPE_AP_VLAN:
2795 case NL80211_IFTYPE_P2P_GO:
2796 /* parse WME attributes if sta is WME capable */
2797 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2798 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2799 info->attrs[NL80211_ATTR_STA_WME]) {
2800 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2801 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002802
Johannes Bergbdd90d52011-12-14 12:20:27 +01002803 nla = info->attrs[NL80211_ATTR_STA_WME];
2804 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2805 nl80211_sta_wme_policy);
2806 if (err)
2807 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002808
Johannes Bergbdd90d52011-12-14 12:20:27 +01002809 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2810 params.uapsd_queues =
2811 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2812 if (params.uapsd_queues &
2813 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2814 return -EINVAL;
2815
2816 if (tb[NL80211_STA_WME_MAX_SP])
2817 params.max_sp =
2818 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2819
2820 if (params.max_sp &
2821 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2822 return -EINVAL;
2823
2824 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2825 }
2826 /* TDLS peers cannot be added */
2827 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002828 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002829 /* but don't bother the driver with it */
2830 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002831
Johannes Bergbdd90d52011-12-14 12:20:27 +01002832 /* must be last in here for error handling */
2833 params.vlan = get_vlan(info, rdev);
2834 if (IS_ERR(params.vlan))
2835 return PTR_ERR(params.vlan);
2836 break;
2837 case NL80211_IFTYPE_MESH_POINT:
2838 /* TDLS peers cannot be added */
2839 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002840 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002841 break;
2842 case NL80211_IFTYPE_STATION:
2843 /* Only TDLS peers can be added */
2844 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2845 return -EINVAL;
2846 /* Can only add if TDLS ... */
2847 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2848 return -EOPNOTSUPP;
2849 /* ... with external setup is supported */
2850 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2851 return -EOPNOTSUPP;
2852 break;
2853 default:
2854 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002855 }
2856
Johannes Bergbdd90d52011-12-14 12:20:27 +01002857 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002858
Johannes Berg79c97e92009-07-07 03:56:12 +02002859 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002860
Johannes Berg5727ef12007-12-19 02:03:34 +01002861 if (params.vlan)
2862 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002863 return err;
2864}
2865
2866static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2867{
Johannes Berg4c476992010-10-04 21:36:35 +02002868 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2869 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002870 u8 *mac_addr = NULL;
2871
2872 if (info->attrs[NL80211_ATTR_MAC])
2873 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2874
Johannes Berge80cf852009-05-11 14:43:13 +02002875 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002876 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002877 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002878 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2879 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002880
Johannes Berg4c476992010-10-04 21:36:35 +02002881 if (!rdev->ops->del_station)
2882 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002883
Johannes Berg4c476992010-10-04 21:36:35 +02002884 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002885}
2886
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002887static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2888 int flags, struct net_device *dev,
2889 u8 *dst, u8 *next_hop,
2890 struct mpath_info *pinfo)
2891{
2892 void *hdr;
2893 struct nlattr *pinfoattr;
2894
2895 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2896 if (!hdr)
2897 return -1;
2898
2899 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2900 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2901 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2902
Johannes Bergf5ea9122009-08-07 16:17:38 +02002903 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2904
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002905 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2906 if (!pinfoattr)
2907 goto nla_put_failure;
2908 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2909 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2910 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002911 if (pinfo->filled & MPATH_INFO_SN)
2912 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2913 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002914 if (pinfo->filled & MPATH_INFO_METRIC)
2915 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2916 pinfo->metric);
2917 if (pinfo->filled & MPATH_INFO_EXPTIME)
2918 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2919 pinfo->exptime);
2920 if (pinfo->filled & MPATH_INFO_FLAGS)
2921 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2922 pinfo->flags);
2923 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2924 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2925 pinfo->discovery_timeout);
2926 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2927 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2928 pinfo->discovery_retries);
2929
2930 nla_nest_end(msg, pinfoattr);
2931
2932 return genlmsg_end(msg, hdr);
2933
2934 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002935 genlmsg_cancel(msg, hdr);
2936 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002937}
2938
2939static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002940 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002941{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002942 struct mpath_info pinfo;
2943 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002944 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002945 u8 dst[ETH_ALEN];
2946 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002947 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002948 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002949
Johannes Berg67748892010-10-04 21:14:06 +02002950 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2951 if (err)
2952 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002953
2954 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002955 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002956 goto out_err;
2957 }
2958
Jouni Malineneec60b02009-03-20 21:21:19 +02002959 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2960 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002961 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002962 }
2963
Johannes Bergbba95fe2008-07-29 13:22:51 +02002964 while (1) {
2965 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2966 dst, next_hop, &pinfo);
2967 if (err == -ENOENT)
2968 break;
2969 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002970 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002971
2972 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2973 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2974 netdev, dst, next_hop,
2975 &pinfo) < 0)
2976 goto out;
2977
2978 path_idx++;
2979 }
2980
2981
2982 out:
2983 cb->args[1] = path_idx;
2984 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002985 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002986 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002987 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002988}
2989
2990static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2991{
Johannes Berg4c476992010-10-04 21:36:35 +02002992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002993 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002994 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002995 struct mpath_info pinfo;
2996 struct sk_buff *msg;
2997 u8 *dst = NULL;
2998 u8 next_hop[ETH_ALEN];
2999
3000 memset(&pinfo, 0, sizeof(pinfo));
3001
3002 if (!info->attrs[NL80211_ATTR_MAC])
3003 return -EINVAL;
3004
3005 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3006
Johannes Berg4c476992010-10-04 21:36:35 +02003007 if (!rdev->ops->get_mpath)
3008 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003009
Johannes Berg4c476992010-10-04 21:36:35 +02003010 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3011 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003012
Johannes Berg79c97e92009-07-07 03:56:12 +02003013 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003014 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003015 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003016
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003017 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003018 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003019 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003020
3021 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003022 dev, dst, next_hop, &pinfo) < 0) {
3023 nlmsg_free(msg);
3024 return -ENOBUFS;
3025 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003026
Johannes Berg4c476992010-10-04 21:36:35 +02003027 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003028}
3029
3030static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3031{
Johannes Berg4c476992010-10-04 21:36:35 +02003032 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3033 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003034 u8 *dst = NULL;
3035 u8 *next_hop = NULL;
3036
3037 if (!info->attrs[NL80211_ATTR_MAC])
3038 return -EINVAL;
3039
3040 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3041 return -EINVAL;
3042
3043 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3044 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3045
Johannes Berg4c476992010-10-04 21:36:35 +02003046 if (!rdev->ops->change_mpath)
3047 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003048
Johannes Berg4c476992010-10-04 21:36:35 +02003049 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3050 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003051
Johannes Berg4c476992010-10-04 21:36:35 +02003052 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003053}
Johannes Berg4c476992010-10-04 21:36:35 +02003054
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003055static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3056{
Johannes Berg4c476992010-10-04 21:36:35 +02003057 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3058 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003059 u8 *dst = NULL;
3060 u8 *next_hop = NULL;
3061
3062 if (!info->attrs[NL80211_ATTR_MAC])
3063 return -EINVAL;
3064
3065 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3066 return -EINVAL;
3067
3068 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3069 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3070
Johannes Berg4c476992010-10-04 21:36:35 +02003071 if (!rdev->ops->add_mpath)
3072 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003073
Johannes Berg4c476992010-10-04 21:36:35 +02003074 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3075 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003076
Johannes Berg4c476992010-10-04 21:36:35 +02003077 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003078}
3079
3080static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3081{
Johannes Berg4c476992010-10-04 21:36:35 +02003082 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3083 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003084 u8 *dst = NULL;
3085
3086 if (info->attrs[NL80211_ATTR_MAC])
3087 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3088
Johannes Berg4c476992010-10-04 21:36:35 +02003089 if (!rdev->ops->del_mpath)
3090 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003091
Johannes Berg4c476992010-10-04 21:36:35 +02003092 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003093}
3094
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003095static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3096{
Johannes Berg4c476992010-10-04 21:36:35 +02003097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3098 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003099 struct bss_parameters params;
3100
3101 memset(&params, 0, sizeof(params));
3102 /* default to not changing parameters */
3103 params.use_cts_prot = -1;
3104 params.use_short_preamble = -1;
3105 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003106 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003107 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003108
3109 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3110 params.use_cts_prot =
3111 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3112 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3113 params.use_short_preamble =
3114 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3115 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3116 params.use_short_slot_time =
3117 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003118 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3119 params.basic_rates =
3120 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3121 params.basic_rates_len =
3122 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3123 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003124 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3125 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003126 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3127 params.ht_opmode =
3128 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003129
Johannes Berg4c476992010-10-04 21:36:35 +02003130 if (!rdev->ops->change_bss)
3131 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003132
Johannes Berg074ac8d2010-09-16 14:58:22 +02003133 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003134 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3135 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003136
Johannes Berg4c476992010-10-04 21:36:35 +02003137 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003138}
3139
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003140static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003141 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3142 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3143 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3144 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3145 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3146 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3147};
3148
3149static int parse_reg_rule(struct nlattr *tb[],
3150 struct ieee80211_reg_rule *reg_rule)
3151{
3152 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3153 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3154
3155 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3156 return -EINVAL;
3157 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3158 return -EINVAL;
3159 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3160 return -EINVAL;
3161 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3162 return -EINVAL;
3163 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3164 return -EINVAL;
3165
3166 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3167
3168 freq_range->start_freq_khz =
3169 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3170 freq_range->end_freq_khz =
3171 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3172 freq_range->max_bandwidth_khz =
3173 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3174
3175 power_rule->max_eirp =
3176 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3177
3178 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3179 power_rule->max_antenna_gain =
3180 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3181
3182 return 0;
3183}
3184
3185static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3186{
3187 int r;
3188 char *data = NULL;
3189
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003190 /*
3191 * You should only get this when cfg80211 hasn't yet initialized
3192 * completely when built-in to the kernel right between the time
3193 * window between nl80211_init() and regulatory_init(), if that is
3194 * even possible.
3195 */
3196 mutex_lock(&cfg80211_mutex);
3197 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003198 mutex_unlock(&cfg80211_mutex);
3199 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003200 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003201 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003202
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003203 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3204 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003205
3206 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3207
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003208 r = regulatory_hint_user(data);
3209
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003210 return r;
3211}
3212
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003213static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003214 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003215{
Johannes Berg4c476992010-10-04 21:36:35 +02003216 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003217 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003218 struct wireless_dev *wdev = dev->ieee80211_ptr;
3219 struct mesh_config cur_params;
3220 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003221 void *hdr;
3222 struct nlattr *pinfoattr;
3223 struct sk_buff *msg;
3224
Johannes Berg29cbe682010-12-03 09:20:44 +01003225 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3226 return -EOPNOTSUPP;
3227
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003228 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003229 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003230
Johannes Berg29cbe682010-12-03 09:20:44 +01003231 wdev_lock(wdev);
3232 /* If not connected, get default parameters */
3233 if (!wdev->mesh_id_len)
3234 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3235 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003236 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003237 &cur_params);
3238 wdev_unlock(wdev);
3239
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003240 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003241 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003242
3243 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003245 if (!msg)
3246 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003247 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003248 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003249 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003250 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003251 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003252 if (!pinfoattr)
3253 goto nla_put_failure;
3254 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3255 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3256 cur_params.dot11MeshRetryTimeout);
3257 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3258 cur_params.dot11MeshConfirmTimeout);
3259 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3260 cur_params.dot11MeshHoldingTimeout);
3261 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3262 cur_params.dot11MeshMaxPeerLinks);
3263 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3264 cur_params.dot11MeshMaxRetries);
3265 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3266 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003267 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3268 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003269 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3270 cur_params.auto_open_plinks);
3271 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3272 cur_params.dot11MeshHWMPmaxPREQretries);
3273 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3274 cur_params.path_refresh_time);
3275 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3276 cur_params.min_discovery_timeout);
3277 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3278 cur_params.dot11MeshHWMPactivePathTimeout);
3279 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3280 cur_params.dot11MeshHWMPpreqMinInterval);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003281 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3282 cur_params.dot11MeshHWMPperrMinInterval);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003283 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3284 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003285 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3286 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003287 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3288 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003289 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3290 cur_params.dot11MeshGateAnnouncementProtocol);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003291 NLA_PUT_U8(msg, NL80211_MESHCONF_FORWARDING,
3292 cur_params.dot11MeshForwarding);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003293 NLA_PUT_U32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3294 cur_params.rssi_threshold);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003295 nla_nest_end(msg, pinfoattr);
3296 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003297 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003298
Johannes Berg3b858752009-03-12 09:55:09 +01003299 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003300 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003301 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003302 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003303 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003304}
3305
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003306static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003307 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3308 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3309 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3310 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3311 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3312 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003313 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003314 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3315
3316 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3317 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3318 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3319 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3320 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003321 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003322 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003323 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003324 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003325 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003326 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003327 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003328};
3329
Javier Cardonac80d5452010-12-16 17:37:49 -08003330static const struct nla_policy
3331 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3332 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3333 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003334 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003335 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003336 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003337 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003338};
3339
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003340static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003341 struct mesh_config *cfg,
3342 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003343{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003344 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003345 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003346
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003347#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3348do {\
3349 if (table[attr_num]) {\
3350 cfg->param = nla_fn(table[attr_num]); \
3351 mask |= (1 << (attr_num - 1)); \
3352 } \
3353} while (0);\
3354
3355
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003356 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003357 return -EINVAL;
3358 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003359 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003360 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003361 return -EINVAL;
3362
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003363 /* This makes sure that there aren't more than 32 mesh config
3364 * parameters (otherwise our bitfield scheme would not work.) */
3365 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3366
3367 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003368 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3369 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3370 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3371 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3372 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3373 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3374 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3375 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3376 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3377 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3378 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3379 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003380 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3381 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003382 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3383 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3384 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3385 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3386 nla_get_u8);
3387 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3388 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3389 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3390 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3391 nla_get_u16);
3392 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3393 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3394 nla_get_u32);
3395 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3396 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3397 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003398 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3399 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3400 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003401 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3402 dot11MeshHWMPnetDiameterTraversalTime,
3403 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3404 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003405 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3406 dot11MeshHWMPRootMode, mask,
3407 NL80211_MESHCONF_HWMP_ROOTMODE,
3408 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003409 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3410 dot11MeshHWMPRannInterval, mask,
3411 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3412 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003413 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3414 dot11MeshGateAnnouncementProtocol, mask,
3415 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3416 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003417 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3418 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003419 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3420 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003421 if (mask_out)
3422 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003423
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003424 return 0;
3425
3426#undef FILL_IN_MESH_PARAM_IF_SET
3427}
3428
Javier Cardonac80d5452010-12-16 17:37:49 -08003429static int nl80211_parse_mesh_setup(struct genl_info *info,
3430 struct mesh_setup *setup)
3431{
3432 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3433
3434 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3435 return -EINVAL;
3436 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3437 info->attrs[NL80211_ATTR_MESH_SETUP],
3438 nl80211_mesh_setup_params_policy))
3439 return -EINVAL;
3440
3441 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3442 setup->path_sel_proto =
3443 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3444 IEEE80211_PATH_PROTOCOL_VENDOR :
3445 IEEE80211_PATH_PROTOCOL_HWMP;
3446
3447 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3448 setup->path_metric =
3449 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3450 IEEE80211_PATH_METRIC_VENDOR :
3451 IEEE80211_PATH_METRIC_AIRTIME;
3452
Javier Cardona581a8b02011-04-07 15:08:27 -07003453
3454 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003455 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003456 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003457 if (!is_valid_ie_attr(ieattr))
3458 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003459 setup->ie = nla_data(ieattr);
3460 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003461 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003462 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3463 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003464
3465 return 0;
3466}
3467
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003468static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003469 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003470{
3471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3472 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003473 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003474 struct mesh_config cfg;
3475 u32 mask;
3476 int err;
3477
Johannes Berg29cbe682010-12-03 09:20:44 +01003478 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3479 return -EOPNOTSUPP;
3480
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003481 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003482 return -EOPNOTSUPP;
3483
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003484 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003485 if (err)
3486 return err;
3487
Johannes Berg29cbe682010-12-03 09:20:44 +01003488 wdev_lock(wdev);
3489 if (!wdev->mesh_id_len)
3490 err = -ENOLINK;
3491
3492 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003493 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003494 mask, &cfg);
3495
3496 wdev_unlock(wdev);
3497
3498 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003499}
3500
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003501static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3502{
3503 struct sk_buff *msg;
3504 void *hdr = NULL;
3505 struct nlattr *nl_reg_rules;
3506 unsigned int i;
3507 int err = -EINVAL;
3508
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003509 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003510
3511 if (!cfg80211_regdomain)
3512 goto out;
3513
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003515 if (!msg) {
3516 err = -ENOBUFS;
3517 goto out;
3518 }
3519
3520 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3521 NL80211_CMD_GET_REG);
3522 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003523 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003524
3525 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3526 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003527 if (cfg80211_regdomain->dfs_region)
3528 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3529 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003530
3531 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3532 if (!nl_reg_rules)
3533 goto nla_put_failure;
3534
3535 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3536 struct nlattr *nl_reg_rule;
3537 const struct ieee80211_reg_rule *reg_rule;
3538 const struct ieee80211_freq_range *freq_range;
3539 const struct ieee80211_power_rule *power_rule;
3540
3541 reg_rule = &cfg80211_regdomain->reg_rules[i];
3542 freq_range = &reg_rule->freq_range;
3543 power_rule = &reg_rule->power_rule;
3544
3545 nl_reg_rule = nla_nest_start(msg, i);
3546 if (!nl_reg_rule)
3547 goto nla_put_failure;
3548
3549 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3550 reg_rule->flags);
3551 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3552 freq_range->start_freq_khz);
3553 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3554 freq_range->end_freq_khz);
3555 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3556 freq_range->max_bandwidth_khz);
3557 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3558 power_rule->max_antenna_gain);
3559 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3560 power_rule->max_eirp);
3561
3562 nla_nest_end(msg, nl_reg_rule);
3563 }
3564
3565 nla_nest_end(msg, nl_reg_rules);
3566
3567 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003568 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003569 goto out;
3570
3571nla_put_failure:
3572 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003573put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003574 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003575 err = -EMSGSIZE;
3576out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003577 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003578 return err;
3579}
3580
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003581static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3582{
3583 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3584 struct nlattr *nl_reg_rule;
3585 char *alpha2 = NULL;
3586 int rem_reg_rules = 0, r = 0;
3587 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003588 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003589 struct ieee80211_regdomain *rd = NULL;
3590
3591 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3592 return -EINVAL;
3593
3594 if (!info->attrs[NL80211_ATTR_REG_RULES])
3595 return -EINVAL;
3596
3597 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3598
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003599 if (info->attrs[NL80211_ATTR_DFS_REGION])
3600 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3601
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003602 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3603 rem_reg_rules) {
3604 num_rules++;
3605 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003606 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003607 }
3608
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003609 mutex_lock(&cfg80211_mutex);
3610
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003611 if (!reg_is_valid_request(alpha2)) {
3612 r = -EINVAL;
3613 goto bad_reg;
3614 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003615
3616 size_of_regd = sizeof(struct ieee80211_regdomain) +
3617 (num_rules * sizeof(struct ieee80211_reg_rule));
3618
3619 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003620 if (!rd) {
3621 r = -ENOMEM;
3622 goto bad_reg;
3623 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003624
3625 rd->n_reg_rules = num_rules;
3626 rd->alpha2[0] = alpha2[0];
3627 rd->alpha2[1] = alpha2[1];
3628
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003629 /*
3630 * Disable DFS master mode if the DFS region was
3631 * not supported or known on this kernel.
3632 */
3633 if (reg_supported_dfs_region(dfs_region))
3634 rd->dfs_region = dfs_region;
3635
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003636 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3637 rem_reg_rules) {
3638 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3639 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3640 reg_rule_policy);
3641 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3642 if (r)
3643 goto bad_reg;
3644
3645 rule_idx++;
3646
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003647 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3648 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003649 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003650 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003651 }
3652
3653 BUG_ON(rule_idx != num_rules);
3654
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003655 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003656
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003657 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003658
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003659 return r;
3660
Johannes Bergd2372b32008-10-24 20:32:20 +02003661 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003662 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003663 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003664 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003665}
3666
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003667static int validate_scan_freqs(struct nlattr *freqs)
3668{
3669 struct nlattr *attr1, *attr2;
3670 int n_channels = 0, tmp1, tmp2;
3671
3672 nla_for_each_nested(attr1, freqs, tmp1) {
3673 n_channels++;
3674 /*
3675 * Some hardware has a limited channel list for
3676 * scanning, and it is pretty much nonsensical
3677 * to scan for a channel twice, so disallow that
3678 * and don't require drivers to check that the
3679 * channel list they get isn't longer than what
3680 * they can scan, as long as they can scan all
3681 * the channels they registered at once.
3682 */
3683 nla_for_each_nested(attr2, freqs, tmp2)
3684 if (attr1 != attr2 &&
3685 nla_get_u32(attr1) == nla_get_u32(attr2))
3686 return 0;
3687 }
3688
3689 return n_channels;
3690}
3691
Johannes Berg2a519312009-02-10 21:25:55 +01003692static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3693{
Johannes Berg4c476992010-10-04 21:36:35 +02003694 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3695 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003696 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003697 struct nlattr *attr;
3698 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003699 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003700 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003701
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003702 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3703 return -EINVAL;
3704
Johannes Berg79c97e92009-07-07 03:56:12 +02003705 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003706
Johannes Berg4c476992010-10-04 21:36:35 +02003707 if (!rdev->ops->scan)
3708 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003709
Johannes Berg4c476992010-10-04 21:36:35 +02003710 if (rdev->scan_req)
3711 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003712
3713 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003714 n_channels = validate_scan_freqs(
3715 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003716 if (!n_channels)
3717 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003718 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003719 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003720 n_channels = 0;
3721
Johannes Berg2a519312009-02-10 21:25:55 +01003722 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3723 if (wiphy->bands[band])
3724 n_channels += wiphy->bands[band]->n_channels;
3725 }
3726
3727 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3728 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3729 n_ssids++;
3730
Johannes Berg4c476992010-10-04 21:36:35 +02003731 if (n_ssids > wiphy->max_scan_ssids)
3732 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003733
Jouni Malinen70692ad2009-02-16 19:39:13 +02003734 if (info->attrs[NL80211_ATTR_IE])
3735 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3736 else
3737 ie_len = 0;
3738
Johannes Berg4c476992010-10-04 21:36:35 +02003739 if (ie_len > wiphy->max_scan_ie_len)
3740 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003741
Johannes Berg2a519312009-02-10 21:25:55 +01003742 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003743 + sizeof(*request->ssids) * n_ssids
3744 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003745 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003746 if (!request)
3747 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003748
Johannes Berg2a519312009-02-10 21:25:55 +01003749 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003750 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003751 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003752 if (ie_len) {
3753 if (request->ssids)
3754 request->ie = (void *)(request->ssids + n_ssids);
3755 else
3756 request->ie = (void *)(request->channels + n_channels);
3757 }
Johannes Berg2a519312009-02-10 21:25:55 +01003758
Johannes Berg584991d2009-11-02 13:32:03 +01003759 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003760 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3761 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003762 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003763 struct ieee80211_channel *chan;
3764
3765 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3766
3767 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003768 err = -EINVAL;
3769 goto out_free;
3770 }
Johannes Berg584991d2009-11-02 13:32:03 +01003771
3772 /* ignore disabled channels */
3773 if (chan->flags & IEEE80211_CHAN_DISABLED)
3774 continue;
3775
3776 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003777 i++;
3778 }
3779 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003780 enum ieee80211_band band;
3781
Johannes Berg2a519312009-02-10 21:25:55 +01003782 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003783 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3784 int j;
3785 if (!wiphy->bands[band])
3786 continue;
3787 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003788 struct ieee80211_channel *chan;
3789
3790 chan = &wiphy->bands[band]->channels[j];
3791
3792 if (chan->flags & IEEE80211_CHAN_DISABLED)
3793 continue;
3794
3795 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003796 i++;
3797 }
3798 }
3799 }
3800
Johannes Berg584991d2009-11-02 13:32:03 +01003801 if (!i) {
3802 err = -EINVAL;
3803 goto out_free;
3804 }
3805
3806 request->n_channels = i;
3807
Johannes Berg2a519312009-02-10 21:25:55 +01003808 i = 0;
3809 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3810 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003811 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003812 err = -EINVAL;
3813 goto out_free;
3814 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003815 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003816 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003817 i++;
3818 }
3819 }
3820
Jouni Malinen70692ad2009-02-16 19:39:13 +02003821 if (info->attrs[NL80211_ATTR_IE]) {
3822 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003823 memcpy((void *)request->ie,
3824 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003825 request->ie_len);
3826 }
3827
Johannes Berg34850ab2011-07-18 18:08:35 +02003828 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003829 if (wiphy->bands[i])
3830 request->rates[i] =
3831 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003832
3833 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3834 nla_for_each_nested(attr,
3835 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3836 tmp) {
3837 enum ieee80211_band band = nla_type(attr);
3838
Dan Carpenter84404622011-07-29 11:52:18 +03003839 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003840 err = -EINVAL;
3841 goto out_free;
3842 }
3843 err = ieee80211_get_ratemask(wiphy->bands[band],
3844 nla_data(attr),
3845 nla_len(attr),
3846 &request->rates[band]);
3847 if (err)
3848 goto out_free;
3849 }
3850 }
3851
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303852 request->no_cck =
3853 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3854
Johannes Berg463d0182009-07-14 00:33:35 +02003855 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003856 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003857
Johannes Berg79c97e92009-07-07 03:56:12 +02003858 rdev->scan_req = request;
3859 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003860
Johannes Berg463d0182009-07-14 00:33:35 +02003861 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003862 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003863 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003864 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003865 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003866 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003867 kfree(request);
3868 }
Johannes Berg3b858752009-03-12 09:55:09 +01003869
Johannes Berg2a519312009-02-10 21:25:55 +01003870 return err;
3871}
3872
Luciano Coelho807f8a82011-05-11 17:09:35 +03003873static int nl80211_start_sched_scan(struct sk_buff *skb,
3874 struct genl_info *info)
3875{
3876 struct cfg80211_sched_scan_request *request;
3877 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3878 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003879 struct nlattr *attr;
3880 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003881 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003882 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003883 enum ieee80211_band band;
3884 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003885 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003886
3887 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3888 !rdev->ops->sched_scan_start)
3889 return -EOPNOTSUPP;
3890
3891 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3892 return -EINVAL;
3893
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003894 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3895 return -EINVAL;
3896
3897 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3898 if (interval == 0)
3899 return -EINVAL;
3900
Luciano Coelho807f8a82011-05-11 17:09:35 +03003901 wiphy = &rdev->wiphy;
3902
3903 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3904 n_channels = validate_scan_freqs(
3905 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3906 if (!n_channels)
3907 return -EINVAL;
3908 } else {
3909 n_channels = 0;
3910
3911 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3912 if (wiphy->bands[band])
3913 n_channels += wiphy->bands[band]->n_channels;
3914 }
3915
3916 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3917 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3918 tmp)
3919 n_ssids++;
3920
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003921 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003922 return -EINVAL;
3923
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003924 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3925 nla_for_each_nested(attr,
3926 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3927 tmp)
3928 n_match_sets++;
3929
3930 if (n_match_sets > wiphy->max_match_sets)
3931 return -EINVAL;
3932
Luciano Coelho807f8a82011-05-11 17:09:35 +03003933 if (info->attrs[NL80211_ATTR_IE])
3934 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3935 else
3936 ie_len = 0;
3937
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003938 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003939 return -EINVAL;
3940
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003941 mutex_lock(&rdev->sched_scan_mtx);
3942
3943 if (rdev->sched_scan_req) {
3944 err = -EINPROGRESS;
3945 goto out;
3946 }
3947
Luciano Coelho807f8a82011-05-11 17:09:35 +03003948 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003949 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003950 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003951 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003952 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003953 if (!request) {
3954 err = -ENOMEM;
3955 goto out;
3956 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003957
3958 if (n_ssids)
3959 request->ssids = (void *)&request->channels[n_channels];
3960 request->n_ssids = n_ssids;
3961 if (ie_len) {
3962 if (request->ssids)
3963 request->ie = (void *)(request->ssids + n_ssids);
3964 else
3965 request->ie = (void *)(request->channels + n_channels);
3966 }
3967
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003968 if (n_match_sets) {
3969 if (request->ie)
3970 request->match_sets = (void *)(request->ie + ie_len);
3971 else if (request->ssids)
3972 request->match_sets =
3973 (void *)(request->ssids + n_ssids);
3974 else
3975 request->match_sets =
3976 (void *)(request->channels + n_channels);
3977 }
3978 request->n_match_sets = n_match_sets;
3979
Luciano Coelho807f8a82011-05-11 17:09:35 +03003980 i = 0;
3981 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3982 /* user specified, bail out if channel not found */
3983 nla_for_each_nested(attr,
3984 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3985 tmp) {
3986 struct ieee80211_channel *chan;
3987
3988 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3989
3990 if (!chan) {
3991 err = -EINVAL;
3992 goto out_free;
3993 }
3994
3995 /* ignore disabled channels */
3996 if (chan->flags & IEEE80211_CHAN_DISABLED)
3997 continue;
3998
3999 request->channels[i] = chan;
4000 i++;
4001 }
4002 } else {
4003 /* all channels */
4004 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4005 int j;
4006 if (!wiphy->bands[band])
4007 continue;
4008 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4009 struct ieee80211_channel *chan;
4010
4011 chan = &wiphy->bands[band]->channels[j];
4012
4013 if (chan->flags & IEEE80211_CHAN_DISABLED)
4014 continue;
4015
4016 request->channels[i] = chan;
4017 i++;
4018 }
4019 }
4020 }
4021
4022 if (!i) {
4023 err = -EINVAL;
4024 goto out_free;
4025 }
4026
4027 request->n_channels = i;
4028
4029 i = 0;
4030 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4031 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4032 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004033 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004034 err = -EINVAL;
4035 goto out_free;
4036 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004037 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004038 memcpy(request->ssids[i].ssid, nla_data(attr),
4039 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004040 i++;
4041 }
4042 }
4043
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004044 i = 0;
4045 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4046 nla_for_each_nested(attr,
4047 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4048 tmp) {
4049 struct nlattr *ssid;
4050
4051 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4052 nla_data(attr), nla_len(attr),
4053 nl80211_match_policy);
4054 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4055 if (ssid) {
4056 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4057 err = -EINVAL;
4058 goto out_free;
4059 }
4060 memcpy(request->match_sets[i].ssid.ssid,
4061 nla_data(ssid), nla_len(ssid));
4062 request->match_sets[i].ssid.ssid_len =
4063 nla_len(ssid);
4064 }
4065 i++;
4066 }
4067 }
4068
Luciano Coelho807f8a82011-05-11 17:09:35 +03004069 if (info->attrs[NL80211_ATTR_IE]) {
4070 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4071 memcpy((void *)request->ie,
4072 nla_data(info->attrs[NL80211_ATTR_IE]),
4073 request->ie_len);
4074 }
4075
4076 request->dev = dev;
4077 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004078 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004079
4080 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4081 if (!err) {
4082 rdev->sched_scan_req = request;
4083 nl80211_send_sched_scan(rdev, dev,
4084 NL80211_CMD_START_SCHED_SCAN);
4085 goto out;
4086 }
4087
4088out_free:
4089 kfree(request);
4090out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004091 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004092 return err;
4093}
4094
4095static int nl80211_stop_sched_scan(struct sk_buff *skb,
4096 struct genl_info *info)
4097{
4098 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004099 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004100
4101 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4102 !rdev->ops->sched_scan_stop)
4103 return -EOPNOTSUPP;
4104
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004105 mutex_lock(&rdev->sched_scan_mtx);
4106 err = __cfg80211_stop_sched_scan(rdev, false);
4107 mutex_unlock(&rdev->sched_scan_mtx);
4108
4109 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004110}
4111
Johannes Berg9720bb32011-06-21 09:45:33 +02004112static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4113 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004114 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004115 struct wireless_dev *wdev,
4116 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004117{
Johannes Berg48ab9052009-07-10 18:42:31 +02004118 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004119 void *hdr;
4120 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004121
4122 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004123
Johannes Berg9720bb32011-06-21 09:45:33 +02004124 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004125 NL80211_CMD_NEW_SCAN_RESULTS);
4126 if (!hdr)
4127 return -1;
4128
Johannes Berg9720bb32011-06-21 09:45:33 +02004129 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4130
Johannes Bergf5ea9122009-08-07 16:17:38 +02004131 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02004132 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01004133
4134 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4135 if (!bss)
4136 goto nla_put_failure;
4137 if (!is_zero_ether_addr(res->bssid))
4138 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4139 if (res->information_elements && res->len_information_elements)
4140 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4141 res->len_information_elements,
4142 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004143 if (res->beacon_ies && res->len_beacon_ies &&
4144 res->beacon_ies != res->information_elements)
4145 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4146 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004147 if (res->tsf)
4148 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4149 if (res->beacon_interval)
4150 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4151 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4152 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004153 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4154 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004155
Johannes Berg77965c92009-02-18 18:45:06 +01004156 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004157 case CFG80211_SIGNAL_TYPE_MBM:
4158 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4159 break;
4160 case CFG80211_SIGNAL_TYPE_UNSPEC:
4161 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4162 break;
4163 default:
4164 break;
4165 }
4166
Johannes Berg48ab9052009-07-10 18:42:31 +02004167 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004168 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004169 case NL80211_IFTYPE_STATION:
4170 if (intbss == wdev->current_bss)
4171 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4172 NL80211_BSS_STATUS_ASSOCIATED);
Johannes Berg48ab9052009-07-10 18:42:31 +02004173 break;
4174 case NL80211_IFTYPE_ADHOC:
4175 if (intbss == wdev->current_bss)
4176 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4177 NL80211_BSS_STATUS_IBSS_JOINED);
4178 break;
4179 default:
4180 break;
4181 }
4182
Johannes Berg2a519312009-02-10 21:25:55 +01004183 nla_nest_end(msg, bss);
4184
4185 return genlmsg_end(msg, hdr);
4186
4187 nla_put_failure:
4188 genlmsg_cancel(msg, hdr);
4189 return -EMSGSIZE;
4190}
4191
4192static int nl80211_dump_scan(struct sk_buff *skb,
4193 struct netlink_callback *cb)
4194{
Johannes Berg48ab9052009-07-10 18:42:31 +02004195 struct cfg80211_registered_device *rdev;
4196 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004197 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004198 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004199 int start = cb->args[1], idx = 0;
4200 int err;
4201
Johannes Berg67748892010-10-04 21:14:06 +02004202 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4203 if (err)
4204 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004205
Johannes Berg48ab9052009-07-10 18:42:31 +02004206 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004207
Johannes Berg48ab9052009-07-10 18:42:31 +02004208 wdev_lock(wdev);
4209 spin_lock_bh(&rdev->bss_lock);
4210 cfg80211_bss_expire(rdev);
4211
Johannes Berg9720bb32011-06-21 09:45:33 +02004212 cb->seq = rdev->bss_generation;
4213
Johannes Berg48ab9052009-07-10 18:42:31 +02004214 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004215 if (++idx <= start)
4216 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004217 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004218 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004219 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004220 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004221 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004222 }
4223 }
4224
Johannes Berg48ab9052009-07-10 18:42:31 +02004225 spin_unlock_bh(&rdev->bss_lock);
4226 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004227
4228 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004229 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004230
Johannes Berg67748892010-10-04 21:14:06 +02004231 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004232}
4233
Holger Schurig61fa7132009-11-11 12:25:40 +01004234static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4235 int flags, struct net_device *dev,
4236 struct survey_info *survey)
4237{
4238 void *hdr;
4239 struct nlattr *infoattr;
4240
Holger Schurig61fa7132009-11-11 12:25:40 +01004241 hdr = nl80211hdr_put(msg, pid, seq, flags,
4242 NL80211_CMD_NEW_SURVEY_RESULTS);
4243 if (!hdr)
4244 return -ENOMEM;
4245
4246 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4247
4248 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4249 if (!infoattr)
4250 goto nla_put_failure;
4251
4252 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4253 survey->channel->center_freq);
4254 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4255 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4256 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004257 if (survey->filled & SURVEY_INFO_IN_USE)
4258 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004259 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4260 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4261 survey->channel_time);
4262 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4263 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4264 survey->channel_time_busy);
4265 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4266 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4267 survey->channel_time_ext_busy);
4268 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4269 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4270 survey->channel_time_rx);
4271 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4272 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4273 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004274
4275 nla_nest_end(msg, infoattr);
4276
4277 return genlmsg_end(msg, hdr);
4278
4279 nla_put_failure:
4280 genlmsg_cancel(msg, hdr);
4281 return -EMSGSIZE;
4282}
4283
4284static int nl80211_dump_survey(struct sk_buff *skb,
4285 struct netlink_callback *cb)
4286{
4287 struct survey_info survey;
4288 struct cfg80211_registered_device *dev;
4289 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004290 int survey_idx = cb->args[1];
4291 int res;
4292
Johannes Berg67748892010-10-04 21:14:06 +02004293 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4294 if (res)
4295 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004296
4297 if (!dev->ops->dump_survey) {
4298 res = -EOPNOTSUPP;
4299 goto out_err;
4300 }
4301
4302 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004303 struct ieee80211_channel *chan;
4304
Holger Schurig61fa7132009-11-11 12:25:40 +01004305 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4306 &survey);
4307 if (res == -ENOENT)
4308 break;
4309 if (res)
4310 goto out_err;
4311
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004312 /* Survey without a channel doesn't make sense */
4313 if (!survey.channel) {
4314 res = -EINVAL;
4315 goto out;
4316 }
4317
4318 chan = ieee80211_get_channel(&dev->wiphy,
4319 survey.channel->center_freq);
4320 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4321 survey_idx++;
4322 continue;
4323 }
4324
Holger Schurig61fa7132009-11-11 12:25:40 +01004325 if (nl80211_send_survey(skb,
4326 NETLINK_CB(cb->skb).pid,
4327 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4328 netdev,
4329 &survey) < 0)
4330 goto out;
4331 survey_idx++;
4332 }
4333
4334 out:
4335 cb->args[1] = survey_idx;
4336 res = skb->len;
4337 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004338 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004339 return res;
4340}
4341
Jouni Malinen255e7372009-03-20 21:21:17 +02004342static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4343{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004344 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004345}
4346
Samuel Ortizb23aa672009-07-01 21:26:54 +02004347static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4348{
4349 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4350 NL80211_WPA_VERSION_2));
4351}
4352
Jouni Malinen636a5d32009-03-19 13:39:22 +02004353static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4354{
Johannes Berg4c476992010-10-04 21:36:35 +02004355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4356 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004357 struct ieee80211_channel *chan;
4358 const u8 *bssid, *ssid, *ie = NULL;
4359 int err, ssid_len, ie_len = 0;
4360 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004361 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004362 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004363
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004364 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4365 return -EINVAL;
4366
4367 if (!info->attrs[NL80211_ATTR_MAC])
4368 return -EINVAL;
4369
Jouni Malinen17780922009-03-27 20:52:47 +02004370 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4371 return -EINVAL;
4372
Johannes Berg19957bb2009-07-02 17:20:43 +02004373 if (!info->attrs[NL80211_ATTR_SSID])
4374 return -EINVAL;
4375
4376 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4377 return -EINVAL;
4378
Johannes Bergfffd0932009-07-08 14:22:54 +02004379 err = nl80211_parse_key(info, &key);
4380 if (err)
4381 return err;
4382
4383 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004384 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4385 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004386 if (!key.p.key || !key.p.key_len)
4387 return -EINVAL;
4388 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4389 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4390 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4391 key.p.key_len != WLAN_KEY_LEN_WEP104))
4392 return -EINVAL;
4393 if (key.idx > 4)
4394 return -EINVAL;
4395 } else {
4396 key.p.key_len = 0;
4397 key.p.key = NULL;
4398 }
4399
Johannes Bergafea0b72010-08-10 09:46:42 +02004400 if (key.idx >= 0) {
4401 int i;
4402 bool ok = false;
4403 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4404 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4405 ok = true;
4406 break;
4407 }
4408 }
Johannes Berg4c476992010-10-04 21:36:35 +02004409 if (!ok)
4410 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004411 }
4412
Johannes Berg4c476992010-10-04 21:36:35 +02004413 if (!rdev->ops->auth)
4414 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004415
Johannes Berg074ac8d2010-09-16 14:58:22 +02004416 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004417 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4418 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004419
Johannes Berg19957bb2009-07-02 17:20:43 +02004420 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004421 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004422 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004423 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4424 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004425
Johannes Berg19957bb2009-07-02 17:20:43 +02004426 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4427 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4428
4429 if (info->attrs[NL80211_ATTR_IE]) {
4430 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4431 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4432 }
4433
4434 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004435 if (!nl80211_valid_auth_type(auth_type))
4436 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004437
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004438 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4439
Johannes Berg95de8172012-01-20 13:55:25 +01004440 /*
4441 * Since we no longer track auth state, ignore
4442 * requests to only change local state.
4443 */
4444 if (local_state_change)
4445 return 0;
4446
Johannes Berg4c476992010-10-04 21:36:35 +02004447 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4448 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004449 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004450}
4451
Johannes Bergc0692b82010-08-27 14:26:53 +03004452static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4453 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004454 struct cfg80211_crypto_settings *settings,
4455 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004456{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004457 memset(settings, 0, sizeof(*settings));
4458
Samuel Ortizb23aa672009-07-01 21:26:54 +02004459 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4460
Johannes Bergc0692b82010-08-27 14:26:53 +03004461 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4462 u16 proto;
4463 proto = nla_get_u16(
4464 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4465 settings->control_port_ethertype = cpu_to_be16(proto);
4466 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4467 proto != ETH_P_PAE)
4468 return -EINVAL;
4469 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4470 settings->control_port_no_encrypt = true;
4471 } else
4472 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4473
Samuel Ortizb23aa672009-07-01 21:26:54 +02004474 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4475 void *data;
4476 int len, i;
4477
4478 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4479 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4480 settings->n_ciphers_pairwise = len / sizeof(u32);
4481
4482 if (len % sizeof(u32))
4483 return -EINVAL;
4484
Johannes Berg3dc27d22009-07-02 21:36:37 +02004485 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004486 return -EINVAL;
4487
4488 memcpy(settings->ciphers_pairwise, data, len);
4489
4490 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004491 if (!cfg80211_supported_cipher_suite(
4492 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004493 settings->ciphers_pairwise[i]))
4494 return -EINVAL;
4495 }
4496
4497 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4498 settings->cipher_group =
4499 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004500 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4501 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004502 return -EINVAL;
4503 }
4504
4505 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4506 settings->wpa_versions =
4507 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4508 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4509 return -EINVAL;
4510 }
4511
4512 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4513 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004514 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004515
4516 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4517 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4518 settings->n_akm_suites = len / sizeof(u32);
4519
4520 if (len % sizeof(u32))
4521 return -EINVAL;
4522
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004523 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4524 return -EINVAL;
4525
Samuel Ortizb23aa672009-07-01 21:26:54 +02004526 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004527 }
4528
4529 return 0;
4530}
4531
Jouni Malinen636a5d32009-03-19 13:39:22 +02004532static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4533{
Johannes Berg4c476992010-10-04 21:36:35 +02004534 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4535 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004536 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004537 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004538 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004539 int err, ssid_len, ie_len = 0;
4540 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004541 u32 flags = 0;
4542 struct ieee80211_ht_cap *ht_capa = NULL;
4543 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004544
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004545 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4546 return -EINVAL;
4547
4548 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004549 !info->attrs[NL80211_ATTR_SSID] ||
4550 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004551 return -EINVAL;
4552
Johannes Berg4c476992010-10-04 21:36:35 +02004553 if (!rdev->ops->assoc)
4554 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004555
Johannes Berg074ac8d2010-09-16 14:58:22 +02004556 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004557 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4558 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004559
Johannes Berg19957bb2009-07-02 17:20:43 +02004560 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004561
Johannes Berg19957bb2009-07-02 17:20:43 +02004562 chan = ieee80211_get_channel(&rdev->wiphy,
4563 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004564 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4565 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004566
Johannes Berg19957bb2009-07-02 17:20:43 +02004567 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4568 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004569
4570 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004571 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4572 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004573 }
4574
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004575 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004576 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004577 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004578 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004579 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004580 else if (mfp != NL80211_MFP_NO)
4581 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004582 }
4583
Johannes Berg3e5d7642009-07-07 14:37:26 +02004584 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4585 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4586
Ben Greear7e7c8922011-11-18 11:31:59 -08004587 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4588 flags |= ASSOC_REQ_DISABLE_HT;
4589
4590 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4591 ht_capa_mask =
4592 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4593
4594 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4595 if (!ht_capa_mask)
4596 return -EINVAL;
4597 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4598 }
4599
Johannes Bergc0692b82010-08-27 14:26:53 +03004600 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004601 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004602 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4603 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004604 &crypto, flags, ht_capa,
4605 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004606
Jouni Malinen636a5d32009-03-19 13:39:22 +02004607 return err;
4608}
4609
4610static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4611{
Johannes Berg4c476992010-10-04 21:36:35 +02004612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4613 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004614 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004615 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004616 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004617 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004618
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004619 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4620 return -EINVAL;
4621
4622 if (!info->attrs[NL80211_ATTR_MAC])
4623 return -EINVAL;
4624
4625 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4626 return -EINVAL;
4627
Johannes Berg4c476992010-10-04 21:36:35 +02004628 if (!rdev->ops->deauth)
4629 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004630
Johannes Berg074ac8d2010-09-16 14:58:22 +02004631 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004632 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4633 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004634
Johannes Berg19957bb2009-07-02 17:20:43 +02004635 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004636
Johannes Berg19957bb2009-07-02 17:20:43 +02004637 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4638 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004639 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004640 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004641 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004642
4643 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004644 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4645 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004646 }
4647
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004648 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4649
Johannes Berg4c476992010-10-04 21:36:35 +02004650 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4651 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004652}
4653
4654static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4655{
Johannes Berg4c476992010-10-04 21:36:35 +02004656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4657 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004658 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004659 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004660 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004661 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004662
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004663 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4664 return -EINVAL;
4665
4666 if (!info->attrs[NL80211_ATTR_MAC])
4667 return -EINVAL;
4668
4669 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4670 return -EINVAL;
4671
Johannes Berg4c476992010-10-04 21:36:35 +02004672 if (!rdev->ops->disassoc)
4673 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004674
Johannes Berg074ac8d2010-09-16 14:58:22 +02004675 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004676 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4677 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004678
Johannes Berg19957bb2009-07-02 17:20:43 +02004679 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004680
Johannes Berg19957bb2009-07-02 17:20:43 +02004681 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4682 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004683 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004684 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004685 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004686
4687 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004688 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4689 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004690 }
4691
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004692 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4693
Johannes Berg4c476992010-10-04 21:36:35 +02004694 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4695 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004696}
4697
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004698static bool
4699nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4700 int mcast_rate[IEEE80211_NUM_BANDS],
4701 int rateval)
4702{
4703 struct wiphy *wiphy = &rdev->wiphy;
4704 bool found = false;
4705 int band, i;
4706
4707 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4708 struct ieee80211_supported_band *sband;
4709
4710 sband = wiphy->bands[band];
4711 if (!sband)
4712 continue;
4713
4714 for (i = 0; i < sband->n_bitrates; i++) {
4715 if (sband->bitrates[i].bitrate == rateval) {
4716 mcast_rate[band] = i + 1;
4717 found = true;
4718 break;
4719 }
4720 }
4721 }
4722
4723 return found;
4724}
4725
Johannes Berg04a773a2009-04-19 21:24:32 +02004726static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4727{
Johannes Berg4c476992010-10-04 21:36:35 +02004728 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4729 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004730 struct cfg80211_ibss_params ibss;
4731 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004732 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004733 int err;
4734
Johannes Berg8e30bc52009-04-22 17:45:38 +02004735 memset(&ibss, 0, sizeof(ibss));
4736
Johannes Berg04a773a2009-04-19 21:24:32 +02004737 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4738 return -EINVAL;
4739
4740 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4741 !info->attrs[NL80211_ATTR_SSID] ||
4742 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4743 return -EINVAL;
4744
Johannes Berg8e30bc52009-04-22 17:45:38 +02004745 ibss.beacon_interval = 100;
4746
4747 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4748 ibss.beacon_interval =
4749 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4750 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4751 return -EINVAL;
4752 }
4753
Johannes Berg4c476992010-10-04 21:36:35 +02004754 if (!rdev->ops->join_ibss)
4755 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004756
Johannes Berg4c476992010-10-04 21:36:35 +02004757 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4758 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004759
Johannes Berg79c97e92009-07-07 03:56:12 +02004760 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004761
Johannes Berg39193492011-09-16 13:45:25 +02004762 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004763 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004764
4765 if (!is_valid_ether_addr(ibss.bssid))
4766 return -EINVAL;
4767 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004768 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4769 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4770
4771 if (info->attrs[NL80211_ATTR_IE]) {
4772 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4773 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4774 }
4775
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004776 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4777 enum nl80211_channel_type channel_type;
4778
4779 channel_type = nla_get_u32(
4780 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4781 if (channel_type != NL80211_CHAN_NO_HT &&
4782 channel_type != NL80211_CHAN_HT20 &&
4783 channel_type != NL80211_CHAN_HT40MINUS &&
4784 channel_type != NL80211_CHAN_HT40PLUS)
4785 return -EINVAL;
4786
4787 if (channel_type != NL80211_CHAN_NO_HT &&
4788 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4789 return -EINVAL;
4790
4791 ibss.channel_type = channel_type;
4792 } else {
4793 ibss.channel_type = NL80211_CHAN_NO_HT;
4794 }
4795
4796 ibss.channel = rdev_freq_to_chan(rdev,
4797 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4798 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004799 if (!ibss.channel ||
4800 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004801 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4802 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004803
Alexander Simon54858ee5b2011-11-30 16:56:32 +01004804 /* Both channels should be able to initiate communication */
4805 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4806 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4807 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4808 ibss.channel_type))
4809 return -EINVAL;
4810
Johannes Berg04a773a2009-04-19 21:24:32 +02004811 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004812 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004813
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004814 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4815 u8 *rates =
4816 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4817 int n_rates =
4818 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4819 struct ieee80211_supported_band *sband =
4820 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004821
Johannes Berg34850ab2011-07-18 18:08:35 +02004822 err = ieee80211_get_ratemask(sband, rates, n_rates,
4823 &ibss.basic_rates);
4824 if (err)
4825 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004826 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004827
4828 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4829 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4830 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4831 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004832
Johannes Berg4c476992010-10-04 21:36:35 +02004833 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4834 connkeys = nl80211_parse_connkeys(rdev,
4835 info->attrs[NL80211_ATTR_KEYS]);
4836 if (IS_ERR(connkeys))
4837 return PTR_ERR(connkeys);
4838 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004839
Antonio Quartulli267335d2012-01-31 20:25:47 +01004840 ibss.control_port =
4841 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4842
Johannes Berg4c476992010-10-04 21:36:35 +02004843 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004844 if (err)
4845 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004846 return err;
4847}
4848
4849static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4850{
Johannes Berg4c476992010-10-04 21:36:35 +02004851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4852 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004853
Johannes Berg4c476992010-10-04 21:36:35 +02004854 if (!rdev->ops->leave_ibss)
4855 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004856
Johannes Berg4c476992010-10-04 21:36:35 +02004857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4858 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004859
Johannes Berg4c476992010-10-04 21:36:35 +02004860 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004861}
4862
Johannes Bergaff89a92009-07-01 21:26:51 +02004863#ifdef CONFIG_NL80211_TESTMODE
4864static struct genl_multicast_group nl80211_testmode_mcgrp = {
4865 .name = "testmode",
4866};
4867
4868static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4869{
Johannes Berg4c476992010-10-04 21:36:35 +02004870 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004871 int err;
4872
4873 if (!info->attrs[NL80211_ATTR_TESTDATA])
4874 return -EINVAL;
4875
Johannes Bergaff89a92009-07-01 21:26:51 +02004876 err = -EOPNOTSUPP;
4877 if (rdev->ops->testmode_cmd) {
4878 rdev->testmode_info = info;
4879 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4880 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4881 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4882 rdev->testmode_info = NULL;
4883 }
4884
Johannes Bergaff89a92009-07-01 21:26:51 +02004885 return err;
4886}
4887
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004888static int nl80211_testmode_dump(struct sk_buff *skb,
4889 struct netlink_callback *cb)
4890{
Johannes Berg00918d32011-12-13 17:22:05 +01004891 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004892 int err;
4893 long phy_idx;
4894 void *data = NULL;
4895 int data_len = 0;
4896
4897 if (cb->args[0]) {
4898 /*
4899 * 0 is a valid index, but not valid for args[0],
4900 * so we need to offset by 1.
4901 */
4902 phy_idx = cb->args[0] - 1;
4903 } else {
4904 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4905 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4906 nl80211_policy);
4907 if (err)
4908 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01004909 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
4910 phy_idx = nla_get_u32(
4911 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4912 } else {
4913 struct net_device *netdev;
4914
4915 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
4916 nl80211_fam.attrbuf,
4917 &rdev, &netdev);
4918 if (err)
4919 return err;
4920 dev_put(netdev);
4921 phy_idx = rdev->wiphy_idx;
4922 cfg80211_unlock_rdev(rdev);
4923 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004924 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4925 cb->args[1] =
4926 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4927 }
4928
4929 if (cb->args[1]) {
4930 data = nla_data((void *)cb->args[1]);
4931 data_len = nla_len((void *)cb->args[1]);
4932 }
4933
4934 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01004935 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4936 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004937 mutex_unlock(&cfg80211_mutex);
4938 return -ENOENT;
4939 }
Johannes Berg00918d32011-12-13 17:22:05 +01004940 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004941 mutex_unlock(&cfg80211_mutex);
4942
Johannes Berg00918d32011-12-13 17:22:05 +01004943 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004944 err = -EOPNOTSUPP;
4945 goto out_err;
4946 }
4947
4948 while (1) {
4949 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4950 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4951 NL80211_CMD_TESTMODE);
4952 struct nlattr *tmdata;
4953
Johannes Berg00918d32011-12-13 17:22:05 +01004954 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004955 genlmsg_cancel(skb, hdr);
4956 break;
4957 }
4958
4959 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4960 if (!tmdata) {
4961 genlmsg_cancel(skb, hdr);
4962 break;
4963 }
Johannes Berg00918d32011-12-13 17:22:05 +01004964 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
4965 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004966 nla_nest_end(skb, tmdata);
4967
4968 if (err == -ENOBUFS || err == -ENOENT) {
4969 genlmsg_cancel(skb, hdr);
4970 break;
4971 } else if (err) {
4972 genlmsg_cancel(skb, hdr);
4973 goto out_err;
4974 }
4975
4976 genlmsg_end(skb, hdr);
4977 }
4978
4979 err = skb->len;
4980 /* see above */
4981 cb->args[0] = phy_idx + 1;
4982 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01004983 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004984 return err;
4985}
4986
Johannes Bergaff89a92009-07-01 21:26:51 +02004987static struct sk_buff *
4988__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4989 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4990{
4991 struct sk_buff *skb;
4992 void *hdr;
4993 struct nlattr *data;
4994
4995 skb = nlmsg_new(approxlen + 100, gfp);
4996 if (!skb)
4997 return NULL;
4998
4999 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5000 if (!hdr) {
5001 kfree_skb(skb);
5002 return NULL;
5003 }
5004
5005 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5006 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5007
5008 ((void **)skb->cb)[0] = rdev;
5009 ((void **)skb->cb)[1] = hdr;
5010 ((void **)skb->cb)[2] = data;
5011
5012 return skb;
5013
5014 nla_put_failure:
5015 kfree_skb(skb);
5016 return NULL;
5017}
5018
5019struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5020 int approxlen)
5021{
5022 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5023
5024 if (WARN_ON(!rdev->testmode_info))
5025 return NULL;
5026
5027 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5028 rdev->testmode_info->snd_pid,
5029 rdev->testmode_info->snd_seq,
5030 GFP_KERNEL);
5031}
5032EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5033
5034int cfg80211_testmode_reply(struct sk_buff *skb)
5035{
5036 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5037 void *hdr = ((void **)skb->cb)[1];
5038 struct nlattr *data = ((void **)skb->cb)[2];
5039
5040 if (WARN_ON(!rdev->testmode_info)) {
5041 kfree_skb(skb);
5042 return -EINVAL;
5043 }
5044
5045 nla_nest_end(skb, data);
5046 genlmsg_end(skb, hdr);
5047 return genlmsg_reply(skb, rdev->testmode_info);
5048}
5049EXPORT_SYMBOL(cfg80211_testmode_reply);
5050
5051struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5052 int approxlen, gfp_t gfp)
5053{
5054 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5055
5056 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5057}
5058EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5059
5060void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5061{
5062 void *hdr = ((void **)skb->cb)[1];
5063 struct nlattr *data = ((void **)skb->cb)[2];
5064
5065 nla_nest_end(skb, data);
5066 genlmsg_end(skb, hdr);
5067 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5068}
5069EXPORT_SYMBOL(cfg80211_testmode_event);
5070#endif
5071
Samuel Ortizb23aa672009-07-01 21:26:54 +02005072static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5073{
Johannes Berg4c476992010-10-04 21:36:35 +02005074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5075 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005076 struct cfg80211_connect_params connect;
5077 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005078 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005079 int err;
5080
5081 memset(&connect, 0, sizeof(connect));
5082
5083 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5084 return -EINVAL;
5085
5086 if (!info->attrs[NL80211_ATTR_SSID] ||
5087 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5088 return -EINVAL;
5089
5090 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5091 connect.auth_type =
5092 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5093 if (!nl80211_valid_auth_type(connect.auth_type))
5094 return -EINVAL;
5095 } else
5096 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5097
5098 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5099
Johannes Bergc0692b82010-08-27 14:26:53 +03005100 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005101 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005102 if (err)
5103 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005104
Johannes Berg074ac8d2010-09-16 14:58:22 +02005105 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005106 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5107 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005108
Johannes Berg79c97e92009-07-07 03:56:12 +02005109 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005110
Samuel Ortizb23aa672009-07-01 21:26:54 +02005111 if (info->attrs[NL80211_ATTR_MAC])
5112 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5113 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5114 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5115
5116 if (info->attrs[NL80211_ATTR_IE]) {
5117 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5118 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5119 }
5120
5121 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5122 connect.channel =
5123 ieee80211_get_channel(wiphy,
5124 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5125 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005126 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5127 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005128 }
5129
Johannes Bergfffd0932009-07-08 14:22:54 +02005130 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5131 connkeys = nl80211_parse_connkeys(rdev,
5132 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005133 if (IS_ERR(connkeys))
5134 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005135 }
5136
Ben Greear7e7c8922011-11-18 11:31:59 -08005137 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5138 connect.flags |= ASSOC_REQ_DISABLE_HT;
5139
5140 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5141 memcpy(&connect.ht_capa_mask,
5142 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5143 sizeof(connect.ht_capa_mask));
5144
5145 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5146 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5147 return -EINVAL;
5148 memcpy(&connect.ht_capa,
5149 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5150 sizeof(connect.ht_capa));
5151 }
5152
Johannes Bergfffd0932009-07-08 14:22:54 +02005153 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005154 if (err)
5155 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005156 return err;
5157}
5158
5159static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5160{
Johannes Berg4c476992010-10-04 21:36:35 +02005161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5162 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005163 u16 reason;
5164
5165 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5166 reason = WLAN_REASON_DEAUTH_LEAVING;
5167 else
5168 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5169
5170 if (reason == 0)
5171 return -EINVAL;
5172
Johannes Berg074ac8d2010-09-16 14:58:22 +02005173 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005174 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5175 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005176
Johannes Berg4c476992010-10-04 21:36:35 +02005177 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005178}
5179
Johannes Berg463d0182009-07-14 00:33:35 +02005180static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5181{
Johannes Berg4c476992010-10-04 21:36:35 +02005182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005183 struct net *net;
5184 int err;
5185 u32 pid;
5186
5187 if (!info->attrs[NL80211_ATTR_PID])
5188 return -EINVAL;
5189
5190 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5191
Johannes Berg463d0182009-07-14 00:33:35 +02005192 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005193 if (IS_ERR(net))
5194 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005195
5196 err = 0;
5197
5198 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005199 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5200 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005201
Johannes Berg463d0182009-07-14 00:33:35 +02005202 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005203 return err;
5204}
5205
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005206static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5207{
Johannes Berg4c476992010-10-04 21:36:35 +02005208 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005209 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5210 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005211 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005212 struct cfg80211_pmksa pmksa;
5213
5214 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5215
5216 if (!info->attrs[NL80211_ATTR_MAC])
5217 return -EINVAL;
5218
5219 if (!info->attrs[NL80211_ATTR_PMKID])
5220 return -EINVAL;
5221
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005222 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5223 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5224
Johannes Berg074ac8d2010-09-16 14:58:22 +02005225 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005226 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5227 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005228
5229 switch (info->genlhdr->cmd) {
5230 case NL80211_CMD_SET_PMKSA:
5231 rdev_ops = rdev->ops->set_pmksa;
5232 break;
5233 case NL80211_CMD_DEL_PMKSA:
5234 rdev_ops = rdev->ops->del_pmksa;
5235 break;
5236 default:
5237 WARN_ON(1);
5238 break;
5239 }
5240
Johannes Berg4c476992010-10-04 21:36:35 +02005241 if (!rdev_ops)
5242 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005243
Johannes Berg4c476992010-10-04 21:36:35 +02005244 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005245}
5246
5247static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5248{
Johannes Berg4c476992010-10-04 21:36:35 +02005249 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5250 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005251
Johannes Berg074ac8d2010-09-16 14:58:22 +02005252 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005253 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5254 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005255
Johannes Berg4c476992010-10-04 21:36:35 +02005256 if (!rdev->ops->flush_pmksa)
5257 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005258
Johannes Berg4c476992010-10-04 21:36:35 +02005259 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005260}
5261
Arik Nemtsov109086c2011-09-28 14:12:50 +03005262static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5263{
5264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5265 struct net_device *dev = info->user_ptr[1];
5266 u8 action_code, dialog_token;
5267 u16 status_code;
5268 u8 *peer;
5269
5270 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5271 !rdev->ops->tdls_mgmt)
5272 return -EOPNOTSUPP;
5273
5274 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5275 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5276 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5277 !info->attrs[NL80211_ATTR_IE] ||
5278 !info->attrs[NL80211_ATTR_MAC])
5279 return -EINVAL;
5280
5281 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5282 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5283 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5284 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5285
5286 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5287 dialog_token, status_code,
5288 nla_data(info->attrs[NL80211_ATTR_IE]),
5289 nla_len(info->attrs[NL80211_ATTR_IE]));
5290}
5291
5292static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5293{
5294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5295 struct net_device *dev = info->user_ptr[1];
5296 enum nl80211_tdls_operation operation;
5297 u8 *peer;
5298
5299 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5300 !rdev->ops->tdls_oper)
5301 return -EOPNOTSUPP;
5302
5303 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5304 !info->attrs[NL80211_ATTR_MAC])
5305 return -EINVAL;
5306
5307 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5308 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5309
5310 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5311}
5312
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005313static int nl80211_remain_on_channel(struct sk_buff *skb,
5314 struct genl_info *info)
5315{
Johannes Berg4c476992010-10-04 21:36:35 +02005316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5317 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005318 struct ieee80211_channel *chan;
5319 struct sk_buff *msg;
5320 void *hdr;
5321 u64 cookie;
5322 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5323 u32 freq, duration;
5324 int err;
5325
5326 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5327 !info->attrs[NL80211_ATTR_DURATION])
5328 return -EINVAL;
5329
5330 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5331
5332 /*
5333 * We should be on that channel for at least one jiffie,
5334 * and more than 5 seconds seems excessive.
5335 */
Johannes Berga2939112010-12-14 17:54:28 +01005336 if (!duration || !msecs_to_jiffies(duration) ||
5337 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005338 return -EINVAL;
5339
Johannes Berg7c4ef712011-11-18 15:33:48 +01005340 if (!rdev->ops->remain_on_channel ||
5341 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005342 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005343
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005344 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5345 channel_type = nla_get_u32(
5346 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5347 if (channel_type != NL80211_CHAN_NO_HT &&
5348 channel_type != NL80211_CHAN_HT20 &&
5349 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005350 channel_type != NL80211_CHAN_HT40MINUS)
5351 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005352 }
5353
5354 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5355 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005356 if (chan == NULL)
5357 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005358
5359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005360 if (!msg)
5361 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005362
5363 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5364 NL80211_CMD_REMAIN_ON_CHANNEL);
5365
5366 if (IS_ERR(hdr)) {
5367 err = PTR_ERR(hdr);
5368 goto free_msg;
5369 }
5370
5371 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5372 channel_type, duration, &cookie);
5373
5374 if (err)
5375 goto free_msg;
5376
5377 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5378
5379 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005380
5381 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005382
5383 nla_put_failure:
5384 err = -ENOBUFS;
5385 free_msg:
5386 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005387 return err;
5388}
5389
5390static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5391 struct genl_info *info)
5392{
Johannes Berg4c476992010-10-04 21:36:35 +02005393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5394 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005395 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005396
5397 if (!info->attrs[NL80211_ATTR_COOKIE])
5398 return -EINVAL;
5399
Johannes Berg4c476992010-10-04 21:36:35 +02005400 if (!rdev->ops->cancel_remain_on_channel)
5401 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005402
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005403 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5404
Johannes Berg4c476992010-10-04 21:36:35 +02005405 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005406}
5407
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005408static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5409 u8 *rates, u8 rates_len)
5410{
5411 u8 i;
5412 u32 mask = 0;
5413
5414 for (i = 0; i < rates_len; i++) {
5415 int rate = (rates[i] & 0x7f) * 5;
5416 int ridx;
5417 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5418 struct ieee80211_rate *srate =
5419 &sband->bitrates[ridx];
5420 if (rate == srate->bitrate) {
5421 mask |= 1 << ridx;
5422 break;
5423 }
5424 }
5425 if (ridx == sband->n_bitrates)
5426 return 0; /* rate not found */
5427 }
5428
5429 return mask;
5430}
5431
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005432static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5433 u8 *rates, u8 rates_len,
5434 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5435{
5436 u8 i;
5437
5438 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5439
5440 for (i = 0; i < rates_len; i++) {
5441 int ridx, rbit;
5442
5443 ridx = rates[i] / 8;
5444 rbit = BIT(rates[i] % 8);
5445
5446 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005447 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005448 return false;
5449
5450 /* check availability */
5451 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5452 mcs[ridx] |= rbit;
5453 else
5454 return false;
5455 }
5456
5457 return true;
5458}
5459
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005460static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005461 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5462 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005463 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5464 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005465};
5466
5467static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5468 struct genl_info *info)
5469{
5470 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005472 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005473 int rem, i;
5474 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005475 struct nlattr *tx_rates;
5476 struct ieee80211_supported_band *sband;
5477
5478 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5479 return -EINVAL;
5480
Johannes Berg4c476992010-10-04 21:36:35 +02005481 if (!rdev->ops->set_bitrate_mask)
5482 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005483
5484 memset(&mask, 0, sizeof(mask));
5485 /* Default to all rates enabled */
5486 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5487 sband = rdev->wiphy.bands[i];
5488 mask.control[i].legacy =
5489 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005490 if (sband)
5491 memcpy(mask.control[i].mcs,
5492 sband->ht_cap.mcs.rx_mask,
5493 sizeof(mask.control[i].mcs));
5494 else
5495 memset(mask.control[i].mcs, 0,
5496 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005497 }
5498
5499 /*
5500 * The nested attribute uses enum nl80211_band as the index. This maps
5501 * directly to the enum ieee80211_band values used in cfg80211.
5502 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005503 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005504 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5505 {
5506 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005507 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5508 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005509 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005510 if (sband == NULL)
5511 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005512 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5513 nla_len(tx_rates), nl80211_txattr_policy);
5514 if (tb[NL80211_TXRATE_LEGACY]) {
5515 mask.control[band].legacy = rateset_to_mask(
5516 sband,
5517 nla_data(tb[NL80211_TXRATE_LEGACY]),
5518 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005519 }
5520 if (tb[NL80211_TXRATE_MCS]) {
5521 if (!ht_rateset_to_mask(
5522 sband,
5523 nla_data(tb[NL80211_TXRATE_MCS]),
5524 nla_len(tb[NL80211_TXRATE_MCS]),
5525 mask.control[band].mcs))
5526 return -EINVAL;
5527 }
5528
5529 if (mask.control[band].legacy == 0) {
5530 /* don't allow empty legacy rates if HT
5531 * is not even supported. */
5532 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5533 return -EINVAL;
5534
5535 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5536 if (mask.control[band].mcs[i])
5537 break;
5538
5539 /* legacy and mcs rates may not be both empty */
5540 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005541 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005542 }
5543 }
5544
Johannes Berg4c476992010-10-04 21:36:35 +02005545 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005546}
5547
Johannes Berg2e161f72010-08-12 15:38:38 +02005548static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005549{
Johannes Berg4c476992010-10-04 21:36:35 +02005550 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5551 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005552 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005553
5554 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5555 return -EINVAL;
5556
Johannes Berg2e161f72010-08-12 15:38:38 +02005557 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5558 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005559
Johannes Berg9d38d852010-06-09 17:20:33 +02005560 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005561 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005562 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5563 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5564 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005565 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005566 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5567 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005568
5569 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005570 if (!rdev->ops->mgmt_tx)
5571 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005572
Johannes Berg4c476992010-10-04 21:36:35 +02005573 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005574 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005575 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5576 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005577}
5578
Johannes Berg2e161f72010-08-12 15:38:38 +02005579static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005580{
Johannes Berg4c476992010-10-04 21:36:35 +02005581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5582 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005583 struct ieee80211_channel *chan;
5584 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005585 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005586 u32 freq;
5587 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005588 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005589 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005590 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005591 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005592 bool offchan, no_cck, dont_wait_for_ack;
5593
5594 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005595
5596 if (!info->attrs[NL80211_ATTR_FRAME] ||
5597 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5598 return -EINVAL;
5599
Johannes Berg4c476992010-10-04 21:36:35 +02005600 if (!rdev->ops->mgmt_tx)
5601 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005602
Johannes Berg9d38d852010-06-09 17:20:33 +02005603 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005604 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005605 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5606 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5607 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005608 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005609 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5610 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005611
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005612 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005613 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005614 return -EINVAL;
5615 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5616 }
5617
Jouni Malinen026331c2010-02-15 12:53:10 +02005618 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5619 channel_type = nla_get_u32(
5620 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5621 if (channel_type != NL80211_CHAN_NO_HT &&
5622 channel_type != NL80211_CHAN_HT20 &&
5623 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005624 channel_type != NL80211_CHAN_HT40MINUS)
5625 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005626 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005627 }
5628
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005629 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5630
Johannes Berg7c4ef712011-11-18 15:33:48 +01005631 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5632 return -EINVAL;
5633
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305634 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5635
Jouni Malinen026331c2010-02-15 12:53:10 +02005636 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5637 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005638 if (chan == NULL)
5639 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005640
Johannes Berge247bd902011-11-04 11:18:21 +01005641 if (!dont_wait_for_ack) {
5642 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5643 if (!msg)
5644 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005645
Johannes Berge247bd902011-11-04 11:18:21 +01005646 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5647 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005648
Johannes Berge247bd902011-11-04 11:18:21 +01005649 if (IS_ERR(hdr)) {
5650 err = PTR_ERR(hdr);
5651 goto free_msg;
5652 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005653 }
Johannes Berge247bd902011-11-04 11:18:21 +01005654
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005655 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5656 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005657 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5658 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005659 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005660 if (err)
5661 goto free_msg;
5662
Johannes Berge247bd902011-11-04 11:18:21 +01005663 if (msg) {
5664 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005665
Johannes Berge247bd902011-11-04 11:18:21 +01005666 genlmsg_end(msg, hdr);
5667 return genlmsg_reply(msg, info);
5668 }
5669
5670 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005671
5672 nla_put_failure:
5673 err = -ENOBUFS;
5674 free_msg:
5675 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005676 return err;
5677}
5678
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005679static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5680{
5681 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5682 struct net_device *dev = info->user_ptr[1];
5683 u64 cookie;
5684
5685 if (!info->attrs[NL80211_ATTR_COOKIE])
5686 return -EINVAL;
5687
5688 if (!rdev->ops->mgmt_tx_cancel_wait)
5689 return -EOPNOTSUPP;
5690
5691 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5692 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5693 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5694 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5696 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5697 return -EOPNOTSUPP;
5698
5699 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5700
5701 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5702}
5703
Kalle Valoffb9eb32010-02-17 17:58:10 +02005704static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5705{
Johannes Berg4c476992010-10-04 21:36:35 +02005706 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005707 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005708 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005709 u8 ps_state;
5710 bool state;
5711 int err;
5712
Johannes Berg4c476992010-10-04 21:36:35 +02005713 if (!info->attrs[NL80211_ATTR_PS_STATE])
5714 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005715
5716 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5717
Johannes Berg4c476992010-10-04 21:36:35 +02005718 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5719 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005720
5721 wdev = dev->ieee80211_ptr;
5722
Johannes Berg4c476992010-10-04 21:36:35 +02005723 if (!rdev->ops->set_power_mgmt)
5724 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005725
5726 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5727
5728 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005729 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005730
Johannes Berg4c476992010-10-04 21:36:35 +02005731 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5732 wdev->ps_timeout);
5733 if (!err)
5734 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005735 return err;
5736}
5737
5738static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5739{
Johannes Berg4c476992010-10-04 21:36:35 +02005740 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005741 enum nl80211_ps_state ps_state;
5742 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005743 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005744 struct sk_buff *msg;
5745 void *hdr;
5746 int err;
5747
Kalle Valoffb9eb32010-02-17 17:58:10 +02005748 wdev = dev->ieee80211_ptr;
5749
Johannes Berg4c476992010-10-04 21:36:35 +02005750 if (!rdev->ops->set_power_mgmt)
5751 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005752
5753 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005754 if (!msg)
5755 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005756
5757 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5758 NL80211_CMD_GET_POWER_SAVE);
5759 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005760 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005761 goto free_msg;
5762 }
5763
5764 if (wdev->ps)
5765 ps_state = NL80211_PS_ENABLED;
5766 else
5767 ps_state = NL80211_PS_DISABLED;
5768
5769 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5770
5771 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005772 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005773
Johannes Berg4c476992010-10-04 21:36:35 +02005774 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005775 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005776 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005777 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005778 return err;
5779}
5780
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005781static struct nla_policy
5782nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5783 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5784 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5785 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5786};
5787
5788static int nl80211_set_cqm_rssi(struct genl_info *info,
5789 s32 threshold, u32 hysteresis)
5790{
Johannes Berg4c476992010-10-04 21:36:35 +02005791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005792 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005793 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005794
5795 if (threshold > 0)
5796 return -EINVAL;
5797
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005798 wdev = dev->ieee80211_ptr;
5799
Johannes Berg4c476992010-10-04 21:36:35 +02005800 if (!rdev->ops->set_cqm_rssi_config)
5801 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005802
Johannes Berg074ac8d2010-09-16 14:58:22 +02005803 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005804 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5805 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005806
Johannes Berg4c476992010-10-04 21:36:35 +02005807 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5808 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005809}
5810
5811static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5812{
5813 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5814 struct nlattr *cqm;
5815 int err;
5816
5817 cqm = info->attrs[NL80211_ATTR_CQM];
5818 if (!cqm) {
5819 err = -EINVAL;
5820 goto out;
5821 }
5822
5823 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5824 nl80211_attr_cqm_policy);
5825 if (err)
5826 goto out;
5827
5828 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5829 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5830 s32 threshold;
5831 u32 hysteresis;
5832 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5833 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5834 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5835 } else
5836 err = -EINVAL;
5837
5838out:
5839 return err;
5840}
5841
Johannes Berg29cbe682010-12-03 09:20:44 +01005842static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5843{
5844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5845 struct net_device *dev = info->user_ptr[1];
5846 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005847 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005848 int err;
5849
5850 /* start with default */
5851 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005852 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005853
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005854 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005855 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005856 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005857 if (err)
5858 return err;
5859 }
5860
5861 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5862 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5863 return -EINVAL;
5864
Javier Cardonac80d5452010-12-16 17:37:49 -08005865 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5866 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5867
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08005868 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5869 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5870 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5871 return -EINVAL;
5872
Javier Cardonac80d5452010-12-16 17:37:49 -08005873 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5874 /* parse additional setup parameters if given */
5875 err = nl80211_parse_mesh_setup(info, &setup);
5876 if (err)
5877 return err;
5878 }
5879
5880 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005881}
5882
5883static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5884{
5885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5886 struct net_device *dev = info->user_ptr[1];
5887
5888 return cfg80211_leave_mesh(rdev, dev);
5889}
5890
Johannes Bergff1b6e62011-05-04 15:37:28 +02005891static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5892{
5893 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5894 struct sk_buff *msg;
5895 void *hdr;
5896
5897 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5898 return -EOPNOTSUPP;
5899
5900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5901 if (!msg)
5902 return -ENOMEM;
5903
5904 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5905 NL80211_CMD_GET_WOWLAN);
5906 if (!hdr)
5907 goto nla_put_failure;
5908
5909 if (rdev->wowlan) {
5910 struct nlattr *nl_wowlan;
5911
5912 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5913 if (!nl_wowlan)
5914 goto nla_put_failure;
5915
5916 if (rdev->wowlan->any)
5917 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5918 if (rdev->wowlan->disconnect)
5919 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5920 if (rdev->wowlan->magic_pkt)
5921 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005922 if (rdev->wowlan->gtk_rekey_failure)
5923 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5924 if (rdev->wowlan->eap_identity_req)
5925 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5926 if (rdev->wowlan->four_way_handshake)
5927 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5928 if (rdev->wowlan->rfkill_release)
5929 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005930 if (rdev->wowlan->n_patterns) {
5931 struct nlattr *nl_pats, *nl_pat;
5932 int i, pat_len;
5933
5934 nl_pats = nla_nest_start(msg,
5935 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5936 if (!nl_pats)
5937 goto nla_put_failure;
5938
5939 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5940 nl_pat = nla_nest_start(msg, i + 1);
5941 if (!nl_pat)
5942 goto nla_put_failure;
5943 pat_len = rdev->wowlan->patterns[i].pattern_len;
5944 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5945 DIV_ROUND_UP(pat_len, 8),
5946 rdev->wowlan->patterns[i].mask);
5947 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5948 pat_len,
5949 rdev->wowlan->patterns[i].pattern);
5950 nla_nest_end(msg, nl_pat);
5951 }
5952 nla_nest_end(msg, nl_pats);
5953 }
5954
5955 nla_nest_end(msg, nl_wowlan);
5956 }
5957
5958 genlmsg_end(msg, hdr);
5959 return genlmsg_reply(msg, info);
5960
5961nla_put_failure:
5962 nlmsg_free(msg);
5963 return -ENOBUFS;
5964}
5965
5966static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5967{
5968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5969 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5970 struct cfg80211_wowlan no_triggers = {};
5971 struct cfg80211_wowlan new_triggers = {};
5972 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5973 int err, i;
5974
5975 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5976 return -EOPNOTSUPP;
5977
5978 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5979 goto no_triggers;
5980
5981 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5982 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5983 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5984 nl80211_wowlan_policy);
5985 if (err)
5986 return err;
5987
5988 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5989 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5990 return -EINVAL;
5991 new_triggers.any = true;
5992 }
5993
5994 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5995 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5996 return -EINVAL;
5997 new_triggers.disconnect = true;
5998 }
5999
6000 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6001 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6002 return -EINVAL;
6003 new_triggers.magic_pkt = true;
6004 }
6005
Johannes Berg77dbbb12011-07-13 10:48:55 +02006006 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6007 return -EINVAL;
6008
6009 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6010 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6011 return -EINVAL;
6012 new_triggers.gtk_rekey_failure = true;
6013 }
6014
6015 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6016 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6017 return -EINVAL;
6018 new_triggers.eap_identity_req = true;
6019 }
6020
6021 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6022 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6023 return -EINVAL;
6024 new_triggers.four_way_handshake = true;
6025 }
6026
6027 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6028 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6029 return -EINVAL;
6030 new_triggers.rfkill_release = true;
6031 }
6032
Johannes Bergff1b6e62011-05-04 15:37:28 +02006033 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6034 struct nlattr *pat;
6035 int n_patterns = 0;
6036 int rem, pat_len, mask_len;
6037 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6038
6039 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6040 rem)
6041 n_patterns++;
6042 if (n_patterns > wowlan->n_patterns)
6043 return -EINVAL;
6044
6045 new_triggers.patterns = kcalloc(n_patterns,
6046 sizeof(new_triggers.patterns[0]),
6047 GFP_KERNEL);
6048 if (!new_triggers.patterns)
6049 return -ENOMEM;
6050
6051 new_triggers.n_patterns = n_patterns;
6052 i = 0;
6053
6054 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6055 rem) {
6056 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6057 nla_data(pat), nla_len(pat), NULL);
6058 err = -EINVAL;
6059 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6060 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6061 goto error;
6062 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6063 mask_len = DIV_ROUND_UP(pat_len, 8);
6064 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6065 mask_len)
6066 goto error;
6067 if (pat_len > wowlan->pattern_max_len ||
6068 pat_len < wowlan->pattern_min_len)
6069 goto error;
6070
6071 new_triggers.patterns[i].mask =
6072 kmalloc(mask_len + pat_len, GFP_KERNEL);
6073 if (!new_triggers.patterns[i].mask) {
6074 err = -ENOMEM;
6075 goto error;
6076 }
6077 new_triggers.patterns[i].pattern =
6078 new_triggers.patterns[i].mask + mask_len;
6079 memcpy(new_triggers.patterns[i].mask,
6080 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6081 mask_len);
6082 new_triggers.patterns[i].pattern_len = pat_len;
6083 memcpy(new_triggers.patterns[i].pattern,
6084 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6085 pat_len);
6086 i++;
6087 }
6088 }
6089
6090 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6091 struct cfg80211_wowlan *ntrig;
6092 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6093 GFP_KERNEL);
6094 if (!ntrig) {
6095 err = -ENOMEM;
6096 goto error;
6097 }
6098 cfg80211_rdev_free_wowlan(rdev);
6099 rdev->wowlan = ntrig;
6100 } else {
6101 no_triggers:
6102 cfg80211_rdev_free_wowlan(rdev);
6103 rdev->wowlan = NULL;
6104 }
6105
6106 return 0;
6107 error:
6108 for (i = 0; i < new_triggers.n_patterns; i++)
6109 kfree(new_triggers.patterns[i].mask);
6110 kfree(new_triggers.patterns);
6111 return err;
6112}
6113
Johannes Berge5497d72011-07-05 16:35:40 +02006114static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6115{
6116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6117 struct net_device *dev = info->user_ptr[1];
6118 struct wireless_dev *wdev = dev->ieee80211_ptr;
6119 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6120 struct cfg80211_gtk_rekey_data rekey_data;
6121 int err;
6122
6123 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6124 return -EINVAL;
6125
6126 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6127 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6128 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6129 nl80211_rekey_policy);
6130 if (err)
6131 return err;
6132
6133 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6134 return -ERANGE;
6135 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6136 return -ERANGE;
6137 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6138 return -ERANGE;
6139
6140 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6141 NL80211_KEK_LEN);
6142 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6143 NL80211_KCK_LEN);
6144 memcpy(rekey_data.replay_ctr,
6145 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6146 NL80211_REPLAY_CTR_LEN);
6147
6148 wdev_lock(wdev);
6149 if (!wdev->current_bss) {
6150 err = -ENOTCONN;
6151 goto out;
6152 }
6153
6154 if (!rdev->ops->set_rekey_data) {
6155 err = -EOPNOTSUPP;
6156 goto out;
6157 }
6158
6159 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6160 out:
6161 wdev_unlock(wdev);
6162 return err;
6163}
6164
Johannes Berg28946da2011-11-04 11:18:12 +01006165static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6166 struct genl_info *info)
6167{
6168 struct net_device *dev = info->user_ptr[1];
6169 struct wireless_dev *wdev = dev->ieee80211_ptr;
6170
6171 if (wdev->iftype != NL80211_IFTYPE_AP &&
6172 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6173 return -EINVAL;
6174
6175 if (wdev->ap_unexpected_nlpid)
6176 return -EBUSY;
6177
6178 wdev->ap_unexpected_nlpid = info->snd_pid;
6179 return 0;
6180}
6181
Johannes Berg7f6cf312011-11-04 11:18:15 +01006182static int nl80211_probe_client(struct sk_buff *skb,
6183 struct genl_info *info)
6184{
6185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6186 struct net_device *dev = info->user_ptr[1];
6187 struct wireless_dev *wdev = dev->ieee80211_ptr;
6188 struct sk_buff *msg;
6189 void *hdr;
6190 const u8 *addr;
6191 u64 cookie;
6192 int err;
6193
6194 if (wdev->iftype != NL80211_IFTYPE_AP &&
6195 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6196 return -EOPNOTSUPP;
6197
6198 if (!info->attrs[NL80211_ATTR_MAC])
6199 return -EINVAL;
6200
6201 if (!rdev->ops->probe_client)
6202 return -EOPNOTSUPP;
6203
6204 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6205 if (!msg)
6206 return -ENOMEM;
6207
6208 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6209 NL80211_CMD_PROBE_CLIENT);
6210
6211 if (IS_ERR(hdr)) {
6212 err = PTR_ERR(hdr);
6213 goto free_msg;
6214 }
6215
6216 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6217
6218 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6219 if (err)
6220 goto free_msg;
6221
6222 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6223
6224 genlmsg_end(msg, hdr);
6225
6226 return genlmsg_reply(msg, info);
6227
6228 nla_put_failure:
6229 err = -ENOBUFS;
6230 free_msg:
6231 nlmsg_free(msg);
6232 return err;
6233}
6234
Johannes Berg5e760232011-11-04 11:18:17 +01006235static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6236{
6237 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6238
6239 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6240 return -EOPNOTSUPP;
6241
6242 if (rdev->ap_beacons_nlpid)
6243 return -EBUSY;
6244
6245 rdev->ap_beacons_nlpid = info->snd_pid;
6246
6247 return 0;
6248}
6249
Johannes Berg4c476992010-10-04 21:36:35 +02006250#define NL80211_FLAG_NEED_WIPHY 0x01
6251#define NL80211_FLAG_NEED_NETDEV 0x02
6252#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006253#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6254#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6255 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006256
6257static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6258 struct genl_info *info)
6259{
6260 struct cfg80211_registered_device *rdev;
6261 struct net_device *dev;
6262 int err;
6263 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6264
6265 if (rtnl)
6266 rtnl_lock();
6267
6268 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6269 rdev = cfg80211_get_dev_from_info(info);
6270 if (IS_ERR(rdev)) {
6271 if (rtnl)
6272 rtnl_unlock();
6273 return PTR_ERR(rdev);
6274 }
6275 info->user_ptr[0] = rdev;
6276 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006277 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6278 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006279 if (err) {
6280 if (rtnl)
6281 rtnl_unlock();
6282 return err;
6283 }
Johannes Berg41265712010-10-04 21:14:05 +02006284 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6285 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006286 cfg80211_unlock_rdev(rdev);
6287 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006288 if (rtnl)
6289 rtnl_unlock();
6290 return -ENETDOWN;
6291 }
Johannes Berg4c476992010-10-04 21:36:35 +02006292 info->user_ptr[0] = rdev;
6293 info->user_ptr[1] = dev;
6294 }
6295
6296 return 0;
6297}
6298
6299static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6300 struct genl_info *info)
6301{
6302 if (info->user_ptr[0])
6303 cfg80211_unlock_rdev(info->user_ptr[0]);
6304 if (info->user_ptr[1])
6305 dev_put(info->user_ptr[1]);
6306 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6307 rtnl_unlock();
6308}
6309
Johannes Berg55682962007-09-20 13:09:35 -04006310static struct genl_ops nl80211_ops[] = {
6311 {
6312 .cmd = NL80211_CMD_GET_WIPHY,
6313 .doit = nl80211_get_wiphy,
6314 .dumpit = nl80211_dump_wiphy,
6315 .policy = nl80211_policy,
6316 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006317 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006318 },
6319 {
6320 .cmd = NL80211_CMD_SET_WIPHY,
6321 .doit = nl80211_set_wiphy,
6322 .policy = nl80211_policy,
6323 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006324 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006325 },
6326 {
6327 .cmd = NL80211_CMD_GET_INTERFACE,
6328 .doit = nl80211_get_interface,
6329 .dumpit = nl80211_dump_interface,
6330 .policy = nl80211_policy,
6331 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006332 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006333 },
6334 {
6335 .cmd = NL80211_CMD_SET_INTERFACE,
6336 .doit = nl80211_set_interface,
6337 .policy = nl80211_policy,
6338 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006339 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6340 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006341 },
6342 {
6343 .cmd = NL80211_CMD_NEW_INTERFACE,
6344 .doit = nl80211_new_interface,
6345 .policy = nl80211_policy,
6346 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006347 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6348 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006349 },
6350 {
6351 .cmd = NL80211_CMD_DEL_INTERFACE,
6352 .doit = nl80211_del_interface,
6353 .policy = nl80211_policy,
6354 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006355 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6356 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006357 },
Johannes Berg41ade002007-12-19 02:03:29 +01006358 {
6359 .cmd = NL80211_CMD_GET_KEY,
6360 .doit = nl80211_get_key,
6361 .policy = nl80211_policy,
6362 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006363 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6364 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006365 },
6366 {
6367 .cmd = NL80211_CMD_SET_KEY,
6368 .doit = nl80211_set_key,
6369 .policy = nl80211_policy,
6370 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006371 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006372 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006373 },
6374 {
6375 .cmd = NL80211_CMD_NEW_KEY,
6376 .doit = nl80211_new_key,
6377 .policy = nl80211_policy,
6378 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006379 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006380 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006381 },
6382 {
6383 .cmd = NL80211_CMD_DEL_KEY,
6384 .doit = nl80211_del_key,
6385 .policy = nl80211_policy,
6386 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006387 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006388 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006389 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006390 {
6391 .cmd = NL80211_CMD_SET_BEACON,
6392 .policy = nl80211_policy,
6393 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006394 .doit = nl80211_set_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006395 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6396 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006397 },
6398 {
Johannes Berg88600202012-02-13 15:17:18 +01006399 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006400 .policy = nl80211_policy,
6401 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006402 .doit = nl80211_start_ap,
Johannes Berg4c476992010-10-04 21:36:35 +02006403 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6404 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006405 },
6406 {
Johannes Berg88600202012-02-13 15:17:18 +01006407 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006408 .policy = nl80211_policy,
6409 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006410 .doit = nl80211_stop_ap,
Johannes Berg4c476992010-10-04 21:36:35 +02006411 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6412 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006413 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006414 {
6415 .cmd = NL80211_CMD_GET_STATION,
6416 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006417 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006418 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006419 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6420 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006421 },
6422 {
6423 .cmd = NL80211_CMD_SET_STATION,
6424 .doit = nl80211_set_station,
6425 .policy = nl80211_policy,
6426 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006427 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6428 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006429 },
6430 {
6431 .cmd = NL80211_CMD_NEW_STATION,
6432 .doit = nl80211_new_station,
6433 .policy = nl80211_policy,
6434 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006435 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006436 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006437 },
6438 {
6439 .cmd = NL80211_CMD_DEL_STATION,
6440 .doit = nl80211_del_station,
6441 .policy = nl80211_policy,
6442 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006443 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6444 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006445 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006446 {
6447 .cmd = NL80211_CMD_GET_MPATH,
6448 .doit = nl80211_get_mpath,
6449 .dumpit = nl80211_dump_mpath,
6450 .policy = nl80211_policy,
6451 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006452 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006453 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006454 },
6455 {
6456 .cmd = NL80211_CMD_SET_MPATH,
6457 .doit = nl80211_set_mpath,
6458 .policy = nl80211_policy,
6459 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006460 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006461 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006462 },
6463 {
6464 .cmd = NL80211_CMD_NEW_MPATH,
6465 .doit = nl80211_new_mpath,
6466 .policy = nl80211_policy,
6467 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006468 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006469 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006470 },
6471 {
6472 .cmd = NL80211_CMD_DEL_MPATH,
6473 .doit = nl80211_del_mpath,
6474 .policy = nl80211_policy,
6475 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006476 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6477 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006478 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006479 {
6480 .cmd = NL80211_CMD_SET_BSS,
6481 .doit = nl80211_set_bss,
6482 .policy = nl80211_policy,
6483 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006484 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6485 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006486 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006487 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006488 .cmd = NL80211_CMD_GET_REG,
6489 .doit = nl80211_get_reg,
6490 .policy = nl80211_policy,
6491 /* can be retrieved by unprivileged users */
6492 },
6493 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006494 .cmd = NL80211_CMD_SET_REG,
6495 .doit = nl80211_set_reg,
6496 .policy = nl80211_policy,
6497 .flags = GENL_ADMIN_PERM,
6498 },
6499 {
6500 .cmd = NL80211_CMD_REQ_SET_REG,
6501 .doit = nl80211_req_set_reg,
6502 .policy = nl80211_policy,
6503 .flags = GENL_ADMIN_PERM,
6504 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006505 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006506 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6507 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006508 .policy = nl80211_policy,
6509 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006510 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6511 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006512 },
6513 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006514 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6515 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006516 .policy = nl80211_policy,
6517 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006519 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006520 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006521 {
Johannes Berg2a519312009-02-10 21:25:55 +01006522 .cmd = NL80211_CMD_TRIGGER_SCAN,
6523 .doit = nl80211_trigger_scan,
6524 .policy = nl80211_policy,
6525 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006527 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006528 },
6529 {
6530 .cmd = NL80211_CMD_GET_SCAN,
6531 .policy = nl80211_policy,
6532 .dumpit = nl80211_dump_scan,
6533 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006534 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006535 .cmd = NL80211_CMD_START_SCHED_SCAN,
6536 .doit = nl80211_start_sched_scan,
6537 .policy = nl80211_policy,
6538 .flags = GENL_ADMIN_PERM,
6539 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6540 NL80211_FLAG_NEED_RTNL,
6541 },
6542 {
6543 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6544 .doit = nl80211_stop_sched_scan,
6545 .policy = nl80211_policy,
6546 .flags = GENL_ADMIN_PERM,
6547 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6548 NL80211_FLAG_NEED_RTNL,
6549 },
6550 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006551 .cmd = NL80211_CMD_AUTHENTICATE,
6552 .doit = nl80211_authenticate,
6553 .policy = nl80211_policy,
6554 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006555 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006556 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006557 },
6558 {
6559 .cmd = NL80211_CMD_ASSOCIATE,
6560 .doit = nl80211_associate,
6561 .policy = nl80211_policy,
6562 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006563 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006564 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006565 },
6566 {
6567 .cmd = NL80211_CMD_DEAUTHENTICATE,
6568 .doit = nl80211_deauthenticate,
6569 .policy = nl80211_policy,
6570 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006571 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006572 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006573 },
6574 {
6575 .cmd = NL80211_CMD_DISASSOCIATE,
6576 .doit = nl80211_disassociate,
6577 .policy = nl80211_policy,
6578 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006579 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006580 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006581 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006582 {
6583 .cmd = NL80211_CMD_JOIN_IBSS,
6584 .doit = nl80211_join_ibss,
6585 .policy = nl80211_policy,
6586 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006587 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006588 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006589 },
6590 {
6591 .cmd = NL80211_CMD_LEAVE_IBSS,
6592 .doit = nl80211_leave_ibss,
6593 .policy = nl80211_policy,
6594 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006595 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006596 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006597 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006598#ifdef CONFIG_NL80211_TESTMODE
6599 {
6600 .cmd = NL80211_CMD_TESTMODE,
6601 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006602 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006603 .policy = nl80211_policy,
6604 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006605 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6606 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006607 },
6608#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006609 {
6610 .cmd = NL80211_CMD_CONNECT,
6611 .doit = nl80211_connect,
6612 .policy = nl80211_policy,
6613 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006614 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006615 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006616 },
6617 {
6618 .cmd = NL80211_CMD_DISCONNECT,
6619 .doit = nl80211_disconnect,
6620 .policy = nl80211_policy,
6621 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006622 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006623 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006624 },
Johannes Berg463d0182009-07-14 00:33:35 +02006625 {
6626 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6627 .doit = nl80211_wiphy_netns,
6628 .policy = nl80211_policy,
6629 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006630 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6631 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006632 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006633 {
6634 .cmd = NL80211_CMD_GET_SURVEY,
6635 .policy = nl80211_policy,
6636 .dumpit = nl80211_dump_survey,
6637 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006638 {
6639 .cmd = NL80211_CMD_SET_PMKSA,
6640 .doit = nl80211_setdel_pmksa,
6641 .policy = nl80211_policy,
6642 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006643 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6644 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006645 },
6646 {
6647 .cmd = NL80211_CMD_DEL_PMKSA,
6648 .doit = nl80211_setdel_pmksa,
6649 .policy = nl80211_policy,
6650 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006651 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6652 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006653 },
6654 {
6655 .cmd = NL80211_CMD_FLUSH_PMKSA,
6656 .doit = nl80211_flush_pmksa,
6657 .policy = nl80211_policy,
6658 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006659 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6660 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006661 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006662 {
6663 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6664 .doit = nl80211_remain_on_channel,
6665 .policy = nl80211_policy,
6666 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006667 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006668 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006669 },
6670 {
6671 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6672 .doit = nl80211_cancel_remain_on_channel,
6673 .policy = nl80211_policy,
6674 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006675 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006676 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006677 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006678 {
6679 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6680 .doit = nl80211_set_tx_bitrate_mask,
6681 .policy = nl80211_policy,
6682 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006683 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6684 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006685 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006686 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006687 .cmd = NL80211_CMD_REGISTER_FRAME,
6688 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006689 .policy = nl80211_policy,
6690 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006691 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6692 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006693 },
6694 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006695 .cmd = NL80211_CMD_FRAME,
6696 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006697 .policy = nl80211_policy,
6698 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006699 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006700 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006701 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006702 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006703 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6704 .doit = nl80211_tx_mgmt_cancel_wait,
6705 .policy = nl80211_policy,
6706 .flags = GENL_ADMIN_PERM,
6707 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6708 NL80211_FLAG_NEED_RTNL,
6709 },
6710 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006711 .cmd = NL80211_CMD_SET_POWER_SAVE,
6712 .doit = nl80211_set_power_save,
6713 .policy = nl80211_policy,
6714 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006715 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6716 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006717 },
6718 {
6719 .cmd = NL80211_CMD_GET_POWER_SAVE,
6720 .doit = nl80211_get_power_save,
6721 .policy = nl80211_policy,
6722 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006723 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6724 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006725 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006726 {
6727 .cmd = NL80211_CMD_SET_CQM,
6728 .doit = nl80211_set_cqm,
6729 .policy = nl80211_policy,
6730 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006731 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6732 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006733 },
Johannes Bergf444de02010-05-05 15:25:02 +02006734 {
6735 .cmd = NL80211_CMD_SET_CHANNEL,
6736 .doit = nl80211_set_channel,
6737 .policy = nl80211_policy,
6738 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006739 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6740 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006741 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006742 {
6743 .cmd = NL80211_CMD_SET_WDS_PEER,
6744 .doit = nl80211_set_wds_peer,
6745 .policy = nl80211_policy,
6746 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006747 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6748 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006749 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006750 {
6751 .cmd = NL80211_CMD_JOIN_MESH,
6752 .doit = nl80211_join_mesh,
6753 .policy = nl80211_policy,
6754 .flags = GENL_ADMIN_PERM,
6755 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6756 NL80211_FLAG_NEED_RTNL,
6757 },
6758 {
6759 .cmd = NL80211_CMD_LEAVE_MESH,
6760 .doit = nl80211_leave_mesh,
6761 .policy = nl80211_policy,
6762 .flags = GENL_ADMIN_PERM,
6763 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6764 NL80211_FLAG_NEED_RTNL,
6765 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006766 {
6767 .cmd = NL80211_CMD_GET_WOWLAN,
6768 .doit = nl80211_get_wowlan,
6769 .policy = nl80211_policy,
6770 /* can be retrieved by unprivileged users */
6771 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6772 NL80211_FLAG_NEED_RTNL,
6773 },
6774 {
6775 .cmd = NL80211_CMD_SET_WOWLAN,
6776 .doit = nl80211_set_wowlan,
6777 .policy = nl80211_policy,
6778 .flags = GENL_ADMIN_PERM,
6779 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6780 NL80211_FLAG_NEED_RTNL,
6781 },
Johannes Berge5497d72011-07-05 16:35:40 +02006782 {
6783 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6784 .doit = nl80211_set_rekey_data,
6785 .policy = nl80211_policy,
6786 .flags = GENL_ADMIN_PERM,
6787 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6788 NL80211_FLAG_NEED_RTNL,
6789 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006790 {
6791 .cmd = NL80211_CMD_TDLS_MGMT,
6792 .doit = nl80211_tdls_mgmt,
6793 .policy = nl80211_policy,
6794 .flags = GENL_ADMIN_PERM,
6795 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6796 NL80211_FLAG_NEED_RTNL,
6797 },
6798 {
6799 .cmd = NL80211_CMD_TDLS_OPER,
6800 .doit = nl80211_tdls_oper,
6801 .policy = nl80211_policy,
6802 .flags = GENL_ADMIN_PERM,
6803 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6804 NL80211_FLAG_NEED_RTNL,
6805 },
Johannes Berg28946da2011-11-04 11:18:12 +01006806 {
6807 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6808 .doit = nl80211_register_unexpected_frame,
6809 .policy = nl80211_policy,
6810 .flags = GENL_ADMIN_PERM,
6811 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6812 NL80211_FLAG_NEED_RTNL,
6813 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006814 {
6815 .cmd = NL80211_CMD_PROBE_CLIENT,
6816 .doit = nl80211_probe_client,
6817 .policy = nl80211_policy,
6818 .flags = GENL_ADMIN_PERM,
6819 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6820 NL80211_FLAG_NEED_RTNL,
6821 },
Johannes Berg5e760232011-11-04 11:18:17 +01006822 {
6823 .cmd = NL80211_CMD_REGISTER_BEACONS,
6824 .doit = nl80211_register_beacons,
6825 .policy = nl80211_policy,
6826 .flags = GENL_ADMIN_PERM,
6827 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6828 NL80211_FLAG_NEED_RTNL,
6829 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006830 {
6831 .cmd = NL80211_CMD_SET_NOACK_MAP,
6832 .doit = nl80211_set_noack_map,
6833 .policy = nl80211_policy,
6834 .flags = GENL_ADMIN_PERM,
6835 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6836 NL80211_FLAG_NEED_RTNL,
6837 },
6838
Johannes Berg55682962007-09-20 13:09:35 -04006839};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006840
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006841static struct genl_multicast_group nl80211_mlme_mcgrp = {
6842 .name = "mlme",
6843};
Johannes Berg55682962007-09-20 13:09:35 -04006844
6845/* multicast groups */
6846static struct genl_multicast_group nl80211_config_mcgrp = {
6847 .name = "config",
6848};
Johannes Berg2a519312009-02-10 21:25:55 +01006849static struct genl_multicast_group nl80211_scan_mcgrp = {
6850 .name = "scan",
6851};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006852static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6853 .name = "regulatory",
6854};
Johannes Berg55682962007-09-20 13:09:35 -04006855
6856/* notification functions */
6857
6858void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6859{
6860 struct sk_buff *msg;
6861
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006863 if (!msg)
6864 return;
6865
6866 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6867 nlmsg_free(msg);
6868 return;
6869 }
6870
Johannes Berg463d0182009-07-14 00:33:35 +02006871 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6872 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006873}
6874
Johannes Berg362a4152009-05-24 16:43:15 +02006875static int nl80211_add_scan_req(struct sk_buff *msg,
6876 struct cfg80211_registered_device *rdev)
6877{
6878 struct cfg80211_scan_request *req = rdev->scan_req;
6879 struct nlattr *nest;
6880 int i;
6881
Johannes Berg667503d2009-07-07 03:56:11 +02006882 ASSERT_RDEV_LOCK(rdev);
6883
Johannes Berg362a4152009-05-24 16:43:15 +02006884 if (WARN_ON(!req))
6885 return 0;
6886
6887 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6888 if (!nest)
6889 goto nla_put_failure;
6890 for (i = 0; i < req->n_ssids; i++)
6891 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6892 nla_nest_end(msg, nest);
6893
6894 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6895 if (!nest)
6896 goto nla_put_failure;
6897 for (i = 0; i < req->n_channels; i++)
6898 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6899 nla_nest_end(msg, nest);
6900
6901 if (req->ie)
6902 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6903
6904 return 0;
6905 nla_put_failure:
6906 return -ENOBUFS;
6907}
6908
Johannes Berga538e2d2009-06-16 19:56:42 +02006909static int nl80211_send_scan_msg(struct sk_buff *msg,
6910 struct cfg80211_registered_device *rdev,
6911 struct net_device *netdev,
6912 u32 pid, u32 seq, int flags,
6913 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006914{
6915 void *hdr;
6916
6917 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6918 if (!hdr)
6919 return -1;
6920
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006921 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006922 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6923
Johannes Berg362a4152009-05-24 16:43:15 +02006924 /* ignore errors and send incomplete event anyway */
6925 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006926
6927 return genlmsg_end(msg, hdr);
6928
6929 nla_put_failure:
6930 genlmsg_cancel(msg, hdr);
6931 return -EMSGSIZE;
6932}
6933
Luciano Coelho807f8a82011-05-11 17:09:35 +03006934static int
6935nl80211_send_sched_scan_msg(struct sk_buff *msg,
6936 struct cfg80211_registered_device *rdev,
6937 struct net_device *netdev,
6938 u32 pid, u32 seq, int flags, u32 cmd)
6939{
6940 void *hdr;
6941
6942 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6943 if (!hdr)
6944 return -1;
6945
6946 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6947 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6948
6949 return genlmsg_end(msg, hdr);
6950
6951 nla_put_failure:
6952 genlmsg_cancel(msg, hdr);
6953 return -EMSGSIZE;
6954}
6955
Johannes Berga538e2d2009-06-16 19:56:42 +02006956void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6957 struct net_device *netdev)
6958{
6959 struct sk_buff *msg;
6960
6961 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6962 if (!msg)
6963 return;
6964
6965 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6966 NL80211_CMD_TRIGGER_SCAN) < 0) {
6967 nlmsg_free(msg);
6968 return;
6969 }
6970
Johannes Berg463d0182009-07-14 00:33:35 +02006971 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6972 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006973}
6974
Johannes Berg2a519312009-02-10 21:25:55 +01006975void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6976 struct net_device *netdev)
6977{
6978 struct sk_buff *msg;
6979
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006981 if (!msg)
6982 return;
6983
Johannes Berga538e2d2009-06-16 19:56:42 +02006984 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6985 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006986 nlmsg_free(msg);
6987 return;
6988 }
6989
Johannes Berg463d0182009-07-14 00:33:35 +02006990 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6991 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006992}
6993
6994void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6995 struct net_device *netdev)
6996{
6997 struct sk_buff *msg;
6998
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006999 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007000 if (!msg)
7001 return;
7002
Johannes Berga538e2d2009-06-16 19:56:42 +02007003 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7004 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007005 nlmsg_free(msg);
7006 return;
7007 }
7008
Johannes Berg463d0182009-07-14 00:33:35 +02007009 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7010 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007011}
7012
Luciano Coelho807f8a82011-05-11 17:09:35 +03007013void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7014 struct net_device *netdev)
7015{
7016 struct sk_buff *msg;
7017
7018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7019 if (!msg)
7020 return;
7021
7022 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7023 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7024 nlmsg_free(msg);
7025 return;
7026 }
7027
7028 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7029 nl80211_scan_mcgrp.id, GFP_KERNEL);
7030}
7031
7032void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7033 struct net_device *netdev, u32 cmd)
7034{
7035 struct sk_buff *msg;
7036
7037 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7038 if (!msg)
7039 return;
7040
7041 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7042 nlmsg_free(msg);
7043 return;
7044 }
7045
7046 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7047 nl80211_scan_mcgrp.id, GFP_KERNEL);
7048}
7049
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007050/*
7051 * This can happen on global regulatory changes or device specific settings
7052 * based on custom world regulatory domains.
7053 */
7054void nl80211_send_reg_change_event(struct regulatory_request *request)
7055{
7056 struct sk_buff *msg;
7057 void *hdr;
7058
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007059 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007060 if (!msg)
7061 return;
7062
7063 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7064 if (!hdr) {
7065 nlmsg_free(msg);
7066 return;
7067 }
7068
7069 /* Userspace can always count this one always being set */
7070 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
7071
7072 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
7073 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7074 NL80211_REGDOM_TYPE_WORLD);
7075 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
7076 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7077 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
7078 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7079 request->intersect)
7080 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7081 NL80211_REGDOM_TYPE_INTERSECTION);
7082 else {
7083 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7084 NL80211_REGDOM_TYPE_COUNTRY);
7085 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
7086 }
7087
7088 if (wiphy_idx_valid(request->wiphy_idx))
7089 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
7090
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007091 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007092
Johannes Bergbc43b282009-07-25 10:54:13 +02007093 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007094 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007095 GFP_ATOMIC);
7096 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007097
7098 return;
7099
7100nla_put_failure:
7101 genlmsg_cancel(msg, hdr);
7102 nlmsg_free(msg);
7103}
7104
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007105static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7106 struct net_device *netdev,
7107 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007108 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007109{
7110 struct sk_buff *msg;
7111 void *hdr;
7112
Johannes Berge6d6e342009-07-01 21:26:47 +02007113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007114 if (!msg)
7115 return;
7116
7117 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7118 if (!hdr) {
7119 nlmsg_free(msg);
7120 return;
7121 }
7122
7123 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7124 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7125 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7126
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007127 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007128
Johannes Berg463d0182009-07-14 00:33:35 +02007129 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7130 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007131 return;
7132
7133 nla_put_failure:
7134 genlmsg_cancel(msg, hdr);
7135 nlmsg_free(msg);
7136}
7137
7138void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007139 struct net_device *netdev, const u8 *buf,
7140 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007141{
7142 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007143 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007144}
7145
7146void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7147 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007148 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007149{
Johannes Berge6d6e342009-07-01 21:26:47 +02007150 nl80211_send_mlme_event(rdev, netdev, buf, len,
7151 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007152}
7153
Jouni Malinen53b46b82009-03-27 20:53:56 +02007154void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007155 struct net_device *netdev, const u8 *buf,
7156 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007157{
7158 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007159 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007160}
7161
Jouni Malinen53b46b82009-03-27 20:53:56 +02007162void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7163 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007164 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007165{
7166 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007167 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007168}
7169
Jouni Malinencf4e5942010-12-16 00:52:40 +02007170void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7171 struct net_device *netdev, const u8 *buf,
7172 size_t len, gfp_t gfp)
7173{
7174 nl80211_send_mlme_event(rdev, netdev, buf, len,
7175 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7176}
7177
7178void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7179 struct net_device *netdev, const u8 *buf,
7180 size_t len, gfp_t gfp)
7181{
7182 nl80211_send_mlme_event(rdev, netdev, buf, len,
7183 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7184}
7185
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007186static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7187 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007188 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007189{
7190 struct sk_buff *msg;
7191 void *hdr;
7192
Johannes Berge6d6e342009-07-01 21:26:47 +02007193 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007194 if (!msg)
7195 return;
7196
7197 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7198 if (!hdr) {
7199 nlmsg_free(msg);
7200 return;
7201 }
7202
7203 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7204 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7205 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7206 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7207
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007208 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007209
Johannes Berg463d0182009-07-14 00:33:35 +02007210 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7211 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007212 return;
7213
7214 nla_put_failure:
7215 genlmsg_cancel(msg, hdr);
7216 nlmsg_free(msg);
7217}
7218
7219void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007220 struct net_device *netdev, const u8 *addr,
7221 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007222{
7223 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007224 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007225}
7226
7227void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007228 struct net_device *netdev, const u8 *addr,
7229 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007230{
Johannes Berge6d6e342009-07-01 21:26:47 +02007231 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7232 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007233}
7234
Samuel Ortizb23aa672009-07-01 21:26:54 +02007235void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7236 struct net_device *netdev, const u8 *bssid,
7237 const u8 *req_ie, size_t req_ie_len,
7238 const u8 *resp_ie, size_t resp_ie_len,
7239 u16 status, gfp_t gfp)
7240{
7241 struct sk_buff *msg;
7242 void *hdr;
7243
7244 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7245 if (!msg)
7246 return;
7247
7248 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7249 if (!hdr) {
7250 nlmsg_free(msg);
7251 return;
7252 }
7253
7254 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7255 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7256 if (bssid)
7257 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7258 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7259 if (req_ie)
7260 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7261 if (resp_ie)
7262 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7263
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007264 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007265
Johannes Berg463d0182009-07-14 00:33:35 +02007266 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7267 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007268 return;
7269
7270 nla_put_failure:
7271 genlmsg_cancel(msg, hdr);
7272 nlmsg_free(msg);
7273
7274}
7275
7276void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7277 struct net_device *netdev, const u8 *bssid,
7278 const u8 *req_ie, size_t req_ie_len,
7279 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7280{
7281 struct sk_buff *msg;
7282 void *hdr;
7283
7284 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7285 if (!msg)
7286 return;
7287
7288 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7289 if (!hdr) {
7290 nlmsg_free(msg);
7291 return;
7292 }
7293
7294 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7295 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7296 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7297 if (req_ie)
7298 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7299 if (resp_ie)
7300 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7301
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007302 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007303
Johannes Berg463d0182009-07-14 00:33:35 +02007304 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7305 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007306 return;
7307
7308 nla_put_failure:
7309 genlmsg_cancel(msg, hdr);
7310 nlmsg_free(msg);
7311
7312}
7313
7314void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7315 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007316 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007317{
7318 struct sk_buff *msg;
7319 void *hdr;
7320
Johannes Berg667503d2009-07-07 03:56:11 +02007321 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007322 if (!msg)
7323 return;
7324
7325 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7326 if (!hdr) {
7327 nlmsg_free(msg);
7328 return;
7329 }
7330
7331 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7332 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7333 if (from_ap && reason)
7334 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7335 if (from_ap)
7336 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7337 if (ie)
7338 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7339
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007340 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007341
Johannes Berg463d0182009-07-14 00:33:35 +02007342 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7343 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007344 return;
7345
7346 nla_put_failure:
7347 genlmsg_cancel(msg, hdr);
7348 nlmsg_free(msg);
7349
7350}
7351
Johannes Berg04a773a2009-04-19 21:24:32 +02007352void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7353 struct net_device *netdev, const u8 *bssid,
7354 gfp_t gfp)
7355{
7356 struct sk_buff *msg;
7357 void *hdr;
7358
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007360 if (!msg)
7361 return;
7362
7363 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7364 if (!hdr) {
7365 nlmsg_free(msg);
7366 return;
7367 }
7368
7369 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7370 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7371 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7372
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007373 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007374
Johannes Berg463d0182009-07-14 00:33:35 +02007375 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7376 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007377 return;
7378
7379 nla_put_failure:
7380 genlmsg_cancel(msg, hdr);
7381 nlmsg_free(msg);
7382}
7383
Javier Cardonac93b5e72011-04-07 15:08:34 -07007384void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7385 struct net_device *netdev,
7386 const u8 *macaddr, const u8* ie, u8 ie_len,
7387 gfp_t gfp)
7388{
7389 struct sk_buff *msg;
7390 void *hdr;
7391
7392 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7393 if (!msg)
7394 return;
7395
7396 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7397 if (!hdr) {
7398 nlmsg_free(msg);
7399 return;
7400 }
7401
7402 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7403 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7404 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7405 if (ie_len && ie)
7406 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7407
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007408 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007409
7410 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7411 nl80211_mlme_mcgrp.id, gfp);
7412 return;
7413
7414 nla_put_failure:
7415 genlmsg_cancel(msg, hdr);
7416 nlmsg_free(msg);
7417}
7418
Jouni Malinena3b8b052009-03-27 21:59:49 +02007419void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7420 struct net_device *netdev, const u8 *addr,
7421 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007422 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007423{
7424 struct sk_buff *msg;
7425 void *hdr;
7426
Johannes Berge6d6e342009-07-01 21:26:47 +02007427 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007428 if (!msg)
7429 return;
7430
7431 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7432 if (!hdr) {
7433 nlmsg_free(msg);
7434 return;
7435 }
7436
7437 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7438 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7439 if (addr)
7440 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7441 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007442 if (key_id != -1)
7443 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007444 if (tsc)
7445 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7446
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007447 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007448
Johannes Berg463d0182009-07-14 00:33:35 +02007449 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7450 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007451 return;
7452
7453 nla_put_failure:
7454 genlmsg_cancel(msg, hdr);
7455 nlmsg_free(msg);
7456}
7457
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007458void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7459 struct ieee80211_channel *channel_before,
7460 struct ieee80211_channel *channel_after)
7461{
7462 struct sk_buff *msg;
7463 void *hdr;
7464 struct nlattr *nl_freq;
7465
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007467 if (!msg)
7468 return;
7469
7470 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7471 if (!hdr) {
7472 nlmsg_free(msg);
7473 return;
7474 }
7475
7476 /*
7477 * Since we are applying the beacon hint to a wiphy we know its
7478 * wiphy_idx is valid
7479 */
7480 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7481
7482 /* Before */
7483 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7484 if (!nl_freq)
7485 goto nla_put_failure;
7486 if (nl80211_msg_put_channel(msg, channel_before))
7487 goto nla_put_failure;
7488 nla_nest_end(msg, nl_freq);
7489
7490 /* After */
7491 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7492 if (!nl_freq)
7493 goto nla_put_failure;
7494 if (nl80211_msg_put_channel(msg, channel_after))
7495 goto nla_put_failure;
7496 nla_nest_end(msg, nl_freq);
7497
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007498 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007499
Johannes Berg463d0182009-07-14 00:33:35 +02007500 rcu_read_lock();
7501 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7502 GFP_ATOMIC);
7503 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007504
7505 return;
7506
7507nla_put_failure:
7508 genlmsg_cancel(msg, hdr);
7509 nlmsg_free(msg);
7510}
7511
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007512static void nl80211_send_remain_on_chan_event(
7513 int cmd, struct cfg80211_registered_device *rdev,
7514 struct net_device *netdev, u64 cookie,
7515 struct ieee80211_channel *chan,
7516 enum nl80211_channel_type channel_type,
7517 unsigned int duration, gfp_t gfp)
7518{
7519 struct sk_buff *msg;
7520 void *hdr;
7521
7522 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7523 if (!msg)
7524 return;
7525
7526 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7527 if (!hdr) {
7528 nlmsg_free(msg);
7529 return;
7530 }
7531
7532 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7533 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7534 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7535 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7536 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7537
7538 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7539 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7540
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007541 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007542
7543 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7544 nl80211_mlme_mcgrp.id, gfp);
7545 return;
7546
7547 nla_put_failure:
7548 genlmsg_cancel(msg, hdr);
7549 nlmsg_free(msg);
7550}
7551
7552void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7553 struct net_device *netdev, u64 cookie,
7554 struct ieee80211_channel *chan,
7555 enum nl80211_channel_type channel_type,
7556 unsigned int duration, gfp_t gfp)
7557{
7558 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7559 rdev, netdev, cookie, chan,
7560 channel_type, duration, gfp);
7561}
7562
7563void nl80211_send_remain_on_channel_cancel(
7564 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7565 u64 cookie, struct ieee80211_channel *chan,
7566 enum nl80211_channel_type channel_type, gfp_t gfp)
7567{
7568 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7569 rdev, netdev, cookie, chan,
7570 channel_type, 0, gfp);
7571}
7572
Johannes Berg98b62182009-12-23 13:15:44 +01007573void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7574 struct net_device *dev, const u8 *mac_addr,
7575 struct station_info *sinfo, gfp_t gfp)
7576{
7577 struct sk_buff *msg;
7578
7579 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7580 if (!msg)
7581 return;
7582
7583 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7584 nlmsg_free(msg);
7585 return;
7586 }
7587
7588 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7589 nl80211_mlme_mcgrp.id, gfp);
7590}
7591
Jouni Malinenec15e682011-03-23 15:29:52 +02007592void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7593 struct net_device *dev, const u8 *mac_addr,
7594 gfp_t gfp)
7595{
7596 struct sk_buff *msg;
7597 void *hdr;
7598
7599 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7600 if (!msg)
7601 return;
7602
7603 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7604 if (!hdr) {
7605 nlmsg_free(msg);
7606 return;
7607 }
7608
7609 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7610 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7611
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007612 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007613
7614 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7615 nl80211_mlme_mcgrp.id, gfp);
7616 return;
7617
7618 nla_put_failure:
7619 genlmsg_cancel(msg, hdr);
7620 nlmsg_free(msg);
7621}
7622
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007623static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7624 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007625{
7626 struct wireless_dev *wdev = dev->ieee80211_ptr;
7627 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7628 struct sk_buff *msg;
7629 void *hdr;
7630 int err;
7631 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7632
7633 if (!nlpid)
7634 return false;
7635
7636 msg = nlmsg_new(100, gfp);
7637 if (!msg)
7638 return true;
7639
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007640 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007641 if (!hdr) {
7642 nlmsg_free(msg);
7643 return true;
7644 }
7645
7646 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7647 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7648 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7649
7650 err = genlmsg_end(msg, hdr);
7651 if (err < 0) {
7652 nlmsg_free(msg);
7653 return true;
7654 }
7655
7656 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7657 return true;
7658
7659 nla_put_failure:
7660 genlmsg_cancel(msg, hdr);
7661 nlmsg_free(msg);
7662 return true;
7663}
7664
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007665bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7666{
7667 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7668 addr, gfp);
7669}
7670
7671bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7672 const u8 *addr, gfp_t gfp)
7673{
7674 return __nl80211_unexpected_frame(dev,
7675 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7676 addr, gfp);
7677}
7678
Johannes Berg2e161f72010-08-12 15:38:38 +02007679int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7680 struct net_device *netdev, u32 nlpid,
7681 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007682{
7683 struct sk_buff *msg;
7684 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007685
7686 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7687 if (!msg)
7688 return -ENOMEM;
7689
Johannes Berg2e161f72010-08-12 15:38:38 +02007690 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007691 if (!hdr) {
7692 nlmsg_free(msg);
7693 return -ENOMEM;
7694 }
7695
7696 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7697 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7698 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7699 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7700
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007701 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007702
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007703 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007704
7705 nla_put_failure:
7706 genlmsg_cancel(msg, hdr);
7707 nlmsg_free(msg);
7708 return -ENOBUFS;
7709}
7710
Johannes Berg2e161f72010-08-12 15:38:38 +02007711void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7712 struct net_device *netdev, u64 cookie,
7713 const u8 *buf, size_t len, bool ack,
7714 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007715{
7716 struct sk_buff *msg;
7717 void *hdr;
7718
7719 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7720 if (!msg)
7721 return;
7722
Johannes Berg2e161f72010-08-12 15:38:38 +02007723 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007724 if (!hdr) {
7725 nlmsg_free(msg);
7726 return;
7727 }
7728
7729 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7730 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7731 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7732 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7733 if (ack)
7734 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7735
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007736 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007737
7738 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7739 return;
7740
7741 nla_put_failure:
7742 genlmsg_cancel(msg, hdr);
7743 nlmsg_free(msg);
7744}
7745
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007746void
7747nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7748 struct net_device *netdev,
7749 enum nl80211_cqm_rssi_threshold_event rssi_event,
7750 gfp_t gfp)
7751{
7752 struct sk_buff *msg;
7753 struct nlattr *pinfoattr;
7754 void *hdr;
7755
7756 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7757 if (!msg)
7758 return;
7759
7760 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7761 if (!hdr) {
7762 nlmsg_free(msg);
7763 return;
7764 }
7765
7766 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7767 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7768
7769 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7770 if (!pinfoattr)
7771 goto nla_put_failure;
7772
7773 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7774 rssi_event);
7775
7776 nla_nest_end(msg, pinfoattr);
7777
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007778 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007779
7780 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7781 nl80211_mlme_mcgrp.id, gfp);
7782 return;
7783
7784 nla_put_failure:
7785 genlmsg_cancel(msg, hdr);
7786 nlmsg_free(msg);
7787}
7788
Johannes Berge5497d72011-07-05 16:35:40 +02007789void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7790 struct net_device *netdev, const u8 *bssid,
7791 const u8 *replay_ctr, gfp_t gfp)
7792{
7793 struct sk_buff *msg;
7794 struct nlattr *rekey_attr;
7795 void *hdr;
7796
7797 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7798 if (!msg)
7799 return;
7800
7801 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7802 if (!hdr) {
7803 nlmsg_free(msg);
7804 return;
7805 }
7806
7807 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7808 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7809 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7810
7811 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7812 if (!rekey_attr)
7813 goto nla_put_failure;
7814
7815 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7816 NL80211_REPLAY_CTR_LEN, replay_ctr);
7817
7818 nla_nest_end(msg, rekey_attr);
7819
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007820 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007821
7822 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7823 nl80211_mlme_mcgrp.id, gfp);
7824 return;
7825
7826 nla_put_failure:
7827 genlmsg_cancel(msg, hdr);
7828 nlmsg_free(msg);
7829}
7830
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007831void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7832 struct net_device *netdev, int index,
7833 const u8 *bssid, bool preauth, gfp_t gfp)
7834{
7835 struct sk_buff *msg;
7836 struct nlattr *attr;
7837 void *hdr;
7838
7839 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7840 if (!msg)
7841 return;
7842
7843 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7844 if (!hdr) {
7845 nlmsg_free(msg);
7846 return;
7847 }
7848
7849 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7851
7852 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7853 if (!attr)
7854 goto nla_put_failure;
7855
7856 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7857 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7858 if (preauth)
7859 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7860
7861 nla_nest_end(msg, attr);
7862
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007863 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007864
7865 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7866 nl80211_mlme_mcgrp.id, gfp);
7867 return;
7868
7869 nla_put_failure:
7870 genlmsg_cancel(msg, hdr);
7871 nlmsg_free(msg);
7872}
7873
Johannes Bergc063dbf2010-11-24 08:10:05 +01007874void
7875nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7876 struct net_device *netdev, const u8 *peer,
7877 u32 num_packets, gfp_t gfp)
7878{
7879 struct sk_buff *msg;
7880 struct nlattr *pinfoattr;
7881 void *hdr;
7882
7883 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7884 if (!msg)
7885 return;
7886
7887 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7888 if (!hdr) {
7889 nlmsg_free(msg);
7890 return;
7891 }
7892
7893 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7894 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7895 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7896
7897 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7898 if (!pinfoattr)
7899 goto nla_put_failure;
7900
7901 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7902
7903 nla_nest_end(msg, pinfoattr);
7904
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007905 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007906
7907 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7908 nl80211_mlme_mcgrp.id, gfp);
7909 return;
7910
7911 nla_put_failure:
7912 genlmsg_cancel(msg, hdr);
7913 nlmsg_free(msg);
7914}
7915
Johannes Berg7f6cf312011-11-04 11:18:15 +01007916void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7917 u64 cookie, bool acked, gfp_t gfp)
7918{
7919 struct wireless_dev *wdev = dev->ieee80211_ptr;
7920 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7921 struct sk_buff *msg;
7922 void *hdr;
7923 int err;
7924
7925 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7926 if (!msg)
7927 return;
7928
7929 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7930 if (!hdr) {
7931 nlmsg_free(msg);
7932 return;
7933 }
7934
7935 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7936 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7937 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7938 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7939 if (acked)
7940 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7941
7942 err = genlmsg_end(msg, hdr);
7943 if (err < 0) {
7944 nlmsg_free(msg);
7945 return;
7946 }
7947
7948 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7949 nl80211_mlme_mcgrp.id, gfp);
7950 return;
7951
7952 nla_put_failure:
7953 genlmsg_cancel(msg, hdr);
7954 nlmsg_free(msg);
7955}
7956EXPORT_SYMBOL(cfg80211_probe_status);
7957
Johannes Berg5e760232011-11-04 11:18:17 +01007958void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7959 const u8 *frame, size_t len,
7960 int freq, gfp_t gfp)
7961{
7962 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7963 struct sk_buff *msg;
7964 void *hdr;
7965 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7966
7967 if (!nlpid)
7968 return;
7969
7970 msg = nlmsg_new(len + 100, gfp);
7971 if (!msg)
7972 return;
7973
7974 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7975 if (!hdr) {
7976 nlmsg_free(msg);
7977 return;
7978 }
7979
7980 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7981 if (freq)
7982 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7983 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7984
7985 genlmsg_end(msg, hdr);
7986
7987 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7988 return;
7989
7990 nla_put_failure:
7991 genlmsg_cancel(msg, hdr);
7992 nlmsg_free(msg);
7993}
7994EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7995
Jouni Malinen026331c2010-02-15 12:53:10 +02007996static int nl80211_netlink_notify(struct notifier_block * nb,
7997 unsigned long state,
7998 void *_notify)
7999{
8000 struct netlink_notify *notify = _notify;
8001 struct cfg80211_registered_device *rdev;
8002 struct wireless_dev *wdev;
8003
8004 if (state != NETLINK_URELEASE)
8005 return NOTIFY_DONE;
8006
8007 rcu_read_lock();
8008
Johannes Berg5e760232011-11-04 11:18:17 +01008009 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008010 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008011 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008012 if (rdev->ap_beacons_nlpid == notify->pid)
8013 rdev->ap_beacons_nlpid = 0;
8014 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008015
8016 rcu_read_unlock();
8017
8018 return NOTIFY_DONE;
8019}
8020
8021static struct notifier_block nl80211_netlink_notifier = {
8022 .notifier_call = nl80211_netlink_notify,
8023};
8024
Johannes Berg55682962007-09-20 13:09:35 -04008025/* initialisation/exit functions */
8026
8027int nl80211_init(void)
8028{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008029 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008030
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008031 err = genl_register_family_with_ops(&nl80211_fam,
8032 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008033 if (err)
8034 return err;
8035
Johannes Berg55682962007-09-20 13:09:35 -04008036 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8037 if (err)
8038 goto err_out;
8039
Johannes Berg2a519312009-02-10 21:25:55 +01008040 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8041 if (err)
8042 goto err_out;
8043
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008044 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8045 if (err)
8046 goto err_out;
8047
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008048 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8049 if (err)
8050 goto err_out;
8051
Johannes Bergaff89a92009-07-01 21:26:51 +02008052#ifdef CONFIG_NL80211_TESTMODE
8053 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8054 if (err)
8055 goto err_out;
8056#endif
8057
Jouni Malinen026331c2010-02-15 12:53:10 +02008058 err = netlink_register_notifier(&nl80211_netlink_notifier);
8059 if (err)
8060 goto err_out;
8061
Johannes Berg55682962007-09-20 13:09:35 -04008062 return 0;
8063 err_out:
8064 genl_unregister_family(&nl80211_fam);
8065 return err;
8066}
8067
8068void nl80211_exit(void)
8069{
Jouni Malinen026331c2010-02-15 12:53:10 +02008070 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008071 genl_unregister_family(&nl80211_fam);
8072}