2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.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>
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)
38 if (!info->attrs[NL80211_ATTR_IFINDEX])
41 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
42 *dev = dev_get_by_index(&init_net, ifindex);
46 *drv = cfg80211_get_dev_from_ifindex(ifindex);
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 },
74 /* message building helper */
75 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
78 /* since there is no private header just add the generic one */
79 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
82 /* netlink command implementations */
84 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
85 struct cfg80211_registered_device *dev)
89 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
93 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx);
94 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
95 return genlmsg_end(msg, hdr);
98 return genlmsg_cancel(msg, hdr);
101 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
104 int start = cb->args[0];
105 struct cfg80211_registered_device *dev;
107 mutex_lock(&cfg80211_drv_mutex);
108 list_for_each_entry(dev, &cfg80211_drv_list, list) {
111 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
112 cb->nlh->nlmsg_seq, NLM_F_MULTI,
116 mutex_unlock(&cfg80211_drv_mutex);
123 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
126 struct cfg80211_registered_device *dev;
128 dev = cfg80211_get_dev_from_info(info);
132 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
136 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
139 cfg80211_put_dev(dev);
141 return genlmsg_unicast(msg, info->snd_pid);
146 cfg80211_put_dev(dev);
150 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
152 struct cfg80211_registered_device *rdev;
155 if (!info->attrs[NL80211_ATTR_WIPHY_NAME])
158 rdev = cfg80211_get_dev_from_info(info);
160 return PTR_ERR(rdev);
162 result = cfg80211_dev_rename(rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
164 cfg80211_put_dev(rdev);
169 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
170 struct net_device *dev)
174 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
178 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
179 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
180 /* TODO: interface type */
181 return genlmsg_end(msg, hdr);
184 return genlmsg_cancel(msg, hdr);
187 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
191 int wp_start = cb->args[0];
192 int if_start = cb->args[1];
193 struct cfg80211_registered_device *dev;
194 struct wireless_dev *wdev;
196 mutex_lock(&cfg80211_drv_mutex);
197 list_for_each_entry(dev, &cfg80211_drv_list, list) {
198 if (++wp_idx < wp_start)
202 mutex_lock(&dev->devlist_mtx);
203 list_for_each_entry(wdev, &dev->netdev_list, list) {
204 if (++if_idx < if_start)
206 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
207 cb->nlh->nlmsg_seq, NLM_F_MULTI,
211 mutex_unlock(&dev->devlist_mtx);
213 mutex_unlock(&cfg80211_drv_mutex);
215 cb->args[0] = wp_idx;
216 cb->args[1] = if_idx;
221 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
224 struct cfg80211_registered_device *dev;
225 struct net_device *netdev;
228 err = get_drv_dev_by_info_ifindex(info, &dev, &netdev);
232 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
236 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
240 cfg80211_put_dev(dev);
242 return genlmsg_unicast(msg, info->snd_pid);
248 cfg80211_put_dev(dev);
252 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
254 struct cfg80211_registered_device *drv;
256 enum nl80211_iftype type;
257 struct net_device *dev;
259 if (info->attrs[NL80211_ATTR_IFTYPE]) {
260 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
261 if (type > NL80211_IFTYPE_MAX)
266 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
269 ifindex = dev->ifindex;
272 if (!drv->ops->change_virtual_intf) {
278 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex, type);
282 cfg80211_put_dev(drv);
286 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
288 struct cfg80211_registered_device *drv;
290 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
292 if (!info->attrs[NL80211_ATTR_IFNAME])
295 if (info->attrs[NL80211_ATTR_IFTYPE]) {
296 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
297 if (type > NL80211_IFTYPE_MAX)
301 drv = cfg80211_get_dev_from_info(info);
305 if (!drv->ops->add_virtual_intf) {
311 err = drv->ops->add_virtual_intf(&drv->wiphy,
312 nla_data(info->attrs[NL80211_ATTR_IFNAME]), type);
316 cfg80211_put_dev(drv);
320 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
322 struct cfg80211_registered_device *drv;
324 struct net_device *dev;
326 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
329 ifindex = dev->ifindex;
332 if (!drv->ops->del_virtual_intf) {
338 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
342 cfg80211_put_dev(drv);
346 struct get_key_cookie {
351 static void get_key_callback(void *c, struct key_params *params)
353 struct get_key_cookie *cookie = c;
356 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
357 params->key_len, params->key);
360 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
361 params->seq_len, params->seq);
364 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
372 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
374 struct cfg80211_registered_device *drv;
376 struct net_device *dev;
379 struct get_key_cookie cookie = {
385 if (info->attrs[NL80211_ATTR_KEY_IDX])
386 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
391 if (info->attrs[NL80211_ATTR_MAC])
392 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
394 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
398 if (!drv->ops->get_key) {
403 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
409 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
410 NL80211_CMD_NEW_KEY);
419 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
420 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
422 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
425 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
426 &cookie, get_key_callback);
433 goto nla_put_failure;
435 genlmsg_end(msg, hdr);
436 err = genlmsg_unicast(msg, info->snd_pid);
443 cfg80211_put_dev(drv);
448 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
450 struct cfg80211_registered_device *drv;
452 struct net_device *dev;
455 if (!info->attrs[NL80211_ATTR_KEY_IDX])
458 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
463 /* currently only support setting default key */
464 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT])
467 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
471 if (!drv->ops->set_default_key) {
477 err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx);
481 cfg80211_put_dev(drv);
486 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
488 struct cfg80211_registered_device *drv;
490 struct net_device *dev;
491 struct key_params params;
495 memset(¶ms, 0, sizeof(params));
497 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
500 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
501 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
502 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
505 if (info->attrs[NL80211_ATTR_KEY_IDX])
506 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
508 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
510 if (info->attrs[NL80211_ATTR_MAC])
511 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
517 * Disallow pairwise keys with non-zero index unless it's WEP
518 * (because current deployments use pairwise WEP keys with
519 * non-zero indizes but 802.11i clearly specifies to use zero)
521 if (mac_addr && key_idx &&
522 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
523 params.cipher != WLAN_CIPHER_SUITE_WEP104)
526 /* TODO: add definitions for the lengths to linux/ieee80211.h */
527 switch (params.cipher) {
528 case WLAN_CIPHER_SUITE_WEP40:
529 if (params.key_len != 5)
532 case WLAN_CIPHER_SUITE_TKIP:
533 if (params.key_len != 32)
536 case WLAN_CIPHER_SUITE_CCMP:
537 if (params.key_len != 16)
540 case WLAN_CIPHER_SUITE_WEP104:
541 if (params.key_len != 13)
548 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
552 if (!drv->ops->add_key) {
558 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms);
562 cfg80211_put_dev(drv);
567 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
569 struct cfg80211_registered_device *drv;
571 struct net_device *dev;
575 if (info->attrs[NL80211_ATTR_KEY_IDX])
576 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
581 if (info->attrs[NL80211_ATTR_MAC])
582 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
584 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
588 if (!drv->ops->del_key) {
594 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
598 cfg80211_put_dev(drv);
603 static struct genl_ops nl80211_ops[] = {
605 .cmd = NL80211_CMD_GET_WIPHY,
606 .doit = nl80211_get_wiphy,
607 .dumpit = nl80211_dump_wiphy,
608 .policy = nl80211_policy,
609 /* can be retrieved by unprivileged users */
612 .cmd = NL80211_CMD_SET_WIPHY,
613 .doit = nl80211_set_wiphy,
614 .policy = nl80211_policy,
615 .flags = GENL_ADMIN_PERM,
618 .cmd = NL80211_CMD_GET_INTERFACE,
619 .doit = nl80211_get_interface,
620 .dumpit = nl80211_dump_interface,
621 .policy = nl80211_policy,
622 /* can be retrieved by unprivileged users */
625 .cmd = NL80211_CMD_SET_INTERFACE,
626 .doit = nl80211_set_interface,
627 .policy = nl80211_policy,
628 .flags = GENL_ADMIN_PERM,
631 .cmd = NL80211_CMD_NEW_INTERFACE,
632 .doit = nl80211_new_interface,
633 .policy = nl80211_policy,
634 .flags = GENL_ADMIN_PERM,
637 .cmd = NL80211_CMD_DEL_INTERFACE,
638 .doit = nl80211_del_interface,
639 .policy = nl80211_policy,
640 .flags = GENL_ADMIN_PERM,
643 .cmd = NL80211_CMD_GET_KEY,
644 .doit = nl80211_get_key,
645 .policy = nl80211_policy,
646 .flags = GENL_ADMIN_PERM,
649 .cmd = NL80211_CMD_SET_KEY,
650 .doit = nl80211_set_key,
651 .policy = nl80211_policy,
652 .flags = GENL_ADMIN_PERM,
655 .cmd = NL80211_CMD_NEW_KEY,
656 .doit = nl80211_new_key,
657 .policy = nl80211_policy,
658 .flags = GENL_ADMIN_PERM,
661 .cmd = NL80211_CMD_DEL_KEY,
662 .doit = nl80211_del_key,
663 .policy = nl80211_policy,
664 .flags = GENL_ADMIN_PERM,
668 /* multicast groups */
669 static struct genl_multicast_group nl80211_config_mcgrp = {
673 /* notification functions */
675 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
679 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
683 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
688 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
691 /* initialisation/exit functions */
693 int nl80211_init(void)
697 err = genl_register_family(&nl80211_fam);
701 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
702 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
707 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
713 genl_unregister_family(&nl80211_fam);
717 void nl80211_exit(void)
719 genl_unregister_family(&nl80211_fam);