blob: 634496b3ed77cb7adf5e1ec1356a307b6b897356 [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)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200450 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100451 result = PTR_ERR(rdev);
452 goto unlock;
453 }
454
455 mutex_lock(&rdev->mtx);
456
457 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200458 result = cfg80211_dev_rename(
459 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100460
461 mutex_unlock(&cfg80211_mutex);
462
463 if (result)
464 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400465
Jouni Malinen31888482008-10-30 16:59:24 +0200466 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
467 struct ieee80211_txq_params txq_params;
468 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
469
470 if (!rdev->ops->set_txq_params) {
471 result = -EOPNOTSUPP;
472 goto bad_res;
473 }
474
475 nla_for_each_nested(nl_txq_params,
476 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
477 rem_txq_params) {
478 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
479 nla_data(nl_txq_params),
480 nla_len(nl_txq_params),
481 txq_params_policy);
482 result = parse_txq_params(tb, &txq_params);
483 if (result)
484 goto bad_res;
485
486 result = rdev->ops->set_txq_params(&rdev->wiphy,
487 &txq_params);
488 if (result)
489 goto bad_res;
490 }
491 }
492
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200493 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530494 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200495 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100496 struct ieee80211_sta_ht_cap *ht_cap;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400497 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200498
499 if (!rdev->ops->set_channel) {
500 result = -EOPNOTSUPP;
501 goto bad_res;
502 }
503
Johannes Berg306d6112008-12-08 12:39:04 +0100504 result = -EINVAL;
505
Sujith094d05d2008-12-12 11:57:43 +0530506 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
507 channel_type = nla_get_u32(info->attrs[
508 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
509 if (channel_type != NL80211_CHAN_NO_HT &&
510 channel_type != NL80211_CHAN_HT20 &&
511 channel_type != NL80211_CHAN_HT40PLUS &&
512 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200513 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200514 }
515
516 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
517 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100518
519 /* Primary channel not allowed */
520 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200521 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100522
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400523 if (channel_type == NL80211_CHAN_HT40MINUS &&
524 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS))
525 goto bad_res;
526 else if (channel_type == NL80211_CHAN_HT40PLUS &&
527 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS))
528 goto bad_res;
529
530 /*
531 * At this point we know if that if HT40 was requested
532 * we are allowed to use it and the extension channel
533 * exists.
534 */
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200535
Johannes Berg306d6112008-12-08 12:39:04 +0100536 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
537
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400538 /* no HT capabilities or intolerant */
539 if (channel_type != NL80211_CHAN_NO_HT) {
540 if (!ht_cap->ht_supported)
541 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100542 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
543 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200544 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200545 }
546
547 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530548 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200549 if (result)
550 goto bad_res;
551 }
552
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200553 changed = 0;
554
555 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
556 retry_short = nla_get_u8(
557 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
558 if (retry_short == 0) {
559 result = -EINVAL;
560 goto bad_res;
561 }
562 changed |= WIPHY_PARAM_RETRY_SHORT;
563 }
564
565 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
566 retry_long = nla_get_u8(
567 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
568 if (retry_long == 0) {
569 result = -EINVAL;
570 goto bad_res;
571 }
572 changed |= WIPHY_PARAM_RETRY_LONG;
573 }
574
575 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
576 frag_threshold = nla_get_u32(
577 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
578 if (frag_threshold < 256) {
579 result = -EINVAL;
580 goto bad_res;
581 }
582 if (frag_threshold != (u32) -1) {
583 /*
584 * Fragments (apart from the last one) are required to
585 * have even length. Make the fragmentation code
586 * simpler by stripping LSB should someone try to use
587 * odd threshold value.
588 */
589 frag_threshold &= ~0x1;
590 }
591 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
592 }
593
594 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
595 rts_threshold = nla_get_u32(
596 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
597 changed |= WIPHY_PARAM_RTS_THRESHOLD;
598 }
599
600 if (changed) {
601 u8 old_retry_short, old_retry_long;
602 u32 old_frag_threshold, old_rts_threshold;
603
604 if (!rdev->ops->set_wiphy_params) {
605 result = -EOPNOTSUPP;
606 goto bad_res;
607 }
608
609 old_retry_short = rdev->wiphy.retry_short;
610 old_retry_long = rdev->wiphy.retry_long;
611 old_frag_threshold = rdev->wiphy.frag_threshold;
612 old_rts_threshold = rdev->wiphy.rts_threshold;
613
614 if (changed & WIPHY_PARAM_RETRY_SHORT)
615 rdev->wiphy.retry_short = retry_short;
616 if (changed & WIPHY_PARAM_RETRY_LONG)
617 rdev->wiphy.retry_long = retry_long;
618 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
619 rdev->wiphy.frag_threshold = frag_threshold;
620 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
621 rdev->wiphy.rts_threshold = rts_threshold;
622
623 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
624 if (result) {
625 rdev->wiphy.retry_short = old_retry_short;
626 rdev->wiphy.retry_long = old_retry_long;
627 rdev->wiphy.frag_threshold = old_frag_threshold;
628 rdev->wiphy.rts_threshold = old_rts_threshold;
629 }
630 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200631
Johannes Berg306d6112008-12-08 12:39:04 +0100632 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100633 mutex_unlock(&rdev->mtx);
634 unlock:
635 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400636 return result;
637}
638
639
640static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200641 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400642 struct net_device *dev)
643{
644 void *hdr;
645
646 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
647 if (!hdr)
648 return -1;
649
650 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200651 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400652 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200653 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400654 return genlmsg_end(msg, hdr);
655
656 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700657 genlmsg_cancel(msg, hdr);
658 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400659}
660
661static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
662{
663 int wp_idx = 0;
664 int if_idx = 0;
665 int wp_start = cb->args[0];
666 int if_start = cb->args[1];
667 struct cfg80211_registered_device *dev;
668 struct wireless_dev *wdev;
669
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500670 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400671 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200672 if (wp_idx < wp_start) {
673 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400674 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200675 }
Johannes Berg55682962007-09-20 13:09:35 -0400676 if_idx = 0;
677
678 mutex_lock(&dev->devlist_mtx);
679 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200680 if (if_idx < if_start) {
681 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400682 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200683 }
Johannes Berg55682962007-09-20 13:09:35 -0400684 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
685 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergd7264052009-04-19 16:23:20 +0200686 dev, wdev->netdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200687 mutex_unlock(&dev->devlist_mtx);
688 goto out;
689 }
690 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400691 }
692 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200693
694 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400695 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200696 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500697 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400698
699 cb->args[0] = wp_idx;
700 cb->args[1] = if_idx;
701
702 return skb->len;
703}
704
705static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
706{
707 struct sk_buff *msg;
708 struct cfg80211_registered_device *dev;
709 struct net_device *netdev;
710 int err;
711
Johannes Bergbba95fe2008-07-29 13:22:51 +0200712 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400713 if (err)
714 return err;
715
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700716 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400717 if (!msg)
718 goto out_err;
719
Johannes Bergd7264052009-04-19 16:23:20 +0200720 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
721 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400722 goto out_free;
723
724 dev_put(netdev);
725 cfg80211_put_dev(dev);
726
727 return genlmsg_unicast(msg, info->snd_pid);
728
729 out_free:
730 nlmsg_free(msg);
731 out_err:
732 dev_put(netdev);
733 cfg80211_put_dev(dev);
734 return -ENOBUFS;
735}
736
Michael Wu66f7ac52008-01-31 19:48:22 +0100737static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
738 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
739 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
740 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
741 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
742 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
743};
744
745static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
746{
747 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
748 int flag;
749
750 *mntrflags = 0;
751
752 if (!nla)
753 return -EINVAL;
754
755 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
756 nla, mntr_flags_policy))
757 return -EINVAL;
758
759 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
760 if (flags[flag])
761 *mntrflags |= (1<<flag);
762
763 return 0;
764}
765
Johannes Berg55682962007-09-20 13:09:35 -0400766static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
767{
768 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100769 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400770 int err, ifindex;
Johannes Berg04a773a2009-04-19 21:24:32 +0200771 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -0400772 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200773 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100774 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400775
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100776 memset(&params, 0, sizeof(params));
777
Johannes Berg3b858752009-03-12 09:55:09 +0100778 rtnl_lock();
779
Johannes Bergbba95fe2008-07-29 13:22:51 +0200780 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400781 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100782 goto unlock_rtnl;
783
Johannes Berg55682962007-09-20 13:09:35 -0400784 ifindex = dev->ifindex;
Johannes Berg04a773a2009-04-19 21:24:32 +0200785 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400786 dev_put(dev);
787
Johannes Berg723b0382008-09-16 20:22:09 +0200788 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100789 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +0200790 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100791 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200792 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100793 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200794 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100795 }
Johannes Berg723b0382008-09-16 20:22:09 +0200796 }
797
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700798 if (!drv->ops->change_virtual_intf ||
Johannes Berg04a773a2009-04-19 21:24:32 +0200799 !(drv->wiphy.interface_modes & (1 << ntype))) {
Johannes Berg55682962007-09-20 13:09:35 -0400800 err = -EOPNOTSUPP;
801 goto unlock;
802 }
803
Johannes Berg92ffe052008-09-16 20:39:36 +0200804 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200805 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200806 err = -EINVAL;
807 goto unlock;
808 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100809 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
810 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100811 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100812 }
813
Johannes Berg92ffe052008-09-16 20:39:36 +0200814 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200815 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200816 err = -EINVAL;
817 goto unlock;
818 }
819 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
820 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100821 if (err)
822 goto unlock;
823
824 flags = &_flags;
825 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +0200826 }
Johannes Berg3b858752009-03-12 09:55:09 +0100827
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100828 if (change)
829 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
Johannes Berg04a773a2009-04-19 21:24:32 +0200830 ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100831 else
832 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +0200833
834 dev = __dev_get_by_index(&init_net, ifindex);
Johannes Berg04a773a2009-04-19 21:24:32 +0200835 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != ntype));
836
837 if (dev && !err && (ntype != otype)) {
838 if (otype == NL80211_IFTYPE_ADHOC)
Johannes Berg9d308422009-04-20 18:43:46 +0200839 cfg80211_clear_ibss(dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +0200840 }
Johannes Berg60719ff2008-09-16 14:55:09 +0200841
Johannes Berg55682962007-09-20 13:09:35 -0400842 unlock:
843 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100844 unlock_rtnl:
845 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400846 return err;
847}
848
849static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
850{
851 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100852 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400853 int err;
854 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100855 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400856
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100857 memset(&params, 0, sizeof(params));
858
Johannes Berg55682962007-09-20 13:09:35 -0400859 if (!info->attrs[NL80211_ATTR_IFNAME])
860 return -EINVAL;
861
862 if (info->attrs[NL80211_ATTR_IFTYPE]) {
863 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
864 if (type > NL80211_IFTYPE_MAX)
865 return -EINVAL;
866 }
867
Johannes Berg3b858752009-03-12 09:55:09 +0100868 rtnl_lock();
869
Johannes Berg55682962007-09-20 13:09:35 -0400870 drv = cfg80211_get_dev_from_info(info);
Johannes Berg3b858752009-03-12 09:55:09 +0100871 if (IS_ERR(drv)) {
872 err = PTR_ERR(drv);
873 goto unlock_rtnl;
874 }
Johannes Berg55682962007-09-20 13:09:35 -0400875
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700876 if (!drv->ops->add_virtual_intf ||
877 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400878 err = -EOPNOTSUPP;
879 goto unlock;
880 }
881
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100882 if (type == NL80211_IFTYPE_MESH_POINT &&
883 info->attrs[NL80211_ATTR_MESH_ID]) {
884 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
885 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
886 }
887
Michael Wu66f7ac52008-01-31 19:48:22 +0100888 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
889 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
890 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400891 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100892 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100893 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100894
Johannes Berg55682962007-09-20 13:09:35 -0400895 unlock:
896 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100897 unlock_rtnl:
898 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400899 return err;
900}
901
902static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
903{
904 struct cfg80211_registered_device *drv;
905 int ifindex, err;
906 struct net_device *dev;
907
Johannes Berg3b858752009-03-12 09:55:09 +0100908 rtnl_lock();
909
Johannes Bergbba95fe2008-07-29 13:22:51 +0200910 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400911 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100912 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -0400913 ifindex = dev->ifindex;
914 dev_put(dev);
915
916 if (!drv->ops->del_virtual_intf) {
917 err = -EOPNOTSUPP;
918 goto out;
919 }
920
Johannes Berg55682962007-09-20 13:09:35 -0400921 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -0400922
923 out:
924 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100925 unlock_rtnl:
926 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400927 return err;
928}
929
Johannes Berg41ade002007-12-19 02:03:29 +0100930struct get_key_cookie {
931 struct sk_buff *msg;
932 int error;
933};
934
935static void get_key_callback(void *c, struct key_params *params)
936{
937 struct get_key_cookie *cookie = c;
938
939 if (params->key)
940 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
941 params->key_len, params->key);
942
943 if (params->seq)
944 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
945 params->seq_len, params->seq);
946
947 if (params->cipher)
948 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
949 params->cipher);
950
951 return;
952 nla_put_failure:
953 cookie->error = 1;
954}
955
956static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
957{
958 struct cfg80211_registered_device *drv;
959 int err;
960 struct net_device *dev;
961 u8 key_idx = 0;
962 u8 *mac_addr = NULL;
963 struct get_key_cookie cookie = {
964 .error = 0,
965 };
966 void *hdr;
967 struct sk_buff *msg;
968
969 if (info->attrs[NL80211_ATTR_KEY_IDX])
970 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
971
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200972 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100973 return -EINVAL;
974
975 if (info->attrs[NL80211_ATTR_MAC])
976 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
977
Johannes Berg3b858752009-03-12 09:55:09 +0100978 rtnl_lock();
979
Johannes Bergbba95fe2008-07-29 13:22:51 +0200980 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100981 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100982 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100983
984 if (!drv->ops->get_key) {
985 err = -EOPNOTSUPP;
986 goto out;
987 }
988
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700989 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +0100990 if (!msg) {
991 err = -ENOMEM;
992 goto out;
993 }
994
995 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
996 NL80211_CMD_NEW_KEY);
997
998 if (IS_ERR(hdr)) {
999 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001000 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001001 }
1002
1003 cookie.msg = msg;
1004
1005 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1006 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1007 if (mac_addr)
1008 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1009
Johannes Berg41ade002007-12-19 02:03:29 +01001010 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
1011 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001012
1013 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001014 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001015
1016 if (cookie.error)
1017 goto nla_put_failure;
1018
1019 genlmsg_end(msg, hdr);
1020 err = genlmsg_unicast(msg, info->snd_pid);
1021 goto out;
1022
1023 nla_put_failure:
1024 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001025 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001026 nlmsg_free(msg);
1027 out:
1028 cfg80211_put_dev(drv);
1029 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001030 unlock_rtnl:
1031 rtnl_unlock();
1032
Johannes Berg41ade002007-12-19 02:03:29 +01001033 return err;
1034}
1035
1036static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1037{
1038 struct cfg80211_registered_device *drv;
1039 int err;
1040 struct net_device *dev;
1041 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001042 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1043 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001044
1045 if (!info->attrs[NL80211_ATTR_KEY_IDX])
1046 return -EINVAL;
1047
1048 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1049
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001050 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
1051 if (key_idx < 4 || key_idx > 5)
1052 return -EINVAL;
1053 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +01001054 return -EINVAL;
1055
1056 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001057 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
1058 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +01001059 return -EINVAL;
1060
Johannes Berg3b858752009-03-12 09:55:09 +01001061 rtnl_lock();
1062
Johannes Bergbba95fe2008-07-29 13:22:51 +02001063 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001064 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001065 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001066
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001067 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
1068 func = drv->ops->set_default_key;
1069 else
1070 func = drv->ops->set_default_mgmt_key;
1071
1072 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001073 err = -EOPNOTSUPP;
1074 goto out;
1075 }
1076
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001077 err = func(&drv->wiphy, dev, key_idx);
Johannes Berg08645122009-05-11 13:54:58 +02001078#ifdef CONFIG_WIRELESS_EXT
1079 if (!err) {
1080 if (func == drv->ops->set_default_key)
1081 dev->ieee80211_ptr->wext.default_key = key_idx;
1082 else
1083 dev->ieee80211_ptr->wext.default_mgmt_key = key_idx;
1084 }
1085#endif
Johannes Berg41ade002007-12-19 02:03:29 +01001086
1087 out:
1088 cfg80211_put_dev(drv);
1089 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001090
1091 unlock_rtnl:
1092 rtnl_unlock();
1093
Johannes Berg41ade002007-12-19 02:03:29 +01001094 return err;
1095}
1096
1097static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1098{
1099 struct cfg80211_registered_device *drv;
Johannes Berg25e47c182009-04-02 20:14:06 +02001100 int err, i;
Johannes Berg41ade002007-12-19 02:03:29 +01001101 struct net_device *dev;
1102 struct key_params params;
1103 u8 key_idx = 0;
1104 u8 *mac_addr = NULL;
1105
1106 memset(&params, 0, sizeof(params));
1107
1108 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
1109 return -EINVAL;
1110
1111 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1112 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1113 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1114 }
1115
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +03001116 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1117 params.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1118 params.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1119 }
1120
Johannes Berg41ade002007-12-19 02:03:29 +01001121 if (info->attrs[NL80211_ATTR_KEY_IDX])
1122 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1123
1124 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1125
1126 if (info->attrs[NL80211_ATTR_MAC])
1127 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1128
Johannes Berg08645122009-05-11 13:54:58 +02001129 if (cfg80211_validate_key_settings(&params, key_idx, mac_addr))
Johannes Berg41ade002007-12-19 02:03:29 +01001130 return -EINVAL;
1131
Johannes Berg3b858752009-03-12 09:55:09 +01001132 rtnl_lock();
1133
Johannes Bergbba95fe2008-07-29 13:22:51 +02001134 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001135 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001136 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001137
Johannes Berg25e47c182009-04-02 20:14:06 +02001138 for (i = 0; i < drv->wiphy.n_cipher_suites; i++)
1139 if (params.cipher == drv->wiphy.cipher_suites[i])
1140 break;
1141 if (i == drv->wiphy.n_cipher_suites) {
1142 err = -EINVAL;
1143 goto out;
1144 }
1145
Johannes Berg41ade002007-12-19 02:03:29 +01001146 if (!drv->ops->add_key) {
1147 err = -EOPNOTSUPP;
1148 goto out;
1149 }
1150
Johannes Berg41ade002007-12-19 02:03:29 +01001151 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
Johannes Berg41ade002007-12-19 02:03:29 +01001152
1153 out:
1154 cfg80211_put_dev(drv);
1155 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001156 unlock_rtnl:
1157 rtnl_unlock();
1158
Johannes Berg41ade002007-12-19 02:03:29 +01001159 return err;
1160}
1161
1162static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1163{
1164 struct cfg80211_registered_device *drv;
1165 int err;
1166 struct net_device *dev;
1167 u8 key_idx = 0;
1168 u8 *mac_addr = NULL;
1169
1170 if (info->attrs[NL80211_ATTR_KEY_IDX])
1171 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1172
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001173 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001174 return -EINVAL;
1175
1176 if (info->attrs[NL80211_ATTR_MAC])
1177 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1178
Johannes Berg3b858752009-03-12 09:55:09 +01001179 rtnl_lock();
1180
Johannes Bergbba95fe2008-07-29 13:22:51 +02001181 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001182 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001183 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001184
1185 if (!drv->ops->del_key) {
1186 err = -EOPNOTSUPP;
1187 goto out;
1188 }
1189
Johannes Berg41ade002007-12-19 02:03:29 +01001190 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001191
Johannes Berg08645122009-05-11 13:54:58 +02001192#ifdef CONFIG_WIRELESS_EXT
1193 if (!err) {
1194 if (key_idx == dev->ieee80211_ptr->wext.default_key)
1195 dev->ieee80211_ptr->wext.default_key = -1;
1196 else if (key_idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1197 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1198 }
1199#endif
1200
Johannes Berg41ade002007-12-19 02:03:29 +01001201 out:
1202 cfg80211_put_dev(drv);
1203 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001204
1205 unlock_rtnl:
1206 rtnl_unlock();
1207
Johannes Berg41ade002007-12-19 02:03:29 +01001208 return err;
1209}
1210
Johannes Berged1b6cc2007-12-19 02:03:32 +01001211static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1212{
1213 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1214 struct beacon_parameters *info);
1215 struct cfg80211_registered_device *drv;
1216 int err;
1217 struct net_device *dev;
1218 struct beacon_parameters params;
1219 int haveinfo = 0;
1220
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001221 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1222 return -EINVAL;
1223
Johannes Berg3b858752009-03-12 09:55:09 +01001224 rtnl_lock();
1225
Johannes Bergbba95fe2008-07-29 13:22:51 +02001226 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001227 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001228 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001229
Jouni Malineneec60b02009-03-20 21:21:19 +02001230 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1231 err = -EOPNOTSUPP;
1232 goto out;
1233 }
1234
Johannes Berged1b6cc2007-12-19 02:03:32 +01001235 switch (info->genlhdr->cmd) {
1236 case NL80211_CMD_NEW_BEACON:
1237 /* these are required for NEW_BEACON */
1238 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1239 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1240 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1241 err = -EINVAL;
1242 goto out;
1243 }
1244
1245 call = drv->ops->add_beacon;
1246 break;
1247 case NL80211_CMD_SET_BEACON:
1248 call = drv->ops->set_beacon;
1249 break;
1250 default:
1251 WARN_ON(1);
1252 err = -EOPNOTSUPP;
1253 goto out;
1254 }
1255
1256 if (!call) {
1257 err = -EOPNOTSUPP;
1258 goto out;
1259 }
1260
1261 memset(&params, 0, sizeof(params));
1262
1263 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1264 params.interval =
1265 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1266 haveinfo = 1;
1267 }
1268
1269 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1270 params.dtim_period =
1271 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1272 haveinfo = 1;
1273 }
1274
1275 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1276 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1277 params.head_len =
1278 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1279 haveinfo = 1;
1280 }
1281
1282 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1283 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1284 params.tail_len =
1285 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1286 haveinfo = 1;
1287 }
1288
1289 if (!haveinfo) {
1290 err = -EINVAL;
1291 goto out;
1292 }
1293
Johannes Berged1b6cc2007-12-19 02:03:32 +01001294 err = call(&drv->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001295
1296 out:
1297 cfg80211_put_dev(drv);
1298 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001299 unlock_rtnl:
1300 rtnl_unlock();
1301
Johannes Berged1b6cc2007-12-19 02:03:32 +01001302 return err;
1303}
1304
1305static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1306{
1307 struct cfg80211_registered_device *drv;
1308 int err;
1309 struct net_device *dev;
1310
Johannes Berg3b858752009-03-12 09:55:09 +01001311 rtnl_lock();
1312
Johannes Bergbba95fe2008-07-29 13:22:51 +02001313 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001314 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001315 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001316
1317 if (!drv->ops->del_beacon) {
1318 err = -EOPNOTSUPP;
1319 goto out;
1320 }
1321
Jouni Malineneec60b02009-03-20 21:21:19 +02001322 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1323 err = -EOPNOTSUPP;
1324 goto out;
1325 }
Johannes Berged1b6cc2007-12-19 02:03:32 +01001326 err = drv->ops->del_beacon(&drv->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001327
1328 out:
1329 cfg80211_put_dev(drv);
1330 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001331 unlock_rtnl:
1332 rtnl_unlock();
1333
Johannes Berged1b6cc2007-12-19 02:03:32 +01001334 return err;
1335}
1336
Johannes Berg5727ef12007-12-19 02:03:34 +01001337static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1338 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1339 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1340 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001341 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001342};
1343
Johannes Bergeccb8e82009-05-11 21:57:56 +03001344static int parse_station_flags(struct genl_info *info,
1345 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001346{
1347 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001348 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001349 int flag;
1350
Johannes Bergeccb8e82009-05-11 21:57:56 +03001351 /*
1352 * Try parsing the new attribute first so userspace
1353 * can specify both for older kernels.
1354 */
1355 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1356 if (nla) {
1357 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001358
Johannes Bergeccb8e82009-05-11 21:57:56 +03001359 sta_flags = nla_data(nla);
1360 params->sta_flags_mask = sta_flags->mask;
1361 params->sta_flags_set = sta_flags->set;
1362 if ((params->sta_flags_mask |
1363 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1364 return -EINVAL;
1365 return 0;
1366 }
1367
1368 /* if present, parse the old attribute */
1369
1370 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001371 if (!nla)
1372 return 0;
1373
1374 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1375 nla, sta_flags_policy))
1376 return -EINVAL;
1377
Johannes Bergeccb8e82009-05-11 21:57:56 +03001378 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1379 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001380
1381 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1382 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001383 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001384
1385 return 0;
1386}
1387
Henning Rogge420e7fa2008-12-11 22:04:19 +01001388static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1389{
1390 int modulation, streams, bitrate;
1391
1392 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1393 return rate->legacy;
1394
1395 /* the formula below does only work for MCS values smaller than 32 */
1396 if (rate->mcs >= 32)
1397 return 0;
1398
1399 modulation = rate->mcs & 7;
1400 streams = (rate->mcs >> 3) + 1;
1401
1402 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1403 13500000 : 6500000;
1404
1405 if (modulation < 4)
1406 bitrate *= (modulation + 1);
1407 else if (modulation == 4)
1408 bitrate *= (modulation + 2);
1409 else
1410 bitrate *= (modulation + 3);
1411
1412 bitrate *= streams;
1413
1414 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1415 bitrate = (bitrate / 9) * 10;
1416
1417 /* do NOT round down here */
1418 return (bitrate + 50000) / 100000;
1419}
1420
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001421static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1422 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001423 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001424{
1425 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001426 struct nlattr *sinfoattr, *txrate;
1427 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001428
1429 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1430 if (!hdr)
1431 return -1;
1432
1433 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1434 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1435
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001436 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1437 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001438 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001439 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1440 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1441 sinfo->inactive_time);
1442 if (sinfo->filled & STATION_INFO_RX_BYTES)
1443 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1444 sinfo->rx_bytes);
1445 if (sinfo->filled & STATION_INFO_TX_BYTES)
1446 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1447 sinfo->tx_bytes);
1448 if (sinfo->filled & STATION_INFO_LLID)
1449 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1450 sinfo->llid);
1451 if (sinfo->filled & STATION_INFO_PLID)
1452 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1453 sinfo->plid);
1454 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1455 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1456 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001457 if (sinfo->filled & STATION_INFO_SIGNAL)
1458 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1459 sinfo->signal);
1460 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1461 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1462 if (!txrate)
1463 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001464
Henning Rogge420e7fa2008-12-11 22:04:19 +01001465 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1466 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1467 if (bitrate > 0)
1468 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1469
1470 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1471 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1472 sinfo->txrate.mcs);
1473 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1474 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1475 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1476 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1477
1478 nla_nest_end(msg, txrate);
1479 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001480 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1481 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1482 sinfo->rx_packets);
1483 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1484 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1485 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001486 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001487
1488 return genlmsg_end(msg, hdr);
1489
1490 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001491 genlmsg_cancel(msg, hdr);
1492 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001493}
1494
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001495static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001496 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001497{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001498 struct station_info sinfo;
1499 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001500 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001501 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001502 int ifidx = cb->args[0];
1503 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001504 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001505
Johannes Bergbba95fe2008-07-29 13:22:51 +02001506 if (!ifidx) {
1507 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1508 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1509 nl80211_policy);
1510 if (err)
1511 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001512
Johannes Bergbba95fe2008-07-29 13:22:51 +02001513 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1514 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001515
Johannes Bergbba95fe2008-07-29 13:22:51 +02001516 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1517 if (!ifidx)
1518 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001519 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001520
Johannes Berg3b858752009-03-12 09:55:09 +01001521 rtnl_lock();
1522
1523 netdev = __dev_get_by_index(&init_net, ifidx);
1524 if (!netdev) {
1525 err = -ENODEV;
1526 goto out_rtnl;
1527 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001528
Johannes Bergbba95fe2008-07-29 13:22:51 +02001529 dev = cfg80211_get_dev_from_ifindex(ifidx);
1530 if (IS_ERR(dev)) {
1531 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001532 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001533 }
1534
1535 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001536 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001537 goto out_err;
1538 }
1539
Johannes Bergbba95fe2008-07-29 13:22:51 +02001540 while (1) {
1541 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1542 mac_addr, &sinfo);
1543 if (err == -ENOENT)
1544 break;
1545 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001546 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001547
1548 if (nl80211_send_station(skb,
1549 NETLINK_CB(cb->skb).pid,
1550 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1551 netdev, mac_addr,
1552 &sinfo) < 0)
1553 goto out;
1554
1555 sta_idx++;
1556 }
1557
1558
1559 out:
1560 cb->args[1] = sta_idx;
1561 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001562 out_err:
1563 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001564 out_rtnl:
1565 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001566
1567 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001568}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001569
Johannes Berg5727ef12007-12-19 02:03:34 +01001570static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1571{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001572 struct cfg80211_registered_device *drv;
1573 int err;
1574 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001575 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001576 struct sk_buff *msg;
1577 u8 *mac_addr = NULL;
1578
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001579 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001580
1581 if (!info->attrs[NL80211_ATTR_MAC])
1582 return -EINVAL;
1583
1584 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1585
Johannes Berg3b858752009-03-12 09:55:09 +01001586 rtnl_lock();
1587
Johannes Bergbba95fe2008-07-29 13:22:51 +02001588 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001589 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001590 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001591
1592 if (!drv->ops->get_station) {
1593 err = -EOPNOTSUPP;
1594 goto out;
1595 }
1596
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001597 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001598 if (err)
1599 goto out;
1600
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001602 if (!msg)
1603 goto out;
1604
1605 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001606 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001607 goto out_free;
1608
1609 err = genlmsg_unicast(msg, info->snd_pid);
1610 goto out;
1611
1612 out_free:
1613 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001614 out:
1615 cfg80211_put_dev(drv);
1616 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001617 out_rtnl:
1618 rtnl_unlock();
1619
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001620 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001621}
1622
1623/*
1624 * Get vlan interface making sure it is on the right wiphy.
1625 */
1626static int get_vlan(struct nlattr *vlanattr,
1627 struct cfg80211_registered_device *rdev,
1628 struct net_device **vlan)
1629{
1630 *vlan = NULL;
1631
1632 if (vlanattr) {
1633 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1634 if (!*vlan)
1635 return -ENODEV;
1636 if (!(*vlan)->ieee80211_ptr)
1637 return -EINVAL;
1638 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1639 return -EINVAL;
1640 }
1641 return 0;
1642}
1643
1644static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1645{
1646 struct cfg80211_registered_device *drv;
1647 int err;
1648 struct net_device *dev;
1649 struct station_parameters params;
1650 u8 *mac_addr = NULL;
1651
1652 memset(&params, 0, sizeof(params));
1653
1654 params.listen_interval = -1;
1655
1656 if (info->attrs[NL80211_ATTR_STA_AID])
1657 return -EINVAL;
1658
1659 if (!info->attrs[NL80211_ATTR_MAC])
1660 return -EINVAL;
1661
1662 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1663
1664 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1665 params.supported_rates =
1666 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1667 params.supported_rates_len =
1668 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1669 }
1670
1671 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1672 params.listen_interval =
1673 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1674
Jouni Malinen36aedc92008-08-25 11:58:58 +03001675 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1676 params.ht_capa =
1677 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1678
Johannes Bergeccb8e82009-05-11 21:57:56 +03001679 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001680 return -EINVAL;
1681
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001682 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1683 params.plink_action =
1684 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1685
Johannes Berg3b858752009-03-12 09:55:09 +01001686 rtnl_lock();
1687
Johannes Bergbba95fe2008-07-29 13:22:51 +02001688 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001689 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001690 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001691
Johannes Berga97f4422009-06-18 17:23:43 +02001692 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1693 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001694 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001695
1696 /* validate settings */
1697 err = 0;
1698
1699 switch (dev->ieee80211_ptr->iftype) {
1700 case NL80211_IFTYPE_AP:
1701 case NL80211_IFTYPE_AP_VLAN:
1702 /* disallow mesh-specific things */
1703 if (params.plink_action)
1704 err = -EINVAL;
1705 break;
1706 case NL80211_IFTYPE_STATION:
1707 /* disallow everything but AUTHORIZED flag */
1708 if (params.plink_action)
1709 err = -EINVAL;
1710 if (params.vlan)
1711 err = -EINVAL;
1712 if (params.supported_rates)
1713 err = -EINVAL;
1714 if (params.ht_capa)
1715 err = -EINVAL;
1716 if (params.listen_interval >= 0)
1717 err = -EINVAL;
1718 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1719 err = -EINVAL;
1720 break;
1721 case NL80211_IFTYPE_MESH_POINT:
1722 /* disallow things mesh doesn't support */
1723 if (params.vlan)
1724 err = -EINVAL;
1725 if (params.ht_capa)
1726 err = -EINVAL;
1727 if (params.listen_interval >= 0)
1728 err = -EINVAL;
1729 if (params.supported_rates)
1730 err = -EINVAL;
1731 if (params.sta_flags_mask)
1732 err = -EINVAL;
1733 break;
1734 default:
1735 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001736 }
1737
Johannes Berg5727ef12007-12-19 02:03:34 +01001738 if (err)
1739 goto out;
1740
1741 if (!drv->ops->change_station) {
1742 err = -EOPNOTSUPP;
1743 goto out;
1744 }
1745
Johannes Berg5727ef12007-12-19 02:03:34 +01001746 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001747
1748 out:
1749 if (params.vlan)
1750 dev_put(params.vlan);
1751 cfg80211_put_dev(drv);
1752 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001753 out_rtnl:
1754 rtnl_unlock();
1755
Johannes Berg5727ef12007-12-19 02:03:34 +01001756 return err;
1757}
1758
1759static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1760{
1761 struct cfg80211_registered_device *drv;
1762 int err;
1763 struct net_device *dev;
1764 struct station_parameters params;
1765 u8 *mac_addr = NULL;
1766
1767 memset(&params, 0, sizeof(params));
1768
1769 if (!info->attrs[NL80211_ATTR_MAC])
1770 return -EINVAL;
1771
Johannes Berg5727ef12007-12-19 02:03:34 +01001772 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1773 return -EINVAL;
1774
1775 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1776 return -EINVAL;
1777
1778 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1779 params.supported_rates =
1780 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1781 params.supported_rates_len =
1782 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1783 params.listen_interval =
1784 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001785
Johannes Berga97f4422009-06-18 17:23:43 +02001786 if (info->attrs[NL80211_ATTR_STA_AID]) {
1787 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1788 if (!params.aid || params.aid > IEEE80211_MAX_AID)
1789 return -EINVAL;
1790 }
Johannes Berg51b50fb2009-05-24 16:42:30 +02001791
Jouni Malinen36aedc92008-08-25 11:58:58 +03001792 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1793 params.ht_capa =
1794 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001795
Johannes Bergeccb8e82009-05-11 21:57:56 +03001796 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001797 return -EINVAL;
1798
Johannes Berg3b858752009-03-12 09:55:09 +01001799 rtnl_lock();
1800
Johannes Bergbba95fe2008-07-29 13:22:51 +02001801 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001802 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001803 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001804
Johannes Berga97f4422009-06-18 17:23:43 +02001805 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1806 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02001807 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001808
1809 /* validate settings */
1810 err = 0;
1811
1812 switch (dev->ieee80211_ptr->iftype) {
1813 case NL80211_IFTYPE_AP:
1814 case NL80211_IFTYPE_AP_VLAN:
1815 /* all ok but must have AID */
1816 if (!params.aid)
1817 err = -EINVAL;
1818 break;
1819 case NL80211_IFTYPE_MESH_POINT:
1820 /* disallow things mesh doesn't support */
1821 if (params.vlan)
1822 err = -EINVAL;
1823 if (params.aid)
1824 err = -EINVAL;
1825 if (params.ht_capa)
1826 err = -EINVAL;
1827 if (params.listen_interval >= 0)
1828 err = -EINVAL;
1829 if (params.supported_rates)
1830 err = -EINVAL;
1831 if (params.sta_flags_mask)
1832 err = -EINVAL;
1833 break;
1834 default:
1835 err = -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02001836 }
1837
Johannes Berg5727ef12007-12-19 02:03:34 +01001838 if (err)
1839 goto out;
1840
1841 if (!drv->ops->add_station) {
1842 err = -EOPNOTSUPP;
1843 goto out;
1844 }
1845
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001846 if (!netif_running(dev)) {
1847 err = -ENETDOWN;
1848 goto out;
1849 }
1850
Johannes Berg5727ef12007-12-19 02:03:34 +01001851 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001852
1853 out:
1854 if (params.vlan)
1855 dev_put(params.vlan);
1856 cfg80211_put_dev(drv);
1857 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001858 out_rtnl:
1859 rtnl_unlock();
1860
Johannes Berg5727ef12007-12-19 02:03:34 +01001861 return err;
1862}
1863
1864static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1865{
1866 struct cfg80211_registered_device *drv;
1867 int err;
1868 struct net_device *dev;
1869 u8 *mac_addr = NULL;
1870
1871 if (info->attrs[NL80211_ATTR_MAC])
1872 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1873
Johannes Berg3b858752009-03-12 09:55:09 +01001874 rtnl_lock();
1875
Johannes Bergbba95fe2008-07-29 13:22:51 +02001876 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001877 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001878 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001879
Johannes Berge80cf852009-05-11 14:43:13 +02001880 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Andrey Yurovsky155cc9e2009-06-16 11:31:04 -07001881 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
1882 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02001883 err = -EINVAL;
1884 goto out;
1885 }
1886
Johannes Berg5727ef12007-12-19 02:03:34 +01001887 if (!drv->ops->del_station) {
1888 err = -EOPNOTSUPP;
1889 goto out;
1890 }
1891
Johannes Berg5727ef12007-12-19 02:03:34 +01001892 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01001893
1894 out:
1895 cfg80211_put_dev(drv);
1896 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001897 out_rtnl:
1898 rtnl_unlock();
1899
Johannes Berg5727ef12007-12-19 02:03:34 +01001900 return err;
1901}
1902
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001903static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1904 int flags, struct net_device *dev,
1905 u8 *dst, u8 *next_hop,
1906 struct mpath_info *pinfo)
1907{
1908 void *hdr;
1909 struct nlattr *pinfoattr;
1910
1911 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1912 if (!hdr)
1913 return -1;
1914
1915 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1916 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1917 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1918
1919 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1920 if (!pinfoattr)
1921 goto nla_put_failure;
1922 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1923 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1924 pinfo->frame_qlen);
1925 if (pinfo->filled & MPATH_INFO_DSN)
1926 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1927 pinfo->dsn);
1928 if (pinfo->filled & MPATH_INFO_METRIC)
1929 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1930 pinfo->metric);
1931 if (pinfo->filled & MPATH_INFO_EXPTIME)
1932 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1933 pinfo->exptime);
1934 if (pinfo->filled & MPATH_INFO_FLAGS)
1935 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1936 pinfo->flags);
1937 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1938 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1939 pinfo->discovery_timeout);
1940 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1941 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1942 pinfo->discovery_retries);
1943
1944 nla_nest_end(msg, pinfoattr);
1945
1946 return genlmsg_end(msg, hdr);
1947
1948 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001949 genlmsg_cancel(msg, hdr);
1950 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001951}
1952
1953static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001954 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001955{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001956 struct mpath_info pinfo;
1957 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001958 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001959 u8 dst[ETH_ALEN];
1960 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001961 int ifidx = cb->args[0];
1962 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001963 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001964
Johannes Bergbba95fe2008-07-29 13:22:51 +02001965 if (!ifidx) {
1966 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1967 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1968 nl80211_policy);
1969 if (err)
1970 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001971
Johannes Bergbba95fe2008-07-29 13:22:51 +02001972 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1973 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001974
Johannes Bergbba95fe2008-07-29 13:22:51 +02001975 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1976 if (!ifidx)
1977 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001978 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001979
Johannes Berg3b858752009-03-12 09:55:09 +01001980 rtnl_lock();
1981
1982 netdev = __dev_get_by_index(&init_net, ifidx);
1983 if (!netdev) {
1984 err = -ENODEV;
1985 goto out_rtnl;
1986 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001987
Johannes Bergbba95fe2008-07-29 13:22:51 +02001988 dev = cfg80211_get_dev_from_ifindex(ifidx);
1989 if (IS_ERR(dev)) {
1990 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001991 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001992 }
1993
1994 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001995 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001996 goto out_err;
1997 }
1998
Jouni Malineneec60b02009-03-20 21:21:19 +02001999 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2000 err = -EOPNOTSUPP;
2001 goto out;
2002 }
2003
Johannes Bergbba95fe2008-07-29 13:22:51 +02002004 while (1) {
2005 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2006 dst, next_hop, &pinfo);
2007 if (err == -ENOENT)
2008 break;
2009 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002010 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002011
2012 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2013 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2014 netdev, dst, next_hop,
2015 &pinfo) < 0)
2016 goto out;
2017
2018 path_idx++;
2019 }
2020
2021
2022 out:
2023 cb->args[1] = path_idx;
2024 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002025 out_err:
2026 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002027 out_rtnl:
2028 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002029
2030 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002031}
2032
2033static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2034{
2035 struct cfg80211_registered_device *drv;
2036 int err;
2037 struct net_device *dev;
2038 struct mpath_info pinfo;
2039 struct sk_buff *msg;
2040 u8 *dst = NULL;
2041 u8 next_hop[ETH_ALEN];
2042
2043 memset(&pinfo, 0, sizeof(pinfo));
2044
2045 if (!info->attrs[NL80211_ATTR_MAC])
2046 return -EINVAL;
2047
2048 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2049
Johannes Berg3b858752009-03-12 09:55:09 +01002050 rtnl_lock();
2051
Johannes Bergbba95fe2008-07-29 13:22:51 +02002052 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002053 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002054 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002055
2056 if (!drv->ops->get_mpath) {
2057 err = -EOPNOTSUPP;
2058 goto out;
2059 }
2060
Jouni Malineneec60b02009-03-20 21:21:19 +02002061 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2062 err = -EOPNOTSUPP;
2063 goto out;
2064 }
2065
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002066 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002067 if (err)
2068 goto out;
2069
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002070 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002071 if (!msg)
2072 goto out;
2073
2074 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2075 dev, dst, next_hop, &pinfo) < 0)
2076 goto out_free;
2077
2078 err = genlmsg_unicast(msg, info->snd_pid);
2079 goto out;
2080
2081 out_free:
2082 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002083 out:
2084 cfg80211_put_dev(drv);
2085 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002086 out_rtnl:
2087 rtnl_unlock();
2088
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002089 return err;
2090}
2091
2092static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2093{
2094 struct cfg80211_registered_device *drv;
2095 int err;
2096 struct net_device *dev;
2097 u8 *dst = NULL;
2098 u8 *next_hop = NULL;
2099
2100 if (!info->attrs[NL80211_ATTR_MAC])
2101 return -EINVAL;
2102
2103 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2104 return -EINVAL;
2105
2106 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2107 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2108
Johannes Berg3b858752009-03-12 09:55:09 +01002109 rtnl_lock();
2110
Johannes Bergbba95fe2008-07-29 13:22:51 +02002111 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002112 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002113 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002114
2115 if (!drv->ops->change_mpath) {
2116 err = -EOPNOTSUPP;
2117 goto out;
2118 }
2119
Jouni Malineneec60b02009-03-20 21:21:19 +02002120 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2121 err = -EOPNOTSUPP;
2122 goto out;
2123 }
2124
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002125 if (!netif_running(dev)) {
2126 err = -ENETDOWN;
2127 goto out;
2128 }
2129
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002130 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002131
2132 out:
2133 cfg80211_put_dev(drv);
2134 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002135 out_rtnl:
2136 rtnl_unlock();
2137
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002138 return err;
2139}
2140static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2141{
2142 struct cfg80211_registered_device *drv;
2143 int err;
2144 struct net_device *dev;
2145 u8 *dst = NULL;
2146 u8 *next_hop = NULL;
2147
2148 if (!info->attrs[NL80211_ATTR_MAC])
2149 return -EINVAL;
2150
2151 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2152 return -EINVAL;
2153
2154 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2155 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2156
Johannes Berg3b858752009-03-12 09:55:09 +01002157 rtnl_lock();
2158
Johannes Bergbba95fe2008-07-29 13:22:51 +02002159 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002160 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002161 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002162
2163 if (!drv->ops->add_mpath) {
2164 err = -EOPNOTSUPP;
2165 goto out;
2166 }
2167
Jouni Malineneec60b02009-03-20 21:21:19 +02002168 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2169 err = -EOPNOTSUPP;
2170 goto out;
2171 }
2172
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002173 if (!netif_running(dev)) {
2174 err = -ENETDOWN;
2175 goto out;
2176 }
2177
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002178 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002179
2180 out:
2181 cfg80211_put_dev(drv);
2182 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002183 out_rtnl:
2184 rtnl_unlock();
2185
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002186 return err;
2187}
2188
2189static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2190{
2191 struct cfg80211_registered_device *drv;
2192 int err;
2193 struct net_device *dev;
2194 u8 *dst = NULL;
2195
2196 if (info->attrs[NL80211_ATTR_MAC])
2197 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2198
Johannes Berg3b858752009-03-12 09:55:09 +01002199 rtnl_lock();
2200
Johannes Bergbba95fe2008-07-29 13:22:51 +02002201 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002202 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002203 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002204
2205 if (!drv->ops->del_mpath) {
2206 err = -EOPNOTSUPP;
2207 goto out;
2208 }
2209
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002210 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002211
2212 out:
2213 cfg80211_put_dev(drv);
2214 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002215 out_rtnl:
2216 rtnl_unlock();
2217
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002218 return err;
2219}
2220
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002221static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2222{
2223 struct cfg80211_registered_device *drv;
2224 int err;
2225 struct net_device *dev;
2226 struct bss_parameters params;
2227
2228 memset(&params, 0, sizeof(params));
2229 /* default to not changing parameters */
2230 params.use_cts_prot = -1;
2231 params.use_short_preamble = -1;
2232 params.use_short_slot_time = -1;
2233
2234 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2235 params.use_cts_prot =
2236 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2237 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2238 params.use_short_preamble =
2239 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2240 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2241 params.use_short_slot_time =
2242 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002243 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2244 params.basic_rates =
2245 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2246 params.basic_rates_len =
2247 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2248 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002249
Johannes Berg3b858752009-03-12 09:55:09 +01002250 rtnl_lock();
2251
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002252 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2253 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002254 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002255
2256 if (!drv->ops->change_bss) {
2257 err = -EOPNOTSUPP;
2258 goto out;
2259 }
2260
Jouni Malineneec60b02009-03-20 21:21:19 +02002261 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2262 err = -EOPNOTSUPP;
2263 goto out;
2264 }
2265
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002266 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002267
2268 out:
2269 cfg80211_put_dev(drv);
2270 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002271 out_rtnl:
2272 rtnl_unlock();
2273
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002274 return err;
2275}
2276
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002277static const struct nla_policy
2278 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2279 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2280 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2281 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2282 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2283 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2284 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2285};
2286
2287static int parse_reg_rule(struct nlattr *tb[],
2288 struct ieee80211_reg_rule *reg_rule)
2289{
2290 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2291 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2292
2293 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2294 return -EINVAL;
2295 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2296 return -EINVAL;
2297 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2298 return -EINVAL;
2299 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2300 return -EINVAL;
2301 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2302 return -EINVAL;
2303
2304 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2305
2306 freq_range->start_freq_khz =
2307 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2308 freq_range->end_freq_khz =
2309 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2310 freq_range->max_bandwidth_khz =
2311 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2312
2313 power_rule->max_eirp =
2314 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2315
2316 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2317 power_rule->max_antenna_gain =
2318 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2319
2320 return 0;
2321}
2322
2323static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2324{
2325 int r;
2326 char *data = NULL;
2327
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002328 /*
2329 * You should only get this when cfg80211 hasn't yet initialized
2330 * completely when built-in to the kernel right between the time
2331 * window between nl80211_init() and regulatory_init(), if that is
2332 * even possible.
2333 */
2334 mutex_lock(&cfg80211_mutex);
2335 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002336 mutex_unlock(&cfg80211_mutex);
2337 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002338 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002339 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002340
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002341 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2342 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002343
2344 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2345
2346#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2347 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002348 if (is_world_regdom(data))
2349 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002350#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002351
2352 r = regulatory_hint_user(data);
2353
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002354 return r;
2355}
2356
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002357static int nl80211_get_mesh_params(struct sk_buff *skb,
2358 struct genl_info *info)
2359{
2360 struct cfg80211_registered_device *drv;
2361 struct mesh_config cur_params;
2362 int err;
2363 struct net_device *dev;
2364 void *hdr;
2365 struct nlattr *pinfoattr;
2366 struct sk_buff *msg;
2367
Johannes Berg3b858752009-03-12 09:55:09 +01002368 rtnl_lock();
2369
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002370 /* Look up our device */
2371 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2372 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002373 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002374
Jouni Malinenf3f92582009-03-20 17:57:36 +02002375 if (!drv->ops->get_mesh_params) {
2376 err = -EOPNOTSUPP;
2377 goto out;
2378 }
2379
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002380 /* Get the mesh params */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002381 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002382 if (err)
2383 goto out;
2384
2385 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002386 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002387 if (!msg) {
2388 err = -ENOBUFS;
2389 goto out;
2390 }
2391 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2392 NL80211_CMD_GET_MESH_PARAMS);
2393 if (!hdr)
2394 goto nla_put_failure;
2395 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2396 if (!pinfoattr)
2397 goto nla_put_failure;
2398 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2399 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2400 cur_params.dot11MeshRetryTimeout);
2401 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2402 cur_params.dot11MeshConfirmTimeout);
2403 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2404 cur_params.dot11MeshHoldingTimeout);
2405 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2406 cur_params.dot11MeshMaxPeerLinks);
2407 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2408 cur_params.dot11MeshMaxRetries);
2409 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2410 cur_params.dot11MeshTTL);
2411 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2412 cur_params.auto_open_plinks);
2413 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2414 cur_params.dot11MeshHWMPmaxPREQretries);
2415 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2416 cur_params.path_refresh_time);
2417 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2418 cur_params.min_discovery_timeout);
2419 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2420 cur_params.dot11MeshHWMPactivePathTimeout);
2421 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2422 cur_params.dot11MeshHWMPpreqMinInterval);
2423 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2424 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2425 nla_nest_end(msg, pinfoattr);
2426 genlmsg_end(msg, hdr);
2427 err = genlmsg_unicast(msg, info->snd_pid);
2428 goto out;
2429
Johannes Berg3b858752009-03-12 09:55:09 +01002430 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002431 genlmsg_cancel(msg, hdr);
2432 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002433 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002434 /* Cleanup */
2435 cfg80211_put_dev(drv);
2436 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002437 out_rtnl:
2438 rtnl_unlock();
2439
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002440 return err;
2441}
2442
2443#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2444do {\
2445 if (table[attr_num]) {\
2446 cfg.param = nla_fn(table[attr_num]); \
2447 mask |= (1 << (attr_num - 1)); \
2448 } \
2449} while (0);\
2450
2451static struct nla_policy
2452nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2453 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2454 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2455 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2456 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2457 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2458 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2459 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2460
2461 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2462 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2463 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2464 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2465 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2466 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2467};
2468
2469static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2470{
2471 int err;
2472 u32 mask;
2473 struct cfg80211_registered_device *drv;
2474 struct net_device *dev;
2475 struct mesh_config cfg;
2476 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2477 struct nlattr *parent_attr;
2478
2479 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2480 if (!parent_attr)
2481 return -EINVAL;
2482 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2483 parent_attr, nl80211_meshconf_params_policy))
2484 return -EINVAL;
2485
Johannes Berg3b858752009-03-12 09:55:09 +01002486 rtnl_lock();
2487
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002488 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2489 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002490 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002491
Jouni Malinenf3f92582009-03-20 17:57:36 +02002492 if (!drv->ops->set_mesh_params) {
2493 err = -EOPNOTSUPP;
2494 goto out;
2495 }
2496
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002497 /* This makes sure that there aren't more than 32 mesh config
2498 * parameters (otherwise our bitfield scheme would not work.) */
2499 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2500
2501 /* Fill in the params struct */
2502 mask = 0;
2503 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2504 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2505 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2506 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2507 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2508 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2509 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2510 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2511 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2512 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2513 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2514 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2515 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2516 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2517 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2518 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2519 nla_get_u8);
2520 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2521 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2522 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2523 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2524 nla_get_u16);
2525 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2526 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2527 nla_get_u32);
2528 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2529 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2530 nla_get_u16);
2531 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2532 dot11MeshHWMPnetDiameterTraversalTime,
2533 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2534 nla_get_u16);
2535
2536 /* Apply changes */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002537 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002538
Jouni Malinenf3f92582009-03-20 17:57:36 +02002539 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002540 /* cleanup */
2541 cfg80211_put_dev(drv);
2542 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002543 out_rtnl:
2544 rtnl_unlock();
2545
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002546 return err;
2547}
2548
2549#undef FILL_IN_MESH_PARAM_IF_SET
2550
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002551static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2552{
2553 struct sk_buff *msg;
2554 void *hdr = NULL;
2555 struct nlattr *nl_reg_rules;
2556 unsigned int i;
2557 int err = -EINVAL;
2558
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002559 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002560
2561 if (!cfg80211_regdomain)
2562 goto out;
2563
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002564 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002565 if (!msg) {
2566 err = -ENOBUFS;
2567 goto out;
2568 }
2569
2570 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2571 NL80211_CMD_GET_REG);
2572 if (!hdr)
2573 goto nla_put_failure;
2574
2575 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2576 cfg80211_regdomain->alpha2);
2577
2578 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2579 if (!nl_reg_rules)
2580 goto nla_put_failure;
2581
2582 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2583 struct nlattr *nl_reg_rule;
2584 const struct ieee80211_reg_rule *reg_rule;
2585 const struct ieee80211_freq_range *freq_range;
2586 const struct ieee80211_power_rule *power_rule;
2587
2588 reg_rule = &cfg80211_regdomain->reg_rules[i];
2589 freq_range = &reg_rule->freq_range;
2590 power_rule = &reg_rule->power_rule;
2591
2592 nl_reg_rule = nla_nest_start(msg, i);
2593 if (!nl_reg_rule)
2594 goto nla_put_failure;
2595
2596 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2597 reg_rule->flags);
2598 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2599 freq_range->start_freq_khz);
2600 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2601 freq_range->end_freq_khz);
2602 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2603 freq_range->max_bandwidth_khz);
2604 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2605 power_rule->max_antenna_gain);
2606 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2607 power_rule->max_eirp);
2608
2609 nla_nest_end(msg, nl_reg_rule);
2610 }
2611
2612 nla_nest_end(msg, nl_reg_rules);
2613
2614 genlmsg_end(msg, hdr);
2615 err = genlmsg_unicast(msg, info->snd_pid);
2616 goto out;
2617
2618nla_put_failure:
2619 genlmsg_cancel(msg, hdr);
2620 err = -EMSGSIZE;
2621out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002622 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002623 return err;
2624}
2625
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002626static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2627{
2628 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2629 struct nlattr *nl_reg_rule;
2630 char *alpha2 = NULL;
2631 int rem_reg_rules = 0, r = 0;
2632 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2633 struct ieee80211_regdomain *rd = NULL;
2634
2635 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2636 return -EINVAL;
2637
2638 if (!info->attrs[NL80211_ATTR_REG_RULES])
2639 return -EINVAL;
2640
2641 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2642
2643 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2644 rem_reg_rules) {
2645 num_rules++;
2646 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002647 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002648 }
2649
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002650 mutex_lock(&cfg80211_mutex);
2651
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002652 if (!reg_is_valid_request(alpha2)) {
2653 r = -EINVAL;
2654 goto bad_reg;
2655 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002656
2657 size_of_regd = sizeof(struct ieee80211_regdomain) +
2658 (num_rules * sizeof(struct ieee80211_reg_rule));
2659
2660 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002661 if (!rd) {
2662 r = -ENOMEM;
2663 goto bad_reg;
2664 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002665
2666 rd->n_reg_rules = num_rules;
2667 rd->alpha2[0] = alpha2[0];
2668 rd->alpha2[1] = alpha2[1];
2669
2670 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2671 rem_reg_rules) {
2672 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2673 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2674 reg_rule_policy);
2675 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2676 if (r)
2677 goto bad_reg;
2678
2679 rule_idx++;
2680
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002681 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2682 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002683 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002684 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002685 }
2686
2687 BUG_ON(rule_idx != num_rules);
2688
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002689 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002690
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002691 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002692
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002693 return r;
2694
Johannes Bergd2372b32008-10-24 20:32:20 +02002695 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002696 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002697 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002698 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002699}
2700
Johannes Berg2a519312009-02-10 21:25:55 +01002701static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2702{
2703 struct cfg80211_registered_device *drv;
2704 struct net_device *dev;
2705 struct cfg80211_scan_request *request;
2706 struct cfg80211_ssid *ssid;
2707 struct ieee80211_channel *channel;
2708 struct nlattr *attr;
2709 struct wiphy *wiphy;
2710 int err, tmp, n_ssids = 0, n_channels = 0, i;
2711 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002712 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002713
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002714 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2715 return -EINVAL;
2716
Johannes Berg3b858752009-03-12 09:55:09 +01002717 rtnl_lock();
2718
Johannes Berg2a519312009-02-10 21:25:55 +01002719 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2720 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002721 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002722
2723 wiphy = &drv->wiphy;
2724
2725 if (!drv->ops->scan) {
2726 err = -EOPNOTSUPP;
2727 goto out;
2728 }
2729
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002730 if (!netif_running(dev)) {
2731 err = -ENETDOWN;
2732 goto out;
2733 }
2734
Johannes Berg2a519312009-02-10 21:25:55 +01002735 if (drv->scan_req) {
2736 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002737 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002738 }
2739
2740 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2741 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2742 n_channels++;
2743 if (!n_channels) {
2744 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002745 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002746 }
2747 } else {
2748 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2749 if (wiphy->bands[band])
2750 n_channels += wiphy->bands[band]->n_channels;
2751 }
2752
2753 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2754 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2755 n_ssids++;
2756
2757 if (n_ssids > wiphy->max_scan_ssids) {
2758 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002759 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002760 }
2761
Jouni Malinen70692ad2009-02-16 19:39:13 +02002762 if (info->attrs[NL80211_ATTR_IE])
2763 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2764 else
2765 ie_len = 0;
2766
Johannes Berg18a83652009-03-31 12:12:05 +02002767 if (ie_len > wiphy->max_scan_ie_len) {
2768 err = -EINVAL;
2769 goto out;
2770 }
2771
Johannes Berg2a519312009-02-10 21:25:55 +01002772 request = kzalloc(sizeof(*request)
2773 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002774 + sizeof(channel) * n_channels
2775 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002776 if (!request) {
2777 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002778 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002779 }
2780
2781 request->channels = (void *)((char *)request + sizeof(*request));
2782 request->n_channels = n_channels;
2783 if (n_ssids)
2784 request->ssids = (void *)(request->channels + n_channels);
2785 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002786 if (ie_len) {
2787 if (request->ssids)
2788 request->ie = (void *)(request->ssids + n_ssids);
2789 else
2790 request->ie = (void *)(request->channels + n_channels);
2791 }
Johannes Berg2a519312009-02-10 21:25:55 +01002792
2793 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2794 /* user specified, bail out if channel not found */
2795 request->n_channels = n_channels;
2796 i = 0;
2797 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2798 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2799 if (!request->channels[i]) {
2800 err = -EINVAL;
2801 goto out_free;
2802 }
2803 i++;
2804 }
2805 } else {
2806 /* all channels */
2807 i = 0;
2808 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2809 int j;
2810 if (!wiphy->bands[band])
2811 continue;
2812 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2813 request->channels[i] = &wiphy->bands[band]->channels[j];
2814 i++;
2815 }
2816 }
2817 }
2818
2819 i = 0;
2820 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2821 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2822 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2823 err = -EINVAL;
2824 goto out_free;
2825 }
2826 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2827 request->ssids[i].ssid_len = nla_len(attr);
2828 i++;
2829 }
2830 }
2831
Jouni Malinen70692ad2009-02-16 19:39:13 +02002832 if (info->attrs[NL80211_ATTR_IE]) {
2833 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02002834 memcpy((void *)request->ie,
2835 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02002836 request->ie_len);
2837 }
2838
Johannes Berg2a519312009-02-10 21:25:55 +01002839 request->ifidx = dev->ifindex;
2840 request->wiphy = &drv->wiphy;
2841
2842 drv->scan_req = request;
2843 err = drv->ops->scan(&drv->wiphy, dev, request);
2844
2845 out_free:
2846 if (err) {
2847 drv->scan_req = NULL;
2848 kfree(request);
2849 }
Johannes Berg2a519312009-02-10 21:25:55 +01002850 out:
2851 cfg80211_put_dev(drv);
2852 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002853 out_rtnl:
2854 rtnl_unlock();
2855
Johannes Berg2a519312009-02-10 21:25:55 +01002856 return err;
2857}
2858
2859static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2860 struct cfg80211_registered_device *rdev,
2861 struct net_device *dev,
2862 struct cfg80211_bss *res)
2863{
2864 void *hdr;
2865 struct nlattr *bss;
2866
2867 hdr = nl80211hdr_put(msg, pid, seq, flags,
2868 NL80211_CMD_NEW_SCAN_RESULTS);
2869 if (!hdr)
2870 return -1;
2871
2872 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2873 rdev->bss_generation);
2874 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2875
2876 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2877 if (!bss)
2878 goto nla_put_failure;
2879 if (!is_zero_ether_addr(res->bssid))
2880 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2881 if (res->information_elements && res->len_information_elements)
2882 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2883 res->len_information_elements,
2884 res->information_elements);
2885 if (res->tsf)
2886 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2887 if (res->beacon_interval)
2888 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2889 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2890 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2891
Johannes Berg77965c92009-02-18 18:45:06 +01002892 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002893 case CFG80211_SIGNAL_TYPE_MBM:
2894 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2895 break;
2896 case CFG80211_SIGNAL_TYPE_UNSPEC:
2897 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2898 break;
2899 default:
2900 break;
2901 }
2902
2903 nla_nest_end(msg, bss);
2904
2905 return genlmsg_end(msg, hdr);
2906
2907 nla_put_failure:
2908 genlmsg_cancel(msg, hdr);
2909 return -EMSGSIZE;
2910}
2911
2912static int nl80211_dump_scan(struct sk_buff *skb,
2913 struct netlink_callback *cb)
2914{
2915 struct cfg80211_registered_device *dev;
2916 struct net_device *netdev;
2917 struct cfg80211_internal_bss *scan;
2918 int ifidx = cb->args[0];
2919 int start = cb->args[1], idx = 0;
2920 int err;
2921
2922 if (!ifidx) {
2923 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2924 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2925 nl80211_policy);
2926 if (err)
2927 return err;
2928
2929 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2930 return -EINVAL;
2931
2932 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2933 if (!ifidx)
2934 return -EINVAL;
2935 cb->args[0] = ifidx;
2936 }
2937
2938 netdev = dev_get_by_index(&init_net, ifidx);
2939 if (!netdev)
2940 return -ENODEV;
2941
2942 dev = cfg80211_get_dev_from_ifindex(ifidx);
2943 if (IS_ERR(dev)) {
2944 err = PTR_ERR(dev);
2945 goto out_put_netdev;
2946 }
2947
2948 spin_lock_bh(&dev->bss_lock);
2949 cfg80211_bss_expire(dev);
2950
2951 list_for_each_entry(scan, &dev->bss_list, list) {
2952 if (++idx <= start)
2953 continue;
2954 if (nl80211_send_bss(skb,
2955 NETLINK_CB(cb->skb).pid,
2956 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2957 dev, netdev, &scan->pub) < 0) {
2958 idx--;
2959 goto out;
2960 }
2961 }
2962
2963 out:
2964 spin_unlock_bh(&dev->bss_lock);
2965
2966 cb->args[1] = idx;
2967 err = skb->len;
2968 cfg80211_put_dev(dev);
2969 out_put_netdev:
2970 dev_put(netdev);
2971
2972 return err;
2973}
2974
Jouni Malinen255e7372009-03-20 21:21:17 +02002975static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
2976{
2977 return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM ||
2978 auth_type == NL80211_AUTHTYPE_SHARED_KEY ||
2979 auth_type == NL80211_AUTHTYPE_FT ||
2980 auth_type == NL80211_AUTHTYPE_NETWORK_EAP;
2981}
2982
Jouni Malinen636a5d32009-03-19 13:39:22 +02002983static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2984{
2985 struct cfg80211_registered_device *drv;
2986 struct net_device *dev;
2987 struct cfg80211_auth_request req;
2988 struct wiphy *wiphy;
2989 int err;
2990
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002991 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2992 return -EINVAL;
2993
2994 if (!info->attrs[NL80211_ATTR_MAC])
2995 return -EINVAL;
2996
Jouni Malinen17780922009-03-27 20:52:47 +02002997 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
2998 return -EINVAL;
2999
Jouni Malinen636a5d32009-03-19 13:39:22 +02003000 rtnl_lock();
3001
3002 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3003 if (err)
3004 goto unlock_rtnl;
3005
3006 if (!drv->ops->auth) {
3007 err = -EOPNOTSUPP;
3008 goto out;
3009 }
3010
Jouni Malineneec60b02009-03-20 21:21:19 +02003011 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3012 err = -EOPNOTSUPP;
3013 goto out;
3014 }
3015
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003016 if (!netif_running(dev)) {
3017 err = -ENETDOWN;
3018 goto out;
3019 }
3020
Jouni Malinen636a5d32009-03-19 13:39:22 +02003021 wiphy = &drv->wiphy;
3022 memset(&req, 0, sizeof(req));
3023
3024 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3025
3026 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3027 req.chan = ieee80211_get_channel(
3028 wiphy,
3029 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3030 if (!req.chan) {
3031 err = -EINVAL;
3032 goto out;
3033 }
3034 }
3035
3036 if (info->attrs[NL80211_ATTR_SSID]) {
3037 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3038 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3039 }
3040
3041 if (info->attrs[NL80211_ATTR_IE]) {
3042 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3043 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3044 }
3045
Jouni Malinen17780922009-03-27 20:52:47 +02003046 req.auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3047 if (!nl80211_valid_auth_type(req.auth_type)) {
3048 err = -EINVAL;
3049 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003050 }
3051
3052 err = drv->ops->auth(&drv->wiphy, dev, &req);
3053
3054out:
3055 cfg80211_put_dev(drv);
3056 dev_put(dev);
3057unlock_rtnl:
3058 rtnl_unlock();
3059 return err;
3060}
3061
3062static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3063{
3064 struct cfg80211_registered_device *drv;
3065 struct net_device *dev;
3066 struct cfg80211_assoc_request req;
3067 struct wiphy *wiphy;
3068 int err;
3069
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003070 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3071 return -EINVAL;
3072
3073 if (!info->attrs[NL80211_ATTR_MAC] ||
3074 !info->attrs[NL80211_ATTR_SSID])
3075 return -EINVAL;
3076
Jouni Malinen636a5d32009-03-19 13:39:22 +02003077 rtnl_lock();
3078
3079 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3080 if (err)
3081 goto unlock_rtnl;
3082
3083 if (!drv->ops->assoc) {
3084 err = -EOPNOTSUPP;
3085 goto out;
3086 }
3087
Jouni Malineneec60b02009-03-20 21:21:19 +02003088 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3089 err = -EOPNOTSUPP;
3090 goto out;
3091 }
3092
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003093 if (!netif_running(dev)) {
3094 err = -ENETDOWN;
3095 goto out;
3096 }
3097
Jouni Malinen636a5d32009-03-19 13:39:22 +02003098 wiphy = &drv->wiphy;
3099 memset(&req, 0, sizeof(req));
3100
3101 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3102
3103 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3104 req.chan = ieee80211_get_channel(
3105 wiphy,
3106 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3107 if (!req.chan) {
3108 err = -EINVAL;
3109 goto out;
3110 }
3111 }
3112
Jouni Malinen636a5d32009-03-19 13:39:22 +02003113 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3114 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3115
3116 if (info->attrs[NL80211_ATTR_IE]) {
3117 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3118 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3119 }
3120
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003121 if (info->attrs[NL80211_ATTR_USE_MFP]) {
3122 enum nl80211_mfp use_mfp =
3123 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3124 if (use_mfp == NL80211_MFP_REQUIRED)
3125 req.use_mfp = true;
3126 else if (use_mfp != NL80211_MFP_NO) {
3127 err = -EINVAL;
3128 goto out;
3129 }
3130 }
3131
Jouni Malinen3f77316c2009-05-11 21:57:57 +03003132 req.control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3133
Jouni Malinen636a5d32009-03-19 13:39:22 +02003134 err = drv->ops->assoc(&drv->wiphy, dev, &req);
3135
3136out:
3137 cfg80211_put_dev(drv);
3138 dev_put(dev);
3139unlock_rtnl:
3140 rtnl_unlock();
3141 return err;
3142}
3143
3144static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3145{
3146 struct cfg80211_registered_device *drv;
3147 struct net_device *dev;
3148 struct cfg80211_deauth_request req;
3149 struct wiphy *wiphy;
3150 int err;
3151
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003152 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3153 return -EINVAL;
3154
3155 if (!info->attrs[NL80211_ATTR_MAC])
3156 return -EINVAL;
3157
3158 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3159 return -EINVAL;
3160
Jouni Malinen636a5d32009-03-19 13:39:22 +02003161 rtnl_lock();
3162
3163 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3164 if (err)
3165 goto unlock_rtnl;
3166
3167 if (!drv->ops->deauth) {
3168 err = -EOPNOTSUPP;
3169 goto out;
3170 }
3171
Jouni Malineneec60b02009-03-20 21:21:19 +02003172 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3173 err = -EOPNOTSUPP;
3174 goto out;
3175 }
3176
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003177 if (!netif_running(dev)) {
3178 err = -ENETDOWN;
3179 goto out;
3180 }
3181
Jouni Malinen636a5d32009-03-19 13:39:22 +02003182 wiphy = &drv->wiphy;
3183 memset(&req, 0, sizeof(req));
3184
3185 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3186
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003187 req.reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3188 if (req.reason_code == 0) {
3189 /* Reason Code 0 is reserved */
3190 err = -EINVAL;
3191 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003192 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003193
3194 if (info->attrs[NL80211_ATTR_IE]) {
3195 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3196 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3197 }
3198
3199 err = drv->ops->deauth(&drv->wiphy, dev, &req);
3200
3201out:
3202 cfg80211_put_dev(drv);
3203 dev_put(dev);
3204unlock_rtnl:
3205 rtnl_unlock();
3206 return err;
3207}
3208
3209static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3210{
3211 struct cfg80211_registered_device *drv;
3212 struct net_device *dev;
3213 struct cfg80211_disassoc_request req;
3214 struct wiphy *wiphy;
3215 int err;
3216
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003217 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3218 return -EINVAL;
3219
3220 if (!info->attrs[NL80211_ATTR_MAC])
3221 return -EINVAL;
3222
3223 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3224 return -EINVAL;
3225
Jouni Malinen636a5d32009-03-19 13:39:22 +02003226 rtnl_lock();
3227
3228 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3229 if (err)
3230 goto unlock_rtnl;
3231
3232 if (!drv->ops->disassoc) {
3233 err = -EOPNOTSUPP;
3234 goto out;
3235 }
3236
Jouni Malineneec60b02009-03-20 21:21:19 +02003237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3238 err = -EOPNOTSUPP;
3239 goto out;
3240 }
3241
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003242 if (!netif_running(dev)) {
3243 err = -ENETDOWN;
3244 goto out;
3245 }
3246
Jouni Malinen636a5d32009-03-19 13:39:22 +02003247 wiphy = &drv->wiphy;
3248 memset(&req, 0, sizeof(req));
3249
3250 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3251
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003252 req.reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3253 if (req.reason_code == 0) {
3254 /* Reason Code 0 is reserved */
3255 err = -EINVAL;
3256 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003257 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003258
3259 if (info->attrs[NL80211_ATTR_IE]) {
3260 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3261 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3262 }
3263
3264 err = drv->ops->disassoc(&drv->wiphy, dev, &req);
3265
3266out:
3267 cfg80211_put_dev(drv);
3268 dev_put(dev);
3269unlock_rtnl:
3270 rtnl_unlock();
3271 return err;
3272}
3273
Johannes Berg04a773a2009-04-19 21:24:32 +02003274static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3275{
3276 struct cfg80211_registered_device *drv;
3277 struct net_device *dev;
3278 struct cfg80211_ibss_params ibss;
3279 struct wiphy *wiphy;
3280 int err;
3281
Johannes Berg8e30bc52009-04-22 17:45:38 +02003282 memset(&ibss, 0, sizeof(ibss));
3283
Johannes Berg04a773a2009-04-19 21:24:32 +02003284 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3285 return -EINVAL;
3286
3287 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3288 !info->attrs[NL80211_ATTR_SSID] ||
3289 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3290 return -EINVAL;
3291
Johannes Berg8e30bc52009-04-22 17:45:38 +02003292 ibss.beacon_interval = 100;
3293
3294 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3295 ibss.beacon_interval =
3296 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3297 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3298 return -EINVAL;
3299 }
3300
Johannes Berg04a773a2009-04-19 21:24:32 +02003301 rtnl_lock();
3302
3303 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3304 if (err)
3305 goto unlock_rtnl;
3306
3307 if (!drv->ops->join_ibss) {
3308 err = -EOPNOTSUPP;
3309 goto out;
3310 }
3311
3312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3313 err = -EOPNOTSUPP;
3314 goto out;
3315 }
3316
3317 if (!netif_running(dev)) {
3318 err = -ENETDOWN;
3319 goto out;
3320 }
3321
3322 wiphy = &drv->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003323
3324 if (info->attrs[NL80211_ATTR_MAC])
3325 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3326 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3327 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3328
3329 if (info->attrs[NL80211_ATTR_IE]) {
3330 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3331 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3332 }
3333
3334 ibss.channel = ieee80211_get_channel(wiphy,
3335 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3336 if (!ibss.channel ||
3337 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3338 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3339 err = -EINVAL;
3340 goto out;
3341 }
3342
3343 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3344
3345 err = cfg80211_join_ibss(drv, dev, &ibss);
3346
3347out:
3348 cfg80211_put_dev(drv);
3349 dev_put(dev);
3350unlock_rtnl:
3351 rtnl_unlock();
3352 return err;
3353}
3354
3355static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3356{
3357 struct cfg80211_registered_device *drv;
3358 struct net_device *dev;
3359 int err;
3360
3361 rtnl_lock();
3362
3363 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3364 if (err)
3365 goto unlock_rtnl;
3366
3367 if (!drv->ops->leave_ibss) {
3368 err = -EOPNOTSUPP;
3369 goto out;
3370 }
3371
3372 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3373 err = -EOPNOTSUPP;
3374 goto out;
3375 }
3376
3377 if (!netif_running(dev)) {
3378 err = -ENETDOWN;
3379 goto out;
3380 }
3381
Johannes Berg9d308422009-04-20 18:43:46 +02003382 err = cfg80211_leave_ibss(drv, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003383
3384out:
3385 cfg80211_put_dev(drv);
3386 dev_put(dev);
3387unlock_rtnl:
3388 rtnl_unlock();
3389 return err;
3390}
3391
Johannes Berg55682962007-09-20 13:09:35 -04003392static struct genl_ops nl80211_ops[] = {
3393 {
3394 .cmd = NL80211_CMD_GET_WIPHY,
3395 .doit = nl80211_get_wiphy,
3396 .dumpit = nl80211_dump_wiphy,
3397 .policy = nl80211_policy,
3398 /* can be retrieved by unprivileged users */
3399 },
3400 {
3401 .cmd = NL80211_CMD_SET_WIPHY,
3402 .doit = nl80211_set_wiphy,
3403 .policy = nl80211_policy,
3404 .flags = GENL_ADMIN_PERM,
3405 },
3406 {
3407 .cmd = NL80211_CMD_GET_INTERFACE,
3408 .doit = nl80211_get_interface,
3409 .dumpit = nl80211_dump_interface,
3410 .policy = nl80211_policy,
3411 /* can be retrieved by unprivileged users */
3412 },
3413 {
3414 .cmd = NL80211_CMD_SET_INTERFACE,
3415 .doit = nl80211_set_interface,
3416 .policy = nl80211_policy,
3417 .flags = GENL_ADMIN_PERM,
3418 },
3419 {
3420 .cmd = NL80211_CMD_NEW_INTERFACE,
3421 .doit = nl80211_new_interface,
3422 .policy = nl80211_policy,
3423 .flags = GENL_ADMIN_PERM,
3424 },
3425 {
3426 .cmd = NL80211_CMD_DEL_INTERFACE,
3427 .doit = nl80211_del_interface,
3428 .policy = nl80211_policy,
3429 .flags = GENL_ADMIN_PERM,
3430 },
Johannes Berg41ade002007-12-19 02:03:29 +01003431 {
3432 .cmd = NL80211_CMD_GET_KEY,
3433 .doit = nl80211_get_key,
3434 .policy = nl80211_policy,
3435 .flags = GENL_ADMIN_PERM,
3436 },
3437 {
3438 .cmd = NL80211_CMD_SET_KEY,
3439 .doit = nl80211_set_key,
3440 .policy = nl80211_policy,
3441 .flags = GENL_ADMIN_PERM,
3442 },
3443 {
3444 .cmd = NL80211_CMD_NEW_KEY,
3445 .doit = nl80211_new_key,
3446 .policy = nl80211_policy,
3447 .flags = GENL_ADMIN_PERM,
3448 },
3449 {
3450 .cmd = NL80211_CMD_DEL_KEY,
3451 .doit = nl80211_del_key,
3452 .policy = nl80211_policy,
3453 .flags = GENL_ADMIN_PERM,
3454 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01003455 {
3456 .cmd = NL80211_CMD_SET_BEACON,
3457 .policy = nl80211_policy,
3458 .flags = GENL_ADMIN_PERM,
3459 .doit = nl80211_addset_beacon,
3460 },
3461 {
3462 .cmd = NL80211_CMD_NEW_BEACON,
3463 .policy = nl80211_policy,
3464 .flags = GENL_ADMIN_PERM,
3465 .doit = nl80211_addset_beacon,
3466 },
3467 {
3468 .cmd = NL80211_CMD_DEL_BEACON,
3469 .policy = nl80211_policy,
3470 .flags = GENL_ADMIN_PERM,
3471 .doit = nl80211_del_beacon,
3472 },
Johannes Berg5727ef12007-12-19 02:03:34 +01003473 {
3474 .cmd = NL80211_CMD_GET_STATION,
3475 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003476 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01003477 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01003478 },
3479 {
3480 .cmd = NL80211_CMD_SET_STATION,
3481 .doit = nl80211_set_station,
3482 .policy = nl80211_policy,
3483 .flags = GENL_ADMIN_PERM,
3484 },
3485 {
3486 .cmd = NL80211_CMD_NEW_STATION,
3487 .doit = nl80211_new_station,
3488 .policy = nl80211_policy,
3489 .flags = GENL_ADMIN_PERM,
3490 },
3491 {
3492 .cmd = NL80211_CMD_DEL_STATION,
3493 .doit = nl80211_del_station,
3494 .policy = nl80211_policy,
3495 .flags = GENL_ADMIN_PERM,
3496 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003497 {
3498 .cmd = NL80211_CMD_GET_MPATH,
3499 .doit = nl80211_get_mpath,
3500 .dumpit = nl80211_dump_mpath,
3501 .policy = nl80211_policy,
3502 .flags = GENL_ADMIN_PERM,
3503 },
3504 {
3505 .cmd = NL80211_CMD_SET_MPATH,
3506 .doit = nl80211_set_mpath,
3507 .policy = nl80211_policy,
3508 .flags = GENL_ADMIN_PERM,
3509 },
3510 {
3511 .cmd = NL80211_CMD_NEW_MPATH,
3512 .doit = nl80211_new_mpath,
3513 .policy = nl80211_policy,
3514 .flags = GENL_ADMIN_PERM,
3515 },
3516 {
3517 .cmd = NL80211_CMD_DEL_MPATH,
3518 .doit = nl80211_del_mpath,
3519 .policy = nl80211_policy,
3520 .flags = GENL_ADMIN_PERM,
3521 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003522 {
3523 .cmd = NL80211_CMD_SET_BSS,
3524 .doit = nl80211_set_bss,
3525 .policy = nl80211_policy,
3526 .flags = GENL_ADMIN_PERM,
3527 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003528 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003529 .cmd = NL80211_CMD_GET_REG,
3530 .doit = nl80211_get_reg,
3531 .policy = nl80211_policy,
3532 /* can be retrieved by unprivileged users */
3533 },
3534 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003535 .cmd = NL80211_CMD_SET_REG,
3536 .doit = nl80211_set_reg,
3537 .policy = nl80211_policy,
3538 .flags = GENL_ADMIN_PERM,
3539 },
3540 {
3541 .cmd = NL80211_CMD_REQ_SET_REG,
3542 .doit = nl80211_req_set_reg,
3543 .policy = nl80211_policy,
3544 .flags = GENL_ADMIN_PERM,
3545 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003546 {
3547 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3548 .doit = nl80211_get_mesh_params,
3549 .policy = nl80211_policy,
3550 /* can be retrieved by unprivileged users */
3551 },
3552 {
3553 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3554 .doit = nl80211_set_mesh_params,
3555 .policy = nl80211_policy,
3556 .flags = GENL_ADMIN_PERM,
3557 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02003558 {
Johannes Berg2a519312009-02-10 21:25:55 +01003559 .cmd = NL80211_CMD_TRIGGER_SCAN,
3560 .doit = nl80211_trigger_scan,
3561 .policy = nl80211_policy,
3562 .flags = GENL_ADMIN_PERM,
3563 },
3564 {
3565 .cmd = NL80211_CMD_GET_SCAN,
3566 .policy = nl80211_policy,
3567 .dumpit = nl80211_dump_scan,
3568 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02003569 {
3570 .cmd = NL80211_CMD_AUTHENTICATE,
3571 .doit = nl80211_authenticate,
3572 .policy = nl80211_policy,
3573 .flags = GENL_ADMIN_PERM,
3574 },
3575 {
3576 .cmd = NL80211_CMD_ASSOCIATE,
3577 .doit = nl80211_associate,
3578 .policy = nl80211_policy,
3579 .flags = GENL_ADMIN_PERM,
3580 },
3581 {
3582 .cmd = NL80211_CMD_DEAUTHENTICATE,
3583 .doit = nl80211_deauthenticate,
3584 .policy = nl80211_policy,
3585 .flags = GENL_ADMIN_PERM,
3586 },
3587 {
3588 .cmd = NL80211_CMD_DISASSOCIATE,
3589 .doit = nl80211_disassociate,
3590 .policy = nl80211_policy,
3591 .flags = GENL_ADMIN_PERM,
3592 },
Johannes Berg04a773a2009-04-19 21:24:32 +02003593 {
3594 .cmd = NL80211_CMD_JOIN_IBSS,
3595 .doit = nl80211_join_ibss,
3596 .policy = nl80211_policy,
3597 .flags = GENL_ADMIN_PERM,
3598 },
3599 {
3600 .cmd = NL80211_CMD_LEAVE_IBSS,
3601 .doit = nl80211_leave_ibss,
3602 .policy = nl80211_policy,
3603 .flags = GENL_ADMIN_PERM,
3604 },
Johannes Berg55682962007-09-20 13:09:35 -04003605};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003606static struct genl_multicast_group nl80211_mlme_mcgrp = {
3607 .name = "mlme",
3608};
Johannes Berg55682962007-09-20 13:09:35 -04003609
3610/* multicast groups */
3611static struct genl_multicast_group nl80211_config_mcgrp = {
3612 .name = "config",
3613};
Johannes Berg2a519312009-02-10 21:25:55 +01003614static struct genl_multicast_group nl80211_scan_mcgrp = {
3615 .name = "scan",
3616};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003617static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3618 .name = "regulatory",
3619};
Johannes Berg55682962007-09-20 13:09:35 -04003620
3621/* notification functions */
3622
3623void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3624{
3625 struct sk_buff *msg;
3626
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003627 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04003628 if (!msg)
3629 return;
3630
3631 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3632 nlmsg_free(msg);
3633 return;
3634 }
3635
3636 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3637}
3638
Johannes Berg362a4152009-05-24 16:43:15 +02003639static int nl80211_add_scan_req(struct sk_buff *msg,
3640 struct cfg80211_registered_device *rdev)
3641{
3642 struct cfg80211_scan_request *req = rdev->scan_req;
3643 struct nlattr *nest;
3644 int i;
3645
3646 if (WARN_ON(!req))
3647 return 0;
3648
3649 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
3650 if (!nest)
3651 goto nla_put_failure;
3652 for (i = 0; i < req->n_ssids; i++)
3653 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
3654 nla_nest_end(msg, nest);
3655
3656 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
3657 if (!nest)
3658 goto nla_put_failure;
3659 for (i = 0; i < req->n_channels; i++)
3660 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
3661 nla_nest_end(msg, nest);
3662
3663 if (req->ie)
3664 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
3665
3666 return 0;
3667 nla_put_failure:
3668 return -ENOBUFS;
3669}
3670
Johannes Berg2a519312009-02-10 21:25:55 +01003671static int nl80211_send_scan_donemsg(struct sk_buff *msg,
Johannes Berg362a4152009-05-24 16:43:15 +02003672 struct cfg80211_registered_device *rdev,
3673 struct net_device *netdev,
3674 u32 pid, u32 seq, int flags,
3675 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01003676{
3677 void *hdr;
3678
3679 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3680 if (!hdr)
3681 return -1;
3682
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05003683 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01003684 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3685
Johannes Berg362a4152009-05-24 16:43:15 +02003686 /* ignore errors and send incomplete event anyway */
3687 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003688
3689 return genlmsg_end(msg, hdr);
3690
3691 nla_put_failure:
3692 genlmsg_cancel(msg, hdr);
3693 return -EMSGSIZE;
3694}
3695
3696void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3697 struct net_device *netdev)
3698{
3699 struct sk_buff *msg;
3700
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003701 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003702 if (!msg)
3703 return;
3704
3705 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3706 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3707 nlmsg_free(msg);
3708 return;
3709 }
3710
3711 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3712}
3713
3714void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3715 struct net_device *netdev)
3716{
3717 struct sk_buff *msg;
3718
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003719 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003720 if (!msg)
3721 return;
3722
3723 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3724 NL80211_CMD_SCAN_ABORTED) < 0) {
3725 nlmsg_free(msg);
3726 return;
3727 }
3728
3729 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3730}
3731
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003732/*
3733 * This can happen on global regulatory changes or device specific settings
3734 * based on custom world regulatory domains.
3735 */
3736void nl80211_send_reg_change_event(struct regulatory_request *request)
3737{
3738 struct sk_buff *msg;
3739 void *hdr;
3740
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003741 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003742 if (!msg)
3743 return;
3744
3745 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3746 if (!hdr) {
3747 nlmsg_free(msg);
3748 return;
3749 }
3750
3751 /* Userspace can always count this one always being set */
3752 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3753
3754 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3755 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3756 NL80211_REGDOM_TYPE_WORLD);
3757 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3758 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3759 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3760 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3761 request->intersect)
3762 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3763 NL80211_REGDOM_TYPE_INTERSECTION);
3764 else {
3765 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3766 NL80211_REGDOM_TYPE_COUNTRY);
3767 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3768 }
3769
3770 if (wiphy_idx_valid(request->wiphy_idx))
3771 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3772
3773 if (genlmsg_end(msg, hdr) < 0) {
3774 nlmsg_free(msg);
3775 return;
3776 }
3777
3778 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3779
3780 return;
3781
3782nla_put_failure:
3783 genlmsg_cancel(msg, hdr);
3784 nlmsg_free(msg);
3785}
3786
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003787static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3788 struct net_device *netdev,
3789 const u8 *buf, size_t len,
3790 enum nl80211_commands cmd)
3791{
3792 struct sk_buff *msg;
3793 void *hdr;
3794
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003795 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003796 if (!msg)
3797 return;
3798
3799 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3800 if (!hdr) {
3801 nlmsg_free(msg);
3802 return;
3803 }
3804
3805 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3806 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3807 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3808
3809 if (genlmsg_end(msg, hdr) < 0) {
3810 nlmsg_free(msg);
3811 return;
3812 }
3813
Jouni Malinend91c01c2009-04-18 21:53:15 +03003814 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003815 return;
3816
3817 nla_put_failure:
3818 genlmsg_cancel(msg, hdr);
3819 nlmsg_free(msg);
3820}
3821
3822void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3823 struct net_device *netdev, const u8 *buf, size_t len)
3824{
3825 nl80211_send_mlme_event(rdev, netdev, buf, len,
3826 NL80211_CMD_AUTHENTICATE);
3827}
3828
3829void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3830 struct net_device *netdev, const u8 *buf,
3831 size_t len)
3832{
3833 nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3834}
3835
Jouni Malinen53b46b82009-03-27 20:53:56 +02003836void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
3837 struct net_device *netdev, const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003838{
3839 nl80211_send_mlme_event(rdev, netdev, buf, len,
3840 NL80211_CMD_DEAUTHENTICATE);
3841}
3842
Jouni Malinen53b46b82009-03-27 20:53:56 +02003843void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
3844 struct net_device *netdev, const u8 *buf,
3845 size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003846{
3847 nl80211_send_mlme_event(rdev, netdev, buf, len,
3848 NL80211_CMD_DISASSOCIATE);
3849}
3850
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04003851static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
3852 struct net_device *netdev, int cmd,
3853 const u8 *addr)
Jouni Malinen1965c852009-04-22 21:38:25 +03003854{
3855 struct sk_buff *msg;
3856 void *hdr;
3857
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003858 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Jouni Malinen1965c852009-04-22 21:38:25 +03003859 if (!msg)
3860 return;
3861
3862 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3863 if (!hdr) {
3864 nlmsg_free(msg);
3865 return;
3866 }
3867
3868 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3870 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
3871 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3872
3873 if (genlmsg_end(msg, hdr) < 0) {
3874 nlmsg_free(msg);
3875 return;
3876 }
3877
3878 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
3879 return;
3880
3881 nla_put_failure:
3882 genlmsg_cancel(msg, hdr);
3883 nlmsg_free(msg);
3884}
3885
3886void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
3887 struct net_device *netdev, const u8 *addr)
3888{
3889 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
3890 addr);
3891}
3892
3893void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
3894 struct net_device *netdev, const u8 *addr)
3895{
3896 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE, addr);
3897}
3898
Johannes Berg04a773a2009-04-19 21:24:32 +02003899void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
3900 struct net_device *netdev, const u8 *bssid,
3901 gfp_t gfp)
3902{
3903 struct sk_buff *msg;
3904 void *hdr;
3905
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02003907 if (!msg)
3908 return;
3909
3910 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
3911 if (!hdr) {
3912 nlmsg_free(msg);
3913 return;
3914 }
3915
3916 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3917 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3918 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
3919
3920 if (genlmsg_end(msg, hdr) < 0) {
3921 nlmsg_free(msg);
3922 return;
3923 }
3924
3925 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
3926 return;
3927
3928 nla_put_failure:
3929 genlmsg_cancel(msg, hdr);
3930 nlmsg_free(msg);
3931}
3932
Jouni Malinena3b8b052009-03-27 21:59:49 +02003933void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
3934 struct net_device *netdev, const u8 *addr,
3935 enum nl80211_key_type key_type, int key_id,
3936 const u8 *tsc)
3937{
3938 struct sk_buff *msg;
3939 void *hdr;
3940
Bob Copeland0f6399c2009-06-02 00:05:04 -04003941 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Jouni Malinena3b8b052009-03-27 21:59:49 +02003942 if (!msg)
3943 return;
3944
3945 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
3946 if (!hdr) {
3947 nlmsg_free(msg);
3948 return;
3949 }
3950
3951 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3952 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3953 if (addr)
3954 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3955 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
3956 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
3957 if (tsc)
3958 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
3959
3960 if (genlmsg_end(msg, hdr) < 0) {
3961 nlmsg_free(msg);
3962 return;
3963 }
3964
Bob Copeland0f6399c2009-06-02 00:05:04 -04003965 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
Jouni Malinena3b8b052009-03-27 21:59:49 +02003966 return;
3967
3968 nla_put_failure:
3969 genlmsg_cancel(msg, hdr);
3970 nlmsg_free(msg);
3971}
3972
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04003973void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
3974 struct ieee80211_channel *channel_before,
3975 struct ieee80211_channel *channel_after)
3976{
3977 struct sk_buff *msg;
3978 void *hdr;
3979 struct nlattr *nl_freq;
3980
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003981 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04003982 if (!msg)
3983 return;
3984
3985 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
3986 if (!hdr) {
3987 nlmsg_free(msg);
3988 return;
3989 }
3990
3991 /*
3992 * Since we are applying the beacon hint to a wiphy we know its
3993 * wiphy_idx is valid
3994 */
3995 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
3996
3997 /* Before */
3998 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
3999 if (!nl_freq)
4000 goto nla_put_failure;
4001 if (nl80211_msg_put_channel(msg, channel_before))
4002 goto nla_put_failure;
4003 nla_nest_end(msg, nl_freq);
4004
4005 /* After */
4006 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
4007 if (!nl_freq)
4008 goto nla_put_failure;
4009 if (nl80211_msg_put_channel(msg, channel_after))
4010 goto nla_put_failure;
4011 nla_nest_end(msg, nl_freq);
4012
4013 if (genlmsg_end(msg, hdr) < 0) {
4014 nlmsg_free(msg);
4015 return;
4016 }
4017
4018 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_ATOMIC);
4019
4020 return;
4021
4022nla_put_failure:
4023 genlmsg_cancel(msg, hdr);
4024 nlmsg_free(msg);
4025}
4026
Johannes Berg55682962007-09-20 13:09:35 -04004027/* initialisation/exit functions */
4028
4029int nl80211_init(void)
4030{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004031 int err;
Johannes Berg55682962007-09-20 13:09:35 -04004032
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004033 err = genl_register_family_with_ops(&nl80211_fam,
4034 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04004035 if (err)
4036 return err;
4037
Johannes Berg55682962007-09-20 13:09:35 -04004038 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
4039 if (err)
4040 goto err_out;
4041
Johannes Berg2a519312009-02-10 21:25:55 +01004042 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
4043 if (err)
4044 goto err_out;
4045
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004046 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
4047 if (err)
4048 goto err_out;
4049
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004050 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
4051 if (err)
4052 goto err_out;
4053
Johannes Berg55682962007-09-20 13:09:35 -04004054 return 0;
4055 err_out:
4056 genl_unregister_family(&nl80211_fam);
4057 return err;
4058}
4059
4060void nl80211_exit(void)
4061{
4062 genl_unregister_family(&nl80211_fam);
4063}