blob: 5b659068b02092ebf54b17726250c3b91a081917 [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 Malinendc6382c2009-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 Berg55682962007-09-20 13:09:35 -0400199};
200
Johannes Berge31b8212010-10-05 19:39:30 +0200201/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000202static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200203 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200204 [NL80211_KEY_IDX] = { .type = NLA_U8 },
205 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200206 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200207 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
208 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200209 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100210 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
211};
212
213/* policy for the key default flags */
214static const struct nla_policy
215nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
216 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
217 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200218};
219
Johannes Bergff1b6e62011-05-04 15:37:28 +0200220/* policy for WoWLAN attributes */
221static const struct nla_policy
222nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
223 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
224 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
225 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
226 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200227 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
228 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
229 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
230 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200231};
232
Johannes Berge5497d72011-07-05 16:35:40 +0200233/* policy for GTK rekey offload attributes */
234static const struct nla_policy
235nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
236 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
237 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
238 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
239};
240
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300241static const struct nla_policy
242nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
243 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
244 .len = IEEE80211_MAX_SSID_LEN },
245};
246
Holger Schuriga0438972009-11-11 11:30:02 +0100247/* ifidx get helper */
248static int nl80211_get_ifidx(struct netlink_callback *cb)
249{
250 int res;
251
252 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
253 nl80211_fam.attrbuf, nl80211_fam.maxattr,
254 nl80211_policy);
255 if (res)
256 return res;
257
258 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
259 return -EINVAL;
260
261 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
262 if (!res)
263 return -EINVAL;
264 return res;
265}
266
Johannes Berg67748892010-10-04 21:14:06 +0200267static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
268 struct netlink_callback *cb,
269 struct cfg80211_registered_device **rdev,
270 struct net_device **dev)
271{
272 int ifidx = cb->args[0];
273 int err;
274
275 if (!ifidx)
276 ifidx = nl80211_get_ifidx(cb);
277 if (ifidx < 0)
278 return ifidx;
279
280 cb->args[0] = ifidx;
281
282 rtnl_lock();
283
284 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
285 if (!*dev) {
286 err = -ENODEV;
287 goto out_rtnl;
288 }
289
290 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100291 if (IS_ERR(*rdev)) {
292 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200293 goto out_rtnl;
294 }
295
296 return 0;
297 out_rtnl:
298 rtnl_unlock();
299 return err;
300}
301
302static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
303{
304 cfg80211_unlock_rdev(rdev);
305 rtnl_unlock();
306}
307
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100308/* IE validation */
309static bool is_valid_ie_attr(const struct nlattr *attr)
310{
311 const u8 *pos;
312 int len;
313
314 if (!attr)
315 return true;
316
317 pos = nla_data(attr);
318 len = nla_len(attr);
319
320 while (len) {
321 u8 elemlen;
322
323 if (len < 2)
324 return false;
325 len -= 2;
326
327 elemlen = pos[1];
328 if (elemlen > len)
329 return false;
330
331 len -= elemlen;
332 pos += 2 + elemlen;
333 }
334
335 return true;
336}
337
Johannes Berg55682962007-09-20 13:09:35 -0400338/* message building helper */
339static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
340 int flags, u8 cmd)
341{
342 /* since there is no private header just add the generic one */
343 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
344}
345
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400346static int nl80211_msg_put_channel(struct sk_buff *msg,
347 struct ieee80211_channel *chan)
348{
349 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
350 chan->center_freq);
351
352 if (chan->flags & IEEE80211_CHAN_DISABLED)
353 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
354 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
355 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
356 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
357 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
358 if (chan->flags & IEEE80211_CHAN_RADAR)
359 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
360
361 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
362 DBM_TO_MBM(chan->max_power));
363
364 return 0;
365
366 nla_put_failure:
367 return -ENOBUFS;
368}
369
Johannes Berg55682962007-09-20 13:09:35 -0400370/* netlink command implementations */
371
Johannes Bergb9454e82009-07-08 13:29:08 +0200372struct key_parse {
373 struct key_params p;
374 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200375 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200376 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100377 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200378};
379
380static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
381{
382 struct nlattr *tb[NL80211_KEY_MAX + 1];
383 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
384 nl80211_key_policy);
385 if (err)
386 return err;
387
388 k->def = !!tb[NL80211_KEY_DEFAULT];
389 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
390
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100391 if (k->def) {
392 k->def_uni = true;
393 k->def_multi = true;
394 }
395 if (k->defmgmt)
396 k->def_multi = true;
397
Johannes Bergb9454e82009-07-08 13:29:08 +0200398 if (tb[NL80211_KEY_IDX])
399 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
400
401 if (tb[NL80211_KEY_DATA]) {
402 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
403 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
404 }
405
406 if (tb[NL80211_KEY_SEQ]) {
407 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
408 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
409 }
410
411 if (tb[NL80211_KEY_CIPHER])
412 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
413
Johannes Berge31b8212010-10-05 19:39:30 +0200414 if (tb[NL80211_KEY_TYPE]) {
415 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
416 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
417 return -EINVAL;
418 }
419
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100420 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
421 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
422 int err = nla_parse_nested(kdt,
423 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
424 tb[NL80211_KEY_DEFAULT_TYPES],
425 nl80211_key_default_policy);
426 if (err)
427 return err;
428
429 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
430 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
431 }
432
Johannes Bergb9454e82009-07-08 13:29:08 +0200433 return 0;
434}
435
436static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
437{
438 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
439 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
440 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
441 }
442
443 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
444 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
445 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
446 }
447
448 if (info->attrs[NL80211_ATTR_KEY_IDX])
449 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
450
451 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
452 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
453
454 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
455 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
456
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100457 if (k->def) {
458 k->def_uni = true;
459 k->def_multi = true;
460 }
461 if (k->defmgmt)
462 k->def_multi = true;
463
Johannes Berge31b8212010-10-05 19:39:30 +0200464 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
465 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
466 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
467 return -EINVAL;
468 }
469
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100470 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
471 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
472 int err = nla_parse_nested(
473 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
474 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
475 nl80211_key_default_policy);
476 if (err)
477 return err;
478
479 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
480 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
481 }
482
Johannes Bergb9454e82009-07-08 13:29:08 +0200483 return 0;
484}
485
486static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
487{
488 int err;
489
490 memset(k, 0, sizeof(*k));
491 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200492 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200493
494 if (info->attrs[NL80211_ATTR_KEY])
495 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
496 else
497 err = nl80211_parse_key_old(info, k);
498
499 if (err)
500 return err;
501
502 if (k->def && k->defmgmt)
503 return -EINVAL;
504
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100505 if (k->defmgmt) {
506 if (k->def_uni || !k->def_multi)
507 return -EINVAL;
508 }
509
Johannes Bergb9454e82009-07-08 13:29:08 +0200510 if (k->idx != -1) {
511 if (k->defmgmt) {
512 if (k->idx < 4 || k->idx > 5)
513 return -EINVAL;
514 } else if (k->def) {
515 if (k->idx < 0 || k->idx > 3)
516 return -EINVAL;
517 } else {
518 if (k->idx < 0 || k->idx > 5)
519 return -EINVAL;
520 }
521 }
522
523 return 0;
524}
525
Johannes Bergfffd0932009-07-08 14:22:54 +0200526static struct cfg80211_cached_keys *
527nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
528 struct nlattr *keys)
529{
530 struct key_parse parse;
531 struct nlattr *key;
532 struct cfg80211_cached_keys *result;
533 int rem, err, def = 0;
534
535 result = kzalloc(sizeof(*result), GFP_KERNEL);
536 if (!result)
537 return ERR_PTR(-ENOMEM);
538
539 result->def = -1;
540 result->defmgmt = -1;
541
542 nla_for_each_nested(key, keys, rem) {
543 memset(&parse, 0, sizeof(parse));
544 parse.idx = -1;
545
546 err = nl80211_parse_key_new(key, &parse);
547 if (err)
548 goto error;
549 err = -EINVAL;
550 if (!parse.p.key)
551 goto error;
552 if (parse.idx < 0 || parse.idx > 4)
553 goto error;
554 if (parse.def) {
555 if (def)
556 goto error;
557 def = 1;
558 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100559 if (!parse.def_uni || !parse.def_multi)
560 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200561 } else if (parse.defmgmt)
562 goto error;
563 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200564 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200565 if (err)
566 goto error;
567 result->params[parse.idx].cipher = parse.p.cipher;
568 result->params[parse.idx].key_len = parse.p.key_len;
569 result->params[parse.idx].key = result->data[parse.idx];
570 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
571 }
572
573 return result;
574 error:
575 kfree(result);
576 return ERR_PTR(err);
577}
578
579static int nl80211_key_allowed(struct wireless_dev *wdev)
580{
581 ASSERT_WDEV_LOCK(wdev);
582
Johannes Bergfffd0932009-07-08 14:22:54 +0200583 switch (wdev->iftype) {
584 case NL80211_IFTYPE_AP:
585 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200586 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700587 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200588 break;
589 case NL80211_IFTYPE_ADHOC:
590 if (!wdev->current_bss)
591 return -ENOLINK;
592 break;
593 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200594 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200595 if (wdev->sme_state != CFG80211_SME_CONNECTED)
596 return -ENOLINK;
597 break;
598 default:
599 return -EINVAL;
600 }
601
602 return 0;
603}
604
Johannes Berg7527a782011-05-13 10:58:57 +0200605static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
606{
607 struct nlattr *nl_modes = nla_nest_start(msg, attr);
608 int i;
609
610 if (!nl_modes)
611 goto nla_put_failure;
612
613 i = 0;
614 while (ifmodes) {
615 if (ifmodes & 1)
616 NLA_PUT_FLAG(msg, i);
617 ifmodes >>= 1;
618 i++;
619 }
620
621 nla_nest_end(msg, nl_modes);
622 return 0;
623
624nla_put_failure:
625 return -ENOBUFS;
626}
627
628static int nl80211_put_iface_combinations(struct wiphy *wiphy,
629 struct sk_buff *msg)
630{
631 struct nlattr *nl_combis;
632 int i, j;
633
634 nl_combis = nla_nest_start(msg,
635 NL80211_ATTR_INTERFACE_COMBINATIONS);
636 if (!nl_combis)
637 goto nla_put_failure;
638
639 for (i = 0; i < wiphy->n_iface_combinations; i++) {
640 const struct ieee80211_iface_combination *c;
641 struct nlattr *nl_combi, *nl_limits;
642
643 c = &wiphy->iface_combinations[i];
644
645 nl_combi = nla_nest_start(msg, i + 1);
646 if (!nl_combi)
647 goto nla_put_failure;
648
649 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
650 if (!nl_limits)
651 goto nla_put_failure;
652
653 for (j = 0; j < c->n_limits; j++) {
654 struct nlattr *nl_limit;
655
656 nl_limit = nla_nest_start(msg, j + 1);
657 if (!nl_limit)
658 goto nla_put_failure;
659 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
660 c->limits[j].max);
661 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
662 c->limits[j].types))
663 goto nla_put_failure;
664 nla_nest_end(msg, nl_limit);
665 }
666
667 nla_nest_end(msg, nl_limits);
668
669 if (c->beacon_int_infra_match)
670 NLA_PUT_FLAG(msg,
671 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
672 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
673 c->num_different_channels);
674 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
675 c->max_interfaces);
676
677 nla_nest_end(msg, nl_combi);
678 }
679
680 nla_nest_end(msg, nl_combis);
681
682 return 0;
683nla_put_failure:
684 return -ENOBUFS;
685}
686
Johannes Berg55682962007-09-20 13:09:35 -0400687static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
688 struct cfg80211_registered_device *dev)
689{
690 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100691 struct nlattr *nl_bands, *nl_band;
692 struct nlattr *nl_freqs, *nl_freq;
693 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100694 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100695 enum ieee80211_band band;
696 struct ieee80211_channel *chan;
697 struct ieee80211_rate *rate;
698 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200699 const struct ieee80211_txrx_stypes *mgmt_stypes =
700 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400701
702 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
703 if (!hdr)
704 return -1;
705
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500706 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400707 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200708
Johannes Bergf5ea9122009-08-07 16:17:38 +0200709 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
710 cfg80211_rdev_list_generation);
711
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200712 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
713 dev->wiphy.retry_short);
714 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
715 dev->wiphy.retry_long);
716 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
717 dev->wiphy.frag_threshold);
718 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
719 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100720 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
721 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100722 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
723 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300724 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
725 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200726 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
727 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300728 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
729 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300730 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
731 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100732
Johannes Berge31b8212010-10-05 19:39:30 +0200733 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
734 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700735 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
736 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300737 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
738 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530739 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
740 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300741 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
742 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
743 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
744 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530745
Johannes Berg25e47c12009-04-02 20:14:06 +0200746 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
747 sizeof(u32) * dev->wiphy.n_cipher_suites,
748 dev->wiphy.cipher_suites);
749
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100750 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
751 dev->wiphy.max_num_pmkids);
752
Johannes Bergc0692b82010-08-27 14:26:53 +0300753 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
754 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
755
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900756 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
757 dev->wiphy.available_antennas_tx);
758 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
759 dev->wiphy.available_antennas_rx);
760
Bruno Randolf7f531e02010-12-16 11:30:22 +0900761 if ((dev->wiphy.available_antennas_tx ||
762 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900763 u32 tx_ant = 0, rx_ant = 0;
764 int res;
765 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
766 if (!res) {
767 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
768 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
769 }
770 }
771
Johannes Berg7527a782011-05-13 10:58:57 +0200772 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
773 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700774 goto nla_put_failure;
775
Johannes Bergee688b002008-01-24 19:38:39 +0100776 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
777 if (!nl_bands)
778 goto nla_put_failure;
779
780 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
781 if (!dev->wiphy.bands[band])
782 continue;
783
784 nl_band = nla_nest_start(msg, band);
785 if (!nl_band)
786 goto nla_put_failure;
787
Johannes Bergd51626d2008-10-09 12:20:13 +0200788 /* add HT info */
789 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
790 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
791 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
792 &dev->wiphy.bands[band]->ht_cap.mcs);
793 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
794 dev->wiphy.bands[band]->ht_cap.cap);
795 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
796 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
797 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
798 dev->wiphy.bands[band]->ht_cap.ampdu_density);
799 }
800
Johannes Bergee688b002008-01-24 19:38:39 +0100801 /* add frequencies */
802 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
803 if (!nl_freqs)
804 goto nla_put_failure;
805
806 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
807 nl_freq = nla_nest_start(msg, i);
808 if (!nl_freq)
809 goto nla_put_failure;
810
811 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100812
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400813 if (nl80211_msg_put_channel(msg, chan))
814 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200815
Johannes Bergee688b002008-01-24 19:38:39 +0100816 nla_nest_end(msg, nl_freq);
817 }
818
819 nla_nest_end(msg, nl_freqs);
820
821 /* add bitrates */
822 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
823 if (!nl_rates)
824 goto nla_put_failure;
825
826 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
827 nl_rate = nla_nest_start(msg, i);
828 if (!nl_rate)
829 goto nla_put_failure;
830
831 rate = &dev->wiphy.bands[band]->bitrates[i];
832 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
833 rate->bitrate);
834 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
835 NLA_PUT_FLAG(msg,
836 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
837
838 nla_nest_end(msg, nl_rate);
839 }
840
841 nla_nest_end(msg, nl_rates);
842
843 nla_nest_end(msg, nl_band);
844 }
845 nla_nest_end(msg, nl_bands);
846
Johannes Berg8fdc6212009-03-14 09:34:01 +0100847 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
848 if (!nl_cmds)
849 goto nla_put_failure;
850
851 i = 0;
852#define CMD(op, n) \
853 do { \
854 if (dev->ops->op) { \
855 i++; \
856 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
857 } \
858 } while (0)
859
860 CMD(add_virtual_intf, NEW_INTERFACE);
861 CMD(change_virtual_intf, SET_INTERFACE);
862 CMD(add_key, NEW_KEY);
863 CMD(add_beacon, NEW_BEACON);
864 CMD(add_station, NEW_STATION);
865 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800866 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100867 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200868 CMD(auth, AUTHENTICATE);
869 CMD(assoc, ASSOCIATE);
870 CMD(deauth, DEAUTHENTICATE);
871 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200872 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100873 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100874 CMD(set_pmksa, SET_PMKSA);
875 CMD(del_pmksa, DEL_PMKSA);
876 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100877 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200878 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200879 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100880 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100881 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200882 i++;
883 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
884 }
Johannes Bergf444de02010-05-05 15:25:02 +0200885 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400886 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300887 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
888 CMD(tdls_mgmt, TDLS_MGMT);
889 CMD(tdls_oper, TDLS_OPER);
890 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300891 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
892 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100893 CMD(probe_client, PROBE_CLIENT);
Johannes Berg5e760232011-11-04 11:18:17 +0100894 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
895 i++;
896 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
897 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100898
899#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200900
Johannes Berg6829c872009-07-02 09:13:27 +0200901 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200902 i++;
903 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
904 }
905
Johannes Berg6829c872009-07-02 09:13:27 +0200906 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200907 i++;
908 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
909 }
910
Johannes Berg8fdc6212009-03-14 09:34:01 +0100911 nla_nest_end(msg, nl_cmds);
912
Johannes Berga2939112010-12-14 17:54:28 +0100913 if (dev->ops->remain_on_channel)
914 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
915 dev->wiphy.max_remain_on_channel_duration);
916
Jouni Malinend2da5872011-08-08 12:10:30 +0300917 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100918 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
919
Johannes Berg2e161f72010-08-12 15:38:38 +0200920 if (mgmt_stypes) {
921 u16 stypes;
922 struct nlattr *nl_ftypes, *nl_ifs;
923 enum nl80211_iftype ift;
924
925 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
926 if (!nl_ifs)
927 goto nla_put_failure;
928
929 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
930 nl_ftypes = nla_nest_start(msg, ift);
931 if (!nl_ftypes)
932 goto nla_put_failure;
933 i = 0;
934 stypes = mgmt_stypes[ift].tx;
935 while (stypes) {
936 if (stypes & 1)
937 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
938 (i << 4) | IEEE80211_FTYPE_MGMT);
939 stypes >>= 1;
940 i++;
941 }
942 nla_nest_end(msg, nl_ftypes);
943 }
944
Johannes Berg74b70a42010-08-24 12:15:53 +0200945 nla_nest_end(msg, nl_ifs);
946
Johannes Berg2e161f72010-08-12 15:38:38 +0200947 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
948 if (!nl_ifs)
949 goto nla_put_failure;
950
951 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
952 nl_ftypes = nla_nest_start(msg, ift);
953 if (!nl_ftypes)
954 goto nla_put_failure;
955 i = 0;
956 stypes = mgmt_stypes[ift].rx;
957 while (stypes) {
958 if (stypes & 1)
959 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
960 (i << 4) | IEEE80211_FTYPE_MGMT);
961 stypes >>= 1;
962 i++;
963 }
964 nla_nest_end(msg, nl_ftypes);
965 }
966 nla_nest_end(msg, nl_ifs);
967 }
968
Johannes Bergff1b6e62011-05-04 15:37:28 +0200969 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
970 struct nlattr *nl_wowlan;
971
972 nl_wowlan = nla_nest_start(msg,
973 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
974 if (!nl_wowlan)
975 goto nla_put_failure;
976
977 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
978 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
979 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
980 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
981 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
982 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200983 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
984 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
985 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
986 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
987 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
988 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
989 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
990 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
991 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
992 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200993 if (dev->wiphy.wowlan.n_patterns) {
994 struct nl80211_wowlan_pattern_support pat = {
995 .max_patterns = dev->wiphy.wowlan.n_patterns,
996 .min_pattern_len =
997 dev->wiphy.wowlan.pattern_min_len,
998 .max_pattern_len =
999 dev->wiphy.wowlan.pattern_max_len,
1000 };
1001 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1002 sizeof(pat), &pat);
1003 }
1004
1005 nla_nest_end(msg, nl_wowlan);
1006 }
1007
Johannes Berg7527a782011-05-13 10:58:57 +02001008 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1009 dev->wiphy.software_iftypes))
1010 goto nla_put_failure;
1011
1012 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1013 goto nla_put_failure;
1014
Johannes Berg562a7482011-11-07 12:39:33 +01001015 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1016 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1017 dev->wiphy.ap_sme_capa);
1018
Johannes Berg55682962007-09-20 13:09:35 -04001019 return genlmsg_end(msg, hdr);
1020
1021 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001022 genlmsg_cancel(msg, hdr);
1023 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001024}
1025
1026static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1027{
1028 int idx = 0;
1029 int start = cb->args[0];
1030 struct cfg80211_registered_device *dev;
1031
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001032 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001033 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001034 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1035 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001036 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001037 continue;
1038 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1039 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001040 dev) < 0) {
1041 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001042 break;
Julius Volzb4637272008-07-08 14:02:19 +02001043 }
Johannes Berg55682962007-09-20 13:09:35 -04001044 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001045 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001046
1047 cb->args[0] = idx;
1048
1049 return skb->len;
1050}
1051
1052static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1053{
1054 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001055 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001056
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001058 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001059 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001060
Johannes Berg4c476992010-10-04 21:36:35 +02001061 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1062 nlmsg_free(msg);
1063 return -ENOBUFS;
1064 }
Johannes Berg55682962007-09-20 13:09:35 -04001065
Johannes Berg134e6372009-07-10 09:51:34 +00001066 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001067}
1068
Jouni Malinen31888482008-10-30 16:59:24 +02001069static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1070 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1071 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1072 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1073 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1074 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1075};
1076
1077static int parse_txq_params(struct nlattr *tb[],
1078 struct ieee80211_txq_params *txq_params)
1079{
1080 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1081 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1082 !tb[NL80211_TXQ_ATTR_AIFS])
1083 return -EINVAL;
1084
1085 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1086 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1087 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1088 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1089 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1090
1091 return 0;
1092}
1093
Johannes Bergf444de02010-05-05 15:25:02 +02001094static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1095{
1096 /*
1097 * You can only set the channel explicitly for AP, mesh
1098 * and WDS type interfaces; all others have their channel
1099 * managed via their respective "establish a connection"
1100 * command (connect, join, ...)
1101 *
1102 * Monitors are special as they are normally slaved to
1103 * whatever else is going on, so they behave as though
1104 * you tried setting the wiphy channel itself.
1105 */
1106 return !wdev ||
1107 wdev->iftype == NL80211_IFTYPE_AP ||
1108 wdev->iftype == NL80211_IFTYPE_WDS ||
1109 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001110 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1111 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001112}
1113
1114static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1115 struct wireless_dev *wdev,
1116 struct genl_info *info)
1117{
1118 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1119 u32 freq;
1120 int result;
1121
1122 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1123 return -EINVAL;
1124
1125 if (!nl80211_can_set_dev_channel(wdev))
1126 return -EOPNOTSUPP;
1127
1128 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1129 channel_type = nla_get_u32(info->attrs[
1130 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1131 if (channel_type != NL80211_CHAN_NO_HT &&
1132 channel_type != NL80211_CHAN_HT20 &&
1133 channel_type != NL80211_CHAN_HT40PLUS &&
1134 channel_type != NL80211_CHAN_HT40MINUS)
1135 return -EINVAL;
1136 }
1137
1138 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1139
1140 mutex_lock(&rdev->devlist_mtx);
1141 if (wdev) {
1142 wdev_lock(wdev);
1143 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1144 wdev_unlock(wdev);
1145 } else {
1146 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1147 }
1148 mutex_unlock(&rdev->devlist_mtx);
1149
1150 return result;
1151}
1152
1153static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1154{
Johannes Berg4c476992010-10-04 21:36:35 +02001155 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1156 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001157
Johannes Berg4c476992010-10-04 21:36:35 +02001158 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001159}
1160
Bill Jordane8347eb2010-10-01 13:54:28 -04001161static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1162{
Johannes Berg43b19952010-10-07 13:10:30 +02001163 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1164 struct net_device *dev = info->user_ptr[1];
1165 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001166 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001167
1168 if (!info->attrs[NL80211_ATTR_MAC])
1169 return -EINVAL;
1170
Johannes Berg43b19952010-10-07 13:10:30 +02001171 if (netif_running(dev))
1172 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001173
Johannes Berg43b19952010-10-07 13:10:30 +02001174 if (!rdev->ops->set_wds_peer)
1175 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001176
Johannes Berg43b19952010-10-07 13:10:30 +02001177 if (wdev->iftype != NL80211_IFTYPE_WDS)
1178 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001179
1180 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001181 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001182}
1183
1184
Johannes Berg55682962007-09-20 13:09:35 -04001185static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1186{
1187 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001188 struct net_device *netdev = NULL;
1189 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001190 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001191 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001192 u32 changed;
1193 u8 retry_short = 0, retry_long = 0;
1194 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001195 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001196
Johannes Bergf444de02010-05-05 15:25:02 +02001197 /*
1198 * Try to find the wiphy and netdev. Normally this
1199 * function shouldn't need the netdev, but this is
1200 * done for backward compatibility -- previously
1201 * setting the channel was done per wiphy, but now
1202 * it is per netdev. Previous userland like hostapd
1203 * also passed a netdev to set_wiphy, so that it is
1204 * possible to let that go to the right netdev!
1205 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001206 mutex_lock(&cfg80211_mutex);
1207
Johannes Bergf444de02010-05-05 15:25:02 +02001208 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1209 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1210
1211 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1212 if (netdev && netdev->ieee80211_ptr) {
1213 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1214 mutex_lock(&rdev->mtx);
1215 } else
1216 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001217 }
1218
Johannes Bergf444de02010-05-05 15:25:02 +02001219 if (!netdev) {
1220 rdev = __cfg80211_rdev_from_info(info);
1221 if (IS_ERR(rdev)) {
1222 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001223 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001224 }
1225 wdev = NULL;
1226 netdev = NULL;
1227 result = 0;
1228
1229 mutex_lock(&rdev->mtx);
1230 } else if (netif_running(netdev) &&
1231 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1232 wdev = netdev->ieee80211_ptr;
1233 else
1234 wdev = NULL;
1235
1236 /*
1237 * end workaround code, by now the rdev is available
1238 * and locked, and wdev may or may not be NULL.
1239 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001240
1241 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001242 result = cfg80211_dev_rename(
1243 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001244
1245 mutex_unlock(&cfg80211_mutex);
1246
1247 if (result)
1248 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001249
Jouni Malinen31888482008-10-30 16:59:24 +02001250 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1251 struct ieee80211_txq_params txq_params;
1252 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1253
1254 if (!rdev->ops->set_txq_params) {
1255 result = -EOPNOTSUPP;
1256 goto bad_res;
1257 }
1258
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001259 if (!netdev) {
1260 result = -EINVAL;
1261 goto bad_res;
1262 }
1263
Johannes Berg133a3ff2011-11-03 14:50:13 +01001264 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1265 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1266 result = -EINVAL;
1267 goto bad_res;
1268 }
1269
Jouni Malinen31888482008-10-30 16:59:24 +02001270 nla_for_each_nested(nl_txq_params,
1271 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1272 rem_txq_params) {
1273 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1274 nla_data(nl_txq_params),
1275 nla_len(nl_txq_params),
1276 txq_params_policy);
1277 result = parse_txq_params(tb, &txq_params);
1278 if (result)
1279 goto bad_res;
1280
1281 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001282 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001283 &txq_params);
1284 if (result)
1285 goto bad_res;
1286 }
1287 }
1288
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001289 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001290 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001291 if (result)
1292 goto bad_res;
1293 }
1294
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001295 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1296 enum nl80211_tx_power_setting type;
1297 int idx, mbm = 0;
1298
1299 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001300 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001301 goto bad_res;
1302 }
1303
1304 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1305 type = nla_get_u32(info->attrs[idx]);
1306
1307 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1308 (type != NL80211_TX_POWER_AUTOMATIC)) {
1309 result = -EINVAL;
1310 goto bad_res;
1311 }
1312
1313 if (type != NL80211_TX_POWER_AUTOMATIC) {
1314 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1315 mbm = nla_get_u32(info->attrs[idx]);
1316 }
1317
1318 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1319 if (result)
1320 goto bad_res;
1321 }
1322
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001323 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1324 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1325 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001326 if ((!rdev->wiphy.available_antennas_tx &&
1327 !rdev->wiphy.available_antennas_rx) ||
1328 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001329 result = -EOPNOTSUPP;
1330 goto bad_res;
1331 }
1332
1333 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1334 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1335
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001336 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001337 * available antenna masks, except for the "all" mask */
1338 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1339 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001340 result = -EINVAL;
1341 goto bad_res;
1342 }
1343
Bruno Randolf7f531e02010-12-16 11:30:22 +09001344 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1345 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001346
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001347 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1348 if (result)
1349 goto bad_res;
1350 }
1351
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001352 changed = 0;
1353
1354 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1355 retry_short = nla_get_u8(
1356 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1357 if (retry_short == 0) {
1358 result = -EINVAL;
1359 goto bad_res;
1360 }
1361 changed |= WIPHY_PARAM_RETRY_SHORT;
1362 }
1363
1364 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1365 retry_long = nla_get_u8(
1366 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1367 if (retry_long == 0) {
1368 result = -EINVAL;
1369 goto bad_res;
1370 }
1371 changed |= WIPHY_PARAM_RETRY_LONG;
1372 }
1373
1374 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1375 frag_threshold = nla_get_u32(
1376 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1377 if (frag_threshold < 256) {
1378 result = -EINVAL;
1379 goto bad_res;
1380 }
1381 if (frag_threshold != (u32) -1) {
1382 /*
1383 * Fragments (apart from the last one) are required to
1384 * have even length. Make the fragmentation code
1385 * simpler by stripping LSB should someone try to use
1386 * odd threshold value.
1387 */
1388 frag_threshold &= ~0x1;
1389 }
1390 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1391 }
1392
1393 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1394 rts_threshold = nla_get_u32(
1395 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1396 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1397 }
1398
Lukáš Turek81077e82009-12-21 22:50:47 +01001399 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1400 coverage_class = nla_get_u8(
1401 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1402 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1403 }
1404
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001405 if (changed) {
1406 u8 old_retry_short, old_retry_long;
1407 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001408 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001409
1410 if (!rdev->ops->set_wiphy_params) {
1411 result = -EOPNOTSUPP;
1412 goto bad_res;
1413 }
1414
1415 old_retry_short = rdev->wiphy.retry_short;
1416 old_retry_long = rdev->wiphy.retry_long;
1417 old_frag_threshold = rdev->wiphy.frag_threshold;
1418 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001419 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001420
1421 if (changed & WIPHY_PARAM_RETRY_SHORT)
1422 rdev->wiphy.retry_short = retry_short;
1423 if (changed & WIPHY_PARAM_RETRY_LONG)
1424 rdev->wiphy.retry_long = retry_long;
1425 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1426 rdev->wiphy.frag_threshold = frag_threshold;
1427 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1428 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001429 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1430 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001431
1432 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1433 if (result) {
1434 rdev->wiphy.retry_short = old_retry_short;
1435 rdev->wiphy.retry_long = old_retry_long;
1436 rdev->wiphy.frag_threshold = old_frag_threshold;
1437 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001438 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001439 }
1440 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001441
Johannes Berg306d6112008-12-08 12:39:04 +01001442 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001443 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001444 if (netdev)
1445 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001446 return result;
1447}
1448
1449
1450static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001451 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001452 struct net_device *dev)
1453{
1454 void *hdr;
1455
1456 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1457 if (!hdr)
1458 return -1;
1459
1460 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001461 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001462 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001463 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001464
1465 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1466 rdev->devlist_generation ^
1467 (cfg80211_rdev_list_generation << 2));
1468
Johannes Berg55682962007-09-20 13:09:35 -04001469 return genlmsg_end(msg, hdr);
1470
1471 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001472 genlmsg_cancel(msg, hdr);
1473 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001474}
1475
1476static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1477{
1478 int wp_idx = 0;
1479 int if_idx = 0;
1480 int wp_start = cb->args[0];
1481 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001482 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001483 struct wireless_dev *wdev;
1484
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001485 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001486 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1487 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001488 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001489 if (wp_idx < wp_start) {
1490 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001491 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001492 }
Johannes Berg55682962007-09-20 13:09:35 -04001493 if_idx = 0;
1494
Johannes Bergf5ea9122009-08-07 16:17:38 +02001495 mutex_lock(&rdev->devlist_mtx);
1496 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001497 if (if_idx < if_start) {
1498 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001499 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001500 }
Johannes Berg55682962007-09-20 13:09:35 -04001501 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1502 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001503 rdev, wdev->netdev) < 0) {
1504 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001505 goto out;
1506 }
1507 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001508 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001509 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001510
1511 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001512 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001513 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001514 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001515
1516 cb->args[0] = wp_idx;
1517 cb->args[1] = if_idx;
1518
1519 return skb->len;
1520}
1521
1522static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1523{
1524 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001525 struct cfg80211_registered_device *dev = info->user_ptr[0];
1526 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001527
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001528 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001529 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001530 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001531
Johannes Bergd7264052009-04-19 16:23:20 +02001532 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001533 dev, netdev) < 0) {
1534 nlmsg_free(msg);
1535 return -ENOBUFS;
1536 }
Johannes Berg55682962007-09-20 13:09:35 -04001537
Johannes Berg134e6372009-07-10 09:51:34 +00001538 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001539}
1540
Michael Wu66f7ac52008-01-31 19:48:22 +01001541static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1542 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1543 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1544 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1545 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1546 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1547};
1548
1549static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1550{
1551 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1552 int flag;
1553
1554 *mntrflags = 0;
1555
1556 if (!nla)
1557 return -EINVAL;
1558
1559 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1560 nla, mntr_flags_policy))
1561 return -EINVAL;
1562
1563 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1564 if (flags[flag])
1565 *mntrflags |= (1<<flag);
1566
1567 return 0;
1568}
1569
Johannes Berg9bc383d2009-11-19 11:55:19 +01001570static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001571 struct net_device *netdev, u8 use_4addr,
1572 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001573{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001574 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001575 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001576 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001577 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001578 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001579
1580 switch (iftype) {
1581 case NL80211_IFTYPE_AP_VLAN:
1582 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1583 return 0;
1584 break;
1585 case NL80211_IFTYPE_STATION:
1586 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1587 return 0;
1588 break;
1589 default:
1590 break;
1591 }
1592
1593 return -EOPNOTSUPP;
1594}
1595
Johannes Berg55682962007-09-20 13:09:35 -04001596static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1597{
Johannes Berg4c476992010-10-04 21:36:35 +02001598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001599 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001600 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001601 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001602 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001603 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001604 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001605
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001606 memset(&params, 0, sizeof(params));
1607
Johannes Berg04a773a2009-04-19 21:24:32 +02001608 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001609
Johannes Berg723b0382008-09-16 20:22:09 +02001610 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001611 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001612 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001613 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001614 if (ntype > NL80211_IFTYPE_MAX)
1615 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001616 }
1617
Johannes Berg92ffe052008-09-16 20:39:36 +02001618 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001619 struct wireless_dev *wdev = dev->ieee80211_ptr;
1620
Johannes Berg4c476992010-10-04 21:36:35 +02001621 if (ntype != NL80211_IFTYPE_MESH_POINT)
1622 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001623 if (netif_running(dev))
1624 return -EBUSY;
1625
1626 wdev_lock(wdev);
1627 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1628 IEEE80211_MAX_MESH_ID_LEN);
1629 wdev->mesh_id_up_len =
1630 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1631 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1632 wdev->mesh_id_up_len);
1633 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001634 }
1635
Felix Fietkau8b787642009-11-10 18:53:10 +01001636 if (info->attrs[NL80211_ATTR_4ADDR]) {
1637 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1638 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001639 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001640 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001641 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001642 } else {
1643 params.use_4addr = -1;
1644 }
1645
Johannes Berg92ffe052008-09-16 20:39:36 +02001646 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001647 if (ntype != NL80211_IFTYPE_MONITOR)
1648 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001649 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1650 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001651 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001652 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001653
1654 flags = &_flags;
1655 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001656 }
Johannes Berg3b858752009-03-12 09:55:09 +01001657
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001658 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001659 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001660 else
1661 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001662
Johannes Berg9bc383d2009-11-19 11:55:19 +01001663 if (!err && params.use_4addr != -1)
1664 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1665
Johannes Berg55682962007-09-20 13:09:35 -04001666 return err;
1667}
1668
1669static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1670{
Johannes Berg4c476992010-10-04 21:36:35 +02001671 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001672 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001673 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001674 int err;
1675 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001676 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001677
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001678 memset(&params, 0, sizeof(params));
1679
Johannes Berg55682962007-09-20 13:09:35 -04001680 if (!info->attrs[NL80211_ATTR_IFNAME])
1681 return -EINVAL;
1682
1683 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1684 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1685 if (type > NL80211_IFTYPE_MAX)
1686 return -EINVAL;
1687 }
1688
Johannes Berg79c97e92009-07-07 03:56:12 +02001689 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001690 !(rdev->wiphy.interface_modes & (1 << type)))
1691 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001692
Johannes Berg9bc383d2009-11-19 11:55:19 +01001693 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001694 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001695 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001696 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001697 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001698 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001699
Michael Wu66f7ac52008-01-31 19:48:22 +01001700 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1701 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1702 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001703 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001704 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001705 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001706 if (IS_ERR(dev))
1707 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001708
Johannes Berg29cbe682010-12-03 09:20:44 +01001709 if (type == NL80211_IFTYPE_MESH_POINT &&
1710 info->attrs[NL80211_ATTR_MESH_ID]) {
1711 struct wireless_dev *wdev = dev->ieee80211_ptr;
1712
1713 wdev_lock(wdev);
1714 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1715 IEEE80211_MAX_MESH_ID_LEN);
1716 wdev->mesh_id_up_len =
1717 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1718 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1719 wdev->mesh_id_up_len);
1720 wdev_unlock(wdev);
1721 }
1722
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001723 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001724}
1725
1726static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1727{
Johannes Berg4c476992010-10-04 21:36:35 +02001728 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1729 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001730
Johannes Berg4c476992010-10-04 21:36:35 +02001731 if (!rdev->ops->del_virtual_intf)
1732 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001733
Johannes Berg4c476992010-10-04 21:36:35 +02001734 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001735}
1736
Johannes Berg41ade002007-12-19 02:03:29 +01001737struct get_key_cookie {
1738 struct sk_buff *msg;
1739 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001740 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001741};
1742
1743static void get_key_callback(void *c, struct key_params *params)
1744{
Johannes Bergb9454e82009-07-08 13:29:08 +02001745 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001746 struct get_key_cookie *cookie = c;
1747
1748 if (params->key)
1749 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1750 params->key_len, params->key);
1751
1752 if (params->seq)
1753 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1754 params->seq_len, params->seq);
1755
1756 if (params->cipher)
1757 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1758 params->cipher);
1759
Johannes Bergb9454e82009-07-08 13:29:08 +02001760 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1761 if (!key)
1762 goto nla_put_failure;
1763
1764 if (params->key)
1765 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1766 params->key_len, params->key);
1767
1768 if (params->seq)
1769 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1770 params->seq_len, params->seq);
1771
1772 if (params->cipher)
1773 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1774 params->cipher);
1775
1776 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1777
1778 nla_nest_end(cookie->msg, key);
1779
Johannes Berg41ade002007-12-19 02:03:29 +01001780 return;
1781 nla_put_failure:
1782 cookie->error = 1;
1783}
1784
1785static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1786{
Johannes Berg4c476992010-10-04 21:36:35 +02001787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001788 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001789 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001790 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001791 const u8 *mac_addr = NULL;
1792 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001793 struct get_key_cookie cookie = {
1794 .error = 0,
1795 };
1796 void *hdr;
1797 struct sk_buff *msg;
1798
1799 if (info->attrs[NL80211_ATTR_KEY_IDX])
1800 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1801
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001802 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001803 return -EINVAL;
1804
1805 if (info->attrs[NL80211_ATTR_MAC])
1806 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1807
Johannes Berge31b8212010-10-05 19:39:30 +02001808 pairwise = !!mac_addr;
1809 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1810 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1811 if (kt >= NUM_NL80211_KEYTYPES)
1812 return -EINVAL;
1813 if (kt != NL80211_KEYTYPE_GROUP &&
1814 kt != NL80211_KEYTYPE_PAIRWISE)
1815 return -EINVAL;
1816 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1817 }
1818
Johannes Berg4c476992010-10-04 21:36:35 +02001819 if (!rdev->ops->get_key)
1820 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001821
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001823 if (!msg)
1824 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001825
1826 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1827 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001828 if (IS_ERR(hdr))
1829 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001830
1831 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001832 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001833
1834 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1835 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1836 if (mac_addr)
1837 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1838
Johannes Berge31b8212010-10-05 19:39:30 +02001839 if (pairwise && mac_addr &&
1840 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1841 return -ENOENT;
1842
1843 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1844 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001845
1846 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001847 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001848
1849 if (cookie.error)
1850 goto nla_put_failure;
1851
1852 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001853 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001854
1855 nla_put_failure:
1856 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001857 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001858 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001859 return err;
1860}
1861
1862static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1863{
Johannes Berg4c476992010-10-04 21:36:35 +02001864 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001865 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001866 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001867 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001868
Johannes Bergb9454e82009-07-08 13:29:08 +02001869 err = nl80211_parse_key(info, &key);
1870 if (err)
1871 return err;
1872
1873 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001874 return -EINVAL;
1875
Johannes Bergb9454e82009-07-08 13:29:08 +02001876 /* only support setting default key */
1877 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001878 return -EINVAL;
1879
Johannes Bergfffd0932009-07-08 14:22:54 +02001880 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001881
1882 if (key.def) {
1883 if (!rdev->ops->set_default_key) {
1884 err = -EOPNOTSUPP;
1885 goto out;
1886 }
1887
1888 err = nl80211_key_allowed(dev->ieee80211_ptr);
1889 if (err)
1890 goto out;
1891
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001892 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1893 key.def_uni, key.def_multi);
1894
1895 if (err)
1896 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001897
Johannes Berg3d23e342009-09-29 23:27:28 +02001898#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001899 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001900#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001901 } else {
1902 if (key.def_uni || !key.def_multi) {
1903 err = -EINVAL;
1904 goto out;
1905 }
1906
1907 if (!rdev->ops->set_default_mgmt_key) {
1908 err = -EOPNOTSUPP;
1909 goto out;
1910 }
1911
1912 err = nl80211_key_allowed(dev->ieee80211_ptr);
1913 if (err)
1914 goto out;
1915
1916 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1917 dev, key.idx);
1918 if (err)
1919 goto out;
1920
1921#ifdef CONFIG_CFG80211_WEXT
1922 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1923#endif
1924 }
1925
1926 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001927 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001928
Johannes Berg41ade002007-12-19 02:03:29 +01001929 return err;
1930}
1931
1932static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1933{
Johannes Berg4c476992010-10-04 21:36:35 +02001934 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001935 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001936 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001937 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001938 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001939
Johannes Bergb9454e82009-07-08 13:29:08 +02001940 err = nl80211_parse_key(info, &key);
1941 if (err)
1942 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001943
Johannes Bergb9454e82009-07-08 13:29:08 +02001944 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001945 return -EINVAL;
1946
Johannes Berg41ade002007-12-19 02:03:29 +01001947 if (info->attrs[NL80211_ATTR_MAC])
1948 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1949
Johannes Berge31b8212010-10-05 19:39:30 +02001950 if (key.type == -1) {
1951 if (mac_addr)
1952 key.type = NL80211_KEYTYPE_PAIRWISE;
1953 else
1954 key.type = NL80211_KEYTYPE_GROUP;
1955 }
1956
1957 /* for now */
1958 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1959 key.type != NL80211_KEYTYPE_GROUP)
1960 return -EINVAL;
1961
Johannes Berg4c476992010-10-04 21:36:35 +02001962 if (!rdev->ops->add_key)
1963 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001964
Johannes Berge31b8212010-10-05 19:39:30 +02001965 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1966 key.type == NL80211_KEYTYPE_PAIRWISE,
1967 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001968 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001969
1970 wdev_lock(dev->ieee80211_ptr);
1971 err = nl80211_key_allowed(dev->ieee80211_ptr);
1972 if (!err)
1973 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001974 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001975 mac_addr, &key.p);
1976 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001977
Johannes Berg41ade002007-12-19 02:03:29 +01001978 return err;
1979}
1980
1981static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1982{
Johannes Berg4c476992010-10-04 21:36:35 +02001983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001984 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001985 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001986 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001987 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001988
Johannes Bergb9454e82009-07-08 13:29:08 +02001989 err = nl80211_parse_key(info, &key);
1990 if (err)
1991 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001992
1993 if (info->attrs[NL80211_ATTR_MAC])
1994 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1995
Johannes Berge31b8212010-10-05 19:39:30 +02001996 if (key.type == -1) {
1997 if (mac_addr)
1998 key.type = NL80211_KEYTYPE_PAIRWISE;
1999 else
2000 key.type = NL80211_KEYTYPE_GROUP;
2001 }
2002
2003 /* for now */
2004 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2005 key.type != NL80211_KEYTYPE_GROUP)
2006 return -EINVAL;
2007
Johannes Berg4c476992010-10-04 21:36:35 +02002008 if (!rdev->ops->del_key)
2009 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002010
Johannes Bergfffd0932009-07-08 14:22:54 +02002011 wdev_lock(dev->ieee80211_ptr);
2012 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002013
2014 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2015 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2016 err = -ENOENT;
2017
Johannes Bergfffd0932009-07-08 14:22:54 +02002018 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002019 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2020 key.type == NL80211_KEYTYPE_PAIRWISE,
2021 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002022
Johannes Berg3d23e342009-09-29 23:27:28 +02002023#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002024 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002025 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002026 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002027 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002028 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2029 }
2030#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002031 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002032
Johannes Berg41ade002007-12-19 02:03:29 +01002033 return err;
2034}
2035
Johannes Berged1b6cc2007-12-19 02:03:32 +01002036static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2037{
2038 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2039 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2041 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002042 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002043 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002044 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002045
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002046 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2047 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2048 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2049 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002050 return -EINVAL;
2051
Johannes Berg074ac8d2010-09-16 14:58:22 +02002052 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002053 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2054 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002055
Johannes Berg56d18932011-05-09 18:41:15 +02002056 memset(&params, 0, sizeof(params));
2057
Johannes Berged1b6cc2007-12-19 02:03:32 +01002058 switch (info->genlhdr->cmd) {
2059 case NL80211_CMD_NEW_BEACON:
2060 /* these are required for NEW_BEACON */
2061 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2062 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002063 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2064 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002065
Johannes Berg56d18932011-05-09 18:41:15 +02002066 params.interval =
2067 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2068 params.dtim_period =
2069 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2070
2071 err = cfg80211_validate_beacon_int(rdev, params.interval);
2072 if (err)
2073 return err;
2074
Jouni Malinen32e9de82011-08-10 23:53:31 +03002075 /*
2076 * In theory, some of these attributes could be required for
2077 * NEW_BEACON, but since they were not used when the command was
2078 * originally added, keep them optional for old user space
2079 * programs to work with drivers that do not need the additional
2080 * information.
2081 */
2082 if (info->attrs[NL80211_ATTR_SSID]) {
2083 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2084 params.ssid_len =
2085 nla_len(info->attrs[NL80211_ATTR_SSID]);
2086 if (params.ssid_len == 0 ||
2087 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2088 return -EINVAL;
2089 }
2090
2091 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2092 params.hidden_ssid = nla_get_u32(
2093 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2094 if (params.hidden_ssid !=
2095 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2096 params.hidden_ssid !=
2097 NL80211_HIDDEN_SSID_ZERO_LEN &&
2098 params.hidden_ssid !=
2099 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2100 return -EINVAL;
2101 }
2102
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002103 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2104
2105 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2106 params.auth_type = nla_get_u32(
2107 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2108 if (!nl80211_valid_auth_type(params.auth_type))
2109 return -EINVAL;
2110 } else
2111 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2112
2113 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2114 NL80211_MAX_NR_CIPHER_SUITES);
2115 if (err)
2116 return err;
2117
Johannes Berg79c97e92009-07-07 03:56:12 +02002118 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002119 break;
2120 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002121 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002122 break;
2123 default:
2124 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002125 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002126 }
2127
Johannes Berg4c476992010-10-04 21:36:35 +02002128 if (!call)
2129 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002130
Johannes Berged1b6cc2007-12-19 02:03:32 +01002131 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2132 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2133 params.head_len =
2134 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2135 haveinfo = 1;
2136 }
2137
2138 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2139 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2140 params.tail_len =
2141 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2142 haveinfo = 1;
2143 }
2144
Johannes Berg4c476992010-10-04 21:36:35 +02002145 if (!haveinfo)
2146 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002147
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002148 if (info->attrs[NL80211_ATTR_IE]) {
2149 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2150 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2151 }
2152
2153 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2154 params.proberesp_ies =
2155 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2156 params.proberesp_ies_len =
2157 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2158 }
2159
2160 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2161 params.assocresp_ies =
2162 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2163 params.assocresp_ies_len =
2164 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2165 }
2166
Johannes Berg56d18932011-05-09 18:41:15 +02002167 err = call(&rdev->wiphy, dev, &params);
2168 if (!err && params.interval)
2169 wdev->beacon_interval = params.interval;
2170 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002171}
2172
2173static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2174{
Johannes Berg4c476992010-10-04 21:36:35 +02002175 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2176 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002177 struct wireless_dev *wdev = dev->ieee80211_ptr;
2178 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002179
Johannes Berg4c476992010-10-04 21:36:35 +02002180 if (!rdev->ops->del_beacon)
2181 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002182
Johannes Berg074ac8d2010-09-16 14:58:22 +02002183 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002184 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2185 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002186
Johannes Berg56d18932011-05-09 18:41:15 +02002187 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2188 if (!err)
2189 wdev->beacon_interval = 0;
2190 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002191}
2192
Johannes Berg5727ef12007-12-19 02:03:34 +01002193static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2194 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2195 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2196 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002197 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002198 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002199};
2200
Johannes Bergeccb8e82009-05-11 21:57:56 +03002201static int parse_station_flags(struct genl_info *info,
2202 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002203{
2204 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002205 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002206 int flag;
2207
Johannes Bergeccb8e82009-05-11 21:57:56 +03002208 /*
2209 * Try parsing the new attribute first so userspace
2210 * can specify both for older kernels.
2211 */
2212 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2213 if (nla) {
2214 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002215
Johannes Bergeccb8e82009-05-11 21:57:56 +03002216 sta_flags = nla_data(nla);
2217 params->sta_flags_mask = sta_flags->mask;
2218 params->sta_flags_set = sta_flags->set;
2219 if ((params->sta_flags_mask |
2220 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2221 return -EINVAL;
2222 return 0;
2223 }
2224
2225 /* if present, parse the old attribute */
2226
2227 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002228 if (!nla)
2229 return 0;
2230
2231 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2232 nla, sta_flags_policy))
2233 return -EINVAL;
2234
Johannes Bergeccb8e82009-05-11 21:57:56 +03002235 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2236 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002237
2238 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2239 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002240 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002241
2242 return 0;
2243}
2244
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002245static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2246 int attr)
2247{
2248 struct nlattr *rate;
2249 u16 bitrate;
2250
2251 rate = nla_nest_start(msg, attr);
2252 if (!rate)
2253 goto nla_put_failure;
2254
2255 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2256 bitrate = cfg80211_calculate_bitrate(info);
2257 if (bitrate > 0)
2258 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2259
2260 if (info->flags & RATE_INFO_FLAGS_MCS)
2261 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2262 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2263 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2264 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2265 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2266
2267 nla_nest_end(msg, rate);
2268 return true;
2269
2270nla_put_failure:
2271 return false;
2272}
2273
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002274static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2275 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002276 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002277{
2278 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002279 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002280
2281 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2282 if (!hdr)
2283 return -1;
2284
2285 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2286 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2287
Johannes Bergf5ea9122009-08-07 16:17:38 +02002288 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2289
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002290 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2291 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002292 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302293 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2294 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2295 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002296 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2297 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2298 sinfo->inactive_time);
2299 if (sinfo->filled & STATION_INFO_RX_BYTES)
2300 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2301 sinfo->rx_bytes);
2302 if (sinfo->filled & STATION_INFO_TX_BYTES)
2303 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2304 sinfo->tx_bytes);
2305 if (sinfo->filled & STATION_INFO_LLID)
2306 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2307 sinfo->llid);
2308 if (sinfo->filled & STATION_INFO_PLID)
2309 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2310 sinfo->plid);
2311 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2312 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2313 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002314 if (sinfo->filled & STATION_INFO_SIGNAL)
2315 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2316 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002317 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2318 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2319 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002320 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002321 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2322 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002323 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002324 }
2325 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2326 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2327 NL80211_STA_INFO_RX_BITRATE))
2328 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002329 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002330 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2331 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2332 sinfo->rx_packets);
2333 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2334 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2335 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002336 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2337 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2338 sinfo->tx_retries);
2339 if (sinfo->filled & STATION_INFO_TX_FAILED)
2340 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2341 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002342 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2343 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2344 if (!bss_param)
2345 goto nla_put_failure;
2346
2347 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2348 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2349 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2350 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2351 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2352 NLA_PUT_FLAG(msg,
2353 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2354 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2355 sinfo->bss_param.dtim_period);
2356 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2357 sinfo->bss_param.beacon_interval);
2358
2359 nla_nest_end(msg, bss_param);
2360 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002361 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2362 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2363 sizeof(struct nl80211_sta_flag_update),
2364 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002366
Felix Fietkau040bdf72011-08-10 19:00:33 -06002367 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002368 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2369 sinfo->assoc_req_ies);
2370
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002371 return genlmsg_end(msg, hdr);
2372
2373 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002374 genlmsg_cancel(msg, hdr);
2375 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002376}
2377
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002379 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002380{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002381 struct station_info sinfo;
2382 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002383 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002385 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002386 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387
Johannes Berg67748892010-10-04 21:14:06 +02002388 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2389 if (err)
2390 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002391
2392 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002393 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002394 goto out_err;
2395 }
2396
Johannes Bergbba95fe2008-07-29 13:22:51 +02002397 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002398 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002399 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2400 mac_addr, &sinfo);
2401 if (err == -ENOENT)
2402 break;
2403 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002404 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002405
2406 if (nl80211_send_station(skb,
2407 NETLINK_CB(cb->skb).pid,
2408 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2409 netdev, mac_addr,
2410 &sinfo) < 0)
2411 goto out;
2412
2413 sta_idx++;
2414 }
2415
2416
2417 out:
2418 cb->args[1] = sta_idx;
2419 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002420 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002421 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002422
2423 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002424}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002425
Johannes Berg5727ef12007-12-19 02:03:34 +01002426static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2427{
Johannes Berg4c476992010-10-04 21:36:35 +02002428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2429 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002430 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002431 struct sk_buff *msg;
2432 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002433 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002434
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002435 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002436
2437 if (!info->attrs[NL80211_ATTR_MAC])
2438 return -EINVAL;
2439
2440 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2441
Johannes Berg4c476992010-10-04 21:36:35 +02002442 if (!rdev->ops->get_station)
2443 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002444
Johannes Berg79c97e92009-07-07 03:56:12 +02002445 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002446 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002447 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002448
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002449 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002450 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002451 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002452
2453 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002454 dev, mac_addr, &sinfo) < 0) {
2455 nlmsg_free(msg);
2456 return -ENOBUFS;
2457 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002458
Johannes Berg4c476992010-10-04 21:36:35 +02002459 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002460}
2461
2462/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002463 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002464 */
Johannes Berg463d0182009-07-14 00:33:35 +02002465static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002466 struct cfg80211_registered_device *rdev,
2467 struct net_device **vlan)
2468{
Johannes Berg463d0182009-07-14 00:33:35 +02002469 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002470 *vlan = NULL;
2471
2472 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002473 *vlan = dev_get_by_index(genl_info_net(info),
2474 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002475 if (!*vlan)
2476 return -ENODEV;
2477 if (!(*vlan)->ieee80211_ptr)
2478 return -EINVAL;
2479 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2480 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002481 if (!netif_running(*vlan))
2482 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002483 }
2484 return 0;
2485}
2486
2487static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2488{
Johannes Berg4c476992010-10-04 21:36:35 +02002489 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002490 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002491 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002492 struct station_parameters params;
2493 u8 *mac_addr = NULL;
2494
2495 memset(&params, 0, sizeof(params));
2496
2497 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002498 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002499
2500 if (info->attrs[NL80211_ATTR_STA_AID])
2501 return -EINVAL;
2502
2503 if (!info->attrs[NL80211_ATTR_MAC])
2504 return -EINVAL;
2505
2506 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2507
2508 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2509 params.supported_rates =
2510 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2511 params.supported_rates_len =
2512 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2513 }
2514
2515 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2516 params.listen_interval =
2517 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2518
Jouni Malinen36aedc92008-08-25 11:58:58 +03002519 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2520 params.ht_capa =
2521 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2522
Johannes Bergeccb8e82009-05-11 21:57:56 +03002523 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002524 return -EINVAL;
2525
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002526 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2527 params.plink_action =
2528 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2529
Javier Cardona9c3990a2011-05-03 16:57:11 -07002530 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2531 params.plink_state =
2532 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2533
Johannes Berg463d0182009-07-14 00:33:35 +02002534 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002535 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002536 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002537
2538 /* validate settings */
2539 err = 0;
2540
2541 switch (dev->ieee80211_ptr->iftype) {
2542 case NL80211_IFTYPE_AP:
2543 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002544 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002545 /* disallow mesh-specific things */
2546 if (params.plink_action)
2547 err = -EINVAL;
2548 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002549 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002550 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002551 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002552 if (params.plink_action)
2553 err = -EINVAL;
2554 if (params.vlan)
2555 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002556 if (params.supported_rates &&
2557 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002558 err = -EINVAL;
2559 if (params.ht_capa)
2560 err = -EINVAL;
2561 if (params.listen_interval >= 0)
2562 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002563 if (params.sta_flags_mask &
2564 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2565 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2566 err = -EINVAL;
2567 /* can't change the TDLS bit */
2568 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2569 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002570 err = -EINVAL;
2571 break;
2572 case NL80211_IFTYPE_MESH_POINT:
2573 /* disallow things mesh doesn't support */
2574 if (params.vlan)
2575 err = -EINVAL;
2576 if (params.ht_capa)
2577 err = -EINVAL;
2578 if (params.listen_interval >= 0)
2579 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002580 if (params.sta_flags_mask &
2581 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002582 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002583 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002584 err = -EINVAL;
2585 break;
2586 default:
2587 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002588 }
2589
Johannes Berg5727ef12007-12-19 02:03:34 +01002590 if (err)
2591 goto out;
2592
Johannes Berg79c97e92009-07-07 03:56:12 +02002593 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002594 err = -EOPNOTSUPP;
2595 goto out;
2596 }
2597
Johannes Berg79c97e92009-07-07 03:56:12 +02002598 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002599
2600 out:
2601 if (params.vlan)
2602 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002603
Johannes Berg5727ef12007-12-19 02:03:34 +01002604 return err;
2605}
2606
Eliad Pellerc75786c2011-08-23 14:37:46 +03002607static struct nla_policy
2608nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2609 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2610 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2611};
2612
Johannes Berg5727ef12007-12-19 02:03:34 +01002613static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2614{
Johannes Berg4c476992010-10-04 21:36:35 +02002615 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002616 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002617 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002618 struct station_parameters params;
2619 u8 *mac_addr = NULL;
2620
2621 memset(&params, 0, sizeof(params));
2622
2623 if (!info->attrs[NL80211_ATTR_MAC])
2624 return -EINVAL;
2625
Johannes Berg5727ef12007-12-19 02:03:34 +01002626 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2627 return -EINVAL;
2628
2629 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2630 return -EINVAL;
2631
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002632 if (!info->attrs[NL80211_ATTR_STA_AID])
2633 return -EINVAL;
2634
Johannes Berg5727ef12007-12-19 02:03:34 +01002635 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2636 params.supported_rates =
2637 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2638 params.supported_rates_len =
2639 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2640 params.listen_interval =
2641 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002642
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002643 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2644 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2645 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002646
Jouni Malinen36aedc92008-08-25 11:58:58 +03002647 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2648 params.ht_capa =
2649 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002650
Javier Cardona96b78df2011-04-07 15:08:33 -07002651 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2652 params.plink_action =
2653 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2654
Johannes Bergeccb8e82009-05-11 21:57:56 +03002655 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002656 return -EINVAL;
2657
Eliad Pellerc75786c2011-08-23 14:37:46 +03002658 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002659 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002660 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002661 info->attrs[NL80211_ATTR_STA_WME]) {
2662 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2663 struct nlattr *nla;
2664
2665 nla = info->attrs[NL80211_ATTR_STA_WME];
2666 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2667 nl80211_sta_wme_policy);
2668 if (err)
2669 return err;
2670
2671 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2672 params.uapsd_queues =
2673 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002674 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2675 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002676
2677 if (tb[NL80211_STA_WME_MAX_SP])
2678 params.max_sp =
2679 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002680
2681 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2682 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002683
2684 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002685 }
2686
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002687 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002688 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002689 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002690 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2691 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2692 return -EINVAL;
2693
2694 /*
2695 * Only managed stations can add TDLS peers, and only when the
2696 * wiphy supports external TDLS setup.
2697 */
2698 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2699 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2700 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2701 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002702 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002703
Johannes Berg463d0182009-07-14 00:33:35 +02002704 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002705 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002706 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002707
2708 /* validate settings */
2709 err = 0;
2710
Johannes Berg79c97e92009-07-07 03:56:12 +02002711 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002712 err = -EOPNOTSUPP;
2713 goto out;
2714 }
2715
Johannes Berg79c97e92009-07-07 03:56:12 +02002716 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002717
2718 out:
2719 if (params.vlan)
2720 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002721 return err;
2722}
2723
2724static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2725{
Johannes Berg4c476992010-10-04 21:36:35 +02002726 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2727 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002728 u8 *mac_addr = NULL;
2729
2730 if (info->attrs[NL80211_ATTR_MAC])
2731 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2732
Johannes Berge80cf852009-05-11 14:43:13 +02002733 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002734 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002735 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002736 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2737 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002738
Johannes Berg4c476992010-10-04 21:36:35 +02002739 if (!rdev->ops->del_station)
2740 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002741
Johannes Berg4c476992010-10-04 21:36:35 +02002742 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002743}
2744
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002745static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2746 int flags, struct net_device *dev,
2747 u8 *dst, u8 *next_hop,
2748 struct mpath_info *pinfo)
2749{
2750 void *hdr;
2751 struct nlattr *pinfoattr;
2752
2753 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2754 if (!hdr)
2755 return -1;
2756
2757 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2758 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2759 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2760
Johannes Bergf5ea9122009-08-07 16:17:38 +02002761 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2762
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002763 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2764 if (!pinfoattr)
2765 goto nla_put_failure;
2766 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2767 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2768 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002769 if (pinfo->filled & MPATH_INFO_SN)
2770 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2771 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002772 if (pinfo->filled & MPATH_INFO_METRIC)
2773 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2774 pinfo->metric);
2775 if (pinfo->filled & MPATH_INFO_EXPTIME)
2776 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2777 pinfo->exptime);
2778 if (pinfo->filled & MPATH_INFO_FLAGS)
2779 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2780 pinfo->flags);
2781 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2782 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2783 pinfo->discovery_timeout);
2784 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2785 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2786 pinfo->discovery_retries);
2787
2788 nla_nest_end(msg, pinfoattr);
2789
2790 return genlmsg_end(msg, hdr);
2791
2792 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002793 genlmsg_cancel(msg, hdr);
2794 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002795}
2796
2797static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002798 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002799{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002800 struct mpath_info pinfo;
2801 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002802 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002803 u8 dst[ETH_ALEN];
2804 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002805 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002806 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002807
Johannes Berg67748892010-10-04 21:14:06 +02002808 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2809 if (err)
2810 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002811
2812 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002813 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002814 goto out_err;
2815 }
2816
Jouni Malineneec60b02009-03-20 21:21:19 +02002817 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2818 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002819 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002820 }
2821
Johannes Bergbba95fe2008-07-29 13:22:51 +02002822 while (1) {
2823 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2824 dst, next_hop, &pinfo);
2825 if (err == -ENOENT)
2826 break;
2827 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002828 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002829
2830 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2831 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2832 netdev, dst, next_hop,
2833 &pinfo) < 0)
2834 goto out;
2835
2836 path_idx++;
2837 }
2838
2839
2840 out:
2841 cb->args[1] = path_idx;
2842 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002843 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002844 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002845 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002846}
2847
2848static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2849{
Johannes Berg4c476992010-10-04 21:36:35 +02002850 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002851 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002852 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002853 struct mpath_info pinfo;
2854 struct sk_buff *msg;
2855 u8 *dst = NULL;
2856 u8 next_hop[ETH_ALEN];
2857
2858 memset(&pinfo, 0, sizeof(pinfo));
2859
2860 if (!info->attrs[NL80211_ATTR_MAC])
2861 return -EINVAL;
2862
2863 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2864
Johannes Berg4c476992010-10-04 21:36:35 +02002865 if (!rdev->ops->get_mpath)
2866 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002867
Johannes Berg4c476992010-10-04 21:36:35 +02002868 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2869 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002870
Johannes Berg79c97e92009-07-07 03:56:12 +02002871 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002872 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002873 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002874
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002875 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002876 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002877 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002878
2879 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002880 dev, dst, next_hop, &pinfo) < 0) {
2881 nlmsg_free(msg);
2882 return -ENOBUFS;
2883 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002884
Johannes Berg4c476992010-10-04 21:36:35 +02002885 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002886}
2887
2888static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2889{
Johannes Berg4c476992010-10-04 21:36:35 +02002890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2891 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002892 u8 *dst = NULL;
2893 u8 *next_hop = NULL;
2894
2895 if (!info->attrs[NL80211_ATTR_MAC])
2896 return -EINVAL;
2897
2898 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2899 return -EINVAL;
2900
2901 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2902 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2903
Johannes Berg4c476992010-10-04 21:36:35 +02002904 if (!rdev->ops->change_mpath)
2905 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002906
Johannes Berg4c476992010-10-04 21:36:35 +02002907 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2908 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002909
Johannes Berg4c476992010-10-04 21:36:35 +02002910 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002911}
Johannes Berg4c476992010-10-04 21:36:35 +02002912
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002913static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2914{
Johannes Berg4c476992010-10-04 21:36:35 +02002915 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2916 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002917 u8 *dst = NULL;
2918 u8 *next_hop = NULL;
2919
2920 if (!info->attrs[NL80211_ATTR_MAC])
2921 return -EINVAL;
2922
2923 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2924 return -EINVAL;
2925
2926 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2927 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2928
Johannes Berg4c476992010-10-04 21:36:35 +02002929 if (!rdev->ops->add_mpath)
2930 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002931
Johannes Berg4c476992010-10-04 21:36:35 +02002932 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2933 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002934
Johannes Berg4c476992010-10-04 21:36:35 +02002935 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002936}
2937
2938static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2939{
Johannes Berg4c476992010-10-04 21:36:35 +02002940 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2941 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002942 u8 *dst = NULL;
2943
2944 if (info->attrs[NL80211_ATTR_MAC])
2945 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2946
Johannes Berg4c476992010-10-04 21:36:35 +02002947 if (!rdev->ops->del_mpath)
2948 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002949
Johannes Berg4c476992010-10-04 21:36:35 +02002950 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002951}
2952
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002953static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2954{
Johannes Berg4c476992010-10-04 21:36:35 +02002955 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2956 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002957 struct bss_parameters params;
2958
2959 memset(&params, 0, sizeof(params));
2960 /* default to not changing parameters */
2961 params.use_cts_prot = -1;
2962 params.use_short_preamble = -1;
2963 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002964 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002965 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002966
2967 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2968 params.use_cts_prot =
2969 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2970 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2971 params.use_short_preamble =
2972 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2973 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2974 params.use_short_slot_time =
2975 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002976 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2977 params.basic_rates =
2978 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2979 params.basic_rates_len =
2980 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2981 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002982 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2983 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002984 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2985 params.ht_opmode =
2986 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002987
Johannes Berg4c476992010-10-04 21:36:35 +02002988 if (!rdev->ops->change_bss)
2989 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002990
Johannes Berg074ac8d2010-09-16 14:58:22 +02002991 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002992 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2993 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002994
Johannes Berg4c476992010-10-04 21:36:35 +02002995 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002996}
2997
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002998static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002999 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3000 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3001 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3002 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3003 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3004 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3005};
3006
3007static int parse_reg_rule(struct nlattr *tb[],
3008 struct ieee80211_reg_rule *reg_rule)
3009{
3010 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3011 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3012
3013 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3014 return -EINVAL;
3015 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3016 return -EINVAL;
3017 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3018 return -EINVAL;
3019 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3020 return -EINVAL;
3021 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3022 return -EINVAL;
3023
3024 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3025
3026 freq_range->start_freq_khz =
3027 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3028 freq_range->end_freq_khz =
3029 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3030 freq_range->max_bandwidth_khz =
3031 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3032
3033 power_rule->max_eirp =
3034 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3035
3036 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3037 power_rule->max_antenna_gain =
3038 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3039
3040 return 0;
3041}
3042
3043static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3044{
3045 int r;
3046 char *data = NULL;
3047
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003048 /*
3049 * You should only get this when cfg80211 hasn't yet initialized
3050 * completely when built-in to the kernel right between the time
3051 * window between nl80211_init() and regulatory_init(), if that is
3052 * even possible.
3053 */
3054 mutex_lock(&cfg80211_mutex);
3055 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003056 mutex_unlock(&cfg80211_mutex);
3057 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003058 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003059 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003060
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003061 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3062 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003063
3064 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3065
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003066 r = regulatory_hint_user(data);
3067
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003068 return r;
3069}
3070
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003071static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003072 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003073{
Johannes Berg4c476992010-10-04 21:36:35 +02003074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003075 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003076 struct wireless_dev *wdev = dev->ieee80211_ptr;
3077 struct mesh_config cur_params;
3078 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003079 void *hdr;
3080 struct nlattr *pinfoattr;
3081 struct sk_buff *msg;
3082
Johannes Berg29cbe682010-12-03 09:20:44 +01003083 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3084 return -EOPNOTSUPP;
3085
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003086 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003087 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003088
Johannes Berg29cbe682010-12-03 09:20:44 +01003089 wdev_lock(wdev);
3090 /* If not connected, get default parameters */
3091 if (!wdev->mesh_id_len)
3092 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3093 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003094 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003095 &cur_params);
3096 wdev_unlock(wdev);
3097
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003098 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003099 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003100
3101 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003103 if (!msg)
3104 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003105 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003106 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003107 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003108 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003109 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003110 if (!pinfoattr)
3111 goto nla_put_failure;
3112 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3113 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3114 cur_params.dot11MeshRetryTimeout);
3115 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3116 cur_params.dot11MeshConfirmTimeout);
3117 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3118 cur_params.dot11MeshHoldingTimeout);
3119 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3120 cur_params.dot11MeshMaxPeerLinks);
3121 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3122 cur_params.dot11MeshMaxRetries);
3123 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3124 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003125 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3126 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003127 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3128 cur_params.auto_open_plinks);
3129 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3130 cur_params.dot11MeshHWMPmaxPREQretries);
3131 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3132 cur_params.path_refresh_time);
3133 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3134 cur_params.min_discovery_timeout);
3135 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3136 cur_params.dot11MeshHWMPactivePathTimeout);
3137 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3138 cur_params.dot11MeshHWMPpreqMinInterval);
3139 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3140 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003141 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3142 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003143 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3144 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003145 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3146 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003147 nla_nest_end(msg, pinfoattr);
3148 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003149 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003150
Johannes Berg3b858752009-03-12 09:55:09 +01003151 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003152 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003153 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003154 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003155 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003156}
3157
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003158static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003159 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3160 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3161 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3162 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3163 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3164 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003165 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003166 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3167
3168 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3169 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3170 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3171 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3172 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3173 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003174 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003175 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003176 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003177};
3178
Javier Cardonac80d5452010-12-16 17:37:49 -08003179static const struct nla_policy
3180 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3181 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3182 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003183 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003184 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003185 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003186 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003187};
3188
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003189static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003190 struct mesh_config *cfg,
3191 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003192{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003193 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003194 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003195
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003196#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3197do {\
3198 if (table[attr_num]) {\
3199 cfg->param = nla_fn(table[attr_num]); \
3200 mask |= (1 << (attr_num - 1)); \
3201 } \
3202} while (0);\
3203
3204
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003205 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003206 return -EINVAL;
3207 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003208 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003209 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003210 return -EINVAL;
3211
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003212 /* This makes sure that there aren't more than 32 mesh config
3213 * parameters (otherwise our bitfield scheme would not work.) */
3214 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3215
3216 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003217 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3218 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3219 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3220 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3221 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3222 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3223 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3224 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3225 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3226 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3227 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3228 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003229 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3230 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003231 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3232 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3233 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3234 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3235 nla_get_u8);
3236 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3237 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3238 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3239 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3240 nla_get_u16);
3241 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3242 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3243 nla_get_u32);
3244 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3245 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3246 nla_get_u16);
3247 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3248 dot11MeshHWMPnetDiameterTraversalTime,
3249 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3250 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003251 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3252 dot11MeshHWMPRootMode, mask,
3253 NL80211_MESHCONF_HWMP_ROOTMODE,
3254 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003255 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3256 dot11MeshHWMPRannInterval, mask,
3257 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3258 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003259 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3260 dot11MeshGateAnnouncementProtocol, mask,
3261 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3262 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003263 if (mask_out)
3264 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003265
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003266 return 0;
3267
3268#undef FILL_IN_MESH_PARAM_IF_SET
3269}
3270
Javier Cardonac80d5452010-12-16 17:37:49 -08003271static int nl80211_parse_mesh_setup(struct genl_info *info,
3272 struct mesh_setup *setup)
3273{
3274 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3275
3276 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3277 return -EINVAL;
3278 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3279 info->attrs[NL80211_ATTR_MESH_SETUP],
3280 nl80211_mesh_setup_params_policy))
3281 return -EINVAL;
3282
3283 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3284 setup->path_sel_proto =
3285 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3286 IEEE80211_PATH_PROTOCOL_VENDOR :
3287 IEEE80211_PATH_PROTOCOL_HWMP;
3288
3289 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3290 setup->path_metric =
3291 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3292 IEEE80211_PATH_METRIC_VENDOR :
3293 IEEE80211_PATH_METRIC_AIRTIME;
3294
Javier Cardona581a8b02011-04-07 15:08:27 -07003295
3296 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003297 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003298 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003299 if (!is_valid_ie_attr(ieattr))
3300 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003301 setup->ie = nla_data(ieattr);
3302 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003303 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003304 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3305 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003306
3307 return 0;
3308}
3309
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003310static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003311 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003312{
3313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3314 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003315 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003316 struct mesh_config cfg;
3317 u32 mask;
3318 int err;
3319
Johannes Berg29cbe682010-12-03 09:20:44 +01003320 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3321 return -EOPNOTSUPP;
3322
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003323 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003324 return -EOPNOTSUPP;
3325
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003326 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003327 if (err)
3328 return err;
3329
Johannes Berg29cbe682010-12-03 09:20:44 +01003330 wdev_lock(wdev);
3331 if (!wdev->mesh_id_len)
3332 err = -ENOLINK;
3333
3334 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003335 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003336 mask, &cfg);
3337
3338 wdev_unlock(wdev);
3339
3340 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003341}
3342
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003343static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3344{
3345 struct sk_buff *msg;
3346 void *hdr = NULL;
3347 struct nlattr *nl_reg_rules;
3348 unsigned int i;
3349 int err = -EINVAL;
3350
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003351 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003352
3353 if (!cfg80211_regdomain)
3354 goto out;
3355
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003356 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003357 if (!msg) {
3358 err = -ENOBUFS;
3359 goto out;
3360 }
3361
3362 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3363 NL80211_CMD_GET_REG);
3364 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003365 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003366
3367 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3368 cfg80211_regdomain->alpha2);
3369
3370 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3371 if (!nl_reg_rules)
3372 goto nla_put_failure;
3373
3374 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3375 struct nlattr *nl_reg_rule;
3376 const struct ieee80211_reg_rule *reg_rule;
3377 const struct ieee80211_freq_range *freq_range;
3378 const struct ieee80211_power_rule *power_rule;
3379
3380 reg_rule = &cfg80211_regdomain->reg_rules[i];
3381 freq_range = &reg_rule->freq_range;
3382 power_rule = &reg_rule->power_rule;
3383
3384 nl_reg_rule = nla_nest_start(msg, i);
3385 if (!nl_reg_rule)
3386 goto nla_put_failure;
3387
3388 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3389 reg_rule->flags);
3390 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3391 freq_range->start_freq_khz);
3392 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3393 freq_range->end_freq_khz);
3394 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3395 freq_range->max_bandwidth_khz);
3396 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3397 power_rule->max_antenna_gain);
3398 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3399 power_rule->max_eirp);
3400
3401 nla_nest_end(msg, nl_reg_rule);
3402 }
3403
3404 nla_nest_end(msg, nl_reg_rules);
3405
3406 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003407 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003408 goto out;
3409
3410nla_put_failure:
3411 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003412put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003413 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003414 err = -EMSGSIZE;
3415out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003416 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003417 return err;
3418}
3419
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003420static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3421{
3422 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3423 struct nlattr *nl_reg_rule;
3424 char *alpha2 = NULL;
3425 int rem_reg_rules = 0, r = 0;
3426 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3427 struct ieee80211_regdomain *rd = NULL;
3428
3429 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3430 return -EINVAL;
3431
3432 if (!info->attrs[NL80211_ATTR_REG_RULES])
3433 return -EINVAL;
3434
3435 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3436
3437 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3438 rem_reg_rules) {
3439 num_rules++;
3440 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003441 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003442 }
3443
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003444 mutex_lock(&cfg80211_mutex);
3445
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003446 if (!reg_is_valid_request(alpha2)) {
3447 r = -EINVAL;
3448 goto bad_reg;
3449 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003450
3451 size_of_regd = sizeof(struct ieee80211_regdomain) +
3452 (num_rules * sizeof(struct ieee80211_reg_rule));
3453
3454 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003455 if (!rd) {
3456 r = -ENOMEM;
3457 goto bad_reg;
3458 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003459
3460 rd->n_reg_rules = num_rules;
3461 rd->alpha2[0] = alpha2[0];
3462 rd->alpha2[1] = alpha2[1];
3463
3464 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3465 rem_reg_rules) {
3466 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3467 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3468 reg_rule_policy);
3469 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3470 if (r)
3471 goto bad_reg;
3472
3473 rule_idx++;
3474
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003475 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3476 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003477 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003478 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003479 }
3480
3481 BUG_ON(rule_idx != num_rules);
3482
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003483 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003484
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003485 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003486
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003487 return r;
3488
Johannes Bergd2372b32008-10-24 20:32:20 +02003489 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003490 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003491 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003492 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003493}
3494
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003495static int validate_scan_freqs(struct nlattr *freqs)
3496{
3497 struct nlattr *attr1, *attr2;
3498 int n_channels = 0, tmp1, tmp2;
3499
3500 nla_for_each_nested(attr1, freqs, tmp1) {
3501 n_channels++;
3502 /*
3503 * Some hardware has a limited channel list for
3504 * scanning, and it is pretty much nonsensical
3505 * to scan for a channel twice, so disallow that
3506 * and don't require drivers to check that the
3507 * channel list they get isn't longer than what
3508 * they can scan, as long as they can scan all
3509 * the channels they registered at once.
3510 */
3511 nla_for_each_nested(attr2, freqs, tmp2)
3512 if (attr1 != attr2 &&
3513 nla_get_u32(attr1) == nla_get_u32(attr2))
3514 return 0;
3515 }
3516
3517 return n_channels;
3518}
3519
Johannes Berg2a519312009-02-10 21:25:55 +01003520static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3521{
Johannes Berg4c476992010-10-04 21:36:35 +02003522 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3523 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003524 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003525 struct nlattr *attr;
3526 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003527 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003528 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003529
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003530 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3531 return -EINVAL;
3532
Johannes Berg79c97e92009-07-07 03:56:12 +02003533 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003534
Johannes Berg4c476992010-10-04 21:36:35 +02003535 if (!rdev->ops->scan)
3536 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003537
Johannes Berg4c476992010-10-04 21:36:35 +02003538 if (rdev->scan_req)
3539 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003540
3541 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003542 n_channels = validate_scan_freqs(
3543 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003544 if (!n_channels)
3545 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003546 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003547 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003548 n_channels = 0;
3549
Johannes Berg2a519312009-02-10 21:25:55 +01003550 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3551 if (wiphy->bands[band])
3552 n_channels += wiphy->bands[band]->n_channels;
3553 }
3554
3555 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3556 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3557 n_ssids++;
3558
Johannes Berg4c476992010-10-04 21:36:35 +02003559 if (n_ssids > wiphy->max_scan_ssids)
3560 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003561
Jouni Malinen70692ad2009-02-16 19:39:13 +02003562 if (info->attrs[NL80211_ATTR_IE])
3563 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3564 else
3565 ie_len = 0;
3566
Johannes Berg4c476992010-10-04 21:36:35 +02003567 if (ie_len > wiphy->max_scan_ie_len)
3568 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003569
Johannes Berg2a519312009-02-10 21:25:55 +01003570 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003571 + sizeof(*request->ssids) * n_ssids
3572 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003573 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003574 if (!request)
3575 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003576
Johannes Berg2a519312009-02-10 21:25:55 +01003577 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003578 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003579 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003580 if (ie_len) {
3581 if (request->ssids)
3582 request->ie = (void *)(request->ssids + n_ssids);
3583 else
3584 request->ie = (void *)(request->channels + n_channels);
3585 }
Johannes Berg2a519312009-02-10 21:25:55 +01003586
Johannes Berg584991d2009-11-02 13:32:03 +01003587 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003588 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3589 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003590 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003591 struct ieee80211_channel *chan;
3592
3593 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3594
3595 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003596 err = -EINVAL;
3597 goto out_free;
3598 }
Johannes Berg584991d2009-11-02 13:32:03 +01003599
3600 /* ignore disabled channels */
3601 if (chan->flags & IEEE80211_CHAN_DISABLED)
3602 continue;
3603
3604 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003605 i++;
3606 }
3607 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003608 enum ieee80211_band band;
3609
Johannes Berg2a519312009-02-10 21:25:55 +01003610 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003611 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3612 int j;
3613 if (!wiphy->bands[band])
3614 continue;
3615 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003616 struct ieee80211_channel *chan;
3617
3618 chan = &wiphy->bands[band]->channels[j];
3619
3620 if (chan->flags & IEEE80211_CHAN_DISABLED)
3621 continue;
3622
3623 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003624 i++;
3625 }
3626 }
3627 }
3628
Johannes Berg584991d2009-11-02 13:32:03 +01003629 if (!i) {
3630 err = -EINVAL;
3631 goto out_free;
3632 }
3633
3634 request->n_channels = i;
3635
Johannes Berg2a519312009-02-10 21:25:55 +01003636 i = 0;
3637 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3638 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003639 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003640 err = -EINVAL;
3641 goto out_free;
3642 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003643 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003644 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003645 i++;
3646 }
3647 }
3648
Jouni Malinen70692ad2009-02-16 19:39:13 +02003649 if (info->attrs[NL80211_ATTR_IE]) {
3650 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003651 memcpy((void *)request->ie,
3652 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003653 request->ie_len);
3654 }
3655
Johannes Berg34850ab2011-07-18 18:08:35 +02003656 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003657 if (wiphy->bands[i])
3658 request->rates[i] =
3659 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003660
3661 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3662 nla_for_each_nested(attr,
3663 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3664 tmp) {
3665 enum ieee80211_band band = nla_type(attr);
3666
Dan Carpenter84404622011-07-29 11:52:18 +03003667 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003668 err = -EINVAL;
3669 goto out_free;
3670 }
3671 err = ieee80211_get_ratemask(wiphy->bands[band],
3672 nla_data(attr),
3673 nla_len(attr),
3674 &request->rates[band]);
3675 if (err)
3676 goto out_free;
3677 }
3678 }
3679
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303680 request->no_cck =
3681 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3682
Johannes Berg463d0182009-07-14 00:33:35 +02003683 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003684 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003685
Johannes Berg79c97e92009-07-07 03:56:12 +02003686 rdev->scan_req = request;
3687 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003688
Johannes Berg463d0182009-07-14 00:33:35 +02003689 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003690 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003691 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003692 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003693 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003694 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003695 kfree(request);
3696 }
Johannes Berg3b858752009-03-12 09:55:09 +01003697
Johannes Berg2a519312009-02-10 21:25:55 +01003698 return err;
3699}
3700
Luciano Coelho807f8a82011-05-11 17:09:35 +03003701static int nl80211_start_sched_scan(struct sk_buff *skb,
3702 struct genl_info *info)
3703{
3704 struct cfg80211_sched_scan_request *request;
3705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3706 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003707 struct nlattr *attr;
3708 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003709 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003710 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003711 enum ieee80211_band band;
3712 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003713 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003714
3715 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3716 !rdev->ops->sched_scan_start)
3717 return -EOPNOTSUPP;
3718
3719 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3720 return -EINVAL;
3721
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003722 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3723 return -EINVAL;
3724
3725 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3726 if (interval == 0)
3727 return -EINVAL;
3728
Luciano Coelho807f8a82011-05-11 17:09:35 +03003729 wiphy = &rdev->wiphy;
3730
3731 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3732 n_channels = validate_scan_freqs(
3733 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3734 if (!n_channels)
3735 return -EINVAL;
3736 } else {
3737 n_channels = 0;
3738
3739 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3740 if (wiphy->bands[band])
3741 n_channels += wiphy->bands[band]->n_channels;
3742 }
3743
3744 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3745 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3746 tmp)
3747 n_ssids++;
3748
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003749 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003750 return -EINVAL;
3751
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003752 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3753 nla_for_each_nested(attr,
3754 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3755 tmp)
3756 n_match_sets++;
3757
3758 if (n_match_sets > wiphy->max_match_sets)
3759 return -EINVAL;
3760
Luciano Coelho807f8a82011-05-11 17:09:35 +03003761 if (info->attrs[NL80211_ATTR_IE])
3762 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3763 else
3764 ie_len = 0;
3765
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003766 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003767 return -EINVAL;
3768
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003769 mutex_lock(&rdev->sched_scan_mtx);
3770
3771 if (rdev->sched_scan_req) {
3772 err = -EINPROGRESS;
3773 goto out;
3774 }
3775
Luciano Coelho807f8a82011-05-11 17:09:35 +03003776 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003777 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003778 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003779 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003780 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003781 if (!request) {
3782 err = -ENOMEM;
3783 goto out;
3784 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003785
3786 if (n_ssids)
3787 request->ssids = (void *)&request->channels[n_channels];
3788 request->n_ssids = n_ssids;
3789 if (ie_len) {
3790 if (request->ssids)
3791 request->ie = (void *)(request->ssids + n_ssids);
3792 else
3793 request->ie = (void *)(request->channels + n_channels);
3794 }
3795
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003796 if (n_match_sets) {
3797 if (request->ie)
3798 request->match_sets = (void *)(request->ie + ie_len);
3799 else if (request->ssids)
3800 request->match_sets =
3801 (void *)(request->ssids + n_ssids);
3802 else
3803 request->match_sets =
3804 (void *)(request->channels + n_channels);
3805 }
3806 request->n_match_sets = n_match_sets;
3807
Luciano Coelho807f8a82011-05-11 17:09:35 +03003808 i = 0;
3809 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3810 /* user specified, bail out if channel not found */
3811 nla_for_each_nested(attr,
3812 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3813 tmp) {
3814 struct ieee80211_channel *chan;
3815
3816 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3817
3818 if (!chan) {
3819 err = -EINVAL;
3820 goto out_free;
3821 }
3822
3823 /* ignore disabled channels */
3824 if (chan->flags & IEEE80211_CHAN_DISABLED)
3825 continue;
3826
3827 request->channels[i] = chan;
3828 i++;
3829 }
3830 } else {
3831 /* all channels */
3832 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3833 int j;
3834 if (!wiphy->bands[band])
3835 continue;
3836 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3837 struct ieee80211_channel *chan;
3838
3839 chan = &wiphy->bands[band]->channels[j];
3840
3841 if (chan->flags & IEEE80211_CHAN_DISABLED)
3842 continue;
3843
3844 request->channels[i] = chan;
3845 i++;
3846 }
3847 }
3848 }
3849
3850 if (!i) {
3851 err = -EINVAL;
3852 goto out_free;
3853 }
3854
3855 request->n_channels = i;
3856
3857 i = 0;
3858 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3859 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3860 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003861 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003862 err = -EINVAL;
3863 goto out_free;
3864 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003865 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003866 memcpy(request->ssids[i].ssid, nla_data(attr),
3867 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003868 i++;
3869 }
3870 }
3871
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003872 i = 0;
3873 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3874 nla_for_each_nested(attr,
3875 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3876 tmp) {
3877 struct nlattr *ssid;
3878
3879 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3880 nla_data(attr), nla_len(attr),
3881 nl80211_match_policy);
3882 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3883 if (ssid) {
3884 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3885 err = -EINVAL;
3886 goto out_free;
3887 }
3888 memcpy(request->match_sets[i].ssid.ssid,
3889 nla_data(ssid), nla_len(ssid));
3890 request->match_sets[i].ssid.ssid_len =
3891 nla_len(ssid);
3892 }
3893 i++;
3894 }
3895 }
3896
Luciano Coelho807f8a82011-05-11 17:09:35 +03003897 if (info->attrs[NL80211_ATTR_IE]) {
3898 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3899 memcpy((void *)request->ie,
3900 nla_data(info->attrs[NL80211_ATTR_IE]),
3901 request->ie_len);
3902 }
3903
3904 request->dev = dev;
3905 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003906 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003907
3908 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3909 if (!err) {
3910 rdev->sched_scan_req = request;
3911 nl80211_send_sched_scan(rdev, dev,
3912 NL80211_CMD_START_SCHED_SCAN);
3913 goto out;
3914 }
3915
3916out_free:
3917 kfree(request);
3918out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003919 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003920 return err;
3921}
3922
3923static int nl80211_stop_sched_scan(struct sk_buff *skb,
3924 struct genl_info *info)
3925{
3926 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003927 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003928
3929 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3930 !rdev->ops->sched_scan_stop)
3931 return -EOPNOTSUPP;
3932
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003933 mutex_lock(&rdev->sched_scan_mtx);
3934 err = __cfg80211_stop_sched_scan(rdev, false);
3935 mutex_unlock(&rdev->sched_scan_mtx);
3936
3937 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003938}
3939
Johannes Berg9720bb32011-06-21 09:45:33 +02003940static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3941 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003942 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003943 struct wireless_dev *wdev,
3944 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003945{
Johannes Berg48ab9052009-07-10 18:42:31 +02003946 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003947 void *hdr;
3948 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003949 int i;
3950
3951 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003952
Johannes Berg9720bb32011-06-21 09:45:33 +02003953 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003954 NL80211_CMD_NEW_SCAN_RESULTS);
3955 if (!hdr)
3956 return -1;
3957
Johannes Berg9720bb32011-06-21 09:45:33 +02003958 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3959
Johannes Bergf5ea9122009-08-07 16:17:38 +02003960 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003961 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003962
3963 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3964 if (!bss)
3965 goto nla_put_failure;
3966 if (!is_zero_ether_addr(res->bssid))
3967 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3968 if (res->information_elements && res->len_information_elements)
3969 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3970 res->len_information_elements,
3971 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003972 if (res->beacon_ies && res->len_beacon_ies &&
3973 res->beacon_ies != res->information_elements)
3974 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3975 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003976 if (res->tsf)
3977 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3978 if (res->beacon_interval)
3979 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3980 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3981 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003982 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3983 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003984
Johannes Berg77965c92009-02-18 18:45:06 +01003985 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003986 case CFG80211_SIGNAL_TYPE_MBM:
3987 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3988 break;
3989 case CFG80211_SIGNAL_TYPE_UNSPEC:
3990 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3991 break;
3992 default:
3993 break;
3994 }
3995
Johannes Berg48ab9052009-07-10 18:42:31 +02003996 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003997 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003998 case NL80211_IFTYPE_STATION:
3999 if (intbss == wdev->current_bss)
4000 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4001 NL80211_BSS_STATUS_ASSOCIATED);
4002 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4003 if (intbss != wdev->auth_bsses[i])
4004 continue;
4005 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4006 NL80211_BSS_STATUS_AUTHENTICATED);
4007 break;
4008 }
4009 break;
4010 case NL80211_IFTYPE_ADHOC:
4011 if (intbss == wdev->current_bss)
4012 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4013 NL80211_BSS_STATUS_IBSS_JOINED);
4014 break;
4015 default:
4016 break;
4017 }
4018
Johannes Berg2a519312009-02-10 21:25:55 +01004019 nla_nest_end(msg, bss);
4020
4021 return genlmsg_end(msg, hdr);
4022
4023 nla_put_failure:
4024 genlmsg_cancel(msg, hdr);
4025 return -EMSGSIZE;
4026}
4027
4028static int nl80211_dump_scan(struct sk_buff *skb,
4029 struct netlink_callback *cb)
4030{
Johannes Berg48ab9052009-07-10 18:42:31 +02004031 struct cfg80211_registered_device *rdev;
4032 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004033 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004034 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004035 int start = cb->args[1], idx = 0;
4036 int err;
4037
Johannes Berg67748892010-10-04 21:14:06 +02004038 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4039 if (err)
4040 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004041
Johannes Berg48ab9052009-07-10 18:42:31 +02004042 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004043
Johannes Berg48ab9052009-07-10 18:42:31 +02004044 wdev_lock(wdev);
4045 spin_lock_bh(&rdev->bss_lock);
4046 cfg80211_bss_expire(rdev);
4047
Johannes Berg9720bb32011-06-21 09:45:33 +02004048 cb->seq = rdev->bss_generation;
4049
Johannes Berg48ab9052009-07-10 18:42:31 +02004050 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004051 if (++idx <= start)
4052 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004053 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004054 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004055 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004056 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004057 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004058 }
4059 }
4060
Johannes Berg48ab9052009-07-10 18:42:31 +02004061 spin_unlock_bh(&rdev->bss_lock);
4062 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004063
4064 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004065 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004066
Johannes Berg67748892010-10-04 21:14:06 +02004067 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004068}
4069
Holger Schurig61fa7132009-11-11 12:25:40 +01004070static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4071 int flags, struct net_device *dev,
4072 struct survey_info *survey)
4073{
4074 void *hdr;
4075 struct nlattr *infoattr;
4076
Holger Schurig61fa7132009-11-11 12:25:40 +01004077 hdr = nl80211hdr_put(msg, pid, seq, flags,
4078 NL80211_CMD_NEW_SURVEY_RESULTS);
4079 if (!hdr)
4080 return -ENOMEM;
4081
4082 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4083
4084 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4085 if (!infoattr)
4086 goto nla_put_failure;
4087
4088 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4089 survey->channel->center_freq);
4090 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4091 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4092 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004093 if (survey->filled & SURVEY_INFO_IN_USE)
4094 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004095 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4096 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4097 survey->channel_time);
4098 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4099 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4100 survey->channel_time_busy);
4101 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4102 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4103 survey->channel_time_ext_busy);
4104 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4105 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4106 survey->channel_time_rx);
4107 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4108 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4109 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004110
4111 nla_nest_end(msg, infoattr);
4112
4113 return genlmsg_end(msg, hdr);
4114
4115 nla_put_failure:
4116 genlmsg_cancel(msg, hdr);
4117 return -EMSGSIZE;
4118}
4119
4120static int nl80211_dump_survey(struct sk_buff *skb,
4121 struct netlink_callback *cb)
4122{
4123 struct survey_info survey;
4124 struct cfg80211_registered_device *dev;
4125 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004126 int survey_idx = cb->args[1];
4127 int res;
4128
Johannes Berg67748892010-10-04 21:14:06 +02004129 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4130 if (res)
4131 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004132
4133 if (!dev->ops->dump_survey) {
4134 res = -EOPNOTSUPP;
4135 goto out_err;
4136 }
4137
4138 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004139 struct ieee80211_channel *chan;
4140
Holger Schurig61fa7132009-11-11 12:25:40 +01004141 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4142 &survey);
4143 if (res == -ENOENT)
4144 break;
4145 if (res)
4146 goto out_err;
4147
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004148 /* Survey without a channel doesn't make sense */
4149 if (!survey.channel) {
4150 res = -EINVAL;
4151 goto out;
4152 }
4153
4154 chan = ieee80211_get_channel(&dev->wiphy,
4155 survey.channel->center_freq);
4156 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4157 survey_idx++;
4158 continue;
4159 }
4160
Holger Schurig61fa7132009-11-11 12:25:40 +01004161 if (nl80211_send_survey(skb,
4162 NETLINK_CB(cb->skb).pid,
4163 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4164 netdev,
4165 &survey) < 0)
4166 goto out;
4167 survey_idx++;
4168 }
4169
4170 out:
4171 cb->args[1] = survey_idx;
4172 res = skb->len;
4173 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004174 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004175 return res;
4176}
4177
Jouni Malinen255e7372009-03-20 21:21:17 +02004178static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4179{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004180 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004181}
4182
Samuel Ortizb23aa672009-07-01 21:26:54 +02004183static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4184{
4185 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4186 NL80211_WPA_VERSION_2));
4187}
4188
Jouni Malinen636a5d32009-03-19 13:39:22 +02004189static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4190{
Johannes Berg4c476992010-10-04 21:36:35 +02004191 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4192 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004193 struct ieee80211_channel *chan;
4194 const u8 *bssid, *ssid, *ie = NULL;
4195 int err, ssid_len, ie_len = 0;
4196 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004197 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004198 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004199
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004200 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4201 return -EINVAL;
4202
4203 if (!info->attrs[NL80211_ATTR_MAC])
4204 return -EINVAL;
4205
Jouni Malinen17780922009-03-27 20:52:47 +02004206 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4207 return -EINVAL;
4208
Johannes Berg19957bb2009-07-02 17:20:43 +02004209 if (!info->attrs[NL80211_ATTR_SSID])
4210 return -EINVAL;
4211
4212 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4213 return -EINVAL;
4214
Johannes Bergfffd0932009-07-08 14:22:54 +02004215 err = nl80211_parse_key(info, &key);
4216 if (err)
4217 return err;
4218
4219 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004220 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4221 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004222 if (!key.p.key || !key.p.key_len)
4223 return -EINVAL;
4224 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4225 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4226 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4227 key.p.key_len != WLAN_KEY_LEN_WEP104))
4228 return -EINVAL;
4229 if (key.idx > 4)
4230 return -EINVAL;
4231 } else {
4232 key.p.key_len = 0;
4233 key.p.key = NULL;
4234 }
4235
Johannes Bergafea0b72010-08-10 09:46:42 +02004236 if (key.idx >= 0) {
4237 int i;
4238 bool ok = false;
4239 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4240 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4241 ok = true;
4242 break;
4243 }
4244 }
Johannes Berg4c476992010-10-04 21:36:35 +02004245 if (!ok)
4246 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004247 }
4248
Johannes Berg4c476992010-10-04 21:36:35 +02004249 if (!rdev->ops->auth)
4250 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004251
Johannes Berg074ac8d2010-09-16 14:58:22 +02004252 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004253 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4254 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004255
Johannes Berg19957bb2009-07-02 17:20:43 +02004256 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004257 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004258 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004259 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4260 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004261
Johannes Berg19957bb2009-07-02 17:20:43 +02004262 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4263 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4264
4265 if (info->attrs[NL80211_ATTR_IE]) {
4266 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4267 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4268 }
4269
4270 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004271 if (!nl80211_valid_auth_type(auth_type))
4272 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004273
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004274 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4275
Johannes Berg4c476992010-10-04 21:36:35 +02004276 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4277 ssid, ssid_len, ie, ie_len,
4278 key.p.key, key.p.key_len, key.idx,
4279 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004280}
4281
Johannes Bergc0692b82010-08-27 14:26:53 +03004282static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4283 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004284 struct cfg80211_crypto_settings *settings,
4285 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004286{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004287 memset(settings, 0, sizeof(*settings));
4288
Samuel Ortizb23aa672009-07-01 21:26:54 +02004289 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4290
Johannes Bergc0692b82010-08-27 14:26:53 +03004291 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4292 u16 proto;
4293 proto = nla_get_u16(
4294 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4295 settings->control_port_ethertype = cpu_to_be16(proto);
4296 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4297 proto != ETH_P_PAE)
4298 return -EINVAL;
4299 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4300 settings->control_port_no_encrypt = true;
4301 } else
4302 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4303
Samuel Ortizb23aa672009-07-01 21:26:54 +02004304 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4305 void *data;
4306 int len, i;
4307
4308 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4309 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4310 settings->n_ciphers_pairwise = len / sizeof(u32);
4311
4312 if (len % sizeof(u32))
4313 return -EINVAL;
4314
Johannes Berg3dc27d22009-07-02 21:36:37 +02004315 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004316 return -EINVAL;
4317
4318 memcpy(settings->ciphers_pairwise, data, len);
4319
4320 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004321 if (!cfg80211_supported_cipher_suite(
4322 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004323 settings->ciphers_pairwise[i]))
4324 return -EINVAL;
4325 }
4326
4327 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4328 settings->cipher_group =
4329 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004330 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4331 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004332 return -EINVAL;
4333 }
4334
4335 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4336 settings->wpa_versions =
4337 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4338 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4339 return -EINVAL;
4340 }
4341
4342 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4343 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004344 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004345
4346 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4347 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4348 settings->n_akm_suites = len / sizeof(u32);
4349
4350 if (len % sizeof(u32))
4351 return -EINVAL;
4352
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004353 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4354 return -EINVAL;
4355
Samuel Ortizb23aa672009-07-01 21:26:54 +02004356 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004357 }
4358
4359 return 0;
4360}
4361
Jouni Malinen636a5d32009-03-19 13:39:22 +02004362static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4363{
Johannes Berg4c476992010-10-04 21:36:35 +02004364 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4365 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004366 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004367 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004368 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004369 int err, ssid_len, ie_len = 0;
4370 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004371
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004372 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4373 return -EINVAL;
4374
4375 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004376 !info->attrs[NL80211_ATTR_SSID] ||
4377 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004378 return -EINVAL;
4379
Johannes Berg4c476992010-10-04 21:36:35 +02004380 if (!rdev->ops->assoc)
4381 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004382
Johannes Berg074ac8d2010-09-16 14:58:22 +02004383 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004384 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4385 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004386
Johannes Berg19957bb2009-07-02 17:20:43 +02004387 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004388
Johannes Berg19957bb2009-07-02 17:20:43 +02004389 chan = ieee80211_get_channel(&rdev->wiphy,
4390 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004391 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4392 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004393
Johannes Berg19957bb2009-07-02 17:20:43 +02004394 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4395 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004396
4397 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004398 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4399 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004400 }
4401
Jouni Malinendc6382c2009-05-06 22:09:37 +03004402 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004403 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004404 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004405 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004406 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004407 else if (mfp != NL80211_MFP_NO)
4408 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004409 }
4410
Johannes Berg3e5d7642009-07-07 14:37:26 +02004411 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4412 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4413
Johannes Bergc0692b82010-08-27 14:26:53 +03004414 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004415 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004416 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4417 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004418 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004419
Jouni Malinen636a5d32009-03-19 13:39:22 +02004420 return err;
4421}
4422
4423static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4424{
Johannes Berg4c476992010-10-04 21:36:35 +02004425 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4426 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004427 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004428 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004429 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004430 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004431
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004432 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4433 return -EINVAL;
4434
4435 if (!info->attrs[NL80211_ATTR_MAC])
4436 return -EINVAL;
4437
4438 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4439 return -EINVAL;
4440
Johannes Berg4c476992010-10-04 21:36:35 +02004441 if (!rdev->ops->deauth)
4442 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004443
Johannes Berg074ac8d2010-09-16 14:58:22 +02004444 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004445 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4446 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004447
Johannes Berg19957bb2009-07-02 17:20:43 +02004448 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004449
Johannes Berg19957bb2009-07-02 17:20:43 +02004450 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4451 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004452 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004453 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004454 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004455
4456 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004457 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4458 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004459 }
4460
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004461 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4464 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004465}
4466
4467static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4468{
Johannes Berg4c476992010-10-04 21:36:35 +02004469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4470 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004471 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004472 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004473 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004474 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004475
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004476 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4477 return -EINVAL;
4478
4479 if (!info->attrs[NL80211_ATTR_MAC])
4480 return -EINVAL;
4481
4482 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4483 return -EINVAL;
4484
Johannes Berg4c476992010-10-04 21:36:35 +02004485 if (!rdev->ops->disassoc)
4486 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004487
Johannes Berg074ac8d2010-09-16 14:58:22 +02004488 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004489 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4490 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004491
Johannes Berg19957bb2009-07-02 17:20:43 +02004492 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004493
Johannes Berg19957bb2009-07-02 17:20:43 +02004494 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4495 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004496 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004497 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004498 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004499
4500 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004501 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4502 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004503 }
4504
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004505 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4506
Johannes Berg4c476992010-10-04 21:36:35 +02004507 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4508 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004509}
4510
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004511static bool
4512nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4513 int mcast_rate[IEEE80211_NUM_BANDS],
4514 int rateval)
4515{
4516 struct wiphy *wiphy = &rdev->wiphy;
4517 bool found = false;
4518 int band, i;
4519
4520 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4521 struct ieee80211_supported_band *sband;
4522
4523 sband = wiphy->bands[band];
4524 if (!sband)
4525 continue;
4526
4527 for (i = 0; i < sband->n_bitrates; i++) {
4528 if (sband->bitrates[i].bitrate == rateval) {
4529 mcast_rate[band] = i + 1;
4530 found = true;
4531 break;
4532 }
4533 }
4534 }
4535
4536 return found;
4537}
4538
Johannes Berg04a773a2009-04-19 21:24:32 +02004539static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4540{
Johannes Berg4c476992010-10-04 21:36:35 +02004541 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4542 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004543 struct cfg80211_ibss_params ibss;
4544 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004545 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004546 int err;
4547
Johannes Berg8e30bc52009-04-22 17:45:38 +02004548 memset(&ibss, 0, sizeof(ibss));
4549
Johannes Berg04a773a2009-04-19 21:24:32 +02004550 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4551 return -EINVAL;
4552
4553 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4554 !info->attrs[NL80211_ATTR_SSID] ||
4555 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4556 return -EINVAL;
4557
Johannes Berg8e30bc52009-04-22 17:45:38 +02004558 ibss.beacon_interval = 100;
4559
4560 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4561 ibss.beacon_interval =
4562 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4563 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4564 return -EINVAL;
4565 }
4566
Johannes Berg4c476992010-10-04 21:36:35 +02004567 if (!rdev->ops->join_ibss)
4568 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004569
Johannes Berg4c476992010-10-04 21:36:35 +02004570 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4571 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004572
Johannes Berg79c97e92009-07-07 03:56:12 +02004573 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004574
Johannes Berg39193492011-09-16 13:45:25 +02004575 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004576 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004577
4578 if (!is_valid_ether_addr(ibss.bssid))
4579 return -EINVAL;
4580 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004581 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4582 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4583
4584 if (info->attrs[NL80211_ATTR_IE]) {
4585 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4586 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4587 }
4588
4589 ibss.channel = ieee80211_get_channel(wiphy,
4590 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4591 if (!ibss.channel ||
4592 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004593 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4594 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004595
4596 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004597 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004598
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004599 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4600 u8 *rates =
4601 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4602 int n_rates =
4603 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4604 struct ieee80211_supported_band *sband =
4605 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004606 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004607
Johannes Berg34850ab2011-07-18 18:08:35 +02004608 err = ieee80211_get_ratemask(sband, rates, n_rates,
4609 &ibss.basic_rates);
4610 if (err)
4611 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004612 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004613
4614 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4615 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4616 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4617 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004618
Johannes Berg4c476992010-10-04 21:36:35 +02004619 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4620 connkeys = nl80211_parse_connkeys(rdev,
4621 info->attrs[NL80211_ATTR_KEYS]);
4622 if (IS_ERR(connkeys))
4623 return PTR_ERR(connkeys);
4624 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004625
Johannes Berg4c476992010-10-04 21:36:35 +02004626 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004627 if (err)
4628 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004629 return err;
4630}
4631
4632static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4633{
Johannes Berg4c476992010-10-04 21:36:35 +02004634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4635 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004636
Johannes Berg4c476992010-10-04 21:36:35 +02004637 if (!rdev->ops->leave_ibss)
4638 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004639
Johannes Berg4c476992010-10-04 21:36:35 +02004640 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4641 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004642
Johannes Berg4c476992010-10-04 21:36:35 +02004643 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004644}
4645
Johannes Bergaff89a92009-07-01 21:26:51 +02004646#ifdef CONFIG_NL80211_TESTMODE
4647static struct genl_multicast_group nl80211_testmode_mcgrp = {
4648 .name = "testmode",
4649};
4650
4651static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4652{
Johannes Berg4c476992010-10-04 21:36:35 +02004653 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004654 int err;
4655
4656 if (!info->attrs[NL80211_ATTR_TESTDATA])
4657 return -EINVAL;
4658
Johannes Bergaff89a92009-07-01 21:26:51 +02004659 err = -EOPNOTSUPP;
4660 if (rdev->ops->testmode_cmd) {
4661 rdev->testmode_info = info;
4662 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4663 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4664 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4665 rdev->testmode_info = NULL;
4666 }
4667
Johannes Bergaff89a92009-07-01 21:26:51 +02004668 return err;
4669}
4670
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004671static int nl80211_testmode_dump(struct sk_buff *skb,
4672 struct netlink_callback *cb)
4673{
4674 struct cfg80211_registered_device *dev;
4675 int err;
4676 long phy_idx;
4677 void *data = NULL;
4678 int data_len = 0;
4679
4680 if (cb->args[0]) {
4681 /*
4682 * 0 is a valid index, but not valid for args[0],
4683 * so we need to offset by 1.
4684 */
4685 phy_idx = cb->args[0] - 1;
4686 } else {
4687 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4688 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4689 nl80211_policy);
4690 if (err)
4691 return err;
4692 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4693 return -EINVAL;
4694 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4695 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4696 cb->args[1] =
4697 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4698 }
4699
4700 if (cb->args[1]) {
4701 data = nla_data((void *)cb->args[1]);
4702 data_len = nla_len((void *)cb->args[1]);
4703 }
4704
4705 mutex_lock(&cfg80211_mutex);
4706 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4707 if (!dev) {
4708 mutex_unlock(&cfg80211_mutex);
4709 return -ENOENT;
4710 }
4711 cfg80211_lock_rdev(dev);
4712 mutex_unlock(&cfg80211_mutex);
4713
4714 if (!dev->ops->testmode_dump) {
4715 err = -EOPNOTSUPP;
4716 goto out_err;
4717 }
4718
4719 while (1) {
4720 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4721 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4722 NL80211_CMD_TESTMODE);
4723 struct nlattr *tmdata;
4724
4725 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4726 genlmsg_cancel(skb, hdr);
4727 break;
4728 }
4729
4730 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4731 if (!tmdata) {
4732 genlmsg_cancel(skb, hdr);
4733 break;
4734 }
4735 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4736 data, data_len);
4737 nla_nest_end(skb, tmdata);
4738
4739 if (err == -ENOBUFS || err == -ENOENT) {
4740 genlmsg_cancel(skb, hdr);
4741 break;
4742 } else if (err) {
4743 genlmsg_cancel(skb, hdr);
4744 goto out_err;
4745 }
4746
4747 genlmsg_end(skb, hdr);
4748 }
4749
4750 err = skb->len;
4751 /* see above */
4752 cb->args[0] = phy_idx + 1;
4753 out_err:
4754 cfg80211_unlock_rdev(dev);
4755 return err;
4756}
4757
Johannes Bergaff89a92009-07-01 21:26:51 +02004758static struct sk_buff *
4759__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4760 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4761{
4762 struct sk_buff *skb;
4763 void *hdr;
4764 struct nlattr *data;
4765
4766 skb = nlmsg_new(approxlen + 100, gfp);
4767 if (!skb)
4768 return NULL;
4769
4770 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4771 if (!hdr) {
4772 kfree_skb(skb);
4773 return NULL;
4774 }
4775
4776 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4777 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4778
4779 ((void **)skb->cb)[0] = rdev;
4780 ((void **)skb->cb)[1] = hdr;
4781 ((void **)skb->cb)[2] = data;
4782
4783 return skb;
4784
4785 nla_put_failure:
4786 kfree_skb(skb);
4787 return NULL;
4788}
4789
4790struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4791 int approxlen)
4792{
4793 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4794
4795 if (WARN_ON(!rdev->testmode_info))
4796 return NULL;
4797
4798 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4799 rdev->testmode_info->snd_pid,
4800 rdev->testmode_info->snd_seq,
4801 GFP_KERNEL);
4802}
4803EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4804
4805int cfg80211_testmode_reply(struct sk_buff *skb)
4806{
4807 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4808 void *hdr = ((void **)skb->cb)[1];
4809 struct nlattr *data = ((void **)skb->cb)[2];
4810
4811 if (WARN_ON(!rdev->testmode_info)) {
4812 kfree_skb(skb);
4813 return -EINVAL;
4814 }
4815
4816 nla_nest_end(skb, data);
4817 genlmsg_end(skb, hdr);
4818 return genlmsg_reply(skb, rdev->testmode_info);
4819}
4820EXPORT_SYMBOL(cfg80211_testmode_reply);
4821
4822struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4823 int approxlen, gfp_t gfp)
4824{
4825 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4826
4827 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4828}
4829EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4830
4831void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4832{
4833 void *hdr = ((void **)skb->cb)[1];
4834 struct nlattr *data = ((void **)skb->cb)[2];
4835
4836 nla_nest_end(skb, data);
4837 genlmsg_end(skb, hdr);
4838 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4839}
4840EXPORT_SYMBOL(cfg80211_testmode_event);
4841#endif
4842
Samuel Ortizb23aa672009-07-01 21:26:54 +02004843static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4844{
Johannes Berg4c476992010-10-04 21:36:35 +02004845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4846 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004847 struct cfg80211_connect_params connect;
4848 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004849 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004850 int err;
4851
4852 memset(&connect, 0, sizeof(connect));
4853
4854 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4855 return -EINVAL;
4856
4857 if (!info->attrs[NL80211_ATTR_SSID] ||
4858 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4859 return -EINVAL;
4860
4861 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4862 connect.auth_type =
4863 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4864 if (!nl80211_valid_auth_type(connect.auth_type))
4865 return -EINVAL;
4866 } else
4867 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4868
4869 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4870
Johannes Bergc0692b82010-08-27 14:26:53 +03004871 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004872 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004873 if (err)
4874 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004875
Johannes Berg074ac8d2010-09-16 14:58:22 +02004876 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004877 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4878 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004879
Johannes Berg79c97e92009-07-07 03:56:12 +02004880 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004881
Samuel Ortizb23aa672009-07-01 21:26:54 +02004882 if (info->attrs[NL80211_ATTR_MAC])
4883 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4884 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4885 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4886
4887 if (info->attrs[NL80211_ATTR_IE]) {
4888 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4889 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4890 }
4891
4892 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4893 connect.channel =
4894 ieee80211_get_channel(wiphy,
4895 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4896 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004897 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4898 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004899 }
4900
Johannes Bergfffd0932009-07-08 14:22:54 +02004901 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4902 connkeys = nl80211_parse_connkeys(rdev,
4903 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004904 if (IS_ERR(connkeys))
4905 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004906 }
4907
4908 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004909 if (err)
4910 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004911 return err;
4912}
4913
4914static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4915{
Johannes Berg4c476992010-10-04 21:36:35 +02004916 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4917 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004918 u16 reason;
4919
4920 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4921 reason = WLAN_REASON_DEAUTH_LEAVING;
4922 else
4923 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4924
4925 if (reason == 0)
4926 return -EINVAL;
4927
Johannes Berg074ac8d2010-09-16 14:58:22 +02004928 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004929 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4930 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004931
Johannes Berg4c476992010-10-04 21:36:35 +02004932 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004933}
4934
Johannes Berg463d0182009-07-14 00:33:35 +02004935static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4936{
Johannes Berg4c476992010-10-04 21:36:35 +02004937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004938 struct net *net;
4939 int err;
4940 u32 pid;
4941
4942 if (!info->attrs[NL80211_ATTR_PID])
4943 return -EINVAL;
4944
4945 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4946
Johannes Berg463d0182009-07-14 00:33:35 +02004947 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004948 if (IS_ERR(net))
4949 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004950
4951 err = 0;
4952
4953 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004954 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4955 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004956
Johannes Berg463d0182009-07-14 00:33:35 +02004957 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004958 return err;
4959}
4960
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004961static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4962{
Johannes Berg4c476992010-10-04 21:36:35 +02004963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004964 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4965 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004966 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004967 struct cfg80211_pmksa pmksa;
4968
4969 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4970
4971 if (!info->attrs[NL80211_ATTR_MAC])
4972 return -EINVAL;
4973
4974 if (!info->attrs[NL80211_ATTR_PMKID])
4975 return -EINVAL;
4976
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004977 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4978 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4979
Johannes Berg074ac8d2010-09-16 14:58:22 +02004980 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004981 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4982 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004983
4984 switch (info->genlhdr->cmd) {
4985 case NL80211_CMD_SET_PMKSA:
4986 rdev_ops = rdev->ops->set_pmksa;
4987 break;
4988 case NL80211_CMD_DEL_PMKSA:
4989 rdev_ops = rdev->ops->del_pmksa;
4990 break;
4991 default:
4992 WARN_ON(1);
4993 break;
4994 }
4995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 if (!rdev_ops)
4997 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004998
Johannes Berg4c476992010-10-04 21:36:35 +02004999 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005000}
5001
5002static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5003{
Johannes Berg4c476992010-10-04 21:36:35 +02005004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5005 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005006
Johannes Berg074ac8d2010-09-16 14:58:22 +02005007 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005008 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5009 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005010
Johannes Berg4c476992010-10-04 21:36:35 +02005011 if (!rdev->ops->flush_pmksa)
5012 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005013
Johannes Berg4c476992010-10-04 21:36:35 +02005014 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005015}
5016
Arik Nemtsov109086c2011-09-28 14:12:50 +03005017static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5018{
5019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5020 struct net_device *dev = info->user_ptr[1];
5021 u8 action_code, dialog_token;
5022 u16 status_code;
5023 u8 *peer;
5024
5025 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5026 !rdev->ops->tdls_mgmt)
5027 return -EOPNOTSUPP;
5028
5029 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5030 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5031 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5032 !info->attrs[NL80211_ATTR_IE] ||
5033 !info->attrs[NL80211_ATTR_MAC])
5034 return -EINVAL;
5035
5036 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5037 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5038 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5039 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5040
5041 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5042 dialog_token, status_code,
5043 nla_data(info->attrs[NL80211_ATTR_IE]),
5044 nla_len(info->attrs[NL80211_ATTR_IE]));
5045}
5046
5047static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5048{
5049 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5050 struct net_device *dev = info->user_ptr[1];
5051 enum nl80211_tdls_operation operation;
5052 u8 *peer;
5053
5054 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5055 !rdev->ops->tdls_oper)
5056 return -EOPNOTSUPP;
5057
5058 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5059 !info->attrs[NL80211_ATTR_MAC])
5060 return -EINVAL;
5061
5062 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5063 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5064
5065 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5066}
5067
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005068static int nl80211_remain_on_channel(struct sk_buff *skb,
5069 struct genl_info *info)
5070{
Johannes Berg4c476992010-10-04 21:36:35 +02005071 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5072 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005073 struct ieee80211_channel *chan;
5074 struct sk_buff *msg;
5075 void *hdr;
5076 u64 cookie;
5077 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5078 u32 freq, duration;
5079 int err;
5080
5081 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5082 !info->attrs[NL80211_ATTR_DURATION])
5083 return -EINVAL;
5084
5085 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5086
5087 /*
5088 * We should be on that channel for at least one jiffie,
5089 * and more than 5 seconds seems excessive.
5090 */
Johannes Berga2939112010-12-14 17:54:28 +01005091 if (!duration || !msecs_to_jiffies(duration) ||
5092 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005093 return -EINVAL;
5094
Johannes Berg4c476992010-10-04 21:36:35 +02005095 if (!rdev->ops->remain_on_channel)
5096 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005097
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005098 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5099 channel_type = nla_get_u32(
5100 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5101 if (channel_type != NL80211_CHAN_NO_HT &&
5102 channel_type != NL80211_CHAN_HT20 &&
5103 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005104 channel_type != NL80211_CHAN_HT40MINUS)
5105 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005106 }
5107
5108 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5109 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005110 if (chan == NULL)
5111 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005112
5113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005114 if (!msg)
5115 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005116
5117 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5118 NL80211_CMD_REMAIN_ON_CHANNEL);
5119
5120 if (IS_ERR(hdr)) {
5121 err = PTR_ERR(hdr);
5122 goto free_msg;
5123 }
5124
5125 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5126 channel_type, duration, &cookie);
5127
5128 if (err)
5129 goto free_msg;
5130
5131 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5132
5133 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005134
5135 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005136
5137 nla_put_failure:
5138 err = -ENOBUFS;
5139 free_msg:
5140 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005141 return err;
5142}
5143
5144static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5145 struct genl_info *info)
5146{
Johannes Berg4c476992010-10-04 21:36:35 +02005147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5148 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005149 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005150
5151 if (!info->attrs[NL80211_ATTR_COOKIE])
5152 return -EINVAL;
5153
Johannes Berg4c476992010-10-04 21:36:35 +02005154 if (!rdev->ops->cancel_remain_on_channel)
5155 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005156
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005157 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5158
Johannes Berg4c476992010-10-04 21:36:35 +02005159 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005160}
5161
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005162static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5163 u8 *rates, u8 rates_len)
5164{
5165 u8 i;
5166 u32 mask = 0;
5167
5168 for (i = 0; i < rates_len; i++) {
5169 int rate = (rates[i] & 0x7f) * 5;
5170 int ridx;
5171 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5172 struct ieee80211_rate *srate =
5173 &sband->bitrates[ridx];
5174 if (rate == srate->bitrate) {
5175 mask |= 1 << ridx;
5176 break;
5177 }
5178 }
5179 if (ridx == sband->n_bitrates)
5180 return 0; /* rate not found */
5181 }
5182
5183 return mask;
5184}
5185
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005186static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005187 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5188 .len = NL80211_MAX_SUPP_RATES },
5189};
5190
5191static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5192 struct genl_info *info)
5193{
5194 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005196 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005197 int rem, i;
5198 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005199 struct nlattr *tx_rates;
5200 struct ieee80211_supported_band *sband;
5201
5202 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5203 return -EINVAL;
5204
Johannes Berg4c476992010-10-04 21:36:35 +02005205 if (!rdev->ops->set_bitrate_mask)
5206 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005207
5208 memset(&mask, 0, sizeof(mask));
5209 /* Default to all rates enabled */
5210 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5211 sband = rdev->wiphy.bands[i];
5212 mask.control[i].legacy =
5213 sband ? (1 << sband->n_bitrates) - 1 : 0;
5214 }
5215
5216 /*
5217 * The nested attribute uses enum nl80211_band as the index. This maps
5218 * directly to the enum ieee80211_band values used in cfg80211.
5219 */
5220 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5221 {
5222 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005223 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5224 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005225 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005226 if (sband == NULL)
5227 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005228 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5229 nla_len(tx_rates), nl80211_txattr_policy);
5230 if (tb[NL80211_TXRATE_LEGACY]) {
5231 mask.control[band].legacy = rateset_to_mask(
5232 sband,
5233 nla_data(tb[NL80211_TXRATE_LEGACY]),
5234 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005235 if (mask.control[band].legacy == 0)
5236 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005237 }
5238 }
5239
Johannes Berg4c476992010-10-04 21:36:35 +02005240 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005241}
5242
Johannes Berg2e161f72010-08-12 15:38:38 +02005243static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005244{
Johannes Berg4c476992010-10-04 21:36:35 +02005245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5246 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005247 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005248
5249 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5250 return -EINVAL;
5251
Johannes Berg2e161f72010-08-12 15:38:38 +02005252 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5253 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005254
Johannes Berg9d38d852010-06-09 17:20:33 +02005255 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005256 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005257 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5258 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5259 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005260 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005261 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5262 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005263
5264 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005265 if (!rdev->ops->mgmt_tx)
5266 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005267
Johannes Berg4c476992010-10-04 21:36:35 +02005268 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005269 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005270 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5271 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005272}
5273
Johannes Berg2e161f72010-08-12 15:38:38 +02005274static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005275{
Johannes Berg4c476992010-10-04 21:36:35 +02005276 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5277 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005278 struct ieee80211_channel *chan;
5279 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005280 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005281 u32 freq;
5282 int err;
5283 void *hdr;
5284 u64 cookie;
5285 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005286 unsigned int wait = 0;
5287 bool offchan;
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305288 bool no_cck;
Jouni Malinen026331c2010-02-15 12:53:10 +02005289
5290 if (!info->attrs[NL80211_ATTR_FRAME] ||
5291 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5292 return -EINVAL;
5293
Johannes Berg4c476992010-10-04 21:36:35 +02005294 if (!rdev->ops->mgmt_tx)
5295 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005296
Johannes Berg9d38d852010-06-09 17:20:33 +02005297 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005298 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005299 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5300 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5301 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005303 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5304 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005305
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005306 if (info->attrs[NL80211_ATTR_DURATION]) {
5307 if (!rdev->ops->mgmt_tx_cancel_wait)
5308 return -EINVAL;
5309 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5310 }
5311
Jouni Malinen026331c2010-02-15 12:53:10 +02005312 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5313 channel_type = nla_get_u32(
5314 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5315 if (channel_type != NL80211_CHAN_NO_HT &&
5316 channel_type != NL80211_CHAN_HT20 &&
5317 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005318 channel_type != NL80211_CHAN_HT40MINUS)
5319 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005320 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005321 }
5322
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005323 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5324
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305325 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5326
Jouni Malinen026331c2010-02-15 12:53:10 +02005327 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5328 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005329 if (chan == NULL)
5330 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005331
5332 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005333 if (!msg)
5334 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005335
5336 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005337 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005338
5339 if (IS_ERR(hdr)) {
5340 err = PTR_ERR(hdr);
5341 goto free_msg;
5342 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005343 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5344 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005345 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5346 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305347 no_cck, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005348 if (err)
5349 goto free_msg;
5350
5351 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5352
5353 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005354 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005355
5356 nla_put_failure:
5357 err = -ENOBUFS;
5358 free_msg:
5359 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005360 return err;
5361}
5362
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005363static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5364{
5365 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5366 struct net_device *dev = info->user_ptr[1];
5367 u64 cookie;
5368
5369 if (!info->attrs[NL80211_ATTR_COOKIE])
5370 return -EINVAL;
5371
5372 if (!rdev->ops->mgmt_tx_cancel_wait)
5373 return -EOPNOTSUPP;
5374
5375 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5376 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5377 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5378 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5379 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5380 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5381 return -EOPNOTSUPP;
5382
5383 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5384
5385 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5386}
5387
Kalle Valoffb9eb32010-02-17 17:58:10 +02005388static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5389{
Johannes Berg4c476992010-10-04 21:36:35 +02005390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005391 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005392 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005393 u8 ps_state;
5394 bool state;
5395 int err;
5396
Johannes Berg4c476992010-10-04 21:36:35 +02005397 if (!info->attrs[NL80211_ATTR_PS_STATE])
5398 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005399
5400 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5401
Johannes Berg4c476992010-10-04 21:36:35 +02005402 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5403 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005404
5405 wdev = dev->ieee80211_ptr;
5406
Johannes Berg4c476992010-10-04 21:36:35 +02005407 if (!rdev->ops->set_power_mgmt)
5408 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005409
5410 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5411
5412 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005413 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005414
Johannes Berg4c476992010-10-04 21:36:35 +02005415 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5416 wdev->ps_timeout);
5417 if (!err)
5418 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005419 return err;
5420}
5421
5422static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5423{
Johannes Berg4c476992010-10-04 21:36:35 +02005424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005425 enum nl80211_ps_state ps_state;
5426 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005427 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005428 struct sk_buff *msg;
5429 void *hdr;
5430 int err;
5431
Kalle Valoffb9eb32010-02-17 17:58:10 +02005432 wdev = dev->ieee80211_ptr;
5433
Johannes Berg4c476992010-10-04 21:36:35 +02005434 if (!rdev->ops->set_power_mgmt)
5435 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005436
5437 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005438 if (!msg)
5439 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005440
5441 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5442 NL80211_CMD_GET_POWER_SAVE);
5443 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005444 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005445 goto free_msg;
5446 }
5447
5448 if (wdev->ps)
5449 ps_state = NL80211_PS_ENABLED;
5450 else
5451 ps_state = NL80211_PS_DISABLED;
5452
5453 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5454
5455 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005456 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005457
Johannes Berg4c476992010-10-04 21:36:35 +02005458 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005459 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005460 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005461 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005462 return err;
5463}
5464
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005465static struct nla_policy
5466nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5467 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5468 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5469 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5470};
5471
5472static int nl80211_set_cqm_rssi(struct genl_info *info,
5473 s32 threshold, u32 hysteresis)
5474{
Johannes Berg4c476992010-10-04 21:36:35 +02005475 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005476 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005477 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005478
5479 if (threshold > 0)
5480 return -EINVAL;
5481
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005482 wdev = dev->ieee80211_ptr;
5483
Johannes Berg4c476992010-10-04 21:36:35 +02005484 if (!rdev->ops->set_cqm_rssi_config)
5485 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005486
Johannes Berg074ac8d2010-09-16 14:58:22 +02005487 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005488 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5489 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005490
Johannes Berg4c476992010-10-04 21:36:35 +02005491 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5492 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005493}
5494
5495static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5496{
5497 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5498 struct nlattr *cqm;
5499 int err;
5500
5501 cqm = info->attrs[NL80211_ATTR_CQM];
5502 if (!cqm) {
5503 err = -EINVAL;
5504 goto out;
5505 }
5506
5507 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5508 nl80211_attr_cqm_policy);
5509 if (err)
5510 goto out;
5511
5512 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5513 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5514 s32 threshold;
5515 u32 hysteresis;
5516 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5517 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5518 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5519 } else
5520 err = -EINVAL;
5521
5522out:
5523 return err;
5524}
5525
Johannes Berg29cbe682010-12-03 09:20:44 +01005526static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5527{
5528 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5529 struct net_device *dev = info->user_ptr[1];
5530 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005531 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005532 int err;
5533
5534 /* start with default */
5535 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005536 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005537
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005538 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005539 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005540 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005541 if (err)
5542 return err;
5543 }
5544
5545 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5546 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5547 return -EINVAL;
5548
Javier Cardonac80d5452010-12-16 17:37:49 -08005549 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5550 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5551
5552 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5553 /* parse additional setup parameters if given */
5554 err = nl80211_parse_mesh_setup(info, &setup);
5555 if (err)
5556 return err;
5557 }
5558
5559 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005560}
5561
5562static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5563{
5564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5565 struct net_device *dev = info->user_ptr[1];
5566
5567 return cfg80211_leave_mesh(rdev, dev);
5568}
5569
Johannes Bergff1b6e62011-05-04 15:37:28 +02005570static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5571{
5572 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5573 struct sk_buff *msg;
5574 void *hdr;
5575
5576 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5577 return -EOPNOTSUPP;
5578
5579 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5580 if (!msg)
5581 return -ENOMEM;
5582
5583 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5584 NL80211_CMD_GET_WOWLAN);
5585 if (!hdr)
5586 goto nla_put_failure;
5587
5588 if (rdev->wowlan) {
5589 struct nlattr *nl_wowlan;
5590
5591 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5592 if (!nl_wowlan)
5593 goto nla_put_failure;
5594
5595 if (rdev->wowlan->any)
5596 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5597 if (rdev->wowlan->disconnect)
5598 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5599 if (rdev->wowlan->magic_pkt)
5600 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005601 if (rdev->wowlan->gtk_rekey_failure)
5602 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5603 if (rdev->wowlan->eap_identity_req)
5604 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5605 if (rdev->wowlan->four_way_handshake)
5606 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5607 if (rdev->wowlan->rfkill_release)
5608 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005609 if (rdev->wowlan->n_patterns) {
5610 struct nlattr *nl_pats, *nl_pat;
5611 int i, pat_len;
5612
5613 nl_pats = nla_nest_start(msg,
5614 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5615 if (!nl_pats)
5616 goto nla_put_failure;
5617
5618 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5619 nl_pat = nla_nest_start(msg, i + 1);
5620 if (!nl_pat)
5621 goto nla_put_failure;
5622 pat_len = rdev->wowlan->patterns[i].pattern_len;
5623 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5624 DIV_ROUND_UP(pat_len, 8),
5625 rdev->wowlan->patterns[i].mask);
5626 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5627 pat_len,
5628 rdev->wowlan->patterns[i].pattern);
5629 nla_nest_end(msg, nl_pat);
5630 }
5631 nla_nest_end(msg, nl_pats);
5632 }
5633
5634 nla_nest_end(msg, nl_wowlan);
5635 }
5636
5637 genlmsg_end(msg, hdr);
5638 return genlmsg_reply(msg, info);
5639
5640nla_put_failure:
5641 nlmsg_free(msg);
5642 return -ENOBUFS;
5643}
5644
5645static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5646{
5647 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5648 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5649 struct cfg80211_wowlan no_triggers = {};
5650 struct cfg80211_wowlan new_triggers = {};
5651 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5652 int err, i;
5653
5654 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5655 return -EOPNOTSUPP;
5656
5657 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5658 goto no_triggers;
5659
5660 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5661 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5662 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5663 nl80211_wowlan_policy);
5664 if (err)
5665 return err;
5666
5667 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5668 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5669 return -EINVAL;
5670 new_triggers.any = true;
5671 }
5672
5673 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5674 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5675 return -EINVAL;
5676 new_triggers.disconnect = true;
5677 }
5678
5679 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5680 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5681 return -EINVAL;
5682 new_triggers.magic_pkt = true;
5683 }
5684
Johannes Berg77dbbb12011-07-13 10:48:55 +02005685 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5686 return -EINVAL;
5687
5688 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5689 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5690 return -EINVAL;
5691 new_triggers.gtk_rekey_failure = true;
5692 }
5693
5694 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5695 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5696 return -EINVAL;
5697 new_triggers.eap_identity_req = true;
5698 }
5699
5700 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5701 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5702 return -EINVAL;
5703 new_triggers.four_way_handshake = true;
5704 }
5705
5706 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5707 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5708 return -EINVAL;
5709 new_triggers.rfkill_release = true;
5710 }
5711
Johannes Bergff1b6e62011-05-04 15:37:28 +02005712 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5713 struct nlattr *pat;
5714 int n_patterns = 0;
5715 int rem, pat_len, mask_len;
5716 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5717
5718 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5719 rem)
5720 n_patterns++;
5721 if (n_patterns > wowlan->n_patterns)
5722 return -EINVAL;
5723
5724 new_triggers.patterns = kcalloc(n_patterns,
5725 sizeof(new_triggers.patterns[0]),
5726 GFP_KERNEL);
5727 if (!new_triggers.patterns)
5728 return -ENOMEM;
5729
5730 new_triggers.n_patterns = n_patterns;
5731 i = 0;
5732
5733 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5734 rem) {
5735 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5736 nla_data(pat), nla_len(pat), NULL);
5737 err = -EINVAL;
5738 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5739 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5740 goto error;
5741 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5742 mask_len = DIV_ROUND_UP(pat_len, 8);
5743 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5744 mask_len)
5745 goto error;
5746 if (pat_len > wowlan->pattern_max_len ||
5747 pat_len < wowlan->pattern_min_len)
5748 goto error;
5749
5750 new_triggers.patterns[i].mask =
5751 kmalloc(mask_len + pat_len, GFP_KERNEL);
5752 if (!new_triggers.patterns[i].mask) {
5753 err = -ENOMEM;
5754 goto error;
5755 }
5756 new_triggers.patterns[i].pattern =
5757 new_triggers.patterns[i].mask + mask_len;
5758 memcpy(new_triggers.patterns[i].mask,
5759 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5760 mask_len);
5761 new_triggers.patterns[i].pattern_len = pat_len;
5762 memcpy(new_triggers.patterns[i].pattern,
5763 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5764 pat_len);
5765 i++;
5766 }
5767 }
5768
5769 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5770 struct cfg80211_wowlan *ntrig;
5771 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5772 GFP_KERNEL);
5773 if (!ntrig) {
5774 err = -ENOMEM;
5775 goto error;
5776 }
5777 cfg80211_rdev_free_wowlan(rdev);
5778 rdev->wowlan = ntrig;
5779 } else {
5780 no_triggers:
5781 cfg80211_rdev_free_wowlan(rdev);
5782 rdev->wowlan = NULL;
5783 }
5784
5785 return 0;
5786 error:
5787 for (i = 0; i < new_triggers.n_patterns; i++)
5788 kfree(new_triggers.patterns[i].mask);
5789 kfree(new_triggers.patterns);
5790 return err;
5791}
5792
Johannes Berge5497d72011-07-05 16:35:40 +02005793static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5794{
5795 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5796 struct net_device *dev = info->user_ptr[1];
5797 struct wireless_dev *wdev = dev->ieee80211_ptr;
5798 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5799 struct cfg80211_gtk_rekey_data rekey_data;
5800 int err;
5801
5802 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5803 return -EINVAL;
5804
5805 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5806 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5807 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5808 nl80211_rekey_policy);
5809 if (err)
5810 return err;
5811
5812 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5813 return -ERANGE;
5814 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5815 return -ERANGE;
5816 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5817 return -ERANGE;
5818
5819 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5820 NL80211_KEK_LEN);
5821 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5822 NL80211_KCK_LEN);
5823 memcpy(rekey_data.replay_ctr,
5824 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5825 NL80211_REPLAY_CTR_LEN);
5826
5827 wdev_lock(wdev);
5828 if (!wdev->current_bss) {
5829 err = -ENOTCONN;
5830 goto out;
5831 }
5832
5833 if (!rdev->ops->set_rekey_data) {
5834 err = -EOPNOTSUPP;
5835 goto out;
5836 }
5837
5838 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5839 out:
5840 wdev_unlock(wdev);
5841 return err;
5842}
5843
Johannes Berg28946da2011-11-04 11:18:12 +01005844static int nl80211_register_unexpected_frame(struct sk_buff *skb,
5845 struct genl_info *info)
5846{
5847 struct net_device *dev = info->user_ptr[1];
5848 struct wireless_dev *wdev = dev->ieee80211_ptr;
5849
5850 if (wdev->iftype != NL80211_IFTYPE_AP &&
5851 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5852 return -EINVAL;
5853
5854 if (wdev->ap_unexpected_nlpid)
5855 return -EBUSY;
5856
5857 wdev->ap_unexpected_nlpid = info->snd_pid;
5858 return 0;
5859}
5860
Johannes Berg7f6cf312011-11-04 11:18:15 +01005861static int nl80211_probe_client(struct sk_buff *skb,
5862 struct genl_info *info)
5863{
5864 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5865 struct net_device *dev = info->user_ptr[1];
5866 struct wireless_dev *wdev = dev->ieee80211_ptr;
5867 struct sk_buff *msg;
5868 void *hdr;
5869 const u8 *addr;
5870 u64 cookie;
5871 int err;
5872
5873 if (wdev->iftype != NL80211_IFTYPE_AP &&
5874 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5875 return -EOPNOTSUPP;
5876
5877 if (!info->attrs[NL80211_ATTR_MAC])
5878 return -EINVAL;
5879
5880 if (!rdev->ops->probe_client)
5881 return -EOPNOTSUPP;
5882
5883 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5884 if (!msg)
5885 return -ENOMEM;
5886
5887 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5888 NL80211_CMD_PROBE_CLIENT);
5889
5890 if (IS_ERR(hdr)) {
5891 err = PTR_ERR(hdr);
5892 goto free_msg;
5893 }
5894
5895 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5896
5897 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
5898 if (err)
5899 goto free_msg;
5900
5901 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5902
5903 genlmsg_end(msg, hdr);
5904
5905 return genlmsg_reply(msg, info);
5906
5907 nla_put_failure:
5908 err = -ENOBUFS;
5909 free_msg:
5910 nlmsg_free(msg);
5911 return err;
5912}
5913
Johannes Berg5e760232011-11-04 11:18:17 +01005914static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
5915{
5916 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5917
5918 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
5919 return -EOPNOTSUPP;
5920
5921 if (rdev->ap_beacons_nlpid)
5922 return -EBUSY;
5923
5924 rdev->ap_beacons_nlpid = info->snd_pid;
5925
5926 return 0;
5927}
5928
Johannes Berg4c476992010-10-04 21:36:35 +02005929#define NL80211_FLAG_NEED_WIPHY 0x01
5930#define NL80211_FLAG_NEED_NETDEV 0x02
5931#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005932#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5933#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5934 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005935
5936static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5937 struct genl_info *info)
5938{
5939 struct cfg80211_registered_device *rdev;
5940 struct net_device *dev;
5941 int err;
5942 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5943
5944 if (rtnl)
5945 rtnl_lock();
5946
5947 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5948 rdev = cfg80211_get_dev_from_info(info);
5949 if (IS_ERR(rdev)) {
5950 if (rtnl)
5951 rtnl_unlock();
5952 return PTR_ERR(rdev);
5953 }
5954 info->user_ptr[0] = rdev;
5955 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5956 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5957 if (err) {
5958 if (rtnl)
5959 rtnl_unlock();
5960 return err;
5961 }
Johannes Berg41265712010-10-04 21:14:05 +02005962 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5963 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005964 cfg80211_unlock_rdev(rdev);
5965 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005966 if (rtnl)
5967 rtnl_unlock();
5968 return -ENETDOWN;
5969 }
Johannes Berg4c476992010-10-04 21:36:35 +02005970 info->user_ptr[0] = rdev;
5971 info->user_ptr[1] = dev;
5972 }
5973
5974 return 0;
5975}
5976
5977static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5978 struct genl_info *info)
5979{
5980 if (info->user_ptr[0])
5981 cfg80211_unlock_rdev(info->user_ptr[0]);
5982 if (info->user_ptr[1])
5983 dev_put(info->user_ptr[1]);
5984 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5985 rtnl_unlock();
5986}
5987
Johannes Berg55682962007-09-20 13:09:35 -04005988static struct genl_ops nl80211_ops[] = {
5989 {
5990 .cmd = NL80211_CMD_GET_WIPHY,
5991 .doit = nl80211_get_wiphy,
5992 .dumpit = nl80211_dump_wiphy,
5993 .policy = nl80211_policy,
5994 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005995 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005996 },
5997 {
5998 .cmd = NL80211_CMD_SET_WIPHY,
5999 .doit = nl80211_set_wiphy,
6000 .policy = nl80211_policy,
6001 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006002 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006003 },
6004 {
6005 .cmd = NL80211_CMD_GET_INTERFACE,
6006 .doit = nl80211_get_interface,
6007 .dumpit = nl80211_dump_interface,
6008 .policy = nl80211_policy,
6009 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006010 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006011 },
6012 {
6013 .cmd = NL80211_CMD_SET_INTERFACE,
6014 .doit = nl80211_set_interface,
6015 .policy = nl80211_policy,
6016 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006017 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6018 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006019 },
6020 {
6021 .cmd = NL80211_CMD_NEW_INTERFACE,
6022 .doit = nl80211_new_interface,
6023 .policy = nl80211_policy,
6024 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006025 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6026 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006027 },
6028 {
6029 .cmd = NL80211_CMD_DEL_INTERFACE,
6030 .doit = nl80211_del_interface,
6031 .policy = nl80211_policy,
6032 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006033 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6034 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006035 },
Johannes Berg41ade002007-12-19 02:03:29 +01006036 {
6037 .cmd = NL80211_CMD_GET_KEY,
6038 .doit = nl80211_get_key,
6039 .policy = nl80211_policy,
6040 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006041 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6042 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006043 },
6044 {
6045 .cmd = NL80211_CMD_SET_KEY,
6046 .doit = nl80211_set_key,
6047 .policy = nl80211_policy,
6048 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006049 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006050 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006051 },
6052 {
6053 .cmd = NL80211_CMD_NEW_KEY,
6054 .doit = nl80211_new_key,
6055 .policy = nl80211_policy,
6056 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006058 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006059 },
6060 {
6061 .cmd = NL80211_CMD_DEL_KEY,
6062 .doit = nl80211_del_key,
6063 .policy = nl80211_policy,
6064 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006066 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006067 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006068 {
6069 .cmd = NL80211_CMD_SET_BEACON,
6070 .policy = nl80211_policy,
6071 .flags = GENL_ADMIN_PERM,
6072 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006073 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6074 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006075 },
6076 {
6077 .cmd = NL80211_CMD_NEW_BEACON,
6078 .policy = nl80211_policy,
6079 .flags = GENL_ADMIN_PERM,
6080 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006081 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6082 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006083 },
6084 {
6085 .cmd = NL80211_CMD_DEL_BEACON,
6086 .policy = nl80211_policy,
6087 .flags = GENL_ADMIN_PERM,
6088 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006089 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6090 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006091 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006092 {
6093 .cmd = NL80211_CMD_GET_STATION,
6094 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006095 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006096 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006097 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6098 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006099 },
6100 {
6101 .cmd = NL80211_CMD_SET_STATION,
6102 .doit = nl80211_set_station,
6103 .policy = nl80211_policy,
6104 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006105 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6106 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006107 },
6108 {
6109 .cmd = NL80211_CMD_NEW_STATION,
6110 .doit = nl80211_new_station,
6111 .policy = nl80211_policy,
6112 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006113 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006114 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006115 },
6116 {
6117 .cmd = NL80211_CMD_DEL_STATION,
6118 .doit = nl80211_del_station,
6119 .policy = nl80211_policy,
6120 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006121 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6122 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006123 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006124 {
6125 .cmd = NL80211_CMD_GET_MPATH,
6126 .doit = nl80211_get_mpath,
6127 .dumpit = nl80211_dump_mpath,
6128 .policy = nl80211_policy,
6129 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006130 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006131 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006132 },
6133 {
6134 .cmd = NL80211_CMD_SET_MPATH,
6135 .doit = nl80211_set_mpath,
6136 .policy = nl80211_policy,
6137 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006138 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006139 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006140 },
6141 {
6142 .cmd = NL80211_CMD_NEW_MPATH,
6143 .doit = nl80211_new_mpath,
6144 .policy = nl80211_policy,
6145 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006146 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006147 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006148 },
6149 {
6150 .cmd = NL80211_CMD_DEL_MPATH,
6151 .doit = nl80211_del_mpath,
6152 .policy = nl80211_policy,
6153 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006154 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6155 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006156 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006157 {
6158 .cmd = NL80211_CMD_SET_BSS,
6159 .doit = nl80211_set_bss,
6160 .policy = nl80211_policy,
6161 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006162 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6163 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006164 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006165 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006166 .cmd = NL80211_CMD_GET_REG,
6167 .doit = nl80211_get_reg,
6168 .policy = nl80211_policy,
6169 /* can be retrieved by unprivileged users */
6170 },
6171 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006172 .cmd = NL80211_CMD_SET_REG,
6173 .doit = nl80211_set_reg,
6174 .policy = nl80211_policy,
6175 .flags = GENL_ADMIN_PERM,
6176 },
6177 {
6178 .cmd = NL80211_CMD_REQ_SET_REG,
6179 .doit = nl80211_req_set_reg,
6180 .policy = nl80211_policy,
6181 .flags = GENL_ADMIN_PERM,
6182 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006183 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006184 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6185 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006186 .policy = nl80211_policy,
6187 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006188 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6189 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006190 },
6191 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006192 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6193 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006194 .policy = nl80211_policy,
6195 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006196 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006197 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006198 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006199 {
Johannes Berg2a519312009-02-10 21:25:55 +01006200 .cmd = NL80211_CMD_TRIGGER_SCAN,
6201 .doit = nl80211_trigger_scan,
6202 .policy = nl80211_policy,
6203 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006204 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006205 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006206 },
6207 {
6208 .cmd = NL80211_CMD_GET_SCAN,
6209 .policy = nl80211_policy,
6210 .dumpit = nl80211_dump_scan,
6211 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006212 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006213 .cmd = NL80211_CMD_START_SCHED_SCAN,
6214 .doit = nl80211_start_sched_scan,
6215 .policy = nl80211_policy,
6216 .flags = GENL_ADMIN_PERM,
6217 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6218 NL80211_FLAG_NEED_RTNL,
6219 },
6220 {
6221 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6222 .doit = nl80211_stop_sched_scan,
6223 .policy = nl80211_policy,
6224 .flags = GENL_ADMIN_PERM,
6225 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6226 NL80211_FLAG_NEED_RTNL,
6227 },
6228 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006229 .cmd = NL80211_CMD_AUTHENTICATE,
6230 .doit = nl80211_authenticate,
6231 .policy = nl80211_policy,
6232 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006233 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006234 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006235 },
6236 {
6237 .cmd = NL80211_CMD_ASSOCIATE,
6238 .doit = nl80211_associate,
6239 .policy = nl80211_policy,
6240 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006241 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006242 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006243 },
6244 {
6245 .cmd = NL80211_CMD_DEAUTHENTICATE,
6246 .doit = nl80211_deauthenticate,
6247 .policy = nl80211_policy,
6248 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006249 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006250 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006251 },
6252 {
6253 .cmd = NL80211_CMD_DISASSOCIATE,
6254 .doit = nl80211_disassociate,
6255 .policy = nl80211_policy,
6256 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006257 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006258 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006259 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006260 {
6261 .cmd = NL80211_CMD_JOIN_IBSS,
6262 .doit = nl80211_join_ibss,
6263 .policy = nl80211_policy,
6264 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006265 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006266 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006267 },
6268 {
6269 .cmd = NL80211_CMD_LEAVE_IBSS,
6270 .doit = nl80211_leave_ibss,
6271 .policy = nl80211_policy,
6272 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006273 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006274 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006275 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006276#ifdef CONFIG_NL80211_TESTMODE
6277 {
6278 .cmd = NL80211_CMD_TESTMODE,
6279 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006280 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006281 .policy = nl80211_policy,
6282 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006283 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6284 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006285 },
6286#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006287 {
6288 .cmd = NL80211_CMD_CONNECT,
6289 .doit = nl80211_connect,
6290 .policy = nl80211_policy,
6291 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006292 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006293 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006294 },
6295 {
6296 .cmd = NL80211_CMD_DISCONNECT,
6297 .doit = nl80211_disconnect,
6298 .policy = nl80211_policy,
6299 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006300 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006301 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006302 },
Johannes Berg463d0182009-07-14 00:33:35 +02006303 {
6304 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6305 .doit = nl80211_wiphy_netns,
6306 .policy = nl80211_policy,
6307 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006308 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6309 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006310 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006311 {
6312 .cmd = NL80211_CMD_GET_SURVEY,
6313 .policy = nl80211_policy,
6314 .dumpit = nl80211_dump_survey,
6315 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006316 {
6317 .cmd = NL80211_CMD_SET_PMKSA,
6318 .doit = nl80211_setdel_pmksa,
6319 .policy = nl80211_policy,
6320 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006321 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6322 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006323 },
6324 {
6325 .cmd = NL80211_CMD_DEL_PMKSA,
6326 .doit = nl80211_setdel_pmksa,
6327 .policy = nl80211_policy,
6328 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006329 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6330 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006331 },
6332 {
6333 .cmd = NL80211_CMD_FLUSH_PMKSA,
6334 .doit = nl80211_flush_pmksa,
6335 .policy = nl80211_policy,
6336 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006337 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6338 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006339 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006340 {
6341 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6342 .doit = nl80211_remain_on_channel,
6343 .policy = nl80211_policy,
6344 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006345 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006346 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006347 },
6348 {
6349 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6350 .doit = nl80211_cancel_remain_on_channel,
6351 .policy = nl80211_policy,
6352 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006353 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006354 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006355 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006356 {
6357 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6358 .doit = nl80211_set_tx_bitrate_mask,
6359 .policy = nl80211_policy,
6360 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006361 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6362 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006363 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006364 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006365 .cmd = NL80211_CMD_REGISTER_FRAME,
6366 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006367 .policy = nl80211_policy,
6368 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006369 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6370 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006371 },
6372 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006373 .cmd = NL80211_CMD_FRAME,
6374 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006375 .policy = nl80211_policy,
6376 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006377 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006378 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006379 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006380 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006381 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6382 .doit = nl80211_tx_mgmt_cancel_wait,
6383 .policy = nl80211_policy,
6384 .flags = GENL_ADMIN_PERM,
6385 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6386 NL80211_FLAG_NEED_RTNL,
6387 },
6388 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006389 .cmd = NL80211_CMD_SET_POWER_SAVE,
6390 .doit = nl80211_set_power_save,
6391 .policy = nl80211_policy,
6392 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006393 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6394 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006395 },
6396 {
6397 .cmd = NL80211_CMD_GET_POWER_SAVE,
6398 .doit = nl80211_get_power_save,
6399 .policy = nl80211_policy,
6400 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006401 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6402 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006403 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006404 {
6405 .cmd = NL80211_CMD_SET_CQM,
6406 .doit = nl80211_set_cqm,
6407 .policy = nl80211_policy,
6408 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006409 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6410 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006411 },
Johannes Bergf444de02010-05-05 15:25:02 +02006412 {
6413 .cmd = NL80211_CMD_SET_CHANNEL,
6414 .doit = nl80211_set_channel,
6415 .policy = nl80211_policy,
6416 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006417 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6418 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006419 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006420 {
6421 .cmd = NL80211_CMD_SET_WDS_PEER,
6422 .doit = nl80211_set_wds_peer,
6423 .policy = nl80211_policy,
6424 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006425 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6426 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006427 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006428 {
6429 .cmd = NL80211_CMD_JOIN_MESH,
6430 .doit = nl80211_join_mesh,
6431 .policy = nl80211_policy,
6432 .flags = GENL_ADMIN_PERM,
6433 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6434 NL80211_FLAG_NEED_RTNL,
6435 },
6436 {
6437 .cmd = NL80211_CMD_LEAVE_MESH,
6438 .doit = nl80211_leave_mesh,
6439 .policy = nl80211_policy,
6440 .flags = GENL_ADMIN_PERM,
6441 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6442 NL80211_FLAG_NEED_RTNL,
6443 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006444 {
6445 .cmd = NL80211_CMD_GET_WOWLAN,
6446 .doit = nl80211_get_wowlan,
6447 .policy = nl80211_policy,
6448 /* can be retrieved by unprivileged users */
6449 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6450 NL80211_FLAG_NEED_RTNL,
6451 },
6452 {
6453 .cmd = NL80211_CMD_SET_WOWLAN,
6454 .doit = nl80211_set_wowlan,
6455 .policy = nl80211_policy,
6456 .flags = GENL_ADMIN_PERM,
6457 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6458 NL80211_FLAG_NEED_RTNL,
6459 },
Johannes Berge5497d72011-07-05 16:35:40 +02006460 {
6461 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6462 .doit = nl80211_set_rekey_data,
6463 .policy = nl80211_policy,
6464 .flags = GENL_ADMIN_PERM,
6465 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6466 NL80211_FLAG_NEED_RTNL,
6467 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006468 {
6469 .cmd = NL80211_CMD_TDLS_MGMT,
6470 .doit = nl80211_tdls_mgmt,
6471 .policy = nl80211_policy,
6472 .flags = GENL_ADMIN_PERM,
6473 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6474 NL80211_FLAG_NEED_RTNL,
6475 },
6476 {
6477 .cmd = NL80211_CMD_TDLS_OPER,
6478 .doit = nl80211_tdls_oper,
6479 .policy = nl80211_policy,
6480 .flags = GENL_ADMIN_PERM,
6481 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6482 NL80211_FLAG_NEED_RTNL,
6483 },
Johannes Berg28946da2011-11-04 11:18:12 +01006484 {
6485 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6486 .doit = nl80211_register_unexpected_frame,
6487 .policy = nl80211_policy,
6488 .flags = GENL_ADMIN_PERM,
6489 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6490 NL80211_FLAG_NEED_RTNL,
6491 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006492 {
6493 .cmd = NL80211_CMD_PROBE_CLIENT,
6494 .doit = nl80211_probe_client,
6495 .policy = nl80211_policy,
6496 .flags = GENL_ADMIN_PERM,
6497 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6498 NL80211_FLAG_NEED_RTNL,
6499 },
Johannes Berg5e760232011-11-04 11:18:17 +01006500 {
6501 .cmd = NL80211_CMD_REGISTER_BEACONS,
6502 .doit = nl80211_register_beacons,
6503 .policy = nl80211_policy,
6504 .flags = GENL_ADMIN_PERM,
6505 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6506 NL80211_FLAG_NEED_RTNL,
6507 },
Johannes Berg55682962007-09-20 13:09:35 -04006508};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006509
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006510static struct genl_multicast_group nl80211_mlme_mcgrp = {
6511 .name = "mlme",
6512};
Johannes Berg55682962007-09-20 13:09:35 -04006513
6514/* multicast groups */
6515static struct genl_multicast_group nl80211_config_mcgrp = {
6516 .name = "config",
6517};
Johannes Berg2a519312009-02-10 21:25:55 +01006518static struct genl_multicast_group nl80211_scan_mcgrp = {
6519 .name = "scan",
6520};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006521static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6522 .name = "regulatory",
6523};
Johannes Berg55682962007-09-20 13:09:35 -04006524
6525/* notification functions */
6526
6527void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6528{
6529 struct sk_buff *msg;
6530
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006531 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006532 if (!msg)
6533 return;
6534
6535 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6536 nlmsg_free(msg);
6537 return;
6538 }
6539
Johannes Berg463d0182009-07-14 00:33:35 +02006540 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6541 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006542}
6543
Johannes Berg362a4152009-05-24 16:43:15 +02006544static int nl80211_add_scan_req(struct sk_buff *msg,
6545 struct cfg80211_registered_device *rdev)
6546{
6547 struct cfg80211_scan_request *req = rdev->scan_req;
6548 struct nlattr *nest;
6549 int i;
6550
Johannes Berg667503dd2009-07-07 03:56:11 +02006551 ASSERT_RDEV_LOCK(rdev);
6552
Johannes Berg362a4152009-05-24 16:43:15 +02006553 if (WARN_ON(!req))
6554 return 0;
6555
6556 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6557 if (!nest)
6558 goto nla_put_failure;
6559 for (i = 0; i < req->n_ssids; i++)
6560 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6561 nla_nest_end(msg, nest);
6562
6563 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6564 if (!nest)
6565 goto nla_put_failure;
6566 for (i = 0; i < req->n_channels; i++)
6567 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6568 nla_nest_end(msg, nest);
6569
6570 if (req->ie)
6571 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6572
6573 return 0;
6574 nla_put_failure:
6575 return -ENOBUFS;
6576}
6577
Johannes Berga538e2d2009-06-16 19:56:42 +02006578static int nl80211_send_scan_msg(struct sk_buff *msg,
6579 struct cfg80211_registered_device *rdev,
6580 struct net_device *netdev,
6581 u32 pid, u32 seq, int flags,
6582 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006583{
6584 void *hdr;
6585
6586 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6587 if (!hdr)
6588 return -1;
6589
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006590 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006591 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6592
Johannes Berg362a4152009-05-24 16:43:15 +02006593 /* ignore errors and send incomplete event anyway */
6594 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006595
6596 return genlmsg_end(msg, hdr);
6597
6598 nla_put_failure:
6599 genlmsg_cancel(msg, hdr);
6600 return -EMSGSIZE;
6601}
6602
Luciano Coelho807f8a82011-05-11 17:09:35 +03006603static int
6604nl80211_send_sched_scan_msg(struct sk_buff *msg,
6605 struct cfg80211_registered_device *rdev,
6606 struct net_device *netdev,
6607 u32 pid, u32 seq, int flags, u32 cmd)
6608{
6609 void *hdr;
6610
6611 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6612 if (!hdr)
6613 return -1;
6614
6615 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6616 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6617
6618 return genlmsg_end(msg, hdr);
6619
6620 nla_put_failure:
6621 genlmsg_cancel(msg, hdr);
6622 return -EMSGSIZE;
6623}
6624
Johannes Berga538e2d2009-06-16 19:56:42 +02006625void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6626 struct net_device *netdev)
6627{
6628 struct sk_buff *msg;
6629
6630 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6631 if (!msg)
6632 return;
6633
6634 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6635 NL80211_CMD_TRIGGER_SCAN) < 0) {
6636 nlmsg_free(msg);
6637 return;
6638 }
6639
Johannes Berg463d0182009-07-14 00:33:35 +02006640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6641 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006642}
6643
Johannes Berg2a519312009-02-10 21:25:55 +01006644void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6645 struct net_device *netdev)
6646{
6647 struct sk_buff *msg;
6648
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006650 if (!msg)
6651 return;
6652
Johannes Berga538e2d2009-06-16 19:56:42 +02006653 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6654 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006655 nlmsg_free(msg);
6656 return;
6657 }
6658
Johannes Berg463d0182009-07-14 00:33:35 +02006659 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6660 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006661}
6662
6663void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6664 struct net_device *netdev)
6665{
6666 struct sk_buff *msg;
6667
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006669 if (!msg)
6670 return;
6671
Johannes Berga538e2d2009-06-16 19:56:42 +02006672 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6673 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006674 nlmsg_free(msg);
6675 return;
6676 }
6677
Johannes Berg463d0182009-07-14 00:33:35 +02006678 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6679 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006680}
6681
Luciano Coelho807f8a82011-05-11 17:09:35 +03006682void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6683 struct net_device *netdev)
6684{
6685 struct sk_buff *msg;
6686
6687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6688 if (!msg)
6689 return;
6690
6691 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6692 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6693 nlmsg_free(msg);
6694 return;
6695 }
6696
6697 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6698 nl80211_scan_mcgrp.id, GFP_KERNEL);
6699}
6700
6701void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6702 struct net_device *netdev, u32 cmd)
6703{
6704 struct sk_buff *msg;
6705
6706 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6707 if (!msg)
6708 return;
6709
6710 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6711 nlmsg_free(msg);
6712 return;
6713 }
6714
6715 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6716 nl80211_scan_mcgrp.id, GFP_KERNEL);
6717}
6718
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006719/*
6720 * This can happen on global regulatory changes or device specific settings
6721 * based on custom world regulatory domains.
6722 */
6723void nl80211_send_reg_change_event(struct regulatory_request *request)
6724{
6725 struct sk_buff *msg;
6726 void *hdr;
6727
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006728 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006729 if (!msg)
6730 return;
6731
6732 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6733 if (!hdr) {
6734 nlmsg_free(msg);
6735 return;
6736 }
6737
6738 /* Userspace can always count this one always being set */
6739 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6740
6741 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6742 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6743 NL80211_REGDOM_TYPE_WORLD);
6744 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6745 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6746 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6747 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6748 request->intersect)
6749 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6750 NL80211_REGDOM_TYPE_INTERSECTION);
6751 else {
6752 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6753 NL80211_REGDOM_TYPE_COUNTRY);
6754 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6755 }
6756
6757 if (wiphy_idx_valid(request->wiphy_idx))
6758 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6759
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006760 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006761
Johannes Bergbc43b282009-07-25 10:54:13 +02006762 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006763 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006764 GFP_ATOMIC);
6765 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006766
6767 return;
6768
6769nla_put_failure:
6770 genlmsg_cancel(msg, hdr);
6771 nlmsg_free(msg);
6772}
6773
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006774static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6775 struct net_device *netdev,
6776 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006777 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006778{
6779 struct sk_buff *msg;
6780 void *hdr;
6781
Johannes Berge6d6e342009-07-01 21:26:47 +02006782 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006783 if (!msg)
6784 return;
6785
6786 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6787 if (!hdr) {
6788 nlmsg_free(msg);
6789 return;
6790 }
6791
6792 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6793 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6794 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6795
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006796 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006797
Johannes Berg463d0182009-07-14 00:33:35 +02006798 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6799 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006800 return;
6801
6802 nla_put_failure:
6803 genlmsg_cancel(msg, hdr);
6804 nlmsg_free(msg);
6805}
6806
6807void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006808 struct net_device *netdev, const u8 *buf,
6809 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006810{
6811 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006812 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006813}
6814
6815void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6816 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006817 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006818{
Johannes Berge6d6e342009-07-01 21:26:47 +02006819 nl80211_send_mlme_event(rdev, netdev, buf, len,
6820 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006821}
6822
Jouni Malinen53b46b82009-03-27 20:53:56 +02006823void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006824 struct net_device *netdev, const u8 *buf,
6825 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006826{
6827 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006828 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006829}
6830
Jouni Malinen53b46b82009-03-27 20:53:56 +02006831void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6832 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006833 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006834{
6835 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006836 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006837}
6838
Jouni Malinencf4e5942010-12-16 00:52:40 +02006839void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6840 struct net_device *netdev, const u8 *buf,
6841 size_t len, gfp_t gfp)
6842{
6843 nl80211_send_mlme_event(rdev, netdev, buf, len,
6844 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6845}
6846
6847void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6848 struct net_device *netdev, const u8 *buf,
6849 size_t len, gfp_t gfp)
6850{
6851 nl80211_send_mlme_event(rdev, netdev, buf, len,
6852 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6853}
6854
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006855static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6856 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006857 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006858{
6859 struct sk_buff *msg;
6860 void *hdr;
6861
Johannes Berge6d6e342009-07-01 21:26:47 +02006862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006863 if (!msg)
6864 return;
6865
6866 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6867 if (!hdr) {
6868 nlmsg_free(msg);
6869 return;
6870 }
6871
6872 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6873 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6874 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6875 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6876
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006877 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03006878
Johannes Berg463d0182009-07-14 00:33:35 +02006879 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6880 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006881 return;
6882
6883 nla_put_failure:
6884 genlmsg_cancel(msg, hdr);
6885 nlmsg_free(msg);
6886}
6887
6888void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006889 struct net_device *netdev, const u8 *addr,
6890 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006891{
6892 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006893 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006894}
6895
6896void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006897 struct net_device *netdev, const u8 *addr,
6898 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006899{
Johannes Berge6d6e342009-07-01 21:26:47 +02006900 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6901 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006902}
6903
Samuel Ortizb23aa672009-07-01 21:26:54 +02006904void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6905 struct net_device *netdev, const u8 *bssid,
6906 const u8 *req_ie, size_t req_ie_len,
6907 const u8 *resp_ie, size_t resp_ie_len,
6908 u16 status, gfp_t gfp)
6909{
6910 struct sk_buff *msg;
6911 void *hdr;
6912
6913 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6914 if (!msg)
6915 return;
6916
6917 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6918 if (!hdr) {
6919 nlmsg_free(msg);
6920 return;
6921 }
6922
6923 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6924 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6925 if (bssid)
6926 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6927 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6928 if (req_ie)
6929 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6930 if (resp_ie)
6931 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6932
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006933 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006934
Johannes Berg463d0182009-07-14 00:33:35 +02006935 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6936 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006937 return;
6938
6939 nla_put_failure:
6940 genlmsg_cancel(msg, hdr);
6941 nlmsg_free(msg);
6942
6943}
6944
6945void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6946 struct net_device *netdev, const u8 *bssid,
6947 const u8 *req_ie, size_t req_ie_len,
6948 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6949{
6950 struct sk_buff *msg;
6951 void *hdr;
6952
6953 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6954 if (!msg)
6955 return;
6956
6957 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6958 if (!hdr) {
6959 nlmsg_free(msg);
6960 return;
6961 }
6962
6963 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6964 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6965 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6966 if (req_ie)
6967 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6968 if (resp_ie)
6969 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6970
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006971 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006972
Johannes Berg463d0182009-07-14 00:33:35 +02006973 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6974 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006975 return;
6976
6977 nla_put_failure:
6978 genlmsg_cancel(msg, hdr);
6979 nlmsg_free(msg);
6980
6981}
6982
6983void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6984 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02006985 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006986{
6987 struct sk_buff *msg;
6988 void *hdr;
6989
Johannes Berg667503dd2009-07-07 03:56:11 +02006990 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006991 if (!msg)
6992 return;
6993
6994 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6995 if (!hdr) {
6996 nlmsg_free(msg);
6997 return;
6998 }
6999
7000 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7001 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7002 if (from_ap && reason)
7003 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7004 if (from_ap)
7005 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7006 if (ie)
7007 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7008
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007009 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007010
Johannes Berg463d0182009-07-14 00:33:35 +02007011 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7012 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007013 return;
7014
7015 nla_put_failure:
7016 genlmsg_cancel(msg, hdr);
7017 nlmsg_free(msg);
7018
7019}
7020
Johannes Berg04a773a2009-04-19 21:24:32 +02007021void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7022 struct net_device *netdev, const u8 *bssid,
7023 gfp_t gfp)
7024{
7025 struct sk_buff *msg;
7026 void *hdr;
7027
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007028 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007029 if (!msg)
7030 return;
7031
7032 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7033 if (!hdr) {
7034 nlmsg_free(msg);
7035 return;
7036 }
7037
7038 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7039 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7040 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7041
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007042 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007043
Johannes Berg463d0182009-07-14 00:33:35 +02007044 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7045 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007046 return;
7047
7048 nla_put_failure:
7049 genlmsg_cancel(msg, hdr);
7050 nlmsg_free(msg);
7051}
7052
Javier Cardonac93b5e72011-04-07 15:08:34 -07007053void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7054 struct net_device *netdev,
7055 const u8 *macaddr, const u8* ie, u8 ie_len,
7056 gfp_t gfp)
7057{
7058 struct sk_buff *msg;
7059 void *hdr;
7060
7061 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7062 if (!msg)
7063 return;
7064
7065 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7066 if (!hdr) {
7067 nlmsg_free(msg);
7068 return;
7069 }
7070
7071 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7072 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7073 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7074 if (ie_len && ie)
7075 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7076
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007077 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007078
7079 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7080 nl80211_mlme_mcgrp.id, gfp);
7081 return;
7082
7083 nla_put_failure:
7084 genlmsg_cancel(msg, hdr);
7085 nlmsg_free(msg);
7086}
7087
Jouni Malinena3b8b052009-03-27 21:59:49 +02007088void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7089 struct net_device *netdev, const u8 *addr,
7090 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007091 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007092{
7093 struct sk_buff *msg;
7094 void *hdr;
7095
Johannes Berge6d6e342009-07-01 21:26:47 +02007096 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007097 if (!msg)
7098 return;
7099
7100 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7101 if (!hdr) {
7102 nlmsg_free(msg);
7103 return;
7104 }
7105
7106 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7108 if (addr)
7109 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7110 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007111 if (key_id != -1)
7112 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007113 if (tsc)
7114 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7115
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007116 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007117
Johannes Berg463d0182009-07-14 00:33:35 +02007118 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7119 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007120 return;
7121
7122 nla_put_failure:
7123 genlmsg_cancel(msg, hdr);
7124 nlmsg_free(msg);
7125}
7126
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007127void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7128 struct ieee80211_channel *channel_before,
7129 struct ieee80211_channel *channel_after)
7130{
7131 struct sk_buff *msg;
7132 void *hdr;
7133 struct nlattr *nl_freq;
7134
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007135 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007136 if (!msg)
7137 return;
7138
7139 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7140 if (!hdr) {
7141 nlmsg_free(msg);
7142 return;
7143 }
7144
7145 /*
7146 * Since we are applying the beacon hint to a wiphy we know its
7147 * wiphy_idx is valid
7148 */
7149 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7150
7151 /* Before */
7152 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7153 if (!nl_freq)
7154 goto nla_put_failure;
7155 if (nl80211_msg_put_channel(msg, channel_before))
7156 goto nla_put_failure;
7157 nla_nest_end(msg, nl_freq);
7158
7159 /* After */
7160 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7161 if (!nl_freq)
7162 goto nla_put_failure;
7163 if (nl80211_msg_put_channel(msg, channel_after))
7164 goto nla_put_failure;
7165 nla_nest_end(msg, nl_freq);
7166
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007167 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007168
Johannes Berg463d0182009-07-14 00:33:35 +02007169 rcu_read_lock();
7170 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7171 GFP_ATOMIC);
7172 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007173
7174 return;
7175
7176nla_put_failure:
7177 genlmsg_cancel(msg, hdr);
7178 nlmsg_free(msg);
7179}
7180
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007181static void nl80211_send_remain_on_chan_event(
7182 int cmd, struct cfg80211_registered_device *rdev,
7183 struct net_device *netdev, u64 cookie,
7184 struct ieee80211_channel *chan,
7185 enum nl80211_channel_type channel_type,
7186 unsigned int duration, gfp_t gfp)
7187{
7188 struct sk_buff *msg;
7189 void *hdr;
7190
7191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7192 if (!msg)
7193 return;
7194
7195 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7196 if (!hdr) {
7197 nlmsg_free(msg);
7198 return;
7199 }
7200
7201 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7202 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7203 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7204 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7205 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7206
7207 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7208 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7209
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007210 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007211
7212 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7213 nl80211_mlme_mcgrp.id, gfp);
7214 return;
7215
7216 nla_put_failure:
7217 genlmsg_cancel(msg, hdr);
7218 nlmsg_free(msg);
7219}
7220
7221void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7222 struct net_device *netdev, u64 cookie,
7223 struct ieee80211_channel *chan,
7224 enum nl80211_channel_type channel_type,
7225 unsigned int duration, gfp_t gfp)
7226{
7227 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7228 rdev, netdev, cookie, chan,
7229 channel_type, duration, gfp);
7230}
7231
7232void nl80211_send_remain_on_channel_cancel(
7233 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7234 u64 cookie, struct ieee80211_channel *chan,
7235 enum nl80211_channel_type channel_type, gfp_t gfp)
7236{
7237 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7238 rdev, netdev, cookie, chan,
7239 channel_type, 0, gfp);
7240}
7241
Johannes Berg98b62182009-12-23 13:15:44 +01007242void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7243 struct net_device *dev, const u8 *mac_addr,
7244 struct station_info *sinfo, gfp_t gfp)
7245{
7246 struct sk_buff *msg;
7247
7248 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7249 if (!msg)
7250 return;
7251
7252 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7253 nlmsg_free(msg);
7254 return;
7255 }
7256
7257 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7258 nl80211_mlme_mcgrp.id, gfp);
7259}
7260
Jouni Malinenec15e682011-03-23 15:29:52 +02007261void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7262 struct net_device *dev, const u8 *mac_addr,
7263 gfp_t gfp)
7264{
7265 struct sk_buff *msg;
7266 void *hdr;
7267
7268 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7269 if (!msg)
7270 return;
7271
7272 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7273 if (!hdr) {
7274 nlmsg_free(msg);
7275 return;
7276 }
7277
7278 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7279 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7280
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007281 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007282
7283 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7284 nl80211_mlme_mcgrp.id, gfp);
7285 return;
7286
7287 nla_put_failure:
7288 genlmsg_cancel(msg, hdr);
7289 nlmsg_free(msg);
7290}
7291
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007292static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7293 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007294{
7295 struct wireless_dev *wdev = dev->ieee80211_ptr;
7296 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7297 struct sk_buff *msg;
7298 void *hdr;
7299 int err;
7300 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7301
7302 if (!nlpid)
7303 return false;
7304
7305 msg = nlmsg_new(100, gfp);
7306 if (!msg)
7307 return true;
7308
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007309 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007310 if (!hdr) {
7311 nlmsg_free(msg);
7312 return true;
7313 }
7314
7315 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7316 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7317 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7318
7319 err = genlmsg_end(msg, hdr);
7320 if (err < 0) {
7321 nlmsg_free(msg);
7322 return true;
7323 }
7324
7325 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7326 return true;
7327
7328 nla_put_failure:
7329 genlmsg_cancel(msg, hdr);
7330 nlmsg_free(msg);
7331 return true;
7332}
7333
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007334bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7335{
7336 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7337 addr, gfp);
7338}
7339
7340bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7341 const u8 *addr, gfp_t gfp)
7342{
7343 return __nl80211_unexpected_frame(dev,
7344 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7345 addr, gfp);
7346}
7347
Johannes Berg2e161f72010-08-12 15:38:38 +02007348int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7349 struct net_device *netdev, u32 nlpid,
7350 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007351{
7352 struct sk_buff *msg;
7353 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007354
7355 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7356 if (!msg)
7357 return -ENOMEM;
7358
Johannes Berg2e161f72010-08-12 15:38:38 +02007359 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007360 if (!hdr) {
7361 nlmsg_free(msg);
7362 return -ENOMEM;
7363 }
7364
7365 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7366 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7367 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7368 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7369
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007370 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007371
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007372 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007373
7374 nla_put_failure:
7375 genlmsg_cancel(msg, hdr);
7376 nlmsg_free(msg);
7377 return -ENOBUFS;
7378}
7379
Johannes Berg2e161f72010-08-12 15:38:38 +02007380void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7381 struct net_device *netdev, u64 cookie,
7382 const u8 *buf, size_t len, bool ack,
7383 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007384{
7385 struct sk_buff *msg;
7386 void *hdr;
7387
7388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7389 if (!msg)
7390 return;
7391
Johannes Berg2e161f72010-08-12 15:38:38 +02007392 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007393 if (!hdr) {
7394 nlmsg_free(msg);
7395 return;
7396 }
7397
7398 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7399 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7400 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7401 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7402 if (ack)
7403 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7404
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007405 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007406
7407 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7408 return;
7409
7410 nla_put_failure:
7411 genlmsg_cancel(msg, hdr);
7412 nlmsg_free(msg);
7413}
7414
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007415void
7416nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7417 struct net_device *netdev,
7418 enum nl80211_cqm_rssi_threshold_event rssi_event,
7419 gfp_t gfp)
7420{
7421 struct sk_buff *msg;
7422 struct nlattr *pinfoattr;
7423 void *hdr;
7424
7425 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7426 if (!msg)
7427 return;
7428
7429 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7430 if (!hdr) {
7431 nlmsg_free(msg);
7432 return;
7433 }
7434
7435 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7436 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7437
7438 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7439 if (!pinfoattr)
7440 goto nla_put_failure;
7441
7442 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7443 rssi_event);
7444
7445 nla_nest_end(msg, pinfoattr);
7446
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007447 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007448
7449 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7450 nl80211_mlme_mcgrp.id, gfp);
7451 return;
7452
7453 nla_put_failure:
7454 genlmsg_cancel(msg, hdr);
7455 nlmsg_free(msg);
7456}
7457
Johannes Berge5497d72011-07-05 16:35:40 +02007458void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7459 struct net_device *netdev, const u8 *bssid,
7460 const u8 *replay_ctr, gfp_t gfp)
7461{
7462 struct sk_buff *msg;
7463 struct nlattr *rekey_attr;
7464 void *hdr;
7465
7466 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7467 if (!msg)
7468 return;
7469
7470 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7471 if (!hdr) {
7472 nlmsg_free(msg);
7473 return;
7474 }
7475
7476 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7477 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7478 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7479
7480 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7481 if (!rekey_attr)
7482 goto nla_put_failure;
7483
7484 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7485 NL80211_REPLAY_CTR_LEN, replay_ctr);
7486
7487 nla_nest_end(msg, rekey_attr);
7488
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007489 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007490
7491 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7492 nl80211_mlme_mcgrp.id, gfp);
7493 return;
7494
7495 nla_put_failure:
7496 genlmsg_cancel(msg, hdr);
7497 nlmsg_free(msg);
7498}
7499
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007500void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7501 struct net_device *netdev, int index,
7502 const u8 *bssid, bool preauth, gfp_t gfp)
7503{
7504 struct sk_buff *msg;
7505 struct nlattr *attr;
7506 void *hdr;
7507
7508 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7509 if (!msg)
7510 return;
7511
7512 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7513 if (!hdr) {
7514 nlmsg_free(msg);
7515 return;
7516 }
7517
7518 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7519 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7520
7521 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7522 if (!attr)
7523 goto nla_put_failure;
7524
7525 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7526 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7527 if (preauth)
7528 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7529
7530 nla_nest_end(msg, attr);
7531
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007532 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007533
7534 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7535 nl80211_mlme_mcgrp.id, gfp);
7536 return;
7537
7538 nla_put_failure:
7539 genlmsg_cancel(msg, hdr);
7540 nlmsg_free(msg);
7541}
7542
Johannes Bergc063dbf2010-11-24 08:10:05 +01007543void
7544nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7545 struct net_device *netdev, const u8 *peer,
7546 u32 num_packets, gfp_t gfp)
7547{
7548 struct sk_buff *msg;
7549 struct nlattr *pinfoattr;
7550 void *hdr;
7551
7552 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7553 if (!msg)
7554 return;
7555
7556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7557 if (!hdr) {
7558 nlmsg_free(msg);
7559 return;
7560 }
7561
7562 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7563 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7564 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7565
7566 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7567 if (!pinfoattr)
7568 goto nla_put_failure;
7569
7570 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7571
7572 nla_nest_end(msg, pinfoattr);
7573
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007574 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007575
7576 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7577 nl80211_mlme_mcgrp.id, gfp);
7578 return;
7579
7580 nla_put_failure:
7581 genlmsg_cancel(msg, hdr);
7582 nlmsg_free(msg);
7583}
7584
Johannes Berg7f6cf312011-11-04 11:18:15 +01007585void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7586 u64 cookie, bool acked, gfp_t gfp)
7587{
7588 struct wireless_dev *wdev = dev->ieee80211_ptr;
7589 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7590 struct sk_buff *msg;
7591 void *hdr;
7592 int err;
7593
7594 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7595 if (!msg)
7596 return;
7597
7598 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7599 if (!hdr) {
7600 nlmsg_free(msg);
7601 return;
7602 }
7603
7604 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7605 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7606 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7607 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7608 if (acked)
7609 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7610
7611 err = genlmsg_end(msg, hdr);
7612 if (err < 0) {
7613 nlmsg_free(msg);
7614 return;
7615 }
7616
7617 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7618 nl80211_mlme_mcgrp.id, gfp);
7619 return;
7620
7621 nla_put_failure:
7622 genlmsg_cancel(msg, hdr);
7623 nlmsg_free(msg);
7624}
7625EXPORT_SYMBOL(cfg80211_probe_status);
7626
Johannes Berg5e760232011-11-04 11:18:17 +01007627void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7628 const u8 *frame, size_t len,
7629 int freq, gfp_t gfp)
7630{
7631 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7632 struct sk_buff *msg;
7633 void *hdr;
7634 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7635
7636 if (!nlpid)
7637 return;
7638
7639 msg = nlmsg_new(len + 100, gfp);
7640 if (!msg)
7641 return;
7642
7643 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7644 if (!hdr) {
7645 nlmsg_free(msg);
7646 return;
7647 }
7648
7649 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7650 if (freq)
7651 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7652 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7653
7654 genlmsg_end(msg, hdr);
7655
7656 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7657 return;
7658
7659 nla_put_failure:
7660 genlmsg_cancel(msg, hdr);
7661 nlmsg_free(msg);
7662}
7663EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7664
Jouni Malinen026331c2010-02-15 12:53:10 +02007665static int nl80211_netlink_notify(struct notifier_block * nb,
7666 unsigned long state,
7667 void *_notify)
7668{
7669 struct netlink_notify *notify = _notify;
7670 struct cfg80211_registered_device *rdev;
7671 struct wireless_dev *wdev;
7672
7673 if (state != NETLINK_URELEASE)
7674 return NOTIFY_DONE;
7675
7676 rcu_read_lock();
7677
Johannes Berg5e760232011-11-04 11:18:17 +01007678 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007679 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007680 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01007681 if (rdev->ap_beacons_nlpid == notify->pid)
7682 rdev->ap_beacons_nlpid = 0;
7683 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007684
7685 rcu_read_unlock();
7686
7687 return NOTIFY_DONE;
7688}
7689
7690static struct notifier_block nl80211_netlink_notifier = {
7691 .notifier_call = nl80211_netlink_notify,
7692};
7693
Johannes Berg55682962007-09-20 13:09:35 -04007694/* initialisation/exit functions */
7695
7696int nl80211_init(void)
7697{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007698 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007699
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007700 err = genl_register_family_with_ops(&nl80211_fam,
7701 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007702 if (err)
7703 return err;
7704
Johannes Berg55682962007-09-20 13:09:35 -04007705 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7706 if (err)
7707 goto err_out;
7708
Johannes Berg2a519312009-02-10 21:25:55 +01007709 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7710 if (err)
7711 goto err_out;
7712
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007713 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7714 if (err)
7715 goto err_out;
7716
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007717 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7718 if (err)
7719 goto err_out;
7720
Johannes Bergaff89a92009-07-01 21:26:51 +02007721#ifdef CONFIG_NL80211_TESTMODE
7722 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7723 if (err)
7724 goto err_out;
7725#endif
7726
Jouni Malinen026331c2010-02-15 12:53:10 +02007727 err = netlink_register_notifier(&nl80211_netlink_notifier);
7728 if (err)
7729 goto err_out;
7730
Johannes Berg55682962007-09-20 13:09:35 -04007731 return 0;
7732 err_out:
7733 genl_unregister_family(&nl80211_fam);
7734 return err;
7735}
7736
7737void nl80211_exit(void)
7738{
Jouni Malinen026331c2010-02-15 12:53:10 +02007739 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007740 genl_unregister_family(&nl80211_fam);
7741}