Driver core: Fix cleanup when failing device_add().
[wrt350n-kernel.git] / net / wireless / nl80211.c
blobe3a214f63f9178e6689c2749ba7b275795718652
1 /*
2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
5 */
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/mutex.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <net/genetlink.h>
18 #include <net/cfg80211.h>
19 #include "core.h"
20 #include "nl80211.h"
22 /* the netlink family */
23 static struct genl_family nl80211_fam = {
24 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
25 .name = "nl80211", /* have users key off the name instead */
26 .hdrsize = 0, /* no private header */
27 .version = 1, /* no particular meaning now */
28 .maxattr = NL80211_ATTR_MAX,
31 /* internal helper: get drv and dev */
32 static int get_drv_dev_by_info_ifindex(struct genl_info *info,
33 struct cfg80211_registered_device **drv,
34 struct net_device **dev)
36 int ifindex;
38 if (!info->attrs[NL80211_ATTR_IFINDEX])
39 return -EINVAL;
41 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
42 *dev = dev_get_by_index(&init_net, ifindex);
43 if (!*dev)
44 return -ENODEV;
46 *drv = cfg80211_get_dev_from_ifindex(ifindex);
47 if (IS_ERR(*drv)) {
48 dev_put(*dev);
49 return PTR_ERR(*drv);
52 return 0;
55 /* policy for the attributes */
56 static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
57 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
58 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
59 .len = BUS_ID_SIZE-1 },
61 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
62 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
63 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
65 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
67 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
68 .len = WLAN_MAX_KEY_LEN },
69 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
70 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
71 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
73 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
74 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
75 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
76 .len = IEEE80211_MAX_DATA_LEN },
77 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
78 .len = IEEE80211_MAX_DATA_LEN },
79 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
80 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
81 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
82 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
83 .len = NL80211_MAX_SUPP_RATES },
84 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
87 /* message building helper */
88 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
89 int flags, u8 cmd)
91 /* since there is no private header just add the generic one */
92 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
95 /* netlink command implementations */
97 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
98 struct cfg80211_registered_device *dev)
100 void *hdr;
102 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
103 if (!hdr)
104 return -1;
106 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx);
107 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
108 return genlmsg_end(msg, hdr);
110 nla_put_failure:
111 return genlmsg_cancel(msg, hdr);
114 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
116 int idx = 0;
117 int start = cb->args[0];
118 struct cfg80211_registered_device *dev;
120 mutex_lock(&cfg80211_drv_mutex);
121 list_for_each_entry(dev, &cfg80211_drv_list, list) {
122 if (++idx < start)
123 continue;
124 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
125 cb->nlh->nlmsg_seq, NLM_F_MULTI,
126 dev) < 0)
127 break;
129 mutex_unlock(&cfg80211_drv_mutex);
131 cb->args[0] = idx;
133 return skb->len;
136 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
138 struct sk_buff *msg;
139 struct cfg80211_registered_device *dev;
141 dev = cfg80211_get_dev_from_info(info);
142 if (IS_ERR(dev))
143 return PTR_ERR(dev);
145 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
146 if (!msg)
147 goto out_err;
149 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
150 goto out_free;
152 cfg80211_put_dev(dev);
154 return genlmsg_unicast(msg, info->snd_pid);
156 out_free:
157 nlmsg_free(msg);
158 out_err:
159 cfg80211_put_dev(dev);
160 return -ENOBUFS;
163 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
165 struct cfg80211_registered_device *rdev;
166 int result;
168 if (!info->attrs[NL80211_ATTR_WIPHY_NAME])
169 return -EINVAL;
171 rdev = cfg80211_get_dev_from_info(info);
172 if (IS_ERR(rdev))
173 return PTR_ERR(rdev);
175 result = cfg80211_dev_rename(rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
177 cfg80211_put_dev(rdev);
178 return result;
182 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
183 struct net_device *dev)
185 void *hdr;
187 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
188 if (!hdr)
189 return -1;
191 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
192 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
193 /* TODO: interface type */
194 return genlmsg_end(msg, hdr);
196 nla_put_failure:
197 return genlmsg_cancel(msg, hdr);
200 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
202 int wp_idx = 0;
203 int if_idx = 0;
204 int wp_start = cb->args[0];
205 int if_start = cb->args[1];
206 struct cfg80211_registered_device *dev;
207 struct wireless_dev *wdev;
209 mutex_lock(&cfg80211_drv_mutex);
210 list_for_each_entry(dev, &cfg80211_drv_list, list) {
211 if (++wp_idx < wp_start)
212 continue;
213 if_idx = 0;
215 mutex_lock(&dev->devlist_mtx);
216 list_for_each_entry(wdev, &dev->netdev_list, list) {
217 if (++if_idx < if_start)
218 continue;
219 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
220 cb->nlh->nlmsg_seq, NLM_F_MULTI,
221 wdev->netdev) < 0)
222 break;
224 mutex_unlock(&dev->devlist_mtx);
226 mutex_unlock(&cfg80211_drv_mutex);
228 cb->args[0] = wp_idx;
229 cb->args[1] = if_idx;
231 return skb->len;
234 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
236 struct sk_buff *msg;
237 struct cfg80211_registered_device *dev;
238 struct net_device *netdev;
239 int err;
241 err = get_drv_dev_by_info_ifindex(info, &dev, &netdev);
242 if (err)
243 return err;
245 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
246 if (!msg)
247 goto out_err;
249 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
250 goto out_free;
252 dev_put(netdev);
253 cfg80211_put_dev(dev);
255 return genlmsg_unicast(msg, info->snd_pid);
257 out_free:
258 nlmsg_free(msg);
259 out_err:
260 dev_put(netdev);
261 cfg80211_put_dev(dev);
262 return -ENOBUFS;
265 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
267 struct cfg80211_registered_device *drv;
268 int err, ifindex;
269 enum nl80211_iftype type;
270 struct net_device *dev;
272 if (info->attrs[NL80211_ATTR_IFTYPE]) {
273 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
274 if (type > NL80211_IFTYPE_MAX)
275 return -EINVAL;
276 } else
277 return -EINVAL;
279 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
280 if (err)
281 return err;
282 ifindex = dev->ifindex;
283 dev_put(dev);
285 if (!drv->ops->change_virtual_intf) {
286 err = -EOPNOTSUPP;
287 goto unlock;
290 rtnl_lock();
291 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex, type);
292 rtnl_unlock();
294 unlock:
295 cfg80211_put_dev(drv);
296 return err;
299 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
301 struct cfg80211_registered_device *drv;
302 int err;
303 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
305 if (!info->attrs[NL80211_ATTR_IFNAME])
306 return -EINVAL;
308 if (info->attrs[NL80211_ATTR_IFTYPE]) {
309 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
310 if (type > NL80211_IFTYPE_MAX)
311 return -EINVAL;
314 drv = cfg80211_get_dev_from_info(info);
315 if (IS_ERR(drv))
316 return PTR_ERR(drv);
318 if (!drv->ops->add_virtual_intf) {
319 err = -EOPNOTSUPP;
320 goto unlock;
323 rtnl_lock();
324 err = drv->ops->add_virtual_intf(&drv->wiphy,
325 nla_data(info->attrs[NL80211_ATTR_IFNAME]), type);
326 rtnl_unlock();
328 unlock:
329 cfg80211_put_dev(drv);
330 return err;
333 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
335 struct cfg80211_registered_device *drv;
336 int ifindex, err;
337 struct net_device *dev;
339 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
340 if (err)
341 return err;
342 ifindex = dev->ifindex;
343 dev_put(dev);
345 if (!drv->ops->del_virtual_intf) {
346 err = -EOPNOTSUPP;
347 goto out;
350 rtnl_lock();
351 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
352 rtnl_unlock();
354 out:
355 cfg80211_put_dev(drv);
356 return err;
359 struct get_key_cookie {
360 struct sk_buff *msg;
361 int error;
364 static void get_key_callback(void *c, struct key_params *params)
366 struct get_key_cookie *cookie = c;
368 if (params->key)
369 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
370 params->key_len, params->key);
372 if (params->seq)
373 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
374 params->seq_len, params->seq);
376 if (params->cipher)
377 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
378 params->cipher);
380 return;
381 nla_put_failure:
382 cookie->error = 1;
385 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
387 struct cfg80211_registered_device *drv;
388 int err;
389 struct net_device *dev;
390 u8 key_idx = 0;
391 u8 *mac_addr = NULL;
392 struct get_key_cookie cookie = {
393 .error = 0,
395 void *hdr;
396 struct sk_buff *msg;
398 if (info->attrs[NL80211_ATTR_KEY_IDX])
399 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
401 if (key_idx > 3)
402 return -EINVAL;
404 if (info->attrs[NL80211_ATTR_MAC])
405 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
407 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
408 if (err)
409 return err;
411 if (!drv->ops->get_key) {
412 err = -EOPNOTSUPP;
413 goto out;
416 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
417 if (!msg) {
418 err = -ENOMEM;
419 goto out;
422 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
423 NL80211_CMD_NEW_KEY);
425 if (IS_ERR(hdr)) {
426 err = PTR_ERR(hdr);
427 goto out;
430 cookie.msg = msg;
432 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
433 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
434 if (mac_addr)
435 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
437 rtnl_lock();
438 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
439 &cookie, get_key_callback);
440 rtnl_unlock();
442 if (err)
443 goto out;
445 if (cookie.error)
446 goto nla_put_failure;
448 genlmsg_end(msg, hdr);
449 err = genlmsg_unicast(msg, info->snd_pid);
450 goto out;
452 nla_put_failure:
453 err = -ENOBUFS;
454 nlmsg_free(msg);
455 out:
456 cfg80211_put_dev(drv);
457 dev_put(dev);
458 return err;
461 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
463 struct cfg80211_registered_device *drv;
464 int err;
465 struct net_device *dev;
466 u8 key_idx;
468 if (!info->attrs[NL80211_ATTR_KEY_IDX])
469 return -EINVAL;
471 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
473 if (key_idx > 3)
474 return -EINVAL;
476 /* currently only support setting default key */
477 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT])
478 return -EINVAL;
480 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
481 if (err)
482 return err;
484 if (!drv->ops->set_default_key) {
485 err = -EOPNOTSUPP;
486 goto out;
489 rtnl_lock();
490 err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx);
491 rtnl_unlock();
493 out:
494 cfg80211_put_dev(drv);
495 dev_put(dev);
496 return err;
499 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
501 struct cfg80211_registered_device *drv;
502 int err;
503 struct net_device *dev;
504 struct key_params params;
505 u8 key_idx = 0;
506 u8 *mac_addr = NULL;
508 memset(&params, 0, sizeof(params));
510 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
511 return -EINVAL;
513 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
514 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
515 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
518 if (info->attrs[NL80211_ATTR_KEY_IDX])
519 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
521 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
523 if (info->attrs[NL80211_ATTR_MAC])
524 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
526 if (key_idx > 3)
527 return -EINVAL;
530 * Disallow pairwise keys with non-zero index unless it's WEP
531 * (because current deployments use pairwise WEP keys with
532 * non-zero indizes but 802.11i clearly specifies to use zero)
534 if (mac_addr && key_idx &&
535 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
536 params.cipher != WLAN_CIPHER_SUITE_WEP104)
537 return -EINVAL;
539 /* TODO: add definitions for the lengths to linux/ieee80211.h */
540 switch (params.cipher) {
541 case WLAN_CIPHER_SUITE_WEP40:
542 if (params.key_len != 5)
543 return -EINVAL;
544 break;
545 case WLAN_CIPHER_SUITE_TKIP:
546 if (params.key_len != 32)
547 return -EINVAL;
548 break;
549 case WLAN_CIPHER_SUITE_CCMP:
550 if (params.key_len != 16)
551 return -EINVAL;
552 break;
553 case WLAN_CIPHER_SUITE_WEP104:
554 if (params.key_len != 13)
555 return -EINVAL;
556 break;
557 default:
558 return -EINVAL;
561 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
562 if (err)
563 return err;
565 if (!drv->ops->add_key) {
566 err = -EOPNOTSUPP;
567 goto out;
570 rtnl_lock();
571 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
572 rtnl_unlock();
574 out:
575 cfg80211_put_dev(drv);
576 dev_put(dev);
577 return err;
580 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
582 struct cfg80211_registered_device *drv;
583 int err;
584 struct net_device *dev;
585 u8 key_idx = 0;
586 u8 *mac_addr = NULL;
588 if (info->attrs[NL80211_ATTR_KEY_IDX])
589 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
591 if (key_idx > 3)
592 return -EINVAL;
594 if (info->attrs[NL80211_ATTR_MAC])
595 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
597 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
598 if (err)
599 return err;
601 if (!drv->ops->del_key) {
602 err = -EOPNOTSUPP;
603 goto out;
606 rtnl_lock();
607 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
608 rtnl_unlock();
610 out:
611 cfg80211_put_dev(drv);
612 dev_put(dev);
613 return err;
616 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
618 int (*call)(struct wiphy *wiphy, struct net_device *dev,
619 struct beacon_parameters *info);
620 struct cfg80211_registered_device *drv;
621 int err;
622 struct net_device *dev;
623 struct beacon_parameters params;
624 int haveinfo = 0;
626 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
627 if (err)
628 return err;
630 switch (info->genlhdr->cmd) {
631 case NL80211_CMD_NEW_BEACON:
632 /* these are required for NEW_BEACON */
633 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
634 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
635 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
636 err = -EINVAL;
637 goto out;
640 call = drv->ops->add_beacon;
641 break;
642 case NL80211_CMD_SET_BEACON:
643 call = drv->ops->set_beacon;
644 break;
645 default:
646 WARN_ON(1);
647 err = -EOPNOTSUPP;
648 goto out;
651 if (!call) {
652 err = -EOPNOTSUPP;
653 goto out;
656 memset(&params, 0, sizeof(params));
658 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
659 params.interval =
660 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
661 haveinfo = 1;
664 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
665 params.dtim_period =
666 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
667 haveinfo = 1;
670 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
671 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
672 params.head_len =
673 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
674 haveinfo = 1;
677 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
678 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
679 params.tail_len =
680 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
681 haveinfo = 1;
684 if (!haveinfo) {
685 err = -EINVAL;
686 goto out;
689 rtnl_lock();
690 err = call(&drv->wiphy, dev, &params);
691 rtnl_unlock();
693 out:
694 cfg80211_put_dev(drv);
695 dev_put(dev);
696 return err;
699 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
701 struct cfg80211_registered_device *drv;
702 int err;
703 struct net_device *dev;
705 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
706 if (err)
707 return err;
709 if (!drv->ops->del_beacon) {
710 err = -EOPNOTSUPP;
711 goto out;
714 rtnl_lock();
715 err = drv->ops->del_beacon(&drv->wiphy, dev);
716 rtnl_unlock();
718 out:
719 cfg80211_put_dev(drv);
720 dev_put(dev);
721 return err;
724 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
725 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
726 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
727 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
730 static int parse_station_flags(struct nlattr *nla, u32 *staflags)
732 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
733 int flag;
735 *staflags = 0;
737 if (!nla)
738 return 0;
740 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
741 nla, sta_flags_policy))
742 return -EINVAL;
744 *staflags = STATION_FLAG_CHANGED;
746 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
747 if (flags[flag])
748 *staflags |= (1<<flag);
750 return 0;
753 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
754 int flags, struct net_device *dev,
755 u8 *mac_addr, struct station_stats *stats)
757 void *hdr;
758 struct nlattr *statsattr;
760 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
761 if (!hdr)
762 return -1;
764 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
765 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
767 statsattr = nla_nest_start(msg, NL80211_ATTR_STA_STATS);
768 if (!statsattr)
769 goto nla_put_failure;
770 if (stats->filled & STATION_STAT_INACTIVE_TIME)
771 NLA_PUT_U32(msg, NL80211_STA_STAT_INACTIVE_TIME,
772 stats->inactive_time);
773 if (stats->filled & STATION_STAT_RX_BYTES)
774 NLA_PUT_U32(msg, NL80211_STA_STAT_RX_BYTES,
775 stats->rx_bytes);
776 if (stats->filled & STATION_STAT_TX_BYTES)
777 NLA_PUT_U32(msg, NL80211_STA_STAT_TX_BYTES,
778 stats->tx_bytes);
780 nla_nest_end(msg, statsattr);
782 return genlmsg_end(msg, hdr);
784 nla_put_failure:
785 return genlmsg_cancel(msg, hdr);
789 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
791 struct cfg80211_registered_device *drv;
792 int err;
793 struct net_device *dev;
794 struct station_stats stats;
795 struct sk_buff *msg;
796 u8 *mac_addr = NULL;
798 memset(&stats, 0, sizeof(stats));
800 if (!info->attrs[NL80211_ATTR_MAC])
801 return -EINVAL;
803 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
805 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
806 if (err)
807 return err;
809 if (!drv->ops->get_station) {
810 err = -EOPNOTSUPP;
811 goto out;
814 rtnl_lock();
815 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &stats);
816 rtnl_unlock();
818 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
819 if (!msg)
820 goto out;
822 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
823 dev, mac_addr, &stats) < 0)
824 goto out_free;
826 err = genlmsg_unicast(msg, info->snd_pid);
827 goto out;
829 out_free:
830 nlmsg_free(msg);
832 out:
833 cfg80211_put_dev(drv);
834 dev_put(dev);
835 return err;
839 * Get vlan interface making sure it is on the right wiphy.
841 static int get_vlan(struct nlattr *vlanattr,
842 struct cfg80211_registered_device *rdev,
843 struct net_device **vlan)
845 *vlan = NULL;
847 if (vlanattr) {
848 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
849 if (!*vlan)
850 return -ENODEV;
851 if (!(*vlan)->ieee80211_ptr)
852 return -EINVAL;
853 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
854 return -EINVAL;
856 return 0;
859 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
861 struct cfg80211_registered_device *drv;
862 int err;
863 struct net_device *dev;
864 struct station_parameters params;
865 u8 *mac_addr = NULL;
867 memset(&params, 0, sizeof(params));
869 params.listen_interval = -1;
871 if (info->attrs[NL80211_ATTR_STA_AID])
872 return -EINVAL;
874 if (!info->attrs[NL80211_ATTR_MAC])
875 return -EINVAL;
877 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
879 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
880 params.supported_rates =
881 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
882 params.supported_rates_len =
883 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
886 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
887 params.listen_interval =
888 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
890 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
891 &params.station_flags))
892 return -EINVAL;
894 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
895 if (err)
896 return err;
898 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
899 if (err)
900 goto out;
902 if (!drv->ops->change_station) {
903 err = -EOPNOTSUPP;
904 goto out;
907 rtnl_lock();
908 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
909 rtnl_unlock();
911 out:
912 if (params.vlan)
913 dev_put(params.vlan);
914 cfg80211_put_dev(drv);
915 dev_put(dev);
916 return err;
919 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
921 struct cfg80211_registered_device *drv;
922 int err;
923 struct net_device *dev;
924 struct station_parameters params;
925 u8 *mac_addr = NULL;
927 memset(&params, 0, sizeof(params));
929 if (!info->attrs[NL80211_ATTR_MAC])
930 return -EINVAL;
932 if (!info->attrs[NL80211_ATTR_STA_AID])
933 return -EINVAL;
935 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
936 return -EINVAL;
938 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
939 return -EINVAL;
941 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
942 params.supported_rates =
943 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
944 params.supported_rates_len =
945 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
946 params.listen_interval =
947 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
948 params.listen_interval = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
950 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
951 &params.station_flags))
952 return -EINVAL;
954 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
955 if (err)
956 return err;
958 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
959 if (err)
960 goto out;
962 if (!drv->ops->add_station) {
963 err = -EOPNOTSUPP;
964 goto out;
967 rtnl_lock();
968 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
969 rtnl_unlock();
971 out:
972 if (params.vlan)
973 dev_put(params.vlan);
974 cfg80211_put_dev(drv);
975 dev_put(dev);
976 return err;
979 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
981 struct cfg80211_registered_device *drv;
982 int err;
983 struct net_device *dev;
984 u8 *mac_addr = NULL;
986 if (info->attrs[NL80211_ATTR_MAC])
987 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
989 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
990 if (err)
991 return err;
993 if (!drv->ops->del_station) {
994 err = -EOPNOTSUPP;
995 goto out;
998 rtnl_lock();
999 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1000 rtnl_unlock();
1002 out:
1003 cfg80211_put_dev(drv);
1004 dev_put(dev);
1005 return err;
1008 static struct genl_ops nl80211_ops[] = {
1010 .cmd = NL80211_CMD_GET_WIPHY,
1011 .doit = nl80211_get_wiphy,
1012 .dumpit = nl80211_dump_wiphy,
1013 .policy = nl80211_policy,
1014 /* can be retrieved by unprivileged users */
1017 .cmd = NL80211_CMD_SET_WIPHY,
1018 .doit = nl80211_set_wiphy,
1019 .policy = nl80211_policy,
1020 .flags = GENL_ADMIN_PERM,
1023 .cmd = NL80211_CMD_GET_INTERFACE,
1024 .doit = nl80211_get_interface,
1025 .dumpit = nl80211_dump_interface,
1026 .policy = nl80211_policy,
1027 /* can be retrieved by unprivileged users */
1030 .cmd = NL80211_CMD_SET_INTERFACE,
1031 .doit = nl80211_set_interface,
1032 .policy = nl80211_policy,
1033 .flags = GENL_ADMIN_PERM,
1036 .cmd = NL80211_CMD_NEW_INTERFACE,
1037 .doit = nl80211_new_interface,
1038 .policy = nl80211_policy,
1039 .flags = GENL_ADMIN_PERM,
1042 .cmd = NL80211_CMD_DEL_INTERFACE,
1043 .doit = nl80211_del_interface,
1044 .policy = nl80211_policy,
1045 .flags = GENL_ADMIN_PERM,
1048 .cmd = NL80211_CMD_GET_KEY,
1049 .doit = nl80211_get_key,
1050 .policy = nl80211_policy,
1051 .flags = GENL_ADMIN_PERM,
1054 .cmd = NL80211_CMD_SET_KEY,
1055 .doit = nl80211_set_key,
1056 .policy = nl80211_policy,
1057 .flags = GENL_ADMIN_PERM,
1060 .cmd = NL80211_CMD_NEW_KEY,
1061 .doit = nl80211_new_key,
1062 .policy = nl80211_policy,
1063 .flags = GENL_ADMIN_PERM,
1066 .cmd = NL80211_CMD_DEL_KEY,
1067 .doit = nl80211_del_key,
1068 .policy = nl80211_policy,
1069 .flags = GENL_ADMIN_PERM,
1072 .cmd = NL80211_CMD_SET_BEACON,
1073 .policy = nl80211_policy,
1074 .flags = GENL_ADMIN_PERM,
1075 .doit = nl80211_addset_beacon,
1078 .cmd = NL80211_CMD_NEW_BEACON,
1079 .policy = nl80211_policy,
1080 .flags = GENL_ADMIN_PERM,
1081 .doit = nl80211_addset_beacon,
1084 .cmd = NL80211_CMD_DEL_BEACON,
1085 .policy = nl80211_policy,
1086 .flags = GENL_ADMIN_PERM,
1087 .doit = nl80211_del_beacon,
1090 .cmd = NL80211_CMD_GET_STATION,
1091 .doit = nl80211_get_station,
1092 /* TODO: implement dumpit */
1093 .policy = nl80211_policy,
1094 .flags = GENL_ADMIN_PERM,
1097 .cmd = NL80211_CMD_SET_STATION,
1098 .doit = nl80211_set_station,
1099 .policy = nl80211_policy,
1100 .flags = GENL_ADMIN_PERM,
1103 .cmd = NL80211_CMD_NEW_STATION,
1104 .doit = nl80211_new_station,
1105 .policy = nl80211_policy,
1106 .flags = GENL_ADMIN_PERM,
1109 .cmd = NL80211_CMD_DEL_STATION,
1110 .doit = nl80211_del_station,
1111 .policy = nl80211_policy,
1112 .flags = GENL_ADMIN_PERM,
1116 /* multicast groups */
1117 static struct genl_multicast_group nl80211_config_mcgrp = {
1118 .name = "config",
1121 /* notification functions */
1123 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
1125 struct sk_buff *msg;
1127 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1128 if (!msg)
1129 return;
1131 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
1132 nlmsg_free(msg);
1133 return;
1136 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
1139 /* initialisation/exit functions */
1141 int nl80211_init(void)
1143 int err, i;
1145 err = genl_register_family(&nl80211_fam);
1146 if (err)
1147 return err;
1149 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
1150 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
1151 if (err)
1152 goto err_out;
1155 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
1156 if (err)
1157 goto err_out;
1159 return 0;
1160 err_out:
1161 genl_unregister_family(&nl80211_fam);
1162 return err;
1165 void nl80211_exit(void)
1167 genl_unregister_family(&nl80211_fam);