blob: fd392fff5fb6aacf4c5597f612231e730775da29 [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
32/* internal helper: get drv and dev */
Johannes Bergbba95fe2008-07-29 13:22:51 +020033static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
Johannes Berg55682962007-09-20 13:09:35 -040034 struct cfg80211_registered_device **drv,
35 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
47 *drv = cfg80211_get_dev_from_ifindex(ifindex);
48 if (IS_ERR(*drv)) {
49 dev_put(*dev);
50 return PTR_ERR(*drv);
51 }
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 },
74
75 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
76 .len = WLAN_MAX_KEY_LEN },
77 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
78 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
79 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030080 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010081
82 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
83 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
84 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
85 .len = IEEE80211_MAX_DATA_LEN },
86 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
87 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010088 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
89 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
90 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
91 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
92 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010093 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010094 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +020095 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010096 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
97 .len = IEEE80211_MAX_MESH_ID_LEN },
98 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030099
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700100 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
101 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
102
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300103 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
104 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
105 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200106 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
107 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300108
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700109 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
110
Jouni Malinen36aedc92008-08-25 11:58:58 +0300111 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
112 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200113
114 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
115 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
116 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100117 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
118 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200119
120 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
121 .len = IEEE80211_MAX_SSID_LEN },
122 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
123 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200124 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300125 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300126 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300127 [NL80211_ATTR_STA_FLAGS2] = {
128 .len = sizeof(struct nl80211_sta_flag_update),
129 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300130 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400131};
132
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100133/* IE validation */
134static bool is_valid_ie_attr(const struct nlattr *attr)
135{
136 const u8 *pos;
137 int len;
138
139 if (!attr)
140 return true;
141
142 pos = nla_data(attr);
143 len = nla_len(attr);
144
145 while (len) {
146 u8 elemlen;
147
148 if (len < 2)
149 return false;
150 len -= 2;
151
152 elemlen = pos[1];
153 if (elemlen > len)
154 return false;
155
156 len -= elemlen;
157 pos += 2 + elemlen;
158 }
159
160 return true;
161}
162
Johannes Berg55682962007-09-20 13:09:35 -0400163/* message building helper */
164static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
165 int flags, u8 cmd)
166{
167 /* since there is no private header just add the generic one */
168 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
169}
170
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400171static int nl80211_msg_put_channel(struct sk_buff *msg,
172 struct ieee80211_channel *chan)
173{
174 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
175 chan->center_freq);
176
177 if (chan->flags & IEEE80211_CHAN_DISABLED)
178 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
179 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
180 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
181 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
182 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
183 if (chan->flags & IEEE80211_CHAN_RADAR)
184 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
185
186 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
187 DBM_TO_MBM(chan->max_power));
188
189 return 0;
190
191 nla_put_failure:
192 return -ENOBUFS;
193}
194
Johannes Berg55682962007-09-20 13:09:35 -0400195/* netlink command implementations */
196
197static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
198 struct cfg80211_registered_device *dev)
199{
200 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100201 struct nlattr *nl_bands, *nl_band;
202 struct nlattr *nl_freqs, *nl_freq;
203 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700204 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100205 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100206 enum ieee80211_band band;
207 struct ieee80211_channel *chan;
208 struct ieee80211_rate *rate;
209 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700210 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400211
212 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
213 if (!hdr)
214 return -1;
215
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500216 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400217 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200218
219 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
220 dev->wiphy.retry_short);
221 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
222 dev->wiphy.retry_long);
223 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
224 dev->wiphy.frag_threshold);
225 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
226 dev->wiphy.rts_threshold);
227
Johannes Berg2a519312009-02-10 21:25:55 +0100228 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
229 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200230 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
231 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100232
Johannes Berg25e47c182009-04-02 20:14:06 +0200233 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
234 sizeof(u32) * dev->wiphy.n_cipher_suites,
235 dev->wiphy.cipher_suites);
236
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700237 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
238 if (!nl_modes)
239 goto nla_put_failure;
240
241 i = 0;
242 while (ifmodes) {
243 if (ifmodes & 1)
244 NLA_PUT_FLAG(msg, i);
245 ifmodes >>= 1;
246 i++;
247 }
248
249 nla_nest_end(msg, nl_modes);
250
Johannes Bergee688b002008-01-24 19:38:39 +0100251 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
252 if (!nl_bands)
253 goto nla_put_failure;
254
255 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
256 if (!dev->wiphy.bands[band])
257 continue;
258
259 nl_band = nla_nest_start(msg, band);
260 if (!nl_band)
261 goto nla_put_failure;
262
Johannes Bergd51626d2008-10-09 12:20:13 +0200263 /* add HT info */
264 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
265 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
266 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
267 &dev->wiphy.bands[band]->ht_cap.mcs);
268 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
269 dev->wiphy.bands[band]->ht_cap.cap);
270 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
271 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
272 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
273 dev->wiphy.bands[band]->ht_cap.ampdu_density);
274 }
275
Johannes Bergee688b002008-01-24 19:38:39 +0100276 /* add frequencies */
277 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
278 if (!nl_freqs)
279 goto nla_put_failure;
280
281 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
282 nl_freq = nla_nest_start(msg, i);
283 if (!nl_freq)
284 goto nla_put_failure;
285
286 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100287
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400288 if (nl80211_msg_put_channel(msg, chan))
289 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200290
Johannes Bergee688b002008-01-24 19:38:39 +0100291 nla_nest_end(msg, nl_freq);
292 }
293
294 nla_nest_end(msg, nl_freqs);
295
296 /* add bitrates */
297 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
298 if (!nl_rates)
299 goto nla_put_failure;
300
301 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
302 nl_rate = nla_nest_start(msg, i);
303 if (!nl_rate)
304 goto nla_put_failure;
305
306 rate = &dev->wiphy.bands[band]->bitrates[i];
307 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
308 rate->bitrate);
309 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
310 NLA_PUT_FLAG(msg,
311 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
312
313 nla_nest_end(msg, nl_rate);
314 }
315
316 nla_nest_end(msg, nl_rates);
317
318 nla_nest_end(msg, nl_band);
319 }
320 nla_nest_end(msg, nl_bands);
321
Johannes Berg8fdc6212009-03-14 09:34:01 +0100322 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
323 if (!nl_cmds)
324 goto nla_put_failure;
325
326 i = 0;
327#define CMD(op, n) \
328 do { \
329 if (dev->ops->op) { \
330 i++; \
331 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
332 } \
333 } while (0)
334
335 CMD(add_virtual_intf, NEW_INTERFACE);
336 CMD(change_virtual_intf, SET_INTERFACE);
337 CMD(add_key, NEW_KEY);
338 CMD(add_beacon, NEW_BEACON);
339 CMD(add_station, NEW_STATION);
340 CMD(add_mpath, NEW_MPATH);
341 CMD(set_mesh_params, SET_MESH_PARAMS);
342 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200343 CMD(auth, AUTHENTICATE);
344 CMD(assoc, ASSOCIATE);
345 CMD(deauth, DEAUTHENTICATE);
346 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200347 CMD(join_ibss, JOIN_IBSS);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100348
349#undef CMD
350 nla_nest_end(msg, nl_cmds);
351
Johannes Berg55682962007-09-20 13:09:35 -0400352 return genlmsg_end(msg, hdr);
353
354 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700355 genlmsg_cancel(msg, hdr);
356 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400357}
358
359static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
360{
361 int idx = 0;
362 int start = cb->args[0];
363 struct cfg80211_registered_device *dev;
364
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500365 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400366 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200367 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400368 continue;
369 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
370 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200371 dev) < 0) {
372 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400373 break;
Julius Volzb4637272008-07-08 14:02:19 +0200374 }
Johannes Berg55682962007-09-20 13:09:35 -0400375 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500376 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400377
378 cb->args[0] = idx;
379
380 return skb->len;
381}
382
383static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
384{
385 struct sk_buff *msg;
386 struct cfg80211_registered_device *dev;
387
388 dev = cfg80211_get_dev_from_info(info);
389 if (IS_ERR(dev))
390 return PTR_ERR(dev);
391
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700392 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400393 if (!msg)
394 goto out_err;
395
396 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
397 goto out_free;
398
399 cfg80211_put_dev(dev);
400
401 return genlmsg_unicast(msg, info->snd_pid);
402
403 out_free:
404 nlmsg_free(msg);
405 out_err:
406 cfg80211_put_dev(dev);
407 return -ENOBUFS;
408}
409
Jouni Malinen31888482008-10-30 16:59:24 +0200410static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
411 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
412 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
413 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
414 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
415 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
416};
417
418static int parse_txq_params(struct nlattr *tb[],
419 struct ieee80211_txq_params *txq_params)
420{
421 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
422 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
423 !tb[NL80211_TXQ_ATTR_AIFS])
424 return -EINVAL;
425
426 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
427 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
428 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
429 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
430 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
431
432 return 0;
433}
434
Johannes Berg55682962007-09-20 13:09:35 -0400435static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
436{
437 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200438 int result = 0, rem_txq_params = 0;
439 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200440 u32 changed;
441 u8 retry_short = 0, retry_long = 0;
442 u32 frag_threshold = 0, rts_threshold = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400443
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100444 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400445
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100446 mutex_lock(&cfg80211_mutex);
447
448 rdev = __cfg80211_drv_from_info(info);
449 if (IS_ERR(rdev)) {
450 result = PTR_ERR(rdev);
451 goto unlock;
452 }
453
454 mutex_lock(&rdev->mtx);
455
456 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200457 result = cfg80211_dev_rename(
458 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100459
460 mutex_unlock(&cfg80211_mutex);
461
462 if (result)
463 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400464
Jouni Malinen31888482008-10-30 16:59:24 +0200465 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
466 struct ieee80211_txq_params txq_params;
467 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
468
469 if (!rdev->ops->set_txq_params) {
470 result = -EOPNOTSUPP;
471 goto bad_res;
472 }
473
474 nla_for_each_nested(nl_txq_params,
475 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
476 rem_txq_params) {
477 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
478 nla_data(nl_txq_params),
479 nla_len(nl_txq_params),
480 txq_params_policy);
481 result = parse_txq_params(tb, &txq_params);
482 if (result)
483 goto bad_res;
484
485 result = rdev->ops->set_txq_params(&rdev->wiphy,
486 &txq_params);
487 if (result)
488 goto bad_res;
489 }
490 }
491
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200492 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530493 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200494 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100495 struct ieee80211_sta_ht_cap *ht_cap;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400496 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200497
498 if (!rdev->ops->set_channel) {
499 result = -EOPNOTSUPP;
500 goto bad_res;
501 }
502
Johannes Berg306d6112008-12-08 12:39:04 +0100503 result = -EINVAL;
504
Sujith094d05d2008-12-12 11:57:43 +0530505 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
506 channel_type = nla_get_u32(info->attrs[
507 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
508 if (channel_type != NL80211_CHAN_NO_HT &&
509 channel_type != NL80211_CHAN_HT20 &&
510 channel_type != NL80211_CHAN_HT40PLUS &&
511 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200512 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200513 }
514
515 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
516 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100517
518 /* Primary channel not allowed */
519 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200520 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100521
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400522 if (channel_type == NL80211_CHAN_HT40MINUS &&
523 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS))
524 goto bad_res;
525 else if (channel_type == NL80211_CHAN_HT40PLUS &&
526 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS))
527 goto bad_res;
528
529 /*
530 * At this point we know if that if HT40 was requested
531 * we are allowed to use it and the extension channel
532 * exists.
533 */
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200534
Johannes Berg306d6112008-12-08 12:39:04 +0100535 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
536
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400537 /* no HT capabilities or intolerant */
538 if (channel_type != NL80211_CHAN_NO_HT) {
539 if (!ht_cap->ht_supported)
540 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100541 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
542 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200543 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200544 }
545
546 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530547 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200548 if (result)
549 goto bad_res;
550 }
551
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200552 changed = 0;
553
554 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
555 retry_short = nla_get_u8(
556 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
557 if (retry_short == 0) {
558 result = -EINVAL;
559 goto bad_res;
560 }
561 changed |= WIPHY_PARAM_RETRY_SHORT;
562 }
563
564 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
565 retry_long = nla_get_u8(
566 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
567 if (retry_long == 0) {
568 result = -EINVAL;
569 goto bad_res;
570 }
571 changed |= WIPHY_PARAM_RETRY_LONG;
572 }
573
574 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
575 frag_threshold = nla_get_u32(
576 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
577 if (frag_threshold < 256) {
578 result = -EINVAL;
579 goto bad_res;
580 }
581 if (frag_threshold != (u32) -1) {
582 /*
583 * Fragments (apart from the last one) are required to
584 * have even length. Make the fragmentation code
585 * simpler by stripping LSB should someone try to use
586 * odd threshold value.
587 */
588 frag_threshold &= ~0x1;
589 }
590 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
591 }
592
593 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
594 rts_threshold = nla_get_u32(
595 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
596 changed |= WIPHY_PARAM_RTS_THRESHOLD;
597 }
598
599 if (changed) {
600 u8 old_retry_short, old_retry_long;
601 u32 old_frag_threshold, old_rts_threshold;
602
603 if (!rdev->ops->set_wiphy_params) {
604 result = -EOPNOTSUPP;
605 goto bad_res;
606 }
607
608 old_retry_short = rdev->wiphy.retry_short;
609 old_retry_long = rdev->wiphy.retry_long;
610 old_frag_threshold = rdev->wiphy.frag_threshold;
611 old_rts_threshold = rdev->wiphy.rts_threshold;
612
613 if (changed & WIPHY_PARAM_RETRY_SHORT)
614 rdev->wiphy.retry_short = retry_short;
615 if (changed & WIPHY_PARAM_RETRY_LONG)
616 rdev->wiphy.retry_long = retry_long;
617 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
618 rdev->wiphy.frag_threshold = frag_threshold;
619 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
620 rdev->wiphy.rts_threshold = rts_threshold;
621
622 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
623 if (result) {
624 rdev->wiphy.retry_short = old_retry_short;
625 rdev->wiphy.retry_long = old_retry_long;
626 rdev->wiphy.frag_threshold = old_frag_threshold;
627 rdev->wiphy.rts_threshold = old_rts_threshold;
628 }
629 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200630
Johannes Berg306d6112008-12-08 12:39:04 +0100631 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100632 mutex_unlock(&rdev->mtx);
633 unlock:
634 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400635 return result;
636}
637
638
639static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200640 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400641 struct net_device *dev)
642{
643 void *hdr;
644
645 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
646 if (!hdr)
647 return -1;
648
649 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200650 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400651 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200652 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400653 return genlmsg_end(msg, hdr);
654
655 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700656 genlmsg_cancel(msg, hdr);
657 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400658}
659
660static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
661{
662 int wp_idx = 0;
663 int if_idx = 0;
664 int wp_start = cb->args[0];
665 int if_start = cb->args[1];
666 struct cfg80211_registered_device *dev;
667 struct wireless_dev *wdev;
668
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500669 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400670 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200671 if (wp_idx < wp_start) {
672 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400673 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200674 }
Johannes Berg55682962007-09-20 13:09:35 -0400675 if_idx = 0;
676
677 mutex_lock(&dev->devlist_mtx);
678 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200679 if (if_idx < if_start) {
680 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400681 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200682 }
Johannes Berg55682962007-09-20 13:09:35 -0400683 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
684 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergd7264052009-04-19 16:23:20 +0200685 dev, wdev->netdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200686 mutex_unlock(&dev->devlist_mtx);
687 goto out;
688 }
689 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400690 }
691 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200692
693 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400694 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200695 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500696 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400697
698 cb->args[0] = wp_idx;
699 cb->args[1] = if_idx;
700
701 return skb->len;
702}
703
704static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
705{
706 struct sk_buff *msg;
707 struct cfg80211_registered_device *dev;
708 struct net_device *netdev;
709 int err;
710
Johannes Bergbba95fe2008-07-29 13:22:51 +0200711 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400712 if (err)
713 return err;
714
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400716 if (!msg)
717 goto out_err;
718
Johannes Bergd7264052009-04-19 16:23:20 +0200719 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
720 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400721 goto out_free;
722
723 dev_put(netdev);
724 cfg80211_put_dev(dev);
725
726 return genlmsg_unicast(msg, info->snd_pid);
727
728 out_free:
729 nlmsg_free(msg);
730 out_err:
731 dev_put(netdev);
732 cfg80211_put_dev(dev);
733 return -ENOBUFS;
734}
735
Michael Wu66f7ac52008-01-31 19:48:22 +0100736static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
737 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
738 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
739 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
740 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
741 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
742};
743
744static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
745{
746 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
747 int flag;
748
749 *mntrflags = 0;
750
751 if (!nla)
752 return -EINVAL;
753
754 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
755 nla, mntr_flags_policy))
756 return -EINVAL;
757
758 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
759 if (flags[flag])
760 *mntrflags |= (1<<flag);
761
762 return 0;
763}
764
Johannes Berg55682962007-09-20 13:09:35 -0400765static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
766{
767 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100768 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400769 int err, ifindex;
Johannes Berg04a773a2009-04-19 21:24:32 +0200770 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -0400771 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200772 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100773 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400774
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100775 memset(&params, 0, sizeof(params));
776
Johannes Berg3b858752009-03-12 09:55:09 +0100777 rtnl_lock();
778
Johannes Bergbba95fe2008-07-29 13:22:51 +0200779 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400780 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100781 goto unlock_rtnl;
782
Johannes Berg55682962007-09-20 13:09:35 -0400783 ifindex = dev->ifindex;
Johannes Berg04a773a2009-04-19 21:24:32 +0200784 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400785 dev_put(dev);
786
Johannes Berg723b0382008-09-16 20:22:09 +0200787 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100788 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +0200789 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100790 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200791 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100792 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200793 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100794 }
Johannes Berg723b0382008-09-16 20:22:09 +0200795 }
796
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700797 if (!drv->ops->change_virtual_intf ||
Johannes Berg04a773a2009-04-19 21:24:32 +0200798 !(drv->wiphy.interface_modes & (1 << ntype))) {
Johannes Berg55682962007-09-20 13:09:35 -0400799 err = -EOPNOTSUPP;
800 goto unlock;
801 }
802
Johannes Berg92ffe052008-09-16 20:39:36 +0200803 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200804 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200805 err = -EINVAL;
806 goto unlock;
807 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100808 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
809 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100810 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100811 }
812
Johannes Berg92ffe052008-09-16 20:39:36 +0200813 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200814 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200815 err = -EINVAL;
816 goto unlock;
817 }
818 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
819 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100820 if (err)
821 goto unlock;
822
823 flags = &_flags;
824 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +0200825 }
Johannes Berg3b858752009-03-12 09:55:09 +0100826
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100827 if (change)
828 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
Johannes Berg04a773a2009-04-19 21:24:32 +0200829 ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100830 else
831 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +0200832
833 dev = __dev_get_by_index(&init_net, ifindex);
Johannes Berg04a773a2009-04-19 21:24:32 +0200834 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != ntype));
835
836 if (dev && !err && (ntype != otype)) {
837 if (otype == NL80211_IFTYPE_ADHOC)
Johannes Berg9d308422009-04-20 18:43:46 +0200838 cfg80211_clear_ibss(dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +0200839 }
Johannes Berg60719ff2008-09-16 14:55:09 +0200840
Johannes Berg55682962007-09-20 13:09:35 -0400841 unlock:
842 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100843 unlock_rtnl:
844 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400845 return err;
846}
847
848static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
849{
850 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100851 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400852 int err;
853 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100854 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400855
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100856 memset(&params, 0, sizeof(params));
857
Johannes Berg55682962007-09-20 13:09:35 -0400858 if (!info->attrs[NL80211_ATTR_IFNAME])
859 return -EINVAL;
860
861 if (info->attrs[NL80211_ATTR_IFTYPE]) {
862 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
863 if (type > NL80211_IFTYPE_MAX)
864 return -EINVAL;
865 }
866
Johannes Berg3b858752009-03-12 09:55:09 +0100867 rtnl_lock();
868
Johannes Berg55682962007-09-20 13:09:35 -0400869 drv = cfg80211_get_dev_from_info(info);
Johannes Berg3b858752009-03-12 09:55:09 +0100870 if (IS_ERR(drv)) {
871 err = PTR_ERR(drv);
872 goto unlock_rtnl;
873 }
Johannes Berg55682962007-09-20 13:09:35 -0400874
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700875 if (!drv->ops->add_virtual_intf ||
876 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400877 err = -EOPNOTSUPP;
878 goto unlock;
879 }
880
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100881 if (type == NL80211_IFTYPE_MESH_POINT &&
882 info->attrs[NL80211_ATTR_MESH_ID]) {
883 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
884 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
885 }
886
Michael Wu66f7ac52008-01-31 19:48:22 +0100887 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
888 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
889 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400890 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100891 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100892 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100893
Johannes Berg55682962007-09-20 13:09:35 -0400894 unlock:
895 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100896 unlock_rtnl:
897 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400898 return err;
899}
900
901static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
902{
903 struct cfg80211_registered_device *drv;
904 int ifindex, err;
905 struct net_device *dev;
906
Johannes Berg3b858752009-03-12 09:55:09 +0100907 rtnl_lock();
908
Johannes Bergbba95fe2008-07-29 13:22:51 +0200909 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400910 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100911 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -0400912 ifindex = dev->ifindex;
913 dev_put(dev);
914
915 if (!drv->ops->del_virtual_intf) {
916 err = -EOPNOTSUPP;
917 goto out;
918 }
919
Johannes Berg55682962007-09-20 13:09:35 -0400920 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -0400921
922 out:
923 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100924 unlock_rtnl:
925 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400926 return err;
927}
928
Johannes Berg41ade002007-12-19 02:03:29 +0100929struct get_key_cookie {
930 struct sk_buff *msg;
931 int error;
932};
933
934static void get_key_callback(void *c, struct key_params *params)
935{
936 struct get_key_cookie *cookie = c;
937
938 if (params->key)
939 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
940 params->key_len, params->key);
941
942 if (params->seq)
943 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
944 params->seq_len, params->seq);
945
946 if (params->cipher)
947 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
948 params->cipher);
949
950 return;
951 nla_put_failure:
952 cookie->error = 1;
953}
954
955static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
956{
957 struct cfg80211_registered_device *drv;
958 int err;
959 struct net_device *dev;
960 u8 key_idx = 0;
961 u8 *mac_addr = NULL;
962 struct get_key_cookie cookie = {
963 .error = 0,
964 };
965 void *hdr;
966 struct sk_buff *msg;
967
968 if (info->attrs[NL80211_ATTR_KEY_IDX])
969 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
970
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200971 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100972 return -EINVAL;
973
974 if (info->attrs[NL80211_ATTR_MAC])
975 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
976
Johannes Berg3b858752009-03-12 09:55:09 +0100977 rtnl_lock();
978
Johannes Bergbba95fe2008-07-29 13:22:51 +0200979 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100980 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100981 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100982
983 if (!drv->ops->get_key) {
984 err = -EOPNOTSUPP;
985 goto out;
986 }
987
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700988 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +0100989 if (!msg) {
990 err = -ENOMEM;
991 goto out;
992 }
993
994 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
995 NL80211_CMD_NEW_KEY);
996
997 if (IS_ERR(hdr)) {
998 err = PTR_ERR(hdr);
999 goto out;
1000 }
1001
1002 cookie.msg = msg;
1003
1004 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1005 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1006 if (mac_addr)
1007 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1008
Johannes Berg41ade002007-12-19 02:03:29 +01001009 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
1010 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001011
1012 if (err)
1013 goto out;
1014
1015 if (cookie.error)
1016 goto nla_put_failure;
1017
1018 genlmsg_end(msg, hdr);
1019 err = genlmsg_unicast(msg, info->snd_pid);
1020 goto out;
1021
1022 nla_put_failure:
1023 err = -ENOBUFS;
1024 nlmsg_free(msg);
1025 out:
1026 cfg80211_put_dev(drv);
1027 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001028 unlock_rtnl:
1029 rtnl_unlock();
1030
Johannes Berg41ade002007-12-19 02:03:29 +01001031 return err;
1032}
1033
1034static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1035{
1036 struct cfg80211_registered_device *drv;
1037 int err;
1038 struct net_device *dev;
1039 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001040 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1041 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001042
1043 if (!info->attrs[NL80211_ATTR_KEY_IDX])
1044 return -EINVAL;
1045
1046 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1047
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001048 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
1049 if (key_idx < 4 || key_idx > 5)
1050 return -EINVAL;
1051 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +01001052 return -EINVAL;
1053
1054 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001055 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
1056 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +01001057 return -EINVAL;
1058
Johannes Berg3b858752009-03-12 09:55:09 +01001059 rtnl_lock();
1060
Johannes Bergbba95fe2008-07-29 13:22:51 +02001061 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001062 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001063 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001064
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001065 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
1066 func = drv->ops->set_default_key;
1067 else
1068 func = drv->ops->set_default_mgmt_key;
1069
1070 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001071 err = -EOPNOTSUPP;
1072 goto out;
1073 }
1074
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001075 err = func(&drv->wiphy, dev, key_idx);
Johannes Berg08645122009-05-11 13:54:58 +02001076#ifdef CONFIG_WIRELESS_EXT
1077 if (!err) {
1078 if (func == drv->ops->set_default_key)
1079 dev->ieee80211_ptr->wext.default_key = key_idx;
1080 else
1081 dev->ieee80211_ptr->wext.default_mgmt_key = key_idx;
1082 }
1083#endif
Johannes Berg41ade002007-12-19 02:03:29 +01001084
1085 out:
1086 cfg80211_put_dev(drv);
1087 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001088
1089 unlock_rtnl:
1090 rtnl_unlock();
1091
Johannes Berg41ade002007-12-19 02:03:29 +01001092 return err;
1093}
1094
1095static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1096{
1097 struct cfg80211_registered_device *drv;
Johannes Berg25e47c182009-04-02 20:14:06 +02001098 int err, i;
Johannes Berg41ade002007-12-19 02:03:29 +01001099 struct net_device *dev;
1100 struct key_params params;
1101 u8 key_idx = 0;
1102 u8 *mac_addr = NULL;
1103
1104 memset(&params, 0, sizeof(params));
1105
1106 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
1107 return -EINVAL;
1108
1109 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1110 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1111 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1112 }
1113
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +03001114 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1115 params.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1116 params.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1117 }
1118
Johannes Berg41ade002007-12-19 02:03:29 +01001119 if (info->attrs[NL80211_ATTR_KEY_IDX])
1120 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1121
1122 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1123
1124 if (info->attrs[NL80211_ATTR_MAC])
1125 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1126
Johannes Berg08645122009-05-11 13:54:58 +02001127 if (cfg80211_validate_key_settings(&params, key_idx, mac_addr))
Johannes Berg41ade002007-12-19 02:03:29 +01001128 return -EINVAL;
1129
Johannes Berg3b858752009-03-12 09:55:09 +01001130 rtnl_lock();
1131
Johannes Bergbba95fe2008-07-29 13:22:51 +02001132 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001133 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001134 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001135
Johannes Berg25e47c182009-04-02 20:14:06 +02001136 for (i = 0; i < drv->wiphy.n_cipher_suites; i++)
1137 if (params.cipher == drv->wiphy.cipher_suites[i])
1138 break;
1139 if (i == drv->wiphy.n_cipher_suites) {
1140 err = -EINVAL;
1141 goto out;
1142 }
1143
Johannes Berg41ade002007-12-19 02:03:29 +01001144 if (!drv->ops->add_key) {
1145 err = -EOPNOTSUPP;
1146 goto out;
1147 }
1148
Johannes Berg41ade002007-12-19 02:03:29 +01001149 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
Johannes Berg41ade002007-12-19 02:03:29 +01001150
1151 out:
1152 cfg80211_put_dev(drv);
1153 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001154 unlock_rtnl:
1155 rtnl_unlock();
1156
Johannes Berg41ade002007-12-19 02:03:29 +01001157 return err;
1158}
1159
1160static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1161{
1162 struct cfg80211_registered_device *drv;
1163 int err;
1164 struct net_device *dev;
1165 u8 key_idx = 0;
1166 u8 *mac_addr = NULL;
1167
1168 if (info->attrs[NL80211_ATTR_KEY_IDX])
1169 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1170
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001171 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001172 return -EINVAL;
1173
1174 if (info->attrs[NL80211_ATTR_MAC])
1175 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1176
Johannes Berg3b858752009-03-12 09:55:09 +01001177 rtnl_lock();
1178
Johannes Bergbba95fe2008-07-29 13:22:51 +02001179 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001180 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001181 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001182
1183 if (!drv->ops->del_key) {
1184 err = -EOPNOTSUPP;
1185 goto out;
1186 }
1187
Johannes Berg41ade002007-12-19 02:03:29 +01001188 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001189
Johannes Berg08645122009-05-11 13:54:58 +02001190#ifdef CONFIG_WIRELESS_EXT
1191 if (!err) {
1192 if (key_idx == dev->ieee80211_ptr->wext.default_key)
1193 dev->ieee80211_ptr->wext.default_key = -1;
1194 else if (key_idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1195 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1196 }
1197#endif
1198
Johannes Berg41ade002007-12-19 02:03:29 +01001199 out:
1200 cfg80211_put_dev(drv);
1201 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001202
1203 unlock_rtnl:
1204 rtnl_unlock();
1205
Johannes Berg41ade002007-12-19 02:03:29 +01001206 return err;
1207}
1208
Johannes Berged1b6cc2007-12-19 02:03:32 +01001209static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1210{
1211 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1212 struct beacon_parameters *info);
1213 struct cfg80211_registered_device *drv;
1214 int err;
1215 struct net_device *dev;
1216 struct beacon_parameters params;
1217 int haveinfo = 0;
1218
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001219 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1220 return -EINVAL;
1221
Johannes Berg3b858752009-03-12 09:55:09 +01001222 rtnl_lock();
1223
Johannes Bergbba95fe2008-07-29 13:22:51 +02001224 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001225 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001226 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001227
Jouni Malineneec60b02009-03-20 21:21:19 +02001228 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1229 err = -EOPNOTSUPP;
1230 goto out;
1231 }
1232
Johannes Berged1b6cc2007-12-19 02:03:32 +01001233 switch (info->genlhdr->cmd) {
1234 case NL80211_CMD_NEW_BEACON:
1235 /* these are required for NEW_BEACON */
1236 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1237 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1238 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1239 err = -EINVAL;
1240 goto out;
1241 }
1242
1243 call = drv->ops->add_beacon;
1244 break;
1245 case NL80211_CMD_SET_BEACON:
1246 call = drv->ops->set_beacon;
1247 break;
1248 default:
1249 WARN_ON(1);
1250 err = -EOPNOTSUPP;
1251 goto out;
1252 }
1253
1254 if (!call) {
1255 err = -EOPNOTSUPP;
1256 goto out;
1257 }
1258
1259 memset(&params, 0, sizeof(params));
1260
1261 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1262 params.interval =
1263 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1264 haveinfo = 1;
1265 }
1266
1267 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1268 params.dtim_period =
1269 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1270 haveinfo = 1;
1271 }
1272
1273 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1274 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1275 params.head_len =
1276 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1277 haveinfo = 1;
1278 }
1279
1280 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1281 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1282 params.tail_len =
1283 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1284 haveinfo = 1;
1285 }
1286
1287 if (!haveinfo) {
1288 err = -EINVAL;
1289 goto out;
1290 }
1291
Johannes Berged1b6cc2007-12-19 02:03:32 +01001292 err = call(&drv->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001293
1294 out:
1295 cfg80211_put_dev(drv);
1296 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001297 unlock_rtnl:
1298 rtnl_unlock();
1299
Johannes Berged1b6cc2007-12-19 02:03:32 +01001300 return err;
1301}
1302
1303static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1304{
1305 struct cfg80211_registered_device *drv;
1306 int err;
1307 struct net_device *dev;
1308
Johannes Berg3b858752009-03-12 09:55:09 +01001309 rtnl_lock();
1310
Johannes Bergbba95fe2008-07-29 13:22:51 +02001311 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001312 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001313 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001314
1315 if (!drv->ops->del_beacon) {
1316 err = -EOPNOTSUPP;
1317 goto out;
1318 }
1319
Jouni Malineneec60b02009-03-20 21:21:19 +02001320 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1321 err = -EOPNOTSUPP;
1322 goto out;
1323 }
Johannes Berged1b6cc2007-12-19 02:03:32 +01001324 err = drv->ops->del_beacon(&drv->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001325
1326 out:
1327 cfg80211_put_dev(drv);
1328 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001329 unlock_rtnl:
1330 rtnl_unlock();
1331
Johannes Berged1b6cc2007-12-19 02:03:32 +01001332 return err;
1333}
1334
Johannes Berg5727ef12007-12-19 02:03:34 +01001335static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1336 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1337 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1338 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001339 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001340};
1341
Johannes Bergeccb8e82009-05-11 21:57:56 +03001342static int parse_station_flags(struct genl_info *info,
1343 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001344{
1345 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001346 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001347 int flag;
1348
Johannes Bergeccb8e82009-05-11 21:57:56 +03001349 /*
1350 * Try parsing the new attribute first so userspace
1351 * can specify both for older kernels.
1352 */
1353 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1354 if (nla) {
1355 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001356
Johannes Bergeccb8e82009-05-11 21:57:56 +03001357 sta_flags = nla_data(nla);
1358 params->sta_flags_mask = sta_flags->mask;
1359 params->sta_flags_set = sta_flags->set;
1360 if ((params->sta_flags_mask |
1361 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1362 return -EINVAL;
1363 return 0;
1364 }
1365
1366 /* if present, parse the old attribute */
1367
1368 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001369 if (!nla)
1370 return 0;
1371
1372 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1373 nla, sta_flags_policy))
1374 return -EINVAL;
1375
Johannes Bergeccb8e82009-05-11 21:57:56 +03001376 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1377 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001378
1379 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1380 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001381 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001382
1383 return 0;
1384}
1385
Henning Rogge420e7fa2008-12-11 22:04:19 +01001386static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1387{
1388 int modulation, streams, bitrate;
1389
1390 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1391 return rate->legacy;
1392
1393 /* the formula below does only work for MCS values smaller than 32 */
1394 if (rate->mcs >= 32)
1395 return 0;
1396
1397 modulation = rate->mcs & 7;
1398 streams = (rate->mcs >> 3) + 1;
1399
1400 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1401 13500000 : 6500000;
1402
1403 if (modulation < 4)
1404 bitrate *= (modulation + 1);
1405 else if (modulation == 4)
1406 bitrate *= (modulation + 2);
1407 else
1408 bitrate *= (modulation + 3);
1409
1410 bitrate *= streams;
1411
1412 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1413 bitrate = (bitrate / 9) * 10;
1414
1415 /* do NOT round down here */
1416 return (bitrate + 50000) / 100000;
1417}
1418
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001419static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1420 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001421 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001422{
1423 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001424 struct nlattr *sinfoattr, *txrate;
1425 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001426
1427 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1428 if (!hdr)
1429 return -1;
1430
1431 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1432 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1433
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001434 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1435 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001436 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001437 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1438 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1439 sinfo->inactive_time);
1440 if (sinfo->filled & STATION_INFO_RX_BYTES)
1441 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1442 sinfo->rx_bytes);
1443 if (sinfo->filled & STATION_INFO_TX_BYTES)
1444 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1445 sinfo->tx_bytes);
1446 if (sinfo->filled & STATION_INFO_LLID)
1447 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1448 sinfo->llid);
1449 if (sinfo->filled & STATION_INFO_PLID)
1450 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1451 sinfo->plid);
1452 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1453 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1454 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001455 if (sinfo->filled & STATION_INFO_SIGNAL)
1456 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1457 sinfo->signal);
1458 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1459 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1460 if (!txrate)
1461 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001462
Henning Rogge420e7fa2008-12-11 22:04:19 +01001463 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1464 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1465 if (bitrate > 0)
1466 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1467
1468 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1469 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1470 sinfo->txrate.mcs);
1471 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1472 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1473 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1474 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1475
1476 nla_nest_end(msg, txrate);
1477 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001478 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1479 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1480 sinfo->rx_packets);
1481 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1482 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1483 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001484 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001485
1486 return genlmsg_end(msg, hdr);
1487
1488 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001489 genlmsg_cancel(msg, hdr);
1490 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001491}
1492
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001493static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001494 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001495{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001496 struct station_info sinfo;
1497 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001498 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001499 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001500 int ifidx = cb->args[0];
1501 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001502 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001503
Johannes Bergbba95fe2008-07-29 13:22:51 +02001504 if (!ifidx) {
1505 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1506 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1507 nl80211_policy);
1508 if (err)
1509 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001510
Johannes Bergbba95fe2008-07-29 13:22:51 +02001511 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1512 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001513
Johannes Bergbba95fe2008-07-29 13:22:51 +02001514 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1515 if (!ifidx)
1516 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001517 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001518
Johannes Berg3b858752009-03-12 09:55:09 +01001519 rtnl_lock();
1520
1521 netdev = __dev_get_by_index(&init_net, ifidx);
1522 if (!netdev) {
1523 err = -ENODEV;
1524 goto out_rtnl;
1525 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001526
Johannes Bergbba95fe2008-07-29 13:22:51 +02001527 dev = cfg80211_get_dev_from_ifindex(ifidx);
1528 if (IS_ERR(dev)) {
1529 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001530 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001531 }
1532
1533 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001534 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001535 goto out_err;
1536 }
1537
Johannes Bergbba95fe2008-07-29 13:22:51 +02001538 while (1) {
1539 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1540 mac_addr, &sinfo);
1541 if (err == -ENOENT)
1542 break;
1543 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001544 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001545
1546 if (nl80211_send_station(skb,
1547 NETLINK_CB(cb->skb).pid,
1548 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1549 netdev, mac_addr,
1550 &sinfo) < 0)
1551 goto out;
1552
1553 sta_idx++;
1554 }
1555
1556
1557 out:
1558 cb->args[1] = sta_idx;
1559 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001560 out_err:
1561 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001562 out_rtnl:
1563 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001564
1565 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001566}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001567
Johannes Berg5727ef12007-12-19 02:03:34 +01001568static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1569{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001570 struct cfg80211_registered_device *drv;
1571 int err;
1572 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001573 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001574 struct sk_buff *msg;
1575 u8 *mac_addr = NULL;
1576
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001577 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001578
1579 if (!info->attrs[NL80211_ATTR_MAC])
1580 return -EINVAL;
1581
1582 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1583
Johannes Berg3b858752009-03-12 09:55:09 +01001584 rtnl_lock();
1585
Johannes Bergbba95fe2008-07-29 13:22:51 +02001586 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001587 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001588 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001589
1590 if (!drv->ops->get_station) {
1591 err = -EOPNOTSUPP;
1592 goto out;
1593 }
1594
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001595 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001596 if (err)
1597 goto out;
1598
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001599 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001600 if (!msg)
1601 goto out;
1602
1603 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001604 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001605 goto out_free;
1606
1607 err = genlmsg_unicast(msg, info->snd_pid);
1608 goto out;
1609
1610 out_free:
1611 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001612 out:
1613 cfg80211_put_dev(drv);
1614 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001615 out_rtnl:
1616 rtnl_unlock();
1617
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001618 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001619}
1620
1621/*
1622 * Get vlan interface making sure it is on the right wiphy.
1623 */
1624static int get_vlan(struct nlattr *vlanattr,
1625 struct cfg80211_registered_device *rdev,
1626 struct net_device **vlan)
1627{
1628 *vlan = NULL;
1629
1630 if (vlanattr) {
1631 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1632 if (!*vlan)
1633 return -ENODEV;
1634 if (!(*vlan)->ieee80211_ptr)
1635 return -EINVAL;
1636 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1637 return -EINVAL;
1638 }
1639 return 0;
1640}
1641
1642static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1643{
1644 struct cfg80211_registered_device *drv;
1645 int err;
1646 struct net_device *dev;
1647 struct station_parameters params;
1648 u8 *mac_addr = NULL;
1649
1650 memset(&params, 0, sizeof(params));
1651
1652 params.listen_interval = -1;
1653
1654 if (info->attrs[NL80211_ATTR_STA_AID])
1655 return -EINVAL;
1656
1657 if (!info->attrs[NL80211_ATTR_MAC])
1658 return -EINVAL;
1659
1660 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1661
1662 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1663 params.supported_rates =
1664 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1665 params.supported_rates_len =
1666 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1667 }
1668
1669 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1670 params.listen_interval =
1671 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1672
Jouni Malinen36aedc92008-08-25 11:58:58 +03001673 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1674 params.ht_capa =
1675 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1676
Johannes Bergeccb8e82009-05-11 21:57:56 +03001677 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001678 return -EINVAL;
1679
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001680 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1681 params.plink_action =
1682 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1683
Johannes Berg3b858752009-03-12 09:55:09 +01001684 rtnl_lock();
1685
Johannes Bergbba95fe2008-07-29 13:22:51 +02001686 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001687 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001688 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001689
Johannes Berg034d6552009-05-27 10:35:29 +02001690 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1691 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
1692 err = -EINVAL;
1693 goto out;
1694 }
1695
Johannes Berg5727ef12007-12-19 02:03:34 +01001696 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1697 if (err)
1698 goto out;
1699
1700 if (!drv->ops->change_station) {
1701 err = -EOPNOTSUPP;
1702 goto out;
1703 }
1704
Johannes Berg5727ef12007-12-19 02:03:34 +01001705 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001706
1707 out:
1708 if (params.vlan)
1709 dev_put(params.vlan);
1710 cfg80211_put_dev(drv);
1711 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001712 out_rtnl:
1713 rtnl_unlock();
1714
Johannes Berg5727ef12007-12-19 02:03:34 +01001715 return err;
1716}
1717
1718static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1719{
1720 struct cfg80211_registered_device *drv;
1721 int err;
1722 struct net_device *dev;
1723 struct station_parameters params;
1724 u8 *mac_addr = NULL;
1725
1726 memset(&params, 0, sizeof(params));
1727
1728 if (!info->attrs[NL80211_ATTR_MAC])
1729 return -EINVAL;
1730
1731 if (!info->attrs[NL80211_ATTR_STA_AID])
1732 return -EINVAL;
1733
1734 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1735 return -EINVAL;
1736
1737 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1738 return -EINVAL;
1739
1740 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1741 params.supported_rates =
1742 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1743 params.supported_rates_len =
1744 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1745 params.listen_interval =
1746 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001747
Johannes Berg16f2e852008-04-07 14:35:46 +02001748 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001749 if (!params.aid || params.aid > IEEE80211_MAX_AID)
1750 return -EINVAL;
1751
Jouni Malinen36aedc92008-08-25 11:58:58 +03001752 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1753 params.ht_capa =
1754 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001755
Johannes Bergeccb8e82009-05-11 21:57:56 +03001756 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001757 return -EINVAL;
1758
Johannes Berg3b858752009-03-12 09:55:09 +01001759 rtnl_lock();
1760
Johannes Bergbba95fe2008-07-29 13:22:51 +02001761 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001762 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001763 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001764
Johannes Berge80cf852009-05-11 14:43:13 +02001765 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1766 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
1767 err = -EINVAL;
1768 goto out;
1769 }
1770
Johannes Berg5727ef12007-12-19 02:03:34 +01001771 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1772 if (err)
1773 goto out;
1774
1775 if (!drv->ops->add_station) {
1776 err = -EOPNOTSUPP;
1777 goto out;
1778 }
1779
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001780 if (!netif_running(dev)) {
1781 err = -ENETDOWN;
1782 goto out;
1783 }
1784
Johannes Berg5727ef12007-12-19 02:03:34 +01001785 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001786
1787 out:
1788 if (params.vlan)
1789 dev_put(params.vlan);
1790 cfg80211_put_dev(drv);
1791 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001792 out_rtnl:
1793 rtnl_unlock();
1794
Johannes Berg5727ef12007-12-19 02:03:34 +01001795 return err;
1796}
1797
1798static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1799{
1800 struct cfg80211_registered_device *drv;
1801 int err;
1802 struct net_device *dev;
1803 u8 *mac_addr = NULL;
1804
1805 if (info->attrs[NL80211_ATTR_MAC])
1806 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1807
Johannes Berg3b858752009-03-12 09:55:09 +01001808 rtnl_lock();
1809
Johannes Bergbba95fe2008-07-29 13:22:51 +02001810 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001811 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001812 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001813
Johannes Berge80cf852009-05-11 14:43:13 +02001814 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1815 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
1816 err = -EINVAL;
1817 goto out;
1818 }
1819
Johannes Berg5727ef12007-12-19 02:03:34 +01001820 if (!drv->ops->del_station) {
1821 err = -EOPNOTSUPP;
1822 goto out;
1823 }
1824
Johannes Berg5727ef12007-12-19 02:03:34 +01001825 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01001826
1827 out:
1828 cfg80211_put_dev(drv);
1829 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001830 out_rtnl:
1831 rtnl_unlock();
1832
Johannes Berg5727ef12007-12-19 02:03:34 +01001833 return err;
1834}
1835
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001836static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1837 int flags, struct net_device *dev,
1838 u8 *dst, u8 *next_hop,
1839 struct mpath_info *pinfo)
1840{
1841 void *hdr;
1842 struct nlattr *pinfoattr;
1843
1844 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1845 if (!hdr)
1846 return -1;
1847
1848 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1849 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1850 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1851
1852 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1853 if (!pinfoattr)
1854 goto nla_put_failure;
1855 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1856 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1857 pinfo->frame_qlen);
1858 if (pinfo->filled & MPATH_INFO_DSN)
1859 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1860 pinfo->dsn);
1861 if (pinfo->filled & MPATH_INFO_METRIC)
1862 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1863 pinfo->metric);
1864 if (pinfo->filled & MPATH_INFO_EXPTIME)
1865 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1866 pinfo->exptime);
1867 if (pinfo->filled & MPATH_INFO_FLAGS)
1868 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1869 pinfo->flags);
1870 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1871 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1872 pinfo->discovery_timeout);
1873 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1874 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1875 pinfo->discovery_retries);
1876
1877 nla_nest_end(msg, pinfoattr);
1878
1879 return genlmsg_end(msg, hdr);
1880
1881 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001882 genlmsg_cancel(msg, hdr);
1883 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001884}
1885
1886static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001887 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001888{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001889 struct mpath_info pinfo;
1890 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001891 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001892 u8 dst[ETH_ALEN];
1893 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001894 int ifidx = cb->args[0];
1895 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001896 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001897
Johannes Bergbba95fe2008-07-29 13:22:51 +02001898 if (!ifidx) {
1899 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1900 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1901 nl80211_policy);
1902 if (err)
1903 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001904
Johannes Bergbba95fe2008-07-29 13:22:51 +02001905 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1906 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001907
Johannes Bergbba95fe2008-07-29 13:22:51 +02001908 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1909 if (!ifidx)
1910 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001911 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001912
Johannes Berg3b858752009-03-12 09:55:09 +01001913 rtnl_lock();
1914
1915 netdev = __dev_get_by_index(&init_net, ifidx);
1916 if (!netdev) {
1917 err = -ENODEV;
1918 goto out_rtnl;
1919 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001920
Johannes Bergbba95fe2008-07-29 13:22:51 +02001921 dev = cfg80211_get_dev_from_ifindex(ifidx);
1922 if (IS_ERR(dev)) {
1923 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001924 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001925 }
1926
1927 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001928 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001929 goto out_err;
1930 }
1931
Jouni Malineneec60b02009-03-20 21:21:19 +02001932 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1933 err = -EOPNOTSUPP;
1934 goto out;
1935 }
1936
Johannes Bergbba95fe2008-07-29 13:22:51 +02001937 while (1) {
1938 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1939 dst, next_hop, &pinfo);
1940 if (err == -ENOENT)
1941 break;
1942 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001943 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001944
1945 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1946 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1947 netdev, dst, next_hop,
1948 &pinfo) < 0)
1949 goto out;
1950
1951 path_idx++;
1952 }
1953
1954
1955 out:
1956 cb->args[1] = path_idx;
1957 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001958 out_err:
1959 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001960 out_rtnl:
1961 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001962
1963 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001964}
1965
1966static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1967{
1968 struct cfg80211_registered_device *drv;
1969 int err;
1970 struct net_device *dev;
1971 struct mpath_info pinfo;
1972 struct sk_buff *msg;
1973 u8 *dst = NULL;
1974 u8 next_hop[ETH_ALEN];
1975
1976 memset(&pinfo, 0, sizeof(pinfo));
1977
1978 if (!info->attrs[NL80211_ATTR_MAC])
1979 return -EINVAL;
1980
1981 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1982
Johannes Berg3b858752009-03-12 09:55:09 +01001983 rtnl_lock();
1984
Johannes Bergbba95fe2008-07-29 13:22:51 +02001985 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001986 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001987 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001988
1989 if (!drv->ops->get_mpath) {
1990 err = -EOPNOTSUPP;
1991 goto out;
1992 }
1993
Jouni Malineneec60b02009-03-20 21:21:19 +02001994 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1995 err = -EOPNOTSUPP;
1996 goto out;
1997 }
1998
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001999 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002000 if (err)
2001 goto out;
2002
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002004 if (!msg)
2005 goto out;
2006
2007 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2008 dev, dst, next_hop, &pinfo) < 0)
2009 goto out_free;
2010
2011 err = genlmsg_unicast(msg, info->snd_pid);
2012 goto out;
2013
2014 out_free:
2015 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002016 out:
2017 cfg80211_put_dev(drv);
2018 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002019 out_rtnl:
2020 rtnl_unlock();
2021
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002022 return err;
2023}
2024
2025static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2026{
2027 struct cfg80211_registered_device *drv;
2028 int err;
2029 struct net_device *dev;
2030 u8 *dst = NULL;
2031 u8 *next_hop = NULL;
2032
2033 if (!info->attrs[NL80211_ATTR_MAC])
2034 return -EINVAL;
2035
2036 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2037 return -EINVAL;
2038
2039 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2040 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2041
Johannes Berg3b858752009-03-12 09:55:09 +01002042 rtnl_lock();
2043
Johannes Bergbba95fe2008-07-29 13:22:51 +02002044 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002045 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002046 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002047
2048 if (!drv->ops->change_mpath) {
2049 err = -EOPNOTSUPP;
2050 goto out;
2051 }
2052
Jouni Malineneec60b02009-03-20 21:21:19 +02002053 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2054 err = -EOPNOTSUPP;
2055 goto out;
2056 }
2057
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002058 if (!netif_running(dev)) {
2059 err = -ENETDOWN;
2060 goto out;
2061 }
2062
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002063 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002064
2065 out:
2066 cfg80211_put_dev(drv);
2067 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002068 out_rtnl:
2069 rtnl_unlock();
2070
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002071 return err;
2072}
2073static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2074{
2075 struct cfg80211_registered_device *drv;
2076 int err;
2077 struct net_device *dev;
2078 u8 *dst = NULL;
2079 u8 *next_hop = NULL;
2080
2081 if (!info->attrs[NL80211_ATTR_MAC])
2082 return -EINVAL;
2083
2084 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2085 return -EINVAL;
2086
2087 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2088 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2089
Johannes Berg3b858752009-03-12 09:55:09 +01002090 rtnl_lock();
2091
Johannes Bergbba95fe2008-07-29 13:22:51 +02002092 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002093 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002094 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002095
2096 if (!drv->ops->add_mpath) {
2097 err = -EOPNOTSUPP;
2098 goto out;
2099 }
2100
Jouni Malineneec60b02009-03-20 21:21:19 +02002101 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2102 err = -EOPNOTSUPP;
2103 goto out;
2104 }
2105
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002106 if (!netif_running(dev)) {
2107 err = -ENETDOWN;
2108 goto out;
2109 }
2110
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002111 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002112
2113 out:
2114 cfg80211_put_dev(drv);
2115 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002116 out_rtnl:
2117 rtnl_unlock();
2118
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002119 return err;
2120}
2121
2122static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2123{
2124 struct cfg80211_registered_device *drv;
2125 int err;
2126 struct net_device *dev;
2127 u8 *dst = NULL;
2128
2129 if (info->attrs[NL80211_ATTR_MAC])
2130 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2131
Johannes Berg3b858752009-03-12 09:55:09 +01002132 rtnl_lock();
2133
Johannes Bergbba95fe2008-07-29 13:22:51 +02002134 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002135 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002136 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002137
2138 if (!drv->ops->del_mpath) {
2139 err = -EOPNOTSUPP;
2140 goto out;
2141 }
2142
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002143 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002144
2145 out:
2146 cfg80211_put_dev(drv);
2147 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002148 out_rtnl:
2149 rtnl_unlock();
2150
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002151 return err;
2152}
2153
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002154static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2155{
2156 struct cfg80211_registered_device *drv;
2157 int err;
2158 struct net_device *dev;
2159 struct bss_parameters params;
2160
2161 memset(&params, 0, sizeof(params));
2162 /* default to not changing parameters */
2163 params.use_cts_prot = -1;
2164 params.use_short_preamble = -1;
2165 params.use_short_slot_time = -1;
2166
2167 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2168 params.use_cts_prot =
2169 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2170 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2171 params.use_short_preamble =
2172 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2173 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2174 params.use_short_slot_time =
2175 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002176 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2177 params.basic_rates =
2178 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2179 params.basic_rates_len =
2180 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2181 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002182
Johannes Berg3b858752009-03-12 09:55:09 +01002183 rtnl_lock();
2184
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002185 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2186 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002187 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002188
2189 if (!drv->ops->change_bss) {
2190 err = -EOPNOTSUPP;
2191 goto out;
2192 }
2193
Jouni Malineneec60b02009-03-20 21:21:19 +02002194 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2195 err = -EOPNOTSUPP;
2196 goto out;
2197 }
2198
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002199 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002200
2201 out:
2202 cfg80211_put_dev(drv);
2203 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002204 out_rtnl:
2205 rtnl_unlock();
2206
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002207 return err;
2208}
2209
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002210static const struct nla_policy
2211 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2212 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2213 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2214 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2215 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2216 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2217 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2218};
2219
2220static int parse_reg_rule(struct nlattr *tb[],
2221 struct ieee80211_reg_rule *reg_rule)
2222{
2223 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2224 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2225
2226 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2227 return -EINVAL;
2228 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2229 return -EINVAL;
2230 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2231 return -EINVAL;
2232 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2233 return -EINVAL;
2234 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2235 return -EINVAL;
2236
2237 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2238
2239 freq_range->start_freq_khz =
2240 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2241 freq_range->end_freq_khz =
2242 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2243 freq_range->max_bandwidth_khz =
2244 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2245
2246 power_rule->max_eirp =
2247 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2248
2249 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2250 power_rule->max_antenna_gain =
2251 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2252
2253 return 0;
2254}
2255
2256static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2257{
2258 int r;
2259 char *data = NULL;
2260
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002261 /*
2262 * You should only get this when cfg80211 hasn't yet initialized
2263 * completely when built-in to the kernel right between the time
2264 * window between nl80211_init() and regulatory_init(), if that is
2265 * even possible.
2266 */
2267 mutex_lock(&cfg80211_mutex);
2268 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002269 mutex_unlock(&cfg80211_mutex);
2270 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002271 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002272 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002273
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002274 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2275 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002276
2277 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2278
2279#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2280 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002281 if (is_world_regdom(data))
2282 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002283#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002284
2285 r = regulatory_hint_user(data);
2286
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002287 return r;
2288}
2289
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002290static int nl80211_get_mesh_params(struct sk_buff *skb,
2291 struct genl_info *info)
2292{
2293 struct cfg80211_registered_device *drv;
2294 struct mesh_config cur_params;
2295 int err;
2296 struct net_device *dev;
2297 void *hdr;
2298 struct nlattr *pinfoattr;
2299 struct sk_buff *msg;
2300
Johannes Berg3b858752009-03-12 09:55:09 +01002301 rtnl_lock();
2302
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002303 /* Look up our device */
2304 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2305 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002306 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002307
Jouni Malinenf3f92582009-03-20 17:57:36 +02002308 if (!drv->ops->get_mesh_params) {
2309 err = -EOPNOTSUPP;
2310 goto out;
2311 }
2312
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002313 /* Get the mesh params */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002314 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002315 if (err)
2316 goto out;
2317
2318 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002319 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002320 if (!msg) {
2321 err = -ENOBUFS;
2322 goto out;
2323 }
2324 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2325 NL80211_CMD_GET_MESH_PARAMS);
2326 if (!hdr)
2327 goto nla_put_failure;
2328 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2329 if (!pinfoattr)
2330 goto nla_put_failure;
2331 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2332 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2333 cur_params.dot11MeshRetryTimeout);
2334 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2335 cur_params.dot11MeshConfirmTimeout);
2336 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2337 cur_params.dot11MeshHoldingTimeout);
2338 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2339 cur_params.dot11MeshMaxPeerLinks);
2340 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2341 cur_params.dot11MeshMaxRetries);
2342 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2343 cur_params.dot11MeshTTL);
2344 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2345 cur_params.auto_open_plinks);
2346 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2347 cur_params.dot11MeshHWMPmaxPREQretries);
2348 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2349 cur_params.path_refresh_time);
2350 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2351 cur_params.min_discovery_timeout);
2352 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2353 cur_params.dot11MeshHWMPactivePathTimeout);
2354 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2355 cur_params.dot11MeshHWMPpreqMinInterval);
2356 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2357 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2358 nla_nest_end(msg, pinfoattr);
2359 genlmsg_end(msg, hdr);
2360 err = genlmsg_unicast(msg, info->snd_pid);
2361 goto out;
2362
Johannes Berg3b858752009-03-12 09:55:09 +01002363 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002364 genlmsg_cancel(msg, hdr);
2365 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002366 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002367 /* Cleanup */
2368 cfg80211_put_dev(drv);
2369 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002370 out_rtnl:
2371 rtnl_unlock();
2372
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002373 return err;
2374}
2375
2376#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2377do {\
2378 if (table[attr_num]) {\
2379 cfg.param = nla_fn(table[attr_num]); \
2380 mask |= (1 << (attr_num - 1)); \
2381 } \
2382} while (0);\
2383
2384static struct nla_policy
2385nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2386 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2387 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2388 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2389 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2390 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2391 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2392 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2393
2394 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2395 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2396 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2397 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2398 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2399 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2400};
2401
2402static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2403{
2404 int err;
2405 u32 mask;
2406 struct cfg80211_registered_device *drv;
2407 struct net_device *dev;
2408 struct mesh_config cfg;
2409 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2410 struct nlattr *parent_attr;
2411
2412 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2413 if (!parent_attr)
2414 return -EINVAL;
2415 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2416 parent_attr, nl80211_meshconf_params_policy))
2417 return -EINVAL;
2418
Johannes Berg3b858752009-03-12 09:55:09 +01002419 rtnl_lock();
2420
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002421 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2422 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002423 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002424
Jouni Malinenf3f92582009-03-20 17:57:36 +02002425 if (!drv->ops->set_mesh_params) {
2426 err = -EOPNOTSUPP;
2427 goto out;
2428 }
2429
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002430 /* This makes sure that there aren't more than 32 mesh config
2431 * parameters (otherwise our bitfield scheme would not work.) */
2432 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2433
2434 /* Fill in the params struct */
2435 mask = 0;
2436 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2437 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2438 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2439 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2440 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2441 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2442 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2443 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2444 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2445 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2446 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2447 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2448 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2449 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2450 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2451 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2452 nla_get_u8);
2453 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2454 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2455 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2456 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2457 nla_get_u16);
2458 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2459 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2460 nla_get_u32);
2461 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2462 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2463 nla_get_u16);
2464 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2465 dot11MeshHWMPnetDiameterTraversalTime,
2466 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2467 nla_get_u16);
2468
2469 /* Apply changes */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002470 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002471
Jouni Malinenf3f92582009-03-20 17:57:36 +02002472 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002473 /* cleanup */
2474 cfg80211_put_dev(drv);
2475 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002476 out_rtnl:
2477 rtnl_unlock();
2478
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002479 return err;
2480}
2481
2482#undef FILL_IN_MESH_PARAM_IF_SET
2483
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002484static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2485{
2486 struct sk_buff *msg;
2487 void *hdr = NULL;
2488 struct nlattr *nl_reg_rules;
2489 unsigned int i;
2490 int err = -EINVAL;
2491
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002492 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002493
2494 if (!cfg80211_regdomain)
2495 goto out;
2496
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002497 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002498 if (!msg) {
2499 err = -ENOBUFS;
2500 goto out;
2501 }
2502
2503 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2504 NL80211_CMD_GET_REG);
2505 if (!hdr)
2506 goto nla_put_failure;
2507
2508 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2509 cfg80211_regdomain->alpha2);
2510
2511 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2512 if (!nl_reg_rules)
2513 goto nla_put_failure;
2514
2515 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2516 struct nlattr *nl_reg_rule;
2517 const struct ieee80211_reg_rule *reg_rule;
2518 const struct ieee80211_freq_range *freq_range;
2519 const struct ieee80211_power_rule *power_rule;
2520
2521 reg_rule = &cfg80211_regdomain->reg_rules[i];
2522 freq_range = &reg_rule->freq_range;
2523 power_rule = &reg_rule->power_rule;
2524
2525 nl_reg_rule = nla_nest_start(msg, i);
2526 if (!nl_reg_rule)
2527 goto nla_put_failure;
2528
2529 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2530 reg_rule->flags);
2531 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2532 freq_range->start_freq_khz);
2533 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2534 freq_range->end_freq_khz);
2535 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2536 freq_range->max_bandwidth_khz);
2537 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2538 power_rule->max_antenna_gain);
2539 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2540 power_rule->max_eirp);
2541
2542 nla_nest_end(msg, nl_reg_rule);
2543 }
2544
2545 nla_nest_end(msg, nl_reg_rules);
2546
2547 genlmsg_end(msg, hdr);
2548 err = genlmsg_unicast(msg, info->snd_pid);
2549 goto out;
2550
2551nla_put_failure:
2552 genlmsg_cancel(msg, hdr);
2553 err = -EMSGSIZE;
2554out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002555 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002556 return err;
2557}
2558
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002559static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2560{
2561 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2562 struct nlattr *nl_reg_rule;
2563 char *alpha2 = NULL;
2564 int rem_reg_rules = 0, r = 0;
2565 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2566 struct ieee80211_regdomain *rd = NULL;
2567
2568 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2569 return -EINVAL;
2570
2571 if (!info->attrs[NL80211_ATTR_REG_RULES])
2572 return -EINVAL;
2573
2574 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2575
2576 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2577 rem_reg_rules) {
2578 num_rules++;
2579 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002580 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002581 }
2582
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002583 mutex_lock(&cfg80211_mutex);
2584
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002585 if (!reg_is_valid_request(alpha2)) {
2586 r = -EINVAL;
2587 goto bad_reg;
2588 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002589
2590 size_of_regd = sizeof(struct ieee80211_regdomain) +
2591 (num_rules * sizeof(struct ieee80211_reg_rule));
2592
2593 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002594 if (!rd) {
2595 r = -ENOMEM;
2596 goto bad_reg;
2597 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002598
2599 rd->n_reg_rules = num_rules;
2600 rd->alpha2[0] = alpha2[0];
2601 rd->alpha2[1] = alpha2[1];
2602
2603 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2604 rem_reg_rules) {
2605 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2606 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2607 reg_rule_policy);
2608 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2609 if (r)
2610 goto bad_reg;
2611
2612 rule_idx++;
2613
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002614 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2615 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002616 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002617 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002618 }
2619
2620 BUG_ON(rule_idx != num_rules);
2621
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002622 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002623
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002624 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002625
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002626 return r;
2627
Johannes Bergd2372b32008-10-24 20:32:20 +02002628 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002629 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002630 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002631 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002632}
2633
Johannes Berg2a519312009-02-10 21:25:55 +01002634static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2635{
2636 struct cfg80211_registered_device *drv;
2637 struct net_device *dev;
2638 struct cfg80211_scan_request *request;
2639 struct cfg80211_ssid *ssid;
2640 struct ieee80211_channel *channel;
2641 struct nlattr *attr;
2642 struct wiphy *wiphy;
2643 int err, tmp, n_ssids = 0, n_channels = 0, i;
2644 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002645 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002646
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002647 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2648 return -EINVAL;
2649
Johannes Berg3b858752009-03-12 09:55:09 +01002650 rtnl_lock();
2651
Johannes Berg2a519312009-02-10 21:25:55 +01002652 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2653 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002654 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002655
2656 wiphy = &drv->wiphy;
2657
2658 if (!drv->ops->scan) {
2659 err = -EOPNOTSUPP;
2660 goto out;
2661 }
2662
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002663 if (!netif_running(dev)) {
2664 err = -ENETDOWN;
2665 goto out;
2666 }
2667
Johannes Berg2a519312009-02-10 21:25:55 +01002668 if (drv->scan_req) {
2669 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002670 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002671 }
2672
2673 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2674 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2675 n_channels++;
2676 if (!n_channels) {
2677 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002678 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002679 }
2680 } else {
2681 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2682 if (wiphy->bands[band])
2683 n_channels += wiphy->bands[band]->n_channels;
2684 }
2685
2686 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2687 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2688 n_ssids++;
2689
2690 if (n_ssids > wiphy->max_scan_ssids) {
2691 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002692 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002693 }
2694
Jouni Malinen70692ad2009-02-16 19:39:13 +02002695 if (info->attrs[NL80211_ATTR_IE])
2696 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2697 else
2698 ie_len = 0;
2699
Johannes Berg18a83652009-03-31 12:12:05 +02002700 if (ie_len > wiphy->max_scan_ie_len) {
2701 err = -EINVAL;
2702 goto out;
2703 }
2704
Johannes Berg2a519312009-02-10 21:25:55 +01002705 request = kzalloc(sizeof(*request)
2706 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002707 + sizeof(channel) * n_channels
2708 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002709 if (!request) {
2710 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002711 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002712 }
2713
2714 request->channels = (void *)((char *)request + sizeof(*request));
2715 request->n_channels = n_channels;
2716 if (n_ssids)
2717 request->ssids = (void *)(request->channels + n_channels);
2718 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002719 if (ie_len) {
2720 if (request->ssids)
2721 request->ie = (void *)(request->ssids + n_ssids);
2722 else
2723 request->ie = (void *)(request->channels + n_channels);
2724 }
Johannes Berg2a519312009-02-10 21:25:55 +01002725
2726 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2727 /* user specified, bail out if channel not found */
2728 request->n_channels = n_channels;
2729 i = 0;
2730 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2731 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2732 if (!request->channels[i]) {
2733 err = -EINVAL;
2734 goto out_free;
2735 }
2736 i++;
2737 }
2738 } else {
2739 /* all channels */
2740 i = 0;
2741 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2742 int j;
2743 if (!wiphy->bands[band])
2744 continue;
2745 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2746 request->channels[i] = &wiphy->bands[band]->channels[j];
2747 i++;
2748 }
2749 }
2750 }
2751
2752 i = 0;
2753 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2754 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2755 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2756 err = -EINVAL;
2757 goto out_free;
2758 }
2759 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2760 request->ssids[i].ssid_len = nla_len(attr);
2761 i++;
2762 }
2763 }
2764
Jouni Malinen70692ad2009-02-16 19:39:13 +02002765 if (info->attrs[NL80211_ATTR_IE]) {
2766 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02002767 memcpy((void *)request->ie,
2768 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02002769 request->ie_len);
2770 }
2771
Johannes Berg2a519312009-02-10 21:25:55 +01002772 request->ifidx = dev->ifindex;
2773 request->wiphy = &drv->wiphy;
2774
2775 drv->scan_req = request;
2776 err = drv->ops->scan(&drv->wiphy, dev, request);
2777
2778 out_free:
2779 if (err) {
2780 drv->scan_req = NULL;
2781 kfree(request);
2782 }
Johannes Berg2a519312009-02-10 21:25:55 +01002783 out:
2784 cfg80211_put_dev(drv);
2785 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002786 out_rtnl:
2787 rtnl_unlock();
2788
Johannes Berg2a519312009-02-10 21:25:55 +01002789 return err;
2790}
2791
2792static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2793 struct cfg80211_registered_device *rdev,
2794 struct net_device *dev,
2795 struct cfg80211_bss *res)
2796{
2797 void *hdr;
2798 struct nlattr *bss;
2799
2800 hdr = nl80211hdr_put(msg, pid, seq, flags,
2801 NL80211_CMD_NEW_SCAN_RESULTS);
2802 if (!hdr)
2803 return -1;
2804
2805 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2806 rdev->bss_generation);
2807 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2808
2809 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2810 if (!bss)
2811 goto nla_put_failure;
2812 if (!is_zero_ether_addr(res->bssid))
2813 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2814 if (res->information_elements && res->len_information_elements)
2815 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2816 res->len_information_elements,
2817 res->information_elements);
2818 if (res->tsf)
2819 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2820 if (res->beacon_interval)
2821 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2822 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2823 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2824
Johannes Berg77965c92009-02-18 18:45:06 +01002825 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002826 case CFG80211_SIGNAL_TYPE_MBM:
2827 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2828 break;
2829 case CFG80211_SIGNAL_TYPE_UNSPEC:
2830 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2831 break;
2832 default:
2833 break;
2834 }
2835
2836 nla_nest_end(msg, bss);
2837
2838 return genlmsg_end(msg, hdr);
2839
2840 nla_put_failure:
2841 genlmsg_cancel(msg, hdr);
2842 return -EMSGSIZE;
2843}
2844
2845static int nl80211_dump_scan(struct sk_buff *skb,
2846 struct netlink_callback *cb)
2847{
2848 struct cfg80211_registered_device *dev;
2849 struct net_device *netdev;
2850 struct cfg80211_internal_bss *scan;
2851 int ifidx = cb->args[0];
2852 int start = cb->args[1], idx = 0;
2853 int err;
2854
2855 if (!ifidx) {
2856 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2857 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2858 nl80211_policy);
2859 if (err)
2860 return err;
2861
2862 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2863 return -EINVAL;
2864
2865 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2866 if (!ifidx)
2867 return -EINVAL;
2868 cb->args[0] = ifidx;
2869 }
2870
2871 netdev = dev_get_by_index(&init_net, ifidx);
2872 if (!netdev)
2873 return -ENODEV;
2874
2875 dev = cfg80211_get_dev_from_ifindex(ifidx);
2876 if (IS_ERR(dev)) {
2877 err = PTR_ERR(dev);
2878 goto out_put_netdev;
2879 }
2880
2881 spin_lock_bh(&dev->bss_lock);
2882 cfg80211_bss_expire(dev);
2883
2884 list_for_each_entry(scan, &dev->bss_list, list) {
2885 if (++idx <= start)
2886 continue;
2887 if (nl80211_send_bss(skb,
2888 NETLINK_CB(cb->skb).pid,
2889 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2890 dev, netdev, &scan->pub) < 0) {
2891 idx--;
2892 goto out;
2893 }
2894 }
2895
2896 out:
2897 spin_unlock_bh(&dev->bss_lock);
2898
2899 cb->args[1] = idx;
2900 err = skb->len;
2901 cfg80211_put_dev(dev);
2902 out_put_netdev:
2903 dev_put(netdev);
2904
2905 return err;
2906}
2907
Jouni Malinen255e7372009-03-20 21:21:17 +02002908static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
2909{
2910 return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM ||
2911 auth_type == NL80211_AUTHTYPE_SHARED_KEY ||
2912 auth_type == NL80211_AUTHTYPE_FT ||
2913 auth_type == NL80211_AUTHTYPE_NETWORK_EAP;
2914}
2915
Jouni Malinen636a5d32009-03-19 13:39:22 +02002916static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2917{
2918 struct cfg80211_registered_device *drv;
2919 struct net_device *dev;
2920 struct cfg80211_auth_request req;
2921 struct wiphy *wiphy;
2922 int err;
2923
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002924 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2925 return -EINVAL;
2926
2927 if (!info->attrs[NL80211_ATTR_MAC])
2928 return -EINVAL;
2929
Jouni Malinen17780922009-03-27 20:52:47 +02002930 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
2931 return -EINVAL;
2932
Jouni Malinen636a5d32009-03-19 13:39:22 +02002933 rtnl_lock();
2934
2935 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2936 if (err)
2937 goto unlock_rtnl;
2938
2939 if (!drv->ops->auth) {
2940 err = -EOPNOTSUPP;
2941 goto out;
2942 }
2943
Jouni Malineneec60b02009-03-20 21:21:19 +02002944 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
2945 err = -EOPNOTSUPP;
2946 goto out;
2947 }
2948
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002949 if (!netif_running(dev)) {
2950 err = -ENETDOWN;
2951 goto out;
2952 }
2953
Jouni Malinen636a5d32009-03-19 13:39:22 +02002954 wiphy = &drv->wiphy;
2955 memset(&req, 0, sizeof(req));
2956
2957 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2958
2959 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2960 req.chan = ieee80211_get_channel(
2961 wiphy,
2962 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2963 if (!req.chan) {
2964 err = -EINVAL;
2965 goto out;
2966 }
2967 }
2968
2969 if (info->attrs[NL80211_ATTR_SSID]) {
2970 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2971 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2972 }
2973
2974 if (info->attrs[NL80211_ATTR_IE]) {
2975 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2976 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2977 }
2978
Jouni Malinen17780922009-03-27 20:52:47 +02002979 req.auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
2980 if (!nl80211_valid_auth_type(req.auth_type)) {
2981 err = -EINVAL;
2982 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02002983 }
2984
2985 err = drv->ops->auth(&drv->wiphy, dev, &req);
2986
2987out:
2988 cfg80211_put_dev(drv);
2989 dev_put(dev);
2990unlock_rtnl:
2991 rtnl_unlock();
2992 return err;
2993}
2994
2995static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
2996{
2997 struct cfg80211_registered_device *drv;
2998 struct net_device *dev;
2999 struct cfg80211_assoc_request req;
3000 struct wiphy *wiphy;
3001 int err;
3002
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003003 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3004 return -EINVAL;
3005
3006 if (!info->attrs[NL80211_ATTR_MAC] ||
3007 !info->attrs[NL80211_ATTR_SSID])
3008 return -EINVAL;
3009
Jouni Malinen636a5d32009-03-19 13:39:22 +02003010 rtnl_lock();
3011
3012 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3013 if (err)
3014 goto unlock_rtnl;
3015
3016 if (!drv->ops->assoc) {
3017 err = -EOPNOTSUPP;
3018 goto out;
3019 }
3020
Jouni Malineneec60b02009-03-20 21:21:19 +02003021 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3022 err = -EOPNOTSUPP;
3023 goto out;
3024 }
3025
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003026 if (!netif_running(dev)) {
3027 err = -ENETDOWN;
3028 goto out;
3029 }
3030
Jouni Malinen636a5d32009-03-19 13:39:22 +02003031 wiphy = &drv->wiphy;
3032 memset(&req, 0, sizeof(req));
3033
3034 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3035
3036 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3037 req.chan = ieee80211_get_channel(
3038 wiphy,
3039 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3040 if (!req.chan) {
3041 err = -EINVAL;
3042 goto out;
3043 }
3044 }
3045
Jouni Malinen636a5d32009-03-19 13:39:22 +02003046 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3047 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3048
3049 if (info->attrs[NL80211_ATTR_IE]) {
3050 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3051 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3052 }
3053
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003054 if (info->attrs[NL80211_ATTR_USE_MFP]) {
3055 enum nl80211_mfp use_mfp =
3056 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3057 if (use_mfp == NL80211_MFP_REQUIRED)
3058 req.use_mfp = true;
3059 else if (use_mfp != NL80211_MFP_NO) {
3060 err = -EINVAL;
3061 goto out;
3062 }
3063 }
3064
Jouni Malinen3f77316c2009-05-11 21:57:57 +03003065 req.control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3066
Jouni Malinen636a5d32009-03-19 13:39:22 +02003067 err = drv->ops->assoc(&drv->wiphy, dev, &req);
3068
3069out:
3070 cfg80211_put_dev(drv);
3071 dev_put(dev);
3072unlock_rtnl:
3073 rtnl_unlock();
3074 return err;
3075}
3076
3077static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3078{
3079 struct cfg80211_registered_device *drv;
3080 struct net_device *dev;
3081 struct cfg80211_deauth_request req;
3082 struct wiphy *wiphy;
3083 int err;
3084
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003085 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3086 return -EINVAL;
3087
3088 if (!info->attrs[NL80211_ATTR_MAC])
3089 return -EINVAL;
3090
3091 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3092 return -EINVAL;
3093
Jouni Malinen636a5d32009-03-19 13:39:22 +02003094 rtnl_lock();
3095
3096 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3097 if (err)
3098 goto unlock_rtnl;
3099
3100 if (!drv->ops->deauth) {
3101 err = -EOPNOTSUPP;
3102 goto out;
3103 }
3104
Jouni Malineneec60b02009-03-20 21:21:19 +02003105 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3106 err = -EOPNOTSUPP;
3107 goto out;
3108 }
3109
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003110 if (!netif_running(dev)) {
3111 err = -ENETDOWN;
3112 goto out;
3113 }
3114
Jouni Malinen636a5d32009-03-19 13:39:22 +02003115 wiphy = &drv->wiphy;
3116 memset(&req, 0, sizeof(req));
3117
3118 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3119
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003120 req.reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3121 if (req.reason_code == 0) {
3122 /* Reason Code 0 is reserved */
3123 err = -EINVAL;
3124 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003125 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003126
3127 if (info->attrs[NL80211_ATTR_IE]) {
3128 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3129 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3130 }
3131
3132 err = drv->ops->deauth(&drv->wiphy, dev, &req);
3133
3134out:
3135 cfg80211_put_dev(drv);
3136 dev_put(dev);
3137unlock_rtnl:
3138 rtnl_unlock();
3139 return err;
3140}
3141
3142static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3143{
3144 struct cfg80211_registered_device *drv;
3145 struct net_device *dev;
3146 struct cfg80211_disassoc_request req;
3147 struct wiphy *wiphy;
3148 int err;
3149
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003150 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3151 return -EINVAL;
3152
3153 if (!info->attrs[NL80211_ATTR_MAC])
3154 return -EINVAL;
3155
3156 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3157 return -EINVAL;
3158
Jouni Malinen636a5d32009-03-19 13:39:22 +02003159 rtnl_lock();
3160
3161 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3162 if (err)
3163 goto unlock_rtnl;
3164
3165 if (!drv->ops->disassoc) {
3166 err = -EOPNOTSUPP;
3167 goto out;
3168 }
3169
Jouni Malineneec60b02009-03-20 21:21:19 +02003170 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3171 err = -EOPNOTSUPP;
3172 goto out;
3173 }
3174
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003175 if (!netif_running(dev)) {
3176 err = -ENETDOWN;
3177 goto out;
3178 }
3179
Jouni Malinen636a5d32009-03-19 13:39:22 +02003180 wiphy = &drv->wiphy;
3181 memset(&req, 0, sizeof(req));
3182
3183 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3184
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003185 req.reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3186 if (req.reason_code == 0) {
3187 /* Reason Code 0 is reserved */
3188 err = -EINVAL;
3189 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003190 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003191
3192 if (info->attrs[NL80211_ATTR_IE]) {
3193 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3194 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3195 }
3196
3197 err = drv->ops->disassoc(&drv->wiphy, dev, &req);
3198
3199out:
3200 cfg80211_put_dev(drv);
3201 dev_put(dev);
3202unlock_rtnl:
3203 rtnl_unlock();
3204 return err;
3205}
3206
Johannes Berg04a773a2009-04-19 21:24:32 +02003207static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3208{
3209 struct cfg80211_registered_device *drv;
3210 struct net_device *dev;
3211 struct cfg80211_ibss_params ibss;
3212 struct wiphy *wiphy;
3213 int err;
3214
Johannes Berg8e30bc52009-04-22 17:45:38 +02003215 memset(&ibss, 0, sizeof(ibss));
3216
Johannes Berg04a773a2009-04-19 21:24:32 +02003217 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3218 return -EINVAL;
3219
3220 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3221 !info->attrs[NL80211_ATTR_SSID] ||
3222 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3223 return -EINVAL;
3224
Johannes Berg8e30bc52009-04-22 17:45:38 +02003225 ibss.beacon_interval = 100;
3226
3227 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3228 ibss.beacon_interval =
3229 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3230 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3231 return -EINVAL;
3232 }
3233
Johannes Berg04a773a2009-04-19 21:24:32 +02003234 rtnl_lock();
3235
3236 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3237 if (err)
3238 goto unlock_rtnl;
3239
3240 if (!drv->ops->join_ibss) {
3241 err = -EOPNOTSUPP;
3242 goto out;
3243 }
3244
3245 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3246 err = -EOPNOTSUPP;
3247 goto out;
3248 }
3249
3250 if (!netif_running(dev)) {
3251 err = -ENETDOWN;
3252 goto out;
3253 }
3254
3255 wiphy = &drv->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003256
3257 if (info->attrs[NL80211_ATTR_MAC])
3258 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3259 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3260 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3261
3262 if (info->attrs[NL80211_ATTR_IE]) {
3263 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3264 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3265 }
3266
3267 ibss.channel = ieee80211_get_channel(wiphy,
3268 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3269 if (!ibss.channel ||
3270 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3271 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3272 err = -EINVAL;
3273 goto out;
3274 }
3275
3276 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3277
3278 err = cfg80211_join_ibss(drv, dev, &ibss);
3279
3280out:
3281 cfg80211_put_dev(drv);
3282 dev_put(dev);
3283unlock_rtnl:
3284 rtnl_unlock();
3285 return err;
3286}
3287
3288static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3289{
3290 struct cfg80211_registered_device *drv;
3291 struct net_device *dev;
3292 int err;
3293
3294 rtnl_lock();
3295
3296 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3297 if (err)
3298 goto unlock_rtnl;
3299
3300 if (!drv->ops->leave_ibss) {
3301 err = -EOPNOTSUPP;
3302 goto out;
3303 }
3304
3305 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3306 err = -EOPNOTSUPP;
3307 goto out;
3308 }
3309
3310 if (!netif_running(dev)) {
3311 err = -ENETDOWN;
3312 goto out;
3313 }
3314
Johannes Berg9d308422009-04-20 18:43:46 +02003315 err = cfg80211_leave_ibss(drv, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003316
3317out:
3318 cfg80211_put_dev(drv);
3319 dev_put(dev);
3320unlock_rtnl:
3321 rtnl_unlock();
3322 return err;
3323}
3324
Johannes Berg55682962007-09-20 13:09:35 -04003325static struct genl_ops nl80211_ops[] = {
3326 {
3327 .cmd = NL80211_CMD_GET_WIPHY,
3328 .doit = nl80211_get_wiphy,
3329 .dumpit = nl80211_dump_wiphy,
3330 .policy = nl80211_policy,
3331 /* can be retrieved by unprivileged users */
3332 },
3333 {
3334 .cmd = NL80211_CMD_SET_WIPHY,
3335 .doit = nl80211_set_wiphy,
3336 .policy = nl80211_policy,
3337 .flags = GENL_ADMIN_PERM,
3338 },
3339 {
3340 .cmd = NL80211_CMD_GET_INTERFACE,
3341 .doit = nl80211_get_interface,
3342 .dumpit = nl80211_dump_interface,
3343 .policy = nl80211_policy,
3344 /* can be retrieved by unprivileged users */
3345 },
3346 {
3347 .cmd = NL80211_CMD_SET_INTERFACE,
3348 .doit = nl80211_set_interface,
3349 .policy = nl80211_policy,
3350 .flags = GENL_ADMIN_PERM,
3351 },
3352 {
3353 .cmd = NL80211_CMD_NEW_INTERFACE,
3354 .doit = nl80211_new_interface,
3355 .policy = nl80211_policy,
3356 .flags = GENL_ADMIN_PERM,
3357 },
3358 {
3359 .cmd = NL80211_CMD_DEL_INTERFACE,
3360 .doit = nl80211_del_interface,
3361 .policy = nl80211_policy,
3362 .flags = GENL_ADMIN_PERM,
3363 },
Johannes Berg41ade002007-12-19 02:03:29 +01003364 {
3365 .cmd = NL80211_CMD_GET_KEY,
3366 .doit = nl80211_get_key,
3367 .policy = nl80211_policy,
3368 .flags = GENL_ADMIN_PERM,
3369 },
3370 {
3371 .cmd = NL80211_CMD_SET_KEY,
3372 .doit = nl80211_set_key,
3373 .policy = nl80211_policy,
3374 .flags = GENL_ADMIN_PERM,
3375 },
3376 {
3377 .cmd = NL80211_CMD_NEW_KEY,
3378 .doit = nl80211_new_key,
3379 .policy = nl80211_policy,
3380 .flags = GENL_ADMIN_PERM,
3381 },
3382 {
3383 .cmd = NL80211_CMD_DEL_KEY,
3384 .doit = nl80211_del_key,
3385 .policy = nl80211_policy,
3386 .flags = GENL_ADMIN_PERM,
3387 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01003388 {
3389 .cmd = NL80211_CMD_SET_BEACON,
3390 .policy = nl80211_policy,
3391 .flags = GENL_ADMIN_PERM,
3392 .doit = nl80211_addset_beacon,
3393 },
3394 {
3395 .cmd = NL80211_CMD_NEW_BEACON,
3396 .policy = nl80211_policy,
3397 .flags = GENL_ADMIN_PERM,
3398 .doit = nl80211_addset_beacon,
3399 },
3400 {
3401 .cmd = NL80211_CMD_DEL_BEACON,
3402 .policy = nl80211_policy,
3403 .flags = GENL_ADMIN_PERM,
3404 .doit = nl80211_del_beacon,
3405 },
Johannes Berg5727ef12007-12-19 02:03:34 +01003406 {
3407 .cmd = NL80211_CMD_GET_STATION,
3408 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003409 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01003410 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01003411 },
3412 {
3413 .cmd = NL80211_CMD_SET_STATION,
3414 .doit = nl80211_set_station,
3415 .policy = nl80211_policy,
3416 .flags = GENL_ADMIN_PERM,
3417 },
3418 {
3419 .cmd = NL80211_CMD_NEW_STATION,
3420 .doit = nl80211_new_station,
3421 .policy = nl80211_policy,
3422 .flags = GENL_ADMIN_PERM,
3423 },
3424 {
3425 .cmd = NL80211_CMD_DEL_STATION,
3426 .doit = nl80211_del_station,
3427 .policy = nl80211_policy,
3428 .flags = GENL_ADMIN_PERM,
3429 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003430 {
3431 .cmd = NL80211_CMD_GET_MPATH,
3432 .doit = nl80211_get_mpath,
3433 .dumpit = nl80211_dump_mpath,
3434 .policy = nl80211_policy,
3435 .flags = GENL_ADMIN_PERM,
3436 },
3437 {
3438 .cmd = NL80211_CMD_SET_MPATH,
3439 .doit = nl80211_set_mpath,
3440 .policy = nl80211_policy,
3441 .flags = GENL_ADMIN_PERM,
3442 },
3443 {
3444 .cmd = NL80211_CMD_NEW_MPATH,
3445 .doit = nl80211_new_mpath,
3446 .policy = nl80211_policy,
3447 .flags = GENL_ADMIN_PERM,
3448 },
3449 {
3450 .cmd = NL80211_CMD_DEL_MPATH,
3451 .doit = nl80211_del_mpath,
3452 .policy = nl80211_policy,
3453 .flags = GENL_ADMIN_PERM,
3454 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003455 {
3456 .cmd = NL80211_CMD_SET_BSS,
3457 .doit = nl80211_set_bss,
3458 .policy = nl80211_policy,
3459 .flags = GENL_ADMIN_PERM,
3460 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003461 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003462 .cmd = NL80211_CMD_GET_REG,
3463 .doit = nl80211_get_reg,
3464 .policy = nl80211_policy,
3465 /* can be retrieved by unprivileged users */
3466 },
3467 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003468 .cmd = NL80211_CMD_SET_REG,
3469 .doit = nl80211_set_reg,
3470 .policy = nl80211_policy,
3471 .flags = GENL_ADMIN_PERM,
3472 },
3473 {
3474 .cmd = NL80211_CMD_REQ_SET_REG,
3475 .doit = nl80211_req_set_reg,
3476 .policy = nl80211_policy,
3477 .flags = GENL_ADMIN_PERM,
3478 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003479 {
3480 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3481 .doit = nl80211_get_mesh_params,
3482 .policy = nl80211_policy,
3483 /* can be retrieved by unprivileged users */
3484 },
3485 {
3486 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3487 .doit = nl80211_set_mesh_params,
3488 .policy = nl80211_policy,
3489 .flags = GENL_ADMIN_PERM,
3490 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02003491 {
Johannes Berg2a519312009-02-10 21:25:55 +01003492 .cmd = NL80211_CMD_TRIGGER_SCAN,
3493 .doit = nl80211_trigger_scan,
3494 .policy = nl80211_policy,
3495 .flags = GENL_ADMIN_PERM,
3496 },
3497 {
3498 .cmd = NL80211_CMD_GET_SCAN,
3499 .policy = nl80211_policy,
3500 .dumpit = nl80211_dump_scan,
3501 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02003502 {
3503 .cmd = NL80211_CMD_AUTHENTICATE,
3504 .doit = nl80211_authenticate,
3505 .policy = nl80211_policy,
3506 .flags = GENL_ADMIN_PERM,
3507 },
3508 {
3509 .cmd = NL80211_CMD_ASSOCIATE,
3510 .doit = nl80211_associate,
3511 .policy = nl80211_policy,
3512 .flags = GENL_ADMIN_PERM,
3513 },
3514 {
3515 .cmd = NL80211_CMD_DEAUTHENTICATE,
3516 .doit = nl80211_deauthenticate,
3517 .policy = nl80211_policy,
3518 .flags = GENL_ADMIN_PERM,
3519 },
3520 {
3521 .cmd = NL80211_CMD_DISASSOCIATE,
3522 .doit = nl80211_disassociate,
3523 .policy = nl80211_policy,
3524 .flags = GENL_ADMIN_PERM,
3525 },
Johannes Berg04a773a2009-04-19 21:24:32 +02003526 {
3527 .cmd = NL80211_CMD_JOIN_IBSS,
3528 .doit = nl80211_join_ibss,
3529 .policy = nl80211_policy,
3530 .flags = GENL_ADMIN_PERM,
3531 },
3532 {
3533 .cmd = NL80211_CMD_LEAVE_IBSS,
3534 .doit = nl80211_leave_ibss,
3535 .policy = nl80211_policy,
3536 .flags = GENL_ADMIN_PERM,
3537 },
Johannes Berg55682962007-09-20 13:09:35 -04003538};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003539static struct genl_multicast_group nl80211_mlme_mcgrp = {
3540 .name = "mlme",
3541};
Johannes Berg55682962007-09-20 13:09:35 -04003542
3543/* multicast groups */
3544static struct genl_multicast_group nl80211_config_mcgrp = {
3545 .name = "config",
3546};
Johannes Berg2a519312009-02-10 21:25:55 +01003547static struct genl_multicast_group nl80211_scan_mcgrp = {
3548 .name = "scan",
3549};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003550static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3551 .name = "regulatory",
3552};
Johannes Berg55682962007-09-20 13:09:35 -04003553
3554/* notification functions */
3555
3556void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3557{
3558 struct sk_buff *msg;
3559
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003560 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04003561 if (!msg)
3562 return;
3563
3564 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3565 nlmsg_free(msg);
3566 return;
3567 }
3568
3569 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3570}
3571
Johannes Berg362a4152009-05-24 16:43:15 +02003572static int nl80211_add_scan_req(struct sk_buff *msg,
3573 struct cfg80211_registered_device *rdev)
3574{
3575 struct cfg80211_scan_request *req = rdev->scan_req;
3576 struct nlattr *nest;
3577 int i;
3578
3579 if (WARN_ON(!req))
3580 return 0;
3581
3582 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
3583 if (!nest)
3584 goto nla_put_failure;
3585 for (i = 0; i < req->n_ssids; i++)
3586 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
3587 nla_nest_end(msg, nest);
3588
3589 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
3590 if (!nest)
3591 goto nla_put_failure;
3592 for (i = 0; i < req->n_channels; i++)
3593 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
3594 nla_nest_end(msg, nest);
3595
3596 if (req->ie)
3597 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
3598
3599 return 0;
3600 nla_put_failure:
3601 return -ENOBUFS;
3602}
3603
Johannes Berg2a519312009-02-10 21:25:55 +01003604static int nl80211_send_scan_donemsg(struct sk_buff *msg,
Johannes Berg362a4152009-05-24 16:43:15 +02003605 struct cfg80211_registered_device *rdev,
3606 struct net_device *netdev,
3607 u32 pid, u32 seq, int flags,
3608 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01003609{
3610 void *hdr;
3611
3612 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3613 if (!hdr)
3614 return -1;
3615
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05003616 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01003617 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3618
Johannes Berg362a4152009-05-24 16:43:15 +02003619 /* ignore errors and send incomplete event anyway */
3620 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003621
3622 return genlmsg_end(msg, hdr);
3623
3624 nla_put_failure:
3625 genlmsg_cancel(msg, hdr);
3626 return -EMSGSIZE;
3627}
3628
3629void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3630 struct net_device *netdev)
3631{
3632 struct sk_buff *msg;
3633
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003634 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003635 if (!msg)
3636 return;
3637
3638 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3639 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3640 nlmsg_free(msg);
3641 return;
3642 }
3643
3644 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3645}
3646
3647void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3648 struct net_device *netdev)
3649{
3650 struct sk_buff *msg;
3651
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003652 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003653 if (!msg)
3654 return;
3655
3656 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3657 NL80211_CMD_SCAN_ABORTED) < 0) {
3658 nlmsg_free(msg);
3659 return;
3660 }
3661
3662 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3663}
3664
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003665/*
3666 * This can happen on global regulatory changes or device specific settings
3667 * based on custom world regulatory domains.
3668 */
3669void nl80211_send_reg_change_event(struct regulatory_request *request)
3670{
3671 struct sk_buff *msg;
3672 void *hdr;
3673
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003674 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003675 if (!msg)
3676 return;
3677
3678 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3679 if (!hdr) {
3680 nlmsg_free(msg);
3681 return;
3682 }
3683
3684 /* Userspace can always count this one always being set */
3685 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3686
3687 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3688 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3689 NL80211_REGDOM_TYPE_WORLD);
3690 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3691 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3692 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3693 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3694 request->intersect)
3695 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3696 NL80211_REGDOM_TYPE_INTERSECTION);
3697 else {
3698 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3699 NL80211_REGDOM_TYPE_COUNTRY);
3700 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3701 }
3702
3703 if (wiphy_idx_valid(request->wiphy_idx))
3704 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3705
3706 if (genlmsg_end(msg, hdr) < 0) {
3707 nlmsg_free(msg);
3708 return;
3709 }
3710
3711 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3712
3713 return;
3714
3715nla_put_failure:
3716 genlmsg_cancel(msg, hdr);
3717 nlmsg_free(msg);
3718}
3719
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003720static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3721 struct net_device *netdev,
3722 const u8 *buf, size_t len,
3723 enum nl80211_commands cmd)
3724{
3725 struct sk_buff *msg;
3726 void *hdr;
3727
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003728 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003729 if (!msg)
3730 return;
3731
3732 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3733 if (!hdr) {
3734 nlmsg_free(msg);
3735 return;
3736 }
3737
3738 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3739 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3740 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3741
3742 if (genlmsg_end(msg, hdr) < 0) {
3743 nlmsg_free(msg);
3744 return;
3745 }
3746
Jouni Malinend91c01c2009-04-18 21:53:15 +03003747 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003748 return;
3749
3750 nla_put_failure:
3751 genlmsg_cancel(msg, hdr);
3752 nlmsg_free(msg);
3753}
3754
3755void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3756 struct net_device *netdev, const u8 *buf, size_t len)
3757{
3758 nl80211_send_mlme_event(rdev, netdev, buf, len,
3759 NL80211_CMD_AUTHENTICATE);
3760}
3761
3762void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3763 struct net_device *netdev, const u8 *buf,
3764 size_t len)
3765{
3766 nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3767}
3768
Jouni Malinen53b46b82009-03-27 20:53:56 +02003769void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
3770 struct net_device *netdev, const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003771{
3772 nl80211_send_mlme_event(rdev, netdev, buf, len,
3773 NL80211_CMD_DEAUTHENTICATE);
3774}
3775
Jouni Malinen53b46b82009-03-27 20:53:56 +02003776void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
3777 struct net_device *netdev, const u8 *buf,
3778 size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003779{
3780 nl80211_send_mlme_event(rdev, netdev, buf, len,
3781 NL80211_CMD_DISASSOCIATE);
3782}
3783
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04003784static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
3785 struct net_device *netdev, int cmd,
3786 const u8 *addr)
Jouni Malinen1965c852009-04-22 21:38:25 +03003787{
3788 struct sk_buff *msg;
3789 void *hdr;
3790
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Jouni Malinen1965c852009-04-22 21:38:25 +03003792 if (!msg)
3793 return;
3794
3795 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3796 if (!hdr) {
3797 nlmsg_free(msg);
3798 return;
3799 }
3800
3801 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3802 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3803 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
3804 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3805
3806 if (genlmsg_end(msg, hdr) < 0) {
3807 nlmsg_free(msg);
3808 return;
3809 }
3810
3811 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
3812 return;
3813
3814 nla_put_failure:
3815 genlmsg_cancel(msg, hdr);
3816 nlmsg_free(msg);
3817}
3818
3819void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
3820 struct net_device *netdev, const u8 *addr)
3821{
3822 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
3823 addr);
3824}
3825
3826void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
3827 struct net_device *netdev, const u8 *addr)
3828{
3829 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE, addr);
3830}
3831
Johannes Berg04a773a2009-04-19 21:24:32 +02003832void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
3833 struct net_device *netdev, const u8 *bssid,
3834 gfp_t gfp)
3835{
3836 struct sk_buff *msg;
3837 void *hdr;
3838
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003839 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02003840 if (!msg)
3841 return;
3842
3843 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
3844 if (!hdr) {
3845 nlmsg_free(msg);
3846 return;
3847 }
3848
3849 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3851 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
3852
3853 if (genlmsg_end(msg, hdr) < 0) {
3854 nlmsg_free(msg);
3855 return;
3856 }
3857
3858 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
3859 return;
3860
3861 nla_put_failure:
3862 genlmsg_cancel(msg, hdr);
3863 nlmsg_free(msg);
3864}
3865
Jouni Malinena3b8b052009-03-27 21:59:49 +02003866void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
3867 struct net_device *netdev, const u8 *addr,
3868 enum nl80211_key_type key_type, int key_id,
3869 const u8 *tsc)
3870{
3871 struct sk_buff *msg;
3872 void *hdr;
3873
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003874 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jouni Malinena3b8b052009-03-27 21:59:49 +02003875 if (!msg)
3876 return;
3877
3878 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
3879 if (!hdr) {
3880 nlmsg_free(msg);
3881 return;
3882 }
3883
3884 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3885 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3886 if (addr)
3887 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3888 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
3889 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
3890 if (tsc)
3891 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
3892
3893 if (genlmsg_end(msg, hdr) < 0) {
3894 nlmsg_free(msg);
3895 return;
3896 }
3897
3898 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL);
3899 return;
3900
3901 nla_put_failure:
3902 genlmsg_cancel(msg, hdr);
3903 nlmsg_free(msg);
3904}
3905
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04003906void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
3907 struct ieee80211_channel *channel_before,
3908 struct ieee80211_channel *channel_after)
3909{
3910 struct sk_buff *msg;
3911 void *hdr;
3912 struct nlattr *nl_freq;
3913
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003914 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04003915 if (!msg)
3916 return;
3917
3918 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
3919 if (!hdr) {
3920 nlmsg_free(msg);
3921 return;
3922 }
3923
3924 /*
3925 * Since we are applying the beacon hint to a wiphy we know its
3926 * wiphy_idx is valid
3927 */
3928 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
3929
3930 /* Before */
3931 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
3932 if (!nl_freq)
3933 goto nla_put_failure;
3934 if (nl80211_msg_put_channel(msg, channel_before))
3935 goto nla_put_failure;
3936 nla_nest_end(msg, nl_freq);
3937
3938 /* After */
3939 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
3940 if (!nl_freq)
3941 goto nla_put_failure;
3942 if (nl80211_msg_put_channel(msg, channel_after))
3943 goto nla_put_failure;
3944 nla_nest_end(msg, nl_freq);
3945
3946 if (genlmsg_end(msg, hdr) < 0) {
3947 nlmsg_free(msg);
3948 return;
3949 }
3950
3951 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_ATOMIC);
3952
3953 return;
3954
3955nla_put_failure:
3956 genlmsg_cancel(msg, hdr);
3957 nlmsg_free(msg);
3958}
3959
Johannes Berg55682962007-09-20 13:09:35 -04003960/* initialisation/exit functions */
3961
3962int nl80211_init(void)
3963{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00003964 int err;
Johannes Berg55682962007-09-20 13:09:35 -04003965
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00003966 err = genl_register_family_with_ops(&nl80211_fam,
3967 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04003968 if (err)
3969 return err;
3970
Johannes Berg55682962007-09-20 13:09:35 -04003971 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
3972 if (err)
3973 goto err_out;
3974
Johannes Berg2a519312009-02-10 21:25:55 +01003975 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
3976 if (err)
3977 goto err_out;
3978
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003979 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
3980 if (err)
3981 goto err_out;
3982
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003983 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
3984 if (err)
3985 goto err_out;
3986
Johannes Berg55682962007-09-20 13:09:35 -04003987 return 0;
3988 err_out:
3989 genl_unregister_family(&nl80211_fam);
3990 return err;
3991}
3992
3993void nl80211_exit(void)
3994{
3995 genl_unregister_family(&nl80211_fam);
3996}