blob: edf655aeea0015513f7fc79290b3f5cecd02bc81 [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
Jouni Malinen36aedc92008-08-25 11:58:58 +0300135 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
136 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200137
138 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
139 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
140 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100141 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
142 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200143
144 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
145 .len = IEEE80211_MAX_SSID_LEN },
146 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
147 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200148 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300149 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300150 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300151 [NL80211_ATTR_STA_FLAGS2] = {
152 .len = sizeof(struct nl80211_sta_flag_update),
153 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300154 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300155 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
156 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200157 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
158 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
159 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200160 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100161 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100162 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
163 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100164 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
165 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200166 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200167 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
168 .len = IEEE80211_MAX_DATA_LEN },
169 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200170 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200171 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300172 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200173 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300174 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
175 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200176 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900177 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
178 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100179 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100180 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100181 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200182 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700183 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300184 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200185 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200186 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300187 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300188 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
190 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
191 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530192 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300193 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530194 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300195 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
197 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
198 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
199 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400200};
201
Johannes Berge31b8212010-10-05 19:39:30 +0200202/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000203static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200204 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200205 [NL80211_KEY_IDX] = { .type = NLA_U8 },
206 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
207 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
208 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
209 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200210 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100211 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
212};
213
214/* policy for the key default flags */
215static const struct nla_policy
216nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
217 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
218 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200219};
220
Johannes Bergff1b6e62011-05-04 15:37:28 +0200221/* policy for WoWLAN attributes */
222static const struct nla_policy
223nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
224 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
225 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
226 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
227 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200228 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
229 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
230 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
231 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200232};
233
Johannes Berge5497d72011-07-05 16:35:40 +0200234/* policy for GTK rekey offload attributes */
235static const struct nla_policy
236nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
237 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
238 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
239 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
240};
241
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300242static const struct nla_policy
243nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
244 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
245 .len = IEEE80211_MAX_SSID_LEN },
246};
247
Holger Schuriga0438972009-11-11 11:30:02 +0100248/* ifidx get helper */
249static int nl80211_get_ifidx(struct netlink_callback *cb)
250{
251 int res;
252
253 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
254 nl80211_fam.attrbuf, nl80211_fam.maxattr,
255 nl80211_policy);
256 if (res)
257 return res;
258
259 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
260 return -EINVAL;
261
262 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
263 if (!res)
264 return -EINVAL;
265 return res;
266}
267
Johannes Berg67748892010-10-04 21:14:06 +0200268static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
269 struct netlink_callback *cb,
270 struct cfg80211_registered_device **rdev,
271 struct net_device **dev)
272{
273 int ifidx = cb->args[0];
274 int err;
275
276 if (!ifidx)
277 ifidx = nl80211_get_ifidx(cb);
278 if (ifidx < 0)
279 return ifidx;
280
281 cb->args[0] = ifidx;
282
283 rtnl_lock();
284
285 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
286 if (!*dev) {
287 err = -ENODEV;
288 goto out_rtnl;
289 }
290
291 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100292 if (IS_ERR(*rdev)) {
293 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200294 goto out_rtnl;
295 }
296
297 return 0;
298 out_rtnl:
299 rtnl_unlock();
300 return err;
301}
302
303static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
304{
305 cfg80211_unlock_rdev(rdev);
306 rtnl_unlock();
307}
308
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100309/* IE validation */
310static bool is_valid_ie_attr(const struct nlattr *attr)
311{
312 const u8 *pos;
313 int len;
314
315 if (!attr)
316 return true;
317
318 pos = nla_data(attr);
319 len = nla_len(attr);
320
321 while (len) {
322 u8 elemlen;
323
324 if (len < 2)
325 return false;
326 len -= 2;
327
328 elemlen = pos[1];
329 if (elemlen > len)
330 return false;
331
332 len -= elemlen;
333 pos += 2 + elemlen;
334 }
335
336 return true;
337}
338
Johannes Berg55682962007-09-20 13:09:35 -0400339/* message building helper */
340static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
341 int flags, u8 cmd)
342{
343 /* since there is no private header just add the generic one */
344 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
345}
346
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400347static int nl80211_msg_put_channel(struct sk_buff *msg,
348 struct ieee80211_channel *chan)
349{
350 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
351 chan->center_freq);
352
353 if (chan->flags & IEEE80211_CHAN_DISABLED)
354 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
355 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
356 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
357 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
358 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
359 if (chan->flags & IEEE80211_CHAN_RADAR)
360 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
361
362 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
363 DBM_TO_MBM(chan->max_power));
364
365 return 0;
366
367 nla_put_failure:
368 return -ENOBUFS;
369}
370
Johannes Berg55682962007-09-20 13:09:35 -0400371/* netlink command implementations */
372
Johannes Bergb9454e82009-07-08 13:29:08 +0200373struct key_parse {
374 struct key_params p;
375 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200376 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200377 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100378 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200379};
380
381static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
382{
383 struct nlattr *tb[NL80211_KEY_MAX + 1];
384 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
385 nl80211_key_policy);
386 if (err)
387 return err;
388
389 k->def = !!tb[NL80211_KEY_DEFAULT];
390 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
391
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100392 if (k->def) {
393 k->def_uni = true;
394 k->def_multi = true;
395 }
396 if (k->defmgmt)
397 k->def_multi = true;
398
Johannes Bergb9454e82009-07-08 13:29:08 +0200399 if (tb[NL80211_KEY_IDX])
400 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
401
402 if (tb[NL80211_KEY_DATA]) {
403 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
404 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
405 }
406
407 if (tb[NL80211_KEY_SEQ]) {
408 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
409 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
410 }
411
412 if (tb[NL80211_KEY_CIPHER])
413 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
414
Johannes Berge31b8212010-10-05 19:39:30 +0200415 if (tb[NL80211_KEY_TYPE]) {
416 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
417 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
418 return -EINVAL;
419 }
420
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100421 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
422 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
423 int err = nla_parse_nested(kdt,
424 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
425 tb[NL80211_KEY_DEFAULT_TYPES],
426 nl80211_key_default_policy);
427 if (err)
428 return err;
429
430 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
431 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
432 }
433
Johannes Bergb9454e82009-07-08 13:29:08 +0200434 return 0;
435}
436
437static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
438{
439 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
440 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
441 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
442 }
443
444 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
445 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
446 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
447 }
448
449 if (info->attrs[NL80211_ATTR_KEY_IDX])
450 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
451
452 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
453 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
454
455 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
456 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
457
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100458 if (k->def) {
459 k->def_uni = true;
460 k->def_multi = true;
461 }
462 if (k->defmgmt)
463 k->def_multi = true;
464
Johannes Berge31b8212010-10-05 19:39:30 +0200465 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
466 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
467 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
468 return -EINVAL;
469 }
470
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100471 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
472 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
473 int err = nla_parse_nested(
474 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
475 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
476 nl80211_key_default_policy);
477 if (err)
478 return err;
479
480 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
481 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
482 }
483
Johannes Bergb9454e82009-07-08 13:29:08 +0200484 return 0;
485}
486
487static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
488{
489 int err;
490
491 memset(k, 0, sizeof(*k));
492 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200493 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200494
495 if (info->attrs[NL80211_ATTR_KEY])
496 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
497 else
498 err = nl80211_parse_key_old(info, k);
499
500 if (err)
501 return err;
502
503 if (k->def && k->defmgmt)
504 return -EINVAL;
505
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100506 if (k->defmgmt) {
507 if (k->def_uni || !k->def_multi)
508 return -EINVAL;
509 }
510
Johannes Bergb9454e82009-07-08 13:29:08 +0200511 if (k->idx != -1) {
512 if (k->defmgmt) {
513 if (k->idx < 4 || k->idx > 5)
514 return -EINVAL;
515 } else if (k->def) {
516 if (k->idx < 0 || k->idx > 3)
517 return -EINVAL;
518 } else {
519 if (k->idx < 0 || k->idx > 5)
520 return -EINVAL;
521 }
522 }
523
524 return 0;
525}
526
Johannes Bergfffd0932009-07-08 14:22:54 +0200527static struct cfg80211_cached_keys *
528nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
529 struct nlattr *keys)
530{
531 struct key_parse parse;
532 struct nlattr *key;
533 struct cfg80211_cached_keys *result;
534 int rem, err, def = 0;
535
536 result = kzalloc(sizeof(*result), GFP_KERNEL);
537 if (!result)
538 return ERR_PTR(-ENOMEM);
539
540 result->def = -1;
541 result->defmgmt = -1;
542
543 nla_for_each_nested(key, keys, rem) {
544 memset(&parse, 0, sizeof(parse));
545 parse.idx = -1;
546
547 err = nl80211_parse_key_new(key, &parse);
548 if (err)
549 goto error;
550 err = -EINVAL;
551 if (!parse.p.key)
552 goto error;
553 if (parse.idx < 0 || parse.idx > 4)
554 goto error;
555 if (parse.def) {
556 if (def)
557 goto error;
558 def = 1;
559 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100560 if (!parse.def_uni || !parse.def_multi)
561 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200562 } else if (parse.defmgmt)
563 goto error;
564 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200565 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200566 if (err)
567 goto error;
568 result->params[parse.idx].cipher = parse.p.cipher;
569 result->params[parse.idx].key_len = parse.p.key_len;
570 result->params[parse.idx].key = result->data[parse.idx];
571 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
572 }
573
574 return result;
575 error:
576 kfree(result);
577 return ERR_PTR(err);
578}
579
580static int nl80211_key_allowed(struct wireless_dev *wdev)
581{
582 ASSERT_WDEV_LOCK(wdev);
583
Johannes Bergfffd0932009-07-08 14:22:54 +0200584 switch (wdev->iftype) {
585 case NL80211_IFTYPE_AP:
586 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200587 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700588 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200589 break;
590 case NL80211_IFTYPE_ADHOC:
591 if (!wdev->current_bss)
592 return -ENOLINK;
593 break;
594 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200595 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200596 if (wdev->sme_state != CFG80211_SME_CONNECTED)
597 return -ENOLINK;
598 break;
599 default:
600 return -EINVAL;
601 }
602
603 return 0;
604}
605
Johannes Berg7527a782011-05-13 10:58:57 +0200606static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
607{
608 struct nlattr *nl_modes = nla_nest_start(msg, attr);
609 int i;
610
611 if (!nl_modes)
612 goto nla_put_failure;
613
614 i = 0;
615 while (ifmodes) {
616 if (ifmodes & 1)
617 NLA_PUT_FLAG(msg, i);
618 ifmodes >>= 1;
619 i++;
620 }
621
622 nla_nest_end(msg, nl_modes);
623 return 0;
624
625nla_put_failure:
626 return -ENOBUFS;
627}
628
629static int nl80211_put_iface_combinations(struct wiphy *wiphy,
630 struct sk_buff *msg)
631{
632 struct nlattr *nl_combis;
633 int i, j;
634
635 nl_combis = nla_nest_start(msg,
636 NL80211_ATTR_INTERFACE_COMBINATIONS);
637 if (!nl_combis)
638 goto nla_put_failure;
639
640 for (i = 0; i < wiphy->n_iface_combinations; i++) {
641 const struct ieee80211_iface_combination *c;
642 struct nlattr *nl_combi, *nl_limits;
643
644 c = &wiphy->iface_combinations[i];
645
646 nl_combi = nla_nest_start(msg, i + 1);
647 if (!nl_combi)
648 goto nla_put_failure;
649
650 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
651 if (!nl_limits)
652 goto nla_put_failure;
653
654 for (j = 0; j < c->n_limits; j++) {
655 struct nlattr *nl_limit;
656
657 nl_limit = nla_nest_start(msg, j + 1);
658 if (!nl_limit)
659 goto nla_put_failure;
660 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
661 c->limits[j].max);
662 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
663 c->limits[j].types))
664 goto nla_put_failure;
665 nla_nest_end(msg, nl_limit);
666 }
667
668 nla_nest_end(msg, nl_limits);
669
670 if (c->beacon_int_infra_match)
671 NLA_PUT_FLAG(msg,
672 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
673 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
674 c->num_different_channels);
675 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
676 c->max_interfaces);
677
678 nla_nest_end(msg, nl_combi);
679 }
680
681 nla_nest_end(msg, nl_combis);
682
683 return 0;
684nla_put_failure:
685 return -ENOBUFS;
686}
687
Johannes Berg55682962007-09-20 13:09:35 -0400688static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
689 struct cfg80211_registered_device *dev)
690{
691 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100692 struct nlattr *nl_bands, *nl_band;
693 struct nlattr *nl_freqs, *nl_freq;
694 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100695 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100696 enum ieee80211_band band;
697 struct ieee80211_channel *chan;
698 struct ieee80211_rate *rate;
699 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200700 const struct ieee80211_txrx_stypes *mgmt_stypes =
701 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400702
703 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
704 if (!hdr)
705 return -1;
706
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500707 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400708 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200709
Johannes Bergf5ea9122009-08-07 16:17:38 +0200710 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
711 cfg80211_rdev_list_generation);
712
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200713 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
714 dev->wiphy.retry_short);
715 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
716 dev->wiphy.retry_long);
717 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
718 dev->wiphy.frag_threshold);
719 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
720 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100721 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
722 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100723 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
724 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300725 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
726 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200727 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
728 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300729 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
730 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300731 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
732 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100733
Johannes Berge31b8212010-10-05 19:39:30 +0200734 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
735 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700736 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
737 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300738 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
739 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530740 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
741 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300742 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
743 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
744 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
745 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530746
Johannes Berg25e47c12009-04-02 20:14:06 +0200747 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
748 sizeof(u32) * dev->wiphy.n_cipher_suites,
749 dev->wiphy.cipher_suites);
750
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100751 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
752 dev->wiphy.max_num_pmkids);
753
Johannes Bergc0692b82010-08-27 14:26:53 +0300754 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
755 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
756
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900757 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
758 dev->wiphy.available_antennas_tx);
759 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
760 dev->wiphy.available_antennas_rx);
761
Bruno Randolf7f531e02010-12-16 11:30:22 +0900762 if ((dev->wiphy.available_antennas_tx ||
763 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900764 u32 tx_ant = 0, rx_ant = 0;
765 int res;
766 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
767 if (!res) {
768 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
769 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
770 }
771 }
772
Johannes Berg7527a782011-05-13 10:58:57 +0200773 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
774 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700775 goto nla_put_failure;
776
Johannes Bergee688b002008-01-24 19:38:39 +0100777 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
778 if (!nl_bands)
779 goto nla_put_failure;
780
781 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
782 if (!dev->wiphy.bands[band])
783 continue;
784
785 nl_band = nla_nest_start(msg, band);
786 if (!nl_band)
787 goto nla_put_failure;
788
Johannes Bergd51626d2008-10-09 12:20:13 +0200789 /* add HT info */
790 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
791 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
792 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
793 &dev->wiphy.bands[band]->ht_cap.mcs);
794 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
795 dev->wiphy.bands[band]->ht_cap.cap);
796 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
797 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
798 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
799 dev->wiphy.bands[band]->ht_cap.ampdu_density);
800 }
801
Johannes Bergee688b002008-01-24 19:38:39 +0100802 /* add frequencies */
803 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
804 if (!nl_freqs)
805 goto nla_put_failure;
806
807 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
808 nl_freq = nla_nest_start(msg, i);
809 if (!nl_freq)
810 goto nla_put_failure;
811
812 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100813
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400814 if (nl80211_msg_put_channel(msg, chan))
815 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200816
Johannes Bergee688b002008-01-24 19:38:39 +0100817 nla_nest_end(msg, nl_freq);
818 }
819
820 nla_nest_end(msg, nl_freqs);
821
822 /* add bitrates */
823 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
824 if (!nl_rates)
825 goto nla_put_failure;
826
827 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
828 nl_rate = nla_nest_start(msg, i);
829 if (!nl_rate)
830 goto nla_put_failure;
831
832 rate = &dev->wiphy.bands[band]->bitrates[i];
833 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
834 rate->bitrate);
835 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
836 NLA_PUT_FLAG(msg,
837 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
838
839 nla_nest_end(msg, nl_rate);
840 }
841
842 nla_nest_end(msg, nl_rates);
843
844 nla_nest_end(msg, nl_band);
845 }
846 nla_nest_end(msg, nl_bands);
847
Johannes Berg8fdc6212009-03-14 09:34:01 +0100848 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
849 if (!nl_cmds)
850 goto nla_put_failure;
851
852 i = 0;
853#define CMD(op, n) \
854 do { \
855 if (dev->ops->op) { \
856 i++; \
857 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
858 } \
859 } while (0)
860
861 CMD(add_virtual_intf, NEW_INTERFACE);
862 CMD(change_virtual_intf, SET_INTERFACE);
863 CMD(add_key, NEW_KEY);
864 CMD(add_beacon, NEW_BEACON);
865 CMD(add_station, NEW_STATION);
866 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800867 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100868 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200869 CMD(auth, AUTHENTICATE);
870 CMD(assoc, ASSOCIATE);
871 CMD(deauth, DEAUTHENTICATE);
872 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200873 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100874 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100875 CMD(set_pmksa, SET_PMKSA);
876 CMD(del_pmksa, DEL_PMKSA);
877 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100878 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200879 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200880 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100881 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100882 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200883 i++;
884 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
885 }
Johannes Bergf444de02010-05-05 15:25:02 +0200886 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400887 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300888 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
889 CMD(tdls_mgmt, TDLS_MGMT);
890 CMD(tdls_oper, TDLS_OPER);
891 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300892 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
893 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100894
895#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200896
Johannes Berg6829c872009-07-02 09:13:27 +0200897 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200898 i++;
899 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
900 }
901
Johannes Berg6829c872009-07-02 09:13:27 +0200902 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903 i++;
904 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
905 }
906
Johannes Berg8fdc6212009-03-14 09:34:01 +0100907 nla_nest_end(msg, nl_cmds);
908
Johannes Berga2939112010-12-14 17:54:28 +0100909 if (dev->ops->remain_on_channel)
910 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
911 dev->wiphy.max_remain_on_channel_duration);
912
Jouni Malinend2da5872011-08-08 12:10:30 +0300913 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100914 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
915
Johannes Berg2e161f72010-08-12 15:38:38 +0200916 if (mgmt_stypes) {
917 u16 stypes;
918 struct nlattr *nl_ftypes, *nl_ifs;
919 enum nl80211_iftype ift;
920
921 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
922 if (!nl_ifs)
923 goto nla_put_failure;
924
925 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
926 nl_ftypes = nla_nest_start(msg, ift);
927 if (!nl_ftypes)
928 goto nla_put_failure;
929 i = 0;
930 stypes = mgmt_stypes[ift].tx;
931 while (stypes) {
932 if (stypes & 1)
933 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
934 (i << 4) | IEEE80211_FTYPE_MGMT);
935 stypes >>= 1;
936 i++;
937 }
938 nla_nest_end(msg, nl_ftypes);
939 }
940
Johannes Berg74b70a42010-08-24 12:15:53 +0200941 nla_nest_end(msg, nl_ifs);
942
Johannes Berg2e161f72010-08-12 15:38:38 +0200943 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
944 if (!nl_ifs)
945 goto nla_put_failure;
946
947 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
948 nl_ftypes = nla_nest_start(msg, ift);
949 if (!nl_ftypes)
950 goto nla_put_failure;
951 i = 0;
952 stypes = mgmt_stypes[ift].rx;
953 while (stypes) {
954 if (stypes & 1)
955 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
956 (i << 4) | IEEE80211_FTYPE_MGMT);
957 stypes >>= 1;
958 i++;
959 }
960 nla_nest_end(msg, nl_ftypes);
961 }
962 nla_nest_end(msg, nl_ifs);
963 }
964
Johannes Bergff1b6e62011-05-04 15:37:28 +0200965 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
966 struct nlattr *nl_wowlan;
967
968 nl_wowlan = nla_nest_start(msg,
969 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
970 if (!nl_wowlan)
971 goto nla_put_failure;
972
973 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
974 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
975 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
976 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
977 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
978 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200979 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
980 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
981 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
982 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
983 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
984 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
985 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
986 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
987 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
988 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200989 if (dev->wiphy.wowlan.n_patterns) {
990 struct nl80211_wowlan_pattern_support pat = {
991 .max_patterns = dev->wiphy.wowlan.n_patterns,
992 .min_pattern_len =
993 dev->wiphy.wowlan.pattern_min_len,
994 .max_pattern_len =
995 dev->wiphy.wowlan.pattern_max_len,
996 };
997 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
998 sizeof(pat), &pat);
999 }
1000
1001 nla_nest_end(msg, nl_wowlan);
1002 }
1003
Johannes Berg7527a782011-05-13 10:58:57 +02001004 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1005 dev->wiphy.software_iftypes))
1006 goto nla_put_failure;
1007
1008 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1009 goto nla_put_failure;
1010
Johannes Berg55682962007-09-20 13:09:35 -04001011 return genlmsg_end(msg, hdr);
1012
1013 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001014 genlmsg_cancel(msg, hdr);
1015 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001016}
1017
1018static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1019{
1020 int idx = 0;
1021 int start = cb->args[0];
1022 struct cfg80211_registered_device *dev;
1023
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001024 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001025 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001026 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1027 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001028 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001029 continue;
1030 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1031 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001032 dev) < 0) {
1033 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001034 break;
Julius Volzb4637272008-07-08 14:02:19 +02001035 }
Johannes Berg55682962007-09-20 13:09:35 -04001036 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001037 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001038
1039 cb->args[0] = idx;
1040
1041 return skb->len;
1042}
1043
1044static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1045{
1046 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001047 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001048
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001049 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001050 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001051 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001052
Johannes Berg4c476992010-10-04 21:36:35 +02001053 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1054 nlmsg_free(msg);
1055 return -ENOBUFS;
1056 }
Johannes Berg55682962007-09-20 13:09:35 -04001057
Johannes Berg134e6372009-07-10 09:51:34 +00001058 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001059}
1060
Jouni Malinen31888482008-10-30 16:59:24 +02001061static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1062 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1063 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1064 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1065 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1066 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1067};
1068
1069static int parse_txq_params(struct nlattr *tb[],
1070 struct ieee80211_txq_params *txq_params)
1071{
1072 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1073 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1074 !tb[NL80211_TXQ_ATTR_AIFS])
1075 return -EINVAL;
1076
1077 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1078 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1079 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1080 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1081 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1082
1083 return 0;
1084}
1085
Johannes Bergf444de02010-05-05 15:25:02 +02001086static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1087{
1088 /*
1089 * You can only set the channel explicitly for AP, mesh
1090 * and WDS type interfaces; all others have their channel
1091 * managed via their respective "establish a connection"
1092 * command (connect, join, ...)
1093 *
1094 * Monitors are special as they are normally slaved to
1095 * whatever else is going on, so they behave as though
1096 * you tried setting the wiphy channel itself.
1097 */
1098 return !wdev ||
1099 wdev->iftype == NL80211_IFTYPE_AP ||
1100 wdev->iftype == NL80211_IFTYPE_WDS ||
1101 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001102 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1103 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001104}
1105
1106static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1107 struct wireless_dev *wdev,
1108 struct genl_info *info)
1109{
1110 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1111 u32 freq;
1112 int result;
1113
1114 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1115 return -EINVAL;
1116
1117 if (!nl80211_can_set_dev_channel(wdev))
1118 return -EOPNOTSUPP;
1119
1120 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1121 channel_type = nla_get_u32(info->attrs[
1122 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1123 if (channel_type != NL80211_CHAN_NO_HT &&
1124 channel_type != NL80211_CHAN_HT20 &&
1125 channel_type != NL80211_CHAN_HT40PLUS &&
1126 channel_type != NL80211_CHAN_HT40MINUS)
1127 return -EINVAL;
1128 }
1129
1130 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1131
1132 mutex_lock(&rdev->devlist_mtx);
1133 if (wdev) {
1134 wdev_lock(wdev);
1135 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1136 wdev_unlock(wdev);
1137 } else {
1138 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1139 }
1140 mutex_unlock(&rdev->devlist_mtx);
1141
1142 return result;
1143}
1144
1145static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1146{
Johannes Berg4c476992010-10-04 21:36:35 +02001147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1148 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001149
Johannes Berg4c476992010-10-04 21:36:35 +02001150 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001151}
1152
Bill Jordane8347eb2010-10-01 13:54:28 -04001153static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1154{
Johannes Berg43b19952010-10-07 13:10:30 +02001155 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1156 struct net_device *dev = info->user_ptr[1];
1157 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001158 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001159
1160 if (!info->attrs[NL80211_ATTR_MAC])
1161 return -EINVAL;
1162
Johannes Berg43b19952010-10-07 13:10:30 +02001163 if (netif_running(dev))
1164 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001165
Johannes Berg43b19952010-10-07 13:10:30 +02001166 if (!rdev->ops->set_wds_peer)
1167 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001168
Johannes Berg43b19952010-10-07 13:10:30 +02001169 if (wdev->iftype != NL80211_IFTYPE_WDS)
1170 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001171
1172 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001173 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001174}
1175
1176
Johannes Berg55682962007-09-20 13:09:35 -04001177static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1178{
1179 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001180 struct net_device *netdev = NULL;
1181 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001182 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001183 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001184 u32 changed;
1185 u8 retry_short = 0, retry_long = 0;
1186 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001187 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001188
Johannes Bergf444de02010-05-05 15:25:02 +02001189 /*
1190 * Try to find the wiphy and netdev. Normally this
1191 * function shouldn't need the netdev, but this is
1192 * done for backward compatibility -- previously
1193 * setting the channel was done per wiphy, but now
1194 * it is per netdev. Previous userland like hostapd
1195 * also passed a netdev to set_wiphy, so that it is
1196 * possible to let that go to the right netdev!
1197 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001198 mutex_lock(&cfg80211_mutex);
1199
Johannes Bergf444de02010-05-05 15:25:02 +02001200 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1201 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1202
1203 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1204 if (netdev && netdev->ieee80211_ptr) {
1205 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1206 mutex_lock(&rdev->mtx);
1207 } else
1208 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001209 }
1210
Johannes Bergf444de02010-05-05 15:25:02 +02001211 if (!netdev) {
1212 rdev = __cfg80211_rdev_from_info(info);
1213 if (IS_ERR(rdev)) {
1214 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001215 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001216 }
1217 wdev = NULL;
1218 netdev = NULL;
1219 result = 0;
1220
1221 mutex_lock(&rdev->mtx);
1222 } else if (netif_running(netdev) &&
1223 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1224 wdev = netdev->ieee80211_ptr;
1225 else
1226 wdev = NULL;
1227
1228 /*
1229 * end workaround code, by now the rdev is available
1230 * and locked, and wdev may or may not be NULL.
1231 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001232
1233 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001234 result = cfg80211_dev_rename(
1235 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001236
1237 mutex_unlock(&cfg80211_mutex);
1238
1239 if (result)
1240 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001241
Jouni Malinen31888482008-10-30 16:59:24 +02001242 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1243 struct ieee80211_txq_params txq_params;
1244 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1245
1246 if (!rdev->ops->set_txq_params) {
1247 result = -EOPNOTSUPP;
1248 goto bad_res;
1249 }
1250
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001251 if (!netdev) {
1252 result = -EINVAL;
1253 goto bad_res;
1254 }
1255
Jouni Malinen31888482008-10-30 16:59:24 +02001256 nla_for_each_nested(nl_txq_params,
1257 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1258 rem_txq_params) {
1259 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1260 nla_data(nl_txq_params),
1261 nla_len(nl_txq_params),
1262 txq_params_policy);
1263 result = parse_txq_params(tb, &txq_params);
1264 if (result)
1265 goto bad_res;
1266
1267 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001268 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001269 &txq_params);
1270 if (result)
1271 goto bad_res;
1272 }
1273 }
1274
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001275 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001276 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001277 if (result)
1278 goto bad_res;
1279 }
1280
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001281 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1282 enum nl80211_tx_power_setting type;
1283 int idx, mbm = 0;
1284
1285 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001286 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001287 goto bad_res;
1288 }
1289
1290 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1291 type = nla_get_u32(info->attrs[idx]);
1292
1293 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1294 (type != NL80211_TX_POWER_AUTOMATIC)) {
1295 result = -EINVAL;
1296 goto bad_res;
1297 }
1298
1299 if (type != NL80211_TX_POWER_AUTOMATIC) {
1300 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1301 mbm = nla_get_u32(info->attrs[idx]);
1302 }
1303
1304 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1305 if (result)
1306 goto bad_res;
1307 }
1308
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001309 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1310 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1311 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001312 if ((!rdev->wiphy.available_antennas_tx &&
1313 !rdev->wiphy.available_antennas_rx) ||
1314 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001315 result = -EOPNOTSUPP;
1316 goto bad_res;
1317 }
1318
1319 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1320 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1321
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001322 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001323 * available antenna masks, except for the "all" mask */
1324 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1325 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001326 result = -EINVAL;
1327 goto bad_res;
1328 }
1329
Bruno Randolf7f531e02010-12-16 11:30:22 +09001330 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1331 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001332
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001333 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1334 if (result)
1335 goto bad_res;
1336 }
1337
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001338 changed = 0;
1339
1340 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1341 retry_short = nla_get_u8(
1342 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1343 if (retry_short == 0) {
1344 result = -EINVAL;
1345 goto bad_res;
1346 }
1347 changed |= WIPHY_PARAM_RETRY_SHORT;
1348 }
1349
1350 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1351 retry_long = nla_get_u8(
1352 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1353 if (retry_long == 0) {
1354 result = -EINVAL;
1355 goto bad_res;
1356 }
1357 changed |= WIPHY_PARAM_RETRY_LONG;
1358 }
1359
1360 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1361 frag_threshold = nla_get_u32(
1362 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1363 if (frag_threshold < 256) {
1364 result = -EINVAL;
1365 goto bad_res;
1366 }
1367 if (frag_threshold != (u32) -1) {
1368 /*
1369 * Fragments (apart from the last one) are required to
1370 * have even length. Make the fragmentation code
1371 * simpler by stripping LSB should someone try to use
1372 * odd threshold value.
1373 */
1374 frag_threshold &= ~0x1;
1375 }
1376 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1377 }
1378
1379 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1380 rts_threshold = nla_get_u32(
1381 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1382 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1383 }
1384
Lukáš Turek81077e82009-12-21 22:50:47 +01001385 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1386 coverage_class = nla_get_u8(
1387 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1388 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1389 }
1390
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001391 if (changed) {
1392 u8 old_retry_short, old_retry_long;
1393 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001394 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001395
1396 if (!rdev->ops->set_wiphy_params) {
1397 result = -EOPNOTSUPP;
1398 goto bad_res;
1399 }
1400
1401 old_retry_short = rdev->wiphy.retry_short;
1402 old_retry_long = rdev->wiphy.retry_long;
1403 old_frag_threshold = rdev->wiphy.frag_threshold;
1404 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001405 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001406
1407 if (changed & WIPHY_PARAM_RETRY_SHORT)
1408 rdev->wiphy.retry_short = retry_short;
1409 if (changed & WIPHY_PARAM_RETRY_LONG)
1410 rdev->wiphy.retry_long = retry_long;
1411 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1412 rdev->wiphy.frag_threshold = frag_threshold;
1413 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1414 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001415 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1416 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001417
1418 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1419 if (result) {
1420 rdev->wiphy.retry_short = old_retry_short;
1421 rdev->wiphy.retry_long = old_retry_long;
1422 rdev->wiphy.frag_threshold = old_frag_threshold;
1423 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001424 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001425 }
1426 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001427
Johannes Berg306d6112008-12-08 12:39:04 +01001428 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001429 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001430 if (netdev)
1431 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001432 return result;
1433}
1434
1435
1436static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001437 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001438 struct net_device *dev)
1439{
1440 void *hdr;
1441
1442 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1443 if (!hdr)
1444 return -1;
1445
1446 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001447 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001448 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001449 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001450
1451 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1452 rdev->devlist_generation ^
1453 (cfg80211_rdev_list_generation << 2));
1454
Johannes Berg55682962007-09-20 13:09:35 -04001455 return genlmsg_end(msg, hdr);
1456
1457 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001458 genlmsg_cancel(msg, hdr);
1459 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001460}
1461
1462static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1463{
1464 int wp_idx = 0;
1465 int if_idx = 0;
1466 int wp_start = cb->args[0];
1467 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001468 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001469 struct wireless_dev *wdev;
1470
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001471 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001472 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1473 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001474 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001475 if (wp_idx < wp_start) {
1476 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001477 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001478 }
Johannes Berg55682962007-09-20 13:09:35 -04001479 if_idx = 0;
1480
Johannes Bergf5ea9122009-08-07 16:17:38 +02001481 mutex_lock(&rdev->devlist_mtx);
1482 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001483 if (if_idx < if_start) {
1484 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001485 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001486 }
Johannes Berg55682962007-09-20 13:09:35 -04001487 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1488 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001489 rdev, wdev->netdev) < 0) {
1490 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001491 goto out;
1492 }
1493 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001494 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001495 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001496
1497 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001498 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001499 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001500 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001501
1502 cb->args[0] = wp_idx;
1503 cb->args[1] = if_idx;
1504
1505 return skb->len;
1506}
1507
1508static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1509{
1510 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001511 struct cfg80211_registered_device *dev = info->user_ptr[0];
1512 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001513
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001515 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001516 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001517
Johannes Bergd7264052009-04-19 16:23:20 +02001518 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001519 dev, netdev) < 0) {
1520 nlmsg_free(msg);
1521 return -ENOBUFS;
1522 }
Johannes Berg55682962007-09-20 13:09:35 -04001523
Johannes Berg134e6372009-07-10 09:51:34 +00001524 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001525}
1526
Michael Wu66f7ac52008-01-31 19:48:22 +01001527static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1528 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1529 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1530 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1531 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1532 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1533};
1534
1535static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1536{
1537 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1538 int flag;
1539
1540 *mntrflags = 0;
1541
1542 if (!nla)
1543 return -EINVAL;
1544
1545 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1546 nla, mntr_flags_policy))
1547 return -EINVAL;
1548
1549 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1550 if (flags[flag])
1551 *mntrflags |= (1<<flag);
1552
1553 return 0;
1554}
1555
Johannes Berg9bc383d2009-11-19 11:55:19 +01001556static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001557 struct net_device *netdev, u8 use_4addr,
1558 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001559{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001560 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001561 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001562 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001563 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001564 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001565
1566 switch (iftype) {
1567 case NL80211_IFTYPE_AP_VLAN:
1568 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1569 return 0;
1570 break;
1571 case NL80211_IFTYPE_STATION:
1572 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1573 return 0;
1574 break;
1575 default:
1576 break;
1577 }
1578
1579 return -EOPNOTSUPP;
1580}
1581
Johannes Berg55682962007-09-20 13:09:35 -04001582static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1583{
Johannes Berg4c476992010-10-04 21:36:35 +02001584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001585 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001586 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001587 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001588 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001589 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001590 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001591
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001592 memset(&params, 0, sizeof(params));
1593
Johannes Berg04a773a2009-04-19 21:24:32 +02001594 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001595
Johannes Berg723b0382008-09-16 20:22:09 +02001596 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001597 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001598 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001599 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001600 if (ntype > NL80211_IFTYPE_MAX)
1601 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001602 }
1603
Johannes Berg92ffe052008-09-16 20:39:36 +02001604 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001605 struct wireless_dev *wdev = dev->ieee80211_ptr;
1606
Johannes Berg4c476992010-10-04 21:36:35 +02001607 if (ntype != NL80211_IFTYPE_MESH_POINT)
1608 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001609 if (netif_running(dev))
1610 return -EBUSY;
1611
1612 wdev_lock(wdev);
1613 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1614 IEEE80211_MAX_MESH_ID_LEN);
1615 wdev->mesh_id_up_len =
1616 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1617 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1618 wdev->mesh_id_up_len);
1619 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001620 }
1621
Felix Fietkau8b787642009-11-10 18:53:10 +01001622 if (info->attrs[NL80211_ATTR_4ADDR]) {
1623 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1624 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001625 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001626 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001627 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001628 } else {
1629 params.use_4addr = -1;
1630 }
1631
Johannes Berg92ffe052008-09-16 20:39:36 +02001632 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001633 if (ntype != NL80211_IFTYPE_MONITOR)
1634 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001635 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1636 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001637 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001638 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001639
1640 flags = &_flags;
1641 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001642 }
Johannes Berg3b858752009-03-12 09:55:09 +01001643
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001644 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001645 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001646 else
1647 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001648
Johannes Berg9bc383d2009-11-19 11:55:19 +01001649 if (!err && params.use_4addr != -1)
1650 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1651
Johannes Berg55682962007-09-20 13:09:35 -04001652 return err;
1653}
1654
1655static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1656{
Johannes Berg4c476992010-10-04 21:36:35 +02001657 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001658 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001659 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001660 int err;
1661 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001662 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001663
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001664 memset(&params, 0, sizeof(params));
1665
Johannes Berg55682962007-09-20 13:09:35 -04001666 if (!info->attrs[NL80211_ATTR_IFNAME])
1667 return -EINVAL;
1668
1669 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1670 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1671 if (type > NL80211_IFTYPE_MAX)
1672 return -EINVAL;
1673 }
1674
Johannes Berg79c97e92009-07-07 03:56:12 +02001675 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001676 !(rdev->wiphy.interface_modes & (1 << type)))
1677 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001678
Johannes Berg9bc383d2009-11-19 11:55:19 +01001679 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001680 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001681 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001682 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001683 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001684 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001685
Michael Wu66f7ac52008-01-31 19:48:22 +01001686 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1687 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1688 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001689 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001690 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001691 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001692 if (IS_ERR(dev))
1693 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001694
Johannes Berg29cbe682010-12-03 09:20:44 +01001695 if (type == NL80211_IFTYPE_MESH_POINT &&
1696 info->attrs[NL80211_ATTR_MESH_ID]) {
1697 struct wireless_dev *wdev = dev->ieee80211_ptr;
1698
1699 wdev_lock(wdev);
1700 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1701 IEEE80211_MAX_MESH_ID_LEN);
1702 wdev->mesh_id_up_len =
1703 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1704 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1705 wdev->mesh_id_up_len);
1706 wdev_unlock(wdev);
1707 }
1708
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001709 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001710}
1711
1712static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1713{
Johannes Berg4c476992010-10-04 21:36:35 +02001714 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1715 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001716
Johannes Berg4c476992010-10-04 21:36:35 +02001717 if (!rdev->ops->del_virtual_intf)
1718 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001719
Johannes Berg4c476992010-10-04 21:36:35 +02001720 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001721}
1722
Johannes Berg41ade002007-12-19 02:03:29 +01001723struct get_key_cookie {
1724 struct sk_buff *msg;
1725 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001726 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001727};
1728
1729static void get_key_callback(void *c, struct key_params *params)
1730{
Johannes Bergb9454e82009-07-08 13:29:08 +02001731 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001732 struct get_key_cookie *cookie = c;
1733
1734 if (params->key)
1735 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1736 params->key_len, params->key);
1737
1738 if (params->seq)
1739 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1740 params->seq_len, params->seq);
1741
1742 if (params->cipher)
1743 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1744 params->cipher);
1745
Johannes Bergb9454e82009-07-08 13:29:08 +02001746 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1747 if (!key)
1748 goto nla_put_failure;
1749
1750 if (params->key)
1751 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1752 params->key_len, params->key);
1753
1754 if (params->seq)
1755 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1756 params->seq_len, params->seq);
1757
1758 if (params->cipher)
1759 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1760 params->cipher);
1761
1762 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1763
1764 nla_nest_end(cookie->msg, key);
1765
Johannes Berg41ade002007-12-19 02:03:29 +01001766 return;
1767 nla_put_failure:
1768 cookie->error = 1;
1769}
1770
1771static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1772{
Johannes Berg4c476992010-10-04 21:36:35 +02001773 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001774 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001775 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001776 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001777 const u8 *mac_addr = NULL;
1778 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001779 struct get_key_cookie cookie = {
1780 .error = 0,
1781 };
1782 void *hdr;
1783 struct sk_buff *msg;
1784
1785 if (info->attrs[NL80211_ATTR_KEY_IDX])
1786 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1787
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001788 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001789 return -EINVAL;
1790
1791 if (info->attrs[NL80211_ATTR_MAC])
1792 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1793
Johannes Berge31b8212010-10-05 19:39:30 +02001794 pairwise = !!mac_addr;
1795 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1796 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1797 if (kt >= NUM_NL80211_KEYTYPES)
1798 return -EINVAL;
1799 if (kt != NL80211_KEYTYPE_GROUP &&
1800 kt != NL80211_KEYTYPE_PAIRWISE)
1801 return -EINVAL;
1802 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1803 }
1804
Johannes Berg4c476992010-10-04 21:36:35 +02001805 if (!rdev->ops->get_key)
1806 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001807
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001808 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001809 if (!msg)
1810 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001811
1812 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1813 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001814 if (IS_ERR(hdr))
1815 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001816
1817 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001818 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001819
1820 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1821 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1822 if (mac_addr)
1823 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1824
Johannes Berge31b8212010-10-05 19:39:30 +02001825 if (pairwise && mac_addr &&
1826 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1827 return -ENOENT;
1828
1829 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1830 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001831
1832 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001833 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001834
1835 if (cookie.error)
1836 goto nla_put_failure;
1837
1838 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001839 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001840
1841 nla_put_failure:
1842 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001843 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001844 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001845 return err;
1846}
1847
1848static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1849{
Johannes Berg4c476992010-10-04 21:36:35 +02001850 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001851 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001852 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001853 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001854
Johannes Bergb9454e82009-07-08 13:29:08 +02001855 err = nl80211_parse_key(info, &key);
1856 if (err)
1857 return err;
1858
1859 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001860 return -EINVAL;
1861
Johannes Bergb9454e82009-07-08 13:29:08 +02001862 /* only support setting default key */
1863 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001864 return -EINVAL;
1865
Johannes Bergfffd0932009-07-08 14:22:54 +02001866 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001867
1868 if (key.def) {
1869 if (!rdev->ops->set_default_key) {
1870 err = -EOPNOTSUPP;
1871 goto out;
1872 }
1873
1874 err = nl80211_key_allowed(dev->ieee80211_ptr);
1875 if (err)
1876 goto out;
1877
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001878 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1879 key.def_uni, key.def_multi);
1880
1881 if (err)
1882 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001883
Johannes Berg3d23e342009-09-29 23:27:28 +02001884#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001885 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001886#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001887 } else {
1888 if (key.def_uni || !key.def_multi) {
1889 err = -EINVAL;
1890 goto out;
1891 }
1892
1893 if (!rdev->ops->set_default_mgmt_key) {
1894 err = -EOPNOTSUPP;
1895 goto out;
1896 }
1897
1898 err = nl80211_key_allowed(dev->ieee80211_ptr);
1899 if (err)
1900 goto out;
1901
1902 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1903 dev, key.idx);
1904 if (err)
1905 goto out;
1906
1907#ifdef CONFIG_CFG80211_WEXT
1908 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1909#endif
1910 }
1911
1912 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001913 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001914
Johannes Berg41ade002007-12-19 02:03:29 +01001915 return err;
1916}
1917
1918static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1919{
Johannes Berg4c476992010-10-04 21:36:35 +02001920 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001921 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001922 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001923 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001924 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001925
Johannes Bergb9454e82009-07-08 13:29:08 +02001926 err = nl80211_parse_key(info, &key);
1927 if (err)
1928 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001929
Johannes Bergb9454e82009-07-08 13:29:08 +02001930 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001931 return -EINVAL;
1932
Johannes Berg41ade002007-12-19 02:03:29 +01001933 if (info->attrs[NL80211_ATTR_MAC])
1934 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1935
Johannes Berge31b8212010-10-05 19:39:30 +02001936 if (key.type == -1) {
1937 if (mac_addr)
1938 key.type = NL80211_KEYTYPE_PAIRWISE;
1939 else
1940 key.type = NL80211_KEYTYPE_GROUP;
1941 }
1942
1943 /* for now */
1944 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1945 key.type != NL80211_KEYTYPE_GROUP)
1946 return -EINVAL;
1947
Johannes Berg4c476992010-10-04 21:36:35 +02001948 if (!rdev->ops->add_key)
1949 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001950
Johannes Berge31b8212010-10-05 19:39:30 +02001951 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1952 key.type == NL80211_KEYTYPE_PAIRWISE,
1953 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001954 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001955
1956 wdev_lock(dev->ieee80211_ptr);
1957 err = nl80211_key_allowed(dev->ieee80211_ptr);
1958 if (!err)
1959 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001960 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001961 mac_addr, &key.p);
1962 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001963
Johannes Berg41ade002007-12-19 02:03:29 +01001964 return err;
1965}
1966
1967static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1968{
Johannes Berg4c476992010-10-04 21:36:35 +02001969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001970 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001971 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001972 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001973 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001974
Johannes Bergb9454e82009-07-08 13:29:08 +02001975 err = nl80211_parse_key(info, &key);
1976 if (err)
1977 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001978
1979 if (info->attrs[NL80211_ATTR_MAC])
1980 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1981
Johannes Berge31b8212010-10-05 19:39:30 +02001982 if (key.type == -1) {
1983 if (mac_addr)
1984 key.type = NL80211_KEYTYPE_PAIRWISE;
1985 else
1986 key.type = NL80211_KEYTYPE_GROUP;
1987 }
1988
1989 /* for now */
1990 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1991 key.type != NL80211_KEYTYPE_GROUP)
1992 return -EINVAL;
1993
Johannes Berg4c476992010-10-04 21:36:35 +02001994 if (!rdev->ops->del_key)
1995 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001996
Johannes Bergfffd0932009-07-08 14:22:54 +02001997 wdev_lock(dev->ieee80211_ptr);
1998 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001999
2000 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2001 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2002 err = -ENOENT;
2003
Johannes Bergfffd0932009-07-08 14:22:54 +02002004 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002005 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2006 key.type == NL80211_KEYTYPE_PAIRWISE,
2007 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002008
Johannes Berg3d23e342009-09-29 23:27:28 +02002009#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002010 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002011 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002012 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002013 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002014 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2015 }
2016#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002017 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002018
Johannes Berg41ade002007-12-19 02:03:29 +01002019 return err;
2020}
2021
Johannes Berged1b6cc2007-12-19 02:03:32 +01002022static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2023{
2024 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2025 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2027 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002028 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002029 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002030 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002031
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002032 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2033 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2034 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2035 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002036 return -EINVAL;
2037
Johannes Berg074ac8d2010-09-16 14:58:22 +02002038 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002039 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2040 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002041
Johannes Berg56d18932011-05-09 18:41:15 +02002042 memset(&params, 0, sizeof(params));
2043
Johannes Berged1b6cc2007-12-19 02:03:32 +01002044 switch (info->genlhdr->cmd) {
2045 case NL80211_CMD_NEW_BEACON:
2046 /* these are required for NEW_BEACON */
2047 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2048 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002049 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2050 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002051
Johannes Berg56d18932011-05-09 18:41:15 +02002052 params.interval =
2053 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2054 params.dtim_period =
2055 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2056
2057 err = cfg80211_validate_beacon_int(rdev, params.interval);
2058 if (err)
2059 return err;
2060
Jouni Malinen32e9de82011-08-10 23:53:31 +03002061 /*
2062 * In theory, some of these attributes could be required for
2063 * NEW_BEACON, but since they were not used when the command was
2064 * originally added, keep them optional for old user space
2065 * programs to work with drivers that do not need the additional
2066 * information.
2067 */
2068 if (info->attrs[NL80211_ATTR_SSID]) {
2069 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2070 params.ssid_len =
2071 nla_len(info->attrs[NL80211_ATTR_SSID]);
2072 if (params.ssid_len == 0 ||
2073 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2074 return -EINVAL;
2075 }
2076
2077 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2078 params.hidden_ssid = nla_get_u32(
2079 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2080 if (params.hidden_ssid !=
2081 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2082 params.hidden_ssid !=
2083 NL80211_HIDDEN_SSID_ZERO_LEN &&
2084 params.hidden_ssid !=
2085 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2086 return -EINVAL;
2087 }
2088
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002089 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2090
2091 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2092 params.auth_type = nla_get_u32(
2093 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2094 if (!nl80211_valid_auth_type(params.auth_type))
2095 return -EINVAL;
2096 } else
2097 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2098
2099 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2100 NL80211_MAX_NR_CIPHER_SUITES);
2101 if (err)
2102 return err;
2103
Johannes Berg79c97e92009-07-07 03:56:12 +02002104 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002105 break;
2106 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002107 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002108 break;
2109 default:
2110 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002111 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002112 }
2113
Johannes Berg4c476992010-10-04 21:36:35 +02002114 if (!call)
2115 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002116
Johannes Berged1b6cc2007-12-19 02:03:32 +01002117 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2118 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2119 params.head_len =
2120 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2121 haveinfo = 1;
2122 }
2123
2124 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2125 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2126 params.tail_len =
2127 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2128 haveinfo = 1;
2129 }
2130
Johannes Berg4c476992010-10-04 21:36:35 +02002131 if (!haveinfo)
2132 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002133
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002134 if (info->attrs[NL80211_ATTR_IE]) {
2135 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2136 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2137 }
2138
2139 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2140 params.proberesp_ies =
2141 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2142 params.proberesp_ies_len =
2143 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2144 }
2145
2146 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2147 params.assocresp_ies =
2148 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2149 params.assocresp_ies_len =
2150 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2151 }
2152
Johannes Berg56d18932011-05-09 18:41:15 +02002153 err = call(&rdev->wiphy, dev, &params);
2154 if (!err && params.interval)
2155 wdev->beacon_interval = params.interval;
2156 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002157}
2158
2159static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2160{
Johannes Berg4c476992010-10-04 21:36:35 +02002161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2162 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002163 struct wireless_dev *wdev = dev->ieee80211_ptr;
2164 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002165
Johannes Berg4c476992010-10-04 21:36:35 +02002166 if (!rdev->ops->del_beacon)
2167 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002168
Johannes Berg074ac8d2010-09-16 14:58:22 +02002169 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002170 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2171 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002172
Johannes Berg56d18932011-05-09 18:41:15 +02002173 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2174 if (!err)
2175 wdev->beacon_interval = 0;
2176 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002177}
2178
Johannes Berg5727ef12007-12-19 02:03:34 +01002179static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2180 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2181 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2182 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002183 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002184 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002185};
2186
Johannes Bergeccb8e82009-05-11 21:57:56 +03002187static int parse_station_flags(struct genl_info *info,
2188 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002189{
2190 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002191 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002192 int flag;
2193
Johannes Bergeccb8e82009-05-11 21:57:56 +03002194 /*
2195 * Try parsing the new attribute first so userspace
2196 * can specify both for older kernels.
2197 */
2198 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2199 if (nla) {
2200 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002201
Johannes Bergeccb8e82009-05-11 21:57:56 +03002202 sta_flags = nla_data(nla);
2203 params->sta_flags_mask = sta_flags->mask;
2204 params->sta_flags_set = sta_flags->set;
2205 if ((params->sta_flags_mask |
2206 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2207 return -EINVAL;
2208 return 0;
2209 }
2210
2211 /* if present, parse the old attribute */
2212
2213 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002214 if (!nla)
2215 return 0;
2216
2217 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2218 nla, sta_flags_policy))
2219 return -EINVAL;
2220
Johannes Bergeccb8e82009-05-11 21:57:56 +03002221 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2222 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002223
2224 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2225 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002226 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002227
2228 return 0;
2229}
2230
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002231static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2232 int attr)
2233{
2234 struct nlattr *rate;
2235 u16 bitrate;
2236
2237 rate = nla_nest_start(msg, attr);
2238 if (!rate)
2239 goto nla_put_failure;
2240
2241 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2242 bitrate = cfg80211_calculate_bitrate(info);
2243 if (bitrate > 0)
2244 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2245
2246 if (info->flags & RATE_INFO_FLAGS_MCS)
2247 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2248 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2249 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2250 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2251 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2252
2253 nla_nest_end(msg, rate);
2254 return true;
2255
2256nla_put_failure:
2257 return false;
2258}
2259
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002260static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2261 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002262 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002263{
2264 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002265 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002266
2267 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2268 if (!hdr)
2269 return -1;
2270
2271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2272 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2273
Johannes Bergf5ea9122009-08-07 16:17:38 +02002274 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2275
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002276 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2277 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002278 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302279 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2280 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2281 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002282 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2283 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2284 sinfo->inactive_time);
2285 if (sinfo->filled & STATION_INFO_RX_BYTES)
2286 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2287 sinfo->rx_bytes);
2288 if (sinfo->filled & STATION_INFO_TX_BYTES)
2289 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2290 sinfo->tx_bytes);
2291 if (sinfo->filled & STATION_INFO_LLID)
2292 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2293 sinfo->llid);
2294 if (sinfo->filled & STATION_INFO_PLID)
2295 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2296 sinfo->plid);
2297 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2298 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2299 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002300 if (sinfo->filled & STATION_INFO_SIGNAL)
2301 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2302 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002303 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2304 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2305 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002306 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002307 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2308 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002309 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002310 }
2311 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2312 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2313 NL80211_STA_INFO_RX_BITRATE))
2314 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002315 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002316 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2317 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2318 sinfo->rx_packets);
2319 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2320 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2321 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002322 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2323 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2324 sinfo->tx_retries);
2325 if (sinfo->filled & STATION_INFO_TX_FAILED)
2326 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2327 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002328 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2329 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2330 if (!bss_param)
2331 goto nla_put_failure;
2332
2333 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2334 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2335 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2336 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2337 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2338 NLA_PUT_FLAG(msg,
2339 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2340 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2341 sinfo->bss_param.dtim_period);
2342 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2343 sinfo->bss_param.beacon_interval);
2344
2345 nla_nest_end(msg, bss_param);
2346 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002348
Felix Fietkau040bdf72011-08-10 19:00:33 -06002349 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002350 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2351 sinfo->assoc_req_ies);
2352
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002353 return genlmsg_end(msg, hdr);
2354
2355 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002356 genlmsg_cancel(msg, hdr);
2357 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002358}
2359
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002360static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002361 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002362{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002363 struct station_info sinfo;
2364 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002365 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002367 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002368 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369
Johannes Berg67748892010-10-04 21:14:06 +02002370 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2371 if (err)
2372 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002373
2374 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002375 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002376 goto out_err;
2377 }
2378
Johannes Bergbba95fe2008-07-29 13:22:51 +02002379 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002380 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002381 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2382 mac_addr, &sinfo);
2383 if (err == -ENOENT)
2384 break;
2385 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002386 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002387
2388 if (nl80211_send_station(skb,
2389 NETLINK_CB(cb->skb).pid,
2390 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2391 netdev, mac_addr,
2392 &sinfo) < 0)
2393 goto out;
2394
2395 sta_idx++;
2396 }
2397
2398
2399 out:
2400 cb->args[1] = sta_idx;
2401 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002402 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002403 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002404
2405 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002406}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002407
Johannes Berg5727ef12007-12-19 02:03:34 +01002408static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2409{
Johannes Berg4c476992010-10-04 21:36:35 +02002410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2411 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002412 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002413 struct sk_buff *msg;
2414 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002415 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002416
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002417 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002418
2419 if (!info->attrs[NL80211_ATTR_MAC])
2420 return -EINVAL;
2421
2422 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2423
Johannes Berg4c476992010-10-04 21:36:35 +02002424 if (!rdev->ops->get_station)
2425 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002426
Johannes Berg79c97e92009-07-07 03:56:12 +02002427 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002428 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002429 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002430
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002431 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002432 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002433 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002434
2435 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002436 dev, mac_addr, &sinfo) < 0) {
2437 nlmsg_free(msg);
2438 return -ENOBUFS;
2439 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002440
Johannes Berg4c476992010-10-04 21:36:35 +02002441 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002442}
2443
2444/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002445 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002446 */
Johannes Berg463d0182009-07-14 00:33:35 +02002447static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002448 struct cfg80211_registered_device *rdev,
2449 struct net_device **vlan)
2450{
Johannes Berg463d0182009-07-14 00:33:35 +02002451 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002452 *vlan = NULL;
2453
2454 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002455 *vlan = dev_get_by_index(genl_info_net(info),
2456 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002457 if (!*vlan)
2458 return -ENODEV;
2459 if (!(*vlan)->ieee80211_ptr)
2460 return -EINVAL;
2461 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2462 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002463 if (!netif_running(*vlan))
2464 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002465 }
2466 return 0;
2467}
2468
2469static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2470{
Johannes Berg4c476992010-10-04 21:36:35 +02002471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002472 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002473 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002474 struct station_parameters params;
2475 u8 *mac_addr = NULL;
2476
2477 memset(&params, 0, sizeof(params));
2478
2479 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002480 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002481
2482 if (info->attrs[NL80211_ATTR_STA_AID])
2483 return -EINVAL;
2484
2485 if (!info->attrs[NL80211_ATTR_MAC])
2486 return -EINVAL;
2487
2488 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2489
2490 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2491 params.supported_rates =
2492 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2493 params.supported_rates_len =
2494 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2495 }
2496
2497 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2498 params.listen_interval =
2499 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2500
Jouni Malinen36aedc92008-08-25 11:58:58 +03002501 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2502 params.ht_capa =
2503 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2504
Johannes Bergeccb8e82009-05-11 21:57:56 +03002505 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002506 return -EINVAL;
2507
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002508 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2509 params.plink_action =
2510 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2511
Javier Cardona9c3990a2011-05-03 16:57:11 -07002512 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2513 params.plink_state =
2514 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2515
Johannes Berg463d0182009-07-14 00:33:35 +02002516 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002517 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002518 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002519
2520 /* validate settings */
2521 err = 0;
2522
2523 switch (dev->ieee80211_ptr->iftype) {
2524 case NL80211_IFTYPE_AP:
2525 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002526 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002527 /* disallow mesh-specific things */
2528 if (params.plink_action)
2529 err = -EINVAL;
2530 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002531 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002532 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002533 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002534 if (params.plink_action)
2535 err = -EINVAL;
2536 if (params.vlan)
2537 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002538 if (params.supported_rates &&
2539 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002540 err = -EINVAL;
2541 if (params.ht_capa)
2542 err = -EINVAL;
2543 if (params.listen_interval >= 0)
2544 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002545 if (params.sta_flags_mask &
2546 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2547 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2548 err = -EINVAL;
2549 /* can't change the TDLS bit */
2550 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2551 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002552 err = -EINVAL;
2553 break;
2554 case NL80211_IFTYPE_MESH_POINT:
2555 /* disallow things mesh doesn't support */
2556 if (params.vlan)
2557 err = -EINVAL;
2558 if (params.ht_capa)
2559 err = -EINVAL;
2560 if (params.listen_interval >= 0)
2561 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002562 if (params.sta_flags_mask &
2563 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002564 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002565 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002566 err = -EINVAL;
2567 break;
2568 default:
2569 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002570 }
2571
Johannes Berg5727ef12007-12-19 02:03:34 +01002572 if (err)
2573 goto out;
2574
Johannes Berg79c97e92009-07-07 03:56:12 +02002575 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002576 err = -EOPNOTSUPP;
2577 goto out;
2578 }
2579
Johannes Berg79c97e92009-07-07 03:56:12 +02002580 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002581
2582 out:
2583 if (params.vlan)
2584 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002585
Johannes Berg5727ef12007-12-19 02:03:34 +01002586 return err;
2587}
2588
Eliad Pellerc75786c2011-08-23 14:37:46 +03002589static struct nla_policy
2590nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2591 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2592 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2593};
2594
Johannes Berg5727ef12007-12-19 02:03:34 +01002595static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2596{
Johannes Berg4c476992010-10-04 21:36:35 +02002597 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002598 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002599 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002600 struct station_parameters params;
2601 u8 *mac_addr = NULL;
2602
2603 memset(&params, 0, sizeof(params));
2604
2605 if (!info->attrs[NL80211_ATTR_MAC])
2606 return -EINVAL;
2607
Johannes Berg5727ef12007-12-19 02:03:34 +01002608 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2609 return -EINVAL;
2610
2611 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2612 return -EINVAL;
2613
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002614 if (!info->attrs[NL80211_ATTR_STA_AID])
2615 return -EINVAL;
2616
Johannes Berg5727ef12007-12-19 02:03:34 +01002617 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2618 params.supported_rates =
2619 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2620 params.supported_rates_len =
2621 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2622 params.listen_interval =
2623 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002624
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002625 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2626 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2627 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002628
Jouni Malinen36aedc92008-08-25 11:58:58 +03002629 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2630 params.ht_capa =
2631 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002632
Javier Cardona96b78df2011-04-07 15:08:33 -07002633 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2634 params.plink_action =
2635 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2636
Johannes Bergeccb8e82009-05-11 21:57:56 +03002637 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002638 return -EINVAL;
2639
Eliad Pellerc75786c2011-08-23 14:37:46 +03002640 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002641 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002642 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002643 info->attrs[NL80211_ATTR_STA_WME]) {
2644 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2645 struct nlattr *nla;
2646
2647 nla = info->attrs[NL80211_ATTR_STA_WME];
2648 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2649 nl80211_sta_wme_policy);
2650 if (err)
2651 return err;
2652
2653 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2654 params.uapsd_queues =
2655 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002656 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2657 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002658
2659 if (tb[NL80211_STA_WME_MAX_SP])
2660 params.max_sp =
2661 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002662
2663 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2664 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002665
2666 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002667 }
2668
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002669 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002670 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002671 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002672 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2673 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2674 return -EINVAL;
2675
2676 /*
2677 * Only managed stations can add TDLS peers, and only when the
2678 * wiphy supports external TDLS setup.
2679 */
2680 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2681 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2682 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2683 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002684 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002685
Johannes Berg463d0182009-07-14 00:33:35 +02002686 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002687 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002688 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002689
2690 /* validate settings */
2691 err = 0;
2692
Johannes Berg79c97e92009-07-07 03:56:12 +02002693 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002694 err = -EOPNOTSUPP;
2695 goto out;
2696 }
2697
Johannes Berg79c97e92009-07-07 03:56:12 +02002698 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002699
2700 out:
2701 if (params.vlan)
2702 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002703 return err;
2704}
2705
2706static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2707{
Johannes Berg4c476992010-10-04 21:36:35 +02002708 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2709 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002710 u8 *mac_addr = NULL;
2711
2712 if (info->attrs[NL80211_ATTR_MAC])
2713 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2714
Johannes Berge80cf852009-05-11 14:43:13 +02002715 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002716 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002717 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002718 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2719 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002720
Johannes Berg4c476992010-10-04 21:36:35 +02002721 if (!rdev->ops->del_station)
2722 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002723
Johannes Berg4c476992010-10-04 21:36:35 +02002724 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002725}
2726
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002727static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2728 int flags, struct net_device *dev,
2729 u8 *dst, u8 *next_hop,
2730 struct mpath_info *pinfo)
2731{
2732 void *hdr;
2733 struct nlattr *pinfoattr;
2734
2735 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2736 if (!hdr)
2737 return -1;
2738
2739 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2740 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2741 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2742
Johannes Bergf5ea9122009-08-07 16:17:38 +02002743 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2744
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002745 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2746 if (!pinfoattr)
2747 goto nla_put_failure;
2748 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2749 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2750 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002751 if (pinfo->filled & MPATH_INFO_SN)
2752 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2753 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002754 if (pinfo->filled & MPATH_INFO_METRIC)
2755 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2756 pinfo->metric);
2757 if (pinfo->filled & MPATH_INFO_EXPTIME)
2758 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2759 pinfo->exptime);
2760 if (pinfo->filled & MPATH_INFO_FLAGS)
2761 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2762 pinfo->flags);
2763 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2764 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2765 pinfo->discovery_timeout);
2766 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2767 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2768 pinfo->discovery_retries);
2769
2770 nla_nest_end(msg, pinfoattr);
2771
2772 return genlmsg_end(msg, hdr);
2773
2774 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002775 genlmsg_cancel(msg, hdr);
2776 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002777}
2778
2779static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002780 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002781{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002782 struct mpath_info pinfo;
2783 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002784 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002785 u8 dst[ETH_ALEN];
2786 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002787 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002788 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002789
Johannes Berg67748892010-10-04 21:14:06 +02002790 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2791 if (err)
2792 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002793
2794 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002795 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002796 goto out_err;
2797 }
2798
Jouni Malineneec60b02009-03-20 21:21:19 +02002799 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2800 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002801 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002802 }
2803
Johannes Bergbba95fe2008-07-29 13:22:51 +02002804 while (1) {
2805 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2806 dst, next_hop, &pinfo);
2807 if (err == -ENOENT)
2808 break;
2809 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002810 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002811
2812 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2813 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2814 netdev, dst, next_hop,
2815 &pinfo) < 0)
2816 goto out;
2817
2818 path_idx++;
2819 }
2820
2821
2822 out:
2823 cb->args[1] = path_idx;
2824 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002825 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002826 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002827 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002828}
2829
2830static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2831{
Johannes Berg4c476992010-10-04 21:36:35 +02002832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002833 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002834 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002835 struct mpath_info pinfo;
2836 struct sk_buff *msg;
2837 u8 *dst = NULL;
2838 u8 next_hop[ETH_ALEN];
2839
2840 memset(&pinfo, 0, sizeof(pinfo));
2841
2842 if (!info->attrs[NL80211_ATTR_MAC])
2843 return -EINVAL;
2844
2845 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2846
Johannes Berg4c476992010-10-04 21:36:35 +02002847 if (!rdev->ops->get_mpath)
2848 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002849
Johannes Berg4c476992010-10-04 21:36:35 +02002850 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2851 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002852
Johannes Berg79c97e92009-07-07 03:56:12 +02002853 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002854 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002855 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002856
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002857 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002858 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002859 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002860
2861 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002862 dev, dst, next_hop, &pinfo) < 0) {
2863 nlmsg_free(msg);
2864 return -ENOBUFS;
2865 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002866
Johannes Berg4c476992010-10-04 21:36:35 +02002867 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002868}
2869
2870static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2871{
Johannes Berg4c476992010-10-04 21:36:35 +02002872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2873 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002874 u8 *dst = NULL;
2875 u8 *next_hop = NULL;
2876
2877 if (!info->attrs[NL80211_ATTR_MAC])
2878 return -EINVAL;
2879
2880 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2881 return -EINVAL;
2882
2883 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2884 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2885
Johannes Berg4c476992010-10-04 21:36:35 +02002886 if (!rdev->ops->change_mpath)
2887 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002888
Johannes Berg4c476992010-10-04 21:36:35 +02002889 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2890 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002891
Johannes Berg4c476992010-10-04 21:36:35 +02002892 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002893}
Johannes Berg4c476992010-10-04 21:36:35 +02002894
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002895static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2896{
Johannes Berg4c476992010-10-04 21:36:35 +02002897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2898 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002899 u8 *dst = NULL;
2900 u8 *next_hop = NULL;
2901
2902 if (!info->attrs[NL80211_ATTR_MAC])
2903 return -EINVAL;
2904
2905 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2906 return -EINVAL;
2907
2908 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2909 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2910
Johannes Berg4c476992010-10-04 21:36:35 +02002911 if (!rdev->ops->add_mpath)
2912 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002913
Johannes Berg4c476992010-10-04 21:36:35 +02002914 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2915 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002916
Johannes Berg4c476992010-10-04 21:36:35 +02002917 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002918}
2919
2920static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2921{
Johannes Berg4c476992010-10-04 21:36:35 +02002922 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2923 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002924 u8 *dst = NULL;
2925
2926 if (info->attrs[NL80211_ATTR_MAC])
2927 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2928
Johannes Berg4c476992010-10-04 21:36:35 +02002929 if (!rdev->ops->del_mpath)
2930 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002931
Johannes Berg4c476992010-10-04 21:36:35 +02002932 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002933}
2934
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002935static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2936{
Johannes Berg4c476992010-10-04 21:36:35 +02002937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2938 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002939 struct bss_parameters params;
2940
2941 memset(&params, 0, sizeof(params));
2942 /* default to not changing parameters */
2943 params.use_cts_prot = -1;
2944 params.use_short_preamble = -1;
2945 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002946 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002947 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002948
2949 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2950 params.use_cts_prot =
2951 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2952 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2953 params.use_short_preamble =
2954 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2955 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2956 params.use_short_slot_time =
2957 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002958 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2959 params.basic_rates =
2960 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2961 params.basic_rates_len =
2962 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2963 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002964 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2965 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002966 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2967 params.ht_opmode =
2968 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002969
Johannes Berg4c476992010-10-04 21:36:35 +02002970 if (!rdev->ops->change_bss)
2971 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002972
Johannes Berg074ac8d2010-09-16 14:58:22 +02002973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2975 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002976
Johannes Berg4c476992010-10-04 21:36:35 +02002977 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002978}
2979
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002980static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002981 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2982 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2983 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2984 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2985 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2986 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2987};
2988
2989static int parse_reg_rule(struct nlattr *tb[],
2990 struct ieee80211_reg_rule *reg_rule)
2991{
2992 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2993 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2994
2995 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2996 return -EINVAL;
2997 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2998 return -EINVAL;
2999 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3000 return -EINVAL;
3001 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3002 return -EINVAL;
3003 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3004 return -EINVAL;
3005
3006 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3007
3008 freq_range->start_freq_khz =
3009 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3010 freq_range->end_freq_khz =
3011 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3012 freq_range->max_bandwidth_khz =
3013 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3014
3015 power_rule->max_eirp =
3016 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3017
3018 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3019 power_rule->max_antenna_gain =
3020 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3021
3022 return 0;
3023}
3024
3025static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3026{
3027 int r;
3028 char *data = NULL;
3029
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003030 /*
3031 * You should only get this when cfg80211 hasn't yet initialized
3032 * completely when built-in to the kernel right between the time
3033 * window between nl80211_init() and regulatory_init(), if that is
3034 * even possible.
3035 */
3036 mutex_lock(&cfg80211_mutex);
3037 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003038 mutex_unlock(&cfg80211_mutex);
3039 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003040 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003041 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003042
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003043 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3044 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003045
3046 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3047
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003048 r = regulatory_hint_user(data);
3049
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003050 return r;
3051}
3052
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003053static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003054 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003055{
Johannes Berg4c476992010-10-04 21:36:35 +02003056 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003057 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003058 struct wireless_dev *wdev = dev->ieee80211_ptr;
3059 struct mesh_config cur_params;
3060 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003061 void *hdr;
3062 struct nlattr *pinfoattr;
3063 struct sk_buff *msg;
3064
Johannes Berg29cbe682010-12-03 09:20:44 +01003065 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3066 return -EOPNOTSUPP;
3067
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003068 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003069 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003070
Johannes Berg29cbe682010-12-03 09:20:44 +01003071 wdev_lock(wdev);
3072 /* If not connected, get default parameters */
3073 if (!wdev->mesh_id_len)
3074 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3075 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003076 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003077 &cur_params);
3078 wdev_unlock(wdev);
3079
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003080 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003081 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003082
3083 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003084 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003085 if (!msg)
3086 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003087 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003088 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003089 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003090 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003091 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003092 if (!pinfoattr)
3093 goto nla_put_failure;
3094 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3095 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3096 cur_params.dot11MeshRetryTimeout);
3097 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3098 cur_params.dot11MeshConfirmTimeout);
3099 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3100 cur_params.dot11MeshHoldingTimeout);
3101 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3102 cur_params.dot11MeshMaxPeerLinks);
3103 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3104 cur_params.dot11MeshMaxRetries);
3105 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3106 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003107 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3108 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003109 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3110 cur_params.auto_open_plinks);
3111 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3112 cur_params.dot11MeshHWMPmaxPREQretries);
3113 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3114 cur_params.path_refresh_time);
3115 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3116 cur_params.min_discovery_timeout);
3117 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3118 cur_params.dot11MeshHWMPactivePathTimeout);
3119 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3120 cur_params.dot11MeshHWMPpreqMinInterval);
3121 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3122 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003123 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3124 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003125 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3126 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003127 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3128 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003129 nla_nest_end(msg, pinfoattr);
3130 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003131 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003132
Johannes Berg3b858752009-03-12 09:55:09 +01003133 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003134 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003135 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003136 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003137 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003138}
3139
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003140static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003141 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3142 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3143 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3144 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3145 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3146 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003147 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003148 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3149
3150 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3151 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3152 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3153 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3154 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3155 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003156 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003157 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003158 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003159};
3160
Javier Cardonac80d5452010-12-16 17:37:49 -08003161static const struct nla_policy
3162 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3163 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3164 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003165 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003166 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003167 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003168 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003169};
3170
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003171static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003172 struct mesh_config *cfg,
3173 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003174{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003175 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003176 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003177
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003178#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3179do {\
3180 if (table[attr_num]) {\
3181 cfg->param = nla_fn(table[attr_num]); \
3182 mask |= (1 << (attr_num - 1)); \
3183 } \
3184} while (0);\
3185
3186
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003187 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003188 return -EINVAL;
3189 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003190 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003191 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003192 return -EINVAL;
3193
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003194 /* This makes sure that there aren't more than 32 mesh config
3195 * parameters (otherwise our bitfield scheme would not work.) */
3196 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3197
3198 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003199 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3200 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3201 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3202 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3203 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3204 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3205 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3206 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3207 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3208 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3209 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3210 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003211 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3212 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003213 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3214 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3215 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3216 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3217 nla_get_u8);
3218 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3219 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3220 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3221 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3222 nla_get_u16);
3223 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3224 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3225 nla_get_u32);
3226 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3227 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3228 nla_get_u16);
3229 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3230 dot11MeshHWMPnetDiameterTraversalTime,
3231 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3232 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003233 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3234 dot11MeshHWMPRootMode, mask,
3235 NL80211_MESHCONF_HWMP_ROOTMODE,
3236 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003237 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3238 dot11MeshHWMPRannInterval, mask,
3239 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3240 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003241 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3242 dot11MeshGateAnnouncementProtocol, mask,
3243 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3244 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003245 if (mask_out)
3246 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003247
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003248 return 0;
3249
3250#undef FILL_IN_MESH_PARAM_IF_SET
3251}
3252
Javier Cardonac80d5452010-12-16 17:37:49 -08003253static int nl80211_parse_mesh_setup(struct genl_info *info,
3254 struct mesh_setup *setup)
3255{
3256 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3257
3258 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3259 return -EINVAL;
3260 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3261 info->attrs[NL80211_ATTR_MESH_SETUP],
3262 nl80211_mesh_setup_params_policy))
3263 return -EINVAL;
3264
3265 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3266 setup->path_sel_proto =
3267 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3268 IEEE80211_PATH_PROTOCOL_VENDOR :
3269 IEEE80211_PATH_PROTOCOL_HWMP;
3270
3271 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3272 setup->path_metric =
3273 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3274 IEEE80211_PATH_METRIC_VENDOR :
3275 IEEE80211_PATH_METRIC_AIRTIME;
3276
Javier Cardona581a8b02011-04-07 15:08:27 -07003277
3278 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003279 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003280 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003281 if (!is_valid_ie_attr(ieattr))
3282 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003283 setup->ie = nla_data(ieattr);
3284 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003285 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003286 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3287 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003288
3289 return 0;
3290}
3291
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003292static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003293 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003294{
3295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3296 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003297 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003298 struct mesh_config cfg;
3299 u32 mask;
3300 int err;
3301
Johannes Berg29cbe682010-12-03 09:20:44 +01003302 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3303 return -EOPNOTSUPP;
3304
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003305 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003306 return -EOPNOTSUPP;
3307
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003308 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003309 if (err)
3310 return err;
3311
Johannes Berg29cbe682010-12-03 09:20:44 +01003312 wdev_lock(wdev);
3313 if (!wdev->mesh_id_len)
3314 err = -ENOLINK;
3315
3316 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003317 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003318 mask, &cfg);
3319
3320 wdev_unlock(wdev);
3321
3322 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003323}
3324
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003325static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3326{
3327 struct sk_buff *msg;
3328 void *hdr = NULL;
3329 struct nlattr *nl_reg_rules;
3330 unsigned int i;
3331 int err = -EINVAL;
3332
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003333 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003334
3335 if (!cfg80211_regdomain)
3336 goto out;
3337
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003338 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003339 if (!msg) {
3340 err = -ENOBUFS;
3341 goto out;
3342 }
3343
3344 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3345 NL80211_CMD_GET_REG);
3346 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003347 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003348
3349 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3350 cfg80211_regdomain->alpha2);
3351
3352 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3353 if (!nl_reg_rules)
3354 goto nla_put_failure;
3355
3356 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3357 struct nlattr *nl_reg_rule;
3358 const struct ieee80211_reg_rule *reg_rule;
3359 const struct ieee80211_freq_range *freq_range;
3360 const struct ieee80211_power_rule *power_rule;
3361
3362 reg_rule = &cfg80211_regdomain->reg_rules[i];
3363 freq_range = &reg_rule->freq_range;
3364 power_rule = &reg_rule->power_rule;
3365
3366 nl_reg_rule = nla_nest_start(msg, i);
3367 if (!nl_reg_rule)
3368 goto nla_put_failure;
3369
3370 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3371 reg_rule->flags);
3372 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3373 freq_range->start_freq_khz);
3374 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3375 freq_range->end_freq_khz);
3376 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3377 freq_range->max_bandwidth_khz);
3378 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3379 power_rule->max_antenna_gain);
3380 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3381 power_rule->max_eirp);
3382
3383 nla_nest_end(msg, nl_reg_rule);
3384 }
3385
3386 nla_nest_end(msg, nl_reg_rules);
3387
3388 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003389 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003390 goto out;
3391
3392nla_put_failure:
3393 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003394put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003395 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003396 err = -EMSGSIZE;
3397out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003398 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003399 return err;
3400}
3401
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003402static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3403{
3404 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3405 struct nlattr *nl_reg_rule;
3406 char *alpha2 = NULL;
3407 int rem_reg_rules = 0, r = 0;
3408 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3409 struct ieee80211_regdomain *rd = NULL;
3410
3411 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3412 return -EINVAL;
3413
3414 if (!info->attrs[NL80211_ATTR_REG_RULES])
3415 return -EINVAL;
3416
3417 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3418
3419 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3420 rem_reg_rules) {
3421 num_rules++;
3422 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003423 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003424 }
3425
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003426 mutex_lock(&cfg80211_mutex);
3427
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003428 if (!reg_is_valid_request(alpha2)) {
3429 r = -EINVAL;
3430 goto bad_reg;
3431 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003432
3433 size_of_regd = sizeof(struct ieee80211_regdomain) +
3434 (num_rules * sizeof(struct ieee80211_reg_rule));
3435
3436 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003437 if (!rd) {
3438 r = -ENOMEM;
3439 goto bad_reg;
3440 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003441
3442 rd->n_reg_rules = num_rules;
3443 rd->alpha2[0] = alpha2[0];
3444 rd->alpha2[1] = alpha2[1];
3445
3446 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3447 rem_reg_rules) {
3448 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3449 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3450 reg_rule_policy);
3451 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3452 if (r)
3453 goto bad_reg;
3454
3455 rule_idx++;
3456
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003457 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3458 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003459 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003460 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003461 }
3462
3463 BUG_ON(rule_idx != num_rules);
3464
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003465 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003466
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003467 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003468
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003469 return r;
3470
Johannes Bergd2372b32008-10-24 20:32:20 +02003471 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003472 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003473 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003474 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003475}
3476
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003477static int validate_scan_freqs(struct nlattr *freqs)
3478{
3479 struct nlattr *attr1, *attr2;
3480 int n_channels = 0, tmp1, tmp2;
3481
3482 nla_for_each_nested(attr1, freqs, tmp1) {
3483 n_channels++;
3484 /*
3485 * Some hardware has a limited channel list for
3486 * scanning, and it is pretty much nonsensical
3487 * to scan for a channel twice, so disallow that
3488 * and don't require drivers to check that the
3489 * channel list they get isn't longer than what
3490 * they can scan, as long as they can scan all
3491 * the channels they registered at once.
3492 */
3493 nla_for_each_nested(attr2, freqs, tmp2)
3494 if (attr1 != attr2 &&
3495 nla_get_u32(attr1) == nla_get_u32(attr2))
3496 return 0;
3497 }
3498
3499 return n_channels;
3500}
3501
Johannes Berg2a519312009-02-10 21:25:55 +01003502static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3503{
Johannes Berg4c476992010-10-04 21:36:35 +02003504 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3505 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003506 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003507 struct nlattr *attr;
3508 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003509 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003510 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003511
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003512 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3513 return -EINVAL;
3514
Johannes Berg79c97e92009-07-07 03:56:12 +02003515 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003516
Johannes Berg4c476992010-10-04 21:36:35 +02003517 if (!rdev->ops->scan)
3518 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003519
Johannes Berg4c476992010-10-04 21:36:35 +02003520 if (rdev->scan_req)
3521 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003522
3523 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003524 n_channels = validate_scan_freqs(
3525 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003526 if (!n_channels)
3527 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003528 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003529 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003530 n_channels = 0;
3531
Johannes Berg2a519312009-02-10 21:25:55 +01003532 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3533 if (wiphy->bands[band])
3534 n_channels += wiphy->bands[band]->n_channels;
3535 }
3536
3537 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3538 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3539 n_ssids++;
3540
Johannes Berg4c476992010-10-04 21:36:35 +02003541 if (n_ssids > wiphy->max_scan_ssids)
3542 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003543
Jouni Malinen70692ad2009-02-16 19:39:13 +02003544 if (info->attrs[NL80211_ATTR_IE])
3545 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3546 else
3547 ie_len = 0;
3548
Johannes Berg4c476992010-10-04 21:36:35 +02003549 if (ie_len > wiphy->max_scan_ie_len)
3550 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003551
Johannes Berg2a519312009-02-10 21:25:55 +01003552 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003553 + sizeof(*request->ssids) * n_ssids
3554 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003555 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003556 if (!request)
3557 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003558
Johannes Berg2a519312009-02-10 21:25:55 +01003559 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003560 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003561 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003562 if (ie_len) {
3563 if (request->ssids)
3564 request->ie = (void *)(request->ssids + n_ssids);
3565 else
3566 request->ie = (void *)(request->channels + n_channels);
3567 }
Johannes Berg2a519312009-02-10 21:25:55 +01003568
Johannes Berg584991d2009-11-02 13:32:03 +01003569 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003570 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3571 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003572 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003573 struct ieee80211_channel *chan;
3574
3575 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3576
3577 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003578 err = -EINVAL;
3579 goto out_free;
3580 }
Johannes Berg584991d2009-11-02 13:32:03 +01003581
3582 /* ignore disabled channels */
3583 if (chan->flags & IEEE80211_CHAN_DISABLED)
3584 continue;
3585
3586 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003587 i++;
3588 }
3589 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003590 enum ieee80211_band band;
3591
Johannes Berg2a519312009-02-10 21:25:55 +01003592 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003593 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3594 int j;
3595 if (!wiphy->bands[band])
3596 continue;
3597 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003598 struct ieee80211_channel *chan;
3599
3600 chan = &wiphy->bands[band]->channels[j];
3601
3602 if (chan->flags & IEEE80211_CHAN_DISABLED)
3603 continue;
3604
3605 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003606 i++;
3607 }
3608 }
3609 }
3610
Johannes Berg584991d2009-11-02 13:32:03 +01003611 if (!i) {
3612 err = -EINVAL;
3613 goto out_free;
3614 }
3615
3616 request->n_channels = i;
3617
Johannes Berg2a519312009-02-10 21:25:55 +01003618 i = 0;
3619 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3620 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003621 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003622 err = -EINVAL;
3623 goto out_free;
3624 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003625 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003626 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003627 i++;
3628 }
3629 }
3630
Jouni Malinen70692ad2009-02-16 19:39:13 +02003631 if (info->attrs[NL80211_ATTR_IE]) {
3632 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003633 memcpy((void *)request->ie,
3634 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003635 request->ie_len);
3636 }
3637
Johannes Berg34850ab2011-07-18 18:08:35 +02003638 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003639 if (wiphy->bands[i])
3640 request->rates[i] =
3641 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003642
3643 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3644 nla_for_each_nested(attr,
3645 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3646 tmp) {
3647 enum ieee80211_band band = nla_type(attr);
3648
Dan Carpenter84404622011-07-29 11:52:18 +03003649 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003650 err = -EINVAL;
3651 goto out_free;
3652 }
3653 err = ieee80211_get_ratemask(wiphy->bands[band],
3654 nla_data(attr),
3655 nla_len(attr),
3656 &request->rates[band]);
3657 if (err)
3658 goto out_free;
3659 }
3660 }
3661
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303662 request->no_cck =
3663 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3664
Johannes Berg463d0182009-07-14 00:33:35 +02003665 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003666 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003667
Johannes Berg79c97e92009-07-07 03:56:12 +02003668 rdev->scan_req = request;
3669 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003670
Johannes Berg463d0182009-07-14 00:33:35 +02003671 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003672 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003673 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003674 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003675 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003676 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003677 kfree(request);
3678 }
Johannes Berg3b858752009-03-12 09:55:09 +01003679
Johannes Berg2a519312009-02-10 21:25:55 +01003680 return err;
3681}
3682
Luciano Coelho807f8a82011-05-11 17:09:35 +03003683static int nl80211_start_sched_scan(struct sk_buff *skb,
3684 struct genl_info *info)
3685{
3686 struct cfg80211_sched_scan_request *request;
3687 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3688 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003689 struct nlattr *attr;
3690 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003691 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003692 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003693 enum ieee80211_band band;
3694 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003695 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003696
3697 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3698 !rdev->ops->sched_scan_start)
3699 return -EOPNOTSUPP;
3700
3701 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3702 return -EINVAL;
3703
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003704 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3705 return -EINVAL;
3706
3707 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3708 if (interval == 0)
3709 return -EINVAL;
3710
Luciano Coelho807f8a82011-05-11 17:09:35 +03003711 wiphy = &rdev->wiphy;
3712
3713 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3714 n_channels = validate_scan_freqs(
3715 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3716 if (!n_channels)
3717 return -EINVAL;
3718 } else {
3719 n_channels = 0;
3720
3721 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3722 if (wiphy->bands[band])
3723 n_channels += wiphy->bands[band]->n_channels;
3724 }
3725
3726 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3727 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3728 tmp)
3729 n_ssids++;
3730
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003731 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003732 return -EINVAL;
3733
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003734 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3735 nla_for_each_nested(attr,
3736 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3737 tmp)
3738 n_match_sets++;
3739
3740 if (n_match_sets > wiphy->max_match_sets)
3741 return -EINVAL;
3742
Luciano Coelho807f8a82011-05-11 17:09:35 +03003743 if (info->attrs[NL80211_ATTR_IE])
3744 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3745 else
3746 ie_len = 0;
3747
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003748 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003749 return -EINVAL;
3750
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003751 mutex_lock(&rdev->sched_scan_mtx);
3752
3753 if (rdev->sched_scan_req) {
3754 err = -EINPROGRESS;
3755 goto out;
3756 }
3757
Luciano Coelho807f8a82011-05-11 17:09:35 +03003758 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003759 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003760 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003761 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003762 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003763 if (!request) {
3764 err = -ENOMEM;
3765 goto out;
3766 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003767
3768 if (n_ssids)
3769 request->ssids = (void *)&request->channels[n_channels];
3770 request->n_ssids = n_ssids;
3771 if (ie_len) {
3772 if (request->ssids)
3773 request->ie = (void *)(request->ssids + n_ssids);
3774 else
3775 request->ie = (void *)(request->channels + n_channels);
3776 }
3777
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003778 if (n_match_sets) {
3779 if (request->ie)
3780 request->match_sets = (void *)(request->ie + ie_len);
3781 else if (request->ssids)
3782 request->match_sets =
3783 (void *)(request->ssids + n_ssids);
3784 else
3785 request->match_sets =
3786 (void *)(request->channels + n_channels);
3787 }
3788 request->n_match_sets = n_match_sets;
3789
Luciano Coelho807f8a82011-05-11 17:09:35 +03003790 i = 0;
3791 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3792 /* user specified, bail out if channel not found */
3793 nla_for_each_nested(attr,
3794 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3795 tmp) {
3796 struct ieee80211_channel *chan;
3797
3798 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3799
3800 if (!chan) {
3801 err = -EINVAL;
3802 goto out_free;
3803 }
3804
3805 /* ignore disabled channels */
3806 if (chan->flags & IEEE80211_CHAN_DISABLED)
3807 continue;
3808
3809 request->channels[i] = chan;
3810 i++;
3811 }
3812 } else {
3813 /* all channels */
3814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3815 int j;
3816 if (!wiphy->bands[band])
3817 continue;
3818 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3819 struct ieee80211_channel *chan;
3820
3821 chan = &wiphy->bands[band]->channels[j];
3822
3823 if (chan->flags & IEEE80211_CHAN_DISABLED)
3824 continue;
3825
3826 request->channels[i] = chan;
3827 i++;
3828 }
3829 }
3830 }
3831
3832 if (!i) {
3833 err = -EINVAL;
3834 goto out_free;
3835 }
3836
3837 request->n_channels = i;
3838
3839 i = 0;
3840 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3841 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3842 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003843 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003844 err = -EINVAL;
3845 goto out_free;
3846 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003847 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003848 memcpy(request->ssids[i].ssid, nla_data(attr),
3849 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003850 i++;
3851 }
3852 }
3853
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003854 i = 0;
3855 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3856 nla_for_each_nested(attr,
3857 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3858 tmp) {
3859 struct nlattr *ssid;
3860
3861 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3862 nla_data(attr), nla_len(attr),
3863 nl80211_match_policy);
3864 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3865 if (ssid) {
3866 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3867 err = -EINVAL;
3868 goto out_free;
3869 }
3870 memcpy(request->match_sets[i].ssid.ssid,
3871 nla_data(ssid), nla_len(ssid));
3872 request->match_sets[i].ssid.ssid_len =
3873 nla_len(ssid);
3874 }
3875 i++;
3876 }
3877 }
3878
Luciano Coelho807f8a82011-05-11 17:09:35 +03003879 if (info->attrs[NL80211_ATTR_IE]) {
3880 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3881 memcpy((void *)request->ie,
3882 nla_data(info->attrs[NL80211_ATTR_IE]),
3883 request->ie_len);
3884 }
3885
3886 request->dev = dev;
3887 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003888 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003889
3890 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3891 if (!err) {
3892 rdev->sched_scan_req = request;
3893 nl80211_send_sched_scan(rdev, dev,
3894 NL80211_CMD_START_SCHED_SCAN);
3895 goto out;
3896 }
3897
3898out_free:
3899 kfree(request);
3900out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003901 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003902 return err;
3903}
3904
3905static int nl80211_stop_sched_scan(struct sk_buff *skb,
3906 struct genl_info *info)
3907{
3908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003909 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003910
3911 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3912 !rdev->ops->sched_scan_stop)
3913 return -EOPNOTSUPP;
3914
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003915 mutex_lock(&rdev->sched_scan_mtx);
3916 err = __cfg80211_stop_sched_scan(rdev, false);
3917 mutex_unlock(&rdev->sched_scan_mtx);
3918
3919 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003920}
3921
Johannes Berg9720bb32011-06-21 09:45:33 +02003922static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3923 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003924 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003925 struct wireless_dev *wdev,
3926 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003927{
Johannes Berg48ab9052009-07-10 18:42:31 +02003928 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003929 void *hdr;
3930 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003931 int i;
3932
3933 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003934
Johannes Berg9720bb32011-06-21 09:45:33 +02003935 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003936 NL80211_CMD_NEW_SCAN_RESULTS);
3937 if (!hdr)
3938 return -1;
3939
Johannes Berg9720bb32011-06-21 09:45:33 +02003940 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3941
Johannes Bergf5ea9122009-08-07 16:17:38 +02003942 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003943 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003944
3945 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3946 if (!bss)
3947 goto nla_put_failure;
3948 if (!is_zero_ether_addr(res->bssid))
3949 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3950 if (res->information_elements && res->len_information_elements)
3951 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3952 res->len_information_elements,
3953 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003954 if (res->beacon_ies && res->len_beacon_ies &&
3955 res->beacon_ies != res->information_elements)
3956 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3957 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003958 if (res->tsf)
3959 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3960 if (res->beacon_interval)
3961 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3962 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3963 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003964 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3965 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003966
Johannes Berg77965c92009-02-18 18:45:06 +01003967 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003968 case CFG80211_SIGNAL_TYPE_MBM:
3969 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3970 break;
3971 case CFG80211_SIGNAL_TYPE_UNSPEC:
3972 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3973 break;
3974 default:
3975 break;
3976 }
3977
Johannes Berg48ab9052009-07-10 18:42:31 +02003978 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003979 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003980 case NL80211_IFTYPE_STATION:
3981 if (intbss == wdev->current_bss)
3982 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3983 NL80211_BSS_STATUS_ASSOCIATED);
3984 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3985 if (intbss != wdev->auth_bsses[i])
3986 continue;
3987 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3988 NL80211_BSS_STATUS_AUTHENTICATED);
3989 break;
3990 }
3991 break;
3992 case NL80211_IFTYPE_ADHOC:
3993 if (intbss == wdev->current_bss)
3994 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3995 NL80211_BSS_STATUS_IBSS_JOINED);
3996 break;
3997 default:
3998 break;
3999 }
4000
Johannes Berg2a519312009-02-10 21:25:55 +01004001 nla_nest_end(msg, bss);
4002
4003 return genlmsg_end(msg, hdr);
4004
4005 nla_put_failure:
4006 genlmsg_cancel(msg, hdr);
4007 return -EMSGSIZE;
4008}
4009
4010static int nl80211_dump_scan(struct sk_buff *skb,
4011 struct netlink_callback *cb)
4012{
Johannes Berg48ab9052009-07-10 18:42:31 +02004013 struct cfg80211_registered_device *rdev;
4014 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004015 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004016 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004017 int start = cb->args[1], idx = 0;
4018 int err;
4019
Johannes Berg67748892010-10-04 21:14:06 +02004020 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4021 if (err)
4022 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004023
Johannes Berg48ab9052009-07-10 18:42:31 +02004024 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004025
Johannes Berg48ab9052009-07-10 18:42:31 +02004026 wdev_lock(wdev);
4027 spin_lock_bh(&rdev->bss_lock);
4028 cfg80211_bss_expire(rdev);
4029
Johannes Berg9720bb32011-06-21 09:45:33 +02004030 cb->seq = rdev->bss_generation;
4031
Johannes Berg48ab9052009-07-10 18:42:31 +02004032 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004033 if (++idx <= start)
4034 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004035 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004036 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004037 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004038 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004039 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004040 }
4041 }
4042
Johannes Berg48ab9052009-07-10 18:42:31 +02004043 spin_unlock_bh(&rdev->bss_lock);
4044 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004045
4046 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004047 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004048
Johannes Berg67748892010-10-04 21:14:06 +02004049 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004050}
4051
Holger Schurig61fa7132009-11-11 12:25:40 +01004052static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4053 int flags, struct net_device *dev,
4054 struct survey_info *survey)
4055{
4056 void *hdr;
4057 struct nlattr *infoattr;
4058
Holger Schurig61fa7132009-11-11 12:25:40 +01004059 hdr = nl80211hdr_put(msg, pid, seq, flags,
4060 NL80211_CMD_NEW_SURVEY_RESULTS);
4061 if (!hdr)
4062 return -ENOMEM;
4063
4064 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4065
4066 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4067 if (!infoattr)
4068 goto nla_put_failure;
4069
4070 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4071 survey->channel->center_freq);
4072 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4073 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4074 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004075 if (survey->filled & SURVEY_INFO_IN_USE)
4076 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004077 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4078 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4079 survey->channel_time);
4080 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4081 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4082 survey->channel_time_busy);
4083 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4084 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4085 survey->channel_time_ext_busy);
4086 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4087 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4088 survey->channel_time_rx);
4089 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4090 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4091 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004092
4093 nla_nest_end(msg, infoattr);
4094
4095 return genlmsg_end(msg, hdr);
4096
4097 nla_put_failure:
4098 genlmsg_cancel(msg, hdr);
4099 return -EMSGSIZE;
4100}
4101
4102static int nl80211_dump_survey(struct sk_buff *skb,
4103 struct netlink_callback *cb)
4104{
4105 struct survey_info survey;
4106 struct cfg80211_registered_device *dev;
4107 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004108 int survey_idx = cb->args[1];
4109 int res;
4110
Johannes Berg67748892010-10-04 21:14:06 +02004111 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4112 if (res)
4113 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004114
4115 if (!dev->ops->dump_survey) {
4116 res = -EOPNOTSUPP;
4117 goto out_err;
4118 }
4119
4120 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004121 struct ieee80211_channel *chan;
4122
Holger Schurig61fa7132009-11-11 12:25:40 +01004123 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4124 &survey);
4125 if (res == -ENOENT)
4126 break;
4127 if (res)
4128 goto out_err;
4129
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004130 /* Survey without a channel doesn't make sense */
4131 if (!survey.channel) {
4132 res = -EINVAL;
4133 goto out;
4134 }
4135
4136 chan = ieee80211_get_channel(&dev->wiphy,
4137 survey.channel->center_freq);
4138 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4139 survey_idx++;
4140 continue;
4141 }
4142
Holger Schurig61fa7132009-11-11 12:25:40 +01004143 if (nl80211_send_survey(skb,
4144 NETLINK_CB(cb->skb).pid,
4145 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4146 netdev,
4147 &survey) < 0)
4148 goto out;
4149 survey_idx++;
4150 }
4151
4152 out:
4153 cb->args[1] = survey_idx;
4154 res = skb->len;
4155 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004156 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004157 return res;
4158}
4159
Jouni Malinen255e7372009-03-20 21:21:17 +02004160static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4161{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004162 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004163}
4164
Samuel Ortizb23aa672009-07-01 21:26:54 +02004165static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4166{
4167 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4168 NL80211_WPA_VERSION_2));
4169}
4170
Jouni Malinen636a5d32009-03-19 13:39:22 +02004171static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4172{
Johannes Berg4c476992010-10-04 21:36:35 +02004173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4174 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004175 struct ieee80211_channel *chan;
4176 const u8 *bssid, *ssid, *ie = NULL;
4177 int err, ssid_len, ie_len = 0;
4178 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004179 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004180 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004181
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004182 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4183 return -EINVAL;
4184
4185 if (!info->attrs[NL80211_ATTR_MAC])
4186 return -EINVAL;
4187
Jouni Malinen17780922009-03-27 20:52:47 +02004188 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4189 return -EINVAL;
4190
Johannes Berg19957bb2009-07-02 17:20:43 +02004191 if (!info->attrs[NL80211_ATTR_SSID])
4192 return -EINVAL;
4193
4194 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4195 return -EINVAL;
4196
Johannes Bergfffd0932009-07-08 14:22:54 +02004197 err = nl80211_parse_key(info, &key);
4198 if (err)
4199 return err;
4200
4201 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004202 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4203 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004204 if (!key.p.key || !key.p.key_len)
4205 return -EINVAL;
4206 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4207 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4208 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4209 key.p.key_len != WLAN_KEY_LEN_WEP104))
4210 return -EINVAL;
4211 if (key.idx > 4)
4212 return -EINVAL;
4213 } else {
4214 key.p.key_len = 0;
4215 key.p.key = NULL;
4216 }
4217
Johannes Bergafea0b72010-08-10 09:46:42 +02004218 if (key.idx >= 0) {
4219 int i;
4220 bool ok = false;
4221 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4222 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4223 ok = true;
4224 break;
4225 }
4226 }
Johannes Berg4c476992010-10-04 21:36:35 +02004227 if (!ok)
4228 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004229 }
4230
Johannes Berg4c476992010-10-04 21:36:35 +02004231 if (!rdev->ops->auth)
4232 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004233
Johannes Berg074ac8d2010-09-16 14:58:22 +02004234 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004235 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4236 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004237
Johannes Berg19957bb2009-07-02 17:20:43 +02004238 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004239 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004240 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004241 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4242 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004243
Johannes Berg19957bb2009-07-02 17:20:43 +02004244 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4245 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4246
4247 if (info->attrs[NL80211_ATTR_IE]) {
4248 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4249 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4250 }
4251
4252 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004253 if (!nl80211_valid_auth_type(auth_type))
4254 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004255
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004256 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4257
Johannes Berg4c476992010-10-04 21:36:35 +02004258 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4259 ssid, ssid_len, ie, ie_len,
4260 key.p.key, key.p.key_len, key.idx,
4261 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004262}
4263
Johannes Bergc0692b82010-08-27 14:26:53 +03004264static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4265 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004266 struct cfg80211_crypto_settings *settings,
4267 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004268{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004269 memset(settings, 0, sizeof(*settings));
4270
Samuel Ortizb23aa672009-07-01 21:26:54 +02004271 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4272
Johannes Bergc0692b82010-08-27 14:26:53 +03004273 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4274 u16 proto;
4275 proto = nla_get_u16(
4276 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4277 settings->control_port_ethertype = cpu_to_be16(proto);
4278 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4279 proto != ETH_P_PAE)
4280 return -EINVAL;
4281 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4282 settings->control_port_no_encrypt = true;
4283 } else
4284 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4285
Samuel Ortizb23aa672009-07-01 21:26:54 +02004286 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4287 void *data;
4288 int len, i;
4289
4290 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4291 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4292 settings->n_ciphers_pairwise = len / sizeof(u32);
4293
4294 if (len % sizeof(u32))
4295 return -EINVAL;
4296
Johannes Berg3dc27d22009-07-02 21:36:37 +02004297 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004298 return -EINVAL;
4299
4300 memcpy(settings->ciphers_pairwise, data, len);
4301
4302 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004303 if (!cfg80211_supported_cipher_suite(
4304 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004305 settings->ciphers_pairwise[i]))
4306 return -EINVAL;
4307 }
4308
4309 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4310 settings->cipher_group =
4311 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004312 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4313 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004314 return -EINVAL;
4315 }
4316
4317 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4318 settings->wpa_versions =
4319 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4320 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4321 return -EINVAL;
4322 }
4323
4324 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4325 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004326 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004327
4328 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4329 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4330 settings->n_akm_suites = len / sizeof(u32);
4331
4332 if (len % sizeof(u32))
4333 return -EINVAL;
4334
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004335 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4336 return -EINVAL;
4337
Samuel Ortizb23aa672009-07-01 21:26:54 +02004338 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004339 }
4340
4341 return 0;
4342}
4343
Jouni Malinen636a5d32009-03-19 13:39:22 +02004344static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4345{
Johannes Berg4c476992010-10-04 21:36:35 +02004346 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4347 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004348 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004349 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004350 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004351 int err, ssid_len, ie_len = 0;
4352 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004353
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004354 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4355 return -EINVAL;
4356
4357 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004358 !info->attrs[NL80211_ATTR_SSID] ||
4359 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004360 return -EINVAL;
4361
Johannes Berg4c476992010-10-04 21:36:35 +02004362 if (!rdev->ops->assoc)
4363 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004364
Johannes Berg074ac8d2010-09-16 14:58:22 +02004365 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004366 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4367 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004368
Johannes Berg19957bb2009-07-02 17:20:43 +02004369 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004370
Johannes Berg19957bb2009-07-02 17:20:43 +02004371 chan = ieee80211_get_channel(&rdev->wiphy,
4372 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004373 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4374 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004375
Johannes Berg19957bb2009-07-02 17:20:43 +02004376 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4377 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004378
4379 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004380 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4381 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004382 }
4383
Jouni Malinendc6382c2009-05-06 22:09:37 +03004384 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004385 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004386 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004387 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004388 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004389 else if (mfp != NL80211_MFP_NO)
4390 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004391 }
4392
Johannes Berg3e5d7642009-07-07 14:37:26 +02004393 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4394 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4395
Johannes Bergc0692b82010-08-27 14:26:53 +03004396 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004397 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004398 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4399 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004400 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004401
Jouni Malinen636a5d32009-03-19 13:39:22 +02004402 return err;
4403}
4404
4405static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4406{
Johannes Berg4c476992010-10-04 21:36:35 +02004407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4408 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004409 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004410 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004411 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004412 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004413
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004414 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4415 return -EINVAL;
4416
4417 if (!info->attrs[NL80211_ATTR_MAC])
4418 return -EINVAL;
4419
4420 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4421 return -EINVAL;
4422
Johannes Berg4c476992010-10-04 21:36:35 +02004423 if (!rdev->ops->deauth)
4424 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004425
Johannes Berg074ac8d2010-09-16 14:58:22 +02004426 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004427 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4428 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004429
Johannes Berg19957bb2009-07-02 17:20:43 +02004430 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004431
Johannes Berg19957bb2009-07-02 17:20:43 +02004432 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4433 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004434 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004435 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004436 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004437
4438 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004439 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4440 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004441 }
4442
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004443 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4444
Johannes Berg4c476992010-10-04 21:36:35 +02004445 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4446 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004447}
4448
4449static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4450{
Johannes Berg4c476992010-10-04 21:36:35 +02004451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4452 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004453 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004454 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004455 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004456 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004457
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004458 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4459 return -EINVAL;
4460
4461 if (!info->attrs[NL80211_ATTR_MAC])
4462 return -EINVAL;
4463
4464 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4465 return -EINVAL;
4466
Johannes Berg4c476992010-10-04 21:36:35 +02004467 if (!rdev->ops->disassoc)
4468 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004469
Johannes Berg074ac8d2010-09-16 14:58:22 +02004470 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004471 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4472 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004473
Johannes Berg19957bb2009-07-02 17:20:43 +02004474 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004475
Johannes Berg19957bb2009-07-02 17:20:43 +02004476 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4477 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004478 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004479 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004480 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004481
4482 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004483 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4484 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004485 }
4486
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004487 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4488
Johannes Berg4c476992010-10-04 21:36:35 +02004489 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4490 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004491}
4492
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004493static bool
4494nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4495 int mcast_rate[IEEE80211_NUM_BANDS],
4496 int rateval)
4497{
4498 struct wiphy *wiphy = &rdev->wiphy;
4499 bool found = false;
4500 int band, i;
4501
4502 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4503 struct ieee80211_supported_band *sband;
4504
4505 sband = wiphy->bands[band];
4506 if (!sband)
4507 continue;
4508
4509 for (i = 0; i < sband->n_bitrates; i++) {
4510 if (sband->bitrates[i].bitrate == rateval) {
4511 mcast_rate[band] = i + 1;
4512 found = true;
4513 break;
4514 }
4515 }
4516 }
4517
4518 return found;
4519}
4520
Johannes Berg04a773a2009-04-19 21:24:32 +02004521static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4522{
Johannes Berg4c476992010-10-04 21:36:35 +02004523 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4524 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004525 struct cfg80211_ibss_params ibss;
4526 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004527 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004528 int err;
4529
Johannes Berg8e30bc52009-04-22 17:45:38 +02004530 memset(&ibss, 0, sizeof(ibss));
4531
Johannes Berg04a773a2009-04-19 21:24:32 +02004532 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4533 return -EINVAL;
4534
4535 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4536 !info->attrs[NL80211_ATTR_SSID] ||
4537 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4538 return -EINVAL;
4539
Johannes Berg8e30bc52009-04-22 17:45:38 +02004540 ibss.beacon_interval = 100;
4541
4542 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4543 ibss.beacon_interval =
4544 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4545 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4546 return -EINVAL;
4547 }
4548
Johannes Berg4c476992010-10-04 21:36:35 +02004549 if (!rdev->ops->join_ibss)
4550 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004551
Johannes Berg4c476992010-10-04 21:36:35 +02004552 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4553 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004554
Johannes Berg79c97e92009-07-07 03:56:12 +02004555 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004556
Johannes Berg39193492011-09-16 13:45:25 +02004557 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004558 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004559
4560 if (!is_valid_ether_addr(ibss.bssid))
4561 return -EINVAL;
4562 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004563 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4564 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4565
4566 if (info->attrs[NL80211_ATTR_IE]) {
4567 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4568 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4569 }
4570
4571 ibss.channel = ieee80211_get_channel(wiphy,
4572 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4573 if (!ibss.channel ||
4574 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004575 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4576 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004577
4578 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004579 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004580
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004581 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4582 u8 *rates =
4583 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4584 int n_rates =
4585 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4586 struct ieee80211_supported_band *sband =
4587 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004588 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004589
Johannes Berg34850ab2011-07-18 18:08:35 +02004590 err = ieee80211_get_ratemask(sband, rates, n_rates,
4591 &ibss.basic_rates);
4592 if (err)
4593 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004594 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004595
4596 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4597 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4598 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4599 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004600
Johannes Berg4c476992010-10-04 21:36:35 +02004601 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4602 connkeys = nl80211_parse_connkeys(rdev,
4603 info->attrs[NL80211_ATTR_KEYS]);
4604 if (IS_ERR(connkeys))
4605 return PTR_ERR(connkeys);
4606 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004607
Johannes Berg4c476992010-10-04 21:36:35 +02004608 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004609 if (err)
4610 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004611 return err;
4612}
4613
4614static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4615{
Johannes Berg4c476992010-10-04 21:36:35 +02004616 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4617 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004618
Johannes Berg4c476992010-10-04 21:36:35 +02004619 if (!rdev->ops->leave_ibss)
4620 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004621
Johannes Berg4c476992010-10-04 21:36:35 +02004622 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4623 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004624
Johannes Berg4c476992010-10-04 21:36:35 +02004625 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004626}
4627
Johannes Bergaff89a92009-07-01 21:26:51 +02004628#ifdef CONFIG_NL80211_TESTMODE
4629static struct genl_multicast_group nl80211_testmode_mcgrp = {
4630 .name = "testmode",
4631};
4632
4633static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4634{
Johannes Berg4c476992010-10-04 21:36:35 +02004635 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004636 int err;
4637
4638 if (!info->attrs[NL80211_ATTR_TESTDATA])
4639 return -EINVAL;
4640
Johannes Bergaff89a92009-07-01 21:26:51 +02004641 err = -EOPNOTSUPP;
4642 if (rdev->ops->testmode_cmd) {
4643 rdev->testmode_info = info;
4644 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4645 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4646 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4647 rdev->testmode_info = NULL;
4648 }
4649
Johannes Bergaff89a92009-07-01 21:26:51 +02004650 return err;
4651}
4652
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004653static int nl80211_testmode_dump(struct sk_buff *skb,
4654 struct netlink_callback *cb)
4655{
4656 struct cfg80211_registered_device *dev;
4657 int err;
4658 long phy_idx;
4659 void *data = NULL;
4660 int data_len = 0;
4661
4662 if (cb->args[0]) {
4663 /*
4664 * 0 is a valid index, but not valid for args[0],
4665 * so we need to offset by 1.
4666 */
4667 phy_idx = cb->args[0] - 1;
4668 } else {
4669 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4670 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4671 nl80211_policy);
4672 if (err)
4673 return err;
4674 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4675 return -EINVAL;
4676 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4677 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4678 cb->args[1] =
4679 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4680 }
4681
4682 if (cb->args[1]) {
4683 data = nla_data((void *)cb->args[1]);
4684 data_len = nla_len((void *)cb->args[1]);
4685 }
4686
4687 mutex_lock(&cfg80211_mutex);
4688 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4689 if (!dev) {
4690 mutex_unlock(&cfg80211_mutex);
4691 return -ENOENT;
4692 }
4693 cfg80211_lock_rdev(dev);
4694 mutex_unlock(&cfg80211_mutex);
4695
4696 if (!dev->ops->testmode_dump) {
4697 err = -EOPNOTSUPP;
4698 goto out_err;
4699 }
4700
4701 while (1) {
4702 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4703 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4704 NL80211_CMD_TESTMODE);
4705 struct nlattr *tmdata;
4706
4707 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4708 genlmsg_cancel(skb, hdr);
4709 break;
4710 }
4711
4712 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4713 if (!tmdata) {
4714 genlmsg_cancel(skb, hdr);
4715 break;
4716 }
4717 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4718 data, data_len);
4719 nla_nest_end(skb, tmdata);
4720
4721 if (err == -ENOBUFS || err == -ENOENT) {
4722 genlmsg_cancel(skb, hdr);
4723 break;
4724 } else if (err) {
4725 genlmsg_cancel(skb, hdr);
4726 goto out_err;
4727 }
4728
4729 genlmsg_end(skb, hdr);
4730 }
4731
4732 err = skb->len;
4733 /* see above */
4734 cb->args[0] = phy_idx + 1;
4735 out_err:
4736 cfg80211_unlock_rdev(dev);
4737 return err;
4738}
4739
Johannes Bergaff89a92009-07-01 21:26:51 +02004740static struct sk_buff *
4741__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4742 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4743{
4744 struct sk_buff *skb;
4745 void *hdr;
4746 struct nlattr *data;
4747
4748 skb = nlmsg_new(approxlen + 100, gfp);
4749 if (!skb)
4750 return NULL;
4751
4752 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4753 if (!hdr) {
4754 kfree_skb(skb);
4755 return NULL;
4756 }
4757
4758 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4759 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4760
4761 ((void **)skb->cb)[0] = rdev;
4762 ((void **)skb->cb)[1] = hdr;
4763 ((void **)skb->cb)[2] = data;
4764
4765 return skb;
4766
4767 nla_put_failure:
4768 kfree_skb(skb);
4769 return NULL;
4770}
4771
4772struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4773 int approxlen)
4774{
4775 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4776
4777 if (WARN_ON(!rdev->testmode_info))
4778 return NULL;
4779
4780 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4781 rdev->testmode_info->snd_pid,
4782 rdev->testmode_info->snd_seq,
4783 GFP_KERNEL);
4784}
4785EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4786
4787int cfg80211_testmode_reply(struct sk_buff *skb)
4788{
4789 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4790 void *hdr = ((void **)skb->cb)[1];
4791 struct nlattr *data = ((void **)skb->cb)[2];
4792
4793 if (WARN_ON(!rdev->testmode_info)) {
4794 kfree_skb(skb);
4795 return -EINVAL;
4796 }
4797
4798 nla_nest_end(skb, data);
4799 genlmsg_end(skb, hdr);
4800 return genlmsg_reply(skb, rdev->testmode_info);
4801}
4802EXPORT_SYMBOL(cfg80211_testmode_reply);
4803
4804struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4805 int approxlen, gfp_t gfp)
4806{
4807 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4808
4809 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4810}
4811EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4812
4813void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4814{
4815 void *hdr = ((void **)skb->cb)[1];
4816 struct nlattr *data = ((void **)skb->cb)[2];
4817
4818 nla_nest_end(skb, data);
4819 genlmsg_end(skb, hdr);
4820 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4821}
4822EXPORT_SYMBOL(cfg80211_testmode_event);
4823#endif
4824
Samuel Ortizb23aa672009-07-01 21:26:54 +02004825static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4826{
Johannes Berg4c476992010-10-04 21:36:35 +02004827 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4828 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004829 struct cfg80211_connect_params connect;
4830 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004831 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004832 int err;
4833
4834 memset(&connect, 0, sizeof(connect));
4835
4836 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4837 return -EINVAL;
4838
4839 if (!info->attrs[NL80211_ATTR_SSID] ||
4840 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4841 return -EINVAL;
4842
4843 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4844 connect.auth_type =
4845 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4846 if (!nl80211_valid_auth_type(connect.auth_type))
4847 return -EINVAL;
4848 } else
4849 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4850
4851 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4852
Johannes Bergc0692b82010-08-27 14:26:53 +03004853 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004854 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004855 if (err)
4856 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004857
Johannes Berg074ac8d2010-09-16 14:58:22 +02004858 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004859 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4860 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004861
Johannes Berg79c97e92009-07-07 03:56:12 +02004862 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004863
Samuel Ortizb23aa672009-07-01 21:26:54 +02004864 if (info->attrs[NL80211_ATTR_MAC])
4865 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4866 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4867 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4868
4869 if (info->attrs[NL80211_ATTR_IE]) {
4870 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4871 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4872 }
4873
4874 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4875 connect.channel =
4876 ieee80211_get_channel(wiphy,
4877 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4878 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004879 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4880 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004881 }
4882
Johannes Bergfffd0932009-07-08 14:22:54 +02004883 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4884 connkeys = nl80211_parse_connkeys(rdev,
4885 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004886 if (IS_ERR(connkeys))
4887 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004888 }
4889
4890 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004891 if (err)
4892 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004893 return err;
4894}
4895
4896static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4897{
Johannes Berg4c476992010-10-04 21:36:35 +02004898 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4899 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004900 u16 reason;
4901
4902 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4903 reason = WLAN_REASON_DEAUTH_LEAVING;
4904 else
4905 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4906
4907 if (reason == 0)
4908 return -EINVAL;
4909
Johannes Berg074ac8d2010-09-16 14:58:22 +02004910 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004911 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4912 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004913
Johannes Berg4c476992010-10-04 21:36:35 +02004914 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004915}
4916
Johannes Berg463d0182009-07-14 00:33:35 +02004917static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4918{
Johannes Berg4c476992010-10-04 21:36:35 +02004919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004920 struct net *net;
4921 int err;
4922 u32 pid;
4923
4924 if (!info->attrs[NL80211_ATTR_PID])
4925 return -EINVAL;
4926
4927 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4928
Johannes Berg463d0182009-07-14 00:33:35 +02004929 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004930 if (IS_ERR(net))
4931 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004932
4933 err = 0;
4934
4935 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004936 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4937 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004938
Johannes Berg463d0182009-07-14 00:33:35 +02004939 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004940 return err;
4941}
4942
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004943static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4944{
Johannes Berg4c476992010-10-04 21:36:35 +02004945 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004946 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4947 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004948 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004949 struct cfg80211_pmksa pmksa;
4950
4951 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4952
4953 if (!info->attrs[NL80211_ATTR_MAC])
4954 return -EINVAL;
4955
4956 if (!info->attrs[NL80211_ATTR_PMKID])
4957 return -EINVAL;
4958
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004959 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4960 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4961
Johannes Berg074ac8d2010-09-16 14:58:22 +02004962 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004963 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4964 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004965
4966 switch (info->genlhdr->cmd) {
4967 case NL80211_CMD_SET_PMKSA:
4968 rdev_ops = rdev->ops->set_pmksa;
4969 break;
4970 case NL80211_CMD_DEL_PMKSA:
4971 rdev_ops = rdev->ops->del_pmksa;
4972 break;
4973 default:
4974 WARN_ON(1);
4975 break;
4976 }
4977
Johannes Berg4c476992010-10-04 21:36:35 +02004978 if (!rdev_ops)
4979 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004980
Johannes Berg4c476992010-10-04 21:36:35 +02004981 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004982}
4983
4984static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4985{
Johannes Berg4c476992010-10-04 21:36:35 +02004986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4987 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004988
Johannes Berg074ac8d2010-09-16 14:58:22 +02004989 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004990 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4991 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004992
Johannes Berg4c476992010-10-04 21:36:35 +02004993 if (!rdev->ops->flush_pmksa)
4994 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004995
Johannes Berg4c476992010-10-04 21:36:35 +02004996 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004997}
4998
Arik Nemtsov109086c2011-09-28 14:12:50 +03004999static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5000{
5001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5002 struct net_device *dev = info->user_ptr[1];
5003 u8 action_code, dialog_token;
5004 u16 status_code;
5005 u8 *peer;
5006
5007 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5008 !rdev->ops->tdls_mgmt)
5009 return -EOPNOTSUPP;
5010
5011 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5012 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5013 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5014 !info->attrs[NL80211_ATTR_IE] ||
5015 !info->attrs[NL80211_ATTR_MAC])
5016 return -EINVAL;
5017
5018 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5019 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5020 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5021 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5022
5023 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5024 dialog_token, status_code,
5025 nla_data(info->attrs[NL80211_ATTR_IE]),
5026 nla_len(info->attrs[NL80211_ATTR_IE]));
5027}
5028
5029static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5030{
5031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5032 struct net_device *dev = info->user_ptr[1];
5033 enum nl80211_tdls_operation operation;
5034 u8 *peer;
5035
5036 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5037 !rdev->ops->tdls_oper)
5038 return -EOPNOTSUPP;
5039
5040 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5041 !info->attrs[NL80211_ATTR_MAC])
5042 return -EINVAL;
5043
5044 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5045 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5046
5047 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5048}
5049
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005050static int nl80211_remain_on_channel(struct sk_buff *skb,
5051 struct genl_info *info)
5052{
Johannes Berg4c476992010-10-04 21:36:35 +02005053 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5054 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005055 struct ieee80211_channel *chan;
5056 struct sk_buff *msg;
5057 void *hdr;
5058 u64 cookie;
5059 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5060 u32 freq, duration;
5061 int err;
5062
5063 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5064 !info->attrs[NL80211_ATTR_DURATION])
5065 return -EINVAL;
5066
5067 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5068
5069 /*
5070 * We should be on that channel for at least one jiffie,
5071 * and more than 5 seconds seems excessive.
5072 */
Johannes Berga2939112010-12-14 17:54:28 +01005073 if (!duration || !msecs_to_jiffies(duration) ||
5074 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005075 return -EINVAL;
5076
Johannes Berg4c476992010-10-04 21:36:35 +02005077 if (!rdev->ops->remain_on_channel)
5078 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005079
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005080 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5081 channel_type = nla_get_u32(
5082 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5083 if (channel_type != NL80211_CHAN_NO_HT &&
5084 channel_type != NL80211_CHAN_HT20 &&
5085 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005086 channel_type != NL80211_CHAN_HT40MINUS)
5087 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005088 }
5089
5090 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5091 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005092 if (chan == NULL)
5093 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005094
5095 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005096 if (!msg)
5097 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005098
5099 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5100 NL80211_CMD_REMAIN_ON_CHANNEL);
5101
5102 if (IS_ERR(hdr)) {
5103 err = PTR_ERR(hdr);
5104 goto free_msg;
5105 }
5106
5107 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5108 channel_type, duration, &cookie);
5109
5110 if (err)
5111 goto free_msg;
5112
5113 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5114
5115 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005116
5117 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005118
5119 nla_put_failure:
5120 err = -ENOBUFS;
5121 free_msg:
5122 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005123 return err;
5124}
5125
5126static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5127 struct genl_info *info)
5128{
Johannes Berg4c476992010-10-04 21:36:35 +02005129 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5130 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005131 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005132
5133 if (!info->attrs[NL80211_ATTR_COOKIE])
5134 return -EINVAL;
5135
Johannes Berg4c476992010-10-04 21:36:35 +02005136 if (!rdev->ops->cancel_remain_on_channel)
5137 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005138
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005139 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5140
Johannes Berg4c476992010-10-04 21:36:35 +02005141 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005142}
5143
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005144static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5145 u8 *rates, u8 rates_len)
5146{
5147 u8 i;
5148 u32 mask = 0;
5149
5150 for (i = 0; i < rates_len; i++) {
5151 int rate = (rates[i] & 0x7f) * 5;
5152 int ridx;
5153 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5154 struct ieee80211_rate *srate =
5155 &sband->bitrates[ridx];
5156 if (rate == srate->bitrate) {
5157 mask |= 1 << ridx;
5158 break;
5159 }
5160 }
5161 if (ridx == sband->n_bitrates)
5162 return 0; /* rate not found */
5163 }
5164
5165 return mask;
5166}
5167
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005168static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005169 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5170 .len = NL80211_MAX_SUPP_RATES },
5171};
5172
5173static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5174 struct genl_info *info)
5175{
5176 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005178 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005179 int rem, i;
5180 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005181 struct nlattr *tx_rates;
5182 struct ieee80211_supported_band *sband;
5183
5184 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5185 return -EINVAL;
5186
Johannes Berg4c476992010-10-04 21:36:35 +02005187 if (!rdev->ops->set_bitrate_mask)
5188 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005189
5190 memset(&mask, 0, sizeof(mask));
5191 /* Default to all rates enabled */
5192 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5193 sband = rdev->wiphy.bands[i];
5194 mask.control[i].legacy =
5195 sband ? (1 << sband->n_bitrates) - 1 : 0;
5196 }
5197
5198 /*
5199 * The nested attribute uses enum nl80211_band as the index. This maps
5200 * directly to the enum ieee80211_band values used in cfg80211.
5201 */
5202 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5203 {
5204 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005205 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5206 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005207 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005208 if (sband == NULL)
5209 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005210 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5211 nla_len(tx_rates), nl80211_txattr_policy);
5212 if (tb[NL80211_TXRATE_LEGACY]) {
5213 mask.control[band].legacy = rateset_to_mask(
5214 sband,
5215 nla_data(tb[NL80211_TXRATE_LEGACY]),
5216 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005217 if (mask.control[band].legacy == 0)
5218 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005219 }
5220 }
5221
Johannes Berg4c476992010-10-04 21:36:35 +02005222 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005223}
5224
Johannes Berg2e161f72010-08-12 15:38:38 +02005225static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005226{
Johannes Berg4c476992010-10-04 21:36:35 +02005227 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5228 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005229 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005230
5231 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5232 return -EINVAL;
5233
Johannes Berg2e161f72010-08-12 15:38:38 +02005234 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5235 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005236
Johannes Berg9d38d852010-06-09 17:20:33 +02005237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005238 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005239 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5240 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5241 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005242 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005243 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5244 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005245
5246 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005247 if (!rdev->ops->mgmt_tx)
5248 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005249
Johannes Berg4c476992010-10-04 21:36:35 +02005250 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005251 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005252 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5253 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005254}
5255
Johannes Berg2e161f72010-08-12 15:38:38 +02005256static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005257{
Johannes Berg4c476992010-10-04 21:36:35 +02005258 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5259 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005260 struct ieee80211_channel *chan;
5261 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005262 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005263 u32 freq;
5264 int err;
5265 void *hdr;
5266 u64 cookie;
5267 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005268 unsigned int wait = 0;
5269 bool offchan;
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305270 bool no_cck;
Jouni Malinen026331c2010-02-15 12:53:10 +02005271
5272 if (!info->attrs[NL80211_ATTR_FRAME] ||
5273 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5274 return -EINVAL;
5275
Johannes Berg4c476992010-10-04 21:36:35 +02005276 if (!rdev->ops->mgmt_tx)
5277 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005278
Johannes Berg9d38d852010-06-09 17:20:33 +02005279 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005280 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005281 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5282 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5283 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005284 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005285 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5286 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005287
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005288 if (info->attrs[NL80211_ATTR_DURATION]) {
5289 if (!rdev->ops->mgmt_tx_cancel_wait)
5290 return -EINVAL;
5291 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5292 }
5293
Jouni Malinen026331c2010-02-15 12:53:10 +02005294 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5295 channel_type = nla_get_u32(
5296 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5297 if (channel_type != NL80211_CHAN_NO_HT &&
5298 channel_type != NL80211_CHAN_HT20 &&
5299 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005300 channel_type != NL80211_CHAN_HT40MINUS)
5301 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005302 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005303 }
5304
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005305 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5306
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305307 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5308
Jouni Malinen026331c2010-02-15 12:53:10 +02005309 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5310 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005311 if (chan == NULL)
5312 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005313
5314 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005315 if (!msg)
5316 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005317
5318 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005319 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005320
5321 if (IS_ERR(hdr)) {
5322 err = PTR_ERR(hdr);
5323 goto free_msg;
5324 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005325 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5326 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005327 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5328 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305329 no_cck, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005330 if (err)
5331 goto free_msg;
5332
5333 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5334
5335 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005336 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005337
5338 nla_put_failure:
5339 err = -ENOBUFS;
5340 free_msg:
5341 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005342 return err;
5343}
5344
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005345static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5346{
5347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5348 struct net_device *dev = info->user_ptr[1];
5349 u64 cookie;
5350
5351 if (!info->attrs[NL80211_ATTR_COOKIE])
5352 return -EINVAL;
5353
5354 if (!rdev->ops->mgmt_tx_cancel_wait)
5355 return -EOPNOTSUPP;
5356
5357 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5358 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5359 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5360 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5361 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5362 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5363 return -EOPNOTSUPP;
5364
5365 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5366
5367 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5368}
5369
Kalle Valoffb9eb32010-02-17 17:58:10 +02005370static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5371{
Johannes Berg4c476992010-10-04 21:36:35 +02005372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005373 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005374 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005375 u8 ps_state;
5376 bool state;
5377 int err;
5378
Johannes Berg4c476992010-10-04 21:36:35 +02005379 if (!info->attrs[NL80211_ATTR_PS_STATE])
5380 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005381
5382 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5383
Johannes Berg4c476992010-10-04 21:36:35 +02005384 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5385 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005386
5387 wdev = dev->ieee80211_ptr;
5388
Johannes Berg4c476992010-10-04 21:36:35 +02005389 if (!rdev->ops->set_power_mgmt)
5390 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005391
5392 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5393
5394 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005395 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005396
Johannes Berg4c476992010-10-04 21:36:35 +02005397 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5398 wdev->ps_timeout);
5399 if (!err)
5400 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005401 return err;
5402}
5403
5404static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5405{
Johannes Berg4c476992010-10-04 21:36:35 +02005406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005407 enum nl80211_ps_state ps_state;
5408 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005409 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005410 struct sk_buff *msg;
5411 void *hdr;
5412 int err;
5413
Kalle Valoffb9eb32010-02-17 17:58:10 +02005414 wdev = dev->ieee80211_ptr;
5415
Johannes Berg4c476992010-10-04 21:36:35 +02005416 if (!rdev->ops->set_power_mgmt)
5417 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005418
5419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005420 if (!msg)
5421 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005422
5423 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5424 NL80211_CMD_GET_POWER_SAVE);
5425 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005426 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005427 goto free_msg;
5428 }
5429
5430 if (wdev->ps)
5431 ps_state = NL80211_PS_ENABLED;
5432 else
5433 ps_state = NL80211_PS_DISABLED;
5434
5435 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5436
5437 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005438 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005439
Johannes Berg4c476992010-10-04 21:36:35 +02005440 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005441 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005442 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005443 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005444 return err;
5445}
5446
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005447static struct nla_policy
5448nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5449 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5450 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5451 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5452};
5453
5454static int nl80211_set_cqm_rssi(struct genl_info *info,
5455 s32 threshold, u32 hysteresis)
5456{
Johannes Berg4c476992010-10-04 21:36:35 +02005457 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005458 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005459 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005460
5461 if (threshold > 0)
5462 return -EINVAL;
5463
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005464 wdev = dev->ieee80211_ptr;
5465
Johannes Berg4c476992010-10-04 21:36:35 +02005466 if (!rdev->ops->set_cqm_rssi_config)
5467 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005468
Johannes Berg074ac8d2010-09-16 14:58:22 +02005469 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005470 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5471 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005472
Johannes Berg4c476992010-10-04 21:36:35 +02005473 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5474 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005475}
5476
5477static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5478{
5479 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5480 struct nlattr *cqm;
5481 int err;
5482
5483 cqm = info->attrs[NL80211_ATTR_CQM];
5484 if (!cqm) {
5485 err = -EINVAL;
5486 goto out;
5487 }
5488
5489 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5490 nl80211_attr_cqm_policy);
5491 if (err)
5492 goto out;
5493
5494 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5495 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5496 s32 threshold;
5497 u32 hysteresis;
5498 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5499 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5500 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5501 } else
5502 err = -EINVAL;
5503
5504out:
5505 return err;
5506}
5507
Johannes Berg29cbe682010-12-03 09:20:44 +01005508static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5509{
5510 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5511 struct net_device *dev = info->user_ptr[1];
5512 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005513 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005514 int err;
5515
5516 /* start with default */
5517 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005518 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005519
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005520 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005521 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005522 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005523 if (err)
5524 return err;
5525 }
5526
5527 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5528 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5529 return -EINVAL;
5530
Javier Cardonac80d5452010-12-16 17:37:49 -08005531 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5532 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5533
5534 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5535 /* parse additional setup parameters if given */
5536 err = nl80211_parse_mesh_setup(info, &setup);
5537 if (err)
5538 return err;
5539 }
5540
5541 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005542}
5543
5544static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5545{
5546 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5547 struct net_device *dev = info->user_ptr[1];
5548
5549 return cfg80211_leave_mesh(rdev, dev);
5550}
5551
Johannes Bergff1b6e62011-05-04 15:37:28 +02005552static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5553{
5554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5555 struct sk_buff *msg;
5556 void *hdr;
5557
5558 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5559 return -EOPNOTSUPP;
5560
5561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5562 if (!msg)
5563 return -ENOMEM;
5564
5565 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5566 NL80211_CMD_GET_WOWLAN);
5567 if (!hdr)
5568 goto nla_put_failure;
5569
5570 if (rdev->wowlan) {
5571 struct nlattr *nl_wowlan;
5572
5573 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5574 if (!nl_wowlan)
5575 goto nla_put_failure;
5576
5577 if (rdev->wowlan->any)
5578 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5579 if (rdev->wowlan->disconnect)
5580 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5581 if (rdev->wowlan->magic_pkt)
5582 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005583 if (rdev->wowlan->gtk_rekey_failure)
5584 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5585 if (rdev->wowlan->eap_identity_req)
5586 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5587 if (rdev->wowlan->four_way_handshake)
5588 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5589 if (rdev->wowlan->rfkill_release)
5590 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005591 if (rdev->wowlan->n_patterns) {
5592 struct nlattr *nl_pats, *nl_pat;
5593 int i, pat_len;
5594
5595 nl_pats = nla_nest_start(msg,
5596 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5597 if (!nl_pats)
5598 goto nla_put_failure;
5599
5600 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5601 nl_pat = nla_nest_start(msg, i + 1);
5602 if (!nl_pat)
5603 goto nla_put_failure;
5604 pat_len = rdev->wowlan->patterns[i].pattern_len;
5605 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5606 DIV_ROUND_UP(pat_len, 8),
5607 rdev->wowlan->patterns[i].mask);
5608 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5609 pat_len,
5610 rdev->wowlan->patterns[i].pattern);
5611 nla_nest_end(msg, nl_pat);
5612 }
5613 nla_nest_end(msg, nl_pats);
5614 }
5615
5616 nla_nest_end(msg, nl_wowlan);
5617 }
5618
5619 genlmsg_end(msg, hdr);
5620 return genlmsg_reply(msg, info);
5621
5622nla_put_failure:
5623 nlmsg_free(msg);
5624 return -ENOBUFS;
5625}
5626
5627static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5628{
5629 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5630 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5631 struct cfg80211_wowlan no_triggers = {};
5632 struct cfg80211_wowlan new_triggers = {};
5633 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5634 int err, i;
5635
5636 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5637 return -EOPNOTSUPP;
5638
5639 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5640 goto no_triggers;
5641
5642 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5643 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5644 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5645 nl80211_wowlan_policy);
5646 if (err)
5647 return err;
5648
5649 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5650 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5651 return -EINVAL;
5652 new_triggers.any = true;
5653 }
5654
5655 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5656 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5657 return -EINVAL;
5658 new_triggers.disconnect = true;
5659 }
5660
5661 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5662 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5663 return -EINVAL;
5664 new_triggers.magic_pkt = true;
5665 }
5666
Johannes Berg77dbbb12011-07-13 10:48:55 +02005667 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5668 return -EINVAL;
5669
5670 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5671 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5672 return -EINVAL;
5673 new_triggers.gtk_rekey_failure = true;
5674 }
5675
5676 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5677 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5678 return -EINVAL;
5679 new_triggers.eap_identity_req = true;
5680 }
5681
5682 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5683 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5684 return -EINVAL;
5685 new_triggers.four_way_handshake = true;
5686 }
5687
5688 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5689 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5690 return -EINVAL;
5691 new_triggers.rfkill_release = true;
5692 }
5693
Johannes Bergff1b6e62011-05-04 15:37:28 +02005694 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5695 struct nlattr *pat;
5696 int n_patterns = 0;
5697 int rem, pat_len, mask_len;
5698 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5699
5700 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5701 rem)
5702 n_patterns++;
5703 if (n_patterns > wowlan->n_patterns)
5704 return -EINVAL;
5705
5706 new_triggers.patterns = kcalloc(n_patterns,
5707 sizeof(new_triggers.patterns[0]),
5708 GFP_KERNEL);
5709 if (!new_triggers.patterns)
5710 return -ENOMEM;
5711
5712 new_triggers.n_patterns = n_patterns;
5713 i = 0;
5714
5715 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5716 rem) {
5717 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5718 nla_data(pat), nla_len(pat), NULL);
5719 err = -EINVAL;
5720 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5721 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5722 goto error;
5723 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5724 mask_len = DIV_ROUND_UP(pat_len, 8);
5725 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5726 mask_len)
5727 goto error;
5728 if (pat_len > wowlan->pattern_max_len ||
5729 pat_len < wowlan->pattern_min_len)
5730 goto error;
5731
5732 new_triggers.patterns[i].mask =
5733 kmalloc(mask_len + pat_len, GFP_KERNEL);
5734 if (!new_triggers.patterns[i].mask) {
5735 err = -ENOMEM;
5736 goto error;
5737 }
5738 new_triggers.patterns[i].pattern =
5739 new_triggers.patterns[i].mask + mask_len;
5740 memcpy(new_triggers.patterns[i].mask,
5741 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5742 mask_len);
5743 new_triggers.patterns[i].pattern_len = pat_len;
5744 memcpy(new_triggers.patterns[i].pattern,
5745 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5746 pat_len);
5747 i++;
5748 }
5749 }
5750
5751 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5752 struct cfg80211_wowlan *ntrig;
5753 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5754 GFP_KERNEL);
5755 if (!ntrig) {
5756 err = -ENOMEM;
5757 goto error;
5758 }
5759 cfg80211_rdev_free_wowlan(rdev);
5760 rdev->wowlan = ntrig;
5761 } else {
5762 no_triggers:
5763 cfg80211_rdev_free_wowlan(rdev);
5764 rdev->wowlan = NULL;
5765 }
5766
5767 return 0;
5768 error:
5769 for (i = 0; i < new_triggers.n_patterns; i++)
5770 kfree(new_triggers.patterns[i].mask);
5771 kfree(new_triggers.patterns);
5772 return err;
5773}
5774
Johannes Berge5497d72011-07-05 16:35:40 +02005775static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5776{
5777 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5778 struct net_device *dev = info->user_ptr[1];
5779 struct wireless_dev *wdev = dev->ieee80211_ptr;
5780 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5781 struct cfg80211_gtk_rekey_data rekey_data;
5782 int err;
5783
5784 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5785 return -EINVAL;
5786
5787 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5788 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5789 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5790 nl80211_rekey_policy);
5791 if (err)
5792 return err;
5793
5794 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5795 return -ERANGE;
5796 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5797 return -ERANGE;
5798 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5799 return -ERANGE;
5800
5801 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5802 NL80211_KEK_LEN);
5803 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5804 NL80211_KCK_LEN);
5805 memcpy(rekey_data.replay_ctr,
5806 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5807 NL80211_REPLAY_CTR_LEN);
5808
5809 wdev_lock(wdev);
5810 if (!wdev->current_bss) {
5811 err = -ENOTCONN;
5812 goto out;
5813 }
5814
5815 if (!rdev->ops->set_rekey_data) {
5816 err = -EOPNOTSUPP;
5817 goto out;
5818 }
5819
5820 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5821 out:
5822 wdev_unlock(wdev);
5823 return err;
5824}
5825
Johannes Berg4c476992010-10-04 21:36:35 +02005826#define NL80211_FLAG_NEED_WIPHY 0x01
5827#define NL80211_FLAG_NEED_NETDEV 0x02
5828#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005829#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5830#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5831 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005832
5833static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5834 struct genl_info *info)
5835{
5836 struct cfg80211_registered_device *rdev;
5837 struct net_device *dev;
5838 int err;
5839 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5840
5841 if (rtnl)
5842 rtnl_lock();
5843
5844 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5845 rdev = cfg80211_get_dev_from_info(info);
5846 if (IS_ERR(rdev)) {
5847 if (rtnl)
5848 rtnl_unlock();
5849 return PTR_ERR(rdev);
5850 }
5851 info->user_ptr[0] = rdev;
5852 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5853 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5854 if (err) {
5855 if (rtnl)
5856 rtnl_unlock();
5857 return err;
5858 }
Johannes Berg41265712010-10-04 21:14:05 +02005859 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5860 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005861 cfg80211_unlock_rdev(rdev);
5862 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005863 if (rtnl)
5864 rtnl_unlock();
5865 return -ENETDOWN;
5866 }
Johannes Berg4c476992010-10-04 21:36:35 +02005867 info->user_ptr[0] = rdev;
5868 info->user_ptr[1] = dev;
5869 }
5870
5871 return 0;
5872}
5873
5874static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5875 struct genl_info *info)
5876{
5877 if (info->user_ptr[0])
5878 cfg80211_unlock_rdev(info->user_ptr[0]);
5879 if (info->user_ptr[1])
5880 dev_put(info->user_ptr[1]);
5881 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5882 rtnl_unlock();
5883}
5884
Johannes Berg55682962007-09-20 13:09:35 -04005885static struct genl_ops nl80211_ops[] = {
5886 {
5887 .cmd = NL80211_CMD_GET_WIPHY,
5888 .doit = nl80211_get_wiphy,
5889 .dumpit = nl80211_dump_wiphy,
5890 .policy = nl80211_policy,
5891 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005892 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005893 },
5894 {
5895 .cmd = NL80211_CMD_SET_WIPHY,
5896 .doit = nl80211_set_wiphy,
5897 .policy = nl80211_policy,
5898 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005899 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005900 },
5901 {
5902 .cmd = NL80211_CMD_GET_INTERFACE,
5903 .doit = nl80211_get_interface,
5904 .dumpit = nl80211_dump_interface,
5905 .policy = nl80211_policy,
5906 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005907 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005908 },
5909 {
5910 .cmd = NL80211_CMD_SET_INTERFACE,
5911 .doit = nl80211_set_interface,
5912 .policy = nl80211_policy,
5913 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005914 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5915 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005916 },
5917 {
5918 .cmd = NL80211_CMD_NEW_INTERFACE,
5919 .doit = nl80211_new_interface,
5920 .policy = nl80211_policy,
5921 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005922 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5923 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005924 },
5925 {
5926 .cmd = NL80211_CMD_DEL_INTERFACE,
5927 .doit = nl80211_del_interface,
5928 .policy = nl80211_policy,
5929 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005930 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5931 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005932 },
Johannes Berg41ade002007-12-19 02:03:29 +01005933 {
5934 .cmd = NL80211_CMD_GET_KEY,
5935 .doit = nl80211_get_key,
5936 .policy = nl80211_policy,
5937 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005938 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5939 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005940 },
5941 {
5942 .cmd = NL80211_CMD_SET_KEY,
5943 .doit = nl80211_set_key,
5944 .policy = nl80211_policy,
5945 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005947 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005948 },
5949 {
5950 .cmd = NL80211_CMD_NEW_KEY,
5951 .doit = nl80211_new_key,
5952 .policy = nl80211_policy,
5953 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005955 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005956 },
5957 {
5958 .cmd = NL80211_CMD_DEL_KEY,
5959 .doit = nl80211_del_key,
5960 .policy = nl80211_policy,
5961 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005962 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005963 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005964 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005965 {
5966 .cmd = NL80211_CMD_SET_BEACON,
5967 .policy = nl80211_policy,
5968 .flags = GENL_ADMIN_PERM,
5969 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005970 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5971 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005972 },
5973 {
5974 .cmd = NL80211_CMD_NEW_BEACON,
5975 .policy = nl80211_policy,
5976 .flags = GENL_ADMIN_PERM,
5977 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005978 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5979 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005980 },
5981 {
5982 .cmd = NL80211_CMD_DEL_BEACON,
5983 .policy = nl80211_policy,
5984 .flags = GENL_ADMIN_PERM,
5985 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005986 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5987 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005988 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005989 {
5990 .cmd = NL80211_CMD_GET_STATION,
5991 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005992 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005993 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005994 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5995 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005996 },
5997 {
5998 .cmd = NL80211_CMD_SET_STATION,
5999 .doit = nl80211_set_station,
6000 .policy = nl80211_policy,
6001 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006002 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6003 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006004 },
6005 {
6006 .cmd = NL80211_CMD_NEW_STATION,
6007 .doit = nl80211_new_station,
6008 .policy = nl80211_policy,
6009 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006010 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006011 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006012 },
6013 {
6014 .cmd = NL80211_CMD_DEL_STATION,
6015 .doit = nl80211_del_station,
6016 .policy = nl80211_policy,
6017 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006018 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6019 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006020 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006021 {
6022 .cmd = NL80211_CMD_GET_MPATH,
6023 .doit = nl80211_get_mpath,
6024 .dumpit = nl80211_dump_mpath,
6025 .policy = nl80211_policy,
6026 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006027 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006028 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006029 },
6030 {
6031 .cmd = NL80211_CMD_SET_MPATH,
6032 .doit = nl80211_set_mpath,
6033 .policy = nl80211_policy,
6034 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006035 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006036 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006037 },
6038 {
6039 .cmd = NL80211_CMD_NEW_MPATH,
6040 .doit = nl80211_new_mpath,
6041 .policy = nl80211_policy,
6042 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006043 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006044 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006045 },
6046 {
6047 .cmd = NL80211_CMD_DEL_MPATH,
6048 .doit = nl80211_del_mpath,
6049 .policy = nl80211_policy,
6050 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006051 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6052 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006053 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006054 {
6055 .cmd = NL80211_CMD_SET_BSS,
6056 .doit = nl80211_set_bss,
6057 .policy = nl80211_policy,
6058 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006059 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6060 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006061 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006062 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006063 .cmd = NL80211_CMD_GET_REG,
6064 .doit = nl80211_get_reg,
6065 .policy = nl80211_policy,
6066 /* can be retrieved by unprivileged users */
6067 },
6068 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006069 .cmd = NL80211_CMD_SET_REG,
6070 .doit = nl80211_set_reg,
6071 .policy = nl80211_policy,
6072 .flags = GENL_ADMIN_PERM,
6073 },
6074 {
6075 .cmd = NL80211_CMD_REQ_SET_REG,
6076 .doit = nl80211_req_set_reg,
6077 .policy = nl80211_policy,
6078 .flags = GENL_ADMIN_PERM,
6079 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006080 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006081 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6082 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006083 .policy = nl80211_policy,
6084 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006085 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6086 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006087 },
6088 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006089 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6090 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006091 .policy = nl80211_policy,
6092 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006093 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006094 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006095 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006096 {
Johannes Berg2a519312009-02-10 21:25:55 +01006097 .cmd = NL80211_CMD_TRIGGER_SCAN,
6098 .doit = nl80211_trigger_scan,
6099 .policy = nl80211_policy,
6100 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006101 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006102 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006103 },
6104 {
6105 .cmd = NL80211_CMD_GET_SCAN,
6106 .policy = nl80211_policy,
6107 .dumpit = nl80211_dump_scan,
6108 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006109 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006110 .cmd = NL80211_CMD_START_SCHED_SCAN,
6111 .doit = nl80211_start_sched_scan,
6112 .policy = nl80211_policy,
6113 .flags = GENL_ADMIN_PERM,
6114 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6115 NL80211_FLAG_NEED_RTNL,
6116 },
6117 {
6118 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6119 .doit = nl80211_stop_sched_scan,
6120 .policy = nl80211_policy,
6121 .flags = GENL_ADMIN_PERM,
6122 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6123 NL80211_FLAG_NEED_RTNL,
6124 },
6125 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006126 .cmd = NL80211_CMD_AUTHENTICATE,
6127 .doit = nl80211_authenticate,
6128 .policy = nl80211_policy,
6129 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006130 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006131 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006132 },
6133 {
6134 .cmd = NL80211_CMD_ASSOCIATE,
6135 .doit = nl80211_associate,
6136 .policy = nl80211_policy,
6137 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006138 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006139 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006140 },
6141 {
6142 .cmd = NL80211_CMD_DEAUTHENTICATE,
6143 .doit = nl80211_deauthenticate,
6144 .policy = nl80211_policy,
6145 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006146 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006147 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006148 },
6149 {
6150 .cmd = NL80211_CMD_DISASSOCIATE,
6151 .doit = nl80211_disassociate,
6152 .policy = nl80211_policy,
6153 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006154 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006155 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006156 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006157 {
6158 .cmd = NL80211_CMD_JOIN_IBSS,
6159 .doit = nl80211_join_ibss,
6160 .policy = nl80211_policy,
6161 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006162 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006163 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006164 },
6165 {
6166 .cmd = NL80211_CMD_LEAVE_IBSS,
6167 .doit = nl80211_leave_ibss,
6168 .policy = nl80211_policy,
6169 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006170 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006171 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006172 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006173#ifdef CONFIG_NL80211_TESTMODE
6174 {
6175 .cmd = NL80211_CMD_TESTMODE,
6176 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006177 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006178 .policy = nl80211_policy,
6179 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006180 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6181 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006182 },
6183#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006184 {
6185 .cmd = NL80211_CMD_CONNECT,
6186 .doit = nl80211_connect,
6187 .policy = nl80211_policy,
6188 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006189 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006190 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006191 },
6192 {
6193 .cmd = NL80211_CMD_DISCONNECT,
6194 .doit = nl80211_disconnect,
6195 .policy = nl80211_policy,
6196 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006197 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006198 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006199 },
Johannes Berg463d0182009-07-14 00:33:35 +02006200 {
6201 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6202 .doit = nl80211_wiphy_netns,
6203 .policy = nl80211_policy,
6204 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006205 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6206 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006207 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006208 {
6209 .cmd = NL80211_CMD_GET_SURVEY,
6210 .policy = nl80211_policy,
6211 .dumpit = nl80211_dump_survey,
6212 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006213 {
6214 .cmd = NL80211_CMD_SET_PMKSA,
6215 .doit = nl80211_setdel_pmksa,
6216 .policy = nl80211_policy,
6217 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006218 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6219 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006220 },
6221 {
6222 .cmd = NL80211_CMD_DEL_PMKSA,
6223 .doit = nl80211_setdel_pmksa,
6224 .policy = nl80211_policy,
6225 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006226 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6227 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006228 },
6229 {
6230 .cmd = NL80211_CMD_FLUSH_PMKSA,
6231 .doit = nl80211_flush_pmksa,
6232 .policy = nl80211_policy,
6233 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006234 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6235 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006236 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006237 {
6238 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6239 .doit = nl80211_remain_on_channel,
6240 .policy = nl80211_policy,
6241 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006242 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006243 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006244 },
6245 {
6246 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6247 .doit = nl80211_cancel_remain_on_channel,
6248 .policy = nl80211_policy,
6249 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006250 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006251 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006252 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006253 {
6254 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6255 .doit = nl80211_set_tx_bitrate_mask,
6256 .policy = nl80211_policy,
6257 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006258 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6259 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006260 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006261 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006262 .cmd = NL80211_CMD_REGISTER_FRAME,
6263 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006264 .policy = nl80211_policy,
6265 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006266 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6267 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006268 },
6269 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006270 .cmd = NL80211_CMD_FRAME,
6271 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006272 .policy = nl80211_policy,
6273 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006274 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006275 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006276 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006277 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006278 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6279 .doit = nl80211_tx_mgmt_cancel_wait,
6280 .policy = nl80211_policy,
6281 .flags = GENL_ADMIN_PERM,
6282 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6283 NL80211_FLAG_NEED_RTNL,
6284 },
6285 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006286 .cmd = NL80211_CMD_SET_POWER_SAVE,
6287 .doit = nl80211_set_power_save,
6288 .policy = nl80211_policy,
6289 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006290 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6291 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006292 },
6293 {
6294 .cmd = NL80211_CMD_GET_POWER_SAVE,
6295 .doit = nl80211_get_power_save,
6296 .policy = nl80211_policy,
6297 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006298 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6299 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006300 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006301 {
6302 .cmd = NL80211_CMD_SET_CQM,
6303 .doit = nl80211_set_cqm,
6304 .policy = nl80211_policy,
6305 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006306 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6307 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006308 },
Johannes Bergf444de02010-05-05 15:25:02 +02006309 {
6310 .cmd = NL80211_CMD_SET_CHANNEL,
6311 .doit = nl80211_set_channel,
6312 .policy = nl80211_policy,
6313 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006314 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6315 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006316 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006317 {
6318 .cmd = NL80211_CMD_SET_WDS_PEER,
6319 .doit = nl80211_set_wds_peer,
6320 .policy = nl80211_policy,
6321 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006322 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6323 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006324 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006325 {
6326 .cmd = NL80211_CMD_JOIN_MESH,
6327 .doit = nl80211_join_mesh,
6328 .policy = nl80211_policy,
6329 .flags = GENL_ADMIN_PERM,
6330 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6331 NL80211_FLAG_NEED_RTNL,
6332 },
6333 {
6334 .cmd = NL80211_CMD_LEAVE_MESH,
6335 .doit = nl80211_leave_mesh,
6336 .policy = nl80211_policy,
6337 .flags = GENL_ADMIN_PERM,
6338 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6339 NL80211_FLAG_NEED_RTNL,
6340 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006341 {
6342 .cmd = NL80211_CMD_GET_WOWLAN,
6343 .doit = nl80211_get_wowlan,
6344 .policy = nl80211_policy,
6345 /* can be retrieved by unprivileged users */
6346 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6347 NL80211_FLAG_NEED_RTNL,
6348 },
6349 {
6350 .cmd = NL80211_CMD_SET_WOWLAN,
6351 .doit = nl80211_set_wowlan,
6352 .policy = nl80211_policy,
6353 .flags = GENL_ADMIN_PERM,
6354 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6355 NL80211_FLAG_NEED_RTNL,
6356 },
Johannes Berge5497d72011-07-05 16:35:40 +02006357 {
6358 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6359 .doit = nl80211_set_rekey_data,
6360 .policy = nl80211_policy,
6361 .flags = GENL_ADMIN_PERM,
6362 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6363 NL80211_FLAG_NEED_RTNL,
6364 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006365 {
6366 .cmd = NL80211_CMD_TDLS_MGMT,
6367 .doit = nl80211_tdls_mgmt,
6368 .policy = nl80211_policy,
6369 .flags = GENL_ADMIN_PERM,
6370 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6371 NL80211_FLAG_NEED_RTNL,
6372 },
6373 {
6374 .cmd = NL80211_CMD_TDLS_OPER,
6375 .doit = nl80211_tdls_oper,
6376 .policy = nl80211_policy,
6377 .flags = GENL_ADMIN_PERM,
6378 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6379 NL80211_FLAG_NEED_RTNL,
6380 },
Johannes Berg55682962007-09-20 13:09:35 -04006381};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006382
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006383static struct genl_multicast_group nl80211_mlme_mcgrp = {
6384 .name = "mlme",
6385};
Johannes Berg55682962007-09-20 13:09:35 -04006386
6387/* multicast groups */
6388static struct genl_multicast_group nl80211_config_mcgrp = {
6389 .name = "config",
6390};
Johannes Berg2a519312009-02-10 21:25:55 +01006391static struct genl_multicast_group nl80211_scan_mcgrp = {
6392 .name = "scan",
6393};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006394static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6395 .name = "regulatory",
6396};
Johannes Berg55682962007-09-20 13:09:35 -04006397
6398/* notification functions */
6399
6400void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6401{
6402 struct sk_buff *msg;
6403
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006405 if (!msg)
6406 return;
6407
6408 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6409 nlmsg_free(msg);
6410 return;
6411 }
6412
Johannes Berg463d0182009-07-14 00:33:35 +02006413 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6414 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006415}
6416
Johannes Berg362a4152009-05-24 16:43:15 +02006417static int nl80211_add_scan_req(struct sk_buff *msg,
6418 struct cfg80211_registered_device *rdev)
6419{
6420 struct cfg80211_scan_request *req = rdev->scan_req;
6421 struct nlattr *nest;
6422 int i;
6423
Johannes Berg667503dd2009-07-07 03:56:11 +02006424 ASSERT_RDEV_LOCK(rdev);
6425
Johannes Berg362a4152009-05-24 16:43:15 +02006426 if (WARN_ON(!req))
6427 return 0;
6428
6429 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6430 if (!nest)
6431 goto nla_put_failure;
6432 for (i = 0; i < req->n_ssids; i++)
6433 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6434 nla_nest_end(msg, nest);
6435
6436 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6437 if (!nest)
6438 goto nla_put_failure;
6439 for (i = 0; i < req->n_channels; i++)
6440 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6441 nla_nest_end(msg, nest);
6442
6443 if (req->ie)
6444 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6445
6446 return 0;
6447 nla_put_failure:
6448 return -ENOBUFS;
6449}
6450
Johannes Berga538e2d2009-06-16 19:56:42 +02006451static int nl80211_send_scan_msg(struct sk_buff *msg,
6452 struct cfg80211_registered_device *rdev,
6453 struct net_device *netdev,
6454 u32 pid, u32 seq, int flags,
6455 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006456{
6457 void *hdr;
6458
6459 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6460 if (!hdr)
6461 return -1;
6462
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006463 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006464 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6465
Johannes Berg362a4152009-05-24 16:43:15 +02006466 /* ignore errors and send incomplete event anyway */
6467 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006468
6469 return genlmsg_end(msg, hdr);
6470
6471 nla_put_failure:
6472 genlmsg_cancel(msg, hdr);
6473 return -EMSGSIZE;
6474}
6475
Luciano Coelho807f8a82011-05-11 17:09:35 +03006476static int
6477nl80211_send_sched_scan_msg(struct sk_buff *msg,
6478 struct cfg80211_registered_device *rdev,
6479 struct net_device *netdev,
6480 u32 pid, u32 seq, int flags, u32 cmd)
6481{
6482 void *hdr;
6483
6484 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6485 if (!hdr)
6486 return -1;
6487
6488 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6489 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6490
6491 return genlmsg_end(msg, hdr);
6492
6493 nla_put_failure:
6494 genlmsg_cancel(msg, hdr);
6495 return -EMSGSIZE;
6496}
6497
Johannes Berga538e2d2009-06-16 19:56:42 +02006498void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6499 struct net_device *netdev)
6500{
6501 struct sk_buff *msg;
6502
6503 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6504 if (!msg)
6505 return;
6506
6507 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6508 NL80211_CMD_TRIGGER_SCAN) < 0) {
6509 nlmsg_free(msg);
6510 return;
6511 }
6512
Johannes Berg463d0182009-07-14 00:33:35 +02006513 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6514 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006515}
6516
Johannes Berg2a519312009-02-10 21:25:55 +01006517void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6518 struct net_device *netdev)
6519{
6520 struct sk_buff *msg;
6521
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006522 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006523 if (!msg)
6524 return;
6525
Johannes Berga538e2d2009-06-16 19:56:42 +02006526 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6527 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006528 nlmsg_free(msg);
6529 return;
6530 }
6531
Johannes Berg463d0182009-07-14 00:33:35 +02006532 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6533 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006534}
6535
6536void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6537 struct net_device *netdev)
6538{
6539 struct sk_buff *msg;
6540
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006541 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006542 if (!msg)
6543 return;
6544
Johannes Berga538e2d2009-06-16 19:56:42 +02006545 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6546 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006547 nlmsg_free(msg);
6548 return;
6549 }
6550
Johannes Berg463d0182009-07-14 00:33:35 +02006551 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6552 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006553}
6554
Luciano Coelho807f8a82011-05-11 17:09:35 +03006555void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6556 struct net_device *netdev)
6557{
6558 struct sk_buff *msg;
6559
6560 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6561 if (!msg)
6562 return;
6563
6564 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6565 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6566 nlmsg_free(msg);
6567 return;
6568 }
6569
6570 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6571 nl80211_scan_mcgrp.id, GFP_KERNEL);
6572}
6573
6574void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6575 struct net_device *netdev, u32 cmd)
6576{
6577 struct sk_buff *msg;
6578
6579 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6580 if (!msg)
6581 return;
6582
6583 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6584 nlmsg_free(msg);
6585 return;
6586 }
6587
6588 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6589 nl80211_scan_mcgrp.id, GFP_KERNEL);
6590}
6591
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006592/*
6593 * This can happen on global regulatory changes or device specific settings
6594 * based on custom world regulatory domains.
6595 */
6596void nl80211_send_reg_change_event(struct regulatory_request *request)
6597{
6598 struct sk_buff *msg;
6599 void *hdr;
6600
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006602 if (!msg)
6603 return;
6604
6605 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6606 if (!hdr) {
6607 nlmsg_free(msg);
6608 return;
6609 }
6610
6611 /* Userspace can always count this one always being set */
6612 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6613
6614 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6615 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6616 NL80211_REGDOM_TYPE_WORLD);
6617 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6618 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6619 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6620 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6621 request->intersect)
6622 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6623 NL80211_REGDOM_TYPE_INTERSECTION);
6624 else {
6625 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6626 NL80211_REGDOM_TYPE_COUNTRY);
6627 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6628 }
6629
6630 if (wiphy_idx_valid(request->wiphy_idx))
6631 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6632
6633 if (genlmsg_end(msg, hdr) < 0) {
6634 nlmsg_free(msg);
6635 return;
6636 }
6637
Johannes Bergbc43b282009-07-25 10:54:13 +02006638 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006639 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006640 GFP_ATOMIC);
6641 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006642
6643 return;
6644
6645nla_put_failure:
6646 genlmsg_cancel(msg, hdr);
6647 nlmsg_free(msg);
6648}
6649
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006650static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6651 struct net_device *netdev,
6652 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006653 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006654{
6655 struct sk_buff *msg;
6656 void *hdr;
6657
Johannes Berge6d6e342009-07-01 21:26:47 +02006658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006659 if (!msg)
6660 return;
6661
6662 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6663 if (!hdr) {
6664 nlmsg_free(msg);
6665 return;
6666 }
6667
6668 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6669 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6670 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6671
6672 if (genlmsg_end(msg, hdr) < 0) {
6673 nlmsg_free(msg);
6674 return;
6675 }
6676
Johannes Berg463d0182009-07-14 00:33:35 +02006677 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6678 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006679 return;
6680
6681 nla_put_failure:
6682 genlmsg_cancel(msg, hdr);
6683 nlmsg_free(msg);
6684}
6685
6686void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006687 struct net_device *netdev, const u8 *buf,
6688 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006689{
6690 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006691 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006692}
6693
6694void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6695 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006696 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006697{
Johannes Berge6d6e342009-07-01 21:26:47 +02006698 nl80211_send_mlme_event(rdev, netdev, buf, len,
6699 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006700}
6701
Jouni Malinen53b46b82009-03-27 20:53:56 +02006702void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006703 struct net_device *netdev, const u8 *buf,
6704 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006705{
6706 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006707 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006708}
6709
Jouni Malinen53b46b82009-03-27 20:53:56 +02006710void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6711 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006712 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006713{
6714 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006715 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006716}
6717
Jouni Malinencf4e5942010-12-16 00:52:40 +02006718void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6719 struct net_device *netdev, const u8 *buf,
6720 size_t len, gfp_t gfp)
6721{
6722 nl80211_send_mlme_event(rdev, netdev, buf, len,
6723 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6724}
6725
6726void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6727 struct net_device *netdev, const u8 *buf,
6728 size_t len, gfp_t gfp)
6729{
6730 nl80211_send_mlme_event(rdev, netdev, buf, len,
6731 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6732}
6733
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006734static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6735 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006736 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006737{
6738 struct sk_buff *msg;
6739 void *hdr;
6740
Johannes Berge6d6e342009-07-01 21:26:47 +02006741 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006742 if (!msg)
6743 return;
6744
6745 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6746 if (!hdr) {
6747 nlmsg_free(msg);
6748 return;
6749 }
6750
6751 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6752 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6753 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6754 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6755
6756 if (genlmsg_end(msg, hdr) < 0) {
6757 nlmsg_free(msg);
6758 return;
6759 }
6760
Johannes Berg463d0182009-07-14 00:33:35 +02006761 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6762 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006763 return;
6764
6765 nla_put_failure:
6766 genlmsg_cancel(msg, hdr);
6767 nlmsg_free(msg);
6768}
6769
6770void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006771 struct net_device *netdev, const u8 *addr,
6772 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006773{
6774 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006775 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006776}
6777
6778void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006779 struct net_device *netdev, const u8 *addr,
6780 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006781{
Johannes Berge6d6e342009-07-01 21:26:47 +02006782 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6783 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006784}
6785
Samuel Ortizb23aa672009-07-01 21:26:54 +02006786void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6787 struct net_device *netdev, const u8 *bssid,
6788 const u8 *req_ie, size_t req_ie_len,
6789 const u8 *resp_ie, size_t resp_ie_len,
6790 u16 status, gfp_t gfp)
6791{
6792 struct sk_buff *msg;
6793 void *hdr;
6794
6795 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6796 if (!msg)
6797 return;
6798
6799 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6800 if (!hdr) {
6801 nlmsg_free(msg);
6802 return;
6803 }
6804
6805 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6806 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6807 if (bssid)
6808 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6809 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6810 if (req_ie)
6811 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6812 if (resp_ie)
6813 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6814
6815 if (genlmsg_end(msg, hdr) < 0) {
6816 nlmsg_free(msg);
6817 return;
6818 }
6819
Johannes Berg463d0182009-07-14 00:33:35 +02006820 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6821 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006822 return;
6823
6824 nla_put_failure:
6825 genlmsg_cancel(msg, hdr);
6826 nlmsg_free(msg);
6827
6828}
6829
6830void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6831 struct net_device *netdev, const u8 *bssid,
6832 const u8 *req_ie, size_t req_ie_len,
6833 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6834{
6835 struct sk_buff *msg;
6836 void *hdr;
6837
6838 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6839 if (!msg)
6840 return;
6841
6842 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6843 if (!hdr) {
6844 nlmsg_free(msg);
6845 return;
6846 }
6847
6848 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6849 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6850 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6851 if (req_ie)
6852 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6853 if (resp_ie)
6854 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6855
6856 if (genlmsg_end(msg, hdr) < 0) {
6857 nlmsg_free(msg);
6858 return;
6859 }
6860
Johannes Berg463d0182009-07-14 00:33:35 +02006861 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6862 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006863 return;
6864
6865 nla_put_failure:
6866 genlmsg_cancel(msg, hdr);
6867 nlmsg_free(msg);
6868
6869}
6870
6871void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6872 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02006873 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006874{
6875 struct sk_buff *msg;
6876 void *hdr;
6877
Johannes Berg667503dd2009-07-07 03:56:11 +02006878 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006879 if (!msg)
6880 return;
6881
6882 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6883 if (!hdr) {
6884 nlmsg_free(msg);
6885 return;
6886 }
6887
6888 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6889 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6890 if (from_ap && reason)
6891 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6892 if (from_ap)
6893 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6894 if (ie)
6895 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6896
6897 if (genlmsg_end(msg, hdr) < 0) {
6898 nlmsg_free(msg);
6899 return;
6900 }
6901
Johannes Berg463d0182009-07-14 00:33:35 +02006902 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6903 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006904 return;
6905
6906 nla_put_failure:
6907 genlmsg_cancel(msg, hdr);
6908 nlmsg_free(msg);
6909
6910}
6911
Johannes Berg04a773a2009-04-19 21:24:32 +02006912void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6913 struct net_device *netdev, const u8 *bssid,
6914 gfp_t gfp)
6915{
6916 struct sk_buff *msg;
6917 void *hdr;
6918
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006919 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006920 if (!msg)
6921 return;
6922
6923 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6924 if (!hdr) {
6925 nlmsg_free(msg);
6926 return;
6927 }
6928
6929 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6930 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6931 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6932
6933 if (genlmsg_end(msg, hdr) < 0) {
6934 nlmsg_free(msg);
6935 return;
6936 }
6937
Johannes Berg463d0182009-07-14 00:33:35 +02006938 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6939 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006940 return;
6941
6942 nla_put_failure:
6943 genlmsg_cancel(msg, hdr);
6944 nlmsg_free(msg);
6945}
6946
Javier Cardonac93b5e72011-04-07 15:08:34 -07006947void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6948 struct net_device *netdev,
6949 const u8 *macaddr, const u8* ie, u8 ie_len,
6950 gfp_t gfp)
6951{
6952 struct sk_buff *msg;
6953 void *hdr;
6954
6955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6956 if (!msg)
6957 return;
6958
6959 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6960 if (!hdr) {
6961 nlmsg_free(msg);
6962 return;
6963 }
6964
6965 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6966 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6967 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6968 if (ie_len && ie)
6969 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6970
6971 if (genlmsg_end(msg, hdr) < 0) {
6972 nlmsg_free(msg);
6973 return;
6974 }
6975
6976 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6977 nl80211_mlme_mcgrp.id, gfp);
6978 return;
6979
6980 nla_put_failure:
6981 genlmsg_cancel(msg, hdr);
6982 nlmsg_free(msg);
6983}
6984
Jouni Malinena3b8b052009-03-27 21:59:49 +02006985void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6986 struct net_device *netdev, const u8 *addr,
6987 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006988 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006989{
6990 struct sk_buff *msg;
6991 void *hdr;
6992
Johannes Berge6d6e342009-07-01 21:26:47 +02006993 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006994 if (!msg)
6995 return;
6996
6997 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6998 if (!hdr) {
6999 nlmsg_free(msg);
7000 return;
7001 }
7002
7003 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7004 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7005 if (addr)
7006 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7007 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007008 if (key_id != -1)
7009 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007010 if (tsc)
7011 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7012
7013 if (genlmsg_end(msg, hdr) < 0) {
7014 nlmsg_free(msg);
7015 return;
7016 }
7017
Johannes Berg463d0182009-07-14 00:33:35 +02007018 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7019 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007020 return;
7021
7022 nla_put_failure:
7023 genlmsg_cancel(msg, hdr);
7024 nlmsg_free(msg);
7025}
7026
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007027void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7028 struct ieee80211_channel *channel_before,
7029 struct ieee80211_channel *channel_after)
7030{
7031 struct sk_buff *msg;
7032 void *hdr;
7033 struct nlattr *nl_freq;
7034
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007036 if (!msg)
7037 return;
7038
7039 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7040 if (!hdr) {
7041 nlmsg_free(msg);
7042 return;
7043 }
7044
7045 /*
7046 * Since we are applying the beacon hint to a wiphy we know its
7047 * wiphy_idx is valid
7048 */
7049 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7050
7051 /* Before */
7052 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7053 if (!nl_freq)
7054 goto nla_put_failure;
7055 if (nl80211_msg_put_channel(msg, channel_before))
7056 goto nla_put_failure;
7057 nla_nest_end(msg, nl_freq);
7058
7059 /* After */
7060 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7061 if (!nl_freq)
7062 goto nla_put_failure;
7063 if (nl80211_msg_put_channel(msg, channel_after))
7064 goto nla_put_failure;
7065 nla_nest_end(msg, nl_freq);
7066
7067 if (genlmsg_end(msg, hdr) < 0) {
7068 nlmsg_free(msg);
7069 return;
7070 }
7071
Johannes Berg463d0182009-07-14 00:33:35 +02007072 rcu_read_lock();
7073 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7074 GFP_ATOMIC);
7075 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007076
7077 return;
7078
7079nla_put_failure:
7080 genlmsg_cancel(msg, hdr);
7081 nlmsg_free(msg);
7082}
7083
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007084static void nl80211_send_remain_on_chan_event(
7085 int cmd, struct cfg80211_registered_device *rdev,
7086 struct net_device *netdev, u64 cookie,
7087 struct ieee80211_channel *chan,
7088 enum nl80211_channel_type channel_type,
7089 unsigned int duration, gfp_t gfp)
7090{
7091 struct sk_buff *msg;
7092 void *hdr;
7093
7094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7095 if (!msg)
7096 return;
7097
7098 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7099 if (!hdr) {
7100 nlmsg_free(msg);
7101 return;
7102 }
7103
7104 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7105 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7106 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7107 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7108 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7109
7110 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7111 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7112
7113 if (genlmsg_end(msg, hdr) < 0) {
7114 nlmsg_free(msg);
7115 return;
7116 }
7117
7118 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7119 nl80211_mlme_mcgrp.id, gfp);
7120 return;
7121
7122 nla_put_failure:
7123 genlmsg_cancel(msg, hdr);
7124 nlmsg_free(msg);
7125}
7126
7127void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7128 struct net_device *netdev, u64 cookie,
7129 struct ieee80211_channel *chan,
7130 enum nl80211_channel_type channel_type,
7131 unsigned int duration, gfp_t gfp)
7132{
7133 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7134 rdev, netdev, cookie, chan,
7135 channel_type, duration, gfp);
7136}
7137
7138void nl80211_send_remain_on_channel_cancel(
7139 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7140 u64 cookie, struct ieee80211_channel *chan,
7141 enum nl80211_channel_type channel_type, gfp_t gfp)
7142{
7143 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7144 rdev, netdev, cookie, chan,
7145 channel_type, 0, gfp);
7146}
7147
Johannes Berg98b62182009-12-23 13:15:44 +01007148void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7149 struct net_device *dev, const u8 *mac_addr,
7150 struct station_info *sinfo, gfp_t gfp)
7151{
7152 struct sk_buff *msg;
7153
7154 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7155 if (!msg)
7156 return;
7157
7158 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7159 nlmsg_free(msg);
7160 return;
7161 }
7162
7163 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7164 nl80211_mlme_mcgrp.id, gfp);
7165}
7166
Jouni Malinenec15e682011-03-23 15:29:52 +02007167void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7168 struct net_device *dev, const u8 *mac_addr,
7169 gfp_t gfp)
7170{
7171 struct sk_buff *msg;
7172 void *hdr;
7173
7174 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7175 if (!msg)
7176 return;
7177
7178 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7179 if (!hdr) {
7180 nlmsg_free(msg);
7181 return;
7182 }
7183
7184 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7185 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7186
7187 if (genlmsg_end(msg, hdr) < 0) {
7188 nlmsg_free(msg);
7189 return;
7190 }
7191
7192 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7193 nl80211_mlme_mcgrp.id, gfp);
7194 return;
7195
7196 nla_put_failure:
7197 genlmsg_cancel(msg, hdr);
7198 nlmsg_free(msg);
7199}
7200
Johannes Berg2e161f72010-08-12 15:38:38 +02007201int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7202 struct net_device *netdev, u32 nlpid,
7203 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007204{
7205 struct sk_buff *msg;
7206 void *hdr;
7207 int err;
7208
7209 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7210 if (!msg)
7211 return -ENOMEM;
7212
Johannes Berg2e161f72010-08-12 15:38:38 +02007213 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007214 if (!hdr) {
7215 nlmsg_free(msg);
7216 return -ENOMEM;
7217 }
7218
7219 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7220 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7221 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7222 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7223
7224 err = genlmsg_end(msg, hdr);
7225 if (err < 0) {
7226 nlmsg_free(msg);
7227 return err;
7228 }
7229
7230 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7231 if (err < 0)
7232 return err;
7233 return 0;
7234
7235 nla_put_failure:
7236 genlmsg_cancel(msg, hdr);
7237 nlmsg_free(msg);
7238 return -ENOBUFS;
7239}
7240
Johannes Berg2e161f72010-08-12 15:38:38 +02007241void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7242 struct net_device *netdev, u64 cookie,
7243 const u8 *buf, size_t len, bool ack,
7244 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007245{
7246 struct sk_buff *msg;
7247 void *hdr;
7248
7249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7250 if (!msg)
7251 return;
7252
Johannes Berg2e161f72010-08-12 15:38:38 +02007253 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007254 if (!hdr) {
7255 nlmsg_free(msg);
7256 return;
7257 }
7258
7259 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7260 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7261 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7262 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7263 if (ack)
7264 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7265
7266 if (genlmsg_end(msg, hdr) < 0) {
7267 nlmsg_free(msg);
7268 return;
7269 }
7270
7271 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7272 return;
7273
7274 nla_put_failure:
7275 genlmsg_cancel(msg, hdr);
7276 nlmsg_free(msg);
7277}
7278
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007279void
7280nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7281 struct net_device *netdev,
7282 enum nl80211_cqm_rssi_threshold_event rssi_event,
7283 gfp_t gfp)
7284{
7285 struct sk_buff *msg;
7286 struct nlattr *pinfoattr;
7287 void *hdr;
7288
7289 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7290 if (!msg)
7291 return;
7292
7293 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7294 if (!hdr) {
7295 nlmsg_free(msg);
7296 return;
7297 }
7298
7299 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7300 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7301
7302 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7303 if (!pinfoattr)
7304 goto nla_put_failure;
7305
7306 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7307 rssi_event);
7308
7309 nla_nest_end(msg, pinfoattr);
7310
7311 if (genlmsg_end(msg, hdr) < 0) {
7312 nlmsg_free(msg);
7313 return;
7314 }
7315
7316 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7317 nl80211_mlme_mcgrp.id, gfp);
7318 return;
7319
7320 nla_put_failure:
7321 genlmsg_cancel(msg, hdr);
7322 nlmsg_free(msg);
7323}
7324
Johannes Berge5497d72011-07-05 16:35:40 +02007325void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7326 struct net_device *netdev, const u8 *bssid,
7327 const u8 *replay_ctr, gfp_t gfp)
7328{
7329 struct sk_buff *msg;
7330 struct nlattr *rekey_attr;
7331 void *hdr;
7332
7333 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7334 if (!msg)
7335 return;
7336
7337 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7338 if (!hdr) {
7339 nlmsg_free(msg);
7340 return;
7341 }
7342
7343 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7344 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7345 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7346
7347 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7348 if (!rekey_attr)
7349 goto nla_put_failure;
7350
7351 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7352 NL80211_REPLAY_CTR_LEN, replay_ctr);
7353
7354 nla_nest_end(msg, rekey_attr);
7355
7356 if (genlmsg_end(msg, hdr) < 0) {
7357 nlmsg_free(msg);
7358 return;
7359 }
7360
7361 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7362 nl80211_mlme_mcgrp.id, gfp);
7363 return;
7364
7365 nla_put_failure:
7366 genlmsg_cancel(msg, hdr);
7367 nlmsg_free(msg);
7368}
7369
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007370void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7371 struct net_device *netdev, int index,
7372 const u8 *bssid, bool preauth, gfp_t gfp)
7373{
7374 struct sk_buff *msg;
7375 struct nlattr *attr;
7376 void *hdr;
7377
7378 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7379 if (!msg)
7380 return;
7381
7382 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7383 if (!hdr) {
7384 nlmsg_free(msg);
7385 return;
7386 }
7387
7388 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7389 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7390
7391 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7392 if (!attr)
7393 goto nla_put_failure;
7394
7395 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7396 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7397 if (preauth)
7398 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7399
7400 nla_nest_end(msg, attr);
7401
7402 if (genlmsg_end(msg, hdr) < 0) {
7403 nlmsg_free(msg);
7404 return;
7405 }
7406
7407 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7408 nl80211_mlme_mcgrp.id, gfp);
7409 return;
7410
7411 nla_put_failure:
7412 genlmsg_cancel(msg, hdr);
7413 nlmsg_free(msg);
7414}
7415
Johannes Bergc063dbf2010-11-24 08:10:05 +01007416void
7417nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7418 struct net_device *netdev, const u8 *peer,
7419 u32 num_packets, gfp_t gfp)
7420{
7421 struct sk_buff *msg;
7422 struct nlattr *pinfoattr;
7423 void *hdr;
7424
7425 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7426 if (!msg)
7427 return;
7428
7429 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7430 if (!hdr) {
7431 nlmsg_free(msg);
7432 return;
7433 }
7434
7435 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7436 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7437 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7438
7439 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7440 if (!pinfoattr)
7441 goto nla_put_failure;
7442
7443 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7444
7445 nla_nest_end(msg, pinfoattr);
7446
7447 if (genlmsg_end(msg, hdr) < 0) {
7448 nlmsg_free(msg);
7449 return;
7450 }
7451
7452 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7453 nl80211_mlme_mcgrp.id, gfp);
7454 return;
7455
7456 nla_put_failure:
7457 genlmsg_cancel(msg, hdr);
7458 nlmsg_free(msg);
7459}
7460
Jouni Malinen026331c2010-02-15 12:53:10 +02007461static int nl80211_netlink_notify(struct notifier_block * nb,
7462 unsigned long state,
7463 void *_notify)
7464{
7465 struct netlink_notify *notify = _notify;
7466 struct cfg80211_registered_device *rdev;
7467 struct wireless_dev *wdev;
7468
7469 if (state != NETLINK_URELEASE)
7470 return NOTIFY_DONE;
7471
7472 rcu_read_lock();
7473
7474 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7475 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007476 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007477
7478 rcu_read_unlock();
7479
7480 return NOTIFY_DONE;
7481}
7482
7483static struct notifier_block nl80211_netlink_notifier = {
7484 .notifier_call = nl80211_netlink_notify,
7485};
7486
Johannes Berg55682962007-09-20 13:09:35 -04007487/* initialisation/exit functions */
7488
7489int nl80211_init(void)
7490{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007491 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007492
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007493 err = genl_register_family_with_ops(&nl80211_fam,
7494 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007495 if (err)
7496 return err;
7497
Johannes Berg55682962007-09-20 13:09:35 -04007498 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7499 if (err)
7500 goto err_out;
7501
Johannes Berg2a519312009-02-10 21:25:55 +01007502 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7503 if (err)
7504 goto err_out;
7505
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007506 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7507 if (err)
7508 goto err_out;
7509
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007510 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7511 if (err)
7512 goto err_out;
7513
Johannes Bergaff89a92009-07-01 21:26:51 +02007514#ifdef CONFIG_NL80211_TESTMODE
7515 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7516 if (err)
7517 goto err_out;
7518#endif
7519
Jouni Malinen026331c2010-02-15 12:53:10 +02007520 err = netlink_register_notifier(&nl80211_netlink_notifier);
7521 if (err)
7522 goto err_out;
7523
Johannes Berg55682962007-09-20 13:09:35 -04007524 return 0;
7525 err_out:
7526 genl_unregister_family(&nl80211_fam);
7527 return err;
7528}
7529
7530void nl80211_exit(void)
7531{
Jouni Malinen026331c2010-02-15 12:53:10 +02007532 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007533 genl_unregister_family(&nl80211_fam);
7534}