blob: 9deb12f73c448f6e1f914ac299e04aa3a93494df [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Johannes Berg08645122009-05-11 13:54:58 +02004 * Copyright 2006-2009 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>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg55682962007-09-20 13:09:35 -040017#include <net/genetlink.h>
18#include <net/cfg80211.h>
19#include "core.h"
20#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070021#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040022
23/* the netlink family */
24static struct genl_family nl80211_fam = {
25 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
26 .name = "nl80211", /* have users key off the name instead */
27 .hdrsize = 0, /* no private header */
28 .version = 1, /* no particular meaning now */
29 .maxattr = NL80211_ATTR_MAX,
30};
31
Johannes Berg79c97e92009-07-07 03:56:12 +020032/* internal helper: get rdev and dev */
33static int get_rdev_dev_by_info_ifindex(struct nlattr **attrs,
34 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040035 struct net_device **dev)
36{
37 int ifindex;
38
Johannes Bergbba95fe2008-07-29 13:22:51 +020039 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040040 return -EINVAL;
41
Johannes Bergbba95fe2008-07-29 13:22:51 +020042 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg55682962007-09-20 13:09:35 -040043 *dev = dev_get_by_index(&init_net, ifindex);
44 if (!*dev)
45 return -ENODEV;
46
Johannes Berg79c97e92009-07-07 03:56:12 +020047 *rdev = cfg80211_get_dev_from_ifindex(ifindex);
48 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040049 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020050 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040051 }
52
53 return 0;
54}
55
56/* policy for the attributes */
57static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
58 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
59 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070060 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020061 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020062 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053063 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020064 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
65 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
66 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
67 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -040068
69 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
70 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
71 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010072
73 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020074 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010075
76 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
77 .len = WLAN_MAX_KEY_LEN },
78 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
79 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
80 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030081 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010082
83 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
84 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
85 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
86 .len = IEEE80211_MAX_DATA_LEN },
87 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
88 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010089 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
90 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
91 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
92 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
93 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010094 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010095 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +020096 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010097 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
98 .len = IEEE80211_MAX_MESH_ID_LEN },
99 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300100
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700101 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
102 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
103
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300104 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
105 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
106 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200107 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300109
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700110 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
111
Jouni Malinen36aedc92008-08-25 11:58:58 +0300112 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
113 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200114
115 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
116 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
117 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100118 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
119 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200120
121 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
122 .len = IEEE80211_MAX_SSID_LEN },
123 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
124 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200125 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300126 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300127 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300128 [NL80211_ATTR_STA_FLAGS2] = {
129 .len = sizeof(struct nl80211_sta_flag_update),
130 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300131 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200132 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
133 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
134 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400135};
136
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100137/* IE validation */
138static bool is_valid_ie_attr(const struct nlattr *attr)
139{
140 const u8 *pos;
141 int len;
142
143 if (!attr)
144 return true;
145
146 pos = nla_data(attr);
147 len = nla_len(attr);
148
149 while (len) {
150 u8 elemlen;
151
152 if (len < 2)
153 return false;
154 len -= 2;
155
156 elemlen = pos[1];
157 if (elemlen > len)
158 return false;
159
160 len -= elemlen;
161 pos += 2 + elemlen;
162 }
163
164 return true;
165}
166
Johannes Berg55682962007-09-20 13:09:35 -0400167/* message building helper */
168static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
169 int flags, u8 cmd)
170{
171 /* since there is no private header just add the generic one */
172 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
173}
174
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400175static int nl80211_msg_put_channel(struct sk_buff *msg,
176 struct ieee80211_channel *chan)
177{
178 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
179 chan->center_freq);
180
181 if (chan->flags & IEEE80211_CHAN_DISABLED)
182 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
183 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
184 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
185 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
186 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
187 if (chan->flags & IEEE80211_CHAN_RADAR)
188 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
189
190 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
191 DBM_TO_MBM(chan->max_power));
192
193 return 0;
194
195 nla_put_failure:
196 return -ENOBUFS;
197}
198
Johannes Berg55682962007-09-20 13:09:35 -0400199/* netlink command implementations */
200
201static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
202 struct cfg80211_registered_device *dev)
203{
204 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100205 struct nlattr *nl_bands, *nl_band;
206 struct nlattr *nl_freqs, *nl_freq;
207 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700208 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100209 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100210 enum ieee80211_band band;
211 struct ieee80211_channel *chan;
212 struct ieee80211_rate *rate;
213 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700214 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400215
216 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
217 if (!hdr)
218 return -1;
219
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500220 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400221 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200222
223 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
224 dev->wiphy.retry_short);
225 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
226 dev->wiphy.retry_long);
227 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
228 dev->wiphy.frag_threshold);
229 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
230 dev->wiphy.rts_threshold);
231
Johannes Berg2a519312009-02-10 21:25:55 +0100232 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
233 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200234 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
235 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100236
Johannes Berg25e47c12009-04-02 20:14:06 +0200237 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
238 sizeof(u32) * dev->wiphy.n_cipher_suites,
239 dev->wiphy.cipher_suites);
240
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700241 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
242 if (!nl_modes)
243 goto nla_put_failure;
244
245 i = 0;
246 while (ifmodes) {
247 if (ifmodes & 1)
248 NLA_PUT_FLAG(msg, i);
249 ifmodes >>= 1;
250 i++;
251 }
252
253 nla_nest_end(msg, nl_modes);
254
Johannes Bergee688b002008-01-24 19:38:39 +0100255 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
256 if (!nl_bands)
257 goto nla_put_failure;
258
259 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
260 if (!dev->wiphy.bands[band])
261 continue;
262
263 nl_band = nla_nest_start(msg, band);
264 if (!nl_band)
265 goto nla_put_failure;
266
Johannes Bergd51626d2008-10-09 12:20:13 +0200267 /* add HT info */
268 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
269 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
270 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
271 &dev->wiphy.bands[band]->ht_cap.mcs);
272 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
273 dev->wiphy.bands[band]->ht_cap.cap);
274 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
275 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
276 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
277 dev->wiphy.bands[band]->ht_cap.ampdu_density);
278 }
279
Johannes Bergee688b002008-01-24 19:38:39 +0100280 /* add frequencies */
281 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
282 if (!nl_freqs)
283 goto nla_put_failure;
284
285 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
286 nl_freq = nla_nest_start(msg, i);
287 if (!nl_freq)
288 goto nla_put_failure;
289
290 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100291
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400292 if (nl80211_msg_put_channel(msg, chan))
293 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200294
Johannes Bergee688b002008-01-24 19:38:39 +0100295 nla_nest_end(msg, nl_freq);
296 }
297
298 nla_nest_end(msg, nl_freqs);
299
300 /* add bitrates */
301 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
302 if (!nl_rates)
303 goto nla_put_failure;
304
305 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
306 nl_rate = nla_nest_start(msg, i);
307 if (!nl_rate)
308 goto nla_put_failure;
309
310 rate = &dev->wiphy.bands[band]->bitrates[i];
311 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
312 rate->bitrate);
313 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
314 NLA_PUT_FLAG(msg,
315 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
316
317 nla_nest_end(msg, nl_rate);
318 }
319
320 nla_nest_end(msg, nl_rates);
321
322 nla_nest_end(msg, nl_band);
323 }
324 nla_nest_end(msg, nl_bands);
325
Johannes Berg8fdc6212009-03-14 09:34:01 +0100326 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
327 if (!nl_cmds)
328 goto nla_put_failure;
329
330 i = 0;
331#define CMD(op, n) \
332 do { \
333 if (dev->ops->op) { \
334 i++; \
335 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
336 } \
337 } while (0)
338
339 CMD(add_virtual_intf, NEW_INTERFACE);
340 CMD(change_virtual_intf, SET_INTERFACE);
341 CMD(add_key, NEW_KEY);
342 CMD(add_beacon, NEW_BEACON);
343 CMD(add_station, NEW_STATION);
344 CMD(add_mpath, NEW_MPATH);
345 CMD(set_mesh_params, SET_MESH_PARAMS);
346 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200347 CMD(auth, AUTHENTICATE);
348 CMD(assoc, ASSOCIATE);
349 CMD(deauth, DEAUTHENTICATE);
350 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200351 CMD(join_ibss, JOIN_IBSS);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100352
353#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200354
Johannes Berg6829c872009-07-02 09:13:27 +0200355 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200356 i++;
357 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
358 }
359
Johannes Berg6829c872009-07-02 09:13:27 +0200360 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200361 i++;
362 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
363 }
364
Johannes Berg8fdc6212009-03-14 09:34:01 +0100365 nla_nest_end(msg, nl_cmds);
366
Johannes Berg55682962007-09-20 13:09:35 -0400367 return genlmsg_end(msg, hdr);
368
369 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700370 genlmsg_cancel(msg, hdr);
371 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400372}
373
374static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
375{
376 int idx = 0;
377 int start = cb->args[0];
378 struct cfg80211_registered_device *dev;
379
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500380 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200381 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200382 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400383 continue;
384 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
385 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200386 dev) < 0) {
387 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400388 break;
Julius Volzb4637272008-07-08 14:02:19 +0200389 }
Johannes Berg55682962007-09-20 13:09:35 -0400390 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500391 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400392
393 cb->args[0] = idx;
394
395 return skb->len;
396}
397
398static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
399{
400 struct sk_buff *msg;
401 struct cfg80211_registered_device *dev;
402
403 dev = cfg80211_get_dev_from_info(info);
404 if (IS_ERR(dev))
405 return PTR_ERR(dev);
406
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700407 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400408 if (!msg)
409 goto out_err;
410
411 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
412 goto out_free;
413
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200414 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400415
416 return genlmsg_unicast(msg, info->snd_pid);
417
418 out_free:
419 nlmsg_free(msg);
420 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200421 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400422 return -ENOBUFS;
423}
424
Jouni Malinen31888482008-10-30 16:59:24 +0200425static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
426 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
427 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
428 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
429 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
430 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
431};
432
433static int parse_txq_params(struct nlattr *tb[],
434 struct ieee80211_txq_params *txq_params)
435{
436 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
437 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
438 !tb[NL80211_TXQ_ATTR_AIFS])
439 return -EINVAL;
440
441 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
442 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
443 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
444 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
445 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
446
447 return 0;
448}
449
Johannes Berg55682962007-09-20 13:09:35 -0400450static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
451{
452 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200453 int result = 0, rem_txq_params = 0;
454 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200455 u32 changed;
456 u8 retry_short = 0, retry_long = 0;
457 u32 frag_threshold = 0, rts_threshold = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400458
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100459 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400460
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100461 mutex_lock(&cfg80211_mutex);
462
Johannes Berg79c97e92009-07-07 03:56:12 +0200463 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100464 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200465 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100466 result = PTR_ERR(rdev);
467 goto unlock;
468 }
469
470 mutex_lock(&rdev->mtx);
471
472 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200473 result = cfg80211_dev_rename(
474 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100475
476 mutex_unlock(&cfg80211_mutex);
477
478 if (result)
479 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400480
Jouni Malinen31888482008-10-30 16:59:24 +0200481 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
482 struct ieee80211_txq_params txq_params;
483 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
484
485 if (!rdev->ops->set_txq_params) {
486 result = -EOPNOTSUPP;
487 goto bad_res;
488 }
489
490 nla_for_each_nested(nl_txq_params,
491 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
492 rem_txq_params) {
493 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
494 nla_data(nl_txq_params),
495 nla_len(nl_txq_params),
496 txq_params_policy);
497 result = parse_txq_params(tb, &txq_params);
498 if (result)
499 goto bad_res;
500
501 result = rdev->ops->set_txq_params(&rdev->wiphy,
502 &txq_params);
503 if (result)
504 goto bad_res;
505 }
506 }
507
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200508 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530509 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200510 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100511 struct ieee80211_sta_ht_cap *ht_cap;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400512 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200513
514 if (!rdev->ops->set_channel) {
515 result = -EOPNOTSUPP;
516 goto bad_res;
517 }
518
Johannes Berg306d6112008-12-08 12:39:04 +0100519 result = -EINVAL;
520
Sujith094d05d2008-12-12 11:57:43 +0530521 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
522 channel_type = nla_get_u32(info->attrs[
523 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
524 if (channel_type != NL80211_CHAN_NO_HT &&
525 channel_type != NL80211_CHAN_HT20 &&
526 channel_type != NL80211_CHAN_HT40PLUS &&
527 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200528 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200529 }
530
531 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
532 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100533
534 /* Primary channel not allowed */
535 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200536 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100537
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400538 if (channel_type == NL80211_CHAN_HT40MINUS &&
539 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS))
540 goto bad_res;
541 else if (channel_type == NL80211_CHAN_HT40PLUS &&
542 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS))
543 goto bad_res;
544
545 /*
546 * At this point we know if that if HT40 was requested
547 * we are allowed to use it and the extension channel
548 * exists.
549 */
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200550
Johannes Berg306d6112008-12-08 12:39:04 +0100551 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
552
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400553 /* no HT capabilities or intolerant */
554 if (channel_type != NL80211_CHAN_NO_HT) {
555 if (!ht_cap->ht_supported)
556 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100557 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
558 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200559 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200560 }
561
562 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530563 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200564 if (result)
565 goto bad_res;
566 }
567
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200568 changed = 0;
569
570 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
571 retry_short = nla_get_u8(
572 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
573 if (retry_short == 0) {
574 result = -EINVAL;
575 goto bad_res;
576 }
577 changed |= WIPHY_PARAM_RETRY_SHORT;
578 }
579
580 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
581 retry_long = nla_get_u8(
582 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
583 if (retry_long == 0) {
584 result = -EINVAL;
585 goto bad_res;
586 }
587 changed |= WIPHY_PARAM_RETRY_LONG;
588 }
589
590 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
591 frag_threshold = nla_get_u32(
592 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
593 if (frag_threshold < 256) {
594 result = -EINVAL;
595 goto bad_res;
596 }
597 if (frag_threshold != (u32) -1) {
598 /*
599 * Fragments (apart from the last one) are required to
600 * have even length. Make the fragmentation code
601 * simpler by stripping LSB should someone try to use
602 * odd threshold value.
603 */
604 frag_threshold &= ~0x1;
605 }
606 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
607 }
608
609 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
610 rts_threshold = nla_get_u32(
611 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
612 changed |= WIPHY_PARAM_RTS_THRESHOLD;
613 }
614
615 if (changed) {
616 u8 old_retry_short, old_retry_long;
617 u32 old_frag_threshold, old_rts_threshold;
618
619 if (!rdev->ops->set_wiphy_params) {
620 result = -EOPNOTSUPP;
621 goto bad_res;
622 }
623
624 old_retry_short = rdev->wiphy.retry_short;
625 old_retry_long = rdev->wiphy.retry_long;
626 old_frag_threshold = rdev->wiphy.frag_threshold;
627 old_rts_threshold = rdev->wiphy.rts_threshold;
628
629 if (changed & WIPHY_PARAM_RETRY_SHORT)
630 rdev->wiphy.retry_short = retry_short;
631 if (changed & WIPHY_PARAM_RETRY_LONG)
632 rdev->wiphy.retry_long = retry_long;
633 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
634 rdev->wiphy.frag_threshold = frag_threshold;
635 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
636 rdev->wiphy.rts_threshold = rts_threshold;
637
638 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
639 if (result) {
640 rdev->wiphy.retry_short = old_retry_short;
641 rdev->wiphy.retry_long = old_retry_long;
642 rdev->wiphy.frag_threshold = old_frag_threshold;
643 rdev->wiphy.rts_threshold = old_rts_threshold;
644 }
645 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200646
Johannes Berg306d6112008-12-08 12:39:04 +0100647 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100648 mutex_unlock(&rdev->mtx);
649 unlock:
650 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400651 return result;
652}
653
654
655static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200656 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400657 struct net_device *dev)
658{
659 void *hdr;
660
661 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
662 if (!hdr)
663 return -1;
664
665 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200666 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400667 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200668 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400669 return genlmsg_end(msg, hdr);
670
671 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700672 genlmsg_cancel(msg, hdr);
673 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400674}
675
676static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
677{
678 int wp_idx = 0;
679 int if_idx = 0;
680 int wp_start = cb->args[0];
681 int if_start = cb->args[1];
682 struct cfg80211_registered_device *dev;
683 struct wireless_dev *wdev;
684
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500685 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200686 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200687 if (wp_idx < wp_start) {
688 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400689 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200690 }
Johannes Berg55682962007-09-20 13:09:35 -0400691 if_idx = 0;
692
693 mutex_lock(&dev->devlist_mtx);
694 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200695 if (if_idx < if_start) {
696 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400697 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200698 }
Johannes Berg55682962007-09-20 13:09:35 -0400699 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
700 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergd7264052009-04-19 16:23:20 +0200701 dev, wdev->netdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200702 mutex_unlock(&dev->devlist_mtx);
703 goto out;
704 }
705 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400706 }
707 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200708
709 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400710 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200711 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500712 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400713
714 cb->args[0] = wp_idx;
715 cb->args[1] = if_idx;
716
717 return skb->len;
718}
719
720static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
721{
722 struct sk_buff *msg;
723 struct cfg80211_registered_device *dev;
724 struct net_device *netdev;
725 int err;
726
Johannes Berg79c97e92009-07-07 03:56:12 +0200727 err = get_rdev_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400728 if (err)
729 return err;
730
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700731 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400732 if (!msg)
733 goto out_err;
734
Johannes Bergd7264052009-04-19 16:23:20 +0200735 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
736 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400737 goto out_free;
738
739 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200740 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400741
742 return genlmsg_unicast(msg, info->snd_pid);
743
744 out_free:
745 nlmsg_free(msg);
746 out_err:
747 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200748 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400749 return -ENOBUFS;
750}
751
Michael Wu66f7ac52008-01-31 19:48:22 +0100752static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
753 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
754 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
755 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
756 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
757 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
758};
759
760static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
761{
762 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
763 int flag;
764
765 *mntrflags = 0;
766
767 if (!nla)
768 return -EINVAL;
769
770 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
771 nla, mntr_flags_policy))
772 return -EINVAL;
773
774 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
775 if (flags[flag])
776 *mntrflags |= (1<<flag);
777
778 return 0;
779}
780
Johannes Berg55682962007-09-20 13:09:35 -0400781static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
782{
Johannes Berg79c97e92009-07-07 03:56:12 +0200783 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100784 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +0200785 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200786 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -0400787 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200788 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100789 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400790
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100791 memset(&params, 0, sizeof(params));
792
Johannes Berg3b858752009-03-12 09:55:09 +0100793 rtnl_lock();
794
Johannes Berg79c97e92009-07-07 03:56:12 +0200795 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400796 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100797 goto unlock_rtnl;
798
Johannes Berg04a773a2009-04-19 21:24:32 +0200799 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400800
Johannes Berg723b0382008-09-16 20:22:09 +0200801 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100802 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +0200803 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100804 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200805 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100806 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200807 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100808 }
Johannes Berg723b0382008-09-16 20:22:09 +0200809 }
810
Johannes Berg79c97e92009-07-07 03:56:12 +0200811 if (!rdev->ops->change_virtual_intf ||
812 !(rdev->wiphy.interface_modes & (1 << ntype))) {
Johannes Berg55682962007-09-20 13:09:35 -0400813 err = -EOPNOTSUPP;
814 goto unlock;
815 }
816
Johannes Berg92ffe052008-09-16 20:39:36 +0200817 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200818 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200819 err = -EINVAL;
820 goto unlock;
821 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100822 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
823 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100824 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100825 }
826
Johannes Berg92ffe052008-09-16 20:39:36 +0200827 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200828 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200829 err = -EINVAL;
830 goto unlock;
831 }
832 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
833 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100834 if (err)
835 goto unlock;
836
837 flags = &_flags;
838 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +0200839 }
Johannes Berg3b858752009-03-12 09:55:09 +0100840
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100841 if (change)
Johannes Berg79c97e92009-07-07 03:56:12 +0200842 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
Johannes Berg04a773a2009-04-19 21:24:32 +0200843 ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100844 else
845 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +0200846
Johannes Berge36d56b2009-06-09 21:04:43 +0200847 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
Johannes Berg04a773a2009-04-19 21:24:32 +0200848
Johannes Berge36d56b2009-06-09 21:04:43 +0200849 if (!err && (ntype != otype)) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200850 if (otype == NL80211_IFTYPE_ADHOC)
Johannes Berg9d308422009-04-20 18:43:46 +0200851 cfg80211_clear_ibss(dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +0200852 }
Johannes Berg60719ff2008-09-16 14:55:09 +0200853
Johannes Berg55682962007-09-20 13:09:35 -0400854 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +0200855 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +0200856 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +0100857 unlock_rtnl:
858 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400859 return err;
860}
861
862static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
863{
Johannes Berg79c97e92009-07-07 03:56:12 +0200864 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100865 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400866 int err;
867 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100868 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400869
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100870 memset(&params, 0, sizeof(params));
871
Johannes Berg55682962007-09-20 13:09:35 -0400872 if (!info->attrs[NL80211_ATTR_IFNAME])
873 return -EINVAL;
874
875 if (info->attrs[NL80211_ATTR_IFTYPE]) {
876 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
877 if (type > NL80211_IFTYPE_MAX)
878 return -EINVAL;
879 }
880
Johannes Berg3b858752009-03-12 09:55:09 +0100881 rtnl_lock();
882
Johannes Berg79c97e92009-07-07 03:56:12 +0200883 rdev = cfg80211_get_dev_from_info(info);
884 if (IS_ERR(rdev)) {
885 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +0100886 goto unlock_rtnl;
887 }
Johannes Berg55682962007-09-20 13:09:35 -0400888
Johannes Berg79c97e92009-07-07 03:56:12 +0200889 if (!rdev->ops->add_virtual_intf ||
890 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400891 err = -EOPNOTSUPP;
892 goto unlock;
893 }
894
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100895 if (type == NL80211_IFTYPE_MESH_POINT &&
896 info->attrs[NL80211_ATTR_MESH_ID]) {
897 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
898 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
899 }
900
Michael Wu66f7ac52008-01-31 19:48:22 +0100901 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
902 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
903 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +0200904 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100905 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100906 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100907
Johannes Berg55682962007-09-20 13:09:35 -0400908 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +0200909 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +0100910 unlock_rtnl:
911 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400912 return err;
913}
914
915static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
916{
Johannes Berg79c97e92009-07-07 03:56:12 +0200917 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400918 int ifindex, err;
919 struct net_device *dev;
920
Johannes Berg3b858752009-03-12 09:55:09 +0100921 rtnl_lock();
922
Johannes Berg79c97e92009-07-07 03:56:12 +0200923 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400924 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100925 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -0400926 ifindex = dev->ifindex;
927 dev_put(dev);
928
Johannes Berg79c97e92009-07-07 03:56:12 +0200929 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -0400930 err = -EOPNOTSUPP;
931 goto out;
932 }
933
Johannes Berg79c97e92009-07-07 03:56:12 +0200934 err = rdev->ops->del_virtual_intf(&rdev->wiphy, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -0400935
936 out:
Johannes Berg79c97e92009-07-07 03:56:12 +0200937 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +0100938 unlock_rtnl:
939 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400940 return err;
941}
942
Johannes Berg41ade002007-12-19 02:03:29 +0100943struct get_key_cookie {
944 struct sk_buff *msg;
945 int error;
946};
947
948static void get_key_callback(void *c, struct key_params *params)
949{
950 struct get_key_cookie *cookie = c;
951
952 if (params->key)
953 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
954 params->key_len, params->key);
955
956 if (params->seq)
957 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
958 params->seq_len, params->seq);
959
960 if (params->cipher)
961 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
962 params->cipher);
963
964 return;
965 nla_put_failure:
966 cookie->error = 1;
967}
968
969static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
970{
Johannes Berg79c97e92009-07-07 03:56:12 +0200971 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +0100972 int err;
973 struct net_device *dev;
974 u8 key_idx = 0;
975 u8 *mac_addr = NULL;
976 struct get_key_cookie cookie = {
977 .error = 0,
978 };
979 void *hdr;
980 struct sk_buff *msg;
981
982 if (info->attrs[NL80211_ATTR_KEY_IDX])
983 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
984
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200985 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100986 return -EINVAL;
987
988 if (info->attrs[NL80211_ATTR_MAC])
989 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
990
Johannes Berg3b858752009-03-12 09:55:09 +0100991 rtnl_lock();
992
Johannes Berg79c97e92009-07-07 03:56:12 +0200993 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100994 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100995 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100996
Johannes Berg79c97e92009-07-07 03:56:12 +0200997 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +0100998 err = -EOPNOTSUPP;
999 goto out;
1000 }
1001
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001003 if (!msg) {
1004 err = -ENOMEM;
1005 goto out;
1006 }
1007
1008 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1009 NL80211_CMD_NEW_KEY);
1010
1011 if (IS_ERR(hdr)) {
1012 err = PTR_ERR(hdr);
1013 goto out;
1014 }
1015
1016 cookie.msg = msg;
1017
1018 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1019 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1020 if (mac_addr)
1021 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1022
Johannes Berg79c97e92009-07-07 03:56:12 +02001023 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001024 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001025
1026 if (err)
1027 goto out;
1028
1029 if (cookie.error)
1030 goto nla_put_failure;
1031
1032 genlmsg_end(msg, hdr);
1033 err = genlmsg_unicast(msg, info->snd_pid);
1034 goto out;
1035
1036 nla_put_failure:
1037 err = -ENOBUFS;
1038 nlmsg_free(msg);
1039 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001040 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001041 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001042 unlock_rtnl:
1043 rtnl_unlock();
1044
Johannes Berg41ade002007-12-19 02:03:29 +01001045 return err;
1046}
1047
1048static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1049{
Johannes Berg79c97e92009-07-07 03:56:12 +02001050 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001051 int err;
1052 struct net_device *dev;
1053 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001054 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1055 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001056
1057 if (!info->attrs[NL80211_ATTR_KEY_IDX])
1058 return -EINVAL;
1059
1060 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1061
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001062 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
1063 if (key_idx < 4 || key_idx > 5)
1064 return -EINVAL;
1065 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +01001066 return -EINVAL;
1067
1068 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001069 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
1070 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +01001071 return -EINVAL;
1072
Johannes Berg3b858752009-03-12 09:55:09 +01001073 rtnl_lock();
1074
Johannes Berg79c97e92009-07-07 03:56:12 +02001075 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001076 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001077 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001078
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001079 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
Johannes Berg79c97e92009-07-07 03:56:12 +02001080 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001081 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001082 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001083
1084 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001085 err = -EOPNOTSUPP;
1086 goto out;
1087 }
1088
Johannes Berg79c97e92009-07-07 03:56:12 +02001089 err = func(&rdev->wiphy, dev, key_idx);
Johannes Berg08645122009-05-11 13:54:58 +02001090#ifdef CONFIG_WIRELESS_EXT
1091 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001092 if (func == rdev->ops->set_default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001093 dev->ieee80211_ptr->wext.default_key = key_idx;
1094 else
1095 dev->ieee80211_ptr->wext.default_mgmt_key = key_idx;
1096 }
1097#endif
Johannes Berg41ade002007-12-19 02:03:29 +01001098
1099 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001100 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001101 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001102
1103 unlock_rtnl:
1104 rtnl_unlock();
1105
Johannes Berg41ade002007-12-19 02:03:29 +01001106 return err;
1107}
1108
1109static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1110{
Johannes Berg79c97e92009-07-07 03:56:12 +02001111 struct cfg80211_registered_device *rdev;
Johannes Berg25e47c12009-04-02 20:14:06 +02001112 int err, i;
Johannes Berg41ade002007-12-19 02:03:29 +01001113 struct net_device *dev;
1114 struct key_params params;
1115 u8 key_idx = 0;
1116 u8 *mac_addr = NULL;
1117
1118 memset(&params, 0, sizeof(params));
1119
1120 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
1121 return -EINVAL;
1122
1123 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1124 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1125 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1126 }
1127
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +03001128 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1129 params.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1130 params.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1131 }
1132
Johannes Berg41ade002007-12-19 02:03:29 +01001133 if (info->attrs[NL80211_ATTR_KEY_IDX])
1134 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1135
1136 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1137
1138 if (info->attrs[NL80211_ATTR_MAC])
1139 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1140
Johannes Berg08645122009-05-11 13:54:58 +02001141 if (cfg80211_validate_key_settings(&params, key_idx, mac_addr))
Johannes Berg41ade002007-12-19 02:03:29 +01001142 return -EINVAL;
1143
Johannes Berg3b858752009-03-12 09:55:09 +01001144 rtnl_lock();
1145
Johannes Berg79c97e92009-07-07 03:56:12 +02001146 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001147 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001148 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001149
Johannes Berg79c97e92009-07-07 03:56:12 +02001150 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
1151 if (params.cipher == rdev->wiphy.cipher_suites[i])
Johannes Berg25e47c12009-04-02 20:14:06 +02001152 break;
Johannes Berg79c97e92009-07-07 03:56:12 +02001153 if (i == rdev->wiphy.n_cipher_suites) {
Johannes Berg25e47c12009-04-02 20:14:06 +02001154 err = -EINVAL;
1155 goto out;
1156 }
1157
Johannes Berg79c97e92009-07-07 03:56:12 +02001158 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001159 err = -EOPNOTSUPP;
1160 goto out;
1161 }
1162
Johannes Berg79c97e92009-07-07 03:56:12 +02001163 err = rdev->ops->add_key(&rdev->wiphy, dev, key_idx, mac_addr, &params);
Johannes Berg41ade002007-12-19 02:03:29 +01001164
1165 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001166 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001167 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001168 unlock_rtnl:
1169 rtnl_unlock();
1170
Johannes Berg41ade002007-12-19 02:03:29 +01001171 return err;
1172}
1173
1174static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1175{
Johannes Berg79c97e92009-07-07 03:56:12 +02001176 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001177 int err;
1178 struct net_device *dev;
1179 u8 key_idx = 0;
1180 u8 *mac_addr = NULL;
1181
1182 if (info->attrs[NL80211_ATTR_KEY_IDX])
1183 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1184
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001185 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001186 return -EINVAL;
1187
1188 if (info->attrs[NL80211_ATTR_MAC])
1189 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1190
Johannes Berg3b858752009-03-12 09:55:09 +01001191 rtnl_lock();
1192
Johannes Berg79c97e92009-07-07 03:56:12 +02001193 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001194 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001195 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001196
Johannes Berg79c97e92009-07-07 03:56:12 +02001197 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001198 err = -EOPNOTSUPP;
1199 goto out;
1200 }
1201
Johannes Berg79c97e92009-07-07 03:56:12 +02001202 err = rdev->ops->del_key(&rdev->wiphy, dev, key_idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001203
Johannes Berg08645122009-05-11 13:54:58 +02001204#ifdef CONFIG_WIRELESS_EXT
1205 if (!err) {
1206 if (key_idx == dev->ieee80211_ptr->wext.default_key)
1207 dev->ieee80211_ptr->wext.default_key = -1;
1208 else if (key_idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1209 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1210 }
1211#endif
1212
Johannes Berg41ade002007-12-19 02:03:29 +01001213 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001214 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001215 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001216
1217 unlock_rtnl:
1218 rtnl_unlock();
1219
Johannes Berg41ade002007-12-19 02:03:29 +01001220 return err;
1221}
1222
Johannes Berged1b6cc2007-12-19 02:03:32 +01001223static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1224{
1225 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1226 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001227 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001228 int err;
1229 struct net_device *dev;
1230 struct beacon_parameters params;
1231 int haveinfo = 0;
1232
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001233 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1234 return -EINVAL;
1235
Johannes Berg3b858752009-03-12 09:55:09 +01001236 rtnl_lock();
1237
Johannes Berg79c97e92009-07-07 03:56:12 +02001238 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001239 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001240 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001241
Jouni Malineneec60b02009-03-20 21:21:19 +02001242 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1243 err = -EOPNOTSUPP;
1244 goto out;
1245 }
1246
Johannes Berged1b6cc2007-12-19 02:03:32 +01001247 switch (info->genlhdr->cmd) {
1248 case NL80211_CMD_NEW_BEACON:
1249 /* these are required for NEW_BEACON */
1250 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1251 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1252 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1253 err = -EINVAL;
1254 goto out;
1255 }
1256
Johannes Berg79c97e92009-07-07 03:56:12 +02001257 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001258 break;
1259 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001260 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001261 break;
1262 default:
1263 WARN_ON(1);
1264 err = -EOPNOTSUPP;
1265 goto out;
1266 }
1267
1268 if (!call) {
1269 err = -EOPNOTSUPP;
1270 goto out;
1271 }
1272
1273 memset(&params, 0, sizeof(params));
1274
1275 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1276 params.interval =
1277 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1278 haveinfo = 1;
1279 }
1280
1281 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1282 params.dtim_period =
1283 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1284 haveinfo = 1;
1285 }
1286
1287 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1288 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1289 params.head_len =
1290 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1291 haveinfo = 1;
1292 }
1293
1294 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1295 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1296 params.tail_len =
1297 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1298 haveinfo = 1;
1299 }
1300
1301 if (!haveinfo) {
1302 err = -EINVAL;
1303 goto out;
1304 }
1305
Johannes Berg79c97e92009-07-07 03:56:12 +02001306 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001307
1308 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001309 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001310 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001311 unlock_rtnl:
1312 rtnl_unlock();
1313
Johannes Berged1b6cc2007-12-19 02:03:32 +01001314 return err;
1315}
1316
1317static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1318{
Johannes Berg79c97e92009-07-07 03:56:12 +02001319 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001320 int err;
1321 struct net_device *dev;
1322
Johannes Berg3b858752009-03-12 09:55:09 +01001323 rtnl_lock();
1324
Johannes Berg79c97e92009-07-07 03:56:12 +02001325 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001326 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001327 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001328
Johannes Berg79c97e92009-07-07 03:56:12 +02001329 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001330 err = -EOPNOTSUPP;
1331 goto out;
1332 }
1333
Jouni Malineneec60b02009-03-20 21:21:19 +02001334 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1335 err = -EOPNOTSUPP;
1336 goto out;
1337 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001338 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001339
1340 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001341 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001342 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001343 unlock_rtnl:
1344 rtnl_unlock();
1345
Johannes Berged1b6cc2007-12-19 02:03:32 +01001346 return err;
1347}
1348
Johannes Berg5727ef12007-12-19 02:03:34 +01001349static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1350 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1351 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1352 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001353 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001354};
1355
Johannes Bergeccb8e82009-05-11 21:57:56 +03001356static int parse_station_flags(struct genl_info *info,
1357 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001358{
1359 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001360 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001361 int flag;
1362
Johannes Bergeccb8e82009-05-11 21:57:56 +03001363 /*
1364 * Try parsing the new attribute first so userspace
1365 * can specify both for older kernels.
1366 */
1367 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1368 if (nla) {
1369 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001370
Johannes Bergeccb8e82009-05-11 21:57:56 +03001371 sta_flags = nla_data(nla);
1372 params->sta_flags_mask = sta_flags->mask;
1373 params->sta_flags_set = sta_flags->set;
1374 if ((params->sta_flags_mask |
1375 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1376 return -EINVAL;
1377 return 0;
1378 }
1379
1380 /* if present, parse the old attribute */
1381
1382 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001383 if (!nla)
1384 return 0;
1385
1386 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1387 nla, sta_flags_policy))
1388 return -EINVAL;
1389
Johannes Bergeccb8e82009-05-11 21:57:56 +03001390 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1391 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001392
1393 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1394 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001395 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001396
1397 return 0;
1398}
1399
Henning Rogge420e7fa2008-12-11 22:04:19 +01001400static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1401{
1402 int modulation, streams, bitrate;
1403
1404 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1405 return rate->legacy;
1406
1407 /* the formula below does only work for MCS values smaller than 32 */
1408 if (rate->mcs >= 32)
1409 return 0;
1410
1411 modulation = rate->mcs & 7;
1412 streams = (rate->mcs >> 3) + 1;
1413
1414 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1415 13500000 : 6500000;
1416
1417 if (modulation < 4)
1418 bitrate *= (modulation + 1);
1419 else if (modulation == 4)
1420 bitrate *= (modulation + 2);
1421 else
1422 bitrate *= (modulation + 3);
1423
1424 bitrate *= streams;
1425
1426 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1427 bitrate = (bitrate / 9) * 10;
1428
1429 /* do NOT round down here */
1430 return (bitrate + 50000) / 100000;
1431}
1432
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001433static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1434 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001435 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001436{
1437 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001438 struct nlattr *sinfoattr, *txrate;
1439 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001440
1441 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1442 if (!hdr)
1443 return -1;
1444
1445 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1446 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1447
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001448 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1449 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001450 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001451 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1452 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1453 sinfo->inactive_time);
1454 if (sinfo->filled & STATION_INFO_RX_BYTES)
1455 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1456 sinfo->rx_bytes);
1457 if (sinfo->filled & STATION_INFO_TX_BYTES)
1458 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1459 sinfo->tx_bytes);
1460 if (sinfo->filled & STATION_INFO_LLID)
1461 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1462 sinfo->llid);
1463 if (sinfo->filled & STATION_INFO_PLID)
1464 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1465 sinfo->plid);
1466 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1467 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1468 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001469 if (sinfo->filled & STATION_INFO_SIGNAL)
1470 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1471 sinfo->signal);
1472 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1473 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1474 if (!txrate)
1475 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001476
Henning Rogge420e7fa2008-12-11 22:04:19 +01001477 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1478 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1479 if (bitrate > 0)
1480 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1481
1482 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1483 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1484 sinfo->txrate.mcs);
1485 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1486 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1487 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1488 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1489
1490 nla_nest_end(msg, txrate);
1491 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001492 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1493 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1494 sinfo->rx_packets);
1495 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1496 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1497 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001498 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001499
1500 return genlmsg_end(msg, hdr);
1501
1502 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001503 genlmsg_cancel(msg, hdr);
1504 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001505}
1506
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001507static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001508 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001509{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001510 struct station_info sinfo;
1511 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001512 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001513 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001514 int ifidx = cb->args[0];
1515 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001516 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001517
Johannes Bergbba95fe2008-07-29 13:22:51 +02001518 if (!ifidx) {
1519 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1520 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1521 nl80211_policy);
1522 if (err)
1523 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001524
Johannes Bergbba95fe2008-07-29 13:22:51 +02001525 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1526 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001527
Johannes Bergbba95fe2008-07-29 13:22:51 +02001528 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1529 if (!ifidx)
1530 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001531 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001532
Johannes Berg3b858752009-03-12 09:55:09 +01001533 rtnl_lock();
1534
1535 netdev = __dev_get_by_index(&init_net, ifidx);
1536 if (!netdev) {
1537 err = -ENODEV;
1538 goto out_rtnl;
1539 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001540
Johannes Bergbba95fe2008-07-29 13:22:51 +02001541 dev = cfg80211_get_dev_from_ifindex(ifidx);
1542 if (IS_ERR(dev)) {
1543 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001544 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001545 }
1546
1547 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001548 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001549 goto out_err;
1550 }
1551
Johannes Bergbba95fe2008-07-29 13:22:51 +02001552 while (1) {
1553 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1554 mac_addr, &sinfo);
1555 if (err == -ENOENT)
1556 break;
1557 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001558 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001559
1560 if (nl80211_send_station(skb,
1561 NETLINK_CB(cb->skb).pid,
1562 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1563 netdev, mac_addr,
1564 &sinfo) < 0)
1565 goto out;
1566
1567 sta_idx++;
1568 }
1569
1570
1571 out:
1572 cb->args[1] = sta_idx;
1573 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001574 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001575 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001576 out_rtnl:
1577 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001578
1579 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001580}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001581
Johannes Berg5727ef12007-12-19 02:03:34 +01001582static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1583{
Johannes Berg79c97e92009-07-07 03:56:12 +02001584 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001585 int err;
1586 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001587 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001588 struct sk_buff *msg;
1589 u8 *mac_addr = NULL;
1590
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001591 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001592
1593 if (!info->attrs[NL80211_ATTR_MAC])
1594 return -EINVAL;
1595
1596 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1597
Johannes Berg3b858752009-03-12 09:55:09 +01001598 rtnl_lock();
1599
Johannes Berg79c97e92009-07-07 03:56:12 +02001600 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001601 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001602 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001603
Johannes Berg79c97e92009-07-07 03:56:12 +02001604 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001605 err = -EOPNOTSUPP;
1606 goto out;
1607 }
1608
Johannes Berg79c97e92009-07-07 03:56:12 +02001609 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001610 if (err)
1611 goto out;
1612
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001614 if (!msg)
1615 goto out;
1616
1617 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001618 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001619 goto out_free;
1620
1621 err = genlmsg_unicast(msg, info->snd_pid);
1622 goto out;
1623
1624 out_free:
1625 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001626 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001627 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001628 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001629 out_rtnl:
1630 rtnl_unlock();
1631
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001632 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001633}
1634
1635/*
1636 * Get vlan interface making sure it is on the right wiphy.
1637 */
1638static int get_vlan(struct nlattr *vlanattr,
1639 struct cfg80211_registered_device *rdev,
1640 struct net_device **vlan)
1641{
1642 *vlan = NULL;
1643
1644 if (vlanattr) {
1645 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1646 if (!*vlan)
1647 return -ENODEV;
1648 if (!(*vlan)->ieee80211_ptr)
1649 return -EINVAL;
1650 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1651 return -EINVAL;
1652 }
1653 return 0;
1654}
1655
1656static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1657{
Johannes Berg79c97e92009-07-07 03:56:12 +02001658 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001659 int err;
1660 struct net_device *dev;
1661 struct station_parameters params;
1662 u8 *mac_addr = NULL;
1663
1664 memset(&params, 0, sizeof(params));
1665
1666 params.listen_interval = -1;
1667
1668 if (info->attrs[NL80211_ATTR_STA_AID])
1669 return -EINVAL;
1670
1671 if (!info->attrs[NL80211_ATTR_MAC])
1672 return -EINVAL;
1673
1674 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1675
1676 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1677 params.supported_rates =
1678 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1679 params.supported_rates_len =
1680 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1681 }
1682
1683 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1684 params.listen_interval =
1685 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1686
Jouni Malinen36aedc92008-08-25 11:58:58 +03001687 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1688 params.ht_capa =
1689 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1690
Johannes Bergeccb8e82009-05-11 21:57:56 +03001691 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001692 return -EINVAL;
1693
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001694 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1695 params.plink_action =
1696 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1697
Johannes Berg3b858752009-03-12 09:55:09 +01001698 rtnl_lock();
1699
Johannes Berg79c97e92009-07-07 03:56:12 +02001700 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001701 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001702 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001703
Johannes Berg79c97e92009-07-07 03:56:12 +02001704 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001705 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001706 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001707
1708 /* validate settings */
1709 err = 0;
1710
1711 switch (dev->ieee80211_ptr->iftype) {
1712 case NL80211_IFTYPE_AP:
1713 case NL80211_IFTYPE_AP_VLAN:
1714 /* disallow mesh-specific things */
1715 if (params.plink_action)
1716 err = -EINVAL;
1717 break;
1718 case NL80211_IFTYPE_STATION:
1719 /* disallow everything but AUTHORIZED flag */
1720 if (params.plink_action)
1721 err = -EINVAL;
1722 if (params.vlan)
1723 err = -EINVAL;
1724 if (params.supported_rates)
1725 err = -EINVAL;
1726 if (params.ht_capa)
1727 err = -EINVAL;
1728 if (params.listen_interval >= 0)
1729 err = -EINVAL;
1730 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1731 err = -EINVAL;
1732 break;
1733 case NL80211_IFTYPE_MESH_POINT:
1734 /* disallow things mesh doesn't support */
1735 if (params.vlan)
1736 err = -EINVAL;
1737 if (params.ht_capa)
1738 err = -EINVAL;
1739 if (params.listen_interval >= 0)
1740 err = -EINVAL;
1741 if (params.supported_rates)
1742 err = -EINVAL;
1743 if (params.sta_flags_mask)
1744 err = -EINVAL;
1745 break;
1746 default:
1747 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001748 }
1749
Johannes Berg5727ef12007-12-19 02:03:34 +01001750 if (err)
1751 goto out;
1752
Johannes Berg79c97e92009-07-07 03:56:12 +02001753 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001754 err = -EOPNOTSUPP;
1755 goto out;
1756 }
1757
Johannes Berg79c97e92009-07-07 03:56:12 +02001758 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001759
1760 out:
1761 if (params.vlan)
1762 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001763 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001764 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001765 out_rtnl:
1766 rtnl_unlock();
1767
Johannes Berg5727ef12007-12-19 02:03:34 +01001768 return err;
1769}
1770
1771static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1772{
Johannes Berg79c97e92009-07-07 03:56:12 +02001773 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001774 int err;
1775 struct net_device *dev;
1776 struct station_parameters params;
1777 u8 *mac_addr = NULL;
1778
1779 memset(&params, 0, sizeof(params));
1780
1781 if (!info->attrs[NL80211_ATTR_MAC])
1782 return -EINVAL;
1783
Johannes Berg5727ef12007-12-19 02:03:34 +01001784 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1785 return -EINVAL;
1786
1787 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1788 return -EINVAL;
1789
1790 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1791 params.supported_rates =
1792 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1793 params.supported_rates_len =
1794 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1795 params.listen_interval =
1796 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001797
Johannes Berga97f4422009-06-18 17:23:43 +02001798 if (info->attrs[NL80211_ATTR_STA_AID]) {
1799 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1800 if (!params.aid || params.aid > IEEE80211_MAX_AID)
1801 return -EINVAL;
1802 }
Johannes Berg51b50fb2009-05-24 16:42:30 +02001803
Jouni Malinen36aedc92008-08-25 11:58:58 +03001804 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1805 params.ht_capa =
1806 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001807
Johannes Bergeccb8e82009-05-11 21:57:56 +03001808 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001809 return -EINVAL;
1810
Johannes Berg3b858752009-03-12 09:55:09 +01001811 rtnl_lock();
1812
Johannes Berg79c97e92009-07-07 03:56:12 +02001813 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001814 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001815 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001816
Johannes Berg79c97e92009-07-07 03:56:12 +02001817 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001818 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02001819 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001820
1821 /* validate settings */
1822 err = 0;
1823
1824 switch (dev->ieee80211_ptr->iftype) {
1825 case NL80211_IFTYPE_AP:
1826 case NL80211_IFTYPE_AP_VLAN:
1827 /* all ok but must have AID */
1828 if (!params.aid)
1829 err = -EINVAL;
1830 break;
1831 case NL80211_IFTYPE_MESH_POINT:
1832 /* disallow things mesh doesn't support */
1833 if (params.vlan)
1834 err = -EINVAL;
1835 if (params.aid)
1836 err = -EINVAL;
1837 if (params.ht_capa)
1838 err = -EINVAL;
1839 if (params.listen_interval >= 0)
1840 err = -EINVAL;
1841 if (params.supported_rates)
1842 err = -EINVAL;
1843 if (params.sta_flags_mask)
1844 err = -EINVAL;
1845 break;
1846 default:
1847 err = -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02001848 }
1849
Johannes Berg5727ef12007-12-19 02:03:34 +01001850 if (err)
1851 goto out;
1852
Johannes Berg79c97e92009-07-07 03:56:12 +02001853 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001854 err = -EOPNOTSUPP;
1855 goto out;
1856 }
1857
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001858 if (!netif_running(dev)) {
1859 err = -ENETDOWN;
1860 goto out;
1861 }
1862
Johannes Berg79c97e92009-07-07 03:56:12 +02001863 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001864
1865 out:
1866 if (params.vlan)
1867 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001868 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001869 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001870 out_rtnl:
1871 rtnl_unlock();
1872
Johannes Berg5727ef12007-12-19 02:03:34 +01001873 return err;
1874}
1875
1876static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1877{
Johannes Berg79c97e92009-07-07 03:56:12 +02001878 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001879 int err;
1880 struct net_device *dev;
1881 u8 *mac_addr = NULL;
1882
1883 if (info->attrs[NL80211_ATTR_MAC])
1884 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1885
Johannes Berg3b858752009-03-12 09:55:09 +01001886 rtnl_lock();
1887
Johannes Berg79c97e92009-07-07 03:56:12 +02001888 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001889 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001890 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001891
Johannes Berge80cf852009-05-11 14:43:13 +02001892 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Andrey Yurovsky155cc9e2009-06-16 11:31:04 -07001893 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
1894 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02001895 err = -EINVAL;
1896 goto out;
1897 }
1898
Johannes Berg79c97e92009-07-07 03:56:12 +02001899 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001900 err = -EOPNOTSUPP;
1901 goto out;
1902 }
1903
Johannes Berg79c97e92009-07-07 03:56:12 +02001904 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01001905
1906 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001907 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001908 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001909 out_rtnl:
1910 rtnl_unlock();
1911
Johannes Berg5727ef12007-12-19 02:03:34 +01001912 return err;
1913}
1914
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001915static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1916 int flags, struct net_device *dev,
1917 u8 *dst, u8 *next_hop,
1918 struct mpath_info *pinfo)
1919{
1920 void *hdr;
1921 struct nlattr *pinfoattr;
1922
1923 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1924 if (!hdr)
1925 return -1;
1926
1927 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1928 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1929 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1930
1931 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1932 if (!pinfoattr)
1933 goto nla_put_failure;
1934 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1935 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1936 pinfo->frame_qlen);
1937 if (pinfo->filled & MPATH_INFO_DSN)
1938 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1939 pinfo->dsn);
1940 if (pinfo->filled & MPATH_INFO_METRIC)
1941 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1942 pinfo->metric);
1943 if (pinfo->filled & MPATH_INFO_EXPTIME)
1944 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1945 pinfo->exptime);
1946 if (pinfo->filled & MPATH_INFO_FLAGS)
1947 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1948 pinfo->flags);
1949 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1950 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1951 pinfo->discovery_timeout);
1952 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1953 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1954 pinfo->discovery_retries);
1955
1956 nla_nest_end(msg, pinfoattr);
1957
1958 return genlmsg_end(msg, hdr);
1959
1960 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001961 genlmsg_cancel(msg, hdr);
1962 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001963}
1964
1965static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001966 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001967{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001968 struct mpath_info pinfo;
1969 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001970 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001971 u8 dst[ETH_ALEN];
1972 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001973 int ifidx = cb->args[0];
1974 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001975 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001976
Johannes Bergbba95fe2008-07-29 13:22:51 +02001977 if (!ifidx) {
1978 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1979 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1980 nl80211_policy);
1981 if (err)
1982 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001983
Johannes Bergbba95fe2008-07-29 13:22:51 +02001984 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1985 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001986
Johannes Bergbba95fe2008-07-29 13:22:51 +02001987 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1988 if (!ifidx)
1989 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001990 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001991
Johannes Berg3b858752009-03-12 09:55:09 +01001992 rtnl_lock();
1993
1994 netdev = __dev_get_by_index(&init_net, ifidx);
1995 if (!netdev) {
1996 err = -ENODEV;
1997 goto out_rtnl;
1998 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001999
Johannes Bergbba95fe2008-07-29 13:22:51 +02002000 dev = cfg80211_get_dev_from_ifindex(ifidx);
2001 if (IS_ERR(dev)) {
2002 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002003 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002004 }
2005
2006 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002007 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002008 goto out_err;
2009 }
2010
Jouni Malineneec60b02009-03-20 21:21:19 +02002011 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2012 err = -EOPNOTSUPP;
2013 goto out;
2014 }
2015
Johannes Bergbba95fe2008-07-29 13:22:51 +02002016 while (1) {
2017 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2018 dst, next_hop, &pinfo);
2019 if (err == -ENOENT)
2020 break;
2021 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002022 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002023
2024 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2025 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2026 netdev, dst, next_hop,
2027 &pinfo) < 0)
2028 goto out;
2029
2030 path_idx++;
2031 }
2032
2033
2034 out:
2035 cb->args[1] = path_idx;
2036 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002037 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002038 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002039 out_rtnl:
2040 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002041
2042 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002043}
2044
2045static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2046{
Johannes Berg79c97e92009-07-07 03:56:12 +02002047 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002048 int err;
2049 struct net_device *dev;
2050 struct mpath_info pinfo;
2051 struct sk_buff *msg;
2052 u8 *dst = NULL;
2053 u8 next_hop[ETH_ALEN];
2054
2055 memset(&pinfo, 0, sizeof(pinfo));
2056
2057 if (!info->attrs[NL80211_ATTR_MAC])
2058 return -EINVAL;
2059
2060 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2061
Johannes Berg3b858752009-03-12 09:55:09 +01002062 rtnl_lock();
2063
Johannes Berg79c97e92009-07-07 03:56:12 +02002064 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002065 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002066 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002067
Johannes Berg79c97e92009-07-07 03:56:12 +02002068 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002069 err = -EOPNOTSUPP;
2070 goto out;
2071 }
2072
Jouni Malineneec60b02009-03-20 21:21:19 +02002073 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2074 err = -EOPNOTSUPP;
2075 goto out;
2076 }
2077
Johannes Berg79c97e92009-07-07 03:56:12 +02002078 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002079 if (err)
2080 goto out;
2081
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002082 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002083 if (!msg)
2084 goto out;
2085
2086 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2087 dev, dst, next_hop, &pinfo) < 0)
2088 goto out_free;
2089
2090 err = genlmsg_unicast(msg, info->snd_pid);
2091 goto out;
2092
2093 out_free:
2094 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002095 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002096 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002097 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002098 out_rtnl:
2099 rtnl_unlock();
2100
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002101 return err;
2102}
2103
2104static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2105{
Johannes Berg79c97e92009-07-07 03:56:12 +02002106 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002107 int err;
2108 struct net_device *dev;
2109 u8 *dst = NULL;
2110 u8 *next_hop = NULL;
2111
2112 if (!info->attrs[NL80211_ATTR_MAC])
2113 return -EINVAL;
2114
2115 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2116 return -EINVAL;
2117
2118 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2119 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2120
Johannes Berg3b858752009-03-12 09:55:09 +01002121 rtnl_lock();
2122
Johannes Berg79c97e92009-07-07 03:56:12 +02002123 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002124 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002125 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002126
Johannes Berg79c97e92009-07-07 03:56:12 +02002127 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002128 err = -EOPNOTSUPP;
2129 goto out;
2130 }
2131
Jouni Malineneec60b02009-03-20 21:21:19 +02002132 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2133 err = -EOPNOTSUPP;
2134 goto out;
2135 }
2136
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002137 if (!netif_running(dev)) {
2138 err = -ENETDOWN;
2139 goto out;
2140 }
2141
Johannes Berg79c97e92009-07-07 03:56:12 +02002142 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002143
2144 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002145 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002146 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002147 out_rtnl:
2148 rtnl_unlock();
2149
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002150 return err;
2151}
2152static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2153{
Johannes Berg79c97e92009-07-07 03:56:12 +02002154 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002155 int err;
2156 struct net_device *dev;
2157 u8 *dst = NULL;
2158 u8 *next_hop = NULL;
2159
2160 if (!info->attrs[NL80211_ATTR_MAC])
2161 return -EINVAL;
2162
2163 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2164 return -EINVAL;
2165
2166 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2167 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2168
Johannes Berg3b858752009-03-12 09:55:09 +01002169 rtnl_lock();
2170
Johannes Berg79c97e92009-07-07 03:56:12 +02002171 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002172 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002173 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002174
Johannes Berg79c97e92009-07-07 03:56:12 +02002175 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002176 err = -EOPNOTSUPP;
2177 goto out;
2178 }
2179
Jouni Malineneec60b02009-03-20 21:21:19 +02002180 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2181 err = -EOPNOTSUPP;
2182 goto out;
2183 }
2184
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002185 if (!netif_running(dev)) {
2186 err = -ENETDOWN;
2187 goto out;
2188 }
2189
Johannes Berg79c97e92009-07-07 03:56:12 +02002190 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002191
2192 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002193 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002194 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002195 out_rtnl:
2196 rtnl_unlock();
2197
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002198 return err;
2199}
2200
2201static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2202{
Johannes Berg79c97e92009-07-07 03:56:12 +02002203 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002204 int err;
2205 struct net_device *dev;
2206 u8 *dst = NULL;
2207
2208 if (info->attrs[NL80211_ATTR_MAC])
2209 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2210
Johannes Berg3b858752009-03-12 09:55:09 +01002211 rtnl_lock();
2212
Johannes Berg79c97e92009-07-07 03:56:12 +02002213 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002214 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002215 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002216
Johannes Berg79c97e92009-07-07 03:56:12 +02002217 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002218 err = -EOPNOTSUPP;
2219 goto out;
2220 }
2221
Johannes Berg79c97e92009-07-07 03:56:12 +02002222 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002223
2224 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002225 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002226 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002227 out_rtnl:
2228 rtnl_unlock();
2229
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002230 return err;
2231}
2232
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002233static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2234{
Johannes Berg79c97e92009-07-07 03:56:12 +02002235 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002236 int err;
2237 struct net_device *dev;
2238 struct bss_parameters params;
2239
2240 memset(&params, 0, sizeof(params));
2241 /* default to not changing parameters */
2242 params.use_cts_prot = -1;
2243 params.use_short_preamble = -1;
2244 params.use_short_slot_time = -1;
2245
2246 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2247 params.use_cts_prot =
2248 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2249 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2250 params.use_short_preamble =
2251 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2252 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2253 params.use_short_slot_time =
2254 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002255 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2256 params.basic_rates =
2257 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2258 params.basic_rates_len =
2259 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2260 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002261
Johannes Berg3b858752009-03-12 09:55:09 +01002262 rtnl_lock();
2263
Johannes Berg79c97e92009-07-07 03:56:12 +02002264 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002265 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002266 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002267
Johannes Berg79c97e92009-07-07 03:56:12 +02002268 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002269 err = -EOPNOTSUPP;
2270 goto out;
2271 }
2272
Jouni Malineneec60b02009-03-20 21:21:19 +02002273 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2274 err = -EOPNOTSUPP;
2275 goto out;
2276 }
2277
Johannes Berg79c97e92009-07-07 03:56:12 +02002278 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002279
2280 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002281 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002282 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002283 out_rtnl:
2284 rtnl_unlock();
2285
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002286 return err;
2287}
2288
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002289static const struct nla_policy
2290 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2291 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2292 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2293 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2294 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2295 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2296 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2297};
2298
2299static int parse_reg_rule(struct nlattr *tb[],
2300 struct ieee80211_reg_rule *reg_rule)
2301{
2302 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2303 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2304
2305 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2306 return -EINVAL;
2307 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2308 return -EINVAL;
2309 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2310 return -EINVAL;
2311 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2312 return -EINVAL;
2313 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2314 return -EINVAL;
2315
2316 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2317
2318 freq_range->start_freq_khz =
2319 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2320 freq_range->end_freq_khz =
2321 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2322 freq_range->max_bandwidth_khz =
2323 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2324
2325 power_rule->max_eirp =
2326 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2327
2328 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2329 power_rule->max_antenna_gain =
2330 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2331
2332 return 0;
2333}
2334
2335static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2336{
2337 int r;
2338 char *data = NULL;
2339
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002340 /*
2341 * You should only get this when cfg80211 hasn't yet initialized
2342 * completely when built-in to the kernel right between the time
2343 * window between nl80211_init() and regulatory_init(), if that is
2344 * even possible.
2345 */
2346 mutex_lock(&cfg80211_mutex);
2347 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002348 mutex_unlock(&cfg80211_mutex);
2349 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002350 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002351 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002352
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002353 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2354 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002355
2356 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2357
2358#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2359 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002360 if (is_world_regdom(data))
2361 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002362#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002363
2364 r = regulatory_hint_user(data);
2365
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002366 return r;
2367}
2368
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002369static int nl80211_get_mesh_params(struct sk_buff *skb,
2370 struct genl_info *info)
2371{
Johannes Berg79c97e92009-07-07 03:56:12 +02002372 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002373 struct mesh_config cur_params;
2374 int err;
2375 struct net_device *dev;
2376 void *hdr;
2377 struct nlattr *pinfoattr;
2378 struct sk_buff *msg;
2379
Johannes Berg3b858752009-03-12 09:55:09 +01002380 rtnl_lock();
2381
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002382 /* Look up our device */
Johannes Berg79c97e92009-07-07 03:56:12 +02002383 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002384 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002385 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002386
Johannes Berg79c97e92009-07-07 03:56:12 +02002387 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002388 err = -EOPNOTSUPP;
2389 goto out;
2390 }
2391
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002392 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002393 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002394 if (err)
2395 goto out;
2396
2397 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002398 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002399 if (!msg) {
2400 err = -ENOBUFS;
2401 goto out;
2402 }
2403 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2404 NL80211_CMD_GET_MESH_PARAMS);
2405 if (!hdr)
2406 goto nla_put_failure;
2407 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2408 if (!pinfoattr)
2409 goto nla_put_failure;
2410 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2411 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2412 cur_params.dot11MeshRetryTimeout);
2413 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2414 cur_params.dot11MeshConfirmTimeout);
2415 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2416 cur_params.dot11MeshHoldingTimeout);
2417 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2418 cur_params.dot11MeshMaxPeerLinks);
2419 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2420 cur_params.dot11MeshMaxRetries);
2421 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2422 cur_params.dot11MeshTTL);
2423 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2424 cur_params.auto_open_plinks);
2425 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2426 cur_params.dot11MeshHWMPmaxPREQretries);
2427 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2428 cur_params.path_refresh_time);
2429 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2430 cur_params.min_discovery_timeout);
2431 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2432 cur_params.dot11MeshHWMPactivePathTimeout);
2433 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2434 cur_params.dot11MeshHWMPpreqMinInterval);
2435 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2436 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2437 nla_nest_end(msg, pinfoattr);
2438 genlmsg_end(msg, hdr);
2439 err = genlmsg_unicast(msg, info->snd_pid);
2440 goto out;
2441
Johannes Berg3b858752009-03-12 09:55:09 +01002442 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002443 genlmsg_cancel(msg, hdr);
2444 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002445 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002446 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002447 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002448 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002449 out_rtnl:
2450 rtnl_unlock();
2451
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002452 return err;
2453}
2454
2455#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2456do {\
2457 if (table[attr_num]) {\
2458 cfg.param = nla_fn(table[attr_num]); \
2459 mask |= (1 << (attr_num - 1)); \
2460 } \
2461} while (0);\
2462
2463static struct nla_policy
2464nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2465 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2466 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2467 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2468 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2469 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2470 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2471 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2472
2473 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2474 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2475 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2476 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2477 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2478 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2479};
2480
2481static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2482{
2483 int err;
2484 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002485 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002486 struct net_device *dev;
2487 struct mesh_config cfg;
2488 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2489 struct nlattr *parent_attr;
2490
2491 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2492 if (!parent_attr)
2493 return -EINVAL;
2494 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2495 parent_attr, nl80211_meshconf_params_policy))
2496 return -EINVAL;
2497
Johannes Berg3b858752009-03-12 09:55:09 +01002498 rtnl_lock();
2499
Johannes Berg79c97e92009-07-07 03:56:12 +02002500 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002501 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002502 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002503
Johannes Berg79c97e92009-07-07 03:56:12 +02002504 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002505 err = -EOPNOTSUPP;
2506 goto out;
2507 }
2508
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002509 /* This makes sure that there aren't more than 32 mesh config
2510 * parameters (otherwise our bitfield scheme would not work.) */
2511 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2512
2513 /* Fill in the params struct */
2514 mask = 0;
2515 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2516 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2517 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2518 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2519 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2520 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2521 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2522 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2523 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2524 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2525 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2526 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2527 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2528 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2529 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2530 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2531 nla_get_u8);
2532 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2533 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2534 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2535 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2536 nla_get_u16);
2537 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2538 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2539 nla_get_u32);
2540 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2541 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2542 nla_get_u16);
2543 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2544 dot11MeshHWMPnetDiameterTraversalTime,
2545 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2546 nla_get_u16);
2547
2548 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002549 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002550
Jouni Malinenf3f92582009-03-20 17:57:36 +02002551 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002552 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002553 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002554 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002555 out_rtnl:
2556 rtnl_unlock();
2557
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002558 return err;
2559}
2560
2561#undef FILL_IN_MESH_PARAM_IF_SET
2562
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002563static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2564{
2565 struct sk_buff *msg;
2566 void *hdr = NULL;
2567 struct nlattr *nl_reg_rules;
2568 unsigned int i;
2569 int err = -EINVAL;
2570
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002571 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002572
2573 if (!cfg80211_regdomain)
2574 goto out;
2575
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002576 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002577 if (!msg) {
2578 err = -ENOBUFS;
2579 goto out;
2580 }
2581
2582 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2583 NL80211_CMD_GET_REG);
2584 if (!hdr)
2585 goto nla_put_failure;
2586
2587 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2588 cfg80211_regdomain->alpha2);
2589
2590 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2591 if (!nl_reg_rules)
2592 goto nla_put_failure;
2593
2594 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2595 struct nlattr *nl_reg_rule;
2596 const struct ieee80211_reg_rule *reg_rule;
2597 const struct ieee80211_freq_range *freq_range;
2598 const struct ieee80211_power_rule *power_rule;
2599
2600 reg_rule = &cfg80211_regdomain->reg_rules[i];
2601 freq_range = &reg_rule->freq_range;
2602 power_rule = &reg_rule->power_rule;
2603
2604 nl_reg_rule = nla_nest_start(msg, i);
2605 if (!nl_reg_rule)
2606 goto nla_put_failure;
2607
2608 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2609 reg_rule->flags);
2610 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2611 freq_range->start_freq_khz);
2612 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2613 freq_range->end_freq_khz);
2614 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2615 freq_range->max_bandwidth_khz);
2616 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2617 power_rule->max_antenna_gain);
2618 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2619 power_rule->max_eirp);
2620
2621 nla_nest_end(msg, nl_reg_rule);
2622 }
2623
2624 nla_nest_end(msg, nl_reg_rules);
2625
2626 genlmsg_end(msg, hdr);
2627 err = genlmsg_unicast(msg, info->snd_pid);
2628 goto out;
2629
2630nla_put_failure:
2631 genlmsg_cancel(msg, hdr);
2632 err = -EMSGSIZE;
2633out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002634 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002635 return err;
2636}
2637
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002638static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2639{
2640 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2641 struct nlattr *nl_reg_rule;
2642 char *alpha2 = NULL;
2643 int rem_reg_rules = 0, r = 0;
2644 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2645 struct ieee80211_regdomain *rd = NULL;
2646
2647 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2648 return -EINVAL;
2649
2650 if (!info->attrs[NL80211_ATTR_REG_RULES])
2651 return -EINVAL;
2652
2653 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2654
2655 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2656 rem_reg_rules) {
2657 num_rules++;
2658 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002659 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002660 }
2661
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002662 mutex_lock(&cfg80211_mutex);
2663
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002664 if (!reg_is_valid_request(alpha2)) {
2665 r = -EINVAL;
2666 goto bad_reg;
2667 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002668
2669 size_of_regd = sizeof(struct ieee80211_regdomain) +
2670 (num_rules * sizeof(struct ieee80211_reg_rule));
2671
2672 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002673 if (!rd) {
2674 r = -ENOMEM;
2675 goto bad_reg;
2676 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002677
2678 rd->n_reg_rules = num_rules;
2679 rd->alpha2[0] = alpha2[0];
2680 rd->alpha2[1] = alpha2[1];
2681
2682 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2683 rem_reg_rules) {
2684 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2685 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2686 reg_rule_policy);
2687 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2688 if (r)
2689 goto bad_reg;
2690
2691 rule_idx++;
2692
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002693 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2694 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002695 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002696 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002697 }
2698
2699 BUG_ON(rule_idx != num_rules);
2700
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002701 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002702
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002703 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002704
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002705 return r;
2706
Johannes Bergd2372b32008-10-24 20:32:20 +02002707 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002708 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002709 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002710 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002711}
2712
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002713static int validate_scan_freqs(struct nlattr *freqs)
2714{
2715 struct nlattr *attr1, *attr2;
2716 int n_channels = 0, tmp1, tmp2;
2717
2718 nla_for_each_nested(attr1, freqs, tmp1) {
2719 n_channels++;
2720 /*
2721 * Some hardware has a limited channel list for
2722 * scanning, and it is pretty much nonsensical
2723 * to scan for a channel twice, so disallow that
2724 * and don't require drivers to check that the
2725 * channel list they get isn't longer than what
2726 * they can scan, as long as they can scan all
2727 * the channels they registered at once.
2728 */
2729 nla_for_each_nested(attr2, freqs, tmp2)
2730 if (attr1 != attr2 &&
2731 nla_get_u32(attr1) == nla_get_u32(attr2))
2732 return 0;
2733 }
2734
2735 return n_channels;
2736}
2737
Johannes Berg2a519312009-02-10 21:25:55 +01002738static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2739{
Johannes Berg79c97e92009-07-07 03:56:12 +02002740 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002741 struct net_device *dev;
2742 struct cfg80211_scan_request *request;
2743 struct cfg80211_ssid *ssid;
2744 struct ieee80211_channel *channel;
2745 struct nlattr *attr;
2746 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002747 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002748 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002749 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002750
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002751 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2752 return -EINVAL;
2753
Johannes Berg3b858752009-03-12 09:55:09 +01002754 rtnl_lock();
2755
Johannes Berg79c97e92009-07-07 03:56:12 +02002756 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002757 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002758 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002759
Johannes Berg79c97e92009-07-07 03:56:12 +02002760 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002761
Johannes Berg79c97e92009-07-07 03:56:12 +02002762 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002763 err = -EOPNOTSUPP;
2764 goto out;
2765 }
2766
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002767 if (!netif_running(dev)) {
2768 err = -ENETDOWN;
2769 goto out;
2770 }
2771
Johannes Berg79c97e92009-07-07 03:56:12 +02002772 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002773 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002774 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002775 }
2776
2777 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002778 n_channels = validate_scan_freqs(
2779 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002780 if (!n_channels) {
2781 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002782 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002783 }
2784 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002785 n_channels = 0;
2786
Johannes Berg2a519312009-02-10 21:25:55 +01002787 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2788 if (wiphy->bands[band])
2789 n_channels += wiphy->bands[band]->n_channels;
2790 }
2791
2792 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2793 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2794 n_ssids++;
2795
2796 if (n_ssids > wiphy->max_scan_ssids) {
2797 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002798 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002799 }
2800
Jouni Malinen70692ad2009-02-16 19:39:13 +02002801 if (info->attrs[NL80211_ATTR_IE])
2802 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2803 else
2804 ie_len = 0;
2805
Johannes Berg18a83652009-03-31 12:12:05 +02002806 if (ie_len > wiphy->max_scan_ie_len) {
2807 err = -EINVAL;
2808 goto out;
2809 }
2810
Johannes Berg2a519312009-02-10 21:25:55 +01002811 request = kzalloc(sizeof(*request)
2812 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002813 + sizeof(channel) * n_channels
2814 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002815 if (!request) {
2816 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002817 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002818 }
2819
2820 request->channels = (void *)((char *)request + sizeof(*request));
2821 request->n_channels = n_channels;
2822 if (n_ssids)
2823 request->ssids = (void *)(request->channels + n_channels);
2824 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002825 if (ie_len) {
2826 if (request->ssids)
2827 request->ie = (void *)(request->ssids + n_ssids);
2828 else
2829 request->ie = (void *)(request->channels + n_channels);
2830 }
Johannes Berg2a519312009-02-10 21:25:55 +01002831
2832 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2833 /* user specified, bail out if channel not found */
2834 request->n_channels = n_channels;
2835 i = 0;
2836 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2837 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2838 if (!request->channels[i]) {
2839 err = -EINVAL;
2840 goto out_free;
2841 }
2842 i++;
2843 }
2844 } else {
2845 /* all channels */
2846 i = 0;
2847 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2848 int j;
2849 if (!wiphy->bands[band])
2850 continue;
2851 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2852 request->channels[i] = &wiphy->bands[band]->channels[j];
2853 i++;
2854 }
2855 }
2856 }
2857
2858 i = 0;
2859 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2860 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2861 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2862 err = -EINVAL;
2863 goto out_free;
2864 }
2865 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2866 request->ssids[i].ssid_len = nla_len(attr);
2867 i++;
2868 }
2869 }
2870
Jouni Malinen70692ad2009-02-16 19:39:13 +02002871 if (info->attrs[NL80211_ATTR_IE]) {
2872 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02002873 memcpy((void *)request->ie,
2874 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02002875 request->ie_len);
2876 }
2877
Johannes Berg2a519312009-02-10 21:25:55 +01002878 request->ifidx = dev->ifindex;
Johannes Berg79c97e92009-07-07 03:56:12 +02002879 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002880
Johannes Berg79c97e92009-07-07 03:56:12 +02002881 rdev->scan_req = request;
2882 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01002883
Johannes Berga538e2d2009-06-16 19:56:42 +02002884 if (!err)
Johannes Berg79c97e92009-07-07 03:56:12 +02002885 nl80211_send_scan_start(rdev, dev);
Johannes Berga538e2d2009-06-16 19:56:42 +02002886
Johannes Berg2a519312009-02-10 21:25:55 +01002887 out_free:
2888 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002889 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01002890 kfree(request);
2891 }
Johannes Berg2a519312009-02-10 21:25:55 +01002892 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002893 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01002894 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002895 out_rtnl:
2896 rtnl_unlock();
2897
Johannes Berg2a519312009-02-10 21:25:55 +01002898 return err;
2899}
2900
2901static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2902 struct cfg80211_registered_device *rdev,
2903 struct net_device *dev,
2904 struct cfg80211_bss *res)
2905{
2906 void *hdr;
2907 struct nlattr *bss;
2908
2909 hdr = nl80211hdr_put(msg, pid, seq, flags,
2910 NL80211_CMD_NEW_SCAN_RESULTS);
2911 if (!hdr)
2912 return -1;
2913
2914 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2915 rdev->bss_generation);
2916 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2917
2918 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2919 if (!bss)
2920 goto nla_put_failure;
2921 if (!is_zero_ether_addr(res->bssid))
2922 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2923 if (res->information_elements && res->len_information_elements)
2924 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2925 res->len_information_elements,
2926 res->information_elements);
2927 if (res->tsf)
2928 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2929 if (res->beacon_interval)
2930 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2931 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2932 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2933
Johannes Berg77965c92009-02-18 18:45:06 +01002934 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002935 case CFG80211_SIGNAL_TYPE_MBM:
2936 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2937 break;
2938 case CFG80211_SIGNAL_TYPE_UNSPEC:
2939 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2940 break;
2941 default:
2942 break;
2943 }
2944
2945 nla_nest_end(msg, bss);
2946
2947 return genlmsg_end(msg, hdr);
2948
2949 nla_put_failure:
2950 genlmsg_cancel(msg, hdr);
2951 return -EMSGSIZE;
2952}
2953
2954static int nl80211_dump_scan(struct sk_buff *skb,
2955 struct netlink_callback *cb)
2956{
2957 struct cfg80211_registered_device *dev;
2958 struct net_device *netdev;
2959 struct cfg80211_internal_bss *scan;
2960 int ifidx = cb->args[0];
2961 int start = cb->args[1], idx = 0;
2962 int err;
2963
2964 if (!ifidx) {
2965 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2966 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2967 nl80211_policy);
2968 if (err)
2969 return err;
2970
2971 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2972 return -EINVAL;
2973
2974 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2975 if (!ifidx)
2976 return -EINVAL;
2977 cb->args[0] = ifidx;
2978 }
2979
2980 netdev = dev_get_by_index(&init_net, ifidx);
2981 if (!netdev)
2982 return -ENODEV;
2983
2984 dev = cfg80211_get_dev_from_ifindex(ifidx);
2985 if (IS_ERR(dev)) {
2986 err = PTR_ERR(dev);
2987 goto out_put_netdev;
2988 }
2989
2990 spin_lock_bh(&dev->bss_lock);
2991 cfg80211_bss_expire(dev);
2992
2993 list_for_each_entry(scan, &dev->bss_list, list) {
2994 if (++idx <= start)
2995 continue;
2996 if (nl80211_send_bss(skb,
2997 NETLINK_CB(cb->skb).pid,
2998 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2999 dev, netdev, &scan->pub) < 0) {
3000 idx--;
3001 goto out;
3002 }
3003 }
3004
3005 out:
3006 spin_unlock_bh(&dev->bss_lock);
3007
3008 cb->args[1] = idx;
3009 err = skb->len;
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003010 cfg80211_unlock_rdev(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003011 out_put_netdev:
3012 dev_put(netdev);
3013
3014 return err;
3015}
3016
Jouni Malinen255e7372009-03-20 21:21:17 +02003017static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3018{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003019 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003020}
3021
Samuel Ortizb23aa672009-07-01 21:26:54 +02003022static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3023{
3024 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3025 NL80211_WPA_VERSION_2));
3026}
3027
3028static bool nl80211_valid_akm_suite(u32 akm)
3029{
3030 return akm == WLAN_AKM_SUITE_8021X ||
3031 akm == WLAN_AKM_SUITE_PSK;
3032}
3033
3034static bool nl80211_valid_cipher_suite(u32 cipher)
3035{
3036 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3037 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3038 cipher == WLAN_CIPHER_SUITE_TKIP ||
3039 cipher == WLAN_CIPHER_SUITE_CCMP ||
3040 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3041}
3042
3043
Jouni Malinen636a5d32009-03-19 13:39:22 +02003044static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3045{
Johannes Berg79c97e92009-07-07 03:56:12 +02003046 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003047 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003048 struct ieee80211_channel *chan;
3049 const u8 *bssid, *ssid, *ie = NULL;
3050 int err, ssid_len, ie_len = 0;
3051 enum nl80211_auth_type auth_type;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003052
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003053 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3054 return -EINVAL;
3055
3056 if (!info->attrs[NL80211_ATTR_MAC])
3057 return -EINVAL;
3058
Jouni Malinen17780922009-03-27 20:52:47 +02003059 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3060 return -EINVAL;
3061
Johannes Berg19957bb2009-07-02 17:20:43 +02003062 if (!info->attrs[NL80211_ATTR_SSID])
3063 return -EINVAL;
3064
3065 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3066 return -EINVAL;
3067
Jouni Malinen636a5d32009-03-19 13:39:22 +02003068 rtnl_lock();
3069
Johannes Berg79c97e92009-07-07 03:56:12 +02003070 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003071 if (err)
3072 goto unlock_rtnl;
3073
Johannes Berg79c97e92009-07-07 03:56:12 +02003074 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003075 err = -EOPNOTSUPP;
3076 goto out;
3077 }
3078
Jouni Malineneec60b02009-03-20 21:21:19 +02003079 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3080 err = -EOPNOTSUPP;
3081 goto out;
3082 }
3083
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003084 if (!netif_running(dev)) {
3085 err = -ENETDOWN;
3086 goto out;
3087 }
3088
Johannes Berg19957bb2009-07-02 17:20:43 +02003089 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003090 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003091 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3092 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003093 err = -EINVAL;
3094 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003095 }
3096
Johannes Berg19957bb2009-07-02 17:20:43 +02003097 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3098 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3099
3100 if (info->attrs[NL80211_ATTR_IE]) {
3101 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3102 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3103 }
3104
3105 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3106 if (!nl80211_valid_auth_type(auth_type)) {
3107 err = -EINVAL;
3108 goto out;
3109 }
3110
Johannes Berg79c97e92009-07-07 03:56:12 +02003111 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Berg19957bb2009-07-02 17:20:43 +02003112 ssid, ssid_len, ie, ie_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003113
3114out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003115 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003116 dev_put(dev);
3117unlock_rtnl:
3118 rtnl_unlock();
3119 return err;
3120}
3121
Samuel Ortizb23aa672009-07-01 21:26:54 +02003122static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003123 struct cfg80211_crypto_settings *settings,
3124 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003125{
3126 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3127
3128 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3129 void *data;
3130 int len, i;
3131
3132 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3133 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3134 settings->n_ciphers_pairwise = len / sizeof(u32);
3135
3136 if (len % sizeof(u32))
3137 return -EINVAL;
3138
Johannes Berg3dc27d22009-07-02 21:36:37 +02003139 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003140 return -EINVAL;
3141
3142 memcpy(settings->ciphers_pairwise, data, len);
3143
3144 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3145 if (!nl80211_valid_cipher_suite(
3146 settings->ciphers_pairwise[i]))
3147 return -EINVAL;
3148 }
3149
3150 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3151 settings->cipher_group =
3152 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3153 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3154 return -EINVAL;
3155 }
3156
3157 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3158 settings->wpa_versions =
3159 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3160 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3161 return -EINVAL;
3162 }
3163
3164 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3165 void *data;
3166 int len, i;
3167
3168 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3169 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3170 settings->n_akm_suites = len / sizeof(u32);
3171
3172 if (len % sizeof(u32))
3173 return -EINVAL;
3174
3175 memcpy(settings->akm_suites, data, len);
3176
3177 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3178 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3179 return -EINVAL;
3180 }
3181
3182 return 0;
3183}
3184
Jouni Malinen636a5d32009-03-19 13:39:22 +02003185static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3186{
Johannes Berg19957bb2009-07-02 17:20:43 +02003187 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003188 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003189 struct cfg80211_crypto_settings crypto;
3190 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003191 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003192 int err, ssid_len, ie_len = 0;
3193 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003194
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003195 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3196 return -EINVAL;
3197
3198 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003199 !info->attrs[NL80211_ATTR_SSID] ||
3200 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003201 return -EINVAL;
3202
Jouni Malinen636a5d32009-03-19 13:39:22 +02003203 rtnl_lock();
3204
Johannes Berg79c97e92009-07-07 03:56:12 +02003205 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003206 if (err)
3207 goto unlock_rtnl;
3208
Johannes Berg19957bb2009-07-02 17:20:43 +02003209 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003210 err = -EOPNOTSUPP;
3211 goto out;
3212 }
3213
Jouni Malineneec60b02009-03-20 21:21:19 +02003214 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3215 err = -EOPNOTSUPP;
3216 goto out;
3217 }
3218
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003219 if (!netif_running(dev)) {
3220 err = -ENETDOWN;
3221 goto out;
3222 }
3223
Johannes Berg19957bb2009-07-02 17:20:43 +02003224 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003225
Johannes Berg19957bb2009-07-02 17:20:43 +02003226 chan = ieee80211_get_channel(&rdev->wiphy,
3227 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3228 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3229 err = -EINVAL;
3230 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003231 }
3232
Johannes Berg19957bb2009-07-02 17:20:43 +02003233 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3234 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003235
3236 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003237 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3238 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003239 }
3240
Jouni Malinendc6382c2009-05-06 22:09:37 +03003241 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003242 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003243 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003244 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003245 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003246 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382c2009-05-06 22:09:37 +03003247 err = -EINVAL;
3248 goto out;
3249 }
3250 }
3251
Johannes Berg3e5d7642009-07-07 14:37:26 +02003252 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3253 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3254
Johannes Berg3dc27d22009-07-02 21:36:37 +02003255 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003256 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003257 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3258 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003259 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003260
3261out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003262 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003263 dev_put(dev);
3264unlock_rtnl:
3265 rtnl_unlock();
3266 return err;
3267}
3268
3269static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3270{
Johannes Berg79c97e92009-07-07 03:56:12 +02003271 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003272 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003273 const u8 *ie = NULL, *bssid;
3274 int err, ie_len = 0;
3275 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003276
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003277 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3278 return -EINVAL;
3279
3280 if (!info->attrs[NL80211_ATTR_MAC])
3281 return -EINVAL;
3282
3283 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3284 return -EINVAL;
3285
Jouni Malinen636a5d32009-03-19 13:39:22 +02003286 rtnl_lock();
3287
Johannes Berg79c97e92009-07-07 03:56:12 +02003288 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003289 if (err)
3290 goto unlock_rtnl;
3291
Johannes Berg79c97e92009-07-07 03:56:12 +02003292 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003293 err = -EOPNOTSUPP;
3294 goto out;
3295 }
3296
Jouni Malineneec60b02009-03-20 21:21:19 +02003297 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3298 err = -EOPNOTSUPP;
3299 goto out;
3300 }
3301
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003302 if (!netif_running(dev)) {
3303 err = -ENETDOWN;
3304 goto out;
3305 }
3306
Johannes Berg19957bb2009-07-02 17:20:43 +02003307 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003308
Johannes Berg19957bb2009-07-02 17:20:43 +02003309 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3310 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003311 /* Reason Code 0 is reserved */
3312 err = -EINVAL;
3313 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003314 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003315
3316 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003317 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3318 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003319 }
3320
Johannes Berg79c97e92009-07-07 03:56:12 +02003321 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003322
3323out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003324 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003325 dev_put(dev);
3326unlock_rtnl:
3327 rtnl_unlock();
3328 return err;
3329}
3330
3331static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3332{
Johannes Berg79c97e92009-07-07 03:56:12 +02003333 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003334 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003335 const u8 *ie = NULL, *bssid;
3336 int err, ie_len = 0;
3337 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003338
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003339 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3340 return -EINVAL;
3341
3342 if (!info->attrs[NL80211_ATTR_MAC])
3343 return -EINVAL;
3344
3345 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3346 return -EINVAL;
3347
Jouni Malinen636a5d32009-03-19 13:39:22 +02003348 rtnl_lock();
3349
Johannes Berg79c97e92009-07-07 03:56:12 +02003350 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003351 if (err)
3352 goto unlock_rtnl;
3353
Johannes Berg79c97e92009-07-07 03:56:12 +02003354 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003355 err = -EOPNOTSUPP;
3356 goto out;
3357 }
3358
Jouni Malineneec60b02009-03-20 21:21:19 +02003359 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3360 err = -EOPNOTSUPP;
3361 goto out;
3362 }
3363
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003364 if (!netif_running(dev)) {
3365 err = -ENETDOWN;
3366 goto out;
3367 }
3368
Johannes Berg19957bb2009-07-02 17:20:43 +02003369 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003370
Johannes Berg19957bb2009-07-02 17:20:43 +02003371 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3372 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003373 /* Reason Code 0 is reserved */
3374 err = -EINVAL;
3375 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003376 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003377
3378 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003379 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3380 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003381 }
3382
Johannes Berg79c97e92009-07-07 03:56:12 +02003383 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003384
3385out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003386 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003387 dev_put(dev);
3388unlock_rtnl:
3389 rtnl_unlock();
3390 return err;
3391}
3392
Johannes Berg04a773a2009-04-19 21:24:32 +02003393static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3394{
Johannes Berg79c97e92009-07-07 03:56:12 +02003395 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003396 struct net_device *dev;
3397 struct cfg80211_ibss_params ibss;
3398 struct wiphy *wiphy;
3399 int err;
3400
Johannes Berg8e30bc52009-04-22 17:45:38 +02003401 memset(&ibss, 0, sizeof(ibss));
3402
Johannes Berg04a773a2009-04-19 21:24:32 +02003403 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3404 return -EINVAL;
3405
3406 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3407 !info->attrs[NL80211_ATTR_SSID] ||
3408 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3409 return -EINVAL;
3410
Johannes Berg8e30bc52009-04-22 17:45:38 +02003411 ibss.beacon_interval = 100;
3412
3413 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3414 ibss.beacon_interval =
3415 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3416 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3417 return -EINVAL;
3418 }
3419
Johannes Berg04a773a2009-04-19 21:24:32 +02003420 rtnl_lock();
3421
Johannes Berg79c97e92009-07-07 03:56:12 +02003422 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003423 if (err)
3424 goto unlock_rtnl;
3425
Johannes Berg79c97e92009-07-07 03:56:12 +02003426 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003427 err = -EOPNOTSUPP;
3428 goto out;
3429 }
3430
3431 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3432 err = -EOPNOTSUPP;
3433 goto out;
3434 }
3435
3436 if (!netif_running(dev)) {
3437 err = -ENETDOWN;
3438 goto out;
3439 }
3440
Johannes Berg79c97e92009-07-07 03:56:12 +02003441 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003442
3443 if (info->attrs[NL80211_ATTR_MAC])
3444 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3445 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3446 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3447
3448 if (info->attrs[NL80211_ATTR_IE]) {
3449 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3450 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3451 }
3452
3453 ibss.channel = ieee80211_get_channel(wiphy,
3454 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3455 if (!ibss.channel ||
3456 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3457 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3458 err = -EINVAL;
3459 goto out;
3460 }
3461
3462 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3463
Johannes Berg79c97e92009-07-07 03:56:12 +02003464 err = cfg80211_join_ibss(rdev, dev, &ibss);
Johannes Berg04a773a2009-04-19 21:24:32 +02003465
3466out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003467 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003468 dev_put(dev);
3469unlock_rtnl:
3470 rtnl_unlock();
3471 return err;
3472}
3473
3474static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3475{
Johannes Berg79c97e92009-07-07 03:56:12 +02003476 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003477 struct net_device *dev;
3478 int err;
3479
3480 rtnl_lock();
3481
Johannes Berg79c97e92009-07-07 03:56:12 +02003482 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003483 if (err)
3484 goto unlock_rtnl;
3485
Johannes Berg79c97e92009-07-07 03:56:12 +02003486 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003487 err = -EOPNOTSUPP;
3488 goto out;
3489 }
3490
3491 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3492 err = -EOPNOTSUPP;
3493 goto out;
3494 }
3495
3496 if (!netif_running(dev)) {
3497 err = -ENETDOWN;
3498 goto out;
3499 }
3500
Johannes Berg79c97e92009-07-07 03:56:12 +02003501 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003502
3503out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003504 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003505 dev_put(dev);
3506unlock_rtnl:
3507 rtnl_unlock();
3508 return err;
3509}
3510
Johannes Bergaff89a92009-07-01 21:26:51 +02003511#ifdef CONFIG_NL80211_TESTMODE
3512static struct genl_multicast_group nl80211_testmode_mcgrp = {
3513 .name = "testmode",
3514};
3515
3516static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3517{
3518 struct cfg80211_registered_device *rdev;
3519 int err;
3520
3521 if (!info->attrs[NL80211_ATTR_TESTDATA])
3522 return -EINVAL;
3523
3524 rtnl_lock();
3525
3526 rdev = cfg80211_get_dev_from_info(info);
3527 if (IS_ERR(rdev)) {
3528 err = PTR_ERR(rdev);
3529 goto unlock_rtnl;
3530 }
3531
3532 err = -EOPNOTSUPP;
3533 if (rdev->ops->testmode_cmd) {
3534 rdev->testmode_info = info;
3535 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3536 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3537 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3538 rdev->testmode_info = NULL;
3539 }
3540
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003541 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003542
3543 unlock_rtnl:
3544 rtnl_unlock();
3545 return err;
3546}
3547
3548static struct sk_buff *
3549__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3550 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3551{
3552 struct sk_buff *skb;
3553 void *hdr;
3554 struct nlattr *data;
3555
3556 skb = nlmsg_new(approxlen + 100, gfp);
3557 if (!skb)
3558 return NULL;
3559
3560 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3561 if (!hdr) {
3562 kfree_skb(skb);
3563 return NULL;
3564 }
3565
3566 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3567 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3568
3569 ((void **)skb->cb)[0] = rdev;
3570 ((void **)skb->cb)[1] = hdr;
3571 ((void **)skb->cb)[2] = data;
3572
3573 return skb;
3574
3575 nla_put_failure:
3576 kfree_skb(skb);
3577 return NULL;
3578}
3579
3580struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3581 int approxlen)
3582{
3583 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3584
3585 if (WARN_ON(!rdev->testmode_info))
3586 return NULL;
3587
3588 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3589 rdev->testmode_info->snd_pid,
3590 rdev->testmode_info->snd_seq,
3591 GFP_KERNEL);
3592}
3593EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3594
3595int cfg80211_testmode_reply(struct sk_buff *skb)
3596{
3597 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3598 void *hdr = ((void **)skb->cb)[1];
3599 struct nlattr *data = ((void **)skb->cb)[2];
3600
3601 if (WARN_ON(!rdev->testmode_info)) {
3602 kfree_skb(skb);
3603 return -EINVAL;
3604 }
3605
3606 nla_nest_end(skb, data);
3607 genlmsg_end(skb, hdr);
3608 return genlmsg_reply(skb, rdev->testmode_info);
3609}
3610EXPORT_SYMBOL(cfg80211_testmode_reply);
3611
3612struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3613 int approxlen, gfp_t gfp)
3614{
3615 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3616
3617 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3618}
3619EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3620
3621void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3622{
3623 void *hdr = ((void **)skb->cb)[1];
3624 struct nlattr *data = ((void **)skb->cb)[2];
3625
3626 nla_nest_end(skb, data);
3627 genlmsg_end(skb, hdr);
3628 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3629}
3630EXPORT_SYMBOL(cfg80211_testmode_event);
3631#endif
3632
Samuel Ortizb23aa672009-07-01 21:26:54 +02003633static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3634{
Johannes Berg79c97e92009-07-07 03:56:12 +02003635 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003636 struct net_device *dev;
3637 struct cfg80211_connect_params connect;
3638 struct wiphy *wiphy;
3639 int err;
3640
3641 memset(&connect, 0, sizeof(connect));
3642
3643 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3644 return -EINVAL;
3645
3646 if (!info->attrs[NL80211_ATTR_SSID] ||
3647 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3648 return -EINVAL;
3649
3650 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3651 connect.auth_type =
3652 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3653 if (!nl80211_valid_auth_type(connect.auth_type))
3654 return -EINVAL;
3655 } else
3656 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3657
3658 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3659
Johannes Berg3dc27d22009-07-02 21:36:37 +02003660 err = nl80211_crypto_settings(info, &connect.crypto,
3661 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003662 if (err)
3663 return err;
3664 rtnl_lock();
3665
Johannes Berg79c97e92009-07-07 03:56:12 +02003666 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003667 if (err)
3668 goto unlock_rtnl;
3669
3670 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3671 err = -EOPNOTSUPP;
3672 goto out;
3673 }
3674
3675 if (!netif_running(dev)) {
3676 err = -ENETDOWN;
3677 goto out;
3678 }
3679
Johannes Berg79c97e92009-07-07 03:56:12 +02003680 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003681
3682 connect.bssid = NULL;
3683 connect.channel = NULL;
3684 connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3685
3686 if (info->attrs[NL80211_ATTR_MAC])
3687 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3688 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3689 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3690
3691 if (info->attrs[NL80211_ATTR_IE]) {
3692 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3693 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3694 }
3695
3696 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3697 connect.channel =
3698 ieee80211_get_channel(wiphy,
3699 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3700 if (!connect.channel ||
3701 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
3702 err = -EINVAL;
3703 goto out;
3704 }
3705 }
3706
Johannes Berg79c97e92009-07-07 03:56:12 +02003707 err = cfg80211_connect(rdev, dev, &connect);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003708
3709out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003710 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003711 dev_put(dev);
3712unlock_rtnl:
3713 rtnl_unlock();
3714 return err;
3715}
3716
3717static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3718{
Johannes Berg79c97e92009-07-07 03:56:12 +02003719 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003720 struct net_device *dev;
3721 int err;
3722 u16 reason;
3723
3724 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3725 reason = WLAN_REASON_DEAUTH_LEAVING;
3726 else
3727 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3728
3729 if (reason == 0)
3730 return -EINVAL;
3731
3732 rtnl_lock();
3733
Johannes Berg79c97e92009-07-07 03:56:12 +02003734 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003735 if (err)
3736 goto unlock_rtnl;
3737
3738 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3739 err = -EOPNOTSUPP;
3740 goto out;
3741 }
3742
3743 if (!netif_running(dev)) {
3744 err = -ENETDOWN;
3745 goto out;
3746 }
3747
Johannes Berg79c97e92009-07-07 03:56:12 +02003748 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003749
3750out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003751 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003752 dev_put(dev);
3753unlock_rtnl:
3754 rtnl_unlock();
3755 return err;
3756}
3757
Johannes Berg55682962007-09-20 13:09:35 -04003758static struct genl_ops nl80211_ops[] = {
3759 {
3760 .cmd = NL80211_CMD_GET_WIPHY,
3761 .doit = nl80211_get_wiphy,
3762 .dumpit = nl80211_dump_wiphy,
3763 .policy = nl80211_policy,
3764 /* can be retrieved by unprivileged users */
3765 },
3766 {
3767 .cmd = NL80211_CMD_SET_WIPHY,
3768 .doit = nl80211_set_wiphy,
3769 .policy = nl80211_policy,
3770 .flags = GENL_ADMIN_PERM,
3771 },
3772 {
3773 .cmd = NL80211_CMD_GET_INTERFACE,
3774 .doit = nl80211_get_interface,
3775 .dumpit = nl80211_dump_interface,
3776 .policy = nl80211_policy,
3777 /* can be retrieved by unprivileged users */
3778 },
3779 {
3780 .cmd = NL80211_CMD_SET_INTERFACE,
3781 .doit = nl80211_set_interface,
3782 .policy = nl80211_policy,
3783 .flags = GENL_ADMIN_PERM,
3784 },
3785 {
3786 .cmd = NL80211_CMD_NEW_INTERFACE,
3787 .doit = nl80211_new_interface,
3788 .policy = nl80211_policy,
3789 .flags = GENL_ADMIN_PERM,
3790 },
3791 {
3792 .cmd = NL80211_CMD_DEL_INTERFACE,
3793 .doit = nl80211_del_interface,
3794 .policy = nl80211_policy,
3795 .flags = GENL_ADMIN_PERM,
3796 },
Johannes Berg41ade002007-12-19 02:03:29 +01003797 {
3798 .cmd = NL80211_CMD_GET_KEY,
3799 .doit = nl80211_get_key,
3800 .policy = nl80211_policy,
3801 .flags = GENL_ADMIN_PERM,
3802 },
3803 {
3804 .cmd = NL80211_CMD_SET_KEY,
3805 .doit = nl80211_set_key,
3806 .policy = nl80211_policy,
3807 .flags = GENL_ADMIN_PERM,
3808 },
3809 {
3810 .cmd = NL80211_CMD_NEW_KEY,
3811 .doit = nl80211_new_key,
3812 .policy = nl80211_policy,
3813 .flags = GENL_ADMIN_PERM,
3814 },
3815 {
3816 .cmd = NL80211_CMD_DEL_KEY,
3817 .doit = nl80211_del_key,
3818 .policy = nl80211_policy,
3819 .flags = GENL_ADMIN_PERM,
3820 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01003821 {
3822 .cmd = NL80211_CMD_SET_BEACON,
3823 .policy = nl80211_policy,
3824 .flags = GENL_ADMIN_PERM,
3825 .doit = nl80211_addset_beacon,
3826 },
3827 {
3828 .cmd = NL80211_CMD_NEW_BEACON,
3829 .policy = nl80211_policy,
3830 .flags = GENL_ADMIN_PERM,
3831 .doit = nl80211_addset_beacon,
3832 },
3833 {
3834 .cmd = NL80211_CMD_DEL_BEACON,
3835 .policy = nl80211_policy,
3836 .flags = GENL_ADMIN_PERM,
3837 .doit = nl80211_del_beacon,
3838 },
Johannes Berg5727ef12007-12-19 02:03:34 +01003839 {
3840 .cmd = NL80211_CMD_GET_STATION,
3841 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003842 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01003843 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01003844 },
3845 {
3846 .cmd = NL80211_CMD_SET_STATION,
3847 .doit = nl80211_set_station,
3848 .policy = nl80211_policy,
3849 .flags = GENL_ADMIN_PERM,
3850 },
3851 {
3852 .cmd = NL80211_CMD_NEW_STATION,
3853 .doit = nl80211_new_station,
3854 .policy = nl80211_policy,
3855 .flags = GENL_ADMIN_PERM,
3856 },
3857 {
3858 .cmd = NL80211_CMD_DEL_STATION,
3859 .doit = nl80211_del_station,
3860 .policy = nl80211_policy,
3861 .flags = GENL_ADMIN_PERM,
3862 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003863 {
3864 .cmd = NL80211_CMD_GET_MPATH,
3865 .doit = nl80211_get_mpath,
3866 .dumpit = nl80211_dump_mpath,
3867 .policy = nl80211_policy,
3868 .flags = GENL_ADMIN_PERM,
3869 },
3870 {
3871 .cmd = NL80211_CMD_SET_MPATH,
3872 .doit = nl80211_set_mpath,
3873 .policy = nl80211_policy,
3874 .flags = GENL_ADMIN_PERM,
3875 },
3876 {
3877 .cmd = NL80211_CMD_NEW_MPATH,
3878 .doit = nl80211_new_mpath,
3879 .policy = nl80211_policy,
3880 .flags = GENL_ADMIN_PERM,
3881 },
3882 {
3883 .cmd = NL80211_CMD_DEL_MPATH,
3884 .doit = nl80211_del_mpath,
3885 .policy = nl80211_policy,
3886 .flags = GENL_ADMIN_PERM,
3887 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003888 {
3889 .cmd = NL80211_CMD_SET_BSS,
3890 .doit = nl80211_set_bss,
3891 .policy = nl80211_policy,
3892 .flags = GENL_ADMIN_PERM,
3893 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003894 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003895 .cmd = NL80211_CMD_GET_REG,
3896 .doit = nl80211_get_reg,
3897 .policy = nl80211_policy,
3898 /* can be retrieved by unprivileged users */
3899 },
3900 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003901 .cmd = NL80211_CMD_SET_REG,
3902 .doit = nl80211_set_reg,
3903 .policy = nl80211_policy,
3904 .flags = GENL_ADMIN_PERM,
3905 },
3906 {
3907 .cmd = NL80211_CMD_REQ_SET_REG,
3908 .doit = nl80211_req_set_reg,
3909 .policy = nl80211_policy,
3910 .flags = GENL_ADMIN_PERM,
3911 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003912 {
3913 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3914 .doit = nl80211_get_mesh_params,
3915 .policy = nl80211_policy,
3916 /* can be retrieved by unprivileged users */
3917 },
3918 {
3919 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3920 .doit = nl80211_set_mesh_params,
3921 .policy = nl80211_policy,
3922 .flags = GENL_ADMIN_PERM,
3923 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02003924 {
Johannes Berg2a519312009-02-10 21:25:55 +01003925 .cmd = NL80211_CMD_TRIGGER_SCAN,
3926 .doit = nl80211_trigger_scan,
3927 .policy = nl80211_policy,
3928 .flags = GENL_ADMIN_PERM,
3929 },
3930 {
3931 .cmd = NL80211_CMD_GET_SCAN,
3932 .policy = nl80211_policy,
3933 .dumpit = nl80211_dump_scan,
3934 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02003935 {
3936 .cmd = NL80211_CMD_AUTHENTICATE,
3937 .doit = nl80211_authenticate,
3938 .policy = nl80211_policy,
3939 .flags = GENL_ADMIN_PERM,
3940 },
3941 {
3942 .cmd = NL80211_CMD_ASSOCIATE,
3943 .doit = nl80211_associate,
3944 .policy = nl80211_policy,
3945 .flags = GENL_ADMIN_PERM,
3946 },
3947 {
3948 .cmd = NL80211_CMD_DEAUTHENTICATE,
3949 .doit = nl80211_deauthenticate,
3950 .policy = nl80211_policy,
3951 .flags = GENL_ADMIN_PERM,
3952 },
3953 {
3954 .cmd = NL80211_CMD_DISASSOCIATE,
3955 .doit = nl80211_disassociate,
3956 .policy = nl80211_policy,
3957 .flags = GENL_ADMIN_PERM,
3958 },
Johannes Berg04a773a2009-04-19 21:24:32 +02003959 {
3960 .cmd = NL80211_CMD_JOIN_IBSS,
3961 .doit = nl80211_join_ibss,
3962 .policy = nl80211_policy,
3963 .flags = GENL_ADMIN_PERM,
3964 },
3965 {
3966 .cmd = NL80211_CMD_LEAVE_IBSS,
3967 .doit = nl80211_leave_ibss,
3968 .policy = nl80211_policy,
3969 .flags = GENL_ADMIN_PERM,
3970 },
Johannes Bergaff89a92009-07-01 21:26:51 +02003971#ifdef CONFIG_NL80211_TESTMODE
3972 {
3973 .cmd = NL80211_CMD_TESTMODE,
3974 .doit = nl80211_testmode_do,
3975 .policy = nl80211_policy,
3976 .flags = GENL_ADMIN_PERM,
3977 },
3978#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02003979 {
3980 .cmd = NL80211_CMD_CONNECT,
3981 .doit = nl80211_connect,
3982 .policy = nl80211_policy,
3983 .flags = GENL_ADMIN_PERM,
3984 },
3985 {
3986 .cmd = NL80211_CMD_DISCONNECT,
3987 .doit = nl80211_disconnect,
3988 .policy = nl80211_policy,
3989 .flags = GENL_ADMIN_PERM,
3990 },
Johannes Berg55682962007-09-20 13:09:35 -04003991};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003992static struct genl_multicast_group nl80211_mlme_mcgrp = {
3993 .name = "mlme",
3994};
Johannes Berg55682962007-09-20 13:09:35 -04003995
3996/* multicast groups */
3997static struct genl_multicast_group nl80211_config_mcgrp = {
3998 .name = "config",
3999};
Johannes Berg2a519312009-02-10 21:25:55 +01004000static struct genl_multicast_group nl80211_scan_mcgrp = {
4001 .name = "scan",
4002};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004003static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4004 .name = "regulatory",
4005};
Johannes Berg55682962007-09-20 13:09:35 -04004006
4007/* notification functions */
4008
4009void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4010{
4011 struct sk_buff *msg;
4012
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004014 if (!msg)
4015 return;
4016
4017 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4018 nlmsg_free(msg);
4019 return;
4020 }
4021
4022 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
4023}
4024
Johannes Berg362a4152009-05-24 16:43:15 +02004025static int nl80211_add_scan_req(struct sk_buff *msg,
4026 struct cfg80211_registered_device *rdev)
4027{
4028 struct cfg80211_scan_request *req = rdev->scan_req;
4029 struct nlattr *nest;
4030 int i;
4031
Johannes Berg667503dd2009-07-07 03:56:11 +02004032 ASSERT_RDEV_LOCK(rdev);
4033
Johannes Berg362a4152009-05-24 16:43:15 +02004034 if (WARN_ON(!req))
4035 return 0;
4036
4037 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4038 if (!nest)
4039 goto nla_put_failure;
4040 for (i = 0; i < req->n_ssids; i++)
4041 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4042 nla_nest_end(msg, nest);
4043
4044 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4045 if (!nest)
4046 goto nla_put_failure;
4047 for (i = 0; i < req->n_channels; i++)
4048 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4049 nla_nest_end(msg, nest);
4050
4051 if (req->ie)
4052 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4053
4054 return 0;
4055 nla_put_failure:
4056 return -ENOBUFS;
4057}
4058
Johannes Berga538e2d2009-06-16 19:56:42 +02004059static int nl80211_send_scan_msg(struct sk_buff *msg,
4060 struct cfg80211_registered_device *rdev,
4061 struct net_device *netdev,
4062 u32 pid, u32 seq, int flags,
4063 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004064{
4065 void *hdr;
4066
4067 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4068 if (!hdr)
4069 return -1;
4070
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004071 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004072 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4073
Johannes Berg362a4152009-05-24 16:43:15 +02004074 /* ignore errors and send incomplete event anyway */
4075 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004076
4077 return genlmsg_end(msg, hdr);
4078
4079 nla_put_failure:
4080 genlmsg_cancel(msg, hdr);
4081 return -EMSGSIZE;
4082}
4083
Johannes Berga538e2d2009-06-16 19:56:42 +02004084void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4085 struct net_device *netdev)
4086{
4087 struct sk_buff *msg;
4088
4089 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4090 if (!msg)
4091 return;
4092
4093 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4094 NL80211_CMD_TRIGGER_SCAN) < 0) {
4095 nlmsg_free(msg);
4096 return;
4097 }
4098
4099 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
4100}
4101
Johannes Berg2a519312009-02-10 21:25:55 +01004102void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4103 struct net_device *netdev)
4104{
4105 struct sk_buff *msg;
4106
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004107 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004108 if (!msg)
4109 return;
4110
Johannes Berga538e2d2009-06-16 19:56:42 +02004111 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4112 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004113 nlmsg_free(msg);
4114 return;
4115 }
4116
4117 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
4118}
4119
4120void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4121 struct net_device *netdev)
4122{
4123 struct sk_buff *msg;
4124
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004125 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004126 if (!msg)
4127 return;
4128
Johannes Berga538e2d2009-06-16 19:56:42 +02004129 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4130 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004131 nlmsg_free(msg);
4132 return;
4133 }
4134
4135 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
4136}
4137
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004138/*
4139 * This can happen on global regulatory changes or device specific settings
4140 * based on custom world regulatory domains.
4141 */
4142void nl80211_send_reg_change_event(struct regulatory_request *request)
4143{
4144 struct sk_buff *msg;
4145 void *hdr;
4146
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004147 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004148 if (!msg)
4149 return;
4150
4151 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4152 if (!hdr) {
4153 nlmsg_free(msg);
4154 return;
4155 }
4156
4157 /* Userspace can always count this one always being set */
4158 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4159
4160 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4161 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4162 NL80211_REGDOM_TYPE_WORLD);
4163 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4164 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4165 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4166 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4167 request->intersect)
4168 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4169 NL80211_REGDOM_TYPE_INTERSECTION);
4170 else {
4171 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4172 NL80211_REGDOM_TYPE_COUNTRY);
4173 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4174 }
4175
4176 if (wiphy_idx_valid(request->wiphy_idx))
4177 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4178
4179 if (genlmsg_end(msg, hdr) < 0) {
4180 nlmsg_free(msg);
4181 return;
4182 }
4183
4184 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
4185
4186 return;
4187
4188nla_put_failure:
4189 genlmsg_cancel(msg, hdr);
4190 nlmsg_free(msg);
4191}
4192
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004193static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4194 struct net_device *netdev,
4195 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004196 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004197{
4198 struct sk_buff *msg;
4199 void *hdr;
4200
Johannes Berge6d6e342009-07-01 21:26:47 +02004201 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004202 if (!msg)
4203 return;
4204
4205 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4206 if (!hdr) {
4207 nlmsg_free(msg);
4208 return;
4209 }
4210
4211 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4212 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4213 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
4214
4215 if (genlmsg_end(msg, hdr) < 0) {
4216 nlmsg_free(msg);
4217 return;
4218 }
4219
Johannes Berge6d6e342009-07-01 21:26:47 +02004220 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004221 return;
4222
4223 nla_put_failure:
4224 genlmsg_cancel(msg, hdr);
4225 nlmsg_free(msg);
4226}
4227
4228void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004229 struct net_device *netdev, const u8 *buf,
4230 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004231{
4232 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004233 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004234}
4235
4236void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
4237 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004238 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004239{
Johannes Berge6d6e342009-07-01 21:26:47 +02004240 nl80211_send_mlme_event(rdev, netdev, buf, len,
4241 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004242}
4243
Jouni Malinen53b46b82009-03-27 20:53:56 +02004244void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004245 struct net_device *netdev, const u8 *buf,
4246 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004247{
4248 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004249 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004250}
4251
Jouni Malinen53b46b82009-03-27 20:53:56 +02004252void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
4253 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004254 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004255{
4256 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004257 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004258}
4259
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04004260static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
4261 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02004262 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004263{
4264 struct sk_buff *msg;
4265 void *hdr;
4266
Johannes Berge6d6e342009-07-01 21:26:47 +02004267 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004268 if (!msg)
4269 return;
4270
4271 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4272 if (!hdr) {
4273 nlmsg_free(msg);
4274 return;
4275 }
4276
4277 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4278 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4279 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
4280 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4281
4282 if (genlmsg_end(msg, hdr) < 0) {
4283 nlmsg_free(msg);
4284 return;
4285 }
4286
Johannes Berge6d6e342009-07-01 21:26:47 +02004287 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004288 return;
4289
4290 nla_put_failure:
4291 genlmsg_cancel(msg, hdr);
4292 nlmsg_free(msg);
4293}
4294
4295void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004296 struct net_device *netdev, const u8 *addr,
4297 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004298{
4299 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02004300 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004301}
4302
4303void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004304 struct net_device *netdev, const u8 *addr,
4305 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004306{
Johannes Berge6d6e342009-07-01 21:26:47 +02004307 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
4308 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004309}
4310
Samuel Ortizb23aa672009-07-01 21:26:54 +02004311void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
4312 struct net_device *netdev, const u8 *bssid,
4313 const u8 *req_ie, size_t req_ie_len,
4314 const u8 *resp_ie, size_t resp_ie_len,
4315 u16 status, gfp_t gfp)
4316{
4317 struct sk_buff *msg;
4318 void *hdr;
4319
4320 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4321 if (!msg)
4322 return;
4323
4324 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
4325 if (!hdr) {
4326 nlmsg_free(msg);
4327 return;
4328 }
4329
4330 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4331 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4332 if (bssid)
4333 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4334 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
4335 if (req_ie)
4336 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4337 if (resp_ie)
4338 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4339
4340 if (genlmsg_end(msg, hdr) < 0) {
4341 nlmsg_free(msg);
4342 return;
4343 }
4344
4345 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
4346 return;
4347
4348 nla_put_failure:
4349 genlmsg_cancel(msg, hdr);
4350 nlmsg_free(msg);
4351
4352}
4353
4354void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
4355 struct net_device *netdev, const u8 *bssid,
4356 const u8 *req_ie, size_t req_ie_len,
4357 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
4358{
4359 struct sk_buff *msg;
4360 void *hdr;
4361
4362 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4363 if (!msg)
4364 return;
4365
4366 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
4367 if (!hdr) {
4368 nlmsg_free(msg);
4369 return;
4370 }
4371
4372 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4373 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4374 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4375 if (req_ie)
4376 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4377 if (resp_ie)
4378 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4379
4380 if (genlmsg_end(msg, hdr) < 0) {
4381 nlmsg_free(msg);
4382 return;
4383 }
4384
4385 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
4386 return;
4387
4388 nla_put_failure:
4389 genlmsg_cancel(msg, hdr);
4390 nlmsg_free(msg);
4391
4392}
4393
4394void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
4395 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02004396 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004397{
4398 struct sk_buff *msg;
4399 void *hdr;
4400
Johannes Berg667503dd2009-07-07 03:56:11 +02004401 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004402 if (!msg)
4403 return;
4404
4405 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
4406 if (!hdr) {
4407 nlmsg_free(msg);
4408 return;
4409 }
4410
4411 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4412 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4413 if (from_ap && reason)
4414 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
4415 if (from_ap)
4416 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
4417 if (ie)
4418 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
4419
4420 if (genlmsg_end(msg, hdr) < 0) {
4421 nlmsg_free(msg);
4422 return;
4423 }
4424
Johannes Berg667503dd2009-07-07 03:56:11 +02004425 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004426 return;
4427
4428 nla_put_failure:
4429 genlmsg_cancel(msg, hdr);
4430 nlmsg_free(msg);
4431
4432}
4433
Johannes Berg04a773a2009-04-19 21:24:32 +02004434void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
4435 struct net_device *netdev, const u8 *bssid,
4436 gfp_t gfp)
4437{
4438 struct sk_buff *msg;
4439 void *hdr;
4440
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004441 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02004442 if (!msg)
4443 return;
4444
4445 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
4446 if (!hdr) {
4447 nlmsg_free(msg);
4448 return;
4449 }
4450
4451 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4452 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4453 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4454
4455 if (genlmsg_end(msg, hdr) < 0) {
4456 nlmsg_free(msg);
4457 return;
4458 }
4459
4460 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
4461 return;
4462
4463 nla_put_failure:
4464 genlmsg_cancel(msg, hdr);
4465 nlmsg_free(msg);
4466}
4467
Jouni Malinena3b8b052009-03-27 21:59:49 +02004468void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
4469 struct net_device *netdev, const u8 *addr,
4470 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02004471 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02004472{
4473 struct sk_buff *msg;
4474 void *hdr;
4475
Johannes Berge6d6e342009-07-01 21:26:47 +02004476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004477 if (!msg)
4478 return;
4479
4480 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
4481 if (!hdr) {
4482 nlmsg_free(msg);
4483 return;
4484 }
4485
4486 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4487 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4488 if (addr)
4489 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4490 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
4491 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
4492 if (tsc)
4493 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
4494
4495 if (genlmsg_end(msg, hdr) < 0) {
4496 nlmsg_free(msg);
4497 return;
4498 }
4499
Johannes Berge6d6e342009-07-01 21:26:47 +02004500 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004501 return;
4502
4503 nla_put_failure:
4504 genlmsg_cancel(msg, hdr);
4505 nlmsg_free(msg);
4506}
4507
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004508void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
4509 struct ieee80211_channel *channel_before,
4510 struct ieee80211_channel *channel_after)
4511{
4512 struct sk_buff *msg;
4513 void *hdr;
4514 struct nlattr *nl_freq;
4515
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004517 if (!msg)
4518 return;
4519
4520 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
4521 if (!hdr) {
4522 nlmsg_free(msg);
4523 return;
4524 }
4525
4526 /*
4527 * Since we are applying the beacon hint to a wiphy we know its
4528 * wiphy_idx is valid
4529 */
4530 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
4531
4532 /* Before */
4533 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
4534 if (!nl_freq)
4535 goto nla_put_failure;
4536 if (nl80211_msg_put_channel(msg, channel_before))
4537 goto nla_put_failure;
4538 nla_nest_end(msg, nl_freq);
4539
4540 /* After */
4541 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
4542 if (!nl_freq)
4543 goto nla_put_failure;
4544 if (nl80211_msg_put_channel(msg, channel_after))
4545 goto nla_put_failure;
4546 nla_nest_end(msg, nl_freq);
4547
4548 if (genlmsg_end(msg, hdr) < 0) {
4549 nlmsg_free(msg);
4550 return;
4551 }
4552
4553 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_ATOMIC);
4554
4555 return;
4556
4557nla_put_failure:
4558 genlmsg_cancel(msg, hdr);
4559 nlmsg_free(msg);
4560}
4561
Johannes Berg55682962007-09-20 13:09:35 -04004562/* initialisation/exit functions */
4563
4564int nl80211_init(void)
4565{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004566 int err;
Johannes Berg55682962007-09-20 13:09:35 -04004567
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004568 err = genl_register_family_with_ops(&nl80211_fam,
4569 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04004570 if (err)
4571 return err;
4572
Johannes Berg55682962007-09-20 13:09:35 -04004573 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
4574 if (err)
4575 goto err_out;
4576
Johannes Berg2a519312009-02-10 21:25:55 +01004577 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
4578 if (err)
4579 goto err_out;
4580
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004581 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
4582 if (err)
4583 goto err_out;
4584
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004585 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
4586 if (err)
4587 goto err_out;
4588
Johannes Bergaff89a92009-07-01 21:26:51 +02004589#ifdef CONFIG_NL80211_TESTMODE
4590 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
4591 if (err)
4592 goto err_out;
4593#endif
4594
Johannes Berg55682962007-09-20 13:09:35 -04004595 return 0;
4596 err_out:
4597 genl_unregister_family(&nl80211_fam);
4598 return err;
4599}
4600
4601void nl80211_exit(void)
4602{
4603 genl_unregister_family(&nl80211_fam);
4604}