blob: 258fb881c8e366426dde11bf33a407dab1309bed [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 Berg463d0182009-07-14 00:33:35 +020050static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020051 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040052 struct net_device **dev)
53{
Johannes Berg463d0182009-07-14 00:33:35 +020054 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040055 int ifindex;
56
Johannes Bergbba95fe2008-07-29 13:22:51 +020057 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040058 return -EINVAL;
59
Johannes Bergbba95fe2008-07-29 13:22:51 +020060 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020061 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040062 if (!*dev)
63 return -ENODEV;
64
Johannes Berg463d0182009-07-14 00:33:35 +020065 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020066 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040067 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020068 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040069 }
70
71 return 0;
72}
73
74/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000075static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040076 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
77 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070078 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020079 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020080 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053081 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020082 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
84 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010086 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040087
88 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
90 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010091
92 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020093 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010094
Johannes Bergb9454e82009-07-08 13:29:08 +020095 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010096 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
97 .len = WLAN_MAX_KEY_LEN },
98 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
99 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
100 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200102 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100103
104 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
105 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
106 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
107 .len = IEEE80211_MAX_DATA_LEN },
108 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
109 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
111 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
112 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
113 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100115 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100116 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200117 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100118 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
119 .len = IEEE80211_MAX_MESH_ID_LEN },
120 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300121
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700122 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
123 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
124
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300125 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200128 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
129 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100130 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300131
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800132 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700133 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700134
Johannes Berg6c739412011-11-03 09:27:01 +0100135 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200136
137 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
138 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
139 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100140 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
141 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200142
143 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
144 .len = IEEE80211_MAX_SSID_LEN },
145 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
146 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200147 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300148 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300149 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300150 [NL80211_ATTR_STA_FLAGS2] = {
151 .len = sizeof(struct nl80211_sta_flag_update),
152 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300153 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300154 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
155 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200156 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
157 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
158 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200159 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100160 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100161 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
162 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100163 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
164 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200165 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200166 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
167 .len = IEEE80211_MAX_DATA_LEN },
168 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200169 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200170 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300171 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200172 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300173 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
174 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200175 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900176 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
177 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100178 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100179 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100180 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200181 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700182 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300183 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200184 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200185 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300186 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300187 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
188 .len = IEEE80211_MAX_DATA_LEN },
189 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
190 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530191 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300192 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530193 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300194 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
197 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
198 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100199 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400200};
201
Johannes Berge31b8212010-10-05 19:39:30 +0200202/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000203static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200204 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200205 [NL80211_KEY_IDX] = { .type = NLA_U8 },
206 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200207 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200208 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
209 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200210 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100211 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
212};
213
214/* policy for the key default flags */
215static const struct nla_policy
216nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
217 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
218 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200219};
220
Johannes Bergff1b6e62011-05-04 15:37:28 +0200221/* policy for WoWLAN attributes */
222static const struct nla_policy
223nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
224 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
225 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
226 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
227 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200228 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
229 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
230 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
231 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200232};
233
Johannes Berge5497d72011-07-05 16:35:40 +0200234/* policy for GTK rekey offload attributes */
235static const struct nla_policy
236nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
237 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
238 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
239 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
240};
241
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300242static const struct nla_policy
243nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
244 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
245 .len = IEEE80211_MAX_SSID_LEN },
246};
247
Holger Schuriga0438972009-11-11 11:30:02 +0100248/* ifidx get helper */
249static int nl80211_get_ifidx(struct netlink_callback *cb)
250{
251 int res;
252
253 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
254 nl80211_fam.attrbuf, nl80211_fam.maxattr,
255 nl80211_policy);
256 if (res)
257 return res;
258
259 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
260 return -EINVAL;
261
262 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
263 if (!res)
264 return -EINVAL;
265 return res;
266}
267
Johannes Berg67748892010-10-04 21:14:06 +0200268static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
269 struct netlink_callback *cb,
270 struct cfg80211_registered_device **rdev,
271 struct net_device **dev)
272{
273 int ifidx = cb->args[0];
274 int err;
275
276 if (!ifidx)
277 ifidx = nl80211_get_ifidx(cb);
278 if (ifidx < 0)
279 return ifidx;
280
281 cb->args[0] = ifidx;
282
283 rtnl_lock();
284
285 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
286 if (!*dev) {
287 err = -ENODEV;
288 goto out_rtnl;
289 }
290
291 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100292 if (IS_ERR(*rdev)) {
293 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200294 goto out_rtnl;
295 }
296
297 return 0;
298 out_rtnl:
299 rtnl_unlock();
300 return err;
301}
302
303static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
304{
305 cfg80211_unlock_rdev(rdev);
306 rtnl_unlock();
307}
308
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100309/* IE validation */
310static bool is_valid_ie_attr(const struct nlattr *attr)
311{
312 const u8 *pos;
313 int len;
314
315 if (!attr)
316 return true;
317
318 pos = nla_data(attr);
319 len = nla_len(attr);
320
321 while (len) {
322 u8 elemlen;
323
324 if (len < 2)
325 return false;
326 len -= 2;
327
328 elemlen = pos[1];
329 if (elemlen > len)
330 return false;
331
332 len -= elemlen;
333 pos += 2 + elemlen;
334 }
335
336 return true;
337}
338
Johannes Berg55682962007-09-20 13:09:35 -0400339/* message building helper */
340static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
341 int flags, u8 cmd)
342{
343 /* since there is no private header just add the generic one */
344 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
345}
346
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400347static int nl80211_msg_put_channel(struct sk_buff *msg,
348 struct ieee80211_channel *chan)
349{
350 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
351 chan->center_freq);
352
353 if (chan->flags & IEEE80211_CHAN_DISABLED)
354 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
355 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
356 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
357 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
358 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
359 if (chan->flags & IEEE80211_CHAN_RADAR)
360 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
361
362 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
363 DBM_TO_MBM(chan->max_power));
364
365 return 0;
366
367 nla_put_failure:
368 return -ENOBUFS;
369}
370
Johannes Berg55682962007-09-20 13:09:35 -0400371/* netlink command implementations */
372
Johannes Bergb9454e82009-07-08 13:29:08 +0200373struct key_parse {
374 struct key_params p;
375 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200376 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200377 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100378 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200379};
380
381static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
382{
383 struct nlattr *tb[NL80211_KEY_MAX + 1];
384 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
385 nl80211_key_policy);
386 if (err)
387 return err;
388
389 k->def = !!tb[NL80211_KEY_DEFAULT];
390 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
391
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100392 if (k->def) {
393 k->def_uni = true;
394 k->def_multi = true;
395 }
396 if (k->defmgmt)
397 k->def_multi = true;
398
Johannes Bergb9454e82009-07-08 13:29:08 +0200399 if (tb[NL80211_KEY_IDX])
400 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
401
402 if (tb[NL80211_KEY_DATA]) {
403 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
404 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
405 }
406
407 if (tb[NL80211_KEY_SEQ]) {
408 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
409 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
410 }
411
412 if (tb[NL80211_KEY_CIPHER])
413 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
414
Johannes Berge31b8212010-10-05 19:39:30 +0200415 if (tb[NL80211_KEY_TYPE]) {
416 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
417 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
418 return -EINVAL;
419 }
420
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100421 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
422 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
423 int err = nla_parse_nested(kdt,
424 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
425 tb[NL80211_KEY_DEFAULT_TYPES],
426 nl80211_key_default_policy);
427 if (err)
428 return err;
429
430 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
431 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
432 }
433
Johannes Bergb9454e82009-07-08 13:29:08 +0200434 return 0;
435}
436
437static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
438{
439 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
440 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
441 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
442 }
443
444 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
445 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
446 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
447 }
448
449 if (info->attrs[NL80211_ATTR_KEY_IDX])
450 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
451
452 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
453 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
454
455 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
456 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
457
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100458 if (k->def) {
459 k->def_uni = true;
460 k->def_multi = true;
461 }
462 if (k->defmgmt)
463 k->def_multi = true;
464
Johannes Berge31b8212010-10-05 19:39:30 +0200465 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
466 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
467 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
468 return -EINVAL;
469 }
470
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100471 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
472 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
473 int err = nla_parse_nested(
474 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
475 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
476 nl80211_key_default_policy);
477 if (err)
478 return err;
479
480 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
481 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
482 }
483
Johannes Bergb9454e82009-07-08 13:29:08 +0200484 return 0;
485}
486
487static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
488{
489 int err;
490
491 memset(k, 0, sizeof(*k));
492 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200493 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200494
495 if (info->attrs[NL80211_ATTR_KEY])
496 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
497 else
498 err = nl80211_parse_key_old(info, k);
499
500 if (err)
501 return err;
502
503 if (k->def && k->defmgmt)
504 return -EINVAL;
505
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100506 if (k->defmgmt) {
507 if (k->def_uni || !k->def_multi)
508 return -EINVAL;
509 }
510
Johannes Bergb9454e82009-07-08 13:29:08 +0200511 if (k->idx != -1) {
512 if (k->defmgmt) {
513 if (k->idx < 4 || k->idx > 5)
514 return -EINVAL;
515 } else if (k->def) {
516 if (k->idx < 0 || k->idx > 3)
517 return -EINVAL;
518 } else {
519 if (k->idx < 0 || k->idx > 5)
520 return -EINVAL;
521 }
522 }
523
524 return 0;
525}
526
Johannes Bergfffd0932009-07-08 14:22:54 +0200527static struct cfg80211_cached_keys *
528nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
529 struct nlattr *keys)
530{
531 struct key_parse parse;
532 struct nlattr *key;
533 struct cfg80211_cached_keys *result;
534 int rem, err, def = 0;
535
536 result = kzalloc(sizeof(*result), GFP_KERNEL);
537 if (!result)
538 return ERR_PTR(-ENOMEM);
539
540 result->def = -1;
541 result->defmgmt = -1;
542
543 nla_for_each_nested(key, keys, rem) {
544 memset(&parse, 0, sizeof(parse));
545 parse.idx = -1;
546
547 err = nl80211_parse_key_new(key, &parse);
548 if (err)
549 goto error;
550 err = -EINVAL;
551 if (!parse.p.key)
552 goto error;
553 if (parse.idx < 0 || parse.idx > 4)
554 goto error;
555 if (parse.def) {
556 if (def)
557 goto error;
558 def = 1;
559 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100560 if (!parse.def_uni || !parse.def_multi)
561 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200562 } else if (parse.defmgmt)
563 goto error;
564 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200565 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200566 if (err)
567 goto error;
568 result->params[parse.idx].cipher = parse.p.cipher;
569 result->params[parse.idx].key_len = parse.p.key_len;
570 result->params[parse.idx].key = result->data[parse.idx];
571 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
572 }
573
574 return result;
575 error:
576 kfree(result);
577 return ERR_PTR(err);
578}
579
580static int nl80211_key_allowed(struct wireless_dev *wdev)
581{
582 ASSERT_WDEV_LOCK(wdev);
583
Johannes Bergfffd0932009-07-08 14:22:54 +0200584 switch (wdev->iftype) {
585 case NL80211_IFTYPE_AP:
586 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200587 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700588 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200589 break;
590 case NL80211_IFTYPE_ADHOC:
591 if (!wdev->current_bss)
592 return -ENOLINK;
593 break;
594 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200595 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200596 if (wdev->sme_state != CFG80211_SME_CONNECTED)
597 return -ENOLINK;
598 break;
599 default:
600 return -EINVAL;
601 }
602
603 return 0;
604}
605
Johannes Berg7527a782011-05-13 10:58:57 +0200606static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
607{
608 struct nlattr *nl_modes = nla_nest_start(msg, attr);
609 int i;
610
611 if (!nl_modes)
612 goto nla_put_failure;
613
614 i = 0;
615 while (ifmodes) {
616 if (ifmodes & 1)
617 NLA_PUT_FLAG(msg, i);
618 ifmodes >>= 1;
619 i++;
620 }
621
622 nla_nest_end(msg, nl_modes);
623 return 0;
624
625nla_put_failure:
626 return -ENOBUFS;
627}
628
629static int nl80211_put_iface_combinations(struct wiphy *wiphy,
630 struct sk_buff *msg)
631{
632 struct nlattr *nl_combis;
633 int i, j;
634
635 nl_combis = nla_nest_start(msg,
636 NL80211_ATTR_INTERFACE_COMBINATIONS);
637 if (!nl_combis)
638 goto nla_put_failure;
639
640 for (i = 0; i < wiphy->n_iface_combinations; i++) {
641 const struct ieee80211_iface_combination *c;
642 struct nlattr *nl_combi, *nl_limits;
643
644 c = &wiphy->iface_combinations[i];
645
646 nl_combi = nla_nest_start(msg, i + 1);
647 if (!nl_combi)
648 goto nla_put_failure;
649
650 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
651 if (!nl_limits)
652 goto nla_put_failure;
653
654 for (j = 0; j < c->n_limits; j++) {
655 struct nlattr *nl_limit;
656
657 nl_limit = nla_nest_start(msg, j + 1);
658 if (!nl_limit)
659 goto nla_put_failure;
660 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
661 c->limits[j].max);
662 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
663 c->limits[j].types))
664 goto nla_put_failure;
665 nla_nest_end(msg, nl_limit);
666 }
667
668 nla_nest_end(msg, nl_limits);
669
670 if (c->beacon_int_infra_match)
671 NLA_PUT_FLAG(msg,
672 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
673 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
674 c->num_different_channels);
675 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
676 c->max_interfaces);
677
678 nla_nest_end(msg, nl_combi);
679 }
680
681 nla_nest_end(msg, nl_combis);
682
683 return 0;
684nla_put_failure:
685 return -ENOBUFS;
686}
687
Johannes Berg55682962007-09-20 13:09:35 -0400688static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
689 struct cfg80211_registered_device *dev)
690{
691 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100692 struct nlattr *nl_bands, *nl_band;
693 struct nlattr *nl_freqs, *nl_freq;
694 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100695 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100696 enum ieee80211_band band;
697 struct ieee80211_channel *chan;
698 struct ieee80211_rate *rate;
699 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200700 const struct ieee80211_txrx_stypes *mgmt_stypes =
701 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400702
703 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
704 if (!hdr)
705 return -1;
706
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500707 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400708 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200709
Johannes Bergf5ea9122009-08-07 16:17:38 +0200710 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
711 cfg80211_rdev_list_generation);
712
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200713 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
714 dev->wiphy.retry_short);
715 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
716 dev->wiphy.retry_long);
717 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
718 dev->wiphy.frag_threshold);
719 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
720 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100721 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
722 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100723 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
724 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300725 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
726 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200727 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
728 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300729 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
730 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300731 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
732 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100733
Johannes Berge31b8212010-10-05 19:39:30 +0200734 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
735 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700736 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
737 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300738 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
739 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530740 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
741 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300742 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
743 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
744 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
745 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530746
Johannes Berg25e47c182009-04-02 20:14:06 +0200747 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
748 sizeof(u32) * dev->wiphy.n_cipher_suites,
749 dev->wiphy.cipher_suites);
750
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100751 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
752 dev->wiphy.max_num_pmkids);
753
Johannes Bergc0692b82010-08-27 14:26:53 +0300754 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
755 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
756
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900757 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
758 dev->wiphy.available_antennas_tx);
759 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
760 dev->wiphy.available_antennas_rx);
761
Bruno Randolf7f531e02010-12-16 11:30:22 +0900762 if ((dev->wiphy.available_antennas_tx ||
763 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900764 u32 tx_ant = 0, rx_ant = 0;
765 int res;
766 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
767 if (!res) {
768 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
769 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
770 }
771 }
772
Johannes Berg7527a782011-05-13 10:58:57 +0200773 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
774 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700775 goto nla_put_failure;
776
Johannes Bergee688b002008-01-24 19:38:39 +0100777 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
778 if (!nl_bands)
779 goto nla_put_failure;
780
781 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
782 if (!dev->wiphy.bands[band])
783 continue;
784
785 nl_band = nla_nest_start(msg, band);
786 if (!nl_band)
787 goto nla_put_failure;
788
Johannes Bergd51626d2008-10-09 12:20:13 +0200789 /* add HT info */
790 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
791 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
792 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
793 &dev->wiphy.bands[band]->ht_cap.mcs);
794 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
795 dev->wiphy.bands[band]->ht_cap.cap);
796 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
797 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
798 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
799 dev->wiphy.bands[band]->ht_cap.ampdu_density);
800 }
801
Johannes Bergee688b002008-01-24 19:38:39 +0100802 /* add frequencies */
803 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
804 if (!nl_freqs)
805 goto nla_put_failure;
806
807 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
808 nl_freq = nla_nest_start(msg, i);
809 if (!nl_freq)
810 goto nla_put_failure;
811
812 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100813
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400814 if (nl80211_msg_put_channel(msg, chan))
815 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200816
Johannes Bergee688b002008-01-24 19:38:39 +0100817 nla_nest_end(msg, nl_freq);
818 }
819
820 nla_nest_end(msg, nl_freqs);
821
822 /* add bitrates */
823 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
824 if (!nl_rates)
825 goto nla_put_failure;
826
827 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
828 nl_rate = nla_nest_start(msg, i);
829 if (!nl_rate)
830 goto nla_put_failure;
831
832 rate = &dev->wiphy.bands[band]->bitrates[i];
833 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
834 rate->bitrate);
835 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
836 NLA_PUT_FLAG(msg,
837 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
838
839 nla_nest_end(msg, nl_rate);
840 }
841
842 nla_nest_end(msg, nl_rates);
843
844 nla_nest_end(msg, nl_band);
845 }
846 nla_nest_end(msg, nl_bands);
847
Johannes Berg8fdc6212009-03-14 09:34:01 +0100848 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
849 if (!nl_cmds)
850 goto nla_put_failure;
851
852 i = 0;
853#define CMD(op, n) \
854 do { \
855 if (dev->ops->op) { \
856 i++; \
857 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
858 } \
859 } while (0)
860
861 CMD(add_virtual_intf, NEW_INTERFACE);
862 CMD(change_virtual_intf, SET_INTERFACE);
863 CMD(add_key, NEW_KEY);
864 CMD(add_beacon, NEW_BEACON);
865 CMD(add_station, NEW_STATION);
866 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800867 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100868 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200869 CMD(auth, AUTHENTICATE);
870 CMD(assoc, ASSOCIATE);
871 CMD(deauth, DEAUTHENTICATE);
872 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200873 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100874 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100875 CMD(set_pmksa, SET_PMKSA);
876 CMD(del_pmksa, DEL_PMKSA);
877 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100878 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200879 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200880 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100881 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100882 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200883 i++;
884 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
885 }
Johannes Bergf444de02010-05-05 15:25:02 +0200886 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400887 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300888 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
889 CMD(tdls_mgmt, TDLS_MGMT);
890 CMD(tdls_oper, TDLS_OPER);
891 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300892 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
893 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100894 CMD(probe_client, PROBE_CLIENT);
Johannes Berg5e760232011-11-04 11:18:17 +0100895 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
896 i++;
897 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
898 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100899
900#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200901
Johannes Berg6829c872009-07-02 09:13:27 +0200902 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903 i++;
904 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
905 }
906
Johannes Berg6829c872009-07-02 09:13:27 +0200907 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200908 i++;
909 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
910 }
911
Johannes Berg8fdc6212009-03-14 09:34:01 +0100912 nla_nest_end(msg, nl_cmds);
913
Johannes Berga2939112010-12-14 17:54:28 +0100914 if (dev->ops->remain_on_channel)
915 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
916 dev->wiphy.max_remain_on_channel_duration);
917
Jouni Malinend2da5872011-08-08 12:10:30 +0300918 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100919 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
920
Johannes Berg2e161f72010-08-12 15:38:38 +0200921 if (mgmt_stypes) {
922 u16 stypes;
923 struct nlattr *nl_ftypes, *nl_ifs;
924 enum nl80211_iftype ift;
925
926 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
927 if (!nl_ifs)
928 goto nla_put_failure;
929
930 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
931 nl_ftypes = nla_nest_start(msg, ift);
932 if (!nl_ftypes)
933 goto nla_put_failure;
934 i = 0;
935 stypes = mgmt_stypes[ift].tx;
936 while (stypes) {
937 if (stypes & 1)
938 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
939 (i << 4) | IEEE80211_FTYPE_MGMT);
940 stypes >>= 1;
941 i++;
942 }
943 nla_nest_end(msg, nl_ftypes);
944 }
945
Johannes Berg74b70a42010-08-24 12:15:53 +0200946 nla_nest_end(msg, nl_ifs);
947
Johannes Berg2e161f72010-08-12 15:38:38 +0200948 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
949 if (!nl_ifs)
950 goto nla_put_failure;
951
952 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
953 nl_ftypes = nla_nest_start(msg, ift);
954 if (!nl_ftypes)
955 goto nla_put_failure;
956 i = 0;
957 stypes = mgmt_stypes[ift].rx;
958 while (stypes) {
959 if (stypes & 1)
960 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
961 (i << 4) | IEEE80211_FTYPE_MGMT);
962 stypes >>= 1;
963 i++;
964 }
965 nla_nest_end(msg, nl_ftypes);
966 }
967 nla_nest_end(msg, nl_ifs);
968 }
969
Johannes Bergff1b6e62011-05-04 15:37:28 +0200970 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
971 struct nlattr *nl_wowlan;
972
973 nl_wowlan = nla_nest_start(msg,
974 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
975 if (!nl_wowlan)
976 goto nla_put_failure;
977
978 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
979 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
980 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
981 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
982 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
983 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200984 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
985 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
986 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
987 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
988 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
989 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
990 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
991 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
992 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
993 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200994 if (dev->wiphy.wowlan.n_patterns) {
995 struct nl80211_wowlan_pattern_support pat = {
996 .max_patterns = dev->wiphy.wowlan.n_patterns,
997 .min_pattern_len =
998 dev->wiphy.wowlan.pattern_min_len,
999 .max_pattern_len =
1000 dev->wiphy.wowlan.pattern_max_len,
1001 };
1002 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1003 sizeof(pat), &pat);
1004 }
1005
1006 nla_nest_end(msg, nl_wowlan);
1007 }
1008
Johannes Berg7527a782011-05-13 10:58:57 +02001009 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1010 dev->wiphy.software_iftypes))
1011 goto nla_put_failure;
1012
1013 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1014 goto nla_put_failure;
1015
Johannes Berg562a7482011-11-07 12:39:33 +01001016 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1017 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1018 dev->wiphy.ap_sme_capa);
1019
Johannes Berg1f074bd2011-11-06 14:13:33 +01001020 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1021
Johannes Berg55682962007-09-20 13:09:35 -04001022 return genlmsg_end(msg, hdr);
1023
1024 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001025 genlmsg_cancel(msg, hdr);
1026 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001027}
1028
1029static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1030{
1031 int idx = 0;
1032 int start = cb->args[0];
1033 struct cfg80211_registered_device *dev;
1034
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001035 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001036 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001037 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1038 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001039 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001040 continue;
1041 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1042 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001043 dev) < 0) {
1044 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001045 break;
Julius Volzb4637272008-07-08 14:02:19 +02001046 }
Johannes Berg55682962007-09-20 13:09:35 -04001047 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001048 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001049
1050 cb->args[0] = idx;
1051
1052 return skb->len;
1053}
1054
1055static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1056{
1057 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001058 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001059
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001060 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001061 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001062 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001063
Johannes Berg4c476992010-10-04 21:36:35 +02001064 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1065 nlmsg_free(msg);
1066 return -ENOBUFS;
1067 }
Johannes Berg55682962007-09-20 13:09:35 -04001068
Johannes Berg134e6372009-07-10 09:51:34 +00001069 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001070}
1071
Jouni Malinen31888482008-10-30 16:59:24 +02001072static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1073 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1074 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1075 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1076 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1077 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1078};
1079
1080static int parse_txq_params(struct nlattr *tb[],
1081 struct ieee80211_txq_params *txq_params)
1082{
1083 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1084 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1085 !tb[NL80211_TXQ_ATTR_AIFS])
1086 return -EINVAL;
1087
1088 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1089 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1090 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1091 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1092 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1093
1094 return 0;
1095}
1096
Johannes Bergf444de02010-05-05 15:25:02 +02001097static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1098{
1099 /*
1100 * You can only set the channel explicitly for AP, mesh
1101 * and WDS type interfaces; all others have their channel
1102 * managed via their respective "establish a connection"
1103 * command (connect, join, ...)
1104 *
1105 * Monitors are special as they are normally slaved to
1106 * whatever else is going on, so they behave as though
1107 * you tried setting the wiphy channel itself.
1108 */
1109 return !wdev ||
1110 wdev->iftype == NL80211_IFTYPE_AP ||
1111 wdev->iftype == NL80211_IFTYPE_WDS ||
1112 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001113 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1114 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001115}
1116
1117static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1118 struct wireless_dev *wdev,
1119 struct genl_info *info)
1120{
1121 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1122 u32 freq;
1123 int result;
1124
1125 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1126 return -EINVAL;
1127
1128 if (!nl80211_can_set_dev_channel(wdev))
1129 return -EOPNOTSUPP;
1130
1131 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1132 channel_type = nla_get_u32(info->attrs[
1133 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1134 if (channel_type != NL80211_CHAN_NO_HT &&
1135 channel_type != NL80211_CHAN_HT20 &&
1136 channel_type != NL80211_CHAN_HT40PLUS &&
1137 channel_type != NL80211_CHAN_HT40MINUS)
1138 return -EINVAL;
1139 }
1140
1141 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1142
1143 mutex_lock(&rdev->devlist_mtx);
1144 if (wdev) {
1145 wdev_lock(wdev);
1146 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1147 wdev_unlock(wdev);
1148 } else {
1149 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1150 }
1151 mutex_unlock(&rdev->devlist_mtx);
1152
1153 return result;
1154}
1155
1156static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1157{
Johannes Berg4c476992010-10-04 21:36:35 +02001158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1159 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001160
Johannes Berg4c476992010-10-04 21:36:35 +02001161 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001162}
1163
Bill Jordane8347eb2010-10-01 13:54:28 -04001164static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1165{
Johannes Berg43b19952010-10-07 13:10:30 +02001166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1167 struct net_device *dev = info->user_ptr[1];
1168 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001169 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001170
1171 if (!info->attrs[NL80211_ATTR_MAC])
1172 return -EINVAL;
1173
Johannes Berg43b19952010-10-07 13:10:30 +02001174 if (netif_running(dev))
1175 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001176
Johannes Berg43b19952010-10-07 13:10:30 +02001177 if (!rdev->ops->set_wds_peer)
1178 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001179
Johannes Berg43b19952010-10-07 13:10:30 +02001180 if (wdev->iftype != NL80211_IFTYPE_WDS)
1181 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001182
1183 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001184 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001185}
1186
1187
Johannes Berg55682962007-09-20 13:09:35 -04001188static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1189{
1190 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001191 struct net_device *netdev = NULL;
1192 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001193 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001194 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001195 u32 changed;
1196 u8 retry_short = 0, retry_long = 0;
1197 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001198 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001199
Johannes Bergf444de02010-05-05 15:25:02 +02001200 /*
1201 * Try to find the wiphy and netdev. Normally this
1202 * function shouldn't need the netdev, but this is
1203 * done for backward compatibility -- previously
1204 * setting the channel was done per wiphy, but now
1205 * it is per netdev. Previous userland like hostapd
1206 * also passed a netdev to set_wiphy, so that it is
1207 * possible to let that go to the right netdev!
1208 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001209 mutex_lock(&cfg80211_mutex);
1210
Johannes Bergf444de02010-05-05 15:25:02 +02001211 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1212 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1213
1214 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1215 if (netdev && netdev->ieee80211_ptr) {
1216 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1217 mutex_lock(&rdev->mtx);
1218 } else
1219 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001220 }
1221
Johannes Bergf444de02010-05-05 15:25:02 +02001222 if (!netdev) {
1223 rdev = __cfg80211_rdev_from_info(info);
1224 if (IS_ERR(rdev)) {
1225 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001226 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001227 }
1228 wdev = NULL;
1229 netdev = NULL;
1230 result = 0;
1231
1232 mutex_lock(&rdev->mtx);
1233 } else if (netif_running(netdev) &&
1234 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1235 wdev = netdev->ieee80211_ptr;
1236 else
1237 wdev = NULL;
1238
1239 /*
1240 * end workaround code, by now the rdev is available
1241 * and locked, and wdev may or may not be NULL.
1242 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001243
1244 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001245 result = cfg80211_dev_rename(
1246 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001247
1248 mutex_unlock(&cfg80211_mutex);
1249
1250 if (result)
1251 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001252
Jouni Malinen31888482008-10-30 16:59:24 +02001253 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1254 struct ieee80211_txq_params txq_params;
1255 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1256
1257 if (!rdev->ops->set_txq_params) {
1258 result = -EOPNOTSUPP;
1259 goto bad_res;
1260 }
1261
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001262 if (!netdev) {
1263 result = -EINVAL;
1264 goto bad_res;
1265 }
1266
Johannes Berg133a3ff2011-11-03 14:50:13 +01001267 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1268 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1269 result = -EINVAL;
1270 goto bad_res;
1271 }
1272
Jouni Malinen31888482008-10-30 16:59:24 +02001273 nla_for_each_nested(nl_txq_params,
1274 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1275 rem_txq_params) {
1276 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1277 nla_data(nl_txq_params),
1278 nla_len(nl_txq_params),
1279 txq_params_policy);
1280 result = parse_txq_params(tb, &txq_params);
1281 if (result)
1282 goto bad_res;
1283
1284 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001285 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001286 &txq_params);
1287 if (result)
1288 goto bad_res;
1289 }
1290 }
1291
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001292 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001293 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001294 if (result)
1295 goto bad_res;
1296 }
1297
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001298 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1299 enum nl80211_tx_power_setting type;
1300 int idx, mbm = 0;
1301
1302 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001303 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001304 goto bad_res;
1305 }
1306
1307 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1308 type = nla_get_u32(info->attrs[idx]);
1309
1310 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1311 (type != NL80211_TX_POWER_AUTOMATIC)) {
1312 result = -EINVAL;
1313 goto bad_res;
1314 }
1315
1316 if (type != NL80211_TX_POWER_AUTOMATIC) {
1317 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1318 mbm = nla_get_u32(info->attrs[idx]);
1319 }
1320
1321 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1322 if (result)
1323 goto bad_res;
1324 }
1325
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001326 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1327 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1328 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001329 if ((!rdev->wiphy.available_antennas_tx &&
1330 !rdev->wiphy.available_antennas_rx) ||
1331 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001332 result = -EOPNOTSUPP;
1333 goto bad_res;
1334 }
1335
1336 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1337 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1338
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001339 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001340 * available antenna masks, except for the "all" mask */
1341 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1342 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001343 result = -EINVAL;
1344 goto bad_res;
1345 }
1346
Bruno Randolf7f531e02010-12-16 11:30:22 +09001347 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1348 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001349
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001350 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1351 if (result)
1352 goto bad_res;
1353 }
1354
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001355 changed = 0;
1356
1357 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1358 retry_short = nla_get_u8(
1359 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1360 if (retry_short == 0) {
1361 result = -EINVAL;
1362 goto bad_res;
1363 }
1364 changed |= WIPHY_PARAM_RETRY_SHORT;
1365 }
1366
1367 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1368 retry_long = nla_get_u8(
1369 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1370 if (retry_long == 0) {
1371 result = -EINVAL;
1372 goto bad_res;
1373 }
1374 changed |= WIPHY_PARAM_RETRY_LONG;
1375 }
1376
1377 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1378 frag_threshold = nla_get_u32(
1379 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1380 if (frag_threshold < 256) {
1381 result = -EINVAL;
1382 goto bad_res;
1383 }
1384 if (frag_threshold != (u32) -1) {
1385 /*
1386 * Fragments (apart from the last one) are required to
1387 * have even length. Make the fragmentation code
1388 * simpler by stripping LSB should someone try to use
1389 * odd threshold value.
1390 */
1391 frag_threshold &= ~0x1;
1392 }
1393 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1394 }
1395
1396 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1397 rts_threshold = nla_get_u32(
1398 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1399 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1400 }
1401
Lukáš Turek81077e82009-12-21 22:50:47 +01001402 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1403 coverage_class = nla_get_u8(
1404 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1405 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1406 }
1407
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001408 if (changed) {
1409 u8 old_retry_short, old_retry_long;
1410 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001411 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001412
1413 if (!rdev->ops->set_wiphy_params) {
1414 result = -EOPNOTSUPP;
1415 goto bad_res;
1416 }
1417
1418 old_retry_short = rdev->wiphy.retry_short;
1419 old_retry_long = rdev->wiphy.retry_long;
1420 old_frag_threshold = rdev->wiphy.frag_threshold;
1421 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001422 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001423
1424 if (changed & WIPHY_PARAM_RETRY_SHORT)
1425 rdev->wiphy.retry_short = retry_short;
1426 if (changed & WIPHY_PARAM_RETRY_LONG)
1427 rdev->wiphy.retry_long = retry_long;
1428 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1429 rdev->wiphy.frag_threshold = frag_threshold;
1430 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1431 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001432 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1433 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001434
1435 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1436 if (result) {
1437 rdev->wiphy.retry_short = old_retry_short;
1438 rdev->wiphy.retry_long = old_retry_long;
1439 rdev->wiphy.frag_threshold = old_frag_threshold;
1440 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001441 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001442 }
1443 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001444
Johannes Berg306d6112008-12-08 12:39:04 +01001445 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001446 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001447 if (netdev)
1448 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001449 return result;
1450}
1451
1452
1453static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001454 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001455 struct net_device *dev)
1456{
1457 void *hdr;
1458
1459 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1460 if (!hdr)
1461 return -1;
1462
1463 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001464 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001465 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001466 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001467
1468 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1469 rdev->devlist_generation ^
1470 (cfg80211_rdev_list_generation << 2));
1471
Johannes Berg55682962007-09-20 13:09:35 -04001472 return genlmsg_end(msg, hdr);
1473
1474 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001475 genlmsg_cancel(msg, hdr);
1476 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001477}
1478
1479static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1480{
1481 int wp_idx = 0;
1482 int if_idx = 0;
1483 int wp_start = cb->args[0];
1484 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001485 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001486 struct wireless_dev *wdev;
1487
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001488 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001489 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1490 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001491 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001492 if (wp_idx < wp_start) {
1493 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001494 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001495 }
Johannes Berg55682962007-09-20 13:09:35 -04001496 if_idx = 0;
1497
Johannes Bergf5ea9122009-08-07 16:17:38 +02001498 mutex_lock(&rdev->devlist_mtx);
1499 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001500 if (if_idx < if_start) {
1501 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001502 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001503 }
Johannes Berg55682962007-09-20 13:09:35 -04001504 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1505 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001506 rdev, wdev->netdev) < 0) {
1507 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001508 goto out;
1509 }
1510 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001511 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001512 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001513
1514 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001515 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001516 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001517 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001518
1519 cb->args[0] = wp_idx;
1520 cb->args[1] = if_idx;
1521
1522 return skb->len;
1523}
1524
1525static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1526{
1527 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001528 struct cfg80211_registered_device *dev = info->user_ptr[0];
1529 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001530
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001531 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001532 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001533 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001534
Johannes Bergd7264052009-04-19 16:23:20 +02001535 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001536 dev, netdev) < 0) {
1537 nlmsg_free(msg);
1538 return -ENOBUFS;
1539 }
Johannes Berg55682962007-09-20 13:09:35 -04001540
Johannes Berg134e6372009-07-10 09:51:34 +00001541 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001542}
1543
Michael Wu66f7ac52008-01-31 19:48:22 +01001544static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1545 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1546 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1547 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1548 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1549 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1550};
1551
1552static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1553{
1554 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1555 int flag;
1556
1557 *mntrflags = 0;
1558
1559 if (!nla)
1560 return -EINVAL;
1561
1562 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1563 nla, mntr_flags_policy))
1564 return -EINVAL;
1565
1566 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1567 if (flags[flag])
1568 *mntrflags |= (1<<flag);
1569
1570 return 0;
1571}
1572
Johannes Berg9bc383d2009-11-19 11:55:19 +01001573static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001574 struct net_device *netdev, u8 use_4addr,
1575 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001576{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001577 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001578 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001579 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001580 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001581 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001582
1583 switch (iftype) {
1584 case NL80211_IFTYPE_AP_VLAN:
1585 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1586 return 0;
1587 break;
1588 case NL80211_IFTYPE_STATION:
1589 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1590 return 0;
1591 break;
1592 default:
1593 break;
1594 }
1595
1596 return -EOPNOTSUPP;
1597}
1598
Johannes Berg55682962007-09-20 13:09:35 -04001599static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1600{
Johannes Berg4c476992010-10-04 21:36:35 +02001601 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001602 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001603 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001604 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001605 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001606 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001607 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001608
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001609 memset(&params, 0, sizeof(params));
1610
Johannes Berg04a773a2009-04-19 21:24:32 +02001611 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001612
Johannes Berg723b0382008-09-16 20:22:09 +02001613 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001614 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001615 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001616 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001617 if (ntype > NL80211_IFTYPE_MAX)
1618 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001619 }
1620
Johannes Berg92ffe052008-09-16 20:39:36 +02001621 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001622 struct wireless_dev *wdev = dev->ieee80211_ptr;
1623
Johannes Berg4c476992010-10-04 21:36:35 +02001624 if (ntype != NL80211_IFTYPE_MESH_POINT)
1625 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001626 if (netif_running(dev))
1627 return -EBUSY;
1628
1629 wdev_lock(wdev);
1630 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1631 IEEE80211_MAX_MESH_ID_LEN);
1632 wdev->mesh_id_up_len =
1633 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1634 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1635 wdev->mesh_id_up_len);
1636 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001637 }
1638
Felix Fietkau8b787642009-11-10 18:53:10 +01001639 if (info->attrs[NL80211_ATTR_4ADDR]) {
1640 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1641 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001642 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001643 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001644 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001645 } else {
1646 params.use_4addr = -1;
1647 }
1648
Johannes Berg92ffe052008-09-16 20:39:36 +02001649 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001650 if (ntype != NL80211_IFTYPE_MONITOR)
1651 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001652 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1653 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001654 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001655 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001656
1657 flags = &_flags;
1658 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001659 }
Johannes Berg3b858752009-03-12 09:55:09 +01001660
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001661 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001662 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001663 else
1664 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001665
Johannes Berg9bc383d2009-11-19 11:55:19 +01001666 if (!err && params.use_4addr != -1)
1667 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1668
Johannes Berg55682962007-09-20 13:09:35 -04001669 return err;
1670}
1671
1672static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1673{
Johannes Berg4c476992010-10-04 21:36:35 +02001674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001675 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001676 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001677 int err;
1678 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001679 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001680
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001681 memset(&params, 0, sizeof(params));
1682
Johannes Berg55682962007-09-20 13:09:35 -04001683 if (!info->attrs[NL80211_ATTR_IFNAME])
1684 return -EINVAL;
1685
1686 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1687 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1688 if (type > NL80211_IFTYPE_MAX)
1689 return -EINVAL;
1690 }
1691
Johannes Berg79c97e92009-07-07 03:56:12 +02001692 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001693 !(rdev->wiphy.interface_modes & (1 << type)))
1694 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001695
Johannes Berg9bc383d2009-11-19 11:55:19 +01001696 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001697 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001698 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001699 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001700 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001701 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001702
Michael Wu66f7ac52008-01-31 19:48:22 +01001703 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1704 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1705 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001706 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001707 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001708 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001709 if (IS_ERR(dev))
1710 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001711
Johannes Berg29cbe682010-12-03 09:20:44 +01001712 if (type == NL80211_IFTYPE_MESH_POINT &&
1713 info->attrs[NL80211_ATTR_MESH_ID]) {
1714 struct wireless_dev *wdev = dev->ieee80211_ptr;
1715
1716 wdev_lock(wdev);
1717 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1718 IEEE80211_MAX_MESH_ID_LEN);
1719 wdev->mesh_id_up_len =
1720 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1721 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1722 wdev->mesh_id_up_len);
1723 wdev_unlock(wdev);
1724 }
1725
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001726 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001727}
1728
1729static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1730{
Johannes Berg4c476992010-10-04 21:36:35 +02001731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1732 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001733
Johannes Berg4c476992010-10-04 21:36:35 +02001734 if (!rdev->ops->del_virtual_intf)
1735 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001736
Johannes Berg4c476992010-10-04 21:36:35 +02001737 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001738}
1739
Johannes Berg41ade002007-12-19 02:03:29 +01001740struct get_key_cookie {
1741 struct sk_buff *msg;
1742 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001743 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001744};
1745
1746static void get_key_callback(void *c, struct key_params *params)
1747{
Johannes Bergb9454e82009-07-08 13:29:08 +02001748 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001749 struct get_key_cookie *cookie = c;
1750
1751 if (params->key)
1752 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1753 params->key_len, params->key);
1754
1755 if (params->seq)
1756 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1757 params->seq_len, params->seq);
1758
1759 if (params->cipher)
1760 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1761 params->cipher);
1762
Johannes Bergb9454e82009-07-08 13:29:08 +02001763 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1764 if (!key)
1765 goto nla_put_failure;
1766
1767 if (params->key)
1768 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1769 params->key_len, params->key);
1770
1771 if (params->seq)
1772 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1773 params->seq_len, params->seq);
1774
1775 if (params->cipher)
1776 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1777 params->cipher);
1778
1779 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1780
1781 nla_nest_end(cookie->msg, key);
1782
Johannes Berg41ade002007-12-19 02:03:29 +01001783 return;
1784 nla_put_failure:
1785 cookie->error = 1;
1786}
1787
1788static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1789{
Johannes Berg4c476992010-10-04 21:36:35 +02001790 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001791 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001792 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001793 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001794 const u8 *mac_addr = NULL;
1795 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001796 struct get_key_cookie cookie = {
1797 .error = 0,
1798 };
1799 void *hdr;
1800 struct sk_buff *msg;
1801
1802 if (info->attrs[NL80211_ATTR_KEY_IDX])
1803 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1804
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001805 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001806 return -EINVAL;
1807
1808 if (info->attrs[NL80211_ATTR_MAC])
1809 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1810
Johannes Berge31b8212010-10-05 19:39:30 +02001811 pairwise = !!mac_addr;
1812 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1813 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1814 if (kt >= NUM_NL80211_KEYTYPES)
1815 return -EINVAL;
1816 if (kt != NL80211_KEYTYPE_GROUP &&
1817 kt != NL80211_KEYTYPE_PAIRWISE)
1818 return -EINVAL;
1819 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1820 }
1821
Johannes Berg4c476992010-10-04 21:36:35 +02001822 if (!rdev->ops->get_key)
1823 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001824
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001825 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001826 if (!msg)
1827 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001828
1829 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1830 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001831 if (IS_ERR(hdr))
1832 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001833
1834 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001835 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001836
1837 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1838 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1839 if (mac_addr)
1840 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1841
Johannes Berge31b8212010-10-05 19:39:30 +02001842 if (pairwise && mac_addr &&
1843 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1844 return -ENOENT;
1845
1846 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1847 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001848
1849 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001850 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001851
1852 if (cookie.error)
1853 goto nla_put_failure;
1854
1855 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001856 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001857
1858 nla_put_failure:
1859 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001860 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001861 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001862 return err;
1863}
1864
1865static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1866{
Johannes Berg4c476992010-10-04 21:36:35 +02001867 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001868 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001869 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001870 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001871
Johannes Bergb9454e82009-07-08 13:29:08 +02001872 err = nl80211_parse_key(info, &key);
1873 if (err)
1874 return err;
1875
1876 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001877 return -EINVAL;
1878
Johannes Bergb9454e82009-07-08 13:29:08 +02001879 /* only support setting default key */
1880 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001881 return -EINVAL;
1882
Johannes Bergfffd0932009-07-08 14:22:54 +02001883 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001884
1885 if (key.def) {
1886 if (!rdev->ops->set_default_key) {
1887 err = -EOPNOTSUPP;
1888 goto out;
1889 }
1890
1891 err = nl80211_key_allowed(dev->ieee80211_ptr);
1892 if (err)
1893 goto out;
1894
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001895 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1896 key.def_uni, key.def_multi);
1897
1898 if (err)
1899 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001900
Johannes Berg3d23e342009-09-29 23:27:28 +02001901#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001902 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001903#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001904 } else {
1905 if (key.def_uni || !key.def_multi) {
1906 err = -EINVAL;
1907 goto out;
1908 }
1909
1910 if (!rdev->ops->set_default_mgmt_key) {
1911 err = -EOPNOTSUPP;
1912 goto out;
1913 }
1914
1915 err = nl80211_key_allowed(dev->ieee80211_ptr);
1916 if (err)
1917 goto out;
1918
1919 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1920 dev, key.idx);
1921 if (err)
1922 goto out;
1923
1924#ifdef CONFIG_CFG80211_WEXT
1925 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1926#endif
1927 }
1928
1929 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001930 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001931
Johannes Berg41ade002007-12-19 02:03:29 +01001932 return err;
1933}
1934
1935static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1936{
Johannes Berg4c476992010-10-04 21:36:35 +02001937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001938 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001939 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001940 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001941 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001942
Johannes Bergb9454e82009-07-08 13:29:08 +02001943 err = nl80211_parse_key(info, &key);
1944 if (err)
1945 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001946
Johannes Bergb9454e82009-07-08 13:29:08 +02001947 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001948 return -EINVAL;
1949
Johannes Berg41ade002007-12-19 02:03:29 +01001950 if (info->attrs[NL80211_ATTR_MAC])
1951 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1952
Johannes Berge31b8212010-10-05 19:39:30 +02001953 if (key.type == -1) {
1954 if (mac_addr)
1955 key.type = NL80211_KEYTYPE_PAIRWISE;
1956 else
1957 key.type = NL80211_KEYTYPE_GROUP;
1958 }
1959
1960 /* for now */
1961 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1962 key.type != NL80211_KEYTYPE_GROUP)
1963 return -EINVAL;
1964
Johannes Berg4c476992010-10-04 21:36:35 +02001965 if (!rdev->ops->add_key)
1966 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001967
Johannes Berge31b8212010-10-05 19:39:30 +02001968 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1969 key.type == NL80211_KEYTYPE_PAIRWISE,
1970 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001971 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001972
1973 wdev_lock(dev->ieee80211_ptr);
1974 err = nl80211_key_allowed(dev->ieee80211_ptr);
1975 if (!err)
1976 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001977 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001978 mac_addr, &key.p);
1979 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001980
Johannes Berg41ade002007-12-19 02:03:29 +01001981 return err;
1982}
1983
1984static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1985{
Johannes Berg4c476992010-10-04 21:36:35 +02001986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001987 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001988 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001989 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001990 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001991
Johannes Bergb9454e82009-07-08 13:29:08 +02001992 err = nl80211_parse_key(info, &key);
1993 if (err)
1994 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001995
1996 if (info->attrs[NL80211_ATTR_MAC])
1997 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1998
Johannes Berge31b8212010-10-05 19:39:30 +02001999 if (key.type == -1) {
2000 if (mac_addr)
2001 key.type = NL80211_KEYTYPE_PAIRWISE;
2002 else
2003 key.type = NL80211_KEYTYPE_GROUP;
2004 }
2005
2006 /* for now */
2007 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2008 key.type != NL80211_KEYTYPE_GROUP)
2009 return -EINVAL;
2010
Johannes Berg4c476992010-10-04 21:36:35 +02002011 if (!rdev->ops->del_key)
2012 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002013
Johannes Bergfffd0932009-07-08 14:22:54 +02002014 wdev_lock(dev->ieee80211_ptr);
2015 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002016
2017 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2018 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2019 err = -ENOENT;
2020
Johannes Bergfffd0932009-07-08 14:22:54 +02002021 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002022 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2023 key.type == NL80211_KEYTYPE_PAIRWISE,
2024 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002025
Johannes Berg3d23e342009-09-29 23:27:28 +02002026#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002027 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002028 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002029 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002030 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002031 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2032 }
2033#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002034 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002035
Johannes Berg41ade002007-12-19 02:03:29 +01002036 return err;
2037}
2038
Johannes Berged1b6cc2007-12-19 02:03:32 +01002039static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2040{
2041 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2042 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2044 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002045 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002046 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002047 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002048
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002049 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2050 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2051 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2052 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002053 return -EINVAL;
2054
Johannes Berg074ac8d2010-09-16 14:58:22 +02002055 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002056 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2057 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002058
Johannes Berg56d18932011-05-09 18:41:15 +02002059 memset(&params, 0, sizeof(params));
2060
Johannes Berged1b6cc2007-12-19 02:03:32 +01002061 switch (info->genlhdr->cmd) {
2062 case NL80211_CMD_NEW_BEACON:
2063 /* these are required for NEW_BEACON */
2064 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2065 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002066 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2067 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002068
Johannes Berg56d18932011-05-09 18:41:15 +02002069 params.interval =
2070 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2071 params.dtim_period =
2072 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2073
2074 err = cfg80211_validate_beacon_int(rdev, params.interval);
2075 if (err)
2076 return err;
2077
Jouni Malinen32e9de82011-08-10 23:53:31 +03002078 /*
2079 * In theory, some of these attributes could be required for
2080 * NEW_BEACON, but since they were not used when the command was
2081 * originally added, keep them optional for old user space
2082 * programs to work with drivers that do not need the additional
2083 * information.
2084 */
2085 if (info->attrs[NL80211_ATTR_SSID]) {
2086 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2087 params.ssid_len =
2088 nla_len(info->attrs[NL80211_ATTR_SSID]);
2089 if (params.ssid_len == 0 ||
2090 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2091 return -EINVAL;
2092 }
2093
2094 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2095 params.hidden_ssid = nla_get_u32(
2096 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2097 if (params.hidden_ssid !=
2098 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2099 params.hidden_ssid !=
2100 NL80211_HIDDEN_SSID_ZERO_LEN &&
2101 params.hidden_ssid !=
2102 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2103 return -EINVAL;
2104 }
2105
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002106 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2107
2108 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2109 params.auth_type = nla_get_u32(
2110 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2111 if (!nl80211_valid_auth_type(params.auth_type))
2112 return -EINVAL;
2113 } else
2114 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2115
2116 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2117 NL80211_MAX_NR_CIPHER_SUITES);
2118 if (err)
2119 return err;
2120
Johannes Berg79c97e92009-07-07 03:56:12 +02002121 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002122 break;
2123 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002124 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002125 break;
2126 default:
2127 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002128 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002129 }
2130
Johannes Berg4c476992010-10-04 21:36:35 +02002131 if (!call)
2132 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002133
Johannes Berged1b6cc2007-12-19 02:03:32 +01002134 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2135 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2136 params.head_len =
2137 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2138 haveinfo = 1;
2139 }
2140
2141 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2142 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2143 params.tail_len =
2144 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2145 haveinfo = 1;
2146 }
2147
Johannes Berg4c476992010-10-04 21:36:35 +02002148 if (!haveinfo)
2149 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002150
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002151 if (info->attrs[NL80211_ATTR_IE]) {
2152 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2153 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2154 }
2155
2156 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2157 params.proberesp_ies =
2158 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2159 params.proberesp_ies_len =
2160 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2161 }
2162
2163 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2164 params.assocresp_ies =
2165 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2166 params.assocresp_ies_len =
2167 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2168 }
2169
Johannes Berg56d18932011-05-09 18:41:15 +02002170 err = call(&rdev->wiphy, dev, &params);
2171 if (!err && params.interval)
2172 wdev->beacon_interval = params.interval;
2173 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002174}
2175
2176static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2177{
Johannes Berg4c476992010-10-04 21:36:35 +02002178 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2179 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002180 struct wireless_dev *wdev = dev->ieee80211_ptr;
2181 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002182
Johannes Berg4c476992010-10-04 21:36:35 +02002183 if (!rdev->ops->del_beacon)
2184 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002185
Johannes Berg074ac8d2010-09-16 14:58:22 +02002186 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002187 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2188 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002189
Johannes Berg56d18932011-05-09 18:41:15 +02002190 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2191 if (!err)
2192 wdev->beacon_interval = 0;
2193 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002194}
2195
Johannes Berg5727ef12007-12-19 02:03:34 +01002196static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2197 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2198 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2199 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002200 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002201 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002202};
2203
Johannes Bergeccb8e82009-05-11 21:57:56 +03002204static int parse_station_flags(struct genl_info *info,
2205 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002206{
2207 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002208 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002209 int flag;
2210
Johannes Bergeccb8e82009-05-11 21:57:56 +03002211 /*
2212 * Try parsing the new attribute first so userspace
2213 * can specify both for older kernels.
2214 */
2215 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2216 if (nla) {
2217 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002218
Johannes Bergeccb8e82009-05-11 21:57:56 +03002219 sta_flags = nla_data(nla);
2220 params->sta_flags_mask = sta_flags->mask;
2221 params->sta_flags_set = sta_flags->set;
2222 if ((params->sta_flags_mask |
2223 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2224 return -EINVAL;
2225 return 0;
2226 }
2227
2228 /* if present, parse the old attribute */
2229
2230 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002231 if (!nla)
2232 return 0;
2233
2234 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2235 nla, sta_flags_policy))
2236 return -EINVAL;
2237
Johannes Bergeccb8e82009-05-11 21:57:56 +03002238 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2239 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002240
2241 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2242 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002243 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002244
2245 return 0;
2246}
2247
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002248static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2249 int attr)
2250{
2251 struct nlattr *rate;
2252 u16 bitrate;
2253
2254 rate = nla_nest_start(msg, attr);
2255 if (!rate)
2256 goto nla_put_failure;
2257
2258 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2259 bitrate = cfg80211_calculate_bitrate(info);
2260 if (bitrate > 0)
2261 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2262
2263 if (info->flags & RATE_INFO_FLAGS_MCS)
2264 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2265 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2266 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2267 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2268 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2269
2270 nla_nest_end(msg, rate);
2271 return true;
2272
2273nla_put_failure:
2274 return false;
2275}
2276
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002277static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2278 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002279 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002280{
2281 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002282 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002283
2284 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2285 if (!hdr)
2286 return -1;
2287
2288 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2289 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2290
Johannes Bergf5ea9122009-08-07 16:17:38 +02002291 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2292
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002293 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2294 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002295 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302296 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2297 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2298 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002299 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2300 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2301 sinfo->inactive_time);
2302 if (sinfo->filled & STATION_INFO_RX_BYTES)
2303 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2304 sinfo->rx_bytes);
2305 if (sinfo->filled & STATION_INFO_TX_BYTES)
2306 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2307 sinfo->tx_bytes);
2308 if (sinfo->filled & STATION_INFO_LLID)
2309 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2310 sinfo->llid);
2311 if (sinfo->filled & STATION_INFO_PLID)
2312 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2313 sinfo->plid);
2314 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2315 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2316 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002317 if (sinfo->filled & STATION_INFO_SIGNAL)
2318 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2319 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002320 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2321 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2322 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002323 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002324 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2325 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002326 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002327 }
2328 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2329 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2330 NL80211_STA_INFO_RX_BITRATE))
2331 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002332 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002333 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2334 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2335 sinfo->rx_packets);
2336 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2337 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2338 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002339 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2340 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2341 sinfo->tx_retries);
2342 if (sinfo->filled & STATION_INFO_TX_FAILED)
2343 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2344 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002345 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2346 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2347 if (!bss_param)
2348 goto nla_put_failure;
2349
2350 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2351 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2352 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2353 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2354 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2355 NLA_PUT_FLAG(msg,
2356 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2357 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2358 sinfo->bss_param.dtim_period);
2359 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2360 sinfo->bss_param.beacon_interval);
2361
2362 nla_nest_end(msg, bss_param);
2363 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002364 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2365 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2366 sizeof(struct nl80211_sta_flag_update),
2367 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002368 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002369
Felix Fietkau040bdf72011-08-10 19:00:33 -06002370 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002371 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2372 sinfo->assoc_req_ies);
2373
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002374 return genlmsg_end(msg, hdr);
2375
2376 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002377 genlmsg_cancel(msg, hdr);
2378 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002379}
2380
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002381static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002382 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002383{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384 struct station_info sinfo;
2385 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002386 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002388 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002389 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002390
Johannes Berg67748892010-10-04 21:14:06 +02002391 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2392 if (err)
2393 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002394
2395 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002396 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002397 goto out_err;
2398 }
2399
Johannes Bergbba95fe2008-07-29 13:22:51 +02002400 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002401 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002402 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2403 mac_addr, &sinfo);
2404 if (err == -ENOENT)
2405 break;
2406 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002407 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002408
2409 if (nl80211_send_station(skb,
2410 NETLINK_CB(cb->skb).pid,
2411 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2412 netdev, mac_addr,
2413 &sinfo) < 0)
2414 goto out;
2415
2416 sta_idx++;
2417 }
2418
2419
2420 out:
2421 cb->args[1] = sta_idx;
2422 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002423 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002424 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002425
2426 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002427}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002428
Johannes Berg5727ef12007-12-19 02:03:34 +01002429static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2430{
Johannes Berg4c476992010-10-04 21:36:35 +02002431 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2432 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002433 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002434 struct sk_buff *msg;
2435 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002436 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002437
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002438 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002439
2440 if (!info->attrs[NL80211_ATTR_MAC])
2441 return -EINVAL;
2442
2443 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2444
Johannes Berg4c476992010-10-04 21:36:35 +02002445 if (!rdev->ops->get_station)
2446 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002447
Johannes Berg79c97e92009-07-07 03:56:12 +02002448 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002449 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002450 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002451
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002453 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002454 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002455
2456 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002457 dev, mac_addr, &sinfo) < 0) {
2458 nlmsg_free(msg);
2459 return -ENOBUFS;
2460 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002463}
2464
2465/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002466 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002467 */
Johannes Berg463d0182009-07-14 00:33:35 +02002468static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002469 struct cfg80211_registered_device *rdev,
2470 struct net_device **vlan)
2471{
Johannes Berg463d0182009-07-14 00:33:35 +02002472 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002473 *vlan = NULL;
2474
2475 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002476 *vlan = dev_get_by_index(genl_info_net(info),
2477 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002478 if (!*vlan)
2479 return -ENODEV;
2480 if (!(*vlan)->ieee80211_ptr)
2481 return -EINVAL;
2482 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2483 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002484 if (!netif_running(*vlan))
2485 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002486 }
2487 return 0;
2488}
2489
2490static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2491{
Johannes Berg4c476992010-10-04 21:36:35 +02002492 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002493 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002494 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002495 struct station_parameters params;
2496 u8 *mac_addr = NULL;
2497
2498 memset(&params, 0, sizeof(params));
2499
2500 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002501 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002502
2503 if (info->attrs[NL80211_ATTR_STA_AID])
2504 return -EINVAL;
2505
2506 if (!info->attrs[NL80211_ATTR_MAC])
2507 return -EINVAL;
2508
2509 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2510
2511 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2512 params.supported_rates =
2513 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2514 params.supported_rates_len =
2515 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2516 }
2517
2518 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2519 params.listen_interval =
2520 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2521
Jouni Malinen36aedc92008-08-25 11:58:58 +03002522 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2523 params.ht_capa =
2524 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2525
Johannes Bergeccb8e82009-05-11 21:57:56 +03002526 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002527 return -EINVAL;
2528
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002529 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2530 params.plink_action =
2531 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2532
Javier Cardona9c3990a2011-05-03 16:57:11 -07002533 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2534 params.plink_state =
2535 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2536
Johannes Berg463d0182009-07-14 00:33:35 +02002537 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002538 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002539 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002540
2541 /* validate settings */
2542 err = 0;
2543
2544 switch (dev->ieee80211_ptr->iftype) {
2545 case NL80211_IFTYPE_AP:
2546 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002547 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002548 /* disallow mesh-specific things */
2549 if (params.plink_action)
2550 err = -EINVAL;
2551 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002552 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002553 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002554 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002555 if (params.plink_action)
2556 err = -EINVAL;
2557 if (params.vlan)
2558 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002559 if (params.supported_rates &&
2560 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002561 err = -EINVAL;
2562 if (params.ht_capa)
2563 err = -EINVAL;
2564 if (params.listen_interval >= 0)
2565 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002566 if (params.sta_flags_mask &
2567 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2568 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2569 err = -EINVAL;
2570 /* can't change the TDLS bit */
2571 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2572 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002573 err = -EINVAL;
2574 break;
2575 case NL80211_IFTYPE_MESH_POINT:
2576 /* disallow things mesh doesn't support */
2577 if (params.vlan)
2578 err = -EINVAL;
2579 if (params.ht_capa)
2580 err = -EINVAL;
2581 if (params.listen_interval >= 0)
2582 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002583 if (params.sta_flags_mask &
2584 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002585 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002586 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002587 err = -EINVAL;
2588 break;
2589 default:
2590 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002591 }
2592
Johannes Berg5727ef12007-12-19 02:03:34 +01002593 if (err)
2594 goto out;
2595
Johannes Berg79c97e92009-07-07 03:56:12 +02002596 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002597 err = -EOPNOTSUPP;
2598 goto out;
2599 }
2600
Johannes Berg79c97e92009-07-07 03:56:12 +02002601 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002602
2603 out:
2604 if (params.vlan)
2605 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002606
Johannes Berg5727ef12007-12-19 02:03:34 +01002607 return err;
2608}
2609
Eliad Pellerc75786c2011-08-23 14:37:46 +03002610static struct nla_policy
2611nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2612 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2613 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2614};
2615
Johannes Berg5727ef12007-12-19 02:03:34 +01002616static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2617{
Johannes Berg4c476992010-10-04 21:36:35 +02002618 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002619 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002620 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002621 struct station_parameters params;
2622 u8 *mac_addr = NULL;
2623
2624 memset(&params, 0, sizeof(params));
2625
2626 if (!info->attrs[NL80211_ATTR_MAC])
2627 return -EINVAL;
2628
Johannes Berg5727ef12007-12-19 02:03:34 +01002629 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2630 return -EINVAL;
2631
2632 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2633 return -EINVAL;
2634
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002635 if (!info->attrs[NL80211_ATTR_STA_AID])
2636 return -EINVAL;
2637
Johannes Berg5727ef12007-12-19 02:03:34 +01002638 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2639 params.supported_rates =
2640 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2641 params.supported_rates_len =
2642 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2643 params.listen_interval =
2644 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002645
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002646 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2647 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2648 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002649
Jouni Malinen36aedc92008-08-25 11:58:58 +03002650 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2651 params.ht_capa =
2652 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002653
Javier Cardona96b78df2011-04-07 15:08:33 -07002654 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2655 params.plink_action =
2656 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2657
Johannes Bergeccb8e82009-05-11 21:57:56 +03002658 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002659 return -EINVAL;
2660
Eliad Pellerc75786c2011-08-23 14:37:46 +03002661 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002662 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002663 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002664 info->attrs[NL80211_ATTR_STA_WME]) {
2665 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2666 struct nlattr *nla;
2667
2668 nla = info->attrs[NL80211_ATTR_STA_WME];
2669 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2670 nl80211_sta_wme_policy);
2671 if (err)
2672 return err;
2673
2674 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2675 params.uapsd_queues =
2676 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002677 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2678 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002679
2680 if (tb[NL80211_STA_WME_MAX_SP])
2681 params.max_sp =
2682 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002683
2684 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2685 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002686
2687 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002688 }
2689
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002690 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002691 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002692 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002693 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2694 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2695 return -EINVAL;
2696
2697 /*
2698 * Only managed stations can add TDLS peers, and only when the
2699 * wiphy supports external TDLS setup.
2700 */
2701 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2702 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2703 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2704 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002705 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002706
Johannes Berg463d0182009-07-14 00:33:35 +02002707 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002708 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002709 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002710
2711 /* validate settings */
2712 err = 0;
2713
Johannes Berg79c97e92009-07-07 03:56:12 +02002714 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002715 err = -EOPNOTSUPP;
2716 goto out;
2717 }
2718
Johannes Berg79c97e92009-07-07 03:56:12 +02002719 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002720
2721 out:
2722 if (params.vlan)
2723 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002724 return err;
2725}
2726
2727static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2728{
Johannes Berg4c476992010-10-04 21:36:35 +02002729 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2730 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002731 u8 *mac_addr = NULL;
2732
2733 if (info->attrs[NL80211_ATTR_MAC])
2734 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2735
Johannes Berge80cf852009-05-11 14:43:13 +02002736 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002737 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002738 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002739 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2740 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002741
Johannes Berg4c476992010-10-04 21:36:35 +02002742 if (!rdev->ops->del_station)
2743 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002744
Johannes Berg4c476992010-10-04 21:36:35 +02002745 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002746}
2747
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002748static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2749 int flags, struct net_device *dev,
2750 u8 *dst, u8 *next_hop,
2751 struct mpath_info *pinfo)
2752{
2753 void *hdr;
2754 struct nlattr *pinfoattr;
2755
2756 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2757 if (!hdr)
2758 return -1;
2759
2760 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2761 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2762 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2763
Johannes Bergf5ea9122009-08-07 16:17:38 +02002764 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2765
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002766 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2767 if (!pinfoattr)
2768 goto nla_put_failure;
2769 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2770 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2771 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002772 if (pinfo->filled & MPATH_INFO_SN)
2773 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2774 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002775 if (pinfo->filled & MPATH_INFO_METRIC)
2776 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2777 pinfo->metric);
2778 if (pinfo->filled & MPATH_INFO_EXPTIME)
2779 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2780 pinfo->exptime);
2781 if (pinfo->filled & MPATH_INFO_FLAGS)
2782 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2783 pinfo->flags);
2784 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2785 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2786 pinfo->discovery_timeout);
2787 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2788 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2789 pinfo->discovery_retries);
2790
2791 nla_nest_end(msg, pinfoattr);
2792
2793 return genlmsg_end(msg, hdr);
2794
2795 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002796 genlmsg_cancel(msg, hdr);
2797 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002798}
2799
2800static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002801 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002802{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002803 struct mpath_info pinfo;
2804 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002805 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002806 u8 dst[ETH_ALEN];
2807 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002808 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002809 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002810
Johannes Berg67748892010-10-04 21:14:06 +02002811 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2812 if (err)
2813 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002814
2815 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002816 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002817 goto out_err;
2818 }
2819
Jouni Malineneec60b02009-03-20 21:21:19 +02002820 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2821 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002822 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002823 }
2824
Johannes Bergbba95fe2008-07-29 13:22:51 +02002825 while (1) {
2826 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2827 dst, next_hop, &pinfo);
2828 if (err == -ENOENT)
2829 break;
2830 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002831 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002832
2833 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2834 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2835 netdev, dst, next_hop,
2836 &pinfo) < 0)
2837 goto out;
2838
2839 path_idx++;
2840 }
2841
2842
2843 out:
2844 cb->args[1] = path_idx;
2845 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002846 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002847 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002848 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002849}
2850
2851static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2852{
Johannes Berg4c476992010-10-04 21:36:35 +02002853 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002854 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002855 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002856 struct mpath_info pinfo;
2857 struct sk_buff *msg;
2858 u8 *dst = NULL;
2859 u8 next_hop[ETH_ALEN];
2860
2861 memset(&pinfo, 0, sizeof(pinfo));
2862
2863 if (!info->attrs[NL80211_ATTR_MAC])
2864 return -EINVAL;
2865
2866 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2867
Johannes Berg4c476992010-10-04 21:36:35 +02002868 if (!rdev->ops->get_mpath)
2869 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002870
Johannes Berg4c476992010-10-04 21:36:35 +02002871 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2872 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002873
Johannes Berg79c97e92009-07-07 03:56:12 +02002874 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002875 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002876 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002877
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002879 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002880 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002881
2882 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002883 dev, dst, next_hop, &pinfo) < 0) {
2884 nlmsg_free(msg);
2885 return -ENOBUFS;
2886 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002887
Johannes Berg4c476992010-10-04 21:36:35 +02002888 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002889}
2890
2891static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2892{
Johannes Berg4c476992010-10-04 21:36:35 +02002893 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2894 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002895 u8 *dst = NULL;
2896 u8 *next_hop = NULL;
2897
2898 if (!info->attrs[NL80211_ATTR_MAC])
2899 return -EINVAL;
2900
2901 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2902 return -EINVAL;
2903
2904 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2905 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2906
Johannes Berg4c476992010-10-04 21:36:35 +02002907 if (!rdev->ops->change_mpath)
2908 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002909
Johannes Berg4c476992010-10-04 21:36:35 +02002910 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2911 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002912
Johannes Berg4c476992010-10-04 21:36:35 +02002913 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002914}
Johannes Berg4c476992010-10-04 21:36:35 +02002915
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002916static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2917{
Johannes Berg4c476992010-10-04 21:36:35 +02002918 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2919 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002920 u8 *dst = NULL;
2921 u8 *next_hop = NULL;
2922
2923 if (!info->attrs[NL80211_ATTR_MAC])
2924 return -EINVAL;
2925
2926 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2927 return -EINVAL;
2928
2929 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2930 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2931
Johannes Berg4c476992010-10-04 21:36:35 +02002932 if (!rdev->ops->add_mpath)
2933 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002934
Johannes Berg4c476992010-10-04 21:36:35 +02002935 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2936 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002937
Johannes Berg4c476992010-10-04 21:36:35 +02002938 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002939}
2940
2941static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2942{
Johannes Berg4c476992010-10-04 21:36:35 +02002943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2944 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002945 u8 *dst = NULL;
2946
2947 if (info->attrs[NL80211_ATTR_MAC])
2948 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2949
Johannes Berg4c476992010-10-04 21:36:35 +02002950 if (!rdev->ops->del_mpath)
2951 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002952
Johannes Berg4c476992010-10-04 21:36:35 +02002953 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002954}
2955
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002956static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2957{
Johannes Berg4c476992010-10-04 21:36:35 +02002958 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2959 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002960 struct bss_parameters params;
2961
2962 memset(&params, 0, sizeof(params));
2963 /* default to not changing parameters */
2964 params.use_cts_prot = -1;
2965 params.use_short_preamble = -1;
2966 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002967 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002968 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002969
2970 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2971 params.use_cts_prot =
2972 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2973 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2974 params.use_short_preamble =
2975 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2976 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2977 params.use_short_slot_time =
2978 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002979 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2980 params.basic_rates =
2981 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2982 params.basic_rates_len =
2983 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2984 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002985 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2986 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002987 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2988 params.ht_opmode =
2989 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002990
Johannes Berg4c476992010-10-04 21:36:35 +02002991 if (!rdev->ops->change_bss)
2992 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002993
Johannes Berg074ac8d2010-09-16 14:58:22 +02002994 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002995 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2996 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002997
Johannes Berg4c476992010-10-04 21:36:35 +02002998 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002999}
3000
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003001static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003002 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3003 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3004 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3005 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3006 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3007 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3008};
3009
3010static int parse_reg_rule(struct nlattr *tb[],
3011 struct ieee80211_reg_rule *reg_rule)
3012{
3013 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3014 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3015
3016 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3017 return -EINVAL;
3018 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3019 return -EINVAL;
3020 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3021 return -EINVAL;
3022 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3023 return -EINVAL;
3024 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3025 return -EINVAL;
3026
3027 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3028
3029 freq_range->start_freq_khz =
3030 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3031 freq_range->end_freq_khz =
3032 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3033 freq_range->max_bandwidth_khz =
3034 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3035
3036 power_rule->max_eirp =
3037 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3038
3039 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3040 power_rule->max_antenna_gain =
3041 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3042
3043 return 0;
3044}
3045
3046static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3047{
3048 int r;
3049 char *data = NULL;
3050
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003051 /*
3052 * You should only get this when cfg80211 hasn't yet initialized
3053 * completely when built-in to the kernel right between the time
3054 * window between nl80211_init() and regulatory_init(), if that is
3055 * even possible.
3056 */
3057 mutex_lock(&cfg80211_mutex);
3058 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003059 mutex_unlock(&cfg80211_mutex);
3060 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003061 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003062 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003063
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003064 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3065 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003066
3067 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3068
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003069 r = regulatory_hint_user(data);
3070
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003071 return r;
3072}
3073
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003074static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003075 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003076{
Johannes Berg4c476992010-10-04 21:36:35 +02003077 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003078 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003079 struct wireless_dev *wdev = dev->ieee80211_ptr;
3080 struct mesh_config cur_params;
3081 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003082 void *hdr;
3083 struct nlattr *pinfoattr;
3084 struct sk_buff *msg;
3085
Johannes Berg29cbe682010-12-03 09:20:44 +01003086 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3087 return -EOPNOTSUPP;
3088
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003089 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003090 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003091
Johannes Berg29cbe682010-12-03 09:20:44 +01003092 wdev_lock(wdev);
3093 /* If not connected, get default parameters */
3094 if (!wdev->mesh_id_len)
3095 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3096 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003097 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003098 &cur_params);
3099 wdev_unlock(wdev);
3100
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003101 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003102 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003103
3104 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003105 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003106 if (!msg)
3107 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003108 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003109 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003110 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003111 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003112 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003113 if (!pinfoattr)
3114 goto nla_put_failure;
3115 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3116 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3117 cur_params.dot11MeshRetryTimeout);
3118 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3119 cur_params.dot11MeshConfirmTimeout);
3120 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3121 cur_params.dot11MeshHoldingTimeout);
3122 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3123 cur_params.dot11MeshMaxPeerLinks);
3124 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3125 cur_params.dot11MeshMaxRetries);
3126 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3127 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003128 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3129 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003130 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3131 cur_params.auto_open_plinks);
3132 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3133 cur_params.dot11MeshHWMPmaxPREQretries);
3134 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3135 cur_params.path_refresh_time);
3136 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3137 cur_params.min_discovery_timeout);
3138 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3139 cur_params.dot11MeshHWMPactivePathTimeout);
3140 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3141 cur_params.dot11MeshHWMPpreqMinInterval);
3142 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3143 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003144 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3145 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003146 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3147 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003148 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3149 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003150 nla_nest_end(msg, pinfoattr);
3151 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003152 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003153
Johannes Berg3b858752009-03-12 09:55:09 +01003154 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003155 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003156 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003157 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003158 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003159}
3160
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003161static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003162 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3163 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3164 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3165 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3166 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3167 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003168 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003169 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3170
3171 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3172 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3173 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3174 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3175 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3176 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003177 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003178 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003179 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003180};
3181
Javier Cardonac80d5452010-12-16 17:37:49 -08003182static const struct nla_policy
3183 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3184 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3185 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003186 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003187 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003188 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003189 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003190};
3191
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003192static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003193 struct mesh_config *cfg,
3194 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003195{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003196 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003197 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003198
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003199#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3200do {\
3201 if (table[attr_num]) {\
3202 cfg->param = nla_fn(table[attr_num]); \
3203 mask |= (1 << (attr_num - 1)); \
3204 } \
3205} while (0);\
3206
3207
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003208 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003209 return -EINVAL;
3210 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003211 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003212 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003213 return -EINVAL;
3214
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003215 /* This makes sure that there aren't more than 32 mesh config
3216 * parameters (otherwise our bitfield scheme would not work.) */
3217 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3218
3219 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003220 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3221 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3222 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3223 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3224 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3225 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3226 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3227 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3228 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3229 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3230 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3231 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003232 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3233 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003234 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3235 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3236 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3237 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3238 nla_get_u8);
3239 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3240 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3241 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3242 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3243 nla_get_u16);
3244 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3245 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3246 nla_get_u32);
3247 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3248 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3249 nla_get_u16);
3250 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3251 dot11MeshHWMPnetDiameterTraversalTime,
3252 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3253 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003254 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3255 dot11MeshHWMPRootMode, mask,
3256 NL80211_MESHCONF_HWMP_ROOTMODE,
3257 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003258 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3259 dot11MeshHWMPRannInterval, mask,
3260 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3261 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003262 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3263 dot11MeshGateAnnouncementProtocol, mask,
3264 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3265 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003266 if (mask_out)
3267 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003268
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003269 return 0;
3270
3271#undef FILL_IN_MESH_PARAM_IF_SET
3272}
3273
Javier Cardonac80d5452010-12-16 17:37:49 -08003274static int nl80211_parse_mesh_setup(struct genl_info *info,
3275 struct mesh_setup *setup)
3276{
3277 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3278
3279 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3280 return -EINVAL;
3281 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3282 info->attrs[NL80211_ATTR_MESH_SETUP],
3283 nl80211_mesh_setup_params_policy))
3284 return -EINVAL;
3285
3286 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3287 setup->path_sel_proto =
3288 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3289 IEEE80211_PATH_PROTOCOL_VENDOR :
3290 IEEE80211_PATH_PROTOCOL_HWMP;
3291
3292 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3293 setup->path_metric =
3294 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3295 IEEE80211_PATH_METRIC_VENDOR :
3296 IEEE80211_PATH_METRIC_AIRTIME;
3297
Javier Cardona581a8b02011-04-07 15:08:27 -07003298
3299 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003300 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003301 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003302 if (!is_valid_ie_attr(ieattr))
3303 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003304 setup->ie = nla_data(ieattr);
3305 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003306 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003307 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3308 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003309
3310 return 0;
3311}
3312
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003313static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003314 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003315{
3316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3317 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003318 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003319 struct mesh_config cfg;
3320 u32 mask;
3321 int err;
3322
Johannes Berg29cbe682010-12-03 09:20:44 +01003323 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3324 return -EOPNOTSUPP;
3325
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003326 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003327 return -EOPNOTSUPP;
3328
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003329 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003330 if (err)
3331 return err;
3332
Johannes Berg29cbe682010-12-03 09:20:44 +01003333 wdev_lock(wdev);
3334 if (!wdev->mesh_id_len)
3335 err = -ENOLINK;
3336
3337 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003338 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003339 mask, &cfg);
3340
3341 wdev_unlock(wdev);
3342
3343 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003344}
3345
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003346static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3347{
3348 struct sk_buff *msg;
3349 void *hdr = NULL;
3350 struct nlattr *nl_reg_rules;
3351 unsigned int i;
3352 int err = -EINVAL;
3353
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003354 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003355
3356 if (!cfg80211_regdomain)
3357 goto out;
3358
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003360 if (!msg) {
3361 err = -ENOBUFS;
3362 goto out;
3363 }
3364
3365 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3366 NL80211_CMD_GET_REG);
3367 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003368 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003369
3370 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3371 cfg80211_regdomain->alpha2);
3372
3373 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3374 if (!nl_reg_rules)
3375 goto nla_put_failure;
3376
3377 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3378 struct nlattr *nl_reg_rule;
3379 const struct ieee80211_reg_rule *reg_rule;
3380 const struct ieee80211_freq_range *freq_range;
3381 const struct ieee80211_power_rule *power_rule;
3382
3383 reg_rule = &cfg80211_regdomain->reg_rules[i];
3384 freq_range = &reg_rule->freq_range;
3385 power_rule = &reg_rule->power_rule;
3386
3387 nl_reg_rule = nla_nest_start(msg, i);
3388 if (!nl_reg_rule)
3389 goto nla_put_failure;
3390
3391 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3392 reg_rule->flags);
3393 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3394 freq_range->start_freq_khz);
3395 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3396 freq_range->end_freq_khz);
3397 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3398 freq_range->max_bandwidth_khz);
3399 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3400 power_rule->max_antenna_gain);
3401 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3402 power_rule->max_eirp);
3403
3404 nla_nest_end(msg, nl_reg_rule);
3405 }
3406
3407 nla_nest_end(msg, nl_reg_rules);
3408
3409 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003410 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003411 goto out;
3412
3413nla_put_failure:
3414 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003415put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003416 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003417 err = -EMSGSIZE;
3418out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003419 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003420 return err;
3421}
3422
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003423static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3424{
3425 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3426 struct nlattr *nl_reg_rule;
3427 char *alpha2 = NULL;
3428 int rem_reg_rules = 0, r = 0;
3429 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3430 struct ieee80211_regdomain *rd = NULL;
3431
3432 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3433 return -EINVAL;
3434
3435 if (!info->attrs[NL80211_ATTR_REG_RULES])
3436 return -EINVAL;
3437
3438 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3439
3440 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3441 rem_reg_rules) {
3442 num_rules++;
3443 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003444 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003445 }
3446
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003447 mutex_lock(&cfg80211_mutex);
3448
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003449 if (!reg_is_valid_request(alpha2)) {
3450 r = -EINVAL;
3451 goto bad_reg;
3452 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003453
3454 size_of_regd = sizeof(struct ieee80211_regdomain) +
3455 (num_rules * sizeof(struct ieee80211_reg_rule));
3456
3457 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003458 if (!rd) {
3459 r = -ENOMEM;
3460 goto bad_reg;
3461 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003462
3463 rd->n_reg_rules = num_rules;
3464 rd->alpha2[0] = alpha2[0];
3465 rd->alpha2[1] = alpha2[1];
3466
3467 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3468 rem_reg_rules) {
3469 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3470 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3471 reg_rule_policy);
3472 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3473 if (r)
3474 goto bad_reg;
3475
3476 rule_idx++;
3477
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003478 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3479 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003480 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003481 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003482 }
3483
3484 BUG_ON(rule_idx != num_rules);
3485
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003486 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003487
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003488 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003489
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003490 return r;
3491
Johannes Bergd2372b32008-10-24 20:32:20 +02003492 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003493 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003494 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003495 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003496}
3497
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003498static int validate_scan_freqs(struct nlattr *freqs)
3499{
3500 struct nlattr *attr1, *attr2;
3501 int n_channels = 0, tmp1, tmp2;
3502
3503 nla_for_each_nested(attr1, freqs, tmp1) {
3504 n_channels++;
3505 /*
3506 * Some hardware has a limited channel list for
3507 * scanning, and it is pretty much nonsensical
3508 * to scan for a channel twice, so disallow that
3509 * and don't require drivers to check that the
3510 * channel list they get isn't longer than what
3511 * they can scan, as long as they can scan all
3512 * the channels they registered at once.
3513 */
3514 nla_for_each_nested(attr2, freqs, tmp2)
3515 if (attr1 != attr2 &&
3516 nla_get_u32(attr1) == nla_get_u32(attr2))
3517 return 0;
3518 }
3519
3520 return n_channels;
3521}
3522
Johannes Berg2a519312009-02-10 21:25:55 +01003523static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3524{
Johannes Berg4c476992010-10-04 21:36:35 +02003525 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3526 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003527 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003528 struct nlattr *attr;
3529 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003530 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003531 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003532
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003533 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3534 return -EINVAL;
3535
Johannes Berg79c97e92009-07-07 03:56:12 +02003536 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003537
Johannes Berg4c476992010-10-04 21:36:35 +02003538 if (!rdev->ops->scan)
3539 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003540
Johannes Berg4c476992010-10-04 21:36:35 +02003541 if (rdev->scan_req)
3542 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003543
3544 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003545 n_channels = validate_scan_freqs(
3546 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003547 if (!n_channels)
3548 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003549 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003550 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003551 n_channels = 0;
3552
Johannes Berg2a519312009-02-10 21:25:55 +01003553 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3554 if (wiphy->bands[band])
3555 n_channels += wiphy->bands[band]->n_channels;
3556 }
3557
3558 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3559 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3560 n_ssids++;
3561
Johannes Berg4c476992010-10-04 21:36:35 +02003562 if (n_ssids > wiphy->max_scan_ssids)
3563 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003564
Jouni Malinen70692ad2009-02-16 19:39:13 +02003565 if (info->attrs[NL80211_ATTR_IE])
3566 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3567 else
3568 ie_len = 0;
3569
Johannes Berg4c476992010-10-04 21:36:35 +02003570 if (ie_len > wiphy->max_scan_ie_len)
3571 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003572
Johannes Berg2a519312009-02-10 21:25:55 +01003573 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003574 + sizeof(*request->ssids) * n_ssids
3575 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003576 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003577 if (!request)
3578 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003579
Johannes Berg2a519312009-02-10 21:25:55 +01003580 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003581 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003582 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003583 if (ie_len) {
3584 if (request->ssids)
3585 request->ie = (void *)(request->ssids + n_ssids);
3586 else
3587 request->ie = (void *)(request->channels + n_channels);
3588 }
Johannes Berg2a519312009-02-10 21:25:55 +01003589
Johannes Berg584991d2009-11-02 13:32:03 +01003590 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003591 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3592 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003593 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003594 struct ieee80211_channel *chan;
3595
3596 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3597
3598 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003599 err = -EINVAL;
3600 goto out_free;
3601 }
Johannes Berg584991d2009-11-02 13:32:03 +01003602
3603 /* ignore disabled channels */
3604 if (chan->flags & IEEE80211_CHAN_DISABLED)
3605 continue;
3606
3607 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003608 i++;
3609 }
3610 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003611 enum ieee80211_band band;
3612
Johannes Berg2a519312009-02-10 21:25:55 +01003613 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003614 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3615 int j;
3616 if (!wiphy->bands[band])
3617 continue;
3618 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003619 struct ieee80211_channel *chan;
3620
3621 chan = &wiphy->bands[band]->channels[j];
3622
3623 if (chan->flags & IEEE80211_CHAN_DISABLED)
3624 continue;
3625
3626 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003627 i++;
3628 }
3629 }
3630 }
3631
Johannes Berg584991d2009-11-02 13:32:03 +01003632 if (!i) {
3633 err = -EINVAL;
3634 goto out_free;
3635 }
3636
3637 request->n_channels = i;
3638
Johannes Berg2a519312009-02-10 21:25:55 +01003639 i = 0;
3640 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3641 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003642 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003643 err = -EINVAL;
3644 goto out_free;
3645 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003646 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003647 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003648 i++;
3649 }
3650 }
3651
Jouni Malinen70692ad2009-02-16 19:39:13 +02003652 if (info->attrs[NL80211_ATTR_IE]) {
3653 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003654 memcpy((void *)request->ie,
3655 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003656 request->ie_len);
3657 }
3658
Johannes Berg34850ab2011-07-18 18:08:35 +02003659 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003660 if (wiphy->bands[i])
3661 request->rates[i] =
3662 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003663
3664 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3665 nla_for_each_nested(attr,
3666 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3667 tmp) {
3668 enum ieee80211_band band = nla_type(attr);
3669
Dan Carpenter84404622011-07-29 11:52:18 +03003670 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003671 err = -EINVAL;
3672 goto out_free;
3673 }
3674 err = ieee80211_get_ratemask(wiphy->bands[band],
3675 nla_data(attr),
3676 nla_len(attr),
3677 &request->rates[band]);
3678 if (err)
3679 goto out_free;
3680 }
3681 }
3682
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303683 request->no_cck =
3684 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3685
Johannes Berg463d0182009-07-14 00:33:35 +02003686 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003687 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003688
Johannes Berg79c97e92009-07-07 03:56:12 +02003689 rdev->scan_req = request;
3690 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003691
Johannes Berg463d0182009-07-14 00:33:35 +02003692 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003693 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003694 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003695 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003696 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003697 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003698 kfree(request);
3699 }
Johannes Berg3b858752009-03-12 09:55:09 +01003700
Johannes Berg2a519312009-02-10 21:25:55 +01003701 return err;
3702}
3703
Luciano Coelho807f8a82011-05-11 17:09:35 +03003704static int nl80211_start_sched_scan(struct sk_buff *skb,
3705 struct genl_info *info)
3706{
3707 struct cfg80211_sched_scan_request *request;
3708 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3709 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003710 struct nlattr *attr;
3711 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003712 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003713 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003714 enum ieee80211_band band;
3715 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003716 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003717
3718 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3719 !rdev->ops->sched_scan_start)
3720 return -EOPNOTSUPP;
3721
3722 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3723 return -EINVAL;
3724
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003725 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3726 return -EINVAL;
3727
3728 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3729 if (interval == 0)
3730 return -EINVAL;
3731
Luciano Coelho807f8a82011-05-11 17:09:35 +03003732 wiphy = &rdev->wiphy;
3733
3734 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3735 n_channels = validate_scan_freqs(
3736 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3737 if (!n_channels)
3738 return -EINVAL;
3739 } else {
3740 n_channels = 0;
3741
3742 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3743 if (wiphy->bands[band])
3744 n_channels += wiphy->bands[band]->n_channels;
3745 }
3746
3747 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3748 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3749 tmp)
3750 n_ssids++;
3751
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003752 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003753 return -EINVAL;
3754
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003755 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3756 nla_for_each_nested(attr,
3757 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3758 tmp)
3759 n_match_sets++;
3760
3761 if (n_match_sets > wiphy->max_match_sets)
3762 return -EINVAL;
3763
Luciano Coelho807f8a82011-05-11 17:09:35 +03003764 if (info->attrs[NL80211_ATTR_IE])
3765 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3766 else
3767 ie_len = 0;
3768
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003769 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003770 return -EINVAL;
3771
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003772 mutex_lock(&rdev->sched_scan_mtx);
3773
3774 if (rdev->sched_scan_req) {
3775 err = -EINPROGRESS;
3776 goto out;
3777 }
3778
Luciano Coelho807f8a82011-05-11 17:09:35 +03003779 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003780 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003781 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003782 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003783 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003784 if (!request) {
3785 err = -ENOMEM;
3786 goto out;
3787 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003788
3789 if (n_ssids)
3790 request->ssids = (void *)&request->channels[n_channels];
3791 request->n_ssids = n_ssids;
3792 if (ie_len) {
3793 if (request->ssids)
3794 request->ie = (void *)(request->ssids + n_ssids);
3795 else
3796 request->ie = (void *)(request->channels + n_channels);
3797 }
3798
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003799 if (n_match_sets) {
3800 if (request->ie)
3801 request->match_sets = (void *)(request->ie + ie_len);
3802 else if (request->ssids)
3803 request->match_sets =
3804 (void *)(request->ssids + n_ssids);
3805 else
3806 request->match_sets =
3807 (void *)(request->channels + n_channels);
3808 }
3809 request->n_match_sets = n_match_sets;
3810
Luciano Coelho807f8a82011-05-11 17:09:35 +03003811 i = 0;
3812 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3813 /* user specified, bail out if channel not found */
3814 nla_for_each_nested(attr,
3815 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3816 tmp) {
3817 struct ieee80211_channel *chan;
3818
3819 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3820
3821 if (!chan) {
3822 err = -EINVAL;
3823 goto out_free;
3824 }
3825
3826 /* ignore disabled channels */
3827 if (chan->flags & IEEE80211_CHAN_DISABLED)
3828 continue;
3829
3830 request->channels[i] = chan;
3831 i++;
3832 }
3833 } else {
3834 /* all channels */
3835 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3836 int j;
3837 if (!wiphy->bands[band])
3838 continue;
3839 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3840 struct ieee80211_channel *chan;
3841
3842 chan = &wiphy->bands[band]->channels[j];
3843
3844 if (chan->flags & IEEE80211_CHAN_DISABLED)
3845 continue;
3846
3847 request->channels[i] = chan;
3848 i++;
3849 }
3850 }
3851 }
3852
3853 if (!i) {
3854 err = -EINVAL;
3855 goto out_free;
3856 }
3857
3858 request->n_channels = i;
3859
3860 i = 0;
3861 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3862 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3863 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003864 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003865 err = -EINVAL;
3866 goto out_free;
3867 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003868 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003869 memcpy(request->ssids[i].ssid, nla_data(attr),
3870 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003871 i++;
3872 }
3873 }
3874
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003875 i = 0;
3876 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3877 nla_for_each_nested(attr,
3878 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3879 tmp) {
3880 struct nlattr *ssid;
3881
3882 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3883 nla_data(attr), nla_len(attr),
3884 nl80211_match_policy);
3885 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3886 if (ssid) {
3887 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3888 err = -EINVAL;
3889 goto out_free;
3890 }
3891 memcpy(request->match_sets[i].ssid.ssid,
3892 nla_data(ssid), nla_len(ssid));
3893 request->match_sets[i].ssid.ssid_len =
3894 nla_len(ssid);
3895 }
3896 i++;
3897 }
3898 }
3899
Luciano Coelho807f8a82011-05-11 17:09:35 +03003900 if (info->attrs[NL80211_ATTR_IE]) {
3901 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3902 memcpy((void *)request->ie,
3903 nla_data(info->attrs[NL80211_ATTR_IE]),
3904 request->ie_len);
3905 }
3906
3907 request->dev = dev;
3908 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003909 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003910
3911 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3912 if (!err) {
3913 rdev->sched_scan_req = request;
3914 nl80211_send_sched_scan(rdev, dev,
3915 NL80211_CMD_START_SCHED_SCAN);
3916 goto out;
3917 }
3918
3919out_free:
3920 kfree(request);
3921out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003922 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003923 return err;
3924}
3925
3926static int nl80211_stop_sched_scan(struct sk_buff *skb,
3927 struct genl_info *info)
3928{
3929 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003930 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003931
3932 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3933 !rdev->ops->sched_scan_stop)
3934 return -EOPNOTSUPP;
3935
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003936 mutex_lock(&rdev->sched_scan_mtx);
3937 err = __cfg80211_stop_sched_scan(rdev, false);
3938 mutex_unlock(&rdev->sched_scan_mtx);
3939
3940 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003941}
3942
Johannes Berg9720bb32011-06-21 09:45:33 +02003943static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3944 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003945 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003946 struct wireless_dev *wdev,
3947 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003948{
Johannes Berg48ab9052009-07-10 18:42:31 +02003949 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003950 void *hdr;
3951 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003952 int i;
3953
3954 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003955
Johannes Berg9720bb32011-06-21 09:45:33 +02003956 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003957 NL80211_CMD_NEW_SCAN_RESULTS);
3958 if (!hdr)
3959 return -1;
3960
Johannes Berg9720bb32011-06-21 09:45:33 +02003961 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3962
Johannes Bergf5ea9122009-08-07 16:17:38 +02003963 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003964 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003965
3966 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3967 if (!bss)
3968 goto nla_put_failure;
3969 if (!is_zero_ether_addr(res->bssid))
3970 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3971 if (res->information_elements && res->len_information_elements)
3972 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3973 res->len_information_elements,
3974 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003975 if (res->beacon_ies && res->len_beacon_ies &&
3976 res->beacon_ies != res->information_elements)
3977 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3978 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003979 if (res->tsf)
3980 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3981 if (res->beacon_interval)
3982 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3983 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3984 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003985 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3986 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003987
Johannes Berg77965c92009-02-18 18:45:06 +01003988 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003989 case CFG80211_SIGNAL_TYPE_MBM:
3990 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3991 break;
3992 case CFG80211_SIGNAL_TYPE_UNSPEC:
3993 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3994 break;
3995 default:
3996 break;
3997 }
3998
Johannes Berg48ab9052009-07-10 18:42:31 +02003999 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004000 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004001 case NL80211_IFTYPE_STATION:
4002 if (intbss == wdev->current_bss)
4003 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4004 NL80211_BSS_STATUS_ASSOCIATED);
4005 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4006 if (intbss != wdev->auth_bsses[i])
4007 continue;
4008 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4009 NL80211_BSS_STATUS_AUTHENTICATED);
4010 break;
4011 }
4012 break;
4013 case NL80211_IFTYPE_ADHOC:
4014 if (intbss == wdev->current_bss)
4015 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4016 NL80211_BSS_STATUS_IBSS_JOINED);
4017 break;
4018 default:
4019 break;
4020 }
4021
Johannes Berg2a519312009-02-10 21:25:55 +01004022 nla_nest_end(msg, bss);
4023
4024 return genlmsg_end(msg, hdr);
4025
4026 nla_put_failure:
4027 genlmsg_cancel(msg, hdr);
4028 return -EMSGSIZE;
4029}
4030
4031static int nl80211_dump_scan(struct sk_buff *skb,
4032 struct netlink_callback *cb)
4033{
Johannes Berg48ab9052009-07-10 18:42:31 +02004034 struct cfg80211_registered_device *rdev;
4035 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004036 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004037 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004038 int start = cb->args[1], idx = 0;
4039 int err;
4040
Johannes Berg67748892010-10-04 21:14:06 +02004041 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4042 if (err)
4043 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004044
Johannes Berg48ab9052009-07-10 18:42:31 +02004045 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004046
Johannes Berg48ab9052009-07-10 18:42:31 +02004047 wdev_lock(wdev);
4048 spin_lock_bh(&rdev->bss_lock);
4049 cfg80211_bss_expire(rdev);
4050
Johannes Berg9720bb32011-06-21 09:45:33 +02004051 cb->seq = rdev->bss_generation;
4052
Johannes Berg48ab9052009-07-10 18:42:31 +02004053 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004054 if (++idx <= start)
4055 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004056 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004057 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004058 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004059 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004060 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004061 }
4062 }
4063
Johannes Berg48ab9052009-07-10 18:42:31 +02004064 spin_unlock_bh(&rdev->bss_lock);
4065 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004066
4067 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004068 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004069
Johannes Berg67748892010-10-04 21:14:06 +02004070 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004071}
4072
Holger Schurig61fa7132009-11-11 12:25:40 +01004073static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4074 int flags, struct net_device *dev,
4075 struct survey_info *survey)
4076{
4077 void *hdr;
4078 struct nlattr *infoattr;
4079
Holger Schurig61fa7132009-11-11 12:25:40 +01004080 hdr = nl80211hdr_put(msg, pid, seq, flags,
4081 NL80211_CMD_NEW_SURVEY_RESULTS);
4082 if (!hdr)
4083 return -ENOMEM;
4084
4085 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4086
4087 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4088 if (!infoattr)
4089 goto nla_put_failure;
4090
4091 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4092 survey->channel->center_freq);
4093 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4094 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4095 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004096 if (survey->filled & SURVEY_INFO_IN_USE)
4097 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004098 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4099 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4100 survey->channel_time);
4101 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4102 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4103 survey->channel_time_busy);
4104 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4105 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4106 survey->channel_time_ext_busy);
4107 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4108 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4109 survey->channel_time_rx);
4110 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4111 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4112 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004113
4114 nla_nest_end(msg, infoattr);
4115
4116 return genlmsg_end(msg, hdr);
4117
4118 nla_put_failure:
4119 genlmsg_cancel(msg, hdr);
4120 return -EMSGSIZE;
4121}
4122
4123static int nl80211_dump_survey(struct sk_buff *skb,
4124 struct netlink_callback *cb)
4125{
4126 struct survey_info survey;
4127 struct cfg80211_registered_device *dev;
4128 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004129 int survey_idx = cb->args[1];
4130 int res;
4131
Johannes Berg67748892010-10-04 21:14:06 +02004132 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4133 if (res)
4134 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004135
4136 if (!dev->ops->dump_survey) {
4137 res = -EOPNOTSUPP;
4138 goto out_err;
4139 }
4140
4141 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004142 struct ieee80211_channel *chan;
4143
Holger Schurig61fa7132009-11-11 12:25:40 +01004144 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4145 &survey);
4146 if (res == -ENOENT)
4147 break;
4148 if (res)
4149 goto out_err;
4150
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004151 /* Survey without a channel doesn't make sense */
4152 if (!survey.channel) {
4153 res = -EINVAL;
4154 goto out;
4155 }
4156
4157 chan = ieee80211_get_channel(&dev->wiphy,
4158 survey.channel->center_freq);
4159 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4160 survey_idx++;
4161 continue;
4162 }
4163
Holger Schurig61fa7132009-11-11 12:25:40 +01004164 if (nl80211_send_survey(skb,
4165 NETLINK_CB(cb->skb).pid,
4166 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4167 netdev,
4168 &survey) < 0)
4169 goto out;
4170 survey_idx++;
4171 }
4172
4173 out:
4174 cb->args[1] = survey_idx;
4175 res = skb->len;
4176 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004177 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004178 return res;
4179}
4180
Jouni Malinen255e7372009-03-20 21:21:17 +02004181static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4182{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004183 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004184}
4185
Samuel Ortizb23aa672009-07-01 21:26:54 +02004186static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4187{
4188 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4189 NL80211_WPA_VERSION_2));
4190}
4191
Jouni Malinen636a5d32009-03-19 13:39:22 +02004192static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4193{
Johannes Berg4c476992010-10-04 21:36:35 +02004194 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4195 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004196 struct ieee80211_channel *chan;
4197 const u8 *bssid, *ssid, *ie = NULL;
4198 int err, ssid_len, ie_len = 0;
4199 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004200 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004201 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004202
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004203 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4204 return -EINVAL;
4205
4206 if (!info->attrs[NL80211_ATTR_MAC])
4207 return -EINVAL;
4208
Jouni Malinen17780922009-03-27 20:52:47 +02004209 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4210 return -EINVAL;
4211
Johannes Berg19957bb2009-07-02 17:20:43 +02004212 if (!info->attrs[NL80211_ATTR_SSID])
4213 return -EINVAL;
4214
4215 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4216 return -EINVAL;
4217
Johannes Bergfffd0932009-07-08 14:22:54 +02004218 err = nl80211_parse_key(info, &key);
4219 if (err)
4220 return err;
4221
4222 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004223 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4224 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004225 if (!key.p.key || !key.p.key_len)
4226 return -EINVAL;
4227 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4228 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4229 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4230 key.p.key_len != WLAN_KEY_LEN_WEP104))
4231 return -EINVAL;
4232 if (key.idx > 4)
4233 return -EINVAL;
4234 } else {
4235 key.p.key_len = 0;
4236 key.p.key = NULL;
4237 }
4238
Johannes Bergafea0b72010-08-10 09:46:42 +02004239 if (key.idx >= 0) {
4240 int i;
4241 bool ok = false;
4242 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4243 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4244 ok = true;
4245 break;
4246 }
4247 }
Johannes Berg4c476992010-10-04 21:36:35 +02004248 if (!ok)
4249 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004250 }
4251
Johannes Berg4c476992010-10-04 21:36:35 +02004252 if (!rdev->ops->auth)
4253 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004254
Johannes Berg074ac8d2010-09-16 14:58:22 +02004255 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004256 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4257 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004258
Johannes Berg19957bb2009-07-02 17:20:43 +02004259 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004260 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004261 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004262 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4263 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004264
Johannes Berg19957bb2009-07-02 17:20:43 +02004265 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4266 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4267
4268 if (info->attrs[NL80211_ATTR_IE]) {
4269 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4270 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4271 }
4272
4273 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004274 if (!nl80211_valid_auth_type(auth_type))
4275 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004276
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004277 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4278
Johannes Berg4c476992010-10-04 21:36:35 +02004279 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4280 ssid, ssid_len, ie, ie_len,
4281 key.p.key, key.p.key_len, key.idx,
4282 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004283}
4284
Johannes Bergc0692b82010-08-27 14:26:53 +03004285static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4286 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004287 struct cfg80211_crypto_settings *settings,
4288 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004289{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004290 memset(settings, 0, sizeof(*settings));
4291
Samuel Ortizb23aa672009-07-01 21:26:54 +02004292 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4293
Johannes Bergc0692b82010-08-27 14:26:53 +03004294 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4295 u16 proto;
4296 proto = nla_get_u16(
4297 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4298 settings->control_port_ethertype = cpu_to_be16(proto);
4299 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4300 proto != ETH_P_PAE)
4301 return -EINVAL;
4302 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4303 settings->control_port_no_encrypt = true;
4304 } else
4305 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4306
Samuel Ortizb23aa672009-07-01 21:26:54 +02004307 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4308 void *data;
4309 int len, i;
4310
4311 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4312 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4313 settings->n_ciphers_pairwise = len / sizeof(u32);
4314
4315 if (len % sizeof(u32))
4316 return -EINVAL;
4317
Johannes Berg3dc27d22009-07-02 21:36:37 +02004318 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004319 return -EINVAL;
4320
4321 memcpy(settings->ciphers_pairwise, data, len);
4322
4323 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004324 if (!cfg80211_supported_cipher_suite(
4325 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004326 settings->ciphers_pairwise[i]))
4327 return -EINVAL;
4328 }
4329
4330 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4331 settings->cipher_group =
4332 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004333 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4334 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004335 return -EINVAL;
4336 }
4337
4338 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4339 settings->wpa_versions =
4340 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4341 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4342 return -EINVAL;
4343 }
4344
4345 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4346 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004347 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004348
4349 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4350 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4351 settings->n_akm_suites = len / sizeof(u32);
4352
4353 if (len % sizeof(u32))
4354 return -EINVAL;
4355
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004356 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4357 return -EINVAL;
4358
Samuel Ortizb23aa672009-07-01 21:26:54 +02004359 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004360 }
4361
4362 return 0;
4363}
4364
Jouni Malinen636a5d32009-03-19 13:39:22 +02004365static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4366{
Johannes Berg4c476992010-10-04 21:36:35 +02004367 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4368 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004369 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004370 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004371 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004372 int err, ssid_len, ie_len = 0;
4373 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004374
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004375 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4376 return -EINVAL;
4377
4378 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004379 !info->attrs[NL80211_ATTR_SSID] ||
4380 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004381 return -EINVAL;
4382
Johannes Berg4c476992010-10-04 21:36:35 +02004383 if (!rdev->ops->assoc)
4384 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004385
Johannes Berg074ac8d2010-09-16 14:58:22 +02004386 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004387 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4388 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004389
Johannes Berg19957bb2009-07-02 17:20:43 +02004390 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004391
Johannes Berg19957bb2009-07-02 17:20:43 +02004392 chan = ieee80211_get_channel(&rdev->wiphy,
4393 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004394 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4395 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004396
Johannes Berg19957bb2009-07-02 17:20:43 +02004397 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4398 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004399
4400 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004401 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4402 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004403 }
4404
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004405 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004406 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004407 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004408 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004409 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004410 else if (mfp != NL80211_MFP_NO)
4411 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004412 }
4413
Johannes Berg3e5d7642009-07-07 14:37:26 +02004414 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4415 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4416
Johannes Bergc0692b82010-08-27 14:26:53 +03004417 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004418 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004419 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4420 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004421 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004422
Jouni Malinen636a5d32009-03-19 13:39:22 +02004423 return err;
4424}
4425
4426static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4427{
Johannes Berg4c476992010-10-04 21:36:35 +02004428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4429 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004430 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004431 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004432 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004433 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004434
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004435 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4436 return -EINVAL;
4437
4438 if (!info->attrs[NL80211_ATTR_MAC])
4439 return -EINVAL;
4440
4441 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4442 return -EINVAL;
4443
Johannes Berg4c476992010-10-04 21:36:35 +02004444 if (!rdev->ops->deauth)
4445 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004446
Johannes Berg074ac8d2010-09-16 14:58:22 +02004447 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004448 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4449 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004450
Johannes Berg19957bb2009-07-02 17:20:43 +02004451 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004452
Johannes Berg19957bb2009-07-02 17:20:43 +02004453 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4454 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004455 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004456 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004457 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004458
4459 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004460 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4461 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004462 }
4463
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004464 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4465
Johannes Berg4c476992010-10-04 21:36:35 +02004466 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4467 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004468}
4469
4470static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4471{
Johannes Berg4c476992010-10-04 21:36:35 +02004472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4473 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004474 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004475 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004476 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004477 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004478
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004479 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4480 return -EINVAL;
4481
4482 if (!info->attrs[NL80211_ATTR_MAC])
4483 return -EINVAL;
4484
4485 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4486 return -EINVAL;
4487
Johannes Berg4c476992010-10-04 21:36:35 +02004488 if (!rdev->ops->disassoc)
4489 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004490
Johannes Berg074ac8d2010-09-16 14:58:22 +02004491 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004492 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4493 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004494
Johannes Berg19957bb2009-07-02 17:20:43 +02004495 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004496
Johannes Berg19957bb2009-07-02 17:20:43 +02004497 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4498 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004499 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004500 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004501 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004502
4503 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004504 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4505 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004506 }
4507
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004508 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4509
Johannes Berg4c476992010-10-04 21:36:35 +02004510 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4511 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004512}
4513
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004514static bool
4515nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4516 int mcast_rate[IEEE80211_NUM_BANDS],
4517 int rateval)
4518{
4519 struct wiphy *wiphy = &rdev->wiphy;
4520 bool found = false;
4521 int band, i;
4522
4523 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4524 struct ieee80211_supported_band *sband;
4525
4526 sband = wiphy->bands[band];
4527 if (!sband)
4528 continue;
4529
4530 for (i = 0; i < sband->n_bitrates; i++) {
4531 if (sband->bitrates[i].bitrate == rateval) {
4532 mcast_rate[band] = i + 1;
4533 found = true;
4534 break;
4535 }
4536 }
4537 }
4538
4539 return found;
4540}
4541
Johannes Berg04a773a2009-04-19 21:24:32 +02004542static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4543{
Johannes Berg4c476992010-10-04 21:36:35 +02004544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4545 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004546 struct cfg80211_ibss_params ibss;
4547 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004548 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004549 int err;
4550
Johannes Berg8e30bc52009-04-22 17:45:38 +02004551 memset(&ibss, 0, sizeof(ibss));
4552
Johannes Berg04a773a2009-04-19 21:24:32 +02004553 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4554 return -EINVAL;
4555
4556 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4557 !info->attrs[NL80211_ATTR_SSID] ||
4558 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4559 return -EINVAL;
4560
Johannes Berg8e30bc52009-04-22 17:45:38 +02004561 ibss.beacon_interval = 100;
4562
4563 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4564 ibss.beacon_interval =
4565 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4566 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4567 return -EINVAL;
4568 }
4569
Johannes Berg4c476992010-10-04 21:36:35 +02004570 if (!rdev->ops->join_ibss)
4571 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004572
Johannes Berg4c476992010-10-04 21:36:35 +02004573 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4574 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004575
Johannes Berg79c97e92009-07-07 03:56:12 +02004576 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004577
Johannes Berg39193492011-09-16 13:45:25 +02004578 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004579 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004580
4581 if (!is_valid_ether_addr(ibss.bssid))
4582 return -EINVAL;
4583 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004584 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4585 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4586
4587 if (info->attrs[NL80211_ATTR_IE]) {
4588 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4589 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4590 }
4591
4592 ibss.channel = ieee80211_get_channel(wiphy,
4593 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4594 if (!ibss.channel ||
4595 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004596 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4597 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004598
4599 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004600 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004601
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004602 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4603 u8 *rates =
4604 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4605 int n_rates =
4606 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4607 struct ieee80211_supported_band *sband =
4608 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004609 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004610
Johannes Berg34850ab2011-07-18 18:08:35 +02004611 err = ieee80211_get_ratemask(sband, rates, n_rates,
4612 &ibss.basic_rates);
4613 if (err)
4614 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004615 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004616
4617 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4618 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4619 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4620 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004621
Johannes Berg4c476992010-10-04 21:36:35 +02004622 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4623 connkeys = nl80211_parse_connkeys(rdev,
4624 info->attrs[NL80211_ATTR_KEYS]);
4625 if (IS_ERR(connkeys))
4626 return PTR_ERR(connkeys);
4627 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004628
Johannes Berg4c476992010-10-04 21:36:35 +02004629 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004630 if (err)
4631 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004632 return err;
4633}
4634
4635static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4636{
Johannes Berg4c476992010-10-04 21:36:35 +02004637 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4638 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004639
Johannes Berg4c476992010-10-04 21:36:35 +02004640 if (!rdev->ops->leave_ibss)
4641 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004642
Johannes Berg4c476992010-10-04 21:36:35 +02004643 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4644 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004645
Johannes Berg4c476992010-10-04 21:36:35 +02004646 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004647}
4648
Johannes Bergaff89a92009-07-01 21:26:51 +02004649#ifdef CONFIG_NL80211_TESTMODE
4650static struct genl_multicast_group nl80211_testmode_mcgrp = {
4651 .name = "testmode",
4652};
4653
4654static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4655{
Johannes Berg4c476992010-10-04 21:36:35 +02004656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004657 int err;
4658
4659 if (!info->attrs[NL80211_ATTR_TESTDATA])
4660 return -EINVAL;
4661
Johannes Bergaff89a92009-07-01 21:26:51 +02004662 err = -EOPNOTSUPP;
4663 if (rdev->ops->testmode_cmd) {
4664 rdev->testmode_info = info;
4665 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4666 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4667 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4668 rdev->testmode_info = NULL;
4669 }
4670
Johannes Bergaff89a92009-07-01 21:26:51 +02004671 return err;
4672}
4673
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004674static int nl80211_testmode_dump(struct sk_buff *skb,
4675 struct netlink_callback *cb)
4676{
4677 struct cfg80211_registered_device *dev;
4678 int err;
4679 long phy_idx;
4680 void *data = NULL;
4681 int data_len = 0;
4682
4683 if (cb->args[0]) {
4684 /*
4685 * 0 is a valid index, but not valid for args[0],
4686 * so we need to offset by 1.
4687 */
4688 phy_idx = cb->args[0] - 1;
4689 } else {
4690 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4691 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4692 nl80211_policy);
4693 if (err)
4694 return err;
4695 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4696 return -EINVAL;
4697 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4698 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4699 cb->args[1] =
4700 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4701 }
4702
4703 if (cb->args[1]) {
4704 data = nla_data((void *)cb->args[1]);
4705 data_len = nla_len((void *)cb->args[1]);
4706 }
4707
4708 mutex_lock(&cfg80211_mutex);
4709 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4710 if (!dev) {
4711 mutex_unlock(&cfg80211_mutex);
4712 return -ENOENT;
4713 }
4714 cfg80211_lock_rdev(dev);
4715 mutex_unlock(&cfg80211_mutex);
4716
4717 if (!dev->ops->testmode_dump) {
4718 err = -EOPNOTSUPP;
4719 goto out_err;
4720 }
4721
4722 while (1) {
4723 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4724 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4725 NL80211_CMD_TESTMODE);
4726 struct nlattr *tmdata;
4727
4728 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4729 genlmsg_cancel(skb, hdr);
4730 break;
4731 }
4732
4733 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4734 if (!tmdata) {
4735 genlmsg_cancel(skb, hdr);
4736 break;
4737 }
4738 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4739 data, data_len);
4740 nla_nest_end(skb, tmdata);
4741
4742 if (err == -ENOBUFS || err == -ENOENT) {
4743 genlmsg_cancel(skb, hdr);
4744 break;
4745 } else if (err) {
4746 genlmsg_cancel(skb, hdr);
4747 goto out_err;
4748 }
4749
4750 genlmsg_end(skb, hdr);
4751 }
4752
4753 err = skb->len;
4754 /* see above */
4755 cb->args[0] = phy_idx + 1;
4756 out_err:
4757 cfg80211_unlock_rdev(dev);
4758 return err;
4759}
4760
Johannes Bergaff89a92009-07-01 21:26:51 +02004761static struct sk_buff *
4762__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4763 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4764{
4765 struct sk_buff *skb;
4766 void *hdr;
4767 struct nlattr *data;
4768
4769 skb = nlmsg_new(approxlen + 100, gfp);
4770 if (!skb)
4771 return NULL;
4772
4773 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4774 if (!hdr) {
4775 kfree_skb(skb);
4776 return NULL;
4777 }
4778
4779 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4780 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4781
4782 ((void **)skb->cb)[0] = rdev;
4783 ((void **)skb->cb)[1] = hdr;
4784 ((void **)skb->cb)[2] = data;
4785
4786 return skb;
4787
4788 nla_put_failure:
4789 kfree_skb(skb);
4790 return NULL;
4791}
4792
4793struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4794 int approxlen)
4795{
4796 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4797
4798 if (WARN_ON(!rdev->testmode_info))
4799 return NULL;
4800
4801 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4802 rdev->testmode_info->snd_pid,
4803 rdev->testmode_info->snd_seq,
4804 GFP_KERNEL);
4805}
4806EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4807
4808int cfg80211_testmode_reply(struct sk_buff *skb)
4809{
4810 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4811 void *hdr = ((void **)skb->cb)[1];
4812 struct nlattr *data = ((void **)skb->cb)[2];
4813
4814 if (WARN_ON(!rdev->testmode_info)) {
4815 kfree_skb(skb);
4816 return -EINVAL;
4817 }
4818
4819 nla_nest_end(skb, data);
4820 genlmsg_end(skb, hdr);
4821 return genlmsg_reply(skb, rdev->testmode_info);
4822}
4823EXPORT_SYMBOL(cfg80211_testmode_reply);
4824
4825struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4826 int approxlen, gfp_t gfp)
4827{
4828 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4829
4830 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4831}
4832EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4833
4834void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4835{
4836 void *hdr = ((void **)skb->cb)[1];
4837 struct nlattr *data = ((void **)skb->cb)[2];
4838
4839 nla_nest_end(skb, data);
4840 genlmsg_end(skb, hdr);
4841 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4842}
4843EXPORT_SYMBOL(cfg80211_testmode_event);
4844#endif
4845
Samuel Ortizb23aa672009-07-01 21:26:54 +02004846static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4847{
Johannes Berg4c476992010-10-04 21:36:35 +02004848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4849 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004850 struct cfg80211_connect_params connect;
4851 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004852 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004853 int err;
4854
4855 memset(&connect, 0, sizeof(connect));
4856
4857 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4858 return -EINVAL;
4859
4860 if (!info->attrs[NL80211_ATTR_SSID] ||
4861 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4862 return -EINVAL;
4863
4864 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4865 connect.auth_type =
4866 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4867 if (!nl80211_valid_auth_type(connect.auth_type))
4868 return -EINVAL;
4869 } else
4870 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4871
4872 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4873
Johannes Bergc0692b82010-08-27 14:26:53 +03004874 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004875 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004876 if (err)
4877 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004878
Johannes Berg074ac8d2010-09-16 14:58:22 +02004879 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004880 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4881 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004882
Johannes Berg79c97e92009-07-07 03:56:12 +02004883 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004884
Samuel Ortizb23aa672009-07-01 21:26:54 +02004885 if (info->attrs[NL80211_ATTR_MAC])
4886 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4887 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4888 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4889
4890 if (info->attrs[NL80211_ATTR_IE]) {
4891 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4892 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4893 }
4894
4895 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4896 connect.channel =
4897 ieee80211_get_channel(wiphy,
4898 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4899 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004900 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4901 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004902 }
4903
Johannes Bergfffd0932009-07-08 14:22:54 +02004904 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4905 connkeys = nl80211_parse_connkeys(rdev,
4906 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004907 if (IS_ERR(connkeys))
4908 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004909 }
4910
4911 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004912 if (err)
4913 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004914 return err;
4915}
4916
4917static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4918{
Johannes Berg4c476992010-10-04 21:36:35 +02004919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4920 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004921 u16 reason;
4922
4923 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4924 reason = WLAN_REASON_DEAUTH_LEAVING;
4925 else
4926 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4927
4928 if (reason == 0)
4929 return -EINVAL;
4930
Johannes Berg074ac8d2010-09-16 14:58:22 +02004931 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004932 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4933 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004934
Johannes Berg4c476992010-10-04 21:36:35 +02004935 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004936}
4937
Johannes Berg463d0182009-07-14 00:33:35 +02004938static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4939{
Johannes Berg4c476992010-10-04 21:36:35 +02004940 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004941 struct net *net;
4942 int err;
4943 u32 pid;
4944
4945 if (!info->attrs[NL80211_ATTR_PID])
4946 return -EINVAL;
4947
4948 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4949
Johannes Berg463d0182009-07-14 00:33:35 +02004950 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004951 if (IS_ERR(net))
4952 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004953
4954 err = 0;
4955
4956 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004957 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4958 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004959
Johannes Berg463d0182009-07-14 00:33:35 +02004960 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004961 return err;
4962}
4963
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004964static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4965{
Johannes Berg4c476992010-10-04 21:36:35 +02004966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004967 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4968 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004969 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004970 struct cfg80211_pmksa pmksa;
4971
4972 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4973
4974 if (!info->attrs[NL80211_ATTR_MAC])
4975 return -EINVAL;
4976
4977 if (!info->attrs[NL80211_ATTR_PMKID])
4978 return -EINVAL;
4979
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004980 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4981 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4982
Johannes Berg074ac8d2010-09-16 14:58:22 +02004983 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004984 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4985 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004986
4987 switch (info->genlhdr->cmd) {
4988 case NL80211_CMD_SET_PMKSA:
4989 rdev_ops = rdev->ops->set_pmksa;
4990 break;
4991 case NL80211_CMD_DEL_PMKSA:
4992 rdev_ops = rdev->ops->del_pmksa;
4993 break;
4994 default:
4995 WARN_ON(1);
4996 break;
4997 }
4998
Johannes Berg4c476992010-10-04 21:36:35 +02004999 if (!rdev_ops)
5000 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005001
Johannes Berg4c476992010-10-04 21:36:35 +02005002 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005003}
5004
5005static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5006{
Johannes Berg4c476992010-10-04 21:36:35 +02005007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5008 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005009
Johannes Berg074ac8d2010-09-16 14:58:22 +02005010 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005011 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5012 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005013
Johannes Berg4c476992010-10-04 21:36:35 +02005014 if (!rdev->ops->flush_pmksa)
5015 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005016
Johannes Berg4c476992010-10-04 21:36:35 +02005017 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005018}
5019
Arik Nemtsov109086c2011-09-28 14:12:50 +03005020static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5021{
5022 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5023 struct net_device *dev = info->user_ptr[1];
5024 u8 action_code, dialog_token;
5025 u16 status_code;
5026 u8 *peer;
5027
5028 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5029 !rdev->ops->tdls_mgmt)
5030 return -EOPNOTSUPP;
5031
5032 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5033 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5034 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5035 !info->attrs[NL80211_ATTR_IE] ||
5036 !info->attrs[NL80211_ATTR_MAC])
5037 return -EINVAL;
5038
5039 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5040 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5041 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5042 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5043
5044 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5045 dialog_token, status_code,
5046 nla_data(info->attrs[NL80211_ATTR_IE]),
5047 nla_len(info->attrs[NL80211_ATTR_IE]));
5048}
5049
5050static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5051{
5052 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5053 struct net_device *dev = info->user_ptr[1];
5054 enum nl80211_tdls_operation operation;
5055 u8 *peer;
5056
5057 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5058 !rdev->ops->tdls_oper)
5059 return -EOPNOTSUPP;
5060
5061 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5062 !info->attrs[NL80211_ATTR_MAC])
5063 return -EINVAL;
5064
5065 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5066 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5067
5068 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5069}
5070
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005071static int nl80211_remain_on_channel(struct sk_buff *skb,
5072 struct genl_info *info)
5073{
Johannes Berg4c476992010-10-04 21:36:35 +02005074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5075 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005076 struct ieee80211_channel *chan;
5077 struct sk_buff *msg;
5078 void *hdr;
5079 u64 cookie;
5080 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5081 u32 freq, duration;
5082 int err;
5083
5084 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5085 !info->attrs[NL80211_ATTR_DURATION])
5086 return -EINVAL;
5087
5088 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5089
5090 /*
5091 * We should be on that channel for at least one jiffie,
5092 * and more than 5 seconds seems excessive.
5093 */
Johannes Berga2939112010-12-14 17:54:28 +01005094 if (!duration || !msecs_to_jiffies(duration) ||
5095 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005096 return -EINVAL;
5097
Johannes Berg4c476992010-10-04 21:36:35 +02005098 if (!rdev->ops->remain_on_channel)
5099 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005100
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005101 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5102 channel_type = nla_get_u32(
5103 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5104 if (channel_type != NL80211_CHAN_NO_HT &&
5105 channel_type != NL80211_CHAN_HT20 &&
5106 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005107 channel_type != NL80211_CHAN_HT40MINUS)
5108 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005109 }
5110
5111 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5112 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005113 if (chan == NULL)
5114 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005115
5116 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005117 if (!msg)
5118 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005119
5120 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5121 NL80211_CMD_REMAIN_ON_CHANNEL);
5122
5123 if (IS_ERR(hdr)) {
5124 err = PTR_ERR(hdr);
5125 goto free_msg;
5126 }
5127
5128 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5129 channel_type, duration, &cookie);
5130
5131 if (err)
5132 goto free_msg;
5133
5134 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5135
5136 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005137
5138 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005139
5140 nla_put_failure:
5141 err = -ENOBUFS;
5142 free_msg:
5143 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005144 return err;
5145}
5146
5147static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5148 struct genl_info *info)
5149{
Johannes Berg4c476992010-10-04 21:36:35 +02005150 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5151 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005152 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005153
5154 if (!info->attrs[NL80211_ATTR_COOKIE])
5155 return -EINVAL;
5156
Johannes Berg4c476992010-10-04 21:36:35 +02005157 if (!rdev->ops->cancel_remain_on_channel)
5158 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005159
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005160 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5161
Johannes Berg4c476992010-10-04 21:36:35 +02005162 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005163}
5164
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005165static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5166 u8 *rates, u8 rates_len)
5167{
5168 u8 i;
5169 u32 mask = 0;
5170
5171 for (i = 0; i < rates_len; i++) {
5172 int rate = (rates[i] & 0x7f) * 5;
5173 int ridx;
5174 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5175 struct ieee80211_rate *srate =
5176 &sband->bitrates[ridx];
5177 if (rate == srate->bitrate) {
5178 mask |= 1 << ridx;
5179 break;
5180 }
5181 }
5182 if (ridx == sband->n_bitrates)
5183 return 0; /* rate not found */
5184 }
5185
5186 return mask;
5187}
5188
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005189static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005190 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5191 .len = NL80211_MAX_SUPP_RATES },
5192};
5193
5194static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5195 struct genl_info *info)
5196{
5197 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005199 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005200 int rem, i;
5201 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005202 struct nlattr *tx_rates;
5203 struct ieee80211_supported_band *sband;
5204
5205 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5206 return -EINVAL;
5207
Johannes Berg4c476992010-10-04 21:36:35 +02005208 if (!rdev->ops->set_bitrate_mask)
5209 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005210
5211 memset(&mask, 0, sizeof(mask));
5212 /* Default to all rates enabled */
5213 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5214 sband = rdev->wiphy.bands[i];
5215 mask.control[i].legacy =
5216 sband ? (1 << sband->n_bitrates) - 1 : 0;
5217 }
5218
5219 /*
5220 * The nested attribute uses enum nl80211_band as the index. This maps
5221 * directly to the enum ieee80211_band values used in cfg80211.
5222 */
5223 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5224 {
5225 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005226 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5227 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005228 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005229 if (sband == NULL)
5230 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005231 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5232 nla_len(tx_rates), nl80211_txattr_policy);
5233 if (tb[NL80211_TXRATE_LEGACY]) {
5234 mask.control[band].legacy = rateset_to_mask(
5235 sband,
5236 nla_data(tb[NL80211_TXRATE_LEGACY]),
5237 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005238 if (mask.control[band].legacy == 0)
5239 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005240 }
5241 }
5242
Johannes Berg4c476992010-10-04 21:36:35 +02005243 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005244}
5245
Johannes Berg2e161f72010-08-12 15:38:38 +02005246static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005247{
Johannes Berg4c476992010-10-04 21:36:35 +02005248 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5249 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005250 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005251
5252 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5253 return -EINVAL;
5254
Johannes Berg2e161f72010-08-12 15:38:38 +02005255 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5256 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005257
Johannes Berg9d38d852010-06-09 17:20:33 +02005258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005259 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005260 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5261 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5262 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005263 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005264 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5265 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005266
5267 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005268 if (!rdev->ops->mgmt_tx)
5269 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005270
Johannes Berg4c476992010-10-04 21:36:35 +02005271 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005272 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005273 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5274 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005275}
5276
Johannes Berg2e161f72010-08-12 15:38:38 +02005277static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005278{
Johannes Berg4c476992010-10-04 21:36:35 +02005279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5280 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005281 struct ieee80211_channel *chan;
5282 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005283 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005284 u32 freq;
5285 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005286 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005287 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005288 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005289 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005290 bool offchan, no_cck, dont_wait_for_ack;
5291
5292 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005293
5294 if (!info->attrs[NL80211_ATTR_FRAME] ||
5295 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5296 return -EINVAL;
5297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (!rdev->ops->mgmt_tx)
5299 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005300
Johannes Berg9d38d852010-06-09 17:20:33 +02005301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005303 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5304 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5305 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005306 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005307 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5308 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005309
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005310 if (info->attrs[NL80211_ATTR_DURATION]) {
5311 if (!rdev->ops->mgmt_tx_cancel_wait)
5312 return -EINVAL;
5313 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5314 }
5315
Jouni Malinen026331c2010-02-15 12:53:10 +02005316 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5317 channel_type = nla_get_u32(
5318 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5319 if (channel_type != NL80211_CHAN_NO_HT &&
5320 channel_type != NL80211_CHAN_HT20 &&
5321 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005322 channel_type != NL80211_CHAN_HT40MINUS)
5323 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005324 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005325 }
5326
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005327 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5328
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305329 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5330
Jouni Malinen026331c2010-02-15 12:53:10 +02005331 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5332 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005333 if (chan == NULL)
5334 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005335
Johannes Berge247bd902011-11-04 11:18:21 +01005336 if (!dont_wait_for_ack) {
5337 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5338 if (!msg)
5339 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005340
Johannes Berge247bd902011-11-04 11:18:21 +01005341 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5342 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005343
Johannes Berge247bd902011-11-04 11:18:21 +01005344 if (IS_ERR(hdr)) {
5345 err = PTR_ERR(hdr);
5346 goto free_msg;
5347 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005348 }
Johannes Berge247bd902011-11-04 11:18:21 +01005349
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005350 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5351 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005352 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5353 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005354 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005355 if (err)
5356 goto free_msg;
5357
Johannes Berge247bd902011-11-04 11:18:21 +01005358 if (msg) {
5359 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005360
Johannes Berge247bd902011-11-04 11:18:21 +01005361 genlmsg_end(msg, hdr);
5362 return genlmsg_reply(msg, info);
5363 }
5364
5365 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005366
5367 nla_put_failure:
5368 err = -ENOBUFS;
5369 free_msg:
5370 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005371 return err;
5372}
5373
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005374static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5375{
5376 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5377 struct net_device *dev = info->user_ptr[1];
5378 u64 cookie;
5379
5380 if (!info->attrs[NL80211_ATTR_COOKIE])
5381 return -EINVAL;
5382
5383 if (!rdev->ops->mgmt_tx_cancel_wait)
5384 return -EOPNOTSUPP;
5385
5386 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5387 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5388 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5389 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5390 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5391 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5392 return -EOPNOTSUPP;
5393
5394 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5395
5396 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5397}
5398
Kalle Valoffb9eb32010-02-17 17:58:10 +02005399static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5400{
Johannes Berg4c476992010-10-04 21:36:35 +02005401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005402 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005403 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005404 u8 ps_state;
5405 bool state;
5406 int err;
5407
Johannes Berg4c476992010-10-04 21:36:35 +02005408 if (!info->attrs[NL80211_ATTR_PS_STATE])
5409 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005410
5411 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5412
Johannes Berg4c476992010-10-04 21:36:35 +02005413 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5414 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005415
5416 wdev = dev->ieee80211_ptr;
5417
Johannes Berg4c476992010-10-04 21:36:35 +02005418 if (!rdev->ops->set_power_mgmt)
5419 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005420
5421 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5422
5423 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005424 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005425
Johannes Berg4c476992010-10-04 21:36:35 +02005426 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5427 wdev->ps_timeout);
5428 if (!err)
5429 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005430 return err;
5431}
5432
5433static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5434{
Johannes Berg4c476992010-10-04 21:36:35 +02005435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005436 enum nl80211_ps_state ps_state;
5437 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005438 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005439 struct sk_buff *msg;
5440 void *hdr;
5441 int err;
5442
Kalle Valoffb9eb32010-02-17 17:58:10 +02005443 wdev = dev->ieee80211_ptr;
5444
Johannes Berg4c476992010-10-04 21:36:35 +02005445 if (!rdev->ops->set_power_mgmt)
5446 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005447
5448 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005449 if (!msg)
5450 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005451
5452 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5453 NL80211_CMD_GET_POWER_SAVE);
5454 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005455 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005456 goto free_msg;
5457 }
5458
5459 if (wdev->ps)
5460 ps_state = NL80211_PS_ENABLED;
5461 else
5462 ps_state = NL80211_PS_DISABLED;
5463
5464 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5465
5466 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005467 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005468
Johannes Berg4c476992010-10-04 21:36:35 +02005469 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005470 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005471 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005472 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005473 return err;
5474}
5475
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005476static struct nla_policy
5477nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5478 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5479 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5480 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5481};
5482
5483static int nl80211_set_cqm_rssi(struct genl_info *info,
5484 s32 threshold, u32 hysteresis)
5485{
Johannes Berg4c476992010-10-04 21:36:35 +02005486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005487 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005488 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005489
5490 if (threshold > 0)
5491 return -EINVAL;
5492
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005493 wdev = dev->ieee80211_ptr;
5494
Johannes Berg4c476992010-10-04 21:36:35 +02005495 if (!rdev->ops->set_cqm_rssi_config)
5496 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005497
Johannes Berg074ac8d2010-09-16 14:58:22 +02005498 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005499 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5500 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005501
Johannes Berg4c476992010-10-04 21:36:35 +02005502 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5503 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005504}
5505
5506static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5507{
5508 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5509 struct nlattr *cqm;
5510 int err;
5511
5512 cqm = info->attrs[NL80211_ATTR_CQM];
5513 if (!cqm) {
5514 err = -EINVAL;
5515 goto out;
5516 }
5517
5518 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5519 nl80211_attr_cqm_policy);
5520 if (err)
5521 goto out;
5522
5523 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5524 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5525 s32 threshold;
5526 u32 hysteresis;
5527 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5528 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5529 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5530 } else
5531 err = -EINVAL;
5532
5533out:
5534 return err;
5535}
5536
Johannes Berg29cbe682010-12-03 09:20:44 +01005537static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5538{
5539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5540 struct net_device *dev = info->user_ptr[1];
5541 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005542 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005543 int err;
5544
5545 /* start with default */
5546 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005547 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005548
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005549 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005550 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005551 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005552 if (err)
5553 return err;
5554 }
5555
5556 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5557 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5558 return -EINVAL;
5559
Javier Cardonac80d5452010-12-16 17:37:49 -08005560 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5561 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5562
5563 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5564 /* parse additional setup parameters if given */
5565 err = nl80211_parse_mesh_setup(info, &setup);
5566 if (err)
5567 return err;
5568 }
5569
5570 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005571}
5572
5573static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5574{
5575 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5576 struct net_device *dev = info->user_ptr[1];
5577
5578 return cfg80211_leave_mesh(rdev, dev);
5579}
5580
Johannes Bergff1b6e62011-05-04 15:37:28 +02005581static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5582{
5583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5584 struct sk_buff *msg;
5585 void *hdr;
5586
5587 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5588 return -EOPNOTSUPP;
5589
5590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5591 if (!msg)
5592 return -ENOMEM;
5593
5594 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5595 NL80211_CMD_GET_WOWLAN);
5596 if (!hdr)
5597 goto nla_put_failure;
5598
5599 if (rdev->wowlan) {
5600 struct nlattr *nl_wowlan;
5601
5602 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5603 if (!nl_wowlan)
5604 goto nla_put_failure;
5605
5606 if (rdev->wowlan->any)
5607 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5608 if (rdev->wowlan->disconnect)
5609 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5610 if (rdev->wowlan->magic_pkt)
5611 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005612 if (rdev->wowlan->gtk_rekey_failure)
5613 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5614 if (rdev->wowlan->eap_identity_req)
5615 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5616 if (rdev->wowlan->four_way_handshake)
5617 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5618 if (rdev->wowlan->rfkill_release)
5619 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005620 if (rdev->wowlan->n_patterns) {
5621 struct nlattr *nl_pats, *nl_pat;
5622 int i, pat_len;
5623
5624 nl_pats = nla_nest_start(msg,
5625 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5626 if (!nl_pats)
5627 goto nla_put_failure;
5628
5629 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5630 nl_pat = nla_nest_start(msg, i + 1);
5631 if (!nl_pat)
5632 goto nla_put_failure;
5633 pat_len = rdev->wowlan->patterns[i].pattern_len;
5634 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5635 DIV_ROUND_UP(pat_len, 8),
5636 rdev->wowlan->patterns[i].mask);
5637 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5638 pat_len,
5639 rdev->wowlan->patterns[i].pattern);
5640 nla_nest_end(msg, nl_pat);
5641 }
5642 nla_nest_end(msg, nl_pats);
5643 }
5644
5645 nla_nest_end(msg, nl_wowlan);
5646 }
5647
5648 genlmsg_end(msg, hdr);
5649 return genlmsg_reply(msg, info);
5650
5651nla_put_failure:
5652 nlmsg_free(msg);
5653 return -ENOBUFS;
5654}
5655
5656static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5657{
5658 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5659 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5660 struct cfg80211_wowlan no_triggers = {};
5661 struct cfg80211_wowlan new_triggers = {};
5662 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5663 int err, i;
5664
5665 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5666 return -EOPNOTSUPP;
5667
5668 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5669 goto no_triggers;
5670
5671 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5672 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5673 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5674 nl80211_wowlan_policy);
5675 if (err)
5676 return err;
5677
5678 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5679 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5680 return -EINVAL;
5681 new_triggers.any = true;
5682 }
5683
5684 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5685 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5686 return -EINVAL;
5687 new_triggers.disconnect = true;
5688 }
5689
5690 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5691 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5692 return -EINVAL;
5693 new_triggers.magic_pkt = true;
5694 }
5695
Johannes Berg77dbbb12011-07-13 10:48:55 +02005696 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5697 return -EINVAL;
5698
5699 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5700 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5701 return -EINVAL;
5702 new_triggers.gtk_rekey_failure = true;
5703 }
5704
5705 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5706 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5707 return -EINVAL;
5708 new_triggers.eap_identity_req = true;
5709 }
5710
5711 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5712 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5713 return -EINVAL;
5714 new_triggers.four_way_handshake = true;
5715 }
5716
5717 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5718 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5719 return -EINVAL;
5720 new_triggers.rfkill_release = true;
5721 }
5722
Johannes Bergff1b6e62011-05-04 15:37:28 +02005723 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5724 struct nlattr *pat;
5725 int n_patterns = 0;
5726 int rem, pat_len, mask_len;
5727 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5728
5729 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5730 rem)
5731 n_patterns++;
5732 if (n_patterns > wowlan->n_patterns)
5733 return -EINVAL;
5734
5735 new_triggers.patterns = kcalloc(n_patterns,
5736 sizeof(new_triggers.patterns[0]),
5737 GFP_KERNEL);
5738 if (!new_triggers.patterns)
5739 return -ENOMEM;
5740
5741 new_triggers.n_patterns = n_patterns;
5742 i = 0;
5743
5744 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5745 rem) {
5746 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5747 nla_data(pat), nla_len(pat), NULL);
5748 err = -EINVAL;
5749 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5750 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5751 goto error;
5752 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5753 mask_len = DIV_ROUND_UP(pat_len, 8);
5754 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5755 mask_len)
5756 goto error;
5757 if (pat_len > wowlan->pattern_max_len ||
5758 pat_len < wowlan->pattern_min_len)
5759 goto error;
5760
5761 new_triggers.patterns[i].mask =
5762 kmalloc(mask_len + pat_len, GFP_KERNEL);
5763 if (!new_triggers.patterns[i].mask) {
5764 err = -ENOMEM;
5765 goto error;
5766 }
5767 new_triggers.patterns[i].pattern =
5768 new_triggers.patterns[i].mask + mask_len;
5769 memcpy(new_triggers.patterns[i].mask,
5770 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5771 mask_len);
5772 new_triggers.patterns[i].pattern_len = pat_len;
5773 memcpy(new_triggers.patterns[i].pattern,
5774 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5775 pat_len);
5776 i++;
5777 }
5778 }
5779
5780 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5781 struct cfg80211_wowlan *ntrig;
5782 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5783 GFP_KERNEL);
5784 if (!ntrig) {
5785 err = -ENOMEM;
5786 goto error;
5787 }
5788 cfg80211_rdev_free_wowlan(rdev);
5789 rdev->wowlan = ntrig;
5790 } else {
5791 no_triggers:
5792 cfg80211_rdev_free_wowlan(rdev);
5793 rdev->wowlan = NULL;
5794 }
5795
5796 return 0;
5797 error:
5798 for (i = 0; i < new_triggers.n_patterns; i++)
5799 kfree(new_triggers.patterns[i].mask);
5800 kfree(new_triggers.patterns);
5801 return err;
5802}
5803
Johannes Berge5497d72011-07-05 16:35:40 +02005804static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5805{
5806 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5807 struct net_device *dev = info->user_ptr[1];
5808 struct wireless_dev *wdev = dev->ieee80211_ptr;
5809 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5810 struct cfg80211_gtk_rekey_data rekey_data;
5811 int err;
5812
5813 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5814 return -EINVAL;
5815
5816 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5817 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5818 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5819 nl80211_rekey_policy);
5820 if (err)
5821 return err;
5822
5823 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5824 return -ERANGE;
5825 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5826 return -ERANGE;
5827 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5828 return -ERANGE;
5829
5830 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5831 NL80211_KEK_LEN);
5832 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5833 NL80211_KCK_LEN);
5834 memcpy(rekey_data.replay_ctr,
5835 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5836 NL80211_REPLAY_CTR_LEN);
5837
5838 wdev_lock(wdev);
5839 if (!wdev->current_bss) {
5840 err = -ENOTCONN;
5841 goto out;
5842 }
5843
5844 if (!rdev->ops->set_rekey_data) {
5845 err = -EOPNOTSUPP;
5846 goto out;
5847 }
5848
5849 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5850 out:
5851 wdev_unlock(wdev);
5852 return err;
5853}
5854
Johannes Berg28946da2011-11-04 11:18:12 +01005855static int nl80211_register_unexpected_frame(struct sk_buff *skb,
5856 struct genl_info *info)
5857{
5858 struct net_device *dev = info->user_ptr[1];
5859 struct wireless_dev *wdev = dev->ieee80211_ptr;
5860
5861 if (wdev->iftype != NL80211_IFTYPE_AP &&
5862 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5863 return -EINVAL;
5864
5865 if (wdev->ap_unexpected_nlpid)
5866 return -EBUSY;
5867
5868 wdev->ap_unexpected_nlpid = info->snd_pid;
5869 return 0;
5870}
5871
Johannes Berg7f6cf312011-11-04 11:18:15 +01005872static int nl80211_probe_client(struct sk_buff *skb,
5873 struct genl_info *info)
5874{
5875 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5876 struct net_device *dev = info->user_ptr[1];
5877 struct wireless_dev *wdev = dev->ieee80211_ptr;
5878 struct sk_buff *msg;
5879 void *hdr;
5880 const u8 *addr;
5881 u64 cookie;
5882 int err;
5883
5884 if (wdev->iftype != NL80211_IFTYPE_AP &&
5885 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5886 return -EOPNOTSUPP;
5887
5888 if (!info->attrs[NL80211_ATTR_MAC])
5889 return -EINVAL;
5890
5891 if (!rdev->ops->probe_client)
5892 return -EOPNOTSUPP;
5893
5894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5895 if (!msg)
5896 return -ENOMEM;
5897
5898 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5899 NL80211_CMD_PROBE_CLIENT);
5900
5901 if (IS_ERR(hdr)) {
5902 err = PTR_ERR(hdr);
5903 goto free_msg;
5904 }
5905
5906 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5907
5908 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
5909 if (err)
5910 goto free_msg;
5911
5912 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5913
5914 genlmsg_end(msg, hdr);
5915
5916 return genlmsg_reply(msg, info);
5917
5918 nla_put_failure:
5919 err = -ENOBUFS;
5920 free_msg:
5921 nlmsg_free(msg);
5922 return err;
5923}
5924
Johannes Berg5e760232011-11-04 11:18:17 +01005925static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
5926{
5927 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5928
5929 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
5930 return -EOPNOTSUPP;
5931
5932 if (rdev->ap_beacons_nlpid)
5933 return -EBUSY;
5934
5935 rdev->ap_beacons_nlpid = info->snd_pid;
5936
5937 return 0;
5938}
5939
Johannes Berg4c476992010-10-04 21:36:35 +02005940#define NL80211_FLAG_NEED_WIPHY 0x01
5941#define NL80211_FLAG_NEED_NETDEV 0x02
5942#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005943#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5944#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5945 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005946
5947static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5948 struct genl_info *info)
5949{
5950 struct cfg80211_registered_device *rdev;
5951 struct net_device *dev;
5952 int err;
5953 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5954
5955 if (rtnl)
5956 rtnl_lock();
5957
5958 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5959 rdev = cfg80211_get_dev_from_info(info);
5960 if (IS_ERR(rdev)) {
5961 if (rtnl)
5962 rtnl_unlock();
5963 return PTR_ERR(rdev);
5964 }
5965 info->user_ptr[0] = rdev;
5966 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5967 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5968 if (err) {
5969 if (rtnl)
5970 rtnl_unlock();
5971 return err;
5972 }
Johannes Berg41265712010-10-04 21:14:05 +02005973 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5974 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005975 cfg80211_unlock_rdev(rdev);
5976 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005977 if (rtnl)
5978 rtnl_unlock();
5979 return -ENETDOWN;
5980 }
Johannes Berg4c476992010-10-04 21:36:35 +02005981 info->user_ptr[0] = rdev;
5982 info->user_ptr[1] = dev;
5983 }
5984
5985 return 0;
5986}
5987
5988static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5989 struct genl_info *info)
5990{
5991 if (info->user_ptr[0])
5992 cfg80211_unlock_rdev(info->user_ptr[0]);
5993 if (info->user_ptr[1])
5994 dev_put(info->user_ptr[1]);
5995 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5996 rtnl_unlock();
5997}
5998
Johannes Berg55682962007-09-20 13:09:35 -04005999static struct genl_ops nl80211_ops[] = {
6000 {
6001 .cmd = NL80211_CMD_GET_WIPHY,
6002 .doit = nl80211_get_wiphy,
6003 .dumpit = nl80211_dump_wiphy,
6004 .policy = nl80211_policy,
6005 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006006 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006007 },
6008 {
6009 .cmd = NL80211_CMD_SET_WIPHY,
6010 .doit = nl80211_set_wiphy,
6011 .policy = nl80211_policy,
6012 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006013 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006014 },
6015 {
6016 .cmd = NL80211_CMD_GET_INTERFACE,
6017 .doit = nl80211_get_interface,
6018 .dumpit = nl80211_dump_interface,
6019 .policy = nl80211_policy,
6020 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006021 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006022 },
6023 {
6024 .cmd = NL80211_CMD_SET_INTERFACE,
6025 .doit = nl80211_set_interface,
6026 .policy = nl80211_policy,
6027 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006028 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6029 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006030 },
6031 {
6032 .cmd = NL80211_CMD_NEW_INTERFACE,
6033 .doit = nl80211_new_interface,
6034 .policy = nl80211_policy,
6035 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006036 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6037 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006038 },
6039 {
6040 .cmd = NL80211_CMD_DEL_INTERFACE,
6041 .doit = nl80211_del_interface,
6042 .policy = nl80211_policy,
6043 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006044 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6045 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006046 },
Johannes Berg41ade002007-12-19 02:03:29 +01006047 {
6048 .cmd = NL80211_CMD_GET_KEY,
6049 .doit = nl80211_get_key,
6050 .policy = nl80211_policy,
6051 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006052 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6053 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006054 },
6055 {
6056 .cmd = NL80211_CMD_SET_KEY,
6057 .doit = nl80211_set_key,
6058 .policy = nl80211_policy,
6059 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006060 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006061 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006062 },
6063 {
6064 .cmd = NL80211_CMD_NEW_KEY,
6065 .doit = nl80211_new_key,
6066 .policy = nl80211_policy,
6067 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006068 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006069 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006070 },
6071 {
6072 .cmd = NL80211_CMD_DEL_KEY,
6073 .doit = nl80211_del_key,
6074 .policy = nl80211_policy,
6075 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006076 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006077 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006078 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006079 {
6080 .cmd = NL80211_CMD_SET_BEACON,
6081 .policy = nl80211_policy,
6082 .flags = GENL_ADMIN_PERM,
6083 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006084 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6085 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006086 },
6087 {
6088 .cmd = NL80211_CMD_NEW_BEACON,
6089 .policy = nl80211_policy,
6090 .flags = GENL_ADMIN_PERM,
6091 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006092 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6093 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006094 },
6095 {
6096 .cmd = NL80211_CMD_DEL_BEACON,
6097 .policy = nl80211_policy,
6098 .flags = GENL_ADMIN_PERM,
6099 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006100 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6101 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006102 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006103 {
6104 .cmd = NL80211_CMD_GET_STATION,
6105 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006106 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006107 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006108 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6109 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006110 },
6111 {
6112 .cmd = NL80211_CMD_SET_STATION,
6113 .doit = nl80211_set_station,
6114 .policy = nl80211_policy,
6115 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006116 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6117 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006118 },
6119 {
6120 .cmd = NL80211_CMD_NEW_STATION,
6121 .doit = nl80211_new_station,
6122 .policy = nl80211_policy,
6123 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006124 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006125 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006126 },
6127 {
6128 .cmd = NL80211_CMD_DEL_STATION,
6129 .doit = nl80211_del_station,
6130 .policy = nl80211_policy,
6131 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006132 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6133 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006134 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006135 {
6136 .cmd = NL80211_CMD_GET_MPATH,
6137 .doit = nl80211_get_mpath,
6138 .dumpit = nl80211_dump_mpath,
6139 .policy = nl80211_policy,
6140 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006141 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006142 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006143 },
6144 {
6145 .cmd = NL80211_CMD_SET_MPATH,
6146 .doit = nl80211_set_mpath,
6147 .policy = nl80211_policy,
6148 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006149 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006150 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006151 },
6152 {
6153 .cmd = NL80211_CMD_NEW_MPATH,
6154 .doit = nl80211_new_mpath,
6155 .policy = nl80211_policy,
6156 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006157 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006158 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006159 },
6160 {
6161 .cmd = NL80211_CMD_DEL_MPATH,
6162 .doit = nl80211_del_mpath,
6163 .policy = nl80211_policy,
6164 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006165 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6166 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006167 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006168 {
6169 .cmd = NL80211_CMD_SET_BSS,
6170 .doit = nl80211_set_bss,
6171 .policy = nl80211_policy,
6172 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006173 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6174 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006175 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006176 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006177 .cmd = NL80211_CMD_GET_REG,
6178 .doit = nl80211_get_reg,
6179 .policy = nl80211_policy,
6180 /* can be retrieved by unprivileged users */
6181 },
6182 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006183 .cmd = NL80211_CMD_SET_REG,
6184 .doit = nl80211_set_reg,
6185 .policy = nl80211_policy,
6186 .flags = GENL_ADMIN_PERM,
6187 },
6188 {
6189 .cmd = NL80211_CMD_REQ_SET_REG,
6190 .doit = nl80211_req_set_reg,
6191 .policy = nl80211_policy,
6192 .flags = GENL_ADMIN_PERM,
6193 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006194 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006195 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6196 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006197 .policy = nl80211_policy,
6198 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006199 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6200 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006201 },
6202 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006203 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6204 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006205 .policy = nl80211_policy,
6206 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006207 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006208 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006209 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006210 {
Johannes Berg2a519312009-02-10 21:25:55 +01006211 .cmd = NL80211_CMD_TRIGGER_SCAN,
6212 .doit = nl80211_trigger_scan,
6213 .policy = nl80211_policy,
6214 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006216 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006217 },
6218 {
6219 .cmd = NL80211_CMD_GET_SCAN,
6220 .policy = nl80211_policy,
6221 .dumpit = nl80211_dump_scan,
6222 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006223 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006224 .cmd = NL80211_CMD_START_SCHED_SCAN,
6225 .doit = nl80211_start_sched_scan,
6226 .policy = nl80211_policy,
6227 .flags = GENL_ADMIN_PERM,
6228 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6229 NL80211_FLAG_NEED_RTNL,
6230 },
6231 {
6232 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6233 .doit = nl80211_stop_sched_scan,
6234 .policy = nl80211_policy,
6235 .flags = GENL_ADMIN_PERM,
6236 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6237 NL80211_FLAG_NEED_RTNL,
6238 },
6239 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006240 .cmd = NL80211_CMD_AUTHENTICATE,
6241 .doit = nl80211_authenticate,
6242 .policy = nl80211_policy,
6243 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006244 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006245 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006246 },
6247 {
6248 .cmd = NL80211_CMD_ASSOCIATE,
6249 .doit = nl80211_associate,
6250 .policy = nl80211_policy,
6251 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006252 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006253 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006254 },
6255 {
6256 .cmd = NL80211_CMD_DEAUTHENTICATE,
6257 .doit = nl80211_deauthenticate,
6258 .policy = nl80211_policy,
6259 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006260 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006261 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006262 },
6263 {
6264 .cmd = NL80211_CMD_DISASSOCIATE,
6265 .doit = nl80211_disassociate,
6266 .policy = nl80211_policy,
6267 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006268 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006269 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006270 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006271 {
6272 .cmd = NL80211_CMD_JOIN_IBSS,
6273 .doit = nl80211_join_ibss,
6274 .policy = nl80211_policy,
6275 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006276 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006277 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006278 },
6279 {
6280 .cmd = NL80211_CMD_LEAVE_IBSS,
6281 .doit = nl80211_leave_ibss,
6282 .policy = nl80211_policy,
6283 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006284 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006285 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006286 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006287#ifdef CONFIG_NL80211_TESTMODE
6288 {
6289 .cmd = NL80211_CMD_TESTMODE,
6290 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006291 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006292 .policy = nl80211_policy,
6293 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006294 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6295 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006296 },
6297#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006298 {
6299 .cmd = NL80211_CMD_CONNECT,
6300 .doit = nl80211_connect,
6301 .policy = nl80211_policy,
6302 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006303 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006304 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006305 },
6306 {
6307 .cmd = NL80211_CMD_DISCONNECT,
6308 .doit = nl80211_disconnect,
6309 .policy = nl80211_policy,
6310 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006312 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006313 },
Johannes Berg463d0182009-07-14 00:33:35 +02006314 {
6315 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6316 .doit = nl80211_wiphy_netns,
6317 .policy = nl80211_policy,
6318 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006319 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6320 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006321 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006322 {
6323 .cmd = NL80211_CMD_GET_SURVEY,
6324 .policy = nl80211_policy,
6325 .dumpit = nl80211_dump_survey,
6326 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006327 {
6328 .cmd = NL80211_CMD_SET_PMKSA,
6329 .doit = nl80211_setdel_pmksa,
6330 .policy = nl80211_policy,
6331 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006332 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6333 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006334 },
6335 {
6336 .cmd = NL80211_CMD_DEL_PMKSA,
6337 .doit = nl80211_setdel_pmksa,
6338 .policy = nl80211_policy,
6339 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006340 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6341 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006342 },
6343 {
6344 .cmd = NL80211_CMD_FLUSH_PMKSA,
6345 .doit = nl80211_flush_pmksa,
6346 .policy = nl80211_policy,
6347 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006348 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6349 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006350 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006351 {
6352 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6353 .doit = nl80211_remain_on_channel,
6354 .policy = nl80211_policy,
6355 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006356 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006357 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006358 },
6359 {
6360 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6361 .doit = nl80211_cancel_remain_on_channel,
6362 .policy = nl80211_policy,
6363 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006364 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006365 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006366 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006367 {
6368 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6369 .doit = nl80211_set_tx_bitrate_mask,
6370 .policy = nl80211_policy,
6371 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006372 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6373 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006374 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006375 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006376 .cmd = NL80211_CMD_REGISTER_FRAME,
6377 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006378 .policy = nl80211_policy,
6379 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006380 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6381 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006382 },
6383 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006384 .cmd = NL80211_CMD_FRAME,
6385 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006386 .policy = nl80211_policy,
6387 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006388 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006389 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006390 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006391 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006392 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6393 .doit = nl80211_tx_mgmt_cancel_wait,
6394 .policy = nl80211_policy,
6395 .flags = GENL_ADMIN_PERM,
6396 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6397 NL80211_FLAG_NEED_RTNL,
6398 },
6399 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006400 .cmd = NL80211_CMD_SET_POWER_SAVE,
6401 .doit = nl80211_set_power_save,
6402 .policy = nl80211_policy,
6403 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006404 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6405 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006406 },
6407 {
6408 .cmd = NL80211_CMD_GET_POWER_SAVE,
6409 .doit = nl80211_get_power_save,
6410 .policy = nl80211_policy,
6411 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006412 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6413 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006414 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006415 {
6416 .cmd = NL80211_CMD_SET_CQM,
6417 .doit = nl80211_set_cqm,
6418 .policy = nl80211_policy,
6419 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006420 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6421 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006422 },
Johannes Bergf444de02010-05-05 15:25:02 +02006423 {
6424 .cmd = NL80211_CMD_SET_CHANNEL,
6425 .doit = nl80211_set_channel,
6426 .policy = nl80211_policy,
6427 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006428 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6429 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006430 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006431 {
6432 .cmd = NL80211_CMD_SET_WDS_PEER,
6433 .doit = nl80211_set_wds_peer,
6434 .policy = nl80211_policy,
6435 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006436 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6437 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006438 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006439 {
6440 .cmd = NL80211_CMD_JOIN_MESH,
6441 .doit = nl80211_join_mesh,
6442 .policy = nl80211_policy,
6443 .flags = GENL_ADMIN_PERM,
6444 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6445 NL80211_FLAG_NEED_RTNL,
6446 },
6447 {
6448 .cmd = NL80211_CMD_LEAVE_MESH,
6449 .doit = nl80211_leave_mesh,
6450 .policy = nl80211_policy,
6451 .flags = GENL_ADMIN_PERM,
6452 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6453 NL80211_FLAG_NEED_RTNL,
6454 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006455 {
6456 .cmd = NL80211_CMD_GET_WOWLAN,
6457 .doit = nl80211_get_wowlan,
6458 .policy = nl80211_policy,
6459 /* can be retrieved by unprivileged users */
6460 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6461 NL80211_FLAG_NEED_RTNL,
6462 },
6463 {
6464 .cmd = NL80211_CMD_SET_WOWLAN,
6465 .doit = nl80211_set_wowlan,
6466 .policy = nl80211_policy,
6467 .flags = GENL_ADMIN_PERM,
6468 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6469 NL80211_FLAG_NEED_RTNL,
6470 },
Johannes Berge5497d72011-07-05 16:35:40 +02006471 {
6472 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6473 .doit = nl80211_set_rekey_data,
6474 .policy = nl80211_policy,
6475 .flags = GENL_ADMIN_PERM,
6476 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6477 NL80211_FLAG_NEED_RTNL,
6478 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006479 {
6480 .cmd = NL80211_CMD_TDLS_MGMT,
6481 .doit = nl80211_tdls_mgmt,
6482 .policy = nl80211_policy,
6483 .flags = GENL_ADMIN_PERM,
6484 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6485 NL80211_FLAG_NEED_RTNL,
6486 },
6487 {
6488 .cmd = NL80211_CMD_TDLS_OPER,
6489 .doit = nl80211_tdls_oper,
6490 .policy = nl80211_policy,
6491 .flags = GENL_ADMIN_PERM,
6492 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6493 NL80211_FLAG_NEED_RTNL,
6494 },
Johannes Berg28946da2011-11-04 11:18:12 +01006495 {
6496 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6497 .doit = nl80211_register_unexpected_frame,
6498 .policy = nl80211_policy,
6499 .flags = GENL_ADMIN_PERM,
6500 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6501 NL80211_FLAG_NEED_RTNL,
6502 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006503 {
6504 .cmd = NL80211_CMD_PROBE_CLIENT,
6505 .doit = nl80211_probe_client,
6506 .policy = nl80211_policy,
6507 .flags = GENL_ADMIN_PERM,
6508 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6509 NL80211_FLAG_NEED_RTNL,
6510 },
Johannes Berg5e760232011-11-04 11:18:17 +01006511 {
6512 .cmd = NL80211_CMD_REGISTER_BEACONS,
6513 .doit = nl80211_register_beacons,
6514 .policy = nl80211_policy,
6515 .flags = GENL_ADMIN_PERM,
6516 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6517 NL80211_FLAG_NEED_RTNL,
6518 },
Johannes Berg55682962007-09-20 13:09:35 -04006519};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006520
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006521static struct genl_multicast_group nl80211_mlme_mcgrp = {
6522 .name = "mlme",
6523};
Johannes Berg55682962007-09-20 13:09:35 -04006524
6525/* multicast groups */
6526static struct genl_multicast_group nl80211_config_mcgrp = {
6527 .name = "config",
6528};
Johannes Berg2a519312009-02-10 21:25:55 +01006529static struct genl_multicast_group nl80211_scan_mcgrp = {
6530 .name = "scan",
6531};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006532static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6533 .name = "regulatory",
6534};
Johannes Berg55682962007-09-20 13:09:35 -04006535
6536/* notification functions */
6537
6538void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6539{
6540 struct sk_buff *msg;
6541
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006542 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006543 if (!msg)
6544 return;
6545
6546 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6547 nlmsg_free(msg);
6548 return;
6549 }
6550
Johannes Berg463d0182009-07-14 00:33:35 +02006551 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6552 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006553}
6554
Johannes Berg362a4152009-05-24 16:43:15 +02006555static int nl80211_add_scan_req(struct sk_buff *msg,
6556 struct cfg80211_registered_device *rdev)
6557{
6558 struct cfg80211_scan_request *req = rdev->scan_req;
6559 struct nlattr *nest;
6560 int i;
6561
Johannes Berg667503d2009-07-07 03:56:11 +02006562 ASSERT_RDEV_LOCK(rdev);
6563
Johannes Berg362a4152009-05-24 16:43:15 +02006564 if (WARN_ON(!req))
6565 return 0;
6566
6567 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6568 if (!nest)
6569 goto nla_put_failure;
6570 for (i = 0; i < req->n_ssids; i++)
6571 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6572 nla_nest_end(msg, nest);
6573
6574 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6575 if (!nest)
6576 goto nla_put_failure;
6577 for (i = 0; i < req->n_channels; i++)
6578 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6579 nla_nest_end(msg, nest);
6580
6581 if (req->ie)
6582 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6583
6584 return 0;
6585 nla_put_failure:
6586 return -ENOBUFS;
6587}
6588
Johannes Berga538e2d2009-06-16 19:56:42 +02006589static int nl80211_send_scan_msg(struct sk_buff *msg,
6590 struct cfg80211_registered_device *rdev,
6591 struct net_device *netdev,
6592 u32 pid, u32 seq, int flags,
6593 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006594{
6595 void *hdr;
6596
6597 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6598 if (!hdr)
6599 return -1;
6600
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006601 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006602 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6603
Johannes Berg362a4152009-05-24 16:43:15 +02006604 /* ignore errors and send incomplete event anyway */
6605 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006606
6607 return genlmsg_end(msg, hdr);
6608
6609 nla_put_failure:
6610 genlmsg_cancel(msg, hdr);
6611 return -EMSGSIZE;
6612}
6613
Luciano Coelho807f8a82011-05-11 17:09:35 +03006614static int
6615nl80211_send_sched_scan_msg(struct sk_buff *msg,
6616 struct cfg80211_registered_device *rdev,
6617 struct net_device *netdev,
6618 u32 pid, u32 seq, int flags, u32 cmd)
6619{
6620 void *hdr;
6621
6622 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6623 if (!hdr)
6624 return -1;
6625
6626 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6627 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6628
6629 return genlmsg_end(msg, hdr);
6630
6631 nla_put_failure:
6632 genlmsg_cancel(msg, hdr);
6633 return -EMSGSIZE;
6634}
6635
Johannes Berga538e2d2009-06-16 19:56:42 +02006636void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6637 struct net_device *netdev)
6638{
6639 struct sk_buff *msg;
6640
6641 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6642 if (!msg)
6643 return;
6644
6645 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6646 NL80211_CMD_TRIGGER_SCAN) < 0) {
6647 nlmsg_free(msg);
6648 return;
6649 }
6650
Johannes Berg463d0182009-07-14 00:33:35 +02006651 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6652 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006653}
6654
Johannes Berg2a519312009-02-10 21:25:55 +01006655void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6656 struct net_device *netdev)
6657{
6658 struct sk_buff *msg;
6659
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006661 if (!msg)
6662 return;
6663
Johannes Berga538e2d2009-06-16 19:56:42 +02006664 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6665 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006666 nlmsg_free(msg);
6667 return;
6668 }
6669
Johannes Berg463d0182009-07-14 00:33:35 +02006670 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6671 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006672}
6673
6674void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6675 struct net_device *netdev)
6676{
6677 struct sk_buff *msg;
6678
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006679 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006680 if (!msg)
6681 return;
6682
Johannes Berga538e2d2009-06-16 19:56:42 +02006683 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6684 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006685 nlmsg_free(msg);
6686 return;
6687 }
6688
Johannes Berg463d0182009-07-14 00:33:35 +02006689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6690 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006691}
6692
Luciano Coelho807f8a82011-05-11 17:09:35 +03006693void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6694 struct net_device *netdev)
6695{
6696 struct sk_buff *msg;
6697
6698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6699 if (!msg)
6700 return;
6701
6702 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6703 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6704 nlmsg_free(msg);
6705 return;
6706 }
6707
6708 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6709 nl80211_scan_mcgrp.id, GFP_KERNEL);
6710}
6711
6712void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6713 struct net_device *netdev, u32 cmd)
6714{
6715 struct sk_buff *msg;
6716
6717 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6718 if (!msg)
6719 return;
6720
6721 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6722 nlmsg_free(msg);
6723 return;
6724 }
6725
6726 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6727 nl80211_scan_mcgrp.id, GFP_KERNEL);
6728}
6729
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006730/*
6731 * This can happen on global regulatory changes or device specific settings
6732 * based on custom world regulatory domains.
6733 */
6734void nl80211_send_reg_change_event(struct regulatory_request *request)
6735{
6736 struct sk_buff *msg;
6737 void *hdr;
6738
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006740 if (!msg)
6741 return;
6742
6743 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6744 if (!hdr) {
6745 nlmsg_free(msg);
6746 return;
6747 }
6748
6749 /* Userspace can always count this one always being set */
6750 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6751
6752 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6753 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6754 NL80211_REGDOM_TYPE_WORLD);
6755 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6756 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6757 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6758 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6759 request->intersect)
6760 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6761 NL80211_REGDOM_TYPE_INTERSECTION);
6762 else {
6763 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6764 NL80211_REGDOM_TYPE_COUNTRY);
6765 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6766 }
6767
6768 if (wiphy_idx_valid(request->wiphy_idx))
6769 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6770
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006771 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006772
Johannes Bergbc43b282009-07-25 10:54:13 +02006773 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006774 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006775 GFP_ATOMIC);
6776 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006777
6778 return;
6779
6780nla_put_failure:
6781 genlmsg_cancel(msg, hdr);
6782 nlmsg_free(msg);
6783}
6784
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006785static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6786 struct net_device *netdev,
6787 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006788 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006789{
6790 struct sk_buff *msg;
6791 void *hdr;
6792
Johannes Berge6d6e342009-07-01 21:26:47 +02006793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006794 if (!msg)
6795 return;
6796
6797 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6798 if (!hdr) {
6799 nlmsg_free(msg);
6800 return;
6801 }
6802
6803 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6804 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6805 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6806
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006807 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006808
Johannes Berg463d0182009-07-14 00:33:35 +02006809 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6810 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006811 return;
6812
6813 nla_put_failure:
6814 genlmsg_cancel(msg, hdr);
6815 nlmsg_free(msg);
6816}
6817
6818void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006819 struct net_device *netdev, const u8 *buf,
6820 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006821{
6822 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006823 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006824}
6825
6826void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6827 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006828 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006829{
Johannes Berge6d6e342009-07-01 21:26:47 +02006830 nl80211_send_mlme_event(rdev, netdev, buf, len,
6831 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006832}
6833
Jouni Malinen53b46b82009-03-27 20:53:56 +02006834void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006835 struct net_device *netdev, const u8 *buf,
6836 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006837{
6838 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006839 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006840}
6841
Jouni Malinen53b46b82009-03-27 20:53:56 +02006842void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6843 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006844 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006845{
6846 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006847 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006848}
6849
Jouni Malinencf4e5942010-12-16 00:52:40 +02006850void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6851 struct net_device *netdev, const u8 *buf,
6852 size_t len, gfp_t gfp)
6853{
6854 nl80211_send_mlme_event(rdev, netdev, buf, len,
6855 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6856}
6857
6858void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6859 struct net_device *netdev, const u8 *buf,
6860 size_t len, gfp_t gfp)
6861{
6862 nl80211_send_mlme_event(rdev, netdev, buf, len,
6863 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6864}
6865
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006866static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6867 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006868 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006869{
6870 struct sk_buff *msg;
6871 void *hdr;
6872
Johannes Berge6d6e342009-07-01 21:26:47 +02006873 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006874 if (!msg)
6875 return;
6876
6877 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6878 if (!hdr) {
6879 nlmsg_free(msg);
6880 return;
6881 }
6882
6883 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6884 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6885 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6886 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6887
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006888 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03006889
Johannes Berg463d0182009-07-14 00:33:35 +02006890 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6891 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006892 return;
6893
6894 nla_put_failure:
6895 genlmsg_cancel(msg, hdr);
6896 nlmsg_free(msg);
6897}
6898
6899void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006900 struct net_device *netdev, const u8 *addr,
6901 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006902{
6903 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006904 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006905}
6906
6907void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006908 struct net_device *netdev, const u8 *addr,
6909 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006910{
Johannes Berge6d6e342009-07-01 21:26:47 +02006911 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6912 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006913}
6914
Samuel Ortizb23aa672009-07-01 21:26:54 +02006915void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6916 struct net_device *netdev, const u8 *bssid,
6917 const u8 *req_ie, size_t req_ie_len,
6918 const u8 *resp_ie, size_t resp_ie_len,
6919 u16 status, gfp_t gfp)
6920{
6921 struct sk_buff *msg;
6922 void *hdr;
6923
6924 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6925 if (!msg)
6926 return;
6927
6928 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6929 if (!hdr) {
6930 nlmsg_free(msg);
6931 return;
6932 }
6933
6934 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6935 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6936 if (bssid)
6937 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6938 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6939 if (req_ie)
6940 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6941 if (resp_ie)
6942 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6943
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006944 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006945
Johannes Berg463d0182009-07-14 00:33:35 +02006946 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6947 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006948 return;
6949
6950 nla_put_failure:
6951 genlmsg_cancel(msg, hdr);
6952 nlmsg_free(msg);
6953
6954}
6955
6956void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6957 struct net_device *netdev, const u8 *bssid,
6958 const u8 *req_ie, size_t req_ie_len,
6959 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6960{
6961 struct sk_buff *msg;
6962 void *hdr;
6963
6964 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6965 if (!msg)
6966 return;
6967
6968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6969 if (!hdr) {
6970 nlmsg_free(msg);
6971 return;
6972 }
6973
6974 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6975 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6976 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6977 if (req_ie)
6978 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6979 if (resp_ie)
6980 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6981
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006982 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006983
Johannes Berg463d0182009-07-14 00:33:35 +02006984 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6985 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006986 return;
6987
6988 nla_put_failure:
6989 genlmsg_cancel(msg, hdr);
6990 nlmsg_free(msg);
6991
6992}
6993
6994void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6995 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006996 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006997{
6998 struct sk_buff *msg;
6999 void *hdr;
7000
Johannes Berg667503d2009-07-07 03:56:11 +02007001 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007002 if (!msg)
7003 return;
7004
7005 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7006 if (!hdr) {
7007 nlmsg_free(msg);
7008 return;
7009 }
7010
7011 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7012 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7013 if (from_ap && reason)
7014 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7015 if (from_ap)
7016 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7017 if (ie)
7018 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7019
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007020 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007021
Johannes Berg463d0182009-07-14 00:33:35 +02007022 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7023 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007024 return;
7025
7026 nla_put_failure:
7027 genlmsg_cancel(msg, hdr);
7028 nlmsg_free(msg);
7029
7030}
7031
Johannes Berg04a773a2009-04-19 21:24:32 +02007032void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7033 struct net_device *netdev, const u8 *bssid,
7034 gfp_t gfp)
7035{
7036 struct sk_buff *msg;
7037 void *hdr;
7038
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007039 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007040 if (!msg)
7041 return;
7042
7043 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7044 if (!hdr) {
7045 nlmsg_free(msg);
7046 return;
7047 }
7048
7049 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7051 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7052
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007053 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007054
Johannes Berg463d0182009-07-14 00:33:35 +02007055 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7056 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007057 return;
7058
7059 nla_put_failure:
7060 genlmsg_cancel(msg, hdr);
7061 nlmsg_free(msg);
7062}
7063
Javier Cardonac93b5e72011-04-07 15:08:34 -07007064void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7065 struct net_device *netdev,
7066 const u8 *macaddr, const u8* ie, u8 ie_len,
7067 gfp_t gfp)
7068{
7069 struct sk_buff *msg;
7070 void *hdr;
7071
7072 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7073 if (!msg)
7074 return;
7075
7076 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7077 if (!hdr) {
7078 nlmsg_free(msg);
7079 return;
7080 }
7081
7082 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7083 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7084 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7085 if (ie_len && ie)
7086 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7087
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007088 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007089
7090 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7091 nl80211_mlme_mcgrp.id, gfp);
7092 return;
7093
7094 nla_put_failure:
7095 genlmsg_cancel(msg, hdr);
7096 nlmsg_free(msg);
7097}
7098
Jouni Malinena3b8b052009-03-27 21:59:49 +02007099void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7100 struct net_device *netdev, const u8 *addr,
7101 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007102 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007103{
7104 struct sk_buff *msg;
7105 void *hdr;
7106
Johannes Berge6d6e342009-07-01 21:26:47 +02007107 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007108 if (!msg)
7109 return;
7110
7111 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7112 if (!hdr) {
7113 nlmsg_free(msg);
7114 return;
7115 }
7116
7117 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7118 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7119 if (addr)
7120 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7121 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007122 if (key_id != -1)
7123 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007124 if (tsc)
7125 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7126
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007127 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007128
Johannes Berg463d0182009-07-14 00:33:35 +02007129 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7130 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007131 return;
7132
7133 nla_put_failure:
7134 genlmsg_cancel(msg, hdr);
7135 nlmsg_free(msg);
7136}
7137
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007138void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7139 struct ieee80211_channel *channel_before,
7140 struct ieee80211_channel *channel_after)
7141{
7142 struct sk_buff *msg;
7143 void *hdr;
7144 struct nlattr *nl_freq;
7145
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007146 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007147 if (!msg)
7148 return;
7149
7150 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7151 if (!hdr) {
7152 nlmsg_free(msg);
7153 return;
7154 }
7155
7156 /*
7157 * Since we are applying the beacon hint to a wiphy we know its
7158 * wiphy_idx is valid
7159 */
7160 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7161
7162 /* Before */
7163 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7164 if (!nl_freq)
7165 goto nla_put_failure;
7166 if (nl80211_msg_put_channel(msg, channel_before))
7167 goto nla_put_failure;
7168 nla_nest_end(msg, nl_freq);
7169
7170 /* After */
7171 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7172 if (!nl_freq)
7173 goto nla_put_failure;
7174 if (nl80211_msg_put_channel(msg, channel_after))
7175 goto nla_put_failure;
7176 nla_nest_end(msg, nl_freq);
7177
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007178 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007179
Johannes Berg463d0182009-07-14 00:33:35 +02007180 rcu_read_lock();
7181 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7182 GFP_ATOMIC);
7183 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007184
7185 return;
7186
7187nla_put_failure:
7188 genlmsg_cancel(msg, hdr);
7189 nlmsg_free(msg);
7190}
7191
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007192static void nl80211_send_remain_on_chan_event(
7193 int cmd, struct cfg80211_registered_device *rdev,
7194 struct net_device *netdev, u64 cookie,
7195 struct ieee80211_channel *chan,
7196 enum nl80211_channel_type channel_type,
7197 unsigned int duration, gfp_t gfp)
7198{
7199 struct sk_buff *msg;
7200 void *hdr;
7201
7202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7203 if (!msg)
7204 return;
7205
7206 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7207 if (!hdr) {
7208 nlmsg_free(msg);
7209 return;
7210 }
7211
7212 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7213 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7214 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7215 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7216 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7217
7218 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7219 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7220
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007221 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007222
7223 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7224 nl80211_mlme_mcgrp.id, gfp);
7225 return;
7226
7227 nla_put_failure:
7228 genlmsg_cancel(msg, hdr);
7229 nlmsg_free(msg);
7230}
7231
7232void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7233 struct net_device *netdev, u64 cookie,
7234 struct ieee80211_channel *chan,
7235 enum nl80211_channel_type channel_type,
7236 unsigned int duration, gfp_t gfp)
7237{
7238 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7239 rdev, netdev, cookie, chan,
7240 channel_type, duration, gfp);
7241}
7242
7243void nl80211_send_remain_on_channel_cancel(
7244 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7245 u64 cookie, struct ieee80211_channel *chan,
7246 enum nl80211_channel_type channel_type, gfp_t gfp)
7247{
7248 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7249 rdev, netdev, cookie, chan,
7250 channel_type, 0, gfp);
7251}
7252
Johannes Berg98b62182009-12-23 13:15:44 +01007253void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7254 struct net_device *dev, const u8 *mac_addr,
7255 struct station_info *sinfo, gfp_t gfp)
7256{
7257 struct sk_buff *msg;
7258
7259 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7260 if (!msg)
7261 return;
7262
7263 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7264 nlmsg_free(msg);
7265 return;
7266 }
7267
7268 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7269 nl80211_mlme_mcgrp.id, gfp);
7270}
7271
Jouni Malinenec15e682011-03-23 15:29:52 +02007272void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7273 struct net_device *dev, const u8 *mac_addr,
7274 gfp_t gfp)
7275{
7276 struct sk_buff *msg;
7277 void *hdr;
7278
7279 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7280 if (!msg)
7281 return;
7282
7283 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7284 if (!hdr) {
7285 nlmsg_free(msg);
7286 return;
7287 }
7288
7289 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7290 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7291
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007292 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007293
7294 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7295 nl80211_mlme_mcgrp.id, gfp);
7296 return;
7297
7298 nla_put_failure:
7299 genlmsg_cancel(msg, hdr);
7300 nlmsg_free(msg);
7301}
7302
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007303static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7304 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007305{
7306 struct wireless_dev *wdev = dev->ieee80211_ptr;
7307 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7308 struct sk_buff *msg;
7309 void *hdr;
7310 int err;
7311 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7312
7313 if (!nlpid)
7314 return false;
7315
7316 msg = nlmsg_new(100, gfp);
7317 if (!msg)
7318 return true;
7319
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007320 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007321 if (!hdr) {
7322 nlmsg_free(msg);
7323 return true;
7324 }
7325
7326 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7327 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7328 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7329
7330 err = genlmsg_end(msg, hdr);
7331 if (err < 0) {
7332 nlmsg_free(msg);
7333 return true;
7334 }
7335
7336 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7337 return true;
7338
7339 nla_put_failure:
7340 genlmsg_cancel(msg, hdr);
7341 nlmsg_free(msg);
7342 return true;
7343}
7344
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007345bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7346{
7347 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7348 addr, gfp);
7349}
7350
7351bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7352 const u8 *addr, gfp_t gfp)
7353{
7354 return __nl80211_unexpected_frame(dev,
7355 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7356 addr, gfp);
7357}
7358
Johannes Berg2e161f72010-08-12 15:38:38 +02007359int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7360 struct net_device *netdev, u32 nlpid,
7361 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007362{
7363 struct sk_buff *msg;
7364 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007365
7366 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7367 if (!msg)
7368 return -ENOMEM;
7369
Johannes Berg2e161f72010-08-12 15:38:38 +02007370 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007371 if (!hdr) {
7372 nlmsg_free(msg);
7373 return -ENOMEM;
7374 }
7375
7376 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7377 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7378 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7379 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7380
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007381 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007382
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007383 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007384
7385 nla_put_failure:
7386 genlmsg_cancel(msg, hdr);
7387 nlmsg_free(msg);
7388 return -ENOBUFS;
7389}
7390
Johannes Berg2e161f72010-08-12 15:38:38 +02007391void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7392 struct net_device *netdev, u64 cookie,
7393 const u8 *buf, size_t len, bool ack,
7394 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007395{
7396 struct sk_buff *msg;
7397 void *hdr;
7398
7399 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7400 if (!msg)
7401 return;
7402
Johannes Berg2e161f72010-08-12 15:38:38 +02007403 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007404 if (!hdr) {
7405 nlmsg_free(msg);
7406 return;
7407 }
7408
7409 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7410 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7411 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7412 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7413 if (ack)
7414 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7415
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007416 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007417
7418 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7419 return;
7420
7421 nla_put_failure:
7422 genlmsg_cancel(msg, hdr);
7423 nlmsg_free(msg);
7424}
7425
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007426void
7427nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7428 struct net_device *netdev,
7429 enum nl80211_cqm_rssi_threshold_event rssi_event,
7430 gfp_t gfp)
7431{
7432 struct sk_buff *msg;
7433 struct nlattr *pinfoattr;
7434 void *hdr;
7435
7436 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7437 if (!msg)
7438 return;
7439
7440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7441 if (!hdr) {
7442 nlmsg_free(msg);
7443 return;
7444 }
7445
7446 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7447 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7448
7449 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7450 if (!pinfoattr)
7451 goto nla_put_failure;
7452
7453 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7454 rssi_event);
7455
7456 nla_nest_end(msg, pinfoattr);
7457
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007458 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007459
7460 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7461 nl80211_mlme_mcgrp.id, gfp);
7462 return;
7463
7464 nla_put_failure:
7465 genlmsg_cancel(msg, hdr);
7466 nlmsg_free(msg);
7467}
7468
Johannes Berge5497d72011-07-05 16:35:40 +02007469void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7470 struct net_device *netdev, const u8 *bssid,
7471 const u8 *replay_ctr, gfp_t gfp)
7472{
7473 struct sk_buff *msg;
7474 struct nlattr *rekey_attr;
7475 void *hdr;
7476
7477 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7478 if (!msg)
7479 return;
7480
7481 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7482 if (!hdr) {
7483 nlmsg_free(msg);
7484 return;
7485 }
7486
7487 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7488 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7489 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7490
7491 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7492 if (!rekey_attr)
7493 goto nla_put_failure;
7494
7495 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7496 NL80211_REPLAY_CTR_LEN, replay_ctr);
7497
7498 nla_nest_end(msg, rekey_attr);
7499
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007500 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007501
7502 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7503 nl80211_mlme_mcgrp.id, gfp);
7504 return;
7505
7506 nla_put_failure:
7507 genlmsg_cancel(msg, hdr);
7508 nlmsg_free(msg);
7509}
7510
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007511void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7512 struct net_device *netdev, int index,
7513 const u8 *bssid, bool preauth, gfp_t gfp)
7514{
7515 struct sk_buff *msg;
7516 struct nlattr *attr;
7517 void *hdr;
7518
7519 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7520 if (!msg)
7521 return;
7522
7523 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
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
7532 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7533 if (!attr)
7534 goto nla_put_failure;
7535
7536 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7537 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7538 if (preauth)
7539 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7540
7541 nla_nest_end(msg, attr);
7542
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007543 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007544
7545 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7546 nl80211_mlme_mcgrp.id, gfp);
7547 return;
7548
7549 nla_put_failure:
7550 genlmsg_cancel(msg, hdr);
7551 nlmsg_free(msg);
7552}
7553
Johannes Bergc063dbf2010-11-24 08:10:05 +01007554void
7555nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7556 struct net_device *netdev, const u8 *peer,
7557 u32 num_packets, gfp_t gfp)
7558{
7559 struct sk_buff *msg;
7560 struct nlattr *pinfoattr;
7561 void *hdr;
7562
7563 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7564 if (!msg)
7565 return;
7566
7567 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7568 if (!hdr) {
7569 nlmsg_free(msg);
7570 return;
7571 }
7572
7573 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7574 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7575 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7576
7577 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7578 if (!pinfoattr)
7579 goto nla_put_failure;
7580
7581 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7582
7583 nla_nest_end(msg, pinfoattr);
7584
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007585 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007586
7587 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7588 nl80211_mlme_mcgrp.id, gfp);
7589 return;
7590
7591 nla_put_failure:
7592 genlmsg_cancel(msg, hdr);
7593 nlmsg_free(msg);
7594}
7595
Johannes Berg7f6cf312011-11-04 11:18:15 +01007596void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7597 u64 cookie, bool acked, gfp_t gfp)
7598{
7599 struct wireless_dev *wdev = dev->ieee80211_ptr;
7600 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7601 struct sk_buff *msg;
7602 void *hdr;
7603 int err;
7604
7605 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7606 if (!msg)
7607 return;
7608
7609 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7610 if (!hdr) {
7611 nlmsg_free(msg);
7612 return;
7613 }
7614
7615 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7616 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7617 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7618 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7619 if (acked)
7620 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7621
7622 err = genlmsg_end(msg, hdr);
7623 if (err < 0) {
7624 nlmsg_free(msg);
7625 return;
7626 }
7627
7628 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7629 nl80211_mlme_mcgrp.id, gfp);
7630 return;
7631
7632 nla_put_failure:
7633 genlmsg_cancel(msg, hdr);
7634 nlmsg_free(msg);
7635}
7636EXPORT_SYMBOL(cfg80211_probe_status);
7637
Johannes Berg5e760232011-11-04 11:18:17 +01007638void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7639 const u8 *frame, size_t len,
7640 int freq, gfp_t gfp)
7641{
7642 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7643 struct sk_buff *msg;
7644 void *hdr;
7645 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7646
7647 if (!nlpid)
7648 return;
7649
7650 msg = nlmsg_new(len + 100, gfp);
7651 if (!msg)
7652 return;
7653
7654 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7655 if (!hdr) {
7656 nlmsg_free(msg);
7657 return;
7658 }
7659
7660 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7661 if (freq)
7662 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7663 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7664
7665 genlmsg_end(msg, hdr);
7666
7667 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7668 return;
7669
7670 nla_put_failure:
7671 genlmsg_cancel(msg, hdr);
7672 nlmsg_free(msg);
7673}
7674EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7675
Jouni Malinen026331c2010-02-15 12:53:10 +02007676static int nl80211_netlink_notify(struct notifier_block * nb,
7677 unsigned long state,
7678 void *_notify)
7679{
7680 struct netlink_notify *notify = _notify;
7681 struct cfg80211_registered_device *rdev;
7682 struct wireless_dev *wdev;
7683
7684 if (state != NETLINK_URELEASE)
7685 return NOTIFY_DONE;
7686
7687 rcu_read_lock();
7688
Johannes Berg5e760232011-11-04 11:18:17 +01007689 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007690 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007691 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01007692 if (rdev->ap_beacons_nlpid == notify->pid)
7693 rdev->ap_beacons_nlpid = 0;
7694 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007695
7696 rcu_read_unlock();
7697
7698 return NOTIFY_DONE;
7699}
7700
7701static struct notifier_block nl80211_netlink_notifier = {
7702 .notifier_call = nl80211_netlink_notify,
7703};
7704
Johannes Berg55682962007-09-20 13:09:35 -04007705/* initialisation/exit functions */
7706
7707int nl80211_init(void)
7708{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007709 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007710
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007711 err = genl_register_family_with_ops(&nl80211_fam,
7712 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007713 if (err)
7714 return err;
7715
Johannes Berg55682962007-09-20 13:09:35 -04007716 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7717 if (err)
7718 goto err_out;
7719
Johannes Berg2a519312009-02-10 21:25:55 +01007720 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7721 if (err)
7722 goto err_out;
7723
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007724 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7725 if (err)
7726 goto err_out;
7727
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007728 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7729 if (err)
7730 goto err_out;
7731
Johannes Bergaff89a92009-07-01 21:26:51 +02007732#ifdef CONFIG_NL80211_TESTMODE
7733 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7734 if (err)
7735 goto err_out;
7736#endif
7737
Jouni Malinen026331c2010-02-15 12:53:10 +02007738 err = netlink_register_notifier(&nl80211_netlink_notifier);
7739 if (err)
7740 goto err_out;
7741
Johannes Berg55682962007-09-20 13:09:35 -04007742 return 0;
7743 err_out:
7744 genl_unregister_family(&nl80211_fam);
7745 return err;
7746}
7747
7748void nl80211_exit(void)
7749{
Jouni Malinen026331c2010-02-15 12:53:10 +02007750 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007751 genl_unregister_family(&nl80211_fam);
7752}