blob: 50970365ba547a54e83c52fde39e1d1af68a5397 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg00918d32011-12-13 17:22:05 +010050static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
Johannes Berg55682962007-09-20 13:09:35 -040053{
54 int ifindex;
55
Johannes Bergbba95fe2008-07-29 13:22:51 +020056 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040057 return -EINVAL;
58
Johannes Bergbba95fe2008-07-29 13:22:51 +020059 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg00918d32011-12-13 17:22:05 +010060 *dev = dev_get_by_index(netns, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040061 if (!*dev)
62 return -ENODEV;
63
Johannes Berg00918d32011-12-13 17:22:05 +010064 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020065 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040066 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020067 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040068 }
69
70 return 0;
71}
72
73/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040075 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070077 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020078 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020079 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053080 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020081 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010085 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040086
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010090
Eliad Pellere007b852011-11-24 18:13:56 +020091 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010093
Johannes Bergb9454e82009-07-08 13:29:08 +020094 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010095 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100102
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300130
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700133
Johannes Berg6c739412011-11-03 09:27:01 +0100134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200135
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200141
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
151 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
205 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530207 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530208 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Mahesh Palivelacf7f78d2012-10-11 08:04:52 +0000209 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
210 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
211 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
212 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Jouni Malinen1400ced2013-02-14 19:10:13 +0000213 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
214 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg55682962007-09-20 13:09:35 -0400215};
216
Johannes Berge31b8212010-10-05 19:39:30 +0200217/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000218static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200219 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200220 [NL80211_KEY_IDX] = { .type = NLA_U8 },
221 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200222 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200223 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
224 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200225 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100226 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
227};
228
229/* policy for the key default flags */
230static const struct nla_policy
231nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
232 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
233 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200234};
235
Johannes Bergff1b6e62011-05-04 15:37:28 +0200236/* policy for WoWLAN attributes */
237static const struct nla_policy
238nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
239 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
240 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
241 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
242 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200243 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
244 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
245 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
246 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200247};
248
Johannes Berge5497d72011-07-05 16:35:40 +0200249/* policy for GTK rekey offload attributes */
250static const struct nla_policy
251nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
252 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
253 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
254 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
255};
256
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300257static const struct nla_policy
258nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
259 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
260 .len = IEEE80211_MAX_SSID_LEN },
261};
262
Holger Schuriga0438972009-11-11 11:30:02 +0100263/* ifidx get helper */
264static int nl80211_get_ifidx(struct netlink_callback *cb)
265{
266 int res;
267
268 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
269 nl80211_fam.attrbuf, nl80211_fam.maxattr,
270 nl80211_policy);
271 if (res)
272 return res;
273
274 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
275 return -EINVAL;
276
277 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
278 if (!res)
279 return -EINVAL;
280 return res;
281}
282
Johannes Berg67748892010-10-04 21:14:06 +0200283static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
284 struct netlink_callback *cb,
285 struct cfg80211_registered_device **rdev,
286 struct net_device **dev)
287{
288 int ifidx = cb->args[0];
289 int err;
290
291 if (!ifidx)
292 ifidx = nl80211_get_ifidx(cb);
293 if (ifidx < 0)
294 return ifidx;
295
296 cb->args[0] = ifidx;
297
298 rtnl_lock();
299
300 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
301 if (!*dev) {
302 err = -ENODEV;
303 goto out_rtnl;
304 }
305
306 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100307 if (IS_ERR(*rdev)) {
308 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200309 goto out_rtnl;
310 }
311
312 return 0;
313 out_rtnl:
314 rtnl_unlock();
315 return err;
316}
317
318static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
319{
320 cfg80211_unlock_rdev(rdev);
321 rtnl_unlock();
322}
323
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100324/* IE validation */
325static bool is_valid_ie_attr(const struct nlattr *attr)
326{
327 const u8 *pos;
328 int len;
329
330 if (!attr)
331 return true;
332
333 pos = nla_data(attr);
334 len = nla_len(attr);
335
336 while (len) {
337 u8 elemlen;
338
339 if (len < 2)
340 return false;
341 len -= 2;
342
343 elemlen = pos[1];
344 if (elemlen > len)
345 return false;
346
347 len -= elemlen;
348 pos += 2 + elemlen;
349 }
350
351 return true;
352}
353
Johannes Berg55682962007-09-20 13:09:35 -0400354/* message building helper */
355static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
356 int flags, u8 cmd)
357{
358 /* since there is no private header just add the generic one */
359 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
360}
361
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400362static int nl80211_msg_put_channel(struct sk_buff *msg,
363 struct ieee80211_channel *chan)
364{
365 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
366 chan->center_freq);
367
368 if (chan->flags & IEEE80211_CHAN_DISABLED)
369 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
370 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
371 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
372 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
373 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
374 if (chan->flags & IEEE80211_CHAN_RADAR)
375 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
376
377 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
378 DBM_TO_MBM(chan->max_power));
379
380 return 0;
381
382 nla_put_failure:
383 return -ENOBUFS;
384}
385
Johannes Berg55682962007-09-20 13:09:35 -0400386/* netlink command implementations */
387
Johannes Bergb9454e82009-07-08 13:29:08 +0200388struct key_parse {
389 struct key_params p;
390 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394};
395
396static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
397{
398 struct nlattr *tb[NL80211_KEY_MAX + 1];
399 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
400 nl80211_key_policy);
401 if (err)
402 return err;
403
404 k->def = !!tb[NL80211_KEY_DEFAULT];
405 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (k->def) {
408 k->def_uni = true;
409 k->def_multi = true;
410 }
411 if (k->defmgmt)
412 k->def_multi = true;
413
Johannes Bergb9454e82009-07-08 13:29:08 +0200414 if (tb[NL80211_KEY_IDX])
415 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
416
417 if (tb[NL80211_KEY_DATA]) {
418 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
419 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
420 }
421
422 if (tb[NL80211_KEY_SEQ]) {
423 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
424 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
425 }
426
427 if (tb[NL80211_KEY_CIPHER])
428 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
429
Johannes Berge31b8212010-10-05 19:39:30 +0200430 if (tb[NL80211_KEY_TYPE]) {
431 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
432 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
433 return -EINVAL;
434 }
435
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100436 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
437 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100438 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
439 tb[NL80211_KEY_DEFAULT_TYPES],
440 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100441 if (err)
442 return err;
443
444 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
445 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
446 }
447
Johannes Bergb9454e82009-07-08 13:29:08 +0200448 return 0;
449}
450
451static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
452{
453 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
454 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
455 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
456 }
457
458 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
459 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
460 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
461 }
462
463 if (info->attrs[NL80211_ATTR_KEY_IDX])
464 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
465
466 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
467 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
468
469 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
470 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
471
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100472 if (k->def) {
473 k->def_uni = true;
474 k->def_multi = true;
475 }
476 if (k->defmgmt)
477 k->def_multi = true;
478
Johannes Berge31b8212010-10-05 19:39:30 +0200479 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
480 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
481 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
482 return -EINVAL;
483 }
484
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100485 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
486 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
487 int err = nla_parse_nested(
488 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
489 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
490 nl80211_key_default_policy);
491 if (err)
492 return err;
493
494 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
495 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
496 }
497
Johannes Bergb9454e82009-07-08 13:29:08 +0200498 return 0;
499}
500
501static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
502{
503 int err;
504
505 memset(k, 0, sizeof(*k));
506 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200507 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200508
509 if (info->attrs[NL80211_ATTR_KEY])
510 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
511 else
512 err = nl80211_parse_key_old(info, k);
513
514 if (err)
515 return err;
516
517 if (k->def && k->defmgmt)
518 return -EINVAL;
519
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (k->defmgmt) {
521 if (k->def_uni || !k->def_multi)
522 return -EINVAL;
523 }
524
Johannes Bergb9454e82009-07-08 13:29:08 +0200525 if (k->idx != -1) {
526 if (k->defmgmt) {
527 if (k->idx < 4 || k->idx > 5)
528 return -EINVAL;
529 } else if (k->def) {
530 if (k->idx < 0 || k->idx > 3)
531 return -EINVAL;
532 } else {
533 if (k->idx < 0 || k->idx > 5)
534 return -EINVAL;
535 }
536 }
537
538 return 0;
539}
540
Johannes Bergfffd0932009-07-08 14:22:54 +0200541static struct cfg80211_cached_keys *
542nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
543 struct nlattr *keys)
544{
545 struct key_parse parse;
546 struct nlattr *key;
547 struct cfg80211_cached_keys *result;
548 int rem, err, def = 0;
549
550 result = kzalloc(sizeof(*result), GFP_KERNEL);
551 if (!result)
552 return ERR_PTR(-ENOMEM);
553
554 result->def = -1;
555 result->defmgmt = -1;
556
557 nla_for_each_nested(key, keys, rem) {
558 memset(&parse, 0, sizeof(parse));
559 parse.idx = -1;
560
561 err = nl80211_parse_key_new(key, &parse);
562 if (err)
563 goto error;
564 err = -EINVAL;
565 if (!parse.p.key)
566 goto error;
567 if (parse.idx < 0 || parse.idx > 4)
568 goto error;
569 if (parse.def) {
570 if (def)
571 goto error;
572 def = 1;
573 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100574 if (!parse.def_uni || !parse.def_multi)
575 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200576 } else if (parse.defmgmt)
577 goto error;
578 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200579 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200580 if (err)
581 goto error;
582 result->params[parse.idx].cipher = parse.p.cipher;
583 result->params[parse.idx].key_len = parse.p.key_len;
584 result->params[parse.idx].key = result->data[parse.idx];
585 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
586 }
587
588 return result;
589 error:
590 kfree(result);
591 return ERR_PTR(err);
592}
593
594static int nl80211_key_allowed(struct wireless_dev *wdev)
595{
596 ASSERT_WDEV_LOCK(wdev);
597
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 switch (wdev->iftype) {
599 case NL80211_IFTYPE_AP:
600 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200601 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700602 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200603 break;
604 case NL80211_IFTYPE_ADHOC:
605 if (!wdev->current_bss)
606 return -ENOLINK;
607 break;
608 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200609 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200610 if (wdev->sme_state != CFG80211_SME_CONNECTED)
611 return -ENOLINK;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 return 0;
618}
619
Johannes Berg7527a782011-05-13 10:58:57 +0200620static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
621{
622 struct nlattr *nl_modes = nla_nest_start(msg, attr);
623 int i;
624
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
630 if (ifmodes & 1)
631 NLA_PUT_FLAG(msg, i);
632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637 return 0;
638
639nla_put_failure:
640 return -ENOBUFS;
641}
642
643static int nl80211_put_iface_combinations(struct wiphy *wiphy,
644 struct sk_buff *msg)
645{
646 struct nlattr *nl_combis;
647 int i, j;
648
649 nl_combis = nla_nest_start(msg,
650 NL80211_ATTR_INTERFACE_COMBINATIONS);
651 if (!nl_combis)
652 goto nla_put_failure;
653
654 for (i = 0; i < wiphy->n_iface_combinations; i++) {
655 const struct ieee80211_iface_combination *c;
656 struct nlattr *nl_combi, *nl_limits;
657
658 c = &wiphy->iface_combinations[i];
659
660 nl_combi = nla_nest_start(msg, i + 1);
661 if (!nl_combi)
662 goto nla_put_failure;
663
664 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
665 if (!nl_limits)
666 goto nla_put_failure;
667
668 for (j = 0; j < c->n_limits; j++) {
669 struct nlattr *nl_limit;
670
671 nl_limit = nla_nest_start(msg, j + 1);
672 if (!nl_limit)
673 goto nla_put_failure;
674 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
675 c->limits[j].max);
676 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
677 c->limits[j].types))
678 goto nla_put_failure;
679 nla_nest_end(msg, nl_limit);
680 }
681
682 nla_nest_end(msg, nl_limits);
683
684 if (c->beacon_int_infra_match)
685 NLA_PUT_FLAG(msg,
686 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
687 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
688 c->num_different_channels);
689 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
690 c->max_interfaces);
691
692 nla_nest_end(msg, nl_combi);
693 }
694
695 nla_nest_end(msg, nl_combis);
696
697 return 0;
698nla_put_failure:
699 return -ENOBUFS;
700}
701
Johannes Berg55682962007-09-20 13:09:35 -0400702static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
703 struct cfg80211_registered_device *dev)
704{
705 void *hdr;
Johannes Bergee688b02008-01-24 19:38:39 +0100706 struct nlattr *nl_bands, *nl_band;
707 struct nlattr *nl_freqs, *nl_freq;
708 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100709 struct nlattr *nl_cmds;
Johannes Bergee688b02008-01-24 19:38:39 +0100710 enum ieee80211_band band;
711 struct ieee80211_channel *chan;
712 struct ieee80211_rate *rate;
713 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200714 const struct ieee80211_txrx_stypes *mgmt_stypes =
715 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400716
717 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
718 if (!hdr)
719 return -1;
720
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500721 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400722 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200723
Johannes Bergf5ea9122009-08-07 16:17:38 +0200724 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
725 cfg80211_rdev_list_generation);
726
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200727 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
728 dev->wiphy.retry_short);
729 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
730 dev->wiphy.retry_long);
731 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
732 dev->wiphy.frag_threshold);
733 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
734 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100735 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
736 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100737 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
738 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300739 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
740 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200741 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
742 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300743 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
744 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300745 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
746 dev->wiphy.max_match_sets);
Johannes Bergee688b02008-01-24 19:38:39 +0100747
Johannes Berge31b8212010-10-05 19:39:30 +0200748 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
749 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700750 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
751 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300752 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
753 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530754 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
755 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300756 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
757 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
758 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
759 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530760
Johannes Berg25e47c12009-04-02 20:14:06 +0200761 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
762 sizeof(u32) * dev->wiphy.n_cipher_suites,
763 dev->wiphy.cipher_suites);
764
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100765 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
766 dev->wiphy.max_num_pmkids);
767
Johannes Bergc0692b82010-08-27 14:26:53 +0300768 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
769 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
770
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900771 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
772 dev->wiphy.available_antennas_tx);
773 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
774 dev->wiphy.available_antennas_rx);
775
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200776 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
777 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
778 dev->wiphy.probe_resp_offload);
779
Bruno Randolf7f531e02010-12-16 11:30:22 +0900780 if ((dev->wiphy.available_antennas_tx ||
781 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900782 u32 tx_ant = 0, rx_ant = 0;
783 int res;
784 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
785 if (!res) {
786 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
787 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
788 }
789 }
790
Johannes Berg7527a782011-05-13 10:58:57 +0200791 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
792 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700793 goto nla_put_failure;
794
Johannes Bergee688b02008-01-24 19:38:39 +0100795 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
796 if (!nl_bands)
797 goto nla_put_failure;
798
799 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
800 if (!dev->wiphy.bands[band])
801 continue;
802
803 nl_band = nla_nest_start(msg, band);
804 if (!nl_band)
805 goto nla_put_failure;
806
Johannes Bergd51626d2008-10-09 12:20:13 +0200807 /* add HT info */
808 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
809 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
810 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
811 &dev->wiphy.bands[band]->ht_cap.mcs);
812 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
813 dev->wiphy.bands[band]->ht_cap.cap);
814 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
815 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
816 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
817 dev->wiphy.bands[band]->ht_cap.ampdu_density);
818 }
819
Mahesh Palivela04d2e732012-06-22 07:27:46 +0000820 /* add VHT info */
821 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
822 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
823 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
824 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
825 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
826 dev->wiphy.bands[band]->vht_cap.cap)))
827 goto nla_put_failure;
828
Johannes Bergee688b02008-01-24 19:38:39 +0100829 /* add frequencies */
830 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
831 if (!nl_freqs)
832 goto nla_put_failure;
833
834 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
835 nl_freq = nla_nest_start(msg, i);
836 if (!nl_freq)
837 goto nla_put_failure;
838
839 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b02008-01-24 19:38:39 +0100840
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400841 if (nl80211_msg_put_channel(msg, chan))
842 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200843
Johannes Bergee688b02008-01-24 19:38:39 +0100844 nla_nest_end(msg, nl_freq);
845 }
846
847 nla_nest_end(msg, nl_freqs);
848
849 /* add bitrates */
850 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
851 if (!nl_rates)
852 goto nla_put_failure;
853
854 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
855 nl_rate = nla_nest_start(msg, i);
856 if (!nl_rate)
857 goto nla_put_failure;
858
859 rate = &dev->wiphy.bands[band]->bitrates[i];
860 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
861 rate->bitrate);
862 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
863 NLA_PUT_FLAG(msg,
864 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
865
866 nla_nest_end(msg, nl_rate);
867 }
868
869 nla_nest_end(msg, nl_rates);
870
871 nla_nest_end(msg, nl_band);
872 }
873 nla_nest_end(msg, nl_bands);
874
Johannes Berg8fdc6212009-03-14 09:34:01 +0100875 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
876 if (!nl_cmds)
877 goto nla_put_failure;
878
879 i = 0;
880#define CMD(op, n) \
881 do { \
882 if (dev->ops->op) { \
883 i++; \
884 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
885 } \
886 } while (0)
887
888 CMD(add_virtual_intf, NEW_INTERFACE);
889 CMD(change_virtual_intf, SET_INTERFACE);
890 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +0100891 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100892 CMD(add_station, NEW_STATION);
893 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800894 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100895 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200896 CMD(auth, AUTHENTICATE);
897 CMD(assoc, ASSOCIATE);
898 CMD(deauth, DEAUTHENTICATE);
899 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200900 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100901 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100902 CMD(set_pmksa, SET_PMKSA);
903 CMD(del_pmksa, DEL_PMKSA);
904 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100905 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
906 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200907 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200908 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100909 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100910 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200911 i++;
912 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
913 }
Johannes Bergf444de02010-05-05 15:25:02 +0200914 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400915 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300916 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
917 CMD(tdls_mgmt, TDLS_MGMT);
918 CMD(tdls_oper, TDLS_OPER);
919 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300920 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
921 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100922 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100923 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100924 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
925 i++;
926 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
927 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100928
Kalle Valo4745fc02011-11-17 19:06:10 +0200929#ifdef CONFIG_NL80211_TESTMODE
930 CMD(testmode_cmd, TESTMODE);
931#endif
932
Johannes Berg8fdc6212009-03-14 09:34:01 +0100933#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200934
Johannes Berg6829c872009-07-02 09:13:27 +0200935 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200936 i++;
937 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
938 }
939
Johannes Berg6829c872009-07-02 09:13:27 +0200940 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200941 i++;
942 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
943 }
944
Johannes Berg8fdc6212009-03-14 09:34:01 +0100945 nla_nest_end(msg, nl_cmds);
946
Johannes Berg7c4ef712011-11-18 15:33:48 +0100947 if (dev->ops->remain_on_channel &&
948 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100949 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
950 dev->wiphy.max_remain_on_channel_duration);
951
Johannes Berg7c4ef712011-11-18 15:33:48 +0100952 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100953 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
954
Johannes Berg2e161f72010-08-12 15:38:38 +0200955 if (mgmt_stypes) {
956 u16 stypes;
957 struct nlattr *nl_ftypes, *nl_ifs;
958 enum nl80211_iftype ift;
959
960 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
961 if (!nl_ifs)
962 goto nla_put_failure;
963
964 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
965 nl_ftypes = nla_nest_start(msg, ift);
966 if (!nl_ftypes)
967 goto nla_put_failure;
968 i = 0;
969 stypes = mgmt_stypes[ift].tx;
970 while (stypes) {
971 if (stypes & 1)
972 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
973 (i << 4) | IEEE80211_FTYPE_MGMT);
974 stypes >>= 1;
975 i++;
976 }
977 nla_nest_end(msg, nl_ftypes);
978 }
979
Johannes Berg74b70a42010-08-24 12:15:53 +0200980 nla_nest_end(msg, nl_ifs);
981
Johannes Berg2e161f72010-08-12 15:38:38 +0200982 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
983 if (!nl_ifs)
984 goto nla_put_failure;
985
986 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
987 nl_ftypes = nla_nest_start(msg, ift);
988 if (!nl_ftypes)
989 goto nla_put_failure;
990 i = 0;
991 stypes = mgmt_stypes[ift].rx;
992 while (stypes) {
993 if (stypes & 1)
994 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
995 (i << 4) | IEEE80211_FTYPE_MGMT);
996 stypes >>= 1;
997 i++;
998 }
999 nla_nest_end(msg, nl_ftypes);
1000 }
1001 nla_nest_end(msg, nl_ifs);
1002 }
1003
Johannes Bergff1b6e62011-05-04 15:37:28 +02001004 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1005 struct nlattr *nl_wowlan;
1006
1007 nl_wowlan = nla_nest_start(msg,
1008 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1009 if (!nl_wowlan)
1010 goto nla_put_failure;
1011
1012 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
1013 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
1014 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
1015 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1016 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1017 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02001018 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1019 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1020 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1021 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1022 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1023 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1024 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1025 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1026 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1027 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001028 if (dev->wiphy.wowlan.n_patterns) {
1029 struct nl80211_wowlan_pattern_support pat = {
1030 .max_patterns = dev->wiphy.wowlan.n_patterns,
1031 .min_pattern_len =
1032 dev->wiphy.wowlan.pattern_min_len,
1033 .max_pattern_len =
1034 dev->wiphy.wowlan.pattern_max_len,
1035 };
1036 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1037 sizeof(pat), &pat);
1038 }
1039
1040 nla_nest_end(msg, nl_wowlan);
1041 }
1042
Johannes Berg7527a782011-05-13 10:58:57 +02001043 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1044 dev->wiphy.software_iftypes))
1045 goto nla_put_failure;
1046
1047 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1048 goto nla_put_failure;
1049
Johannes Berg562a7482011-11-07 12:39:33 +01001050 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1051 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1052 dev->wiphy.ap_sme_capa);
1053
Johannes Berg1f074bd2011-11-06 14:13:33 +01001054 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1055
Ben Greear7e7c8922011-11-18 11:31:59 -08001056 if (dev->wiphy.ht_capa_mod_mask)
1057 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1058 sizeof(*dev->wiphy.ht_capa_mod_mask),
1059 dev->wiphy.ht_capa_mod_mask);
1060
Johannes Berg55682962007-09-20 13:09:35 -04001061 return genlmsg_end(msg, hdr);
1062
1063 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001064 genlmsg_cancel(msg, hdr);
1065 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001066}
1067
1068static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1069{
1070 int idx = 0;
1071 int start = cb->args[0];
1072 struct cfg80211_registered_device *dev;
1073
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001074 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001075 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001076 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1077 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001078 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001079 continue;
1080 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1081 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001082 dev) < 0) {
1083 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001084 break;
Julius Volzb4637272008-07-08 14:02:19 +02001085 }
Johannes Berg55682962007-09-20 13:09:35 -04001086 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001087 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001088
1089 cb->args[0] = idx;
1090
1091 return skb->len;
1092}
1093
1094static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1095{
1096 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001097 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001098
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001099 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001100 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001101 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001102
Johannes Berg4c476992010-10-04 21:36:35 +02001103 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1104 nlmsg_free(msg);
1105 return -ENOBUFS;
1106 }
Johannes Berg55682962007-09-20 13:09:35 -04001107
Johannes Berg134e6372009-07-10 09:51:34 +00001108 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001109}
1110
Jouni Malinen31888482008-10-30 16:59:24 +02001111static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1112 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1113 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1114 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1115 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1116 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1117};
1118
1119static int parse_txq_params(struct nlattr *tb[],
1120 struct ieee80211_txq_params *txq_params)
1121{
1122 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1123 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1124 !tb[NL80211_TXQ_ATTR_AIFS])
1125 return -EINVAL;
1126
1127 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1128 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1129 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1130 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1131 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1132
1133 return 0;
1134}
1135
Johannes Bergf444de02010-05-05 15:25:02 +02001136static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1137{
1138 /*
1139 * You can only set the channel explicitly for AP, mesh
1140 * and WDS type interfaces; all others have their channel
1141 * managed via their respective "establish a connection"
1142 * command (connect, join, ...)
1143 *
1144 * Monitors are special as they are normally slaved to
1145 * whatever else is going on, so they behave as though
1146 * you tried setting the wiphy channel itself.
1147 */
1148 return !wdev ||
1149 wdev->iftype == NL80211_IFTYPE_AP ||
1150 wdev->iftype == NL80211_IFTYPE_WDS ||
1151 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001152 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1153 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001154}
1155
1156static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1157 struct wireless_dev *wdev,
1158 struct genl_info *info)
1159{
1160 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1161 u32 freq;
1162 int result;
1163
1164 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1165 return -EINVAL;
1166
1167 if (!nl80211_can_set_dev_channel(wdev))
1168 return -EOPNOTSUPP;
1169
1170 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1171 channel_type = nla_get_u32(info->attrs[
1172 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1173 if (channel_type != NL80211_CHAN_NO_HT &&
1174 channel_type != NL80211_CHAN_HT20 &&
1175 channel_type != NL80211_CHAN_HT40PLUS &&
1176 channel_type != NL80211_CHAN_HT40MINUS)
1177 return -EINVAL;
1178 }
1179
1180 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1181
1182 mutex_lock(&rdev->devlist_mtx);
1183 if (wdev) {
1184 wdev_lock(wdev);
1185 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1186 wdev_unlock(wdev);
1187 } else {
1188 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1189 }
1190 mutex_unlock(&rdev->devlist_mtx);
1191
1192 return result;
1193}
1194
1195static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1196{
Johannes Berg4c476992010-10-04 21:36:35 +02001197 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1198 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001199
Johannes Berg4c476992010-10-04 21:36:35 +02001200 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001201}
1202
Bill Jordane8347eb2010-10-01 13:54:28 -04001203static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1204{
Johannes Berg43b19952010-10-07 13:10:30 +02001205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1206 struct net_device *dev = info->user_ptr[1];
1207 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001208 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001209
1210 if (!info->attrs[NL80211_ATTR_MAC])
1211 return -EINVAL;
1212
Johannes Berg43b19952010-10-07 13:10:30 +02001213 if (netif_running(dev))
1214 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001215
Johannes Berg43b19952010-10-07 13:10:30 +02001216 if (!rdev->ops->set_wds_peer)
1217 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001218
Johannes Berg43b19952010-10-07 13:10:30 +02001219 if (wdev->iftype != NL80211_IFTYPE_WDS)
1220 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001221
1222 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001223 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001224}
1225
1226
Johannes Berg55682962007-09-20 13:09:35 -04001227static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1228{
1229 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001230 struct net_device *netdev = NULL;
1231 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001232 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001233 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001234 u32 changed;
1235 u8 retry_short = 0, retry_long = 0;
1236 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001237 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001238
Johannes Bergf444de02010-05-05 15:25:02 +02001239 /*
1240 * Try to find the wiphy and netdev. Normally this
1241 * function shouldn't need the netdev, but this is
1242 * done for backward compatibility -- previously
1243 * setting the channel was done per wiphy, but now
1244 * it is per netdev. Previous userland like hostapd
1245 * also passed a netdev to set_wiphy, so that it is
1246 * possible to let that go to the right netdev!
1247 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001248 mutex_lock(&cfg80211_mutex);
1249
Johannes Bergf444de02010-05-05 15:25:02 +02001250 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1251 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1252
1253 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1254 if (netdev && netdev->ieee80211_ptr) {
1255 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1256 mutex_lock(&rdev->mtx);
1257 } else
1258 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001259 }
1260
Johannes Bergf444de02010-05-05 15:25:02 +02001261 if (!netdev) {
1262 rdev = __cfg80211_rdev_from_info(info);
1263 if (IS_ERR(rdev)) {
1264 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001265 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001266 }
1267 wdev = NULL;
1268 netdev = NULL;
1269 result = 0;
1270
1271 mutex_lock(&rdev->mtx);
1272 } else if (netif_running(netdev) &&
1273 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1274 wdev = netdev->ieee80211_ptr;
1275 else
1276 wdev = NULL;
1277
1278 /*
1279 * end workaround code, by now the rdev is available
1280 * and locked, and wdev may or may not be NULL.
1281 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001282
1283 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001284 result = cfg80211_dev_rename(
1285 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001286
1287 mutex_unlock(&cfg80211_mutex);
1288
1289 if (result)
1290 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001291
Jouni Malinen31888482008-10-30 16:59:24 +02001292 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1293 struct ieee80211_txq_params txq_params;
1294 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1295
1296 if (!rdev->ops->set_txq_params) {
1297 result = -EOPNOTSUPP;
1298 goto bad_res;
1299 }
1300
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001301 if (!netdev) {
1302 result = -EINVAL;
1303 goto bad_res;
1304 }
1305
Johannes Berg133a3ff2011-11-03 14:50:13 +01001306 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1307 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1308 result = -EINVAL;
1309 goto bad_res;
1310 }
1311
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001312 if (!netif_running(netdev)) {
1313 result = -ENETDOWN;
1314 goto bad_res;
1315 }
1316
Jouni Malinen31888482008-10-30 16:59:24 +02001317 nla_for_each_nested(nl_txq_params,
1318 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1319 rem_txq_params) {
1320 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1321 nla_data(nl_txq_params),
1322 nla_len(nl_txq_params),
1323 txq_params_policy);
1324 result = parse_txq_params(tb, &txq_params);
1325 if (result)
1326 goto bad_res;
1327
1328 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001329 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001330 &txq_params);
1331 if (result)
1332 goto bad_res;
1333 }
1334 }
1335
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001336 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001337 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001338 if (result)
1339 goto bad_res;
1340 }
1341
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001342 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1343 enum nl80211_tx_power_setting type;
1344 int idx, mbm = 0;
1345
1346 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001347 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001348 goto bad_res;
1349 }
1350
1351 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1352 type = nla_get_u32(info->attrs[idx]);
1353
1354 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1355 (type != NL80211_TX_POWER_AUTOMATIC)) {
1356 result = -EINVAL;
1357 goto bad_res;
1358 }
1359
1360 if (type != NL80211_TX_POWER_AUTOMATIC) {
1361 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1362 mbm = nla_get_u32(info->attrs[idx]);
1363 }
1364
1365 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1366 if (result)
1367 goto bad_res;
1368 }
1369
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001370 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1371 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1372 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001373 if ((!rdev->wiphy.available_antennas_tx &&
1374 !rdev->wiphy.available_antennas_rx) ||
1375 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001376 result = -EOPNOTSUPP;
1377 goto bad_res;
1378 }
1379
1380 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1381 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1382
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001383 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001384 * available antenna masks, except for the "all" mask */
1385 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1386 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001387 result = -EINVAL;
1388 goto bad_res;
1389 }
1390
Bruno Randolf7f531e02010-12-16 11:30:22 +09001391 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1392 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001393
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001394 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1395 if (result)
1396 goto bad_res;
1397 }
1398
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001399 changed = 0;
1400
1401 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1402 retry_short = nla_get_u8(
1403 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1404 if (retry_short == 0) {
1405 result = -EINVAL;
1406 goto bad_res;
1407 }
1408 changed |= WIPHY_PARAM_RETRY_SHORT;
1409 }
1410
1411 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1412 retry_long = nla_get_u8(
1413 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1414 if (retry_long == 0) {
1415 result = -EINVAL;
1416 goto bad_res;
1417 }
1418 changed |= WIPHY_PARAM_RETRY_LONG;
1419 }
1420
1421 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1422 frag_threshold = nla_get_u32(
1423 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1424 if (frag_threshold < 256) {
1425 result = -EINVAL;
1426 goto bad_res;
1427 }
1428 if (frag_threshold != (u32) -1) {
1429 /*
1430 * Fragments (apart from the last one) are required to
1431 * have even length. Make the fragmentation code
1432 * simpler by stripping LSB should someone try to use
1433 * odd threshold value.
1434 */
1435 frag_threshold &= ~0x1;
1436 }
1437 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1438 }
1439
1440 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1441 rts_threshold = nla_get_u32(
1442 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1443 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1444 }
1445
Lukáš Turek81077e82009-12-21 22:50:47 +01001446 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1447 coverage_class = nla_get_u8(
1448 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1449 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1450 }
1451
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001452 if (changed) {
1453 u8 old_retry_short, old_retry_long;
1454 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001455 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001456
1457 if (!rdev->ops->set_wiphy_params) {
1458 result = -EOPNOTSUPP;
1459 goto bad_res;
1460 }
1461
1462 old_retry_short = rdev->wiphy.retry_short;
1463 old_retry_long = rdev->wiphy.retry_long;
1464 old_frag_threshold = rdev->wiphy.frag_threshold;
1465 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001466 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001467
1468 if (changed & WIPHY_PARAM_RETRY_SHORT)
1469 rdev->wiphy.retry_short = retry_short;
1470 if (changed & WIPHY_PARAM_RETRY_LONG)
1471 rdev->wiphy.retry_long = retry_long;
1472 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1473 rdev->wiphy.frag_threshold = frag_threshold;
1474 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1475 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001476 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1477 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001478
1479 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1480 if (result) {
1481 rdev->wiphy.retry_short = old_retry_short;
1482 rdev->wiphy.retry_long = old_retry_long;
1483 rdev->wiphy.frag_threshold = old_frag_threshold;
1484 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001485 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001486 }
1487 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001488
Johannes Berg306d6112008-12-08 12:39:04 +01001489 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001490 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001491 if (netdev)
1492 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001493 return result;
1494}
1495
1496
1497static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001498 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001499 struct net_device *dev)
1500{
1501 void *hdr;
1502
1503 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1504 if (!hdr)
1505 return -1;
1506
1507 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001508 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001509 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001510 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001511
1512 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1513 rdev->devlist_generation ^
1514 (cfg80211_rdev_list_generation << 2));
1515
Johannes Berg55682962007-09-20 13:09:35 -04001516 return genlmsg_end(msg, hdr);
1517
1518 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001519 genlmsg_cancel(msg, hdr);
1520 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001521}
1522
1523static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1524{
1525 int wp_idx = 0;
1526 int if_idx = 0;
1527 int wp_start = cb->args[0];
1528 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001529 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001530 struct wireless_dev *wdev;
1531
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001532 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001533 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1534 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001535 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001536 if (wp_idx < wp_start) {
1537 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001538 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001539 }
Johannes Berg55682962007-09-20 13:09:35 -04001540 if_idx = 0;
1541
Johannes Bergf5ea9122009-08-07 16:17:38 +02001542 mutex_lock(&rdev->devlist_mtx);
1543 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001544 if (if_idx < if_start) {
1545 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001546 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001547 }
Johannes Berg55682962007-09-20 13:09:35 -04001548 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1549 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001550 rdev, wdev->netdev) < 0) {
1551 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001552 goto out;
1553 }
1554 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001555 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001556 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001557
1558 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001559 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001560 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001561 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001562
1563 cb->args[0] = wp_idx;
1564 cb->args[1] = if_idx;
1565
1566 return skb->len;
1567}
1568
1569static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1570{
1571 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001572 struct cfg80211_registered_device *dev = info->user_ptr[0];
1573 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001574
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001576 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001577 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001578
Johannes Bergd7264052009-04-19 16:23:20 +02001579 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001580 dev, netdev) < 0) {
1581 nlmsg_free(msg);
1582 return -ENOBUFS;
1583 }
Johannes Berg55682962007-09-20 13:09:35 -04001584
Johannes Berg134e6372009-07-10 09:51:34 +00001585 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001586}
1587
Michael Wu66f7ac52008-01-31 19:48:22 +01001588static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1589 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1590 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1591 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1592 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1593 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1594};
1595
1596static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1597{
1598 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1599 int flag;
1600
1601 *mntrflags = 0;
1602
1603 if (!nla)
1604 return -EINVAL;
1605
1606 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1607 nla, mntr_flags_policy))
1608 return -EINVAL;
1609
1610 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1611 if (flags[flag])
1612 *mntrflags |= (1<<flag);
1613
1614 return 0;
1615}
1616
Johannes Berg9bc383d2009-11-19 11:55:19 +01001617static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001618 struct net_device *netdev, u8 use_4addr,
1619 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001620{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001621 if (!use_4addr) {
Jiri Pirkof350a0a2010-06-15 06:50:45 +00001622 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001623 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001624 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001625 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001626
1627 switch (iftype) {
1628 case NL80211_IFTYPE_AP_VLAN:
1629 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1630 return 0;
1631 break;
1632 case NL80211_IFTYPE_STATION:
1633 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1634 return 0;
1635 break;
1636 default:
1637 break;
1638 }
1639
1640 return -EOPNOTSUPP;
1641}
1642
Johannes Berg55682962007-09-20 13:09:35 -04001643static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1644{
Johannes Berg4c476992010-10-04 21:36:35 +02001645 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001646 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001647 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001648 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001649 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001650 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001651 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001652
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001653 memset(&params, 0, sizeof(params));
1654
Johannes Berg04a773a2009-04-19 21:24:32 +02001655 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001656
Johannes Berg723b0382008-09-16 20:22:09 +02001657 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001658 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001659 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001660 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001661 if (ntype > NL80211_IFTYPE_MAX)
1662 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001663 }
1664
Johannes Berg92ffe052008-09-16 20:39:36 +02001665 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001666 struct wireless_dev *wdev = dev->ieee80211_ptr;
1667
Johannes Berg4c476992010-10-04 21:36:35 +02001668 if (ntype != NL80211_IFTYPE_MESH_POINT)
1669 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001670 if (netif_running(dev))
1671 return -EBUSY;
1672
1673 wdev_lock(wdev);
1674 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1675 IEEE80211_MAX_MESH_ID_LEN);
1676 wdev->mesh_id_up_len =
1677 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1678 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1679 wdev->mesh_id_up_len);
1680 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001681 }
1682
Felix Fietkau8b787642009-11-10 18:53:10 +01001683 if (info->attrs[NL80211_ATTR_4ADDR]) {
1684 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1685 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001686 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001687 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001688 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001689 } else {
1690 params.use_4addr = -1;
1691 }
1692
Johannes Berg92ffe052008-09-16 20:39:36 +02001693 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001694 if (ntype != NL80211_IFTYPE_MONITOR)
1695 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001696 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1697 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001698 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001699 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001700
1701 flags = &_flags;
1702 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001703 }
Johannes Berg3b858752009-03-12 09:55:09 +01001704
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001705 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001706 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001707 else
1708 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001709
Johannes Berg9bc383d2009-11-19 11:55:19 +01001710 if (!err && params.use_4addr != -1)
1711 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1712
Johannes Berg55682962007-09-20 13:09:35 -04001713 return err;
1714}
1715
1716static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1717{
Johannes Berg4c476992010-10-04 21:36:35 +02001718 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001719 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001720 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001721 int err;
1722 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001723 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001724
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001725 memset(&params, 0, sizeof(params));
1726
Johannes Berg55682962007-09-20 13:09:35 -04001727 if (!info->attrs[NL80211_ATTR_IFNAME])
1728 return -EINVAL;
1729
1730 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1731 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1732 if (type > NL80211_IFTYPE_MAX)
1733 return -EINVAL;
1734 }
1735
Johannes Berg79c97e92009-07-07 03:56:12 +02001736 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001737 !(rdev->wiphy.interface_modes & (1 << type)))
1738 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001739
Johannes Berg9bc383d2009-11-19 11:55:19 +01001740 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001741 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001742 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001743 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001744 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001745 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001746
Michael Wu66f7ac52008-01-31 19:48:22 +01001747 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1748 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1749 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001750 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001751 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001752 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001753 if (IS_ERR(dev))
1754 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001755
Johannes Berg29cbe682010-12-03 09:20:44 +01001756 if (type == NL80211_IFTYPE_MESH_POINT &&
1757 info->attrs[NL80211_ATTR_MESH_ID]) {
1758 struct wireless_dev *wdev = dev->ieee80211_ptr;
1759
1760 wdev_lock(wdev);
1761 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1762 IEEE80211_MAX_MESH_ID_LEN);
1763 wdev->mesh_id_up_len =
1764 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1765 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1766 wdev->mesh_id_up_len);
1767 wdev_unlock(wdev);
1768 }
1769
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001770 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001771}
1772
1773static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1774{
Johannes Berg4c476992010-10-04 21:36:35 +02001775 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1776 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001777
Johannes Berg4c476992010-10-04 21:36:35 +02001778 if (!rdev->ops->del_virtual_intf)
1779 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001780
Johannes Berg4c476992010-10-04 21:36:35 +02001781 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001782}
1783
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001784static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1785{
1786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1787 struct net_device *dev = info->user_ptr[1];
1788 u16 noack_map;
1789
1790 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1791 return -EINVAL;
1792
1793 if (!rdev->ops->set_noack_map)
1794 return -EOPNOTSUPP;
1795
1796 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1797
1798 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1799}
1800
Johannes Berg41ade002007-12-19 02:03:29 +01001801struct get_key_cookie {
1802 struct sk_buff *msg;
1803 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001804 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001805};
1806
1807static void get_key_callback(void *c, struct key_params *params)
1808{
Johannes Bergb9454e82009-07-08 13:29:08 +02001809 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001810 struct get_key_cookie *cookie = c;
1811
1812 if (params->key)
1813 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1814 params->key_len, params->key);
1815
1816 if (params->seq)
1817 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1818 params->seq_len, params->seq);
1819
1820 if (params->cipher)
1821 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1822 params->cipher);
1823
Johannes Bergb9454e82009-07-08 13:29:08 +02001824 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1825 if (!key)
1826 goto nla_put_failure;
1827
1828 if (params->key)
1829 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1830 params->key_len, params->key);
1831
1832 if (params->seq)
1833 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1834 params->seq_len, params->seq);
1835
1836 if (params->cipher)
1837 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1838 params->cipher);
1839
1840 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1841
1842 nla_nest_end(cookie->msg, key);
1843
Johannes Berg41ade002007-12-19 02:03:29 +01001844 return;
1845 nla_put_failure:
1846 cookie->error = 1;
1847}
1848
1849static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1850{
Johannes Berg4c476992010-10-04 21:36:35 +02001851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001852 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001853 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001854 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001855 const u8 *mac_addr = NULL;
1856 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001857 struct get_key_cookie cookie = {
1858 .error = 0,
1859 };
1860 void *hdr;
1861 struct sk_buff *msg;
1862
1863 if (info->attrs[NL80211_ATTR_KEY_IDX])
1864 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1865
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001866 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001867 return -EINVAL;
1868
1869 if (info->attrs[NL80211_ATTR_MAC])
1870 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1871
Johannes Berge31b8212010-10-05 19:39:30 +02001872 pairwise = !!mac_addr;
1873 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1874 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1875 if (kt >= NUM_NL80211_KEYTYPES)
1876 return -EINVAL;
1877 if (kt != NL80211_KEYTYPE_GROUP &&
1878 kt != NL80211_KEYTYPE_PAIRWISE)
1879 return -EINVAL;
1880 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1881 }
1882
Johannes Berg4c476992010-10-04 21:36:35 +02001883 if (!rdev->ops->get_key)
1884 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001885
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001887 if (!msg)
1888 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001889
1890 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1891 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001892 if (IS_ERR(hdr))
1893 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001894
1895 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001896 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001897
1898 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1899 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1900 if (mac_addr)
1901 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1902
Johannes Berge31b8212010-10-05 19:39:30 +02001903 if (pairwise && mac_addr &&
1904 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1905 return -ENOENT;
1906
1907 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1908 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001909
1910 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001911 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001912
1913 if (cookie.error)
1914 goto nla_put_failure;
1915
1916 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001917 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001918
1919 nla_put_failure:
1920 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001921 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001922 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001923 return err;
1924}
1925
1926static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1927{
Johannes Berg4c476992010-10-04 21:36:35 +02001928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001929 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001930 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001931 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001932
Johannes Bergb9454e82009-07-08 13:29:08 +02001933 err = nl80211_parse_key(info, &key);
1934 if (err)
1935 return err;
1936
1937 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001938 return -EINVAL;
1939
Johannes Bergb9454e82009-07-08 13:29:08 +02001940 /* only support setting default key */
1941 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001942 return -EINVAL;
1943
Johannes Bergfffd0932009-07-08 14:22:54 +02001944 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001945
1946 if (key.def) {
1947 if (!rdev->ops->set_default_key) {
1948 err = -EOPNOTSUPP;
1949 goto out;
1950 }
1951
1952 err = nl80211_key_allowed(dev->ieee80211_ptr);
1953 if (err)
1954 goto out;
1955
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001956 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1957 key.def_uni, key.def_multi);
1958
1959 if (err)
1960 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001961
Johannes Berg3d23e342009-09-29 23:27:28 +02001962#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001963 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001964#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001965 } else {
1966 if (key.def_uni || !key.def_multi) {
1967 err = -EINVAL;
1968 goto out;
1969 }
1970
1971 if (!rdev->ops->set_default_mgmt_key) {
1972 err = -EOPNOTSUPP;
1973 goto out;
1974 }
1975
1976 err = nl80211_key_allowed(dev->ieee80211_ptr);
1977 if (err)
1978 goto out;
1979
1980 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1981 dev, key.idx);
1982 if (err)
1983 goto out;
1984
1985#ifdef CONFIG_CFG80211_WEXT
1986 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1987#endif
1988 }
1989
1990 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001991 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001992
Johannes Berg41ade002007-12-19 02:03:29 +01001993 return err;
1994}
1995
1996static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1997{
Johannes Berg4c476992010-10-04 21:36:35 +02001998 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001999 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002000 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002001 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002002 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002003
Johannes Bergb9454e82009-07-08 13:29:08 +02002004 err = nl80211_parse_key(info, &key);
2005 if (err)
2006 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002007
Johannes Bergb9454e82009-07-08 13:29:08 +02002008 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002009 return -EINVAL;
2010
Johannes Berg41ade002007-12-19 02:03:29 +01002011 if (info->attrs[NL80211_ATTR_MAC])
2012 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2013
Johannes Berge31b8212010-10-05 19:39:30 +02002014 if (key.type == -1) {
2015 if (mac_addr)
2016 key.type = NL80211_KEYTYPE_PAIRWISE;
2017 else
2018 key.type = NL80211_KEYTYPE_GROUP;
2019 }
2020
2021 /* for now */
2022 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2023 key.type != NL80211_KEYTYPE_GROUP)
2024 return -EINVAL;
2025
Johannes Berg4c476992010-10-04 21:36:35 +02002026 if (!rdev->ops->add_key)
2027 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002028
Johannes Berge31b8212010-10-05 19:39:30 +02002029 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2030 key.type == NL80211_KEYTYPE_PAIRWISE,
2031 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002032 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002033
2034 wdev_lock(dev->ieee80211_ptr);
2035 err = nl80211_key_allowed(dev->ieee80211_ptr);
2036 if (!err)
2037 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002038 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002039 mac_addr, &key.p);
2040 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002041
Johannes Berg41ade002007-12-19 02:03:29 +01002042 return err;
2043}
2044
2045static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2046{
Johannes Berg4c476992010-10-04 21:36:35 +02002047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002048 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002049 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002050 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002051 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002052
Johannes Bergb9454e82009-07-08 13:29:08 +02002053 err = nl80211_parse_key(info, &key);
2054 if (err)
2055 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002056
2057 if (info->attrs[NL80211_ATTR_MAC])
2058 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2059
Johannes Berge31b8212010-10-05 19:39:30 +02002060 if (key.type == -1) {
2061 if (mac_addr)
2062 key.type = NL80211_KEYTYPE_PAIRWISE;
2063 else
2064 key.type = NL80211_KEYTYPE_GROUP;
2065 }
2066
2067 /* for now */
2068 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2069 key.type != NL80211_KEYTYPE_GROUP)
2070 return -EINVAL;
2071
Johannes Berg4c476992010-10-04 21:36:35 +02002072 if (!rdev->ops->del_key)
2073 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002074
Johannes Bergfffd0932009-07-08 14:22:54 +02002075 wdev_lock(dev->ieee80211_ptr);
2076 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002077
2078 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2079 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2080 err = -ENOENT;
2081
Johannes Bergfffd0932009-07-08 14:22:54 +02002082 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002083 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2084 key.type == NL80211_KEYTYPE_PAIRWISE,
2085 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002086
Johannes Berg3d23e342009-09-29 23:27:28 +02002087#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002088 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002089 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002090 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002091 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002092 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2093 }
2094#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002095 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002096
Johannes Berg41ade002007-12-19 02:03:29 +01002097 return err;
2098}
2099
Johannes Berg88600202012-02-13 15:17:18 +01002100static int nl80211_parse_beacon(struct genl_info *info,
2101 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002102{
Johannes Berg88600202012-02-13 15:17:18 +01002103 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002104
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002105 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2106 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2107 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2108 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002109 return -EINVAL;
2110
Johannes Berg88600202012-02-13 15:17:18 +01002111 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002112
Johannes Berged1b6cc2007-12-19 02:03:32 +01002113 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002114 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2115 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2116 if (!bcn->head_len)
2117 return -EINVAL;
2118 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002119 }
2120
2121 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002122 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2123 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002124 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002125 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002126 }
2127
Johannes Berg4c476992010-10-04 21:36:35 +02002128 if (!haveinfo)
2129 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002130
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002131 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002132 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2133 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002134 }
2135
2136 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002137 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002138 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002139 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002140 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2141 }
2142
2143 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002144 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002145 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002146 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002147 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2148 }
2149
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002150 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002151 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002152 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002153 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002154 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2155 }
2156
Johannes Berg88600202012-02-13 15:17:18 +01002157 return 0;
2158}
2159
2160static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2161{
2162 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2163 struct net_device *dev = info->user_ptr[1];
2164 struct wireless_dev *wdev = dev->ieee80211_ptr;
2165 struct cfg80211_ap_settings params;
2166 int err;
2167
2168 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2169 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2170 return -EOPNOTSUPP;
2171
2172 if (!rdev->ops->start_ap)
2173 return -EOPNOTSUPP;
2174
2175 if (wdev->beacon_interval)
2176 return -EALREADY;
2177
2178 memset(&params, 0, sizeof(params));
2179
2180 /* these are required for START_AP */
2181 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2182 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2183 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2184 return -EINVAL;
2185
2186 err = nl80211_parse_beacon(info, &params.beacon);
2187 if (err)
2188 return err;
2189
2190 params.beacon_interval =
2191 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2192 params.dtim_period =
2193 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2194
2195 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2196 if (err)
2197 return err;
2198
2199 /*
2200 * In theory, some of these attributes should be required here
2201 * but since they were not used when the command was originally
2202 * added, keep them optional for old user space programs to let
2203 * them continue to work with drivers that do not need the
2204 * additional information -- drivers must check!
2205 */
2206 if (info->attrs[NL80211_ATTR_SSID]) {
2207 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2208 params.ssid_len =
2209 nla_len(info->attrs[NL80211_ATTR_SSID]);
2210 if (params.ssid_len == 0 ||
2211 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2212 return -EINVAL;
2213 }
2214
2215 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2216 params.hidden_ssid = nla_get_u32(
2217 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2218 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2219 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2220 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2221 return -EINVAL;
2222 }
2223
2224 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2225
2226 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2227 params.auth_type = nla_get_u32(
2228 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2229 if (!nl80211_valid_auth_type(params.auth_type))
2230 return -EINVAL;
2231 } else
2232 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2233
2234 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2235 NL80211_MAX_NR_CIPHER_SUITES);
2236 if (err)
2237 return err;
2238
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302239 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2240 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2241 return -EOPNOTSUPP;
2242 params.inactivity_timeout = nla_get_u16(
2243 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2244 }
2245
Johannes Berg88600202012-02-13 15:17:18 +01002246 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2247 if (!err)
2248 wdev->beacon_interval = params.beacon_interval;
Johannes Berg56d18932011-05-09 18:41:15 +02002249 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002250}
2251
Johannes Berg88600202012-02-13 15:17:18 +01002252static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2253{
2254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2255 struct net_device *dev = info->user_ptr[1];
2256 struct wireless_dev *wdev = dev->ieee80211_ptr;
2257 struct cfg80211_beacon_data params;
2258 int err;
2259
2260 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2261 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2262 return -EOPNOTSUPP;
2263
2264 if (!rdev->ops->change_beacon)
2265 return -EOPNOTSUPP;
2266
2267 if (!wdev->beacon_interval)
2268 return -EINVAL;
2269
2270 err = nl80211_parse_beacon(info, &params);
2271 if (err)
2272 return err;
2273
2274 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2275}
2276
2277static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002278{
Johannes Berg4c476992010-10-04 21:36:35 +02002279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2280 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002281 struct wireless_dev *wdev = dev->ieee80211_ptr;
2282 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002283
Johannes Berg88600202012-02-13 15:17:18 +01002284 if (!rdev->ops->stop_ap)
Johannes Berg4c476992010-10-04 21:36:35 +02002285 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002286
Johannes Berg074ac8d2010-09-16 14:58:22 +02002287 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002288 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2289 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002290
Johannes Berg88600202012-02-13 15:17:18 +01002291 if (!wdev->beacon_interval)
2292 return -ENOENT;
2293
2294 err = rdev->ops->stop_ap(&rdev->wiphy, dev);
Johannes Berg56d18932011-05-09 18:41:15 +02002295 if (!err)
2296 wdev->beacon_interval = 0;
2297 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002298}
2299
Johannes Berg5727ef12007-12-19 02:03:34 +01002300static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2301 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2302 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2303 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002304 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002305 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002306 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002307};
2308
Johannes Bergeccb8e82009-05-11 21:57:56 +03002309static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002310 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002311 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002312{
2313 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002314 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002315 int flag;
2316
Johannes Bergeccb8e82009-05-11 21:57:56 +03002317 /*
2318 * Try parsing the new attribute first so userspace
2319 * can specify both for older kernels.
2320 */
2321 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2322 if (nla) {
2323 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002324
Johannes Bergeccb8e82009-05-11 21:57:56 +03002325 sta_flags = nla_data(nla);
2326 params->sta_flags_mask = sta_flags->mask;
2327 params->sta_flags_set = sta_flags->set;
2328 if ((params->sta_flags_mask |
2329 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2330 return -EINVAL;
2331 return 0;
2332 }
2333
2334 /* if present, parse the old attribute */
2335
2336 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002337 if (!nla)
2338 return 0;
2339
2340 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2341 nla, sta_flags_policy))
2342 return -EINVAL;
2343
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002344 /*
2345 * Only allow certain flags for interface types so that
2346 * other attributes are silently ignored. Remember that
2347 * this is backward compatibility code with old userspace
2348 * and shouldn't be hit in other cases anyway.
2349 */
2350 switch (iftype) {
2351 case NL80211_IFTYPE_AP:
2352 case NL80211_IFTYPE_AP_VLAN:
2353 case NL80211_IFTYPE_P2P_GO:
2354 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2355 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2356 BIT(NL80211_STA_FLAG_WME) |
2357 BIT(NL80211_STA_FLAG_MFP);
2358 break;
2359 case NL80211_IFTYPE_P2P_CLIENT:
2360 case NL80211_IFTYPE_STATION:
2361 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2362 BIT(NL80211_STA_FLAG_TDLS_PEER);
2363 break;
2364 case NL80211_IFTYPE_MESH_POINT:
2365 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2366 BIT(NL80211_STA_FLAG_MFP) |
2367 BIT(NL80211_STA_FLAG_AUTHORIZED);
2368 default:
2369 return -EINVAL;
2370 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002371
2372 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2373 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002374 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002375
2376 return 0;
2377}
2378
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002379static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2380 int attr)
2381{
2382 struct nlattr *rate;
2383 u16 bitrate;
2384
2385 rate = nla_nest_start(msg, attr);
2386 if (!rate)
Johannes Berg99958b22013-03-19 14:28:37 -07002387 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002388
2389 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2390 bitrate = cfg80211_calculate_bitrate(info);
2391 if (bitrate > 0)
Johannes Berg99958b22013-03-19 14:28:37 -07002392 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002393
Johannes Berg99958b22013-03-19 14:28:37 -07002394 if (info->flags & RATE_INFO_FLAGS_MCS) {
2395 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
2396 return false;
2397 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2398 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2399 return false;
2400 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2401 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2402 return false;
2403 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
2404 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
2405 return false;
2406 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
2407 return false;
2408 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2409 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2410 return false;
2411 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
2412 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
2413 return false;
2414 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
2415 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
2416 return false;
2417 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
2418 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
2419 return false;
2420 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2421 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2422 return false;
2423 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002424
2425 nla_nest_end(msg, rate);
2426 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002427}
2428
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002429static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002430 int flags,
2431 struct cfg80211_registered_device *rdev,
2432 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002433 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002434{
2435 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002436 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002437
2438 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2439 if (!hdr)
2440 return -1;
2441
2442 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2443 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2444
Johannes Bergf5ea9122009-08-07 16:17:38 +02002445 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2446
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002447 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2448 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002449 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302450 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2451 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2452 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002453 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2454 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2455 sinfo->inactive_time);
2456 if (sinfo->filled & STATION_INFO_RX_BYTES)
2457 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2458 sinfo->rx_bytes);
2459 if (sinfo->filled & STATION_INFO_TX_BYTES)
2460 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2461 sinfo->tx_bytes);
2462 if (sinfo->filled & STATION_INFO_LLID)
2463 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2464 sinfo->llid);
2465 if (sinfo->filled & STATION_INFO_PLID)
2466 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2467 sinfo->plid);
2468 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2469 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2470 sinfo->plink_state);
John W. Linville66266b32012-03-15 13:25:41 -04002471 switch (rdev->wiphy.signal_type) {
2472 case CFG80211_SIGNAL_TYPE_MBM:
2473 if (sinfo->filled & STATION_INFO_SIGNAL)
2474 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2475 sinfo->signal);
2476 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2477 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2478 sinfo->signal_avg);
2479 break;
2480 default:
2481 break;
2482 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002483 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002484 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2485 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002486 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002487 }
2488 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2489 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2490 NL80211_STA_INFO_RX_BITRATE))
2491 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002492 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002493 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2494 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2495 sinfo->rx_packets);
2496 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2497 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2498 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002499 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2500 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2501 sinfo->tx_retries);
2502 if (sinfo->filled & STATION_INFO_TX_FAILED)
2503 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2504 sinfo->tx_failed);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002505 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2506 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2507 sinfo->beacon_loss_count);
Paul Stewartf4263c92011-03-31 09:25:41 -07002508 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2509 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2510 if (!bss_param)
2511 goto nla_put_failure;
2512
2513 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2514 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2515 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2516 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2517 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2518 NLA_PUT_FLAG(msg,
2519 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2520 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2521 sinfo->bss_param.dtim_period);
2522 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2523 sinfo->bss_param.beacon_interval);
2524
2525 nla_nest_end(msg, bss_param);
2526 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002527 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2528 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2529 sizeof(struct nl80211_sta_flag_update),
2530 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002531 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002532
Felix Fietkau040bdf72011-08-10 19:00:33 -06002533 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002534 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2535 sinfo->assoc_req_ies);
2536
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002537 return genlmsg_end(msg, hdr);
2538
2539 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002540 genlmsg_cancel(msg, hdr);
2541 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002542}
2543
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002544static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002545 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002546{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002547 struct station_info sinfo;
2548 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002549 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002550 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002551 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002552 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002553
Johannes Berg67748892010-10-04 21:14:06 +02002554 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2555 if (err)
2556 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002557
2558 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002559 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002560 goto out_err;
2561 }
2562
Johannes Bergbba95fe2008-07-29 13:22:51 +02002563 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002564 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002565 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2566 mac_addr, &sinfo);
2567 if (err == -ENOENT)
2568 break;
2569 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002570 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002571
2572 if (nl80211_send_station(skb,
2573 NETLINK_CB(cb->skb).pid,
2574 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002575 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002576 &sinfo) < 0)
2577 goto out;
2578
2579 sta_idx++;
2580 }
2581
2582
2583 out:
2584 cb->args[1] = sta_idx;
2585 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002586 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002587 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002588
2589 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002590}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002591
Johannes Berg5727ef12007-12-19 02:03:34 +01002592static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2593{
Johannes Berg4c476992010-10-04 21:36:35 +02002594 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2595 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002596 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002597 struct sk_buff *msg;
2598 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002599 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002600
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002601 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002602
2603 if (!info->attrs[NL80211_ATTR_MAC])
2604 return -EINVAL;
2605
2606 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2607
Johannes Berg4c476992010-10-04 21:36:35 +02002608 if (!rdev->ops->get_station)
2609 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002610
Johannes Berg79c97e92009-07-07 03:56:12 +02002611 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002612 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002613 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002614
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002615 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002616 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002617 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002618
2619 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002620 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002621 nlmsg_free(msg);
2622 return -ENOBUFS;
2623 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002624
Johannes Berg4c476992010-10-04 21:36:35 +02002625 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002626}
2627
2628/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002629 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002630 */
Johannes Berg80b99892011-11-18 16:23:01 +01002631static struct net_device *get_vlan(struct genl_info *info,
2632 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002633{
Johannes Berg463d0182009-07-14 00:33:35 +02002634 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002635 struct net_device *v;
2636 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002637
Johannes Berg80b99892011-11-18 16:23:01 +01002638 if (!vlanattr)
2639 return NULL;
2640
2641 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2642 if (!v)
2643 return ERR_PTR(-ENODEV);
2644
2645 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2646 ret = -EINVAL;
2647 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002648 }
Johannes Berg80b99892011-11-18 16:23:01 +01002649
2650 if (!netif_running(v)) {
2651 ret = -ENETDOWN;
2652 goto error;
2653 }
2654
2655 return v;
2656 error:
2657 dev_put(v);
2658 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002659}
2660
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002661static struct nla_policy
2662nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2663 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2664 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2665};
2666
2667static int nl80211_set_station_tdls(struct genl_info *info,
2668 struct station_parameters *params)
2669{
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002670 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2671 struct nlattr *nla;
2672 int err;
2673
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002674 /* Dummy STA entry gets updated once the peer capabilities are known */
2675 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2676 params->ht_capa =
2677 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2678 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
2679 params->vht_capa =
2680 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
2681
2682 /* parse WME attributes if present */
2683 if (!info->attrs[NL80211_ATTR_STA_WME])
2684 return 0;
2685
2686 nla = info->attrs[NL80211_ATTR_STA_WME];
2687 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2688 nl80211_sta_wme_policy);
2689 if (err)
2690 return err;
2691
2692 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2693 params->uapsd_queues = nla_get_u8(
2694 tb[NL80211_STA_WME_UAPSD_QUEUES]);
2695 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2696 return -EINVAL;
2697
2698 if (tb[NL80211_STA_WME_MAX_SP])
2699 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2700
2701 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2702 return -EINVAL;
2703
2704 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2705
2706 return 0;
2707}
2708
Johannes Berg5727ef12007-12-19 02:03:34 +01002709static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2710{
Johannes Berg4c476992010-10-04 21:36:35 +02002711 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002712 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002713 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002714 struct station_parameters params;
2715 u8 *mac_addr = NULL;
2716
2717 memset(&params, 0, sizeof(params));
2718
2719 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002720 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002721
2722 if (info->attrs[NL80211_ATTR_STA_AID])
2723 return -EINVAL;
2724
2725 if (!info->attrs[NL80211_ATTR_MAC])
2726 return -EINVAL;
2727
2728 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2729
2730 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2731 params.supported_rates =
2732 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2733 params.supported_rates_len =
2734 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2735 }
2736
Jouni Malinen1400ced2013-02-14 19:10:13 +00002737 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
2738 params.capability =
2739 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
2740 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
2741 }
2742
2743 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
2744 params.ext_capab =
2745 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
2746 params.ext_capab_len =
2747 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
2748 }
2749
Johannes Berg5727ef12007-12-19 02:03:34 +01002750 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2751 params.listen_interval =
2752 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2753
Jouni Malinen36aedc92008-08-25 11:58:58 +03002754 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2755 params.ht_capa =
2756 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2757
Johannes Bergbdd90d52011-12-14 12:20:27 +01002758 if (!rdev->ops->change_station)
2759 return -EOPNOTSUPP;
2760
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002761 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002762 return -EINVAL;
2763
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002764 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2765 params.plink_action =
2766 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2767
Javier Cardona9c3990a2011-05-03 16:57:11 -07002768 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2769 params.plink_state =
2770 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2771
Johannes Berga97f4422009-06-18 17:23:43 +02002772 switch (dev->ieee80211_ptr->iftype) {
2773 case NL80211_IFTYPE_AP:
2774 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002775 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002776 /* disallow mesh-specific things */
2777 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002778 return -EINVAL;
2779
2780 /* TDLS can't be set, ... */
2781 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2782 return -EINVAL;
2783 /*
2784 * ... but don't bother the driver with it. This works around
2785 * a hostapd/wpa_supplicant issue -- it always includes the
2786 * TLDS_PEER flag in the mask even for AP mode.
2787 */
2788 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2789
2790 /* accept only the listed bits */
2791 if (params.sta_flags_mask &
2792 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2793 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2794 BIT(NL80211_STA_FLAG_WME) |
2795 BIT(NL80211_STA_FLAG_MFP)))
2796 return -EINVAL;
2797
Jouni Malinen1400ced2013-02-14 19:10:13 +00002798 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
2799 return -EINVAL;
2800 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
2801 return -EINVAL;
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002802 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
2803 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
2804 return -EINVAL;
Jouni Malinen1400ced2013-02-14 19:10:13 +00002805
Johannes Bergbdd90d52011-12-14 12:20:27 +01002806 /* must be last in here for error handling */
2807 params.vlan = get_vlan(info, rdev);
2808 if (IS_ERR(params.vlan))
2809 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002810 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002811 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002812 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002813 /*
2814 * Don't allow userspace to change the TDLS_PEER flag,
2815 * but silently ignore attempts to change it since we
2816 * don't have state here to verify that it doesn't try
2817 * to change the flag.
2818 */
2819 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002820 /* Include parameters for TDLS peer (driver will check) */
2821 err = nl80211_set_station_tdls(info, &params);
2822 if (err)
2823 return err;
2824 /* disallow things sta doesn't support */
2825 if (params.plink_action)
2826 return -EINVAL;
2827 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2828 BIT(NL80211_STA_FLAG_WME)))
2829 return -EINVAL;
2830 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01002831 case NL80211_IFTYPE_ADHOC:
2832 /* disallow things sta doesn't support */
2833 if (params.plink_action)
2834 return -EINVAL;
2835 if (params.ht_capa)
2836 return -EINVAL;
2837 if (params.listen_interval >= 0)
2838 return -EINVAL;
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002839 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
2840 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
2841 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002842 /* reject any changes other than AUTHORIZED */
2843 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2844 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002845 break;
2846 case NL80211_IFTYPE_MESH_POINT:
2847 /* disallow things mesh doesn't support */
2848 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002849 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002850 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002851 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002852 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01002853 return -EINVAL;
Jouni Malinen1400ced2013-02-14 19:10:13 +00002854 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
2855 return -EINVAL;
2856 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
2857 return -EINVAL;
Jouni Malinen95fcfd02013-02-14 19:10:54 +00002858 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
2859 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
2860 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002861 /*
2862 * No special handling for TDLS here -- the userspace
2863 * mesh code doesn't have this bug.
2864 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07002865 if (params.sta_flags_mask &
2866 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002867 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002868 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01002869 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02002870 break;
2871 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01002872 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02002873 }
2874
Johannes Bergbdd90d52011-12-14 12:20:27 +01002875 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01002876
Johannes Berg79c97e92009-07-07 03:56:12 +02002877 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002878
Johannes Berg5727ef12007-12-19 02:03:34 +01002879 if (params.vlan)
2880 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002881
Johannes Berg5727ef12007-12-19 02:03:34 +01002882 return err;
2883}
2884
2885static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2886{
Johannes Berg4c476992010-10-04 21:36:35 +02002887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002888 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002889 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002890 struct station_parameters params;
2891 u8 *mac_addr = NULL;
2892
2893 memset(&params, 0, sizeof(params));
2894
2895 if (!info->attrs[NL80211_ATTR_MAC])
2896 return -EINVAL;
2897
Johannes Berg5727ef12007-12-19 02:03:34 +01002898 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2899 return -EINVAL;
2900
2901 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2902 return -EINVAL;
2903
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002904 if (!info->attrs[NL80211_ATTR_STA_AID])
2905 return -EINVAL;
2906
Johannes Berg5727ef12007-12-19 02:03:34 +01002907 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2908 params.supported_rates =
2909 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2910 params.supported_rates_len =
2911 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2912 params.listen_interval =
2913 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002914
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002915 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2916 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2917 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002918
Jouni Malinen1400ced2013-02-14 19:10:13 +00002919 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
2920 params.capability =
2921 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
2922 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
2923 }
2924
2925 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
2926 params.ext_capab =
2927 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
2928 params.ext_capab_len =
2929 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
2930 }
2931
Jouni Malinen36aedc92008-08-25 11:58:58 +03002932 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2933 params.ht_capa =
2934 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002935
Mahesh Palivelacf7f78d2012-10-11 08:04:52 +00002936 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
2937 params.vht_capa =
2938 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
2939
Javier Cardona96b78df2011-04-07 15:08:33 -07002940 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2941 params.plink_action =
2942 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2943
Johannes Bergbdd90d52011-12-14 12:20:27 +01002944 if (!rdev->ops->add_station)
2945 return -EOPNOTSUPP;
2946
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002947 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002948 return -EINVAL;
2949
Johannes Bergbdd90d52011-12-14 12:20:27 +01002950 switch (dev->ieee80211_ptr->iftype) {
2951 case NL80211_IFTYPE_AP:
2952 case NL80211_IFTYPE_AP_VLAN:
2953 case NL80211_IFTYPE_P2P_GO:
2954 /* parse WME attributes if sta is WME capable */
2955 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2956 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2957 info->attrs[NL80211_ATTR_STA_WME]) {
2958 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2959 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002960
Johannes Bergbdd90d52011-12-14 12:20:27 +01002961 nla = info->attrs[NL80211_ATTR_STA_WME];
2962 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2963 nl80211_sta_wme_policy);
2964 if (err)
2965 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002966
Johannes Bergbdd90d52011-12-14 12:20:27 +01002967 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2968 params.uapsd_queues =
2969 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2970 if (params.uapsd_queues &
2971 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2972 return -EINVAL;
2973
2974 if (tb[NL80211_STA_WME_MAX_SP])
2975 params.max_sp =
2976 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2977
2978 if (params.max_sp &
2979 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2980 return -EINVAL;
2981
2982 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2983 }
2984 /* TDLS peers cannot be added */
2985 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002986 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002987 /* but don't bother the driver with it */
2988 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03002989
Johannes Bergbdd90d52011-12-14 12:20:27 +01002990 /* must be last in here for error handling */
2991 params.vlan = get_vlan(info, rdev);
2992 if (IS_ERR(params.vlan))
2993 return PTR_ERR(params.vlan);
2994 break;
2995 case NL80211_IFTYPE_MESH_POINT:
2996 /* TDLS peers cannot be added */
2997 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02002998 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01002999 break;
3000 case NL80211_IFTYPE_STATION:
Johannes Berg43e652a2013-03-04 08:29:46 +00003001 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003002 /* Only TDLS peers can be added */
3003 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3004 return -EINVAL;
3005 /* Can only add if TDLS ... */
3006 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3007 return -EOPNOTSUPP;
3008 /* ... with external setup is supported */
3009 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3010 return -EOPNOTSUPP;
3011 break;
3012 default:
3013 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003014 }
3015
Johannes Bergbdd90d52011-12-14 12:20:27 +01003016 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003017
Johannes Berg79c97e92009-07-07 03:56:12 +02003018 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003019
Johannes Berg5727ef12007-12-19 02:03:34 +01003020 if (params.vlan)
3021 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003022 return err;
3023}
3024
3025static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3026{
Johannes Berg4c476992010-10-04 21:36:35 +02003027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3028 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003029 u8 *mac_addr = NULL;
3030
3031 if (info->attrs[NL80211_ATTR_MAC])
3032 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3033
Johannes Berge80cf852009-05-11 14:43:13 +02003034 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003035 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003036 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003037 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3038 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003039
Johannes Berg4c476992010-10-04 21:36:35 +02003040 if (!rdev->ops->del_station)
3041 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003042
Johannes Berg4c476992010-10-04 21:36:35 +02003043 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003044}
3045
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003046static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3047 int flags, struct net_device *dev,
3048 u8 *dst, u8 *next_hop,
3049 struct mpath_info *pinfo)
3050{
3051 void *hdr;
3052 struct nlattr *pinfoattr;
3053
3054 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3055 if (!hdr)
3056 return -1;
3057
3058 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3059 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
3060 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
3061
Johannes Bergf5ea9122009-08-07 16:17:38 +02003062 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
3063
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003064 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3065 if (!pinfoattr)
3066 goto nla_put_failure;
3067 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
3068 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3069 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00003070 if (pinfo->filled & MPATH_INFO_SN)
3071 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
3072 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003073 if (pinfo->filled & MPATH_INFO_METRIC)
3074 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
3075 pinfo->metric);
3076 if (pinfo->filled & MPATH_INFO_EXPTIME)
3077 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
3078 pinfo->exptime);
3079 if (pinfo->filled & MPATH_INFO_FLAGS)
3080 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
3081 pinfo->flags);
3082 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
3083 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3084 pinfo->discovery_timeout);
3085 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
3086 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3087 pinfo->discovery_retries);
3088
3089 nla_nest_end(msg, pinfoattr);
3090
3091 return genlmsg_end(msg, hdr);
3092
3093 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003094 genlmsg_cancel(msg, hdr);
3095 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003096}
3097
3098static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003099 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003100{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003101 struct mpath_info pinfo;
3102 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003103 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003104 u8 dst[ETH_ALEN];
3105 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003106 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003107 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003108
Johannes Berg67748892010-10-04 21:14:06 +02003109 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3110 if (err)
3111 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003112
3113 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003114 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003115 goto out_err;
3116 }
3117
Jouni Malineneec60b02009-03-20 21:21:19 +02003118 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3119 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003120 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003121 }
3122
Johannes Bergbba95fe2008-07-29 13:22:51 +02003123 while (1) {
3124 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3125 dst, next_hop, &pinfo);
3126 if (err == -ENOENT)
3127 break;
3128 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003129 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003130
3131 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3132 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3133 netdev, dst, next_hop,
3134 &pinfo) < 0)
3135 goto out;
3136
3137 path_idx++;
3138 }
3139
3140
3141 out:
3142 cb->args[1] = path_idx;
3143 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003144 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003145 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003146 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003147}
3148
3149static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3150{
Johannes Berg4c476992010-10-04 21:36:35 +02003151 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003152 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003153 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003154 struct mpath_info pinfo;
3155 struct sk_buff *msg;
3156 u8 *dst = NULL;
3157 u8 next_hop[ETH_ALEN];
3158
3159 memset(&pinfo, 0, sizeof(pinfo));
3160
3161 if (!info->attrs[NL80211_ATTR_MAC])
3162 return -EINVAL;
3163
3164 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3165
Johannes Berg4c476992010-10-04 21:36:35 +02003166 if (!rdev->ops->get_mpath)
3167 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003168
Johannes Berg4c476992010-10-04 21:36:35 +02003169 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3170 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003171
Johannes Berg79c97e92009-07-07 03:56:12 +02003172 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003173 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003174 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003175
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003176 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003177 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003178 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003179
3180 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003181 dev, dst, next_hop, &pinfo) < 0) {
3182 nlmsg_free(msg);
3183 return -ENOBUFS;
3184 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003185
Johannes Berg4c476992010-10-04 21:36:35 +02003186 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003187}
3188
3189static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3190{
Johannes Berg4c476992010-10-04 21:36:35 +02003191 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3192 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003193 u8 *dst = NULL;
3194 u8 *next_hop = NULL;
3195
3196 if (!info->attrs[NL80211_ATTR_MAC])
3197 return -EINVAL;
3198
3199 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3200 return -EINVAL;
3201
3202 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3203 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3204
Johannes Berg4c476992010-10-04 21:36:35 +02003205 if (!rdev->ops->change_mpath)
3206 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003207
Johannes Berg4c476992010-10-04 21:36:35 +02003208 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3209 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003210
Johannes Berg4c476992010-10-04 21:36:35 +02003211 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003212}
Johannes Berg4c476992010-10-04 21:36:35 +02003213
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003214static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3215{
Johannes Berg4c476992010-10-04 21:36:35 +02003216 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3217 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003218 u8 *dst = NULL;
3219 u8 *next_hop = NULL;
3220
3221 if (!info->attrs[NL80211_ATTR_MAC])
3222 return -EINVAL;
3223
3224 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3225 return -EINVAL;
3226
3227 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3228 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3229
Johannes Berg4c476992010-10-04 21:36:35 +02003230 if (!rdev->ops->add_mpath)
3231 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003232
Johannes Berg4c476992010-10-04 21:36:35 +02003233 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3234 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003235
Johannes Berg4c476992010-10-04 21:36:35 +02003236 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003237}
3238
3239static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3240{
Johannes Berg4c476992010-10-04 21:36:35 +02003241 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3242 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003243 u8 *dst = NULL;
3244
3245 if (info->attrs[NL80211_ATTR_MAC])
3246 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3247
Johannes Berg4c476992010-10-04 21:36:35 +02003248 if (!rdev->ops->del_mpath)
3249 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003250
Johannes Berg4c476992010-10-04 21:36:35 +02003251 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003252}
3253
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003254static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3255{
Johannes Berg4c476992010-10-04 21:36:35 +02003256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3257 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003258 struct bss_parameters params;
3259
3260 memset(&params, 0, sizeof(params));
3261 /* default to not changing parameters */
3262 params.use_cts_prot = -1;
3263 params.use_short_preamble = -1;
3264 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003265 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003266 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003267
3268 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3269 params.use_cts_prot =
3270 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3271 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3272 params.use_short_preamble =
3273 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3274 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3275 params.use_short_slot_time =
3276 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003277 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3278 params.basic_rates =
3279 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3280 params.basic_rates_len =
3281 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3282 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003283 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3284 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003285 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3286 params.ht_opmode =
3287 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003288
Johannes Berg4c476992010-10-04 21:36:35 +02003289 if (!rdev->ops->change_bss)
3290 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003291
Johannes Berg074ac8d2010-09-16 14:58:22 +02003292 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003293 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3294 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003295
Johannes Berg4c476992010-10-04 21:36:35 +02003296 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003297}
3298
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003299static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003300 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3301 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3302 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3303 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3304 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3305 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3306};
3307
3308static int parse_reg_rule(struct nlattr *tb[],
3309 struct ieee80211_reg_rule *reg_rule)
3310{
3311 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3312 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3313
3314 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3315 return -EINVAL;
3316 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3317 return -EINVAL;
3318 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3319 return -EINVAL;
3320 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3321 return -EINVAL;
3322 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3323 return -EINVAL;
3324
3325 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3326
3327 freq_range->start_freq_khz =
3328 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3329 freq_range->end_freq_khz =
3330 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3331 freq_range->max_bandwidth_khz =
3332 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3333
3334 power_rule->max_eirp =
3335 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3336
3337 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3338 power_rule->max_antenna_gain =
3339 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3340
3341 return 0;
3342}
3343
3344static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3345{
3346 int r;
3347 char *data = NULL;
3348
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003349 /*
3350 * You should only get this when cfg80211 hasn't yet initialized
3351 * completely when built-in to the kernel right between the time
3352 * window between nl80211_init() and regulatory_init(), if that is
3353 * even possible.
3354 */
3355 mutex_lock(&cfg80211_mutex);
3356 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003357 mutex_unlock(&cfg80211_mutex);
3358 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003359 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003360 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003361
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003362 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3363 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003364
3365 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3366
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003367 r = regulatory_hint_user(data);
3368
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003369 return r;
3370}
3371
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003372static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003373 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003374{
Johannes Berg4c476992010-10-04 21:36:35 +02003375 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003376 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003377 struct wireless_dev *wdev = dev->ieee80211_ptr;
3378 struct mesh_config cur_params;
3379 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003380 void *hdr;
3381 struct nlattr *pinfoattr;
3382 struct sk_buff *msg;
3383
Johannes Berg29cbe682010-12-03 09:20:44 +01003384 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3385 return -EOPNOTSUPP;
3386
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003387 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003388 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003389
Johannes Berg29cbe682010-12-03 09:20:44 +01003390 wdev_lock(wdev);
3391 /* If not connected, get default parameters */
3392 if (!wdev->mesh_id_len)
3393 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3394 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003395 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003396 &cur_params);
3397 wdev_unlock(wdev);
3398
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003399 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003400 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003401
3402 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003403 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003404 if (!msg)
3405 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003406 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003407 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003408 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003409 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003410 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003411 if (!pinfoattr)
3412 goto nla_put_failure;
3413 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3414 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3415 cur_params.dot11MeshRetryTimeout);
3416 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3417 cur_params.dot11MeshConfirmTimeout);
3418 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3419 cur_params.dot11MeshHoldingTimeout);
3420 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3421 cur_params.dot11MeshMaxPeerLinks);
3422 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3423 cur_params.dot11MeshMaxRetries);
3424 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3425 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003426 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3427 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003428 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3429 cur_params.auto_open_plinks);
3430 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3431 cur_params.dot11MeshHWMPmaxPREQretries);
3432 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3433 cur_params.path_refresh_time);
3434 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3435 cur_params.min_discovery_timeout);
3436 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3437 cur_params.dot11MeshHWMPactivePathTimeout);
3438 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3439 cur_params.dot11MeshHWMPpreqMinInterval);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003440 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3441 cur_params.dot11MeshHWMPperrMinInterval);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003442 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3443 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003444 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3445 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003446 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3447 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003448 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3449 cur_params.dot11MeshGateAnnouncementProtocol);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003450 NLA_PUT_U8(msg, NL80211_MESHCONF_FORWARDING,
3451 cur_params.dot11MeshForwarding);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003452 NLA_PUT_U32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3453 cur_params.rssi_threshold);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003454 nla_nest_end(msg, pinfoattr);
3455 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003456 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003457
Johannes Berg3b858752009-03-12 09:55:09 +01003458 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003459 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003460 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003461 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003462 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003463}
3464
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003465static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003466 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3467 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3468 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3469 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3470 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3471 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003472 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003473 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3474
3475 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3476 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3477 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3478 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3479 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003480 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003481 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003482 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003483 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003484 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003485 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Ashok Nagarajan55335132012-02-28 17:04:08 -08003486 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003487};
3488
Javier Cardonac80d5452010-12-16 17:37:49 -08003489static const struct nla_policy
3490 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3491 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3492 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003493 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003494 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003495 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003496 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003497};
3498
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003499static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003500 struct mesh_config *cfg,
3501 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003502{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003503 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003504 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003505
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003506#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3507do {\
3508 if (table[attr_num]) {\
3509 cfg->param = nla_fn(table[attr_num]); \
3510 mask |= (1 << (attr_num - 1)); \
3511 } \
3512} while (0);\
3513
3514
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003515 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003516 return -EINVAL;
3517 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003518 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003519 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003520 return -EINVAL;
3521
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003522 /* This makes sure that there aren't more than 32 mesh config
3523 * parameters (otherwise our bitfield scheme would not work.) */
3524 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3525
3526 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003527 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3528 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3529 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3530 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3531 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3532 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3533 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3534 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3535 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3536 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3537 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3538 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003539 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3540 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003541 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3542 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3543 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3544 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3545 nla_get_u8);
3546 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3547 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3548 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3549 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3550 nla_get_u16);
3551 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3552 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3553 nla_get_u32);
3554 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3555 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3556 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003557 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3558 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3559 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003560 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3561 dot11MeshHWMPnetDiameterTraversalTime,
3562 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3563 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003564 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3565 dot11MeshHWMPRootMode, mask,
3566 NL80211_MESHCONF_HWMP_ROOTMODE,
3567 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003568 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3569 dot11MeshHWMPRannInterval, mask,
3570 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3571 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003572 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3573 dot11MeshGateAnnouncementProtocol, mask,
3574 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3575 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003576 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3577 mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003578 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3579 mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003580 if (mask_out)
3581 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003582
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003583 return 0;
3584
3585#undef FILL_IN_MESH_PARAM_IF_SET
3586}
3587
Javier Cardonac80d5452010-12-16 17:37:49 -08003588static int nl80211_parse_mesh_setup(struct genl_info *info,
3589 struct mesh_setup *setup)
3590{
3591 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3592
3593 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3594 return -EINVAL;
3595 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3596 info->attrs[NL80211_ATTR_MESH_SETUP],
3597 nl80211_mesh_setup_params_policy))
3598 return -EINVAL;
3599
3600 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3601 setup->path_sel_proto =
3602 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3603 IEEE80211_PATH_PROTOCOL_VENDOR :
3604 IEEE80211_PATH_PROTOCOL_HWMP;
3605
3606 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3607 setup->path_metric =
3608 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3609 IEEE80211_PATH_METRIC_VENDOR :
3610 IEEE80211_PATH_METRIC_AIRTIME;
3611
Javier Cardona581a8b02011-04-07 15:08:27 -07003612
3613 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003614 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003615 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003616 if (!is_valid_ie_attr(ieattr))
3617 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003618 setup->ie = nla_data(ieattr);
3619 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003620 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003621 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3622 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003623
3624 return 0;
3625}
3626
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003627static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003628 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003629{
3630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3631 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003632 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003633 struct mesh_config cfg;
3634 u32 mask;
3635 int err;
3636
Johannes Berg29cbe682010-12-03 09:20:44 +01003637 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3638 return -EOPNOTSUPP;
3639
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003640 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003641 return -EOPNOTSUPP;
3642
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003643 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003644 if (err)
3645 return err;
3646
Johannes Berg29cbe682010-12-03 09:20:44 +01003647 wdev_lock(wdev);
3648 if (!wdev->mesh_id_len)
3649 err = -ENOLINK;
3650
3651 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003652 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003653 mask, &cfg);
3654
3655 wdev_unlock(wdev);
3656
3657 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003658}
3659
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003660static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3661{
3662 struct sk_buff *msg;
3663 void *hdr = NULL;
3664 struct nlattr *nl_reg_rules;
3665 unsigned int i;
3666 int err = -EINVAL;
3667
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003668 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003669
3670 if (!cfg80211_regdomain)
3671 goto out;
3672
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003673 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003674 if (!msg) {
3675 err = -ENOBUFS;
3676 goto out;
3677 }
3678
3679 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3680 NL80211_CMD_GET_REG);
3681 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003682 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003683
3684 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3685 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003686 if (cfg80211_regdomain->dfs_region)
3687 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3688 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003689
3690 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3691 if (!nl_reg_rules)
3692 goto nla_put_failure;
3693
3694 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3695 struct nlattr *nl_reg_rule;
3696 const struct ieee80211_reg_rule *reg_rule;
3697 const struct ieee80211_freq_range *freq_range;
3698 const struct ieee80211_power_rule *power_rule;
3699
3700 reg_rule = &cfg80211_regdomain->reg_rules[i];
3701 freq_range = &reg_rule->freq_range;
3702 power_rule = &reg_rule->power_rule;
3703
3704 nl_reg_rule = nla_nest_start(msg, i);
3705 if (!nl_reg_rule)
3706 goto nla_put_failure;
3707
3708 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3709 reg_rule->flags);
3710 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3711 freq_range->start_freq_khz);
3712 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3713 freq_range->end_freq_khz);
3714 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3715 freq_range->max_bandwidth_khz);
3716 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3717 power_rule->max_antenna_gain);
3718 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3719 power_rule->max_eirp);
3720
3721 nla_nest_end(msg, nl_reg_rule);
3722 }
3723
3724 nla_nest_end(msg, nl_reg_rules);
3725
3726 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003727 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003728 goto out;
3729
3730nla_put_failure:
3731 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003732put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003733 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003734 err = -EMSGSIZE;
3735out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003736 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003737 return err;
3738}
3739
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003740static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3741{
3742 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3743 struct nlattr *nl_reg_rule;
3744 char *alpha2 = NULL;
3745 int rem_reg_rules = 0, r = 0;
3746 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003747 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003748 struct ieee80211_regdomain *rd = NULL;
3749
3750 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3751 return -EINVAL;
3752
3753 if (!info->attrs[NL80211_ATTR_REG_RULES])
3754 return -EINVAL;
3755
3756 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3757
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003758 if (info->attrs[NL80211_ATTR_DFS_REGION])
3759 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3760
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003761 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3762 rem_reg_rules) {
3763 num_rules++;
3764 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003765 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003766 }
3767
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003768 mutex_lock(&cfg80211_mutex);
3769
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003770 if (!reg_is_valid_request(alpha2)) {
3771 r = -EINVAL;
3772 goto bad_reg;
3773 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003774
3775 size_of_regd = sizeof(struct ieee80211_regdomain) +
3776 (num_rules * sizeof(struct ieee80211_reg_rule));
3777
3778 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003779 if (!rd) {
3780 r = -ENOMEM;
3781 goto bad_reg;
3782 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003783
3784 rd->n_reg_rules = num_rules;
3785 rd->alpha2[0] = alpha2[0];
3786 rd->alpha2[1] = alpha2[1];
3787
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003788 /*
3789 * Disable DFS master mode if the DFS region was
3790 * not supported or known on this kernel.
3791 */
3792 if (reg_supported_dfs_region(dfs_region))
3793 rd->dfs_region = dfs_region;
3794
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003795 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3796 rem_reg_rules) {
3797 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3798 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3799 reg_rule_policy);
3800 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3801 if (r)
3802 goto bad_reg;
3803
3804 rule_idx++;
3805
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003806 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3807 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003808 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003809 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003810 }
3811
3812 BUG_ON(rule_idx != num_rules);
3813
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003814 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003815
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003816 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003817
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003818 return r;
3819
Johannes Bergd2372b32008-10-24 20:32:20 +02003820 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003821 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003822 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003823 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003824}
3825
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003826static int validate_scan_freqs(struct nlattr *freqs)
3827{
3828 struct nlattr *attr1, *attr2;
3829 int n_channels = 0, tmp1, tmp2;
3830
3831 nla_for_each_nested(attr1, freqs, tmp1) {
3832 n_channels++;
3833 /*
3834 * Some hardware has a limited channel list for
3835 * scanning, and it is pretty much nonsensical
3836 * to scan for a channel twice, so disallow that
3837 * and don't require drivers to check that the
3838 * channel list they get isn't longer than what
3839 * they can scan, as long as they can scan all
3840 * the channels they registered at once.
3841 */
3842 nla_for_each_nested(attr2, freqs, tmp2)
3843 if (attr1 != attr2 &&
3844 nla_get_u32(attr1) == nla_get_u32(attr2))
3845 return 0;
3846 }
3847
3848 return n_channels;
3849}
3850
Johannes Berg2a519312009-02-10 21:25:55 +01003851static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3852{
Johannes Berg4c476992010-10-04 21:36:35 +02003853 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3854 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003855 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003856 struct nlattr *attr;
3857 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003858 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003859 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003860
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003861 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3862 return -EINVAL;
3863
Johannes Berg79c97e92009-07-07 03:56:12 +02003864 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003865
Johannes Berg4c476992010-10-04 21:36:35 +02003866 if (!rdev->ops->scan)
3867 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003868
Johannes Berg4c476992010-10-04 21:36:35 +02003869 if (rdev->scan_req)
3870 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003871
3872 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003873 n_channels = validate_scan_freqs(
3874 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003875 if (!n_channels)
3876 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003877 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003878 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003879 n_channels = 0;
3880
Johannes Berg2a519312009-02-10 21:25:55 +01003881 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3882 if (wiphy->bands[band])
3883 n_channels += wiphy->bands[band]->n_channels;
3884 }
3885
3886 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3887 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3888 n_ssids++;
3889
Johannes Berg4c476992010-10-04 21:36:35 +02003890 if (n_ssids > wiphy->max_scan_ssids)
3891 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003892
Jouni Malinen70692ad2009-02-16 19:39:13 +02003893 if (info->attrs[NL80211_ATTR_IE])
3894 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3895 else
3896 ie_len = 0;
3897
Johannes Berg4c476992010-10-04 21:36:35 +02003898 if (ie_len > wiphy->max_scan_ie_len)
3899 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003900
Johannes Berg2a519312009-02-10 21:25:55 +01003901 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003902 + sizeof(*request->ssids) * n_ssids
3903 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003904 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003905 if (!request)
3906 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003907
Johannes Berg2a519312009-02-10 21:25:55 +01003908 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003909 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003910 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003911 if (ie_len) {
3912 if (request->ssids)
3913 request->ie = (void *)(request->ssids + n_ssids);
3914 else
3915 request->ie = (void *)(request->channels + n_channels);
3916 }
Johannes Berg2a519312009-02-10 21:25:55 +01003917
Johannes Berg584991d2009-11-02 13:32:03 +01003918 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003919 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3920 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003921 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003922 struct ieee80211_channel *chan;
3923
3924 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3925
3926 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003927 err = -EINVAL;
3928 goto out_free;
3929 }
Johannes Berg584991d2009-11-02 13:32:03 +01003930
3931 /* ignore disabled channels */
3932 if (chan->flags & IEEE80211_CHAN_DISABLED)
3933 continue;
3934
3935 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003936 i++;
3937 }
3938 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003939 enum ieee80211_band band;
3940
Johannes Berg2a519312009-02-10 21:25:55 +01003941 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003942 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3943 int j;
3944 if (!wiphy->bands[band])
3945 continue;
3946 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003947 struct ieee80211_channel *chan;
3948
3949 chan = &wiphy->bands[band]->channels[j];
3950
3951 if (chan->flags & IEEE80211_CHAN_DISABLED)
3952 continue;
3953
3954 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003955 i++;
3956 }
3957 }
3958 }
3959
Johannes Berg584991d2009-11-02 13:32:03 +01003960 if (!i) {
3961 err = -EINVAL;
3962 goto out_free;
3963 }
3964
3965 request->n_channels = i;
3966
Johannes Berg2a519312009-02-10 21:25:55 +01003967 i = 0;
3968 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3969 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003970 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003971 err = -EINVAL;
3972 goto out_free;
3973 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003974 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003975 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003976 i++;
3977 }
3978 }
3979
Jouni Malinen70692ad2009-02-16 19:39:13 +02003980 if (info->attrs[NL80211_ATTR_IE]) {
3981 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003982 memcpy((void *)request->ie,
3983 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003984 request->ie_len);
3985 }
Sunil Dutt Undekari08166fc2012-03-05 21:01:23 +05303986 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Sunil Dutt Undekariae86f142012-03-16 20:17:03 +05303987 if (wiphy->bands[i])
3988 request->rates[i] =
3989 (1 << wiphy->bands[i]->n_bitrates) - 1;
Sunil Dutt Undekari08166fc2012-03-05 21:01:23 +05303990 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3991 nla_for_each_nested(attr,
3992 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3993 tmp) {
3994 enum ieee80211_band band = nla_type(attr);
3995 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
3996 err = -EINVAL;
3997 goto out_free;
3998 }
3999 err = ieee80211_get_ratemask(wiphy->bands[band],
4000 nla_data(attr),
4001 nla_len(attr),
4002 &request->rates[band]);
4003 if (err)
4004 goto out_free;
4005 }
4006 }
Jouni Malinen70692ad2009-02-16 19:39:13 +02004007
Johannes Berg34850ab2011-07-18 18:08:35 +02004008 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004009 if (wiphy->bands[i])
4010 request->rates[i] =
4011 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004012
4013 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4014 nla_for_each_nested(attr,
4015 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4016 tmp) {
4017 enum ieee80211_band band = nla_type(attr);
4018
Dan Carpenter84404622011-07-29 11:52:18 +03004019 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004020 err = -EINVAL;
4021 goto out_free;
4022 }
4023 err = ieee80211_get_ratemask(wiphy->bands[band],
4024 nla_data(attr),
4025 nla_len(attr),
4026 &request->rates[band]);
4027 if (err)
4028 goto out_free;
4029 }
4030 }
4031
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304032 request->no_cck =
4033 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4034
Johannes Berg463d0182009-07-14 00:33:35 +02004035 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004036 request->wiphy = &rdev->wiphy;
Sunil Dutt Undekari08166fc2012-03-05 21:01:23 +05304037 request->no_cck =
4038 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Johannes Berg2a519312009-02-10 21:25:55 +01004039
Johannes Berg79c97e92009-07-07 03:56:12 +02004040 rdev->scan_req = request;
4041 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004042
Johannes Berg463d0182009-07-14 00:33:35 +02004043 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004044 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004045 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004046 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004047 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004048 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004049 kfree(request);
4050 }
Johannes Berg3b858752009-03-12 09:55:09 +01004051
Johannes Berg2a519312009-02-10 21:25:55 +01004052 return err;
4053}
4054
Luciano Coelho807f8a82011-05-11 17:09:35 +03004055static int nl80211_start_sched_scan(struct sk_buff *skb,
4056 struct genl_info *info)
4057{
4058 struct cfg80211_sched_scan_request *request;
4059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4060 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004061 struct nlattr *attr;
4062 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004063 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004064 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004065 enum ieee80211_band band;
4066 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004067 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004068
4069 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4070 !rdev->ops->sched_scan_start)
4071 return -EOPNOTSUPP;
4072
4073 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4074 return -EINVAL;
4075
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004076 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4077 return -EINVAL;
4078
4079 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4080 if (interval == 0)
4081 return -EINVAL;
4082
Luciano Coelho807f8a82011-05-11 17:09:35 +03004083 wiphy = &rdev->wiphy;
4084
4085 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4086 n_channels = validate_scan_freqs(
4087 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4088 if (!n_channels)
4089 return -EINVAL;
4090 } else {
4091 n_channels = 0;
4092
4093 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4094 if (wiphy->bands[band])
4095 n_channels += wiphy->bands[band]->n_channels;
4096 }
4097
4098 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4099 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4100 tmp)
4101 n_ssids++;
4102
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004103 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004104 return -EINVAL;
4105
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004106 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4107 nla_for_each_nested(attr,
4108 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4109 tmp)
4110 n_match_sets++;
4111
4112 if (n_match_sets > wiphy->max_match_sets)
4113 return -EINVAL;
4114
Luciano Coelho807f8a82011-05-11 17:09:35 +03004115 if (info->attrs[NL80211_ATTR_IE])
4116 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4117 else
4118 ie_len = 0;
4119
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004120 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004121 return -EINVAL;
4122
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004123 mutex_lock(&rdev->sched_scan_mtx);
4124
4125 if (rdev->sched_scan_req) {
4126 err = -EINPROGRESS;
4127 goto out;
4128 }
4129
Luciano Coelho807f8a82011-05-11 17:09:35 +03004130 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004131 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004132 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004133 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004134 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004135 if (!request) {
4136 err = -ENOMEM;
4137 goto out;
4138 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004139
4140 if (n_ssids)
4141 request->ssids = (void *)&request->channels[n_channels];
4142 request->n_ssids = n_ssids;
4143 if (ie_len) {
4144 if (request->ssids)
4145 request->ie = (void *)(request->ssids + n_ssids);
4146 else
4147 request->ie = (void *)(request->channels + n_channels);
4148 }
4149
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004150 if (n_match_sets) {
4151 if (request->ie)
4152 request->match_sets = (void *)(request->ie + ie_len);
4153 else if (request->ssids)
4154 request->match_sets =
4155 (void *)(request->ssids + n_ssids);
4156 else
4157 request->match_sets =
4158 (void *)(request->channels + n_channels);
4159 }
4160 request->n_match_sets = n_match_sets;
4161
Luciano Coelho807f8a82011-05-11 17:09:35 +03004162 i = 0;
4163 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4164 /* user specified, bail out if channel not found */
4165 nla_for_each_nested(attr,
4166 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4167 tmp) {
4168 struct ieee80211_channel *chan;
4169
4170 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4171
4172 if (!chan) {
4173 err = -EINVAL;
4174 goto out_free;
4175 }
4176
4177 /* ignore disabled channels */
4178 if (chan->flags & IEEE80211_CHAN_DISABLED)
4179 continue;
4180
4181 request->channels[i] = chan;
4182 i++;
4183 }
4184 } else {
4185 /* all channels */
4186 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4187 int j;
4188 if (!wiphy->bands[band])
4189 continue;
4190 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4191 struct ieee80211_channel *chan;
4192
4193 chan = &wiphy->bands[band]->channels[j];
4194
4195 if (chan->flags & IEEE80211_CHAN_DISABLED)
4196 continue;
4197
4198 request->channels[i] = chan;
4199 i++;
4200 }
4201 }
4202 }
4203
4204 if (!i) {
4205 err = -EINVAL;
4206 goto out_free;
4207 }
4208
4209 request->n_channels = i;
4210
4211 i = 0;
4212 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4213 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4214 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004215 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004216 err = -EINVAL;
4217 goto out_free;
4218 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004219 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004220 memcpy(request->ssids[i].ssid, nla_data(attr),
4221 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004222 i++;
4223 }
4224 }
4225
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004226 i = 0;
4227 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4228 nla_for_each_nested(attr,
4229 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4230 tmp) {
4231 struct nlattr *ssid;
4232
4233 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4234 nla_data(attr), nla_len(attr),
4235 nl80211_match_policy);
4236 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4237 if (ssid) {
4238 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4239 err = -EINVAL;
4240 goto out_free;
4241 }
4242 memcpy(request->match_sets[i].ssid.ssid,
4243 nla_data(ssid), nla_len(ssid));
4244 request->match_sets[i].ssid.ssid_len =
4245 nla_len(ssid);
4246 }
4247 i++;
4248 }
4249 }
4250
Luciano Coelho807f8a82011-05-11 17:09:35 +03004251 if (info->attrs[NL80211_ATTR_IE]) {
4252 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4253 memcpy((void *)request->ie,
4254 nla_data(info->attrs[NL80211_ATTR_IE]),
4255 request->ie_len);
4256 }
4257
4258 request->dev = dev;
4259 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004260 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004261
4262 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4263 if (!err) {
4264 rdev->sched_scan_req = request;
4265 nl80211_send_sched_scan(rdev, dev,
4266 NL80211_CMD_START_SCHED_SCAN);
4267 goto out;
4268 }
4269
4270out_free:
4271 kfree(request);
4272out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004273 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004274 return err;
4275}
4276
4277static int nl80211_stop_sched_scan(struct sk_buff *skb,
4278 struct genl_info *info)
4279{
4280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004281 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004282
4283 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4284 !rdev->ops->sched_scan_stop)
4285 return -EOPNOTSUPP;
4286
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004287 mutex_lock(&rdev->sched_scan_mtx);
4288 err = __cfg80211_stop_sched_scan(rdev, false);
4289 mutex_unlock(&rdev->sched_scan_mtx);
4290
4291 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004292}
4293
Johannes Berg9720bb32011-06-21 09:45:33 +02004294static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4295 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004296 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004297 struct wireless_dev *wdev,
4298 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004299{
Johannes Berg48ab9052009-07-10 18:42:31 +02004300 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004301 void *hdr;
4302 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004303
4304 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004305
Johannes Berg9720bb32011-06-21 09:45:33 +02004306 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004307 NL80211_CMD_NEW_SCAN_RESULTS);
4308 if (!hdr)
4309 return -1;
4310
Johannes Berg9720bb32011-06-21 09:45:33 +02004311 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4312
Johannes Bergf5ea9122009-08-07 16:17:38 +02004313 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02004314 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01004315
4316 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4317 if (!bss)
4318 goto nla_put_failure;
4319 if (!is_zero_ether_addr(res->bssid))
4320 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4321 if (res->information_elements && res->len_information_elements)
4322 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4323 res->len_information_elements,
4324 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004325 if (res->beacon_ies && res->len_beacon_ies &&
4326 res->beacon_ies != res->information_elements)
4327 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4328 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004329 if (res->tsf)
4330 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4331 if (res->beacon_interval)
4332 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4333 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4334 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004335 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4336 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004337
Johannes Berg77965c92009-02-18 18:45:06 +01004338 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004339 case CFG80211_SIGNAL_TYPE_MBM:
4340 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4341 break;
4342 case CFG80211_SIGNAL_TYPE_UNSPEC:
4343 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4344 break;
4345 default:
4346 break;
4347 }
4348
Johannes Berg48ab9052009-07-10 18:42:31 +02004349 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004350 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004351 case NL80211_IFTYPE_STATION:
4352 if (intbss == wdev->current_bss)
4353 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4354 NL80211_BSS_STATUS_ASSOCIATED);
Johannes Berg48ab9052009-07-10 18:42:31 +02004355 break;
4356 case NL80211_IFTYPE_ADHOC:
4357 if (intbss == wdev->current_bss)
4358 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4359 NL80211_BSS_STATUS_IBSS_JOINED);
4360 break;
4361 default:
4362 break;
4363 }
4364
Johannes Berg2a519312009-02-10 21:25:55 +01004365 nla_nest_end(msg, bss);
4366
4367 return genlmsg_end(msg, hdr);
4368
4369 nla_put_failure:
4370 genlmsg_cancel(msg, hdr);
4371 return -EMSGSIZE;
4372}
4373
4374static int nl80211_dump_scan(struct sk_buff *skb,
4375 struct netlink_callback *cb)
4376{
Johannes Berg48ab9052009-07-10 18:42:31 +02004377 struct cfg80211_registered_device *rdev;
4378 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004379 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004380 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004381 int start = cb->args[1], idx = 0;
4382 int err;
4383
Johannes Berg67748892010-10-04 21:14:06 +02004384 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4385 if (err)
4386 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004387
Johannes Berg48ab9052009-07-10 18:42:31 +02004388 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004389
Johannes Berg48ab9052009-07-10 18:42:31 +02004390 wdev_lock(wdev);
4391 spin_lock_bh(&rdev->bss_lock);
4392 cfg80211_bss_expire(rdev);
4393
Johannes Berg9720bb32011-06-21 09:45:33 +02004394 cb->seq = rdev->bss_generation;
4395
Johannes Berg48ab9052009-07-10 18:42:31 +02004396 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004397 if (++idx <= start)
4398 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004399 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004400 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004401 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004402 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004403 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004404 }
4405 }
4406
Johannes Berg48ab9052009-07-10 18:42:31 +02004407 spin_unlock_bh(&rdev->bss_lock);
4408 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004409
4410 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004411 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004412
Johannes Berg67748892010-10-04 21:14:06 +02004413 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004414}
4415
Holger Schurig61fa7132009-11-11 12:25:40 +01004416static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4417 int flags, struct net_device *dev,
4418 struct survey_info *survey)
4419{
4420 void *hdr;
4421 struct nlattr *infoattr;
4422
Holger Schurig61fa7132009-11-11 12:25:40 +01004423 hdr = nl80211hdr_put(msg, pid, seq, flags,
4424 NL80211_CMD_NEW_SURVEY_RESULTS);
4425 if (!hdr)
4426 return -ENOMEM;
4427
4428 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4429
4430 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4431 if (!infoattr)
4432 goto nla_put_failure;
4433
4434 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4435 survey->channel->center_freq);
4436 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4437 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4438 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004439 if (survey->filled & SURVEY_INFO_IN_USE)
4440 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c29a2010-10-09 02:39:29 +02004441 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4442 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4443 survey->channel_time);
4444 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4445 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4446 survey->channel_time_busy);
4447 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4448 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4449 survey->channel_time_ext_busy);
4450 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4451 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4452 survey->channel_time_rx);
4453 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4454 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4455 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004456
4457 nla_nest_end(msg, infoattr);
4458
4459 return genlmsg_end(msg, hdr);
4460
4461 nla_put_failure:
4462 genlmsg_cancel(msg, hdr);
4463 return -EMSGSIZE;
4464}
4465
4466static int nl80211_dump_survey(struct sk_buff *skb,
4467 struct netlink_callback *cb)
4468{
4469 struct survey_info survey;
4470 struct cfg80211_registered_device *dev;
4471 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004472 int survey_idx = cb->args[1];
4473 int res;
4474
Johannes Berg67748892010-10-04 21:14:06 +02004475 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4476 if (res)
4477 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004478
4479 if (!dev->ops->dump_survey) {
4480 res = -EOPNOTSUPP;
4481 goto out_err;
4482 }
4483
4484 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004485 struct ieee80211_channel *chan;
4486
Holger Schurig61fa7132009-11-11 12:25:40 +01004487 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4488 &survey);
4489 if (res == -ENOENT)
4490 break;
4491 if (res)
4492 goto out_err;
4493
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004494 /* Survey without a channel doesn't make sense */
4495 if (!survey.channel) {
4496 res = -EINVAL;
4497 goto out;
4498 }
4499
4500 chan = ieee80211_get_channel(&dev->wiphy,
4501 survey.channel->center_freq);
4502 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4503 survey_idx++;
4504 continue;
4505 }
4506
Holger Schurig61fa7132009-11-11 12:25:40 +01004507 if (nl80211_send_survey(skb,
4508 NETLINK_CB(cb->skb).pid,
4509 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4510 netdev,
4511 &survey) < 0)
4512 goto out;
4513 survey_idx++;
4514 }
4515
4516 out:
4517 cb->args[1] = survey_idx;
4518 res = skb->len;
4519 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004520 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004521 return res;
4522}
4523
Jouni Malinen255e7372009-03-20 21:21:17 +02004524static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4525{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004526 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004527}
4528
Samuel Ortizb23aa672009-07-01 21:26:54 +02004529static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4530{
4531 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4532 NL80211_WPA_VERSION_2));
4533}
4534
Jouni Malinen636a5d32009-03-19 13:39:22 +02004535static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4536{
Johannes Berg4c476992010-10-04 21:36:35 +02004537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4538 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004539 struct ieee80211_channel *chan;
4540 const u8 *bssid, *ssid, *ie = NULL;
4541 int err, ssid_len, ie_len = 0;
4542 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004543 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004544 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004545
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004546 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4547 return -EINVAL;
4548
4549 if (!info->attrs[NL80211_ATTR_MAC])
4550 return -EINVAL;
4551
Jouni Malinen17780922009-03-27 20:52:47 +02004552 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4553 return -EINVAL;
4554
Johannes Berg19957bb2009-07-02 17:20:43 +02004555 if (!info->attrs[NL80211_ATTR_SSID])
4556 return -EINVAL;
4557
4558 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4559 return -EINVAL;
4560
Johannes Bergfffd0932009-07-08 14:22:54 +02004561 err = nl80211_parse_key(info, &key);
4562 if (err)
4563 return err;
4564
4565 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004566 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4567 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004568 if (!key.p.key || !key.p.key_len)
4569 return -EINVAL;
4570 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4571 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4572 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4573 key.p.key_len != WLAN_KEY_LEN_WEP104))
4574 return -EINVAL;
4575 if (key.idx > 4)
4576 return -EINVAL;
4577 } else {
4578 key.p.key_len = 0;
4579 key.p.key = NULL;
4580 }
4581
Johannes Bergafea0b72010-08-10 09:46:42 +02004582 if (key.idx >= 0) {
4583 int i;
4584 bool ok = false;
4585 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4586 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4587 ok = true;
4588 break;
4589 }
4590 }
Johannes Berg4c476992010-10-04 21:36:35 +02004591 if (!ok)
4592 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004593 }
4594
Johannes Berg4c476992010-10-04 21:36:35 +02004595 if (!rdev->ops->auth)
4596 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004597
Johannes Berg074ac8d2010-09-16 14:58:22 +02004598 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004599 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4600 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004601
Johannes Berg19957bb2009-07-02 17:20:43 +02004602 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004603 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004604 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004605 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4606 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004607
Johannes Berg19957bb2009-07-02 17:20:43 +02004608 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4609 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4610
4611 if (info->attrs[NL80211_ATTR_IE]) {
4612 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4613 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4614 }
4615
4616 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004617 if (!nl80211_valid_auth_type(auth_type))
4618 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004619
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004620 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4621
Johannes Berg95de8172012-01-20 13:55:25 +01004622 /*
4623 * Since we no longer track auth state, ignore
4624 * requests to only change local state.
4625 */
4626 if (local_state_change)
4627 return 0;
4628
Johannes Berg4c476992010-10-04 21:36:35 +02004629 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4630 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004631 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004632}
4633
Johannes Bergc0692b82010-08-27 14:26:53 +03004634static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4635 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004636 struct cfg80211_crypto_settings *settings,
4637 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004638{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004639 memset(settings, 0, sizeof(*settings));
4640
Samuel Ortizb23aa672009-07-01 21:26:54 +02004641 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4642
Johannes Bergc0692b82010-08-27 14:26:53 +03004643 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4644 u16 proto;
4645 proto = nla_get_u16(
4646 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4647 settings->control_port_ethertype = cpu_to_be16(proto);
4648 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4649 proto != ETH_P_PAE)
4650 return -EINVAL;
4651 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4652 settings->control_port_no_encrypt = true;
4653 } else
4654 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4655
Samuel Ortizb23aa672009-07-01 21:26:54 +02004656 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4657 void *data;
4658 int len, i;
4659
4660 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4661 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4662 settings->n_ciphers_pairwise = len / sizeof(u32);
4663
4664 if (len % sizeof(u32))
4665 return -EINVAL;
4666
Johannes Berg3dc27d22009-07-02 21:36:37 +02004667 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004668 return -EINVAL;
4669
4670 memcpy(settings->ciphers_pairwise, data, len);
4671
4672 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004673 if (!cfg80211_supported_cipher_suite(
4674 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004675 settings->ciphers_pairwise[i]))
4676 return -EINVAL;
4677 }
4678
4679 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4680 settings->cipher_group =
4681 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004682 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4683 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004684 return -EINVAL;
4685 }
4686
4687 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4688 settings->wpa_versions =
4689 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4690 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4691 return -EINVAL;
4692 }
4693
4694 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4695 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004696 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004697
4698 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4699 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4700 settings->n_akm_suites = len / sizeof(u32);
4701
4702 if (len % sizeof(u32))
4703 return -EINVAL;
4704
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004705 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4706 return -EINVAL;
4707
Samuel Ortizb23aa672009-07-01 21:26:54 +02004708 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004709 }
4710
4711 return 0;
4712}
4713
Jouni Malinen636a5d32009-03-19 13:39:22 +02004714static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4715{
Johannes Berg4c476992010-10-04 21:36:35 +02004716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4717 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004718 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004719 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004720 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004721 int err, ssid_len, ie_len = 0;
4722 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004723 u32 flags = 0;
4724 struct ieee80211_ht_cap *ht_capa = NULL;
4725 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004726
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004727 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4728 return -EINVAL;
4729
4730 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004731 !info->attrs[NL80211_ATTR_SSID] ||
4732 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004733 return -EINVAL;
4734
Johannes Berg4c476992010-10-04 21:36:35 +02004735 if (!rdev->ops->assoc)
4736 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004737
Johannes Berg074ac8d2010-09-16 14:58:22 +02004738 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004739 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4740 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004741
Johannes Berg19957bb2009-07-02 17:20:43 +02004742 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004743
Johannes Berg19957bb2009-07-02 17:20:43 +02004744 chan = ieee80211_get_channel(&rdev->wiphy,
4745 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004746 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4747 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004748
Johannes Berg19957bb2009-07-02 17:20:43 +02004749 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4750 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004751
4752 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004753 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4754 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004755 }
4756
Jouni Malinendc6382c2009-05-06 22:09:37 +03004757 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004758 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004759 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004760 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004761 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004762 else if (mfp != NL80211_MFP_NO)
4763 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004764 }
4765
Johannes Berg3e5d7642009-07-07 14:37:26 +02004766 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4767 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4768
Ben Greear7e7c8922011-11-18 11:31:59 -08004769 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4770 flags |= ASSOC_REQ_DISABLE_HT;
4771
4772 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4773 ht_capa_mask =
4774 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4775
4776 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4777 if (!ht_capa_mask)
4778 return -EINVAL;
4779 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4780 }
4781
Johannes Bergc0692b82010-08-27 14:26:53 +03004782 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004783 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004784 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4785 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004786 &crypto, flags, ht_capa,
4787 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004788
Jouni Malinen636a5d32009-03-19 13:39:22 +02004789 return err;
4790}
4791
4792static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4793{
Johannes Berg4c476992010-10-04 21:36:35 +02004794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4795 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004796 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004797 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004798 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004799 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004800
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004801 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4802 return -EINVAL;
4803
4804 if (!info->attrs[NL80211_ATTR_MAC])
4805 return -EINVAL;
4806
4807 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4808 return -EINVAL;
4809
Johannes Berg4c476992010-10-04 21:36:35 +02004810 if (!rdev->ops->deauth)
4811 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004812
Johannes Berg074ac8d2010-09-16 14:58:22 +02004813 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004814 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4815 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004816
Johannes Berg19957bb2009-07-02 17:20:43 +02004817 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004818
Johannes Berg19957bb2009-07-02 17:20:43 +02004819 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4820 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004821 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004822 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004823 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004824
4825 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004826 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4827 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004828 }
4829
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004830 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4831
Johannes Berg4c476992010-10-04 21:36:35 +02004832 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4833 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004834}
4835
4836static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4837{
Johannes Berg4c476992010-10-04 21:36:35 +02004838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4839 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004840 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004841 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004842 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004843 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004844
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004845 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4846 return -EINVAL;
4847
4848 if (!info->attrs[NL80211_ATTR_MAC])
4849 return -EINVAL;
4850
4851 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4852 return -EINVAL;
4853
Johannes Berg4c476992010-10-04 21:36:35 +02004854 if (!rdev->ops->disassoc)
4855 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004856
Johannes Berg074ac8d2010-09-16 14:58:22 +02004857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004858 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4859 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004860
Johannes Berg19957bb2009-07-02 17:20:43 +02004861 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004862
Johannes Berg19957bb2009-07-02 17:20:43 +02004863 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4864 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004865 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004866 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004867 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004868
4869 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004870 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4871 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004872 }
4873
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004874 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4875
Johannes Berg4c476992010-10-04 21:36:35 +02004876 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4877 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004878}
4879
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004880static bool
4881nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4882 int mcast_rate[IEEE80211_NUM_BANDS],
4883 int rateval)
4884{
4885 struct wiphy *wiphy = &rdev->wiphy;
4886 bool found = false;
4887 int band, i;
4888
4889 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4890 struct ieee80211_supported_band *sband;
4891
4892 sband = wiphy->bands[band];
4893 if (!sband)
4894 continue;
4895
4896 for (i = 0; i < sband->n_bitrates; i++) {
4897 if (sband->bitrates[i].bitrate == rateval) {
4898 mcast_rate[band] = i + 1;
4899 found = true;
4900 break;
4901 }
4902 }
4903 }
4904
4905 return found;
4906}
4907
Johannes Berg04a773a2009-04-19 21:24:32 +02004908static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4909{
Johannes Berg4c476992010-10-04 21:36:35 +02004910 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4911 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004912 struct cfg80211_ibss_params ibss;
4913 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004914 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004915 int err;
4916
Johannes Berg8e30bc52009-04-22 17:45:38 +02004917 memset(&ibss, 0, sizeof(ibss));
4918
Johannes Berg04a773a2009-04-19 21:24:32 +02004919 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4920 return -EINVAL;
4921
4922 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4923 !info->attrs[NL80211_ATTR_SSID] ||
4924 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4925 return -EINVAL;
4926
Johannes Berg8e30bc52009-04-22 17:45:38 +02004927 ibss.beacon_interval = 100;
4928
4929 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4930 ibss.beacon_interval =
4931 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4932 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4933 return -EINVAL;
4934 }
4935
Johannes Berg4c476992010-10-04 21:36:35 +02004936 if (!rdev->ops->join_ibss)
4937 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004938
Johannes Berg4c476992010-10-04 21:36:35 +02004939 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4940 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004941
Johannes Berg79c97e92009-07-07 03:56:12 +02004942 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004943
Johannes Berg39193492011-09-16 13:45:25 +02004944 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004945 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004946
4947 if (!is_valid_ether_addr(ibss.bssid))
4948 return -EINVAL;
4949 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004950 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4951 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4952
4953 if (info->attrs[NL80211_ATTR_IE]) {
4954 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4955 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4956 }
4957
Alexander Simon54858ee2011-11-30 16:56:32 +01004958 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4959 enum nl80211_channel_type channel_type;
4960
4961 channel_type = nla_get_u32(
4962 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4963 if (channel_type != NL80211_CHAN_NO_HT &&
4964 channel_type != NL80211_CHAN_HT20 &&
4965 channel_type != NL80211_CHAN_HT40MINUS &&
4966 channel_type != NL80211_CHAN_HT40PLUS)
4967 return -EINVAL;
4968
4969 if (channel_type != NL80211_CHAN_NO_HT &&
4970 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4971 return -EINVAL;
4972
4973 ibss.channel_type = channel_type;
4974 } else {
4975 ibss.channel_type = NL80211_CHAN_NO_HT;
4976 }
4977
4978 ibss.channel = rdev_freq_to_chan(rdev,
4979 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4980 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02004981 if (!ibss.channel ||
4982 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004983 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4984 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004985
Alexander Simon54858ee2011-11-30 16:56:32 +01004986 /* Both channels should be able to initiate communication */
4987 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4988 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4989 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4990 ibss.channel_type))
4991 return -EINVAL;
4992
Johannes Berg04a773a2009-04-19 21:24:32 +02004993 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004994 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004995
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004996 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4997 u8 *rates =
4998 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4999 int n_rates =
5000 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5001 struct ieee80211_supported_band *sband =
5002 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005003
Johannes Berg34850ab2011-07-18 18:08:35 +02005004 err = ieee80211_get_ratemask(sband, rates, n_rates,
5005 &ibss.basic_rates);
5006 if (err)
5007 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005008 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005009
5010 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5011 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5012 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5013 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005014
Johannes Berg4c476992010-10-04 21:36:35 +02005015 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5016 connkeys = nl80211_parse_connkeys(rdev,
5017 info->attrs[NL80211_ATTR_KEYS]);
5018 if (IS_ERR(connkeys))
5019 return PTR_ERR(connkeys);
5020 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005021
Antonio Quartulli267335d2012-01-31 20:25:47 +01005022 ibss.control_port =
5023 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5024
Johannes Berg4c476992010-10-04 21:36:35 +02005025 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005026 if (err)
5027 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005028 return err;
5029}
5030
5031static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5032{
Johannes Berg4c476992010-10-04 21:36:35 +02005033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5034 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005035
Johannes Berg4c476992010-10-04 21:36:35 +02005036 if (!rdev->ops->leave_ibss)
5037 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005038
Johannes Berg4c476992010-10-04 21:36:35 +02005039 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5040 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005041
Johannes Berg4c476992010-10-04 21:36:35 +02005042 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005043}
5044
Johannes Bergaff89a92009-07-01 21:26:51 +02005045#ifdef CONFIG_NL80211_TESTMODE
5046static struct genl_multicast_group nl80211_testmode_mcgrp = {
5047 .name = "testmode",
5048};
5049
5050static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5051{
Johannes Berg4c476992010-10-04 21:36:35 +02005052 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005053 int err;
5054
5055 if (!info->attrs[NL80211_ATTR_TESTDATA])
5056 return -EINVAL;
5057
Johannes Bergaff89a92009-07-01 21:26:51 +02005058 err = -EOPNOTSUPP;
5059 if (rdev->ops->testmode_cmd) {
5060 rdev->testmode_info = info;
5061 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5062 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5063 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5064 rdev->testmode_info = NULL;
5065 }
5066
Johannes Bergaff89a92009-07-01 21:26:51 +02005067 return err;
5068}
5069
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005070static int nl80211_testmode_dump(struct sk_buff *skb,
5071 struct netlink_callback *cb)
5072{
Johannes Berg00918d32011-12-13 17:22:05 +01005073 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005074 int err;
5075 long phy_idx;
5076 void *data = NULL;
5077 int data_len = 0;
5078
5079 if (cb->args[0]) {
5080 /*
5081 * 0 is a valid index, but not valid for args[0],
5082 * so we need to offset by 1.
5083 */
5084 phy_idx = cb->args[0] - 1;
5085 } else {
5086 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5087 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5088 nl80211_policy);
5089 if (err)
5090 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005091 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
5092 phy_idx = nla_get_u32(
5093 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
5094 } else {
5095 struct net_device *netdev;
5096
5097 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
5098 nl80211_fam.attrbuf,
5099 &rdev, &netdev);
5100 if (err)
5101 return err;
5102 dev_put(netdev);
5103 phy_idx = rdev->wiphy_idx;
5104 cfg80211_unlock_rdev(rdev);
5105 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005106 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5107 cb->args[1] =
5108 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5109 }
5110
5111 if (cb->args[1]) {
5112 data = nla_data((void *)cb->args[1]);
5113 data_len = nla_len((void *)cb->args[1]);
5114 }
5115
5116 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005117 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5118 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005119 mutex_unlock(&cfg80211_mutex);
5120 return -ENOENT;
5121 }
Johannes Berg00918d32011-12-13 17:22:05 +01005122 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005123 mutex_unlock(&cfg80211_mutex);
5124
Johannes Berg00918d32011-12-13 17:22:05 +01005125 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005126 err = -EOPNOTSUPP;
5127 goto out_err;
5128 }
5129
5130 while (1) {
5131 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5132 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5133 NL80211_CMD_TESTMODE);
5134 struct nlattr *tmdata;
5135
Johannes Berg00918d32011-12-13 17:22:05 +01005136 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005137 genlmsg_cancel(skb, hdr);
5138 break;
5139 }
5140
5141 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5142 if (!tmdata) {
5143 genlmsg_cancel(skb, hdr);
5144 break;
5145 }
Johannes Berg00918d32011-12-13 17:22:05 +01005146 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5147 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005148 nla_nest_end(skb, tmdata);
5149
5150 if (err == -ENOBUFS || err == -ENOENT) {
5151 genlmsg_cancel(skb, hdr);
5152 break;
5153 } else if (err) {
5154 genlmsg_cancel(skb, hdr);
5155 goto out_err;
5156 }
5157
5158 genlmsg_end(skb, hdr);
5159 }
5160
5161 err = skb->len;
5162 /* see above */
5163 cb->args[0] = phy_idx + 1;
5164 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005165 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005166 return err;
5167}
5168
Johannes Bergaff89a92009-07-01 21:26:51 +02005169static struct sk_buff *
5170__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5171 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5172{
5173 struct sk_buff *skb;
5174 void *hdr;
5175 struct nlattr *data;
5176
5177 skb = nlmsg_new(approxlen + 100, gfp);
5178 if (!skb)
5179 return NULL;
5180
5181 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5182 if (!hdr) {
5183 kfree_skb(skb);
5184 return NULL;
5185 }
5186
5187 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5188 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5189
5190 ((void **)skb->cb)[0] = rdev;
5191 ((void **)skb->cb)[1] = hdr;
5192 ((void **)skb->cb)[2] = data;
5193
5194 return skb;
5195
5196 nla_put_failure:
5197 kfree_skb(skb);
5198 return NULL;
5199}
5200
5201struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5202 int approxlen)
5203{
5204 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5205
5206 if (WARN_ON(!rdev->testmode_info))
5207 return NULL;
5208
5209 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5210 rdev->testmode_info->snd_pid,
5211 rdev->testmode_info->snd_seq,
5212 GFP_KERNEL);
5213}
5214EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5215
5216int cfg80211_testmode_reply(struct sk_buff *skb)
5217{
5218 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5219 void *hdr = ((void **)skb->cb)[1];
5220 struct nlattr *data = ((void **)skb->cb)[2];
5221
5222 if (WARN_ON(!rdev->testmode_info)) {
5223 kfree_skb(skb);
5224 return -EINVAL;
5225 }
5226
5227 nla_nest_end(skb, data);
5228 genlmsg_end(skb, hdr);
5229 return genlmsg_reply(skb, rdev->testmode_info);
5230}
5231EXPORT_SYMBOL(cfg80211_testmode_reply);
5232
5233struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5234 int approxlen, gfp_t gfp)
5235{
5236 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5237
5238 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5239}
5240EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5241
5242void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5243{
5244 void *hdr = ((void **)skb->cb)[1];
5245 struct nlattr *data = ((void **)skb->cb)[2];
5246
5247 nla_nest_end(skb, data);
5248 genlmsg_end(skb, hdr);
5249 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5250}
5251EXPORT_SYMBOL(cfg80211_testmode_event);
5252#endif
5253
Samuel Ortizb23aa672009-07-01 21:26:54 +02005254static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5255{
Johannes Berg4c476992010-10-04 21:36:35 +02005256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5257 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005258 struct cfg80211_connect_params connect;
5259 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005260 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005261 int err;
5262
5263 memset(&connect, 0, sizeof(connect));
5264
5265 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5266 return -EINVAL;
5267
5268 if (!info->attrs[NL80211_ATTR_SSID] ||
5269 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5270 return -EINVAL;
5271
5272 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5273 connect.auth_type =
5274 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5275 if (!nl80211_valid_auth_type(connect.auth_type))
5276 return -EINVAL;
5277 } else
5278 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5279
5280 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5281
Johannes Bergc0692b82010-08-27 14:26:53 +03005282 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005283 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005284 if (err)
5285 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005286
Johannes Berg074ac8d2010-09-16 14:58:22 +02005287 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005288 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5289 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005290
Johannes Berg79c97e92009-07-07 03:56:12 +02005291 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005292
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305293 connect.bg_scan_period = -1;
5294 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5295 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5296 connect.bg_scan_period =
5297 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5298 }
5299
Samuel Ortizb23aa672009-07-01 21:26:54 +02005300 if (info->attrs[NL80211_ATTR_MAC])
5301 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5302 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5303 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5304
5305 if (info->attrs[NL80211_ATTR_IE]) {
5306 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5307 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5308 }
5309
Jouni Malinen7e73ad52013-01-15 15:15:57 +00005310 if (info->attrs[NL80211_ATTR_USE_MFP]) {
5311 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
5312 if (connect.mfp != NL80211_MFP_REQUIRED &&
5313 connect.mfp != NL80211_MFP_NO)
5314 return -EINVAL;
5315 } else {
5316 connect.mfp = NL80211_MFP_NO;
5317 }
5318
Samuel Ortizb23aa672009-07-01 21:26:54 +02005319 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5320 connect.channel =
5321 ieee80211_get_channel(wiphy,
5322 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5323 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005324 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5325 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005326 }
5327
Johannes Bergfffd0932009-07-08 14:22:54 +02005328 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5329 connkeys = nl80211_parse_connkeys(rdev,
5330 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005331 if (IS_ERR(connkeys))
5332 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005333 }
5334
Ben Greear7e7c8922011-11-18 11:31:59 -08005335 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5336 connect.flags |= ASSOC_REQ_DISABLE_HT;
5337
5338 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5339 memcpy(&connect.ht_capa_mask,
5340 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5341 sizeof(connect.ht_capa_mask));
5342
5343 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5344 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5345 return -EINVAL;
5346 memcpy(&connect.ht_capa,
5347 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5348 sizeof(connect.ht_capa));
5349 }
5350
Johannes Bergfffd0932009-07-08 14:22:54 +02005351 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005352 if (err)
5353 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005354 return err;
5355}
5356
5357static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5358{
Johannes Berg4c476992010-10-04 21:36:35 +02005359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5360 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005361 u16 reason;
5362
5363 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5364 reason = WLAN_REASON_DEAUTH_LEAVING;
5365 else
5366 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5367
5368 if (reason == 0)
5369 return -EINVAL;
5370
Johannes Berg074ac8d2010-09-16 14:58:22 +02005371 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005372 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5373 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005374
Johannes Berg4c476992010-10-04 21:36:35 +02005375 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005376}
5377
Johannes Berg463d0182009-07-14 00:33:35 +02005378static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5379{
Johannes Berg4c476992010-10-04 21:36:35 +02005380 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005381 struct net *net;
5382 int err;
5383 u32 pid;
5384
5385 if (!info->attrs[NL80211_ATTR_PID])
5386 return -EINVAL;
5387
5388 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5389
Johannes Berg463d0182009-07-14 00:33:35 +02005390 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005391 if (IS_ERR(net))
5392 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005393
5394 err = 0;
5395
5396 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005397 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5398 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005399
Johannes Berg463d0182009-07-14 00:33:35 +02005400 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005401 return err;
5402}
5403
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005404static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5405{
Johannes Berg4c476992010-10-04 21:36:35 +02005406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005407 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5408 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005409 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005410 struct cfg80211_pmksa pmksa;
5411
5412 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5413
5414 if (!info->attrs[NL80211_ATTR_MAC])
5415 return -EINVAL;
5416
5417 if (!info->attrs[NL80211_ATTR_PMKID])
5418 return -EINVAL;
5419
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005420 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5421 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5422
Johannes Berg074ac8d2010-09-16 14:58:22 +02005423 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005424 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5425 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005426
5427 switch (info->genlhdr->cmd) {
5428 case NL80211_CMD_SET_PMKSA:
5429 rdev_ops = rdev->ops->set_pmksa;
5430 break;
5431 case NL80211_CMD_DEL_PMKSA:
5432 rdev_ops = rdev->ops->del_pmksa;
5433 break;
5434 default:
5435 WARN_ON(1);
5436 break;
5437 }
5438
Johannes Berg4c476992010-10-04 21:36:35 +02005439 if (!rdev_ops)
5440 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005441
Johannes Berg4c476992010-10-04 21:36:35 +02005442 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005443}
5444
5445static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5446{
Johannes Berg4c476992010-10-04 21:36:35 +02005447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5448 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005449
Johannes Berg074ac8d2010-09-16 14:58:22 +02005450 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005451 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5452 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005453
Johannes Berg4c476992010-10-04 21:36:35 +02005454 if (!rdev->ops->flush_pmksa)
5455 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005456
Johannes Berg4c476992010-10-04 21:36:35 +02005457 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005458}
5459
Arik Nemtsov109086c2011-09-28 14:12:50 +03005460static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5461{
5462 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5463 struct net_device *dev = info->user_ptr[1];
5464 u8 action_code, dialog_token;
5465 u16 status_code;
5466 u8 *peer;
5467
5468 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5469 !rdev->ops->tdls_mgmt)
5470 return -EOPNOTSUPP;
5471
5472 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5473 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5474 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5475 !info->attrs[NL80211_ATTR_IE] ||
5476 !info->attrs[NL80211_ATTR_MAC])
5477 return -EINVAL;
5478
5479 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5480 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5481 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5482 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5483
5484 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5485 dialog_token, status_code,
5486 nla_data(info->attrs[NL80211_ATTR_IE]),
5487 nla_len(info->attrs[NL80211_ATTR_IE]));
5488}
5489
5490static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5491{
5492 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5493 struct net_device *dev = info->user_ptr[1];
5494 enum nl80211_tdls_operation operation;
5495 u8 *peer;
5496
5497 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5498 !rdev->ops->tdls_oper)
5499 return -EOPNOTSUPP;
5500
5501 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5502 !info->attrs[NL80211_ATTR_MAC])
5503 return -EINVAL;
5504
5505 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5506 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5507
5508 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5509}
5510
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005511static int nl80211_remain_on_channel(struct sk_buff *skb,
5512 struct genl_info *info)
5513{
Johannes Berg4c476992010-10-04 21:36:35 +02005514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5515 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005516 struct ieee80211_channel *chan;
5517 struct sk_buff *msg;
5518 void *hdr;
5519 u64 cookie;
5520 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5521 u32 freq, duration;
5522 int err;
5523
5524 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5525 !info->attrs[NL80211_ATTR_DURATION])
5526 return -EINVAL;
5527
5528 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5529
5530 /*
5531 * We should be on that channel for at least one jiffie,
5532 * and more than 5 seconds seems excessive.
5533 */
Johannes Berga2939112010-12-14 17:54:28 +01005534 if (!duration || !msecs_to_jiffies(duration) ||
5535 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005536 return -EINVAL;
5537
Johannes Berg7c4ef712011-11-18 15:33:48 +01005538 if (!rdev->ops->remain_on_channel ||
5539 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005540 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005541
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005542 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5543 channel_type = nla_get_u32(
5544 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5545 if (channel_type != NL80211_CHAN_NO_HT &&
5546 channel_type != NL80211_CHAN_HT20 &&
5547 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005548 channel_type != NL80211_CHAN_HT40MINUS)
5549 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005550 }
5551
5552 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5553 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005554 if (chan == NULL)
5555 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005556
5557 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005558 if (!msg)
5559 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005560
5561 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5562 NL80211_CMD_REMAIN_ON_CHANNEL);
5563
5564 if (IS_ERR(hdr)) {
5565 err = PTR_ERR(hdr);
5566 goto free_msg;
5567 }
5568
5569 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5570 channel_type, duration, &cookie);
5571
5572 if (err)
5573 goto free_msg;
5574
5575 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5576
5577 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005578
5579 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005580
5581 nla_put_failure:
5582 err = -ENOBUFS;
5583 free_msg:
5584 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005585 return err;
5586}
5587
5588static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5589 struct genl_info *info)
5590{
Johannes Berg4c476992010-10-04 21:36:35 +02005591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5592 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005593 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005594
5595 if (!info->attrs[NL80211_ATTR_COOKIE])
5596 return -EINVAL;
5597
Johannes Berg4c476992010-10-04 21:36:35 +02005598 if (!rdev->ops->cancel_remain_on_channel)
5599 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005600
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005601 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5602
Johannes Berg4c476992010-10-04 21:36:35 +02005603 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005604}
5605
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005606static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5607 u8 *rates, u8 rates_len)
5608{
5609 u8 i;
5610 u32 mask = 0;
5611
5612 for (i = 0; i < rates_len; i++) {
5613 int rate = (rates[i] & 0x7f) * 5;
5614 int ridx;
5615 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5616 struct ieee80211_rate *srate =
5617 &sband->bitrates[ridx];
5618 if (rate == srate->bitrate) {
5619 mask |= 1 << ridx;
5620 break;
5621 }
5622 }
5623 if (ridx == sband->n_bitrates)
5624 return 0; /* rate not found */
5625 }
5626
5627 return mask;
5628}
5629
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005630static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5631 u8 *rates, u8 rates_len,
5632 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5633{
5634 u8 i;
5635
5636 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5637
5638 for (i = 0; i < rates_len; i++) {
5639 int ridx, rbit;
5640
5641 ridx = rates[i] / 8;
5642 rbit = BIT(rates[i] % 8);
5643
5644 /* check validity */
Dan Carpenter910570b2012-02-01 10:42:11 +03005645 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005646 return false;
5647
5648 /* check availability */
5649 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5650 mcs[ridx] |= rbit;
5651 else
5652 return false;
5653 }
5654
5655 return true;
5656}
5657
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005658static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005659 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5660 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005661 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5662 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005663};
5664
5665static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5666 struct genl_info *info)
5667{
5668 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005669 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005670 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005671 int rem, i;
5672 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005673 struct nlattr *tx_rates;
5674 struct ieee80211_supported_band *sband;
5675
5676 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5677 return -EINVAL;
5678
Johannes Berg4c476992010-10-04 21:36:35 +02005679 if (!rdev->ops->set_bitrate_mask)
5680 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005681
5682 memset(&mask, 0, sizeof(mask));
5683 /* Default to all rates enabled */
5684 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5685 sband = rdev->wiphy.bands[i];
5686 mask.control[i].legacy =
5687 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005688 if (sband)
5689 memcpy(mask.control[i].mcs,
5690 sband->ht_cap.mcs.rx_mask,
5691 sizeof(mask.control[i].mcs));
5692 else
5693 memset(mask.control[i].mcs, 0,
5694 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005695 }
5696
5697 /*
5698 * The nested attribute uses enum nl80211_band as the index. This maps
5699 * directly to the enum ieee80211_band values used in cfg80211.
5700 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005701 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005702 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5703 {
5704 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005705 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5706 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005707 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005708 if (sband == NULL)
5709 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005710 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5711 nla_len(tx_rates), nl80211_txattr_policy);
5712 if (tb[NL80211_TXRATE_LEGACY]) {
5713 mask.control[band].legacy = rateset_to_mask(
5714 sband,
5715 nla_data(tb[NL80211_TXRATE_LEGACY]),
5716 nla_len(tb[NL80211_TXRATE_LEGACY]));
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005717 }
5718 if (tb[NL80211_TXRATE_MCS]) {
5719 if (!ht_rateset_to_mask(
5720 sband,
5721 nla_data(tb[NL80211_TXRATE_MCS]),
5722 nla_len(tb[NL80211_TXRATE_MCS]),
5723 mask.control[band].mcs))
5724 return -EINVAL;
5725 }
5726
5727 if (mask.control[band].legacy == 0) {
5728 /* don't allow empty legacy rates if HT
5729 * is not even supported. */
5730 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5731 return -EINVAL;
5732
5733 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5734 if (mask.control[band].mcs[i])
5735 break;
5736
5737 /* legacy and mcs rates may not be both empty */
5738 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005739 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005740 }
5741 }
5742
Johannes Berg4c476992010-10-04 21:36:35 +02005743 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005744}
5745
Johannes Berg2e161f72010-08-12 15:38:38 +02005746static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005747{
Johannes Berg4c476992010-10-04 21:36:35 +02005748 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5749 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005750 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005751
5752 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5753 return -EINVAL;
5754
Johannes Berg2e161f72010-08-12 15:38:38 +02005755 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5756 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005757
Johannes Berg9d38d852010-06-09 17:20:33 +02005758 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005759 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005760 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5761 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5762 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005764 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5765 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005766
5767 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005768 if (!rdev->ops->mgmt_tx)
5769 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005770
Johannes Berg4c476992010-10-04 21:36:35 +02005771 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005772 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005773 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5774 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005775}
5776
Johannes Berg2e161f72010-08-12 15:38:38 +02005777static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005778{
Johannes Berg4c476992010-10-04 21:36:35 +02005779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5780 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005781 struct ieee80211_channel *chan;
5782 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005783 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005784 u32 freq;
5785 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005786 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005787 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005788 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005789 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005790 bool offchan, no_cck, dont_wait_for_ack;
5791
5792 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005793
5794 if (!info->attrs[NL80211_ATTR_FRAME] ||
5795 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5796 return -EINVAL;
5797
Johannes Berg4c476992010-10-04 21:36:35 +02005798 if (!rdev->ops->mgmt_tx)
5799 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005800
Johannes Berg9d38d852010-06-09 17:20:33 +02005801 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005802 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005803 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5804 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5805 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005806 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005807 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5808 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005809
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005810 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005811 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005812 return -EINVAL;
5813 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5814 }
5815
Jouni Malinen026331c2010-02-15 12:53:10 +02005816 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5817 channel_type = nla_get_u32(
5818 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5819 if (channel_type != NL80211_CHAN_NO_HT &&
5820 channel_type != NL80211_CHAN_HT20 &&
5821 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005822 channel_type != NL80211_CHAN_HT40MINUS)
5823 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005824 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005825 }
5826
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005827 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5828
Johannes Berg7c4ef712011-11-18 15:33:48 +01005829 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5830 return -EINVAL;
5831
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305832 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5833
Jouni Malinen026331c2010-02-15 12:53:10 +02005834 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5835 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005836 if (chan == NULL)
5837 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005838
Johannes Berge247bd902011-11-04 11:18:21 +01005839 if (!dont_wait_for_ack) {
5840 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5841 if (!msg)
5842 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005843
Johannes Berge247bd902011-11-04 11:18:21 +01005844 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5845 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005846
Johannes Berge247bd902011-11-04 11:18:21 +01005847 if (IS_ERR(hdr)) {
5848 err = PTR_ERR(hdr);
5849 goto free_msg;
5850 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005851 }
Johannes Berge247bd902011-11-04 11:18:21 +01005852
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005853 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5854 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005855 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5856 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005857 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005858 if (err)
5859 goto free_msg;
5860
Johannes Berge247bd902011-11-04 11:18:21 +01005861 if (msg) {
5862 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005863
Johannes Berge247bd902011-11-04 11:18:21 +01005864 genlmsg_end(msg, hdr);
5865 return genlmsg_reply(msg, info);
5866 }
5867
5868 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005869
5870 nla_put_failure:
5871 err = -ENOBUFS;
5872 free_msg:
5873 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005874 return err;
5875}
5876
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005877static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5878{
5879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5880 struct net_device *dev = info->user_ptr[1];
5881 u64 cookie;
5882
5883 if (!info->attrs[NL80211_ATTR_COOKIE])
5884 return -EINVAL;
5885
5886 if (!rdev->ops->mgmt_tx_cancel_wait)
5887 return -EOPNOTSUPP;
5888
5889 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5890 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5891 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5892 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5893 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5894 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5895 return -EOPNOTSUPP;
5896
5897 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5898
5899 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5900}
5901
Kalle Valoffb9eb32010-02-17 17:58:10 +02005902static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5903{
Johannes Berg4c476992010-10-04 21:36:35 +02005904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005905 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005906 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005907 u8 ps_state;
5908 bool state;
5909 int err;
5910
Johannes Berg4c476992010-10-04 21:36:35 +02005911 if (!info->attrs[NL80211_ATTR_PS_STATE])
5912 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005913
5914 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5915
Johannes Berg4c476992010-10-04 21:36:35 +02005916 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5917 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005918
5919 wdev = dev->ieee80211_ptr;
5920
Johannes Berg4c476992010-10-04 21:36:35 +02005921 if (!rdev->ops->set_power_mgmt)
5922 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005923
5924 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5925
5926 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005927 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005928
Johannes Berg4c476992010-10-04 21:36:35 +02005929 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5930 wdev->ps_timeout);
5931 if (!err)
5932 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005933 return err;
5934}
5935
5936static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5937{
Johannes Berg4c476992010-10-04 21:36:35 +02005938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005939 enum nl80211_ps_state ps_state;
5940 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005941 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005942 struct sk_buff *msg;
5943 void *hdr;
5944 int err;
5945
Kalle Valoffb9eb32010-02-17 17:58:10 +02005946 wdev = dev->ieee80211_ptr;
5947
Johannes Berg4c476992010-10-04 21:36:35 +02005948 if (!rdev->ops->set_power_mgmt)
5949 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005950
5951 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005952 if (!msg)
5953 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005954
5955 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5956 NL80211_CMD_GET_POWER_SAVE);
5957 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005958 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005959 goto free_msg;
5960 }
5961
5962 if (wdev->ps)
5963 ps_state = NL80211_PS_ENABLED;
5964 else
5965 ps_state = NL80211_PS_DISABLED;
5966
5967 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5968
5969 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005970 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005971
Johannes Berg4c476992010-10-04 21:36:35 +02005972 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005973 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005974 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005975 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005976 return err;
5977}
5978
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005979static struct nla_policy
5980nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5981 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5982 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5983 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5984};
5985
5986static int nl80211_set_cqm_rssi(struct genl_info *info,
5987 s32 threshold, u32 hysteresis)
5988{
Johannes Berg4c476992010-10-04 21:36:35 +02005989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005990 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005991 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005992
5993 if (threshold > 0)
5994 return -EINVAL;
5995
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005996 wdev = dev->ieee80211_ptr;
5997
Johannes Berg4c476992010-10-04 21:36:35 +02005998 if (!rdev->ops->set_cqm_rssi_config)
5999 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006000
Johannes Berg074ac8d2010-09-16 14:58:22 +02006001 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006002 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6003 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006004
Johannes Berg4c476992010-10-04 21:36:35 +02006005 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6006 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006007}
6008
6009static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6010{
6011 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6012 struct nlattr *cqm;
6013 int err;
6014
6015 cqm = info->attrs[NL80211_ATTR_CQM];
6016 if (!cqm) {
6017 err = -EINVAL;
6018 goto out;
6019 }
6020
6021 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6022 nl80211_attr_cqm_policy);
6023 if (err)
6024 goto out;
6025
6026 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6027 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6028 s32 threshold;
6029 u32 hysteresis;
6030 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6031 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6032 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6033 } else
6034 err = -EINVAL;
6035
6036out:
6037 return err;
6038}
6039
Johannes Berg29cbe682010-12-03 09:20:44 +01006040static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6041{
6042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6043 struct net_device *dev = info->user_ptr[1];
6044 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006045 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006046 int err;
6047
6048 /* start with default */
6049 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006050 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006051
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006052 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006053 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006054 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006055 if (err)
6056 return err;
6057 }
6058
6059 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6060 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6061 return -EINVAL;
6062
Javier Cardonac80d5452010-12-16 17:37:49 -08006063 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6064 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6065
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006066 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6067 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6068 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6069 return -EINVAL;
6070
Javier Cardonac80d5452010-12-16 17:37:49 -08006071 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6072 /* parse additional setup parameters if given */
6073 err = nl80211_parse_mesh_setup(info, &setup);
6074 if (err)
6075 return err;
6076 }
6077
6078 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006079}
6080
6081static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6082{
6083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6084 struct net_device *dev = info->user_ptr[1];
6085
6086 return cfg80211_leave_mesh(rdev, dev);
6087}
6088
Johannes Bergff1b6e62011-05-04 15:37:28 +02006089static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6090{
6091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6092 struct sk_buff *msg;
6093 void *hdr;
6094
6095 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6096 return -EOPNOTSUPP;
6097
6098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6099 if (!msg)
6100 return -ENOMEM;
6101
6102 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6103 NL80211_CMD_GET_WOWLAN);
6104 if (!hdr)
6105 goto nla_put_failure;
6106
6107 if (rdev->wowlan) {
6108 struct nlattr *nl_wowlan;
6109
6110 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6111 if (!nl_wowlan)
6112 goto nla_put_failure;
6113
6114 if (rdev->wowlan->any)
6115 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
6116 if (rdev->wowlan->disconnect)
6117 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
6118 if (rdev->wowlan->magic_pkt)
6119 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02006120 if (rdev->wowlan->gtk_rekey_failure)
6121 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
6122 if (rdev->wowlan->eap_identity_req)
6123 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
6124 if (rdev->wowlan->four_way_handshake)
6125 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
6126 if (rdev->wowlan->rfkill_release)
6127 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02006128 if (rdev->wowlan->n_patterns) {
6129 struct nlattr *nl_pats, *nl_pat;
6130 int i, pat_len;
6131
6132 nl_pats = nla_nest_start(msg,
6133 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6134 if (!nl_pats)
6135 goto nla_put_failure;
6136
6137 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6138 nl_pat = nla_nest_start(msg, i + 1);
6139 if (!nl_pat)
6140 goto nla_put_failure;
6141 pat_len = rdev->wowlan->patterns[i].pattern_len;
6142 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
6143 DIV_ROUND_UP(pat_len, 8),
6144 rdev->wowlan->patterns[i].mask);
6145 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6146 pat_len,
6147 rdev->wowlan->patterns[i].pattern);
6148 nla_nest_end(msg, nl_pat);
6149 }
6150 nla_nest_end(msg, nl_pats);
6151 }
6152
6153 nla_nest_end(msg, nl_wowlan);
6154 }
6155
6156 genlmsg_end(msg, hdr);
6157 return genlmsg_reply(msg, info);
6158
6159nla_put_failure:
6160 nlmsg_free(msg);
6161 return -ENOBUFS;
6162}
6163
6164static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6165{
6166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6167 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6168 struct cfg80211_wowlan no_triggers = {};
6169 struct cfg80211_wowlan new_triggers = {};
6170 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6171 int err, i;
6172
6173 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6174 return -EOPNOTSUPP;
6175
6176 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6177 goto no_triggers;
6178
6179 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6180 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6181 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6182 nl80211_wowlan_policy);
6183 if (err)
6184 return err;
6185
6186 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6187 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6188 return -EINVAL;
6189 new_triggers.any = true;
6190 }
6191
6192 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6193 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6194 return -EINVAL;
6195 new_triggers.disconnect = true;
6196 }
6197
6198 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6199 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6200 return -EINVAL;
6201 new_triggers.magic_pkt = true;
6202 }
6203
Johannes Berg77dbbb12011-07-13 10:48:55 +02006204 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6205 return -EINVAL;
6206
6207 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6208 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6209 return -EINVAL;
6210 new_triggers.gtk_rekey_failure = true;
6211 }
6212
6213 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6214 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6215 return -EINVAL;
6216 new_triggers.eap_identity_req = true;
6217 }
6218
6219 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6220 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6221 return -EINVAL;
6222 new_triggers.four_way_handshake = true;
6223 }
6224
6225 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6226 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6227 return -EINVAL;
6228 new_triggers.rfkill_release = true;
6229 }
6230
Johannes Bergff1b6e62011-05-04 15:37:28 +02006231 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6232 struct nlattr *pat;
6233 int n_patterns = 0;
6234 int rem, pat_len, mask_len;
6235 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6236
6237 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6238 rem)
6239 n_patterns++;
6240 if (n_patterns > wowlan->n_patterns)
6241 return -EINVAL;
6242
6243 new_triggers.patterns = kcalloc(n_patterns,
6244 sizeof(new_triggers.patterns[0]),
6245 GFP_KERNEL);
6246 if (!new_triggers.patterns)
6247 return -ENOMEM;
6248
6249 new_triggers.n_patterns = n_patterns;
6250 i = 0;
6251
6252 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6253 rem) {
6254 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6255 nla_data(pat), nla_len(pat), NULL);
6256 err = -EINVAL;
6257 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6258 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6259 goto error;
6260 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6261 mask_len = DIV_ROUND_UP(pat_len, 8);
6262 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6263 mask_len)
6264 goto error;
6265 if (pat_len > wowlan->pattern_max_len ||
6266 pat_len < wowlan->pattern_min_len)
6267 goto error;
6268
6269 new_triggers.patterns[i].mask =
6270 kmalloc(mask_len + pat_len, GFP_KERNEL);
6271 if (!new_triggers.patterns[i].mask) {
6272 err = -ENOMEM;
6273 goto error;
6274 }
6275 new_triggers.patterns[i].pattern =
6276 new_triggers.patterns[i].mask + mask_len;
6277 memcpy(new_triggers.patterns[i].mask,
6278 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6279 mask_len);
6280 new_triggers.patterns[i].pattern_len = pat_len;
6281 memcpy(new_triggers.patterns[i].pattern,
6282 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6283 pat_len);
6284 i++;
6285 }
6286 }
6287
6288 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6289 struct cfg80211_wowlan *ntrig;
6290 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6291 GFP_KERNEL);
6292 if (!ntrig) {
6293 err = -ENOMEM;
6294 goto error;
6295 }
6296 cfg80211_rdev_free_wowlan(rdev);
6297 rdev->wowlan = ntrig;
6298 } else {
6299 no_triggers:
6300 cfg80211_rdev_free_wowlan(rdev);
6301 rdev->wowlan = NULL;
6302 }
6303
6304 return 0;
6305 error:
6306 for (i = 0; i < new_triggers.n_patterns; i++)
6307 kfree(new_triggers.patterns[i].mask);
6308 kfree(new_triggers.patterns);
6309 return err;
6310}
6311
Johannes Berge5497d72011-07-05 16:35:40 +02006312static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6313{
6314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6315 struct net_device *dev = info->user_ptr[1];
6316 struct wireless_dev *wdev = dev->ieee80211_ptr;
6317 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6318 struct cfg80211_gtk_rekey_data rekey_data;
6319 int err;
6320
6321 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6322 return -EINVAL;
6323
6324 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6325 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6326 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6327 nl80211_rekey_policy);
6328 if (err)
6329 return err;
6330
6331 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6332 return -ERANGE;
6333 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6334 return -ERANGE;
6335 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6336 return -ERANGE;
6337
6338 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6339 NL80211_KEK_LEN);
6340 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6341 NL80211_KCK_LEN);
6342 memcpy(rekey_data.replay_ctr,
6343 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6344 NL80211_REPLAY_CTR_LEN);
6345
6346 wdev_lock(wdev);
6347 if (!wdev->current_bss) {
6348 err = -ENOTCONN;
6349 goto out;
6350 }
6351
6352 if (!rdev->ops->set_rekey_data) {
6353 err = -EOPNOTSUPP;
6354 goto out;
6355 }
6356
6357 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6358 out:
6359 wdev_unlock(wdev);
6360 return err;
6361}
6362
Johannes Berg28946da2011-11-04 11:18:12 +01006363static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6364 struct genl_info *info)
6365{
6366 struct net_device *dev = info->user_ptr[1];
6367 struct wireless_dev *wdev = dev->ieee80211_ptr;
6368
6369 if (wdev->iftype != NL80211_IFTYPE_AP &&
6370 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6371 return -EINVAL;
6372
6373 if (wdev->ap_unexpected_nlpid)
6374 return -EBUSY;
6375
6376 wdev->ap_unexpected_nlpid = info->snd_pid;
6377 return 0;
6378}
6379
Johannes Berg7f6cf312011-11-04 11:18:15 +01006380static int nl80211_probe_client(struct sk_buff *skb,
6381 struct genl_info *info)
6382{
6383 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6384 struct net_device *dev = info->user_ptr[1];
6385 struct wireless_dev *wdev = dev->ieee80211_ptr;
6386 struct sk_buff *msg;
6387 void *hdr;
6388 const u8 *addr;
6389 u64 cookie;
6390 int err;
6391
6392 if (wdev->iftype != NL80211_IFTYPE_AP &&
6393 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6394 return -EOPNOTSUPP;
6395
6396 if (!info->attrs[NL80211_ATTR_MAC])
6397 return -EINVAL;
6398
6399 if (!rdev->ops->probe_client)
6400 return -EOPNOTSUPP;
6401
6402 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6403 if (!msg)
6404 return -ENOMEM;
6405
6406 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6407 NL80211_CMD_PROBE_CLIENT);
6408
6409 if (IS_ERR(hdr)) {
6410 err = PTR_ERR(hdr);
6411 goto free_msg;
6412 }
6413
6414 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6415
6416 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6417 if (err)
6418 goto free_msg;
6419
6420 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6421
6422 genlmsg_end(msg, hdr);
6423
6424 return genlmsg_reply(msg, info);
6425
6426 nla_put_failure:
6427 err = -ENOBUFS;
6428 free_msg:
6429 nlmsg_free(msg);
6430 return err;
6431}
6432
Johannes Berg5e760232011-11-04 11:18:17 +01006433static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6434{
6435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6436
6437 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6438 return -EOPNOTSUPP;
6439
6440 if (rdev->ap_beacons_nlpid)
6441 return -EBUSY;
6442
6443 rdev->ap_beacons_nlpid = info->snd_pid;
6444
6445 return 0;
6446}
6447
Johannes Berg4c476992010-10-04 21:36:35 +02006448#define NL80211_FLAG_NEED_WIPHY 0x01
6449#define NL80211_FLAG_NEED_NETDEV 0x02
6450#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006451#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6452#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6453 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006454
6455static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6456 struct genl_info *info)
6457{
6458 struct cfg80211_registered_device *rdev;
6459 struct net_device *dev;
6460 int err;
6461 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6462
6463 if (rtnl)
6464 rtnl_lock();
6465
6466 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6467 rdev = cfg80211_get_dev_from_info(info);
6468 if (IS_ERR(rdev)) {
6469 if (rtnl)
6470 rtnl_unlock();
6471 return PTR_ERR(rdev);
6472 }
6473 info->user_ptr[0] = rdev;
6474 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
Johannes Berg00918d32011-12-13 17:22:05 +01006475 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6476 &rdev, &dev);
Johannes Berg4c476992010-10-04 21:36:35 +02006477 if (err) {
6478 if (rtnl)
6479 rtnl_unlock();
6480 return err;
6481 }
Johannes Berg41265712010-10-04 21:14:05 +02006482 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6483 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006484 cfg80211_unlock_rdev(rdev);
6485 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006486 if (rtnl)
6487 rtnl_unlock();
6488 return -ENETDOWN;
6489 }
Johannes Berg4c476992010-10-04 21:36:35 +02006490 info->user_ptr[0] = rdev;
6491 info->user_ptr[1] = dev;
6492 }
6493
6494 return 0;
6495}
6496
6497static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6498 struct genl_info *info)
6499{
6500 if (info->user_ptr[0])
6501 cfg80211_unlock_rdev(info->user_ptr[0]);
6502 if (info->user_ptr[1])
6503 dev_put(info->user_ptr[1]);
6504 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6505 rtnl_unlock();
6506}
6507
Johannes Berg55682962007-09-20 13:09:35 -04006508static struct genl_ops nl80211_ops[] = {
6509 {
6510 .cmd = NL80211_CMD_GET_WIPHY,
6511 .doit = nl80211_get_wiphy,
6512 .dumpit = nl80211_dump_wiphy,
6513 .policy = nl80211_policy,
6514 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006515 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006516 },
6517 {
6518 .cmd = NL80211_CMD_SET_WIPHY,
6519 .doit = nl80211_set_wiphy,
6520 .policy = nl80211_policy,
6521 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006522 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006523 },
6524 {
6525 .cmd = NL80211_CMD_GET_INTERFACE,
6526 .doit = nl80211_get_interface,
6527 .dumpit = nl80211_dump_interface,
6528 .policy = nl80211_policy,
6529 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006530 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006531 },
6532 {
6533 .cmd = NL80211_CMD_SET_INTERFACE,
6534 .doit = nl80211_set_interface,
6535 .policy = nl80211_policy,
6536 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006537 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6538 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006539 },
6540 {
6541 .cmd = NL80211_CMD_NEW_INTERFACE,
6542 .doit = nl80211_new_interface,
6543 .policy = nl80211_policy,
6544 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006545 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6546 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006547 },
6548 {
6549 .cmd = NL80211_CMD_DEL_INTERFACE,
6550 .doit = nl80211_del_interface,
6551 .policy = nl80211_policy,
6552 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006553 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6554 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006555 },
Johannes Berg41ade002007-12-19 02:03:29 +01006556 {
6557 .cmd = NL80211_CMD_GET_KEY,
6558 .doit = nl80211_get_key,
6559 .policy = nl80211_policy,
6560 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006562 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006563 },
6564 {
6565 .cmd = NL80211_CMD_SET_KEY,
6566 .doit = nl80211_set_key,
6567 .policy = nl80211_policy,
6568 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006569 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006570 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006571 },
6572 {
6573 .cmd = NL80211_CMD_NEW_KEY,
6574 .doit = nl80211_new_key,
6575 .policy = nl80211_policy,
6576 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006577 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006578 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006579 },
6580 {
6581 .cmd = NL80211_CMD_DEL_KEY,
6582 .doit = nl80211_del_key,
6583 .policy = nl80211_policy,
6584 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006585 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006586 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006587 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006588 {
6589 .cmd = NL80211_CMD_SET_BEACON,
6590 .policy = nl80211_policy,
6591 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006592 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006593 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006594 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006595 },
6596 {
Johannes Berg88600202012-02-13 15:17:18 +01006597 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006598 .policy = nl80211_policy,
6599 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006600 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006601 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006602 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006603 },
6604 {
Johannes Berg88600202012-02-13 15:17:18 +01006605 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006606 .policy = nl80211_policy,
6607 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006608 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006609 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006610 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006611 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006612 {
6613 .cmd = NL80211_CMD_GET_STATION,
6614 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006615 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006616 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006617 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6618 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006619 },
6620 {
6621 .cmd = NL80211_CMD_SET_STATION,
6622 .doit = nl80211_set_station,
6623 .policy = nl80211_policy,
6624 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006626 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006627 },
6628 {
6629 .cmd = NL80211_CMD_NEW_STATION,
6630 .doit = nl80211_new_station,
6631 .policy = nl80211_policy,
6632 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006634 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006635 },
6636 {
6637 .cmd = NL80211_CMD_DEL_STATION,
6638 .doit = nl80211_del_station,
6639 .policy = nl80211_policy,
6640 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006641 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006642 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006643 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006644 {
6645 .cmd = NL80211_CMD_GET_MPATH,
6646 .doit = nl80211_get_mpath,
6647 .dumpit = nl80211_dump_mpath,
6648 .policy = nl80211_policy,
6649 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006650 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006651 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006652 },
6653 {
6654 .cmd = NL80211_CMD_SET_MPATH,
6655 .doit = nl80211_set_mpath,
6656 .policy = nl80211_policy,
6657 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006658 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006659 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006660 },
6661 {
6662 .cmd = NL80211_CMD_NEW_MPATH,
6663 .doit = nl80211_new_mpath,
6664 .policy = nl80211_policy,
6665 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006666 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006667 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006668 },
6669 {
6670 .cmd = NL80211_CMD_DEL_MPATH,
6671 .doit = nl80211_del_mpath,
6672 .policy = nl80211_policy,
6673 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006674 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006675 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006676 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006677 {
6678 .cmd = NL80211_CMD_SET_BSS,
6679 .doit = nl80211_set_bss,
6680 .policy = nl80211_policy,
6681 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006682 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006683 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006684 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006685 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006686 .cmd = NL80211_CMD_GET_REG,
6687 .doit = nl80211_get_reg,
6688 .policy = nl80211_policy,
6689 /* can be retrieved by unprivileged users */
6690 },
6691 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006692 .cmd = NL80211_CMD_SET_REG,
6693 .doit = nl80211_set_reg,
6694 .policy = nl80211_policy,
6695 .flags = GENL_ADMIN_PERM,
6696 },
6697 {
6698 .cmd = NL80211_CMD_REQ_SET_REG,
6699 .doit = nl80211_req_set_reg,
6700 .policy = nl80211_policy,
6701 .flags = GENL_ADMIN_PERM,
6702 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006703 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006704 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6705 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006706 .policy = nl80211_policy,
6707 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006708 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006709 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006710 },
6711 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006712 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6713 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006714 .policy = nl80211_policy,
6715 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006716 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006717 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006718 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006719 {
Johannes Berg2a519312009-02-10 21:25:55 +01006720 .cmd = NL80211_CMD_TRIGGER_SCAN,
6721 .doit = nl80211_trigger_scan,
6722 .policy = nl80211_policy,
6723 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006724 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006725 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006726 },
6727 {
6728 .cmd = NL80211_CMD_GET_SCAN,
6729 .policy = nl80211_policy,
6730 .dumpit = nl80211_dump_scan,
6731 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006732 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006733 .cmd = NL80211_CMD_START_SCHED_SCAN,
6734 .doit = nl80211_start_sched_scan,
6735 .policy = nl80211_policy,
6736 .flags = GENL_ADMIN_PERM,
6737 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6738 NL80211_FLAG_NEED_RTNL,
6739 },
6740 {
6741 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6742 .doit = nl80211_stop_sched_scan,
6743 .policy = nl80211_policy,
6744 .flags = GENL_ADMIN_PERM,
6745 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6746 NL80211_FLAG_NEED_RTNL,
6747 },
6748 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006749 .cmd = NL80211_CMD_AUTHENTICATE,
6750 .doit = nl80211_authenticate,
6751 .policy = nl80211_policy,
6752 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006753 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006754 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006755 },
6756 {
6757 .cmd = NL80211_CMD_ASSOCIATE,
6758 .doit = nl80211_associate,
6759 .policy = nl80211_policy,
6760 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006761 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006762 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006763 },
6764 {
6765 .cmd = NL80211_CMD_DEAUTHENTICATE,
6766 .doit = nl80211_deauthenticate,
6767 .policy = nl80211_policy,
6768 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006769 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006770 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006771 },
6772 {
6773 .cmd = NL80211_CMD_DISASSOCIATE,
6774 .doit = nl80211_disassociate,
6775 .policy = nl80211_policy,
6776 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006777 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006778 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006779 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006780 {
6781 .cmd = NL80211_CMD_JOIN_IBSS,
6782 .doit = nl80211_join_ibss,
6783 .policy = nl80211_policy,
6784 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006785 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006786 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006787 },
6788 {
6789 .cmd = NL80211_CMD_LEAVE_IBSS,
6790 .doit = nl80211_leave_ibss,
6791 .policy = nl80211_policy,
6792 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006793 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006794 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006795 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006796#ifdef CONFIG_NL80211_TESTMODE
6797 {
6798 .cmd = NL80211_CMD_TESTMODE,
6799 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006800 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006801 .policy = nl80211_policy,
6802 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006803 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6804 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006805 },
6806#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006807 {
6808 .cmd = NL80211_CMD_CONNECT,
6809 .doit = nl80211_connect,
6810 .policy = nl80211_policy,
6811 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006812 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006813 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006814 },
6815 {
6816 .cmd = NL80211_CMD_DISCONNECT,
6817 .doit = nl80211_disconnect,
6818 .policy = nl80211_policy,
6819 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006820 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006821 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006822 },
Johannes Berg463d0182009-07-14 00:33:35 +02006823 {
6824 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6825 .doit = nl80211_wiphy_netns,
6826 .policy = nl80211_policy,
6827 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006828 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6829 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006830 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006831 {
6832 .cmd = NL80211_CMD_GET_SURVEY,
6833 .policy = nl80211_policy,
6834 .dumpit = nl80211_dump_survey,
6835 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006836 {
6837 .cmd = NL80211_CMD_SET_PMKSA,
6838 .doit = nl80211_setdel_pmksa,
6839 .policy = nl80211_policy,
6840 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006841 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006842 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006843 },
6844 {
6845 .cmd = NL80211_CMD_DEL_PMKSA,
6846 .doit = nl80211_setdel_pmksa,
6847 .policy = nl80211_policy,
6848 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006849 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006850 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006851 },
6852 {
6853 .cmd = NL80211_CMD_FLUSH_PMKSA,
6854 .doit = nl80211_flush_pmksa,
6855 .policy = nl80211_policy,
6856 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006857 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006858 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006859 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006860 {
6861 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6862 .doit = nl80211_remain_on_channel,
6863 .policy = nl80211_policy,
6864 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006866 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006867 },
6868 {
6869 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6870 .doit = nl80211_cancel_remain_on_channel,
6871 .policy = nl80211_policy,
6872 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006873 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006874 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006875 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006876 {
6877 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6878 .doit = nl80211_set_tx_bitrate_mask,
6879 .policy = nl80211_policy,
6880 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006881 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6882 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006883 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006884 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006885 .cmd = NL80211_CMD_REGISTER_FRAME,
6886 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006887 .policy = nl80211_policy,
6888 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6890 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006891 },
6892 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006893 .cmd = NL80211_CMD_FRAME,
6894 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006895 .policy = nl80211_policy,
6896 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006897 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006898 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006899 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006900 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006901 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6902 .doit = nl80211_tx_mgmt_cancel_wait,
6903 .policy = nl80211_policy,
6904 .flags = GENL_ADMIN_PERM,
6905 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6906 NL80211_FLAG_NEED_RTNL,
6907 },
6908 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006909 .cmd = NL80211_CMD_SET_POWER_SAVE,
6910 .doit = nl80211_set_power_save,
6911 .policy = nl80211_policy,
6912 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006913 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6914 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006915 },
6916 {
6917 .cmd = NL80211_CMD_GET_POWER_SAVE,
6918 .doit = nl80211_get_power_save,
6919 .policy = nl80211_policy,
6920 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006921 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6922 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006923 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006924 {
6925 .cmd = NL80211_CMD_SET_CQM,
6926 .doit = nl80211_set_cqm,
6927 .policy = nl80211_policy,
6928 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006929 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6930 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006931 },
Johannes Bergf444de02010-05-05 15:25:02 +02006932 {
6933 .cmd = NL80211_CMD_SET_CHANNEL,
6934 .doit = nl80211_set_channel,
6935 .policy = nl80211_policy,
6936 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006937 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6938 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006939 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006940 {
6941 .cmd = NL80211_CMD_SET_WDS_PEER,
6942 .doit = nl80211_set_wds_peer,
6943 .policy = nl80211_policy,
6944 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006945 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6946 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006947 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006948 {
6949 .cmd = NL80211_CMD_JOIN_MESH,
6950 .doit = nl80211_join_mesh,
6951 .policy = nl80211_policy,
6952 .flags = GENL_ADMIN_PERM,
6953 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6954 NL80211_FLAG_NEED_RTNL,
6955 },
6956 {
6957 .cmd = NL80211_CMD_LEAVE_MESH,
6958 .doit = nl80211_leave_mesh,
6959 .policy = nl80211_policy,
6960 .flags = GENL_ADMIN_PERM,
6961 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6962 NL80211_FLAG_NEED_RTNL,
6963 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006964 {
6965 .cmd = NL80211_CMD_GET_WOWLAN,
6966 .doit = nl80211_get_wowlan,
6967 .policy = nl80211_policy,
6968 /* can be retrieved by unprivileged users */
6969 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6970 NL80211_FLAG_NEED_RTNL,
6971 },
6972 {
6973 .cmd = NL80211_CMD_SET_WOWLAN,
6974 .doit = nl80211_set_wowlan,
6975 .policy = nl80211_policy,
6976 .flags = GENL_ADMIN_PERM,
6977 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6978 NL80211_FLAG_NEED_RTNL,
6979 },
Johannes Berge5497d72011-07-05 16:35:40 +02006980 {
6981 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6982 .doit = nl80211_set_rekey_data,
6983 .policy = nl80211_policy,
6984 .flags = GENL_ADMIN_PERM,
6985 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6986 NL80211_FLAG_NEED_RTNL,
6987 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006988 {
6989 .cmd = NL80211_CMD_TDLS_MGMT,
6990 .doit = nl80211_tdls_mgmt,
6991 .policy = nl80211_policy,
6992 .flags = GENL_ADMIN_PERM,
6993 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6994 NL80211_FLAG_NEED_RTNL,
6995 },
6996 {
6997 .cmd = NL80211_CMD_TDLS_OPER,
6998 .doit = nl80211_tdls_oper,
6999 .policy = nl80211_policy,
7000 .flags = GENL_ADMIN_PERM,
7001 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7002 NL80211_FLAG_NEED_RTNL,
7003 },
Johannes Berg28946da2011-11-04 11:18:12 +01007004 {
7005 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7006 .doit = nl80211_register_unexpected_frame,
7007 .policy = nl80211_policy,
7008 .flags = GENL_ADMIN_PERM,
7009 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7010 NL80211_FLAG_NEED_RTNL,
7011 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007012 {
7013 .cmd = NL80211_CMD_PROBE_CLIENT,
7014 .doit = nl80211_probe_client,
7015 .policy = nl80211_policy,
7016 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007017 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007018 NL80211_FLAG_NEED_RTNL,
7019 },
Johannes Berg5e760232011-11-04 11:18:17 +01007020 {
7021 .cmd = NL80211_CMD_REGISTER_BEACONS,
7022 .doit = nl80211_register_beacons,
7023 .policy = nl80211_policy,
7024 .flags = GENL_ADMIN_PERM,
7025 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7026 NL80211_FLAG_NEED_RTNL,
7027 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007028 {
7029 .cmd = NL80211_CMD_SET_NOACK_MAP,
7030 .doit = nl80211_set_noack_map,
7031 .policy = nl80211_policy,
7032 .flags = GENL_ADMIN_PERM,
7033 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7034 NL80211_FLAG_NEED_RTNL,
7035 },
7036
Johannes Berg55682962007-09-20 13:09:35 -04007037};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007038
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007039static struct genl_multicast_group nl80211_mlme_mcgrp = {
7040 .name = "mlme",
7041};
Johannes Berg55682962007-09-20 13:09:35 -04007042
7043/* multicast groups */
7044static struct genl_multicast_group nl80211_config_mcgrp = {
7045 .name = "config",
7046};
Johannes Berg2a519312009-02-10 21:25:55 +01007047static struct genl_multicast_group nl80211_scan_mcgrp = {
7048 .name = "scan",
7049};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007050static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7051 .name = "regulatory",
7052};
Johannes Berg55682962007-09-20 13:09:35 -04007053
7054/* notification functions */
7055
7056void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7057{
7058 struct sk_buff *msg;
7059
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007060 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007061 if (!msg)
7062 return;
7063
7064 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7065 nlmsg_free(msg);
7066 return;
7067 }
7068
Johannes Berg463d0182009-07-14 00:33:35 +02007069 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7070 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007071}
7072
Johannes Berg362a4152009-05-24 16:43:15 +02007073static int nl80211_add_scan_req(struct sk_buff *msg,
7074 struct cfg80211_registered_device *rdev)
7075{
7076 struct cfg80211_scan_request *req = rdev->scan_req;
7077 struct nlattr *nest;
7078 int i;
7079
Johannes Berg667503d2009-07-07 03:56:11 +02007080 ASSERT_RDEV_LOCK(rdev);
7081
Johannes Berg362a4152009-05-24 16:43:15 +02007082 if (WARN_ON(!req))
7083 return 0;
7084
7085 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7086 if (!nest)
7087 goto nla_put_failure;
7088 for (i = 0; i < req->n_ssids; i++)
7089 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
7090 nla_nest_end(msg, nest);
7091
7092 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7093 if (!nest)
7094 goto nla_put_failure;
7095 for (i = 0; i < req->n_channels; i++)
7096 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
7097 nla_nest_end(msg, nest);
7098
7099 if (req->ie)
7100 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
7101
7102 return 0;
7103 nla_put_failure:
7104 return -ENOBUFS;
7105}
7106
Johannes Berga538e2d2009-06-16 19:56:42 +02007107static int nl80211_send_scan_msg(struct sk_buff *msg,
7108 struct cfg80211_registered_device *rdev,
7109 struct net_device *netdev,
7110 u32 pid, u32 seq, int flags,
7111 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007112{
7113 void *hdr;
7114
7115 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7116 if (!hdr)
7117 return -1;
7118
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05007119 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01007120 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7121
Johannes Berg362a4152009-05-24 16:43:15 +02007122 /* ignore errors and send incomplete event anyway */
7123 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007124
7125 return genlmsg_end(msg, hdr);
7126
7127 nla_put_failure:
7128 genlmsg_cancel(msg, hdr);
7129 return -EMSGSIZE;
7130}
7131
Luciano Coelho807f8a82011-05-11 17:09:35 +03007132static int
7133nl80211_send_sched_scan_msg(struct sk_buff *msg,
7134 struct cfg80211_registered_device *rdev,
7135 struct net_device *netdev,
7136 u32 pid, u32 seq, int flags, u32 cmd)
7137{
7138 void *hdr;
7139
7140 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7141 if (!hdr)
7142 return -1;
7143
7144 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7145 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7146
7147 return genlmsg_end(msg, hdr);
7148
7149 nla_put_failure:
7150 genlmsg_cancel(msg, hdr);
7151 return -EMSGSIZE;
7152}
7153
Johannes Berga538e2d2009-06-16 19:56:42 +02007154void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7155 struct net_device *netdev)
7156{
7157 struct sk_buff *msg;
7158
7159 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7160 if (!msg)
7161 return;
7162
7163 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7164 NL80211_CMD_TRIGGER_SCAN) < 0) {
7165 nlmsg_free(msg);
7166 return;
7167 }
7168
Johannes Berg463d0182009-07-14 00:33:35 +02007169 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7170 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007171}
7172
Johannes Berg2a519312009-02-10 21:25:55 +01007173void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7174 struct net_device *netdev)
7175{
7176 struct sk_buff *msg;
7177
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007179 if (!msg)
7180 return;
7181
Johannes Berga538e2d2009-06-16 19:56:42 +02007182 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7183 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007184 nlmsg_free(msg);
7185 return;
7186 }
7187
Johannes Berg463d0182009-07-14 00:33:35 +02007188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7189 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007190}
7191
7192void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7193 struct net_device *netdev)
7194{
7195 struct sk_buff *msg;
7196
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007198 if (!msg)
7199 return;
7200
Johannes Berga538e2d2009-06-16 19:56:42 +02007201 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7202 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007203 nlmsg_free(msg);
7204 return;
7205 }
7206
Johannes Berg463d0182009-07-14 00:33:35 +02007207 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7208 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007209}
7210
Luciano Coelho807f8a82011-05-11 17:09:35 +03007211void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7212 struct net_device *netdev)
7213{
7214 struct sk_buff *msg;
7215
7216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7217 if (!msg)
7218 return;
7219
7220 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7221 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7222 nlmsg_free(msg);
7223 return;
7224 }
7225
7226 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7227 nl80211_scan_mcgrp.id, GFP_KERNEL);
7228}
7229
7230void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7231 struct net_device *netdev, u32 cmd)
7232{
7233 struct sk_buff *msg;
7234
7235 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7236 if (!msg)
7237 return;
7238
7239 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7240 nlmsg_free(msg);
7241 return;
7242 }
7243
7244 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7245 nl80211_scan_mcgrp.id, GFP_KERNEL);
7246}
7247
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007248/*
7249 * This can happen on global regulatory changes or device specific settings
7250 * based on custom world regulatory domains.
7251 */
7252void nl80211_send_reg_change_event(struct regulatory_request *request)
7253{
7254 struct sk_buff *msg;
7255 void *hdr;
7256
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007258 if (!msg)
7259 return;
7260
7261 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7262 if (!hdr) {
7263 nlmsg_free(msg);
7264 return;
7265 }
7266
7267 /* Userspace can always count this one always being set */
7268 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
7269
7270 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
7271 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7272 NL80211_REGDOM_TYPE_WORLD);
7273 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
7274 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7275 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
7276 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7277 request->intersect)
7278 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7279 NL80211_REGDOM_TYPE_INTERSECTION);
7280 else {
7281 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
7282 NL80211_REGDOM_TYPE_COUNTRY);
7283 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
7284 }
7285
7286 if (wiphy_idx_valid(request->wiphy_idx))
7287 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
7288
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007289 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007290
Johannes Bergbc43b282009-07-25 10:54:13 +02007291 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007292 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007293 GFP_ATOMIC);
7294 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007295
7296 return;
7297
7298nla_put_failure:
7299 genlmsg_cancel(msg, hdr);
7300 nlmsg_free(msg);
7301}
7302
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007303static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7304 struct net_device *netdev,
7305 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007306 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007307{
7308 struct sk_buff *msg;
7309 void *hdr;
7310
Johannes Berge6d6e342009-07-01 21:26:47 +02007311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007312 if (!msg)
7313 return;
7314
7315 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7316 if (!hdr) {
7317 nlmsg_free(msg);
7318 return;
7319 }
7320
7321 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7322 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7323 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7324
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007325 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007326
Johannes Berg463d0182009-07-14 00:33:35 +02007327 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7328 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007329 return;
7330
7331 nla_put_failure:
7332 genlmsg_cancel(msg, hdr);
7333 nlmsg_free(msg);
7334}
7335
7336void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007337 struct net_device *netdev, const u8 *buf,
7338 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007339{
7340 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007341 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007342}
7343
7344void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7345 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007346 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007347{
Johannes Berge6d6e342009-07-01 21:26:47 +02007348 nl80211_send_mlme_event(rdev, netdev, buf, len,
7349 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007350}
7351
Jouni Malinen53b46b82009-03-27 20:53:56 +02007352void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007353 struct net_device *netdev, const u8 *buf,
7354 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007355{
7356 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007357 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007358}
7359
Jouni Malinen53b46b82009-03-27 20:53:56 +02007360void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7361 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007362 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007363{
7364 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007365 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007366}
7367
Jouni Malinencf4e5942010-12-16 00:52:40 +02007368void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7369 struct net_device *netdev, const u8 *buf,
7370 size_t len, gfp_t gfp)
7371{
7372 nl80211_send_mlme_event(rdev, netdev, buf, len,
7373 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7374}
7375
7376void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7377 struct net_device *netdev, const u8 *buf,
7378 size_t len, gfp_t gfp)
7379{
7380 nl80211_send_mlme_event(rdev, netdev, buf, len,
7381 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7382}
7383
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007384static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7385 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007386 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007387{
7388 struct sk_buff *msg;
7389 void *hdr;
7390
Johannes Berge6d6e342009-07-01 21:26:47 +02007391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007392 if (!msg)
7393 return;
7394
7395 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7396 if (!hdr) {
7397 nlmsg_free(msg);
7398 return;
7399 }
7400
7401 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7403 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7404 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7405
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007406 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007407
Johannes Berg463d0182009-07-14 00:33:35 +02007408 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7409 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007410 return;
7411
7412 nla_put_failure:
7413 genlmsg_cancel(msg, hdr);
7414 nlmsg_free(msg);
7415}
7416
7417void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007418 struct net_device *netdev, const u8 *addr,
7419 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007420{
7421 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007422 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007423}
7424
7425void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007426 struct net_device *netdev, const u8 *addr,
7427 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007428{
Johannes Berge6d6e342009-07-01 21:26:47 +02007429 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7430 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007431}
7432
Samuel Ortizb23aa672009-07-01 21:26:54 +02007433void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7434 struct net_device *netdev, const u8 *bssid,
7435 const u8 *req_ie, size_t req_ie_len,
7436 const u8 *resp_ie, size_t resp_ie_len,
7437 u16 status, gfp_t gfp)
7438{
7439 struct sk_buff *msg;
7440 void *hdr;
7441
7442 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7443 if (!msg)
7444 return;
7445
7446 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7447 if (!hdr) {
7448 nlmsg_free(msg);
7449 return;
7450 }
7451
7452 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7453 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7454 if (bssid)
7455 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7456 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7457 if (req_ie)
7458 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7459 if (resp_ie)
7460 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7461
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007462 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007463
Johannes Berg463d0182009-07-14 00:33:35 +02007464 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7465 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007466 return;
7467
7468 nla_put_failure:
7469 genlmsg_cancel(msg, hdr);
7470 nlmsg_free(msg);
7471
7472}
7473
7474void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7475 struct net_device *netdev, const u8 *bssid,
7476 const u8 *req_ie, size_t req_ie_len,
7477 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7478{
7479 struct sk_buff *msg;
7480 void *hdr;
7481
7482 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7483 if (!msg)
7484 return;
7485
7486 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7487 if (!hdr) {
7488 nlmsg_free(msg);
7489 return;
7490 }
7491
7492 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7493 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7494 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7495 if (req_ie)
7496 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7497 if (resp_ie)
7498 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7499
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007500 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007501
Johannes Berg463d0182009-07-14 00:33:35 +02007502 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7503 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007504 return;
7505
7506 nla_put_failure:
7507 genlmsg_cancel(msg, hdr);
7508 nlmsg_free(msg);
7509
7510}
7511
7512void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7513 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007514 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007515{
7516 struct sk_buff *msg;
7517 void *hdr;
7518
Johannes Berg667503d2009-07-07 03:56:11 +02007519 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007520 if (!msg)
7521 return;
7522
7523 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7524 if (!hdr) {
7525 nlmsg_free(msg);
7526 return;
7527 }
7528
7529 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7530 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7531 if (from_ap && reason)
7532 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7533 if (from_ap)
7534 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7535 if (ie)
7536 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7537
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007538 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007539
Johannes Berg463d0182009-07-14 00:33:35 +02007540 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7541 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007542 return;
7543
7544 nla_put_failure:
7545 genlmsg_cancel(msg, hdr);
7546 nlmsg_free(msg);
7547
7548}
7549
Johannes Berg04a773a2009-04-19 21:24:32 +02007550void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7551 struct net_device *netdev, const u8 *bssid,
7552 gfp_t gfp)
7553{
7554 struct sk_buff *msg;
7555 void *hdr;
7556
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007557 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007558 if (!msg)
7559 return;
7560
7561 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7562 if (!hdr) {
7563 nlmsg_free(msg);
7564 return;
7565 }
7566
7567 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7568 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7569 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7570
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007571 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007572
Johannes Berg463d0182009-07-14 00:33:35 +02007573 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7574 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007575 return;
7576
7577 nla_put_failure:
7578 genlmsg_cancel(msg, hdr);
7579 nlmsg_free(msg);
7580}
7581
Javier Cardonac93b5e72011-04-07 15:08:34 -07007582void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7583 struct net_device *netdev,
7584 const u8 *macaddr, const u8* ie, u8 ie_len,
7585 gfp_t gfp)
7586{
7587 struct sk_buff *msg;
7588 void *hdr;
7589
7590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7591 if (!msg)
7592 return;
7593
7594 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7595 if (!hdr) {
7596 nlmsg_free(msg);
7597 return;
7598 }
7599
7600 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7601 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7602 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7603 if (ie_len && ie)
7604 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7605
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007606 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007607
7608 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7609 nl80211_mlme_mcgrp.id, gfp);
7610 return;
7611
7612 nla_put_failure:
7613 genlmsg_cancel(msg, hdr);
7614 nlmsg_free(msg);
7615}
7616
Jouni Malinena3b8b052009-03-27 21:59:49 +02007617void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7618 struct net_device *netdev, const u8 *addr,
7619 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007620 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007621{
7622 struct sk_buff *msg;
7623 void *hdr;
7624
Johannes Berge6d6e342009-07-01 21:26:47 +02007625 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007626 if (!msg)
7627 return;
7628
7629 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7630 if (!hdr) {
7631 nlmsg_free(msg);
7632 return;
7633 }
7634
7635 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7636 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7637 if (addr)
7638 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7639 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007640 if (key_id != -1)
7641 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007642 if (tsc)
7643 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7644
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007645 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007646
Johannes Berg463d0182009-07-14 00:33:35 +02007647 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7648 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007649 return;
7650
7651 nla_put_failure:
7652 genlmsg_cancel(msg, hdr);
7653 nlmsg_free(msg);
7654}
7655
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007656void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7657 struct ieee80211_channel *channel_before,
7658 struct ieee80211_channel *channel_after)
7659{
7660 struct sk_buff *msg;
7661 void *hdr;
7662 struct nlattr *nl_freq;
7663
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007664 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007665 if (!msg)
7666 return;
7667
7668 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7669 if (!hdr) {
7670 nlmsg_free(msg);
7671 return;
7672 }
7673
7674 /*
7675 * Since we are applying the beacon hint to a wiphy we know its
7676 * wiphy_idx is valid
7677 */
7678 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7679
7680 /* Before */
7681 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7682 if (!nl_freq)
7683 goto nla_put_failure;
7684 if (nl80211_msg_put_channel(msg, channel_before))
7685 goto nla_put_failure;
7686 nla_nest_end(msg, nl_freq);
7687
7688 /* After */
7689 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7690 if (!nl_freq)
7691 goto nla_put_failure;
7692 if (nl80211_msg_put_channel(msg, channel_after))
7693 goto nla_put_failure;
7694 nla_nest_end(msg, nl_freq);
7695
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007696 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007697
Johannes Berg463d0182009-07-14 00:33:35 +02007698 rcu_read_lock();
7699 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7700 GFP_ATOMIC);
7701 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007702
7703 return;
7704
7705nla_put_failure:
7706 genlmsg_cancel(msg, hdr);
7707 nlmsg_free(msg);
7708}
7709
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007710static void nl80211_send_remain_on_chan_event(
7711 int cmd, struct cfg80211_registered_device *rdev,
7712 struct net_device *netdev, u64 cookie,
7713 struct ieee80211_channel *chan,
7714 enum nl80211_channel_type channel_type,
7715 unsigned int duration, gfp_t gfp)
7716{
7717 struct sk_buff *msg;
7718 void *hdr;
7719
7720 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7721 if (!msg)
7722 return;
7723
7724 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7725 if (!hdr) {
7726 nlmsg_free(msg);
7727 return;
7728 }
7729
7730 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7731 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7732 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7733 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7734 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7735
7736 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7737 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7738
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007739 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007740
7741 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7742 nl80211_mlme_mcgrp.id, gfp);
7743 return;
7744
7745 nla_put_failure:
7746 genlmsg_cancel(msg, hdr);
7747 nlmsg_free(msg);
7748}
7749
7750void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7751 struct net_device *netdev, u64 cookie,
7752 struct ieee80211_channel *chan,
7753 enum nl80211_channel_type channel_type,
7754 unsigned int duration, gfp_t gfp)
7755{
7756 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7757 rdev, netdev, cookie, chan,
7758 channel_type, duration, gfp);
7759}
7760
7761void nl80211_send_remain_on_channel_cancel(
7762 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7763 u64 cookie, struct ieee80211_channel *chan,
7764 enum nl80211_channel_type channel_type, gfp_t gfp)
7765{
7766 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7767 rdev, netdev, cookie, chan,
7768 channel_type, 0, gfp);
7769}
7770
Johannes Berg98b62182009-12-23 13:15:44 +01007771void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7772 struct net_device *dev, const u8 *mac_addr,
7773 struct station_info *sinfo, gfp_t gfp)
7774{
7775 struct sk_buff *msg;
7776
7777 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7778 if (!msg)
7779 return;
7780
John W. Linville66266b32012-03-15 13:25:41 -04007781 if (nl80211_send_station(msg, 0, 0, 0,
7782 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01007783 nlmsg_free(msg);
7784 return;
7785 }
7786
7787 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7788 nl80211_mlme_mcgrp.id, gfp);
7789}
7790
Jouni Malinenec15e682011-03-23 15:29:52 +02007791void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7792 struct net_device *dev, const u8 *mac_addr,
7793 gfp_t gfp)
7794{
7795 struct sk_buff *msg;
7796 void *hdr;
7797
7798 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7799 if (!msg)
7800 return;
7801
7802 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7803 if (!hdr) {
7804 nlmsg_free(msg);
7805 return;
7806 }
7807
7808 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7809 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7810
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007811 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007812
7813 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7814 nl80211_mlme_mcgrp.id, gfp);
7815 return;
7816
7817 nla_put_failure:
7818 genlmsg_cancel(msg, hdr);
7819 nlmsg_free(msg);
7820}
7821
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007822static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7823 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007824{
7825 struct wireless_dev *wdev = dev->ieee80211_ptr;
7826 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7827 struct sk_buff *msg;
7828 void *hdr;
7829 int err;
7830 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7831
7832 if (!nlpid)
7833 return false;
7834
7835 msg = nlmsg_new(100, gfp);
7836 if (!msg)
7837 return true;
7838
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007839 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007840 if (!hdr) {
7841 nlmsg_free(msg);
7842 return true;
7843 }
7844
7845 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7847 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7848
7849 err = genlmsg_end(msg, hdr);
7850 if (err < 0) {
7851 nlmsg_free(msg);
7852 return true;
7853 }
7854
7855 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7856 return true;
7857
7858 nla_put_failure:
7859 genlmsg_cancel(msg, hdr);
7860 nlmsg_free(msg);
7861 return true;
7862}
7863
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007864bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7865{
7866 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7867 addr, gfp);
7868}
7869
7870bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7871 const u8 *addr, gfp_t gfp)
7872{
7873 return __nl80211_unexpected_frame(dev,
7874 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7875 addr, gfp);
7876}
7877
Johannes Berg2e161f72010-08-12 15:38:38 +02007878int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7879 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01007880 int freq, int sig_dbm,
7881 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007882{
7883 struct sk_buff *msg;
7884 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007885
7886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7887 if (!msg)
7888 return -ENOMEM;
7889
Johannes Berg2e161f72010-08-12 15:38:38 +02007890 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007891 if (!hdr) {
7892 nlmsg_free(msg);
7893 return -ENOMEM;
7894 }
7895
7896 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7897 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7898 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Johannes Berg804483e2012-03-05 22:18:41 +01007899 if (sig_dbm)
7900 NLA_PUT_U32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm);
Jouni Malinen026331c2010-02-15 12:53:10 +02007901 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7902
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007903 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007904
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007905 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007906
7907 nla_put_failure:
7908 genlmsg_cancel(msg, hdr);
7909 nlmsg_free(msg);
7910 return -ENOBUFS;
7911}
7912
Johannes Berg2e161f72010-08-12 15:38:38 +02007913void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7914 struct net_device *netdev, u64 cookie,
7915 const u8 *buf, size_t len, bool ack,
7916 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007917{
7918 struct sk_buff *msg;
7919 void *hdr;
7920
7921 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7922 if (!msg)
7923 return;
7924
Johannes Berg2e161f72010-08-12 15:38:38 +02007925 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007926 if (!hdr) {
7927 nlmsg_free(msg);
7928 return;
7929 }
7930
7931 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7932 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7933 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7934 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7935 if (ack)
7936 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7937
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007938 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007939
7940 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7941 return;
7942
7943 nla_put_failure:
7944 genlmsg_cancel(msg, hdr);
7945 nlmsg_free(msg);
7946}
7947
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007948void
7949nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7950 struct net_device *netdev,
7951 enum nl80211_cqm_rssi_threshold_event rssi_event,
7952 gfp_t gfp)
7953{
7954 struct sk_buff *msg;
7955 struct nlattr *pinfoattr;
7956 void *hdr;
7957
7958 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7959 if (!msg)
7960 return;
7961
7962 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7963 if (!hdr) {
7964 nlmsg_free(msg);
7965 return;
7966 }
7967
7968 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7969 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7970
7971 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7972 if (!pinfoattr)
7973 goto nla_put_failure;
7974
7975 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7976 rssi_event);
7977
7978 nla_nest_end(msg, pinfoattr);
7979
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007980 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007981
7982 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7983 nl80211_mlme_mcgrp.id, gfp);
7984 return;
7985
7986 nla_put_failure:
7987 genlmsg_cancel(msg, hdr);
7988 nlmsg_free(msg);
7989}
7990
Johannes Berge5497d72011-07-05 16:35:40 +02007991void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7992 struct net_device *netdev, const u8 *bssid,
7993 const u8 *replay_ctr, gfp_t gfp)
7994{
7995 struct sk_buff *msg;
7996 struct nlattr *rekey_attr;
7997 void *hdr;
7998
7999 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8000 if (!msg)
8001 return;
8002
8003 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8004 if (!hdr) {
8005 nlmsg_free(msg);
8006 return;
8007 }
8008
8009 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8010 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8011 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
8012
8013 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8014 if (!rekey_attr)
8015 goto nla_put_failure;
8016
8017 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8018 NL80211_REPLAY_CTR_LEN, replay_ctr);
8019
8020 nla_nest_end(msg, rekey_attr);
8021
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008022 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008023
8024 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8025 nl80211_mlme_mcgrp.id, gfp);
8026 return;
8027
8028 nla_put_failure:
8029 genlmsg_cancel(msg, hdr);
8030 nlmsg_free(msg);
8031}
8032
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008033void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8034 struct net_device *netdev, int index,
8035 const u8 *bssid, bool preauth, gfp_t gfp)
8036{
8037 struct sk_buff *msg;
8038 struct nlattr *attr;
8039 void *hdr;
8040
8041 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8042 if (!msg)
8043 return;
8044
8045 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8046 if (!hdr) {
8047 nlmsg_free(msg);
8048 return;
8049 }
8050
8051 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8052 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8053
8054 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8055 if (!attr)
8056 goto nla_put_failure;
8057
8058 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
8059 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
8060 if (preauth)
8061 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
8062
8063 nla_nest_end(msg, attr);
8064
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008065 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008066
8067 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8068 nl80211_mlme_mcgrp.id, gfp);
8069 return;
8070
8071 nla_put_failure:
8072 genlmsg_cancel(msg, hdr);
8073 nlmsg_free(msg);
8074}
8075
Johannes Bergc063dbf2010-11-24 08:10:05 +01008076void
8077nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8078 struct net_device *netdev, const u8 *peer,
8079 u32 num_packets, gfp_t gfp)
8080{
8081 struct sk_buff *msg;
8082 struct nlattr *pinfoattr;
8083 void *hdr;
8084
8085 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8086 if (!msg)
8087 return;
8088
8089 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8090 if (!hdr) {
8091 nlmsg_free(msg);
8092 return;
8093 }
8094
8095 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8096 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
8097 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
8098
8099 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8100 if (!pinfoattr)
8101 goto nla_put_failure;
8102
8103 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
8104
8105 nla_nest_end(msg, pinfoattr);
8106
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008107 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008108
8109 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8110 nl80211_mlme_mcgrp.id, gfp);
8111 return;
8112
8113 nla_put_failure:
8114 genlmsg_cancel(msg, hdr);
8115 nlmsg_free(msg);
8116}
8117
Johannes Berg7f6cf312011-11-04 11:18:15 +01008118void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8119 u64 cookie, bool acked, gfp_t gfp)
8120{
8121 struct wireless_dev *wdev = dev->ieee80211_ptr;
8122 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8123 struct sk_buff *msg;
8124 void *hdr;
8125 int err;
8126
8127 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8128 if (!msg)
8129 return;
8130
8131 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8132 if (!hdr) {
8133 nlmsg_free(msg);
8134 return;
8135 }
8136
8137 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8138 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
8139 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8140 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
8141 if (acked)
8142 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
8143
8144 err = genlmsg_end(msg, hdr);
8145 if (err < 0) {
8146 nlmsg_free(msg);
8147 return;
8148 }
8149
8150 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8151 nl80211_mlme_mcgrp.id, gfp);
8152 return;
8153
8154 nla_put_failure:
8155 genlmsg_cancel(msg, hdr);
8156 nlmsg_free(msg);
8157}
8158EXPORT_SYMBOL(cfg80211_probe_status);
8159
Johannes Berg5e760232011-11-04 11:18:17 +01008160void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8161 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008162 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008163{
8164 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8165 struct sk_buff *msg;
8166 void *hdr;
8167 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8168
8169 if (!nlpid)
8170 return;
8171
8172 msg = nlmsg_new(len + 100, gfp);
8173 if (!msg)
8174 return;
8175
8176 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8177 if (!hdr) {
8178 nlmsg_free(msg);
8179 return;
8180 }
8181
8182 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
8183 if (freq)
8184 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Johannes Berg804483e2012-03-05 22:18:41 +01008185 if (sig_dbm)
8186 NLA_PUT_U32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm);
Johannes Berg5e760232011-11-04 11:18:17 +01008187 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
8188
8189 genlmsg_end(msg, hdr);
8190
8191 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8192 return;
8193
8194 nla_put_failure:
8195 genlmsg_cancel(msg, hdr);
8196 nlmsg_free(msg);
8197}
8198EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8199
Jouni Malinen4e1c8442013-01-28 21:11:03 -08008200void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
8201 enum nl80211_tdls_operation oper,
8202 u16 reason_code, gfp_t gfp)
8203{
8204 struct wireless_dev *wdev = dev->ieee80211_ptr;
8205 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8206 struct sk_buff *msg;
8207 void *hdr;
8208 int err;
8209
8210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8211 if (!msg)
8212 return;
8213
8214 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
8215 if (!hdr) {
8216 nlmsg_free(msg);
8217 return;
8218 }
8219
8220 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8221 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8222 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
8223 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
8224 (reason_code > 0 &&
8225 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
8226 goto nla_put_failure;
8227
8228 err = genlmsg_end(msg, hdr);
8229 if (err < 0) {
8230 nlmsg_free(msg);
8231 return;
8232 }
8233
8234 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8235 nl80211_mlme_mcgrp.id, gfp);
8236 return;
8237
8238 nla_put_failure:
8239 genlmsg_cancel(msg, hdr);
8240 nlmsg_free(msg);
8241}
8242EXPORT_SYMBOL(cfg80211_tdls_oper_request);
8243
Jouni Malinen026331c2010-02-15 12:53:10 +02008244static int nl80211_netlink_notify(struct notifier_block * nb,
8245 unsigned long state,
8246 void *_notify)
8247{
8248 struct netlink_notify *notify = _notify;
8249 struct cfg80211_registered_device *rdev;
8250 struct wireless_dev *wdev;
8251
8252 if (state != NETLINK_URELEASE)
8253 return NOTIFY_DONE;
8254
8255 rcu_read_lock();
8256
Johannes Berg5e760232011-11-04 11:18:17 +01008257 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02008258 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008259 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008260 if (rdev->ap_beacons_nlpid == notify->pid)
8261 rdev->ap_beacons_nlpid = 0;
8262 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008263
8264 rcu_read_unlock();
8265
8266 return NOTIFY_DONE;
8267}
8268
8269static struct notifier_block nl80211_netlink_notifier = {
8270 .notifier_call = nl80211_netlink_notify,
8271};
8272
Johannes Berg55682962007-09-20 13:09:35 -04008273/* initialisation/exit functions */
8274
8275int nl80211_init(void)
8276{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008277 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008278
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008279 err = genl_register_family_with_ops(&nl80211_fam,
8280 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008281 if (err)
8282 return err;
8283
Johannes Berg55682962007-09-20 13:09:35 -04008284 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8285 if (err)
8286 goto err_out;
8287
Johannes Berg2a519312009-02-10 21:25:55 +01008288 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8289 if (err)
8290 goto err_out;
8291
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008292 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8293 if (err)
8294 goto err_out;
8295
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008296 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8297 if (err)
8298 goto err_out;
8299
Johannes Bergaff89a92009-07-01 21:26:51 +02008300#ifdef CONFIG_NL80211_TESTMODE
8301 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8302 if (err)
8303 goto err_out;
8304#endif
8305
Jouni Malinen026331c2010-02-15 12:53:10 +02008306 err = netlink_register_notifier(&nl80211_netlink_notifier);
8307 if (err)
8308 goto err_out;
8309
Johannes Berg55682962007-09-20 13:09:35 -04008310 return 0;
8311 err_out:
8312 genl_unregister_family(&nl80211_fam);
8313 return err;
8314}
8315
8316void nl80211_exit(void)
8317{
Jouni Malinen026331c2010-02-15 12:53:10 +02008318 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008319 genl_unregister_family(&nl80211_fam);
8320}