blob: 6026c29c338df741e366aebee2a5e112d17c4920 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020050static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020051 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040052 struct net_device **dev)
53{
Johannes Berg463d0182009-07-14 00:33:35 +020054 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040055 int ifindex;
56
Johannes Bergbba95fe2008-07-29 13:22:51 +020057 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040058 return -EINVAL;
59
Johannes Bergbba95fe2008-07-29 13:22:51 +020060 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020061 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040062 if (!*dev)
63 return -ENODEV;
64
Johannes Berg463d0182009-07-14 00:33:35 +020065 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020066 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040067 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020068 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040069 }
70
71 return 0;
72}
73
74/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000075static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040076 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
77 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070078 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020079 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020080 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053081 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020082 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
84 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010086 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040087
88 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
90 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010091
92 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020093 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010094
Johannes Bergb9454e82009-07-08 13:29:08 +020095 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010096 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
97 .len = WLAN_MAX_KEY_LEN },
98 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
99 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
100 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200102 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100103
104 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
105 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
106 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
107 .len = IEEE80211_MAX_DATA_LEN },
108 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
109 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
111 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
112 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
113 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100115 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100116 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200117 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100118 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
119 .len = IEEE80211_MAX_MESH_ID_LEN },
120 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300121
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700122 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
123 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
124
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300125 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200128 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
129 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100130 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300131
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800132 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700133 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700134
Johannes Berg6c739412011-11-03 09:27:01 +0100135 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200136
137 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
138 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
139 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100140 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
141 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200142
143 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
144 .len = IEEE80211_MAX_SSID_LEN },
145 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
146 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200147 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300148 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300149 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300150 [NL80211_ATTR_STA_FLAGS2] = {
151 .len = sizeof(struct nl80211_sta_flag_update),
152 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300153 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300154 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
155 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200156 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
157 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
158 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200159 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100160 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100161 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
162 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100163 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
164 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200165 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200166 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
167 .len = IEEE80211_MAX_DATA_LEN },
168 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200169 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200170 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300171 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200172 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300173 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
174 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200175 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900176 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
177 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100178 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100179 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100180 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200181 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700182 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300183 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200184 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200185 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300186 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300187 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
188 .len = IEEE80211_MAX_DATA_LEN },
189 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
190 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530191 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300192 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530193 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300194 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
197 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
198 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100199 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
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 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800203 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
204 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
205 .len = NL80211_HT_CAPABILITY_LEN
206 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100207 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400208};
209
Johannes Berge31b8212010-10-05 19:39:30 +0200210/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000211static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200212 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200213 [NL80211_KEY_IDX] = { .type = NLA_U8 },
214 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200215 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200216 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
217 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200218 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100219 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
220};
221
222/* policy for the key default flags */
223static const struct nla_policy
224nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
225 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
226 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200227};
228
Johannes Bergff1b6e62011-05-04 15:37:28 +0200229/* policy for WoWLAN attributes */
230static const struct nla_policy
231nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
232 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
233 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
235 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200236 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
237 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
239 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200240};
241
Johannes Berge5497d72011-07-05 16:35:40 +0200242/* policy for GTK rekey offload attributes */
243static const struct nla_policy
244nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
245 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
246 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
247 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
248};
249
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300250static const struct nla_policy
251nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
252 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_SSID_LEN },
254};
255
Holger Schuriga0438972009-11-11 11:30:02 +0100256/* ifidx get helper */
257static int nl80211_get_ifidx(struct netlink_callback *cb)
258{
259 int res;
260
261 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
262 nl80211_fam.attrbuf, nl80211_fam.maxattr,
263 nl80211_policy);
264 if (res)
265 return res;
266
267 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
268 return -EINVAL;
269
270 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
271 if (!res)
272 return -EINVAL;
273 return res;
274}
275
Johannes Berg67748892010-10-04 21:14:06 +0200276static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
277 struct netlink_callback *cb,
278 struct cfg80211_registered_device **rdev,
279 struct net_device **dev)
280{
281 int ifidx = cb->args[0];
282 int err;
283
284 if (!ifidx)
285 ifidx = nl80211_get_ifidx(cb);
286 if (ifidx < 0)
287 return ifidx;
288
289 cb->args[0] = ifidx;
290
291 rtnl_lock();
292
293 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
294 if (!*dev) {
295 err = -ENODEV;
296 goto out_rtnl;
297 }
298
299 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100300 if (IS_ERR(*rdev)) {
301 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200302 goto out_rtnl;
303 }
304
305 return 0;
306 out_rtnl:
307 rtnl_unlock();
308 return err;
309}
310
311static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
312{
313 cfg80211_unlock_rdev(rdev);
314 rtnl_unlock();
315}
316
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100317/* IE validation */
318static bool is_valid_ie_attr(const struct nlattr *attr)
319{
320 const u8 *pos;
321 int len;
322
323 if (!attr)
324 return true;
325
326 pos = nla_data(attr);
327 len = nla_len(attr);
328
329 while (len) {
330 u8 elemlen;
331
332 if (len < 2)
333 return false;
334 len -= 2;
335
336 elemlen = pos[1];
337 if (elemlen > len)
338 return false;
339
340 len -= elemlen;
341 pos += 2 + elemlen;
342 }
343
344 return true;
345}
346
Johannes Berg55682962007-09-20 13:09:35 -0400347/* message building helper */
348static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
349 int flags, u8 cmd)
350{
351 /* since there is no private header just add the generic one */
352 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
353}
354
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400355static int nl80211_msg_put_channel(struct sk_buff *msg,
356 struct ieee80211_channel *chan)
357{
358 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
359 chan->center_freq);
360
361 if (chan->flags & IEEE80211_CHAN_DISABLED)
362 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
363 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
364 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
365 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
366 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
367 if (chan->flags & IEEE80211_CHAN_RADAR)
368 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
369
370 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
371 DBM_TO_MBM(chan->max_power));
372
373 return 0;
374
375 nla_put_failure:
376 return -ENOBUFS;
377}
378
Johannes Berg55682962007-09-20 13:09:35 -0400379/* netlink command implementations */
380
Johannes Bergb9454e82009-07-08 13:29:08 +0200381struct key_parse {
382 struct key_params p;
383 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200384 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200385 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100386 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200387};
388
389static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
390{
391 struct nlattr *tb[NL80211_KEY_MAX + 1];
392 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
393 nl80211_key_policy);
394 if (err)
395 return err;
396
397 k->def = !!tb[NL80211_KEY_DEFAULT];
398 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
399
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100400 if (k->def) {
401 k->def_uni = true;
402 k->def_multi = true;
403 }
404 if (k->defmgmt)
405 k->def_multi = true;
406
Johannes Bergb9454e82009-07-08 13:29:08 +0200407 if (tb[NL80211_KEY_IDX])
408 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
409
410 if (tb[NL80211_KEY_DATA]) {
411 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
412 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
413 }
414
415 if (tb[NL80211_KEY_SEQ]) {
416 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
417 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
418 }
419
420 if (tb[NL80211_KEY_CIPHER])
421 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
422
Johannes Berge31b8212010-10-05 19:39:30 +0200423 if (tb[NL80211_KEY_TYPE]) {
424 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
425 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
426 return -EINVAL;
427 }
428
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100429 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
430 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
431 int err = nla_parse_nested(kdt,
432 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
433 tb[NL80211_KEY_DEFAULT_TYPES],
434 nl80211_key_default_policy);
435 if (err)
436 return err;
437
438 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
439 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
440 }
441
Johannes Bergb9454e82009-07-08 13:29:08 +0200442 return 0;
443}
444
445static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
446{
447 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
448 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
449 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
450 }
451
452 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
453 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
454 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
455 }
456
457 if (info->attrs[NL80211_ATTR_KEY_IDX])
458 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
459
460 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
461 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
462
463 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
464 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
465
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100466 if (k->def) {
467 k->def_uni = true;
468 k->def_multi = true;
469 }
470 if (k->defmgmt)
471 k->def_multi = true;
472
Johannes Berge31b8212010-10-05 19:39:30 +0200473 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
474 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
475 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
476 return -EINVAL;
477 }
478
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100479 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
480 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
481 int err = nla_parse_nested(
482 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
483 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
484 nl80211_key_default_policy);
485 if (err)
486 return err;
487
488 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
489 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
490 }
491
Johannes Bergb9454e82009-07-08 13:29:08 +0200492 return 0;
493}
494
495static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
496{
497 int err;
498
499 memset(k, 0, sizeof(*k));
500 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200501 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200502
503 if (info->attrs[NL80211_ATTR_KEY])
504 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
505 else
506 err = nl80211_parse_key_old(info, k);
507
508 if (err)
509 return err;
510
511 if (k->def && k->defmgmt)
512 return -EINVAL;
513
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100514 if (k->defmgmt) {
515 if (k->def_uni || !k->def_multi)
516 return -EINVAL;
517 }
518
Johannes Bergb9454e82009-07-08 13:29:08 +0200519 if (k->idx != -1) {
520 if (k->defmgmt) {
521 if (k->idx < 4 || k->idx > 5)
522 return -EINVAL;
523 } else if (k->def) {
524 if (k->idx < 0 || k->idx > 3)
525 return -EINVAL;
526 } else {
527 if (k->idx < 0 || k->idx > 5)
528 return -EINVAL;
529 }
530 }
531
532 return 0;
533}
534
Johannes Bergfffd0932009-07-08 14:22:54 +0200535static struct cfg80211_cached_keys *
536nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
537 struct nlattr *keys)
538{
539 struct key_parse parse;
540 struct nlattr *key;
541 struct cfg80211_cached_keys *result;
542 int rem, err, def = 0;
543
544 result = kzalloc(sizeof(*result), GFP_KERNEL);
545 if (!result)
546 return ERR_PTR(-ENOMEM);
547
548 result->def = -1;
549 result->defmgmt = -1;
550
551 nla_for_each_nested(key, keys, rem) {
552 memset(&parse, 0, sizeof(parse));
553 parse.idx = -1;
554
555 err = nl80211_parse_key_new(key, &parse);
556 if (err)
557 goto error;
558 err = -EINVAL;
559 if (!parse.p.key)
560 goto error;
561 if (parse.idx < 0 || parse.idx > 4)
562 goto error;
563 if (parse.def) {
564 if (def)
565 goto error;
566 def = 1;
567 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100568 if (!parse.def_uni || !parse.def_multi)
569 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200570 } else if (parse.defmgmt)
571 goto error;
572 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200573 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200574 if (err)
575 goto error;
576 result->params[parse.idx].cipher = parse.p.cipher;
577 result->params[parse.idx].key_len = parse.p.key_len;
578 result->params[parse.idx].key = result->data[parse.idx];
579 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
580 }
581
582 return result;
583 error:
584 kfree(result);
585 return ERR_PTR(err);
586}
587
588static int nl80211_key_allowed(struct wireless_dev *wdev)
589{
590 ASSERT_WDEV_LOCK(wdev);
591
Johannes Bergfffd0932009-07-08 14:22:54 +0200592 switch (wdev->iftype) {
593 case NL80211_IFTYPE_AP:
594 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200595 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700596 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200597 break;
598 case NL80211_IFTYPE_ADHOC:
599 if (!wdev->current_bss)
600 return -ENOLINK;
601 break;
602 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200603 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200604 if (wdev->sme_state != CFG80211_SME_CONNECTED)
605 return -ENOLINK;
606 break;
607 default:
608 return -EINVAL;
609 }
610
611 return 0;
612}
613
Johannes Berg7527a782011-05-13 10:58:57 +0200614static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
615{
616 struct nlattr *nl_modes = nla_nest_start(msg, attr);
617 int i;
618
619 if (!nl_modes)
620 goto nla_put_failure;
621
622 i = 0;
623 while (ifmodes) {
624 if (ifmodes & 1)
625 NLA_PUT_FLAG(msg, i);
626 ifmodes >>= 1;
627 i++;
628 }
629
630 nla_nest_end(msg, nl_modes);
631 return 0;
632
633nla_put_failure:
634 return -ENOBUFS;
635}
636
637static int nl80211_put_iface_combinations(struct wiphy *wiphy,
638 struct sk_buff *msg)
639{
640 struct nlattr *nl_combis;
641 int i, j;
642
643 nl_combis = nla_nest_start(msg,
644 NL80211_ATTR_INTERFACE_COMBINATIONS);
645 if (!nl_combis)
646 goto nla_put_failure;
647
648 for (i = 0; i < wiphy->n_iface_combinations; i++) {
649 const struct ieee80211_iface_combination *c;
650 struct nlattr *nl_combi, *nl_limits;
651
652 c = &wiphy->iface_combinations[i];
653
654 nl_combi = nla_nest_start(msg, i + 1);
655 if (!nl_combi)
656 goto nla_put_failure;
657
658 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
659 if (!nl_limits)
660 goto nla_put_failure;
661
662 for (j = 0; j < c->n_limits; j++) {
663 struct nlattr *nl_limit;
664
665 nl_limit = nla_nest_start(msg, j + 1);
666 if (!nl_limit)
667 goto nla_put_failure;
668 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
669 c->limits[j].max);
670 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
671 c->limits[j].types))
672 goto nla_put_failure;
673 nla_nest_end(msg, nl_limit);
674 }
675
676 nla_nest_end(msg, nl_limits);
677
678 if (c->beacon_int_infra_match)
679 NLA_PUT_FLAG(msg,
680 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
681 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
682 c->num_different_channels);
683 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
684 c->max_interfaces);
685
686 nla_nest_end(msg, nl_combi);
687 }
688
689 nla_nest_end(msg, nl_combis);
690
691 return 0;
692nla_put_failure:
693 return -ENOBUFS;
694}
695
Johannes Berg55682962007-09-20 13:09:35 -0400696static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
697 struct cfg80211_registered_device *dev)
698{
699 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100700 struct nlattr *nl_bands, *nl_band;
701 struct nlattr *nl_freqs, *nl_freq;
702 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100703 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100704 enum ieee80211_band band;
705 struct ieee80211_channel *chan;
706 struct ieee80211_rate *rate;
707 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200708 const struct ieee80211_txrx_stypes *mgmt_stypes =
709 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400710
711 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
712 if (!hdr)
713 return -1;
714
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500715 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400716 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200717
Johannes Bergf5ea9122009-08-07 16:17:38 +0200718 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
719 cfg80211_rdev_list_generation);
720
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200721 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
722 dev->wiphy.retry_short);
723 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
724 dev->wiphy.retry_long);
725 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
726 dev->wiphy.frag_threshold);
727 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
728 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100729 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
730 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100731 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
732 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300733 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
734 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200735 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
736 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300737 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
738 dev->wiphy.max_sched_scan_ie_len);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300739 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
740 dev->wiphy.max_match_sets);
Johannes Bergee688b002008-01-24 19:38:39 +0100741
Johannes Berge31b8212010-10-05 19:39:30 +0200742 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
743 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700744 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
745 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Eliad Pellercedb5412011-08-31 11:29:43 +0300746 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
747 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530748 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
749 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300750 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
751 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
752 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
753 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530754
Johannes Berg25e47c182009-04-02 20:14:06 +0200755 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
756 sizeof(u32) * dev->wiphy.n_cipher_suites,
757 dev->wiphy.cipher_suites);
758
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100759 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
760 dev->wiphy.max_num_pmkids);
761
Johannes Bergc0692b82010-08-27 14:26:53 +0300762 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
763 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
764
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900765 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
766 dev->wiphy.available_antennas_tx);
767 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
768 dev->wiphy.available_antennas_rx);
769
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200770 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
771 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
772 dev->wiphy.probe_resp_offload);
773
Bruno Randolf7f531e02010-12-16 11:30:22 +0900774 if ((dev->wiphy.available_antennas_tx ||
775 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900776 u32 tx_ant = 0, rx_ant = 0;
777 int res;
778 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
779 if (!res) {
780 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
781 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
782 }
783 }
784
Johannes Berg7527a782011-05-13 10:58:57 +0200785 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
786 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700787 goto nla_put_failure;
788
Johannes Bergee688b002008-01-24 19:38:39 +0100789 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
790 if (!nl_bands)
791 goto nla_put_failure;
792
793 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
794 if (!dev->wiphy.bands[band])
795 continue;
796
797 nl_band = nla_nest_start(msg, band);
798 if (!nl_band)
799 goto nla_put_failure;
800
Johannes Bergd51626d2008-10-09 12:20:13 +0200801 /* add HT info */
802 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
803 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
804 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
805 &dev->wiphy.bands[band]->ht_cap.mcs);
806 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
807 dev->wiphy.bands[band]->ht_cap.cap);
808 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
809 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
810 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
811 dev->wiphy.bands[band]->ht_cap.ampdu_density);
812 }
813
Johannes Bergee688b002008-01-24 19:38:39 +0100814 /* add frequencies */
815 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
816 if (!nl_freqs)
817 goto nla_put_failure;
818
819 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
820 nl_freq = nla_nest_start(msg, i);
821 if (!nl_freq)
822 goto nla_put_failure;
823
824 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100825
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400826 if (nl80211_msg_put_channel(msg, chan))
827 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200828
Johannes Bergee688b002008-01-24 19:38:39 +0100829 nla_nest_end(msg, nl_freq);
830 }
831
832 nla_nest_end(msg, nl_freqs);
833
834 /* add bitrates */
835 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
836 if (!nl_rates)
837 goto nla_put_failure;
838
839 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
840 nl_rate = nla_nest_start(msg, i);
841 if (!nl_rate)
842 goto nla_put_failure;
843
844 rate = &dev->wiphy.bands[band]->bitrates[i];
845 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
846 rate->bitrate);
847 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
848 NLA_PUT_FLAG(msg,
849 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
850
851 nla_nest_end(msg, nl_rate);
852 }
853
854 nla_nest_end(msg, nl_rates);
855
856 nla_nest_end(msg, nl_band);
857 }
858 nla_nest_end(msg, nl_bands);
859
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
861 if (!nl_cmds)
862 goto nla_put_failure;
863
864 i = 0;
865#define CMD(op, n) \
866 do { \
867 if (dev->ops->op) { \
868 i++; \
869 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
870 } \
871 } while (0)
872
873 CMD(add_virtual_intf, NEW_INTERFACE);
874 CMD(change_virtual_intf, SET_INTERFACE);
875 CMD(add_key, NEW_KEY);
876 CMD(add_beacon, NEW_BEACON);
877 CMD(add_station, NEW_STATION);
878 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800879 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100880 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200881 CMD(auth, AUTHENTICATE);
882 CMD(assoc, ASSOCIATE);
883 CMD(deauth, DEAUTHENTICATE);
884 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200885 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100886 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100887 CMD(set_pmksa, SET_PMKSA);
888 CMD(del_pmksa, DEL_PMKSA);
889 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +0100890 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
891 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200892 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200893 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100894 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100895 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200896 i++;
897 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
898 }
Johannes Bergf444de02010-05-05 15:25:02 +0200899 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400900 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +0300901 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
902 CMD(tdls_mgmt, TDLS_MGMT);
903 CMD(tdls_oper, TDLS_OPER);
904 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300905 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
906 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +0100907 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100908 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +0100909 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
910 i++;
911 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
912 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100913
Kalle Valo4745fc02011-11-17 19:06:10 +0200914#ifdef CONFIG_NL80211_TESTMODE
915 CMD(testmode_cmd, TESTMODE);
916#endif
917
Johannes Berg8fdc6212009-03-14 09:34:01 +0100918#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919
Johannes Berg6829c8782009-07-02 09:13:27 +0200920 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200921 i++;
922 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
923 }
924
Johannes Berg6829c8782009-07-02 09:13:27 +0200925 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200926 i++;
927 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
928 }
929
Johannes Berg8fdc6212009-03-14 09:34:01 +0100930 nla_nest_end(msg, nl_cmds);
931
Johannes Berg7c4ef712011-11-18 15:33:48 +0100932 if (dev->ops->remain_on_channel &&
933 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berga2939112010-12-14 17:54:28 +0100934 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
935 dev->wiphy.max_remain_on_channel_duration);
936
Johannes Berg7c4ef712011-11-18 15:33:48 +0100937 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100938 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
939
Johannes Berg2e161f72010-08-12 15:38:38 +0200940 if (mgmt_stypes) {
941 u16 stypes;
942 struct nlattr *nl_ftypes, *nl_ifs;
943 enum nl80211_iftype ift;
944
945 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
946 if (!nl_ifs)
947 goto nla_put_failure;
948
949 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
950 nl_ftypes = nla_nest_start(msg, ift);
951 if (!nl_ftypes)
952 goto nla_put_failure;
953 i = 0;
954 stypes = mgmt_stypes[ift].tx;
955 while (stypes) {
956 if (stypes & 1)
957 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
958 (i << 4) | IEEE80211_FTYPE_MGMT);
959 stypes >>= 1;
960 i++;
961 }
962 nla_nest_end(msg, nl_ftypes);
963 }
964
Johannes Berg74b70a42010-08-24 12:15:53 +0200965 nla_nest_end(msg, nl_ifs);
966
Johannes Berg2e161f72010-08-12 15:38:38 +0200967 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
968 if (!nl_ifs)
969 goto nla_put_failure;
970
971 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
972 nl_ftypes = nla_nest_start(msg, ift);
973 if (!nl_ftypes)
974 goto nla_put_failure;
975 i = 0;
976 stypes = mgmt_stypes[ift].rx;
977 while (stypes) {
978 if (stypes & 1)
979 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
980 (i << 4) | IEEE80211_FTYPE_MGMT);
981 stypes >>= 1;
982 i++;
983 }
984 nla_nest_end(msg, nl_ftypes);
985 }
986 nla_nest_end(msg, nl_ifs);
987 }
988
Johannes Bergff1b6e62011-05-04 15:37:28 +0200989 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
990 struct nlattr *nl_wowlan;
991
992 nl_wowlan = nla_nest_start(msg,
993 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
994 if (!nl_wowlan)
995 goto nla_put_failure;
996
997 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
998 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
999 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
1000 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1001 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1002 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02001003 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1004 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1005 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1006 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1007 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1008 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1009 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1010 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1011 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1012 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02001013 if (dev->wiphy.wowlan.n_patterns) {
1014 struct nl80211_wowlan_pattern_support pat = {
1015 .max_patterns = dev->wiphy.wowlan.n_patterns,
1016 .min_pattern_len =
1017 dev->wiphy.wowlan.pattern_min_len,
1018 .max_pattern_len =
1019 dev->wiphy.wowlan.pattern_max_len,
1020 };
1021 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1022 sizeof(pat), &pat);
1023 }
1024
1025 nla_nest_end(msg, nl_wowlan);
1026 }
1027
Johannes Berg7527a782011-05-13 10:58:57 +02001028 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1029 dev->wiphy.software_iftypes))
1030 goto nla_put_failure;
1031
1032 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1033 goto nla_put_failure;
1034
Johannes Berg562a7482011-11-07 12:39:33 +01001035 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1036 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1037 dev->wiphy.ap_sme_capa);
1038
Johannes Berg1f074bd2011-11-06 14:13:33 +01001039 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1040
Ben Greear7e7c8922011-11-18 11:31:59 -08001041 if (dev->wiphy.ht_capa_mod_mask)
1042 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1043 sizeof(*dev->wiphy.ht_capa_mod_mask),
1044 dev->wiphy.ht_capa_mod_mask);
1045
Johannes Berg55682962007-09-20 13:09:35 -04001046 return genlmsg_end(msg, hdr);
1047
1048 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001049 genlmsg_cancel(msg, hdr);
1050 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001051}
1052
1053static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1054{
1055 int idx = 0;
1056 int start = cb->args[0];
1057 struct cfg80211_registered_device *dev;
1058
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001059 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001060 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001061 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1062 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001063 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001064 continue;
1065 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1066 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001067 dev) < 0) {
1068 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001069 break;
Julius Volzb4637272008-07-08 14:02:19 +02001070 }
Johannes Berg55682962007-09-20 13:09:35 -04001071 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001072 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001073
1074 cb->args[0] = idx;
1075
1076 return skb->len;
1077}
1078
1079static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1080{
1081 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001082 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001083
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001084 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001085 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001086 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001087
Johannes Berg4c476992010-10-04 21:36:35 +02001088 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1089 nlmsg_free(msg);
1090 return -ENOBUFS;
1091 }
Johannes Berg55682962007-09-20 13:09:35 -04001092
Johannes Berg134e6372009-07-10 09:51:34 +00001093 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001094}
1095
Jouni Malinen31888482008-10-30 16:59:24 +02001096static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1097 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1098 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1099 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1100 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1101 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1102};
1103
1104static int parse_txq_params(struct nlattr *tb[],
1105 struct ieee80211_txq_params *txq_params)
1106{
1107 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1108 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1109 !tb[NL80211_TXQ_ATTR_AIFS])
1110 return -EINVAL;
1111
1112 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1113 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1114 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1115 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1116 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1117
1118 return 0;
1119}
1120
Johannes Bergf444de02010-05-05 15:25:02 +02001121static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1122{
1123 /*
1124 * You can only set the channel explicitly for AP, mesh
1125 * and WDS type interfaces; all others have their channel
1126 * managed via their respective "establish a connection"
1127 * command (connect, join, ...)
1128 *
1129 * Monitors are special as they are normally slaved to
1130 * whatever else is going on, so they behave as though
1131 * you tried setting the wiphy channel itself.
1132 */
1133 return !wdev ||
1134 wdev->iftype == NL80211_IFTYPE_AP ||
1135 wdev->iftype == NL80211_IFTYPE_WDS ||
1136 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001137 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1138 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001139}
1140
1141static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1142 struct wireless_dev *wdev,
1143 struct genl_info *info)
1144{
1145 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1146 u32 freq;
1147 int result;
1148
1149 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1150 return -EINVAL;
1151
1152 if (!nl80211_can_set_dev_channel(wdev))
1153 return -EOPNOTSUPP;
1154
1155 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1156 channel_type = nla_get_u32(info->attrs[
1157 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1158 if (channel_type != NL80211_CHAN_NO_HT &&
1159 channel_type != NL80211_CHAN_HT20 &&
1160 channel_type != NL80211_CHAN_HT40PLUS &&
1161 channel_type != NL80211_CHAN_HT40MINUS)
1162 return -EINVAL;
1163 }
1164
1165 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1166
1167 mutex_lock(&rdev->devlist_mtx);
1168 if (wdev) {
1169 wdev_lock(wdev);
1170 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1171 wdev_unlock(wdev);
1172 } else {
1173 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1174 }
1175 mutex_unlock(&rdev->devlist_mtx);
1176
1177 return result;
1178}
1179
1180static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1181{
Johannes Berg4c476992010-10-04 21:36:35 +02001182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1183 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001184
Johannes Berg4c476992010-10-04 21:36:35 +02001185 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001186}
1187
Bill Jordane8347eb2010-10-01 13:54:28 -04001188static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1189{
Johannes Berg43b19952010-10-07 13:10:30 +02001190 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1191 struct net_device *dev = info->user_ptr[1];
1192 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001193 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001194
1195 if (!info->attrs[NL80211_ATTR_MAC])
1196 return -EINVAL;
1197
Johannes Berg43b19952010-10-07 13:10:30 +02001198 if (netif_running(dev))
1199 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001200
Johannes Berg43b19952010-10-07 13:10:30 +02001201 if (!rdev->ops->set_wds_peer)
1202 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001203
Johannes Berg43b19952010-10-07 13:10:30 +02001204 if (wdev->iftype != NL80211_IFTYPE_WDS)
1205 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001206
1207 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001208 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001209}
1210
1211
Johannes Berg55682962007-09-20 13:09:35 -04001212static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1213{
1214 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001215 struct net_device *netdev = NULL;
1216 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001217 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001218 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001219 u32 changed;
1220 u8 retry_short = 0, retry_long = 0;
1221 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001222 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001223
Johannes Bergf444de02010-05-05 15:25:02 +02001224 /*
1225 * Try to find the wiphy and netdev. Normally this
1226 * function shouldn't need the netdev, but this is
1227 * done for backward compatibility -- previously
1228 * setting the channel was done per wiphy, but now
1229 * it is per netdev. Previous userland like hostapd
1230 * also passed a netdev to set_wiphy, so that it is
1231 * possible to let that go to the right netdev!
1232 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001233 mutex_lock(&cfg80211_mutex);
1234
Johannes Bergf444de02010-05-05 15:25:02 +02001235 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1236 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1237
1238 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1239 if (netdev && netdev->ieee80211_ptr) {
1240 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1241 mutex_lock(&rdev->mtx);
1242 } else
1243 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001244 }
1245
Johannes Bergf444de02010-05-05 15:25:02 +02001246 if (!netdev) {
1247 rdev = __cfg80211_rdev_from_info(info);
1248 if (IS_ERR(rdev)) {
1249 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001250 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001251 }
1252 wdev = NULL;
1253 netdev = NULL;
1254 result = 0;
1255
1256 mutex_lock(&rdev->mtx);
1257 } else if (netif_running(netdev) &&
1258 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1259 wdev = netdev->ieee80211_ptr;
1260 else
1261 wdev = NULL;
1262
1263 /*
1264 * end workaround code, by now the rdev is available
1265 * and locked, and wdev may or may not be NULL.
1266 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001267
1268 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001269 result = cfg80211_dev_rename(
1270 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001271
1272 mutex_unlock(&cfg80211_mutex);
1273
1274 if (result)
1275 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001276
Jouni Malinen31888482008-10-30 16:59:24 +02001277 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1278 struct ieee80211_txq_params txq_params;
1279 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1280
1281 if (!rdev->ops->set_txq_params) {
1282 result = -EOPNOTSUPP;
1283 goto bad_res;
1284 }
1285
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001286 if (!netdev) {
1287 result = -EINVAL;
1288 goto bad_res;
1289 }
1290
Johannes Berg133a3ff2011-11-03 14:50:13 +01001291 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1292 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1293 result = -EINVAL;
1294 goto bad_res;
1295 }
1296
Jouni Malinen31888482008-10-30 16:59:24 +02001297 nla_for_each_nested(nl_txq_params,
1298 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1299 rem_txq_params) {
1300 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1301 nla_data(nl_txq_params),
1302 nla_len(nl_txq_params),
1303 txq_params_policy);
1304 result = parse_txq_params(tb, &txq_params);
1305 if (result)
1306 goto bad_res;
1307
1308 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001309 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001310 &txq_params);
1311 if (result)
1312 goto bad_res;
1313 }
1314 }
1315
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001316 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001317 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001318 if (result)
1319 goto bad_res;
1320 }
1321
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001322 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1323 enum nl80211_tx_power_setting type;
1324 int idx, mbm = 0;
1325
1326 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001327 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001328 goto bad_res;
1329 }
1330
1331 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1332 type = nla_get_u32(info->attrs[idx]);
1333
1334 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1335 (type != NL80211_TX_POWER_AUTOMATIC)) {
1336 result = -EINVAL;
1337 goto bad_res;
1338 }
1339
1340 if (type != NL80211_TX_POWER_AUTOMATIC) {
1341 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1342 mbm = nla_get_u32(info->attrs[idx]);
1343 }
1344
1345 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1346 if (result)
1347 goto bad_res;
1348 }
1349
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001350 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1351 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1352 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001353 if ((!rdev->wiphy.available_antennas_tx &&
1354 !rdev->wiphy.available_antennas_rx) ||
1355 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001356 result = -EOPNOTSUPP;
1357 goto bad_res;
1358 }
1359
1360 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1361 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1362
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001363 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001364 * available antenna masks, except for the "all" mask */
1365 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1366 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001367 result = -EINVAL;
1368 goto bad_res;
1369 }
1370
Bruno Randolf7f531e02010-12-16 11:30:22 +09001371 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1372 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001373
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001374 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1375 if (result)
1376 goto bad_res;
1377 }
1378
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001379 changed = 0;
1380
1381 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1382 retry_short = nla_get_u8(
1383 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1384 if (retry_short == 0) {
1385 result = -EINVAL;
1386 goto bad_res;
1387 }
1388 changed |= WIPHY_PARAM_RETRY_SHORT;
1389 }
1390
1391 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1392 retry_long = nla_get_u8(
1393 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1394 if (retry_long == 0) {
1395 result = -EINVAL;
1396 goto bad_res;
1397 }
1398 changed |= WIPHY_PARAM_RETRY_LONG;
1399 }
1400
1401 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1402 frag_threshold = nla_get_u32(
1403 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1404 if (frag_threshold < 256) {
1405 result = -EINVAL;
1406 goto bad_res;
1407 }
1408 if (frag_threshold != (u32) -1) {
1409 /*
1410 * Fragments (apart from the last one) are required to
1411 * have even length. Make the fragmentation code
1412 * simpler by stripping LSB should someone try to use
1413 * odd threshold value.
1414 */
1415 frag_threshold &= ~0x1;
1416 }
1417 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1418 }
1419
1420 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1421 rts_threshold = nla_get_u32(
1422 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1423 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1424 }
1425
Lukáš Turek81077e82009-12-21 22:50:47 +01001426 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1427 coverage_class = nla_get_u8(
1428 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1429 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1430 }
1431
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001432 if (changed) {
1433 u8 old_retry_short, old_retry_long;
1434 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001435 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001436
1437 if (!rdev->ops->set_wiphy_params) {
1438 result = -EOPNOTSUPP;
1439 goto bad_res;
1440 }
1441
1442 old_retry_short = rdev->wiphy.retry_short;
1443 old_retry_long = rdev->wiphy.retry_long;
1444 old_frag_threshold = rdev->wiphy.frag_threshold;
1445 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001446 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001447
1448 if (changed & WIPHY_PARAM_RETRY_SHORT)
1449 rdev->wiphy.retry_short = retry_short;
1450 if (changed & WIPHY_PARAM_RETRY_LONG)
1451 rdev->wiphy.retry_long = retry_long;
1452 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1453 rdev->wiphy.frag_threshold = frag_threshold;
1454 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1455 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001456 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1457 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001458
1459 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1460 if (result) {
1461 rdev->wiphy.retry_short = old_retry_short;
1462 rdev->wiphy.retry_long = old_retry_long;
1463 rdev->wiphy.frag_threshold = old_frag_threshold;
1464 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001465 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001466 }
1467 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001468
Johannes Berg306d6112008-12-08 12:39:04 +01001469 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001470 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001471 if (netdev)
1472 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001473 return result;
1474}
1475
1476
1477static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001478 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001479 struct net_device *dev)
1480{
1481 void *hdr;
1482
1483 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1484 if (!hdr)
1485 return -1;
1486
1487 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001488 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001489 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001490 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001491
1492 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1493 rdev->devlist_generation ^
1494 (cfg80211_rdev_list_generation << 2));
1495
Johannes Berg55682962007-09-20 13:09:35 -04001496 return genlmsg_end(msg, hdr);
1497
1498 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001499 genlmsg_cancel(msg, hdr);
1500 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001501}
1502
1503static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1504{
1505 int wp_idx = 0;
1506 int if_idx = 0;
1507 int wp_start = cb->args[0];
1508 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001509 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001510 struct wireless_dev *wdev;
1511
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001512 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001513 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1514 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001515 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001516 if (wp_idx < wp_start) {
1517 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001518 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001519 }
Johannes Berg55682962007-09-20 13:09:35 -04001520 if_idx = 0;
1521
Johannes Bergf5ea9122009-08-07 16:17:38 +02001522 mutex_lock(&rdev->devlist_mtx);
1523 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001524 if (if_idx < if_start) {
1525 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001526 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001527 }
Johannes Berg55682962007-09-20 13:09:35 -04001528 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1529 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001530 rdev, wdev->netdev) < 0) {
1531 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001532 goto out;
1533 }
1534 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001535 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001536 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001537
1538 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001539 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001540 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001541 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001542
1543 cb->args[0] = wp_idx;
1544 cb->args[1] = if_idx;
1545
1546 return skb->len;
1547}
1548
1549static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1550{
1551 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001552 struct cfg80211_registered_device *dev = info->user_ptr[0];
1553 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001554
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001555 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001556 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001557 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001558
Johannes Bergd7264052009-04-19 16:23:20 +02001559 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001560 dev, netdev) < 0) {
1561 nlmsg_free(msg);
1562 return -ENOBUFS;
1563 }
Johannes Berg55682962007-09-20 13:09:35 -04001564
Johannes Berg134e6372009-07-10 09:51:34 +00001565 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001566}
1567
Michael Wu66f7ac52008-01-31 19:48:22 +01001568static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1569 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1570 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1571 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1572 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1573 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1574};
1575
1576static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1577{
1578 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1579 int flag;
1580
1581 *mntrflags = 0;
1582
1583 if (!nla)
1584 return -EINVAL;
1585
1586 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1587 nla, mntr_flags_policy))
1588 return -EINVAL;
1589
1590 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1591 if (flags[flag])
1592 *mntrflags |= (1<<flag);
1593
1594 return 0;
1595}
1596
Johannes Berg9bc383d2009-11-19 11:55:19 +01001597static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001598 struct net_device *netdev, u8 use_4addr,
1599 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001600{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001601 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001602 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001603 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001604 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001605 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001606
1607 switch (iftype) {
1608 case NL80211_IFTYPE_AP_VLAN:
1609 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1610 return 0;
1611 break;
1612 case NL80211_IFTYPE_STATION:
1613 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1614 return 0;
1615 break;
1616 default:
1617 break;
1618 }
1619
1620 return -EOPNOTSUPP;
1621}
1622
Johannes Berg55682962007-09-20 13:09:35 -04001623static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1624{
Johannes Berg4c476992010-10-04 21:36:35 +02001625 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001626 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001627 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001628 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001629 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001630 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001631 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001632
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001633 memset(&params, 0, sizeof(params));
1634
Johannes Berg04a773a2009-04-19 21:24:32 +02001635 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001636
Johannes Berg723b0382008-09-16 20:22:09 +02001637 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001638 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001639 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001640 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001641 if (ntype > NL80211_IFTYPE_MAX)
1642 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001643 }
1644
Johannes Berg92ffe052008-09-16 20:39:36 +02001645 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001646 struct wireless_dev *wdev = dev->ieee80211_ptr;
1647
Johannes Berg4c476992010-10-04 21:36:35 +02001648 if (ntype != NL80211_IFTYPE_MESH_POINT)
1649 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001650 if (netif_running(dev))
1651 return -EBUSY;
1652
1653 wdev_lock(wdev);
1654 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1655 IEEE80211_MAX_MESH_ID_LEN);
1656 wdev->mesh_id_up_len =
1657 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1658 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1659 wdev->mesh_id_up_len);
1660 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001661 }
1662
Felix Fietkau8b787642009-11-10 18:53:10 +01001663 if (info->attrs[NL80211_ATTR_4ADDR]) {
1664 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1665 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001666 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001667 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001668 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001669 } else {
1670 params.use_4addr = -1;
1671 }
1672
Johannes Berg92ffe052008-09-16 20:39:36 +02001673 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001674 if (ntype != NL80211_IFTYPE_MONITOR)
1675 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001676 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1677 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001678 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001679 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001680
1681 flags = &_flags;
1682 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001683 }
Johannes Berg3b858752009-03-12 09:55:09 +01001684
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001685 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001686 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001687 else
1688 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001689
Johannes Berg9bc383d2009-11-19 11:55:19 +01001690 if (!err && params.use_4addr != -1)
1691 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1692
Johannes Berg55682962007-09-20 13:09:35 -04001693 return err;
1694}
1695
1696static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1697{
Johannes Berg4c476992010-10-04 21:36:35 +02001698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001699 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001700 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001701 int err;
1702 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001703 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001704
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001705 memset(&params, 0, sizeof(params));
1706
Johannes Berg55682962007-09-20 13:09:35 -04001707 if (!info->attrs[NL80211_ATTR_IFNAME])
1708 return -EINVAL;
1709
1710 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1711 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1712 if (type > NL80211_IFTYPE_MAX)
1713 return -EINVAL;
1714 }
1715
Johannes Berg79c97e92009-07-07 03:56:12 +02001716 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001717 !(rdev->wiphy.interface_modes & (1 << type)))
1718 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001719
Johannes Berg9bc383d2009-11-19 11:55:19 +01001720 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001721 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001722 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001723 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001724 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001725 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001726
Michael Wu66f7ac52008-01-31 19:48:22 +01001727 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1728 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1729 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001730 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001731 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001732 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001733 if (IS_ERR(dev))
1734 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001735
Johannes Berg29cbe682010-12-03 09:20:44 +01001736 if (type == NL80211_IFTYPE_MESH_POINT &&
1737 info->attrs[NL80211_ATTR_MESH_ID]) {
1738 struct wireless_dev *wdev = dev->ieee80211_ptr;
1739
1740 wdev_lock(wdev);
1741 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1742 IEEE80211_MAX_MESH_ID_LEN);
1743 wdev->mesh_id_up_len =
1744 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1745 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1746 wdev->mesh_id_up_len);
1747 wdev_unlock(wdev);
1748 }
1749
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001750 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001751}
1752
1753static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1754{
Johannes Berg4c476992010-10-04 21:36:35 +02001755 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1756 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001757
Johannes Berg4c476992010-10-04 21:36:35 +02001758 if (!rdev->ops->del_virtual_intf)
1759 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001760
Johannes Berg4c476992010-10-04 21:36:35 +02001761 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001762}
1763
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001764static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1765{
1766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1767 struct net_device *dev = info->user_ptr[1];
1768 u16 noack_map;
1769
1770 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1771 return -EINVAL;
1772
1773 if (!rdev->ops->set_noack_map)
1774 return -EOPNOTSUPP;
1775
1776 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1777
1778 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1779}
1780
Johannes Berg41ade002007-12-19 02:03:29 +01001781struct get_key_cookie {
1782 struct sk_buff *msg;
1783 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001784 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001785};
1786
1787static void get_key_callback(void *c, struct key_params *params)
1788{
Johannes Bergb9454e82009-07-08 13:29:08 +02001789 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001790 struct get_key_cookie *cookie = c;
1791
1792 if (params->key)
1793 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1794 params->key_len, params->key);
1795
1796 if (params->seq)
1797 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1798 params->seq_len, params->seq);
1799
1800 if (params->cipher)
1801 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1802 params->cipher);
1803
Johannes Bergb9454e82009-07-08 13:29:08 +02001804 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1805 if (!key)
1806 goto nla_put_failure;
1807
1808 if (params->key)
1809 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1810 params->key_len, params->key);
1811
1812 if (params->seq)
1813 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1814 params->seq_len, params->seq);
1815
1816 if (params->cipher)
1817 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1818 params->cipher);
1819
1820 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1821
1822 nla_nest_end(cookie->msg, key);
1823
Johannes Berg41ade002007-12-19 02:03:29 +01001824 return;
1825 nla_put_failure:
1826 cookie->error = 1;
1827}
1828
1829static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1830{
Johannes Berg4c476992010-10-04 21:36:35 +02001831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001832 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001833 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001834 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001835 const u8 *mac_addr = NULL;
1836 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001837 struct get_key_cookie cookie = {
1838 .error = 0,
1839 };
1840 void *hdr;
1841 struct sk_buff *msg;
1842
1843 if (info->attrs[NL80211_ATTR_KEY_IDX])
1844 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1845
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001846 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001847 return -EINVAL;
1848
1849 if (info->attrs[NL80211_ATTR_MAC])
1850 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1851
Johannes Berge31b8212010-10-05 19:39:30 +02001852 pairwise = !!mac_addr;
1853 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1854 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1855 if (kt >= NUM_NL80211_KEYTYPES)
1856 return -EINVAL;
1857 if (kt != NL80211_KEYTYPE_GROUP &&
1858 kt != NL80211_KEYTYPE_PAIRWISE)
1859 return -EINVAL;
1860 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1861 }
1862
Johannes Berg4c476992010-10-04 21:36:35 +02001863 if (!rdev->ops->get_key)
1864 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001865
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001867 if (!msg)
1868 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001869
1870 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1871 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001872 if (IS_ERR(hdr))
1873 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001874
1875 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001876 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001877
1878 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1879 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1880 if (mac_addr)
1881 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1882
Johannes Berge31b8212010-10-05 19:39:30 +02001883 if (pairwise && mac_addr &&
1884 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1885 return -ENOENT;
1886
1887 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1888 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001889
1890 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001891 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001892
1893 if (cookie.error)
1894 goto nla_put_failure;
1895
1896 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001897 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001898
1899 nla_put_failure:
1900 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001901 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001902 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001903 return err;
1904}
1905
1906static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1907{
Johannes Berg4c476992010-10-04 21:36:35 +02001908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001909 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001910 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001911 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001912
Johannes Bergb9454e82009-07-08 13:29:08 +02001913 err = nl80211_parse_key(info, &key);
1914 if (err)
1915 return err;
1916
1917 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001918 return -EINVAL;
1919
Johannes Bergb9454e82009-07-08 13:29:08 +02001920 /* only support setting default key */
1921 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001922 return -EINVAL;
1923
Johannes Bergfffd0932009-07-08 14:22:54 +02001924 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001925
1926 if (key.def) {
1927 if (!rdev->ops->set_default_key) {
1928 err = -EOPNOTSUPP;
1929 goto out;
1930 }
1931
1932 err = nl80211_key_allowed(dev->ieee80211_ptr);
1933 if (err)
1934 goto out;
1935
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001936 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1937 key.def_uni, key.def_multi);
1938
1939 if (err)
1940 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001941
Johannes Berg3d23e342009-09-29 23:27:28 +02001942#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001943 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001944#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001945 } else {
1946 if (key.def_uni || !key.def_multi) {
1947 err = -EINVAL;
1948 goto out;
1949 }
1950
1951 if (!rdev->ops->set_default_mgmt_key) {
1952 err = -EOPNOTSUPP;
1953 goto out;
1954 }
1955
1956 err = nl80211_key_allowed(dev->ieee80211_ptr);
1957 if (err)
1958 goto out;
1959
1960 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1961 dev, key.idx);
1962 if (err)
1963 goto out;
1964
1965#ifdef CONFIG_CFG80211_WEXT
1966 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1967#endif
1968 }
1969
1970 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001971 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001972
Johannes Berg41ade002007-12-19 02:03:29 +01001973 return err;
1974}
1975
1976static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1977{
Johannes Berg4c476992010-10-04 21:36:35 +02001978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001979 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001980 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001981 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001982 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001983
Johannes Bergb9454e82009-07-08 13:29:08 +02001984 err = nl80211_parse_key(info, &key);
1985 if (err)
1986 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001987
Johannes Bergb9454e82009-07-08 13:29:08 +02001988 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001989 return -EINVAL;
1990
Johannes Berg41ade002007-12-19 02:03:29 +01001991 if (info->attrs[NL80211_ATTR_MAC])
1992 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1993
Johannes Berge31b8212010-10-05 19:39:30 +02001994 if (key.type == -1) {
1995 if (mac_addr)
1996 key.type = NL80211_KEYTYPE_PAIRWISE;
1997 else
1998 key.type = NL80211_KEYTYPE_GROUP;
1999 }
2000
2001 /* for now */
2002 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2003 key.type != NL80211_KEYTYPE_GROUP)
2004 return -EINVAL;
2005
Johannes Berg4c476992010-10-04 21:36:35 +02002006 if (!rdev->ops->add_key)
2007 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002008
Johannes Berge31b8212010-10-05 19:39:30 +02002009 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2010 key.type == NL80211_KEYTYPE_PAIRWISE,
2011 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002012 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002013
2014 wdev_lock(dev->ieee80211_ptr);
2015 err = nl80211_key_allowed(dev->ieee80211_ptr);
2016 if (!err)
2017 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002018 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002019 mac_addr, &key.p);
2020 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002021
Johannes Berg41ade002007-12-19 02:03:29 +01002022 return err;
2023}
2024
2025static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2026{
Johannes Berg4c476992010-10-04 21:36:35 +02002027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002028 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002029 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002030 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002031 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002032
Johannes Bergb9454e82009-07-08 13:29:08 +02002033 err = nl80211_parse_key(info, &key);
2034 if (err)
2035 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002036
2037 if (info->attrs[NL80211_ATTR_MAC])
2038 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2039
Johannes Berge31b8212010-10-05 19:39:30 +02002040 if (key.type == -1) {
2041 if (mac_addr)
2042 key.type = NL80211_KEYTYPE_PAIRWISE;
2043 else
2044 key.type = NL80211_KEYTYPE_GROUP;
2045 }
2046
2047 /* for now */
2048 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2049 key.type != NL80211_KEYTYPE_GROUP)
2050 return -EINVAL;
2051
Johannes Berg4c476992010-10-04 21:36:35 +02002052 if (!rdev->ops->del_key)
2053 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002054
Johannes Bergfffd0932009-07-08 14:22:54 +02002055 wdev_lock(dev->ieee80211_ptr);
2056 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002057
2058 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2059 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2060 err = -ENOENT;
2061
Johannes Bergfffd0932009-07-08 14:22:54 +02002062 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002063 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2064 key.type == NL80211_KEYTYPE_PAIRWISE,
2065 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002066
Johannes Berg3d23e342009-09-29 23:27:28 +02002067#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002068 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002069 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002070 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002071 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002072 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2073 }
2074#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002075 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002076
Johannes Berg41ade002007-12-19 02:03:29 +01002077 return err;
2078}
2079
Johannes Berged1b6cc2007-12-19 02:03:32 +01002080static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2081{
2082 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2083 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02002084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2085 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002086 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002087 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02002088 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002089
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002090 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2091 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2092 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2093 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002094 return -EINVAL;
2095
Johannes Berg074ac8d2010-09-16 14:58:22 +02002096 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002097 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2098 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002099
Johannes Berg56d18932011-05-09 18:41:15 +02002100 memset(&params, 0, sizeof(params));
2101
Johannes Berged1b6cc2007-12-19 02:03:32 +01002102 switch (info->genlhdr->cmd) {
2103 case NL80211_CMD_NEW_BEACON:
2104 /* these are required for NEW_BEACON */
2105 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2106 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002107 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2108 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002109
Johannes Berg56d18932011-05-09 18:41:15 +02002110 params.interval =
2111 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2112 params.dtim_period =
2113 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2114
2115 err = cfg80211_validate_beacon_int(rdev, params.interval);
2116 if (err)
2117 return err;
2118
Jouni Malinen32e9de82011-08-10 23:53:31 +03002119 /*
2120 * In theory, some of these attributes could be required for
2121 * NEW_BEACON, but since they were not used when the command was
2122 * originally added, keep them optional for old user space
2123 * programs to work with drivers that do not need the additional
2124 * information.
2125 */
2126 if (info->attrs[NL80211_ATTR_SSID]) {
2127 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2128 params.ssid_len =
2129 nla_len(info->attrs[NL80211_ATTR_SSID]);
2130 if (params.ssid_len == 0 ||
2131 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2132 return -EINVAL;
2133 }
2134
2135 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2136 params.hidden_ssid = nla_get_u32(
2137 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2138 if (params.hidden_ssid !=
2139 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2140 params.hidden_ssid !=
2141 NL80211_HIDDEN_SSID_ZERO_LEN &&
2142 params.hidden_ssid !=
2143 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2144 return -EINVAL;
2145 }
2146
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002147 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2148
2149 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2150 params.auth_type = nla_get_u32(
2151 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2152 if (!nl80211_valid_auth_type(params.auth_type))
2153 return -EINVAL;
2154 } else
2155 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2156
2157 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2158 NL80211_MAX_NR_CIPHER_SUITES);
2159 if (err)
2160 return err;
2161
Johannes Berg79c97e92009-07-07 03:56:12 +02002162 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002163 break;
2164 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002165 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002166 break;
2167 default:
2168 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002169 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002170 }
2171
Johannes Berg4c476992010-10-04 21:36:35 +02002172 if (!call)
2173 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002174
Johannes Berged1b6cc2007-12-19 02:03:32 +01002175 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2176 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2177 params.head_len =
2178 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2179 haveinfo = 1;
2180 }
2181
2182 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2183 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2184 params.tail_len =
2185 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2186 haveinfo = 1;
2187 }
2188
Johannes Berg4c476992010-10-04 21:36:35 +02002189 if (!haveinfo)
2190 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002191
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002192 if (info->attrs[NL80211_ATTR_IE]) {
2193 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2194 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2195 }
2196
2197 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2198 params.proberesp_ies =
2199 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2200 params.proberesp_ies_len =
2201 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2202 }
2203
2204 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2205 params.assocresp_ies =
2206 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2207 params.assocresp_ies_len =
2208 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2209 }
2210
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002211 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2212 params.probe_resp =
2213 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2214 params.probe_resp_len =
2215 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2216 }
2217
Johannes Berg56d18932011-05-09 18:41:15 +02002218 err = call(&rdev->wiphy, dev, &params);
2219 if (!err && params.interval)
2220 wdev->beacon_interval = params.interval;
2221 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002222}
2223
2224static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2225{
Johannes Berg4c476992010-10-04 21:36:35 +02002226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2227 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002228 struct wireless_dev *wdev = dev->ieee80211_ptr;
2229 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002230
Johannes Berg4c476992010-10-04 21:36:35 +02002231 if (!rdev->ops->del_beacon)
2232 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002233
Johannes Berg074ac8d2010-09-16 14:58:22 +02002234 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002235 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2236 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002237
Johannes Berg56d18932011-05-09 18:41:15 +02002238 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2239 if (!err)
2240 wdev->beacon_interval = 0;
2241 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002242}
2243
Johannes Berg5727ef12007-12-19 02:03:34 +01002244static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2245 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2246 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2247 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002248 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002249 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002250};
2251
Johannes Bergeccb8e82009-05-11 21:57:56 +03002252static int parse_station_flags(struct genl_info *info,
2253 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002254{
2255 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002256 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002257 int flag;
2258
Johannes Bergeccb8e82009-05-11 21:57:56 +03002259 /*
2260 * Try parsing the new attribute first so userspace
2261 * can specify both for older kernels.
2262 */
2263 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2264 if (nla) {
2265 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002266
Johannes Bergeccb8e82009-05-11 21:57:56 +03002267 sta_flags = nla_data(nla);
2268 params->sta_flags_mask = sta_flags->mask;
2269 params->sta_flags_set = sta_flags->set;
2270 if ((params->sta_flags_mask |
2271 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2272 return -EINVAL;
2273 return 0;
2274 }
2275
2276 /* if present, parse the old attribute */
2277
2278 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002279 if (!nla)
2280 return 0;
2281
2282 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2283 nla, sta_flags_policy))
2284 return -EINVAL;
2285
Johannes Bergeccb8e82009-05-11 21:57:56 +03002286 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2287 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002288
2289 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2290 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002291 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002292
2293 return 0;
2294}
2295
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002296static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2297 int attr)
2298{
2299 struct nlattr *rate;
2300 u16 bitrate;
2301
2302 rate = nla_nest_start(msg, attr);
2303 if (!rate)
2304 goto nla_put_failure;
2305
2306 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2307 bitrate = cfg80211_calculate_bitrate(info);
2308 if (bitrate > 0)
2309 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2310
2311 if (info->flags & RATE_INFO_FLAGS_MCS)
2312 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2313 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2314 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2315 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2316 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2317
2318 nla_nest_end(msg, rate);
2319 return true;
2320
2321nla_put_failure:
2322 return false;
2323}
2324
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002325static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2326 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002327 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002328{
2329 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002330 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002331
2332 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2333 if (!hdr)
2334 return -1;
2335
2336 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2337 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2338
Johannes Bergf5ea9122009-08-07 16:17:38 +02002339 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2340
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2342 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002343 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302344 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2345 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2346 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2348 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2349 sinfo->inactive_time);
2350 if (sinfo->filled & STATION_INFO_RX_BYTES)
2351 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2352 sinfo->rx_bytes);
2353 if (sinfo->filled & STATION_INFO_TX_BYTES)
2354 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2355 sinfo->tx_bytes);
2356 if (sinfo->filled & STATION_INFO_LLID)
2357 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2358 sinfo->llid);
2359 if (sinfo->filled & STATION_INFO_PLID)
2360 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2361 sinfo->plid);
2362 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2363 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2364 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002365 if (sinfo->filled & STATION_INFO_SIGNAL)
2366 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2367 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002368 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2369 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2370 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002371 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002372 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2373 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002374 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002375 }
2376 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2377 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2378 NL80211_STA_INFO_RX_BITRATE))
2379 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002380 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002381 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2382 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2383 sinfo->rx_packets);
2384 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2385 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2386 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002387 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2388 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2389 sinfo->tx_retries);
2390 if (sinfo->filled & STATION_INFO_TX_FAILED)
2391 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2392 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002393 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2394 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2395 if (!bss_param)
2396 goto nla_put_failure;
2397
2398 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2399 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2400 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2401 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2402 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2403 NLA_PUT_FLAG(msg,
2404 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2405 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2406 sinfo->bss_param.dtim_period);
2407 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2408 sinfo->bss_param.beacon_interval);
2409
2410 nla_nest_end(msg, bss_param);
2411 }
Helmut Schaabb6e7532011-10-13 16:30:39 +02002412 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2413 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2414 sizeof(struct nl80211_sta_flag_update),
2415 &sinfo->sta_flags);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002416 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002417
Felix Fietkau040bdf72011-08-10 19:00:33 -06002418 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002419 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2420 sinfo->assoc_req_ies);
2421
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002422 return genlmsg_end(msg, hdr);
2423
2424 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002425 genlmsg_cancel(msg, hdr);
2426 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002427}
2428
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002429static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002430 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002431{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002432 struct station_info sinfo;
2433 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002434 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002435 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002436 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002437 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002438
Johannes Berg67748892010-10-04 21:14:06 +02002439 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2440 if (err)
2441 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002442
2443 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002444 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002445 goto out_err;
2446 }
2447
Johannes Bergbba95fe2008-07-29 13:22:51 +02002448 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002449 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002450 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2451 mac_addr, &sinfo);
2452 if (err == -ENOENT)
2453 break;
2454 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002455 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002456
2457 if (nl80211_send_station(skb,
2458 NETLINK_CB(cb->skb).pid,
2459 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2460 netdev, mac_addr,
2461 &sinfo) < 0)
2462 goto out;
2463
2464 sta_idx++;
2465 }
2466
2467
2468 out:
2469 cb->args[1] = sta_idx;
2470 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002471 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002472 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002473
2474 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002476
Johannes Berg5727ef12007-12-19 02:03:34 +01002477static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2478{
Johannes Berg4c476992010-10-04 21:36:35 +02002479 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2480 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002481 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002482 struct sk_buff *msg;
2483 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002484 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002485
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002486 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002487
2488 if (!info->attrs[NL80211_ATTR_MAC])
2489 return -EINVAL;
2490
2491 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2492
Johannes Berg4c476992010-10-04 21:36:35 +02002493 if (!rdev->ops->get_station)
2494 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002495
Johannes Berg79c97e92009-07-07 03:56:12 +02002496 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002497 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002498 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002499
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002500 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002501 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002502 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002503
2504 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002505 dev, mac_addr, &sinfo) < 0) {
2506 nlmsg_free(msg);
2507 return -ENOBUFS;
2508 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002509
Johannes Berg4c476992010-10-04 21:36:35 +02002510 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002511}
2512
2513/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002514 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002515 */
Johannes Berg80b99892011-11-18 16:23:01 +01002516static struct net_device *get_vlan(struct genl_info *info,
2517 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002518{
Johannes Berg463d0182009-07-14 00:33:35 +02002519 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002520 struct net_device *v;
2521 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002522
Johannes Berg80b99892011-11-18 16:23:01 +01002523 if (!vlanattr)
2524 return NULL;
2525
2526 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2527 if (!v)
2528 return ERR_PTR(-ENODEV);
2529
2530 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2531 ret = -EINVAL;
2532 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002533 }
Johannes Berg80b99892011-11-18 16:23:01 +01002534
2535 if (!netif_running(v)) {
2536 ret = -ENETDOWN;
2537 goto error;
2538 }
2539
2540 return v;
2541 error:
2542 dev_put(v);
2543 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002544}
2545
2546static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2547{
Johannes Berg4c476992010-10-04 21:36:35 +02002548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002549 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002550 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002551 struct station_parameters params;
2552 u8 *mac_addr = NULL;
2553
2554 memset(&params, 0, sizeof(params));
2555
2556 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002557 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002558
2559 if (info->attrs[NL80211_ATTR_STA_AID])
2560 return -EINVAL;
2561
2562 if (!info->attrs[NL80211_ATTR_MAC])
2563 return -EINVAL;
2564
2565 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2566
2567 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2568 params.supported_rates =
2569 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2570 params.supported_rates_len =
2571 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2572 }
2573
2574 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2575 params.listen_interval =
2576 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2577
Jouni Malinen36aedc92008-08-25 11:58:58 +03002578 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2579 params.ht_capa =
2580 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2581
Johannes Bergeccb8e82009-05-11 21:57:56 +03002582 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002583 return -EINVAL;
2584
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002585 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2586 params.plink_action =
2587 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2588
Javier Cardona9c3990a2011-05-03 16:57:11 -07002589 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2590 params.plink_state =
2591 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2592
Johannes Berg80b99892011-11-18 16:23:01 +01002593 params.vlan = get_vlan(info, rdev);
2594 if (IS_ERR(params.vlan))
2595 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002596
2597 /* validate settings */
2598 err = 0;
2599
2600 switch (dev->ieee80211_ptr->iftype) {
2601 case NL80211_IFTYPE_AP:
2602 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002603 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002604 /* disallow mesh-specific things */
2605 if (params.plink_action)
2606 err = -EINVAL;
2607 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002608 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002609 case NL80211_IFTYPE_STATION:
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002610 /* disallow things sta doesn't support */
Johannes Berga97f4422009-06-18 17:23:43 +02002611 if (params.plink_action)
2612 err = -EINVAL;
2613 if (params.vlan)
2614 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002615 if (params.supported_rates &&
2616 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002617 err = -EINVAL;
2618 if (params.ht_capa)
2619 err = -EINVAL;
2620 if (params.listen_interval >= 0)
2621 err = -EINVAL;
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002622 if (params.sta_flags_mask &
2623 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2624 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2625 err = -EINVAL;
2626 /* can't change the TDLS bit */
2627 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2628 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berga97f4422009-06-18 17:23:43 +02002629 err = -EINVAL;
2630 break;
2631 case NL80211_IFTYPE_MESH_POINT:
2632 /* disallow things mesh doesn't support */
2633 if (params.vlan)
2634 err = -EINVAL;
2635 if (params.ht_capa)
2636 err = -EINVAL;
2637 if (params.listen_interval >= 0)
2638 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002639 if (params.sta_flags_mask &
2640 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002641 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002642 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002643 err = -EINVAL;
2644 break;
2645 default:
2646 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002647 }
2648
Johannes Berg5727ef12007-12-19 02:03:34 +01002649 if (err)
2650 goto out;
2651
Johannes Berg79c97e92009-07-07 03:56:12 +02002652 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002653 err = -EOPNOTSUPP;
2654 goto out;
2655 }
2656
Johannes Berg79c97e92009-07-07 03:56:12 +02002657 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002658
2659 out:
2660 if (params.vlan)
2661 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002662
Johannes Berg5727ef12007-12-19 02:03:34 +01002663 return err;
2664}
2665
Eliad Pellerc75786c2011-08-23 14:37:46 +03002666static struct nla_policy
2667nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2668 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2669 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2670};
2671
Johannes Berg5727ef12007-12-19 02:03:34 +01002672static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2673{
Johannes Berg4c476992010-10-04 21:36:35 +02002674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002675 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002676 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002677 struct station_parameters params;
2678 u8 *mac_addr = NULL;
2679
2680 memset(&params, 0, sizeof(params));
2681
2682 if (!info->attrs[NL80211_ATTR_MAC])
2683 return -EINVAL;
2684
Johannes Berg5727ef12007-12-19 02:03:34 +01002685 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2686 return -EINVAL;
2687
2688 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2689 return -EINVAL;
2690
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002691 if (!info->attrs[NL80211_ATTR_STA_AID])
2692 return -EINVAL;
2693
Johannes Berg5727ef12007-12-19 02:03:34 +01002694 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2695 params.supported_rates =
2696 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2697 params.supported_rates_len =
2698 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2699 params.listen_interval =
2700 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002701
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002702 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2703 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2704 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002705
Jouni Malinen36aedc92008-08-25 11:58:58 +03002706 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2707 params.ht_capa =
2708 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002709
Javier Cardona96b78df2011-04-07 15:08:33 -07002710 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2711 params.plink_action =
2712 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2713
Johannes Bergeccb8e82009-05-11 21:57:56 +03002714 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002715 return -EINVAL;
2716
Eliad Pellerc75786c2011-08-23 14:37:46 +03002717 /* parse WME attributes if sta is WME capable */
Eliad Pellercedb5412011-08-31 11:29:43 +03002718 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Arik Nemtsovcd329842011-09-26 09:36:42 +03002719 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
Eliad Pellerc75786c2011-08-23 14:37:46 +03002720 info->attrs[NL80211_ATTR_STA_WME]) {
2721 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2722 struct nlattr *nla;
2723
2724 nla = info->attrs[NL80211_ATTR_STA_WME];
2725 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2726 nl80211_sta_wme_policy);
2727 if (err)
2728 return err;
2729
2730 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2731 params.uapsd_queues =
2732 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
Johannes Berg4319e192011-09-07 11:50:48 +02002733 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2734 return -EINVAL;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002735
2736 if (tb[NL80211_STA_WME_MAX_SP])
2737 params.max_sp =
2738 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
Johannes Berg4319e192011-09-07 11:50:48 +02002739
2740 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2741 return -EINVAL;
Johannes Berg3b9ce802011-09-27 20:56:12 +02002742
2743 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03002744 }
2745
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002746 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002747 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002748 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Arik Nemtsov07ba55d2011-09-28 14:12:53 +03002749 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2750 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2751 return -EINVAL;
2752
2753 /*
2754 * Only managed stations can add TDLS peers, and only when the
2755 * wiphy supports external TDLS setup.
2756 */
2757 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2758 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2759 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2760 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
Johannes Berg4c476992010-10-04 21:36:35 +02002761 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002762
Johannes Berg80b99892011-11-18 16:23:01 +01002763 params.vlan = get_vlan(info, rdev);
2764 if (IS_ERR(params.vlan))
2765 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002766
2767 /* validate settings */
2768 err = 0;
2769
Johannes Berg79c97e92009-07-07 03:56:12 +02002770 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002771 err = -EOPNOTSUPP;
2772 goto out;
2773 }
2774
Johannes Berg79c97e92009-07-07 03:56:12 +02002775 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002776
2777 out:
2778 if (params.vlan)
2779 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002780 return err;
2781}
2782
2783static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2784{
Johannes Berg4c476992010-10-04 21:36:35 +02002785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2786 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002787 u8 *mac_addr = NULL;
2788
2789 if (info->attrs[NL80211_ATTR_MAC])
2790 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2791
Johannes Berge80cf852009-05-11 14:43:13 +02002792 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002793 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002794 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002795 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2796 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002797
Johannes Berg4c476992010-10-04 21:36:35 +02002798 if (!rdev->ops->del_station)
2799 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002800
Johannes Berg4c476992010-10-04 21:36:35 +02002801 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002802}
2803
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002804static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2805 int flags, struct net_device *dev,
2806 u8 *dst, u8 *next_hop,
2807 struct mpath_info *pinfo)
2808{
2809 void *hdr;
2810 struct nlattr *pinfoattr;
2811
2812 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2813 if (!hdr)
2814 return -1;
2815
2816 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2817 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2818 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2819
Johannes Bergf5ea9122009-08-07 16:17:38 +02002820 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2821
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002822 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2823 if (!pinfoattr)
2824 goto nla_put_failure;
2825 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2826 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2827 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002828 if (pinfo->filled & MPATH_INFO_SN)
2829 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2830 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002831 if (pinfo->filled & MPATH_INFO_METRIC)
2832 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2833 pinfo->metric);
2834 if (pinfo->filled & MPATH_INFO_EXPTIME)
2835 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2836 pinfo->exptime);
2837 if (pinfo->filled & MPATH_INFO_FLAGS)
2838 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2839 pinfo->flags);
2840 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2841 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2842 pinfo->discovery_timeout);
2843 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2844 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2845 pinfo->discovery_retries);
2846
2847 nla_nest_end(msg, pinfoattr);
2848
2849 return genlmsg_end(msg, hdr);
2850
2851 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002852 genlmsg_cancel(msg, hdr);
2853 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002854}
2855
2856static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002857 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002858{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002859 struct mpath_info pinfo;
2860 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002861 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002862 u8 dst[ETH_ALEN];
2863 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002864 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002865 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002866
Johannes Berg67748892010-10-04 21:14:06 +02002867 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2868 if (err)
2869 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002870
2871 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002872 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002873 goto out_err;
2874 }
2875
Jouni Malineneec60b02009-03-20 21:21:19 +02002876 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2877 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002878 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002879 }
2880
Johannes Bergbba95fe2008-07-29 13:22:51 +02002881 while (1) {
2882 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2883 dst, next_hop, &pinfo);
2884 if (err == -ENOENT)
2885 break;
2886 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002887 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002888
2889 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2890 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2891 netdev, dst, next_hop,
2892 &pinfo) < 0)
2893 goto out;
2894
2895 path_idx++;
2896 }
2897
2898
2899 out:
2900 cb->args[1] = path_idx;
2901 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002902 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002903 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002904 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002905}
2906
2907static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2908{
Johannes Berg4c476992010-10-04 21:36:35 +02002909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002910 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002911 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002912 struct mpath_info pinfo;
2913 struct sk_buff *msg;
2914 u8 *dst = NULL;
2915 u8 next_hop[ETH_ALEN];
2916
2917 memset(&pinfo, 0, sizeof(pinfo));
2918
2919 if (!info->attrs[NL80211_ATTR_MAC])
2920 return -EINVAL;
2921
2922 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2923
Johannes Berg4c476992010-10-04 21:36:35 +02002924 if (!rdev->ops->get_mpath)
2925 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002926
Johannes Berg4c476992010-10-04 21:36:35 +02002927 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2928 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002929
Johannes Berg79c97e92009-07-07 03:56:12 +02002930 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002931 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002932 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002933
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002934 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002935 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002936 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002937
2938 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002939 dev, dst, next_hop, &pinfo) < 0) {
2940 nlmsg_free(msg);
2941 return -ENOBUFS;
2942 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002943
Johannes Berg4c476992010-10-04 21:36:35 +02002944 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002945}
2946
2947static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2948{
Johannes Berg4c476992010-10-04 21:36:35 +02002949 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2950 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002951 u8 *dst = NULL;
2952 u8 *next_hop = NULL;
2953
2954 if (!info->attrs[NL80211_ATTR_MAC])
2955 return -EINVAL;
2956
2957 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2958 return -EINVAL;
2959
2960 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2961 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2962
Johannes Berg4c476992010-10-04 21:36:35 +02002963 if (!rdev->ops->change_mpath)
2964 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002965
Johannes Berg4c476992010-10-04 21:36:35 +02002966 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2967 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002968
Johannes Berg4c476992010-10-04 21:36:35 +02002969 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002970}
Johannes Berg4c476992010-10-04 21:36:35 +02002971
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002972static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2973{
Johannes Berg4c476992010-10-04 21:36:35 +02002974 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2975 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002976 u8 *dst = NULL;
2977 u8 *next_hop = NULL;
2978
2979 if (!info->attrs[NL80211_ATTR_MAC])
2980 return -EINVAL;
2981
2982 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2983 return -EINVAL;
2984
2985 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2986 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2987
Johannes Berg4c476992010-10-04 21:36:35 +02002988 if (!rdev->ops->add_mpath)
2989 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002990
Johannes Berg4c476992010-10-04 21:36:35 +02002991 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2992 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002993
Johannes Berg4c476992010-10-04 21:36:35 +02002994 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002995}
2996
2997static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2998{
Johannes Berg4c476992010-10-04 21:36:35 +02002999 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3000 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003001 u8 *dst = NULL;
3002
3003 if (info->attrs[NL80211_ATTR_MAC])
3004 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3005
Johannes Berg4c476992010-10-04 21:36:35 +02003006 if (!rdev->ops->del_mpath)
3007 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003008
Johannes Berg4c476992010-10-04 21:36:35 +02003009 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003010}
3011
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003012static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3013{
Johannes Berg4c476992010-10-04 21:36:35 +02003014 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3015 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003016 struct bss_parameters params;
3017
3018 memset(&params, 0, sizeof(params));
3019 /* default to not changing parameters */
3020 params.use_cts_prot = -1;
3021 params.use_short_preamble = -1;
3022 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003023 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003024 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003025
3026 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3027 params.use_cts_prot =
3028 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3029 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3030 params.use_short_preamble =
3031 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3032 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3033 params.use_short_slot_time =
3034 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003035 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3036 params.basic_rates =
3037 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3038 params.basic_rates_len =
3039 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3040 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003041 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3042 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003043 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3044 params.ht_opmode =
3045 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003046
Johannes Berg4c476992010-10-04 21:36:35 +02003047 if (!rdev->ops->change_bss)
3048 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003049
Johannes Berg074ac8d2010-09-16 14:58:22 +02003050 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003051 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3052 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003053
Johannes Berg4c476992010-10-04 21:36:35 +02003054 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003055}
3056
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003057static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003058 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3059 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3060 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3061 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3062 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3063 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3064};
3065
3066static int parse_reg_rule(struct nlattr *tb[],
3067 struct ieee80211_reg_rule *reg_rule)
3068{
3069 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3070 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3071
3072 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3073 return -EINVAL;
3074 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3075 return -EINVAL;
3076 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3077 return -EINVAL;
3078 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3079 return -EINVAL;
3080 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3081 return -EINVAL;
3082
3083 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3084
3085 freq_range->start_freq_khz =
3086 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3087 freq_range->end_freq_khz =
3088 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3089 freq_range->max_bandwidth_khz =
3090 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3091
3092 power_rule->max_eirp =
3093 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3094
3095 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3096 power_rule->max_antenna_gain =
3097 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3098
3099 return 0;
3100}
3101
3102static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3103{
3104 int r;
3105 char *data = NULL;
3106
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003107 /*
3108 * You should only get this when cfg80211 hasn't yet initialized
3109 * completely when built-in to the kernel right between the time
3110 * window between nl80211_init() and regulatory_init(), if that is
3111 * even possible.
3112 */
3113 mutex_lock(&cfg80211_mutex);
3114 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003115 mutex_unlock(&cfg80211_mutex);
3116 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003117 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003118 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003119
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003120 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3121 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003122
3123 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3124
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003125 r = regulatory_hint_user(data);
3126
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003127 return r;
3128}
3129
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003130static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003131 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003132{
Johannes Berg4c476992010-10-04 21:36:35 +02003133 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003134 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003135 struct wireless_dev *wdev = dev->ieee80211_ptr;
3136 struct mesh_config cur_params;
3137 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003138 void *hdr;
3139 struct nlattr *pinfoattr;
3140 struct sk_buff *msg;
3141
Johannes Berg29cbe682010-12-03 09:20:44 +01003142 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3143 return -EOPNOTSUPP;
3144
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003145 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003146 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003147
Johannes Berg29cbe682010-12-03 09:20:44 +01003148 wdev_lock(wdev);
3149 /* If not connected, get default parameters */
3150 if (!wdev->mesh_id_len)
3151 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3152 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003153 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003154 &cur_params);
3155 wdev_unlock(wdev);
3156
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003157 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003158 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003159
3160 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003161 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003162 if (!msg)
3163 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003164 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003165 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003166 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003167 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003168 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003169 if (!pinfoattr)
3170 goto nla_put_failure;
3171 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3172 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3173 cur_params.dot11MeshRetryTimeout);
3174 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3175 cur_params.dot11MeshConfirmTimeout);
3176 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3177 cur_params.dot11MeshHoldingTimeout);
3178 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3179 cur_params.dot11MeshMaxPeerLinks);
3180 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3181 cur_params.dot11MeshMaxRetries);
3182 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3183 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003184 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3185 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003186 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3187 cur_params.auto_open_plinks);
3188 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3189 cur_params.dot11MeshHWMPmaxPREQretries);
3190 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3191 cur_params.path_refresh_time);
3192 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3193 cur_params.min_discovery_timeout);
3194 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3195 cur_params.dot11MeshHWMPactivePathTimeout);
3196 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3197 cur_params.dot11MeshHWMPpreqMinInterval);
3198 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3199 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003200 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3201 cur_params.dot11MeshHWMPRootMode);
Javier Cardona0507e152011-08-09 16:45:10 -07003202 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3203 cur_params.dot11MeshHWMPRannInterval);
Javier Cardona16dd7262011-08-09 16:45:11 -07003204 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3205 cur_params.dot11MeshGateAnnouncementProtocol);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003206 nla_nest_end(msg, pinfoattr);
3207 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003208 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003209
Johannes Berg3b858752009-03-12 09:55:09 +01003210 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003211 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003212 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003213 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003214 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003215}
3216
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003217static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003218 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3219 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3220 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3221 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3222 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3223 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003224 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003225 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3226
3227 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3228 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3229 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3230 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3231 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3232 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003233 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003234 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003235 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003236};
3237
Javier Cardonac80d5452010-12-16 17:37:49 -08003238static const struct nla_policy
3239 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3240 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3241 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003242 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003243 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003244 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003245 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003246};
3247
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003248static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003249 struct mesh_config *cfg,
3250 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003251{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003252 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003253 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003254
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003255#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3256do {\
3257 if (table[attr_num]) {\
3258 cfg->param = nla_fn(table[attr_num]); \
3259 mask |= (1 << (attr_num - 1)); \
3260 } \
3261} while (0);\
3262
3263
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003264 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003265 return -EINVAL;
3266 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003267 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003268 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003269 return -EINVAL;
3270
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003271 /* This makes sure that there aren't more than 32 mesh config
3272 * parameters (otherwise our bitfield scheme would not work.) */
3273 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3274
3275 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003276 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3277 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3278 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3279 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3280 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3281 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3282 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3283 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3284 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3285 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3286 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3287 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003288 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3289 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003290 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3291 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3292 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3293 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3294 nla_get_u8);
3295 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3296 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3297 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3298 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3299 nla_get_u16);
3300 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3301 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3302 nla_get_u32);
3303 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3304 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3305 nla_get_u16);
3306 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3307 dot11MeshHWMPnetDiameterTraversalTime,
3308 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3309 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003310 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3311 dot11MeshHWMPRootMode, mask,
3312 NL80211_MESHCONF_HWMP_ROOTMODE,
3313 nla_get_u8);
Javier Cardona0507e152011-08-09 16:45:10 -07003314 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3315 dot11MeshHWMPRannInterval, mask,
3316 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3317 nla_get_u16);
Javier Cardona16dd7262011-08-09 16:45:11 -07003318 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3319 dot11MeshGateAnnouncementProtocol, mask,
3320 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3321 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003322 if (mask_out)
3323 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003324
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003325 return 0;
3326
3327#undef FILL_IN_MESH_PARAM_IF_SET
3328}
3329
Javier Cardonac80d5452010-12-16 17:37:49 -08003330static int nl80211_parse_mesh_setup(struct genl_info *info,
3331 struct mesh_setup *setup)
3332{
3333 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3334
3335 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3336 return -EINVAL;
3337 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3338 info->attrs[NL80211_ATTR_MESH_SETUP],
3339 nl80211_mesh_setup_params_policy))
3340 return -EINVAL;
3341
3342 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3343 setup->path_sel_proto =
3344 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3345 IEEE80211_PATH_PROTOCOL_VENDOR :
3346 IEEE80211_PATH_PROTOCOL_HWMP;
3347
3348 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3349 setup->path_metric =
3350 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3351 IEEE80211_PATH_METRIC_VENDOR :
3352 IEEE80211_PATH_METRIC_AIRTIME;
3353
Javier Cardona581a8b02011-04-07 15:08:27 -07003354
3355 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003356 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003357 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003358 if (!is_valid_ie_attr(ieattr))
3359 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003360 setup->ie = nla_data(ieattr);
3361 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003362 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003363 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3364 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003365
3366 return 0;
3367}
3368
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003369static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003370 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003371{
3372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3373 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003374 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003375 struct mesh_config cfg;
3376 u32 mask;
3377 int err;
3378
Johannes Berg29cbe682010-12-03 09:20:44 +01003379 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3380 return -EOPNOTSUPP;
3381
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003382 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003383 return -EOPNOTSUPP;
3384
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003385 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003386 if (err)
3387 return err;
3388
Johannes Berg29cbe682010-12-03 09:20:44 +01003389 wdev_lock(wdev);
3390 if (!wdev->mesh_id_len)
3391 err = -ENOLINK;
3392
3393 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003394 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003395 mask, &cfg);
3396
3397 wdev_unlock(wdev);
3398
3399 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003400}
3401
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003402static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3403{
3404 struct sk_buff *msg;
3405 void *hdr = NULL;
3406 struct nlattr *nl_reg_rules;
3407 unsigned int i;
3408 int err = -EINVAL;
3409
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003410 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003411
3412 if (!cfg80211_regdomain)
3413 goto out;
3414
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003416 if (!msg) {
3417 err = -ENOBUFS;
3418 goto out;
3419 }
3420
3421 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3422 NL80211_CMD_GET_REG);
3423 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003424 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003425
3426 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3427 cfg80211_regdomain->alpha2);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003428 if (cfg80211_regdomain->dfs_region)
3429 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3430 cfg80211_regdomain->dfs_region);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003431
3432 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3433 if (!nl_reg_rules)
3434 goto nla_put_failure;
3435
3436 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3437 struct nlattr *nl_reg_rule;
3438 const struct ieee80211_reg_rule *reg_rule;
3439 const struct ieee80211_freq_range *freq_range;
3440 const struct ieee80211_power_rule *power_rule;
3441
3442 reg_rule = &cfg80211_regdomain->reg_rules[i];
3443 freq_range = &reg_rule->freq_range;
3444 power_rule = &reg_rule->power_rule;
3445
3446 nl_reg_rule = nla_nest_start(msg, i);
3447 if (!nl_reg_rule)
3448 goto nla_put_failure;
3449
3450 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3451 reg_rule->flags);
3452 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3453 freq_range->start_freq_khz);
3454 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3455 freq_range->end_freq_khz);
3456 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3457 freq_range->max_bandwidth_khz);
3458 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3459 power_rule->max_antenna_gain);
3460 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3461 power_rule->max_eirp);
3462
3463 nla_nest_end(msg, nl_reg_rule);
3464 }
3465
3466 nla_nest_end(msg, nl_reg_rules);
3467
3468 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003469 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003470 goto out;
3471
3472nla_put_failure:
3473 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003474put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003475 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003476 err = -EMSGSIZE;
3477out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003478 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003479 return err;
3480}
3481
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003482static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3483{
3484 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3485 struct nlattr *nl_reg_rule;
3486 char *alpha2 = NULL;
3487 int rem_reg_rules = 0, r = 0;
3488 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003489 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003490 struct ieee80211_regdomain *rd = NULL;
3491
3492 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3493 return -EINVAL;
3494
3495 if (!info->attrs[NL80211_ATTR_REG_RULES])
3496 return -EINVAL;
3497
3498 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3499
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003500 if (info->attrs[NL80211_ATTR_DFS_REGION])
3501 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3502
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003503 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3504 rem_reg_rules) {
3505 num_rules++;
3506 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003507 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003508 }
3509
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003510 mutex_lock(&cfg80211_mutex);
3511
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003512 if (!reg_is_valid_request(alpha2)) {
3513 r = -EINVAL;
3514 goto bad_reg;
3515 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003516
3517 size_of_regd = sizeof(struct ieee80211_regdomain) +
3518 (num_rules * sizeof(struct ieee80211_reg_rule));
3519
3520 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003521 if (!rd) {
3522 r = -ENOMEM;
3523 goto bad_reg;
3524 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003525
3526 rd->n_reg_rules = num_rules;
3527 rd->alpha2[0] = alpha2[0];
3528 rd->alpha2[1] = alpha2[1];
3529
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003530 /*
3531 * Disable DFS master mode if the DFS region was
3532 * not supported or known on this kernel.
3533 */
3534 if (reg_supported_dfs_region(dfs_region))
3535 rd->dfs_region = dfs_region;
3536
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003537 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3538 rem_reg_rules) {
3539 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3540 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3541 reg_rule_policy);
3542 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3543 if (r)
3544 goto bad_reg;
3545
3546 rule_idx++;
3547
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003548 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3549 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003550 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003551 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003552 }
3553
3554 BUG_ON(rule_idx != num_rules);
3555
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003556 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003557
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003558 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003559
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003560 return r;
3561
Johannes Bergd2372b32008-10-24 20:32:20 +02003562 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003563 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003564 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003565 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003566}
3567
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003568static int validate_scan_freqs(struct nlattr *freqs)
3569{
3570 struct nlattr *attr1, *attr2;
3571 int n_channels = 0, tmp1, tmp2;
3572
3573 nla_for_each_nested(attr1, freqs, tmp1) {
3574 n_channels++;
3575 /*
3576 * Some hardware has a limited channel list for
3577 * scanning, and it is pretty much nonsensical
3578 * to scan for a channel twice, so disallow that
3579 * and don't require drivers to check that the
3580 * channel list they get isn't longer than what
3581 * they can scan, as long as they can scan all
3582 * the channels they registered at once.
3583 */
3584 nla_for_each_nested(attr2, freqs, tmp2)
3585 if (attr1 != attr2 &&
3586 nla_get_u32(attr1) == nla_get_u32(attr2))
3587 return 0;
3588 }
3589
3590 return n_channels;
3591}
3592
Johannes Berg2a519312009-02-10 21:25:55 +01003593static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3594{
Johannes Berg4c476992010-10-04 21:36:35 +02003595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3596 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003597 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003598 struct nlattr *attr;
3599 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003600 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003601 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003602
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003603 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3604 return -EINVAL;
3605
Johannes Berg79c97e92009-07-07 03:56:12 +02003606 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003607
Johannes Berg4c476992010-10-04 21:36:35 +02003608 if (!rdev->ops->scan)
3609 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003610
Johannes Berg4c476992010-10-04 21:36:35 +02003611 if (rdev->scan_req)
3612 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003613
3614 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003615 n_channels = validate_scan_freqs(
3616 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003617 if (!n_channels)
3618 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003619 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003620 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003621 n_channels = 0;
3622
Johannes Berg2a519312009-02-10 21:25:55 +01003623 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3624 if (wiphy->bands[band])
3625 n_channels += wiphy->bands[band]->n_channels;
3626 }
3627
3628 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3629 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3630 n_ssids++;
3631
Johannes Berg4c476992010-10-04 21:36:35 +02003632 if (n_ssids > wiphy->max_scan_ssids)
3633 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003634
Jouni Malinen70692ad2009-02-16 19:39:13 +02003635 if (info->attrs[NL80211_ATTR_IE])
3636 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3637 else
3638 ie_len = 0;
3639
Johannes Berg4c476992010-10-04 21:36:35 +02003640 if (ie_len > wiphy->max_scan_ie_len)
3641 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003642
Johannes Berg2a519312009-02-10 21:25:55 +01003643 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003644 + sizeof(*request->ssids) * n_ssids
3645 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003646 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003647 if (!request)
3648 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003649
Johannes Berg2a519312009-02-10 21:25:55 +01003650 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003651 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003652 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003653 if (ie_len) {
3654 if (request->ssids)
3655 request->ie = (void *)(request->ssids + n_ssids);
3656 else
3657 request->ie = (void *)(request->channels + n_channels);
3658 }
Johannes Berg2a519312009-02-10 21:25:55 +01003659
Johannes Berg584991d2009-11-02 13:32:03 +01003660 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003661 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3662 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003663 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003664 struct ieee80211_channel *chan;
3665
3666 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3667
3668 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003669 err = -EINVAL;
3670 goto out_free;
3671 }
Johannes Berg584991d2009-11-02 13:32:03 +01003672
3673 /* ignore disabled channels */
3674 if (chan->flags & IEEE80211_CHAN_DISABLED)
3675 continue;
3676
3677 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003678 i++;
3679 }
3680 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003681 enum ieee80211_band band;
3682
Johannes Berg2a519312009-02-10 21:25:55 +01003683 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003684 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3685 int j;
3686 if (!wiphy->bands[band])
3687 continue;
3688 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003689 struct ieee80211_channel *chan;
3690
3691 chan = &wiphy->bands[band]->channels[j];
3692
3693 if (chan->flags & IEEE80211_CHAN_DISABLED)
3694 continue;
3695
3696 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003697 i++;
3698 }
3699 }
3700 }
3701
Johannes Berg584991d2009-11-02 13:32:03 +01003702 if (!i) {
3703 err = -EINVAL;
3704 goto out_free;
3705 }
3706
3707 request->n_channels = i;
3708
Johannes Berg2a519312009-02-10 21:25:55 +01003709 i = 0;
3710 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3711 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003712 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003713 err = -EINVAL;
3714 goto out_free;
3715 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003716 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003717 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003718 i++;
3719 }
3720 }
3721
Jouni Malinen70692ad2009-02-16 19:39:13 +02003722 if (info->attrs[NL80211_ATTR_IE]) {
3723 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003724 memcpy((void *)request->ie,
3725 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003726 request->ie_len);
3727 }
3728
Johannes Berg34850ab2011-07-18 18:08:35 +02003729 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003730 if (wiphy->bands[i])
3731 request->rates[i] =
3732 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003733
3734 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3735 nla_for_each_nested(attr,
3736 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3737 tmp) {
3738 enum ieee80211_band band = nla_type(attr);
3739
Dan Carpenter84404622011-07-29 11:52:18 +03003740 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003741 err = -EINVAL;
3742 goto out_free;
3743 }
3744 err = ieee80211_get_ratemask(wiphy->bands[band],
3745 nla_data(attr),
3746 nla_len(attr),
3747 &request->rates[band]);
3748 if (err)
3749 goto out_free;
3750 }
3751 }
3752
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05303753 request->no_cck =
3754 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3755
Johannes Berg463d0182009-07-14 00:33:35 +02003756 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003757 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003758
Johannes Berg79c97e92009-07-07 03:56:12 +02003759 rdev->scan_req = request;
3760 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003761
Johannes Berg463d0182009-07-14 00:33:35 +02003762 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003763 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003764 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003765 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003766 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003767 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003768 kfree(request);
3769 }
Johannes Berg3b858752009-03-12 09:55:09 +01003770
Johannes Berg2a519312009-02-10 21:25:55 +01003771 return err;
3772}
3773
Luciano Coelho807f8a82011-05-11 17:09:35 +03003774static int nl80211_start_sched_scan(struct sk_buff *skb,
3775 struct genl_info *info)
3776{
3777 struct cfg80211_sched_scan_request *request;
3778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3779 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003780 struct nlattr *attr;
3781 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003782 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003783 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003784 enum ieee80211_band band;
3785 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003786 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003787
3788 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3789 !rdev->ops->sched_scan_start)
3790 return -EOPNOTSUPP;
3791
3792 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3793 return -EINVAL;
3794
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003795 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3796 return -EINVAL;
3797
3798 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3799 if (interval == 0)
3800 return -EINVAL;
3801
Luciano Coelho807f8a82011-05-11 17:09:35 +03003802 wiphy = &rdev->wiphy;
3803
3804 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3805 n_channels = validate_scan_freqs(
3806 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3807 if (!n_channels)
3808 return -EINVAL;
3809 } else {
3810 n_channels = 0;
3811
3812 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3813 if (wiphy->bands[band])
3814 n_channels += wiphy->bands[band]->n_channels;
3815 }
3816
3817 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3818 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3819 tmp)
3820 n_ssids++;
3821
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003822 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003823 return -EINVAL;
3824
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003825 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3826 nla_for_each_nested(attr,
3827 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3828 tmp)
3829 n_match_sets++;
3830
3831 if (n_match_sets > wiphy->max_match_sets)
3832 return -EINVAL;
3833
Luciano Coelho807f8a82011-05-11 17:09:35 +03003834 if (info->attrs[NL80211_ATTR_IE])
3835 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3836 else
3837 ie_len = 0;
3838
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003839 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003840 return -EINVAL;
3841
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003842 mutex_lock(&rdev->sched_scan_mtx);
3843
3844 if (rdev->sched_scan_req) {
3845 err = -EINPROGRESS;
3846 goto out;
3847 }
3848
Luciano Coelho807f8a82011-05-11 17:09:35 +03003849 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003850 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003851 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003852 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003853 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003854 if (!request) {
3855 err = -ENOMEM;
3856 goto out;
3857 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003858
3859 if (n_ssids)
3860 request->ssids = (void *)&request->channels[n_channels];
3861 request->n_ssids = n_ssids;
3862 if (ie_len) {
3863 if (request->ssids)
3864 request->ie = (void *)(request->ssids + n_ssids);
3865 else
3866 request->ie = (void *)(request->channels + n_channels);
3867 }
3868
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003869 if (n_match_sets) {
3870 if (request->ie)
3871 request->match_sets = (void *)(request->ie + ie_len);
3872 else if (request->ssids)
3873 request->match_sets =
3874 (void *)(request->ssids + n_ssids);
3875 else
3876 request->match_sets =
3877 (void *)(request->channels + n_channels);
3878 }
3879 request->n_match_sets = n_match_sets;
3880
Luciano Coelho807f8a82011-05-11 17:09:35 +03003881 i = 0;
3882 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3883 /* user specified, bail out if channel not found */
3884 nla_for_each_nested(attr,
3885 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3886 tmp) {
3887 struct ieee80211_channel *chan;
3888
3889 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3890
3891 if (!chan) {
3892 err = -EINVAL;
3893 goto out_free;
3894 }
3895
3896 /* ignore disabled channels */
3897 if (chan->flags & IEEE80211_CHAN_DISABLED)
3898 continue;
3899
3900 request->channels[i] = chan;
3901 i++;
3902 }
3903 } else {
3904 /* all channels */
3905 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3906 int j;
3907 if (!wiphy->bands[band])
3908 continue;
3909 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3910 struct ieee80211_channel *chan;
3911
3912 chan = &wiphy->bands[band]->channels[j];
3913
3914 if (chan->flags & IEEE80211_CHAN_DISABLED)
3915 continue;
3916
3917 request->channels[i] = chan;
3918 i++;
3919 }
3920 }
3921 }
3922
3923 if (!i) {
3924 err = -EINVAL;
3925 goto out_free;
3926 }
3927
3928 request->n_channels = i;
3929
3930 i = 0;
3931 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3932 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3933 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003934 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003935 err = -EINVAL;
3936 goto out_free;
3937 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003938 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003939 memcpy(request->ssids[i].ssid, nla_data(attr),
3940 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003941 i++;
3942 }
3943 }
3944
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03003945 i = 0;
3946 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3947 nla_for_each_nested(attr,
3948 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3949 tmp) {
3950 struct nlattr *ssid;
3951
3952 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3953 nla_data(attr), nla_len(attr),
3954 nl80211_match_policy);
3955 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3956 if (ssid) {
3957 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3958 err = -EINVAL;
3959 goto out_free;
3960 }
3961 memcpy(request->match_sets[i].ssid.ssid,
3962 nla_data(ssid), nla_len(ssid));
3963 request->match_sets[i].ssid.ssid_len =
3964 nla_len(ssid);
3965 }
3966 i++;
3967 }
3968 }
3969
Luciano Coelho807f8a82011-05-11 17:09:35 +03003970 if (info->attrs[NL80211_ATTR_IE]) {
3971 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3972 memcpy((void *)request->ie,
3973 nla_data(info->attrs[NL80211_ATTR_IE]),
3974 request->ie_len);
3975 }
3976
3977 request->dev = dev;
3978 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003979 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003980
3981 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3982 if (!err) {
3983 rdev->sched_scan_req = request;
3984 nl80211_send_sched_scan(rdev, dev,
3985 NL80211_CMD_START_SCHED_SCAN);
3986 goto out;
3987 }
3988
3989out_free:
3990 kfree(request);
3991out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003992 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003993 return err;
3994}
3995
3996static int nl80211_stop_sched_scan(struct sk_buff *skb,
3997 struct genl_info *info)
3998{
3999 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004000 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004001
4002 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4003 !rdev->ops->sched_scan_stop)
4004 return -EOPNOTSUPP;
4005
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004006 mutex_lock(&rdev->sched_scan_mtx);
4007 err = __cfg80211_stop_sched_scan(rdev, false);
4008 mutex_unlock(&rdev->sched_scan_mtx);
4009
4010 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004011}
4012
Johannes Berg9720bb32011-06-21 09:45:33 +02004013static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4014 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004015 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004016 struct wireless_dev *wdev,
4017 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004018{
Johannes Berg48ab9052009-07-10 18:42:31 +02004019 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004020 void *hdr;
4021 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004022 int i;
4023
4024 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004025
Johannes Berg9720bb32011-06-21 09:45:33 +02004026 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004027 NL80211_CMD_NEW_SCAN_RESULTS);
4028 if (!hdr)
4029 return -1;
4030
Johannes Berg9720bb32011-06-21 09:45:33 +02004031 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4032
Johannes Bergf5ea9122009-08-07 16:17:38 +02004033 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02004034 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01004035
4036 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4037 if (!bss)
4038 goto nla_put_failure;
4039 if (!is_zero_ether_addr(res->bssid))
4040 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4041 if (res->information_elements && res->len_information_elements)
4042 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4043 res->len_information_elements,
4044 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02004045 if (res->beacon_ies && res->len_beacon_ies &&
4046 res->beacon_ies != res->information_elements)
4047 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4048 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01004049 if (res->tsf)
4050 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4051 if (res->beacon_interval)
4052 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4053 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4054 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02004055 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4056 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01004057
Johannes Berg77965c92009-02-18 18:45:06 +01004058 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004059 case CFG80211_SIGNAL_TYPE_MBM:
4060 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4061 break;
4062 case CFG80211_SIGNAL_TYPE_UNSPEC:
4063 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4064 break;
4065 default:
4066 break;
4067 }
4068
Johannes Berg48ab9052009-07-10 18:42:31 +02004069 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004070 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004071 case NL80211_IFTYPE_STATION:
4072 if (intbss == wdev->current_bss)
4073 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4074 NL80211_BSS_STATUS_ASSOCIATED);
4075 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4076 if (intbss != wdev->auth_bsses[i])
4077 continue;
4078 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4079 NL80211_BSS_STATUS_AUTHENTICATED);
4080 break;
4081 }
4082 break;
4083 case NL80211_IFTYPE_ADHOC:
4084 if (intbss == wdev->current_bss)
4085 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4086 NL80211_BSS_STATUS_IBSS_JOINED);
4087 break;
4088 default:
4089 break;
4090 }
4091
Johannes Berg2a519312009-02-10 21:25:55 +01004092 nla_nest_end(msg, bss);
4093
4094 return genlmsg_end(msg, hdr);
4095
4096 nla_put_failure:
4097 genlmsg_cancel(msg, hdr);
4098 return -EMSGSIZE;
4099}
4100
4101static int nl80211_dump_scan(struct sk_buff *skb,
4102 struct netlink_callback *cb)
4103{
Johannes Berg48ab9052009-07-10 18:42:31 +02004104 struct cfg80211_registered_device *rdev;
4105 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004106 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004107 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004108 int start = cb->args[1], idx = 0;
4109 int err;
4110
Johannes Berg67748892010-10-04 21:14:06 +02004111 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4112 if (err)
4113 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004114
Johannes Berg48ab9052009-07-10 18:42:31 +02004115 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004116
Johannes Berg48ab9052009-07-10 18:42:31 +02004117 wdev_lock(wdev);
4118 spin_lock_bh(&rdev->bss_lock);
4119 cfg80211_bss_expire(rdev);
4120
Johannes Berg9720bb32011-06-21 09:45:33 +02004121 cb->seq = rdev->bss_generation;
4122
Johannes Berg48ab9052009-07-10 18:42:31 +02004123 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004124 if (++idx <= start)
4125 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004126 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004127 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004128 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004129 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004130 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004131 }
4132 }
4133
Johannes Berg48ab9052009-07-10 18:42:31 +02004134 spin_unlock_bh(&rdev->bss_lock);
4135 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004136
4137 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004138 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004139
Johannes Berg67748892010-10-04 21:14:06 +02004140 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004141}
4142
Holger Schurig61fa7132009-11-11 12:25:40 +01004143static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4144 int flags, struct net_device *dev,
4145 struct survey_info *survey)
4146{
4147 void *hdr;
4148 struct nlattr *infoattr;
4149
Holger Schurig61fa7132009-11-11 12:25:40 +01004150 hdr = nl80211hdr_put(msg, pid, seq, flags,
4151 NL80211_CMD_NEW_SURVEY_RESULTS);
4152 if (!hdr)
4153 return -ENOMEM;
4154
4155 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4156
4157 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4158 if (!infoattr)
4159 goto nla_put_failure;
4160
4161 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4162 survey->channel->center_freq);
4163 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4164 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4165 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02004166 if (survey->filled & SURVEY_INFO_IN_USE)
4167 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02004168 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4169 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4170 survey->channel_time);
4171 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4172 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4173 survey->channel_time_busy);
4174 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4175 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4176 survey->channel_time_ext_busy);
4177 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4178 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4179 survey->channel_time_rx);
4180 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4181 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4182 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01004183
4184 nla_nest_end(msg, infoattr);
4185
4186 return genlmsg_end(msg, hdr);
4187
4188 nla_put_failure:
4189 genlmsg_cancel(msg, hdr);
4190 return -EMSGSIZE;
4191}
4192
4193static int nl80211_dump_survey(struct sk_buff *skb,
4194 struct netlink_callback *cb)
4195{
4196 struct survey_info survey;
4197 struct cfg80211_registered_device *dev;
4198 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004199 int survey_idx = cb->args[1];
4200 int res;
4201
Johannes Berg67748892010-10-04 21:14:06 +02004202 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4203 if (res)
4204 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004205
4206 if (!dev->ops->dump_survey) {
4207 res = -EOPNOTSUPP;
4208 goto out_err;
4209 }
4210
4211 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004212 struct ieee80211_channel *chan;
4213
Holger Schurig61fa7132009-11-11 12:25:40 +01004214 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4215 &survey);
4216 if (res == -ENOENT)
4217 break;
4218 if (res)
4219 goto out_err;
4220
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004221 /* Survey without a channel doesn't make sense */
4222 if (!survey.channel) {
4223 res = -EINVAL;
4224 goto out;
4225 }
4226
4227 chan = ieee80211_get_channel(&dev->wiphy,
4228 survey.channel->center_freq);
4229 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4230 survey_idx++;
4231 continue;
4232 }
4233
Holger Schurig61fa7132009-11-11 12:25:40 +01004234 if (nl80211_send_survey(skb,
4235 NETLINK_CB(cb->skb).pid,
4236 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4237 netdev,
4238 &survey) < 0)
4239 goto out;
4240 survey_idx++;
4241 }
4242
4243 out:
4244 cb->args[1] = survey_idx;
4245 res = skb->len;
4246 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004247 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004248 return res;
4249}
4250
Jouni Malinen255e7372009-03-20 21:21:17 +02004251static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4252{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004253 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004254}
4255
Samuel Ortizb23aa672009-07-01 21:26:54 +02004256static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4257{
4258 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4259 NL80211_WPA_VERSION_2));
4260}
4261
Jouni Malinen636a5d32009-03-19 13:39:22 +02004262static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4263{
Johannes Berg4c476992010-10-04 21:36:35 +02004264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4265 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004266 struct ieee80211_channel *chan;
4267 const u8 *bssid, *ssid, *ie = NULL;
4268 int err, ssid_len, ie_len = 0;
4269 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004270 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004271 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004272
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004273 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4274 return -EINVAL;
4275
4276 if (!info->attrs[NL80211_ATTR_MAC])
4277 return -EINVAL;
4278
Jouni Malinen17780922009-03-27 20:52:47 +02004279 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4280 return -EINVAL;
4281
Johannes Berg19957bb2009-07-02 17:20:43 +02004282 if (!info->attrs[NL80211_ATTR_SSID])
4283 return -EINVAL;
4284
4285 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4286 return -EINVAL;
4287
Johannes Bergfffd0932009-07-08 14:22:54 +02004288 err = nl80211_parse_key(info, &key);
4289 if (err)
4290 return err;
4291
4292 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004293 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4294 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004295 if (!key.p.key || !key.p.key_len)
4296 return -EINVAL;
4297 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4298 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4299 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4300 key.p.key_len != WLAN_KEY_LEN_WEP104))
4301 return -EINVAL;
4302 if (key.idx > 4)
4303 return -EINVAL;
4304 } else {
4305 key.p.key_len = 0;
4306 key.p.key = NULL;
4307 }
4308
Johannes Bergafea0b72010-08-10 09:46:42 +02004309 if (key.idx >= 0) {
4310 int i;
4311 bool ok = false;
4312 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4313 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4314 ok = true;
4315 break;
4316 }
4317 }
Johannes Berg4c476992010-10-04 21:36:35 +02004318 if (!ok)
4319 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004320 }
4321
Johannes Berg4c476992010-10-04 21:36:35 +02004322 if (!rdev->ops->auth)
4323 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004324
Johannes Berg074ac8d2010-09-16 14:58:22 +02004325 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004326 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4327 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004328
Johannes Berg19957bb2009-07-02 17:20:43 +02004329 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004330 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004331 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004332 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4333 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004334
Johannes Berg19957bb2009-07-02 17:20:43 +02004335 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4336 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4337
4338 if (info->attrs[NL80211_ATTR_IE]) {
4339 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4340 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4341 }
4342
4343 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004344 if (!nl80211_valid_auth_type(auth_type))
4345 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004346
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004347 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4348
Johannes Berg4c476992010-10-04 21:36:35 +02004349 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4350 ssid, ssid_len, ie, ie_len,
4351 key.p.key, key.p.key_len, key.idx,
4352 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004353}
4354
Johannes Bergc0692b82010-08-27 14:26:53 +03004355static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4356 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004357 struct cfg80211_crypto_settings *settings,
4358 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004359{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004360 memset(settings, 0, sizeof(*settings));
4361
Samuel Ortizb23aa672009-07-01 21:26:54 +02004362 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4363
Johannes Bergc0692b82010-08-27 14:26:53 +03004364 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4365 u16 proto;
4366 proto = nla_get_u16(
4367 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4368 settings->control_port_ethertype = cpu_to_be16(proto);
4369 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4370 proto != ETH_P_PAE)
4371 return -EINVAL;
4372 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4373 settings->control_port_no_encrypt = true;
4374 } else
4375 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4376
Samuel Ortizb23aa672009-07-01 21:26:54 +02004377 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4378 void *data;
4379 int len, i;
4380
4381 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4382 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4383 settings->n_ciphers_pairwise = len / sizeof(u32);
4384
4385 if (len % sizeof(u32))
4386 return -EINVAL;
4387
Johannes Berg3dc27d22009-07-02 21:36:37 +02004388 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004389 return -EINVAL;
4390
4391 memcpy(settings->ciphers_pairwise, data, len);
4392
4393 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004394 if (!cfg80211_supported_cipher_suite(
4395 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004396 settings->ciphers_pairwise[i]))
4397 return -EINVAL;
4398 }
4399
4400 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4401 settings->cipher_group =
4402 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004403 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4404 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004405 return -EINVAL;
4406 }
4407
4408 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4409 settings->wpa_versions =
4410 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4411 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4412 return -EINVAL;
4413 }
4414
4415 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4416 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004417 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004418
4419 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4420 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4421 settings->n_akm_suites = len / sizeof(u32);
4422
4423 if (len % sizeof(u32))
4424 return -EINVAL;
4425
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004426 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4427 return -EINVAL;
4428
Samuel Ortizb23aa672009-07-01 21:26:54 +02004429 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004430 }
4431
4432 return 0;
4433}
4434
Jouni Malinen636a5d32009-03-19 13:39:22 +02004435static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4436{
Johannes Berg4c476992010-10-04 21:36:35 +02004437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4438 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004439 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004440 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004441 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004442 int err, ssid_len, ie_len = 0;
4443 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004444 u32 flags = 0;
4445 struct ieee80211_ht_cap *ht_capa = NULL;
4446 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004447
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004448 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4449 return -EINVAL;
4450
4451 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004452 !info->attrs[NL80211_ATTR_SSID] ||
4453 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004454 return -EINVAL;
4455
Johannes Berg4c476992010-10-04 21:36:35 +02004456 if (!rdev->ops->assoc)
4457 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004458
Johannes Berg074ac8d2010-09-16 14:58:22 +02004459 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004460 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4461 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004462
Johannes Berg19957bb2009-07-02 17:20:43 +02004463 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004464
Johannes Berg19957bb2009-07-02 17:20:43 +02004465 chan = ieee80211_get_channel(&rdev->wiphy,
4466 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004467 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4468 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004469
Johannes Berg19957bb2009-07-02 17:20:43 +02004470 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4471 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004472
4473 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004474 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4475 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004476 }
4477
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004478 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004479 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004480 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004481 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004482 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004483 else if (mfp != NL80211_MFP_NO)
4484 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004485 }
4486
Johannes Berg3e5d7642009-07-07 14:37:26 +02004487 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4488 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4489
Ben Greear7e7c8922011-11-18 11:31:59 -08004490 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4491 flags |= ASSOC_REQ_DISABLE_HT;
4492
4493 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4494 ht_capa_mask =
4495 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4496
4497 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4498 if (!ht_capa_mask)
4499 return -EINVAL;
4500 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4501 }
4502
Johannes Bergc0692b82010-08-27 14:26:53 +03004503 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004504 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004505 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4506 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08004507 &crypto, flags, ht_capa,
4508 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004509
Jouni Malinen636a5d32009-03-19 13:39:22 +02004510 return err;
4511}
4512
4513static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4514{
Johannes Berg4c476992010-10-04 21:36:35 +02004515 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4516 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004517 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004518 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004519 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004520 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004521
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004522 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4523 return -EINVAL;
4524
4525 if (!info->attrs[NL80211_ATTR_MAC])
4526 return -EINVAL;
4527
4528 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4529 return -EINVAL;
4530
Johannes Berg4c476992010-10-04 21:36:35 +02004531 if (!rdev->ops->deauth)
4532 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004533
Johannes Berg074ac8d2010-09-16 14:58:22 +02004534 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004535 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4536 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004537
Johannes Berg19957bb2009-07-02 17:20:43 +02004538 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004539
Johannes Berg19957bb2009-07-02 17:20:43 +02004540 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4541 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004542 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004543 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004544 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004545
4546 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004547 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4548 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004549 }
4550
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004551 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4552
Johannes Berg4c476992010-10-04 21:36:35 +02004553 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4554 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004555}
4556
4557static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4558{
Johannes Berg4c476992010-10-04 21:36:35 +02004559 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4560 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004561 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004562 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004563 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004564 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004565
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004566 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4567 return -EINVAL;
4568
4569 if (!info->attrs[NL80211_ATTR_MAC])
4570 return -EINVAL;
4571
4572 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4573 return -EINVAL;
4574
Johannes Berg4c476992010-10-04 21:36:35 +02004575 if (!rdev->ops->disassoc)
4576 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004577
Johannes Berg074ac8d2010-09-16 14:58:22 +02004578 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004579 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4580 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004581
Johannes Berg19957bb2009-07-02 17:20:43 +02004582 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004583
Johannes Berg19957bb2009-07-02 17:20:43 +02004584 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4585 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004586 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004587 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004588 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004589
4590 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004591 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4592 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004593 }
4594
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004595 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4596
Johannes Berg4c476992010-10-04 21:36:35 +02004597 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4598 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004599}
4600
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004601static bool
4602nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4603 int mcast_rate[IEEE80211_NUM_BANDS],
4604 int rateval)
4605{
4606 struct wiphy *wiphy = &rdev->wiphy;
4607 bool found = false;
4608 int band, i;
4609
4610 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4611 struct ieee80211_supported_band *sband;
4612
4613 sband = wiphy->bands[band];
4614 if (!sband)
4615 continue;
4616
4617 for (i = 0; i < sband->n_bitrates; i++) {
4618 if (sband->bitrates[i].bitrate == rateval) {
4619 mcast_rate[band] = i + 1;
4620 found = true;
4621 break;
4622 }
4623 }
4624 }
4625
4626 return found;
4627}
4628
Johannes Berg04a773a2009-04-19 21:24:32 +02004629static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4630{
Johannes Berg4c476992010-10-04 21:36:35 +02004631 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4632 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004633 struct cfg80211_ibss_params ibss;
4634 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004635 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004636 int err;
4637
Johannes Berg8e30bc52009-04-22 17:45:38 +02004638 memset(&ibss, 0, sizeof(ibss));
4639
Johannes Berg04a773a2009-04-19 21:24:32 +02004640 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4641 return -EINVAL;
4642
4643 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4644 !info->attrs[NL80211_ATTR_SSID] ||
4645 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4646 return -EINVAL;
4647
Johannes Berg8e30bc52009-04-22 17:45:38 +02004648 ibss.beacon_interval = 100;
4649
4650 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4651 ibss.beacon_interval =
4652 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4653 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4654 return -EINVAL;
4655 }
4656
Johannes Berg4c476992010-10-04 21:36:35 +02004657 if (!rdev->ops->join_ibss)
4658 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004659
Johannes Berg4c476992010-10-04 21:36:35 +02004660 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4661 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004662
Johannes Berg79c97e92009-07-07 03:56:12 +02004663 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004664
Johannes Berg39193492011-09-16 13:45:25 +02004665 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004666 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02004667
4668 if (!is_valid_ether_addr(ibss.bssid))
4669 return -EINVAL;
4670 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004671 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4672 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4673
4674 if (info->attrs[NL80211_ATTR_IE]) {
4675 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4676 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4677 }
4678
4679 ibss.channel = ieee80211_get_channel(wiphy,
4680 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4681 if (!ibss.channel ||
4682 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004683 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4684 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004685
4686 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004687 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004688
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004689 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4690 u8 *rates =
4691 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4692 int n_rates =
4693 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4694 struct ieee80211_supported_band *sband =
4695 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004696 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004697
Johannes Berg34850ab2011-07-18 18:08:35 +02004698 err = ieee80211_get_ratemask(sband, rates, n_rates,
4699 &ibss.basic_rates);
4700 if (err)
4701 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004702 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004703
4704 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4705 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4706 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4707 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004708
Johannes Berg4c476992010-10-04 21:36:35 +02004709 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4710 connkeys = nl80211_parse_connkeys(rdev,
4711 info->attrs[NL80211_ATTR_KEYS]);
4712 if (IS_ERR(connkeys))
4713 return PTR_ERR(connkeys);
4714 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004715
Johannes Berg4c476992010-10-04 21:36:35 +02004716 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004717 if (err)
4718 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004719 return err;
4720}
4721
4722static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4723{
Johannes Berg4c476992010-10-04 21:36:35 +02004724 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4725 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004726
Johannes Berg4c476992010-10-04 21:36:35 +02004727 if (!rdev->ops->leave_ibss)
4728 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004729
Johannes Berg4c476992010-10-04 21:36:35 +02004730 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4731 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004732
Johannes Berg4c476992010-10-04 21:36:35 +02004733 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004734}
4735
Johannes Bergaff89a92009-07-01 21:26:51 +02004736#ifdef CONFIG_NL80211_TESTMODE
4737static struct genl_multicast_group nl80211_testmode_mcgrp = {
4738 .name = "testmode",
4739};
4740
4741static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4742{
Johannes Berg4c476992010-10-04 21:36:35 +02004743 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004744 int err;
4745
4746 if (!info->attrs[NL80211_ATTR_TESTDATA])
4747 return -EINVAL;
4748
Johannes Bergaff89a92009-07-01 21:26:51 +02004749 err = -EOPNOTSUPP;
4750 if (rdev->ops->testmode_cmd) {
4751 rdev->testmode_info = info;
4752 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4753 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4754 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4755 rdev->testmode_info = NULL;
4756 }
4757
Johannes Bergaff89a92009-07-01 21:26:51 +02004758 return err;
4759}
4760
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004761static int nl80211_testmode_dump(struct sk_buff *skb,
4762 struct netlink_callback *cb)
4763{
4764 struct cfg80211_registered_device *dev;
4765 int err;
4766 long phy_idx;
4767 void *data = NULL;
4768 int data_len = 0;
4769
4770 if (cb->args[0]) {
4771 /*
4772 * 0 is a valid index, but not valid for args[0],
4773 * so we need to offset by 1.
4774 */
4775 phy_idx = cb->args[0] - 1;
4776 } else {
4777 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4778 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4779 nl80211_policy);
4780 if (err)
4781 return err;
4782 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4783 return -EINVAL;
4784 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4785 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4786 cb->args[1] =
4787 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4788 }
4789
4790 if (cb->args[1]) {
4791 data = nla_data((void *)cb->args[1]);
4792 data_len = nla_len((void *)cb->args[1]);
4793 }
4794
4795 mutex_lock(&cfg80211_mutex);
4796 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4797 if (!dev) {
4798 mutex_unlock(&cfg80211_mutex);
4799 return -ENOENT;
4800 }
4801 cfg80211_lock_rdev(dev);
4802 mutex_unlock(&cfg80211_mutex);
4803
4804 if (!dev->ops->testmode_dump) {
4805 err = -EOPNOTSUPP;
4806 goto out_err;
4807 }
4808
4809 while (1) {
4810 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4811 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4812 NL80211_CMD_TESTMODE);
4813 struct nlattr *tmdata;
4814
4815 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4816 genlmsg_cancel(skb, hdr);
4817 break;
4818 }
4819
4820 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4821 if (!tmdata) {
4822 genlmsg_cancel(skb, hdr);
4823 break;
4824 }
4825 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4826 data, data_len);
4827 nla_nest_end(skb, tmdata);
4828
4829 if (err == -ENOBUFS || err == -ENOENT) {
4830 genlmsg_cancel(skb, hdr);
4831 break;
4832 } else if (err) {
4833 genlmsg_cancel(skb, hdr);
4834 goto out_err;
4835 }
4836
4837 genlmsg_end(skb, hdr);
4838 }
4839
4840 err = skb->len;
4841 /* see above */
4842 cb->args[0] = phy_idx + 1;
4843 out_err:
4844 cfg80211_unlock_rdev(dev);
4845 return err;
4846}
4847
Johannes Bergaff89a92009-07-01 21:26:51 +02004848static struct sk_buff *
4849__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4850 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4851{
4852 struct sk_buff *skb;
4853 void *hdr;
4854 struct nlattr *data;
4855
4856 skb = nlmsg_new(approxlen + 100, gfp);
4857 if (!skb)
4858 return NULL;
4859
4860 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4861 if (!hdr) {
4862 kfree_skb(skb);
4863 return NULL;
4864 }
4865
4866 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4867 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4868
4869 ((void **)skb->cb)[0] = rdev;
4870 ((void **)skb->cb)[1] = hdr;
4871 ((void **)skb->cb)[2] = data;
4872
4873 return skb;
4874
4875 nla_put_failure:
4876 kfree_skb(skb);
4877 return NULL;
4878}
4879
4880struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4881 int approxlen)
4882{
4883 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4884
4885 if (WARN_ON(!rdev->testmode_info))
4886 return NULL;
4887
4888 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4889 rdev->testmode_info->snd_pid,
4890 rdev->testmode_info->snd_seq,
4891 GFP_KERNEL);
4892}
4893EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4894
4895int cfg80211_testmode_reply(struct sk_buff *skb)
4896{
4897 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4898 void *hdr = ((void **)skb->cb)[1];
4899 struct nlattr *data = ((void **)skb->cb)[2];
4900
4901 if (WARN_ON(!rdev->testmode_info)) {
4902 kfree_skb(skb);
4903 return -EINVAL;
4904 }
4905
4906 nla_nest_end(skb, data);
4907 genlmsg_end(skb, hdr);
4908 return genlmsg_reply(skb, rdev->testmode_info);
4909}
4910EXPORT_SYMBOL(cfg80211_testmode_reply);
4911
4912struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4913 int approxlen, gfp_t gfp)
4914{
4915 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4916
4917 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4918}
4919EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4920
4921void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4922{
4923 void *hdr = ((void **)skb->cb)[1];
4924 struct nlattr *data = ((void **)skb->cb)[2];
4925
4926 nla_nest_end(skb, data);
4927 genlmsg_end(skb, hdr);
4928 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4929}
4930EXPORT_SYMBOL(cfg80211_testmode_event);
4931#endif
4932
Samuel Ortizb23aa672009-07-01 21:26:54 +02004933static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4934{
Johannes Berg4c476992010-10-04 21:36:35 +02004935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4936 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004937 struct cfg80211_connect_params connect;
4938 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004939 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004940 int err;
4941
4942 memset(&connect, 0, sizeof(connect));
4943
4944 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4945 return -EINVAL;
4946
4947 if (!info->attrs[NL80211_ATTR_SSID] ||
4948 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4949 return -EINVAL;
4950
4951 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4952 connect.auth_type =
4953 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4954 if (!nl80211_valid_auth_type(connect.auth_type))
4955 return -EINVAL;
4956 } else
4957 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4958
4959 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4960
Johannes Bergc0692b82010-08-27 14:26:53 +03004961 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004962 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004963 if (err)
4964 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004965
Johannes Berg074ac8d2010-09-16 14:58:22 +02004966 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004967 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4968 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004969
Johannes Berg79c97e92009-07-07 03:56:12 +02004970 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004971
Samuel Ortizb23aa672009-07-01 21:26:54 +02004972 if (info->attrs[NL80211_ATTR_MAC])
4973 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4974 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4975 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4976
4977 if (info->attrs[NL80211_ATTR_IE]) {
4978 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4979 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4980 }
4981
4982 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4983 connect.channel =
4984 ieee80211_get_channel(wiphy,
4985 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4986 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004987 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4988 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004989 }
4990
Johannes Bergfffd0932009-07-08 14:22:54 +02004991 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4992 connkeys = nl80211_parse_connkeys(rdev,
4993 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004994 if (IS_ERR(connkeys))
4995 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004996 }
4997
Ben Greear7e7c8922011-11-18 11:31:59 -08004998 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4999 connect.flags |= ASSOC_REQ_DISABLE_HT;
5000
5001 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5002 memcpy(&connect.ht_capa_mask,
5003 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5004 sizeof(connect.ht_capa_mask));
5005
5006 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5007 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5008 return -EINVAL;
5009 memcpy(&connect.ht_capa,
5010 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5011 sizeof(connect.ht_capa));
5012 }
5013
Johannes Bergfffd0932009-07-08 14:22:54 +02005014 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005015 if (err)
5016 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005017 return err;
5018}
5019
5020static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5021{
Johannes Berg4c476992010-10-04 21:36:35 +02005022 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5023 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005024 u16 reason;
5025
5026 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5027 reason = WLAN_REASON_DEAUTH_LEAVING;
5028 else
5029 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5030
5031 if (reason == 0)
5032 return -EINVAL;
5033
Johannes Berg074ac8d2010-09-16 14:58:22 +02005034 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005035 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5036 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005037
Johannes Berg4c476992010-10-04 21:36:35 +02005038 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005039}
5040
Johannes Berg463d0182009-07-14 00:33:35 +02005041static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5042{
Johannes Berg4c476992010-10-04 21:36:35 +02005043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005044 struct net *net;
5045 int err;
5046 u32 pid;
5047
5048 if (!info->attrs[NL80211_ATTR_PID])
5049 return -EINVAL;
5050
5051 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5052
Johannes Berg463d0182009-07-14 00:33:35 +02005053 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005054 if (IS_ERR(net))
5055 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005056
5057 err = 0;
5058
5059 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005060 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5061 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005062
Johannes Berg463d0182009-07-14 00:33:35 +02005063 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005064 return err;
5065}
5066
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005067static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5068{
Johannes Berg4c476992010-10-04 21:36:35 +02005069 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005070 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5071 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005072 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005073 struct cfg80211_pmksa pmksa;
5074
5075 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5076
5077 if (!info->attrs[NL80211_ATTR_MAC])
5078 return -EINVAL;
5079
5080 if (!info->attrs[NL80211_ATTR_PMKID])
5081 return -EINVAL;
5082
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005083 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5084 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5085
Johannes Berg074ac8d2010-09-16 14:58:22 +02005086 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005087 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5088 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005089
5090 switch (info->genlhdr->cmd) {
5091 case NL80211_CMD_SET_PMKSA:
5092 rdev_ops = rdev->ops->set_pmksa;
5093 break;
5094 case NL80211_CMD_DEL_PMKSA:
5095 rdev_ops = rdev->ops->del_pmksa;
5096 break;
5097 default:
5098 WARN_ON(1);
5099 break;
5100 }
5101
Johannes Berg4c476992010-10-04 21:36:35 +02005102 if (!rdev_ops)
5103 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005104
Johannes Berg4c476992010-10-04 21:36:35 +02005105 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005106}
5107
5108static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5109{
Johannes Berg4c476992010-10-04 21:36:35 +02005110 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5111 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005112
Johannes Berg074ac8d2010-09-16 14:58:22 +02005113 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005114 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5115 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005116
Johannes Berg4c476992010-10-04 21:36:35 +02005117 if (!rdev->ops->flush_pmksa)
5118 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005119
Johannes Berg4c476992010-10-04 21:36:35 +02005120 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005121}
5122
Arik Nemtsov109086c2011-09-28 14:12:50 +03005123static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5124{
5125 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5126 struct net_device *dev = info->user_ptr[1];
5127 u8 action_code, dialog_token;
5128 u16 status_code;
5129 u8 *peer;
5130
5131 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5132 !rdev->ops->tdls_mgmt)
5133 return -EOPNOTSUPP;
5134
5135 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5136 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5137 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5138 !info->attrs[NL80211_ATTR_IE] ||
5139 !info->attrs[NL80211_ATTR_MAC])
5140 return -EINVAL;
5141
5142 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5143 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5144 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5145 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5146
5147 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5148 dialog_token, status_code,
5149 nla_data(info->attrs[NL80211_ATTR_IE]),
5150 nla_len(info->attrs[NL80211_ATTR_IE]));
5151}
5152
5153static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5154{
5155 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5156 struct net_device *dev = info->user_ptr[1];
5157 enum nl80211_tdls_operation operation;
5158 u8 *peer;
5159
5160 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5161 !rdev->ops->tdls_oper)
5162 return -EOPNOTSUPP;
5163
5164 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5165 !info->attrs[NL80211_ATTR_MAC])
5166 return -EINVAL;
5167
5168 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5169 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5170
5171 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5172}
5173
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005174static int nl80211_remain_on_channel(struct sk_buff *skb,
5175 struct genl_info *info)
5176{
Johannes Berg4c476992010-10-04 21:36:35 +02005177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5178 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005179 struct ieee80211_channel *chan;
5180 struct sk_buff *msg;
5181 void *hdr;
5182 u64 cookie;
5183 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5184 u32 freq, duration;
5185 int err;
5186
5187 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5188 !info->attrs[NL80211_ATTR_DURATION])
5189 return -EINVAL;
5190
5191 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5192
5193 /*
5194 * We should be on that channel for at least one jiffie,
5195 * and more than 5 seconds seems excessive.
5196 */
Johannes Berga2939112010-12-14 17:54:28 +01005197 if (!duration || !msecs_to_jiffies(duration) ||
5198 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005199 return -EINVAL;
5200
Johannes Berg7c4ef712011-11-18 15:33:48 +01005201 if (!rdev->ops->remain_on_channel ||
5202 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005203 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005204
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005205 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5206 channel_type = nla_get_u32(
5207 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5208 if (channel_type != NL80211_CHAN_NO_HT &&
5209 channel_type != NL80211_CHAN_HT20 &&
5210 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005211 channel_type != NL80211_CHAN_HT40MINUS)
5212 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005213 }
5214
5215 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5216 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005217 if (chan == NULL)
5218 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005219
5220 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005221 if (!msg)
5222 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005223
5224 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5225 NL80211_CMD_REMAIN_ON_CHANNEL);
5226
5227 if (IS_ERR(hdr)) {
5228 err = PTR_ERR(hdr);
5229 goto free_msg;
5230 }
5231
5232 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5233 channel_type, duration, &cookie);
5234
5235 if (err)
5236 goto free_msg;
5237
5238 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5239
5240 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005241
5242 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005243
5244 nla_put_failure:
5245 err = -ENOBUFS;
5246 free_msg:
5247 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005248 return err;
5249}
5250
5251static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5252 struct genl_info *info)
5253{
Johannes Berg4c476992010-10-04 21:36:35 +02005254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5255 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005256 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005257
5258 if (!info->attrs[NL80211_ATTR_COOKIE])
5259 return -EINVAL;
5260
Johannes Berg4c476992010-10-04 21:36:35 +02005261 if (!rdev->ops->cancel_remain_on_channel)
5262 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005263
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005264 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5265
Johannes Berg4c476992010-10-04 21:36:35 +02005266 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005267}
5268
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005269static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5270 u8 *rates, u8 rates_len)
5271{
5272 u8 i;
5273 u32 mask = 0;
5274
5275 for (i = 0; i < rates_len; i++) {
5276 int rate = (rates[i] & 0x7f) * 5;
5277 int ridx;
5278 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5279 struct ieee80211_rate *srate =
5280 &sband->bitrates[ridx];
5281 if (rate == srate->bitrate) {
5282 mask |= 1 << ridx;
5283 break;
5284 }
5285 }
5286 if (ridx == sband->n_bitrates)
5287 return 0; /* rate not found */
5288 }
5289
5290 return mask;
5291}
5292
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005293static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005294 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5295 .len = NL80211_MAX_SUPP_RATES },
5296};
5297
5298static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5299 struct genl_info *info)
5300{
5301 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005302 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005303 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005304 int rem, i;
5305 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005306 struct nlattr *tx_rates;
5307 struct ieee80211_supported_band *sband;
5308
5309 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5310 return -EINVAL;
5311
Johannes Berg4c476992010-10-04 21:36:35 +02005312 if (!rdev->ops->set_bitrate_mask)
5313 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005314
5315 memset(&mask, 0, sizeof(mask));
5316 /* Default to all rates enabled */
5317 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5318 sband = rdev->wiphy.bands[i];
5319 mask.control[i].legacy =
5320 sband ? (1 << sband->n_bitrates) - 1 : 0;
5321 }
5322
5323 /*
5324 * The nested attribute uses enum nl80211_band as the index. This maps
5325 * directly to the enum ieee80211_band values used in cfg80211.
5326 */
5327 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5328 {
5329 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005330 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5331 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005332 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005333 if (sband == NULL)
5334 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005335 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5336 nla_len(tx_rates), nl80211_txattr_policy);
5337 if (tb[NL80211_TXRATE_LEGACY]) {
5338 mask.control[band].legacy = rateset_to_mask(
5339 sband,
5340 nla_data(tb[NL80211_TXRATE_LEGACY]),
5341 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005342 if (mask.control[band].legacy == 0)
5343 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005344 }
5345 }
5346
Johannes Berg4c476992010-10-04 21:36:35 +02005347 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005348}
5349
Johannes Berg2e161f72010-08-12 15:38:38 +02005350static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005351{
Johannes Berg4c476992010-10-04 21:36:35 +02005352 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5353 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005354 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005355
5356 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5357 return -EINVAL;
5358
Johannes Berg2e161f72010-08-12 15:38:38 +02005359 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5360 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005361
Johannes Berg9d38d852010-06-09 17:20:33 +02005362 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005363 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005364 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5365 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5366 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005367 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005368 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5369 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005370
5371 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005372 if (!rdev->ops->mgmt_tx)
5373 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005374
Johannes Berg4c476992010-10-04 21:36:35 +02005375 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005376 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005377 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5378 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005379}
5380
Johannes Berg2e161f72010-08-12 15:38:38 +02005381static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005382{
Johannes Berg4c476992010-10-04 21:36:35 +02005383 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5384 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005385 struct ieee80211_channel *chan;
5386 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005387 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005388 u32 freq;
5389 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01005390 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005391 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01005392 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005393 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01005394 bool offchan, no_cck, dont_wait_for_ack;
5395
5396 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02005397
5398 if (!info->attrs[NL80211_ATTR_FRAME] ||
5399 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5400 return -EINVAL;
5401
Johannes Berg4c476992010-10-04 21:36:35 +02005402 if (!rdev->ops->mgmt_tx)
5403 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005404
Johannes Berg9d38d852010-06-09 17:20:33 +02005405 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005406 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005407 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5408 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5409 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005410 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005411 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5412 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005413
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005414 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01005415 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005416 return -EINVAL;
5417 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5418 }
5419
Jouni Malinen026331c2010-02-15 12:53:10 +02005420 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5421 channel_type = nla_get_u32(
5422 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5423 if (channel_type != NL80211_CHAN_NO_HT &&
5424 channel_type != NL80211_CHAN_HT20 &&
5425 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005426 channel_type != NL80211_CHAN_HT40MINUS)
5427 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005428 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005429 }
5430
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005431 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5432
Johannes Berg7c4ef712011-11-18 15:33:48 +01005433 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5434 return -EINVAL;
5435
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305436 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5437
Jouni Malinen026331c2010-02-15 12:53:10 +02005438 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5439 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005440 if (chan == NULL)
5441 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005442
Johannes Berge247bd902011-11-04 11:18:21 +01005443 if (!dont_wait_for_ack) {
5444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5445 if (!msg)
5446 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005447
Johannes Berge247bd902011-11-04 11:18:21 +01005448 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5449 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005450
Johannes Berge247bd902011-11-04 11:18:21 +01005451 if (IS_ERR(hdr)) {
5452 err = PTR_ERR(hdr);
5453 goto free_msg;
5454 }
Jouni Malinen026331c2010-02-15 12:53:10 +02005455 }
Johannes Berge247bd902011-11-04 11:18:21 +01005456
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005457 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5458 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005459 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5460 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01005461 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005462 if (err)
5463 goto free_msg;
5464
Johannes Berge247bd902011-11-04 11:18:21 +01005465 if (msg) {
5466 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005467
Johannes Berge247bd902011-11-04 11:18:21 +01005468 genlmsg_end(msg, hdr);
5469 return genlmsg_reply(msg, info);
5470 }
5471
5472 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02005473
5474 nla_put_failure:
5475 err = -ENOBUFS;
5476 free_msg:
5477 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005478 return err;
5479}
5480
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005481static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5482{
5483 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5484 struct net_device *dev = info->user_ptr[1];
5485 u64 cookie;
5486
5487 if (!info->attrs[NL80211_ATTR_COOKIE])
5488 return -EINVAL;
5489
5490 if (!rdev->ops->mgmt_tx_cancel_wait)
5491 return -EOPNOTSUPP;
5492
5493 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5494 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5495 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5496 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5497 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5498 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5499 return -EOPNOTSUPP;
5500
5501 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5502
5503 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5504}
5505
Kalle Valoffb9eb32010-02-17 17:58:10 +02005506static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5507{
Johannes Berg4c476992010-10-04 21:36:35 +02005508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005509 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005510 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005511 u8 ps_state;
5512 bool state;
5513 int err;
5514
Johannes Berg4c476992010-10-04 21:36:35 +02005515 if (!info->attrs[NL80211_ATTR_PS_STATE])
5516 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005517
5518 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5519
Johannes Berg4c476992010-10-04 21:36:35 +02005520 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5521 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005522
5523 wdev = dev->ieee80211_ptr;
5524
Johannes Berg4c476992010-10-04 21:36:35 +02005525 if (!rdev->ops->set_power_mgmt)
5526 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005527
5528 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5529
5530 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005531 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005532
Johannes Berg4c476992010-10-04 21:36:35 +02005533 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5534 wdev->ps_timeout);
5535 if (!err)
5536 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005537 return err;
5538}
5539
5540static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5541{
Johannes Berg4c476992010-10-04 21:36:35 +02005542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005543 enum nl80211_ps_state ps_state;
5544 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005545 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005546 struct sk_buff *msg;
5547 void *hdr;
5548 int err;
5549
Kalle Valoffb9eb32010-02-17 17:58:10 +02005550 wdev = dev->ieee80211_ptr;
5551
Johannes Berg4c476992010-10-04 21:36:35 +02005552 if (!rdev->ops->set_power_mgmt)
5553 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005554
5555 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005556 if (!msg)
5557 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005558
5559 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5560 NL80211_CMD_GET_POWER_SAVE);
5561 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005562 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005563 goto free_msg;
5564 }
5565
5566 if (wdev->ps)
5567 ps_state = NL80211_PS_ENABLED;
5568 else
5569 ps_state = NL80211_PS_DISABLED;
5570
5571 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5572
5573 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005574 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005575
Johannes Berg4c476992010-10-04 21:36:35 +02005576 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005577 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005578 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005579 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005580 return err;
5581}
5582
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005583static struct nla_policy
5584nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5585 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5586 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5587 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5588};
5589
5590static int nl80211_set_cqm_rssi(struct genl_info *info,
5591 s32 threshold, u32 hysteresis)
5592{
Johannes Berg4c476992010-10-04 21:36:35 +02005593 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005594 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005595 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005596
5597 if (threshold > 0)
5598 return -EINVAL;
5599
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005600 wdev = dev->ieee80211_ptr;
5601
Johannes Berg4c476992010-10-04 21:36:35 +02005602 if (!rdev->ops->set_cqm_rssi_config)
5603 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005604
Johannes Berg074ac8d2010-09-16 14:58:22 +02005605 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005606 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5607 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005608
Johannes Berg4c476992010-10-04 21:36:35 +02005609 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5610 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005611}
5612
5613static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5614{
5615 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5616 struct nlattr *cqm;
5617 int err;
5618
5619 cqm = info->attrs[NL80211_ATTR_CQM];
5620 if (!cqm) {
5621 err = -EINVAL;
5622 goto out;
5623 }
5624
5625 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5626 nl80211_attr_cqm_policy);
5627 if (err)
5628 goto out;
5629
5630 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5631 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5632 s32 threshold;
5633 u32 hysteresis;
5634 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5635 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5636 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5637 } else
5638 err = -EINVAL;
5639
5640out:
5641 return err;
5642}
5643
Johannes Berg29cbe682010-12-03 09:20:44 +01005644static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5645{
5646 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5647 struct net_device *dev = info->user_ptr[1];
5648 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005649 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005650 int err;
5651
5652 /* start with default */
5653 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005654 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005655
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005656 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005657 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005658 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005659 if (err)
5660 return err;
5661 }
5662
5663 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5664 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5665 return -EINVAL;
5666
Javier Cardonac80d5452010-12-16 17:37:49 -08005667 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5668 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5669
5670 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5671 /* parse additional setup parameters if given */
5672 err = nl80211_parse_mesh_setup(info, &setup);
5673 if (err)
5674 return err;
5675 }
5676
5677 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005678}
5679
5680static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5681{
5682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5683 struct net_device *dev = info->user_ptr[1];
5684
5685 return cfg80211_leave_mesh(rdev, dev);
5686}
5687
Johannes Bergff1b6e62011-05-04 15:37:28 +02005688static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5689{
5690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5691 struct sk_buff *msg;
5692 void *hdr;
5693
5694 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5695 return -EOPNOTSUPP;
5696
5697 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5698 if (!msg)
5699 return -ENOMEM;
5700
5701 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5702 NL80211_CMD_GET_WOWLAN);
5703 if (!hdr)
5704 goto nla_put_failure;
5705
5706 if (rdev->wowlan) {
5707 struct nlattr *nl_wowlan;
5708
5709 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5710 if (!nl_wowlan)
5711 goto nla_put_failure;
5712
5713 if (rdev->wowlan->any)
5714 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5715 if (rdev->wowlan->disconnect)
5716 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5717 if (rdev->wowlan->magic_pkt)
5718 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005719 if (rdev->wowlan->gtk_rekey_failure)
5720 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5721 if (rdev->wowlan->eap_identity_req)
5722 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5723 if (rdev->wowlan->four_way_handshake)
5724 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5725 if (rdev->wowlan->rfkill_release)
5726 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005727 if (rdev->wowlan->n_patterns) {
5728 struct nlattr *nl_pats, *nl_pat;
5729 int i, pat_len;
5730
5731 nl_pats = nla_nest_start(msg,
5732 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5733 if (!nl_pats)
5734 goto nla_put_failure;
5735
5736 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5737 nl_pat = nla_nest_start(msg, i + 1);
5738 if (!nl_pat)
5739 goto nla_put_failure;
5740 pat_len = rdev->wowlan->patterns[i].pattern_len;
5741 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5742 DIV_ROUND_UP(pat_len, 8),
5743 rdev->wowlan->patterns[i].mask);
5744 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5745 pat_len,
5746 rdev->wowlan->patterns[i].pattern);
5747 nla_nest_end(msg, nl_pat);
5748 }
5749 nla_nest_end(msg, nl_pats);
5750 }
5751
5752 nla_nest_end(msg, nl_wowlan);
5753 }
5754
5755 genlmsg_end(msg, hdr);
5756 return genlmsg_reply(msg, info);
5757
5758nla_put_failure:
5759 nlmsg_free(msg);
5760 return -ENOBUFS;
5761}
5762
5763static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5764{
5765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5766 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5767 struct cfg80211_wowlan no_triggers = {};
5768 struct cfg80211_wowlan new_triggers = {};
5769 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5770 int err, i;
5771
5772 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5773 return -EOPNOTSUPP;
5774
5775 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5776 goto no_triggers;
5777
5778 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5779 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5780 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5781 nl80211_wowlan_policy);
5782 if (err)
5783 return err;
5784
5785 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5786 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5787 return -EINVAL;
5788 new_triggers.any = true;
5789 }
5790
5791 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5792 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5793 return -EINVAL;
5794 new_triggers.disconnect = true;
5795 }
5796
5797 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5798 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5799 return -EINVAL;
5800 new_triggers.magic_pkt = true;
5801 }
5802
Johannes Berg77dbbb12011-07-13 10:48:55 +02005803 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5804 return -EINVAL;
5805
5806 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5807 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5808 return -EINVAL;
5809 new_triggers.gtk_rekey_failure = true;
5810 }
5811
5812 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5813 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5814 return -EINVAL;
5815 new_triggers.eap_identity_req = true;
5816 }
5817
5818 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5819 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5820 return -EINVAL;
5821 new_triggers.four_way_handshake = true;
5822 }
5823
5824 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5825 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5826 return -EINVAL;
5827 new_triggers.rfkill_release = true;
5828 }
5829
Johannes Bergff1b6e62011-05-04 15:37:28 +02005830 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5831 struct nlattr *pat;
5832 int n_patterns = 0;
5833 int rem, pat_len, mask_len;
5834 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5835
5836 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5837 rem)
5838 n_patterns++;
5839 if (n_patterns > wowlan->n_patterns)
5840 return -EINVAL;
5841
5842 new_triggers.patterns = kcalloc(n_patterns,
5843 sizeof(new_triggers.patterns[0]),
5844 GFP_KERNEL);
5845 if (!new_triggers.patterns)
5846 return -ENOMEM;
5847
5848 new_triggers.n_patterns = n_patterns;
5849 i = 0;
5850
5851 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5852 rem) {
5853 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5854 nla_data(pat), nla_len(pat), NULL);
5855 err = -EINVAL;
5856 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5857 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5858 goto error;
5859 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5860 mask_len = DIV_ROUND_UP(pat_len, 8);
5861 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5862 mask_len)
5863 goto error;
5864 if (pat_len > wowlan->pattern_max_len ||
5865 pat_len < wowlan->pattern_min_len)
5866 goto error;
5867
5868 new_triggers.patterns[i].mask =
5869 kmalloc(mask_len + pat_len, GFP_KERNEL);
5870 if (!new_triggers.patterns[i].mask) {
5871 err = -ENOMEM;
5872 goto error;
5873 }
5874 new_triggers.patterns[i].pattern =
5875 new_triggers.patterns[i].mask + mask_len;
5876 memcpy(new_triggers.patterns[i].mask,
5877 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5878 mask_len);
5879 new_triggers.patterns[i].pattern_len = pat_len;
5880 memcpy(new_triggers.patterns[i].pattern,
5881 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5882 pat_len);
5883 i++;
5884 }
5885 }
5886
5887 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5888 struct cfg80211_wowlan *ntrig;
5889 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5890 GFP_KERNEL);
5891 if (!ntrig) {
5892 err = -ENOMEM;
5893 goto error;
5894 }
5895 cfg80211_rdev_free_wowlan(rdev);
5896 rdev->wowlan = ntrig;
5897 } else {
5898 no_triggers:
5899 cfg80211_rdev_free_wowlan(rdev);
5900 rdev->wowlan = NULL;
5901 }
5902
5903 return 0;
5904 error:
5905 for (i = 0; i < new_triggers.n_patterns; i++)
5906 kfree(new_triggers.patterns[i].mask);
5907 kfree(new_triggers.patterns);
5908 return err;
5909}
5910
Johannes Berge5497d72011-07-05 16:35:40 +02005911static int nl80211_set_rekey_data(struct sk_buff *skb, 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 nlattr *tb[NUM_NL80211_REKEY_DATA];
5917 struct cfg80211_gtk_rekey_data rekey_data;
5918 int err;
5919
5920 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5921 return -EINVAL;
5922
5923 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5924 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5925 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5926 nl80211_rekey_policy);
5927 if (err)
5928 return err;
5929
5930 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5931 return -ERANGE;
5932 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5933 return -ERANGE;
5934 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5935 return -ERANGE;
5936
5937 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5938 NL80211_KEK_LEN);
5939 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5940 NL80211_KCK_LEN);
5941 memcpy(rekey_data.replay_ctr,
5942 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5943 NL80211_REPLAY_CTR_LEN);
5944
5945 wdev_lock(wdev);
5946 if (!wdev->current_bss) {
5947 err = -ENOTCONN;
5948 goto out;
5949 }
5950
5951 if (!rdev->ops->set_rekey_data) {
5952 err = -EOPNOTSUPP;
5953 goto out;
5954 }
5955
5956 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5957 out:
5958 wdev_unlock(wdev);
5959 return err;
5960}
5961
Johannes Berg28946da2011-11-04 11:18:12 +01005962static int nl80211_register_unexpected_frame(struct sk_buff *skb,
5963 struct genl_info *info)
5964{
5965 struct net_device *dev = info->user_ptr[1];
5966 struct wireless_dev *wdev = dev->ieee80211_ptr;
5967
5968 if (wdev->iftype != NL80211_IFTYPE_AP &&
5969 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5970 return -EINVAL;
5971
5972 if (wdev->ap_unexpected_nlpid)
5973 return -EBUSY;
5974
5975 wdev->ap_unexpected_nlpid = info->snd_pid;
5976 return 0;
5977}
5978
Johannes Berg7f6cf312011-11-04 11:18:15 +01005979static int nl80211_probe_client(struct sk_buff *skb,
5980 struct genl_info *info)
5981{
5982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5983 struct net_device *dev = info->user_ptr[1];
5984 struct wireless_dev *wdev = dev->ieee80211_ptr;
5985 struct sk_buff *msg;
5986 void *hdr;
5987 const u8 *addr;
5988 u64 cookie;
5989 int err;
5990
5991 if (wdev->iftype != NL80211_IFTYPE_AP &&
5992 wdev->iftype != NL80211_IFTYPE_P2P_GO)
5993 return -EOPNOTSUPP;
5994
5995 if (!info->attrs[NL80211_ATTR_MAC])
5996 return -EINVAL;
5997
5998 if (!rdev->ops->probe_client)
5999 return -EOPNOTSUPP;
6000
6001 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6002 if (!msg)
6003 return -ENOMEM;
6004
6005 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6006 NL80211_CMD_PROBE_CLIENT);
6007
6008 if (IS_ERR(hdr)) {
6009 err = PTR_ERR(hdr);
6010 goto free_msg;
6011 }
6012
6013 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6014
6015 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6016 if (err)
6017 goto free_msg;
6018
6019 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6020
6021 genlmsg_end(msg, hdr);
6022
6023 return genlmsg_reply(msg, info);
6024
6025 nla_put_failure:
6026 err = -ENOBUFS;
6027 free_msg:
6028 nlmsg_free(msg);
6029 return err;
6030}
6031
Johannes Berg5e760232011-11-04 11:18:17 +01006032static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6033{
6034 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6035
6036 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6037 return -EOPNOTSUPP;
6038
6039 if (rdev->ap_beacons_nlpid)
6040 return -EBUSY;
6041
6042 rdev->ap_beacons_nlpid = info->snd_pid;
6043
6044 return 0;
6045}
6046
Johannes Berg4c476992010-10-04 21:36:35 +02006047#define NL80211_FLAG_NEED_WIPHY 0x01
6048#define NL80211_FLAG_NEED_NETDEV 0x02
6049#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006050#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6051#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6052 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006053
6054static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6055 struct genl_info *info)
6056{
6057 struct cfg80211_registered_device *rdev;
6058 struct net_device *dev;
6059 int err;
6060 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6061
6062 if (rtnl)
6063 rtnl_lock();
6064
6065 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6066 rdev = cfg80211_get_dev_from_info(info);
6067 if (IS_ERR(rdev)) {
6068 if (rtnl)
6069 rtnl_unlock();
6070 return PTR_ERR(rdev);
6071 }
6072 info->user_ptr[0] = rdev;
6073 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6074 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
6075 if (err) {
6076 if (rtnl)
6077 rtnl_unlock();
6078 return err;
6079 }
Johannes Berg41265712010-10-04 21:14:05 +02006080 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6081 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02006082 cfg80211_unlock_rdev(rdev);
6083 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02006084 if (rtnl)
6085 rtnl_unlock();
6086 return -ENETDOWN;
6087 }
Johannes Berg4c476992010-10-04 21:36:35 +02006088 info->user_ptr[0] = rdev;
6089 info->user_ptr[1] = dev;
6090 }
6091
6092 return 0;
6093}
6094
6095static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6096 struct genl_info *info)
6097{
6098 if (info->user_ptr[0])
6099 cfg80211_unlock_rdev(info->user_ptr[0]);
6100 if (info->user_ptr[1])
6101 dev_put(info->user_ptr[1]);
6102 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6103 rtnl_unlock();
6104}
6105
Johannes Berg55682962007-09-20 13:09:35 -04006106static struct genl_ops nl80211_ops[] = {
6107 {
6108 .cmd = NL80211_CMD_GET_WIPHY,
6109 .doit = nl80211_get_wiphy,
6110 .dumpit = nl80211_dump_wiphy,
6111 .policy = nl80211_policy,
6112 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006113 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006114 },
6115 {
6116 .cmd = NL80211_CMD_SET_WIPHY,
6117 .doit = nl80211_set_wiphy,
6118 .policy = nl80211_policy,
6119 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006120 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006121 },
6122 {
6123 .cmd = NL80211_CMD_GET_INTERFACE,
6124 .doit = nl80211_get_interface,
6125 .dumpit = nl80211_dump_interface,
6126 .policy = nl80211_policy,
6127 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006128 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006129 },
6130 {
6131 .cmd = NL80211_CMD_SET_INTERFACE,
6132 .doit = nl80211_set_interface,
6133 .policy = nl80211_policy,
6134 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006135 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6136 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006137 },
6138 {
6139 .cmd = NL80211_CMD_NEW_INTERFACE,
6140 .doit = nl80211_new_interface,
6141 .policy = nl80211_policy,
6142 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006143 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6144 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006145 },
6146 {
6147 .cmd = NL80211_CMD_DEL_INTERFACE,
6148 .doit = nl80211_del_interface,
6149 .policy = nl80211_policy,
6150 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006151 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6152 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006153 },
Johannes Berg41ade002007-12-19 02:03:29 +01006154 {
6155 .cmd = NL80211_CMD_GET_KEY,
6156 .doit = nl80211_get_key,
6157 .policy = nl80211_policy,
6158 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006159 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6160 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006161 },
6162 {
6163 .cmd = NL80211_CMD_SET_KEY,
6164 .doit = nl80211_set_key,
6165 .policy = nl80211_policy,
6166 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006167 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006168 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006169 },
6170 {
6171 .cmd = NL80211_CMD_NEW_KEY,
6172 .doit = nl80211_new_key,
6173 .policy = nl80211_policy,
6174 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006175 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006176 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006177 },
6178 {
6179 .cmd = NL80211_CMD_DEL_KEY,
6180 .doit = nl80211_del_key,
6181 .policy = nl80211_policy,
6182 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006183 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006184 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006185 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006186 {
6187 .cmd = NL80211_CMD_SET_BEACON,
6188 .policy = nl80211_policy,
6189 .flags = GENL_ADMIN_PERM,
6190 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006191 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6192 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006193 },
6194 {
6195 .cmd = NL80211_CMD_NEW_BEACON,
6196 .policy = nl80211_policy,
6197 .flags = GENL_ADMIN_PERM,
6198 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006199 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6200 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006201 },
6202 {
6203 .cmd = NL80211_CMD_DEL_BEACON,
6204 .policy = nl80211_policy,
6205 .flags = GENL_ADMIN_PERM,
6206 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02006207 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6208 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006209 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006210 {
6211 .cmd = NL80211_CMD_GET_STATION,
6212 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006213 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006214 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006215 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6216 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006217 },
6218 {
6219 .cmd = NL80211_CMD_SET_STATION,
6220 .doit = nl80211_set_station,
6221 .policy = nl80211_policy,
6222 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006223 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6224 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006225 },
6226 {
6227 .cmd = NL80211_CMD_NEW_STATION,
6228 .doit = nl80211_new_station,
6229 .policy = nl80211_policy,
6230 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006231 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006232 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006233 },
6234 {
6235 .cmd = NL80211_CMD_DEL_STATION,
6236 .doit = nl80211_del_station,
6237 .policy = nl80211_policy,
6238 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006239 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6240 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006241 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006242 {
6243 .cmd = NL80211_CMD_GET_MPATH,
6244 .doit = nl80211_get_mpath,
6245 .dumpit = nl80211_dump_mpath,
6246 .policy = nl80211_policy,
6247 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006248 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006249 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006250 },
6251 {
6252 .cmd = NL80211_CMD_SET_MPATH,
6253 .doit = nl80211_set_mpath,
6254 .policy = nl80211_policy,
6255 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006256 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006257 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006258 },
6259 {
6260 .cmd = NL80211_CMD_NEW_MPATH,
6261 .doit = nl80211_new_mpath,
6262 .policy = nl80211_policy,
6263 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006264 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006265 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006266 },
6267 {
6268 .cmd = NL80211_CMD_DEL_MPATH,
6269 .doit = nl80211_del_mpath,
6270 .policy = nl80211_policy,
6271 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006272 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6273 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006274 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006275 {
6276 .cmd = NL80211_CMD_SET_BSS,
6277 .doit = nl80211_set_bss,
6278 .policy = nl80211_policy,
6279 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006280 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6281 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006282 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006283 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006284 .cmd = NL80211_CMD_GET_REG,
6285 .doit = nl80211_get_reg,
6286 .policy = nl80211_policy,
6287 /* can be retrieved by unprivileged users */
6288 },
6289 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006290 .cmd = NL80211_CMD_SET_REG,
6291 .doit = nl80211_set_reg,
6292 .policy = nl80211_policy,
6293 .flags = GENL_ADMIN_PERM,
6294 },
6295 {
6296 .cmd = NL80211_CMD_REQ_SET_REG,
6297 .doit = nl80211_req_set_reg,
6298 .policy = nl80211_policy,
6299 .flags = GENL_ADMIN_PERM,
6300 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006301 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006302 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6303 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006304 .policy = nl80211_policy,
6305 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006306 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6307 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006308 },
6309 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006310 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6311 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006312 .policy = nl80211_policy,
6313 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01006314 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006315 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006316 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02006317 {
Johannes Berg2a519312009-02-10 21:25:55 +01006318 .cmd = NL80211_CMD_TRIGGER_SCAN,
6319 .doit = nl80211_trigger_scan,
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 Berg2a519312009-02-10 21:25:55 +01006324 },
6325 {
6326 .cmd = NL80211_CMD_GET_SCAN,
6327 .policy = nl80211_policy,
6328 .dumpit = nl80211_dump_scan,
6329 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02006330 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03006331 .cmd = NL80211_CMD_START_SCHED_SCAN,
6332 .doit = nl80211_start_sched_scan,
6333 .policy = nl80211_policy,
6334 .flags = GENL_ADMIN_PERM,
6335 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6336 NL80211_FLAG_NEED_RTNL,
6337 },
6338 {
6339 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6340 .doit = nl80211_stop_sched_scan,
6341 .policy = nl80211_policy,
6342 .flags = GENL_ADMIN_PERM,
6343 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6344 NL80211_FLAG_NEED_RTNL,
6345 },
6346 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02006347 .cmd = NL80211_CMD_AUTHENTICATE,
6348 .doit = nl80211_authenticate,
6349 .policy = nl80211_policy,
6350 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006351 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006352 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006353 },
6354 {
6355 .cmd = NL80211_CMD_ASSOCIATE,
6356 .doit = nl80211_associate,
6357 .policy = nl80211_policy,
6358 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006359 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006360 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006361 },
6362 {
6363 .cmd = NL80211_CMD_DEAUTHENTICATE,
6364 .doit = nl80211_deauthenticate,
6365 .policy = nl80211_policy,
6366 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006368 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006369 },
6370 {
6371 .cmd = NL80211_CMD_DISASSOCIATE,
6372 .doit = nl80211_disassociate,
6373 .policy = nl80211_policy,
6374 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006375 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006376 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02006377 },
Johannes Berg04a773a2009-04-19 21:24:32 +02006378 {
6379 .cmd = NL80211_CMD_JOIN_IBSS,
6380 .doit = nl80211_join_ibss,
6381 .policy = nl80211_policy,
6382 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006383 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006384 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006385 },
6386 {
6387 .cmd = NL80211_CMD_LEAVE_IBSS,
6388 .doit = nl80211_leave_ibss,
6389 .policy = nl80211_policy,
6390 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006391 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006392 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02006393 },
Johannes Bergaff89a92009-07-01 21:26:51 +02006394#ifdef CONFIG_NL80211_TESTMODE
6395 {
6396 .cmd = NL80211_CMD_TESTMODE,
6397 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006398 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02006399 .policy = nl80211_policy,
6400 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006401 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6402 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02006403 },
6404#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02006405 {
6406 .cmd = NL80211_CMD_CONNECT,
6407 .doit = nl80211_connect,
6408 .policy = nl80211_policy,
6409 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006410 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006411 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006412 },
6413 {
6414 .cmd = NL80211_CMD_DISCONNECT,
6415 .doit = nl80211_disconnect,
6416 .policy = nl80211_policy,
6417 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006418 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006419 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006420 },
Johannes Berg463d0182009-07-14 00:33:35 +02006421 {
6422 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6423 .doit = nl80211_wiphy_netns,
6424 .policy = nl80211_policy,
6425 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006426 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6427 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006428 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006429 {
6430 .cmd = NL80211_CMD_GET_SURVEY,
6431 .policy = nl80211_policy,
6432 .dumpit = nl80211_dump_survey,
6433 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006434 {
6435 .cmd = NL80211_CMD_SET_PMKSA,
6436 .doit = nl80211_setdel_pmksa,
6437 .policy = nl80211_policy,
6438 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006439 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6440 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006441 },
6442 {
6443 .cmd = NL80211_CMD_DEL_PMKSA,
6444 .doit = nl80211_setdel_pmksa,
6445 .policy = nl80211_policy,
6446 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006447 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6448 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006449 },
6450 {
6451 .cmd = NL80211_CMD_FLUSH_PMKSA,
6452 .doit = nl80211_flush_pmksa,
6453 .policy = nl80211_policy,
6454 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006455 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6456 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006457 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006458 {
6459 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6460 .doit = nl80211_remain_on_channel,
6461 .policy = nl80211_policy,
6462 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006463 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006464 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006465 },
6466 {
6467 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6468 .doit = nl80211_cancel_remain_on_channel,
6469 .policy = nl80211_policy,
6470 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006471 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006472 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006473 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006474 {
6475 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6476 .doit = nl80211_set_tx_bitrate_mask,
6477 .policy = nl80211_policy,
6478 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006479 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6480 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006481 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006482 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006483 .cmd = NL80211_CMD_REGISTER_FRAME,
6484 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006485 .policy = nl80211_policy,
6486 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006487 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6488 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006489 },
6490 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006491 .cmd = NL80211_CMD_FRAME,
6492 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006493 .policy = nl80211_policy,
6494 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006495 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006496 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006497 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006498 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006499 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6500 .doit = nl80211_tx_mgmt_cancel_wait,
6501 .policy = nl80211_policy,
6502 .flags = GENL_ADMIN_PERM,
6503 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6504 NL80211_FLAG_NEED_RTNL,
6505 },
6506 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006507 .cmd = NL80211_CMD_SET_POWER_SAVE,
6508 .doit = nl80211_set_power_save,
6509 .policy = nl80211_policy,
6510 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006511 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6512 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006513 },
6514 {
6515 .cmd = NL80211_CMD_GET_POWER_SAVE,
6516 .doit = nl80211_get_power_save,
6517 .policy = nl80211_policy,
6518 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006519 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6520 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006521 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006522 {
6523 .cmd = NL80211_CMD_SET_CQM,
6524 .doit = nl80211_set_cqm,
6525 .policy = nl80211_policy,
6526 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006527 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6528 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006529 },
Johannes Bergf444de02010-05-05 15:25:02 +02006530 {
6531 .cmd = NL80211_CMD_SET_CHANNEL,
6532 .doit = nl80211_set_channel,
6533 .policy = nl80211_policy,
6534 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006535 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6536 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006537 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006538 {
6539 .cmd = NL80211_CMD_SET_WDS_PEER,
6540 .doit = nl80211_set_wds_peer,
6541 .policy = nl80211_policy,
6542 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006543 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6544 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006545 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006546 {
6547 .cmd = NL80211_CMD_JOIN_MESH,
6548 .doit = nl80211_join_mesh,
6549 .policy = nl80211_policy,
6550 .flags = GENL_ADMIN_PERM,
6551 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6552 NL80211_FLAG_NEED_RTNL,
6553 },
6554 {
6555 .cmd = NL80211_CMD_LEAVE_MESH,
6556 .doit = nl80211_leave_mesh,
6557 .policy = nl80211_policy,
6558 .flags = GENL_ADMIN_PERM,
6559 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6560 NL80211_FLAG_NEED_RTNL,
6561 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006562 {
6563 .cmd = NL80211_CMD_GET_WOWLAN,
6564 .doit = nl80211_get_wowlan,
6565 .policy = nl80211_policy,
6566 /* can be retrieved by unprivileged users */
6567 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6568 NL80211_FLAG_NEED_RTNL,
6569 },
6570 {
6571 .cmd = NL80211_CMD_SET_WOWLAN,
6572 .doit = nl80211_set_wowlan,
6573 .policy = nl80211_policy,
6574 .flags = GENL_ADMIN_PERM,
6575 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6576 NL80211_FLAG_NEED_RTNL,
6577 },
Johannes Berge5497d72011-07-05 16:35:40 +02006578 {
6579 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6580 .doit = nl80211_set_rekey_data,
6581 .policy = nl80211_policy,
6582 .flags = GENL_ADMIN_PERM,
6583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6584 NL80211_FLAG_NEED_RTNL,
6585 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03006586 {
6587 .cmd = NL80211_CMD_TDLS_MGMT,
6588 .doit = nl80211_tdls_mgmt,
6589 .policy = nl80211_policy,
6590 .flags = GENL_ADMIN_PERM,
6591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6592 NL80211_FLAG_NEED_RTNL,
6593 },
6594 {
6595 .cmd = NL80211_CMD_TDLS_OPER,
6596 .doit = nl80211_tdls_oper,
6597 .policy = nl80211_policy,
6598 .flags = GENL_ADMIN_PERM,
6599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6600 NL80211_FLAG_NEED_RTNL,
6601 },
Johannes Berg28946da2011-11-04 11:18:12 +01006602 {
6603 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6604 .doit = nl80211_register_unexpected_frame,
6605 .policy = nl80211_policy,
6606 .flags = GENL_ADMIN_PERM,
6607 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6608 NL80211_FLAG_NEED_RTNL,
6609 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01006610 {
6611 .cmd = NL80211_CMD_PROBE_CLIENT,
6612 .doit = nl80211_probe_client,
6613 .policy = nl80211_policy,
6614 .flags = GENL_ADMIN_PERM,
6615 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6616 NL80211_FLAG_NEED_RTNL,
6617 },
Johannes Berg5e760232011-11-04 11:18:17 +01006618 {
6619 .cmd = NL80211_CMD_REGISTER_BEACONS,
6620 .doit = nl80211_register_beacons,
6621 .policy = nl80211_policy,
6622 .flags = GENL_ADMIN_PERM,
6623 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6624 NL80211_FLAG_NEED_RTNL,
6625 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01006626 {
6627 .cmd = NL80211_CMD_SET_NOACK_MAP,
6628 .doit = nl80211_set_noack_map,
6629 .policy = nl80211_policy,
6630 .flags = GENL_ADMIN_PERM,
6631 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6632 NL80211_FLAG_NEED_RTNL,
6633 },
6634
Johannes Berg55682962007-09-20 13:09:35 -04006635};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006636
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006637static struct genl_multicast_group nl80211_mlme_mcgrp = {
6638 .name = "mlme",
6639};
Johannes Berg55682962007-09-20 13:09:35 -04006640
6641/* multicast groups */
6642static struct genl_multicast_group nl80211_config_mcgrp = {
6643 .name = "config",
6644};
Johannes Berg2a519312009-02-10 21:25:55 +01006645static struct genl_multicast_group nl80211_scan_mcgrp = {
6646 .name = "scan",
6647};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006648static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6649 .name = "regulatory",
6650};
Johannes Berg55682962007-09-20 13:09:35 -04006651
6652/* notification functions */
6653
6654void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6655{
6656 struct sk_buff *msg;
6657
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006659 if (!msg)
6660 return;
6661
6662 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6663 nlmsg_free(msg);
6664 return;
6665 }
6666
Johannes Berg463d0182009-07-14 00:33:35 +02006667 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6668 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006669}
6670
Johannes Berg362a4152009-05-24 16:43:15 +02006671static int nl80211_add_scan_req(struct sk_buff *msg,
6672 struct cfg80211_registered_device *rdev)
6673{
6674 struct cfg80211_scan_request *req = rdev->scan_req;
6675 struct nlattr *nest;
6676 int i;
6677
Johannes Berg667503d2009-07-07 03:56:11 +02006678 ASSERT_RDEV_LOCK(rdev);
6679
Johannes Berg362a4152009-05-24 16:43:15 +02006680 if (WARN_ON(!req))
6681 return 0;
6682
6683 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6684 if (!nest)
6685 goto nla_put_failure;
6686 for (i = 0; i < req->n_ssids; i++)
6687 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6688 nla_nest_end(msg, nest);
6689
6690 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6691 if (!nest)
6692 goto nla_put_failure;
6693 for (i = 0; i < req->n_channels; i++)
6694 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6695 nla_nest_end(msg, nest);
6696
6697 if (req->ie)
6698 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6699
6700 return 0;
6701 nla_put_failure:
6702 return -ENOBUFS;
6703}
6704
Johannes Berga538e2d2009-06-16 19:56:42 +02006705static int nl80211_send_scan_msg(struct sk_buff *msg,
6706 struct cfg80211_registered_device *rdev,
6707 struct net_device *netdev,
6708 u32 pid, u32 seq, int flags,
6709 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006710{
6711 void *hdr;
6712
6713 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6714 if (!hdr)
6715 return -1;
6716
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006717 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006718 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6719
Johannes Berg362a4152009-05-24 16:43:15 +02006720 /* ignore errors and send incomplete event anyway */
6721 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006722
6723 return genlmsg_end(msg, hdr);
6724
6725 nla_put_failure:
6726 genlmsg_cancel(msg, hdr);
6727 return -EMSGSIZE;
6728}
6729
Luciano Coelho807f8a82011-05-11 17:09:35 +03006730static int
6731nl80211_send_sched_scan_msg(struct sk_buff *msg,
6732 struct cfg80211_registered_device *rdev,
6733 struct net_device *netdev,
6734 u32 pid, u32 seq, int flags, u32 cmd)
6735{
6736 void *hdr;
6737
6738 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6739 if (!hdr)
6740 return -1;
6741
6742 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6743 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6744
6745 return genlmsg_end(msg, hdr);
6746
6747 nla_put_failure:
6748 genlmsg_cancel(msg, hdr);
6749 return -EMSGSIZE;
6750}
6751
Johannes Berga538e2d2009-06-16 19:56:42 +02006752void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6753 struct net_device *netdev)
6754{
6755 struct sk_buff *msg;
6756
6757 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6758 if (!msg)
6759 return;
6760
6761 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6762 NL80211_CMD_TRIGGER_SCAN) < 0) {
6763 nlmsg_free(msg);
6764 return;
6765 }
6766
Johannes Berg463d0182009-07-14 00:33:35 +02006767 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6768 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006769}
6770
Johannes Berg2a519312009-02-10 21:25:55 +01006771void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6772 struct net_device *netdev)
6773{
6774 struct sk_buff *msg;
6775
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006776 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006777 if (!msg)
6778 return;
6779
Johannes Berga538e2d2009-06-16 19:56:42 +02006780 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6781 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006782 nlmsg_free(msg);
6783 return;
6784 }
6785
Johannes Berg463d0182009-07-14 00:33:35 +02006786 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6787 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006788}
6789
6790void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6791 struct net_device *netdev)
6792{
6793 struct sk_buff *msg;
6794
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006795 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006796 if (!msg)
6797 return;
6798
Johannes Berga538e2d2009-06-16 19:56:42 +02006799 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6800 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006801 nlmsg_free(msg);
6802 return;
6803 }
6804
Johannes Berg463d0182009-07-14 00:33:35 +02006805 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6806 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006807}
6808
Luciano Coelho807f8a82011-05-11 17:09:35 +03006809void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6810 struct net_device *netdev)
6811{
6812 struct sk_buff *msg;
6813
6814 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6815 if (!msg)
6816 return;
6817
6818 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6819 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6820 nlmsg_free(msg);
6821 return;
6822 }
6823
6824 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6825 nl80211_scan_mcgrp.id, GFP_KERNEL);
6826}
6827
6828void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6829 struct net_device *netdev, u32 cmd)
6830{
6831 struct sk_buff *msg;
6832
6833 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6834 if (!msg)
6835 return;
6836
6837 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6838 nlmsg_free(msg);
6839 return;
6840 }
6841
6842 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6843 nl80211_scan_mcgrp.id, GFP_KERNEL);
6844}
6845
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006846/*
6847 * This can happen on global regulatory changes or device specific settings
6848 * based on custom world regulatory domains.
6849 */
6850void nl80211_send_reg_change_event(struct regulatory_request *request)
6851{
6852 struct sk_buff *msg;
6853 void *hdr;
6854
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006855 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006856 if (!msg)
6857 return;
6858
6859 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6860 if (!hdr) {
6861 nlmsg_free(msg);
6862 return;
6863 }
6864
6865 /* Userspace can always count this one always being set */
6866 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6867
6868 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6869 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6870 NL80211_REGDOM_TYPE_WORLD);
6871 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6872 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6873 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6874 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6875 request->intersect)
6876 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6877 NL80211_REGDOM_TYPE_INTERSECTION);
6878 else {
6879 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6880 NL80211_REGDOM_TYPE_COUNTRY);
6881 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6882 }
6883
6884 if (wiphy_idx_valid(request->wiphy_idx))
6885 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6886
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006887 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006888
Johannes Bergbc43b282009-07-25 10:54:13 +02006889 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006890 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006891 GFP_ATOMIC);
6892 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006893
6894 return;
6895
6896nla_put_failure:
6897 genlmsg_cancel(msg, hdr);
6898 nlmsg_free(msg);
6899}
6900
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006901static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6902 struct net_device *netdev,
6903 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006904 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006905{
6906 struct sk_buff *msg;
6907 void *hdr;
6908
Johannes Berge6d6e342009-07-01 21:26:47 +02006909 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006910 if (!msg)
6911 return;
6912
6913 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6914 if (!hdr) {
6915 nlmsg_free(msg);
6916 return;
6917 }
6918
6919 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6920 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6921 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6922
Johannes Berg3b7b72e2011-10-22 19:05:51 +02006923 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006924
Johannes Berg463d0182009-07-14 00:33:35 +02006925 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6926 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006927 return;
6928
6929 nla_put_failure:
6930 genlmsg_cancel(msg, hdr);
6931 nlmsg_free(msg);
6932}
6933
6934void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006935 struct net_device *netdev, const u8 *buf,
6936 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006937{
6938 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006939 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006940}
6941
6942void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6943 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006944 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006945{
Johannes Berge6d6e342009-07-01 21:26:47 +02006946 nl80211_send_mlme_event(rdev, netdev, buf, len,
6947 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006948}
6949
Jouni Malinen53b46b82009-03-27 20:53:56 +02006950void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006951 struct net_device *netdev, const u8 *buf,
6952 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006953{
6954 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006955 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006956}
6957
Jouni Malinen53b46b82009-03-27 20:53:56 +02006958void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6959 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006960 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006961{
6962 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006963 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006964}
6965
Jouni Malinencf4e5942010-12-16 00:52:40 +02006966void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6967 struct net_device *netdev, const u8 *buf,
6968 size_t len, gfp_t gfp)
6969{
6970 nl80211_send_mlme_event(rdev, netdev, buf, len,
6971 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6972}
6973
6974void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6975 struct net_device *netdev, const u8 *buf,
6976 size_t len, gfp_t gfp)
6977{
6978 nl80211_send_mlme_event(rdev, netdev, buf, len,
6979 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6980}
6981
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006982static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6983 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006984 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006985{
6986 struct sk_buff *msg;
6987 void *hdr;
6988
Johannes Berge6d6e342009-07-01 21:26:47 +02006989 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006990 if (!msg)
6991 return;
6992
6993 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6994 if (!hdr) {
6995 nlmsg_free(msg);
6996 return;
6997 }
6998
6999 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7000 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7001 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7002 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7003
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007004 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007005
Johannes Berg463d0182009-07-14 00:33:35 +02007006 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7007 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007008 return;
7009
7010 nla_put_failure:
7011 genlmsg_cancel(msg, hdr);
7012 nlmsg_free(msg);
7013}
7014
7015void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007016 struct net_device *netdev, const u8 *addr,
7017 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007018{
7019 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007020 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007021}
7022
7023void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007024 struct net_device *netdev, const u8 *addr,
7025 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007026{
Johannes Berge6d6e342009-07-01 21:26:47 +02007027 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7028 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007029}
7030
Samuel Ortizb23aa672009-07-01 21:26:54 +02007031void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7032 struct net_device *netdev, const u8 *bssid,
7033 const u8 *req_ie, size_t req_ie_len,
7034 const u8 *resp_ie, size_t resp_ie_len,
7035 u16 status, gfp_t gfp)
7036{
7037 struct sk_buff *msg;
7038 void *hdr;
7039
7040 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7041 if (!msg)
7042 return;
7043
7044 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7045 if (!hdr) {
7046 nlmsg_free(msg);
7047 return;
7048 }
7049
7050 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7051 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7052 if (bssid)
7053 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7054 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7055 if (req_ie)
7056 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7057 if (resp_ie)
7058 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7059
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007060 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007061
Johannes Berg463d0182009-07-14 00:33:35 +02007062 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7063 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007064 return;
7065
7066 nla_put_failure:
7067 genlmsg_cancel(msg, hdr);
7068 nlmsg_free(msg);
7069
7070}
7071
7072void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7073 struct net_device *netdev, const u8 *bssid,
7074 const u8 *req_ie, size_t req_ie_len,
7075 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7076{
7077 struct sk_buff *msg;
7078 void *hdr;
7079
7080 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7081 if (!msg)
7082 return;
7083
7084 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7085 if (!hdr) {
7086 nlmsg_free(msg);
7087 return;
7088 }
7089
7090 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7091 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7092 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7093 if (req_ie)
7094 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7095 if (resp_ie)
7096 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7097
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007098 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007099
Johannes Berg463d0182009-07-14 00:33:35 +02007100 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7101 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007102 return;
7103
7104 nla_put_failure:
7105 genlmsg_cancel(msg, hdr);
7106 nlmsg_free(msg);
7107
7108}
7109
7110void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7111 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007112 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007113{
7114 struct sk_buff *msg;
7115 void *hdr;
7116
Johannes Berg667503d2009-07-07 03:56:11 +02007117 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007118 if (!msg)
7119 return;
7120
7121 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7122 if (!hdr) {
7123 nlmsg_free(msg);
7124 return;
7125 }
7126
7127 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7128 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7129 if (from_ap && reason)
7130 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7131 if (from_ap)
7132 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7133 if (ie)
7134 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7135
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007136 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007137
Johannes Berg463d0182009-07-14 00:33:35 +02007138 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7139 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007140 return;
7141
7142 nla_put_failure:
7143 genlmsg_cancel(msg, hdr);
7144 nlmsg_free(msg);
7145
7146}
7147
Johannes Berg04a773a2009-04-19 21:24:32 +02007148void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7149 struct net_device *netdev, const u8 *bssid,
7150 gfp_t gfp)
7151{
7152 struct sk_buff *msg;
7153 void *hdr;
7154
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007156 if (!msg)
7157 return;
7158
7159 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7160 if (!hdr) {
7161 nlmsg_free(msg);
7162 return;
7163 }
7164
7165 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7167 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7168
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007169 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007170
Johannes Berg463d0182009-07-14 00:33:35 +02007171 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7172 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007173 return;
7174
7175 nla_put_failure:
7176 genlmsg_cancel(msg, hdr);
7177 nlmsg_free(msg);
7178}
7179
Javier Cardonac93b5e72011-04-07 15:08:34 -07007180void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7181 struct net_device *netdev,
7182 const u8 *macaddr, const u8* ie, u8 ie_len,
7183 gfp_t gfp)
7184{
7185 struct sk_buff *msg;
7186 void *hdr;
7187
7188 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7189 if (!msg)
7190 return;
7191
7192 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7193 if (!hdr) {
7194 nlmsg_free(msg);
7195 return;
7196 }
7197
7198 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7199 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7200 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7201 if (ie_len && ie)
7202 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7203
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007204 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007205
7206 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7207 nl80211_mlme_mcgrp.id, gfp);
7208 return;
7209
7210 nla_put_failure:
7211 genlmsg_cancel(msg, hdr);
7212 nlmsg_free(msg);
7213}
7214
Jouni Malinena3b8b052009-03-27 21:59:49 +02007215void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7216 struct net_device *netdev, const u8 *addr,
7217 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007218 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007219{
7220 struct sk_buff *msg;
7221 void *hdr;
7222
Johannes Berge6d6e342009-07-01 21:26:47 +02007223 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007224 if (!msg)
7225 return;
7226
7227 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7228 if (!hdr) {
7229 nlmsg_free(msg);
7230 return;
7231 }
7232
7233 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7235 if (addr)
7236 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7237 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03007238 if (key_id != -1)
7239 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007240 if (tsc)
7241 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7242
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007243 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007244
Johannes Berg463d0182009-07-14 00:33:35 +02007245 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7246 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007247 return;
7248
7249 nla_put_failure:
7250 genlmsg_cancel(msg, hdr);
7251 nlmsg_free(msg);
7252}
7253
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007254void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7255 struct ieee80211_channel *channel_before,
7256 struct ieee80211_channel *channel_after)
7257{
7258 struct sk_buff *msg;
7259 void *hdr;
7260 struct nlattr *nl_freq;
7261
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007262 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007263 if (!msg)
7264 return;
7265
7266 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7267 if (!hdr) {
7268 nlmsg_free(msg);
7269 return;
7270 }
7271
7272 /*
7273 * Since we are applying the beacon hint to a wiphy we know its
7274 * wiphy_idx is valid
7275 */
7276 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7277
7278 /* Before */
7279 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7280 if (!nl_freq)
7281 goto nla_put_failure;
7282 if (nl80211_msg_put_channel(msg, channel_before))
7283 goto nla_put_failure;
7284 nla_nest_end(msg, nl_freq);
7285
7286 /* After */
7287 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7288 if (!nl_freq)
7289 goto nla_put_failure;
7290 if (nl80211_msg_put_channel(msg, channel_after))
7291 goto nla_put_failure;
7292 nla_nest_end(msg, nl_freq);
7293
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007294 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007295
Johannes Berg463d0182009-07-14 00:33:35 +02007296 rcu_read_lock();
7297 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7298 GFP_ATOMIC);
7299 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007300
7301 return;
7302
7303nla_put_failure:
7304 genlmsg_cancel(msg, hdr);
7305 nlmsg_free(msg);
7306}
7307
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007308static void nl80211_send_remain_on_chan_event(
7309 int cmd, struct cfg80211_registered_device *rdev,
7310 struct net_device *netdev, u64 cookie,
7311 struct ieee80211_channel *chan,
7312 enum nl80211_channel_type channel_type,
7313 unsigned int duration, gfp_t gfp)
7314{
7315 struct sk_buff *msg;
7316 void *hdr;
7317
7318 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7319 if (!msg)
7320 return;
7321
7322 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7323 if (!hdr) {
7324 nlmsg_free(msg);
7325 return;
7326 }
7327
7328 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7329 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7330 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7331 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7332 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7333
7334 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7335 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7336
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007337 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007338
7339 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7340 nl80211_mlme_mcgrp.id, gfp);
7341 return;
7342
7343 nla_put_failure:
7344 genlmsg_cancel(msg, hdr);
7345 nlmsg_free(msg);
7346}
7347
7348void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7349 struct net_device *netdev, u64 cookie,
7350 struct ieee80211_channel *chan,
7351 enum nl80211_channel_type channel_type,
7352 unsigned int duration, gfp_t gfp)
7353{
7354 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7355 rdev, netdev, cookie, chan,
7356 channel_type, duration, gfp);
7357}
7358
7359void nl80211_send_remain_on_channel_cancel(
7360 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7361 u64 cookie, struct ieee80211_channel *chan,
7362 enum nl80211_channel_type channel_type, gfp_t gfp)
7363{
7364 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7365 rdev, netdev, cookie, chan,
7366 channel_type, 0, gfp);
7367}
7368
Johannes Berg98b62182009-12-23 13:15:44 +01007369void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7370 struct net_device *dev, const u8 *mac_addr,
7371 struct station_info *sinfo, gfp_t gfp)
7372{
7373 struct sk_buff *msg;
7374
7375 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7376 if (!msg)
7377 return;
7378
7379 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7380 nlmsg_free(msg);
7381 return;
7382 }
7383
7384 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7385 nl80211_mlme_mcgrp.id, gfp);
7386}
7387
Jouni Malinenec15e682011-03-23 15:29:52 +02007388void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7389 struct net_device *dev, const u8 *mac_addr,
7390 gfp_t gfp)
7391{
7392 struct sk_buff *msg;
7393 void *hdr;
7394
7395 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7396 if (!msg)
7397 return;
7398
7399 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7400 if (!hdr) {
7401 nlmsg_free(msg);
7402 return;
7403 }
7404
7405 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7406 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7407
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007408 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02007409
7410 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7411 nl80211_mlme_mcgrp.id, gfp);
7412 return;
7413
7414 nla_put_failure:
7415 genlmsg_cancel(msg, hdr);
7416 nlmsg_free(msg);
7417}
7418
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007419static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7420 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01007421{
7422 struct wireless_dev *wdev = dev->ieee80211_ptr;
7423 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7424 struct sk_buff *msg;
7425 void *hdr;
7426 int err;
7427 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7428
7429 if (!nlpid)
7430 return false;
7431
7432 msg = nlmsg_new(100, gfp);
7433 if (!msg)
7434 return true;
7435
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007436 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01007437 if (!hdr) {
7438 nlmsg_free(msg);
7439 return true;
7440 }
7441
7442 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7444 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7445
7446 err = genlmsg_end(msg, hdr);
7447 if (err < 0) {
7448 nlmsg_free(msg);
7449 return true;
7450 }
7451
7452 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7453 return true;
7454
7455 nla_put_failure:
7456 genlmsg_cancel(msg, hdr);
7457 nlmsg_free(msg);
7458 return true;
7459}
7460
Johannes Bergb92ab5d2011-11-04 11:18:19 +01007461bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7462{
7463 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7464 addr, gfp);
7465}
7466
7467bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7468 const u8 *addr, gfp_t gfp)
7469{
7470 return __nl80211_unexpected_frame(dev,
7471 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7472 addr, gfp);
7473}
7474
Johannes Berg2e161f72010-08-12 15:38:38 +02007475int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7476 struct net_device *netdev, u32 nlpid,
7477 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007478{
7479 struct sk_buff *msg;
7480 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02007481
7482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7483 if (!msg)
7484 return -ENOMEM;
7485
Johannes Berg2e161f72010-08-12 15:38:38 +02007486 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007487 if (!hdr) {
7488 nlmsg_free(msg);
7489 return -ENOMEM;
7490 }
7491
7492 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7493 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7494 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7495 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7496
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007497 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007498
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007499 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007500
7501 nla_put_failure:
7502 genlmsg_cancel(msg, hdr);
7503 nlmsg_free(msg);
7504 return -ENOBUFS;
7505}
7506
Johannes Berg2e161f72010-08-12 15:38:38 +02007507void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7508 struct net_device *netdev, u64 cookie,
7509 const u8 *buf, size_t len, bool ack,
7510 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007511{
7512 struct sk_buff *msg;
7513 void *hdr;
7514
7515 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7516 if (!msg)
7517 return;
7518
Johannes Berg2e161f72010-08-12 15:38:38 +02007519 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007520 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_FRAME, len, buf);
7528 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7529 if (ack)
7530 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7531
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007532 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02007533
7534 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7535 return;
7536
7537 nla_put_failure:
7538 genlmsg_cancel(msg, hdr);
7539 nlmsg_free(msg);
7540}
7541
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007542void
7543nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7544 struct net_device *netdev,
7545 enum nl80211_cqm_rssi_threshold_event rssi_event,
7546 gfp_t gfp)
7547{
7548 struct sk_buff *msg;
7549 struct nlattr *pinfoattr;
7550 void *hdr;
7551
7552 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7553 if (!msg)
7554 return;
7555
7556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7557 if (!hdr) {
7558 nlmsg_free(msg);
7559 return;
7560 }
7561
7562 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7563 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7564
7565 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7566 if (!pinfoattr)
7567 goto nla_put_failure;
7568
7569 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7570 rssi_event);
7571
7572 nla_nest_end(msg, pinfoattr);
7573
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007574 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007575
7576 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7577 nl80211_mlme_mcgrp.id, gfp);
7578 return;
7579
7580 nla_put_failure:
7581 genlmsg_cancel(msg, hdr);
7582 nlmsg_free(msg);
7583}
7584
Johannes Berge5497d72011-07-05 16:35:40 +02007585void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7586 struct net_device *netdev, const u8 *bssid,
7587 const u8 *replay_ctr, gfp_t gfp)
7588{
7589 struct sk_buff *msg;
7590 struct nlattr *rekey_attr;
7591 void *hdr;
7592
7593 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7594 if (!msg)
7595 return;
7596
7597 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7598 if (!hdr) {
7599 nlmsg_free(msg);
7600 return;
7601 }
7602
7603 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7604 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7605 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7606
7607 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7608 if (!rekey_attr)
7609 goto nla_put_failure;
7610
7611 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7612 NL80211_REPLAY_CTR_LEN, replay_ctr);
7613
7614 nla_nest_end(msg, rekey_attr);
7615
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007616 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02007617
7618 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7619 nl80211_mlme_mcgrp.id, gfp);
7620 return;
7621
7622 nla_put_failure:
7623 genlmsg_cancel(msg, hdr);
7624 nlmsg_free(msg);
7625}
7626
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007627void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7628 struct net_device *netdev, int index,
7629 const u8 *bssid, bool preauth, gfp_t gfp)
7630{
7631 struct sk_buff *msg;
7632 struct nlattr *attr;
7633 void *hdr;
7634
7635 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7636 if (!msg)
7637 return;
7638
7639 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7640 if (!hdr) {
7641 nlmsg_free(msg);
7642 return;
7643 }
7644
7645 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7646 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7647
7648 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7649 if (!attr)
7650 goto nla_put_failure;
7651
7652 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7653 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7654 if (preauth)
7655 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7656
7657 nla_nest_end(msg, attr);
7658
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007659 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03007660
7661 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7662 nl80211_mlme_mcgrp.id, gfp);
7663 return;
7664
7665 nla_put_failure:
7666 genlmsg_cancel(msg, hdr);
7667 nlmsg_free(msg);
7668}
7669
Johannes Bergc063dbf2010-11-24 08:10:05 +01007670void
7671nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7672 struct net_device *netdev, const u8 *peer,
7673 u32 num_packets, gfp_t gfp)
7674{
7675 struct sk_buff *msg;
7676 struct nlattr *pinfoattr;
7677 void *hdr;
7678
7679 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7680 if (!msg)
7681 return;
7682
7683 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7684 if (!hdr) {
7685 nlmsg_free(msg);
7686 return;
7687 }
7688
7689 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7690 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7691 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7692
7693 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7694 if (!pinfoattr)
7695 goto nla_put_failure;
7696
7697 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7698
7699 nla_nest_end(msg, pinfoattr);
7700
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007701 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01007702
7703 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7704 nl80211_mlme_mcgrp.id, gfp);
7705 return;
7706
7707 nla_put_failure:
7708 genlmsg_cancel(msg, hdr);
7709 nlmsg_free(msg);
7710}
7711
Johannes Berg7f6cf312011-11-04 11:18:15 +01007712void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7713 u64 cookie, bool acked, gfp_t gfp)
7714{
7715 struct wireless_dev *wdev = dev->ieee80211_ptr;
7716 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7717 struct sk_buff *msg;
7718 void *hdr;
7719 int err;
7720
7721 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7722 if (!msg)
7723 return;
7724
7725 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7726 if (!hdr) {
7727 nlmsg_free(msg);
7728 return;
7729 }
7730
7731 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7732 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7733 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7734 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7735 if (acked)
7736 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7737
7738 err = genlmsg_end(msg, hdr);
7739 if (err < 0) {
7740 nlmsg_free(msg);
7741 return;
7742 }
7743
7744 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7745 nl80211_mlme_mcgrp.id, gfp);
7746 return;
7747
7748 nla_put_failure:
7749 genlmsg_cancel(msg, hdr);
7750 nlmsg_free(msg);
7751}
7752EXPORT_SYMBOL(cfg80211_probe_status);
7753
Johannes Berg5e760232011-11-04 11:18:17 +01007754void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7755 const u8 *frame, size_t len,
7756 int freq, gfp_t gfp)
7757{
7758 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7759 struct sk_buff *msg;
7760 void *hdr;
7761 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7762
7763 if (!nlpid)
7764 return;
7765
7766 msg = nlmsg_new(len + 100, gfp);
7767 if (!msg)
7768 return;
7769
7770 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7771 if (!hdr) {
7772 nlmsg_free(msg);
7773 return;
7774 }
7775
7776 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7777 if (freq)
7778 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7779 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7780
7781 genlmsg_end(msg, hdr);
7782
7783 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7784 return;
7785
7786 nla_put_failure:
7787 genlmsg_cancel(msg, hdr);
7788 nlmsg_free(msg);
7789}
7790EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7791
Jouni Malinen026331c2010-02-15 12:53:10 +02007792static int nl80211_netlink_notify(struct notifier_block * nb,
7793 unsigned long state,
7794 void *_notify)
7795{
7796 struct netlink_notify *notify = _notify;
7797 struct cfg80211_registered_device *rdev;
7798 struct wireless_dev *wdev;
7799
7800 if (state != NETLINK_URELEASE)
7801 return NOTIFY_DONE;
7802
7803 rcu_read_lock();
7804
Johannes Berg5e760232011-11-04 11:18:17 +01007805 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +02007806 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007807 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01007808 if (rdev->ap_beacons_nlpid == notify->pid)
7809 rdev->ap_beacons_nlpid = 0;
7810 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007811
7812 rcu_read_unlock();
7813
7814 return NOTIFY_DONE;
7815}
7816
7817static struct notifier_block nl80211_netlink_notifier = {
7818 .notifier_call = nl80211_netlink_notify,
7819};
7820
Johannes Berg55682962007-09-20 13:09:35 -04007821/* initialisation/exit functions */
7822
7823int nl80211_init(void)
7824{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007825 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007826
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007827 err = genl_register_family_with_ops(&nl80211_fam,
7828 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007829 if (err)
7830 return err;
7831
Johannes Berg55682962007-09-20 13:09:35 -04007832 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7833 if (err)
7834 goto err_out;
7835
Johannes Berg2a519312009-02-10 21:25:55 +01007836 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7837 if (err)
7838 goto err_out;
7839
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007840 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7841 if (err)
7842 goto err_out;
7843
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007844 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7845 if (err)
7846 goto err_out;
7847
Johannes Bergaff89a92009-07-01 21:26:51 +02007848#ifdef CONFIG_NL80211_TESTMODE
7849 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7850 if (err)
7851 goto err_out;
7852#endif
7853
Jouni Malinen026331c2010-02-15 12:53:10 +02007854 err = netlink_register_notifier(&nl80211_netlink_notifier);
7855 if (err)
7856 goto err_out;
7857
Johannes Berg55682962007-09-20 13:09:35 -04007858 return 0;
7859 err_out:
7860 genl_unregister_family(&nl80211_fam);
7861 return err;
7862}
7863
7864void nl80211_exit(void)
7865{
Jouni Malinen026331c2010-02-15 12:53:10 +02007866 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007867 genl_unregister_family(&nl80211_fam);
7868}