blob: f395a06c114a9007e8e3d884943efed4bb3e00d0 [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
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200762 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
763 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
764 dev->wiphy.probe_resp_offload);
765
Bruno Randolf7f531e02010-12-16 11:30:22 +0900766 if ((dev->wiphy.available_antennas_tx ||
767 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900768 u32 tx_ant = 0, rx_ant = 0;
769 int res;
770 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
771 if (!res) {
772 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
773 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
774 }
775 }
776
Johannes Berg7527a782011-05-13 10:58:57 +0200777 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
778 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700779 goto nla_put_failure;
780
Johannes Bergee688b002008-01-24 19:38:39 +0100781 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
782 if (!nl_bands)
783 goto nla_put_failure;
784
785 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
786 if (!dev->wiphy.bands[band])
787 continue;
788
789 nl_band = nla_nest_start(msg, band);
790 if (!nl_band)
791 goto nla_put_failure;
792
Johannes Bergd51626d2008-10-09 12:20:13 +0200793 /* add HT info */
794 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
795 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
796 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
797 &dev->wiphy.bands[band]->ht_cap.mcs);
798 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
799 dev->wiphy.bands[band]->ht_cap.cap);
800 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
801 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
802 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
803 dev->wiphy.bands[band]->ht_cap.ampdu_density);
804 }
805
Johannes Bergee688b002008-01-24 19:38:39 +0100806 /* add frequencies */
807 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
808 if (!nl_freqs)
809 goto nla_put_failure;
810
811 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
812 nl_freq = nla_nest_start(msg, i);
813 if (!nl_freq)
814 goto nla_put_failure;
815
816 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100817
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400818 if (nl80211_msg_put_channel(msg, chan))
819 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200820
Johannes Bergee688b002008-01-24 19:38:39 +0100821 nla_nest_end(msg, nl_freq);
822 }
823
824 nla_nest_end(msg, nl_freqs);
825
826 /* add bitrates */
827 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
828 if (!nl_rates)
829 goto nla_put_failure;
830
831 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
832 nl_rate = nla_nest_start(msg, i);
833 if (!nl_rate)
834 goto nla_put_failure;
835
836 rate = &dev->wiphy.bands[band]->bitrates[i];
837 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
838 rate->bitrate);
839 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
840 NLA_PUT_FLAG(msg,
841 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
842
843 nla_nest_end(msg, nl_rate);
844 }
845
846 nla_nest_end(msg, nl_rates);
847
848 nla_nest_end(msg, nl_band);
849 }
850 nla_nest_end(msg, nl_bands);
851
Johannes Berg8fdc6212009-03-14 09:34:01 +0100852 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
853 if (!nl_cmds)
854 goto nla_put_failure;
855
856 i = 0;
857#define CMD(op, n) \
858 do { \
859 if (dev->ops->op) { \
860 i++; \
861 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
862 } \
863 } while (0)
864
865 CMD(add_virtual_intf, NEW_INTERFACE);
866 CMD(change_virtual_intf, SET_INTERFACE);
867 CMD(add_key, NEW_KEY);
868 CMD(add_beacon, NEW_BEACON);
869 CMD(add_station, NEW_STATION);
870 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800871 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100872 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200873 CMD(auth, AUTHENTICATE);
874 CMD(assoc, ASSOCIATE);
875 CMD(deauth, DEAUTHENTICATE);
876 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200877 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100878 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100879 CMD(set_pmksa, SET_PMKSA);
880 CMD(del_pmksa, DEL_PMKSA);
881 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100882 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200883 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200884 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100885 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100886 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200887 i++;
888 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
889 }
Johannes Bergf444de02010-05-05 15:25:02 +0200890 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400891 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300892 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
893 CMD(tdls_mgmt, TDLS_MGMT);
894 CMD(tdls_oper, TDLS_OPER);
895 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300896 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
897 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100898 CMD(probe_client, PROBE_CLIENT);
Johannes Berg5e760232011-11-04 11:18:17 +0100899 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
900 i++;
901 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
902 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100903
904#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200905
Johannes Berg6829c8782009-07-02 09:13:27 +0200906 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200907 i++;
908 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
909 }
910
Johannes Berg6829c8782009-07-02 09:13:27 +0200911 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200912 i++;
913 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
914 }
915
Johannes Berg8fdc6212009-03-14 09:34:01 +0100916 nla_nest_end(msg, nl_cmds);
917
Johannes Berga2939112010-12-14 17:54:28 +0100918 if (dev->ops->remain_on_channel)
919 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
920 dev->wiphy.max_remain_on_channel_duration);
921
Jouni Malinend2da5872011-08-08 12:10:30 +0300922 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100923 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
924
Johannes Berg2e161f72010-08-12 15:38:38 +0200925 if (mgmt_stypes) {
926 u16 stypes;
927 struct nlattr *nl_ftypes, *nl_ifs;
928 enum nl80211_iftype ift;
929
930 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
931 if (!nl_ifs)
932 goto nla_put_failure;
933
934 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
935 nl_ftypes = nla_nest_start(msg, ift);
936 if (!nl_ftypes)
937 goto nla_put_failure;
938 i = 0;
939 stypes = mgmt_stypes[ift].tx;
940 while (stypes) {
941 if (stypes & 1)
942 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
943 (i << 4) | IEEE80211_FTYPE_MGMT);
944 stypes >>= 1;
945 i++;
946 }
947 nla_nest_end(msg, nl_ftypes);
948 }
949
Johannes Berg74b70a42010-08-24 12:15:53 +0200950 nla_nest_end(msg, nl_ifs);
951
Johannes Berg2e161f72010-08-12 15:38:38 +0200952 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
953 if (!nl_ifs)
954 goto nla_put_failure;
955
956 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
957 nl_ftypes = nla_nest_start(msg, ift);
958 if (!nl_ftypes)
959 goto nla_put_failure;
960 i = 0;
961 stypes = mgmt_stypes[ift].rx;
962 while (stypes) {
963 if (stypes & 1)
964 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
965 (i << 4) | IEEE80211_FTYPE_MGMT);
966 stypes >>= 1;
967 i++;
968 }
969 nla_nest_end(msg, nl_ftypes);
970 }
971 nla_nest_end(msg, nl_ifs);
972 }
973
Johannes Bergff1b6e62011-05-04 15:37:28 +0200974 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
975 struct nlattr *nl_wowlan;
976
977 nl_wowlan = nla_nest_start(msg,
978 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
979 if (!nl_wowlan)
980 goto nla_put_failure;
981
982 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
983 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
984 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
985 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
986 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
987 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200988 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
989 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
990 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
991 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
992 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
993 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
994 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
995 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
996 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
997 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200998 if (dev->wiphy.wowlan.n_patterns) {
999 struct nl80211_wowlan_pattern_support pat = {
1000 .max_patterns = dev->wiphy.wowlan.n_patterns,
1001 .min_pattern_len =
1002 dev->wiphy.wowlan.pattern_min_len,
1003 .max_pattern_len =
1004 dev->wiphy.wowlan.pattern_max_len,
1005 };
1006 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1007 sizeof(pat), &pat);
1008 }
1009
1010 nla_nest_end(msg, nl_wowlan);
1011 }
1012
Johannes Berg7527a782011-05-13 10:58:57 +02001013 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1014 dev->wiphy.software_iftypes))
1015 goto nla_put_failure;
1016
1017 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1018 goto nla_put_failure;
1019
Johannes Berg562a7482011-11-07 12:39:33 +01001020 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1021 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1022 dev->wiphy.ap_sme_capa);
1023
Johannes Berg1f074bd2011-11-06 14:13:33 +01001024 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1025
Johannes Berg55682962007-09-20 13:09:35 -04001026 return genlmsg_end(msg, hdr);
1027
1028 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001029 genlmsg_cancel(msg, hdr);
1030 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001031}
1032
1033static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1034{
1035 int idx = 0;
1036 int start = cb->args[0];
1037 struct cfg80211_registered_device *dev;
1038
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001039 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001040 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001041 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1042 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001043 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001044 continue;
1045 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1046 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001047 dev) < 0) {
1048 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001049 break;
Julius Volzb4637272008-07-08 14:02:19 +02001050 }
Johannes Berg55682962007-09-20 13:09:35 -04001051 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001052 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001053
1054 cb->args[0] = idx;
1055
1056 return skb->len;
1057}
1058
1059static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1060{
1061 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001062 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001063
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001064 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001065 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001066 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001067
Johannes Berg4c476992010-10-04 21:36:35 +02001068 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1069 nlmsg_free(msg);
1070 return -ENOBUFS;
1071 }
Johannes Berg55682962007-09-20 13:09:35 -04001072
Johannes Berg134e6372009-07-10 09:51:34 +00001073 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001074}
1075
Jouni Malinen31888482008-10-30 16:59:24 +02001076static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1077 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1078 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1079 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1080 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1081 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1082};
1083
1084static int parse_txq_params(struct nlattr *tb[],
1085 struct ieee80211_txq_params *txq_params)
1086{
1087 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1088 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1089 !tb[NL80211_TXQ_ATTR_AIFS])
1090 return -EINVAL;
1091
1092 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1093 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1094 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1095 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1096 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1097
1098 return 0;
1099}
1100
Johannes Bergf444de02010-05-05 15:25:02 +02001101static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1102{
1103 /*
1104 * You can only set the channel explicitly for AP, mesh
1105 * and WDS type interfaces; all others have their channel
1106 * managed via their respective "establish a connection"
1107 * command (connect, join, ...)
1108 *
1109 * Monitors are special as they are normally slaved to
1110 * whatever else is going on, so they behave as though
1111 * you tried setting the wiphy channel itself.
1112 */
1113 return !wdev ||
1114 wdev->iftype == NL80211_IFTYPE_AP ||
1115 wdev->iftype == NL80211_IFTYPE_WDS ||
1116 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001117 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1118 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001119}
1120
1121static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1122 struct wireless_dev *wdev,
1123 struct genl_info *info)
1124{
1125 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1126 u32 freq;
1127 int result;
1128
1129 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1130 return -EINVAL;
1131
1132 if (!nl80211_can_set_dev_channel(wdev))
1133 return -EOPNOTSUPP;
1134
1135 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1136 channel_type = nla_get_u32(info->attrs[
1137 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1138 if (channel_type != NL80211_CHAN_NO_HT &&
1139 channel_type != NL80211_CHAN_HT20 &&
1140 channel_type != NL80211_CHAN_HT40PLUS &&
1141 channel_type != NL80211_CHAN_HT40MINUS)
1142 return -EINVAL;
1143 }
1144
1145 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1146
1147 mutex_lock(&rdev->devlist_mtx);
1148 if (wdev) {
1149 wdev_lock(wdev);
1150 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1151 wdev_unlock(wdev);
1152 } else {
1153 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1154 }
1155 mutex_unlock(&rdev->devlist_mtx);
1156
1157 return result;
1158}
1159
1160static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1161{
Johannes Berg4c476992010-10-04 21:36:35 +02001162 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1163 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001164
Johannes Berg4c476992010-10-04 21:36:35 +02001165 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001166}
1167
Bill Jordane8347eb2010-10-01 13:54:28 -04001168static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1169{
Johannes Berg43b19952010-10-07 13:10:30 +02001170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1171 struct net_device *dev = info->user_ptr[1];
1172 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001173 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001174
1175 if (!info->attrs[NL80211_ATTR_MAC])
1176 return -EINVAL;
1177
Johannes Berg43b19952010-10-07 13:10:30 +02001178 if (netif_running(dev))
1179 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001180
Johannes Berg43b19952010-10-07 13:10:30 +02001181 if (!rdev->ops->set_wds_peer)
1182 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001183
Johannes Berg43b19952010-10-07 13:10:30 +02001184 if (wdev->iftype != NL80211_IFTYPE_WDS)
1185 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001186
1187 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001188 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001189}
1190
1191
Johannes Berg55682962007-09-20 13:09:35 -04001192static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1193{
1194 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001195 struct net_device *netdev = NULL;
1196 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001197 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001198 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001199 u32 changed;
1200 u8 retry_short = 0, retry_long = 0;
1201 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001202 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001203
Johannes Bergf444de02010-05-05 15:25:02 +02001204 /*
1205 * Try to find the wiphy and netdev. Normally this
1206 * function shouldn't need the netdev, but this is
1207 * done for backward compatibility -- previously
1208 * setting the channel was done per wiphy, but now
1209 * it is per netdev. Previous userland like hostapd
1210 * also passed a netdev to set_wiphy, so that it is
1211 * possible to let that go to the right netdev!
1212 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001213 mutex_lock(&cfg80211_mutex);
1214
Johannes Bergf444de02010-05-05 15:25:02 +02001215 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1216 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1217
1218 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1219 if (netdev && netdev->ieee80211_ptr) {
1220 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1221 mutex_lock(&rdev->mtx);
1222 } else
1223 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001224 }
1225
Johannes Bergf444de02010-05-05 15:25:02 +02001226 if (!netdev) {
1227 rdev = __cfg80211_rdev_from_info(info);
1228 if (IS_ERR(rdev)) {
1229 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001230 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001231 }
1232 wdev = NULL;
1233 netdev = NULL;
1234 result = 0;
1235
1236 mutex_lock(&rdev->mtx);
1237 } else if (netif_running(netdev) &&
1238 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1239 wdev = netdev->ieee80211_ptr;
1240 else
1241 wdev = NULL;
1242
1243 /*
1244 * end workaround code, by now the rdev is available
1245 * and locked, and wdev may or may not be NULL.
1246 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001247
1248 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001249 result = cfg80211_dev_rename(
1250 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001251
1252 mutex_unlock(&cfg80211_mutex);
1253
1254 if (result)
1255 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001256
Jouni Malinen31888482008-10-30 16:59:24 +02001257 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1258 struct ieee80211_txq_params txq_params;
1259 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1260
1261 if (!rdev->ops->set_txq_params) {
1262 result = -EOPNOTSUPP;
1263 goto bad_res;
1264 }
1265
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001266 if (!netdev) {
1267 result = -EINVAL;
1268 goto bad_res;
1269 }
1270
Johannes Berg133a3ff2011-11-03 14:50:13 +01001271 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1272 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1273 result = -EINVAL;
1274 goto bad_res;
1275 }
1276
Jouni Malinen31888482008-10-30 16:59:24 +02001277 nla_for_each_nested(nl_txq_params,
1278 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1279 rem_txq_params) {
1280 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1281 nla_data(nl_txq_params),
1282 nla_len(nl_txq_params),
1283 txq_params_policy);
1284 result = parse_txq_params(tb, &txq_params);
1285 if (result)
1286 goto bad_res;
1287
1288 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001289 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001290 &txq_params);
1291 if (result)
1292 goto bad_res;
1293 }
1294 }
1295
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001296 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001297 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001298 if (result)
1299 goto bad_res;
1300 }
1301
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001302 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1303 enum nl80211_tx_power_setting type;
1304 int idx, mbm = 0;
1305
1306 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001307 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001308 goto bad_res;
1309 }
1310
1311 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1312 type = nla_get_u32(info->attrs[idx]);
1313
1314 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1315 (type != NL80211_TX_POWER_AUTOMATIC)) {
1316 result = -EINVAL;
1317 goto bad_res;
1318 }
1319
1320 if (type != NL80211_TX_POWER_AUTOMATIC) {
1321 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1322 mbm = nla_get_u32(info->attrs[idx]);
1323 }
1324
1325 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1326 if (result)
1327 goto bad_res;
1328 }
1329
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001330 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1331 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1332 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001333 if ((!rdev->wiphy.available_antennas_tx &&
1334 !rdev->wiphy.available_antennas_rx) ||
1335 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001336 result = -EOPNOTSUPP;
1337 goto bad_res;
1338 }
1339
1340 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1341 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1342
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001343 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001344 * available antenna masks, except for the "all" mask */
1345 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1346 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001347 result = -EINVAL;
1348 goto bad_res;
1349 }
1350
Bruno Randolf7f531e02010-12-16 11:30:22 +09001351 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1352 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001353
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001354 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1355 if (result)
1356 goto bad_res;
1357 }
1358
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001359 changed = 0;
1360
1361 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1362 retry_short = nla_get_u8(
1363 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1364 if (retry_short == 0) {
1365 result = -EINVAL;
1366 goto bad_res;
1367 }
1368 changed |= WIPHY_PARAM_RETRY_SHORT;
1369 }
1370
1371 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1372 retry_long = nla_get_u8(
1373 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1374 if (retry_long == 0) {
1375 result = -EINVAL;
1376 goto bad_res;
1377 }
1378 changed |= WIPHY_PARAM_RETRY_LONG;
1379 }
1380
1381 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1382 frag_threshold = nla_get_u32(
1383 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1384 if (frag_threshold < 256) {
1385 result = -EINVAL;
1386 goto bad_res;
1387 }
1388 if (frag_threshold != (u32) -1) {
1389 /*
1390 * Fragments (apart from the last one) are required to
1391 * have even length. Make the fragmentation code
1392 * simpler by stripping LSB should someone try to use
1393 * odd threshold value.
1394 */
1395 frag_threshold &= ~0x1;
1396 }
1397 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1398 }
1399
1400 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1401 rts_threshold = nla_get_u32(
1402 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1403 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1404 }
1405
Lukáš Turek81077e82009-12-21 22:50:47 +01001406 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1407 coverage_class = nla_get_u8(
1408 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1409 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1410 }
1411
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001412 if (changed) {
1413 u8 old_retry_short, old_retry_long;
1414 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001415 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001416
1417 if (!rdev->ops->set_wiphy_params) {
1418 result = -EOPNOTSUPP;
1419 goto bad_res;
1420 }
1421
1422 old_retry_short = rdev->wiphy.retry_short;
1423 old_retry_long = rdev->wiphy.retry_long;
1424 old_frag_threshold = rdev->wiphy.frag_threshold;
1425 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001426 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001427
1428 if (changed & WIPHY_PARAM_RETRY_SHORT)
1429 rdev->wiphy.retry_short = retry_short;
1430 if (changed & WIPHY_PARAM_RETRY_LONG)
1431 rdev->wiphy.retry_long = retry_long;
1432 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1433 rdev->wiphy.frag_threshold = frag_threshold;
1434 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1435 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001436 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1437 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001438
1439 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1440 if (result) {
1441 rdev->wiphy.retry_short = old_retry_short;
1442 rdev->wiphy.retry_long = old_retry_long;
1443 rdev->wiphy.frag_threshold = old_frag_threshold;
1444 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001445 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001446 }
1447 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001448
Johannes Berg306d6112008-12-08 12:39:04 +01001449 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001450 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001451 if (netdev)
1452 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001453 return result;
1454}
1455
1456
1457static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001458 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001459 struct net_device *dev)
1460{
1461 void *hdr;
1462
1463 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1464 if (!hdr)
1465 return -1;
1466
1467 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001468 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001469 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001470 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001471
1472 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1473 rdev->devlist_generation ^
1474 (cfg80211_rdev_list_generation << 2));
1475
Johannes Berg55682962007-09-20 13:09:35 -04001476 return genlmsg_end(msg, hdr);
1477
1478 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001479 genlmsg_cancel(msg, hdr);
1480 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001481}
1482
1483static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1484{
1485 int wp_idx = 0;
1486 int if_idx = 0;
1487 int wp_start = cb->args[0];
1488 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001489 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001490 struct wireless_dev *wdev;
1491
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001492 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001493 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1494 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001495 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001496 if (wp_idx < wp_start) {
1497 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001498 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001499 }
Johannes Berg55682962007-09-20 13:09:35 -04001500 if_idx = 0;
1501
Johannes Bergf5ea9122009-08-07 16:17:38 +02001502 mutex_lock(&rdev->devlist_mtx);
1503 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001504 if (if_idx < if_start) {
1505 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001506 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001507 }
Johannes Berg55682962007-09-20 13:09:35 -04001508 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1509 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001510 rdev, wdev->netdev) < 0) {
1511 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001512 goto out;
1513 }
1514 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001515 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001516 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001517
1518 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001519 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001520 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001521 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001522
1523 cb->args[0] = wp_idx;
1524 cb->args[1] = if_idx;
1525
1526 return skb->len;
1527}
1528
1529static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1530{
1531 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001532 struct cfg80211_registered_device *dev = info->user_ptr[0];
1533 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001534
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001535 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001536 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001537 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001538
Johannes Bergd7264052009-04-19 16:23:20 +02001539 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001540 dev, netdev) < 0) {
1541 nlmsg_free(msg);
1542 return -ENOBUFS;
1543 }
Johannes Berg55682962007-09-20 13:09:35 -04001544
Johannes Berg134e6372009-07-10 09:51:34 +00001545 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001546}
1547
Michael Wu66f7ac52008-01-31 19:48:22 +01001548static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1549 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1550 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1551 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1552 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1553 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1554};
1555
1556static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1557{
1558 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1559 int flag;
1560
1561 *mntrflags = 0;
1562
1563 if (!nla)
1564 return -EINVAL;
1565
1566 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1567 nla, mntr_flags_policy))
1568 return -EINVAL;
1569
1570 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1571 if (flags[flag])
1572 *mntrflags |= (1<<flag);
1573
1574 return 0;
1575}
1576
Johannes Berg9bc383d2009-11-19 11:55:19 +01001577static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001578 struct net_device *netdev, u8 use_4addr,
1579 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001580{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001581 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001582 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001583 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001584 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001585 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001586
1587 switch (iftype) {
1588 case NL80211_IFTYPE_AP_VLAN:
1589 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1590 return 0;
1591 break;
1592 case NL80211_IFTYPE_STATION:
1593 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1594 return 0;
1595 break;
1596 default:
1597 break;
1598 }
1599
1600 return -EOPNOTSUPP;
1601}
1602
Johannes Berg55682962007-09-20 13:09:35 -04001603static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1604{
Johannes Berg4c476992010-10-04 21:36:35 +02001605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001606 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001607 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001608 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001609 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001610 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001611 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001612
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001613 memset(&params, 0, sizeof(params));
1614
Johannes Berg04a773a2009-04-19 21:24:32 +02001615 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001616
Johannes Berg723b0382008-09-16 20:22:09 +02001617 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001618 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001619 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001620 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001621 if (ntype > NL80211_IFTYPE_MAX)
1622 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001623 }
1624
Johannes Berg92ffe052008-09-16 20:39:36 +02001625 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001626 struct wireless_dev *wdev = dev->ieee80211_ptr;
1627
Johannes Berg4c476992010-10-04 21:36:35 +02001628 if (ntype != NL80211_IFTYPE_MESH_POINT)
1629 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001630 if (netif_running(dev))
1631 return -EBUSY;
1632
1633 wdev_lock(wdev);
1634 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1635 IEEE80211_MAX_MESH_ID_LEN);
1636 wdev->mesh_id_up_len =
1637 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1638 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1639 wdev->mesh_id_up_len);
1640 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001641 }
1642
Felix Fietkau8b787642009-11-10 18:53:10 +01001643 if (info->attrs[NL80211_ATTR_4ADDR]) {
1644 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1645 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001646 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001647 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001648 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001649 } else {
1650 params.use_4addr = -1;
1651 }
1652
Johannes Berg92ffe052008-09-16 20:39:36 +02001653 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001654 if (ntype != NL80211_IFTYPE_MONITOR)
1655 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001656 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1657 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001658 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001659 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001660
1661 flags = &_flags;
1662 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001663 }
Johannes Berg3b858752009-03-12 09:55:09 +01001664
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001665 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001666 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001667 else
1668 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001669
Johannes Berg9bc383d2009-11-19 11:55:19 +01001670 if (!err && params.use_4addr != -1)
1671 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1672
Johannes Berg55682962007-09-20 13:09:35 -04001673 return err;
1674}
1675
1676static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1677{
Johannes Berg4c476992010-10-04 21:36:35 +02001678 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001679 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001680 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001681 int err;
1682 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001683 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001684
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001685 memset(&params, 0, sizeof(params));
1686
Johannes Berg55682962007-09-20 13:09:35 -04001687 if (!info->attrs[NL80211_ATTR_IFNAME])
1688 return -EINVAL;
1689
1690 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1691 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1692 if (type > NL80211_IFTYPE_MAX)
1693 return -EINVAL;
1694 }
1695
Johannes Berg79c97e92009-07-07 03:56:12 +02001696 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001697 !(rdev->wiphy.interface_modes & (1 << type)))
1698 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001699
Johannes Berg9bc383d2009-11-19 11:55:19 +01001700 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001701 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001702 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001703 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001704 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001705 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001706
Michael Wu66f7ac52008-01-31 19:48:22 +01001707 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1708 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1709 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001710 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001711 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001712 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001713 if (IS_ERR(dev))
1714 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001715
Johannes Berg29cbe682010-12-03 09:20:44 +01001716 if (type == NL80211_IFTYPE_MESH_POINT &&
1717 info->attrs[NL80211_ATTR_MESH_ID]) {
1718 struct wireless_dev *wdev = dev->ieee80211_ptr;
1719
1720 wdev_lock(wdev);
1721 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1722 IEEE80211_MAX_MESH_ID_LEN);
1723 wdev->mesh_id_up_len =
1724 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1725 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1726 wdev->mesh_id_up_len);
1727 wdev_unlock(wdev);
1728 }
1729
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001730 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001731}
1732
1733static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1734{
Johannes Berg4c476992010-10-04 21:36:35 +02001735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1736 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001737
Johannes Berg4c476992010-10-04 21:36:35 +02001738 if (!rdev->ops->del_virtual_intf)
1739 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001740
Johannes Berg4c476992010-10-04 21:36:35 +02001741 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001742}
1743
Johannes Berg41ade002007-12-19 02:03:29 +01001744struct get_key_cookie {
1745 struct sk_buff *msg;
1746 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001747 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001748};
1749
1750static void get_key_callback(void *c, struct key_params *params)
1751{
Johannes Bergb9454e82009-07-08 13:29:08 +02001752 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001753 struct get_key_cookie *cookie = c;
1754
1755 if (params->key)
1756 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1757 params->key_len, params->key);
1758
1759 if (params->seq)
1760 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1761 params->seq_len, params->seq);
1762
1763 if (params->cipher)
1764 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1765 params->cipher);
1766
Johannes Bergb9454e82009-07-08 13:29:08 +02001767 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1768 if (!key)
1769 goto nla_put_failure;
1770
1771 if (params->key)
1772 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1773 params->key_len, params->key);
1774
1775 if (params->seq)
1776 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1777 params->seq_len, params->seq);
1778
1779 if (params->cipher)
1780 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1781 params->cipher);
1782
1783 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1784
1785 nla_nest_end(cookie->msg, key);
1786
Johannes Berg41ade002007-12-19 02:03:29 +01001787 return;
1788 nla_put_failure:
1789 cookie->error = 1;
1790}
1791
1792static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1793{
Johannes Berg4c476992010-10-04 21:36:35 +02001794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001795 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001796 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001797 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001798 const u8 *mac_addr = NULL;
1799 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001800 struct get_key_cookie cookie = {
1801 .error = 0,
1802 };
1803 void *hdr;
1804 struct sk_buff *msg;
1805
1806 if (info->attrs[NL80211_ATTR_KEY_IDX])
1807 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1808
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001809 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001810 return -EINVAL;
1811
1812 if (info->attrs[NL80211_ATTR_MAC])
1813 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1814
Johannes Berge31b8212010-10-05 19:39:30 +02001815 pairwise = !!mac_addr;
1816 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1817 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1818 if (kt >= NUM_NL80211_KEYTYPES)
1819 return -EINVAL;
1820 if (kt != NL80211_KEYTYPE_GROUP &&
1821 kt != NL80211_KEYTYPE_PAIRWISE)
1822 return -EINVAL;
1823 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1824 }
1825
Johannes Berg4c476992010-10-04 21:36:35 +02001826 if (!rdev->ops->get_key)
1827 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001828
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001829 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001830 if (!msg)
1831 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001832
1833 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1834 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001835 if (IS_ERR(hdr))
1836 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001837
1838 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001839 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001840
1841 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1842 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1843 if (mac_addr)
1844 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1845
Johannes Berge31b8212010-10-05 19:39:30 +02001846 if (pairwise && mac_addr &&
1847 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1848 return -ENOENT;
1849
1850 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1851 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001852
1853 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001854 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001855
1856 if (cookie.error)
1857 goto nla_put_failure;
1858
1859 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001860 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001861
1862 nla_put_failure:
1863 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001864 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001865 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001866 return err;
1867}
1868
1869static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1870{
Johannes Berg4c476992010-10-04 21:36:35 +02001871 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001872 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001873 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001874 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001875
Johannes Bergb9454e82009-07-08 13:29:08 +02001876 err = nl80211_parse_key(info, &key);
1877 if (err)
1878 return err;
1879
1880 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001881 return -EINVAL;
1882
Johannes Bergb9454e82009-07-08 13:29:08 +02001883 /* only support setting default key */
1884 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001885 return -EINVAL;
1886
Johannes Bergfffd0932009-07-08 14:22:54 +02001887 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001888
1889 if (key.def) {
1890 if (!rdev->ops->set_default_key) {
1891 err = -EOPNOTSUPP;
1892 goto out;
1893 }
1894
1895 err = nl80211_key_allowed(dev->ieee80211_ptr);
1896 if (err)
1897 goto out;
1898
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001899 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1900 key.def_uni, key.def_multi);
1901
1902 if (err)
1903 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001904
Johannes Berg3d23e342009-09-29 23:27:28 +02001905#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001906 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001907#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001908 } else {
1909 if (key.def_uni || !key.def_multi) {
1910 err = -EINVAL;
1911 goto out;
1912 }
1913
1914 if (!rdev->ops->set_default_mgmt_key) {
1915 err = -EOPNOTSUPP;
1916 goto out;
1917 }
1918
1919 err = nl80211_key_allowed(dev->ieee80211_ptr);
1920 if (err)
1921 goto out;
1922
1923 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1924 dev, key.idx);
1925 if (err)
1926 goto out;
1927
1928#ifdef CONFIG_CFG80211_WEXT
1929 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1930#endif
1931 }
1932
1933 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001934 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001935
Johannes Berg41ade002007-12-19 02:03:29 +01001936 return err;
1937}
1938
1939static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1940{
Johannes Berg4c476992010-10-04 21:36:35 +02001941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001942 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001943 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001944 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001945 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001946
Johannes Bergb9454e82009-07-08 13:29:08 +02001947 err = nl80211_parse_key(info, &key);
1948 if (err)
1949 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001950
Johannes Bergb9454e82009-07-08 13:29:08 +02001951 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001952 return -EINVAL;
1953
Johannes Berg41ade002007-12-19 02:03:29 +01001954 if (info->attrs[NL80211_ATTR_MAC])
1955 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1956
Johannes Berge31b8212010-10-05 19:39:30 +02001957 if (key.type == -1) {
1958 if (mac_addr)
1959 key.type = NL80211_KEYTYPE_PAIRWISE;
1960 else
1961 key.type = NL80211_KEYTYPE_GROUP;
1962 }
1963
1964 /* for now */
1965 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1966 key.type != NL80211_KEYTYPE_GROUP)
1967 return -EINVAL;
1968
Johannes Berg4c476992010-10-04 21:36:35 +02001969 if (!rdev->ops->add_key)
1970 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001971
Johannes Berge31b8212010-10-05 19:39:30 +02001972 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1973 key.type == NL80211_KEYTYPE_PAIRWISE,
1974 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001975 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001976
1977 wdev_lock(dev->ieee80211_ptr);
1978 err = nl80211_key_allowed(dev->ieee80211_ptr);
1979 if (!err)
1980 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001981 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001982 mac_addr, &key.p);
1983 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001984
Johannes Berg41ade002007-12-19 02:03:29 +01001985 return err;
1986}
1987
1988static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1989{
Johannes Berg4c476992010-10-04 21:36:35 +02001990 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001991 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001992 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001993 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001994 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001995
Johannes Bergb9454e82009-07-08 13:29:08 +02001996 err = nl80211_parse_key(info, &key);
1997 if (err)
1998 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001999
2000 if (info->attrs[NL80211_ATTR_MAC])
2001 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2002
Johannes Berge31b8212010-10-05 19:39:30 +02002003 if (key.type == -1) {
2004 if (mac_addr)
2005 key.type = NL80211_KEYTYPE_PAIRWISE;
2006 else
2007 key.type = NL80211_KEYTYPE_GROUP;
2008 }
2009
2010 /* for now */
2011 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2012 key.type != NL80211_KEYTYPE_GROUP)
2013 return -EINVAL;
2014
Johannes Berg4c476992010-10-04 21:36:35 +02002015 if (!rdev->ops->del_key)
2016 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002017
Johannes Bergfffd0932009-07-08 14:22:54 +02002018 wdev_lock(dev->ieee80211_ptr);
2019 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002020
2021 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2022 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2023 err = -ENOENT;
2024
Johannes Bergfffd0932009-07-08 14:22:54 +02002025 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002026 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2027 key.type == NL80211_KEYTYPE_PAIRWISE,
2028 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002029
Johannes Berg3d23e342009-09-29 23:27:28 +02002030#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002031 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002032 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002033 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002034 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002035 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2036 }
2037#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002038 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002039
Johannes Berg41ade002007-12-19 02:03:29 +01002040 return err;
2041}
2042
Johannes Berged1b6cc2007-12-19 02:03:32 +01002043static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2044{
2045 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2046 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2048 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002049 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002050 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002051 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002052
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002053 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2054 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2055 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2056 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002057 return -EINVAL;
2058
Johannes Berg074ac8d2010-09-16 14:58:22 +02002059 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002060 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2061 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002062
Johannes Berg56d18932011-05-09 18:41:15 +02002063 memset(&params, 0, sizeof(params));
2064
Johannes Berged1b6cc2007-12-19 02:03:32 +01002065 switch (info->genlhdr->cmd) {
2066 case NL80211_CMD_NEW_BEACON:
2067 /* these are required for NEW_BEACON */
2068 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2069 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002070 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2071 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002072
Johannes Berg56d18932011-05-09 18:41:15 +02002073 params.interval =
2074 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2075 params.dtim_period =
2076 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2077
2078 err = cfg80211_validate_beacon_int(rdev, params.interval);
2079 if (err)
2080 return err;
2081
Jouni Malinen32e9de82011-08-10 23:53:31 +03002082 /*
2083 * In theory, some of these attributes could be required for
2084 * NEW_BEACON, but since they were not used when the command was
2085 * originally added, keep them optional for old user space
2086 * programs to work with drivers that do not need the additional
2087 * information.
2088 */
2089 if (info->attrs[NL80211_ATTR_SSID]) {
2090 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2091 params.ssid_len =
2092 nla_len(info->attrs[NL80211_ATTR_SSID]);
2093 if (params.ssid_len == 0 ||
2094 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2095 return -EINVAL;
2096 }
2097
2098 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2099 params.hidden_ssid = nla_get_u32(
2100 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2101 if (params.hidden_ssid !=
2102 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2103 params.hidden_ssid !=
2104 NL80211_HIDDEN_SSID_ZERO_LEN &&
2105 params.hidden_ssid !=
2106 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2107 return -EINVAL;
2108 }
2109
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002110 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2111
2112 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2113 params.auth_type = nla_get_u32(
2114 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2115 if (!nl80211_valid_auth_type(params.auth_type))
2116 return -EINVAL;
2117 } else
2118 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2119
2120 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2121 NL80211_MAX_NR_CIPHER_SUITES);
2122 if (err)
2123 return err;
2124
Johannes Berg79c97e92009-07-07 03:56:12 +02002125 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002126 break;
2127 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002128 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002129 break;
2130 default:
2131 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002132 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002133 }
2134
Johannes Berg4c476992010-10-04 21:36:35 +02002135 if (!call)
2136 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002137
Johannes Berged1b6cc2007-12-19 02:03:32 +01002138 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2139 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2140 params.head_len =
2141 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2142 haveinfo = 1;
2143 }
2144
2145 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2146 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2147 params.tail_len =
2148 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2149 haveinfo = 1;
2150 }
2151
Johannes Berg4c476992010-10-04 21:36:35 +02002152 if (!haveinfo)
2153 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002154
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002155 if (info->attrs[NL80211_ATTR_IE]) {
2156 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2157 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2158 }
2159
2160 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2161 params.proberesp_ies =
2162 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2163 params.proberesp_ies_len =
2164 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2165 }
2166
2167 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2168 params.assocresp_ies =
2169 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2170 params.assocresp_ies_len =
2171 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2172 }
2173
Johannes Berg56d18932011-05-09 18:41:15 +02002174 err = call(&rdev->wiphy, dev, &params);
2175 if (!err && params.interval)
2176 wdev->beacon_interval = params.interval;
2177 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002178}
2179
2180static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2181{
Johannes Berg4c476992010-10-04 21:36:35 +02002182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2183 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002184 struct wireless_dev *wdev = dev->ieee80211_ptr;
2185 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002186
Johannes Berg4c476992010-10-04 21:36:35 +02002187 if (!rdev->ops->del_beacon)
2188 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002189
Johannes Berg074ac8d2010-09-16 14:58:22 +02002190 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002191 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2192 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002193
Johannes Berg56d18932011-05-09 18:41:15 +02002194 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2195 if (!err)
2196 wdev->beacon_interval = 0;
2197 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002198}
2199
Johannes Berg5727ef12007-12-19 02:03:34 +01002200static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2201 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2202 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2203 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002204 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002205 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002206};
2207
Johannes Bergeccb8e82009-05-11 21:57:56 +03002208static int parse_station_flags(struct genl_info *info,
2209 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002210{
2211 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002212 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002213 int flag;
2214
Johannes Bergeccb8e82009-05-11 21:57:56 +03002215 /*
2216 * Try parsing the new attribute first so userspace
2217 * can specify both for older kernels.
2218 */
2219 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2220 if (nla) {
2221 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002222
Johannes Bergeccb8e82009-05-11 21:57:56 +03002223 sta_flags = nla_data(nla);
2224 params->sta_flags_mask = sta_flags->mask;
2225 params->sta_flags_set = sta_flags->set;
2226 if ((params->sta_flags_mask |
2227 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2228 return -EINVAL;
2229 return 0;
2230 }
2231
2232 /* if present, parse the old attribute */
2233
2234 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002235 if (!nla)
2236 return 0;
2237
2238 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2239 nla, sta_flags_policy))
2240 return -EINVAL;
2241
Johannes Bergeccb8e82009-05-11 21:57:56 +03002242 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2243 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002244
2245 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2246 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002247 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002248
2249 return 0;
2250}
2251
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002252static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2253 int attr)
2254{
2255 struct nlattr *rate;
2256 u16 bitrate;
2257
2258 rate = nla_nest_start(msg, attr);
2259 if (!rate)
2260 goto nla_put_failure;
2261
2262 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2263 bitrate = cfg80211_calculate_bitrate(info);
2264 if (bitrate > 0)
2265 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2266
2267 if (info->flags & RATE_INFO_FLAGS_MCS)
2268 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2269 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2270 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2271 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2272 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2273
2274 nla_nest_end(msg, rate);
2275 return true;
2276
2277nla_put_failure:
2278 return false;
2279}
2280
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002281static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2282 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002283 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002284{
2285 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002286 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002287
2288 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2289 if (!hdr)
2290 return -1;
2291
2292 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2293 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2294
Johannes Bergf5ea9122009-08-07 16:17:38 +02002295 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2296
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002297 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2298 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002299 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302300 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2301 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2302 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002303 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2304 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2305 sinfo->inactive_time);
2306 if (sinfo->filled & STATION_INFO_RX_BYTES)
2307 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2308 sinfo->rx_bytes);
2309 if (sinfo->filled & STATION_INFO_TX_BYTES)
2310 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2311 sinfo->tx_bytes);
2312 if (sinfo->filled & STATION_INFO_LLID)
2313 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2314 sinfo->llid);
2315 if (sinfo->filled & STATION_INFO_PLID)
2316 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2317 sinfo->plid);
2318 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2319 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2320 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002321 if (sinfo->filled & STATION_INFO_SIGNAL)
2322 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2323 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002324 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2325 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2326 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002327 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002328 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2329 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002330 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002331 }
2332 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2333 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2334 NL80211_STA_INFO_RX_BITRATE))
2335 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002336 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002337 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2338 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2339 sinfo->rx_packets);
2340 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2341 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2342 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002343 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2344 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2345 sinfo->tx_retries);
2346 if (sinfo->filled & STATION_INFO_TX_FAILED)
2347 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2348 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002349 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2350 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2351 if (!bss_param)
2352 goto nla_put_failure;
2353
2354 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2355 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2356 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2357 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2358 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2359 NLA_PUT_FLAG(msg,
2360 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2361 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2362 sinfo->bss_param.dtim_period);
2363 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2364 sinfo->bss_param.beacon_interval);
2365
2366 nla_nest_end(msg, bss_param);
2367 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002368 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2369 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2370 sizeof(struct nl80211_sta_flag_update),
2371 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002373
Felix Fietkau040bdf72011-08-10 19:00:33 -06002374 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002375 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2376 sinfo->assoc_req_ies);
2377
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002378 return genlmsg_end(msg, hdr);
2379
2380 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002381 genlmsg_cancel(msg, hdr);
2382 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002383}
2384
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002385static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002386 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002388 struct station_info sinfo;
2389 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002390 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002391 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002392 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002393 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394
Johannes Berg67748892010-10-04 21:14:06 +02002395 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2396 if (err)
2397 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002398
2399 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002400 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002401 goto out_err;
2402 }
2403
Johannes Bergbba95fe2008-07-29 13:22:51 +02002404 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002405 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002406 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2407 mac_addr, &sinfo);
2408 if (err == -ENOENT)
2409 break;
2410 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002411 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002412
2413 if (nl80211_send_station(skb,
2414 NETLINK_CB(cb->skb).pid,
2415 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2416 netdev, mac_addr,
2417 &sinfo) < 0)
2418 goto out;
2419
2420 sta_idx++;
2421 }
2422
2423
2424 out:
2425 cb->args[1] = sta_idx;
2426 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002427 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002428 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002429
2430 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002431}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002432
Johannes Berg5727ef12007-12-19 02:03:34 +01002433static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2434{
Johannes Berg4c476992010-10-04 21:36:35 +02002435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2436 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002437 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002438 struct sk_buff *msg;
2439 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002440 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002441
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002442 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002443
2444 if (!info->attrs[NL80211_ATTR_MAC])
2445 return -EINVAL;
2446
2447 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2448
Johannes Berg4c476992010-10-04 21:36:35 +02002449 if (!rdev->ops->get_station)
2450 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002451
Johannes Berg79c97e92009-07-07 03:56:12 +02002452 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002453 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002454 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002455
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002457 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002458 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002459
2460 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002461 dev, mac_addr, &sinfo) < 0) {
2462 nlmsg_free(msg);
2463 return -ENOBUFS;
2464 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002465
Johannes Berg4c476992010-10-04 21:36:35 +02002466 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002467}
2468
2469/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002470 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002471 */
Johannes Berg463d0182009-07-14 00:33:35 +02002472static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002473 struct cfg80211_registered_device *rdev,
2474 struct net_device **vlan)
2475{
Johannes Berg463d0182009-07-14 00:33:35 +02002476 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002477 *vlan = NULL;
2478
2479 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002480 *vlan = dev_get_by_index(genl_info_net(info),
2481 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002482 if (!*vlan)
2483 return -ENODEV;
2484 if (!(*vlan)->ieee80211_ptr)
2485 return -EINVAL;
2486 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2487 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002488 if (!netif_running(*vlan))
2489 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002490 }
2491 return 0;
2492}
2493
2494static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2495{
Johannes Berg4c476992010-10-04 21:36:35 +02002496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002497 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002498 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002499 struct station_parameters params;
2500 u8 *mac_addr = NULL;
2501
2502 memset(&params, 0, sizeof(params));
2503
2504 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002505 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002506
2507 if (info->attrs[NL80211_ATTR_STA_AID])
2508 return -EINVAL;
2509
2510 if (!info->attrs[NL80211_ATTR_MAC])
2511 return -EINVAL;
2512
2513 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2514
2515 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2516 params.supported_rates =
2517 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2518 params.supported_rates_len =
2519 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2520 }
2521
2522 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2523 params.listen_interval =
2524 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2525
Jouni Malinen36aedc92008-08-25 11:58:58 +03002526 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2527 params.ht_capa =
2528 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2529
Johannes Bergeccb8e82009-05-11 21:57:56 +03002530 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002531 return -EINVAL;
2532
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002533 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2534 params.plink_action =
2535 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2536
Javier Cardona9c3990a2011-05-03 16:57:11 -07002537 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2538 params.plink_state =
2539 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2540
Johannes Berg463d0182009-07-14 00:33:35 +02002541 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002542 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002543 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002544
2545 /* validate settings */
2546 err = 0;
2547
2548 switch (dev->ieee80211_ptr->iftype) {
2549 case NL80211_IFTYPE_AP:
2550 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002551 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002552 /* disallow mesh-specific things */
2553 if (params.plink_action)
2554 err = -EINVAL;
2555 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002556 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002557 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002558 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002559 if (params.plink_action)
2560 err = -EINVAL;
2561 if (params.vlan)
2562 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002563 if (params.supported_rates &&
2564 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002565 err = -EINVAL;
2566 if (params.ht_capa)
2567 err = -EINVAL;
2568 if (params.listen_interval >= 0)
2569 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002570 if (params.sta_flags_mask &
2571 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2572 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2573 err = -EINVAL;
2574 /* can't change the TDLS bit */
2575 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2576 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002577 err = -EINVAL;
2578 break;
2579 case NL80211_IFTYPE_MESH_POINT:
2580 /* disallow things mesh doesn't support */
2581 if (params.vlan)
2582 err = -EINVAL;
2583 if (params.ht_capa)
2584 err = -EINVAL;
2585 if (params.listen_interval >= 0)
2586 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002587 if (params.sta_flags_mask &
2588 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002589 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002590 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002591 err = -EINVAL;
2592 break;
2593 default:
2594 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002595 }
2596
Johannes Berg5727ef12007-12-19 02:03:34 +01002597 if (err)
2598 goto out;
2599
Johannes Berg79c97e92009-07-07 03:56:12 +02002600 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002601 err = -EOPNOTSUPP;
2602 goto out;
2603 }
2604
Johannes Berg79c97e92009-07-07 03:56:12 +02002605 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002606
2607 out:
2608 if (params.vlan)
2609 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002610
Johannes Berg5727ef12007-12-19 02:03:34 +01002611 return err;
2612}
2613
Eliad Pellerc75786c2011-08-23 14:37:46 +03002614static struct nla_policy
2615nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2616 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2617 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2618};
2619
Johannes Berg5727ef12007-12-19 02:03:34 +01002620static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2621{
Johannes Berg4c476992010-10-04 21:36:35 +02002622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002623 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002624 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002625 struct station_parameters params;
2626 u8 *mac_addr = NULL;
2627
2628 memset(&params, 0, sizeof(params));
2629
2630 if (!info->attrs[NL80211_ATTR_MAC])
2631 return -EINVAL;
2632
Johannes Berg5727ef12007-12-19 02:03:34 +01002633 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2634 return -EINVAL;
2635
2636 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2637 return -EINVAL;
2638
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002639 if (!info->attrs[NL80211_ATTR_STA_AID])
2640 return -EINVAL;
2641
Johannes Berg5727ef12007-12-19 02:03:34 +01002642 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2643 params.supported_rates =
2644 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2645 params.supported_rates_len =
2646 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2647 params.listen_interval =
2648 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002649
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002650 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2651 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2652 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002653
Jouni Malinen36aedc92008-08-25 11:58:58 +03002654 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2655 params.ht_capa =
2656 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002657
Javier Cardona96b78df2011-04-07 15:08:33 -07002658 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2659 params.plink_action =
2660 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2661
Johannes Bergeccb8e82009-05-11 21:57:56 +03002662 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002663 return -EINVAL;
2664
Eliad Pellerc75786c2011-08-23 14:37:46 +03002665 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002666 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002667 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002668 info->attrs[NL80211_ATTR_STA_WME]) {
2669 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2670 struct nlattr *nla;
2671
2672 nla = info->attrs[NL80211_ATTR_STA_WME];
2673 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2674 nl80211_sta_wme_policy);
2675 if (err)
2676 return err;
2677
2678 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2679 params.uapsd_queues =
2680 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002681 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2682 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002683
2684 if (tb[NL80211_STA_WME_MAX_SP])
2685 params.max_sp =
2686 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002687
2688 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2689 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002690
2691 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002692 }
2693
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002696 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002697 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2698 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2699 return -EINVAL;
2700
2701 /*
2702 * Only managed stations can add TDLS peers, and only when the
2703 * wiphy supports external TDLS setup.
2704 */
2705 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2706 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2707 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2708 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002709 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002710
Johannes Berg463d0182009-07-14 00:33:35 +02002711 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002712 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002713 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002714
2715 /* validate settings */
2716 err = 0;
2717
Johannes Berg79c97e92009-07-07 03:56:12 +02002718 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002719 err = -EOPNOTSUPP;
2720 goto out;
2721 }
2722
Johannes Berg79c97e92009-07-07 03:56:12 +02002723 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002724
2725 out:
2726 if (params.vlan)
2727 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002728 return err;
2729}
2730
2731static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2732{
Johannes Berg4c476992010-10-04 21:36:35 +02002733 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2734 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002735 u8 *mac_addr = NULL;
2736
2737 if (info->attrs[NL80211_ATTR_MAC])
2738 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2739
Johannes Berge80cf852009-05-11 14:43:13 +02002740 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002741 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002742 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002743 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2744 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002745
Johannes Berg4c476992010-10-04 21:36:35 +02002746 if (!rdev->ops->del_station)
2747 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002748
Johannes Berg4c476992010-10-04 21:36:35 +02002749 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002750}
2751
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002752static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2753 int flags, struct net_device *dev,
2754 u8 *dst, u8 *next_hop,
2755 struct mpath_info *pinfo)
2756{
2757 void *hdr;
2758 struct nlattr *pinfoattr;
2759
2760 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2761 if (!hdr)
2762 return -1;
2763
2764 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2765 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2766 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2767
Johannes Bergf5ea9122009-08-07 16:17:38 +02002768 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2769
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002770 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2771 if (!pinfoattr)
2772 goto nla_put_failure;
2773 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2774 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2775 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002776 if (pinfo->filled & MPATH_INFO_SN)
2777 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2778 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002779 if (pinfo->filled & MPATH_INFO_METRIC)
2780 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2781 pinfo->metric);
2782 if (pinfo->filled & MPATH_INFO_EXPTIME)
2783 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2784 pinfo->exptime);
2785 if (pinfo->filled & MPATH_INFO_FLAGS)
2786 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2787 pinfo->flags);
2788 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2789 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2790 pinfo->discovery_timeout);
2791 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2792 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2793 pinfo->discovery_retries);
2794
2795 nla_nest_end(msg, pinfoattr);
2796
2797 return genlmsg_end(msg, hdr);
2798
2799 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002800 genlmsg_cancel(msg, hdr);
2801 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002802}
2803
2804static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002805 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002806{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002807 struct mpath_info pinfo;
2808 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002809 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002810 u8 dst[ETH_ALEN];
2811 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002812 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002813 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002814
Johannes Berg67748892010-10-04 21:14:06 +02002815 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2816 if (err)
2817 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002818
2819 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002820 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002821 goto out_err;
2822 }
2823
Jouni Malineneec60b02009-03-20 21:21:19 +02002824 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2825 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002826 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002827 }
2828
Johannes Bergbba95fe2008-07-29 13:22:51 +02002829 while (1) {
2830 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2831 dst, next_hop, &pinfo);
2832 if (err == -ENOENT)
2833 break;
2834 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002835 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002836
2837 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2838 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2839 netdev, dst, next_hop,
2840 &pinfo) < 0)
2841 goto out;
2842
2843 path_idx++;
2844 }
2845
2846
2847 out:
2848 cb->args[1] = path_idx;
2849 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002850 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002851 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002852 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002853}
2854
2855static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2856{
Johannes Berg4c476992010-10-04 21:36:35 +02002857 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002858 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002859 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002860 struct mpath_info pinfo;
2861 struct sk_buff *msg;
2862 u8 *dst = NULL;
2863 u8 next_hop[ETH_ALEN];
2864
2865 memset(&pinfo, 0, sizeof(pinfo));
2866
2867 if (!info->attrs[NL80211_ATTR_MAC])
2868 return -EINVAL;
2869
2870 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2871
Johannes Berg4c476992010-10-04 21:36:35 +02002872 if (!rdev->ops->get_mpath)
2873 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002874
Johannes Berg4c476992010-10-04 21:36:35 +02002875 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2876 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002877
Johannes Berg79c97e92009-07-07 03:56:12 +02002878 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002879 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002880 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002881
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002883 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002884 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002885
2886 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002887 dev, dst, next_hop, &pinfo) < 0) {
2888 nlmsg_free(msg);
2889 return -ENOBUFS;
2890 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002891
Johannes Berg4c476992010-10-04 21:36:35 +02002892 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002893}
2894
2895static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2896{
Johannes Berg4c476992010-10-04 21:36:35 +02002897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2898 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002899 u8 *dst = NULL;
2900 u8 *next_hop = NULL;
2901
2902 if (!info->attrs[NL80211_ATTR_MAC])
2903 return -EINVAL;
2904
2905 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2906 return -EINVAL;
2907
2908 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2909 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2910
Johannes Berg4c476992010-10-04 21:36:35 +02002911 if (!rdev->ops->change_mpath)
2912 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002913
Johannes Berg4c476992010-10-04 21:36:35 +02002914 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2915 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002916
Johannes Berg4c476992010-10-04 21:36:35 +02002917 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002918}
Johannes Berg4c476992010-10-04 21:36:35 +02002919
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002920static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2921{
Johannes Berg4c476992010-10-04 21:36:35 +02002922 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2923 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002924 u8 *dst = NULL;
2925 u8 *next_hop = NULL;
2926
2927 if (!info->attrs[NL80211_ATTR_MAC])
2928 return -EINVAL;
2929
2930 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2931 return -EINVAL;
2932
2933 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2934 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2935
Johannes Berg4c476992010-10-04 21:36:35 +02002936 if (!rdev->ops->add_mpath)
2937 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002938
Johannes Berg4c476992010-10-04 21:36:35 +02002939 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2940 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002941
Johannes Berg4c476992010-10-04 21:36:35 +02002942 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002943}
2944
2945static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2946{
Johannes Berg4c476992010-10-04 21:36:35 +02002947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2948 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002949 u8 *dst = NULL;
2950
2951 if (info->attrs[NL80211_ATTR_MAC])
2952 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2953
Johannes Berg4c476992010-10-04 21:36:35 +02002954 if (!rdev->ops->del_mpath)
2955 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002956
Johannes Berg4c476992010-10-04 21:36:35 +02002957 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002958}
2959
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002960static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2961{
Johannes Berg4c476992010-10-04 21:36:35 +02002962 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2963 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002964 struct bss_parameters params;
2965
2966 memset(&params, 0, sizeof(params));
2967 /* default to not changing parameters */
2968 params.use_cts_prot = -1;
2969 params.use_short_preamble = -1;
2970 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002971 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002972 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002973
2974 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2975 params.use_cts_prot =
2976 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2977 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2978 params.use_short_preamble =
2979 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2980 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2981 params.use_short_slot_time =
2982 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002983 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2984 params.basic_rates =
2985 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2986 params.basic_rates_len =
2987 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2988 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002989 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2990 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002991 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2992 params.ht_opmode =
2993 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002994
Johannes Berg4c476992010-10-04 21:36:35 +02002995 if (!rdev->ops->change_bss)
2996 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002997
Johannes Berg074ac8d2010-09-16 14:58:22 +02002998 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002999 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3000 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003001
Johannes Berg4c476992010-10-04 21:36:35 +02003002 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003003}
3004
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003005static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003006 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3007 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3008 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3009 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3010 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3011 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3012};
3013
3014static int parse_reg_rule(struct nlattr *tb[],
3015 struct ieee80211_reg_rule *reg_rule)
3016{
3017 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3018 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3019
3020 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3021 return -EINVAL;
3022 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3023 return -EINVAL;
3024 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3025 return -EINVAL;
3026 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3027 return -EINVAL;
3028 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3029 return -EINVAL;
3030
3031 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3032
3033 freq_range->start_freq_khz =
3034 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3035 freq_range->end_freq_khz =
3036 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3037 freq_range->max_bandwidth_khz =
3038 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3039
3040 power_rule->max_eirp =
3041 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3042
3043 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3044 power_rule->max_antenna_gain =
3045 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3046
3047 return 0;
3048}
3049
3050static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3051{
3052 int r;
3053 char *data = NULL;
3054
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003055 /*
3056 * You should only get this when cfg80211 hasn't yet initialized
3057 * completely when built-in to the kernel right between the time
3058 * window between nl80211_init() and regulatory_init(), if that is
3059 * even possible.
3060 */
3061 mutex_lock(&cfg80211_mutex);
3062 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003063 mutex_unlock(&cfg80211_mutex);
3064 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003065 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003066 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003067
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003068 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3069 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003070
3071 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3072
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003073 r = regulatory_hint_user(data);
3074
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003075 return r;
3076}
3077
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003078static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003079 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003080{
Johannes Berg4c476992010-10-04 21:36:35 +02003081 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003082 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003083 struct wireless_dev *wdev = dev->ieee80211_ptr;
3084 struct mesh_config cur_params;
3085 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003086 void *hdr;
3087 struct nlattr *pinfoattr;
3088 struct sk_buff *msg;
3089
Johannes Berg29cbe682010-12-03 09:20:44 +01003090 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3091 return -EOPNOTSUPP;
3092
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003093 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003094 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003095
Johannes Berg29cbe682010-12-03 09:20:44 +01003096 wdev_lock(wdev);
3097 /* If not connected, get default parameters */
3098 if (!wdev->mesh_id_len)
3099 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3100 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003101 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003102 &cur_params);
3103 wdev_unlock(wdev);
3104
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003105 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003106 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003107
3108 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003109 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003110 if (!msg)
3111 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003112 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003113 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003114 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003115 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003116 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003117 if (!pinfoattr)
3118 goto nla_put_failure;
3119 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3120 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3121 cur_params.dot11MeshRetryTimeout);
3122 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3123 cur_params.dot11MeshConfirmTimeout);
3124 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3125 cur_params.dot11MeshHoldingTimeout);
3126 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3127 cur_params.dot11MeshMaxPeerLinks);
3128 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3129 cur_params.dot11MeshMaxRetries);
3130 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3131 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003132 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3133 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003134 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3135 cur_params.auto_open_plinks);
3136 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3137 cur_params.dot11MeshHWMPmaxPREQretries);
3138 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3139 cur_params.path_refresh_time);
3140 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3141 cur_params.min_discovery_timeout);
3142 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3143 cur_params.dot11MeshHWMPactivePathTimeout);
3144 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3145 cur_params.dot11MeshHWMPpreqMinInterval);
3146 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3147 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003148 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3149 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003150 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3151 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003152 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3153 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003154 nla_nest_end(msg, pinfoattr);
3155 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003156 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003157
Johannes Berg3b858752009-03-12 09:55:09 +01003158 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003159 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003160 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003161 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003162 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003163}
3164
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003165static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003166 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3167 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3168 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3169 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3170 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3171 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003172 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003173 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3174
3175 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3176 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3177 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3178 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3179 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3180 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003181 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003182 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003183 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003184};
3185
Javier Cardonac80d5452010-12-16 17:37:49 -08003186static const struct nla_policy
3187 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3188 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3189 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003190 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003191 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003192 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003193 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003194};
3195
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003196static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003197 struct mesh_config *cfg,
3198 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003199{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003200 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003201 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003202
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003203#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3204do {\
3205 if (table[attr_num]) {\
3206 cfg->param = nla_fn(table[attr_num]); \
3207 mask |= (1 << (attr_num - 1)); \
3208 } \
3209} while (0);\
3210
3211
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003212 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003213 return -EINVAL;
3214 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003215 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003216 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003217 return -EINVAL;
3218
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003219 /* This makes sure that there aren't more than 32 mesh config
3220 * parameters (otherwise our bitfield scheme would not work.) */
3221 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3222
3223 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003224 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3225 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3226 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3227 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3228 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3229 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3230 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3231 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3232 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3233 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3234 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3235 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003236 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3237 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003238 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3239 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3240 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3241 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3242 nla_get_u8);
3243 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3244 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3245 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3246 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3247 nla_get_u16);
3248 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3249 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3250 nla_get_u32);
3251 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3252 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3253 nla_get_u16);
3254 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3255 dot11MeshHWMPnetDiameterTraversalTime,
3256 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3257 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003258 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3259 dot11MeshHWMPRootMode, mask,
3260 NL80211_MESHCONF_HWMP_ROOTMODE,
3261 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003262 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3263 dot11MeshHWMPRannInterval, mask,
3264 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3265 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003266 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3267 dot11MeshGateAnnouncementProtocol, mask,
3268 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3269 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003270 if (mask_out)
3271 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003272
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003273 return 0;
3274
3275#undef FILL_IN_MESH_PARAM_IF_SET
3276}
3277
Javier Cardonac80d5452010-12-16 17:37:49 -08003278static int nl80211_parse_mesh_setup(struct genl_info *info,
3279 struct mesh_setup *setup)
3280{
3281 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3282
3283 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3284 return -EINVAL;
3285 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3286 info->attrs[NL80211_ATTR_MESH_SETUP],
3287 nl80211_mesh_setup_params_policy))
3288 return -EINVAL;
3289
3290 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3291 setup->path_sel_proto =
3292 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3293 IEEE80211_PATH_PROTOCOL_VENDOR :
3294 IEEE80211_PATH_PROTOCOL_HWMP;
3295
3296 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3297 setup->path_metric =
3298 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3299 IEEE80211_PATH_METRIC_VENDOR :
3300 IEEE80211_PATH_METRIC_AIRTIME;
3301
Javier Cardona581a8b02011-04-07 15:08:27 -07003302
3303 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003304 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003305 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003306 if (!is_valid_ie_attr(ieattr))
3307 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003308 setup->ie = nla_data(ieattr);
3309 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003310 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003311 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3312 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003313
3314 return 0;
3315}
3316
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003317static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003318 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003319{
3320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3321 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003322 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003323 struct mesh_config cfg;
3324 u32 mask;
3325 int err;
3326
Johannes Berg29cbe682010-12-03 09:20:44 +01003327 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3328 return -EOPNOTSUPP;
3329
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003330 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003331 return -EOPNOTSUPP;
3332
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003333 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003334 if (err)
3335 return err;
3336
Johannes Berg29cbe682010-12-03 09:20:44 +01003337 wdev_lock(wdev);
3338 if (!wdev->mesh_id_len)
3339 err = -ENOLINK;
3340
3341 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003342 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003343 mask, &cfg);
3344
3345 wdev_unlock(wdev);
3346
3347 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003348}
3349
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003350static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3351{
3352 struct sk_buff *msg;
3353 void *hdr = NULL;
3354 struct nlattr *nl_reg_rules;
3355 unsigned int i;
3356 int err = -EINVAL;
3357
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003358 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003359
3360 if (!cfg80211_regdomain)
3361 goto out;
3362
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003363 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003364 if (!msg) {
3365 err = -ENOBUFS;
3366 goto out;
3367 }
3368
3369 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3370 NL80211_CMD_GET_REG);
3371 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003372 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003373
3374 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3375 cfg80211_regdomain->alpha2);
3376
3377 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3378 if (!nl_reg_rules)
3379 goto nla_put_failure;
3380
3381 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3382 struct nlattr *nl_reg_rule;
3383 const struct ieee80211_reg_rule *reg_rule;
3384 const struct ieee80211_freq_range *freq_range;
3385 const struct ieee80211_power_rule *power_rule;
3386
3387 reg_rule = &cfg80211_regdomain->reg_rules[i];
3388 freq_range = &reg_rule->freq_range;
3389 power_rule = &reg_rule->power_rule;
3390
3391 nl_reg_rule = nla_nest_start(msg, i);
3392 if (!nl_reg_rule)
3393 goto nla_put_failure;
3394
3395 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3396 reg_rule->flags);
3397 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3398 freq_range->start_freq_khz);
3399 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3400 freq_range->end_freq_khz);
3401 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3402 freq_range->max_bandwidth_khz);
3403 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3404 power_rule->max_antenna_gain);
3405 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3406 power_rule->max_eirp);
3407
3408 nla_nest_end(msg, nl_reg_rule);
3409 }
3410
3411 nla_nest_end(msg, nl_reg_rules);
3412
3413 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003414 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003415 goto out;
3416
3417nla_put_failure:
3418 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003419put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003420 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003421 err = -EMSGSIZE;
3422out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003423 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003424 return err;
3425}
3426
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003427static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3428{
3429 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3430 struct nlattr *nl_reg_rule;
3431 char *alpha2 = NULL;
3432 int rem_reg_rules = 0, r = 0;
3433 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3434 struct ieee80211_regdomain *rd = NULL;
3435
3436 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3437 return -EINVAL;
3438
3439 if (!info->attrs[NL80211_ATTR_REG_RULES])
3440 return -EINVAL;
3441
3442 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3443
3444 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3445 rem_reg_rules) {
3446 num_rules++;
3447 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003448 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003449 }
3450
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003451 mutex_lock(&cfg80211_mutex);
3452
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003453 if (!reg_is_valid_request(alpha2)) {
3454 r = -EINVAL;
3455 goto bad_reg;
3456 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003457
3458 size_of_regd = sizeof(struct ieee80211_regdomain) +
3459 (num_rules * sizeof(struct ieee80211_reg_rule));
3460
3461 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003462 if (!rd) {
3463 r = -ENOMEM;
3464 goto bad_reg;
3465 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003466
3467 rd->n_reg_rules = num_rules;
3468 rd->alpha2[0] = alpha2[0];
3469 rd->alpha2[1] = alpha2[1];
3470
3471 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3472 rem_reg_rules) {
3473 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3474 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3475 reg_rule_policy);
3476 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3477 if (r)
3478 goto bad_reg;
3479
3480 rule_idx++;
3481
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003482 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3483 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003484 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003485 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003486 }
3487
3488 BUG_ON(rule_idx != num_rules);
3489
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003490 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003491
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003492 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003493
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003494 return r;
3495
Johannes Bergd2372b32008-10-24 20:32:20 +02003496 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003497 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003498 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003499 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003500}
3501
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003502static int validate_scan_freqs(struct nlattr *freqs)
3503{
3504 struct nlattr *attr1, *attr2;
3505 int n_channels = 0, tmp1, tmp2;
3506
3507 nla_for_each_nested(attr1, freqs, tmp1) {
3508 n_channels++;
3509 /*
3510 * Some hardware has a limited channel list for
3511 * scanning, and it is pretty much nonsensical
3512 * to scan for a channel twice, so disallow that
3513 * and don't require drivers to check that the
3514 * channel list they get isn't longer than what
3515 * they can scan, as long as they can scan all
3516 * the channels they registered at once.
3517 */
3518 nla_for_each_nested(attr2, freqs, tmp2)
3519 if (attr1 != attr2 &&
3520 nla_get_u32(attr1) == nla_get_u32(attr2))
3521 return 0;
3522 }
3523
3524 return n_channels;
3525}
3526
Johannes Berg2a519312009-02-10 21:25:55 +01003527static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3528{
Johannes Berg4c476992010-10-04 21:36:35 +02003529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3530 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003531 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003532 struct nlattr *attr;
3533 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003534 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003535 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003536
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003537 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3538 return -EINVAL;
3539
Johannes Berg79c97e92009-07-07 03:56:12 +02003540 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003541
Johannes Berg4c476992010-10-04 21:36:35 +02003542 if (!rdev->ops->scan)
3543 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003544
Johannes Berg4c476992010-10-04 21:36:35 +02003545 if (rdev->scan_req)
3546 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003547
3548 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003549 n_channels = validate_scan_freqs(
3550 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003551 if (!n_channels)
3552 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003553 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003554 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003555 n_channels = 0;
3556
Johannes Berg2a519312009-02-10 21:25:55 +01003557 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3558 if (wiphy->bands[band])
3559 n_channels += wiphy->bands[band]->n_channels;
3560 }
3561
3562 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3563 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3564 n_ssids++;
3565
Johannes Berg4c476992010-10-04 21:36:35 +02003566 if (n_ssids > wiphy->max_scan_ssids)
3567 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003568
Jouni Malinen70692ad2009-02-16 19:39:13 +02003569 if (info->attrs[NL80211_ATTR_IE])
3570 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3571 else
3572 ie_len = 0;
3573
Johannes Berg4c476992010-10-04 21:36:35 +02003574 if (ie_len > wiphy->max_scan_ie_len)
3575 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003576
Johannes Berg2a519312009-02-10 21:25:55 +01003577 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003578 + sizeof(*request->ssids) * n_ssids
3579 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003580 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003581 if (!request)
3582 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003583
Johannes Berg2a519312009-02-10 21:25:55 +01003584 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003585 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003586 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003587 if (ie_len) {
3588 if (request->ssids)
3589 request->ie = (void *)(request->ssids + n_ssids);
3590 else
3591 request->ie = (void *)(request->channels + n_channels);
3592 }
Johannes Berg2a519312009-02-10 21:25:55 +01003593
Johannes Berg584991d2009-11-02 13:32:03 +01003594 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003595 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3596 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003597 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003598 struct ieee80211_channel *chan;
3599
3600 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3601
3602 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003603 err = -EINVAL;
3604 goto out_free;
3605 }
Johannes Berg584991d2009-11-02 13:32:03 +01003606
3607 /* ignore disabled channels */
3608 if (chan->flags & IEEE80211_CHAN_DISABLED)
3609 continue;
3610
3611 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003612 i++;
3613 }
3614 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003615 enum ieee80211_band band;
3616
Johannes Berg2a519312009-02-10 21:25:55 +01003617 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003618 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3619 int j;
3620 if (!wiphy->bands[band])
3621 continue;
3622 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003623 struct ieee80211_channel *chan;
3624
3625 chan = &wiphy->bands[band]->channels[j];
3626
3627 if (chan->flags & IEEE80211_CHAN_DISABLED)
3628 continue;
3629
3630 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003631 i++;
3632 }
3633 }
3634 }
3635
Johannes Berg584991d2009-11-02 13:32:03 +01003636 if (!i) {
3637 err = -EINVAL;
3638 goto out_free;
3639 }
3640
3641 request->n_channels = i;
3642
Johannes Berg2a519312009-02-10 21:25:55 +01003643 i = 0;
3644 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3645 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003646 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003647 err = -EINVAL;
3648 goto out_free;
3649 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003650 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003651 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003652 i++;
3653 }
3654 }
3655
Jouni Malinen70692ad2009-02-16 19:39:13 +02003656 if (info->attrs[NL80211_ATTR_IE]) {
3657 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003658 memcpy((void *)request->ie,
3659 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003660 request->ie_len);
3661 }
3662
Johannes Berg34850ab2011-07-18 18:08:35 +02003663 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003664 if (wiphy->bands[i])
3665 request->rates[i] =
3666 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003667
3668 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3669 nla_for_each_nested(attr,
3670 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3671 tmp) {
3672 enum ieee80211_band band = nla_type(attr);
3673
Dan Carpenter84404622011-07-29 11:52:18 +03003674 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003675 err = -EINVAL;
3676 goto out_free;
3677 }
3678 err = ieee80211_get_ratemask(wiphy->bands[band],
3679 nla_data(attr),
3680 nla_len(attr),
3681 &request->rates[band]);
3682 if (err)
3683 goto out_free;
3684 }
3685 }
3686
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303687 request->no_cck =
3688 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3689
Johannes Berg463d0182009-07-14 00:33:35 +02003690 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003691 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003692
Johannes Berg79c97e92009-07-07 03:56:12 +02003693 rdev->scan_req = request;
3694 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003695
Johannes Berg463d0182009-07-14 00:33:35 +02003696 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003697 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003698 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003699 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003700 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003701 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003702 kfree(request);
3703 }
Johannes Berg3b858752009-03-12 09:55:09 +01003704
Johannes Berg2a519312009-02-10 21:25:55 +01003705 return err;
3706}
3707
Luciano Coelho807f8a82011-05-11 17:09:35 +03003708static int nl80211_start_sched_scan(struct sk_buff *skb,
3709 struct genl_info *info)
3710{
3711 struct cfg80211_sched_scan_request *request;
3712 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3713 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003714 struct nlattr *attr;
3715 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003716 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003717 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003718 enum ieee80211_band band;
3719 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003720 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003721
3722 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3723 !rdev->ops->sched_scan_start)
3724 return -EOPNOTSUPP;
3725
3726 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3727 return -EINVAL;
3728
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003729 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3730 return -EINVAL;
3731
3732 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3733 if (interval == 0)
3734 return -EINVAL;
3735
Luciano Coelho807f8a82011-05-11 17:09:35 +03003736 wiphy = &rdev->wiphy;
3737
3738 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3739 n_channels = validate_scan_freqs(
3740 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3741 if (!n_channels)
3742 return -EINVAL;
3743 } else {
3744 n_channels = 0;
3745
3746 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3747 if (wiphy->bands[band])
3748 n_channels += wiphy->bands[band]->n_channels;
3749 }
3750
3751 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3752 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3753 tmp)
3754 n_ssids++;
3755
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003756 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003757 return -EINVAL;
3758
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003759 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3760 nla_for_each_nested(attr,
3761 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3762 tmp)
3763 n_match_sets++;
3764
3765 if (n_match_sets > wiphy->max_match_sets)
3766 return -EINVAL;
3767
Luciano Coelho807f8a82011-05-11 17:09:35 +03003768 if (info->attrs[NL80211_ATTR_IE])
3769 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3770 else
3771 ie_len = 0;
3772
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003773 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003774 return -EINVAL;
3775
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003776 mutex_lock(&rdev->sched_scan_mtx);
3777
3778 if (rdev->sched_scan_req) {
3779 err = -EINPROGRESS;
3780 goto out;
3781 }
3782
Luciano Coelho807f8a82011-05-11 17:09:35 +03003783 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003784 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003785 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003786 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003787 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003788 if (!request) {
3789 err = -ENOMEM;
3790 goto out;
3791 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003792
3793 if (n_ssids)
3794 request->ssids = (void *)&request->channels[n_channels];
3795 request->n_ssids = n_ssids;
3796 if (ie_len) {
3797 if (request->ssids)
3798 request->ie = (void *)(request->ssids + n_ssids);
3799 else
3800 request->ie = (void *)(request->channels + n_channels);
3801 }
3802
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003803 if (n_match_sets) {
3804 if (request->ie)
3805 request->match_sets = (void *)(request->ie + ie_len);
3806 else if (request->ssids)
3807 request->match_sets =
3808 (void *)(request->ssids + n_ssids);
3809 else
3810 request->match_sets =
3811 (void *)(request->channels + n_channels);
3812 }
3813 request->n_match_sets = n_match_sets;
3814
Luciano Coelho807f8a82011-05-11 17:09:35 +03003815 i = 0;
3816 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3817 /* user specified, bail out if channel not found */
3818 nla_for_each_nested(attr,
3819 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3820 tmp) {
3821 struct ieee80211_channel *chan;
3822
3823 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3824
3825 if (!chan) {
3826 err = -EINVAL;
3827 goto out_free;
3828 }
3829
3830 /* ignore disabled channels */
3831 if (chan->flags & IEEE80211_CHAN_DISABLED)
3832 continue;
3833
3834 request->channels[i] = chan;
3835 i++;
3836 }
3837 } else {
3838 /* all channels */
3839 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3840 int j;
3841 if (!wiphy->bands[band])
3842 continue;
3843 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3844 struct ieee80211_channel *chan;
3845
3846 chan = &wiphy->bands[band]->channels[j];
3847
3848 if (chan->flags & IEEE80211_CHAN_DISABLED)
3849 continue;
3850
3851 request->channels[i] = chan;
3852 i++;
3853 }
3854 }
3855 }
3856
3857 if (!i) {
3858 err = -EINVAL;
3859 goto out_free;
3860 }
3861
3862 request->n_channels = i;
3863
3864 i = 0;
3865 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3866 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3867 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003868 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003869 err = -EINVAL;
3870 goto out_free;
3871 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003872 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003873 memcpy(request->ssids[i].ssid, nla_data(attr),
3874 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003875 i++;
3876 }
3877 }
3878
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003879 i = 0;
3880 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3881 nla_for_each_nested(attr,
3882 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3883 tmp) {
3884 struct nlattr *ssid;
3885
3886 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3887 nla_data(attr), nla_len(attr),
3888 nl80211_match_policy);
3889 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3890 if (ssid) {
3891 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3892 err = -EINVAL;
3893 goto out_free;
3894 }
3895 memcpy(request->match_sets[i].ssid.ssid,
3896 nla_data(ssid), nla_len(ssid));
3897 request->match_sets[i].ssid.ssid_len =
3898 nla_len(ssid);
3899 }
3900 i++;
3901 }
3902 }
3903
Luciano Coelho807f8a82011-05-11 17:09:35 +03003904 if (info->attrs[NL80211_ATTR_IE]) {
3905 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3906 memcpy((void *)request->ie,
3907 nla_data(info->attrs[NL80211_ATTR_IE]),
3908 request->ie_len);
3909 }
3910
3911 request->dev = dev;
3912 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003913 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003914
3915 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3916 if (!err) {
3917 rdev->sched_scan_req = request;
3918 nl80211_send_sched_scan(rdev, dev,
3919 NL80211_CMD_START_SCHED_SCAN);
3920 goto out;
3921 }
3922
3923out_free:
3924 kfree(request);
3925out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003926 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003927 return err;
3928}
3929
3930static int nl80211_stop_sched_scan(struct sk_buff *skb,
3931 struct genl_info *info)
3932{
3933 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003934 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003935
3936 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3937 !rdev->ops->sched_scan_stop)
3938 return -EOPNOTSUPP;
3939
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003940 mutex_lock(&rdev->sched_scan_mtx);
3941 err = __cfg80211_stop_sched_scan(rdev, false);
3942 mutex_unlock(&rdev->sched_scan_mtx);
3943
3944 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003945}
3946
Johannes Berg9720bb32011-06-21 09:45:33 +02003947static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3948 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003949 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003950 struct wireless_dev *wdev,
3951 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003952{
Johannes Berg48ab9052009-07-10 18:42:31 +02003953 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003954 void *hdr;
3955 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003956 int i;
3957
3958 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003959
Johannes Berg9720bb32011-06-21 09:45:33 +02003960 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003961 NL80211_CMD_NEW_SCAN_RESULTS);
3962 if (!hdr)
3963 return -1;
3964
Johannes Berg9720bb32011-06-21 09:45:33 +02003965 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3966
Johannes Bergf5ea9122009-08-07 16:17:38 +02003967 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003968 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003969
3970 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3971 if (!bss)
3972 goto nla_put_failure;
3973 if (!is_zero_ether_addr(res->bssid))
3974 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3975 if (res->information_elements && res->len_information_elements)
3976 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3977 res->len_information_elements,
3978 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003979 if (res->beacon_ies && res->len_beacon_ies &&
3980 res->beacon_ies != res->information_elements)
3981 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3982 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003983 if (res->tsf)
3984 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3985 if (res->beacon_interval)
3986 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3987 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3988 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003989 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3990 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003991
Johannes Berg77965c92009-02-18 18:45:06 +01003992 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003993 case CFG80211_SIGNAL_TYPE_MBM:
3994 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3995 break;
3996 case CFG80211_SIGNAL_TYPE_UNSPEC:
3997 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3998 break;
3999 default:
4000 break;
4001 }
4002
Johannes Berg48ab9052009-07-10 18:42:31 +02004003 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004004 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004005 case NL80211_IFTYPE_STATION:
4006 if (intbss == wdev->current_bss)
4007 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4008 NL80211_BSS_STATUS_ASSOCIATED);
4009 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4010 if (intbss != wdev->auth_bsses[i])
4011 continue;
4012 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4013 NL80211_BSS_STATUS_AUTHENTICATED);
4014 break;
4015 }
4016 break;
4017 case NL80211_IFTYPE_ADHOC:
4018 if (intbss == wdev->current_bss)
4019 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4020 NL80211_BSS_STATUS_IBSS_JOINED);
4021 break;
4022 default:
4023 break;
4024 }
4025
Johannes Berg2a519312009-02-10 21:25:55 +01004026 nla_nest_end(msg, bss);
4027
4028 return genlmsg_end(msg, hdr);
4029
4030 nla_put_failure:
4031 genlmsg_cancel(msg, hdr);
4032 return -EMSGSIZE;
4033}
4034
4035static int nl80211_dump_scan(struct sk_buff *skb,
4036 struct netlink_callback *cb)
4037{
Johannes Berg48ab9052009-07-10 18:42:31 +02004038 struct cfg80211_registered_device *rdev;
4039 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004040 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004041 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004042 int start = cb->args[1], idx = 0;
4043 int err;
4044
Johannes Berg67748892010-10-04 21:14:06 +02004045 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4046 if (err)
4047 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004048
Johannes Berg48ab9052009-07-10 18:42:31 +02004049 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004050
Johannes Berg48ab9052009-07-10 18:42:31 +02004051 wdev_lock(wdev);
4052 spin_lock_bh(&rdev->bss_lock);
4053 cfg80211_bss_expire(rdev);
4054
Johannes Berg9720bb32011-06-21 09:45:33 +02004055 cb->seq = rdev->bss_generation;
4056
Johannes Berg48ab9052009-07-10 18:42:31 +02004057 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004058 if (++idx <= start)
4059 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004060 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004061 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004062 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004063 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004064 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004065 }
4066 }
4067
Johannes Berg48ab9052009-07-10 18:42:31 +02004068 spin_unlock_bh(&rdev->bss_lock);
4069 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004070
4071 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004072 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004073
Johannes Berg67748892010-10-04 21:14:06 +02004074 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004075}
4076
Holger Schurig61fa7132009-11-11 12:25:40 +01004077static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4078 int flags, struct net_device *dev,
4079 struct survey_info *survey)
4080{
4081 void *hdr;
4082 struct nlattr *infoattr;
4083
Holger Schurig61fa7132009-11-11 12:25:40 +01004084 hdr = nl80211hdr_put(msg, pid, seq, flags,
4085 NL80211_CMD_NEW_SURVEY_RESULTS);
4086 if (!hdr)
4087 return -ENOMEM;
4088
4089 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4090
4091 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4092 if (!infoattr)
4093 goto nla_put_failure;
4094
4095 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4096 survey->channel->center_freq);
4097 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4098 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4099 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004100 if (survey->filled & SURVEY_INFO_IN_USE)
4101 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004102 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4103 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4104 survey->channel_time);
4105 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4106 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4107 survey->channel_time_busy);
4108 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4109 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4110 survey->channel_time_ext_busy);
4111 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4112 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4113 survey->channel_time_rx);
4114 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4115 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4116 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004117
4118 nla_nest_end(msg, infoattr);
4119
4120 return genlmsg_end(msg, hdr);
4121
4122 nla_put_failure:
4123 genlmsg_cancel(msg, hdr);
4124 return -EMSGSIZE;
4125}
4126
4127static int nl80211_dump_survey(struct sk_buff *skb,
4128 struct netlink_callback *cb)
4129{
4130 struct survey_info survey;
4131 struct cfg80211_registered_device *dev;
4132 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004133 int survey_idx = cb->args[1];
4134 int res;
4135
Johannes Berg67748892010-10-04 21:14:06 +02004136 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4137 if (res)
4138 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004139
4140 if (!dev->ops->dump_survey) {
4141 res = -EOPNOTSUPP;
4142 goto out_err;
4143 }
4144
4145 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004146 struct ieee80211_channel *chan;
4147
Holger Schurig61fa7132009-11-11 12:25:40 +01004148 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4149 &survey);
4150 if (res == -ENOENT)
4151 break;
4152 if (res)
4153 goto out_err;
4154
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004155 /* Survey without a channel doesn't make sense */
4156 if (!survey.channel) {
4157 res = -EINVAL;
4158 goto out;
4159 }
4160
4161 chan = ieee80211_get_channel(&dev->wiphy,
4162 survey.channel->center_freq);
4163 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4164 survey_idx++;
4165 continue;
4166 }
4167
Holger Schurig61fa7132009-11-11 12:25:40 +01004168 if (nl80211_send_survey(skb,
4169 NETLINK_CB(cb->skb).pid,
4170 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4171 netdev,
4172 &survey) < 0)
4173 goto out;
4174 survey_idx++;
4175 }
4176
4177 out:
4178 cb->args[1] = survey_idx;
4179 res = skb->len;
4180 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004181 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004182 return res;
4183}
4184
Jouni Malinen255e7372009-03-20 21:21:17 +02004185static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4186{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004187 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004188}
4189
Samuel Ortizb23aa672009-07-01 21:26:54 +02004190static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4191{
4192 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4193 NL80211_WPA_VERSION_2));
4194}
4195
Jouni Malinen636a5d32009-03-19 13:39:22 +02004196static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4197{
Johannes Berg4c476992010-10-04 21:36:35 +02004198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4199 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004200 struct ieee80211_channel *chan;
4201 const u8 *bssid, *ssid, *ie = NULL;
4202 int err, ssid_len, ie_len = 0;
4203 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004204 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004205 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004206
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004207 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4208 return -EINVAL;
4209
4210 if (!info->attrs[NL80211_ATTR_MAC])
4211 return -EINVAL;
4212
Jouni Malinen17780922009-03-27 20:52:47 +02004213 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4214 return -EINVAL;
4215
Johannes Berg19957bb2009-07-02 17:20:43 +02004216 if (!info->attrs[NL80211_ATTR_SSID])
4217 return -EINVAL;
4218
4219 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4220 return -EINVAL;
4221
Johannes Bergfffd0932009-07-08 14:22:54 +02004222 err = nl80211_parse_key(info, &key);
4223 if (err)
4224 return err;
4225
4226 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004227 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4228 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004229 if (!key.p.key || !key.p.key_len)
4230 return -EINVAL;
4231 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4232 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4233 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4234 key.p.key_len != WLAN_KEY_LEN_WEP104))
4235 return -EINVAL;
4236 if (key.idx > 4)
4237 return -EINVAL;
4238 } else {
4239 key.p.key_len = 0;
4240 key.p.key = NULL;
4241 }
4242
Johannes Bergafea0b72010-08-10 09:46:42 +02004243 if (key.idx >= 0) {
4244 int i;
4245 bool ok = false;
4246 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4247 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4248 ok = true;
4249 break;
4250 }
4251 }
Johannes Berg4c476992010-10-04 21:36:35 +02004252 if (!ok)
4253 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004254 }
4255
Johannes Berg4c476992010-10-04 21:36:35 +02004256 if (!rdev->ops->auth)
4257 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004258
Johannes Berg074ac8d2010-09-16 14:58:22 +02004259 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004260 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4261 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004262
Johannes Berg19957bb2009-07-02 17:20:43 +02004263 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004264 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004265 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004266 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4267 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004268
Johannes Berg19957bb2009-07-02 17:20:43 +02004269 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4270 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4271
4272 if (info->attrs[NL80211_ATTR_IE]) {
4273 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4274 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4275 }
4276
4277 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004278 if (!nl80211_valid_auth_type(auth_type))
4279 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004280
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004281 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4282
Johannes Berg4c476992010-10-04 21:36:35 +02004283 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4284 ssid, ssid_len, ie, ie_len,
4285 key.p.key, key.p.key_len, key.idx,
4286 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004287}
4288
Johannes Bergc0692b82010-08-27 14:26:53 +03004289static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4290 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004291 struct cfg80211_crypto_settings *settings,
4292 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004293{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004294 memset(settings, 0, sizeof(*settings));
4295
Samuel Ortizb23aa672009-07-01 21:26:54 +02004296 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4297
Johannes Bergc0692b82010-08-27 14:26:53 +03004298 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4299 u16 proto;
4300 proto = nla_get_u16(
4301 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4302 settings->control_port_ethertype = cpu_to_be16(proto);
4303 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4304 proto != ETH_P_PAE)
4305 return -EINVAL;
4306 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4307 settings->control_port_no_encrypt = true;
4308 } else
4309 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4310
Samuel Ortizb23aa672009-07-01 21:26:54 +02004311 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4312 void *data;
4313 int len, i;
4314
4315 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4316 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4317 settings->n_ciphers_pairwise = len / sizeof(u32);
4318
4319 if (len % sizeof(u32))
4320 return -EINVAL;
4321
Johannes Berg3dc27d22009-07-02 21:36:37 +02004322 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004323 return -EINVAL;
4324
4325 memcpy(settings->ciphers_pairwise, data, len);
4326
4327 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004328 if (!cfg80211_supported_cipher_suite(
4329 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004330 settings->ciphers_pairwise[i]))
4331 return -EINVAL;
4332 }
4333
4334 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4335 settings->cipher_group =
4336 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004337 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4338 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004339 return -EINVAL;
4340 }
4341
4342 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4343 settings->wpa_versions =
4344 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4345 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4346 return -EINVAL;
4347 }
4348
4349 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4350 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004351 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004352
4353 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4354 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4355 settings->n_akm_suites = len / sizeof(u32);
4356
4357 if (len % sizeof(u32))
4358 return -EINVAL;
4359
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004360 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4361 return -EINVAL;
4362
Samuel Ortizb23aa672009-07-01 21:26:54 +02004363 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004364 }
4365
4366 return 0;
4367}
4368
Jouni Malinen636a5d32009-03-19 13:39:22 +02004369static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4370{
Johannes Berg4c476992010-10-04 21:36:35 +02004371 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4372 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004373 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004374 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004375 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004376 int err, ssid_len, ie_len = 0;
4377 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004378
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004379 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4380 return -EINVAL;
4381
4382 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004383 !info->attrs[NL80211_ATTR_SSID] ||
4384 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004385 return -EINVAL;
4386
Johannes Berg4c476992010-10-04 21:36:35 +02004387 if (!rdev->ops->assoc)
4388 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004389
Johannes Berg074ac8d2010-09-16 14:58:22 +02004390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004391 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4392 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004393
Johannes Berg19957bb2009-07-02 17:20:43 +02004394 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004395
Johannes Berg19957bb2009-07-02 17:20:43 +02004396 chan = ieee80211_get_channel(&rdev->wiphy,
4397 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004398 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4399 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004400
Johannes Berg19957bb2009-07-02 17:20:43 +02004401 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4402 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004403
4404 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004405 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4406 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004407 }
4408
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004409 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004410 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004411 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004412 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004413 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004414 else if (mfp != NL80211_MFP_NO)
4415 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004416 }
4417
Johannes Berg3e5d7642009-07-07 14:37:26 +02004418 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4419 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4420
Johannes Bergc0692b82010-08-27 14:26:53 +03004421 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004422 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004423 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4424 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004425 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004426
Jouni Malinen636a5d32009-03-19 13:39:22 +02004427 return err;
4428}
4429
4430static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4431{
Johannes Berg4c476992010-10-04 21:36:35 +02004432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4433 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004434 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004435 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004436 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004437 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004438
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004439 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4440 return -EINVAL;
4441
4442 if (!info->attrs[NL80211_ATTR_MAC])
4443 return -EINVAL;
4444
4445 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4446 return -EINVAL;
4447
Johannes Berg4c476992010-10-04 21:36:35 +02004448 if (!rdev->ops->deauth)
4449 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004450
Johannes Berg074ac8d2010-09-16 14:58:22 +02004451 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004452 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4453 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004454
Johannes Berg19957bb2009-07-02 17:20:43 +02004455 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004456
Johannes Berg19957bb2009-07-02 17:20:43 +02004457 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4458 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004459 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004460 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004461 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004462
4463 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004464 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4465 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004466 }
4467
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004468 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4469
Johannes Berg4c476992010-10-04 21:36:35 +02004470 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4471 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004472}
4473
4474static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4475{
Johannes Berg4c476992010-10-04 21:36:35 +02004476 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4477 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004478 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004479 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004480 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004481 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004482
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004483 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4484 return -EINVAL;
4485
4486 if (!info->attrs[NL80211_ATTR_MAC])
4487 return -EINVAL;
4488
4489 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4490 return -EINVAL;
4491
Johannes Berg4c476992010-10-04 21:36:35 +02004492 if (!rdev->ops->disassoc)
4493 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004494
Johannes Berg074ac8d2010-09-16 14:58:22 +02004495 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004496 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4497 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004498
Johannes Berg19957bb2009-07-02 17:20:43 +02004499 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004500
Johannes Berg19957bb2009-07-02 17:20:43 +02004501 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4502 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004503 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004504 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004505 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004506
4507 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004508 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4509 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004510 }
4511
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004512 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4513
Johannes Berg4c476992010-10-04 21:36:35 +02004514 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4515 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004516}
4517
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004518static bool
4519nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4520 int mcast_rate[IEEE80211_NUM_BANDS],
4521 int rateval)
4522{
4523 struct wiphy *wiphy = &rdev->wiphy;
4524 bool found = false;
4525 int band, i;
4526
4527 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4528 struct ieee80211_supported_band *sband;
4529
4530 sband = wiphy->bands[band];
4531 if (!sband)
4532 continue;
4533
4534 for (i = 0; i < sband->n_bitrates; i++) {
4535 if (sband->bitrates[i].bitrate == rateval) {
4536 mcast_rate[band] = i + 1;
4537 found = true;
4538 break;
4539 }
4540 }
4541 }
4542
4543 return found;
4544}
4545
Johannes Berg04a773a2009-04-19 21:24:32 +02004546static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4547{
Johannes Berg4c476992010-10-04 21:36:35 +02004548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4549 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004550 struct cfg80211_ibss_params ibss;
4551 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004552 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004553 int err;
4554
Johannes Berg8e30bc52009-04-22 17:45:38 +02004555 memset(&ibss, 0, sizeof(ibss));
4556
Johannes Berg04a773a2009-04-19 21:24:32 +02004557 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4558 return -EINVAL;
4559
4560 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4561 !info->attrs[NL80211_ATTR_SSID] ||
4562 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4563 return -EINVAL;
4564
Johannes Berg8e30bc52009-04-22 17:45:38 +02004565 ibss.beacon_interval = 100;
4566
4567 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4568 ibss.beacon_interval =
4569 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4570 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4571 return -EINVAL;
4572 }
4573
Johannes Berg4c476992010-10-04 21:36:35 +02004574 if (!rdev->ops->join_ibss)
4575 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004576
Johannes Berg4c476992010-10-04 21:36:35 +02004577 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4578 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004579
Johannes Berg79c97e92009-07-07 03:56:12 +02004580 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004581
Johannes Berg39193492011-09-16 13:45:25 +02004582 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004583 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004584
4585 if (!is_valid_ether_addr(ibss.bssid))
4586 return -EINVAL;
4587 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004588 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4589 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4590
4591 if (info->attrs[NL80211_ATTR_IE]) {
4592 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4593 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4594 }
4595
4596 ibss.channel = ieee80211_get_channel(wiphy,
4597 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4598 if (!ibss.channel ||
4599 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004600 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4601 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004602
4603 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004604 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004605
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004606 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4607 u8 *rates =
4608 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4609 int n_rates =
4610 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4611 struct ieee80211_supported_band *sband =
4612 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004613 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004614
Johannes Berg34850ab2011-07-18 18:08:35 +02004615 err = ieee80211_get_ratemask(sband, rates, n_rates,
4616 &ibss.basic_rates);
4617 if (err)
4618 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004619 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004620
4621 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4622 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4623 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4624 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004625
Johannes Berg4c476992010-10-04 21:36:35 +02004626 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4627 connkeys = nl80211_parse_connkeys(rdev,
4628 info->attrs[NL80211_ATTR_KEYS]);
4629 if (IS_ERR(connkeys))
4630 return PTR_ERR(connkeys);
4631 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004632
Johannes Berg4c476992010-10-04 21:36:35 +02004633 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004634 if (err)
4635 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004636 return err;
4637}
4638
4639static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4640{
Johannes Berg4c476992010-10-04 21:36:35 +02004641 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4642 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004643
Johannes Berg4c476992010-10-04 21:36:35 +02004644 if (!rdev->ops->leave_ibss)
4645 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004646
Johannes Berg4c476992010-10-04 21:36:35 +02004647 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4648 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004649
Johannes Berg4c476992010-10-04 21:36:35 +02004650 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004651}
4652
Johannes Bergaff89a92009-07-01 21:26:51 +02004653#ifdef CONFIG_NL80211_TESTMODE
4654static struct genl_multicast_group nl80211_testmode_mcgrp = {
4655 .name = "testmode",
4656};
4657
4658static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4659{
Johannes Berg4c476992010-10-04 21:36:35 +02004660 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004661 int err;
4662
4663 if (!info->attrs[NL80211_ATTR_TESTDATA])
4664 return -EINVAL;
4665
Johannes Bergaff89a92009-07-01 21:26:51 +02004666 err = -EOPNOTSUPP;
4667 if (rdev->ops->testmode_cmd) {
4668 rdev->testmode_info = info;
4669 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4670 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4671 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4672 rdev->testmode_info = NULL;
4673 }
4674
Johannes Bergaff89a92009-07-01 21:26:51 +02004675 return err;
4676}
4677
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004678static int nl80211_testmode_dump(struct sk_buff *skb,
4679 struct netlink_callback *cb)
4680{
4681 struct cfg80211_registered_device *dev;
4682 int err;
4683 long phy_idx;
4684 void *data = NULL;
4685 int data_len = 0;
4686
4687 if (cb->args[0]) {
4688 /*
4689 * 0 is a valid index, but not valid for args[0],
4690 * so we need to offset by 1.
4691 */
4692 phy_idx = cb->args[0] - 1;
4693 } else {
4694 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4695 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4696 nl80211_policy);
4697 if (err)
4698 return err;
4699 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4700 return -EINVAL;
4701 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4702 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4703 cb->args[1] =
4704 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4705 }
4706
4707 if (cb->args[1]) {
4708 data = nla_data((void *)cb->args[1]);
4709 data_len = nla_len((void *)cb->args[1]);
4710 }
4711
4712 mutex_lock(&cfg80211_mutex);
4713 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4714 if (!dev) {
4715 mutex_unlock(&cfg80211_mutex);
4716 return -ENOENT;
4717 }
4718 cfg80211_lock_rdev(dev);
4719 mutex_unlock(&cfg80211_mutex);
4720
4721 if (!dev->ops->testmode_dump) {
4722 err = -EOPNOTSUPP;
4723 goto out_err;
4724 }
4725
4726 while (1) {
4727 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4728 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4729 NL80211_CMD_TESTMODE);
4730 struct nlattr *tmdata;
4731
4732 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4733 genlmsg_cancel(skb, hdr);
4734 break;
4735 }
4736
4737 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4738 if (!tmdata) {
4739 genlmsg_cancel(skb, hdr);
4740 break;
4741 }
4742 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4743 data, data_len);
4744 nla_nest_end(skb, tmdata);
4745
4746 if (err == -ENOBUFS || err == -ENOENT) {
4747 genlmsg_cancel(skb, hdr);
4748 break;
4749 } else if (err) {
4750 genlmsg_cancel(skb, hdr);
4751 goto out_err;
4752 }
4753
4754 genlmsg_end(skb, hdr);
4755 }
4756
4757 err = skb->len;
4758 /* see above */
4759 cb->args[0] = phy_idx + 1;
4760 out_err:
4761 cfg80211_unlock_rdev(dev);
4762 return err;
4763}
4764
Johannes Bergaff89a92009-07-01 21:26:51 +02004765static struct sk_buff *
4766__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4767 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4768{
4769 struct sk_buff *skb;
4770 void *hdr;
4771 struct nlattr *data;
4772
4773 skb = nlmsg_new(approxlen + 100, gfp);
4774 if (!skb)
4775 return NULL;
4776
4777 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4778 if (!hdr) {
4779 kfree_skb(skb);
4780 return NULL;
4781 }
4782
4783 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4784 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4785
4786 ((void **)skb->cb)[0] = rdev;
4787 ((void **)skb->cb)[1] = hdr;
4788 ((void **)skb->cb)[2] = data;
4789
4790 return skb;
4791
4792 nla_put_failure:
4793 kfree_skb(skb);
4794 return NULL;
4795}
4796
4797struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4798 int approxlen)
4799{
4800 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4801
4802 if (WARN_ON(!rdev->testmode_info))
4803 return NULL;
4804
4805 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4806 rdev->testmode_info->snd_pid,
4807 rdev->testmode_info->snd_seq,
4808 GFP_KERNEL);
4809}
4810EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4811
4812int cfg80211_testmode_reply(struct sk_buff *skb)
4813{
4814 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4815 void *hdr = ((void **)skb->cb)[1];
4816 struct nlattr *data = ((void **)skb->cb)[2];
4817
4818 if (WARN_ON(!rdev->testmode_info)) {
4819 kfree_skb(skb);
4820 return -EINVAL;
4821 }
4822
4823 nla_nest_end(skb, data);
4824 genlmsg_end(skb, hdr);
4825 return genlmsg_reply(skb, rdev->testmode_info);
4826}
4827EXPORT_SYMBOL(cfg80211_testmode_reply);
4828
4829struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4830 int approxlen, gfp_t gfp)
4831{
4832 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4833
4834 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4835}
4836EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4837
4838void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4839{
4840 void *hdr = ((void **)skb->cb)[1];
4841 struct nlattr *data = ((void **)skb->cb)[2];
4842
4843 nla_nest_end(skb, data);
4844 genlmsg_end(skb, hdr);
4845 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4846}
4847EXPORT_SYMBOL(cfg80211_testmode_event);
4848#endif
4849
Samuel Ortizb23aa672009-07-01 21:26:54 +02004850static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4851{
Johannes Berg4c476992010-10-04 21:36:35 +02004852 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4853 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004854 struct cfg80211_connect_params connect;
4855 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004856 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004857 int err;
4858
4859 memset(&connect, 0, sizeof(connect));
4860
4861 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4862 return -EINVAL;
4863
4864 if (!info->attrs[NL80211_ATTR_SSID] ||
4865 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4866 return -EINVAL;
4867
4868 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4869 connect.auth_type =
4870 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4871 if (!nl80211_valid_auth_type(connect.auth_type))
4872 return -EINVAL;
4873 } else
4874 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4875
4876 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4877
Johannes Bergc0692b82010-08-27 14:26:53 +03004878 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004879 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004880 if (err)
4881 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004882
Johannes Berg074ac8d2010-09-16 14:58:22 +02004883 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004884 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4885 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004886
Johannes Berg79c97e92009-07-07 03:56:12 +02004887 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004888
Samuel Ortizb23aa672009-07-01 21:26:54 +02004889 if (info->attrs[NL80211_ATTR_MAC])
4890 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4891 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4892 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4893
4894 if (info->attrs[NL80211_ATTR_IE]) {
4895 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4896 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4897 }
4898
4899 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4900 connect.channel =
4901 ieee80211_get_channel(wiphy,
4902 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4903 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004904 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4905 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004906 }
4907
Johannes Bergfffd0932009-07-08 14:22:54 +02004908 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4909 connkeys = nl80211_parse_connkeys(rdev,
4910 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004911 if (IS_ERR(connkeys))
4912 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004913 }
4914
4915 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004916 if (err)
4917 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004918 return err;
4919}
4920
4921static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4922{
Johannes Berg4c476992010-10-04 21:36:35 +02004923 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4924 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004925 u16 reason;
4926
4927 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4928 reason = WLAN_REASON_DEAUTH_LEAVING;
4929 else
4930 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4931
4932 if (reason == 0)
4933 return -EINVAL;
4934
Johannes Berg074ac8d2010-09-16 14:58:22 +02004935 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004936 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4937 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004938
Johannes Berg4c476992010-10-04 21:36:35 +02004939 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004940}
4941
Johannes Berg463d0182009-07-14 00:33:35 +02004942static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4943{
Johannes Berg4c476992010-10-04 21:36:35 +02004944 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004945 struct net *net;
4946 int err;
4947 u32 pid;
4948
4949 if (!info->attrs[NL80211_ATTR_PID])
4950 return -EINVAL;
4951
4952 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4953
Johannes Berg463d0182009-07-14 00:33:35 +02004954 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004955 if (IS_ERR(net))
4956 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004957
4958 err = 0;
4959
4960 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004961 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4962 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004963
Johannes Berg463d0182009-07-14 00:33:35 +02004964 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004965 return err;
4966}
4967
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004968static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4969{
Johannes Berg4c476992010-10-04 21:36:35 +02004970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004971 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4972 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004973 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004974 struct cfg80211_pmksa pmksa;
4975
4976 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4977
4978 if (!info->attrs[NL80211_ATTR_MAC])
4979 return -EINVAL;
4980
4981 if (!info->attrs[NL80211_ATTR_PMKID])
4982 return -EINVAL;
4983
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004984 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4985 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4986
Johannes Berg074ac8d2010-09-16 14:58:22 +02004987 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004988 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4989 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004990
4991 switch (info->genlhdr->cmd) {
4992 case NL80211_CMD_SET_PMKSA:
4993 rdev_ops = rdev->ops->set_pmksa;
4994 break;
4995 case NL80211_CMD_DEL_PMKSA:
4996 rdev_ops = rdev->ops->del_pmksa;
4997 break;
4998 default:
4999 WARN_ON(1);
5000 break;
5001 }
5002
Johannes Berg4c476992010-10-04 21:36:35 +02005003 if (!rdev_ops)
5004 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005005
Johannes Berg4c476992010-10-04 21:36:35 +02005006 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005007}
5008
5009static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5010{
Johannes Berg4c476992010-10-04 21:36:35 +02005011 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5012 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005013
Johannes Berg074ac8d2010-09-16 14:58:22 +02005014 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005015 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5016 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005017
Johannes Berg4c476992010-10-04 21:36:35 +02005018 if (!rdev->ops->flush_pmksa)
5019 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005020
Johannes Berg4c476992010-10-04 21:36:35 +02005021 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005022}
5023
Arik Nemtsov109086c2011-09-28 14:12:50 +03005024static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5025{
5026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5027 struct net_device *dev = info->user_ptr[1];
5028 u8 action_code, dialog_token;
5029 u16 status_code;
5030 u8 *peer;
5031
5032 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5033 !rdev->ops->tdls_mgmt)
5034 return -EOPNOTSUPP;
5035
5036 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5037 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5038 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5039 !info->attrs[NL80211_ATTR_IE] ||
5040 !info->attrs[NL80211_ATTR_MAC])
5041 return -EINVAL;
5042
5043 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5044 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5045 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5046 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5047
5048 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5049 dialog_token, status_code,
5050 nla_data(info->attrs[NL80211_ATTR_IE]),
5051 nla_len(info->attrs[NL80211_ATTR_IE]));
5052}
5053
5054static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5055{
5056 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5057 struct net_device *dev = info->user_ptr[1];
5058 enum nl80211_tdls_operation operation;
5059 u8 *peer;
5060
5061 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5062 !rdev->ops->tdls_oper)
5063 return -EOPNOTSUPP;
5064
5065 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5066 !info->attrs[NL80211_ATTR_MAC])
5067 return -EINVAL;
5068
5069 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5070 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5071
5072 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5073}
5074
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005075static int nl80211_remain_on_channel(struct sk_buff *skb,
5076 struct genl_info *info)
5077{
Johannes Berg4c476992010-10-04 21:36:35 +02005078 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5079 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005080 struct ieee80211_channel *chan;
5081 struct sk_buff *msg;
5082 void *hdr;
5083 u64 cookie;
5084 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5085 u32 freq, duration;
5086 int err;
5087
5088 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5089 !info->attrs[NL80211_ATTR_DURATION])
5090 return -EINVAL;
5091
5092 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5093
5094 /*
5095 * We should be on that channel for at least one jiffie,
5096 * and more than 5 seconds seems excessive.
5097 */
Johannes Berga2939112010-12-14 17:54:28 +01005098 if (!duration || !msecs_to_jiffies(duration) ||
5099 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005100 return -EINVAL;
5101
Johannes Berg4c476992010-10-04 21:36:35 +02005102 if (!rdev->ops->remain_on_channel)
5103 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005104
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005105 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5106 channel_type = nla_get_u32(
5107 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5108 if (channel_type != NL80211_CHAN_NO_HT &&
5109 channel_type != NL80211_CHAN_HT20 &&
5110 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005111 channel_type != NL80211_CHAN_HT40MINUS)
5112 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005113 }
5114
5115 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5116 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005117 if (chan == NULL)
5118 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005119
5120 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005121 if (!msg)
5122 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005123
5124 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5125 NL80211_CMD_REMAIN_ON_CHANNEL);
5126
5127 if (IS_ERR(hdr)) {
5128 err = PTR_ERR(hdr);
5129 goto free_msg;
5130 }
5131
5132 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5133 channel_type, duration, &cookie);
5134
5135 if (err)
5136 goto free_msg;
5137
5138 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5139
5140 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005141
5142 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005143
5144 nla_put_failure:
5145 err = -ENOBUFS;
5146 free_msg:
5147 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005148 return err;
5149}
5150
5151static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5152 struct genl_info *info)
5153{
Johannes Berg4c476992010-10-04 21:36:35 +02005154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5155 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005156 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005157
5158 if (!info->attrs[NL80211_ATTR_COOKIE])
5159 return -EINVAL;
5160
Johannes Berg4c476992010-10-04 21:36:35 +02005161 if (!rdev->ops->cancel_remain_on_channel)
5162 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005163
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005164 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5165
Johannes Berg4c476992010-10-04 21:36:35 +02005166 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005167}
5168
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005169static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5170 u8 *rates, u8 rates_len)
5171{
5172 u8 i;
5173 u32 mask = 0;
5174
5175 for (i = 0; i < rates_len; i++) {
5176 int rate = (rates[i] & 0x7f) * 5;
5177 int ridx;
5178 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5179 struct ieee80211_rate *srate =
5180 &sband->bitrates[ridx];
5181 if (rate == srate->bitrate) {
5182 mask |= 1 << ridx;
5183 break;
5184 }
5185 }
5186 if (ridx == sband->n_bitrates)
5187 return 0; /* rate not found */
5188 }
5189
5190 return mask;
5191}
5192
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005193static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005194 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5195 .len = NL80211_MAX_SUPP_RATES },
5196};
5197
5198static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5199 struct genl_info *info)
5200{
5201 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005202 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005203 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005204 int rem, i;
5205 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005206 struct nlattr *tx_rates;
5207 struct ieee80211_supported_band *sband;
5208
5209 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5210 return -EINVAL;
5211
Johannes Berg4c476992010-10-04 21:36:35 +02005212 if (!rdev->ops->set_bitrate_mask)
5213 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005214
5215 memset(&mask, 0, sizeof(mask));
5216 /* Default to all rates enabled */
5217 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5218 sband = rdev->wiphy.bands[i];
5219 mask.control[i].legacy =
5220 sband ? (1 << sband->n_bitrates) - 1 : 0;
5221 }
5222
5223 /*
5224 * The nested attribute uses enum nl80211_band as the index. This maps
5225 * directly to the enum ieee80211_band values used in cfg80211.
5226 */
5227 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5228 {
5229 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005230 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5231 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005232 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005233 if (sband == NULL)
5234 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005235 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5236 nla_len(tx_rates), nl80211_txattr_policy);
5237 if (tb[NL80211_TXRATE_LEGACY]) {
5238 mask.control[band].legacy = rateset_to_mask(
5239 sband,
5240 nla_data(tb[NL80211_TXRATE_LEGACY]),
5241 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005242 if (mask.control[band].legacy == 0)
5243 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005244 }
5245 }
5246
Johannes Berg4c476992010-10-04 21:36:35 +02005247 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005248}
5249
Johannes Berg2e161f72010-08-12 15:38:38 +02005250static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005251{
Johannes Berg4c476992010-10-04 21:36:35 +02005252 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5253 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005254 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005255
5256 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5257 return -EINVAL;
5258
Johannes Berg2e161f72010-08-12 15:38:38 +02005259 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5260 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005261
Johannes Berg9d38d852010-06-09 17:20:33 +02005262 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005263 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005264 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5265 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5266 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005267 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005268 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5269 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005270
5271 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005272 if (!rdev->ops->mgmt_tx)
5273 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005274
Johannes Berg4c476992010-10-04 21:36:35 +02005275 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005276 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005277 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5278 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005279}
5280
Johannes Berg2e161f72010-08-12 15:38:38 +02005281static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005282{
Johannes Berg4c476992010-10-04 21:36:35 +02005283 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5284 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005285 struct ieee80211_channel *chan;
5286 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005287 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005288 u32 freq;
5289 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005290 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005291 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005292 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005293 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005294 bool offchan, no_cck, dont_wait_for_ack;
5295
5296 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005297
5298 if (!info->attrs[NL80211_ATTR_FRAME] ||
5299 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5300 return -EINVAL;
5301
Johannes Berg4c476992010-10-04 21:36:35 +02005302 if (!rdev->ops->mgmt_tx)
5303 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005304
Johannes Berg9d38d852010-06-09 17:20:33 +02005305 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005306 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005307 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5308 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5309 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005310 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005311 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5312 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005313
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005314 if (info->attrs[NL80211_ATTR_DURATION]) {
5315 if (!rdev->ops->mgmt_tx_cancel_wait)
5316 return -EINVAL;
5317 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5318 }
5319
Jouni Malinen026331c2010-02-15 12:53:10 +02005320 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5321 channel_type = nla_get_u32(
5322 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5323 if (channel_type != NL80211_CHAN_NO_HT &&
5324 channel_type != NL80211_CHAN_HT20 &&
5325 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005326 channel_type != NL80211_CHAN_HT40MINUS)
5327 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005328 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005329 }
5330
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005331 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5332
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305333 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5334
Jouni Malinen026331c2010-02-15 12:53:10 +02005335 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5336 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005337 if (chan == NULL)
5338 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005339
Johannes Berge247bd902011-11-04 11:18:21 +01005340 if (!dont_wait_for_ack) {
5341 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5342 if (!msg)
5343 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005344
Johannes Berge247bd902011-11-04 11:18:21 +01005345 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5346 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005347
Johannes Berge247bd902011-11-04 11:18:21 +01005348 if (IS_ERR(hdr)) {
5349 err = PTR_ERR(hdr);
5350 goto free_msg;
5351 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005352 }
Johannes Berge247bd902011-11-04 11:18:21 +01005353
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005354 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5355 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005356 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5357 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005358 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005359 if (err)
5360 goto free_msg;
5361
Johannes Berge247bd902011-11-04 11:18:21 +01005362 if (msg) {
5363 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005364
Johannes Berge247bd902011-11-04 11:18:21 +01005365 genlmsg_end(msg, hdr);
5366 return genlmsg_reply(msg, info);
5367 }
5368
5369 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005370
5371 nla_put_failure:
5372 err = -ENOBUFS;
5373 free_msg:
5374 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005375 return err;
5376}
5377
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005378static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5379{
5380 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5381 struct net_device *dev = info->user_ptr[1];
5382 u64 cookie;
5383
5384 if (!info->attrs[NL80211_ATTR_COOKIE])
5385 return -EINVAL;
5386
5387 if (!rdev->ops->mgmt_tx_cancel_wait)
5388 return -EOPNOTSUPP;
5389
5390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5391 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5392 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5393 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5394 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5395 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5396 return -EOPNOTSUPP;
5397
5398 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5399
5400 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5401}
5402
Kalle Valoffb9eb32010-02-17 17:58:10 +02005403static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5404{
Johannes Berg4c476992010-10-04 21:36:35 +02005405 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005406 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005407 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005408 u8 ps_state;
5409 bool state;
5410 int err;
5411
Johannes Berg4c476992010-10-04 21:36:35 +02005412 if (!info->attrs[NL80211_ATTR_PS_STATE])
5413 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005414
5415 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5416
Johannes Berg4c476992010-10-04 21:36:35 +02005417 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5418 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005419
5420 wdev = dev->ieee80211_ptr;
5421
Johannes Berg4c476992010-10-04 21:36:35 +02005422 if (!rdev->ops->set_power_mgmt)
5423 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005424
5425 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5426
5427 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005428 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005429
Johannes Berg4c476992010-10-04 21:36:35 +02005430 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5431 wdev->ps_timeout);
5432 if (!err)
5433 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005434 return err;
5435}
5436
5437static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5438{
Johannes Berg4c476992010-10-04 21:36:35 +02005439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005440 enum nl80211_ps_state ps_state;
5441 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005442 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005443 struct sk_buff *msg;
5444 void *hdr;
5445 int err;
5446
Kalle Valoffb9eb32010-02-17 17:58:10 +02005447 wdev = dev->ieee80211_ptr;
5448
Johannes Berg4c476992010-10-04 21:36:35 +02005449 if (!rdev->ops->set_power_mgmt)
5450 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005451
5452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005453 if (!msg)
5454 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005455
5456 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5457 NL80211_CMD_GET_POWER_SAVE);
5458 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005459 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005460 goto free_msg;
5461 }
5462
5463 if (wdev->ps)
5464 ps_state = NL80211_PS_ENABLED;
5465 else
5466 ps_state = NL80211_PS_DISABLED;
5467
5468 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5469
5470 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005471 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005472
Johannes Berg4c476992010-10-04 21:36:35 +02005473 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005474 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005475 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005476 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005477 return err;
5478}
5479
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005480static struct nla_policy
5481nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5482 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5483 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5484 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5485};
5486
5487static int nl80211_set_cqm_rssi(struct genl_info *info,
5488 s32 threshold, u32 hysteresis)
5489{
Johannes Berg4c476992010-10-04 21:36:35 +02005490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005491 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005492 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005493
5494 if (threshold > 0)
5495 return -EINVAL;
5496
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005497 wdev = dev->ieee80211_ptr;
5498
Johannes Berg4c476992010-10-04 21:36:35 +02005499 if (!rdev->ops->set_cqm_rssi_config)
5500 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005501
Johannes Berg074ac8d2010-09-16 14:58:22 +02005502 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005503 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5504 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005505
Johannes Berg4c476992010-10-04 21:36:35 +02005506 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5507 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005508}
5509
5510static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5511{
5512 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5513 struct nlattr *cqm;
5514 int err;
5515
5516 cqm = info->attrs[NL80211_ATTR_CQM];
5517 if (!cqm) {
5518 err = -EINVAL;
5519 goto out;
5520 }
5521
5522 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5523 nl80211_attr_cqm_policy);
5524 if (err)
5525 goto out;
5526
5527 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5528 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5529 s32 threshold;
5530 u32 hysteresis;
5531 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5532 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5533 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5534 } else
5535 err = -EINVAL;
5536
5537out:
5538 return err;
5539}
5540
Johannes Berg29cbe682010-12-03 09:20:44 +01005541static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5542{
5543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5544 struct net_device *dev = info->user_ptr[1];
5545 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005546 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005547 int err;
5548
5549 /* start with default */
5550 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005551 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005552
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005553 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005554 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005555 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005556 if (err)
5557 return err;
5558 }
5559
5560 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5561 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5562 return -EINVAL;
5563
Javier Cardonac80d5452010-12-16 17:37:49 -08005564 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5565 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5566
5567 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5568 /* parse additional setup parameters if given */
5569 err = nl80211_parse_mesh_setup(info, &setup);
5570 if (err)
5571 return err;
5572 }
5573
5574 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005575}
5576
5577static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5578{
5579 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5580 struct net_device *dev = info->user_ptr[1];
5581
5582 return cfg80211_leave_mesh(rdev, dev);
5583}
5584
Johannes Bergff1b6e62011-05-04 15:37:28 +02005585static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5586{
5587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5588 struct sk_buff *msg;
5589 void *hdr;
5590
5591 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5592 return -EOPNOTSUPP;
5593
5594 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5595 if (!msg)
5596 return -ENOMEM;
5597
5598 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5599 NL80211_CMD_GET_WOWLAN);
5600 if (!hdr)
5601 goto nla_put_failure;
5602
5603 if (rdev->wowlan) {
5604 struct nlattr *nl_wowlan;
5605
5606 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5607 if (!nl_wowlan)
5608 goto nla_put_failure;
5609
5610 if (rdev->wowlan->any)
5611 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5612 if (rdev->wowlan->disconnect)
5613 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5614 if (rdev->wowlan->magic_pkt)
5615 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005616 if (rdev->wowlan->gtk_rekey_failure)
5617 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5618 if (rdev->wowlan->eap_identity_req)
5619 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5620 if (rdev->wowlan->four_way_handshake)
5621 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5622 if (rdev->wowlan->rfkill_release)
5623 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005624 if (rdev->wowlan->n_patterns) {
5625 struct nlattr *nl_pats, *nl_pat;
5626 int i, pat_len;
5627
5628 nl_pats = nla_nest_start(msg,
5629 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5630 if (!nl_pats)
5631 goto nla_put_failure;
5632
5633 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5634 nl_pat = nla_nest_start(msg, i + 1);
5635 if (!nl_pat)
5636 goto nla_put_failure;
5637 pat_len = rdev->wowlan->patterns[i].pattern_len;
5638 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5639 DIV_ROUND_UP(pat_len, 8),
5640 rdev->wowlan->patterns[i].mask);
5641 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5642 pat_len,
5643 rdev->wowlan->patterns[i].pattern);
5644 nla_nest_end(msg, nl_pat);
5645 }
5646 nla_nest_end(msg, nl_pats);
5647 }
5648
5649 nla_nest_end(msg, nl_wowlan);
5650 }
5651
5652 genlmsg_end(msg, hdr);
5653 return genlmsg_reply(msg, info);
5654
5655nla_put_failure:
5656 nlmsg_free(msg);
5657 return -ENOBUFS;
5658}
5659
5660static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5661{
5662 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5663 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5664 struct cfg80211_wowlan no_triggers = {};
5665 struct cfg80211_wowlan new_triggers = {};
5666 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5667 int err, i;
5668
5669 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5670 return -EOPNOTSUPP;
5671
5672 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5673 goto no_triggers;
5674
5675 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5676 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5677 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5678 nl80211_wowlan_policy);
5679 if (err)
5680 return err;
5681
5682 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5683 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5684 return -EINVAL;
5685 new_triggers.any = true;
5686 }
5687
5688 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5689 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5690 return -EINVAL;
5691 new_triggers.disconnect = true;
5692 }
5693
5694 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5695 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5696 return -EINVAL;
5697 new_triggers.magic_pkt = true;
5698 }
5699
Johannes Berg77dbbb12011-07-13 10:48:55 +02005700 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5701 return -EINVAL;
5702
5703 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5704 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5705 return -EINVAL;
5706 new_triggers.gtk_rekey_failure = true;
5707 }
5708
5709 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5710 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5711 return -EINVAL;
5712 new_triggers.eap_identity_req = true;
5713 }
5714
5715 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5716 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5717 return -EINVAL;
5718 new_triggers.four_way_handshake = true;
5719 }
5720
5721 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5722 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5723 return -EINVAL;
5724 new_triggers.rfkill_release = true;
5725 }
5726
Johannes Bergff1b6e62011-05-04 15:37:28 +02005727 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5728 struct nlattr *pat;
5729 int n_patterns = 0;
5730 int rem, pat_len, mask_len;
5731 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5732
5733 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5734 rem)
5735 n_patterns++;
5736 if (n_patterns > wowlan->n_patterns)
5737 return -EINVAL;
5738
5739 new_triggers.patterns = kcalloc(n_patterns,
5740 sizeof(new_triggers.patterns[0]),
5741 GFP_KERNEL);
5742 if (!new_triggers.patterns)
5743 return -ENOMEM;
5744
5745 new_triggers.n_patterns = n_patterns;
5746 i = 0;
5747
5748 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5749 rem) {
5750 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5751 nla_data(pat), nla_len(pat), NULL);
5752 err = -EINVAL;
5753 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5754 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5755 goto error;
5756 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5757 mask_len = DIV_ROUND_UP(pat_len, 8);
5758 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5759 mask_len)
5760 goto error;
5761 if (pat_len > wowlan->pattern_max_len ||
5762 pat_len < wowlan->pattern_min_len)
5763 goto error;
5764
5765 new_triggers.patterns[i].mask =
5766 kmalloc(mask_len + pat_len, GFP_KERNEL);
5767 if (!new_triggers.patterns[i].mask) {
5768 err = -ENOMEM;
5769 goto error;
5770 }
5771 new_triggers.patterns[i].pattern =
5772 new_triggers.patterns[i].mask + mask_len;
5773 memcpy(new_triggers.patterns[i].mask,
5774 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5775 mask_len);
5776 new_triggers.patterns[i].pattern_len = pat_len;
5777 memcpy(new_triggers.patterns[i].pattern,
5778 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5779 pat_len);
5780 i++;
5781 }
5782 }
5783
5784 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5785 struct cfg80211_wowlan *ntrig;
5786 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5787 GFP_KERNEL);
5788 if (!ntrig) {
5789 err = -ENOMEM;
5790 goto error;
5791 }
5792 cfg80211_rdev_free_wowlan(rdev);
5793 rdev->wowlan = ntrig;
5794 } else {
5795 no_triggers:
5796 cfg80211_rdev_free_wowlan(rdev);
5797 rdev->wowlan = NULL;
5798 }
5799
5800 return 0;
5801 error:
5802 for (i = 0; i < new_triggers.n_patterns; i++)
5803 kfree(new_triggers.patterns[i].mask);
5804 kfree(new_triggers.patterns);
5805 return err;
5806}
5807
Johannes Berge5497d72011-07-05 16:35:40 +02005808static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5809{
5810 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5811 struct net_device *dev = info->user_ptr[1];
5812 struct wireless_dev *wdev = dev->ieee80211_ptr;
5813 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5814 struct cfg80211_gtk_rekey_data rekey_data;
5815 int err;
5816
5817 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5818 return -EINVAL;
5819
5820 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5821 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5822 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5823 nl80211_rekey_policy);
5824 if (err)
5825 return err;
5826
5827 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5828 return -ERANGE;
5829 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5830 return -ERANGE;
5831 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5832 return -ERANGE;
5833
5834 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5835 NL80211_KEK_LEN);
5836 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5837 NL80211_KCK_LEN);
5838 memcpy(rekey_data.replay_ctr,
5839 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5840 NL80211_REPLAY_CTR_LEN);
5841
5842 wdev_lock(wdev);
5843 if (!wdev->current_bss) {
5844 err = -ENOTCONN;
5845 goto out;
5846 }
5847
5848 if (!rdev->ops->set_rekey_data) {
5849 err = -EOPNOTSUPP;
5850 goto out;
5851 }
5852
5853 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5854 out:
5855 wdev_unlock(wdev);
5856 return err;
5857}
5858
Johannes Berg28946da2011-11-04 11:18:12 +01005859static int nl80211_register_unexpected_frame(struct sk_buff *skb,
5860 struct genl_info *info)
5861{
5862 struct net_device *dev = info->user_ptr[1];
5863 struct wireless_dev *wdev = dev->ieee80211_ptr;
5864
5865 if (wdev->iftype != NL80211_IFTYPE_AP &&
5866 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5867 return -EINVAL;
5868
5869 if (wdev->ap_unexpected_nlpid)
5870 return -EBUSY;
5871
5872 wdev->ap_unexpected_nlpid = info->snd_pid;
5873 return 0;
5874}
5875
Johannes Berg7f6cf312011-11-04 11:18:15 +01005876static int nl80211_probe_client(struct sk_buff *skb,
5877 struct genl_info *info)
5878{
5879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5880 struct net_device *dev = info->user_ptr[1];
5881 struct wireless_dev *wdev = dev->ieee80211_ptr;
5882 struct sk_buff *msg;
5883 void *hdr;
5884 const u8 *addr;
5885 u64 cookie;
5886 int err;
5887
5888 if (wdev->iftype != NL80211_IFTYPE_AP &&
5889 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5890 return -EOPNOTSUPP;
5891
5892 if (!info->attrs[NL80211_ATTR_MAC])
5893 return -EINVAL;
5894
5895 if (!rdev->ops->probe_client)
5896 return -EOPNOTSUPP;
5897
5898 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5899 if (!msg)
5900 return -ENOMEM;
5901
5902 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5903 NL80211_CMD_PROBE_CLIENT);
5904
5905 if (IS_ERR(hdr)) {
5906 err = PTR_ERR(hdr);
5907 goto free_msg;
5908 }
5909
5910 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5911
5912 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
5913 if (err)
5914 goto free_msg;
5915
5916 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5917
5918 genlmsg_end(msg, hdr);
5919
5920 return genlmsg_reply(msg, info);
5921
5922 nla_put_failure:
5923 err = -ENOBUFS;
5924 free_msg:
5925 nlmsg_free(msg);
5926 return err;
5927}
5928
Johannes Berg5e760232011-11-04 11:18:17 +01005929static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
5930{
5931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5932
5933 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
5934 return -EOPNOTSUPP;
5935
5936 if (rdev->ap_beacons_nlpid)
5937 return -EBUSY;
5938
5939 rdev->ap_beacons_nlpid = info->snd_pid;
5940
5941 return 0;
5942}
5943
Johannes Berg4c476992010-10-04 21:36:35 +02005944#define NL80211_FLAG_NEED_WIPHY 0x01
5945#define NL80211_FLAG_NEED_NETDEV 0x02
5946#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005947#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5948#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5949 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005950
5951static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5952 struct genl_info *info)
5953{
5954 struct cfg80211_registered_device *rdev;
5955 struct net_device *dev;
5956 int err;
5957 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5958
5959 if (rtnl)
5960 rtnl_lock();
5961
5962 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5963 rdev = cfg80211_get_dev_from_info(info);
5964 if (IS_ERR(rdev)) {
5965 if (rtnl)
5966 rtnl_unlock();
5967 return PTR_ERR(rdev);
5968 }
5969 info->user_ptr[0] = rdev;
5970 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5971 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5972 if (err) {
5973 if (rtnl)
5974 rtnl_unlock();
5975 return err;
5976 }
Johannes Berg41265712010-10-04 21:14:05 +02005977 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5978 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005979 cfg80211_unlock_rdev(rdev);
5980 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005981 if (rtnl)
5982 rtnl_unlock();
5983 return -ENETDOWN;
5984 }
Johannes Berg4c476992010-10-04 21:36:35 +02005985 info->user_ptr[0] = rdev;
5986 info->user_ptr[1] = dev;
5987 }
5988
5989 return 0;
5990}
5991
5992static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5993 struct genl_info *info)
5994{
5995 if (info->user_ptr[0])
5996 cfg80211_unlock_rdev(info->user_ptr[0]);
5997 if (info->user_ptr[1])
5998 dev_put(info->user_ptr[1]);
5999 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6000 rtnl_unlock();
6001}
6002
Johannes Berg55682962007-09-20 13:09:35 -04006003static struct genl_ops nl80211_ops[] = {
6004 {
6005 .cmd = NL80211_CMD_GET_WIPHY,
6006 .doit = nl80211_get_wiphy,
6007 .dumpit = nl80211_dump_wiphy,
6008 .policy = nl80211_policy,
6009 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006010 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006011 },
6012 {
6013 .cmd = NL80211_CMD_SET_WIPHY,
6014 .doit = nl80211_set_wiphy,
6015 .policy = nl80211_policy,
6016 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006017 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006018 },
6019 {
6020 .cmd = NL80211_CMD_GET_INTERFACE,
6021 .doit = nl80211_get_interface,
6022 .dumpit = nl80211_dump_interface,
6023 .policy = nl80211_policy,
6024 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006025 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006026 },
6027 {
6028 .cmd = NL80211_CMD_SET_INTERFACE,
6029 .doit = nl80211_set_interface,
6030 .policy = nl80211_policy,
6031 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006032 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6033 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006034 },
6035 {
6036 .cmd = NL80211_CMD_NEW_INTERFACE,
6037 .doit = nl80211_new_interface,
6038 .policy = nl80211_policy,
6039 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006040 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6041 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006042 },
6043 {
6044 .cmd = NL80211_CMD_DEL_INTERFACE,
6045 .doit = nl80211_del_interface,
6046 .policy = nl80211_policy,
6047 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006048 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6049 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006050 },
Johannes Berg41ade002007-12-19 02:03:29 +01006051 {
6052 .cmd = NL80211_CMD_GET_KEY,
6053 .doit = nl80211_get_key,
6054 .policy = nl80211_policy,
6055 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006056 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6057 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006058 },
6059 {
6060 .cmd = NL80211_CMD_SET_KEY,
6061 .doit = nl80211_set_key,
6062 .policy = nl80211_policy,
6063 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006064 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006065 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006066 },
6067 {
6068 .cmd = NL80211_CMD_NEW_KEY,
6069 .doit = nl80211_new_key,
6070 .policy = nl80211_policy,
6071 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006072 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006073 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006074 },
6075 {
6076 .cmd = NL80211_CMD_DEL_KEY,
6077 .doit = nl80211_del_key,
6078 .policy = nl80211_policy,
6079 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006080 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006081 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006082 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006083 {
6084 .cmd = NL80211_CMD_SET_BEACON,
6085 .policy = nl80211_policy,
6086 .flags = GENL_ADMIN_PERM,
6087 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006088 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6089 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006090 },
6091 {
6092 .cmd = NL80211_CMD_NEW_BEACON,
6093 .policy = nl80211_policy,
6094 .flags = GENL_ADMIN_PERM,
6095 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006096 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6097 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006098 },
6099 {
6100 .cmd = NL80211_CMD_DEL_BEACON,
6101 .policy = nl80211_policy,
6102 .flags = GENL_ADMIN_PERM,
6103 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006104 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6105 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006106 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006107 {
6108 .cmd = NL80211_CMD_GET_STATION,
6109 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006110 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006111 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006112 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6113 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006114 },
6115 {
6116 .cmd = NL80211_CMD_SET_STATION,
6117 .doit = nl80211_set_station,
6118 .policy = nl80211_policy,
6119 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006120 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6121 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006122 },
6123 {
6124 .cmd = NL80211_CMD_NEW_STATION,
6125 .doit = nl80211_new_station,
6126 .policy = nl80211_policy,
6127 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006128 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006129 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006130 },
6131 {
6132 .cmd = NL80211_CMD_DEL_STATION,
6133 .doit = nl80211_del_station,
6134 .policy = nl80211_policy,
6135 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006136 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6137 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006138 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006139 {
6140 .cmd = NL80211_CMD_GET_MPATH,
6141 .doit = nl80211_get_mpath,
6142 .dumpit = nl80211_dump_mpath,
6143 .policy = nl80211_policy,
6144 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006145 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006146 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006147 },
6148 {
6149 .cmd = NL80211_CMD_SET_MPATH,
6150 .doit = nl80211_set_mpath,
6151 .policy = nl80211_policy,
6152 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006153 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006154 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006155 },
6156 {
6157 .cmd = NL80211_CMD_NEW_MPATH,
6158 .doit = nl80211_new_mpath,
6159 .policy = nl80211_policy,
6160 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006161 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006162 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006163 },
6164 {
6165 .cmd = NL80211_CMD_DEL_MPATH,
6166 .doit = nl80211_del_mpath,
6167 .policy = nl80211_policy,
6168 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006169 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6170 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006171 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006172 {
6173 .cmd = NL80211_CMD_SET_BSS,
6174 .doit = nl80211_set_bss,
6175 .policy = nl80211_policy,
6176 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006177 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6178 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006179 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006180 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006181 .cmd = NL80211_CMD_GET_REG,
6182 .doit = nl80211_get_reg,
6183 .policy = nl80211_policy,
6184 /* can be retrieved by unprivileged users */
6185 },
6186 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006187 .cmd = NL80211_CMD_SET_REG,
6188 .doit = nl80211_set_reg,
6189 .policy = nl80211_policy,
6190 .flags = GENL_ADMIN_PERM,
6191 },
6192 {
6193 .cmd = NL80211_CMD_REQ_SET_REG,
6194 .doit = nl80211_req_set_reg,
6195 .policy = nl80211_policy,
6196 .flags = GENL_ADMIN_PERM,
6197 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006198 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006199 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6200 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006201 .policy = nl80211_policy,
6202 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006203 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6204 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006205 },
6206 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006207 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6208 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006209 .policy = nl80211_policy,
6210 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006211 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006212 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006213 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006214 {
Johannes Berg2a519312009-02-10 21:25:55 +01006215 .cmd = NL80211_CMD_TRIGGER_SCAN,
6216 .doit = nl80211_trigger_scan,
6217 .policy = nl80211_policy,
6218 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006219 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006220 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006221 },
6222 {
6223 .cmd = NL80211_CMD_GET_SCAN,
6224 .policy = nl80211_policy,
6225 .dumpit = nl80211_dump_scan,
6226 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006227 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006228 .cmd = NL80211_CMD_START_SCHED_SCAN,
6229 .doit = nl80211_start_sched_scan,
6230 .policy = nl80211_policy,
6231 .flags = GENL_ADMIN_PERM,
6232 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6233 NL80211_FLAG_NEED_RTNL,
6234 },
6235 {
6236 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6237 .doit = nl80211_stop_sched_scan,
6238 .policy = nl80211_policy,
6239 .flags = GENL_ADMIN_PERM,
6240 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6241 NL80211_FLAG_NEED_RTNL,
6242 },
6243 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006244 .cmd = NL80211_CMD_AUTHENTICATE,
6245 .doit = nl80211_authenticate,
6246 .policy = nl80211_policy,
6247 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006248 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006249 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006250 },
6251 {
6252 .cmd = NL80211_CMD_ASSOCIATE,
6253 .doit = nl80211_associate,
6254 .policy = nl80211_policy,
6255 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006256 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006257 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006258 },
6259 {
6260 .cmd = NL80211_CMD_DEAUTHENTICATE,
6261 .doit = nl80211_deauthenticate,
6262 .policy = nl80211_policy,
6263 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006264 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006265 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006266 },
6267 {
6268 .cmd = NL80211_CMD_DISASSOCIATE,
6269 .doit = nl80211_disassociate,
6270 .policy = nl80211_policy,
6271 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006272 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006273 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006274 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006275 {
6276 .cmd = NL80211_CMD_JOIN_IBSS,
6277 .doit = nl80211_join_ibss,
6278 .policy = nl80211_policy,
6279 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006280 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006281 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006282 },
6283 {
6284 .cmd = NL80211_CMD_LEAVE_IBSS,
6285 .doit = nl80211_leave_ibss,
6286 .policy = nl80211_policy,
6287 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006288 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006289 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006290 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006291#ifdef CONFIG_NL80211_TESTMODE
6292 {
6293 .cmd = NL80211_CMD_TESTMODE,
6294 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006295 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006296 .policy = nl80211_policy,
6297 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006298 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6299 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006300 },
6301#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006302 {
6303 .cmd = NL80211_CMD_CONNECT,
6304 .doit = nl80211_connect,
6305 .policy = nl80211_policy,
6306 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006307 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006308 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006309 },
6310 {
6311 .cmd = NL80211_CMD_DISCONNECT,
6312 .doit = nl80211_disconnect,
6313 .policy = nl80211_policy,
6314 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006315 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006316 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006317 },
Johannes Berg463d0182009-07-14 00:33:35 +02006318 {
6319 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6320 .doit = nl80211_wiphy_netns,
6321 .policy = nl80211_policy,
6322 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006323 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6324 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006325 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006326 {
6327 .cmd = NL80211_CMD_GET_SURVEY,
6328 .policy = nl80211_policy,
6329 .dumpit = nl80211_dump_survey,
6330 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006331 {
6332 .cmd = NL80211_CMD_SET_PMKSA,
6333 .doit = nl80211_setdel_pmksa,
6334 .policy = nl80211_policy,
6335 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006336 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6337 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006338 },
6339 {
6340 .cmd = NL80211_CMD_DEL_PMKSA,
6341 .doit = nl80211_setdel_pmksa,
6342 .policy = nl80211_policy,
6343 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006344 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6345 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006346 },
6347 {
6348 .cmd = NL80211_CMD_FLUSH_PMKSA,
6349 .doit = nl80211_flush_pmksa,
6350 .policy = nl80211_policy,
6351 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006352 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6353 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006354 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006355 {
6356 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6357 .doit = nl80211_remain_on_channel,
6358 .policy = nl80211_policy,
6359 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006360 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006361 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006362 },
6363 {
6364 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6365 .doit = nl80211_cancel_remain_on_channel,
6366 .policy = nl80211_policy,
6367 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006368 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006369 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006370 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006371 {
6372 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6373 .doit = nl80211_set_tx_bitrate_mask,
6374 .policy = nl80211_policy,
6375 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006376 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6377 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006378 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006379 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006380 .cmd = NL80211_CMD_REGISTER_FRAME,
6381 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006382 .policy = nl80211_policy,
6383 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006384 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6385 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006386 },
6387 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006388 .cmd = NL80211_CMD_FRAME,
6389 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006390 .policy = nl80211_policy,
6391 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006392 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006393 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006394 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006395 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006396 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6397 .doit = nl80211_tx_mgmt_cancel_wait,
6398 .policy = nl80211_policy,
6399 .flags = GENL_ADMIN_PERM,
6400 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6401 NL80211_FLAG_NEED_RTNL,
6402 },
6403 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006404 .cmd = NL80211_CMD_SET_POWER_SAVE,
6405 .doit = nl80211_set_power_save,
6406 .policy = nl80211_policy,
6407 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006408 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6409 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006410 },
6411 {
6412 .cmd = NL80211_CMD_GET_POWER_SAVE,
6413 .doit = nl80211_get_power_save,
6414 .policy = nl80211_policy,
6415 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006416 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6417 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006418 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006419 {
6420 .cmd = NL80211_CMD_SET_CQM,
6421 .doit = nl80211_set_cqm,
6422 .policy = nl80211_policy,
6423 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006424 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6425 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006426 },
Johannes Bergf444de02010-05-05 15:25:02 +02006427 {
6428 .cmd = NL80211_CMD_SET_CHANNEL,
6429 .doit = nl80211_set_channel,
6430 .policy = nl80211_policy,
6431 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006432 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6433 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006434 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006435 {
6436 .cmd = NL80211_CMD_SET_WDS_PEER,
6437 .doit = nl80211_set_wds_peer,
6438 .policy = nl80211_policy,
6439 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006440 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6441 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006442 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006443 {
6444 .cmd = NL80211_CMD_JOIN_MESH,
6445 .doit = nl80211_join_mesh,
6446 .policy = nl80211_policy,
6447 .flags = GENL_ADMIN_PERM,
6448 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6449 NL80211_FLAG_NEED_RTNL,
6450 },
6451 {
6452 .cmd = NL80211_CMD_LEAVE_MESH,
6453 .doit = nl80211_leave_mesh,
6454 .policy = nl80211_policy,
6455 .flags = GENL_ADMIN_PERM,
6456 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6457 NL80211_FLAG_NEED_RTNL,
6458 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006459 {
6460 .cmd = NL80211_CMD_GET_WOWLAN,
6461 .doit = nl80211_get_wowlan,
6462 .policy = nl80211_policy,
6463 /* can be retrieved by unprivileged users */
6464 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6465 NL80211_FLAG_NEED_RTNL,
6466 },
6467 {
6468 .cmd = NL80211_CMD_SET_WOWLAN,
6469 .doit = nl80211_set_wowlan,
6470 .policy = nl80211_policy,
6471 .flags = GENL_ADMIN_PERM,
6472 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6473 NL80211_FLAG_NEED_RTNL,
6474 },
Johannes Berge5497d72011-07-05 16:35:40 +02006475 {
6476 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6477 .doit = nl80211_set_rekey_data,
6478 .policy = nl80211_policy,
6479 .flags = GENL_ADMIN_PERM,
6480 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6481 NL80211_FLAG_NEED_RTNL,
6482 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006483 {
6484 .cmd = NL80211_CMD_TDLS_MGMT,
6485 .doit = nl80211_tdls_mgmt,
6486 .policy = nl80211_policy,
6487 .flags = GENL_ADMIN_PERM,
6488 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6489 NL80211_FLAG_NEED_RTNL,
6490 },
6491 {
6492 .cmd = NL80211_CMD_TDLS_OPER,
6493 .doit = nl80211_tdls_oper,
6494 .policy = nl80211_policy,
6495 .flags = GENL_ADMIN_PERM,
6496 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6497 NL80211_FLAG_NEED_RTNL,
6498 },
Johannes Berg28946da2011-11-04 11:18:12 +01006499 {
6500 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6501 .doit = nl80211_register_unexpected_frame,
6502 .policy = nl80211_policy,
6503 .flags = GENL_ADMIN_PERM,
6504 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6505 NL80211_FLAG_NEED_RTNL,
6506 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006507 {
6508 .cmd = NL80211_CMD_PROBE_CLIENT,
6509 .doit = nl80211_probe_client,
6510 .policy = nl80211_policy,
6511 .flags = GENL_ADMIN_PERM,
6512 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6513 NL80211_FLAG_NEED_RTNL,
6514 },
Johannes Berg5e760232011-11-04 11:18:17 +01006515 {
6516 .cmd = NL80211_CMD_REGISTER_BEACONS,
6517 .doit = nl80211_register_beacons,
6518 .policy = nl80211_policy,
6519 .flags = GENL_ADMIN_PERM,
6520 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6521 NL80211_FLAG_NEED_RTNL,
6522 },
Johannes Berg55682962007-09-20 13:09:35 -04006523};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006524
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006525static struct genl_multicast_group nl80211_mlme_mcgrp = {
6526 .name = "mlme",
6527};
Johannes Berg55682962007-09-20 13:09:35 -04006528
6529/* multicast groups */
6530static struct genl_multicast_group nl80211_config_mcgrp = {
6531 .name = "config",
6532};
Johannes Berg2a519312009-02-10 21:25:55 +01006533static struct genl_multicast_group nl80211_scan_mcgrp = {
6534 .name = "scan",
6535};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006536static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6537 .name = "regulatory",
6538};
Johannes Berg55682962007-09-20 13:09:35 -04006539
6540/* notification functions */
6541
6542void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6543{
6544 struct sk_buff *msg;
6545
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006547 if (!msg)
6548 return;
6549
6550 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6551 nlmsg_free(msg);
6552 return;
6553 }
6554
Johannes Berg463d0182009-07-14 00:33:35 +02006555 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6556 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006557}
6558
Johannes Berg362a4152009-05-24 16:43:15 +02006559static int nl80211_add_scan_req(struct sk_buff *msg,
6560 struct cfg80211_registered_device *rdev)
6561{
6562 struct cfg80211_scan_request *req = rdev->scan_req;
6563 struct nlattr *nest;
6564 int i;
6565
Johannes Berg667503d2009-07-07 03:56:11 +02006566 ASSERT_RDEV_LOCK(rdev);
6567
Johannes Berg362a4152009-05-24 16:43:15 +02006568 if (WARN_ON(!req))
6569 return 0;
6570
6571 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6572 if (!nest)
6573 goto nla_put_failure;
6574 for (i = 0; i < req->n_ssids; i++)
6575 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6576 nla_nest_end(msg, nest);
6577
6578 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6579 if (!nest)
6580 goto nla_put_failure;
6581 for (i = 0; i < req->n_channels; i++)
6582 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6583 nla_nest_end(msg, nest);
6584
6585 if (req->ie)
6586 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6587
6588 return 0;
6589 nla_put_failure:
6590 return -ENOBUFS;
6591}
6592
Johannes Berga538e2d2009-06-16 19:56:42 +02006593static int nl80211_send_scan_msg(struct sk_buff *msg,
6594 struct cfg80211_registered_device *rdev,
6595 struct net_device *netdev,
6596 u32 pid, u32 seq, int flags,
6597 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006598{
6599 void *hdr;
6600
6601 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6602 if (!hdr)
6603 return -1;
6604
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006605 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006606 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6607
Johannes Berg362a4152009-05-24 16:43:15 +02006608 /* ignore errors and send incomplete event anyway */
6609 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006610
6611 return genlmsg_end(msg, hdr);
6612
6613 nla_put_failure:
6614 genlmsg_cancel(msg, hdr);
6615 return -EMSGSIZE;
6616}
6617
Luciano Coelho807f8a82011-05-11 17:09:35 +03006618static int
6619nl80211_send_sched_scan_msg(struct sk_buff *msg,
6620 struct cfg80211_registered_device *rdev,
6621 struct net_device *netdev,
6622 u32 pid, u32 seq, int flags, u32 cmd)
6623{
6624 void *hdr;
6625
6626 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6627 if (!hdr)
6628 return -1;
6629
6630 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6631 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6632
6633 return genlmsg_end(msg, hdr);
6634
6635 nla_put_failure:
6636 genlmsg_cancel(msg, hdr);
6637 return -EMSGSIZE;
6638}
6639
Johannes Berga538e2d2009-06-16 19:56:42 +02006640void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6641 struct net_device *netdev)
6642{
6643 struct sk_buff *msg;
6644
6645 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6646 if (!msg)
6647 return;
6648
6649 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6650 NL80211_CMD_TRIGGER_SCAN) < 0) {
6651 nlmsg_free(msg);
6652 return;
6653 }
6654
Johannes Berg463d0182009-07-14 00:33:35 +02006655 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6656 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006657}
6658
Johannes Berg2a519312009-02-10 21:25:55 +01006659void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6660 struct net_device *netdev)
6661{
6662 struct sk_buff *msg;
6663
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006664 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006665 if (!msg)
6666 return;
6667
Johannes Berga538e2d2009-06-16 19:56:42 +02006668 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6669 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006670 nlmsg_free(msg);
6671 return;
6672 }
6673
Johannes Berg463d0182009-07-14 00:33:35 +02006674 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6675 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006676}
6677
6678void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6679 struct net_device *netdev)
6680{
6681 struct sk_buff *msg;
6682
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006684 if (!msg)
6685 return;
6686
Johannes Berga538e2d2009-06-16 19:56:42 +02006687 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6688 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006689 nlmsg_free(msg);
6690 return;
6691 }
6692
Johannes Berg463d0182009-07-14 00:33:35 +02006693 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6694 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006695}
6696
Luciano Coelho807f8a82011-05-11 17:09:35 +03006697void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6698 struct net_device *netdev)
6699{
6700 struct sk_buff *msg;
6701
6702 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6703 if (!msg)
6704 return;
6705
6706 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6707 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6708 nlmsg_free(msg);
6709 return;
6710 }
6711
6712 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6713 nl80211_scan_mcgrp.id, GFP_KERNEL);
6714}
6715
6716void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6717 struct net_device *netdev, u32 cmd)
6718{
6719 struct sk_buff *msg;
6720
6721 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6722 if (!msg)
6723 return;
6724
6725 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6726 nlmsg_free(msg);
6727 return;
6728 }
6729
6730 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6731 nl80211_scan_mcgrp.id, GFP_KERNEL);
6732}
6733
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006734/*
6735 * This can happen on global regulatory changes or device specific settings
6736 * based on custom world regulatory domains.
6737 */
6738void nl80211_send_reg_change_event(struct regulatory_request *request)
6739{
6740 struct sk_buff *msg;
6741 void *hdr;
6742
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006743 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006744 if (!msg)
6745 return;
6746
6747 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6748 if (!hdr) {
6749 nlmsg_free(msg);
6750 return;
6751 }
6752
6753 /* Userspace can always count this one always being set */
6754 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6755
6756 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6757 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6758 NL80211_REGDOM_TYPE_WORLD);
6759 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6760 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6761 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6762 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6763 request->intersect)
6764 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6765 NL80211_REGDOM_TYPE_INTERSECTION);
6766 else {
6767 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6768 NL80211_REGDOM_TYPE_COUNTRY);
6769 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6770 }
6771
6772 if (wiphy_idx_valid(request->wiphy_idx))
6773 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6774
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006775 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006776
Johannes Bergbc43b282009-07-25 10:54:13 +02006777 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006778 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006779 GFP_ATOMIC);
6780 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006781
6782 return;
6783
6784nla_put_failure:
6785 genlmsg_cancel(msg, hdr);
6786 nlmsg_free(msg);
6787}
6788
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006789static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6790 struct net_device *netdev,
6791 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006792 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006793{
6794 struct sk_buff *msg;
6795 void *hdr;
6796
Johannes Berge6d6e342009-07-01 21:26:47 +02006797 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006798 if (!msg)
6799 return;
6800
6801 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6802 if (!hdr) {
6803 nlmsg_free(msg);
6804 return;
6805 }
6806
6807 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6808 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6809 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6810
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006811 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006812
Johannes Berg463d0182009-07-14 00:33:35 +02006813 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6814 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006815 return;
6816
6817 nla_put_failure:
6818 genlmsg_cancel(msg, hdr);
6819 nlmsg_free(msg);
6820}
6821
6822void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006823 struct net_device *netdev, const u8 *buf,
6824 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006825{
6826 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006827 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006828}
6829
6830void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6831 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006832 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006833{
Johannes Berge6d6e342009-07-01 21:26:47 +02006834 nl80211_send_mlme_event(rdev, netdev, buf, len,
6835 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006836}
6837
Jouni Malinen53b46b82009-03-27 20:53:56 +02006838void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006839 struct net_device *netdev, const u8 *buf,
6840 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006841{
6842 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006843 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006844}
6845
Jouni Malinen53b46b82009-03-27 20:53:56 +02006846void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6847 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006848 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006849{
6850 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006851 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006852}
6853
Jouni Malinencf4e5942010-12-16 00:52:40 +02006854void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6855 struct net_device *netdev, const u8 *buf,
6856 size_t len, gfp_t gfp)
6857{
6858 nl80211_send_mlme_event(rdev, netdev, buf, len,
6859 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6860}
6861
6862void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6863 struct net_device *netdev, const u8 *buf,
6864 size_t len, gfp_t gfp)
6865{
6866 nl80211_send_mlme_event(rdev, netdev, buf, len,
6867 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6868}
6869
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006870static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6871 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006872 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006873{
6874 struct sk_buff *msg;
6875 void *hdr;
6876
Johannes Berge6d6e342009-07-01 21:26:47 +02006877 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006878 if (!msg)
6879 return;
6880
6881 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6882 if (!hdr) {
6883 nlmsg_free(msg);
6884 return;
6885 }
6886
6887 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6888 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6889 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6890 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6891
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006892 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03006893
Johannes Berg463d0182009-07-14 00:33:35 +02006894 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6895 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006896 return;
6897
6898 nla_put_failure:
6899 genlmsg_cancel(msg, hdr);
6900 nlmsg_free(msg);
6901}
6902
6903void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006904 struct net_device *netdev, const u8 *addr,
6905 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006906{
6907 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006908 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006909}
6910
6911void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006912 struct net_device *netdev, const u8 *addr,
6913 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006914{
Johannes Berge6d6e342009-07-01 21:26:47 +02006915 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6916 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006917}
6918
Samuel Ortizb23aa672009-07-01 21:26:54 +02006919void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6920 struct net_device *netdev, const u8 *bssid,
6921 const u8 *req_ie, size_t req_ie_len,
6922 const u8 *resp_ie, size_t resp_ie_len,
6923 u16 status, gfp_t gfp)
6924{
6925 struct sk_buff *msg;
6926 void *hdr;
6927
6928 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6929 if (!msg)
6930 return;
6931
6932 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6933 if (!hdr) {
6934 nlmsg_free(msg);
6935 return;
6936 }
6937
6938 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6939 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6940 if (bssid)
6941 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6942 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6943 if (req_ie)
6944 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6945 if (resp_ie)
6946 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6947
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006948 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006949
Johannes Berg463d0182009-07-14 00:33:35 +02006950 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6951 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006952 return;
6953
6954 nla_put_failure:
6955 genlmsg_cancel(msg, hdr);
6956 nlmsg_free(msg);
6957
6958}
6959
6960void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6961 struct net_device *netdev, const u8 *bssid,
6962 const u8 *req_ie, size_t req_ie_len,
6963 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6964{
6965 struct sk_buff *msg;
6966 void *hdr;
6967
6968 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6969 if (!msg)
6970 return;
6971
6972 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6973 if (!hdr) {
6974 nlmsg_free(msg);
6975 return;
6976 }
6977
6978 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6979 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6980 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6981 if (req_ie)
6982 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6983 if (resp_ie)
6984 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6985
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006986 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006987
Johannes Berg463d0182009-07-14 00:33:35 +02006988 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6989 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006990 return;
6991
6992 nla_put_failure:
6993 genlmsg_cancel(msg, hdr);
6994 nlmsg_free(msg);
6995
6996}
6997
6998void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6999 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007000 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007001{
7002 struct sk_buff *msg;
7003 void *hdr;
7004
Johannes Berg667503d2009-07-07 03:56:11 +02007005 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007006 if (!msg)
7007 return;
7008
7009 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7010 if (!hdr) {
7011 nlmsg_free(msg);
7012 return;
7013 }
7014
7015 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7016 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7017 if (from_ap && reason)
7018 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7019 if (from_ap)
7020 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7021 if (ie)
7022 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7023
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007024 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007025
Johannes Berg463d0182009-07-14 00:33:35 +02007026 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7027 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007028 return;
7029
7030 nla_put_failure:
7031 genlmsg_cancel(msg, hdr);
7032 nlmsg_free(msg);
7033
7034}
7035
Johannes Berg04a773a2009-04-19 21:24:32 +02007036void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7037 struct net_device *netdev, const u8 *bssid,
7038 gfp_t gfp)
7039{
7040 struct sk_buff *msg;
7041 void *hdr;
7042
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007043 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007044 if (!msg)
7045 return;
7046
7047 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7048 if (!hdr) {
7049 nlmsg_free(msg);
7050 return;
7051 }
7052
7053 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7054 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7055 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7056
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007057 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007058
Johannes Berg463d0182009-07-14 00:33:35 +02007059 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7060 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007061 return;
7062
7063 nla_put_failure:
7064 genlmsg_cancel(msg, hdr);
7065 nlmsg_free(msg);
7066}
7067
Javier Cardonac93b5e72011-04-07 15:08:34 -07007068void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7069 struct net_device *netdev,
7070 const u8 *macaddr, const u8* ie, u8 ie_len,
7071 gfp_t gfp)
7072{
7073 struct sk_buff *msg;
7074 void *hdr;
7075
7076 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7077 if (!msg)
7078 return;
7079
7080 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7081 if (!hdr) {
7082 nlmsg_free(msg);
7083 return;
7084 }
7085
7086 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7087 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7088 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7089 if (ie_len && ie)
7090 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7091
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007092 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007093
7094 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7095 nl80211_mlme_mcgrp.id, gfp);
7096 return;
7097
7098 nla_put_failure:
7099 genlmsg_cancel(msg, hdr);
7100 nlmsg_free(msg);
7101}
7102
Jouni Malinena3b8b052009-03-27 21:59:49 +02007103void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7104 struct net_device *netdev, const u8 *addr,
7105 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007106 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007107{
7108 struct sk_buff *msg;
7109 void *hdr;
7110
Johannes Berge6d6e342009-07-01 21:26:47 +02007111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007112 if (!msg)
7113 return;
7114
7115 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7116 if (!hdr) {
7117 nlmsg_free(msg);
7118 return;
7119 }
7120
7121 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7123 if (addr)
7124 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7125 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007126 if (key_id != -1)
7127 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007128 if (tsc)
7129 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7130
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007131 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007132
Johannes Berg463d0182009-07-14 00:33:35 +02007133 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7134 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007135 return;
7136
7137 nla_put_failure:
7138 genlmsg_cancel(msg, hdr);
7139 nlmsg_free(msg);
7140}
7141
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007142void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7143 struct ieee80211_channel *channel_before,
7144 struct ieee80211_channel *channel_after)
7145{
7146 struct sk_buff *msg;
7147 void *hdr;
7148 struct nlattr *nl_freq;
7149
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007150 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007151 if (!msg)
7152 return;
7153
7154 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7155 if (!hdr) {
7156 nlmsg_free(msg);
7157 return;
7158 }
7159
7160 /*
7161 * Since we are applying the beacon hint to a wiphy we know its
7162 * wiphy_idx is valid
7163 */
7164 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7165
7166 /* Before */
7167 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7168 if (!nl_freq)
7169 goto nla_put_failure;
7170 if (nl80211_msg_put_channel(msg, channel_before))
7171 goto nla_put_failure;
7172 nla_nest_end(msg, nl_freq);
7173
7174 /* After */
7175 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7176 if (!nl_freq)
7177 goto nla_put_failure;
7178 if (nl80211_msg_put_channel(msg, channel_after))
7179 goto nla_put_failure;
7180 nla_nest_end(msg, nl_freq);
7181
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007182 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007183
Johannes Berg463d0182009-07-14 00:33:35 +02007184 rcu_read_lock();
7185 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7186 GFP_ATOMIC);
7187 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007188
7189 return;
7190
7191nla_put_failure:
7192 genlmsg_cancel(msg, hdr);
7193 nlmsg_free(msg);
7194}
7195
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007196static void nl80211_send_remain_on_chan_event(
7197 int cmd, struct cfg80211_registered_device *rdev,
7198 struct net_device *netdev, u64 cookie,
7199 struct ieee80211_channel *chan,
7200 enum nl80211_channel_type channel_type,
7201 unsigned int duration, gfp_t gfp)
7202{
7203 struct sk_buff *msg;
7204 void *hdr;
7205
7206 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7207 if (!msg)
7208 return;
7209
7210 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7211 if (!hdr) {
7212 nlmsg_free(msg);
7213 return;
7214 }
7215
7216 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7217 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7218 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7219 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7220 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7221
7222 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7223 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7224
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007225 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007226
7227 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7228 nl80211_mlme_mcgrp.id, gfp);
7229 return;
7230
7231 nla_put_failure:
7232 genlmsg_cancel(msg, hdr);
7233 nlmsg_free(msg);
7234}
7235
7236void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7237 struct net_device *netdev, u64 cookie,
7238 struct ieee80211_channel *chan,
7239 enum nl80211_channel_type channel_type,
7240 unsigned int duration, gfp_t gfp)
7241{
7242 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7243 rdev, netdev, cookie, chan,
7244 channel_type, duration, gfp);
7245}
7246
7247void nl80211_send_remain_on_channel_cancel(
7248 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7249 u64 cookie, struct ieee80211_channel *chan,
7250 enum nl80211_channel_type channel_type, gfp_t gfp)
7251{
7252 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7253 rdev, netdev, cookie, chan,
7254 channel_type, 0, gfp);
7255}
7256
Johannes Berg98b62182009-12-23 13:15:44 +01007257void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7258 struct net_device *dev, const u8 *mac_addr,
7259 struct station_info *sinfo, gfp_t gfp)
7260{
7261 struct sk_buff *msg;
7262
7263 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7264 if (!msg)
7265 return;
7266
7267 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7268 nlmsg_free(msg);
7269 return;
7270 }
7271
7272 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7273 nl80211_mlme_mcgrp.id, gfp);
7274}
7275
Jouni Malinenec15e682011-03-23 15:29:52 +02007276void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7277 struct net_device *dev, const u8 *mac_addr,
7278 gfp_t gfp)
7279{
7280 struct sk_buff *msg;
7281 void *hdr;
7282
7283 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7284 if (!msg)
7285 return;
7286
7287 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7288 if (!hdr) {
7289 nlmsg_free(msg);
7290 return;
7291 }
7292
7293 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7294 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7295
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007296 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007297
7298 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7299 nl80211_mlme_mcgrp.id, gfp);
7300 return;
7301
7302 nla_put_failure:
7303 genlmsg_cancel(msg, hdr);
7304 nlmsg_free(msg);
7305}
7306
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007307static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7308 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007309{
7310 struct wireless_dev *wdev = dev->ieee80211_ptr;
7311 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7312 struct sk_buff *msg;
7313 void *hdr;
7314 int err;
7315 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7316
7317 if (!nlpid)
7318 return false;
7319
7320 msg = nlmsg_new(100, gfp);
7321 if (!msg)
7322 return true;
7323
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007324 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007325 if (!hdr) {
7326 nlmsg_free(msg);
7327 return true;
7328 }
7329
7330 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7331 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7332 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7333
7334 err = genlmsg_end(msg, hdr);
7335 if (err < 0) {
7336 nlmsg_free(msg);
7337 return true;
7338 }
7339
7340 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7341 return true;
7342
7343 nla_put_failure:
7344 genlmsg_cancel(msg, hdr);
7345 nlmsg_free(msg);
7346 return true;
7347}
7348
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007349bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7350{
7351 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7352 addr, gfp);
7353}
7354
7355bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7356 const u8 *addr, gfp_t gfp)
7357{
7358 return __nl80211_unexpected_frame(dev,
7359 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7360 addr, gfp);
7361}
7362
Johannes Berg2e161f72010-08-12 15:38:38 +02007363int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7364 struct net_device *netdev, u32 nlpid,
7365 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007366{
7367 struct sk_buff *msg;
7368 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007369
7370 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7371 if (!msg)
7372 return -ENOMEM;
7373
Johannes Berg2e161f72010-08-12 15:38:38 +02007374 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007375 if (!hdr) {
7376 nlmsg_free(msg);
7377 return -ENOMEM;
7378 }
7379
7380 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7381 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7382 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7383 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7384
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007385 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007386
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007387 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007388
7389 nla_put_failure:
7390 genlmsg_cancel(msg, hdr);
7391 nlmsg_free(msg);
7392 return -ENOBUFS;
7393}
7394
Johannes Berg2e161f72010-08-12 15:38:38 +02007395void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7396 struct net_device *netdev, u64 cookie,
7397 const u8 *buf, size_t len, bool ack,
7398 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007399{
7400 struct sk_buff *msg;
7401 void *hdr;
7402
7403 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7404 if (!msg)
7405 return;
7406
Johannes Berg2e161f72010-08-12 15:38:38 +02007407 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007408 if (!hdr) {
7409 nlmsg_free(msg);
7410 return;
7411 }
7412
7413 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7414 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7415 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7416 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7417 if (ack)
7418 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7419
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007420 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007421
7422 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7423 return;
7424
7425 nla_put_failure:
7426 genlmsg_cancel(msg, hdr);
7427 nlmsg_free(msg);
7428}
7429
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007430void
7431nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7432 struct net_device *netdev,
7433 enum nl80211_cqm_rssi_threshold_event rssi_event,
7434 gfp_t gfp)
7435{
7436 struct sk_buff *msg;
7437 struct nlattr *pinfoattr;
7438 void *hdr;
7439
7440 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7441 if (!msg)
7442 return;
7443
7444 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7445 if (!hdr) {
7446 nlmsg_free(msg);
7447 return;
7448 }
7449
7450 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7451 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7452
7453 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7454 if (!pinfoattr)
7455 goto nla_put_failure;
7456
7457 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7458 rssi_event);
7459
7460 nla_nest_end(msg, pinfoattr);
7461
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007462 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007463
7464 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7465 nl80211_mlme_mcgrp.id, gfp);
7466 return;
7467
7468 nla_put_failure:
7469 genlmsg_cancel(msg, hdr);
7470 nlmsg_free(msg);
7471}
7472
Johannes Berge5497d72011-07-05 16:35:40 +02007473void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7474 struct net_device *netdev, const u8 *bssid,
7475 const u8 *replay_ctr, gfp_t gfp)
7476{
7477 struct sk_buff *msg;
7478 struct nlattr *rekey_attr;
7479 void *hdr;
7480
7481 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7482 if (!msg)
7483 return;
7484
7485 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7486 if (!hdr) {
7487 nlmsg_free(msg);
7488 return;
7489 }
7490
7491 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7492 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7493 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7494
7495 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7496 if (!rekey_attr)
7497 goto nla_put_failure;
7498
7499 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7500 NL80211_REPLAY_CTR_LEN, replay_ctr);
7501
7502 nla_nest_end(msg, rekey_attr);
7503
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007504 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007505
7506 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7507 nl80211_mlme_mcgrp.id, gfp);
7508 return;
7509
7510 nla_put_failure:
7511 genlmsg_cancel(msg, hdr);
7512 nlmsg_free(msg);
7513}
7514
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007515void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7516 struct net_device *netdev, int index,
7517 const u8 *bssid, bool preauth, gfp_t gfp)
7518{
7519 struct sk_buff *msg;
7520 struct nlattr *attr;
7521 void *hdr;
7522
7523 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7524 if (!msg)
7525 return;
7526
7527 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7528 if (!hdr) {
7529 nlmsg_free(msg);
7530 return;
7531 }
7532
7533 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7534 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7535
7536 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7537 if (!attr)
7538 goto nla_put_failure;
7539
7540 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7541 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7542 if (preauth)
7543 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7544
7545 nla_nest_end(msg, attr);
7546
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007547 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007548
7549 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7550 nl80211_mlme_mcgrp.id, gfp);
7551 return;
7552
7553 nla_put_failure:
7554 genlmsg_cancel(msg, hdr);
7555 nlmsg_free(msg);
7556}
7557
Johannes Bergc063dbf2010-11-24 08:10:05 +01007558void
7559nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7560 struct net_device *netdev, const u8 *peer,
7561 u32 num_packets, gfp_t gfp)
7562{
7563 struct sk_buff *msg;
7564 struct nlattr *pinfoattr;
7565 void *hdr;
7566
7567 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7568 if (!msg)
7569 return;
7570
7571 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7572 if (!hdr) {
7573 nlmsg_free(msg);
7574 return;
7575 }
7576
7577 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7578 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7579 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7580
7581 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7582 if (!pinfoattr)
7583 goto nla_put_failure;
7584
7585 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7586
7587 nla_nest_end(msg, pinfoattr);
7588
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007589 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007590
7591 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7592 nl80211_mlme_mcgrp.id, gfp);
7593 return;
7594
7595 nla_put_failure:
7596 genlmsg_cancel(msg, hdr);
7597 nlmsg_free(msg);
7598}
7599
Johannes Berg7f6cf312011-11-04 11:18:15 +01007600void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7601 u64 cookie, bool acked, gfp_t gfp)
7602{
7603 struct wireless_dev *wdev = dev->ieee80211_ptr;
7604 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7605 struct sk_buff *msg;
7606 void *hdr;
7607 int err;
7608
7609 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7610 if (!msg)
7611 return;
7612
7613 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7614 if (!hdr) {
7615 nlmsg_free(msg);
7616 return;
7617 }
7618
7619 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7621 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7622 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7623 if (acked)
7624 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7625
7626 err = genlmsg_end(msg, hdr);
7627 if (err < 0) {
7628 nlmsg_free(msg);
7629 return;
7630 }
7631
7632 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7633 nl80211_mlme_mcgrp.id, gfp);
7634 return;
7635
7636 nla_put_failure:
7637 genlmsg_cancel(msg, hdr);
7638 nlmsg_free(msg);
7639}
7640EXPORT_SYMBOL(cfg80211_probe_status);
7641
Johannes Berg5e760232011-11-04 11:18:17 +01007642void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7643 const u8 *frame, size_t len,
7644 int freq, gfp_t gfp)
7645{
7646 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7647 struct sk_buff *msg;
7648 void *hdr;
7649 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7650
7651 if (!nlpid)
7652 return;
7653
7654 msg = nlmsg_new(len + 100, gfp);
7655 if (!msg)
7656 return;
7657
7658 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7659 if (!hdr) {
7660 nlmsg_free(msg);
7661 return;
7662 }
7663
7664 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7665 if (freq)
7666 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7667 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7668
7669 genlmsg_end(msg, hdr);
7670
7671 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7672 return;
7673
7674 nla_put_failure:
7675 genlmsg_cancel(msg, hdr);
7676 nlmsg_free(msg);
7677}
7678EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7679
Jouni Malinen026331c2010-02-15 12:53:10 +02007680static int nl80211_netlink_notify(struct notifier_block * nb,
7681 unsigned long state,
7682 void *_notify)
7683{
7684 struct netlink_notify *notify = _notify;
7685 struct cfg80211_registered_device *rdev;
7686 struct wireless_dev *wdev;
7687
7688 if (state != NETLINK_URELEASE)
7689 return NOTIFY_DONE;
7690
7691 rcu_read_lock();
7692
Johannes Berg5e760232011-11-04 11:18:17 +01007693 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007694 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007695 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01007696 if (rdev->ap_beacons_nlpid == notify->pid)
7697 rdev->ap_beacons_nlpid = 0;
7698 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007699
7700 rcu_read_unlock();
7701
7702 return NOTIFY_DONE;
7703}
7704
7705static struct notifier_block nl80211_netlink_notifier = {
7706 .notifier_call = nl80211_netlink_notify,
7707};
7708
Johannes Berg55682962007-09-20 13:09:35 -04007709/* initialisation/exit functions */
7710
7711int nl80211_init(void)
7712{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007713 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007714
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007715 err = genl_register_family_with_ops(&nl80211_fam,
7716 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007717 if (err)
7718 return err;
7719
Johannes Berg55682962007-09-20 13:09:35 -04007720 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7721 if (err)
7722 goto err_out;
7723
Johannes Berg2a519312009-02-10 21:25:55 +01007724 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7725 if (err)
7726 goto err_out;
7727
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007728 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7729 if (err)
7730 goto err_out;
7731
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007732 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7733 if (err)
7734 goto err_out;
7735
Johannes Bergaff89a92009-07-01 21:26:51 +02007736#ifdef CONFIG_NL80211_TESTMODE
7737 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7738 if (err)
7739 goto err_out;
7740#endif
7741
Jouni Malinen026331c2010-02-15 12:53:10 +02007742 err = netlink_register_notifier(&nl80211_netlink_notifier);
7743 if (err)
7744 goto err_out;
7745
Johannes Berg55682962007-09-20 13:09:35 -04007746 return 0;
7747 err_out:
7748 genl_unregister_family(&nl80211_fam);
7749 return err;
7750}
7751
7752void nl80211_exit(void)
7753{
Jouni Malinen026331c2010-02-15 12:53:10 +02007754 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007755 genl_unregister_family(&nl80211_fam);
7756}