blob: 9755b3f04dd74a40812287060e6b7b1057b8dcd7 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020050static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020051 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040052 struct net_device **dev)
53{
Johannes Berg463d0182009-07-14 00:33:35 +020054 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040055 int ifindex;
56
Johannes Bergbba95fe2008-07-29 13:22:51 +020057 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040058 return -EINVAL;
59
Johannes Bergbba95fe2008-07-29 13:22:51 +020060 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020061 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040062 if (!*dev)
63 return -ENODEV;
64
Johannes Berg463d0182009-07-14 00:33:35 +020065 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020066 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040067 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020068 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040069 }
70
71 return 0;
72}
73
74/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000075static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040076 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
77 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070078 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020079 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020080 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053081 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020082 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
84 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010086 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040087
88 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
90 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010091
92 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020093 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010094
Johannes Bergb9454e82009-07-08 13:29:08 +020095 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010096 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
97 .len = WLAN_MAX_KEY_LEN },
98 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
99 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
100 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200102 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100103
104 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
105 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
106 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
107 .len = IEEE80211_MAX_DATA_LEN },
108 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
109 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
111 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
112 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
113 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100115 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100116 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200117 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100118 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
119 .len = IEEE80211_MAX_MESH_ID_LEN },
120 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300121
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700122 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
123 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
124
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300125 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200128 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
129 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100130 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300131
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800132 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700133 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700134
Johannes Berg6c739412011-11-03 09:27:01 +0100135 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200136
137 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
138 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
139 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100140 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
141 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200142
143 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
144 .len = IEEE80211_MAX_SSID_LEN },
145 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
146 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200147 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300148 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300149 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300150 [NL80211_ATTR_STA_FLAGS2] = {
151 .len = sizeof(struct nl80211_sta_flag_update),
152 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300153 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300154 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
155 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200156 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
157 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
158 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200159 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100160 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100161 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
162 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100163 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
164 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200165 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200166 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
167 .len = IEEE80211_MAX_DATA_LEN },
168 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200169 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200170 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300171 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200172 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300173 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
174 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200175 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900176 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
177 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100178 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100179 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100180 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200181 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700182 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300183 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200184 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200185 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300186 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300187 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
188 .len = IEEE80211_MAX_DATA_LEN },
189 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
190 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530191 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300192 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530193 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300194 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
197 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
198 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100199 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200200 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
201 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700202 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400203};
204
Johannes Berge31b8212010-10-05 19:39:30 +0200205/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000206static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200207 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200208 [NL80211_KEY_IDX] = { .type = NLA_U8 },
209 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200210 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200211 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
212 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200213 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100214 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
215};
216
217/* policy for the key default flags */
218static const struct nla_policy
219nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
220 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
221 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200222};
223
Johannes Bergff1b6e62011-05-04 15:37:28 +0200224/* policy for WoWLAN attributes */
225static const struct nla_policy
226nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
227 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
228 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
229 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
230 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200231 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
232 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
233 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200235};
236
Johannes Berge5497d72011-07-05 16:35:40 +0200237/* policy for GTK rekey offload attributes */
238static const struct nla_policy
239nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
240 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
241 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
242 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
243};
244
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300245static const struct nla_policy
246nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
247 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
248 .len = IEEE80211_MAX_SSID_LEN },
249};
250
Holger Schuriga0438972009-11-11 11:30:02 +0100251/* ifidx get helper */
252static int nl80211_get_ifidx(struct netlink_callback *cb)
253{
254 int res;
255
256 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
257 nl80211_fam.attrbuf, nl80211_fam.maxattr,
258 nl80211_policy);
259 if (res)
260 return res;
261
262 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
263 return -EINVAL;
264
265 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
266 if (!res)
267 return -EINVAL;
268 return res;
269}
270
Johannes Berg67748892010-10-04 21:14:06 +0200271static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
272 struct netlink_callback *cb,
273 struct cfg80211_registered_device **rdev,
274 struct net_device **dev)
275{
276 int ifidx = cb->args[0];
277 int err;
278
279 if (!ifidx)
280 ifidx = nl80211_get_ifidx(cb);
281 if (ifidx < 0)
282 return ifidx;
283
284 cb->args[0] = ifidx;
285
286 rtnl_lock();
287
288 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
289 if (!*dev) {
290 err = -ENODEV;
291 goto out_rtnl;
292 }
293
294 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100295 if (IS_ERR(*rdev)) {
296 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200297 goto out_rtnl;
298 }
299
300 return 0;
301 out_rtnl:
302 rtnl_unlock();
303 return err;
304}
305
306static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
307{
308 cfg80211_unlock_rdev(rdev);
309 rtnl_unlock();
310}
311
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100312/* IE validation */
313static bool is_valid_ie_attr(const struct nlattr *attr)
314{
315 const u8 *pos;
316 int len;
317
318 if (!attr)
319 return true;
320
321 pos = nla_data(attr);
322 len = nla_len(attr);
323
324 while (len) {
325 u8 elemlen;
326
327 if (len < 2)
328 return false;
329 len -= 2;
330
331 elemlen = pos[1];
332 if (elemlen > len)
333 return false;
334
335 len -= elemlen;
336 pos += 2 + elemlen;
337 }
338
339 return true;
340}
341
Johannes Berg55682962007-09-20 13:09:35 -0400342/* message building helper */
343static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
344 int flags, u8 cmd)
345{
346 /* since there is no private header just add the generic one */
347 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
348}
349
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400350static int nl80211_msg_put_channel(struct sk_buff *msg,
351 struct ieee80211_channel *chan)
352{
353 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
354 chan->center_freq);
355
356 if (chan->flags & IEEE80211_CHAN_DISABLED)
357 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
358 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
359 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
360 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
361 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
362 if (chan->flags & IEEE80211_CHAN_RADAR)
363 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
364
365 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
366 DBM_TO_MBM(chan->max_power));
367
368 return 0;
369
370 nla_put_failure:
371 return -ENOBUFS;
372}
373
Johannes Berg55682962007-09-20 13:09:35 -0400374/* netlink command implementations */
375
Johannes Bergb9454e82009-07-08 13:29:08 +0200376struct key_parse {
377 struct key_params p;
378 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200379 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200380 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100381 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200382};
383
384static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
385{
386 struct nlattr *tb[NL80211_KEY_MAX + 1];
387 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
388 nl80211_key_policy);
389 if (err)
390 return err;
391
392 k->def = !!tb[NL80211_KEY_DEFAULT];
393 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
394
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100395 if (k->def) {
396 k->def_uni = true;
397 k->def_multi = true;
398 }
399 if (k->defmgmt)
400 k->def_multi = true;
401
Johannes Bergb9454e82009-07-08 13:29:08 +0200402 if (tb[NL80211_KEY_IDX])
403 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
404
405 if (tb[NL80211_KEY_DATA]) {
406 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
407 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
408 }
409
410 if (tb[NL80211_KEY_SEQ]) {
411 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
412 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
413 }
414
415 if (tb[NL80211_KEY_CIPHER])
416 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
417
Johannes Berge31b8212010-10-05 19:39:30 +0200418 if (tb[NL80211_KEY_TYPE]) {
419 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
420 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
421 return -EINVAL;
422 }
423
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100424 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
425 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
426 int err = nla_parse_nested(kdt,
427 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
428 tb[NL80211_KEY_DEFAULT_TYPES],
429 nl80211_key_default_policy);
430 if (err)
431 return err;
432
433 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
434 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
435 }
436
Johannes Bergb9454e82009-07-08 13:29:08 +0200437 return 0;
438}
439
440static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
441{
442 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
443 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
444 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
445 }
446
447 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
448 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
449 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
450 }
451
452 if (info->attrs[NL80211_ATTR_KEY_IDX])
453 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
454
455 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
456 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
457
458 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
459 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
460
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100461 if (k->def) {
462 k->def_uni = true;
463 k->def_multi = true;
464 }
465 if (k->defmgmt)
466 k->def_multi = true;
467
Johannes Berge31b8212010-10-05 19:39:30 +0200468 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
469 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
470 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
471 return -EINVAL;
472 }
473
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100474 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
475 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
476 int err = nla_parse_nested(
477 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
478 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
479 nl80211_key_default_policy);
480 if (err)
481 return err;
482
483 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
484 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
485 }
486
Johannes Bergb9454e82009-07-08 13:29:08 +0200487 return 0;
488}
489
490static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
491{
492 int err;
493
494 memset(k, 0, sizeof(*k));
495 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200496 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200497
498 if (info->attrs[NL80211_ATTR_KEY])
499 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
500 else
501 err = nl80211_parse_key_old(info, k);
502
503 if (err)
504 return err;
505
506 if (k->def && k->defmgmt)
507 return -EINVAL;
508
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100509 if (k->defmgmt) {
510 if (k->def_uni || !k->def_multi)
511 return -EINVAL;
512 }
513
Johannes Bergb9454e82009-07-08 13:29:08 +0200514 if (k->idx != -1) {
515 if (k->defmgmt) {
516 if (k->idx < 4 || k->idx > 5)
517 return -EINVAL;
518 } else if (k->def) {
519 if (k->idx < 0 || k->idx > 3)
520 return -EINVAL;
521 } else {
522 if (k->idx < 0 || k->idx > 5)
523 return -EINVAL;
524 }
525 }
526
527 return 0;
528}
529
Johannes Bergfffd0932009-07-08 14:22:54 +0200530static struct cfg80211_cached_keys *
531nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
532 struct nlattr *keys)
533{
534 struct key_parse parse;
535 struct nlattr *key;
536 struct cfg80211_cached_keys *result;
537 int rem, err, def = 0;
538
539 result = kzalloc(sizeof(*result), GFP_KERNEL);
540 if (!result)
541 return ERR_PTR(-ENOMEM);
542
543 result->def = -1;
544 result->defmgmt = -1;
545
546 nla_for_each_nested(key, keys, rem) {
547 memset(&parse, 0, sizeof(parse));
548 parse.idx = -1;
549
550 err = nl80211_parse_key_new(key, &parse);
551 if (err)
552 goto error;
553 err = -EINVAL;
554 if (!parse.p.key)
555 goto error;
556 if (parse.idx < 0 || parse.idx > 4)
557 goto error;
558 if (parse.def) {
559 if (def)
560 goto error;
561 def = 1;
562 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100563 if (!parse.def_uni || !parse.def_multi)
564 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200565 } else if (parse.defmgmt)
566 goto error;
567 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200568 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200569 if (err)
570 goto error;
571 result->params[parse.idx].cipher = parse.p.cipher;
572 result->params[parse.idx].key_len = parse.p.key_len;
573 result->params[parse.idx].key = result->data[parse.idx];
574 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
575 }
576
577 return result;
578 error:
579 kfree(result);
580 return ERR_PTR(err);
581}
582
583static int nl80211_key_allowed(struct wireless_dev *wdev)
584{
585 ASSERT_WDEV_LOCK(wdev);
586
Johannes Bergfffd0932009-07-08 14:22:54 +0200587 switch (wdev->iftype) {
588 case NL80211_IFTYPE_AP:
589 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200590 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700591 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200592 break;
593 case NL80211_IFTYPE_ADHOC:
594 if (!wdev->current_bss)
595 return -ENOLINK;
596 break;
597 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200598 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200599 if (wdev->sme_state != CFG80211_SME_CONNECTED)
600 return -ENOLINK;
601 break;
602 default:
603 return -EINVAL;
604 }
605
606 return 0;
607}
608
Johannes Berg7527a782011-05-13 10:58:57 +0200609static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
610{
611 struct nlattr *nl_modes = nla_nest_start(msg, attr);
612 int i;
613
614 if (!nl_modes)
615 goto nla_put_failure;
616
617 i = 0;
618 while (ifmodes) {
619 if (ifmodes & 1)
620 NLA_PUT_FLAG(msg, i);
621 ifmodes >>= 1;
622 i++;
623 }
624
625 nla_nest_end(msg, nl_modes);
626 return 0;
627
628nla_put_failure:
629 return -ENOBUFS;
630}
631
632static int nl80211_put_iface_combinations(struct wiphy *wiphy,
633 struct sk_buff *msg)
634{
635 struct nlattr *nl_combis;
636 int i, j;
637
638 nl_combis = nla_nest_start(msg,
639 NL80211_ATTR_INTERFACE_COMBINATIONS);
640 if (!nl_combis)
641 goto nla_put_failure;
642
643 for (i = 0; i < wiphy->n_iface_combinations; i++) {
644 const struct ieee80211_iface_combination *c;
645 struct nlattr *nl_combi, *nl_limits;
646
647 c = &wiphy->iface_combinations[i];
648
649 nl_combi = nla_nest_start(msg, i + 1);
650 if (!nl_combi)
651 goto nla_put_failure;
652
653 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
654 if (!nl_limits)
655 goto nla_put_failure;
656
657 for (j = 0; j < c->n_limits; j++) {
658 struct nlattr *nl_limit;
659
660 nl_limit = nla_nest_start(msg, j + 1);
661 if (!nl_limit)
662 goto nla_put_failure;
663 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
664 c->limits[j].max);
665 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
666 c->limits[j].types))
667 goto nla_put_failure;
668 nla_nest_end(msg, nl_limit);
669 }
670
671 nla_nest_end(msg, nl_limits);
672
673 if (c->beacon_int_infra_match)
674 NLA_PUT_FLAG(msg,
675 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
676 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
677 c->num_different_channels);
678 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
679 c->max_interfaces);
680
681 nla_nest_end(msg, nl_combi);
682 }
683
684 nla_nest_end(msg, nl_combis);
685
686 return 0;
687nla_put_failure:
688 return -ENOBUFS;
689}
690
Johannes Berg55682962007-09-20 13:09:35 -0400691static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
692 struct cfg80211_registered_device *dev)
693{
694 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100695 struct nlattr *nl_bands, *nl_band;
696 struct nlattr *nl_freqs, *nl_freq;
697 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100698 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100699 enum ieee80211_band band;
700 struct ieee80211_channel *chan;
701 struct ieee80211_rate *rate;
702 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200703 const struct ieee80211_txrx_stypes *mgmt_stypes =
704 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400705
706 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
707 if (!hdr)
708 return -1;
709
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500710 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400711 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200712
Johannes Bergf5ea9122009-08-07 16:17:38 +0200713 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
714 cfg80211_rdev_list_generation);
715
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200716 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
717 dev->wiphy.retry_short);
718 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
719 dev->wiphy.retry_long);
720 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
721 dev->wiphy.frag_threshold);
722 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
723 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100724 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
725 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100726 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
727 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300728 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
729 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200730 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
731 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300732 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
733 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300734 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
735 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100736
Johannes Berge31b8212010-10-05 19:39:30 +0200737 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
738 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700739 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
740 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300741 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
742 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530743 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
744 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300745 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
746 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
747 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
748 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530749
Johannes Berg25e47c12009-04-02 20:14:06 +0200750 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
751 sizeof(u32) * dev->wiphy.n_cipher_suites,
752 dev->wiphy.cipher_suites);
753
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100754 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
755 dev->wiphy.max_num_pmkids);
756
Johannes Bergc0692b82010-08-27 14:26:53 +0300757 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
758 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
759
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
761 dev->wiphy.available_antennas_tx);
762 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
763 dev->wiphy.available_antennas_rx);
764
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200765 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
766 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
767 dev->wiphy.probe_resp_offload);
768
Bruno Randolf7f531e02010-12-16 11:30:22 +0900769 if ((dev->wiphy.available_antennas_tx ||
770 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900771 u32 tx_ant = 0, rx_ant = 0;
772 int res;
773 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
774 if (!res) {
775 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
776 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
777 }
778 }
779
Johannes Berg7527a782011-05-13 10:58:57 +0200780 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
781 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700782 goto nla_put_failure;
783
Johannes Bergee688b002008-01-24 19:38:39 +0100784 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
785 if (!nl_bands)
786 goto nla_put_failure;
787
788 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
789 if (!dev->wiphy.bands[band])
790 continue;
791
792 nl_band = nla_nest_start(msg, band);
793 if (!nl_band)
794 goto nla_put_failure;
795
Johannes Bergd51626d2008-10-09 12:20:13 +0200796 /* add HT info */
797 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
798 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
799 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
800 &dev->wiphy.bands[band]->ht_cap.mcs);
801 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
802 dev->wiphy.bands[band]->ht_cap.cap);
803 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
804 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
805 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
806 dev->wiphy.bands[band]->ht_cap.ampdu_density);
807 }
808
Johannes Bergee688b002008-01-24 19:38:39 +0100809 /* add frequencies */
810 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
811 if (!nl_freqs)
812 goto nla_put_failure;
813
814 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
815 nl_freq = nla_nest_start(msg, i);
816 if (!nl_freq)
817 goto nla_put_failure;
818
819 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100820
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400821 if (nl80211_msg_put_channel(msg, chan))
822 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200823
Johannes Bergee688b002008-01-24 19:38:39 +0100824 nla_nest_end(msg, nl_freq);
825 }
826
827 nla_nest_end(msg, nl_freqs);
828
829 /* add bitrates */
830 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
831 if (!nl_rates)
832 goto nla_put_failure;
833
834 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
835 nl_rate = nla_nest_start(msg, i);
836 if (!nl_rate)
837 goto nla_put_failure;
838
839 rate = &dev->wiphy.bands[band]->bitrates[i];
840 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
841 rate->bitrate);
842 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
843 NLA_PUT_FLAG(msg,
844 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
845
846 nla_nest_end(msg, nl_rate);
847 }
848
849 nla_nest_end(msg, nl_rates);
850
851 nla_nest_end(msg, nl_band);
852 }
853 nla_nest_end(msg, nl_bands);
854
Johannes Berg8fdc6212009-03-14 09:34:01 +0100855 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
856 if (!nl_cmds)
857 goto nla_put_failure;
858
859 i = 0;
860#define CMD(op, n) \
861 do { \
862 if (dev->ops->op) { \
863 i++; \
864 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
865 } \
866 } while (0)
867
868 CMD(add_virtual_intf, NEW_INTERFACE);
869 CMD(change_virtual_intf, SET_INTERFACE);
870 CMD(add_key, NEW_KEY);
871 CMD(add_beacon, NEW_BEACON);
872 CMD(add_station, NEW_STATION);
873 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800874 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100875 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200876 CMD(auth, AUTHENTICATE);
877 CMD(assoc, ASSOCIATE);
878 CMD(deauth, DEAUTHENTICATE);
879 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200880 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100881 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100882 CMD(set_pmksa, SET_PMKSA);
883 CMD(del_pmksa, DEL_PMKSA);
884 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100885 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
886 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200887 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200888 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100889 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100890 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200891 i++;
892 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
893 }
Johannes Bergf444de02010-05-05 15:25:02 +0200894 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400895 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300896 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
897 CMD(tdls_mgmt, TDLS_MGMT);
898 CMD(tdls_oper, TDLS_OPER);
899 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300900 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
901 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100902 CMD(probe_client, PROBE_CLIENT);
Johannes Berg5e760232011-11-04 11:18:17 +0100903 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
904 i++;
905 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
906 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100907
Kalle Valo4745fc02011-11-17 19:06:10 +0200908#ifdef CONFIG_NL80211_TESTMODE
909 CMD(testmode_cmd, TESTMODE);
910#endif
911
Johannes Berg8fdc6212009-03-14 09:34:01 +0100912#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200913
Johannes Berg6829c872009-07-02 09:13:27 +0200914 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200915 i++;
916 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
917 }
918
Johannes Berg6829c872009-07-02 09:13:27 +0200919 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200920 i++;
921 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
922 }
923
Johannes Berg8fdc6212009-03-14 09:34:01 +0100924 nla_nest_end(msg, nl_cmds);
925
Johannes Berg7c4ef712011-11-18 15:33:48 +0100926 if (dev->ops->remain_on_channel &&
927 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100928 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
929 dev->wiphy.max_remain_on_channel_duration);
930
Johannes Berg7c4ef712011-11-18 15:33:48 +0100931 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100932 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
933
Johannes Berg2e161f72010-08-12 15:38:38 +0200934 if (mgmt_stypes) {
935 u16 stypes;
936 struct nlattr *nl_ftypes, *nl_ifs;
937 enum nl80211_iftype ift;
938
939 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
940 if (!nl_ifs)
941 goto nla_put_failure;
942
943 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
944 nl_ftypes = nla_nest_start(msg, ift);
945 if (!nl_ftypes)
946 goto nla_put_failure;
947 i = 0;
948 stypes = mgmt_stypes[ift].tx;
949 while (stypes) {
950 if (stypes & 1)
951 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
952 (i << 4) | IEEE80211_FTYPE_MGMT);
953 stypes >>= 1;
954 i++;
955 }
956 nla_nest_end(msg, nl_ftypes);
957 }
958
Johannes Berg74b70a42010-08-24 12:15:53 +0200959 nla_nest_end(msg, nl_ifs);
960
Johannes Berg2e161f72010-08-12 15:38:38 +0200961 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
962 if (!nl_ifs)
963 goto nla_put_failure;
964
965 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
966 nl_ftypes = nla_nest_start(msg, ift);
967 if (!nl_ftypes)
968 goto nla_put_failure;
969 i = 0;
970 stypes = mgmt_stypes[ift].rx;
971 while (stypes) {
972 if (stypes & 1)
973 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
974 (i << 4) | IEEE80211_FTYPE_MGMT);
975 stypes >>= 1;
976 i++;
977 }
978 nla_nest_end(msg, nl_ftypes);
979 }
980 nla_nest_end(msg, nl_ifs);
981 }
982
Johannes Bergff1b6e62011-05-04 15:37:28 +0200983 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
984 struct nlattr *nl_wowlan;
985
986 nl_wowlan = nla_nest_start(msg,
987 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
988 if (!nl_wowlan)
989 goto nla_put_failure;
990
991 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
992 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
993 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
994 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
995 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
996 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200997 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
998 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
999 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1000 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1001 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1002 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1003 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1004 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1005 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1006 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001007 if (dev->wiphy.wowlan.n_patterns) {
1008 struct nl80211_wowlan_pattern_support pat = {
1009 .max_patterns = dev->wiphy.wowlan.n_patterns,
1010 .min_pattern_len =
1011 dev->wiphy.wowlan.pattern_min_len,
1012 .max_pattern_len =
1013 dev->wiphy.wowlan.pattern_max_len,
1014 };
1015 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1016 sizeof(pat), &pat);
1017 }
1018
1019 nla_nest_end(msg, nl_wowlan);
1020 }
1021
Johannes Berg7527a782011-05-13 10:58:57 +02001022 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1023 dev->wiphy.software_iftypes))
1024 goto nla_put_failure;
1025
1026 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1027 goto nla_put_failure;
1028
Johannes Berg562a7482011-11-07 12:39:33 +01001029 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1030 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1031 dev->wiphy.ap_sme_capa);
1032
Johannes Berg1f074bd2011-11-06 14:13:33 +01001033 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1034
Johannes Berg55682962007-09-20 13:09:35 -04001035 return genlmsg_end(msg, hdr);
1036
1037 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001038 genlmsg_cancel(msg, hdr);
1039 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001040}
1041
1042static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1043{
1044 int idx = 0;
1045 int start = cb->args[0];
1046 struct cfg80211_registered_device *dev;
1047
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001048 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001049 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001050 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1051 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001052 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001053 continue;
1054 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1055 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001056 dev) < 0) {
1057 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001058 break;
Julius Volzb4637272008-07-08 14:02:19 +02001059 }
Johannes Berg55682962007-09-20 13:09:35 -04001060 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001061 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001062
1063 cb->args[0] = idx;
1064
1065 return skb->len;
1066}
1067
1068static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1069{
1070 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001071 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001072
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001073 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001074 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001075 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001076
Johannes Berg4c476992010-10-04 21:36:35 +02001077 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1078 nlmsg_free(msg);
1079 return -ENOBUFS;
1080 }
Johannes Berg55682962007-09-20 13:09:35 -04001081
Johannes Berg134e6372009-07-10 09:51:34 +00001082 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001083}
1084
Jouni Malinen31888482008-10-30 16:59:24 +02001085static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1086 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1087 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1088 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1089 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1090 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1091};
1092
1093static int parse_txq_params(struct nlattr *tb[],
1094 struct ieee80211_txq_params *txq_params)
1095{
1096 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1097 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1098 !tb[NL80211_TXQ_ATTR_AIFS])
1099 return -EINVAL;
1100
1101 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1102 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1103 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1104 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1105 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1106
1107 return 0;
1108}
1109
Johannes Bergf444de02010-05-05 15:25:02 +02001110static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1111{
1112 /*
1113 * You can only set the channel explicitly for AP, mesh
1114 * and WDS type interfaces; all others have their channel
1115 * managed via their respective "establish a connection"
1116 * command (connect, join, ...)
1117 *
1118 * Monitors are special as they are normally slaved to
1119 * whatever else is going on, so they behave as though
1120 * you tried setting the wiphy channel itself.
1121 */
1122 return !wdev ||
1123 wdev->iftype == NL80211_IFTYPE_AP ||
1124 wdev->iftype == NL80211_IFTYPE_WDS ||
1125 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001126 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1127 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001128}
1129
1130static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1131 struct wireless_dev *wdev,
1132 struct genl_info *info)
1133{
1134 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1135 u32 freq;
1136 int result;
1137
1138 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1139 return -EINVAL;
1140
1141 if (!nl80211_can_set_dev_channel(wdev))
1142 return -EOPNOTSUPP;
1143
1144 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1145 channel_type = nla_get_u32(info->attrs[
1146 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1147 if (channel_type != NL80211_CHAN_NO_HT &&
1148 channel_type != NL80211_CHAN_HT20 &&
1149 channel_type != NL80211_CHAN_HT40PLUS &&
1150 channel_type != NL80211_CHAN_HT40MINUS)
1151 return -EINVAL;
1152 }
1153
1154 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1155
1156 mutex_lock(&rdev->devlist_mtx);
1157 if (wdev) {
1158 wdev_lock(wdev);
1159 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1160 wdev_unlock(wdev);
1161 } else {
1162 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1163 }
1164 mutex_unlock(&rdev->devlist_mtx);
1165
1166 return result;
1167}
1168
1169static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1170{
Johannes Berg4c476992010-10-04 21:36:35 +02001171 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1172 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001173
Johannes Berg4c476992010-10-04 21:36:35 +02001174 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001175}
1176
Bill Jordane8347eb2010-10-01 13:54:28 -04001177static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1178{
Johannes Berg43b19952010-10-07 13:10:30 +02001179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1180 struct net_device *dev = info->user_ptr[1];
1181 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001182 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001183
1184 if (!info->attrs[NL80211_ATTR_MAC])
1185 return -EINVAL;
1186
Johannes Berg43b19952010-10-07 13:10:30 +02001187 if (netif_running(dev))
1188 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001189
Johannes Berg43b19952010-10-07 13:10:30 +02001190 if (!rdev->ops->set_wds_peer)
1191 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001192
Johannes Berg43b19952010-10-07 13:10:30 +02001193 if (wdev->iftype != NL80211_IFTYPE_WDS)
1194 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001195
1196 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001197 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001198}
1199
1200
Johannes Berg55682962007-09-20 13:09:35 -04001201static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1202{
1203 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001204 struct net_device *netdev = NULL;
1205 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001206 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001207 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001208 u32 changed;
1209 u8 retry_short = 0, retry_long = 0;
1210 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001211 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001212
Johannes Bergf444de02010-05-05 15:25:02 +02001213 /*
1214 * Try to find the wiphy and netdev. Normally this
1215 * function shouldn't need the netdev, but this is
1216 * done for backward compatibility -- previously
1217 * setting the channel was done per wiphy, but now
1218 * it is per netdev. Previous userland like hostapd
1219 * also passed a netdev to set_wiphy, so that it is
1220 * possible to let that go to the right netdev!
1221 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001222 mutex_lock(&cfg80211_mutex);
1223
Johannes Bergf444de02010-05-05 15:25:02 +02001224 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1225 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1226
1227 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1228 if (netdev && netdev->ieee80211_ptr) {
1229 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1230 mutex_lock(&rdev->mtx);
1231 } else
1232 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001233 }
1234
Johannes Bergf444de02010-05-05 15:25:02 +02001235 if (!netdev) {
1236 rdev = __cfg80211_rdev_from_info(info);
1237 if (IS_ERR(rdev)) {
1238 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001239 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001240 }
1241 wdev = NULL;
1242 netdev = NULL;
1243 result = 0;
1244
1245 mutex_lock(&rdev->mtx);
1246 } else if (netif_running(netdev) &&
1247 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1248 wdev = netdev->ieee80211_ptr;
1249 else
1250 wdev = NULL;
1251
1252 /*
1253 * end workaround code, by now the rdev is available
1254 * and locked, and wdev may or may not be NULL.
1255 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001256
1257 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001258 result = cfg80211_dev_rename(
1259 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001260
1261 mutex_unlock(&cfg80211_mutex);
1262
1263 if (result)
1264 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001265
Jouni Malinen31888482008-10-30 16:59:24 +02001266 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1267 struct ieee80211_txq_params txq_params;
1268 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1269
1270 if (!rdev->ops->set_txq_params) {
1271 result = -EOPNOTSUPP;
1272 goto bad_res;
1273 }
1274
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001275 if (!netdev) {
1276 result = -EINVAL;
1277 goto bad_res;
1278 }
1279
Johannes Berg133a3ff2011-11-03 14:50:13 +01001280 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1281 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1282 result = -EINVAL;
1283 goto bad_res;
1284 }
1285
Jouni Malinen31888482008-10-30 16:59:24 +02001286 nla_for_each_nested(nl_txq_params,
1287 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1288 rem_txq_params) {
1289 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1290 nla_data(nl_txq_params),
1291 nla_len(nl_txq_params),
1292 txq_params_policy);
1293 result = parse_txq_params(tb, &txq_params);
1294 if (result)
1295 goto bad_res;
1296
1297 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001298 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001299 &txq_params);
1300 if (result)
1301 goto bad_res;
1302 }
1303 }
1304
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001305 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001306 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001307 if (result)
1308 goto bad_res;
1309 }
1310
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001311 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1312 enum nl80211_tx_power_setting type;
1313 int idx, mbm = 0;
1314
1315 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001316 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001317 goto bad_res;
1318 }
1319
1320 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1321 type = nla_get_u32(info->attrs[idx]);
1322
1323 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1324 (type != NL80211_TX_POWER_AUTOMATIC)) {
1325 result = -EINVAL;
1326 goto bad_res;
1327 }
1328
1329 if (type != NL80211_TX_POWER_AUTOMATIC) {
1330 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1331 mbm = nla_get_u32(info->attrs[idx]);
1332 }
1333
1334 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1335 if (result)
1336 goto bad_res;
1337 }
1338
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001339 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1340 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1341 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001342 if ((!rdev->wiphy.available_antennas_tx &&
1343 !rdev->wiphy.available_antennas_rx) ||
1344 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001345 result = -EOPNOTSUPP;
1346 goto bad_res;
1347 }
1348
1349 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1350 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1351
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001352 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001353 * available antenna masks, except for the "all" mask */
1354 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1355 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001356 result = -EINVAL;
1357 goto bad_res;
1358 }
1359
Bruno Randolf7f531e02010-12-16 11:30:22 +09001360 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1361 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001362
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001363 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1364 if (result)
1365 goto bad_res;
1366 }
1367
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001368 changed = 0;
1369
1370 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1371 retry_short = nla_get_u8(
1372 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1373 if (retry_short == 0) {
1374 result = -EINVAL;
1375 goto bad_res;
1376 }
1377 changed |= WIPHY_PARAM_RETRY_SHORT;
1378 }
1379
1380 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1381 retry_long = nla_get_u8(
1382 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1383 if (retry_long == 0) {
1384 result = -EINVAL;
1385 goto bad_res;
1386 }
1387 changed |= WIPHY_PARAM_RETRY_LONG;
1388 }
1389
1390 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1391 frag_threshold = nla_get_u32(
1392 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1393 if (frag_threshold < 256) {
1394 result = -EINVAL;
1395 goto bad_res;
1396 }
1397 if (frag_threshold != (u32) -1) {
1398 /*
1399 * Fragments (apart from the last one) are required to
1400 * have even length. Make the fragmentation code
1401 * simpler by stripping LSB should someone try to use
1402 * odd threshold value.
1403 */
1404 frag_threshold &= ~0x1;
1405 }
1406 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1407 }
1408
1409 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1410 rts_threshold = nla_get_u32(
1411 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1412 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1413 }
1414
Lukáš Turek81077e82009-12-21 22:50:47 +01001415 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1416 coverage_class = nla_get_u8(
1417 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1418 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1419 }
1420
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001421 if (changed) {
1422 u8 old_retry_short, old_retry_long;
1423 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001424 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001425
1426 if (!rdev->ops->set_wiphy_params) {
1427 result = -EOPNOTSUPP;
1428 goto bad_res;
1429 }
1430
1431 old_retry_short = rdev->wiphy.retry_short;
1432 old_retry_long = rdev->wiphy.retry_long;
1433 old_frag_threshold = rdev->wiphy.frag_threshold;
1434 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001435 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001436
1437 if (changed & WIPHY_PARAM_RETRY_SHORT)
1438 rdev->wiphy.retry_short = retry_short;
1439 if (changed & WIPHY_PARAM_RETRY_LONG)
1440 rdev->wiphy.retry_long = retry_long;
1441 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1442 rdev->wiphy.frag_threshold = frag_threshold;
1443 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1444 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001445 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1446 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001447
1448 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1449 if (result) {
1450 rdev->wiphy.retry_short = old_retry_short;
1451 rdev->wiphy.retry_long = old_retry_long;
1452 rdev->wiphy.frag_threshold = old_frag_threshold;
1453 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001454 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001455 }
1456 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001457
Johannes Berg306d6112008-12-08 12:39:04 +01001458 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001459 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001460 if (netdev)
1461 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001462 return result;
1463}
1464
1465
1466static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001467 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001468 struct net_device *dev)
1469{
1470 void *hdr;
1471
1472 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1473 if (!hdr)
1474 return -1;
1475
1476 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001477 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001478 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001479 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001480
1481 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1482 rdev->devlist_generation ^
1483 (cfg80211_rdev_list_generation << 2));
1484
Johannes Berg55682962007-09-20 13:09:35 -04001485 return genlmsg_end(msg, hdr);
1486
1487 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001488 genlmsg_cancel(msg, hdr);
1489 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001490}
1491
1492static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1493{
1494 int wp_idx = 0;
1495 int if_idx = 0;
1496 int wp_start = cb->args[0];
1497 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001498 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001499 struct wireless_dev *wdev;
1500
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001501 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001502 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1503 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001504 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001505 if (wp_idx < wp_start) {
1506 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001507 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001508 }
Johannes Berg55682962007-09-20 13:09:35 -04001509 if_idx = 0;
1510
Johannes Bergf5ea9122009-08-07 16:17:38 +02001511 mutex_lock(&rdev->devlist_mtx);
1512 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001513 if (if_idx < if_start) {
1514 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001515 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001516 }
Johannes Berg55682962007-09-20 13:09:35 -04001517 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001519 rdev, wdev->netdev) < 0) {
1520 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001521 goto out;
1522 }
1523 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001524 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001525 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001526
1527 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001528 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001529 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001530 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001531
1532 cb->args[0] = wp_idx;
1533 cb->args[1] = if_idx;
1534
1535 return skb->len;
1536}
1537
1538static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1539{
1540 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001541 struct cfg80211_registered_device *dev = info->user_ptr[0];
1542 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001543
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001544 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001545 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001546 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001547
Johannes Bergd7264052009-04-19 16:23:20 +02001548 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001549 dev, netdev) < 0) {
1550 nlmsg_free(msg);
1551 return -ENOBUFS;
1552 }
Johannes Berg55682962007-09-20 13:09:35 -04001553
Johannes Berg134e6372009-07-10 09:51:34 +00001554 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001555}
1556
Michael Wu66f7ac52008-01-31 19:48:22 +01001557static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1558 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1559 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1560 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1561 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1562 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1563};
1564
1565static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1566{
1567 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1568 int flag;
1569
1570 *mntrflags = 0;
1571
1572 if (!nla)
1573 return -EINVAL;
1574
1575 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1576 nla, mntr_flags_policy))
1577 return -EINVAL;
1578
1579 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1580 if (flags[flag])
1581 *mntrflags |= (1<<flag);
1582
1583 return 0;
1584}
1585
Johannes Berg9bc383d2009-11-19 11:55:19 +01001586static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001587 struct net_device *netdev, u8 use_4addr,
1588 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001589{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001590 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001591 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001592 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001593 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001594 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001595
1596 switch (iftype) {
1597 case NL80211_IFTYPE_AP_VLAN:
1598 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1599 return 0;
1600 break;
1601 case NL80211_IFTYPE_STATION:
1602 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1603 return 0;
1604 break;
1605 default:
1606 break;
1607 }
1608
1609 return -EOPNOTSUPP;
1610}
1611
Johannes Berg55682962007-09-20 13:09:35 -04001612static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1613{
Johannes Berg4c476992010-10-04 21:36:35 +02001614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001615 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001616 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001617 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001618 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001619 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001620 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001621
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001622 memset(&params, 0, sizeof(params));
1623
Johannes Berg04a773a2009-04-19 21:24:32 +02001624 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001625
Johannes Berg723b0382008-09-16 20:22:09 +02001626 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001627 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001628 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001629 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001630 if (ntype > NL80211_IFTYPE_MAX)
1631 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001632 }
1633
Johannes Berg92ffe052008-09-16 20:39:36 +02001634 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001635 struct wireless_dev *wdev = dev->ieee80211_ptr;
1636
Johannes Berg4c476992010-10-04 21:36:35 +02001637 if (ntype != NL80211_IFTYPE_MESH_POINT)
1638 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001639 if (netif_running(dev))
1640 return -EBUSY;
1641
1642 wdev_lock(wdev);
1643 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1644 IEEE80211_MAX_MESH_ID_LEN);
1645 wdev->mesh_id_up_len =
1646 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1647 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1648 wdev->mesh_id_up_len);
1649 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001650 }
1651
Felix Fietkau8b787642009-11-10 18:53:10 +01001652 if (info->attrs[NL80211_ATTR_4ADDR]) {
1653 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1654 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001655 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001656 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001657 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001658 } else {
1659 params.use_4addr = -1;
1660 }
1661
Johannes Berg92ffe052008-09-16 20:39:36 +02001662 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001663 if (ntype != NL80211_IFTYPE_MONITOR)
1664 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001665 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1666 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001667 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001668 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001669
1670 flags = &_flags;
1671 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001672 }
Johannes Berg3b858752009-03-12 09:55:09 +01001673
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001674 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001675 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001676 else
1677 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001678
Johannes Berg9bc383d2009-11-19 11:55:19 +01001679 if (!err && params.use_4addr != -1)
1680 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1681
Johannes Berg55682962007-09-20 13:09:35 -04001682 return err;
1683}
1684
1685static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1686{
Johannes Berg4c476992010-10-04 21:36:35 +02001687 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001688 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001689 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001690 int err;
1691 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001692 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001693
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001694 memset(&params, 0, sizeof(params));
1695
Johannes Berg55682962007-09-20 13:09:35 -04001696 if (!info->attrs[NL80211_ATTR_IFNAME])
1697 return -EINVAL;
1698
1699 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1700 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1701 if (type > NL80211_IFTYPE_MAX)
1702 return -EINVAL;
1703 }
1704
Johannes Berg79c97e92009-07-07 03:56:12 +02001705 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001706 !(rdev->wiphy.interface_modes & (1 << type)))
1707 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001708
Johannes Berg9bc383d2009-11-19 11:55:19 +01001709 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001710 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001711 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001712 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001713 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001714 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001715
Michael Wu66f7ac52008-01-31 19:48:22 +01001716 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1717 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1718 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001719 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001720 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001721 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001722 if (IS_ERR(dev))
1723 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001724
Johannes Berg29cbe682010-12-03 09:20:44 +01001725 if (type == NL80211_IFTYPE_MESH_POINT &&
1726 info->attrs[NL80211_ATTR_MESH_ID]) {
1727 struct wireless_dev *wdev = dev->ieee80211_ptr;
1728
1729 wdev_lock(wdev);
1730 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1731 IEEE80211_MAX_MESH_ID_LEN);
1732 wdev->mesh_id_up_len =
1733 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1734 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1735 wdev->mesh_id_up_len);
1736 wdev_unlock(wdev);
1737 }
1738
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001739 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001740}
1741
1742static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1743{
Johannes Berg4c476992010-10-04 21:36:35 +02001744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1745 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001746
Johannes Berg4c476992010-10-04 21:36:35 +02001747 if (!rdev->ops->del_virtual_intf)
1748 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001749
Johannes Berg4c476992010-10-04 21:36:35 +02001750 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001751}
1752
Johannes Berg41ade002007-12-19 02:03:29 +01001753struct get_key_cookie {
1754 struct sk_buff *msg;
1755 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001756 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001757};
1758
1759static void get_key_callback(void *c, struct key_params *params)
1760{
Johannes Bergb9454e82009-07-08 13:29:08 +02001761 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001762 struct get_key_cookie *cookie = c;
1763
1764 if (params->key)
1765 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1766 params->key_len, params->key);
1767
1768 if (params->seq)
1769 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1770 params->seq_len, params->seq);
1771
1772 if (params->cipher)
1773 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1774 params->cipher);
1775
Johannes Bergb9454e82009-07-08 13:29:08 +02001776 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1777 if (!key)
1778 goto nla_put_failure;
1779
1780 if (params->key)
1781 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1782 params->key_len, params->key);
1783
1784 if (params->seq)
1785 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1786 params->seq_len, params->seq);
1787
1788 if (params->cipher)
1789 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1790 params->cipher);
1791
1792 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1793
1794 nla_nest_end(cookie->msg, key);
1795
Johannes Berg41ade002007-12-19 02:03:29 +01001796 return;
1797 nla_put_failure:
1798 cookie->error = 1;
1799}
1800
1801static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1802{
Johannes Berg4c476992010-10-04 21:36:35 +02001803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001804 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001805 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001806 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001807 const u8 *mac_addr = NULL;
1808 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001809 struct get_key_cookie cookie = {
1810 .error = 0,
1811 };
1812 void *hdr;
1813 struct sk_buff *msg;
1814
1815 if (info->attrs[NL80211_ATTR_KEY_IDX])
1816 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1817
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001818 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001819 return -EINVAL;
1820
1821 if (info->attrs[NL80211_ATTR_MAC])
1822 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1823
Johannes Berge31b8212010-10-05 19:39:30 +02001824 pairwise = !!mac_addr;
1825 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1826 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1827 if (kt >= NUM_NL80211_KEYTYPES)
1828 return -EINVAL;
1829 if (kt != NL80211_KEYTYPE_GROUP &&
1830 kt != NL80211_KEYTYPE_PAIRWISE)
1831 return -EINVAL;
1832 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1833 }
1834
Johannes Berg4c476992010-10-04 21:36:35 +02001835 if (!rdev->ops->get_key)
1836 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001837
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001838 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001839 if (!msg)
1840 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001841
1842 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1843 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001844 if (IS_ERR(hdr))
1845 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001846
1847 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001848 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001849
1850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1851 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1852 if (mac_addr)
1853 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1854
Johannes Berge31b8212010-10-05 19:39:30 +02001855 if (pairwise && mac_addr &&
1856 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1857 return -ENOENT;
1858
1859 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1860 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001861
1862 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001863 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001864
1865 if (cookie.error)
1866 goto nla_put_failure;
1867
1868 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001869 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001870
1871 nla_put_failure:
1872 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001873 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001874 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001875 return err;
1876}
1877
1878static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1879{
Johannes Berg4c476992010-10-04 21:36:35 +02001880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001881 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001882 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001883 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001884
Johannes Bergb9454e82009-07-08 13:29:08 +02001885 err = nl80211_parse_key(info, &key);
1886 if (err)
1887 return err;
1888
1889 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001890 return -EINVAL;
1891
Johannes Bergb9454e82009-07-08 13:29:08 +02001892 /* only support setting default key */
1893 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001894 return -EINVAL;
1895
Johannes Bergfffd0932009-07-08 14:22:54 +02001896 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001897
1898 if (key.def) {
1899 if (!rdev->ops->set_default_key) {
1900 err = -EOPNOTSUPP;
1901 goto out;
1902 }
1903
1904 err = nl80211_key_allowed(dev->ieee80211_ptr);
1905 if (err)
1906 goto out;
1907
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001908 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1909 key.def_uni, key.def_multi);
1910
1911 if (err)
1912 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001913
Johannes Berg3d23e342009-09-29 23:27:28 +02001914#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001915 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001916#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001917 } else {
1918 if (key.def_uni || !key.def_multi) {
1919 err = -EINVAL;
1920 goto out;
1921 }
1922
1923 if (!rdev->ops->set_default_mgmt_key) {
1924 err = -EOPNOTSUPP;
1925 goto out;
1926 }
1927
1928 err = nl80211_key_allowed(dev->ieee80211_ptr);
1929 if (err)
1930 goto out;
1931
1932 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1933 dev, key.idx);
1934 if (err)
1935 goto out;
1936
1937#ifdef CONFIG_CFG80211_WEXT
1938 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1939#endif
1940 }
1941
1942 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001943 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001944
Johannes Berg41ade002007-12-19 02:03:29 +01001945 return err;
1946}
1947
1948static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1949{
Johannes Berg4c476992010-10-04 21:36:35 +02001950 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001951 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001952 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001953 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001954 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001955
Johannes Bergb9454e82009-07-08 13:29:08 +02001956 err = nl80211_parse_key(info, &key);
1957 if (err)
1958 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001959
Johannes Bergb9454e82009-07-08 13:29:08 +02001960 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001961 return -EINVAL;
1962
Johannes Berg41ade002007-12-19 02:03:29 +01001963 if (info->attrs[NL80211_ATTR_MAC])
1964 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1965
Johannes Berge31b8212010-10-05 19:39:30 +02001966 if (key.type == -1) {
1967 if (mac_addr)
1968 key.type = NL80211_KEYTYPE_PAIRWISE;
1969 else
1970 key.type = NL80211_KEYTYPE_GROUP;
1971 }
1972
1973 /* for now */
1974 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1975 key.type != NL80211_KEYTYPE_GROUP)
1976 return -EINVAL;
1977
Johannes Berg4c476992010-10-04 21:36:35 +02001978 if (!rdev->ops->add_key)
1979 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001980
Johannes Berge31b8212010-10-05 19:39:30 +02001981 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1982 key.type == NL80211_KEYTYPE_PAIRWISE,
1983 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001984 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001985
1986 wdev_lock(dev->ieee80211_ptr);
1987 err = nl80211_key_allowed(dev->ieee80211_ptr);
1988 if (!err)
1989 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001990 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001991 mac_addr, &key.p);
1992 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001993
Johannes Berg41ade002007-12-19 02:03:29 +01001994 return err;
1995}
1996
1997static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1998{
Johannes Berg4c476992010-10-04 21:36:35 +02001999 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002000 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002001 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002002 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002003 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002004
Johannes Bergb9454e82009-07-08 13:29:08 +02002005 err = nl80211_parse_key(info, &key);
2006 if (err)
2007 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002008
2009 if (info->attrs[NL80211_ATTR_MAC])
2010 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2011
Johannes Berge31b8212010-10-05 19:39:30 +02002012 if (key.type == -1) {
2013 if (mac_addr)
2014 key.type = NL80211_KEYTYPE_PAIRWISE;
2015 else
2016 key.type = NL80211_KEYTYPE_GROUP;
2017 }
2018
2019 /* for now */
2020 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2021 key.type != NL80211_KEYTYPE_GROUP)
2022 return -EINVAL;
2023
Johannes Berg4c476992010-10-04 21:36:35 +02002024 if (!rdev->ops->del_key)
2025 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002026
Johannes Bergfffd0932009-07-08 14:22:54 +02002027 wdev_lock(dev->ieee80211_ptr);
2028 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002029
2030 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2031 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2032 err = -ENOENT;
2033
Johannes Bergfffd0932009-07-08 14:22:54 +02002034 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002035 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2036 key.type == NL80211_KEYTYPE_PAIRWISE,
2037 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002038
Johannes Berg3d23e342009-09-29 23:27:28 +02002039#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002040 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002041 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002042 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002043 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002044 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2045 }
2046#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002047 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002048
Johannes Berg41ade002007-12-19 02:03:29 +01002049 return err;
2050}
2051
Johannes Berged1b6cc2007-12-19 02:03:32 +01002052static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2053{
2054 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2055 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002056 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2057 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002058 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002059 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002060 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002061
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002062 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2063 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2064 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2065 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002066 return -EINVAL;
2067
Johannes Berg074ac8d2010-09-16 14:58:22 +02002068 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002069 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2070 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002071
Johannes Berg56d18932011-05-09 18:41:15 +02002072 memset(&params, 0, sizeof(params));
2073
Johannes Berged1b6cc2007-12-19 02:03:32 +01002074 switch (info->genlhdr->cmd) {
2075 case NL80211_CMD_NEW_BEACON:
2076 /* these are required for NEW_BEACON */
2077 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2078 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002079 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2080 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002081
Johannes Berg56d18932011-05-09 18:41:15 +02002082 params.interval =
2083 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2084 params.dtim_period =
2085 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2086
2087 err = cfg80211_validate_beacon_int(rdev, params.interval);
2088 if (err)
2089 return err;
2090
Jouni Malinen32e9de82011-08-10 23:53:31 +03002091 /*
2092 * In theory, some of these attributes could be required for
2093 * NEW_BEACON, but since they were not used when the command was
2094 * originally added, keep them optional for old user space
2095 * programs to work with drivers that do not need the additional
2096 * information.
2097 */
2098 if (info->attrs[NL80211_ATTR_SSID]) {
2099 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2100 params.ssid_len =
2101 nla_len(info->attrs[NL80211_ATTR_SSID]);
2102 if (params.ssid_len == 0 ||
2103 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2104 return -EINVAL;
2105 }
2106
2107 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2108 params.hidden_ssid = nla_get_u32(
2109 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2110 if (params.hidden_ssid !=
2111 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2112 params.hidden_ssid !=
2113 NL80211_HIDDEN_SSID_ZERO_LEN &&
2114 params.hidden_ssid !=
2115 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2116 return -EINVAL;
2117 }
2118
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002119 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2120
2121 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2122 params.auth_type = nla_get_u32(
2123 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2124 if (!nl80211_valid_auth_type(params.auth_type))
2125 return -EINVAL;
2126 } else
2127 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2128
2129 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2130 NL80211_MAX_NR_CIPHER_SUITES);
2131 if (err)
2132 return err;
2133
Johannes Berg79c97e92009-07-07 03:56:12 +02002134 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002135 break;
2136 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002137 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002138 break;
2139 default:
2140 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002141 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002142 }
2143
Johannes Berg4c476992010-10-04 21:36:35 +02002144 if (!call)
2145 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002146
Johannes Berged1b6cc2007-12-19 02:03:32 +01002147 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2148 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2149 params.head_len =
2150 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2151 haveinfo = 1;
2152 }
2153
2154 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2155 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2156 params.tail_len =
2157 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2158 haveinfo = 1;
2159 }
2160
Johannes Berg4c476992010-10-04 21:36:35 +02002161 if (!haveinfo)
2162 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002163
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002164 if (info->attrs[NL80211_ATTR_IE]) {
2165 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2166 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2167 }
2168
2169 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2170 params.proberesp_ies =
2171 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2172 params.proberesp_ies_len =
2173 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2174 }
2175
2176 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2177 params.assocresp_ies =
2178 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2179 params.assocresp_ies_len =
2180 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2181 }
2182
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002183 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2184 params.probe_resp =
2185 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2186 params.probe_resp_len =
2187 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2188 }
2189
Johannes Berg56d18932011-05-09 18:41:15 +02002190 err = call(&rdev->wiphy, dev, &params);
2191 if (!err && params.interval)
2192 wdev->beacon_interval = params.interval;
2193 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002194}
2195
2196static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2197{
Johannes Berg4c476992010-10-04 21:36:35 +02002198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2199 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002200 struct wireless_dev *wdev = dev->ieee80211_ptr;
2201 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002202
Johannes Berg4c476992010-10-04 21:36:35 +02002203 if (!rdev->ops->del_beacon)
2204 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002205
Johannes Berg074ac8d2010-09-16 14:58:22 +02002206 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002207 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2208 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002209
Johannes Berg56d18932011-05-09 18:41:15 +02002210 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2211 if (!err)
2212 wdev->beacon_interval = 0;
2213 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002214}
2215
Johannes Berg5727ef12007-12-19 02:03:34 +01002216static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2217 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2218 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2219 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002220 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002221 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002222};
2223
Johannes Bergeccb8e82009-05-11 21:57:56 +03002224static int parse_station_flags(struct genl_info *info,
2225 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002226{
2227 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002228 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002229 int flag;
2230
Johannes Bergeccb8e82009-05-11 21:57:56 +03002231 /*
2232 * Try parsing the new attribute first so userspace
2233 * can specify both for older kernels.
2234 */
2235 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2236 if (nla) {
2237 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002238
Johannes Bergeccb8e82009-05-11 21:57:56 +03002239 sta_flags = nla_data(nla);
2240 params->sta_flags_mask = sta_flags->mask;
2241 params->sta_flags_set = sta_flags->set;
2242 if ((params->sta_flags_mask |
2243 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2244 return -EINVAL;
2245 return 0;
2246 }
2247
2248 /* if present, parse the old attribute */
2249
2250 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002251 if (!nla)
2252 return 0;
2253
2254 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2255 nla, sta_flags_policy))
2256 return -EINVAL;
2257
Johannes Bergeccb8e82009-05-11 21:57:56 +03002258 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2259 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002260
2261 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2262 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002263 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002264
2265 return 0;
2266}
2267
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002268static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2269 int attr)
2270{
2271 struct nlattr *rate;
2272 u16 bitrate;
2273
2274 rate = nla_nest_start(msg, attr);
2275 if (!rate)
2276 goto nla_put_failure;
2277
2278 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2279 bitrate = cfg80211_calculate_bitrate(info);
2280 if (bitrate > 0)
2281 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2282
2283 if (info->flags & RATE_INFO_FLAGS_MCS)
2284 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2285 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2286 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2287 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2288 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2289
2290 nla_nest_end(msg, rate);
2291 return true;
2292
2293nla_put_failure:
2294 return false;
2295}
2296
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002297static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2298 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002299 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002300{
2301 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002302 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002303
2304 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2305 if (!hdr)
2306 return -1;
2307
2308 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2309 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2310
Johannes Bergf5ea9122009-08-07 16:17:38 +02002311 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2312
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002313 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2314 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002315 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302316 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2317 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2318 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002319 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2320 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2321 sinfo->inactive_time);
2322 if (sinfo->filled & STATION_INFO_RX_BYTES)
2323 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2324 sinfo->rx_bytes);
2325 if (sinfo->filled & STATION_INFO_TX_BYTES)
2326 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2327 sinfo->tx_bytes);
2328 if (sinfo->filled & STATION_INFO_LLID)
2329 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2330 sinfo->llid);
2331 if (sinfo->filled & STATION_INFO_PLID)
2332 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2333 sinfo->plid);
2334 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2335 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2336 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002337 if (sinfo->filled & STATION_INFO_SIGNAL)
2338 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2339 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002340 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2341 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2342 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002343 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002344 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2345 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002346 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002347 }
2348 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2349 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2350 NL80211_STA_INFO_RX_BITRATE))
2351 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002352 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002353 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2354 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2355 sinfo->rx_packets);
2356 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2357 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2358 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002359 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2360 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2361 sinfo->tx_retries);
2362 if (sinfo->filled & STATION_INFO_TX_FAILED)
2363 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2364 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002365 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2366 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2367 if (!bss_param)
2368 goto nla_put_failure;
2369
2370 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2371 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2372 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2373 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2374 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2375 NLA_PUT_FLAG(msg,
2376 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2377 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2378 sinfo->bss_param.dtim_period);
2379 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2380 sinfo->bss_param.beacon_interval);
2381
2382 nla_nest_end(msg, bss_param);
2383 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002384 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2385 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2386 sizeof(struct nl80211_sta_flag_update),
2387 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002388 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002389
Felix Fietkau040bdf72011-08-10 19:00:33 -06002390 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002391 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2392 sinfo->assoc_req_ies);
2393
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002394 return genlmsg_end(msg, hdr);
2395
2396 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002397 genlmsg_cancel(msg, hdr);
2398 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002399}
2400
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002401static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002402 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002403{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002404 struct station_info sinfo;
2405 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002406 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002407 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002408 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002409 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410
Johannes Berg67748892010-10-04 21:14:06 +02002411 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2412 if (err)
2413 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002414
2415 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002416 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002417 goto out_err;
2418 }
2419
Johannes Bergbba95fe2008-07-29 13:22:51 +02002420 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002421 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002422 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2423 mac_addr, &sinfo);
2424 if (err == -ENOENT)
2425 break;
2426 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002427 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002428
2429 if (nl80211_send_station(skb,
2430 NETLINK_CB(cb->skb).pid,
2431 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2432 netdev, mac_addr,
2433 &sinfo) < 0)
2434 goto out;
2435
2436 sta_idx++;
2437 }
2438
2439
2440 out:
2441 cb->args[1] = sta_idx;
2442 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002443 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002444 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002445
2446 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002447}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002448
Johannes Berg5727ef12007-12-19 02:03:34 +01002449static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2450{
Johannes Berg4c476992010-10-04 21:36:35 +02002451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2452 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002453 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002454 struct sk_buff *msg;
2455 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002456 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002457
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002458 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002459
2460 if (!info->attrs[NL80211_ATTR_MAC])
2461 return -EINVAL;
2462
2463 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2464
Johannes Berg4c476992010-10-04 21:36:35 +02002465 if (!rdev->ops->get_station)
2466 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002467
Johannes Berg79c97e92009-07-07 03:56:12 +02002468 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002469 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002470 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002471
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002472 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002473 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002474 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002475
2476 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002477 dev, mac_addr, &sinfo) < 0) {
2478 nlmsg_free(msg);
2479 return -ENOBUFS;
2480 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002481
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002483}
2484
2485/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002486 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002487 */
Johannes Berg463d0182009-07-14 00:33:35 +02002488static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002489 struct cfg80211_registered_device *rdev,
2490 struct net_device **vlan)
2491{
Johannes Berg463d0182009-07-14 00:33:35 +02002492 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002493 *vlan = NULL;
2494
2495 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002496 *vlan = dev_get_by_index(genl_info_net(info),
2497 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002498 if (!*vlan)
2499 return -ENODEV;
2500 if (!(*vlan)->ieee80211_ptr)
2501 return -EINVAL;
2502 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2503 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002504 if (!netif_running(*vlan))
2505 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002506 }
2507 return 0;
2508}
2509
2510static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2511{
Johannes Berg4c476992010-10-04 21:36:35 +02002512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002513 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002514 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002515 struct station_parameters params;
2516 u8 *mac_addr = NULL;
2517
2518 memset(&params, 0, sizeof(params));
2519
2520 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002521 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002522
2523 if (info->attrs[NL80211_ATTR_STA_AID])
2524 return -EINVAL;
2525
2526 if (!info->attrs[NL80211_ATTR_MAC])
2527 return -EINVAL;
2528
2529 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2530
2531 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2532 params.supported_rates =
2533 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2534 params.supported_rates_len =
2535 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2536 }
2537
2538 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2539 params.listen_interval =
2540 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2541
Jouni Malinen36aedc92008-08-25 11:58:58 +03002542 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2543 params.ht_capa =
2544 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2545
Johannes Bergeccb8e82009-05-11 21:57:56 +03002546 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002547 return -EINVAL;
2548
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002549 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2550 params.plink_action =
2551 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2552
Javier Cardona9c3990a2011-05-03 16:57:11 -07002553 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2554 params.plink_state =
2555 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2556
Johannes Berg463d0182009-07-14 00:33:35 +02002557 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002558 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002559 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002560
2561 /* validate settings */
2562 err = 0;
2563
2564 switch (dev->ieee80211_ptr->iftype) {
2565 case NL80211_IFTYPE_AP:
2566 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002567 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002568 /* disallow mesh-specific things */
2569 if (params.plink_action)
2570 err = -EINVAL;
2571 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002572 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002573 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002574 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002575 if (params.plink_action)
2576 err = -EINVAL;
2577 if (params.vlan)
2578 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002579 if (params.supported_rates &&
2580 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002581 err = -EINVAL;
2582 if (params.ht_capa)
2583 err = -EINVAL;
2584 if (params.listen_interval >= 0)
2585 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002586 if (params.sta_flags_mask &
2587 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2588 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2589 err = -EINVAL;
2590 /* can't change the TDLS bit */
2591 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2592 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002593 err = -EINVAL;
2594 break;
2595 case NL80211_IFTYPE_MESH_POINT:
2596 /* disallow things mesh doesn't support */
2597 if (params.vlan)
2598 err = -EINVAL;
2599 if (params.ht_capa)
2600 err = -EINVAL;
2601 if (params.listen_interval >= 0)
2602 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002603 if (params.sta_flags_mask &
2604 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002605 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002606 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002607 err = -EINVAL;
2608 break;
2609 default:
2610 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002611 }
2612
Johannes Berg5727ef12007-12-19 02:03:34 +01002613 if (err)
2614 goto out;
2615
Johannes Berg79c97e92009-07-07 03:56:12 +02002616 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002617 err = -EOPNOTSUPP;
2618 goto out;
2619 }
2620
Johannes Berg79c97e92009-07-07 03:56:12 +02002621 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002622
2623 out:
2624 if (params.vlan)
2625 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002626
Johannes Berg5727ef12007-12-19 02:03:34 +01002627 return err;
2628}
2629
Eliad Pellerc75786c2011-08-23 14:37:46 +03002630static struct nla_policy
2631nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2632 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2633 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2634};
2635
Johannes Berg5727ef12007-12-19 02:03:34 +01002636static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2637{
Johannes Berg4c476992010-10-04 21:36:35 +02002638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002639 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002640 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002641 struct station_parameters params;
2642 u8 *mac_addr = NULL;
2643
2644 memset(&params, 0, sizeof(params));
2645
2646 if (!info->attrs[NL80211_ATTR_MAC])
2647 return -EINVAL;
2648
Johannes Berg5727ef12007-12-19 02:03:34 +01002649 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2650 return -EINVAL;
2651
2652 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2653 return -EINVAL;
2654
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002655 if (!info->attrs[NL80211_ATTR_STA_AID])
2656 return -EINVAL;
2657
Johannes Berg5727ef12007-12-19 02:03:34 +01002658 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2659 params.supported_rates =
2660 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2661 params.supported_rates_len =
2662 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2663 params.listen_interval =
2664 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002665
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002666 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2667 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2668 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002669
Jouni Malinen36aedc92008-08-25 11:58:58 +03002670 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2671 params.ht_capa =
2672 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002673
Javier Cardona96b78df2011-04-07 15:08:33 -07002674 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2675 params.plink_action =
2676 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2677
Johannes Bergeccb8e82009-05-11 21:57:56 +03002678 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002679 return -EINVAL;
2680
Eliad Pellerc75786c2011-08-23 14:37:46 +03002681 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002682 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002683 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002684 info->attrs[NL80211_ATTR_STA_WME]) {
2685 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2686 struct nlattr *nla;
2687
2688 nla = info->attrs[NL80211_ATTR_STA_WME];
2689 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2690 nl80211_sta_wme_policy);
2691 if (err)
2692 return err;
2693
2694 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2695 params.uapsd_queues =
2696 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002697 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2698 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002699
2700 if (tb[NL80211_STA_WME_MAX_SP])
2701 params.max_sp =
2702 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002703
2704 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2705 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002706
2707 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002708 }
2709
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002710 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002711 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002712 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002713 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2714 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2715 return -EINVAL;
2716
2717 /*
2718 * Only managed stations can add TDLS peers, and only when the
2719 * wiphy supports external TDLS setup.
2720 */
2721 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2722 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2723 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2724 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002725 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002726
Johannes Berg463d0182009-07-14 00:33:35 +02002727 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002728 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002729 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002730
2731 /* validate settings */
2732 err = 0;
2733
Johannes Berg79c97e92009-07-07 03:56:12 +02002734 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002735 err = -EOPNOTSUPP;
2736 goto out;
2737 }
2738
Johannes Berg79c97e92009-07-07 03:56:12 +02002739 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002740
2741 out:
2742 if (params.vlan)
2743 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002744 return err;
2745}
2746
2747static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2748{
Johannes Berg4c476992010-10-04 21:36:35 +02002749 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2750 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002751 u8 *mac_addr = NULL;
2752
2753 if (info->attrs[NL80211_ATTR_MAC])
2754 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2755
Johannes Berge80cf852009-05-11 14:43:13 +02002756 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002757 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002758 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002759 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2760 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002761
Johannes Berg4c476992010-10-04 21:36:35 +02002762 if (!rdev->ops->del_station)
2763 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002764
Johannes Berg4c476992010-10-04 21:36:35 +02002765 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002766}
2767
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002768static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2769 int flags, struct net_device *dev,
2770 u8 *dst, u8 *next_hop,
2771 struct mpath_info *pinfo)
2772{
2773 void *hdr;
2774 struct nlattr *pinfoattr;
2775
2776 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2777 if (!hdr)
2778 return -1;
2779
2780 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2781 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2782 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2783
Johannes Bergf5ea9122009-08-07 16:17:38 +02002784 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2785
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002786 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2787 if (!pinfoattr)
2788 goto nla_put_failure;
2789 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2790 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2791 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002792 if (pinfo->filled & MPATH_INFO_SN)
2793 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2794 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002795 if (pinfo->filled & MPATH_INFO_METRIC)
2796 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2797 pinfo->metric);
2798 if (pinfo->filled & MPATH_INFO_EXPTIME)
2799 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2800 pinfo->exptime);
2801 if (pinfo->filled & MPATH_INFO_FLAGS)
2802 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2803 pinfo->flags);
2804 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2805 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2806 pinfo->discovery_timeout);
2807 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2808 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2809 pinfo->discovery_retries);
2810
2811 nla_nest_end(msg, pinfoattr);
2812
2813 return genlmsg_end(msg, hdr);
2814
2815 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002816 genlmsg_cancel(msg, hdr);
2817 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002818}
2819
2820static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002821 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002822{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002823 struct mpath_info pinfo;
2824 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002825 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002826 u8 dst[ETH_ALEN];
2827 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002828 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002829 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002830
Johannes Berg67748892010-10-04 21:14:06 +02002831 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2832 if (err)
2833 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002834
2835 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002836 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002837 goto out_err;
2838 }
2839
Jouni Malineneec60b02009-03-20 21:21:19 +02002840 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2841 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002842 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002843 }
2844
Johannes Bergbba95fe2008-07-29 13:22:51 +02002845 while (1) {
2846 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2847 dst, next_hop, &pinfo);
2848 if (err == -ENOENT)
2849 break;
2850 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002851 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002852
2853 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2854 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2855 netdev, dst, next_hop,
2856 &pinfo) < 0)
2857 goto out;
2858
2859 path_idx++;
2860 }
2861
2862
2863 out:
2864 cb->args[1] = path_idx;
2865 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002866 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002867 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002868 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002869}
2870
2871static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2872{
Johannes Berg4c476992010-10-04 21:36:35 +02002873 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002874 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002875 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002876 struct mpath_info pinfo;
2877 struct sk_buff *msg;
2878 u8 *dst = NULL;
2879 u8 next_hop[ETH_ALEN];
2880
2881 memset(&pinfo, 0, sizeof(pinfo));
2882
2883 if (!info->attrs[NL80211_ATTR_MAC])
2884 return -EINVAL;
2885
2886 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2887
Johannes Berg4c476992010-10-04 21:36:35 +02002888 if (!rdev->ops->get_mpath)
2889 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002890
Johannes Berg4c476992010-10-04 21:36:35 +02002891 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2892 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002893
Johannes Berg79c97e92009-07-07 03:56:12 +02002894 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002895 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002896 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002897
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002898 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002899 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002900 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002901
2902 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002903 dev, dst, next_hop, &pinfo) < 0) {
2904 nlmsg_free(msg);
2905 return -ENOBUFS;
2906 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002907
Johannes Berg4c476992010-10-04 21:36:35 +02002908 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002909}
2910
2911static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2912{
Johannes Berg4c476992010-10-04 21:36:35 +02002913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2914 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002915 u8 *dst = NULL;
2916 u8 *next_hop = NULL;
2917
2918 if (!info->attrs[NL80211_ATTR_MAC])
2919 return -EINVAL;
2920
2921 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2922 return -EINVAL;
2923
2924 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2925 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2926
Johannes Berg4c476992010-10-04 21:36:35 +02002927 if (!rdev->ops->change_mpath)
2928 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002929
Johannes Berg4c476992010-10-04 21:36:35 +02002930 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2931 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002932
Johannes Berg4c476992010-10-04 21:36:35 +02002933 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002934}
Johannes Berg4c476992010-10-04 21:36:35 +02002935
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002936static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2937{
Johannes Berg4c476992010-10-04 21:36:35 +02002938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2939 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002940 u8 *dst = NULL;
2941 u8 *next_hop = NULL;
2942
2943 if (!info->attrs[NL80211_ATTR_MAC])
2944 return -EINVAL;
2945
2946 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2947 return -EINVAL;
2948
2949 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2950 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2951
Johannes Berg4c476992010-10-04 21:36:35 +02002952 if (!rdev->ops->add_mpath)
2953 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002954
Johannes Berg4c476992010-10-04 21:36:35 +02002955 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2956 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002957
Johannes Berg4c476992010-10-04 21:36:35 +02002958 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002959}
2960
2961static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2962{
Johannes Berg4c476992010-10-04 21:36:35 +02002963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2964 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002965 u8 *dst = NULL;
2966
2967 if (info->attrs[NL80211_ATTR_MAC])
2968 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2969
Johannes Berg4c476992010-10-04 21:36:35 +02002970 if (!rdev->ops->del_mpath)
2971 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002972
Johannes Berg4c476992010-10-04 21:36:35 +02002973 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002974}
2975
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002976static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2977{
Johannes Berg4c476992010-10-04 21:36:35 +02002978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2979 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002980 struct bss_parameters params;
2981
2982 memset(&params, 0, sizeof(params));
2983 /* default to not changing parameters */
2984 params.use_cts_prot = -1;
2985 params.use_short_preamble = -1;
2986 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002987 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002988 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002989
2990 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2991 params.use_cts_prot =
2992 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2993 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2994 params.use_short_preamble =
2995 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2996 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2997 params.use_short_slot_time =
2998 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002999 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3000 params.basic_rates =
3001 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3002 params.basic_rates_len =
3003 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3004 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003005 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3006 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003007 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3008 params.ht_opmode =
3009 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003010
Johannes Berg4c476992010-10-04 21:36:35 +02003011 if (!rdev->ops->change_bss)
3012 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003013
Johannes Berg074ac8d2010-09-16 14:58:22 +02003014 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003015 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3016 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003017
Johannes Berg4c476992010-10-04 21:36:35 +02003018 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003019}
3020
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003021static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003022 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3023 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3024 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3025 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3026 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3027 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3028};
3029
3030static int parse_reg_rule(struct nlattr *tb[],
3031 struct ieee80211_reg_rule *reg_rule)
3032{
3033 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3034 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3035
3036 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3037 return -EINVAL;
3038 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3039 return -EINVAL;
3040 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3041 return -EINVAL;
3042 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3043 return -EINVAL;
3044 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3045 return -EINVAL;
3046
3047 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3048
3049 freq_range->start_freq_khz =
3050 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3051 freq_range->end_freq_khz =
3052 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3053 freq_range->max_bandwidth_khz =
3054 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3055
3056 power_rule->max_eirp =
3057 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3058
3059 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3060 power_rule->max_antenna_gain =
3061 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3062
3063 return 0;
3064}
3065
3066static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3067{
3068 int r;
3069 char *data = NULL;
3070
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003071 /*
3072 * You should only get this when cfg80211 hasn't yet initialized
3073 * completely when built-in to the kernel right between the time
3074 * window between nl80211_init() and regulatory_init(), if that is
3075 * even possible.
3076 */
3077 mutex_lock(&cfg80211_mutex);
3078 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003079 mutex_unlock(&cfg80211_mutex);
3080 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003081 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003082 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003083
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003084 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3085 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003086
3087 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3088
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003089 r = regulatory_hint_user(data);
3090
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003091 return r;
3092}
3093
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003094static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003095 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003096{
Johannes Berg4c476992010-10-04 21:36:35 +02003097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003098 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003099 struct wireless_dev *wdev = dev->ieee80211_ptr;
3100 struct mesh_config cur_params;
3101 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003102 void *hdr;
3103 struct nlattr *pinfoattr;
3104 struct sk_buff *msg;
3105
Johannes Berg29cbe682010-12-03 09:20:44 +01003106 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3107 return -EOPNOTSUPP;
3108
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003109 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003110 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003111
Johannes Berg29cbe682010-12-03 09:20:44 +01003112 wdev_lock(wdev);
3113 /* If not connected, get default parameters */
3114 if (!wdev->mesh_id_len)
3115 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3116 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003117 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003118 &cur_params);
3119 wdev_unlock(wdev);
3120
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003121 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003122 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003123
3124 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003125 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003126 if (!msg)
3127 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003128 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003129 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003130 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003131 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003132 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003133 if (!pinfoattr)
3134 goto nla_put_failure;
3135 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3136 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3137 cur_params.dot11MeshRetryTimeout);
3138 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3139 cur_params.dot11MeshConfirmTimeout);
3140 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3141 cur_params.dot11MeshHoldingTimeout);
3142 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3143 cur_params.dot11MeshMaxPeerLinks);
3144 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3145 cur_params.dot11MeshMaxRetries);
3146 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3147 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003148 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3149 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003150 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3151 cur_params.auto_open_plinks);
3152 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3153 cur_params.dot11MeshHWMPmaxPREQretries);
3154 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3155 cur_params.path_refresh_time);
3156 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3157 cur_params.min_discovery_timeout);
3158 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3159 cur_params.dot11MeshHWMPactivePathTimeout);
3160 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3161 cur_params.dot11MeshHWMPpreqMinInterval);
3162 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3163 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003164 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3165 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003166 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3167 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003168 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3169 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003170 nla_nest_end(msg, pinfoattr);
3171 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003172 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003173
Johannes Berg3b858752009-03-12 09:55:09 +01003174 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003175 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003176 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003177 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003178 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003179}
3180
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003181static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003182 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3183 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3184 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3185 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3186 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3187 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003188 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003189 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3190
3191 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3192 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3193 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3194 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3195 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3196 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003197 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003198 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003199 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003200};
3201
Javier Cardonac80d5452010-12-16 17:37:49 -08003202static const struct nla_policy
3203 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3204 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3205 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003206 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003207 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003208 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003209 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003210};
3211
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003212static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003213 struct mesh_config *cfg,
3214 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003215{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003216 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003217 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003218
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003219#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3220do {\
3221 if (table[attr_num]) {\
3222 cfg->param = nla_fn(table[attr_num]); \
3223 mask |= (1 << (attr_num - 1)); \
3224 } \
3225} while (0);\
3226
3227
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003228 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003229 return -EINVAL;
3230 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003231 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003232 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003233 return -EINVAL;
3234
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003235 /* This makes sure that there aren't more than 32 mesh config
3236 * parameters (otherwise our bitfield scheme would not work.) */
3237 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3238
3239 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003240 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3241 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3242 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3243 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3244 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3245 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3246 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3247 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3248 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3249 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3250 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3251 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003252 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3253 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003254 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3255 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3256 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3257 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3258 nla_get_u8);
3259 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3260 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3261 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3262 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3263 nla_get_u16);
3264 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3265 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3266 nla_get_u32);
3267 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3268 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3269 nla_get_u16);
3270 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3271 dot11MeshHWMPnetDiameterTraversalTime,
3272 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3273 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003274 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3275 dot11MeshHWMPRootMode, mask,
3276 NL80211_MESHCONF_HWMP_ROOTMODE,
3277 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003278 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3279 dot11MeshHWMPRannInterval, mask,
3280 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3281 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003282 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3283 dot11MeshGateAnnouncementProtocol, mask,
3284 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3285 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003286 if (mask_out)
3287 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003288
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003289 return 0;
3290
3291#undef FILL_IN_MESH_PARAM_IF_SET
3292}
3293
Javier Cardonac80d5452010-12-16 17:37:49 -08003294static int nl80211_parse_mesh_setup(struct genl_info *info,
3295 struct mesh_setup *setup)
3296{
3297 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3298
3299 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3300 return -EINVAL;
3301 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3302 info->attrs[NL80211_ATTR_MESH_SETUP],
3303 nl80211_mesh_setup_params_policy))
3304 return -EINVAL;
3305
3306 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3307 setup->path_sel_proto =
3308 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3309 IEEE80211_PATH_PROTOCOL_VENDOR :
3310 IEEE80211_PATH_PROTOCOL_HWMP;
3311
3312 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3313 setup->path_metric =
3314 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3315 IEEE80211_PATH_METRIC_VENDOR :
3316 IEEE80211_PATH_METRIC_AIRTIME;
3317
Javier Cardona581a8b02011-04-07 15:08:27 -07003318
3319 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003320 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003321 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003322 if (!is_valid_ie_attr(ieattr))
3323 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003324 setup->ie = nla_data(ieattr);
3325 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003326 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003327 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3328 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003329
3330 return 0;
3331}
3332
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003333static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003334 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003335{
3336 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3337 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003338 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003339 struct mesh_config cfg;
3340 u32 mask;
3341 int err;
3342
Johannes Berg29cbe682010-12-03 09:20:44 +01003343 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3344 return -EOPNOTSUPP;
3345
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003346 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003347 return -EOPNOTSUPP;
3348
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003349 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003350 if (err)
3351 return err;
3352
Johannes Berg29cbe682010-12-03 09:20:44 +01003353 wdev_lock(wdev);
3354 if (!wdev->mesh_id_len)
3355 err = -ENOLINK;
3356
3357 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003358 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003359 mask, &cfg);
3360
3361 wdev_unlock(wdev);
3362
3363 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003364}
3365
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003366static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3367{
3368 struct sk_buff *msg;
3369 void *hdr = NULL;
3370 struct nlattr *nl_reg_rules;
3371 unsigned int i;
3372 int err = -EINVAL;
3373
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003374 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003375
3376 if (!cfg80211_regdomain)
3377 goto out;
3378
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003380 if (!msg) {
3381 err = -ENOBUFS;
3382 goto out;
3383 }
3384
3385 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3386 NL80211_CMD_GET_REG);
3387 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003388 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003389
3390 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3391 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003392 if (cfg80211_regdomain->dfs_region)
3393 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3394 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003395
3396 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3397 if (!nl_reg_rules)
3398 goto nla_put_failure;
3399
3400 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3401 struct nlattr *nl_reg_rule;
3402 const struct ieee80211_reg_rule *reg_rule;
3403 const struct ieee80211_freq_range *freq_range;
3404 const struct ieee80211_power_rule *power_rule;
3405
3406 reg_rule = &cfg80211_regdomain->reg_rules[i];
3407 freq_range = &reg_rule->freq_range;
3408 power_rule = &reg_rule->power_rule;
3409
3410 nl_reg_rule = nla_nest_start(msg, i);
3411 if (!nl_reg_rule)
3412 goto nla_put_failure;
3413
3414 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3415 reg_rule->flags);
3416 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3417 freq_range->start_freq_khz);
3418 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3419 freq_range->end_freq_khz);
3420 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3421 freq_range->max_bandwidth_khz);
3422 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3423 power_rule->max_antenna_gain);
3424 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3425 power_rule->max_eirp);
3426
3427 nla_nest_end(msg, nl_reg_rule);
3428 }
3429
3430 nla_nest_end(msg, nl_reg_rules);
3431
3432 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003433 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003434 goto out;
3435
3436nla_put_failure:
3437 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003438put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003439 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003440 err = -EMSGSIZE;
3441out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003442 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003443 return err;
3444}
3445
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003446static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3447{
3448 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3449 struct nlattr *nl_reg_rule;
3450 char *alpha2 = NULL;
3451 int rem_reg_rules = 0, r = 0;
3452 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003453 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003454 struct ieee80211_regdomain *rd = NULL;
3455
3456 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3457 return -EINVAL;
3458
3459 if (!info->attrs[NL80211_ATTR_REG_RULES])
3460 return -EINVAL;
3461
3462 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3463
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003464 if (info->attrs[NL80211_ATTR_DFS_REGION])
3465 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3466
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003467 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3468 rem_reg_rules) {
3469 num_rules++;
3470 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003471 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003472 }
3473
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003474 mutex_lock(&cfg80211_mutex);
3475
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003476 if (!reg_is_valid_request(alpha2)) {
3477 r = -EINVAL;
3478 goto bad_reg;
3479 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003480
3481 size_of_regd = sizeof(struct ieee80211_regdomain) +
3482 (num_rules * sizeof(struct ieee80211_reg_rule));
3483
3484 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003485 if (!rd) {
3486 r = -ENOMEM;
3487 goto bad_reg;
3488 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003489
3490 rd->n_reg_rules = num_rules;
3491 rd->alpha2[0] = alpha2[0];
3492 rd->alpha2[1] = alpha2[1];
3493
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003494 /*
3495 * Disable DFS master mode if the DFS region was
3496 * not supported or known on this kernel.
3497 */
3498 if (reg_supported_dfs_region(dfs_region))
3499 rd->dfs_region = dfs_region;
3500
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003501 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3502 rem_reg_rules) {
3503 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3504 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3505 reg_rule_policy);
3506 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3507 if (r)
3508 goto bad_reg;
3509
3510 rule_idx++;
3511
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003512 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3513 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003514 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003515 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003516 }
3517
3518 BUG_ON(rule_idx != num_rules);
3519
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003520 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003521
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003522 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003523
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003524 return r;
3525
Johannes Bergd2372b32008-10-24 20:32:20 +02003526 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003527 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003528 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003529 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003530}
3531
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003532static int validate_scan_freqs(struct nlattr *freqs)
3533{
3534 struct nlattr *attr1, *attr2;
3535 int n_channels = 0, tmp1, tmp2;
3536
3537 nla_for_each_nested(attr1, freqs, tmp1) {
3538 n_channels++;
3539 /*
3540 * Some hardware has a limited channel list for
3541 * scanning, and it is pretty much nonsensical
3542 * to scan for a channel twice, so disallow that
3543 * and don't require drivers to check that the
3544 * channel list they get isn't longer than what
3545 * they can scan, as long as they can scan all
3546 * the channels they registered at once.
3547 */
3548 nla_for_each_nested(attr2, freqs, tmp2)
3549 if (attr1 != attr2 &&
3550 nla_get_u32(attr1) == nla_get_u32(attr2))
3551 return 0;
3552 }
3553
3554 return n_channels;
3555}
3556
Johannes Berg2a519312009-02-10 21:25:55 +01003557static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3558{
Johannes Berg4c476992010-10-04 21:36:35 +02003559 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3560 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003561 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003562 struct nlattr *attr;
3563 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003564 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003565 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003566
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003567 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3568 return -EINVAL;
3569
Johannes Berg79c97e92009-07-07 03:56:12 +02003570 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003571
Johannes Berg4c476992010-10-04 21:36:35 +02003572 if (!rdev->ops->scan)
3573 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003574
Johannes Berg4c476992010-10-04 21:36:35 +02003575 if (rdev->scan_req)
3576 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003577
3578 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003579 n_channels = validate_scan_freqs(
3580 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003581 if (!n_channels)
3582 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003583 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003584 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003585 n_channels = 0;
3586
Johannes Berg2a519312009-02-10 21:25:55 +01003587 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3588 if (wiphy->bands[band])
3589 n_channels += wiphy->bands[band]->n_channels;
3590 }
3591
3592 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3593 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3594 n_ssids++;
3595
Johannes Berg4c476992010-10-04 21:36:35 +02003596 if (n_ssids > wiphy->max_scan_ssids)
3597 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003598
Jouni Malinen70692ad2009-02-16 19:39:13 +02003599 if (info->attrs[NL80211_ATTR_IE])
3600 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3601 else
3602 ie_len = 0;
3603
Johannes Berg4c476992010-10-04 21:36:35 +02003604 if (ie_len > wiphy->max_scan_ie_len)
3605 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003606
Johannes Berg2a519312009-02-10 21:25:55 +01003607 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003608 + sizeof(*request->ssids) * n_ssids
3609 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003610 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003611 if (!request)
3612 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003613
Johannes Berg2a519312009-02-10 21:25:55 +01003614 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003615 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003616 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003617 if (ie_len) {
3618 if (request->ssids)
3619 request->ie = (void *)(request->ssids + n_ssids);
3620 else
3621 request->ie = (void *)(request->channels + n_channels);
3622 }
Johannes Berg2a519312009-02-10 21:25:55 +01003623
Johannes Berg584991d2009-11-02 13:32:03 +01003624 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003625 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3626 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003627 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003628 struct ieee80211_channel *chan;
3629
3630 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3631
3632 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003633 err = -EINVAL;
3634 goto out_free;
3635 }
Johannes Berg584991d2009-11-02 13:32:03 +01003636
3637 /* ignore disabled channels */
3638 if (chan->flags & IEEE80211_CHAN_DISABLED)
3639 continue;
3640
3641 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003642 i++;
3643 }
3644 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003645 enum ieee80211_band band;
3646
Johannes Berg2a519312009-02-10 21:25:55 +01003647 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003648 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3649 int j;
3650 if (!wiphy->bands[band])
3651 continue;
3652 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003653 struct ieee80211_channel *chan;
3654
3655 chan = &wiphy->bands[band]->channels[j];
3656
3657 if (chan->flags & IEEE80211_CHAN_DISABLED)
3658 continue;
3659
3660 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003661 i++;
3662 }
3663 }
3664 }
3665
Johannes Berg584991d2009-11-02 13:32:03 +01003666 if (!i) {
3667 err = -EINVAL;
3668 goto out_free;
3669 }
3670
3671 request->n_channels = i;
3672
Johannes Berg2a519312009-02-10 21:25:55 +01003673 i = 0;
3674 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3675 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003676 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003677 err = -EINVAL;
3678 goto out_free;
3679 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003680 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003681 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003682 i++;
3683 }
3684 }
3685
Jouni Malinen70692ad2009-02-16 19:39:13 +02003686 if (info->attrs[NL80211_ATTR_IE]) {
3687 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003688 memcpy((void *)request->ie,
3689 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003690 request->ie_len);
3691 }
3692
Johannes Berg34850ab2011-07-18 18:08:35 +02003693 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003694 if (wiphy->bands[i])
3695 request->rates[i] =
3696 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003697
3698 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3699 nla_for_each_nested(attr,
3700 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3701 tmp) {
3702 enum ieee80211_band band = nla_type(attr);
3703
Dan Carpenter84404622011-07-29 11:52:18 +03003704 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003705 err = -EINVAL;
3706 goto out_free;
3707 }
3708 err = ieee80211_get_ratemask(wiphy->bands[band],
3709 nla_data(attr),
3710 nla_len(attr),
3711 &request->rates[band]);
3712 if (err)
3713 goto out_free;
3714 }
3715 }
3716
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303717 request->no_cck =
3718 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3719
Johannes Berg463d0182009-07-14 00:33:35 +02003720 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003721 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003722
Johannes Berg79c97e92009-07-07 03:56:12 +02003723 rdev->scan_req = request;
3724 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003725
Johannes Berg463d0182009-07-14 00:33:35 +02003726 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003727 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003728 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003729 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003730 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003731 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003732 kfree(request);
3733 }
Johannes Berg3b858752009-03-12 09:55:09 +01003734
Johannes Berg2a519312009-02-10 21:25:55 +01003735 return err;
3736}
3737
Luciano Coelho807f8a82011-05-11 17:09:35 +03003738static int nl80211_start_sched_scan(struct sk_buff *skb,
3739 struct genl_info *info)
3740{
3741 struct cfg80211_sched_scan_request *request;
3742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3743 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003744 struct nlattr *attr;
3745 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003746 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003747 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003748 enum ieee80211_band band;
3749 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003750 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003751
3752 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3753 !rdev->ops->sched_scan_start)
3754 return -EOPNOTSUPP;
3755
3756 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3757 return -EINVAL;
3758
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003759 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3760 return -EINVAL;
3761
3762 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3763 if (interval == 0)
3764 return -EINVAL;
3765
Luciano Coelho807f8a82011-05-11 17:09:35 +03003766 wiphy = &rdev->wiphy;
3767
3768 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3769 n_channels = validate_scan_freqs(
3770 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3771 if (!n_channels)
3772 return -EINVAL;
3773 } else {
3774 n_channels = 0;
3775
3776 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3777 if (wiphy->bands[band])
3778 n_channels += wiphy->bands[band]->n_channels;
3779 }
3780
3781 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3782 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3783 tmp)
3784 n_ssids++;
3785
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003786 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003787 return -EINVAL;
3788
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003789 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3790 nla_for_each_nested(attr,
3791 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3792 tmp)
3793 n_match_sets++;
3794
3795 if (n_match_sets > wiphy->max_match_sets)
3796 return -EINVAL;
3797
Luciano Coelho807f8a82011-05-11 17:09:35 +03003798 if (info->attrs[NL80211_ATTR_IE])
3799 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3800 else
3801 ie_len = 0;
3802
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003803 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003804 return -EINVAL;
3805
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003806 mutex_lock(&rdev->sched_scan_mtx);
3807
3808 if (rdev->sched_scan_req) {
3809 err = -EINPROGRESS;
3810 goto out;
3811 }
3812
Luciano Coelho807f8a82011-05-11 17:09:35 +03003813 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003814 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003815 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003816 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003817 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003818 if (!request) {
3819 err = -ENOMEM;
3820 goto out;
3821 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003822
3823 if (n_ssids)
3824 request->ssids = (void *)&request->channels[n_channels];
3825 request->n_ssids = n_ssids;
3826 if (ie_len) {
3827 if (request->ssids)
3828 request->ie = (void *)(request->ssids + n_ssids);
3829 else
3830 request->ie = (void *)(request->channels + n_channels);
3831 }
3832
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003833 if (n_match_sets) {
3834 if (request->ie)
3835 request->match_sets = (void *)(request->ie + ie_len);
3836 else if (request->ssids)
3837 request->match_sets =
3838 (void *)(request->ssids + n_ssids);
3839 else
3840 request->match_sets =
3841 (void *)(request->channels + n_channels);
3842 }
3843 request->n_match_sets = n_match_sets;
3844
Luciano Coelho807f8a82011-05-11 17:09:35 +03003845 i = 0;
3846 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3847 /* user specified, bail out if channel not found */
3848 nla_for_each_nested(attr,
3849 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3850 tmp) {
3851 struct ieee80211_channel *chan;
3852
3853 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3854
3855 if (!chan) {
3856 err = -EINVAL;
3857 goto out_free;
3858 }
3859
3860 /* ignore disabled channels */
3861 if (chan->flags & IEEE80211_CHAN_DISABLED)
3862 continue;
3863
3864 request->channels[i] = chan;
3865 i++;
3866 }
3867 } else {
3868 /* all channels */
3869 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3870 int j;
3871 if (!wiphy->bands[band])
3872 continue;
3873 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3874 struct ieee80211_channel *chan;
3875
3876 chan = &wiphy->bands[band]->channels[j];
3877
3878 if (chan->flags & IEEE80211_CHAN_DISABLED)
3879 continue;
3880
3881 request->channels[i] = chan;
3882 i++;
3883 }
3884 }
3885 }
3886
3887 if (!i) {
3888 err = -EINVAL;
3889 goto out_free;
3890 }
3891
3892 request->n_channels = i;
3893
3894 i = 0;
3895 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3896 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3897 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003898 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003899 err = -EINVAL;
3900 goto out_free;
3901 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003902 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003903 memcpy(request->ssids[i].ssid, nla_data(attr),
3904 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003905 i++;
3906 }
3907 }
3908
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003909 i = 0;
3910 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3911 nla_for_each_nested(attr,
3912 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3913 tmp) {
3914 struct nlattr *ssid;
3915
3916 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3917 nla_data(attr), nla_len(attr),
3918 nl80211_match_policy);
3919 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3920 if (ssid) {
3921 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3922 err = -EINVAL;
3923 goto out_free;
3924 }
3925 memcpy(request->match_sets[i].ssid.ssid,
3926 nla_data(ssid), nla_len(ssid));
3927 request->match_sets[i].ssid.ssid_len =
3928 nla_len(ssid);
3929 }
3930 i++;
3931 }
3932 }
3933
Luciano Coelho807f8a82011-05-11 17:09:35 +03003934 if (info->attrs[NL80211_ATTR_IE]) {
3935 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3936 memcpy((void *)request->ie,
3937 nla_data(info->attrs[NL80211_ATTR_IE]),
3938 request->ie_len);
3939 }
3940
3941 request->dev = dev;
3942 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003943 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003944
3945 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3946 if (!err) {
3947 rdev->sched_scan_req = request;
3948 nl80211_send_sched_scan(rdev, dev,
3949 NL80211_CMD_START_SCHED_SCAN);
3950 goto out;
3951 }
3952
3953out_free:
3954 kfree(request);
3955out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003956 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003957 return err;
3958}
3959
3960static int nl80211_stop_sched_scan(struct sk_buff *skb,
3961 struct genl_info *info)
3962{
3963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003964 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003965
3966 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3967 !rdev->ops->sched_scan_stop)
3968 return -EOPNOTSUPP;
3969
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003970 mutex_lock(&rdev->sched_scan_mtx);
3971 err = __cfg80211_stop_sched_scan(rdev, false);
3972 mutex_unlock(&rdev->sched_scan_mtx);
3973
3974 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003975}
3976
Johannes Berg9720bb32011-06-21 09:45:33 +02003977static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3978 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003979 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003980 struct wireless_dev *wdev,
3981 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003982{
Johannes Berg48ab9052009-07-10 18:42:31 +02003983 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003984 void *hdr;
3985 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003986 int i;
3987
3988 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003989
Johannes Berg9720bb32011-06-21 09:45:33 +02003990 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003991 NL80211_CMD_NEW_SCAN_RESULTS);
3992 if (!hdr)
3993 return -1;
3994
Johannes Berg9720bb32011-06-21 09:45:33 +02003995 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3996
Johannes Bergf5ea9122009-08-07 16:17:38 +02003997 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003998 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003999
4000 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4001 if (!bss)
4002 goto nla_put_failure;
4003 if (!is_zero_ether_addr(res->bssid))
4004 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4005 if (res->information_elements && res->len_information_elements)
4006 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4007 res->len_information_elements,
4008 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004009 if (res->beacon_ies && res->len_beacon_ies &&
4010 res->beacon_ies != res->information_elements)
4011 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4012 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004013 if (res->tsf)
4014 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4015 if (res->beacon_interval)
4016 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4017 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4018 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004019 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4020 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004021
Johannes Berg77965c92009-02-18 18:45:06 +01004022 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004023 case CFG80211_SIGNAL_TYPE_MBM:
4024 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4025 break;
4026 case CFG80211_SIGNAL_TYPE_UNSPEC:
4027 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4028 break;
4029 default:
4030 break;
4031 }
4032
Johannes Berg48ab9052009-07-10 18:42:31 +02004033 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004034 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004035 case NL80211_IFTYPE_STATION:
4036 if (intbss == wdev->current_bss)
4037 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4038 NL80211_BSS_STATUS_ASSOCIATED);
4039 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4040 if (intbss != wdev->auth_bsses[i])
4041 continue;
4042 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4043 NL80211_BSS_STATUS_AUTHENTICATED);
4044 break;
4045 }
4046 break;
4047 case NL80211_IFTYPE_ADHOC:
4048 if (intbss == wdev->current_bss)
4049 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4050 NL80211_BSS_STATUS_IBSS_JOINED);
4051 break;
4052 default:
4053 break;
4054 }
4055
Johannes Berg2a519312009-02-10 21:25:55 +01004056 nla_nest_end(msg, bss);
4057
4058 return genlmsg_end(msg, hdr);
4059
4060 nla_put_failure:
4061 genlmsg_cancel(msg, hdr);
4062 return -EMSGSIZE;
4063}
4064
4065static int nl80211_dump_scan(struct sk_buff *skb,
4066 struct netlink_callback *cb)
4067{
Johannes Berg48ab9052009-07-10 18:42:31 +02004068 struct cfg80211_registered_device *rdev;
4069 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004070 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004071 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004072 int start = cb->args[1], idx = 0;
4073 int err;
4074
Johannes Berg67748892010-10-04 21:14:06 +02004075 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4076 if (err)
4077 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004078
Johannes Berg48ab9052009-07-10 18:42:31 +02004079 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004080
Johannes Berg48ab9052009-07-10 18:42:31 +02004081 wdev_lock(wdev);
4082 spin_lock_bh(&rdev->bss_lock);
4083 cfg80211_bss_expire(rdev);
4084
Johannes Berg9720bb32011-06-21 09:45:33 +02004085 cb->seq = rdev->bss_generation;
4086
Johannes Berg48ab9052009-07-10 18:42:31 +02004087 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004088 if (++idx <= start)
4089 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004090 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004091 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004092 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004093 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004094 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004095 }
4096 }
4097
Johannes Berg48ab9052009-07-10 18:42:31 +02004098 spin_unlock_bh(&rdev->bss_lock);
4099 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004100
4101 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004102 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004103
Johannes Berg67748892010-10-04 21:14:06 +02004104 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004105}
4106
Holger Schurig61fa7132009-11-11 12:25:40 +01004107static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4108 int flags, struct net_device *dev,
4109 struct survey_info *survey)
4110{
4111 void *hdr;
4112 struct nlattr *infoattr;
4113
Holger Schurig61fa7132009-11-11 12:25:40 +01004114 hdr = nl80211hdr_put(msg, pid, seq, flags,
4115 NL80211_CMD_NEW_SURVEY_RESULTS);
4116 if (!hdr)
4117 return -ENOMEM;
4118
4119 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4120
4121 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4122 if (!infoattr)
4123 goto nla_put_failure;
4124
4125 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4126 survey->channel->center_freq);
4127 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4128 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4129 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004130 if (survey->filled & SURVEY_INFO_IN_USE)
4131 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004132 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4133 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4134 survey->channel_time);
4135 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4136 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4137 survey->channel_time_busy);
4138 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4139 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4140 survey->channel_time_ext_busy);
4141 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4142 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4143 survey->channel_time_rx);
4144 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4145 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4146 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004147
4148 nla_nest_end(msg, infoattr);
4149
4150 return genlmsg_end(msg, hdr);
4151
4152 nla_put_failure:
4153 genlmsg_cancel(msg, hdr);
4154 return -EMSGSIZE;
4155}
4156
4157static int nl80211_dump_survey(struct sk_buff *skb,
4158 struct netlink_callback *cb)
4159{
4160 struct survey_info survey;
4161 struct cfg80211_registered_device *dev;
4162 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004163 int survey_idx = cb->args[1];
4164 int res;
4165
Johannes Berg67748892010-10-04 21:14:06 +02004166 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4167 if (res)
4168 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004169
4170 if (!dev->ops->dump_survey) {
4171 res = -EOPNOTSUPP;
4172 goto out_err;
4173 }
4174
4175 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004176 struct ieee80211_channel *chan;
4177
Holger Schurig61fa7132009-11-11 12:25:40 +01004178 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4179 &survey);
4180 if (res == -ENOENT)
4181 break;
4182 if (res)
4183 goto out_err;
4184
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004185 /* Survey without a channel doesn't make sense */
4186 if (!survey.channel) {
4187 res = -EINVAL;
4188 goto out;
4189 }
4190
4191 chan = ieee80211_get_channel(&dev->wiphy,
4192 survey.channel->center_freq);
4193 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4194 survey_idx++;
4195 continue;
4196 }
4197
Holger Schurig61fa7132009-11-11 12:25:40 +01004198 if (nl80211_send_survey(skb,
4199 NETLINK_CB(cb->skb).pid,
4200 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4201 netdev,
4202 &survey) < 0)
4203 goto out;
4204 survey_idx++;
4205 }
4206
4207 out:
4208 cb->args[1] = survey_idx;
4209 res = skb->len;
4210 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004211 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004212 return res;
4213}
4214
Jouni Malinen255e7372009-03-20 21:21:17 +02004215static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4216{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004217 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004218}
4219
Samuel Ortizb23aa672009-07-01 21:26:54 +02004220static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4221{
4222 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4223 NL80211_WPA_VERSION_2));
4224}
4225
Jouni Malinen636a5d32009-03-19 13:39:22 +02004226static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4227{
Johannes Berg4c476992010-10-04 21:36:35 +02004228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4229 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004230 struct ieee80211_channel *chan;
4231 const u8 *bssid, *ssid, *ie = NULL;
4232 int err, ssid_len, ie_len = 0;
4233 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004234 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004235 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004236
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004237 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4238 return -EINVAL;
4239
4240 if (!info->attrs[NL80211_ATTR_MAC])
4241 return -EINVAL;
4242
Jouni Malinen17780922009-03-27 20:52:47 +02004243 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4244 return -EINVAL;
4245
Johannes Berg19957bb2009-07-02 17:20:43 +02004246 if (!info->attrs[NL80211_ATTR_SSID])
4247 return -EINVAL;
4248
4249 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4250 return -EINVAL;
4251
Johannes Bergfffd0932009-07-08 14:22:54 +02004252 err = nl80211_parse_key(info, &key);
4253 if (err)
4254 return err;
4255
4256 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004257 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4258 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004259 if (!key.p.key || !key.p.key_len)
4260 return -EINVAL;
4261 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4262 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4263 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4264 key.p.key_len != WLAN_KEY_LEN_WEP104))
4265 return -EINVAL;
4266 if (key.idx > 4)
4267 return -EINVAL;
4268 } else {
4269 key.p.key_len = 0;
4270 key.p.key = NULL;
4271 }
4272
Johannes Bergafea0b72010-08-10 09:46:42 +02004273 if (key.idx >= 0) {
4274 int i;
4275 bool ok = false;
4276 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4277 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4278 ok = true;
4279 break;
4280 }
4281 }
Johannes Berg4c476992010-10-04 21:36:35 +02004282 if (!ok)
4283 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004284 }
4285
Johannes Berg4c476992010-10-04 21:36:35 +02004286 if (!rdev->ops->auth)
4287 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004288
Johannes Berg074ac8d2010-09-16 14:58:22 +02004289 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004290 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4291 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004292
Johannes Berg19957bb2009-07-02 17:20:43 +02004293 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004294 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004295 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004296 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4297 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004298
Johannes Berg19957bb2009-07-02 17:20:43 +02004299 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4300 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4301
4302 if (info->attrs[NL80211_ATTR_IE]) {
4303 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4304 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4305 }
4306
4307 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004308 if (!nl80211_valid_auth_type(auth_type))
4309 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004310
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004311 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4312
Johannes Berg4c476992010-10-04 21:36:35 +02004313 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4314 ssid, ssid_len, ie, ie_len,
4315 key.p.key, key.p.key_len, key.idx,
4316 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004317}
4318
Johannes Bergc0692b82010-08-27 14:26:53 +03004319static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4320 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004321 struct cfg80211_crypto_settings *settings,
4322 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004323{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004324 memset(settings, 0, sizeof(*settings));
4325
Samuel Ortizb23aa672009-07-01 21:26:54 +02004326 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4327
Johannes Bergc0692b82010-08-27 14:26:53 +03004328 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4329 u16 proto;
4330 proto = nla_get_u16(
4331 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4332 settings->control_port_ethertype = cpu_to_be16(proto);
4333 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4334 proto != ETH_P_PAE)
4335 return -EINVAL;
4336 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4337 settings->control_port_no_encrypt = true;
4338 } else
4339 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4340
Samuel Ortizb23aa672009-07-01 21:26:54 +02004341 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4342 void *data;
4343 int len, i;
4344
4345 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4346 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4347 settings->n_ciphers_pairwise = len / sizeof(u32);
4348
4349 if (len % sizeof(u32))
4350 return -EINVAL;
4351
Johannes Berg3dc27d22009-07-02 21:36:37 +02004352 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004353 return -EINVAL;
4354
4355 memcpy(settings->ciphers_pairwise, data, len);
4356
4357 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004358 if (!cfg80211_supported_cipher_suite(
4359 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004360 settings->ciphers_pairwise[i]))
4361 return -EINVAL;
4362 }
4363
4364 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4365 settings->cipher_group =
4366 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004367 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4368 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004369 return -EINVAL;
4370 }
4371
4372 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4373 settings->wpa_versions =
4374 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4375 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4376 return -EINVAL;
4377 }
4378
4379 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4380 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004381 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004382
4383 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4384 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4385 settings->n_akm_suites = len / sizeof(u32);
4386
4387 if (len % sizeof(u32))
4388 return -EINVAL;
4389
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004390 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4391 return -EINVAL;
4392
Samuel Ortizb23aa672009-07-01 21:26:54 +02004393 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004394 }
4395
4396 return 0;
4397}
4398
Jouni Malinen636a5d32009-03-19 13:39:22 +02004399static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4400{
Johannes Berg4c476992010-10-04 21:36:35 +02004401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4402 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004403 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004404 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004405 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004406 int err, ssid_len, ie_len = 0;
4407 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004408
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004409 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4410 return -EINVAL;
4411
4412 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004413 !info->attrs[NL80211_ATTR_SSID] ||
4414 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004415 return -EINVAL;
4416
Johannes Berg4c476992010-10-04 21:36:35 +02004417 if (!rdev->ops->assoc)
4418 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004419
Johannes Berg074ac8d2010-09-16 14:58:22 +02004420 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004421 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4422 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004423
Johannes Berg19957bb2009-07-02 17:20:43 +02004424 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004425
Johannes Berg19957bb2009-07-02 17:20:43 +02004426 chan = ieee80211_get_channel(&rdev->wiphy,
4427 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004428 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4429 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004430
Johannes Berg19957bb2009-07-02 17:20:43 +02004431 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4432 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004433
4434 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004435 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4436 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004437 }
4438
Jouni Malinendc6382c2009-05-06 22:09:37 +03004439 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004440 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004441 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004442 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004443 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004444 else if (mfp != NL80211_MFP_NO)
4445 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004446 }
4447
Johannes Berg3e5d7642009-07-07 14:37:26 +02004448 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4449 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4450
Johannes Bergc0692b82010-08-27 14:26:53 +03004451 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004452 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004453 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4454 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004455 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004456
Jouni Malinen636a5d32009-03-19 13:39:22 +02004457 return err;
4458}
4459
4460static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4461{
Johannes Berg4c476992010-10-04 21:36:35 +02004462 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4463 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004464 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004465 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004466 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004467 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004468
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004469 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4470 return -EINVAL;
4471
4472 if (!info->attrs[NL80211_ATTR_MAC])
4473 return -EINVAL;
4474
4475 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4476 return -EINVAL;
4477
Johannes Berg4c476992010-10-04 21:36:35 +02004478 if (!rdev->ops->deauth)
4479 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004480
Johannes Berg074ac8d2010-09-16 14:58:22 +02004481 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004482 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4483 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004484
Johannes Berg19957bb2009-07-02 17:20:43 +02004485 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004486
Johannes Berg19957bb2009-07-02 17:20:43 +02004487 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4488 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004489 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004490 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004491 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004492
4493 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004494 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4495 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004496 }
4497
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004498 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4499
Johannes Berg4c476992010-10-04 21:36:35 +02004500 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4501 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004502}
4503
4504static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4505{
Johannes Berg4c476992010-10-04 21:36:35 +02004506 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4507 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004508 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004509 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004510 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004511 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004512
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004513 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4514 return -EINVAL;
4515
4516 if (!info->attrs[NL80211_ATTR_MAC])
4517 return -EINVAL;
4518
4519 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4520 return -EINVAL;
4521
Johannes Berg4c476992010-10-04 21:36:35 +02004522 if (!rdev->ops->disassoc)
4523 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004524
Johannes Berg074ac8d2010-09-16 14:58:22 +02004525 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004526 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4527 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004528
Johannes Berg19957bb2009-07-02 17:20:43 +02004529 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004530
Johannes Berg19957bb2009-07-02 17:20:43 +02004531 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4532 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004533 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004534 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004535 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004536
4537 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004538 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4539 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004540 }
4541
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004542 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4543
Johannes Berg4c476992010-10-04 21:36:35 +02004544 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4545 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004546}
4547
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004548static bool
4549nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4550 int mcast_rate[IEEE80211_NUM_BANDS],
4551 int rateval)
4552{
4553 struct wiphy *wiphy = &rdev->wiphy;
4554 bool found = false;
4555 int band, i;
4556
4557 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4558 struct ieee80211_supported_band *sband;
4559
4560 sband = wiphy->bands[band];
4561 if (!sband)
4562 continue;
4563
4564 for (i = 0; i < sband->n_bitrates; i++) {
4565 if (sband->bitrates[i].bitrate == rateval) {
4566 mcast_rate[band] = i + 1;
4567 found = true;
4568 break;
4569 }
4570 }
4571 }
4572
4573 return found;
4574}
4575
Johannes Berg04a773a2009-04-19 21:24:32 +02004576static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4577{
Johannes Berg4c476992010-10-04 21:36:35 +02004578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4579 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004580 struct cfg80211_ibss_params ibss;
4581 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004582 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004583 int err;
4584
Johannes Berg8e30bc52009-04-22 17:45:38 +02004585 memset(&ibss, 0, sizeof(ibss));
4586
Johannes Berg04a773a2009-04-19 21:24:32 +02004587 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4588 return -EINVAL;
4589
4590 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4591 !info->attrs[NL80211_ATTR_SSID] ||
4592 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4593 return -EINVAL;
4594
Johannes Berg8e30bc52009-04-22 17:45:38 +02004595 ibss.beacon_interval = 100;
4596
4597 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4598 ibss.beacon_interval =
4599 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4600 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4601 return -EINVAL;
4602 }
4603
Johannes Berg4c476992010-10-04 21:36:35 +02004604 if (!rdev->ops->join_ibss)
4605 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004606
Johannes Berg4c476992010-10-04 21:36:35 +02004607 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4608 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004609
Johannes Berg79c97e92009-07-07 03:56:12 +02004610 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004611
Johannes Berg39193492011-09-16 13:45:25 +02004612 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004613 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004614
4615 if (!is_valid_ether_addr(ibss.bssid))
4616 return -EINVAL;
4617 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004618 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4619 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4620
4621 if (info->attrs[NL80211_ATTR_IE]) {
4622 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4623 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4624 }
4625
4626 ibss.channel = ieee80211_get_channel(wiphy,
4627 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4628 if (!ibss.channel ||
4629 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004630 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4631 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004632
4633 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004634 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004635
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004636 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4637 u8 *rates =
4638 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4639 int n_rates =
4640 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4641 struct ieee80211_supported_band *sband =
4642 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004643 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004644
Johannes Berg34850ab2011-07-18 18:08:35 +02004645 err = ieee80211_get_ratemask(sband, rates, n_rates,
4646 &ibss.basic_rates);
4647 if (err)
4648 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004649 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004650
4651 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4652 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4653 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4654 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004655
Johannes Berg4c476992010-10-04 21:36:35 +02004656 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4657 connkeys = nl80211_parse_connkeys(rdev,
4658 info->attrs[NL80211_ATTR_KEYS]);
4659 if (IS_ERR(connkeys))
4660 return PTR_ERR(connkeys);
4661 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004662
Johannes Berg4c476992010-10-04 21:36:35 +02004663 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004664 if (err)
4665 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004666 return err;
4667}
4668
4669static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4670{
Johannes Berg4c476992010-10-04 21:36:35 +02004671 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4672 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004673
Johannes Berg4c476992010-10-04 21:36:35 +02004674 if (!rdev->ops->leave_ibss)
4675 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004676
Johannes Berg4c476992010-10-04 21:36:35 +02004677 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4678 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004679
Johannes Berg4c476992010-10-04 21:36:35 +02004680 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004681}
4682
Johannes Bergaff89a92009-07-01 21:26:51 +02004683#ifdef CONFIG_NL80211_TESTMODE
4684static struct genl_multicast_group nl80211_testmode_mcgrp = {
4685 .name = "testmode",
4686};
4687
4688static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4689{
Johannes Berg4c476992010-10-04 21:36:35 +02004690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004691 int err;
4692
4693 if (!info->attrs[NL80211_ATTR_TESTDATA])
4694 return -EINVAL;
4695
Johannes Bergaff89a92009-07-01 21:26:51 +02004696 err = -EOPNOTSUPP;
4697 if (rdev->ops->testmode_cmd) {
4698 rdev->testmode_info = info;
4699 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4700 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4701 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4702 rdev->testmode_info = NULL;
4703 }
4704
Johannes Bergaff89a92009-07-01 21:26:51 +02004705 return err;
4706}
4707
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004708static int nl80211_testmode_dump(struct sk_buff *skb,
4709 struct netlink_callback *cb)
4710{
4711 struct cfg80211_registered_device *dev;
4712 int err;
4713 long phy_idx;
4714 void *data = NULL;
4715 int data_len = 0;
4716
4717 if (cb->args[0]) {
4718 /*
4719 * 0 is a valid index, but not valid for args[0],
4720 * so we need to offset by 1.
4721 */
4722 phy_idx = cb->args[0] - 1;
4723 } else {
4724 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4725 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4726 nl80211_policy);
4727 if (err)
4728 return err;
4729 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4730 return -EINVAL;
4731 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4732 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4733 cb->args[1] =
4734 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4735 }
4736
4737 if (cb->args[1]) {
4738 data = nla_data((void *)cb->args[1]);
4739 data_len = nla_len((void *)cb->args[1]);
4740 }
4741
4742 mutex_lock(&cfg80211_mutex);
4743 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4744 if (!dev) {
4745 mutex_unlock(&cfg80211_mutex);
4746 return -ENOENT;
4747 }
4748 cfg80211_lock_rdev(dev);
4749 mutex_unlock(&cfg80211_mutex);
4750
4751 if (!dev->ops->testmode_dump) {
4752 err = -EOPNOTSUPP;
4753 goto out_err;
4754 }
4755
4756 while (1) {
4757 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4758 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4759 NL80211_CMD_TESTMODE);
4760 struct nlattr *tmdata;
4761
4762 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4763 genlmsg_cancel(skb, hdr);
4764 break;
4765 }
4766
4767 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4768 if (!tmdata) {
4769 genlmsg_cancel(skb, hdr);
4770 break;
4771 }
4772 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4773 data, data_len);
4774 nla_nest_end(skb, tmdata);
4775
4776 if (err == -ENOBUFS || err == -ENOENT) {
4777 genlmsg_cancel(skb, hdr);
4778 break;
4779 } else if (err) {
4780 genlmsg_cancel(skb, hdr);
4781 goto out_err;
4782 }
4783
4784 genlmsg_end(skb, hdr);
4785 }
4786
4787 err = skb->len;
4788 /* see above */
4789 cb->args[0] = phy_idx + 1;
4790 out_err:
4791 cfg80211_unlock_rdev(dev);
4792 return err;
4793}
4794
Johannes Bergaff89a92009-07-01 21:26:51 +02004795static struct sk_buff *
4796__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4797 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4798{
4799 struct sk_buff *skb;
4800 void *hdr;
4801 struct nlattr *data;
4802
4803 skb = nlmsg_new(approxlen + 100, gfp);
4804 if (!skb)
4805 return NULL;
4806
4807 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4808 if (!hdr) {
4809 kfree_skb(skb);
4810 return NULL;
4811 }
4812
4813 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4814 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4815
4816 ((void **)skb->cb)[0] = rdev;
4817 ((void **)skb->cb)[1] = hdr;
4818 ((void **)skb->cb)[2] = data;
4819
4820 return skb;
4821
4822 nla_put_failure:
4823 kfree_skb(skb);
4824 return NULL;
4825}
4826
4827struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4828 int approxlen)
4829{
4830 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4831
4832 if (WARN_ON(!rdev->testmode_info))
4833 return NULL;
4834
4835 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4836 rdev->testmode_info->snd_pid,
4837 rdev->testmode_info->snd_seq,
4838 GFP_KERNEL);
4839}
4840EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4841
4842int cfg80211_testmode_reply(struct sk_buff *skb)
4843{
4844 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4845 void *hdr = ((void **)skb->cb)[1];
4846 struct nlattr *data = ((void **)skb->cb)[2];
4847
4848 if (WARN_ON(!rdev->testmode_info)) {
4849 kfree_skb(skb);
4850 return -EINVAL;
4851 }
4852
4853 nla_nest_end(skb, data);
4854 genlmsg_end(skb, hdr);
4855 return genlmsg_reply(skb, rdev->testmode_info);
4856}
4857EXPORT_SYMBOL(cfg80211_testmode_reply);
4858
4859struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4860 int approxlen, gfp_t gfp)
4861{
4862 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4863
4864 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4865}
4866EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4867
4868void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4869{
4870 void *hdr = ((void **)skb->cb)[1];
4871 struct nlattr *data = ((void **)skb->cb)[2];
4872
4873 nla_nest_end(skb, data);
4874 genlmsg_end(skb, hdr);
4875 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4876}
4877EXPORT_SYMBOL(cfg80211_testmode_event);
4878#endif
4879
Samuel Ortizb23aa672009-07-01 21:26:54 +02004880static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4881{
Johannes Berg4c476992010-10-04 21:36:35 +02004882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4883 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004884 struct cfg80211_connect_params connect;
4885 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004886 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004887 int err;
4888
4889 memset(&connect, 0, sizeof(connect));
4890
4891 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4892 return -EINVAL;
4893
4894 if (!info->attrs[NL80211_ATTR_SSID] ||
4895 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4896 return -EINVAL;
4897
4898 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4899 connect.auth_type =
4900 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4901 if (!nl80211_valid_auth_type(connect.auth_type))
4902 return -EINVAL;
4903 } else
4904 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4905
4906 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4907
Johannes Bergc0692b82010-08-27 14:26:53 +03004908 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004909 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004910 if (err)
4911 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004912
Johannes Berg074ac8d2010-09-16 14:58:22 +02004913 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004914 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4915 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004916
Johannes Berg79c97e92009-07-07 03:56:12 +02004917 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004918
Samuel Ortizb23aa672009-07-01 21:26:54 +02004919 if (info->attrs[NL80211_ATTR_MAC])
4920 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4921 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4922 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4923
4924 if (info->attrs[NL80211_ATTR_IE]) {
4925 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4926 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4927 }
4928
4929 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4930 connect.channel =
4931 ieee80211_get_channel(wiphy,
4932 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4933 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004934 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4935 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004936 }
4937
Johannes Bergfffd0932009-07-08 14:22:54 +02004938 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4939 connkeys = nl80211_parse_connkeys(rdev,
4940 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004941 if (IS_ERR(connkeys))
4942 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004943 }
4944
4945 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004946 if (err)
4947 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004948 return err;
4949}
4950
4951static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4952{
Johannes Berg4c476992010-10-04 21:36:35 +02004953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4954 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004955 u16 reason;
4956
4957 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4958 reason = WLAN_REASON_DEAUTH_LEAVING;
4959 else
4960 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4961
4962 if (reason == 0)
4963 return -EINVAL;
4964
Johannes Berg074ac8d2010-09-16 14:58:22 +02004965 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004966 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4967 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004968
Johannes Berg4c476992010-10-04 21:36:35 +02004969 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004970}
4971
Johannes Berg463d0182009-07-14 00:33:35 +02004972static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4973{
Johannes Berg4c476992010-10-04 21:36:35 +02004974 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004975 struct net *net;
4976 int err;
4977 u32 pid;
4978
4979 if (!info->attrs[NL80211_ATTR_PID])
4980 return -EINVAL;
4981
4982 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4983
Johannes Berg463d0182009-07-14 00:33:35 +02004984 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004985 if (IS_ERR(net))
4986 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004987
4988 err = 0;
4989
4990 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004991 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4992 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004993
Johannes Berg463d0182009-07-14 00:33:35 +02004994 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004995 return err;
4996}
4997
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004998static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4999{
Johannes Berg4c476992010-10-04 21:36:35 +02005000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005001 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5002 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005003 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005004 struct cfg80211_pmksa pmksa;
5005
5006 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5007
5008 if (!info->attrs[NL80211_ATTR_MAC])
5009 return -EINVAL;
5010
5011 if (!info->attrs[NL80211_ATTR_PMKID])
5012 return -EINVAL;
5013
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005014 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5015 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5016
Johannes Berg074ac8d2010-09-16 14:58:22 +02005017 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005018 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5019 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005020
5021 switch (info->genlhdr->cmd) {
5022 case NL80211_CMD_SET_PMKSA:
5023 rdev_ops = rdev->ops->set_pmksa;
5024 break;
5025 case NL80211_CMD_DEL_PMKSA:
5026 rdev_ops = rdev->ops->del_pmksa;
5027 break;
5028 default:
5029 WARN_ON(1);
5030 break;
5031 }
5032
Johannes Berg4c476992010-10-04 21:36:35 +02005033 if (!rdev_ops)
5034 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005035
Johannes Berg4c476992010-10-04 21:36:35 +02005036 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005037}
5038
5039static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5040{
Johannes Berg4c476992010-10-04 21:36:35 +02005041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5042 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005043
Johannes Berg074ac8d2010-09-16 14:58:22 +02005044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005045 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5046 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005047
Johannes Berg4c476992010-10-04 21:36:35 +02005048 if (!rdev->ops->flush_pmksa)
5049 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005050
Johannes Berg4c476992010-10-04 21:36:35 +02005051 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005052}
5053
Arik Nemtsov109086c2011-09-28 14:12:50 +03005054static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5055{
5056 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5057 struct net_device *dev = info->user_ptr[1];
5058 u8 action_code, dialog_token;
5059 u16 status_code;
5060 u8 *peer;
5061
5062 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5063 !rdev->ops->tdls_mgmt)
5064 return -EOPNOTSUPP;
5065
5066 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5067 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5068 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5069 !info->attrs[NL80211_ATTR_IE] ||
5070 !info->attrs[NL80211_ATTR_MAC])
5071 return -EINVAL;
5072
5073 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5074 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5075 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5076 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5077
5078 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5079 dialog_token, status_code,
5080 nla_data(info->attrs[NL80211_ATTR_IE]),
5081 nla_len(info->attrs[NL80211_ATTR_IE]));
5082}
5083
5084static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5085{
5086 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5087 struct net_device *dev = info->user_ptr[1];
5088 enum nl80211_tdls_operation operation;
5089 u8 *peer;
5090
5091 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5092 !rdev->ops->tdls_oper)
5093 return -EOPNOTSUPP;
5094
5095 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5096 !info->attrs[NL80211_ATTR_MAC])
5097 return -EINVAL;
5098
5099 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5100 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5101
5102 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5103}
5104
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005105static int nl80211_remain_on_channel(struct sk_buff *skb,
5106 struct genl_info *info)
5107{
Johannes Berg4c476992010-10-04 21:36:35 +02005108 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5109 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005110 struct ieee80211_channel *chan;
5111 struct sk_buff *msg;
5112 void *hdr;
5113 u64 cookie;
5114 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5115 u32 freq, duration;
5116 int err;
5117
5118 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5119 !info->attrs[NL80211_ATTR_DURATION])
5120 return -EINVAL;
5121
5122 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5123
5124 /*
5125 * We should be on that channel for at least one jiffie,
5126 * and more than 5 seconds seems excessive.
5127 */
Johannes Berga2939112010-12-14 17:54:28 +01005128 if (!duration || !msecs_to_jiffies(duration) ||
5129 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005130 return -EINVAL;
5131
Johannes Berg7c4ef712011-11-18 15:33:48 +01005132 if (!rdev->ops->remain_on_channel ||
5133 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005134 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005135
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005136 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5137 channel_type = nla_get_u32(
5138 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5139 if (channel_type != NL80211_CHAN_NO_HT &&
5140 channel_type != NL80211_CHAN_HT20 &&
5141 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005142 channel_type != NL80211_CHAN_HT40MINUS)
5143 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005144 }
5145
5146 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5147 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005148 if (chan == NULL)
5149 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005150
5151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005152 if (!msg)
5153 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005154
5155 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5156 NL80211_CMD_REMAIN_ON_CHANNEL);
5157
5158 if (IS_ERR(hdr)) {
5159 err = PTR_ERR(hdr);
5160 goto free_msg;
5161 }
5162
5163 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5164 channel_type, duration, &cookie);
5165
5166 if (err)
5167 goto free_msg;
5168
5169 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5170
5171 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005172
5173 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005174
5175 nla_put_failure:
5176 err = -ENOBUFS;
5177 free_msg:
5178 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005179 return err;
5180}
5181
5182static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5183 struct genl_info *info)
5184{
Johannes Berg4c476992010-10-04 21:36:35 +02005185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5186 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005187 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005188
5189 if (!info->attrs[NL80211_ATTR_COOKIE])
5190 return -EINVAL;
5191
Johannes Berg4c476992010-10-04 21:36:35 +02005192 if (!rdev->ops->cancel_remain_on_channel)
5193 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005194
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005195 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5196
Johannes Berg4c476992010-10-04 21:36:35 +02005197 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005198}
5199
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005200static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5201 u8 *rates, u8 rates_len)
5202{
5203 u8 i;
5204 u32 mask = 0;
5205
5206 for (i = 0; i < rates_len; i++) {
5207 int rate = (rates[i] & 0x7f) * 5;
5208 int ridx;
5209 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5210 struct ieee80211_rate *srate =
5211 &sband->bitrates[ridx];
5212 if (rate == srate->bitrate) {
5213 mask |= 1 << ridx;
5214 break;
5215 }
5216 }
5217 if (ridx == sband->n_bitrates)
5218 return 0; /* rate not found */
5219 }
5220
5221 return mask;
5222}
5223
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005224static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005225 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5226 .len = NL80211_MAX_SUPP_RATES },
5227};
5228
5229static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5230 struct genl_info *info)
5231{
5232 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005233 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005234 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005235 int rem, i;
5236 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005237 struct nlattr *tx_rates;
5238 struct ieee80211_supported_band *sband;
5239
5240 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5241 return -EINVAL;
5242
Johannes Berg4c476992010-10-04 21:36:35 +02005243 if (!rdev->ops->set_bitrate_mask)
5244 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005245
5246 memset(&mask, 0, sizeof(mask));
5247 /* Default to all rates enabled */
5248 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5249 sband = rdev->wiphy.bands[i];
5250 mask.control[i].legacy =
5251 sband ? (1 << sband->n_bitrates) - 1 : 0;
5252 }
5253
5254 /*
5255 * The nested attribute uses enum nl80211_band as the index. This maps
5256 * directly to the enum ieee80211_band values used in cfg80211.
5257 */
5258 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5259 {
5260 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005261 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5262 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005263 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005264 if (sband == NULL)
5265 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005266 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5267 nla_len(tx_rates), nl80211_txattr_policy);
5268 if (tb[NL80211_TXRATE_LEGACY]) {
5269 mask.control[band].legacy = rateset_to_mask(
5270 sband,
5271 nla_data(tb[NL80211_TXRATE_LEGACY]),
5272 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005273 if (mask.control[band].legacy == 0)
5274 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005275 }
5276 }
5277
Johannes Berg4c476992010-10-04 21:36:35 +02005278 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005279}
5280
Johannes Berg2e161f72010-08-12 15:38:38 +02005281static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005282{
Johannes Berg4c476992010-10-04 21:36:35 +02005283 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5284 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005285 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005286
5287 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5288 return -EINVAL;
5289
Johannes Berg2e161f72010-08-12 15:38:38 +02005290 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5291 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005292
Johannes Berg9d38d852010-06-09 17:20:33 +02005293 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005294 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005295 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5296 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5297 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005298 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005299 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5300 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005301
5302 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005303 if (!rdev->ops->mgmt_tx)
5304 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005305
Johannes Berg4c476992010-10-04 21:36:35 +02005306 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005307 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005308 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5309 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005310}
5311
Johannes Berg2e161f72010-08-12 15:38:38 +02005312static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005313{
Johannes Berg4c476992010-10-04 21:36:35 +02005314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5315 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005316 struct ieee80211_channel *chan;
5317 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005318 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005319 u32 freq;
5320 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005321 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005322 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005323 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005324 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005325 bool offchan, no_cck, dont_wait_for_ack;
5326
5327 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005328
5329 if (!info->attrs[NL80211_ATTR_FRAME] ||
5330 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5331 return -EINVAL;
5332
Johannes Berg4c476992010-10-04 21:36:35 +02005333 if (!rdev->ops->mgmt_tx)
5334 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005335
Johannes Berg9d38d852010-06-09 17:20:33 +02005336 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005337 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005338 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5339 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5340 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005341 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005342 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5343 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005344
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005345 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005346 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005347 return -EINVAL;
5348 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5349 }
5350
Jouni Malinen026331c2010-02-15 12:53:10 +02005351 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5352 channel_type = nla_get_u32(
5353 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5354 if (channel_type != NL80211_CHAN_NO_HT &&
5355 channel_type != NL80211_CHAN_HT20 &&
5356 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005357 channel_type != NL80211_CHAN_HT40MINUS)
5358 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005359 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005360 }
5361
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005362 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5363
Johannes Berg7c4ef712011-11-18 15:33:48 +01005364 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5365 return -EINVAL;
5366
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305367 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5368
Jouni Malinen026331c2010-02-15 12:53:10 +02005369 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5370 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005371 if (chan == NULL)
5372 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005373
Johannes Berge247bd902011-11-04 11:18:21 +01005374 if (!dont_wait_for_ack) {
5375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5376 if (!msg)
5377 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005378
Johannes Berge247bd902011-11-04 11:18:21 +01005379 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5380 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005381
Johannes Berge247bd902011-11-04 11:18:21 +01005382 if (IS_ERR(hdr)) {
5383 err = PTR_ERR(hdr);
5384 goto free_msg;
5385 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005386 }
Johannes Berge247bd902011-11-04 11:18:21 +01005387
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005388 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5389 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005390 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5391 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005392 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005393 if (err)
5394 goto free_msg;
5395
Johannes Berge247bd902011-11-04 11:18:21 +01005396 if (msg) {
5397 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005398
Johannes Berge247bd902011-11-04 11:18:21 +01005399 genlmsg_end(msg, hdr);
5400 return genlmsg_reply(msg, info);
5401 }
5402
5403 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005404
5405 nla_put_failure:
5406 err = -ENOBUFS;
5407 free_msg:
5408 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005409 return err;
5410}
5411
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005412static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5413{
5414 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5415 struct net_device *dev = info->user_ptr[1];
5416 u64 cookie;
5417
5418 if (!info->attrs[NL80211_ATTR_COOKIE])
5419 return -EINVAL;
5420
5421 if (!rdev->ops->mgmt_tx_cancel_wait)
5422 return -EOPNOTSUPP;
5423
5424 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5425 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5426 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5427 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5428 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5429 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5430 return -EOPNOTSUPP;
5431
5432 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5433
5434 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5435}
5436
Kalle Valoffb9eb32010-02-17 17:58:10 +02005437static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5438{
Johannes Berg4c476992010-10-04 21:36:35 +02005439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005440 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005441 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005442 u8 ps_state;
5443 bool state;
5444 int err;
5445
Johannes Berg4c476992010-10-04 21:36:35 +02005446 if (!info->attrs[NL80211_ATTR_PS_STATE])
5447 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005448
5449 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5450
Johannes Berg4c476992010-10-04 21:36:35 +02005451 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5452 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005453
5454 wdev = dev->ieee80211_ptr;
5455
Johannes Berg4c476992010-10-04 21:36:35 +02005456 if (!rdev->ops->set_power_mgmt)
5457 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005458
5459 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5460
5461 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005462 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005463
Johannes Berg4c476992010-10-04 21:36:35 +02005464 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5465 wdev->ps_timeout);
5466 if (!err)
5467 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005468 return err;
5469}
5470
5471static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5472{
Johannes Berg4c476992010-10-04 21:36:35 +02005473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005474 enum nl80211_ps_state ps_state;
5475 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005476 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005477 struct sk_buff *msg;
5478 void *hdr;
5479 int err;
5480
Kalle Valoffb9eb32010-02-17 17:58:10 +02005481 wdev = dev->ieee80211_ptr;
5482
Johannes Berg4c476992010-10-04 21:36:35 +02005483 if (!rdev->ops->set_power_mgmt)
5484 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005485
5486 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005487 if (!msg)
5488 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005489
5490 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5491 NL80211_CMD_GET_POWER_SAVE);
5492 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005493 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005494 goto free_msg;
5495 }
5496
5497 if (wdev->ps)
5498 ps_state = NL80211_PS_ENABLED;
5499 else
5500 ps_state = NL80211_PS_DISABLED;
5501
5502 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5503
5504 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005505 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005506
Johannes Berg4c476992010-10-04 21:36:35 +02005507 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005508 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005509 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005510 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005511 return err;
5512}
5513
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005514static struct nla_policy
5515nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5516 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5517 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5518 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5519};
5520
5521static int nl80211_set_cqm_rssi(struct genl_info *info,
5522 s32 threshold, u32 hysteresis)
5523{
Johannes Berg4c476992010-10-04 21:36:35 +02005524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005525 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005526 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005527
5528 if (threshold > 0)
5529 return -EINVAL;
5530
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005531 wdev = dev->ieee80211_ptr;
5532
Johannes Berg4c476992010-10-04 21:36:35 +02005533 if (!rdev->ops->set_cqm_rssi_config)
5534 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005535
Johannes Berg074ac8d2010-09-16 14:58:22 +02005536 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005537 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5538 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005539
Johannes Berg4c476992010-10-04 21:36:35 +02005540 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5541 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005542}
5543
5544static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5545{
5546 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5547 struct nlattr *cqm;
5548 int err;
5549
5550 cqm = info->attrs[NL80211_ATTR_CQM];
5551 if (!cqm) {
5552 err = -EINVAL;
5553 goto out;
5554 }
5555
5556 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5557 nl80211_attr_cqm_policy);
5558 if (err)
5559 goto out;
5560
5561 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5562 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5563 s32 threshold;
5564 u32 hysteresis;
5565 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5566 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5567 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5568 } else
5569 err = -EINVAL;
5570
5571out:
5572 return err;
5573}
5574
Johannes Berg29cbe682010-12-03 09:20:44 +01005575static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5576{
5577 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5578 struct net_device *dev = info->user_ptr[1];
5579 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005580 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005581 int err;
5582
5583 /* start with default */
5584 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005585 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005586
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005587 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005588 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005589 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005590 if (err)
5591 return err;
5592 }
5593
5594 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5595 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5596 return -EINVAL;
5597
Javier Cardonac80d5452010-12-16 17:37:49 -08005598 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5599 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5600
5601 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5602 /* parse additional setup parameters if given */
5603 err = nl80211_parse_mesh_setup(info, &setup);
5604 if (err)
5605 return err;
5606 }
5607
5608 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005609}
5610
5611static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5612{
5613 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5614 struct net_device *dev = info->user_ptr[1];
5615
5616 return cfg80211_leave_mesh(rdev, dev);
5617}
5618
Johannes Bergff1b6e62011-05-04 15:37:28 +02005619static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5620{
5621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5622 struct sk_buff *msg;
5623 void *hdr;
5624
5625 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5626 return -EOPNOTSUPP;
5627
5628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5629 if (!msg)
5630 return -ENOMEM;
5631
5632 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5633 NL80211_CMD_GET_WOWLAN);
5634 if (!hdr)
5635 goto nla_put_failure;
5636
5637 if (rdev->wowlan) {
5638 struct nlattr *nl_wowlan;
5639
5640 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5641 if (!nl_wowlan)
5642 goto nla_put_failure;
5643
5644 if (rdev->wowlan->any)
5645 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5646 if (rdev->wowlan->disconnect)
5647 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5648 if (rdev->wowlan->magic_pkt)
5649 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005650 if (rdev->wowlan->gtk_rekey_failure)
5651 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5652 if (rdev->wowlan->eap_identity_req)
5653 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5654 if (rdev->wowlan->four_way_handshake)
5655 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5656 if (rdev->wowlan->rfkill_release)
5657 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005658 if (rdev->wowlan->n_patterns) {
5659 struct nlattr *nl_pats, *nl_pat;
5660 int i, pat_len;
5661
5662 nl_pats = nla_nest_start(msg,
5663 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5664 if (!nl_pats)
5665 goto nla_put_failure;
5666
5667 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5668 nl_pat = nla_nest_start(msg, i + 1);
5669 if (!nl_pat)
5670 goto nla_put_failure;
5671 pat_len = rdev->wowlan->patterns[i].pattern_len;
5672 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5673 DIV_ROUND_UP(pat_len, 8),
5674 rdev->wowlan->patterns[i].mask);
5675 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5676 pat_len,
5677 rdev->wowlan->patterns[i].pattern);
5678 nla_nest_end(msg, nl_pat);
5679 }
5680 nla_nest_end(msg, nl_pats);
5681 }
5682
5683 nla_nest_end(msg, nl_wowlan);
5684 }
5685
5686 genlmsg_end(msg, hdr);
5687 return genlmsg_reply(msg, info);
5688
5689nla_put_failure:
5690 nlmsg_free(msg);
5691 return -ENOBUFS;
5692}
5693
5694static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5695{
5696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5697 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5698 struct cfg80211_wowlan no_triggers = {};
5699 struct cfg80211_wowlan new_triggers = {};
5700 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5701 int err, i;
5702
5703 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5704 return -EOPNOTSUPP;
5705
5706 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5707 goto no_triggers;
5708
5709 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5710 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5711 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5712 nl80211_wowlan_policy);
5713 if (err)
5714 return err;
5715
5716 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5717 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5718 return -EINVAL;
5719 new_triggers.any = true;
5720 }
5721
5722 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5723 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5724 return -EINVAL;
5725 new_triggers.disconnect = true;
5726 }
5727
5728 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5729 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5730 return -EINVAL;
5731 new_triggers.magic_pkt = true;
5732 }
5733
Johannes Berg77dbbb12011-07-13 10:48:55 +02005734 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5735 return -EINVAL;
5736
5737 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5738 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5739 return -EINVAL;
5740 new_triggers.gtk_rekey_failure = true;
5741 }
5742
5743 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5744 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5745 return -EINVAL;
5746 new_triggers.eap_identity_req = true;
5747 }
5748
5749 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5750 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5751 return -EINVAL;
5752 new_triggers.four_way_handshake = true;
5753 }
5754
5755 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5756 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5757 return -EINVAL;
5758 new_triggers.rfkill_release = true;
5759 }
5760
Johannes Bergff1b6e62011-05-04 15:37:28 +02005761 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5762 struct nlattr *pat;
5763 int n_patterns = 0;
5764 int rem, pat_len, mask_len;
5765 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5766
5767 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5768 rem)
5769 n_patterns++;
5770 if (n_patterns > wowlan->n_patterns)
5771 return -EINVAL;
5772
5773 new_triggers.patterns = kcalloc(n_patterns,
5774 sizeof(new_triggers.patterns[0]),
5775 GFP_KERNEL);
5776 if (!new_triggers.patterns)
5777 return -ENOMEM;
5778
5779 new_triggers.n_patterns = n_patterns;
5780 i = 0;
5781
5782 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5783 rem) {
5784 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5785 nla_data(pat), nla_len(pat), NULL);
5786 err = -EINVAL;
5787 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5788 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5789 goto error;
5790 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5791 mask_len = DIV_ROUND_UP(pat_len, 8);
5792 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5793 mask_len)
5794 goto error;
5795 if (pat_len > wowlan->pattern_max_len ||
5796 pat_len < wowlan->pattern_min_len)
5797 goto error;
5798
5799 new_triggers.patterns[i].mask =
5800 kmalloc(mask_len + pat_len, GFP_KERNEL);
5801 if (!new_triggers.patterns[i].mask) {
5802 err = -ENOMEM;
5803 goto error;
5804 }
5805 new_triggers.patterns[i].pattern =
5806 new_triggers.patterns[i].mask + mask_len;
5807 memcpy(new_triggers.patterns[i].mask,
5808 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5809 mask_len);
5810 new_triggers.patterns[i].pattern_len = pat_len;
5811 memcpy(new_triggers.patterns[i].pattern,
5812 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5813 pat_len);
5814 i++;
5815 }
5816 }
5817
5818 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5819 struct cfg80211_wowlan *ntrig;
5820 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5821 GFP_KERNEL);
5822 if (!ntrig) {
5823 err = -ENOMEM;
5824 goto error;
5825 }
5826 cfg80211_rdev_free_wowlan(rdev);
5827 rdev->wowlan = ntrig;
5828 } else {
5829 no_triggers:
5830 cfg80211_rdev_free_wowlan(rdev);
5831 rdev->wowlan = NULL;
5832 }
5833
5834 return 0;
5835 error:
5836 for (i = 0; i < new_triggers.n_patterns; i++)
5837 kfree(new_triggers.patterns[i].mask);
5838 kfree(new_triggers.patterns);
5839 return err;
5840}
5841
Johannes Berge5497d72011-07-05 16:35:40 +02005842static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5843{
5844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5845 struct net_device *dev = info->user_ptr[1];
5846 struct wireless_dev *wdev = dev->ieee80211_ptr;
5847 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5848 struct cfg80211_gtk_rekey_data rekey_data;
5849 int err;
5850
5851 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5852 return -EINVAL;
5853
5854 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5855 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5856 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5857 nl80211_rekey_policy);
5858 if (err)
5859 return err;
5860
5861 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5862 return -ERANGE;
5863 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5864 return -ERANGE;
5865 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5866 return -ERANGE;
5867
5868 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5869 NL80211_KEK_LEN);
5870 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5871 NL80211_KCK_LEN);
5872 memcpy(rekey_data.replay_ctr,
5873 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5874 NL80211_REPLAY_CTR_LEN);
5875
5876 wdev_lock(wdev);
5877 if (!wdev->current_bss) {
5878 err = -ENOTCONN;
5879 goto out;
5880 }
5881
5882 if (!rdev->ops->set_rekey_data) {
5883 err = -EOPNOTSUPP;
5884 goto out;
5885 }
5886
5887 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5888 out:
5889 wdev_unlock(wdev);
5890 return err;
5891}
5892
Johannes Berg28946da2011-11-04 11:18:12 +01005893static int nl80211_register_unexpected_frame(struct sk_buff *skb,
5894 struct genl_info *info)
5895{
5896 struct net_device *dev = info->user_ptr[1];
5897 struct wireless_dev *wdev = dev->ieee80211_ptr;
5898
5899 if (wdev->iftype != NL80211_IFTYPE_AP &&
5900 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5901 return -EINVAL;
5902
5903 if (wdev->ap_unexpected_nlpid)
5904 return -EBUSY;
5905
5906 wdev->ap_unexpected_nlpid = info->snd_pid;
5907 return 0;
5908}
5909
Johannes Berg7f6cf312011-11-04 11:18:15 +01005910static int nl80211_probe_client(struct sk_buff *skb,
5911 struct genl_info *info)
5912{
5913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5914 struct net_device *dev = info->user_ptr[1];
5915 struct wireless_dev *wdev = dev->ieee80211_ptr;
5916 struct sk_buff *msg;
5917 void *hdr;
5918 const u8 *addr;
5919 u64 cookie;
5920 int err;
5921
5922 if (wdev->iftype != NL80211_IFTYPE_AP &&
5923 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5924 return -EOPNOTSUPP;
5925
5926 if (!info->attrs[NL80211_ATTR_MAC])
5927 return -EINVAL;
5928
5929 if (!rdev->ops->probe_client)
5930 return -EOPNOTSUPP;
5931
5932 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5933 if (!msg)
5934 return -ENOMEM;
5935
5936 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5937 NL80211_CMD_PROBE_CLIENT);
5938
5939 if (IS_ERR(hdr)) {
5940 err = PTR_ERR(hdr);
5941 goto free_msg;
5942 }
5943
5944 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5945
5946 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
5947 if (err)
5948 goto free_msg;
5949
5950 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5951
5952 genlmsg_end(msg, hdr);
5953
5954 return genlmsg_reply(msg, info);
5955
5956 nla_put_failure:
5957 err = -ENOBUFS;
5958 free_msg:
5959 nlmsg_free(msg);
5960 return err;
5961}
5962
Johannes Berg5e760232011-11-04 11:18:17 +01005963static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
5964{
5965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5966
5967 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
5968 return -EOPNOTSUPP;
5969
5970 if (rdev->ap_beacons_nlpid)
5971 return -EBUSY;
5972
5973 rdev->ap_beacons_nlpid = info->snd_pid;
5974
5975 return 0;
5976}
5977
Johannes Berg4c476992010-10-04 21:36:35 +02005978#define NL80211_FLAG_NEED_WIPHY 0x01
5979#define NL80211_FLAG_NEED_NETDEV 0x02
5980#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005981#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5982#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5983 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005984
5985static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5986 struct genl_info *info)
5987{
5988 struct cfg80211_registered_device *rdev;
5989 struct net_device *dev;
5990 int err;
5991 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5992
5993 if (rtnl)
5994 rtnl_lock();
5995
5996 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5997 rdev = cfg80211_get_dev_from_info(info);
5998 if (IS_ERR(rdev)) {
5999 if (rtnl)
6000 rtnl_unlock();
6001 return PTR_ERR(rdev);
6002 }
6003 info->user_ptr[0] = rdev;
6004 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6005 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
6006 if (err) {
6007 if (rtnl)
6008 rtnl_unlock();
6009 return err;
6010 }
Johannes Berg41265712010-10-04 21:14:05 +02006011 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6012 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006013 cfg80211_unlock_rdev(rdev);
6014 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006015 if (rtnl)
6016 rtnl_unlock();
6017 return -ENETDOWN;
6018 }
Johannes Berg4c476992010-10-04 21:36:35 +02006019 info->user_ptr[0] = rdev;
6020 info->user_ptr[1] = dev;
6021 }
6022
6023 return 0;
6024}
6025
6026static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6027 struct genl_info *info)
6028{
6029 if (info->user_ptr[0])
6030 cfg80211_unlock_rdev(info->user_ptr[0]);
6031 if (info->user_ptr[1])
6032 dev_put(info->user_ptr[1]);
6033 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6034 rtnl_unlock();
6035}
6036
Johannes Berg55682962007-09-20 13:09:35 -04006037static struct genl_ops nl80211_ops[] = {
6038 {
6039 .cmd = NL80211_CMD_GET_WIPHY,
6040 .doit = nl80211_get_wiphy,
6041 .dumpit = nl80211_dump_wiphy,
6042 .policy = nl80211_policy,
6043 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006044 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006045 },
6046 {
6047 .cmd = NL80211_CMD_SET_WIPHY,
6048 .doit = nl80211_set_wiphy,
6049 .policy = nl80211_policy,
6050 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006051 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006052 },
6053 {
6054 .cmd = NL80211_CMD_GET_INTERFACE,
6055 .doit = nl80211_get_interface,
6056 .dumpit = nl80211_dump_interface,
6057 .policy = nl80211_policy,
6058 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006059 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006060 },
6061 {
6062 .cmd = NL80211_CMD_SET_INTERFACE,
6063 .doit = nl80211_set_interface,
6064 .policy = nl80211_policy,
6065 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006066 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6067 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006068 },
6069 {
6070 .cmd = NL80211_CMD_NEW_INTERFACE,
6071 .doit = nl80211_new_interface,
6072 .policy = nl80211_policy,
6073 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006074 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6075 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006076 },
6077 {
6078 .cmd = NL80211_CMD_DEL_INTERFACE,
6079 .doit = nl80211_del_interface,
6080 .policy = nl80211_policy,
6081 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006082 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6083 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006084 },
Johannes Berg41ade002007-12-19 02:03:29 +01006085 {
6086 .cmd = NL80211_CMD_GET_KEY,
6087 .doit = nl80211_get_key,
6088 .policy = nl80211_policy,
6089 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006090 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6091 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006092 },
6093 {
6094 .cmd = NL80211_CMD_SET_KEY,
6095 .doit = nl80211_set_key,
6096 .policy = nl80211_policy,
6097 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006098 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006099 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006100 },
6101 {
6102 .cmd = NL80211_CMD_NEW_KEY,
6103 .doit = nl80211_new_key,
6104 .policy = nl80211_policy,
6105 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006106 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006107 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006108 },
6109 {
6110 .cmd = NL80211_CMD_DEL_KEY,
6111 .doit = nl80211_del_key,
6112 .policy = nl80211_policy,
6113 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006114 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006115 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006116 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006117 {
6118 .cmd = NL80211_CMD_SET_BEACON,
6119 .policy = nl80211_policy,
6120 .flags = GENL_ADMIN_PERM,
6121 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006122 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6123 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006124 },
6125 {
6126 .cmd = NL80211_CMD_NEW_BEACON,
6127 .policy = nl80211_policy,
6128 .flags = GENL_ADMIN_PERM,
6129 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006130 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6131 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006132 },
6133 {
6134 .cmd = NL80211_CMD_DEL_BEACON,
6135 .policy = nl80211_policy,
6136 .flags = GENL_ADMIN_PERM,
6137 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006138 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6139 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006140 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006141 {
6142 .cmd = NL80211_CMD_GET_STATION,
6143 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006144 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006145 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006146 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6147 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006148 },
6149 {
6150 .cmd = NL80211_CMD_SET_STATION,
6151 .doit = nl80211_set_station,
6152 .policy = nl80211_policy,
6153 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006154 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6155 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006156 },
6157 {
6158 .cmd = NL80211_CMD_NEW_STATION,
6159 .doit = nl80211_new_station,
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 Berg5727ef12007-12-19 02:03:34 +01006164 },
6165 {
6166 .cmd = NL80211_CMD_DEL_STATION,
6167 .doit = nl80211_del_station,
6168 .policy = nl80211_policy,
6169 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006170 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6171 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006172 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006173 {
6174 .cmd = NL80211_CMD_GET_MPATH,
6175 .doit = nl80211_get_mpath,
6176 .dumpit = nl80211_dump_mpath,
6177 .policy = nl80211_policy,
6178 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006179 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006180 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006181 },
6182 {
6183 .cmd = NL80211_CMD_SET_MPATH,
6184 .doit = nl80211_set_mpath,
6185 .policy = nl80211_policy,
6186 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006187 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006188 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006189 },
6190 {
6191 .cmd = NL80211_CMD_NEW_MPATH,
6192 .doit = nl80211_new_mpath,
6193 .policy = nl80211_policy,
6194 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006195 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006196 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006197 },
6198 {
6199 .cmd = NL80211_CMD_DEL_MPATH,
6200 .doit = nl80211_del_mpath,
6201 .policy = nl80211_policy,
6202 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006203 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6204 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006205 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006206 {
6207 .cmd = NL80211_CMD_SET_BSS,
6208 .doit = nl80211_set_bss,
6209 .policy = nl80211_policy,
6210 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006211 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6212 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006213 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006214 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006215 .cmd = NL80211_CMD_GET_REG,
6216 .doit = nl80211_get_reg,
6217 .policy = nl80211_policy,
6218 /* can be retrieved by unprivileged users */
6219 },
6220 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006221 .cmd = NL80211_CMD_SET_REG,
6222 .doit = nl80211_set_reg,
6223 .policy = nl80211_policy,
6224 .flags = GENL_ADMIN_PERM,
6225 },
6226 {
6227 .cmd = NL80211_CMD_REQ_SET_REG,
6228 .doit = nl80211_req_set_reg,
6229 .policy = nl80211_policy,
6230 .flags = GENL_ADMIN_PERM,
6231 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006232 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006233 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6234 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006235 .policy = nl80211_policy,
6236 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006237 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6238 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006239 },
6240 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006241 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6242 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006243 .policy = nl80211_policy,
6244 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006245 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006246 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006247 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006248 {
Johannes Berg2a519312009-02-10 21:25:55 +01006249 .cmd = NL80211_CMD_TRIGGER_SCAN,
6250 .doit = nl80211_trigger_scan,
6251 .policy = nl80211_policy,
6252 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006253 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006254 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01006255 },
6256 {
6257 .cmd = NL80211_CMD_GET_SCAN,
6258 .policy = nl80211_policy,
6259 .dumpit = nl80211_dump_scan,
6260 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006261 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006262 .cmd = NL80211_CMD_START_SCHED_SCAN,
6263 .doit = nl80211_start_sched_scan,
6264 .policy = nl80211_policy,
6265 .flags = GENL_ADMIN_PERM,
6266 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6267 NL80211_FLAG_NEED_RTNL,
6268 },
6269 {
6270 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6271 .doit = nl80211_stop_sched_scan,
6272 .policy = nl80211_policy,
6273 .flags = GENL_ADMIN_PERM,
6274 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6275 NL80211_FLAG_NEED_RTNL,
6276 },
6277 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006278 .cmd = NL80211_CMD_AUTHENTICATE,
6279 .doit = nl80211_authenticate,
6280 .policy = nl80211_policy,
6281 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006282 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006283 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006284 },
6285 {
6286 .cmd = NL80211_CMD_ASSOCIATE,
6287 .doit = nl80211_associate,
6288 .policy = nl80211_policy,
6289 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006290 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006291 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006292 },
6293 {
6294 .cmd = NL80211_CMD_DEAUTHENTICATE,
6295 .doit = nl80211_deauthenticate,
6296 .policy = nl80211_policy,
6297 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006298 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006299 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006300 },
6301 {
6302 .cmd = NL80211_CMD_DISASSOCIATE,
6303 .doit = nl80211_disassociate,
6304 .policy = nl80211_policy,
6305 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006306 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006307 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006308 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006309 {
6310 .cmd = NL80211_CMD_JOIN_IBSS,
6311 .doit = nl80211_join_ibss,
6312 .policy = nl80211_policy,
6313 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006314 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006315 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006316 },
6317 {
6318 .cmd = NL80211_CMD_LEAVE_IBSS,
6319 .doit = nl80211_leave_ibss,
6320 .policy = nl80211_policy,
6321 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006322 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006323 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006324 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006325#ifdef CONFIG_NL80211_TESTMODE
6326 {
6327 .cmd = NL80211_CMD_TESTMODE,
6328 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006329 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006330 .policy = nl80211_policy,
6331 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006332 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6333 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006334 },
6335#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006336 {
6337 .cmd = NL80211_CMD_CONNECT,
6338 .doit = nl80211_connect,
6339 .policy = nl80211_policy,
6340 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006342 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006343 },
6344 {
6345 .cmd = NL80211_CMD_DISCONNECT,
6346 .doit = nl80211_disconnect,
6347 .policy = nl80211_policy,
6348 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006349 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006350 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006351 },
Johannes Berg463d0182009-07-14 00:33:35 +02006352 {
6353 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6354 .doit = nl80211_wiphy_netns,
6355 .policy = nl80211_policy,
6356 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006357 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6358 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006359 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006360 {
6361 .cmd = NL80211_CMD_GET_SURVEY,
6362 .policy = nl80211_policy,
6363 .dumpit = nl80211_dump_survey,
6364 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006365 {
6366 .cmd = NL80211_CMD_SET_PMKSA,
6367 .doit = nl80211_setdel_pmksa,
6368 .policy = nl80211_policy,
6369 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006370 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6371 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006372 },
6373 {
6374 .cmd = NL80211_CMD_DEL_PMKSA,
6375 .doit = nl80211_setdel_pmksa,
6376 .policy = nl80211_policy,
6377 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006378 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6379 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006380 },
6381 {
6382 .cmd = NL80211_CMD_FLUSH_PMKSA,
6383 .doit = nl80211_flush_pmksa,
6384 .policy = nl80211_policy,
6385 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006386 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6387 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006388 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006389 {
6390 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6391 .doit = nl80211_remain_on_channel,
6392 .policy = nl80211_policy,
6393 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006395 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006396 },
6397 {
6398 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6399 .doit = nl80211_cancel_remain_on_channel,
6400 .policy = nl80211_policy,
6401 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006403 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006404 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006405 {
6406 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6407 .doit = nl80211_set_tx_bitrate_mask,
6408 .policy = nl80211_policy,
6409 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006410 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6411 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006412 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006413 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006414 .cmd = NL80211_CMD_REGISTER_FRAME,
6415 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006416 .policy = nl80211_policy,
6417 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006418 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6419 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006420 },
6421 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006422 .cmd = NL80211_CMD_FRAME,
6423 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006424 .policy = nl80211_policy,
6425 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006426 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006427 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006428 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006429 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006430 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6431 .doit = nl80211_tx_mgmt_cancel_wait,
6432 .policy = nl80211_policy,
6433 .flags = GENL_ADMIN_PERM,
6434 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6435 NL80211_FLAG_NEED_RTNL,
6436 },
6437 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006438 .cmd = NL80211_CMD_SET_POWER_SAVE,
6439 .doit = nl80211_set_power_save,
6440 .policy = nl80211_policy,
6441 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006442 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6443 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006444 },
6445 {
6446 .cmd = NL80211_CMD_GET_POWER_SAVE,
6447 .doit = nl80211_get_power_save,
6448 .policy = nl80211_policy,
6449 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006450 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6451 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006452 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006453 {
6454 .cmd = NL80211_CMD_SET_CQM,
6455 .doit = nl80211_set_cqm,
6456 .policy = nl80211_policy,
6457 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006458 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6459 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006460 },
Johannes Bergf444de02010-05-05 15:25:02 +02006461 {
6462 .cmd = NL80211_CMD_SET_CHANNEL,
6463 .doit = nl80211_set_channel,
6464 .policy = nl80211_policy,
6465 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006466 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6467 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006468 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006469 {
6470 .cmd = NL80211_CMD_SET_WDS_PEER,
6471 .doit = nl80211_set_wds_peer,
6472 .policy = nl80211_policy,
6473 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006474 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6475 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006476 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006477 {
6478 .cmd = NL80211_CMD_JOIN_MESH,
6479 .doit = nl80211_join_mesh,
6480 .policy = nl80211_policy,
6481 .flags = GENL_ADMIN_PERM,
6482 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6483 NL80211_FLAG_NEED_RTNL,
6484 },
6485 {
6486 .cmd = NL80211_CMD_LEAVE_MESH,
6487 .doit = nl80211_leave_mesh,
6488 .policy = nl80211_policy,
6489 .flags = GENL_ADMIN_PERM,
6490 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6491 NL80211_FLAG_NEED_RTNL,
6492 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006493 {
6494 .cmd = NL80211_CMD_GET_WOWLAN,
6495 .doit = nl80211_get_wowlan,
6496 .policy = nl80211_policy,
6497 /* can be retrieved by unprivileged users */
6498 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6499 NL80211_FLAG_NEED_RTNL,
6500 },
6501 {
6502 .cmd = NL80211_CMD_SET_WOWLAN,
6503 .doit = nl80211_set_wowlan,
6504 .policy = nl80211_policy,
6505 .flags = GENL_ADMIN_PERM,
6506 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6507 NL80211_FLAG_NEED_RTNL,
6508 },
Johannes Berge5497d72011-07-05 16:35:40 +02006509 {
6510 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6511 .doit = nl80211_set_rekey_data,
6512 .policy = nl80211_policy,
6513 .flags = GENL_ADMIN_PERM,
6514 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6515 NL80211_FLAG_NEED_RTNL,
6516 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006517 {
6518 .cmd = NL80211_CMD_TDLS_MGMT,
6519 .doit = nl80211_tdls_mgmt,
6520 .policy = nl80211_policy,
6521 .flags = GENL_ADMIN_PERM,
6522 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6523 NL80211_FLAG_NEED_RTNL,
6524 },
6525 {
6526 .cmd = NL80211_CMD_TDLS_OPER,
6527 .doit = nl80211_tdls_oper,
6528 .policy = nl80211_policy,
6529 .flags = GENL_ADMIN_PERM,
6530 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6531 NL80211_FLAG_NEED_RTNL,
6532 },
Johannes Berg28946da2011-11-04 11:18:12 +01006533 {
6534 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6535 .doit = nl80211_register_unexpected_frame,
6536 .policy = nl80211_policy,
6537 .flags = GENL_ADMIN_PERM,
6538 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6539 NL80211_FLAG_NEED_RTNL,
6540 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006541 {
6542 .cmd = NL80211_CMD_PROBE_CLIENT,
6543 .doit = nl80211_probe_client,
6544 .policy = nl80211_policy,
6545 .flags = GENL_ADMIN_PERM,
6546 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6547 NL80211_FLAG_NEED_RTNL,
6548 },
Johannes Berg5e760232011-11-04 11:18:17 +01006549 {
6550 .cmd = NL80211_CMD_REGISTER_BEACONS,
6551 .doit = nl80211_register_beacons,
6552 .policy = nl80211_policy,
6553 .flags = GENL_ADMIN_PERM,
6554 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6555 NL80211_FLAG_NEED_RTNL,
6556 },
Johannes Berg55682962007-09-20 13:09:35 -04006557};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006558
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006559static struct genl_multicast_group nl80211_mlme_mcgrp = {
6560 .name = "mlme",
6561};
Johannes Berg55682962007-09-20 13:09:35 -04006562
6563/* multicast groups */
6564static struct genl_multicast_group nl80211_config_mcgrp = {
6565 .name = "config",
6566};
Johannes Berg2a519312009-02-10 21:25:55 +01006567static struct genl_multicast_group nl80211_scan_mcgrp = {
6568 .name = "scan",
6569};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006570static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6571 .name = "regulatory",
6572};
Johannes Berg55682962007-09-20 13:09:35 -04006573
6574/* notification functions */
6575
6576void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6577{
6578 struct sk_buff *msg;
6579
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006580 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006581 if (!msg)
6582 return;
6583
6584 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6585 nlmsg_free(msg);
6586 return;
6587 }
6588
Johannes Berg463d0182009-07-14 00:33:35 +02006589 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6590 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006591}
6592
Johannes Berg362a4152009-05-24 16:43:15 +02006593static int nl80211_add_scan_req(struct sk_buff *msg,
6594 struct cfg80211_registered_device *rdev)
6595{
6596 struct cfg80211_scan_request *req = rdev->scan_req;
6597 struct nlattr *nest;
6598 int i;
6599
Johannes Berg667503dd2009-07-07 03:56:11 +02006600 ASSERT_RDEV_LOCK(rdev);
6601
Johannes Berg362a4152009-05-24 16:43:15 +02006602 if (WARN_ON(!req))
6603 return 0;
6604
6605 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6606 if (!nest)
6607 goto nla_put_failure;
6608 for (i = 0; i < req->n_ssids; i++)
6609 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6610 nla_nest_end(msg, nest);
6611
6612 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6613 if (!nest)
6614 goto nla_put_failure;
6615 for (i = 0; i < req->n_channels; i++)
6616 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6617 nla_nest_end(msg, nest);
6618
6619 if (req->ie)
6620 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6621
6622 return 0;
6623 nla_put_failure:
6624 return -ENOBUFS;
6625}
6626
Johannes Berga538e2d2009-06-16 19:56:42 +02006627static int nl80211_send_scan_msg(struct sk_buff *msg,
6628 struct cfg80211_registered_device *rdev,
6629 struct net_device *netdev,
6630 u32 pid, u32 seq, int flags,
6631 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006632{
6633 void *hdr;
6634
6635 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6636 if (!hdr)
6637 return -1;
6638
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006639 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006640 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6641
Johannes Berg362a4152009-05-24 16:43:15 +02006642 /* ignore errors and send incomplete event anyway */
6643 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006644
6645 return genlmsg_end(msg, hdr);
6646
6647 nla_put_failure:
6648 genlmsg_cancel(msg, hdr);
6649 return -EMSGSIZE;
6650}
6651
Luciano Coelho807f8a82011-05-11 17:09:35 +03006652static int
6653nl80211_send_sched_scan_msg(struct sk_buff *msg,
6654 struct cfg80211_registered_device *rdev,
6655 struct net_device *netdev,
6656 u32 pid, u32 seq, int flags, u32 cmd)
6657{
6658 void *hdr;
6659
6660 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6661 if (!hdr)
6662 return -1;
6663
6664 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6665 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6666
6667 return genlmsg_end(msg, hdr);
6668
6669 nla_put_failure:
6670 genlmsg_cancel(msg, hdr);
6671 return -EMSGSIZE;
6672}
6673
Johannes Berga538e2d2009-06-16 19:56:42 +02006674void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6675 struct net_device *netdev)
6676{
6677 struct sk_buff *msg;
6678
6679 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6680 if (!msg)
6681 return;
6682
6683 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6684 NL80211_CMD_TRIGGER_SCAN) < 0) {
6685 nlmsg_free(msg);
6686 return;
6687 }
6688
Johannes Berg463d0182009-07-14 00:33:35 +02006689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6690 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006691}
6692
Johannes Berg2a519312009-02-10 21:25:55 +01006693void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6694 struct net_device *netdev)
6695{
6696 struct sk_buff *msg;
6697
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006699 if (!msg)
6700 return;
6701
Johannes Berga538e2d2009-06-16 19:56:42 +02006702 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6703 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006704 nlmsg_free(msg);
6705 return;
6706 }
6707
Johannes Berg463d0182009-07-14 00:33:35 +02006708 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6709 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006710}
6711
6712void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6713 struct net_device *netdev)
6714{
6715 struct sk_buff *msg;
6716
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006718 if (!msg)
6719 return;
6720
Johannes Berga538e2d2009-06-16 19:56:42 +02006721 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6722 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006723 nlmsg_free(msg);
6724 return;
6725 }
6726
Johannes Berg463d0182009-07-14 00:33:35 +02006727 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6728 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006729}
6730
Luciano Coelho807f8a82011-05-11 17:09:35 +03006731void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6732 struct net_device *netdev)
6733{
6734 struct sk_buff *msg;
6735
6736 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6737 if (!msg)
6738 return;
6739
6740 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6741 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6742 nlmsg_free(msg);
6743 return;
6744 }
6745
6746 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6747 nl80211_scan_mcgrp.id, GFP_KERNEL);
6748}
6749
6750void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6751 struct net_device *netdev, u32 cmd)
6752{
6753 struct sk_buff *msg;
6754
6755 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6756 if (!msg)
6757 return;
6758
6759 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6760 nlmsg_free(msg);
6761 return;
6762 }
6763
6764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6765 nl80211_scan_mcgrp.id, GFP_KERNEL);
6766}
6767
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006768/*
6769 * This can happen on global regulatory changes or device specific settings
6770 * based on custom world regulatory domains.
6771 */
6772void nl80211_send_reg_change_event(struct regulatory_request *request)
6773{
6774 struct sk_buff *msg;
6775 void *hdr;
6776
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006777 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006778 if (!msg)
6779 return;
6780
6781 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6782 if (!hdr) {
6783 nlmsg_free(msg);
6784 return;
6785 }
6786
6787 /* Userspace can always count this one always being set */
6788 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6789
6790 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6791 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6792 NL80211_REGDOM_TYPE_WORLD);
6793 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6794 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6795 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6796 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6797 request->intersect)
6798 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6799 NL80211_REGDOM_TYPE_INTERSECTION);
6800 else {
6801 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6802 NL80211_REGDOM_TYPE_COUNTRY);
6803 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6804 }
6805
6806 if (wiphy_idx_valid(request->wiphy_idx))
6807 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6808
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006809 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006810
Johannes Bergbc43b282009-07-25 10:54:13 +02006811 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006812 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006813 GFP_ATOMIC);
6814 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006815
6816 return;
6817
6818nla_put_failure:
6819 genlmsg_cancel(msg, hdr);
6820 nlmsg_free(msg);
6821}
6822
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006823static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6824 struct net_device *netdev,
6825 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006826 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006827{
6828 struct sk_buff *msg;
6829 void *hdr;
6830
Johannes Berge6d6e342009-07-01 21:26:47 +02006831 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006832 if (!msg)
6833 return;
6834
6835 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6836 if (!hdr) {
6837 nlmsg_free(msg);
6838 return;
6839 }
6840
6841 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6842 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6843 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6844
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006845 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006846
Johannes Berg463d0182009-07-14 00:33:35 +02006847 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6848 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006849 return;
6850
6851 nla_put_failure:
6852 genlmsg_cancel(msg, hdr);
6853 nlmsg_free(msg);
6854}
6855
6856void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006857 struct net_device *netdev, const u8 *buf,
6858 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006859{
6860 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006861 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006862}
6863
6864void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6865 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006866 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006867{
Johannes Berge6d6e342009-07-01 21:26:47 +02006868 nl80211_send_mlme_event(rdev, netdev, buf, len,
6869 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006870}
6871
Jouni Malinen53b46b82009-03-27 20:53:56 +02006872void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006873 struct net_device *netdev, const u8 *buf,
6874 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006875{
6876 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006877 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006878}
6879
Jouni Malinen53b46b82009-03-27 20:53:56 +02006880void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6881 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006882 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006883{
6884 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006885 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006886}
6887
Jouni Malinencf4e5942010-12-16 00:52:40 +02006888void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6889 struct net_device *netdev, const u8 *buf,
6890 size_t len, gfp_t gfp)
6891{
6892 nl80211_send_mlme_event(rdev, netdev, buf, len,
6893 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6894}
6895
6896void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6897 struct net_device *netdev, const u8 *buf,
6898 size_t len, gfp_t gfp)
6899{
6900 nl80211_send_mlme_event(rdev, netdev, buf, len,
6901 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6902}
6903
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006904static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6905 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006906 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006907{
6908 struct sk_buff *msg;
6909 void *hdr;
6910
Johannes Berge6d6e342009-07-01 21:26:47 +02006911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006912 if (!msg)
6913 return;
6914
6915 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6916 if (!hdr) {
6917 nlmsg_free(msg);
6918 return;
6919 }
6920
6921 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6922 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6923 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6924 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6925
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006926 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03006927
Johannes Berg463d0182009-07-14 00:33:35 +02006928 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6929 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006930 return;
6931
6932 nla_put_failure:
6933 genlmsg_cancel(msg, hdr);
6934 nlmsg_free(msg);
6935}
6936
6937void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006938 struct net_device *netdev, const u8 *addr,
6939 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006940{
6941 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006942 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006943}
6944
6945void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006946 struct net_device *netdev, const u8 *addr,
6947 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006948{
Johannes Berge6d6e342009-07-01 21:26:47 +02006949 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6950 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006951}
6952
Samuel Ortizb23aa672009-07-01 21:26:54 +02006953void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6954 struct net_device *netdev, const u8 *bssid,
6955 const u8 *req_ie, size_t req_ie_len,
6956 const u8 *resp_ie, size_t resp_ie_len,
6957 u16 status, gfp_t gfp)
6958{
6959 struct sk_buff *msg;
6960 void *hdr;
6961
6962 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6963 if (!msg)
6964 return;
6965
6966 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6967 if (!hdr) {
6968 nlmsg_free(msg);
6969 return;
6970 }
6971
6972 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6973 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6974 if (bssid)
6975 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6976 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6977 if (req_ie)
6978 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6979 if (resp_ie)
6980 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6981
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006982 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006983
Johannes Berg463d0182009-07-14 00:33:35 +02006984 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6985 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006986 return;
6987
6988 nla_put_failure:
6989 genlmsg_cancel(msg, hdr);
6990 nlmsg_free(msg);
6991
6992}
6993
6994void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6995 struct net_device *netdev, const u8 *bssid,
6996 const u8 *req_ie, size_t req_ie_len,
6997 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6998{
6999 struct sk_buff *msg;
7000 void *hdr;
7001
7002 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7003 if (!msg)
7004 return;
7005
7006 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7007 if (!hdr) {
7008 nlmsg_free(msg);
7009 return;
7010 }
7011
7012 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7013 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7014 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7015 if (req_ie)
7016 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7017 if (resp_ie)
7018 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7019
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007020 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007021
Johannes Berg463d0182009-07-14 00:33:35 +02007022 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7023 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007024 return;
7025
7026 nla_put_failure:
7027 genlmsg_cancel(msg, hdr);
7028 nlmsg_free(msg);
7029
7030}
7031
7032void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7033 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007034 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007035{
7036 struct sk_buff *msg;
7037 void *hdr;
7038
Johannes Berg667503dd2009-07-07 03:56:11 +02007039 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007040 if (!msg)
7041 return;
7042
7043 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7044 if (!hdr) {
7045 nlmsg_free(msg);
7046 return;
7047 }
7048
7049 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7051 if (from_ap && reason)
7052 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7053 if (from_ap)
7054 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7055 if (ie)
7056 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7057
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007058 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007059
Johannes Berg463d0182009-07-14 00:33:35 +02007060 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7061 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007062 return;
7063
7064 nla_put_failure:
7065 genlmsg_cancel(msg, hdr);
7066 nlmsg_free(msg);
7067
7068}
7069
Johannes Berg04a773a2009-04-19 21:24:32 +02007070void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7071 struct net_device *netdev, const u8 *bssid,
7072 gfp_t gfp)
7073{
7074 struct sk_buff *msg;
7075 void *hdr;
7076
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007077 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007078 if (!msg)
7079 return;
7080
7081 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7082 if (!hdr) {
7083 nlmsg_free(msg);
7084 return;
7085 }
7086
7087 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7088 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7089 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7090
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007091 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007092
Johannes Berg463d0182009-07-14 00:33:35 +02007093 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7094 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007095 return;
7096
7097 nla_put_failure:
7098 genlmsg_cancel(msg, hdr);
7099 nlmsg_free(msg);
7100}
7101
Javier Cardonac93b5e72011-04-07 15:08:34 -07007102void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7103 struct net_device *netdev,
7104 const u8 *macaddr, const u8* ie, u8 ie_len,
7105 gfp_t gfp)
7106{
7107 struct sk_buff *msg;
7108 void *hdr;
7109
7110 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7111 if (!msg)
7112 return;
7113
7114 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7115 if (!hdr) {
7116 nlmsg_free(msg);
7117 return;
7118 }
7119
7120 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7122 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7123 if (ie_len && ie)
7124 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7125
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007126 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007127
7128 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7129 nl80211_mlme_mcgrp.id, gfp);
7130 return;
7131
7132 nla_put_failure:
7133 genlmsg_cancel(msg, hdr);
7134 nlmsg_free(msg);
7135}
7136
Jouni Malinena3b8b052009-03-27 21:59:49 +02007137void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7138 struct net_device *netdev, const u8 *addr,
7139 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007140 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007141{
7142 struct sk_buff *msg;
7143 void *hdr;
7144
Johannes Berge6d6e342009-07-01 21:26:47 +02007145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007146 if (!msg)
7147 return;
7148
7149 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7150 if (!hdr) {
7151 nlmsg_free(msg);
7152 return;
7153 }
7154
7155 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7156 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7157 if (addr)
7158 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7159 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007160 if (key_id != -1)
7161 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007162 if (tsc)
7163 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7164
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007165 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007166
Johannes Berg463d0182009-07-14 00:33:35 +02007167 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7168 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007169 return;
7170
7171 nla_put_failure:
7172 genlmsg_cancel(msg, hdr);
7173 nlmsg_free(msg);
7174}
7175
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007176void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7177 struct ieee80211_channel *channel_before,
7178 struct ieee80211_channel *channel_after)
7179{
7180 struct sk_buff *msg;
7181 void *hdr;
7182 struct nlattr *nl_freq;
7183
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007184 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007185 if (!msg)
7186 return;
7187
7188 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7189 if (!hdr) {
7190 nlmsg_free(msg);
7191 return;
7192 }
7193
7194 /*
7195 * Since we are applying the beacon hint to a wiphy we know its
7196 * wiphy_idx is valid
7197 */
7198 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7199
7200 /* Before */
7201 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7202 if (!nl_freq)
7203 goto nla_put_failure;
7204 if (nl80211_msg_put_channel(msg, channel_before))
7205 goto nla_put_failure;
7206 nla_nest_end(msg, nl_freq);
7207
7208 /* After */
7209 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7210 if (!nl_freq)
7211 goto nla_put_failure;
7212 if (nl80211_msg_put_channel(msg, channel_after))
7213 goto nla_put_failure;
7214 nla_nest_end(msg, nl_freq);
7215
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007216 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007217
Johannes Berg463d0182009-07-14 00:33:35 +02007218 rcu_read_lock();
7219 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7220 GFP_ATOMIC);
7221 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007222
7223 return;
7224
7225nla_put_failure:
7226 genlmsg_cancel(msg, hdr);
7227 nlmsg_free(msg);
7228}
7229
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007230static void nl80211_send_remain_on_chan_event(
7231 int cmd, struct cfg80211_registered_device *rdev,
7232 struct net_device *netdev, u64 cookie,
7233 struct ieee80211_channel *chan,
7234 enum nl80211_channel_type channel_type,
7235 unsigned int duration, gfp_t gfp)
7236{
7237 struct sk_buff *msg;
7238 void *hdr;
7239
7240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7241 if (!msg)
7242 return;
7243
7244 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7245 if (!hdr) {
7246 nlmsg_free(msg);
7247 return;
7248 }
7249
7250 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7251 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7252 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7253 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7254 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7255
7256 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7257 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7258
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007259 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007260
7261 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7262 nl80211_mlme_mcgrp.id, gfp);
7263 return;
7264
7265 nla_put_failure:
7266 genlmsg_cancel(msg, hdr);
7267 nlmsg_free(msg);
7268}
7269
7270void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7271 struct net_device *netdev, u64 cookie,
7272 struct ieee80211_channel *chan,
7273 enum nl80211_channel_type channel_type,
7274 unsigned int duration, gfp_t gfp)
7275{
7276 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7277 rdev, netdev, cookie, chan,
7278 channel_type, duration, gfp);
7279}
7280
7281void nl80211_send_remain_on_channel_cancel(
7282 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7283 u64 cookie, struct ieee80211_channel *chan,
7284 enum nl80211_channel_type channel_type, gfp_t gfp)
7285{
7286 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7287 rdev, netdev, cookie, chan,
7288 channel_type, 0, gfp);
7289}
7290
Johannes Berg98b62182009-12-23 13:15:44 +01007291void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7292 struct net_device *dev, const u8 *mac_addr,
7293 struct station_info *sinfo, gfp_t gfp)
7294{
7295 struct sk_buff *msg;
7296
7297 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7298 if (!msg)
7299 return;
7300
7301 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7302 nlmsg_free(msg);
7303 return;
7304 }
7305
7306 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7307 nl80211_mlme_mcgrp.id, gfp);
7308}
7309
Jouni Malinenec15e682011-03-23 15:29:52 +02007310void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7311 struct net_device *dev, const u8 *mac_addr,
7312 gfp_t gfp)
7313{
7314 struct sk_buff *msg;
7315 void *hdr;
7316
7317 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7318 if (!msg)
7319 return;
7320
7321 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7322 if (!hdr) {
7323 nlmsg_free(msg);
7324 return;
7325 }
7326
7327 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7328 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7329
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007330 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007331
7332 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7333 nl80211_mlme_mcgrp.id, gfp);
7334 return;
7335
7336 nla_put_failure:
7337 genlmsg_cancel(msg, hdr);
7338 nlmsg_free(msg);
7339}
7340
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007341static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7342 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007343{
7344 struct wireless_dev *wdev = dev->ieee80211_ptr;
7345 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7346 struct sk_buff *msg;
7347 void *hdr;
7348 int err;
7349 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7350
7351 if (!nlpid)
7352 return false;
7353
7354 msg = nlmsg_new(100, gfp);
7355 if (!msg)
7356 return true;
7357
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007358 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007359 if (!hdr) {
7360 nlmsg_free(msg);
7361 return true;
7362 }
7363
7364 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7365 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7366 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7367
7368 err = genlmsg_end(msg, hdr);
7369 if (err < 0) {
7370 nlmsg_free(msg);
7371 return true;
7372 }
7373
7374 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7375 return true;
7376
7377 nla_put_failure:
7378 genlmsg_cancel(msg, hdr);
7379 nlmsg_free(msg);
7380 return true;
7381}
7382
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007383bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7384{
7385 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7386 addr, gfp);
7387}
7388
7389bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7390 const u8 *addr, gfp_t gfp)
7391{
7392 return __nl80211_unexpected_frame(dev,
7393 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7394 addr, gfp);
7395}
7396
Johannes Berg2e161f72010-08-12 15:38:38 +02007397int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7398 struct net_device *netdev, u32 nlpid,
7399 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007400{
7401 struct sk_buff *msg;
7402 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007403
7404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7405 if (!msg)
7406 return -ENOMEM;
7407
Johannes Berg2e161f72010-08-12 15:38:38 +02007408 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007409 if (!hdr) {
7410 nlmsg_free(msg);
7411 return -ENOMEM;
7412 }
7413
7414 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7415 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7416 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7417 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7418
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007419 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007420
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007421 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007422
7423 nla_put_failure:
7424 genlmsg_cancel(msg, hdr);
7425 nlmsg_free(msg);
7426 return -ENOBUFS;
7427}
7428
Johannes Berg2e161f72010-08-12 15:38:38 +02007429void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7430 struct net_device *netdev, u64 cookie,
7431 const u8 *buf, size_t len, bool ack,
7432 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007433{
7434 struct sk_buff *msg;
7435 void *hdr;
7436
7437 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7438 if (!msg)
7439 return;
7440
Johannes Berg2e161f72010-08-12 15:38:38 +02007441 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007442 if (!hdr) {
7443 nlmsg_free(msg);
7444 return;
7445 }
7446
7447 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7448 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7449 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7450 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7451 if (ack)
7452 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7453
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007454 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007455
7456 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7457 return;
7458
7459 nla_put_failure:
7460 genlmsg_cancel(msg, hdr);
7461 nlmsg_free(msg);
7462}
7463
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007464void
7465nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7466 struct net_device *netdev,
7467 enum nl80211_cqm_rssi_threshold_event rssi_event,
7468 gfp_t gfp)
7469{
7470 struct sk_buff *msg;
7471 struct nlattr *pinfoattr;
7472 void *hdr;
7473
7474 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7475 if (!msg)
7476 return;
7477
7478 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7479 if (!hdr) {
7480 nlmsg_free(msg);
7481 return;
7482 }
7483
7484 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7486
7487 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7488 if (!pinfoattr)
7489 goto nla_put_failure;
7490
7491 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7492 rssi_event);
7493
7494 nla_nest_end(msg, pinfoattr);
7495
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007496 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007497
7498 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7499 nl80211_mlme_mcgrp.id, gfp);
7500 return;
7501
7502 nla_put_failure:
7503 genlmsg_cancel(msg, hdr);
7504 nlmsg_free(msg);
7505}
7506
Johannes Berge5497d72011-07-05 16:35:40 +02007507void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7508 struct net_device *netdev, const u8 *bssid,
7509 const u8 *replay_ctr, gfp_t gfp)
7510{
7511 struct sk_buff *msg;
7512 struct nlattr *rekey_attr;
7513 void *hdr;
7514
7515 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7516 if (!msg)
7517 return;
7518
7519 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7520 if (!hdr) {
7521 nlmsg_free(msg);
7522 return;
7523 }
7524
7525 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7526 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7527 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7528
7529 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7530 if (!rekey_attr)
7531 goto nla_put_failure;
7532
7533 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7534 NL80211_REPLAY_CTR_LEN, replay_ctr);
7535
7536 nla_nest_end(msg, rekey_attr);
7537
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007538 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007539
7540 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7541 nl80211_mlme_mcgrp.id, gfp);
7542 return;
7543
7544 nla_put_failure:
7545 genlmsg_cancel(msg, hdr);
7546 nlmsg_free(msg);
7547}
7548
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007549void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7550 struct net_device *netdev, int index,
7551 const u8 *bssid, bool preauth, gfp_t gfp)
7552{
7553 struct sk_buff *msg;
7554 struct nlattr *attr;
7555 void *hdr;
7556
7557 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7558 if (!msg)
7559 return;
7560
7561 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7562 if (!hdr) {
7563 nlmsg_free(msg);
7564 return;
7565 }
7566
7567 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7568 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7569
7570 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7571 if (!attr)
7572 goto nla_put_failure;
7573
7574 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7575 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7576 if (preauth)
7577 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7578
7579 nla_nest_end(msg, attr);
7580
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007581 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007582
7583 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7584 nl80211_mlme_mcgrp.id, gfp);
7585 return;
7586
7587 nla_put_failure:
7588 genlmsg_cancel(msg, hdr);
7589 nlmsg_free(msg);
7590}
7591
Johannes Bergc063dbf2010-11-24 08:10:05 +01007592void
7593nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7594 struct net_device *netdev, const u8 *peer,
7595 u32 num_packets, gfp_t gfp)
7596{
7597 struct sk_buff *msg;
7598 struct nlattr *pinfoattr;
7599 void *hdr;
7600
7601 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7602 if (!msg)
7603 return;
7604
7605 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7606 if (!hdr) {
7607 nlmsg_free(msg);
7608 return;
7609 }
7610
7611 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7612 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7613 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7614
7615 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7616 if (!pinfoattr)
7617 goto nla_put_failure;
7618
7619 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7620
7621 nla_nest_end(msg, pinfoattr);
7622
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007623 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007624
7625 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7626 nl80211_mlme_mcgrp.id, gfp);
7627 return;
7628
7629 nla_put_failure:
7630 genlmsg_cancel(msg, hdr);
7631 nlmsg_free(msg);
7632}
7633
Johannes Berg7f6cf312011-11-04 11:18:15 +01007634void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7635 u64 cookie, bool acked, gfp_t gfp)
7636{
7637 struct wireless_dev *wdev = dev->ieee80211_ptr;
7638 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7639 struct sk_buff *msg;
7640 void *hdr;
7641 int err;
7642
7643 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7644 if (!msg)
7645 return;
7646
7647 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7648 if (!hdr) {
7649 nlmsg_free(msg);
7650 return;
7651 }
7652
7653 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7654 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7655 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7656 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7657 if (acked)
7658 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7659
7660 err = genlmsg_end(msg, hdr);
7661 if (err < 0) {
7662 nlmsg_free(msg);
7663 return;
7664 }
7665
7666 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7667 nl80211_mlme_mcgrp.id, gfp);
7668 return;
7669
7670 nla_put_failure:
7671 genlmsg_cancel(msg, hdr);
7672 nlmsg_free(msg);
7673}
7674EXPORT_SYMBOL(cfg80211_probe_status);
7675
Johannes Berg5e760232011-11-04 11:18:17 +01007676void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7677 const u8 *frame, size_t len,
7678 int freq, gfp_t gfp)
7679{
7680 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7681 struct sk_buff *msg;
7682 void *hdr;
7683 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7684
7685 if (!nlpid)
7686 return;
7687
7688 msg = nlmsg_new(len + 100, gfp);
7689 if (!msg)
7690 return;
7691
7692 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7693 if (!hdr) {
7694 nlmsg_free(msg);
7695 return;
7696 }
7697
7698 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7699 if (freq)
7700 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7701 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7702
7703 genlmsg_end(msg, hdr);
7704
7705 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7706 return;
7707
7708 nla_put_failure:
7709 genlmsg_cancel(msg, hdr);
7710 nlmsg_free(msg);
7711}
7712EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7713
Jouni Malinen026331c2010-02-15 12:53:10 +02007714static int nl80211_netlink_notify(struct notifier_block * nb,
7715 unsigned long state,
7716 void *_notify)
7717{
7718 struct netlink_notify *notify = _notify;
7719 struct cfg80211_registered_device *rdev;
7720 struct wireless_dev *wdev;
7721
7722 if (state != NETLINK_URELEASE)
7723 return NOTIFY_DONE;
7724
7725 rcu_read_lock();
7726
Johannes Berg5e760232011-11-04 11:18:17 +01007727 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007728 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007729 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01007730 if (rdev->ap_beacons_nlpid == notify->pid)
7731 rdev->ap_beacons_nlpid = 0;
7732 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007733
7734 rcu_read_unlock();
7735
7736 return NOTIFY_DONE;
7737}
7738
7739static struct notifier_block nl80211_netlink_notifier = {
7740 .notifier_call = nl80211_netlink_notify,
7741};
7742
Johannes Berg55682962007-09-20 13:09:35 -04007743/* initialisation/exit functions */
7744
7745int nl80211_init(void)
7746{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007747 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007748
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007749 err = genl_register_family_with_ops(&nl80211_fam,
7750 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007751 if (err)
7752 return err;
7753
Johannes Berg55682962007-09-20 13:09:35 -04007754 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7755 if (err)
7756 goto err_out;
7757
Johannes Berg2a519312009-02-10 21:25:55 +01007758 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7759 if (err)
7760 goto err_out;
7761
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007762 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7763 if (err)
7764 goto err_out;
7765
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007766 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7767 if (err)
7768 goto err_out;
7769
Johannes Bergaff89a92009-07-01 21:26:51 +02007770#ifdef CONFIG_NL80211_TESTMODE
7771 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7772 if (err)
7773 goto err_out;
7774#endif
7775
Jouni Malinen026331c2010-02-15 12:53:10 +02007776 err = netlink_register_notifier(&nl80211_netlink_notifier);
7777 if (err)
7778 goto err_out;
7779
Johannes Berg55682962007-09-20 13:09:35 -04007780 return 0;
7781 err_out:
7782 genl_unregister_family(&nl80211_fam);
7783 return err;
7784}
7785
7786void nl80211_exit(void)
7787{
Jouni Malinen026331c2010-02-15 12:53:10 +02007788 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007789 genl_unregister_family(&nl80211_fam);
7790}