blob: b587857a9d7d17fb02f87be44a75c2fe59958d23 [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 Malinen9f26a952009-05-15 12:38:32 +0300101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
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 },
206 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
207 [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 Berg8fdc6212009-03-14 09:34:01 +0100893
894#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200895
Johannes Berg6829c872009-07-02 09:13:27 +0200896 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200897 i++;
898 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
899 }
900
Johannes Berg6829c872009-07-02 09:13:27 +0200901 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200902 i++;
903 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
904 }
905
Johannes Berg8fdc6212009-03-14 09:34:01 +0100906 nla_nest_end(msg, nl_cmds);
907
Johannes Berga2939112010-12-14 17:54:28 +0100908 if (dev->ops->remain_on_channel)
909 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
910 dev->wiphy.max_remain_on_channel_duration);
911
Jouni Malinend2da5872011-08-08 12:10:30 +0300912 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100913 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
914
Johannes Berg2e161f72010-08-12 15:38:38 +0200915 if (mgmt_stypes) {
916 u16 stypes;
917 struct nlattr *nl_ftypes, *nl_ifs;
918 enum nl80211_iftype ift;
919
920 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
921 if (!nl_ifs)
922 goto nla_put_failure;
923
924 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
925 nl_ftypes = nla_nest_start(msg, ift);
926 if (!nl_ftypes)
927 goto nla_put_failure;
928 i = 0;
929 stypes = mgmt_stypes[ift].tx;
930 while (stypes) {
931 if (stypes & 1)
932 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
933 (i << 4) | IEEE80211_FTYPE_MGMT);
934 stypes >>= 1;
935 i++;
936 }
937 nla_nest_end(msg, nl_ftypes);
938 }
939
Johannes Berg74b70a42010-08-24 12:15:53 +0200940 nla_nest_end(msg, nl_ifs);
941
Johannes Berg2e161f72010-08-12 15:38:38 +0200942 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
943 if (!nl_ifs)
944 goto nla_put_failure;
945
946 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
947 nl_ftypes = nla_nest_start(msg, ift);
948 if (!nl_ftypes)
949 goto nla_put_failure;
950 i = 0;
951 stypes = mgmt_stypes[ift].rx;
952 while (stypes) {
953 if (stypes & 1)
954 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
955 (i << 4) | IEEE80211_FTYPE_MGMT);
956 stypes >>= 1;
957 i++;
958 }
959 nla_nest_end(msg, nl_ftypes);
960 }
961 nla_nest_end(msg, nl_ifs);
962 }
963
Johannes Bergff1b6e62011-05-04 15:37:28 +0200964 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
965 struct nlattr *nl_wowlan;
966
967 nl_wowlan = nla_nest_start(msg,
968 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
969 if (!nl_wowlan)
970 goto nla_put_failure;
971
972 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
973 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
974 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
975 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
976 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
977 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200978 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
979 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
980 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
981 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
982 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
983 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
984 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
985 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
986 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
987 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200988 if (dev->wiphy.wowlan.n_patterns) {
989 struct nl80211_wowlan_pattern_support pat = {
990 .max_patterns = dev->wiphy.wowlan.n_patterns,
991 .min_pattern_len =
992 dev->wiphy.wowlan.pattern_min_len,
993 .max_pattern_len =
994 dev->wiphy.wowlan.pattern_max_len,
995 };
996 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
997 sizeof(pat), &pat);
998 }
999
1000 nla_nest_end(msg, nl_wowlan);
1001 }
1002
Johannes Berg7527a782011-05-13 10:58:57 +02001003 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1004 dev->wiphy.software_iftypes))
1005 goto nla_put_failure;
1006
1007 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1008 goto nla_put_failure;
1009
Johannes Berg55682962007-09-20 13:09:35 -04001010 return genlmsg_end(msg, hdr);
1011
1012 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001013 genlmsg_cancel(msg, hdr);
1014 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001015}
1016
1017static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1018{
1019 int idx = 0;
1020 int start = cb->args[0];
1021 struct cfg80211_registered_device *dev;
1022
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001023 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001024 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001025 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1026 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001027 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001028 continue;
1029 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1030 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001031 dev) < 0) {
1032 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001033 break;
Julius Volzb4637272008-07-08 14:02:19 +02001034 }
Johannes Berg55682962007-09-20 13:09:35 -04001035 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001036 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001037
1038 cb->args[0] = idx;
1039
1040 return skb->len;
1041}
1042
1043static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1044{
1045 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001046 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001047
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001048 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001049 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001050 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001051
Johannes Berg4c476992010-10-04 21:36:35 +02001052 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1053 nlmsg_free(msg);
1054 return -ENOBUFS;
1055 }
Johannes Berg55682962007-09-20 13:09:35 -04001056
Johannes Berg134e6372009-07-10 09:51:34 +00001057 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001058}
1059
Jouni Malinen31888482008-10-30 16:59:24 +02001060static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1061 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1062 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1063 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1064 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1065 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1066};
1067
1068static int parse_txq_params(struct nlattr *tb[],
1069 struct ieee80211_txq_params *txq_params)
1070{
1071 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1072 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1073 !tb[NL80211_TXQ_ATTR_AIFS])
1074 return -EINVAL;
1075
1076 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1077 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1078 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1079 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1080 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1081
1082 return 0;
1083}
1084
Johannes Bergf444de02010-05-05 15:25:02 +02001085static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1086{
1087 /*
1088 * You can only set the channel explicitly for AP, mesh
1089 * and WDS type interfaces; all others have their channel
1090 * managed via their respective "establish a connection"
1091 * command (connect, join, ...)
1092 *
1093 * Monitors are special as they are normally slaved to
1094 * whatever else is going on, so they behave as though
1095 * you tried setting the wiphy channel itself.
1096 */
1097 return !wdev ||
1098 wdev->iftype == NL80211_IFTYPE_AP ||
1099 wdev->iftype == NL80211_IFTYPE_WDS ||
1100 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001101 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1102 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001103}
1104
1105static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1106 struct wireless_dev *wdev,
1107 struct genl_info *info)
1108{
1109 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1110 u32 freq;
1111 int result;
1112
1113 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1114 return -EINVAL;
1115
1116 if (!nl80211_can_set_dev_channel(wdev))
1117 return -EOPNOTSUPP;
1118
1119 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1120 channel_type = nla_get_u32(info->attrs[
1121 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1122 if (channel_type != NL80211_CHAN_NO_HT &&
1123 channel_type != NL80211_CHAN_HT20 &&
1124 channel_type != NL80211_CHAN_HT40PLUS &&
1125 channel_type != NL80211_CHAN_HT40MINUS)
1126 return -EINVAL;
1127 }
1128
1129 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1130
1131 mutex_lock(&rdev->devlist_mtx);
1132 if (wdev) {
1133 wdev_lock(wdev);
1134 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1135 wdev_unlock(wdev);
1136 } else {
1137 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1138 }
1139 mutex_unlock(&rdev->devlist_mtx);
1140
1141 return result;
1142}
1143
1144static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1145{
Johannes Berg4c476992010-10-04 21:36:35 +02001146 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1147 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001148
Johannes Berg4c476992010-10-04 21:36:35 +02001149 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001150}
1151
Bill Jordane8347eb2010-10-01 13:54:28 -04001152static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1153{
Johannes Berg43b19952010-10-07 13:10:30 +02001154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1155 struct net_device *dev = info->user_ptr[1];
1156 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001157 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001158
1159 if (!info->attrs[NL80211_ATTR_MAC])
1160 return -EINVAL;
1161
Johannes Berg43b19952010-10-07 13:10:30 +02001162 if (netif_running(dev))
1163 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001164
Johannes Berg43b19952010-10-07 13:10:30 +02001165 if (!rdev->ops->set_wds_peer)
1166 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001167
Johannes Berg43b19952010-10-07 13:10:30 +02001168 if (wdev->iftype != NL80211_IFTYPE_WDS)
1169 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001170
1171 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001172 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001173}
1174
1175
Johannes Berg55682962007-09-20 13:09:35 -04001176static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1177{
1178 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001179 struct net_device *netdev = NULL;
1180 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001181 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001182 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001183 u32 changed;
1184 u8 retry_short = 0, retry_long = 0;
1185 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001186 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001187
Johannes Bergf444de02010-05-05 15:25:02 +02001188 /*
1189 * Try to find the wiphy and netdev. Normally this
1190 * function shouldn't need the netdev, but this is
1191 * done for backward compatibility -- previously
1192 * setting the channel was done per wiphy, but now
1193 * it is per netdev. Previous userland like hostapd
1194 * also passed a netdev to set_wiphy, so that it is
1195 * possible to let that go to the right netdev!
1196 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001197 mutex_lock(&cfg80211_mutex);
1198
Johannes Bergf444de02010-05-05 15:25:02 +02001199 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1200 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1201
1202 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1203 if (netdev && netdev->ieee80211_ptr) {
1204 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1205 mutex_lock(&rdev->mtx);
1206 } else
1207 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001208 }
1209
Johannes Bergf444de02010-05-05 15:25:02 +02001210 if (!netdev) {
1211 rdev = __cfg80211_rdev_from_info(info);
1212 if (IS_ERR(rdev)) {
1213 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001214 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001215 }
1216 wdev = NULL;
1217 netdev = NULL;
1218 result = 0;
1219
1220 mutex_lock(&rdev->mtx);
1221 } else if (netif_running(netdev) &&
1222 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1223 wdev = netdev->ieee80211_ptr;
1224 else
1225 wdev = NULL;
1226
1227 /*
1228 * end workaround code, by now the rdev is available
1229 * and locked, and wdev may or may not be NULL.
1230 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001231
1232 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001233 result = cfg80211_dev_rename(
1234 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001235
1236 mutex_unlock(&cfg80211_mutex);
1237
1238 if (result)
1239 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001240
Jouni Malinen31888482008-10-30 16:59:24 +02001241 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1242 struct ieee80211_txq_params txq_params;
1243 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1244
1245 if (!rdev->ops->set_txq_params) {
1246 result = -EOPNOTSUPP;
1247 goto bad_res;
1248 }
1249
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001250 if (!netdev) {
1251 result = -EINVAL;
1252 goto bad_res;
1253 }
1254
Jouni Malinen31888482008-10-30 16:59:24 +02001255 nla_for_each_nested(nl_txq_params,
1256 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1257 rem_txq_params) {
1258 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1259 nla_data(nl_txq_params),
1260 nla_len(nl_txq_params),
1261 txq_params_policy);
1262 result = parse_txq_params(tb, &txq_params);
1263 if (result)
1264 goto bad_res;
1265
1266 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001267 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001268 &txq_params);
1269 if (result)
1270 goto bad_res;
1271 }
1272 }
1273
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001274 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001275 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001276 if (result)
1277 goto bad_res;
1278 }
1279
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001280 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1281 enum nl80211_tx_power_setting type;
1282 int idx, mbm = 0;
1283
1284 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001285 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001286 goto bad_res;
1287 }
1288
1289 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1290 type = nla_get_u32(info->attrs[idx]);
1291
1292 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1293 (type != NL80211_TX_POWER_AUTOMATIC)) {
1294 result = -EINVAL;
1295 goto bad_res;
1296 }
1297
1298 if (type != NL80211_TX_POWER_AUTOMATIC) {
1299 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1300 mbm = nla_get_u32(info->attrs[idx]);
1301 }
1302
1303 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1304 if (result)
1305 goto bad_res;
1306 }
1307
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001308 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1309 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1310 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001311 if ((!rdev->wiphy.available_antennas_tx &&
1312 !rdev->wiphy.available_antennas_rx) ||
1313 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001314 result = -EOPNOTSUPP;
1315 goto bad_res;
1316 }
1317
1318 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1319 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1320
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001321 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001322 * available antenna masks, except for the "all" mask */
1323 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1324 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001325 result = -EINVAL;
1326 goto bad_res;
1327 }
1328
Bruno Randolf7f531e02010-12-16 11:30:22 +09001329 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1330 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001331
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001332 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1333 if (result)
1334 goto bad_res;
1335 }
1336
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001337 changed = 0;
1338
1339 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1340 retry_short = nla_get_u8(
1341 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1342 if (retry_short == 0) {
1343 result = -EINVAL;
1344 goto bad_res;
1345 }
1346 changed |= WIPHY_PARAM_RETRY_SHORT;
1347 }
1348
1349 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1350 retry_long = nla_get_u8(
1351 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1352 if (retry_long == 0) {
1353 result = -EINVAL;
1354 goto bad_res;
1355 }
1356 changed |= WIPHY_PARAM_RETRY_LONG;
1357 }
1358
1359 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1360 frag_threshold = nla_get_u32(
1361 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1362 if (frag_threshold < 256) {
1363 result = -EINVAL;
1364 goto bad_res;
1365 }
1366 if (frag_threshold != (u32) -1) {
1367 /*
1368 * Fragments (apart from the last one) are required to
1369 * have even length. Make the fragmentation code
1370 * simpler by stripping LSB should someone try to use
1371 * odd threshold value.
1372 */
1373 frag_threshold &= ~0x1;
1374 }
1375 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1376 }
1377
1378 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1379 rts_threshold = nla_get_u32(
1380 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1381 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1382 }
1383
Lukáš Turek81077e82009-12-21 22:50:47 +01001384 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1385 coverage_class = nla_get_u8(
1386 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1387 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1388 }
1389
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001390 if (changed) {
1391 u8 old_retry_short, old_retry_long;
1392 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001393 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001394
1395 if (!rdev->ops->set_wiphy_params) {
1396 result = -EOPNOTSUPP;
1397 goto bad_res;
1398 }
1399
1400 old_retry_short = rdev->wiphy.retry_short;
1401 old_retry_long = rdev->wiphy.retry_long;
1402 old_frag_threshold = rdev->wiphy.frag_threshold;
1403 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001404 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001405
1406 if (changed & WIPHY_PARAM_RETRY_SHORT)
1407 rdev->wiphy.retry_short = retry_short;
1408 if (changed & WIPHY_PARAM_RETRY_LONG)
1409 rdev->wiphy.retry_long = retry_long;
1410 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1411 rdev->wiphy.frag_threshold = frag_threshold;
1412 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1413 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001414 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1415 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001416
1417 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1418 if (result) {
1419 rdev->wiphy.retry_short = old_retry_short;
1420 rdev->wiphy.retry_long = old_retry_long;
1421 rdev->wiphy.frag_threshold = old_frag_threshold;
1422 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001423 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001424 }
1425 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001426
Johannes Berg306d6112008-12-08 12:39:04 +01001427 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001428 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001429 if (netdev)
1430 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001431 return result;
1432}
1433
1434
1435static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001436 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001437 struct net_device *dev)
1438{
1439 void *hdr;
1440
1441 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1442 if (!hdr)
1443 return -1;
1444
1445 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001446 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001447 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001448 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001449
1450 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1451 rdev->devlist_generation ^
1452 (cfg80211_rdev_list_generation << 2));
1453
Johannes Berg55682962007-09-20 13:09:35 -04001454 return genlmsg_end(msg, hdr);
1455
1456 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001457 genlmsg_cancel(msg, hdr);
1458 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001459}
1460
1461static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1462{
1463 int wp_idx = 0;
1464 int if_idx = 0;
1465 int wp_start = cb->args[0];
1466 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001467 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001468 struct wireless_dev *wdev;
1469
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001470 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001471 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1472 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001473 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001474 if (wp_idx < wp_start) {
1475 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001476 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001477 }
Johannes Berg55682962007-09-20 13:09:35 -04001478 if_idx = 0;
1479
Johannes Bergf5ea9122009-08-07 16:17:38 +02001480 mutex_lock(&rdev->devlist_mtx);
1481 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001482 if (if_idx < if_start) {
1483 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001484 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001485 }
Johannes Berg55682962007-09-20 13:09:35 -04001486 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1487 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001488 rdev, wdev->netdev) < 0) {
1489 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001490 goto out;
1491 }
1492 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001493 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001494 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001495
1496 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001497 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001498 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001499 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001500
1501 cb->args[0] = wp_idx;
1502 cb->args[1] = if_idx;
1503
1504 return skb->len;
1505}
1506
1507static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1508{
1509 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001510 struct cfg80211_registered_device *dev = info->user_ptr[0];
1511 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001512
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001514 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001515 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001516
Johannes Bergd7264052009-04-19 16:23:20 +02001517 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001518 dev, netdev) < 0) {
1519 nlmsg_free(msg);
1520 return -ENOBUFS;
1521 }
Johannes Berg55682962007-09-20 13:09:35 -04001522
Johannes Berg134e6372009-07-10 09:51:34 +00001523 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001524}
1525
Michael Wu66f7ac52008-01-31 19:48:22 +01001526static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1527 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1528 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1529 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1530 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1531 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1532};
1533
1534static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1535{
1536 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1537 int flag;
1538
1539 *mntrflags = 0;
1540
1541 if (!nla)
1542 return -EINVAL;
1543
1544 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1545 nla, mntr_flags_policy))
1546 return -EINVAL;
1547
1548 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1549 if (flags[flag])
1550 *mntrflags |= (1<<flag);
1551
1552 return 0;
1553}
1554
Johannes Berg9bc383d2009-11-19 11:55:19 +01001555static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001556 struct net_device *netdev, u8 use_4addr,
1557 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001558{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001559 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001560 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001561 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001562 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001563 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001564
1565 switch (iftype) {
1566 case NL80211_IFTYPE_AP_VLAN:
1567 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1568 return 0;
1569 break;
1570 case NL80211_IFTYPE_STATION:
1571 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1572 return 0;
1573 break;
1574 default:
1575 break;
1576 }
1577
1578 return -EOPNOTSUPP;
1579}
1580
Johannes Berg55682962007-09-20 13:09:35 -04001581static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1582{
Johannes Berg4c476992010-10-04 21:36:35 +02001583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001584 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001585 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001586 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001587 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001588 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001589 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001590
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001591 memset(&params, 0, sizeof(params));
1592
Johannes Berg04a773a2009-04-19 21:24:32 +02001593 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001594
Johannes Berg723b0382008-09-16 20:22:09 +02001595 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001596 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001597 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001598 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001599 if (ntype > NL80211_IFTYPE_MAX)
1600 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001601 }
1602
Johannes Berg92ffe052008-09-16 20:39:36 +02001603 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001604 struct wireless_dev *wdev = dev->ieee80211_ptr;
1605
Johannes Berg4c476992010-10-04 21:36:35 +02001606 if (ntype != NL80211_IFTYPE_MESH_POINT)
1607 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001608 if (netif_running(dev))
1609 return -EBUSY;
1610
1611 wdev_lock(wdev);
1612 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1613 IEEE80211_MAX_MESH_ID_LEN);
1614 wdev->mesh_id_up_len =
1615 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1616 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1617 wdev->mesh_id_up_len);
1618 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001619 }
1620
Felix Fietkau8b787642009-11-10 18:53:10 +01001621 if (info->attrs[NL80211_ATTR_4ADDR]) {
1622 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1623 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001624 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001625 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001626 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001627 } else {
1628 params.use_4addr = -1;
1629 }
1630
Johannes Berg92ffe052008-09-16 20:39:36 +02001631 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001632 if (ntype != NL80211_IFTYPE_MONITOR)
1633 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001634 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1635 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001636 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001637 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001638
1639 flags = &_flags;
1640 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001641 }
Johannes Berg3b858752009-03-12 09:55:09 +01001642
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001643 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001644 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001645 else
1646 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001647
Johannes Berg9bc383d2009-11-19 11:55:19 +01001648 if (!err && params.use_4addr != -1)
1649 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1650
Johannes Berg55682962007-09-20 13:09:35 -04001651 return err;
1652}
1653
1654static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1655{
Johannes Berg4c476992010-10-04 21:36:35 +02001656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001657 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001658 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001659 int err;
1660 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001661 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001662
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001663 memset(&params, 0, sizeof(params));
1664
Johannes Berg55682962007-09-20 13:09:35 -04001665 if (!info->attrs[NL80211_ATTR_IFNAME])
1666 return -EINVAL;
1667
1668 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1669 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1670 if (type > NL80211_IFTYPE_MAX)
1671 return -EINVAL;
1672 }
1673
Johannes Berg79c97e92009-07-07 03:56:12 +02001674 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001675 !(rdev->wiphy.interface_modes & (1 << type)))
1676 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001677
Johannes Berg9bc383d2009-11-19 11:55:19 +01001678 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001679 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001680 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001681 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001682 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001683 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001684
Michael Wu66f7ac52008-01-31 19:48:22 +01001685 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1686 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1687 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001688 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001689 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001690 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001691 if (IS_ERR(dev))
1692 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001693
Johannes Berg29cbe682010-12-03 09:20:44 +01001694 if (type == NL80211_IFTYPE_MESH_POINT &&
1695 info->attrs[NL80211_ATTR_MESH_ID]) {
1696 struct wireless_dev *wdev = dev->ieee80211_ptr;
1697
1698 wdev_lock(wdev);
1699 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1700 IEEE80211_MAX_MESH_ID_LEN);
1701 wdev->mesh_id_up_len =
1702 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1703 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1704 wdev->mesh_id_up_len);
1705 wdev_unlock(wdev);
1706 }
1707
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001708 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001709}
1710
1711static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1712{
Johannes Berg4c476992010-10-04 21:36:35 +02001713 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1714 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001715
Johannes Berg4c476992010-10-04 21:36:35 +02001716 if (!rdev->ops->del_virtual_intf)
1717 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001718
Johannes Berg4c476992010-10-04 21:36:35 +02001719 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001720}
1721
Johannes Berg41ade002007-12-19 02:03:29 +01001722struct get_key_cookie {
1723 struct sk_buff *msg;
1724 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001725 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001726};
1727
1728static void get_key_callback(void *c, struct key_params *params)
1729{
Johannes Bergb9454e82009-07-08 13:29:08 +02001730 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001731 struct get_key_cookie *cookie = c;
1732
1733 if (params->key)
1734 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1735 params->key_len, params->key);
1736
1737 if (params->seq)
1738 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1739 params->seq_len, params->seq);
1740
1741 if (params->cipher)
1742 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1743 params->cipher);
1744
Johannes Bergb9454e82009-07-08 13:29:08 +02001745 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1746 if (!key)
1747 goto nla_put_failure;
1748
1749 if (params->key)
1750 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1751 params->key_len, params->key);
1752
1753 if (params->seq)
1754 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1755 params->seq_len, params->seq);
1756
1757 if (params->cipher)
1758 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1759 params->cipher);
1760
1761 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1762
1763 nla_nest_end(cookie->msg, key);
1764
Johannes Berg41ade002007-12-19 02:03:29 +01001765 return;
1766 nla_put_failure:
1767 cookie->error = 1;
1768}
1769
1770static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1771{
Johannes Berg4c476992010-10-04 21:36:35 +02001772 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001773 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001774 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001775 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001776 const u8 *mac_addr = NULL;
1777 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001778 struct get_key_cookie cookie = {
1779 .error = 0,
1780 };
1781 void *hdr;
1782 struct sk_buff *msg;
1783
1784 if (info->attrs[NL80211_ATTR_KEY_IDX])
1785 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1786
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001787 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001788 return -EINVAL;
1789
1790 if (info->attrs[NL80211_ATTR_MAC])
1791 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1792
Johannes Berge31b8212010-10-05 19:39:30 +02001793 pairwise = !!mac_addr;
1794 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1795 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1796 if (kt >= NUM_NL80211_KEYTYPES)
1797 return -EINVAL;
1798 if (kt != NL80211_KEYTYPE_GROUP &&
1799 kt != NL80211_KEYTYPE_PAIRWISE)
1800 return -EINVAL;
1801 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1802 }
1803
Johannes Berg4c476992010-10-04 21:36:35 +02001804 if (!rdev->ops->get_key)
1805 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001806
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001807 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001808 if (!msg)
1809 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001810
1811 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1812 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001813 if (IS_ERR(hdr))
1814 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001815
1816 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001817 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001818
1819 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1820 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1821 if (mac_addr)
1822 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1823
Johannes Berge31b8212010-10-05 19:39:30 +02001824 if (pairwise && mac_addr &&
1825 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1826 return -ENOENT;
1827
1828 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1829 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001830
1831 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001832 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001833
1834 if (cookie.error)
1835 goto nla_put_failure;
1836
1837 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001838 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001839
1840 nla_put_failure:
1841 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001842 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001843 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001844 return err;
1845}
1846
1847static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1848{
Johannes Berg4c476992010-10-04 21:36:35 +02001849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001850 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001851 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001852 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001853
Johannes Bergb9454e82009-07-08 13:29:08 +02001854 err = nl80211_parse_key(info, &key);
1855 if (err)
1856 return err;
1857
1858 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001859 return -EINVAL;
1860
Johannes Bergb9454e82009-07-08 13:29:08 +02001861 /* only support setting default key */
1862 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001863 return -EINVAL;
1864
Johannes Bergfffd0932009-07-08 14:22:54 +02001865 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001866
1867 if (key.def) {
1868 if (!rdev->ops->set_default_key) {
1869 err = -EOPNOTSUPP;
1870 goto out;
1871 }
1872
1873 err = nl80211_key_allowed(dev->ieee80211_ptr);
1874 if (err)
1875 goto out;
1876
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001877 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1878 key.def_uni, key.def_multi);
1879
1880 if (err)
1881 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001882
Johannes Berg3d23e342009-09-29 23:27:28 +02001883#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001884 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001885#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001886 } else {
1887 if (key.def_uni || !key.def_multi) {
1888 err = -EINVAL;
1889 goto out;
1890 }
1891
1892 if (!rdev->ops->set_default_mgmt_key) {
1893 err = -EOPNOTSUPP;
1894 goto out;
1895 }
1896
1897 err = nl80211_key_allowed(dev->ieee80211_ptr);
1898 if (err)
1899 goto out;
1900
1901 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1902 dev, key.idx);
1903 if (err)
1904 goto out;
1905
1906#ifdef CONFIG_CFG80211_WEXT
1907 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1908#endif
1909 }
1910
1911 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001912 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001913
Johannes Berg41ade002007-12-19 02:03:29 +01001914 return err;
1915}
1916
1917static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1918{
Johannes Berg4c476992010-10-04 21:36:35 +02001919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001920 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001921 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001922 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001923 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001924
Johannes Bergb9454e82009-07-08 13:29:08 +02001925 err = nl80211_parse_key(info, &key);
1926 if (err)
1927 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001928
Johannes Bergb9454e82009-07-08 13:29:08 +02001929 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001930 return -EINVAL;
1931
Johannes Berg41ade002007-12-19 02:03:29 +01001932 if (info->attrs[NL80211_ATTR_MAC])
1933 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1934
Johannes Berge31b8212010-10-05 19:39:30 +02001935 if (key.type == -1) {
1936 if (mac_addr)
1937 key.type = NL80211_KEYTYPE_PAIRWISE;
1938 else
1939 key.type = NL80211_KEYTYPE_GROUP;
1940 }
1941
1942 /* for now */
1943 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1944 key.type != NL80211_KEYTYPE_GROUP)
1945 return -EINVAL;
1946
Johannes Berg4c476992010-10-04 21:36:35 +02001947 if (!rdev->ops->add_key)
1948 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001949
Johannes Berge31b8212010-10-05 19:39:30 +02001950 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1951 key.type == NL80211_KEYTYPE_PAIRWISE,
1952 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001953 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001954
1955 wdev_lock(dev->ieee80211_ptr);
1956 err = nl80211_key_allowed(dev->ieee80211_ptr);
1957 if (!err)
1958 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001959 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001960 mac_addr, &key.p);
1961 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001962
Johannes Berg41ade002007-12-19 02:03:29 +01001963 return err;
1964}
1965
1966static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1967{
Johannes Berg4c476992010-10-04 21:36:35 +02001968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001969 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001970 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001971 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001972 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001973
Johannes Bergb9454e82009-07-08 13:29:08 +02001974 err = nl80211_parse_key(info, &key);
1975 if (err)
1976 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001977
1978 if (info->attrs[NL80211_ATTR_MAC])
1979 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1980
Johannes Berge31b8212010-10-05 19:39:30 +02001981 if (key.type == -1) {
1982 if (mac_addr)
1983 key.type = NL80211_KEYTYPE_PAIRWISE;
1984 else
1985 key.type = NL80211_KEYTYPE_GROUP;
1986 }
1987
1988 /* for now */
1989 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1990 key.type != NL80211_KEYTYPE_GROUP)
1991 return -EINVAL;
1992
Johannes Berg4c476992010-10-04 21:36:35 +02001993 if (!rdev->ops->del_key)
1994 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001995
Johannes Bergfffd0932009-07-08 14:22:54 +02001996 wdev_lock(dev->ieee80211_ptr);
1997 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001998
1999 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2000 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2001 err = -ENOENT;
2002
Johannes Bergfffd0932009-07-08 14:22:54 +02002003 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002004 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2005 key.type == NL80211_KEYTYPE_PAIRWISE,
2006 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002007
Johannes Berg3d23e342009-09-29 23:27:28 +02002008#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002009 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002010 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002011 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002012 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002013 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2014 }
2015#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002016 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002017
Johannes Berg41ade002007-12-19 02:03:29 +01002018 return err;
2019}
2020
Johannes Berged1b6cc2007-12-19 02:03:32 +01002021static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2022{
2023 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2024 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2026 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002027 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002028 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002029 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002030
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002031 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2032 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2033 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2034 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002035 return -EINVAL;
2036
Johannes Berg074ac8d2010-09-16 14:58:22 +02002037 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002038 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2039 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002040
Johannes Berg56d18932011-05-09 18:41:15 +02002041 memset(&params, 0, sizeof(params));
2042
Johannes Berged1b6cc2007-12-19 02:03:32 +01002043 switch (info->genlhdr->cmd) {
2044 case NL80211_CMD_NEW_BEACON:
2045 /* these are required for NEW_BEACON */
2046 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2047 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002048 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2049 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002050
Johannes Berg56d18932011-05-09 18:41:15 +02002051 params.interval =
2052 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2053 params.dtim_period =
2054 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2055
2056 err = cfg80211_validate_beacon_int(rdev, params.interval);
2057 if (err)
2058 return err;
2059
Jouni Malinen32e9de82011-08-10 23:53:31 +03002060 /*
2061 * In theory, some of these attributes could be required for
2062 * NEW_BEACON, but since they were not used when the command was
2063 * originally added, keep them optional for old user space
2064 * programs to work with drivers that do not need the additional
2065 * information.
2066 */
2067 if (info->attrs[NL80211_ATTR_SSID]) {
2068 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2069 params.ssid_len =
2070 nla_len(info->attrs[NL80211_ATTR_SSID]);
2071 if (params.ssid_len == 0 ||
2072 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2073 return -EINVAL;
2074 }
2075
2076 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2077 params.hidden_ssid = nla_get_u32(
2078 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2079 if (params.hidden_ssid !=
2080 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2081 params.hidden_ssid !=
2082 NL80211_HIDDEN_SSID_ZERO_LEN &&
2083 params.hidden_ssid !=
2084 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2085 return -EINVAL;
2086 }
2087
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002088 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2089
2090 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2091 params.auth_type = nla_get_u32(
2092 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2093 if (!nl80211_valid_auth_type(params.auth_type))
2094 return -EINVAL;
2095 } else
2096 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2097
2098 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2099 NL80211_MAX_NR_CIPHER_SUITES);
2100 if (err)
2101 return err;
2102
Johannes Berg79c97e92009-07-07 03:56:12 +02002103 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002104 break;
2105 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002106 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002107 break;
2108 default:
2109 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002110 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002111 }
2112
Johannes Berg4c476992010-10-04 21:36:35 +02002113 if (!call)
2114 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002115
Johannes Berged1b6cc2007-12-19 02:03:32 +01002116 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2117 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2118 params.head_len =
2119 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2120 haveinfo = 1;
2121 }
2122
2123 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2124 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2125 params.tail_len =
2126 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2127 haveinfo = 1;
2128 }
2129
Johannes Berg4c476992010-10-04 21:36:35 +02002130 if (!haveinfo)
2131 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002132
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002133 if (info->attrs[NL80211_ATTR_IE]) {
2134 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2135 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2136 }
2137
2138 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2139 params.proberesp_ies =
2140 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2141 params.proberesp_ies_len =
2142 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2143 }
2144
2145 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2146 params.assocresp_ies =
2147 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2148 params.assocresp_ies_len =
2149 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2150 }
2151
Johannes Berg56d18932011-05-09 18:41:15 +02002152 err = call(&rdev->wiphy, dev, &params);
2153 if (!err && params.interval)
2154 wdev->beacon_interval = params.interval;
2155 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002156}
2157
2158static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2159{
Johannes Berg4c476992010-10-04 21:36:35 +02002160 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2161 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002162 struct wireless_dev *wdev = dev->ieee80211_ptr;
2163 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002164
Johannes Berg4c476992010-10-04 21:36:35 +02002165 if (!rdev->ops->del_beacon)
2166 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002167
Johannes Berg074ac8d2010-09-16 14:58:22 +02002168 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002169 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2170 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002171
Johannes Berg56d18932011-05-09 18:41:15 +02002172 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2173 if (!err)
2174 wdev->beacon_interval = 0;
2175 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002176}
2177
Johannes Berg5727ef12007-12-19 02:03:34 +01002178static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2179 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2180 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2181 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002182 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002183 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002184};
2185
Johannes Bergeccb8e82009-05-11 21:57:56 +03002186static int parse_station_flags(struct genl_info *info,
2187 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002188{
2189 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002190 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002191 int flag;
2192
Johannes Bergeccb8e82009-05-11 21:57:56 +03002193 /*
2194 * Try parsing the new attribute first so userspace
2195 * can specify both for older kernels.
2196 */
2197 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2198 if (nla) {
2199 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002200
Johannes Bergeccb8e82009-05-11 21:57:56 +03002201 sta_flags = nla_data(nla);
2202 params->sta_flags_mask = sta_flags->mask;
2203 params->sta_flags_set = sta_flags->set;
2204 if ((params->sta_flags_mask |
2205 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2206 return -EINVAL;
2207 return 0;
2208 }
2209
2210 /* if present, parse the old attribute */
2211
2212 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002213 if (!nla)
2214 return 0;
2215
2216 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2217 nla, sta_flags_policy))
2218 return -EINVAL;
2219
Johannes Bergeccb8e82009-05-11 21:57:56 +03002220 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2221 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002222
2223 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2224 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002225 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002226
2227 return 0;
2228}
2229
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002230static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2231 int attr)
2232{
2233 struct nlattr *rate;
2234 u16 bitrate;
2235
2236 rate = nla_nest_start(msg, attr);
2237 if (!rate)
2238 goto nla_put_failure;
2239
2240 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2241 bitrate = cfg80211_calculate_bitrate(info);
2242 if (bitrate > 0)
2243 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2244
2245 if (info->flags & RATE_INFO_FLAGS_MCS)
2246 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2247 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2248 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2249 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2250 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2251
2252 nla_nest_end(msg, rate);
2253 return true;
2254
2255nla_put_failure:
2256 return false;
2257}
2258
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002259static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2260 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002261 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002262{
2263 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002264 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002265
2266 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2267 if (!hdr)
2268 return -1;
2269
2270 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2271 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2272
Johannes Bergf5ea9122009-08-07 16:17:38 +02002273 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2274
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002275 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2276 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002277 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302278 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2279 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2280 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002281 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2282 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2283 sinfo->inactive_time);
2284 if (sinfo->filled & STATION_INFO_RX_BYTES)
2285 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2286 sinfo->rx_bytes);
2287 if (sinfo->filled & STATION_INFO_TX_BYTES)
2288 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2289 sinfo->tx_bytes);
2290 if (sinfo->filled & STATION_INFO_LLID)
2291 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2292 sinfo->llid);
2293 if (sinfo->filled & STATION_INFO_PLID)
2294 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2295 sinfo->plid);
2296 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2297 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2298 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002299 if (sinfo->filled & STATION_INFO_SIGNAL)
2300 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2301 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002302 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2303 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2304 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002305 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002306 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2307 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002308 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002309 }
2310 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2311 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2312 NL80211_STA_INFO_RX_BITRATE))
2313 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002314 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002315 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2316 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2317 sinfo->rx_packets);
2318 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2319 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2320 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002321 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2322 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2323 sinfo->tx_retries);
2324 if (sinfo->filled & STATION_INFO_TX_FAILED)
2325 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2326 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002327 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2328 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2329 if (!bss_param)
2330 goto nla_put_failure;
2331
2332 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2333 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2334 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2335 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2336 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2337 NLA_PUT_FLAG(msg,
2338 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2339 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2340 sinfo->bss_param.dtim_period);
2341 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2342 sinfo->bss_param.beacon_interval);
2343
2344 nla_nest_end(msg, bss_param);
2345 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002346 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2347 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2348 sizeof(struct nl80211_sta_flag_update),
2349 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002350 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002351
Felix Fietkau040bdf72011-08-10 19:00:33 -06002352 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002353 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2354 sinfo->assoc_req_ies);
2355
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002356 return genlmsg_end(msg, hdr);
2357
2358 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002359 genlmsg_cancel(msg, hdr);
2360 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002361}
2362
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002363static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002364 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366 struct station_info sinfo;
2367 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002368 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002370 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002371 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372
Johannes Berg67748892010-10-04 21:14:06 +02002373 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2374 if (err)
2375 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002376
2377 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002378 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002379 goto out_err;
2380 }
2381
Johannes Bergbba95fe2008-07-29 13:22:51 +02002382 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002383 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002384 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2385 mac_addr, &sinfo);
2386 if (err == -ENOENT)
2387 break;
2388 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002389 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002390
2391 if (nl80211_send_station(skb,
2392 NETLINK_CB(cb->skb).pid,
2393 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2394 netdev, mac_addr,
2395 &sinfo) < 0)
2396 goto out;
2397
2398 sta_idx++;
2399 }
2400
2401
2402 out:
2403 cb->args[1] = sta_idx;
2404 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002405 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002406 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002407
2408 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002409}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002410
Johannes Berg5727ef12007-12-19 02:03:34 +01002411static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2412{
Johannes Berg4c476992010-10-04 21:36:35 +02002413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2414 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002415 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002416 struct sk_buff *msg;
2417 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002418 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002419
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002420 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002421
2422 if (!info->attrs[NL80211_ATTR_MAC])
2423 return -EINVAL;
2424
2425 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2426
Johannes Berg4c476992010-10-04 21:36:35 +02002427 if (!rdev->ops->get_station)
2428 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002429
Johannes Berg79c97e92009-07-07 03:56:12 +02002430 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002431 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002432 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002433
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002434 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002435 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002436 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002437
2438 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002439 dev, mac_addr, &sinfo) < 0) {
2440 nlmsg_free(msg);
2441 return -ENOBUFS;
2442 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002443
Johannes Berg4c476992010-10-04 21:36:35 +02002444 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002445}
2446
2447/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002448 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002449 */
Johannes Berg463d0182009-07-14 00:33:35 +02002450static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002451 struct cfg80211_registered_device *rdev,
2452 struct net_device **vlan)
2453{
Johannes Berg463d0182009-07-14 00:33:35 +02002454 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002455 *vlan = NULL;
2456
2457 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002458 *vlan = dev_get_by_index(genl_info_net(info),
2459 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002460 if (!*vlan)
2461 return -ENODEV;
2462 if (!(*vlan)->ieee80211_ptr)
2463 return -EINVAL;
2464 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2465 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002466 if (!netif_running(*vlan))
2467 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002468 }
2469 return 0;
2470}
2471
2472static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2473{
Johannes Berg4c476992010-10-04 21:36:35 +02002474 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002475 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002476 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002477 struct station_parameters params;
2478 u8 *mac_addr = NULL;
2479
2480 memset(&params, 0, sizeof(params));
2481
2482 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002483 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002484
2485 if (info->attrs[NL80211_ATTR_STA_AID])
2486 return -EINVAL;
2487
2488 if (!info->attrs[NL80211_ATTR_MAC])
2489 return -EINVAL;
2490
2491 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2492
2493 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2494 params.supported_rates =
2495 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2496 params.supported_rates_len =
2497 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2498 }
2499
2500 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2501 params.listen_interval =
2502 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2503
Jouni Malinen36aedc92008-08-25 11:58:58 +03002504 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2505 params.ht_capa =
2506 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2507
Johannes Bergeccb8e82009-05-11 21:57:56 +03002508 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002509 return -EINVAL;
2510
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002511 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2512 params.plink_action =
2513 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2514
Javier Cardona9c3990a2011-05-03 16:57:11 -07002515 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2516 params.plink_state =
2517 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2518
Johannes Berg463d0182009-07-14 00:33:35 +02002519 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002520 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002521 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002522
2523 /* validate settings */
2524 err = 0;
2525
2526 switch (dev->ieee80211_ptr->iftype) {
2527 case NL80211_IFTYPE_AP:
2528 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002529 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002530 /* disallow mesh-specific things */
2531 if (params.plink_action)
2532 err = -EINVAL;
2533 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002534 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002535 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002536 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002537 if (params.plink_action)
2538 err = -EINVAL;
2539 if (params.vlan)
2540 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002541 if (params.supported_rates &&
2542 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002543 err = -EINVAL;
2544 if (params.ht_capa)
2545 err = -EINVAL;
2546 if (params.listen_interval >= 0)
2547 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002548 if (params.sta_flags_mask &
2549 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2550 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2551 err = -EINVAL;
2552 /* can't change the TDLS bit */
2553 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2554 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002555 err = -EINVAL;
2556 break;
2557 case NL80211_IFTYPE_MESH_POINT:
2558 /* disallow things mesh doesn't support */
2559 if (params.vlan)
2560 err = -EINVAL;
2561 if (params.ht_capa)
2562 err = -EINVAL;
2563 if (params.listen_interval >= 0)
2564 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002565 if (params.sta_flags_mask &
2566 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002567 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002568 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002569 err = -EINVAL;
2570 break;
2571 default:
2572 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002573 }
2574
Johannes Berg5727ef12007-12-19 02:03:34 +01002575 if (err)
2576 goto out;
2577
Johannes Berg79c97e92009-07-07 03:56:12 +02002578 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002579 err = -EOPNOTSUPP;
2580 goto out;
2581 }
2582
Johannes Berg79c97e92009-07-07 03:56:12 +02002583 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002584
2585 out:
2586 if (params.vlan)
2587 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002588
Johannes Berg5727ef12007-12-19 02:03:34 +01002589 return err;
2590}
2591
Eliad Pellerc75786c2011-08-23 14:37:46 +03002592static struct nla_policy
2593nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2594 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2595 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2596};
2597
Johannes Berg5727ef12007-12-19 02:03:34 +01002598static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2599{
Johannes Berg4c476992010-10-04 21:36:35 +02002600 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002601 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002602 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002603 struct station_parameters params;
2604 u8 *mac_addr = NULL;
2605
2606 memset(&params, 0, sizeof(params));
2607
2608 if (!info->attrs[NL80211_ATTR_MAC])
2609 return -EINVAL;
2610
Johannes Berg5727ef12007-12-19 02:03:34 +01002611 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2612 return -EINVAL;
2613
2614 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2615 return -EINVAL;
2616
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002617 if (!info->attrs[NL80211_ATTR_STA_AID])
2618 return -EINVAL;
2619
Johannes Berg5727ef12007-12-19 02:03:34 +01002620 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2621 params.supported_rates =
2622 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2623 params.supported_rates_len =
2624 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2625 params.listen_interval =
2626 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002627
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002628 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2629 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2630 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002631
Jouni Malinen36aedc92008-08-25 11:58:58 +03002632 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2633 params.ht_capa =
2634 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002635
Javier Cardona96b78df2011-04-07 15:08:33 -07002636 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2637 params.plink_action =
2638 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2639
Johannes Bergeccb8e82009-05-11 21:57:56 +03002640 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002641 return -EINVAL;
2642
Eliad Pellerc75786c2011-08-23 14:37:46 +03002643 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002644 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002645 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002646 info->attrs[NL80211_ATTR_STA_WME]) {
2647 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2648 struct nlattr *nla;
2649
2650 nla = info->attrs[NL80211_ATTR_STA_WME];
2651 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2652 nl80211_sta_wme_policy);
2653 if (err)
2654 return err;
2655
2656 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2657 params.uapsd_queues =
2658 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002659 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2660 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002661
2662 if (tb[NL80211_STA_WME_MAX_SP])
2663 params.max_sp =
2664 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002665
2666 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2667 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002668
2669 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002670 }
2671
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002672 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002673 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002674 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002675 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2676 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2677 return -EINVAL;
2678
2679 /*
2680 * Only managed stations can add TDLS peers, and only when the
2681 * wiphy supports external TDLS setup.
2682 */
2683 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2684 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2685 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2686 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002687 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002688
Johannes Berg463d0182009-07-14 00:33:35 +02002689 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002690 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002691 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002692
2693 /* validate settings */
2694 err = 0;
2695
Johannes Berg79c97e92009-07-07 03:56:12 +02002696 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002697 err = -EOPNOTSUPP;
2698 goto out;
2699 }
2700
Johannes Berg79c97e92009-07-07 03:56:12 +02002701 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002702
2703 out:
2704 if (params.vlan)
2705 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002706 return err;
2707}
2708
2709static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2710{
Johannes Berg4c476992010-10-04 21:36:35 +02002711 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2712 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002713 u8 *mac_addr = NULL;
2714
2715 if (info->attrs[NL80211_ATTR_MAC])
2716 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2717
Johannes Berge80cf852009-05-11 14:43:13 +02002718 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002719 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002720 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002721 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2722 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002723
Johannes Berg4c476992010-10-04 21:36:35 +02002724 if (!rdev->ops->del_station)
2725 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002726
Johannes Berg4c476992010-10-04 21:36:35 +02002727 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002728}
2729
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002730static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2731 int flags, struct net_device *dev,
2732 u8 *dst, u8 *next_hop,
2733 struct mpath_info *pinfo)
2734{
2735 void *hdr;
2736 struct nlattr *pinfoattr;
2737
2738 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2739 if (!hdr)
2740 return -1;
2741
2742 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2743 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2744 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2745
Johannes Bergf5ea9122009-08-07 16:17:38 +02002746 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2747
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002748 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2749 if (!pinfoattr)
2750 goto nla_put_failure;
2751 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2752 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2753 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002754 if (pinfo->filled & MPATH_INFO_SN)
2755 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2756 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002757 if (pinfo->filled & MPATH_INFO_METRIC)
2758 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2759 pinfo->metric);
2760 if (pinfo->filled & MPATH_INFO_EXPTIME)
2761 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2762 pinfo->exptime);
2763 if (pinfo->filled & MPATH_INFO_FLAGS)
2764 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2765 pinfo->flags);
2766 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2767 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2768 pinfo->discovery_timeout);
2769 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2770 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2771 pinfo->discovery_retries);
2772
2773 nla_nest_end(msg, pinfoattr);
2774
2775 return genlmsg_end(msg, hdr);
2776
2777 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002778 genlmsg_cancel(msg, hdr);
2779 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002780}
2781
2782static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002783 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002784{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002785 struct mpath_info pinfo;
2786 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002787 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002788 u8 dst[ETH_ALEN];
2789 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002790 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002791 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002792
Johannes Berg67748892010-10-04 21:14:06 +02002793 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2794 if (err)
2795 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002796
2797 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002798 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002799 goto out_err;
2800 }
2801
Jouni Malineneec60b02009-03-20 21:21:19 +02002802 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2803 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002804 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002805 }
2806
Johannes Bergbba95fe2008-07-29 13:22:51 +02002807 while (1) {
2808 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2809 dst, next_hop, &pinfo);
2810 if (err == -ENOENT)
2811 break;
2812 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002813 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002814
2815 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2816 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2817 netdev, dst, next_hop,
2818 &pinfo) < 0)
2819 goto out;
2820
2821 path_idx++;
2822 }
2823
2824
2825 out:
2826 cb->args[1] = path_idx;
2827 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002828 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002829 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002830 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002831}
2832
2833static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2834{
Johannes Berg4c476992010-10-04 21:36:35 +02002835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002836 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002837 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002838 struct mpath_info pinfo;
2839 struct sk_buff *msg;
2840 u8 *dst = NULL;
2841 u8 next_hop[ETH_ALEN];
2842
2843 memset(&pinfo, 0, sizeof(pinfo));
2844
2845 if (!info->attrs[NL80211_ATTR_MAC])
2846 return -EINVAL;
2847
2848 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2849
Johannes Berg4c476992010-10-04 21:36:35 +02002850 if (!rdev->ops->get_mpath)
2851 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002852
Johannes Berg4c476992010-10-04 21:36:35 +02002853 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2854 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002855
Johannes Berg79c97e92009-07-07 03:56:12 +02002856 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002857 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002858 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002859
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002860 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002861 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002862 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002863
2864 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002865 dev, dst, next_hop, &pinfo) < 0) {
2866 nlmsg_free(msg);
2867 return -ENOBUFS;
2868 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002869
Johannes Berg4c476992010-10-04 21:36:35 +02002870 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002871}
2872
2873static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2874{
Johannes Berg4c476992010-10-04 21:36:35 +02002875 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2876 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002877 u8 *dst = NULL;
2878 u8 *next_hop = NULL;
2879
2880 if (!info->attrs[NL80211_ATTR_MAC])
2881 return -EINVAL;
2882
2883 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2884 return -EINVAL;
2885
2886 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2887 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2888
Johannes Berg4c476992010-10-04 21:36:35 +02002889 if (!rdev->ops->change_mpath)
2890 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002891
Johannes Berg4c476992010-10-04 21:36:35 +02002892 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2893 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002894
Johannes Berg4c476992010-10-04 21:36:35 +02002895 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002896}
Johannes Berg4c476992010-10-04 21:36:35 +02002897
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002898static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2899{
Johannes Berg4c476992010-10-04 21:36:35 +02002900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2901 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002902 u8 *dst = NULL;
2903 u8 *next_hop = NULL;
2904
2905 if (!info->attrs[NL80211_ATTR_MAC])
2906 return -EINVAL;
2907
2908 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2909 return -EINVAL;
2910
2911 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2912 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2913
Johannes Berg4c476992010-10-04 21:36:35 +02002914 if (!rdev->ops->add_mpath)
2915 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002916
Johannes Berg4c476992010-10-04 21:36:35 +02002917 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2918 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002919
Johannes Berg4c476992010-10-04 21:36:35 +02002920 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002921}
2922
2923static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2924{
Johannes Berg4c476992010-10-04 21:36:35 +02002925 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2926 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002927 u8 *dst = NULL;
2928
2929 if (info->attrs[NL80211_ATTR_MAC])
2930 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2931
Johannes Berg4c476992010-10-04 21:36:35 +02002932 if (!rdev->ops->del_mpath)
2933 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002934
Johannes Berg4c476992010-10-04 21:36:35 +02002935 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002936}
2937
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002938static int nl80211_set_bss(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];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002942 struct bss_parameters params;
2943
2944 memset(&params, 0, sizeof(params));
2945 /* default to not changing parameters */
2946 params.use_cts_prot = -1;
2947 params.use_short_preamble = -1;
2948 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002949 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002950 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002951
2952 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2953 params.use_cts_prot =
2954 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2955 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2956 params.use_short_preamble =
2957 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2958 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2959 params.use_short_slot_time =
2960 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002961 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2962 params.basic_rates =
2963 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2964 params.basic_rates_len =
2965 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2966 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002967 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2968 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002969 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2970 params.ht_opmode =
2971 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002972
Johannes Berg4c476992010-10-04 21:36:35 +02002973 if (!rdev->ops->change_bss)
2974 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002975
Johannes Berg074ac8d2010-09-16 14:58:22 +02002976 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002977 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2978 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002979
Johannes Berg4c476992010-10-04 21:36:35 +02002980 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002981}
2982
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002983static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002984 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2985 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2986 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2987 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2988 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2989 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2990};
2991
2992static int parse_reg_rule(struct nlattr *tb[],
2993 struct ieee80211_reg_rule *reg_rule)
2994{
2995 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2996 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2997
2998 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2999 return -EINVAL;
3000 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3001 return -EINVAL;
3002 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3003 return -EINVAL;
3004 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3005 return -EINVAL;
3006 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3007 return -EINVAL;
3008
3009 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3010
3011 freq_range->start_freq_khz =
3012 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3013 freq_range->end_freq_khz =
3014 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3015 freq_range->max_bandwidth_khz =
3016 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3017
3018 power_rule->max_eirp =
3019 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3020
3021 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3022 power_rule->max_antenna_gain =
3023 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3024
3025 return 0;
3026}
3027
3028static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3029{
3030 int r;
3031 char *data = NULL;
3032
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003033 /*
3034 * You should only get this when cfg80211 hasn't yet initialized
3035 * completely when built-in to the kernel right between the time
3036 * window between nl80211_init() and regulatory_init(), if that is
3037 * even possible.
3038 */
3039 mutex_lock(&cfg80211_mutex);
3040 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003041 mutex_unlock(&cfg80211_mutex);
3042 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003043 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003044 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003045
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003046 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3047 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003048
3049 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3050
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003051 r = regulatory_hint_user(data);
3052
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003053 return r;
3054}
3055
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003056static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003057 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003058{
Johannes Berg4c476992010-10-04 21:36:35 +02003059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003060 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003061 struct wireless_dev *wdev = dev->ieee80211_ptr;
3062 struct mesh_config cur_params;
3063 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003064 void *hdr;
3065 struct nlattr *pinfoattr;
3066 struct sk_buff *msg;
3067
Johannes Berg29cbe682010-12-03 09:20:44 +01003068 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3069 return -EOPNOTSUPP;
3070
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003071 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003072 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003073
Johannes Berg29cbe682010-12-03 09:20:44 +01003074 wdev_lock(wdev);
3075 /* If not connected, get default parameters */
3076 if (!wdev->mesh_id_len)
3077 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3078 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003079 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003080 &cur_params);
3081 wdev_unlock(wdev);
3082
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003083 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003084 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003085
3086 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003087 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003088 if (!msg)
3089 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003090 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003091 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003092 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003093 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003094 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003095 if (!pinfoattr)
3096 goto nla_put_failure;
3097 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3098 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3099 cur_params.dot11MeshRetryTimeout);
3100 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3101 cur_params.dot11MeshConfirmTimeout);
3102 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3103 cur_params.dot11MeshHoldingTimeout);
3104 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3105 cur_params.dot11MeshMaxPeerLinks);
3106 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3107 cur_params.dot11MeshMaxRetries);
3108 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3109 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003110 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3111 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003112 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3113 cur_params.auto_open_plinks);
3114 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3115 cur_params.dot11MeshHWMPmaxPREQretries);
3116 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3117 cur_params.path_refresh_time);
3118 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3119 cur_params.min_discovery_timeout);
3120 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3121 cur_params.dot11MeshHWMPactivePathTimeout);
3122 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3123 cur_params.dot11MeshHWMPpreqMinInterval);
3124 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3125 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003126 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3127 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003128 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3129 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003130 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3131 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003132 nla_nest_end(msg, pinfoattr);
3133 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003134 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003135
Johannes Berg3b858752009-03-12 09:55:09 +01003136 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003137 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003138 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003139 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003140 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003141}
3142
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003143static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003144 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3145 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3146 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3147 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3148 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3149 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003150 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003151 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3152
3153 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3154 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3155 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3156 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3157 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3158 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003159 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003160 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003161 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003162};
3163
Javier Cardonac80d5452010-12-16 17:37:49 -08003164static const struct nla_policy
3165 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3166 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3167 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003168 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003169 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003170 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003171 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003172};
3173
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003174static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003175 struct mesh_config *cfg,
3176 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003177{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003178 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003179 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003180
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003181#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3182do {\
3183 if (table[attr_num]) {\
3184 cfg->param = nla_fn(table[attr_num]); \
3185 mask |= (1 << (attr_num - 1)); \
3186 } \
3187} while (0);\
3188
3189
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003190 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003191 return -EINVAL;
3192 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003193 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003194 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003195 return -EINVAL;
3196
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003197 /* This makes sure that there aren't more than 32 mesh config
3198 * parameters (otherwise our bitfield scheme would not work.) */
3199 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3200
3201 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003202 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3203 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3204 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3205 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3206 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3207 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3208 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3209 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3210 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3211 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3212 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3213 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003214 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3215 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003216 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3217 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3218 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3219 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3220 nla_get_u8);
3221 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3222 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3223 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3224 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3225 nla_get_u16);
3226 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3227 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3228 nla_get_u32);
3229 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3230 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3231 nla_get_u16);
3232 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3233 dot11MeshHWMPnetDiameterTraversalTime,
3234 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3235 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003236 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3237 dot11MeshHWMPRootMode, mask,
3238 NL80211_MESHCONF_HWMP_ROOTMODE,
3239 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003240 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3241 dot11MeshHWMPRannInterval, mask,
3242 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3243 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003244 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3245 dot11MeshGateAnnouncementProtocol, mask,
3246 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3247 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003248 if (mask_out)
3249 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003250
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003251 return 0;
3252
3253#undef FILL_IN_MESH_PARAM_IF_SET
3254}
3255
Javier Cardonac80d5452010-12-16 17:37:49 -08003256static int nl80211_parse_mesh_setup(struct genl_info *info,
3257 struct mesh_setup *setup)
3258{
3259 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3260
3261 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3262 return -EINVAL;
3263 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3264 info->attrs[NL80211_ATTR_MESH_SETUP],
3265 nl80211_mesh_setup_params_policy))
3266 return -EINVAL;
3267
3268 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3269 setup->path_sel_proto =
3270 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3271 IEEE80211_PATH_PROTOCOL_VENDOR :
3272 IEEE80211_PATH_PROTOCOL_HWMP;
3273
3274 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3275 setup->path_metric =
3276 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3277 IEEE80211_PATH_METRIC_VENDOR :
3278 IEEE80211_PATH_METRIC_AIRTIME;
3279
Javier Cardona581a8b02011-04-07 15:08:27 -07003280
3281 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003282 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003283 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003284 if (!is_valid_ie_attr(ieattr))
3285 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003286 setup->ie = nla_data(ieattr);
3287 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003288 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003289 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3290 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003291
3292 return 0;
3293}
3294
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003295static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003296 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003297{
3298 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3299 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003300 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003301 struct mesh_config cfg;
3302 u32 mask;
3303 int err;
3304
Johannes Berg29cbe682010-12-03 09:20:44 +01003305 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3306 return -EOPNOTSUPP;
3307
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003308 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003309 return -EOPNOTSUPP;
3310
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003311 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003312 if (err)
3313 return err;
3314
Johannes Berg29cbe682010-12-03 09:20:44 +01003315 wdev_lock(wdev);
3316 if (!wdev->mesh_id_len)
3317 err = -ENOLINK;
3318
3319 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003320 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003321 mask, &cfg);
3322
3323 wdev_unlock(wdev);
3324
3325 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003326}
3327
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003328static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3329{
3330 struct sk_buff *msg;
3331 void *hdr = NULL;
3332 struct nlattr *nl_reg_rules;
3333 unsigned int i;
3334 int err = -EINVAL;
3335
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003336 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003337
3338 if (!cfg80211_regdomain)
3339 goto out;
3340
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003341 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003342 if (!msg) {
3343 err = -ENOBUFS;
3344 goto out;
3345 }
3346
3347 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3348 NL80211_CMD_GET_REG);
3349 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003350 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003351
3352 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3353 cfg80211_regdomain->alpha2);
3354
3355 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3356 if (!nl_reg_rules)
3357 goto nla_put_failure;
3358
3359 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3360 struct nlattr *nl_reg_rule;
3361 const struct ieee80211_reg_rule *reg_rule;
3362 const struct ieee80211_freq_range *freq_range;
3363 const struct ieee80211_power_rule *power_rule;
3364
3365 reg_rule = &cfg80211_regdomain->reg_rules[i];
3366 freq_range = &reg_rule->freq_range;
3367 power_rule = &reg_rule->power_rule;
3368
3369 nl_reg_rule = nla_nest_start(msg, i);
3370 if (!nl_reg_rule)
3371 goto nla_put_failure;
3372
3373 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3374 reg_rule->flags);
3375 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3376 freq_range->start_freq_khz);
3377 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3378 freq_range->end_freq_khz);
3379 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3380 freq_range->max_bandwidth_khz);
3381 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3382 power_rule->max_antenna_gain);
3383 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3384 power_rule->max_eirp);
3385
3386 nla_nest_end(msg, nl_reg_rule);
3387 }
3388
3389 nla_nest_end(msg, nl_reg_rules);
3390
3391 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003392 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003393 goto out;
3394
3395nla_put_failure:
3396 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003397put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003398 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003399 err = -EMSGSIZE;
3400out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003401 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003402 return err;
3403}
3404
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003405static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3406{
3407 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3408 struct nlattr *nl_reg_rule;
3409 char *alpha2 = NULL;
3410 int rem_reg_rules = 0, r = 0;
3411 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3412 struct ieee80211_regdomain *rd = NULL;
3413
3414 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3415 return -EINVAL;
3416
3417 if (!info->attrs[NL80211_ATTR_REG_RULES])
3418 return -EINVAL;
3419
3420 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3421
3422 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3423 rem_reg_rules) {
3424 num_rules++;
3425 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003426 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003427 }
3428
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003429 mutex_lock(&cfg80211_mutex);
3430
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003431 if (!reg_is_valid_request(alpha2)) {
3432 r = -EINVAL;
3433 goto bad_reg;
3434 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003435
3436 size_of_regd = sizeof(struct ieee80211_regdomain) +
3437 (num_rules * sizeof(struct ieee80211_reg_rule));
3438
3439 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003440 if (!rd) {
3441 r = -ENOMEM;
3442 goto bad_reg;
3443 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003444
3445 rd->n_reg_rules = num_rules;
3446 rd->alpha2[0] = alpha2[0];
3447 rd->alpha2[1] = alpha2[1];
3448
3449 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3450 rem_reg_rules) {
3451 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3452 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3453 reg_rule_policy);
3454 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3455 if (r)
3456 goto bad_reg;
3457
3458 rule_idx++;
3459
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003460 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3461 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003462 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003463 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003464 }
3465
3466 BUG_ON(rule_idx != num_rules);
3467
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003468 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003469
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003470 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003471
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003472 return r;
3473
Johannes Bergd2372b32008-10-24 20:32:20 +02003474 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003475 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003476 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003477 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003478}
3479
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003480static int validate_scan_freqs(struct nlattr *freqs)
3481{
3482 struct nlattr *attr1, *attr2;
3483 int n_channels = 0, tmp1, tmp2;
3484
3485 nla_for_each_nested(attr1, freqs, tmp1) {
3486 n_channels++;
3487 /*
3488 * Some hardware has a limited channel list for
3489 * scanning, and it is pretty much nonsensical
3490 * to scan for a channel twice, so disallow that
3491 * and don't require drivers to check that the
3492 * channel list they get isn't longer than what
3493 * they can scan, as long as they can scan all
3494 * the channels they registered at once.
3495 */
3496 nla_for_each_nested(attr2, freqs, tmp2)
3497 if (attr1 != attr2 &&
3498 nla_get_u32(attr1) == nla_get_u32(attr2))
3499 return 0;
3500 }
3501
3502 return n_channels;
3503}
3504
Johannes Berg2a519312009-02-10 21:25:55 +01003505static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3506{
Johannes Berg4c476992010-10-04 21:36:35 +02003507 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3508 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003509 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003510 struct nlattr *attr;
3511 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003512 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003513 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003514
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003515 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3516 return -EINVAL;
3517
Johannes Berg79c97e92009-07-07 03:56:12 +02003518 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003519
Johannes Berg4c476992010-10-04 21:36:35 +02003520 if (!rdev->ops->scan)
3521 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003522
Johannes Berg4c476992010-10-04 21:36:35 +02003523 if (rdev->scan_req)
3524 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003525
3526 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003527 n_channels = validate_scan_freqs(
3528 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003529 if (!n_channels)
3530 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003531 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003532 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003533 n_channels = 0;
3534
Johannes Berg2a519312009-02-10 21:25:55 +01003535 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3536 if (wiphy->bands[band])
3537 n_channels += wiphy->bands[band]->n_channels;
3538 }
3539
3540 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3541 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3542 n_ssids++;
3543
Johannes Berg4c476992010-10-04 21:36:35 +02003544 if (n_ssids > wiphy->max_scan_ssids)
3545 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003546
Jouni Malinen70692ad2009-02-16 19:39:13 +02003547 if (info->attrs[NL80211_ATTR_IE])
3548 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3549 else
3550 ie_len = 0;
3551
Johannes Berg4c476992010-10-04 21:36:35 +02003552 if (ie_len > wiphy->max_scan_ie_len)
3553 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003554
Johannes Berg2a519312009-02-10 21:25:55 +01003555 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003556 + sizeof(*request->ssids) * n_ssids
3557 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003558 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003559 if (!request)
3560 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003561
Johannes Berg2a519312009-02-10 21:25:55 +01003562 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003563 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003564 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003565 if (ie_len) {
3566 if (request->ssids)
3567 request->ie = (void *)(request->ssids + n_ssids);
3568 else
3569 request->ie = (void *)(request->channels + n_channels);
3570 }
Johannes Berg2a519312009-02-10 21:25:55 +01003571
Johannes Berg584991d2009-11-02 13:32:03 +01003572 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003573 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3574 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003575 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003576 struct ieee80211_channel *chan;
3577
3578 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3579
3580 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003581 err = -EINVAL;
3582 goto out_free;
3583 }
Johannes Berg584991d2009-11-02 13:32:03 +01003584
3585 /* ignore disabled channels */
3586 if (chan->flags & IEEE80211_CHAN_DISABLED)
3587 continue;
3588
3589 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003590 i++;
3591 }
3592 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003593 enum ieee80211_band band;
3594
Johannes Berg2a519312009-02-10 21:25:55 +01003595 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003596 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3597 int j;
3598 if (!wiphy->bands[band])
3599 continue;
3600 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003601 struct ieee80211_channel *chan;
3602
3603 chan = &wiphy->bands[band]->channels[j];
3604
3605 if (chan->flags & IEEE80211_CHAN_DISABLED)
3606 continue;
3607
3608 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003609 i++;
3610 }
3611 }
3612 }
3613
Johannes Berg584991d2009-11-02 13:32:03 +01003614 if (!i) {
3615 err = -EINVAL;
3616 goto out_free;
3617 }
3618
3619 request->n_channels = i;
3620
Johannes Berg2a519312009-02-10 21:25:55 +01003621 i = 0;
3622 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3623 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003624 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003625 err = -EINVAL;
3626 goto out_free;
3627 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003628 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003629 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003630 i++;
3631 }
3632 }
3633
Jouni Malinen70692ad2009-02-16 19:39:13 +02003634 if (info->attrs[NL80211_ATTR_IE]) {
3635 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003636 memcpy((void *)request->ie,
3637 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003638 request->ie_len);
3639 }
3640
Johannes Berg34850ab2011-07-18 18:08:35 +02003641 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003642 if (wiphy->bands[i])
3643 request->rates[i] =
3644 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003645
3646 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3647 nla_for_each_nested(attr,
3648 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3649 tmp) {
3650 enum ieee80211_band band = nla_type(attr);
3651
Dan Carpenter84404622011-07-29 11:52:18 +03003652 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003653 err = -EINVAL;
3654 goto out_free;
3655 }
3656 err = ieee80211_get_ratemask(wiphy->bands[band],
3657 nla_data(attr),
3658 nla_len(attr),
3659 &request->rates[band]);
3660 if (err)
3661 goto out_free;
3662 }
3663 }
3664
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303665 request->no_cck =
3666 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3667
Johannes Berg463d0182009-07-14 00:33:35 +02003668 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003669 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003670
Johannes Berg79c97e92009-07-07 03:56:12 +02003671 rdev->scan_req = request;
3672 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003673
Johannes Berg463d0182009-07-14 00:33:35 +02003674 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003675 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003676 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003677 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003678 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003679 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003680 kfree(request);
3681 }
Johannes Berg3b858752009-03-12 09:55:09 +01003682
Johannes Berg2a519312009-02-10 21:25:55 +01003683 return err;
3684}
3685
Luciano Coelho807f8a82011-05-11 17:09:35 +03003686static int nl80211_start_sched_scan(struct sk_buff *skb,
3687 struct genl_info *info)
3688{
3689 struct cfg80211_sched_scan_request *request;
3690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3691 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003692 struct nlattr *attr;
3693 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003694 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003695 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003696 enum ieee80211_band band;
3697 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003698 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003699
3700 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3701 !rdev->ops->sched_scan_start)
3702 return -EOPNOTSUPP;
3703
3704 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3705 return -EINVAL;
3706
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003707 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3708 return -EINVAL;
3709
3710 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3711 if (interval == 0)
3712 return -EINVAL;
3713
Luciano Coelho807f8a82011-05-11 17:09:35 +03003714 wiphy = &rdev->wiphy;
3715
3716 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3717 n_channels = validate_scan_freqs(
3718 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3719 if (!n_channels)
3720 return -EINVAL;
3721 } else {
3722 n_channels = 0;
3723
3724 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3725 if (wiphy->bands[band])
3726 n_channels += wiphy->bands[band]->n_channels;
3727 }
3728
3729 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3730 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3731 tmp)
3732 n_ssids++;
3733
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003734 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003735 return -EINVAL;
3736
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003737 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3738 nla_for_each_nested(attr,
3739 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3740 tmp)
3741 n_match_sets++;
3742
3743 if (n_match_sets > wiphy->max_match_sets)
3744 return -EINVAL;
3745
Luciano Coelho807f8a82011-05-11 17:09:35 +03003746 if (info->attrs[NL80211_ATTR_IE])
3747 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3748 else
3749 ie_len = 0;
3750
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003751 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003752 return -EINVAL;
3753
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003754 mutex_lock(&rdev->sched_scan_mtx);
3755
3756 if (rdev->sched_scan_req) {
3757 err = -EINPROGRESS;
3758 goto out;
3759 }
3760
Luciano Coelho807f8a82011-05-11 17:09:35 +03003761 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003762 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003763 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003764 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003765 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003766 if (!request) {
3767 err = -ENOMEM;
3768 goto out;
3769 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003770
3771 if (n_ssids)
3772 request->ssids = (void *)&request->channels[n_channels];
3773 request->n_ssids = n_ssids;
3774 if (ie_len) {
3775 if (request->ssids)
3776 request->ie = (void *)(request->ssids + n_ssids);
3777 else
3778 request->ie = (void *)(request->channels + n_channels);
3779 }
3780
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003781 if (n_match_sets) {
3782 if (request->ie)
3783 request->match_sets = (void *)(request->ie + ie_len);
3784 else if (request->ssids)
3785 request->match_sets =
3786 (void *)(request->ssids + n_ssids);
3787 else
3788 request->match_sets =
3789 (void *)(request->channels + n_channels);
3790 }
3791 request->n_match_sets = n_match_sets;
3792
Luciano Coelho807f8a82011-05-11 17:09:35 +03003793 i = 0;
3794 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3795 /* user specified, bail out if channel not found */
3796 nla_for_each_nested(attr,
3797 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3798 tmp) {
3799 struct ieee80211_channel *chan;
3800
3801 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3802
3803 if (!chan) {
3804 err = -EINVAL;
3805 goto out_free;
3806 }
3807
3808 /* ignore disabled channels */
3809 if (chan->flags & IEEE80211_CHAN_DISABLED)
3810 continue;
3811
3812 request->channels[i] = chan;
3813 i++;
3814 }
3815 } else {
3816 /* all channels */
3817 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3818 int j;
3819 if (!wiphy->bands[band])
3820 continue;
3821 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3822 struct ieee80211_channel *chan;
3823
3824 chan = &wiphy->bands[band]->channels[j];
3825
3826 if (chan->flags & IEEE80211_CHAN_DISABLED)
3827 continue;
3828
3829 request->channels[i] = chan;
3830 i++;
3831 }
3832 }
3833 }
3834
3835 if (!i) {
3836 err = -EINVAL;
3837 goto out_free;
3838 }
3839
3840 request->n_channels = i;
3841
3842 i = 0;
3843 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3844 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3845 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003846 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003847 err = -EINVAL;
3848 goto out_free;
3849 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003850 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003851 memcpy(request->ssids[i].ssid, nla_data(attr),
3852 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003853 i++;
3854 }
3855 }
3856
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003857 i = 0;
3858 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3859 nla_for_each_nested(attr,
3860 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3861 tmp) {
3862 struct nlattr *ssid;
3863
3864 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3865 nla_data(attr), nla_len(attr),
3866 nl80211_match_policy);
3867 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3868 if (ssid) {
3869 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3870 err = -EINVAL;
3871 goto out_free;
3872 }
3873 memcpy(request->match_sets[i].ssid.ssid,
3874 nla_data(ssid), nla_len(ssid));
3875 request->match_sets[i].ssid.ssid_len =
3876 nla_len(ssid);
3877 }
3878 i++;
3879 }
3880 }
3881
Luciano Coelho807f8a82011-05-11 17:09:35 +03003882 if (info->attrs[NL80211_ATTR_IE]) {
3883 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3884 memcpy((void *)request->ie,
3885 nla_data(info->attrs[NL80211_ATTR_IE]),
3886 request->ie_len);
3887 }
3888
3889 request->dev = dev;
3890 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003891 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003892
3893 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3894 if (!err) {
3895 rdev->sched_scan_req = request;
3896 nl80211_send_sched_scan(rdev, dev,
3897 NL80211_CMD_START_SCHED_SCAN);
3898 goto out;
3899 }
3900
3901out_free:
3902 kfree(request);
3903out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003904 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003905 return err;
3906}
3907
3908static int nl80211_stop_sched_scan(struct sk_buff *skb,
3909 struct genl_info *info)
3910{
3911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003912 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003913
3914 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3915 !rdev->ops->sched_scan_stop)
3916 return -EOPNOTSUPP;
3917
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003918 mutex_lock(&rdev->sched_scan_mtx);
3919 err = __cfg80211_stop_sched_scan(rdev, false);
3920 mutex_unlock(&rdev->sched_scan_mtx);
3921
3922 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003923}
3924
Johannes Berg9720bb32011-06-21 09:45:33 +02003925static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3926 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003927 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003928 struct wireless_dev *wdev,
3929 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003930{
Johannes Berg48ab9052009-07-10 18:42:31 +02003931 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003932 void *hdr;
3933 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003934 int i;
3935
3936 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003937
Johannes Berg9720bb32011-06-21 09:45:33 +02003938 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003939 NL80211_CMD_NEW_SCAN_RESULTS);
3940 if (!hdr)
3941 return -1;
3942
Johannes Berg9720bb32011-06-21 09:45:33 +02003943 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3944
Johannes Bergf5ea9122009-08-07 16:17:38 +02003945 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003946 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003947
3948 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3949 if (!bss)
3950 goto nla_put_failure;
3951 if (!is_zero_ether_addr(res->bssid))
3952 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3953 if (res->information_elements && res->len_information_elements)
3954 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3955 res->len_information_elements,
3956 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003957 if (res->beacon_ies && res->len_beacon_ies &&
3958 res->beacon_ies != res->information_elements)
3959 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3960 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003961 if (res->tsf)
3962 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3963 if (res->beacon_interval)
3964 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3965 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3966 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003967 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3968 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003969
Johannes Berg77965c92009-02-18 18:45:06 +01003970 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003971 case CFG80211_SIGNAL_TYPE_MBM:
3972 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3973 break;
3974 case CFG80211_SIGNAL_TYPE_UNSPEC:
3975 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3976 break;
3977 default:
3978 break;
3979 }
3980
Johannes Berg48ab9052009-07-10 18:42:31 +02003981 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003982 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003983 case NL80211_IFTYPE_STATION:
3984 if (intbss == wdev->current_bss)
3985 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3986 NL80211_BSS_STATUS_ASSOCIATED);
3987 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3988 if (intbss != wdev->auth_bsses[i])
3989 continue;
3990 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3991 NL80211_BSS_STATUS_AUTHENTICATED);
3992 break;
3993 }
3994 break;
3995 case NL80211_IFTYPE_ADHOC:
3996 if (intbss == wdev->current_bss)
3997 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3998 NL80211_BSS_STATUS_IBSS_JOINED);
3999 break;
4000 default:
4001 break;
4002 }
4003
Johannes Berg2a519312009-02-10 21:25:55 +01004004 nla_nest_end(msg, bss);
4005
4006 return genlmsg_end(msg, hdr);
4007
4008 nla_put_failure:
4009 genlmsg_cancel(msg, hdr);
4010 return -EMSGSIZE;
4011}
4012
4013static int nl80211_dump_scan(struct sk_buff *skb,
4014 struct netlink_callback *cb)
4015{
Johannes Berg48ab9052009-07-10 18:42:31 +02004016 struct cfg80211_registered_device *rdev;
4017 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004018 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004019 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004020 int start = cb->args[1], idx = 0;
4021 int err;
4022
Johannes Berg67748892010-10-04 21:14:06 +02004023 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4024 if (err)
4025 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004026
Johannes Berg48ab9052009-07-10 18:42:31 +02004027 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004028
Johannes Berg48ab9052009-07-10 18:42:31 +02004029 wdev_lock(wdev);
4030 spin_lock_bh(&rdev->bss_lock);
4031 cfg80211_bss_expire(rdev);
4032
Johannes Berg9720bb32011-06-21 09:45:33 +02004033 cb->seq = rdev->bss_generation;
4034
Johannes Berg48ab9052009-07-10 18:42:31 +02004035 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004036 if (++idx <= start)
4037 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004038 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004039 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004040 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004041 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004042 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004043 }
4044 }
4045
Johannes Berg48ab9052009-07-10 18:42:31 +02004046 spin_unlock_bh(&rdev->bss_lock);
4047 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004048
4049 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004050 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004051
Johannes Berg67748892010-10-04 21:14:06 +02004052 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004053}
4054
Holger Schurig61fa7132009-11-11 12:25:40 +01004055static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4056 int flags, struct net_device *dev,
4057 struct survey_info *survey)
4058{
4059 void *hdr;
4060 struct nlattr *infoattr;
4061
Holger Schurig61fa7132009-11-11 12:25:40 +01004062 hdr = nl80211hdr_put(msg, pid, seq, flags,
4063 NL80211_CMD_NEW_SURVEY_RESULTS);
4064 if (!hdr)
4065 return -ENOMEM;
4066
4067 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4068
4069 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4070 if (!infoattr)
4071 goto nla_put_failure;
4072
4073 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4074 survey->channel->center_freq);
4075 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4076 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4077 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004078 if (survey->filled & SURVEY_INFO_IN_USE)
4079 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004080 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4081 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4082 survey->channel_time);
4083 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4084 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4085 survey->channel_time_busy);
4086 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4087 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4088 survey->channel_time_ext_busy);
4089 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4090 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4091 survey->channel_time_rx);
4092 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4093 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4094 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004095
4096 nla_nest_end(msg, infoattr);
4097
4098 return genlmsg_end(msg, hdr);
4099
4100 nla_put_failure:
4101 genlmsg_cancel(msg, hdr);
4102 return -EMSGSIZE;
4103}
4104
4105static int nl80211_dump_survey(struct sk_buff *skb,
4106 struct netlink_callback *cb)
4107{
4108 struct survey_info survey;
4109 struct cfg80211_registered_device *dev;
4110 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004111 int survey_idx = cb->args[1];
4112 int res;
4113
Johannes Berg67748892010-10-04 21:14:06 +02004114 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4115 if (res)
4116 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004117
4118 if (!dev->ops->dump_survey) {
4119 res = -EOPNOTSUPP;
4120 goto out_err;
4121 }
4122
4123 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004124 struct ieee80211_channel *chan;
4125
Holger Schurig61fa7132009-11-11 12:25:40 +01004126 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4127 &survey);
4128 if (res == -ENOENT)
4129 break;
4130 if (res)
4131 goto out_err;
4132
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004133 /* Survey without a channel doesn't make sense */
4134 if (!survey.channel) {
4135 res = -EINVAL;
4136 goto out;
4137 }
4138
4139 chan = ieee80211_get_channel(&dev->wiphy,
4140 survey.channel->center_freq);
4141 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4142 survey_idx++;
4143 continue;
4144 }
4145
Holger Schurig61fa7132009-11-11 12:25:40 +01004146 if (nl80211_send_survey(skb,
4147 NETLINK_CB(cb->skb).pid,
4148 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4149 netdev,
4150 &survey) < 0)
4151 goto out;
4152 survey_idx++;
4153 }
4154
4155 out:
4156 cb->args[1] = survey_idx;
4157 res = skb->len;
4158 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004159 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004160 return res;
4161}
4162
Jouni Malinen255e7372009-03-20 21:21:17 +02004163static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4164{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004165 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004166}
4167
Samuel Ortizb23aa672009-07-01 21:26:54 +02004168static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4169{
4170 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4171 NL80211_WPA_VERSION_2));
4172}
4173
Jouni Malinen636a5d32009-03-19 13:39:22 +02004174static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4175{
Johannes Berg4c476992010-10-04 21:36:35 +02004176 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4177 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004178 struct ieee80211_channel *chan;
4179 const u8 *bssid, *ssid, *ie = NULL;
4180 int err, ssid_len, ie_len = 0;
4181 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004182 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004183 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004184
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004185 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4186 return -EINVAL;
4187
4188 if (!info->attrs[NL80211_ATTR_MAC])
4189 return -EINVAL;
4190
Jouni Malinen17780922009-03-27 20:52:47 +02004191 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4192 return -EINVAL;
4193
Johannes Berg19957bb2009-07-02 17:20:43 +02004194 if (!info->attrs[NL80211_ATTR_SSID])
4195 return -EINVAL;
4196
4197 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4198 return -EINVAL;
4199
Johannes Bergfffd0932009-07-08 14:22:54 +02004200 err = nl80211_parse_key(info, &key);
4201 if (err)
4202 return err;
4203
4204 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004205 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4206 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004207 if (!key.p.key || !key.p.key_len)
4208 return -EINVAL;
4209 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4210 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4211 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4212 key.p.key_len != WLAN_KEY_LEN_WEP104))
4213 return -EINVAL;
4214 if (key.idx > 4)
4215 return -EINVAL;
4216 } else {
4217 key.p.key_len = 0;
4218 key.p.key = NULL;
4219 }
4220
Johannes Bergafea0b72010-08-10 09:46:42 +02004221 if (key.idx >= 0) {
4222 int i;
4223 bool ok = false;
4224 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4225 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4226 ok = true;
4227 break;
4228 }
4229 }
Johannes Berg4c476992010-10-04 21:36:35 +02004230 if (!ok)
4231 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004232 }
4233
Johannes Berg4c476992010-10-04 21:36:35 +02004234 if (!rdev->ops->auth)
4235 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004236
Johannes Berg074ac8d2010-09-16 14:58:22 +02004237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004238 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4239 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004240
Johannes Berg19957bb2009-07-02 17:20:43 +02004241 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004242 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004243 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004244 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4245 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004246
Johannes Berg19957bb2009-07-02 17:20:43 +02004247 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4248 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4249
4250 if (info->attrs[NL80211_ATTR_IE]) {
4251 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4252 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4253 }
4254
4255 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004256 if (!nl80211_valid_auth_type(auth_type))
4257 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004258
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004259 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4260
Johannes Berg4c476992010-10-04 21:36:35 +02004261 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4262 ssid, ssid_len, ie, ie_len,
4263 key.p.key, key.p.key_len, key.idx,
4264 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004265}
4266
Johannes Bergc0692b82010-08-27 14:26:53 +03004267static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4268 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004269 struct cfg80211_crypto_settings *settings,
4270 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004271{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004272 memset(settings, 0, sizeof(*settings));
4273
Samuel Ortizb23aa672009-07-01 21:26:54 +02004274 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4275
Johannes Bergc0692b82010-08-27 14:26:53 +03004276 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4277 u16 proto;
4278 proto = nla_get_u16(
4279 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4280 settings->control_port_ethertype = cpu_to_be16(proto);
4281 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4282 proto != ETH_P_PAE)
4283 return -EINVAL;
4284 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4285 settings->control_port_no_encrypt = true;
4286 } else
4287 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4288
Samuel Ortizb23aa672009-07-01 21:26:54 +02004289 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4290 void *data;
4291 int len, i;
4292
4293 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4294 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4295 settings->n_ciphers_pairwise = len / sizeof(u32);
4296
4297 if (len % sizeof(u32))
4298 return -EINVAL;
4299
Johannes Berg3dc27d22009-07-02 21:36:37 +02004300 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004301 return -EINVAL;
4302
4303 memcpy(settings->ciphers_pairwise, data, len);
4304
4305 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004306 if (!cfg80211_supported_cipher_suite(
4307 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004308 settings->ciphers_pairwise[i]))
4309 return -EINVAL;
4310 }
4311
4312 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4313 settings->cipher_group =
4314 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004315 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4316 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004317 return -EINVAL;
4318 }
4319
4320 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4321 settings->wpa_versions =
4322 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4323 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4324 return -EINVAL;
4325 }
4326
4327 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4328 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004329 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004330
4331 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4332 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4333 settings->n_akm_suites = len / sizeof(u32);
4334
4335 if (len % sizeof(u32))
4336 return -EINVAL;
4337
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004338 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4339 return -EINVAL;
4340
Samuel Ortizb23aa672009-07-01 21:26:54 +02004341 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004342 }
4343
4344 return 0;
4345}
4346
Jouni Malinen636a5d32009-03-19 13:39:22 +02004347static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4348{
Johannes Berg4c476992010-10-04 21:36:35 +02004349 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4350 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004351 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004352 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004353 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004354 int err, ssid_len, ie_len = 0;
4355 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004356
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004357 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4358 return -EINVAL;
4359
4360 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004361 !info->attrs[NL80211_ATTR_SSID] ||
4362 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004363 return -EINVAL;
4364
Johannes Berg4c476992010-10-04 21:36:35 +02004365 if (!rdev->ops->assoc)
4366 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004367
Johannes Berg074ac8d2010-09-16 14:58:22 +02004368 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004369 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4370 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004371
Johannes Berg19957bb2009-07-02 17:20:43 +02004372 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004373
Johannes Berg19957bb2009-07-02 17:20:43 +02004374 chan = ieee80211_get_channel(&rdev->wiphy,
4375 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004376 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4377 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004378
Johannes Berg19957bb2009-07-02 17:20:43 +02004379 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4380 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004381
4382 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004383 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4384 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004385 }
4386
Jouni Malinendc6382c2009-05-06 22:09:37 +03004387 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004388 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004389 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004390 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004391 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004392 else if (mfp != NL80211_MFP_NO)
4393 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004394 }
4395
Johannes Berg3e5d7642009-07-07 14:37:26 +02004396 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4397 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4398
Johannes Bergc0692b82010-08-27 14:26:53 +03004399 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004400 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004401 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4402 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004403 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004404
Jouni Malinen636a5d32009-03-19 13:39:22 +02004405 return err;
4406}
4407
4408static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4409{
Johannes Berg4c476992010-10-04 21:36:35 +02004410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4411 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004412 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004413 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004414 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004415 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004416
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004417 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4418 return -EINVAL;
4419
4420 if (!info->attrs[NL80211_ATTR_MAC])
4421 return -EINVAL;
4422
4423 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4424 return -EINVAL;
4425
Johannes Berg4c476992010-10-04 21:36:35 +02004426 if (!rdev->ops->deauth)
4427 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004428
Johannes Berg074ac8d2010-09-16 14:58:22 +02004429 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004430 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4431 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004432
Johannes Berg19957bb2009-07-02 17:20:43 +02004433 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004434
Johannes Berg19957bb2009-07-02 17:20:43 +02004435 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4436 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004437 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004438 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004439 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004440
4441 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004442 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4443 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004444 }
4445
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004446 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4447
Johannes Berg4c476992010-10-04 21:36:35 +02004448 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4449 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004450}
4451
4452static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4453{
Johannes Berg4c476992010-10-04 21:36:35 +02004454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4455 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004456 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004457 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004458 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004459 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004460
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004461 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4462 return -EINVAL;
4463
4464 if (!info->attrs[NL80211_ATTR_MAC])
4465 return -EINVAL;
4466
4467 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4468 return -EINVAL;
4469
Johannes Berg4c476992010-10-04 21:36:35 +02004470 if (!rdev->ops->disassoc)
4471 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004472
Johannes Berg074ac8d2010-09-16 14:58:22 +02004473 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004474 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4475 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004476
Johannes Berg19957bb2009-07-02 17:20:43 +02004477 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004478
Johannes Berg19957bb2009-07-02 17:20:43 +02004479 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4480 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004481 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004482 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004483 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004484
4485 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004486 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4487 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004488 }
4489
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004490 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4491
Johannes Berg4c476992010-10-04 21:36:35 +02004492 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4493 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004494}
4495
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004496static bool
4497nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4498 int mcast_rate[IEEE80211_NUM_BANDS],
4499 int rateval)
4500{
4501 struct wiphy *wiphy = &rdev->wiphy;
4502 bool found = false;
4503 int band, i;
4504
4505 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4506 struct ieee80211_supported_band *sband;
4507
4508 sband = wiphy->bands[band];
4509 if (!sband)
4510 continue;
4511
4512 for (i = 0; i < sband->n_bitrates; i++) {
4513 if (sband->bitrates[i].bitrate == rateval) {
4514 mcast_rate[band] = i + 1;
4515 found = true;
4516 break;
4517 }
4518 }
4519 }
4520
4521 return found;
4522}
4523
Johannes Berg04a773a2009-04-19 21:24:32 +02004524static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4525{
Johannes Berg4c476992010-10-04 21:36:35 +02004526 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4527 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004528 struct cfg80211_ibss_params ibss;
4529 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004530 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004531 int err;
4532
Johannes Berg8e30bc52009-04-22 17:45:38 +02004533 memset(&ibss, 0, sizeof(ibss));
4534
Johannes Berg04a773a2009-04-19 21:24:32 +02004535 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4536 return -EINVAL;
4537
4538 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4539 !info->attrs[NL80211_ATTR_SSID] ||
4540 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4541 return -EINVAL;
4542
Johannes Berg8e30bc52009-04-22 17:45:38 +02004543 ibss.beacon_interval = 100;
4544
4545 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4546 ibss.beacon_interval =
4547 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4548 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4549 return -EINVAL;
4550 }
4551
Johannes Berg4c476992010-10-04 21:36:35 +02004552 if (!rdev->ops->join_ibss)
4553 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004554
Johannes Berg4c476992010-10-04 21:36:35 +02004555 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4556 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004557
Johannes Berg79c97e92009-07-07 03:56:12 +02004558 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004559
Johannes Berg39193492011-09-16 13:45:25 +02004560 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004561 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004562
4563 if (!is_valid_ether_addr(ibss.bssid))
4564 return -EINVAL;
4565 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004566 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4567 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4568
4569 if (info->attrs[NL80211_ATTR_IE]) {
4570 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4571 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4572 }
4573
4574 ibss.channel = ieee80211_get_channel(wiphy,
4575 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4576 if (!ibss.channel ||
4577 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004578 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4579 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004580
4581 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004582 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004583
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004584 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4585 u8 *rates =
4586 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4587 int n_rates =
4588 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4589 struct ieee80211_supported_band *sband =
4590 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004591 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004592
Johannes Berg34850ab2011-07-18 18:08:35 +02004593 err = ieee80211_get_ratemask(sband, rates, n_rates,
4594 &ibss.basic_rates);
4595 if (err)
4596 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004597 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004598
4599 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4600 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4601 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4602 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004603
Johannes Berg4c476992010-10-04 21:36:35 +02004604 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4605 connkeys = nl80211_parse_connkeys(rdev,
4606 info->attrs[NL80211_ATTR_KEYS]);
4607 if (IS_ERR(connkeys))
4608 return PTR_ERR(connkeys);
4609 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004610
Johannes Berg4c476992010-10-04 21:36:35 +02004611 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004612 if (err)
4613 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004614 return err;
4615}
4616
4617static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4618{
Johannes Berg4c476992010-10-04 21:36:35 +02004619 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4620 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004621
Johannes Berg4c476992010-10-04 21:36:35 +02004622 if (!rdev->ops->leave_ibss)
4623 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004624
Johannes Berg4c476992010-10-04 21:36:35 +02004625 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4626 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004627
Johannes Berg4c476992010-10-04 21:36:35 +02004628 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004629}
4630
Johannes Bergaff89a92009-07-01 21:26:51 +02004631#ifdef CONFIG_NL80211_TESTMODE
4632static struct genl_multicast_group nl80211_testmode_mcgrp = {
4633 .name = "testmode",
4634};
4635
4636static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4637{
Johannes Berg4c476992010-10-04 21:36:35 +02004638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004639 int err;
4640
4641 if (!info->attrs[NL80211_ATTR_TESTDATA])
4642 return -EINVAL;
4643
Johannes Bergaff89a92009-07-01 21:26:51 +02004644 err = -EOPNOTSUPP;
4645 if (rdev->ops->testmode_cmd) {
4646 rdev->testmode_info = info;
4647 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4648 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4649 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4650 rdev->testmode_info = NULL;
4651 }
4652
Johannes Bergaff89a92009-07-01 21:26:51 +02004653 return err;
4654}
4655
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004656static int nl80211_testmode_dump(struct sk_buff *skb,
4657 struct netlink_callback *cb)
4658{
4659 struct cfg80211_registered_device *dev;
4660 int err;
4661 long phy_idx;
4662 void *data = NULL;
4663 int data_len = 0;
4664
4665 if (cb->args[0]) {
4666 /*
4667 * 0 is a valid index, but not valid for args[0],
4668 * so we need to offset by 1.
4669 */
4670 phy_idx = cb->args[0] - 1;
4671 } else {
4672 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4673 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4674 nl80211_policy);
4675 if (err)
4676 return err;
4677 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4678 return -EINVAL;
4679 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4680 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4681 cb->args[1] =
4682 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4683 }
4684
4685 if (cb->args[1]) {
4686 data = nla_data((void *)cb->args[1]);
4687 data_len = nla_len((void *)cb->args[1]);
4688 }
4689
4690 mutex_lock(&cfg80211_mutex);
4691 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4692 if (!dev) {
4693 mutex_unlock(&cfg80211_mutex);
4694 return -ENOENT;
4695 }
4696 cfg80211_lock_rdev(dev);
4697 mutex_unlock(&cfg80211_mutex);
4698
4699 if (!dev->ops->testmode_dump) {
4700 err = -EOPNOTSUPP;
4701 goto out_err;
4702 }
4703
4704 while (1) {
4705 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4706 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4707 NL80211_CMD_TESTMODE);
4708 struct nlattr *tmdata;
4709
4710 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4711 genlmsg_cancel(skb, hdr);
4712 break;
4713 }
4714
4715 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4716 if (!tmdata) {
4717 genlmsg_cancel(skb, hdr);
4718 break;
4719 }
4720 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4721 data, data_len);
4722 nla_nest_end(skb, tmdata);
4723
4724 if (err == -ENOBUFS || err == -ENOENT) {
4725 genlmsg_cancel(skb, hdr);
4726 break;
4727 } else if (err) {
4728 genlmsg_cancel(skb, hdr);
4729 goto out_err;
4730 }
4731
4732 genlmsg_end(skb, hdr);
4733 }
4734
4735 err = skb->len;
4736 /* see above */
4737 cb->args[0] = phy_idx + 1;
4738 out_err:
4739 cfg80211_unlock_rdev(dev);
4740 return err;
4741}
4742
Johannes Bergaff89a92009-07-01 21:26:51 +02004743static struct sk_buff *
4744__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4745 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4746{
4747 struct sk_buff *skb;
4748 void *hdr;
4749 struct nlattr *data;
4750
4751 skb = nlmsg_new(approxlen + 100, gfp);
4752 if (!skb)
4753 return NULL;
4754
4755 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4756 if (!hdr) {
4757 kfree_skb(skb);
4758 return NULL;
4759 }
4760
4761 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4762 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4763
4764 ((void **)skb->cb)[0] = rdev;
4765 ((void **)skb->cb)[1] = hdr;
4766 ((void **)skb->cb)[2] = data;
4767
4768 return skb;
4769
4770 nla_put_failure:
4771 kfree_skb(skb);
4772 return NULL;
4773}
4774
4775struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4776 int approxlen)
4777{
4778 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4779
4780 if (WARN_ON(!rdev->testmode_info))
4781 return NULL;
4782
4783 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4784 rdev->testmode_info->snd_pid,
4785 rdev->testmode_info->snd_seq,
4786 GFP_KERNEL);
4787}
4788EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4789
4790int cfg80211_testmode_reply(struct sk_buff *skb)
4791{
4792 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4793 void *hdr = ((void **)skb->cb)[1];
4794 struct nlattr *data = ((void **)skb->cb)[2];
4795
4796 if (WARN_ON(!rdev->testmode_info)) {
4797 kfree_skb(skb);
4798 return -EINVAL;
4799 }
4800
4801 nla_nest_end(skb, data);
4802 genlmsg_end(skb, hdr);
4803 return genlmsg_reply(skb, rdev->testmode_info);
4804}
4805EXPORT_SYMBOL(cfg80211_testmode_reply);
4806
4807struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4808 int approxlen, gfp_t gfp)
4809{
4810 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4811
4812 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4813}
4814EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4815
4816void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4817{
4818 void *hdr = ((void **)skb->cb)[1];
4819 struct nlattr *data = ((void **)skb->cb)[2];
4820
4821 nla_nest_end(skb, data);
4822 genlmsg_end(skb, hdr);
4823 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4824}
4825EXPORT_SYMBOL(cfg80211_testmode_event);
4826#endif
4827
Samuel Ortizb23aa672009-07-01 21:26:54 +02004828static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4829{
Johannes Berg4c476992010-10-04 21:36:35 +02004830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4831 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004832 struct cfg80211_connect_params connect;
4833 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004834 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004835 int err;
4836
4837 memset(&connect, 0, sizeof(connect));
4838
4839 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4840 return -EINVAL;
4841
4842 if (!info->attrs[NL80211_ATTR_SSID] ||
4843 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4844 return -EINVAL;
4845
4846 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4847 connect.auth_type =
4848 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4849 if (!nl80211_valid_auth_type(connect.auth_type))
4850 return -EINVAL;
4851 } else
4852 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4853
4854 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4855
Johannes Bergc0692b82010-08-27 14:26:53 +03004856 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004857 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004858 if (err)
4859 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004860
Johannes Berg074ac8d2010-09-16 14:58:22 +02004861 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004862 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4863 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004864
Johannes Berg79c97e92009-07-07 03:56:12 +02004865 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004866
Samuel Ortizb23aa672009-07-01 21:26:54 +02004867 if (info->attrs[NL80211_ATTR_MAC])
4868 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4869 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4870 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4871
4872 if (info->attrs[NL80211_ATTR_IE]) {
4873 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4874 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4875 }
4876
4877 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4878 connect.channel =
4879 ieee80211_get_channel(wiphy,
4880 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4881 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004882 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4883 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004884 }
4885
Johannes Bergfffd0932009-07-08 14:22:54 +02004886 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4887 connkeys = nl80211_parse_connkeys(rdev,
4888 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004889 if (IS_ERR(connkeys))
4890 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004891 }
4892
4893 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004894 if (err)
4895 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004896 return err;
4897}
4898
4899static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4900{
Johannes Berg4c476992010-10-04 21:36:35 +02004901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4902 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004903 u16 reason;
4904
4905 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4906 reason = WLAN_REASON_DEAUTH_LEAVING;
4907 else
4908 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4909
4910 if (reason == 0)
4911 return -EINVAL;
4912
Johannes Berg074ac8d2010-09-16 14:58:22 +02004913 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004914 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4915 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004916
Johannes Berg4c476992010-10-04 21:36:35 +02004917 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004918}
4919
Johannes Berg463d0182009-07-14 00:33:35 +02004920static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4921{
Johannes Berg4c476992010-10-04 21:36:35 +02004922 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004923 struct net *net;
4924 int err;
4925 u32 pid;
4926
4927 if (!info->attrs[NL80211_ATTR_PID])
4928 return -EINVAL;
4929
4930 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4931
Johannes Berg463d0182009-07-14 00:33:35 +02004932 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004933 if (IS_ERR(net))
4934 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004935
4936 err = 0;
4937
4938 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004939 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4940 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004941
Johannes Berg463d0182009-07-14 00:33:35 +02004942 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004943 return err;
4944}
4945
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004946static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4947{
Johannes Berg4c476992010-10-04 21:36:35 +02004948 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004949 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4950 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004951 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004952 struct cfg80211_pmksa pmksa;
4953
4954 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4955
4956 if (!info->attrs[NL80211_ATTR_MAC])
4957 return -EINVAL;
4958
4959 if (!info->attrs[NL80211_ATTR_PMKID])
4960 return -EINVAL;
4961
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004962 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4963 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4964
Johannes Berg074ac8d2010-09-16 14:58:22 +02004965 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004966 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4967 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004968
4969 switch (info->genlhdr->cmd) {
4970 case NL80211_CMD_SET_PMKSA:
4971 rdev_ops = rdev->ops->set_pmksa;
4972 break;
4973 case NL80211_CMD_DEL_PMKSA:
4974 rdev_ops = rdev->ops->del_pmksa;
4975 break;
4976 default:
4977 WARN_ON(1);
4978 break;
4979 }
4980
Johannes Berg4c476992010-10-04 21:36:35 +02004981 if (!rdev_ops)
4982 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004983
Johannes Berg4c476992010-10-04 21:36:35 +02004984 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004985}
4986
4987static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4988{
Johannes Berg4c476992010-10-04 21:36:35 +02004989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4990 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004991
Johannes Berg074ac8d2010-09-16 14:58:22 +02004992 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004993 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4994 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 if (!rdev->ops->flush_pmksa)
4997 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004998
Johannes Berg4c476992010-10-04 21:36:35 +02004999 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005000}
5001
Arik Nemtsov109086c2011-09-28 14:12:50 +03005002static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5003{
5004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5005 struct net_device *dev = info->user_ptr[1];
5006 u8 action_code, dialog_token;
5007 u16 status_code;
5008 u8 *peer;
5009
5010 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5011 !rdev->ops->tdls_mgmt)
5012 return -EOPNOTSUPP;
5013
5014 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5015 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5016 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5017 !info->attrs[NL80211_ATTR_IE] ||
5018 !info->attrs[NL80211_ATTR_MAC])
5019 return -EINVAL;
5020
5021 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5022 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5023 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5024 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5025
5026 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5027 dialog_token, status_code,
5028 nla_data(info->attrs[NL80211_ATTR_IE]),
5029 nla_len(info->attrs[NL80211_ATTR_IE]));
5030}
5031
5032static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5033{
5034 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5035 struct net_device *dev = info->user_ptr[1];
5036 enum nl80211_tdls_operation operation;
5037 u8 *peer;
5038
5039 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5040 !rdev->ops->tdls_oper)
5041 return -EOPNOTSUPP;
5042
5043 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5044 !info->attrs[NL80211_ATTR_MAC])
5045 return -EINVAL;
5046
5047 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5048 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5049
5050 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5051}
5052
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005053static int nl80211_remain_on_channel(struct sk_buff *skb,
5054 struct genl_info *info)
5055{
Johannes Berg4c476992010-10-04 21:36:35 +02005056 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5057 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005058 struct ieee80211_channel *chan;
5059 struct sk_buff *msg;
5060 void *hdr;
5061 u64 cookie;
5062 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5063 u32 freq, duration;
5064 int err;
5065
5066 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5067 !info->attrs[NL80211_ATTR_DURATION])
5068 return -EINVAL;
5069
5070 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5071
5072 /*
5073 * We should be on that channel for at least one jiffie,
5074 * and more than 5 seconds seems excessive.
5075 */
Johannes Berga2939112010-12-14 17:54:28 +01005076 if (!duration || !msecs_to_jiffies(duration) ||
5077 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005078 return -EINVAL;
5079
Johannes Berg4c476992010-10-04 21:36:35 +02005080 if (!rdev->ops->remain_on_channel)
5081 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005082
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005083 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5084 channel_type = nla_get_u32(
5085 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5086 if (channel_type != NL80211_CHAN_NO_HT &&
5087 channel_type != NL80211_CHAN_HT20 &&
5088 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005089 channel_type != NL80211_CHAN_HT40MINUS)
5090 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005091 }
5092
5093 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5094 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005095 if (chan == NULL)
5096 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005097
5098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005099 if (!msg)
5100 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005101
5102 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5103 NL80211_CMD_REMAIN_ON_CHANNEL);
5104
5105 if (IS_ERR(hdr)) {
5106 err = PTR_ERR(hdr);
5107 goto free_msg;
5108 }
5109
5110 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5111 channel_type, duration, &cookie);
5112
5113 if (err)
5114 goto free_msg;
5115
5116 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5117
5118 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005119
5120 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005121
5122 nla_put_failure:
5123 err = -ENOBUFS;
5124 free_msg:
5125 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005126 return err;
5127}
5128
5129static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5130 struct genl_info *info)
5131{
Johannes Berg4c476992010-10-04 21:36:35 +02005132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5133 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005134 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005135
5136 if (!info->attrs[NL80211_ATTR_COOKIE])
5137 return -EINVAL;
5138
Johannes Berg4c476992010-10-04 21:36:35 +02005139 if (!rdev->ops->cancel_remain_on_channel)
5140 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005141
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005142 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5143
Johannes Berg4c476992010-10-04 21:36:35 +02005144 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005145}
5146
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005147static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5148 u8 *rates, u8 rates_len)
5149{
5150 u8 i;
5151 u32 mask = 0;
5152
5153 for (i = 0; i < rates_len; i++) {
5154 int rate = (rates[i] & 0x7f) * 5;
5155 int ridx;
5156 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5157 struct ieee80211_rate *srate =
5158 &sband->bitrates[ridx];
5159 if (rate == srate->bitrate) {
5160 mask |= 1 << ridx;
5161 break;
5162 }
5163 }
5164 if (ridx == sband->n_bitrates)
5165 return 0; /* rate not found */
5166 }
5167
5168 return mask;
5169}
5170
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005171static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005172 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5173 .len = NL80211_MAX_SUPP_RATES },
5174};
5175
5176static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5177 struct genl_info *info)
5178{
5179 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005181 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005182 int rem, i;
5183 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005184 struct nlattr *tx_rates;
5185 struct ieee80211_supported_band *sband;
5186
5187 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5188 return -EINVAL;
5189
Johannes Berg4c476992010-10-04 21:36:35 +02005190 if (!rdev->ops->set_bitrate_mask)
5191 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005192
5193 memset(&mask, 0, sizeof(mask));
5194 /* Default to all rates enabled */
5195 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5196 sband = rdev->wiphy.bands[i];
5197 mask.control[i].legacy =
5198 sband ? (1 << sband->n_bitrates) - 1 : 0;
5199 }
5200
5201 /*
5202 * The nested attribute uses enum nl80211_band as the index. This maps
5203 * directly to the enum ieee80211_band values used in cfg80211.
5204 */
5205 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5206 {
5207 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005208 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5209 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005210 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005211 if (sband == NULL)
5212 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005213 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5214 nla_len(tx_rates), nl80211_txattr_policy);
5215 if (tb[NL80211_TXRATE_LEGACY]) {
5216 mask.control[band].legacy = rateset_to_mask(
5217 sband,
5218 nla_data(tb[NL80211_TXRATE_LEGACY]),
5219 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005220 if (mask.control[band].legacy == 0)
5221 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005222 }
5223 }
5224
Johannes Berg4c476992010-10-04 21:36:35 +02005225 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005226}
5227
Johannes Berg2e161f72010-08-12 15:38:38 +02005228static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005229{
Johannes Berg4c476992010-10-04 21:36:35 +02005230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5231 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005232 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005233
5234 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5235 return -EINVAL;
5236
Johannes Berg2e161f72010-08-12 15:38:38 +02005237 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5238 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005239
Johannes Berg9d38d852010-06-09 17:20:33 +02005240 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005241 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005242 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5243 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5244 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005245 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005246 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5247 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005248
5249 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005250 if (!rdev->ops->mgmt_tx)
5251 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005252
Johannes Berg4c476992010-10-04 21:36:35 +02005253 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005254 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005255 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5256 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005257}
5258
Johannes Berg2e161f72010-08-12 15:38:38 +02005259static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005260{
Johannes Berg4c476992010-10-04 21:36:35 +02005261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5262 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005263 struct ieee80211_channel *chan;
5264 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005265 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005266 u32 freq;
5267 int err;
5268 void *hdr;
5269 u64 cookie;
5270 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005271 unsigned int wait = 0;
5272 bool offchan;
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305273 bool no_cck;
Jouni Malinen026331c2010-02-15 12:53:10 +02005274
5275 if (!info->attrs[NL80211_ATTR_FRAME] ||
5276 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5277 return -EINVAL;
5278
Johannes Berg4c476992010-10-04 21:36:35 +02005279 if (!rdev->ops->mgmt_tx)
5280 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005281
Johannes Berg9d38d852010-06-09 17:20:33 +02005282 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005283 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005284 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5285 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5286 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005287 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005288 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5289 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005290
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005291 if (info->attrs[NL80211_ATTR_DURATION]) {
5292 if (!rdev->ops->mgmt_tx_cancel_wait)
5293 return -EINVAL;
5294 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5295 }
5296
Jouni Malinen026331c2010-02-15 12:53:10 +02005297 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5298 channel_type = nla_get_u32(
5299 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5300 if (channel_type != NL80211_CHAN_NO_HT &&
5301 channel_type != NL80211_CHAN_HT20 &&
5302 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005303 channel_type != NL80211_CHAN_HT40MINUS)
5304 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005305 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005306 }
5307
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005308 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5309
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305310 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5311
Jouni Malinen026331c2010-02-15 12:53:10 +02005312 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5313 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005314 if (chan == NULL)
5315 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005316
5317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005318 if (!msg)
5319 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005320
5321 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005322 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005323
5324 if (IS_ERR(hdr)) {
5325 err = PTR_ERR(hdr);
5326 goto free_msg;
5327 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005328 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5329 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005330 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5331 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305332 no_cck, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005333 if (err)
5334 goto free_msg;
5335
5336 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5337
5338 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005339 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005340
5341 nla_put_failure:
5342 err = -ENOBUFS;
5343 free_msg:
5344 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005345 return err;
5346}
5347
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005348static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5349{
5350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5351 struct net_device *dev = info->user_ptr[1];
5352 u64 cookie;
5353
5354 if (!info->attrs[NL80211_ATTR_COOKIE])
5355 return -EINVAL;
5356
5357 if (!rdev->ops->mgmt_tx_cancel_wait)
5358 return -EOPNOTSUPP;
5359
5360 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5361 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5362 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5363 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5364 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5365 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5366 return -EOPNOTSUPP;
5367
5368 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5369
5370 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5371}
5372
Kalle Valoffb9eb32010-02-17 17:58:10 +02005373static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5374{
Johannes Berg4c476992010-10-04 21:36:35 +02005375 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005376 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005377 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005378 u8 ps_state;
5379 bool state;
5380 int err;
5381
Johannes Berg4c476992010-10-04 21:36:35 +02005382 if (!info->attrs[NL80211_ATTR_PS_STATE])
5383 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005384
5385 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5386
Johannes Berg4c476992010-10-04 21:36:35 +02005387 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5388 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005389
5390 wdev = dev->ieee80211_ptr;
5391
Johannes Berg4c476992010-10-04 21:36:35 +02005392 if (!rdev->ops->set_power_mgmt)
5393 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005394
5395 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5396
5397 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005398 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005399
Johannes Berg4c476992010-10-04 21:36:35 +02005400 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5401 wdev->ps_timeout);
5402 if (!err)
5403 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005404 return err;
5405}
5406
5407static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5408{
Johannes Berg4c476992010-10-04 21:36:35 +02005409 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005410 enum nl80211_ps_state ps_state;
5411 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005412 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005413 struct sk_buff *msg;
5414 void *hdr;
5415 int err;
5416
Kalle Valoffb9eb32010-02-17 17:58:10 +02005417 wdev = dev->ieee80211_ptr;
5418
Johannes Berg4c476992010-10-04 21:36:35 +02005419 if (!rdev->ops->set_power_mgmt)
5420 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005421
5422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005423 if (!msg)
5424 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005425
5426 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5427 NL80211_CMD_GET_POWER_SAVE);
5428 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005429 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005430 goto free_msg;
5431 }
5432
5433 if (wdev->ps)
5434 ps_state = NL80211_PS_ENABLED;
5435 else
5436 ps_state = NL80211_PS_DISABLED;
5437
5438 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5439
5440 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005441 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005442
Johannes Berg4c476992010-10-04 21:36:35 +02005443 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005444 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005445 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005446 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005447 return err;
5448}
5449
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005450static struct nla_policy
5451nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5452 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5453 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5454 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5455};
5456
5457static int nl80211_set_cqm_rssi(struct genl_info *info,
5458 s32 threshold, u32 hysteresis)
5459{
Johannes Berg4c476992010-10-04 21:36:35 +02005460 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005461 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005462 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005463
5464 if (threshold > 0)
5465 return -EINVAL;
5466
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005467 wdev = dev->ieee80211_ptr;
5468
Johannes Berg4c476992010-10-04 21:36:35 +02005469 if (!rdev->ops->set_cqm_rssi_config)
5470 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005471
Johannes Berg074ac8d2010-09-16 14:58:22 +02005472 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005473 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5474 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005475
Johannes Berg4c476992010-10-04 21:36:35 +02005476 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5477 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005478}
5479
5480static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5481{
5482 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5483 struct nlattr *cqm;
5484 int err;
5485
5486 cqm = info->attrs[NL80211_ATTR_CQM];
5487 if (!cqm) {
5488 err = -EINVAL;
5489 goto out;
5490 }
5491
5492 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5493 nl80211_attr_cqm_policy);
5494 if (err)
5495 goto out;
5496
5497 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5498 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5499 s32 threshold;
5500 u32 hysteresis;
5501 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5502 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5503 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5504 } else
5505 err = -EINVAL;
5506
5507out:
5508 return err;
5509}
5510
Johannes Berg29cbe682010-12-03 09:20:44 +01005511static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5512{
5513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5514 struct net_device *dev = info->user_ptr[1];
5515 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005516 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005517 int err;
5518
5519 /* start with default */
5520 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005521 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005522
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005523 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005524 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005525 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005526 if (err)
5527 return err;
5528 }
5529
5530 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5531 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5532 return -EINVAL;
5533
Javier Cardonac80d5452010-12-16 17:37:49 -08005534 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5535 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5536
5537 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5538 /* parse additional setup parameters if given */
5539 err = nl80211_parse_mesh_setup(info, &setup);
5540 if (err)
5541 return err;
5542 }
5543
5544 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005545}
5546
5547static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5548{
5549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5550 struct net_device *dev = info->user_ptr[1];
5551
5552 return cfg80211_leave_mesh(rdev, dev);
5553}
5554
Johannes Bergff1b6e62011-05-04 15:37:28 +02005555static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5556{
5557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5558 struct sk_buff *msg;
5559 void *hdr;
5560
5561 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5562 return -EOPNOTSUPP;
5563
5564 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5565 if (!msg)
5566 return -ENOMEM;
5567
5568 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5569 NL80211_CMD_GET_WOWLAN);
5570 if (!hdr)
5571 goto nla_put_failure;
5572
5573 if (rdev->wowlan) {
5574 struct nlattr *nl_wowlan;
5575
5576 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5577 if (!nl_wowlan)
5578 goto nla_put_failure;
5579
5580 if (rdev->wowlan->any)
5581 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5582 if (rdev->wowlan->disconnect)
5583 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5584 if (rdev->wowlan->magic_pkt)
5585 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005586 if (rdev->wowlan->gtk_rekey_failure)
5587 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5588 if (rdev->wowlan->eap_identity_req)
5589 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5590 if (rdev->wowlan->four_way_handshake)
5591 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5592 if (rdev->wowlan->rfkill_release)
5593 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005594 if (rdev->wowlan->n_patterns) {
5595 struct nlattr *nl_pats, *nl_pat;
5596 int i, pat_len;
5597
5598 nl_pats = nla_nest_start(msg,
5599 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5600 if (!nl_pats)
5601 goto nla_put_failure;
5602
5603 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5604 nl_pat = nla_nest_start(msg, i + 1);
5605 if (!nl_pat)
5606 goto nla_put_failure;
5607 pat_len = rdev->wowlan->patterns[i].pattern_len;
5608 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5609 DIV_ROUND_UP(pat_len, 8),
5610 rdev->wowlan->patterns[i].mask);
5611 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5612 pat_len,
5613 rdev->wowlan->patterns[i].pattern);
5614 nla_nest_end(msg, nl_pat);
5615 }
5616 nla_nest_end(msg, nl_pats);
5617 }
5618
5619 nla_nest_end(msg, nl_wowlan);
5620 }
5621
5622 genlmsg_end(msg, hdr);
5623 return genlmsg_reply(msg, info);
5624
5625nla_put_failure:
5626 nlmsg_free(msg);
5627 return -ENOBUFS;
5628}
5629
5630static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5631{
5632 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5633 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5634 struct cfg80211_wowlan no_triggers = {};
5635 struct cfg80211_wowlan new_triggers = {};
5636 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5637 int err, i;
5638
5639 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5640 return -EOPNOTSUPP;
5641
5642 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5643 goto no_triggers;
5644
5645 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5646 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5647 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5648 nl80211_wowlan_policy);
5649 if (err)
5650 return err;
5651
5652 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5653 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5654 return -EINVAL;
5655 new_triggers.any = true;
5656 }
5657
5658 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5659 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5660 return -EINVAL;
5661 new_triggers.disconnect = true;
5662 }
5663
5664 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5665 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5666 return -EINVAL;
5667 new_triggers.magic_pkt = true;
5668 }
5669
Johannes Berg77dbbb12011-07-13 10:48:55 +02005670 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5671 return -EINVAL;
5672
5673 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5674 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5675 return -EINVAL;
5676 new_triggers.gtk_rekey_failure = true;
5677 }
5678
5679 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5680 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5681 return -EINVAL;
5682 new_triggers.eap_identity_req = true;
5683 }
5684
5685 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5686 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5687 return -EINVAL;
5688 new_triggers.four_way_handshake = true;
5689 }
5690
5691 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5692 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5693 return -EINVAL;
5694 new_triggers.rfkill_release = true;
5695 }
5696
Johannes Bergff1b6e62011-05-04 15:37:28 +02005697 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5698 struct nlattr *pat;
5699 int n_patterns = 0;
5700 int rem, pat_len, mask_len;
5701 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5702
5703 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5704 rem)
5705 n_patterns++;
5706 if (n_patterns > wowlan->n_patterns)
5707 return -EINVAL;
5708
5709 new_triggers.patterns = kcalloc(n_patterns,
5710 sizeof(new_triggers.patterns[0]),
5711 GFP_KERNEL);
5712 if (!new_triggers.patterns)
5713 return -ENOMEM;
5714
5715 new_triggers.n_patterns = n_patterns;
5716 i = 0;
5717
5718 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5719 rem) {
5720 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5721 nla_data(pat), nla_len(pat), NULL);
5722 err = -EINVAL;
5723 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5724 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5725 goto error;
5726 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5727 mask_len = DIV_ROUND_UP(pat_len, 8);
5728 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5729 mask_len)
5730 goto error;
5731 if (pat_len > wowlan->pattern_max_len ||
5732 pat_len < wowlan->pattern_min_len)
5733 goto error;
5734
5735 new_triggers.patterns[i].mask =
5736 kmalloc(mask_len + pat_len, GFP_KERNEL);
5737 if (!new_triggers.patterns[i].mask) {
5738 err = -ENOMEM;
5739 goto error;
5740 }
5741 new_triggers.patterns[i].pattern =
5742 new_triggers.patterns[i].mask + mask_len;
5743 memcpy(new_triggers.patterns[i].mask,
5744 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5745 mask_len);
5746 new_triggers.patterns[i].pattern_len = pat_len;
5747 memcpy(new_triggers.patterns[i].pattern,
5748 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5749 pat_len);
5750 i++;
5751 }
5752 }
5753
5754 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5755 struct cfg80211_wowlan *ntrig;
5756 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5757 GFP_KERNEL);
5758 if (!ntrig) {
5759 err = -ENOMEM;
5760 goto error;
5761 }
5762 cfg80211_rdev_free_wowlan(rdev);
5763 rdev->wowlan = ntrig;
5764 } else {
5765 no_triggers:
5766 cfg80211_rdev_free_wowlan(rdev);
5767 rdev->wowlan = NULL;
5768 }
5769
5770 return 0;
5771 error:
5772 for (i = 0; i < new_triggers.n_patterns; i++)
5773 kfree(new_triggers.patterns[i].mask);
5774 kfree(new_triggers.patterns);
5775 return err;
5776}
5777
Johannes Berge5497d72011-07-05 16:35:40 +02005778static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5779{
5780 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5781 struct net_device *dev = info->user_ptr[1];
5782 struct wireless_dev *wdev = dev->ieee80211_ptr;
5783 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5784 struct cfg80211_gtk_rekey_data rekey_data;
5785 int err;
5786
5787 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5788 return -EINVAL;
5789
5790 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5791 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5792 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5793 nl80211_rekey_policy);
5794 if (err)
5795 return err;
5796
5797 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5798 return -ERANGE;
5799 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5800 return -ERANGE;
5801 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5802 return -ERANGE;
5803
5804 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5805 NL80211_KEK_LEN);
5806 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5807 NL80211_KCK_LEN);
5808 memcpy(rekey_data.replay_ctr,
5809 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5810 NL80211_REPLAY_CTR_LEN);
5811
5812 wdev_lock(wdev);
5813 if (!wdev->current_bss) {
5814 err = -ENOTCONN;
5815 goto out;
5816 }
5817
5818 if (!rdev->ops->set_rekey_data) {
5819 err = -EOPNOTSUPP;
5820 goto out;
5821 }
5822
5823 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5824 out:
5825 wdev_unlock(wdev);
5826 return err;
5827}
5828
Johannes Berg4c476992010-10-04 21:36:35 +02005829#define NL80211_FLAG_NEED_WIPHY 0x01
5830#define NL80211_FLAG_NEED_NETDEV 0x02
5831#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005832#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5833#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5834 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005835
5836static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5837 struct genl_info *info)
5838{
5839 struct cfg80211_registered_device *rdev;
5840 struct net_device *dev;
5841 int err;
5842 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5843
5844 if (rtnl)
5845 rtnl_lock();
5846
5847 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5848 rdev = cfg80211_get_dev_from_info(info);
5849 if (IS_ERR(rdev)) {
5850 if (rtnl)
5851 rtnl_unlock();
5852 return PTR_ERR(rdev);
5853 }
5854 info->user_ptr[0] = rdev;
5855 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5856 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5857 if (err) {
5858 if (rtnl)
5859 rtnl_unlock();
5860 return err;
5861 }
Johannes Berg41265712010-10-04 21:14:05 +02005862 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5863 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005864 cfg80211_unlock_rdev(rdev);
5865 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005866 if (rtnl)
5867 rtnl_unlock();
5868 return -ENETDOWN;
5869 }
Johannes Berg4c476992010-10-04 21:36:35 +02005870 info->user_ptr[0] = rdev;
5871 info->user_ptr[1] = dev;
5872 }
5873
5874 return 0;
5875}
5876
5877static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5878 struct genl_info *info)
5879{
5880 if (info->user_ptr[0])
5881 cfg80211_unlock_rdev(info->user_ptr[0]);
5882 if (info->user_ptr[1])
5883 dev_put(info->user_ptr[1]);
5884 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5885 rtnl_unlock();
5886}
5887
Johannes Berg55682962007-09-20 13:09:35 -04005888static struct genl_ops nl80211_ops[] = {
5889 {
5890 .cmd = NL80211_CMD_GET_WIPHY,
5891 .doit = nl80211_get_wiphy,
5892 .dumpit = nl80211_dump_wiphy,
5893 .policy = nl80211_policy,
5894 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005895 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005896 },
5897 {
5898 .cmd = NL80211_CMD_SET_WIPHY,
5899 .doit = nl80211_set_wiphy,
5900 .policy = nl80211_policy,
5901 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005902 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005903 },
5904 {
5905 .cmd = NL80211_CMD_GET_INTERFACE,
5906 .doit = nl80211_get_interface,
5907 .dumpit = nl80211_dump_interface,
5908 .policy = nl80211_policy,
5909 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005910 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005911 },
5912 {
5913 .cmd = NL80211_CMD_SET_INTERFACE,
5914 .doit = nl80211_set_interface,
5915 .policy = nl80211_policy,
5916 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005917 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5918 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005919 },
5920 {
5921 .cmd = NL80211_CMD_NEW_INTERFACE,
5922 .doit = nl80211_new_interface,
5923 .policy = nl80211_policy,
5924 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005925 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5926 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005927 },
5928 {
5929 .cmd = NL80211_CMD_DEL_INTERFACE,
5930 .doit = nl80211_del_interface,
5931 .policy = nl80211_policy,
5932 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005933 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5934 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005935 },
Johannes Berg41ade002007-12-19 02:03:29 +01005936 {
5937 .cmd = NL80211_CMD_GET_KEY,
5938 .doit = nl80211_get_key,
5939 .policy = nl80211_policy,
5940 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005941 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5942 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005943 },
5944 {
5945 .cmd = NL80211_CMD_SET_KEY,
5946 .doit = nl80211_set_key,
5947 .policy = nl80211_policy,
5948 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005949 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005950 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005951 },
5952 {
5953 .cmd = NL80211_CMD_NEW_KEY,
5954 .doit = nl80211_new_key,
5955 .policy = nl80211_policy,
5956 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005957 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005958 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005959 },
5960 {
5961 .cmd = NL80211_CMD_DEL_KEY,
5962 .doit = nl80211_del_key,
5963 .policy = nl80211_policy,
5964 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005965 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005966 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005967 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005968 {
5969 .cmd = NL80211_CMD_SET_BEACON,
5970 .policy = nl80211_policy,
5971 .flags = GENL_ADMIN_PERM,
5972 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005973 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5974 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005975 },
5976 {
5977 .cmd = NL80211_CMD_NEW_BEACON,
5978 .policy = nl80211_policy,
5979 .flags = GENL_ADMIN_PERM,
5980 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005981 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5982 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005983 },
5984 {
5985 .cmd = NL80211_CMD_DEL_BEACON,
5986 .policy = nl80211_policy,
5987 .flags = GENL_ADMIN_PERM,
5988 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005989 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5990 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005991 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005992 {
5993 .cmd = NL80211_CMD_GET_STATION,
5994 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005995 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005996 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005997 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5998 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005999 },
6000 {
6001 .cmd = NL80211_CMD_SET_STATION,
6002 .doit = nl80211_set_station,
6003 .policy = nl80211_policy,
6004 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006005 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6006 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006007 },
6008 {
6009 .cmd = NL80211_CMD_NEW_STATION,
6010 .doit = nl80211_new_station,
6011 .policy = nl80211_policy,
6012 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006014 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006015 },
6016 {
6017 .cmd = NL80211_CMD_DEL_STATION,
6018 .doit = nl80211_del_station,
6019 .policy = nl80211_policy,
6020 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006021 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6022 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006023 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006024 {
6025 .cmd = NL80211_CMD_GET_MPATH,
6026 .doit = nl80211_get_mpath,
6027 .dumpit = nl80211_dump_mpath,
6028 .policy = nl80211_policy,
6029 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006030 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006031 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006032 },
6033 {
6034 .cmd = NL80211_CMD_SET_MPATH,
6035 .doit = nl80211_set_mpath,
6036 .policy = nl80211_policy,
6037 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006038 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006039 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006040 },
6041 {
6042 .cmd = NL80211_CMD_NEW_MPATH,
6043 .doit = nl80211_new_mpath,
6044 .policy = nl80211_policy,
6045 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006046 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006047 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006048 },
6049 {
6050 .cmd = NL80211_CMD_DEL_MPATH,
6051 .doit = nl80211_del_mpath,
6052 .policy = nl80211_policy,
6053 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006054 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6055 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006056 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006057 {
6058 .cmd = NL80211_CMD_SET_BSS,
6059 .doit = nl80211_set_bss,
6060 .policy = nl80211_policy,
6061 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006062 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6063 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006064 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006065 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006066 .cmd = NL80211_CMD_GET_REG,
6067 .doit = nl80211_get_reg,
6068 .policy = nl80211_policy,
6069 /* can be retrieved by unprivileged users */
6070 },
6071 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006072 .cmd = NL80211_CMD_SET_REG,
6073 .doit = nl80211_set_reg,
6074 .policy = nl80211_policy,
6075 .flags = GENL_ADMIN_PERM,
6076 },
6077 {
6078 .cmd = NL80211_CMD_REQ_SET_REG,
6079 .doit = nl80211_req_set_reg,
6080 .policy = nl80211_policy,
6081 .flags = GENL_ADMIN_PERM,
6082 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006083 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006084 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6085 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006086 .policy = nl80211_policy,
6087 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006088 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6089 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006090 },
6091 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006092 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6093 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006094 .policy = nl80211_policy,
6095 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006096 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006097 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006098 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006099 {
Johannes Berg2a519312009-02-10 21:25:55 +01006100 .cmd = NL80211_CMD_TRIGGER_SCAN,
6101 .doit = nl80211_trigger_scan,
6102 .policy = nl80211_policy,
6103 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006104 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006105 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006106 },
6107 {
6108 .cmd = NL80211_CMD_GET_SCAN,
6109 .policy = nl80211_policy,
6110 .dumpit = nl80211_dump_scan,
6111 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006112 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006113 .cmd = NL80211_CMD_START_SCHED_SCAN,
6114 .doit = nl80211_start_sched_scan,
6115 .policy = nl80211_policy,
6116 .flags = GENL_ADMIN_PERM,
6117 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6118 NL80211_FLAG_NEED_RTNL,
6119 },
6120 {
6121 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6122 .doit = nl80211_stop_sched_scan,
6123 .policy = nl80211_policy,
6124 .flags = GENL_ADMIN_PERM,
6125 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6126 NL80211_FLAG_NEED_RTNL,
6127 },
6128 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006129 .cmd = NL80211_CMD_AUTHENTICATE,
6130 .doit = nl80211_authenticate,
6131 .policy = nl80211_policy,
6132 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006133 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006134 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006135 },
6136 {
6137 .cmd = NL80211_CMD_ASSOCIATE,
6138 .doit = nl80211_associate,
6139 .policy = nl80211_policy,
6140 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006141 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006142 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006143 },
6144 {
6145 .cmd = NL80211_CMD_DEAUTHENTICATE,
6146 .doit = nl80211_deauthenticate,
6147 .policy = nl80211_policy,
6148 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006149 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006150 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006151 },
6152 {
6153 .cmd = NL80211_CMD_DISASSOCIATE,
6154 .doit = nl80211_disassociate,
6155 .policy = nl80211_policy,
6156 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006157 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006158 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006159 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006160 {
6161 .cmd = NL80211_CMD_JOIN_IBSS,
6162 .doit = nl80211_join_ibss,
6163 .policy = nl80211_policy,
6164 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006165 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006166 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006167 },
6168 {
6169 .cmd = NL80211_CMD_LEAVE_IBSS,
6170 .doit = nl80211_leave_ibss,
6171 .policy = nl80211_policy,
6172 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006173 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006174 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006175 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006176#ifdef CONFIG_NL80211_TESTMODE
6177 {
6178 .cmd = NL80211_CMD_TESTMODE,
6179 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006180 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006181 .policy = nl80211_policy,
6182 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006183 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6184 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006185 },
6186#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006187 {
6188 .cmd = NL80211_CMD_CONNECT,
6189 .doit = nl80211_connect,
6190 .policy = nl80211_policy,
6191 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006192 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006193 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006194 },
6195 {
6196 .cmd = NL80211_CMD_DISCONNECT,
6197 .doit = nl80211_disconnect,
6198 .policy = nl80211_policy,
6199 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006200 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006201 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006202 },
Johannes Berg463d0182009-07-14 00:33:35 +02006203 {
6204 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6205 .doit = nl80211_wiphy_netns,
6206 .policy = nl80211_policy,
6207 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006208 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6209 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006210 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006211 {
6212 .cmd = NL80211_CMD_GET_SURVEY,
6213 .policy = nl80211_policy,
6214 .dumpit = nl80211_dump_survey,
6215 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006216 {
6217 .cmd = NL80211_CMD_SET_PMKSA,
6218 .doit = nl80211_setdel_pmksa,
6219 .policy = nl80211_policy,
6220 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006221 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6222 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006223 },
6224 {
6225 .cmd = NL80211_CMD_DEL_PMKSA,
6226 .doit = nl80211_setdel_pmksa,
6227 .policy = nl80211_policy,
6228 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006229 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6230 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006231 },
6232 {
6233 .cmd = NL80211_CMD_FLUSH_PMKSA,
6234 .doit = nl80211_flush_pmksa,
6235 .policy = nl80211_policy,
6236 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006237 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6238 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006239 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006240 {
6241 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6242 .doit = nl80211_remain_on_channel,
6243 .policy = nl80211_policy,
6244 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006245 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006246 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006247 },
6248 {
6249 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6250 .doit = nl80211_cancel_remain_on_channel,
6251 .policy = nl80211_policy,
6252 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006253 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006254 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006255 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006256 {
6257 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6258 .doit = nl80211_set_tx_bitrate_mask,
6259 .policy = nl80211_policy,
6260 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006261 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6262 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006263 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006264 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006265 .cmd = NL80211_CMD_REGISTER_FRAME,
6266 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006267 .policy = nl80211_policy,
6268 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006269 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6270 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006271 },
6272 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006273 .cmd = NL80211_CMD_FRAME,
6274 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006275 .policy = nl80211_policy,
6276 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006277 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006278 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006279 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006280 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006281 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6282 .doit = nl80211_tx_mgmt_cancel_wait,
6283 .policy = nl80211_policy,
6284 .flags = GENL_ADMIN_PERM,
6285 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6286 NL80211_FLAG_NEED_RTNL,
6287 },
6288 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006289 .cmd = NL80211_CMD_SET_POWER_SAVE,
6290 .doit = nl80211_set_power_save,
6291 .policy = nl80211_policy,
6292 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006293 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6294 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006295 },
6296 {
6297 .cmd = NL80211_CMD_GET_POWER_SAVE,
6298 .doit = nl80211_get_power_save,
6299 .policy = nl80211_policy,
6300 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006301 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6302 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006303 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006304 {
6305 .cmd = NL80211_CMD_SET_CQM,
6306 .doit = nl80211_set_cqm,
6307 .policy = nl80211_policy,
6308 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006309 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6310 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006311 },
Johannes Bergf444de02010-05-05 15:25:02 +02006312 {
6313 .cmd = NL80211_CMD_SET_CHANNEL,
6314 .doit = nl80211_set_channel,
6315 .policy = nl80211_policy,
6316 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006317 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6318 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006319 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006320 {
6321 .cmd = NL80211_CMD_SET_WDS_PEER,
6322 .doit = nl80211_set_wds_peer,
6323 .policy = nl80211_policy,
6324 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006325 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6326 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006327 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006328 {
6329 .cmd = NL80211_CMD_JOIN_MESH,
6330 .doit = nl80211_join_mesh,
6331 .policy = nl80211_policy,
6332 .flags = GENL_ADMIN_PERM,
6333 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6334 NL80211_FLAG_NEED_RTNL,
6335 },
6336 {
6337 .cmd = NL80211_CMD_LEAVE_MESH,
6338 .doit = nl80211_leave_mesh,
6339 .policy = nl80211_policy,
6340 .flags = GENL_ADMIN_PERM,
6341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6342 NL80211_FLAG_NEED_RTNL,
6343 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006344 {
6345 .cmd = NL80211_CMD_GET_WOWLAN,
6346 .doit = nl80211_get_wowlan,
6347 .policy = nl80211_policy,
6348 /* can be retrieved by unprivileged users */
6349 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6350 NL80211_FLAG_NEED_RTNL,
6351 },
6352 {
6353 .cmd = NL80211_CMD_SET_WOWLAN,
6354 .doit = nl80211_set_wowlan,
6355 .policy = nl80211_policy,
6356 .flags = GENL_ADMIN_PERM,
6357 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6358 NL80211_FLAG_NEED_RTNL,
6359 },
Johannes Berge5497d72011-07-05 16:35:40 +02006360 {
6361 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6362 .doit = nl80211_set_rekey_data,
6363 .policy = nl80211_policy,
6364 .flags = GENL_ADMIN_PERM,
6365 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6366 NL80211_FLAG_NEED_RTNL,
6367 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006368 {
6369 .cmd = NL80211_CMD_TDLS_MGMT,
6370 .doit = nl80211_tdls_mgmt,
6371 .policy = nl80211_policy,
6372 .flags = GENL_ADMIN_PERM,
6373 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6374 NL80211_FLAG_NEED_RTNL,
6375 },
6376 {
6377 .cmd = NL80211_CMD_TDLS_OPER,
6378 .doit = nl80211_tdls_oper,
6379 .policy = nl80211_policy,
6380 .flags = GENL_ADMIN_PERM,
6381 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6382 NL80211_FLAG_NEED_RTNL,
6383 },
Johannes Berg55682962007-09-20 13:09:35 -04006384};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006385
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006386static struct genl_multicast_group nl80211_mlme_mcgrp = {
6387 .name = "mlme",
6388};
Johannes Berg55682962007-09-20 13:09:35 -04006389
6390/* multicast groups */
6391static struct genl_multicast_group nl80211_config_mcgrp = {
6392 .name = "config",
6393};
Johannes Berg2a519312009-02-10 21:25:55 +01006394static struct genl_multicast_group nl80211_scan_mcgrp = {
6395 .name = "scan",
6396};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006397static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6398 .name = "regulatory",
6399};
Johannes Berg55682962007-09-20 13:09:35 -04006400
6401/* notification functions */
6402
6403void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6404{
6405 struct sk_buff *msg;
6406
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006407 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006408 if (!msg)
6409 return;
6410
6411 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6412 nlmsg_free(msg);
6413 return;
6414 }
6415
Johannes Berg463d0182009-07-14 00:33:35 +02006416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6417 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006418}
6419
Johannes Berg362a4152009-05-24 16:43:15 +02006420static int nl80211_add_scan_req(struct sk_buff *msg,
6421 struct cfg80211_registered_device *rdev)
6422{
6423 struct cfg80211_scan_request *req = rdev->scan_req;
6424 struct nlattr *nest;
6425 int i;
6426
Johannes Berg667503dd2009-07-07 03:56:11 +02006427 ASSERT_RDEV_LOCK(rdev);
6428
Johannes Berg362a4152009-05-24 16:43:15 +02006429 if (WARN_ON(!req))
6430 return 0;
6431
6432 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6433 if (!nest)
6434 goto nla_put_failure;
6435 for (i = 0; i < req->n_ssids; i++)
6436 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6437 nla_nest_end(msg, nest);
6438
6439 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6440 if (!nest)
6441 goto nla_put_failure;
6442 for (i = 0; i < req->n_channels; i++)
6443 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6444 nla_nest_end(msg, nest);
6445
6446 if (req->ie)
6447 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6448
6449 return 0;
6450 nla_put_failure:
6451 return -ENOBUFS;
6452}
6453
Johannes Berga538e2d2009-06-16 19:56:42 +02006454static int nl80211_send_scan_msg(struct sk_buff *msg,
6455 struct cfg80211_registered_device *rdev,
6456 struct net_device *netdev,
6457 u32 pid, u32 seq, int flags,
6458 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006459{
6460 void *hdr;
6461
6462 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6463 if (!hdr)
6464 return -1;
6465
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006466 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006467 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6468
Johannes Berg362a4152009-05-24 16:43:15 +02006469 /* ignore errors and send incomplete event anyway */
6470 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006471
6472 return genlmsg_end(msg, hdr);
6473
6474 nla_put_failure:
6475 genlmsg_cancel(msg, hdr);
6476 return -EMSGSIZE;
6477}
6478
Luciano Coelho807f8a82011-05-11 17:09:35 +03006479static int
6480nl80211_send_sched_scan_msg(struct sk_buff *msg,
6481 struct cfg80211_registered_device *rdev,
6482 struct net_device *netdev,
6483 u32 pid, u32 seq, int flags, u32 cmd)
6484{
6485 void *hdr;
6486
6487 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6488 if (!hdr)
6489 return -1;
6490
6491 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6492 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6493
6494 return genlmsg_end(msg, hdr);
6495
6496 nla_put_failure:
6497 genlmsg_cancel(msg, hdr);
6498 return -EMSGSIZE;
6499}
6500
Johannes Berga538e2d2009-06-16 19:56:42 +02006501void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6502 struct net_device *netdev)
6503{
6504 struct sk_buff *msg;
6505
6506 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6507 if (!msg)
6508 return;
6509
6510 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6511 NL80211_CMD_TRIGGER_SCAN) < 0) {
6512 nlmsg_free(msg);
6513 return;
6514 }
6515
Johannes Berg463d0182009-07-14 00:33:35 +02006516 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6517 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006518}
6519
Johannes Berg2a519312009-02-10 21:25:55 +01006520void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6521 struct net_device *netdev)
6522{
6523 struct sk_buff *msg;
6524
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006525 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006526 if (!msg)
6527 return;
6528
Johannes Berga538e2d2009-06-16 19:56:42 +02006529 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6530 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006531 nlmsg_free(msg);
6532 return;
6533 }
6534
Johannes Berg463d0182009-07-14 00:33:35 +02006535 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6536 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006537}
6538
6539void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6540 struct net_device *netdev)
6541{
6542 struct sk_buff *msg;
6543
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006544 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006545 if (!msg)
6546 return;
6547
Johannes Berga538e2d2009-06-16 19:56:42 +02006548 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6549 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006550 nlmsg_free(msg);
6551 return;
6552 }
6553
Johannes Berg463d0182009-07-14 00:33:35 +02006554 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6555 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006556}
6557
Luciano Coelho807f8a82011-05-11 17:09:35 +03006558void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6559 struct net_device *netdev)
6560{
6561 struct sk_buff *msg;
6562
6563 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6564 if (!msg)
6565 return;
6566
6567 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6568 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6569 nlmsg_free(msg);
6570 return;
6571 }
6572
6573 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6574 nl80211_scan_mcgrp.id, GFP_KERNEL);
6575}
6576
6577void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6578 struct net_device *netdev, u32 cmd)
6579{
6580 struct sk_buff *msg;
6581
6582 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6583 if (!msg)
6584 return;
6585
6586 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6587 nlmsg_free(msg);
6588 return;
6589 }
6590
6591 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6592 nl80211_scan_mcgrp.id, GFP_KERNEL);
6593}
6594
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006595/*
6596 * This can happen on global regulatory changes or device specific settings
6597 * based on custom world regulatory domains.
6598 */
6599void nl80211_send_reg_change_event(struct regulatory_request *request)
6600{
6601 struct sk_buff *msg;
6602 void *hdr;
6603
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006604 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006605 if (!msg)
6606 return;
6607
6608 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6609 if (!hdr) {
6610 nlmsg_free(msg);
6611 return;
6612 }
6613
6614 /* Userspace can always count this one always being set */
6615 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6616
6617 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6618 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6619 NL80211_REGDOM_TYPE_WORLD);
6620 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6621 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6622 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6623 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6624 request->intersect)
6625 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6626 NL80211_REGDOM_TYPE_INTERSECTION);
6627 else {
6628 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6629 NL80211_REGDOM_TYPE_COUNTRY);
6630 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6631 }
6632
6633 if (wiphy_idx_valid(request->wiphy_idx))
6634 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6635
6636 if (genlmsg_end(msg, hdr) < 0) {
6637 nlmsg_free(msg);
6638 return;
6639 }
6640
Johannes Bergbc43b282009-07-25 10:54:13 +02006641 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006642 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006643 GFP_ATOMIC);
6644 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006645
6646 return;
6647
6648nla_put_failure:
6649 genlmsg_cancel(msg, hdr);
6650 nlmsg_free(msg);
6651}
6652
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006653static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6654 struct net_device *netdev,
6655 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006656 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006657{
6658 struct sk_buff *msg;
6659 void *hdr;
6660
Johannes Berge6d6e342009-07-01 21:26:47 +02006661 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006662 if (!msg)
6663 return;
6664
6665 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6666 if (!hdr) {
6667 nlmsg_free(msg);
6668 return;
6669 }
6670
6671 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6672 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6673 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6674
6675 if (genlmsg_end(msg, hdr) < 0) {
6676 nlmsg_free(msg);
6677 return;
6678 }
6679
Johannes Berg463d0182009-07-14 00:33:35 +02006680 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6681 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006682 return;
6683
6684 nla_put_failure:
6685 genlmsg_cancel(msg, hdr);
6686 nlmsg_free(msg);
6687}
6688
6689void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006690 struct net_device *netdev, const u8 *buf,
6691 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006692{
6693 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006694 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006695}
6696
6697void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6698 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006699 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006700{
Johannes Berge6d6e342009-07-01 21:26:47 +02006701 nl80211_send_mlme_event(rdev, netdev, buf, len,
6702 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006703}
6704
Jouni Malinen53b46b82009-03-27 20:53:56 +02006705void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006706 struct net_device *netdev, const u8 *buf,
6707 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006708{
6709 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006710 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006711}
6712
Jouni Malinen53b46b82009-03-27 20:53:56 +02006713void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6714 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006715 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006716{
6717 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006718 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006719}
6720
Jouni Malinencf4e5942010-12-16 00:52:40 +02006721void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6722 struct net_device *netdev, const u8 *buf,
6723 size_t len, gfp_t gfp)
6724{
6725 nl80211_send_mlme_event(rdev, netdev, buf, len,
6726 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6727}
6728
6729void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6730 struct net_device *netdev, const u8 *buf,
6731 size_t len, gfp_t gfp)
6732{
6733 nl80211_send_mlme_event(rdev, netdev, buf, len,
6734 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6735}
6736
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006737static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6738 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006739 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006740{
6741 struct sk_buff *msg;
6742 void *hdr;
6743
Johannes Berge6d6e342009-07-01 21:26:47 +02006744 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006745 if (!msg)
6746 return;
6747
6748 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6749 if (!hdr) {
6750 nlmsg_free(msg);
6751 return;
6752 }
6753
6754 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6755 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6756 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6757 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6758
6759 if (genlmsg_end(msg, hdr) < 0) {
6760 nlmsg_free(msg);
6761 return;
6762 }
6763
Johannes Berg463d0182009-07-14 00:33:35 +02006764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6765 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006766 return;
6767
6768 nla_put_failure:
6769 genlmsg_cancel(msg, hdr);
6770 nlmsg_free(msg);
6771}
6772
6773void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006774 struct net_device *netdev, const u8 *addr,
6775 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006776{
6777 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006778 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006779}
6780
6781void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006782 struct net_device *netdev, const u8 *addr,
6783 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006784{
Johannes Berge6d6e342009-07-01 21:26:47 +02006785 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6786 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006787}
6788
Samuel Ortizb23aa672009-07-01 21:26:54 +02006789void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6790 struct net_device *netdev, const u8 *bssid,
6791 const u8 *req_ie, size_t req_ie_len,
6792 const u8 *resp_ie, size_t resp_ie_len,
6793 u16 status, gfp_t gfp)
6794{
6795 struct sk_buff *msg;
6796 void *hdr;
6797
6798 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6799 if (!msg)
6800 return;
6801
6802 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6803 if (!hdr) {
6804 nlmsg_free(msg);
6805 return;
6806 }
6807
6808 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6809 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6810 if (bssid)
6811 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6812 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6813 if (req_ie)
6814 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6815 if (resp_ie)
6816 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6817
6818 if (genlmsg_end(msg, hdr) < 0) {
6819 nlmsg_free(msg);
6820 return;
6821 }
6822
Johannes Berg463d0182009-07-14 00:33:35 +02006823 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6824 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006825 return;
6826
6827 nla_put_failure:
6828 genlmsg_cancel(msg, hdr);
6829 nlmsg_free(msg);
6830
6831}
6832
6833void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6834 struct net_device *netdev, const u8 *bssid,
6835 const u8 *req_ie, size_t req_ie_len,
6836 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6837{
6838 struct sk_buff *msg;
6839 void *hdr;
6840
6841 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6842 if (!msg)
6843 return;
6844
6845 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6846 if (!hdr) {
6847 nlmsg_free(msg);
6848 return;
6849 }
6850
6851 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6852 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6853 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6854 if (req_ie)
6855 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6856 if (resp_ie)
6857 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6858
6859 if (genlmsg_end(msg, hdr) < 0) {
6860 nlmsg_free(msg);
6861 return;
6862 }
6863
Johannes Berg463d0182009-07-14 00:33:35 +02006864 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6865 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006866 return;
6867
6868 nla_put_failure:
6869 genlmsg_cancel(msg, hdr);
6870 nlmsg_free(msg);
6871
6872}
6873
6874void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6875 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02006876 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006877{
6878 struct sk_buff *msg;
6879 void *hdr;
6880
Johannes Berg667503dd2009-07-07 03:56:11 +02006881 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006882 if (!msg)
6883 return;
6884
6885 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6886 if (!hdr) {
6887 nlmsg_free(msg);
6888 return;
6889 }
6890
6891 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6892 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6893 if (from_ap && reason)
6894 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6895 if (from_ap)
6896 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6897 if (ie)
6898 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6899
6900 if (genlmsg_end(msg, hdr) < 0) {
6901 nlmsg_free(msg);
6902 return;
6903 }
6904
Johannes Berg463d0182009-07-14 00:33:35 +02006905 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6906 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006907 return;
6908
6909 nla_put_failure:
6910 genlmsg_cancel(msg, hdr);
6911 nlmsg_free(msg);
6912
6913}
6914
Johannes Berg04a773a2009-04-19 21:24:32 +02006915void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6916 struct net_device *netdev, const u8 *bssid,
6917 gfp_t gfp)
6918{
6919 struct sk_buff *msg;
6920 void *hdr;
6921
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006923 if (!msg)
6924 return;
6925
6926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6927 if (!hdr) {
6928 nlmsg_free(msg);
6929 return;
6930 }
6931
6932 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6933 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6934 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6935
6936 if (genlmsg_end(msg, hdr) < 0) {
6937 nlmsg_free(msg);
6938 return;
6939 }
6940
Johannes Berg463d0182009-07-14 00:33:35 +02006941 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6942 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006943 return;
6944
6945 nla_put_failure:
6946 genlmsg_cancel(msg, hdr);
6947 nlmsg_free(msg);
6948}
6949
Javier Cardonac93b5e72011-04-07 15:08:34 -07006950void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6951 struct net_device *netdev,
6952 const u8 *macaddr, const u8* ie, u8 ie_len,
6953 gfp_t gfp)
6954{
6955 struct sk_buff *msg;
6956 void *hdr;
6957
6958 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6959 if (!msg)
6960 return;
6961
6962 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6963 if (!hdr) {
6964 nlmsg_free(msg);
6965 return;
6966 }
6967
6968 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6969 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6970 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6971 if (ie_len && ie)
6972 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6973
6974 if (genlmsg_end(msg, hdr) < 0) {
6975 nlmsg_free(msg);
6976 return;
6977 }
6978
6979 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6980 nl80211_mlme_mcgrp.id, gfp);
6981 return;
6982
6983 nla_put_failure:
6984 genlmsg_cancel(msg, hdr);
6985 nlmsg_free(msg);
6986}
6987
Jouni Malinena3b8b052009-03-27 21:59:49 +02006988void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6989 struct net_device *netdev, const u8 *addr,
6990 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006991 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006992{
6993 struct sk_buff *msg;
6994 void *hdr;
6995
Johannes Berge6d6e342009-07-01 21:26:47 +02006996 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006997 if (!msg)
6998 return;
6999
7000 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7001 if (!hdr) {
7002 nlmsg_free(msg);
7003 return;
7004 }
7005
7006 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7007 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7008 if (addr)
7009 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7010 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007011 if (key_id != -1)
7012 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007013 if (tsc)
7014 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7015
7016 if (genlmsg_end(msg, hdr) < 0) {
7017 nlmsg_free(msg);
7018 return;
7019 }
7020
Johannes Berg463d0182009-07-14 00:33:35 +02007021 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7022 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007023 return;
7024
7025 nla_put_failure:
7026 genlmsg_cancel(msg, hdr);
7027 nlmsg_free(msg);
7028}
7029
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007030void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7031 struct ieee80211_channel *channel_before,
7032 struct ieee80211_channel *channel_after)
7033{
7034 struct sk_buff *msg;
7035 void *hdr;
7036 struct nlattr *nl_freq;
7037
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007038 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007039 if (!msg)
7040 return;
7041
7042 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7043 if (!hdr) {
7044 nlmsg_free(msg);
7045 return;
7046 }
7047
7048 /*
7049 * Since we are applying the beacon hint to a wiphy we know its
7050 * wiphy_idx is valid
7051 */
7052 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7053
7054 /* Before */
7055 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7056 if (!nl_freq)
7057 goto nla_put_failure;
7058 if (nl80211_msg_put_channel(msg, channel_before))
7059 goto nla_put_failure;
7060 nla_nest_end(msg, nl_freq);
7061
7062 /* After */
7063 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7064 if (!nl_freq)
7065 goto nla_put_failure;
7066 if (nl80211_msg_put_channel(msg, channel_after))
7067 goto nla_put_failure;
7068 nla_nest_end(msg, nl_freq);
7069
7070 if (genlmsg_end(msg, hdr) < 0) {
7071 nlmsg_free(msg);
7072 return;
7073 }
7074
Johannes Berg463d0182009-07-14 00:33:35 +02007075 rcu_read_lock();
7076 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7077 GFP_ATOMIC);
7078 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007079
7080 return;
7081
7082nla_put_failure:
7083 genlmsg_cancel(msg, hdr);
7084 nlmsg_free(msg);
7085}
7086
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007087static void nl80211_send_remain_on_chan_event(
7088 int cmd, struct cfg80211_registered_device *rdev,
7089 struct net_device *netdev, u64 cookie,
7090 struct ieee80211_channel *chan,
7091 enum nl80211_channel_type channel_type,
7092 unsigned int duration, gfp_t gfp)
7093{
7094 struct sk_buff *msg;
7095 void *hdr;
7096
7097 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7098 if (!msg)
7099 return;
7100
7101 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7102 if (!hdr) {
7103 nlmsg_free(msg);
7104 return;
7105 }
7106
7107 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7108 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7109 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7110 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7111 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7112
7113 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7114 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7115
7116 if (genlmsg_end(msg, hdr) < 0) {
7117 nlmsg_free(msg);
7118 return;
7119 }
7120
7121 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7122 nl80211_mlme_mcgrp.id, gfp);
7123 return;
7124
7125 nla_put_failure:
7126 genlmsg_cancel(msg, hdr);
7127 nlmsg_free(msg);
7128}
7129
7130void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7131 struct net_device *netdev, u64 cookie,
7132 struct ieee80211_channel *chan,
7133 enum nl80211_channel_type channel_type,
7134 unsigned int duration, gfp_t gfp)
7135{
7136 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7137 rdev, netdev, cookie, chan,
7138 channel_type, duration, gfp);
7139}
7140
7141void nl80211_send_remain_on_channel_cancel(
7142 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7143 u64 cookie, struct ieee80211_channel *chan,
7144 enum nl80211_channel_type channel_type, gfp_t gfp)
7145{
7146 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7147 rdev, netdev, cookie, chan,
7148 channel_type, 0, gfp);
7149}
7150
Johannes Berg98b62182009-12-23 13:15:44 +01007151void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7152 struct net_device *dev, const u8 *mac_addr,
7153 struct station_info *sinfo, gfp_t gfp)
7154{
7155 struct sk_buff *msg;
7156
7157 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7158 if (!msg)
7159 return;
7160
7161 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7162 nlmsg_free(msg);
7163 return;
7164 }
7165
7166 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7167 nl80211_mlme_mcgrp.id, gfp);
7168}
7169
Jouni Malinenec15e682011-03-23 15:29:52 +02007170void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7171 struct net_device *dev, const u8 *mac_addr,
7172 gfp_t gfp)
7173{
7174 struct sk_buff *msg;
7175 void *hdr;
7176
7177 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7178 if (!msg)
7179 return;
7180
7181 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7182 if (!hdr) {
7183 nlmsg_free(msg);
7184 return;
7185 }
7186
7187 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7188 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7189
7190 if (genlmsg_end(msg, hdr) < 0) {
7191 nlmsg_free(msg);
7192 return;
7193 }
7194
7195 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7196 nl80211_mlme_mcgrp.id, gfp);
7197 return;
7198
7199 nla_put_failure:
7200 genlmsg_cancel(msg, hdr);
7201 nlmsg_free(msg);
7202}
7203
Johannes Berg2e161f72010-08-12 15:38:38 +02007204int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7205 struct net_device *netdev, u32 nlpid,
7206 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007207{
7208 struct sk_buff *msg;
7209 void *hdr;
7210 int err;
7211
7212 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7213 if (!msg)
7214 return -ENOMEM;
7215
Johannes Berg2e161f72010-08-12 15:38:38 +02007216 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007217 if (!hdr) {
7218 nlmsg_free(msg);
7219 return -ENOMEM;
7220 }
7221
7222 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7223 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7224 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7225 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7226
7227 err = genlmsg_end(msg, hdr);
7228 if (err < 0) {
7229 nlmsg_free(msg);
7230 return err;
7231 }
7232
7233 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7234 if (err < 0)
7235 return err;
7236 return 0;
7237
7238 nla_put_failure:
7239 genlmsg_cancel(msg, hdr);
7240 nlmsg_free(msg);
7241 return -ENOBUFS;
7242}
7243
Johannes Berg2e161f72010-08-12 15:38:38 +02007244void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7245 struct net_device *netdev, u64 cookie,
7246 const u8 *buf, size_t len, bool ack,
7247 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007248{
7249 struct sk_buff *msg;
7250 void *hdr;
7251
7252 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7253 if (!msg)
7254 return;
7255
Johannes Berg2e161f72010-08-12 15:38:38 +02007256 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007257 if (!hdr) {
7258 nlmsg_free(msg);
7259 return;
7260 }
7261
7262 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7263 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7264 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7265 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7266 if (ack)
7267 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7268
7269 if (genlmsg_end(msg, hdr) < 0) {
7270 nlmsg_free(msg);
7271 return;
7272 }
7273
7274 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7275 return;
7276
7277 nla_put_failure:
7278 genlmsg_cancel(msg, hdr);
7279 nlmsg_free(msg);
7280}
7281
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007282void
7283nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7284 struct net_device *netdev,
7285 enum nl80211_cqm_rssi_threshold_event rssi_event,
7286 gfp_t gfp)
7287{
7288 struct sk_buff *msg;
7289 struct nlattr *pinfoattr;
7290 void *hdr;
7291
7292 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7293 if (!msg)
7294 return;
7295
7296 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7297 if (!hdr) {
7298 nlmsg_free(msg);
7299 return;
7300 }
7301
7302 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7303 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7304
7305 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7306 if (!pinfoattr)
7307 goto nla_put_failure;
7308
7309 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7310 rssi_event);
7311
7312 nla_nest_end(msg, pinfoattr);
7313
7314 if (genlmsg_end(msg, hdr) < 0) {
7315 nlmsg_free(msg);
7316 return;
7317 }
7318
7319 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7320 nl80211_mlme_mcgrp.id, gfp);
7321 return;
7322
7323 nla_put_failure:
7324 genlmsg_cancel(msg, hdr);
7325 nlmsg_free(msg);
7326}
7327
Johannes Berge5497d72011-07-05 16:35:40 +02007328void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7329 struct net_device *netdev, const u8 *bssid,
7330 const u8 *replay_ctr, gfp_t gfp)
7331{
7332 struct sk_buff *msg;
7333 struct nlattr *rekey_attr;
7334 void *hdr;
7335
7336 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7337 if (!msg)
7338 return;
7339
7340 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7341 if (!hdr) {
7342 nlmsg_free(msg);
7343 return;
7344 }
7345
7346 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7347 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7348 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7349
7350 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7351 if (!rekey_attr)
7352 goto nla_put_failure;
7353
7354 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7355 NL80211_REPLAY_CTR_LEN, replay_ctr);
7356
7357 nla_nest_end(msg, rekey_attr);
7358
7359 if (genlmsg_end(msg, hdr) < 0) {
7360 nlmsg_free(msg);
7361 return;
7362 }
7363
7364 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7365 nl80211_mlme_mcgrp.id, gfp);
7366 return;
7367
7368 nla_put_failure:
7369 genlmsg_cancel(msg, hdr);
7370 nlmsg_free(msg);
7371}
7372
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007373void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7374 struct net_device *netdev, int index,
7375 const u8 *bssid, bool preauth, gfp_t gfp)
7376{
7377 struct sk_buff *msg;
7378 struct nlattr *attr;
7379 void *hdr;
7380
7381 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7382 if (!msg)
7383 return;
7384
7385 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7386 if (!hdr) {
7387 nlmsg_free(msg);
7388 return;
7389 }
7390
7391 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7392 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7393
7394 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7395 if (!attr)
7396 goto nla_put_failure;
7397
7398 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7399 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7400 if (preauth)
7401 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7402
7403 nla_nest_end(msg, attr);
7404
7405 if (genlmsg_end(msg, hdr) < 0) {
7406 nlmsg_free(msg);
7407 return;
7408 }
7409
7410 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7411 nl80211_mlme_mcgrp.id, gfp);
7412 return;
7413
7414 nla_put_failure:
7415 genlmsg_cancel(msg, hdr);
7416 nlmsg_free(msg);
7417}
7418
Johannes Bergc063dbf2010-11-24 08:10:05 +01007419void
7420nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7421 struct net_device *netdev, const u8 *peer,
7422 u32 num_packets, gfp_t gfp)
7423{
7424 struct sk_buff *msg;
7425 struct nlattr *pinfoattr;
7426 void *hdr;
7427
7428 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7429 if (!msg)
7430 return;
7431
7432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7433 if (!hdr) {
7434 nlmsg_free(msg);
7435 return;
7436 }
7437
7438 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7440 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7441
7442 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7443 if (!pinfoattr)
7444 goto nla_put_failure;
7445
7446 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7447
7448 nla_nest_end(msg, pinfoattr);
7449
7450 if (genlmsg_end(msg, hdr) < 0) {
7451 nlmsg_free(msg);
7452 return;
7453 }
7454
7455 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7456 nl80211_mlme_mcgrp.id, gfp);
7457 return;
7458
7459 nla_put_failure:
7460 genlmsg_cancel(msg, hdr);
7461 nlmsg_free(msg);
7462}
7463
Jouni Malinen026331c2010-02-15 12:53:10 +02007464static int nl80211_netlink_notify(struct notifier_block * nb,
7465 unsigned long state,
7466 void *_notify)
7467{
7468 struct netlink_notify *notify = _notify;
7469 struct cfg80211_registered_device *rdev;
7470 struct wireless_dev *wdev;
7471
7472 if (state != NETLINK_URELEASE)
7473 return NOTIFY_DONE;
7474
7475 rcu_read_lock();
7476
7477 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7478 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007479 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007480
7481 rcu_read_unlock();
7482
7483 return NOTIFY_DONE;
7484}
7485
7486static struct notifier_block nl80211_netlink_notifier = {
7487 .notifier_call = nl80211_netlink_notify,
7488};
7489
Johannes Berg55682962007-09-20 13:09:35 -04007490/* initialisation/exit functions */
7491
7492int nl80211_init(void)
7493{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007494 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007495
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007496 err = genl_register_family_with_ops(&nl80211_fam,
7497 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007498 if (err)
7499 return err;
7500
Johannes Berg55682962007-09-20 13:09:35 -04007501 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7502 if (err)
7503 goto err_out;
7504
Johannes Berg2a519312009-02-10 21:25:55 +01007505 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7506 if (err)
7507 goto err_out;
7508
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007509 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7510 if (err)
7511 goto err_out;
7512
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007513 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7514 if (err)
7515 goto err_out;
7516
Johannes Bergaff89a92009-07-01 21:26:51 +02007517#ifdef CONFIG_NL80211_TESTMODE
7518 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7519 if (err)
7520 goto err_out;
7521#endif
7522
Jouni Malinen026331c2010-02-15 12:53:10 +02007523 err = netlink_register_notifier(&nl80211_netlink_notifier);
7524 if (err)
7525 goto err_out;
7526
Johannes Berg55682962007-09-20 13:09:35 -04007527 return 0;
7528 err_out:
7529 genl_unregister_family(&nl80211_fam);
7530 return err;
7531}
7532
7533void nl80211_exit(void)
7534{
Jouni Malinen026331c2010-02-15 12:53:10 +02007535 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007536 genl_unregister_family(&nl80211_fam);
7537}