Skip to content

Commit 5568296

Browse files
jmbergDavid S. Miller
authored andcommitted
[NL80211]: add netlink interface to cfg80211
Signed-off-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: John W. Linville <linville@tuxdriver.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0800f17 commit 5568296

10 files changed

Lines changed: 762 additions & 9 deletions

File tree

‎include/linux/nl80211.h‎

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,109 @@
66
* Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
77
*/
88

9+
/**
10+
* enum nl80211_commands - supported nl80211 commands
11+
*
12+
* @NL80211_CMD_UNSPEC: unspecified command to catch errors
13+
*
14+
* @NL80211_CMD_GET_WIPHY: request information about a wiphy or dump request
15+
* to get a list of all present wiphys.
16+
* @NL80211_CMD_SET_WIPHY: set wiphy name, needs %NL80211_ATTR_WIPHY and
17+
* %NL80211_ATTR_WIPHY_NAME.
18+
* @NL80211_CMD_NEW_WIPHY: Newly created wiphy, response to get request
19+
* or rename notification. Has attributes %NL80211_ATTR_WIPHY and
20+
* %NL80211_ATTR_WIPHY_NAME.
21+
* @NL80211_CMD_DEL_WIPHY: Wiphy deleted. Has attributes
22+
* %NL80211_ATTR_WIPHY and %NL80211_ATTR_WIPHY_NAME.
23+
*
24+
* @NL80211_CMD_GET_INTERFACE: Request an interface's configuration;
25+
* either a dump request on a %NL80211_ATTR_WIPHY or a specific get
26+
* on an %NL80211_ATTR_IFINDEX is supported.
27+
* @NL80211_CMD_SET_INTERFACE: Set type of a virtual interface, requires
28+
* %NL80211_ATTR_IFINDEX and %NL80211_ATTR_IFTYPE.
29+
* @NL80211_CMD_NEW_INTERFACE: Newly created virtual interface or response
30+
* to %NL80211_CMD_GET_INTERFACE. Has %NL80211_ATTR_IFINDEX,
31+
* %NL80211_ATTR_WIPHY and %NL80211_ATTR_IFTYPE attributes. Can also
32+
* be sent from userspace to request creation of a new virtual interface,
33+
* then requires attributes %NL80211_ATTR_WIPHY, %NL80211_ATTR_IFTYPE and
34+
* %NL80211_ATTR_IFNAME.
35+
* @NL80211_CMD_DEL_INTERFACE: Virtual interface was deleted, has attributes
36+
* %NL80211_ATTR_IFINDEX and %NL80211_ATTR_WIPHY. Can also be sent from
37+
* userspace to request deletion of a virtual interface, then requires
38+
* attribute %NL80211_ATTR_IFINDEX.
39+
*
40+
* @NL80211_CMD_MAX: highest used command number
41+
* @__NL80211_CMD_AFTER_LAST: internal use
42+
*/
43+
enum nl80211_commands {
44+
/* don't change the order or add anything inbetween, this is ABI! */
45+
NL80211_CMD_UNSPEC,
46+
47+
NL80211_CMD_GET_WIPHY, /* can dump */
48+
NL80211_CMD_SET_WIPHY,
49+
NL80211_CMD_NEW_WIPHY,
50+
NL80211_CMD_DEL_WIPHY,
51+
52+
NL80211_CMD_GET_INTERFACE, /* can dump */
53+
NL80211_CMD_SET_INTERFACE,
54+
NL80211_CMD_NEW_INTERFACE,
55+
NL80211_CMD_DEL_INTERFACE,
56+
57+
/* add commands here */
58+
59+
/* used to define NL80211_CMD_MAX below */
60+
__NL80211_CMD_AFTER_LAST,
61+
NL80211_CMD_MAX = __NL80211_CMD_AFTER_LAST - 1
62+
};
63+
64+
65+
/**
66+
* enum nl80211_attrs - nl80211 netlink attributes
67+
*
68+
* @NL80211_ATTR_UNSPEC: unspecified attribute to catch errors
69+
*
70+
* @NL80211_ATTR_WIPHY: index of wiphy to operate on, cf.
71+
* /sys/class/ieee80211/<phyname>/index
72+
* @NL80211_ATTR_WIPHY_NAME: wiphy name (used for renaming)
73+
*
74+
* @NL80211_ATTR_IFINDEX: network interface index of the device to operate on
75+
* @NL80211_ATTR_IFNAME: network interface name
76+
* @NL80211_ATTR_IFTYPE: type of virtual interface, see &enum nl80211_iftype
77+
*
78+
* @NL80211_ATTR_MAX: highest attribute number currently defined
79+
* @__NL80211_ATTR_AFTER_LAST: internal use
80+
*/
81+
enum nl80211_attrs {
82+
/* don't change the order or add anything inbetween, this is ABI! */
83+
NL80211_ATTR_UNSPEC,
84+
85+
NL80211_ATTR_WIPHY,
86+
NL80211_ATTR_WIPHY_NAME,
87+
88+
NL80211_ATTR_IFINDEX,
89+
NL80211_ATTR_IFNAME,
90+
NL80211_ATTR_IFTYPE,
91+
92+
/* add attributes here, update the policy in nl80211.c */
93+
94+
__NL80211_ATTR_AFTER_LAST,
95+
NL80211_ATTR_MAX = __NL80211_ATTR_AFTER_LAST - 1
96+
};
97+
998
/**
1099
* enum nl80211_iftype - (virtual) interface types
100+
*
11101
* @NL80211_IFTYPE_UNSPECIFIED: unspecified type, driver decides
12102
* @NL80211_IFTYPE_ADHOC: independent BSS member
13103
* @NL80211_IFTYPE_STATION: managed BSS member
14104
* @NL80211_IFTYPE_AP: access point
15105
* @NL80211_IFTYPE_AP_VLAN: VLAN interface for access points
16106
* @NL80211_IFTYPE_WDS: wireless distribution interface
17107
* @NL80211_IFTYPE_MONITOR: monitor interface receiving all frames
108+
* @NL80211_IFTYPE_MAX: highest interface type number currently defined
18109
* @__NL80211_IFTYPE_AFTER_LAST: internal use
19110
*
20-
* These values are used with the NL80211_ATTR_IFTYPE
111+
* These values are used with the %NL80211_ATTR_IFTYPE
21112
* to set the type of an interface.
22113
*
23114
*/
@@ -31,8 +122,8 @@ enum nl80211_iftype {
31122
NL80211_IFTYPE_MONITOR,
32123

33124
/* keep last */
34-
__NL80211_IFTYPE_AFTER_LAST
125+
__NL80211_IFTYPE_AFTER_LAST,
126+
NL80211_IFTYPE_MAX = __NL80211_IFTYPE_AFTER_LAST - 1
35127
};
36-
#define NL80211_IFTYPE_MAX (__NL80211_IFTYPE_AFTER_LAST - 1)
37128

38129
#endif /* __LINUX_NL80211_H */

‎include/net/cfg80211.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
#include <linux/netlink.h>
55
#include <linux/skbuff.h>
6+
#include <linux/nl80211.h>
67
#include <net/genetlink.h>
78

89
/*
910
* 802.11 configuration in-kernel interface
1011
*
11-
* Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
12+
* Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
1213
*/
1314

14-
1515
/* Radiotap header iteration
1616
* implemented in net/wireless/radiotap.c
1717
* docs in Documentation/networking/radiotap-headers.txt
@@ -68,11 +68,16 @@ struct wiphy;
6868
* @add_virtual_intf: create a new virtual interface with the given name
6969
*
7070
* @del_virtual_intf: remove the virtual interface determined by ifindex.
71+
*
72+
* @change_virtual_intf: change type of virtual interface
73+
*
7174
*/
7275
struct cfg80211_ops {
7376
int (*add_virtual_intf)(struct wiphy *wiphy, char *name,
74-
unsigned int type);
77+
enum nl80211_iftype type);
7578
int (*del_virtual_intf)(struct wiphy *wiphy, int ifindex);
79+
int (*change_virtual_intf)(struct wiphy *wiphy, int ifindex,
80+
enum nl80211_iftype type);
7681
};
7782

7883
#endif /* __NET_CFG80211_H */

‎include/net/iw_handler.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,13 @@ struct iw_public_data {
431431
* Those may be called only within the kernel.
432432
*/
433433

434-
/* functions that may be called by driver modules */
434+
/* First : function strictly used inside the kernel */
435+
436+
/* Handle /proc/net/wireless, called in net/code/dev.c */
437+
extern int dev_get_wireless_info(char * buffer, char **start, off_t offset,
438+
int length);
439+
440+
/* Second : functions that may be called by driver modules */
435441

436442
/* Send a single event to user space */
437443
extern void wireless_send_event(struct net_device * dev,

‎net/mac80211/ieee80211_cfg.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "ieee80211_cfg.h"
1515

1616
static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
17-
unsigned int type)
17+
enum nl80211_iftype type)
1818
{
1919
struct ieee80211_local *local = wiphy_priv(wiphy);
2020
int itype;

‎net/wireless/Kconfig‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
config CFG80211
22
tristate "Improved wireless configuration API"
33

4+
config NL80211
5+
bool "nl80211 new netlink interface support"
6+
depends CFG80211
7+
default y
8+
---help---
9+
This option turns on the new netlink interface
10+
(nl80211) support in cfg80211.
11+
12+
If =n, drivers using mac80211 will be configured via
13+
wireless extension support provided by that subsystem.
14+
15+
If unsure, say Y.
16+
417
config WIRELESS_EXT
518
bool "Wireless extensions"
619
default n
@@ -10,7 +23,9 @@ config WIRELESS_EXT
1023

1124
Wireless extensions will be replaced by cfg80211 and
1225
will be required only by legacy drivers that implement
13-
wireless extension handlers.
26+
wireless extension handlers. This option does not
27+
affect the wireless-extension backward compatibility
28+
code in cfg80211.
1429

1530
Say N (if you can) unless you know you need wireless
1631
extensions for external modules.

‎net/wireless/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ obj-$(CONFIG_WIRELESS_EXT) += wext.o
22
obj-$(CONFIG_CFG80211) += cfg80211.o
33

44
cfg80211-y += core.o sysfs.o radiotap.o
5+
cfg80211-$(CONFIG_NL80211) += nl80211.o

‎net/wireless/core.c‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <net/genetlink.h>
1717
#include <net/cfg80211.h>
1818
#include <net/wireless.h>
19+
#include "nl80211.h"
1920
#include "core.h"
2021
#include "sysfs.h"
2122

@@ -36,6 +37,146 @@ static int wiphy_counter;
3637
/* for debugfs */
3738
static struct dentry *ieee80211_debugfs_dir;
3839

40+
/* requires cfg80211_drv_mutex to be held! */
41+
static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
42+
{
43+
struct cfg80211_registered_device *result = NULL, *drv;
44+
45+
list_for_each_entry(drv, &cfg80211_drv_list, list) {
46+
if (drv->idx == wiphy) {
47+
result = drv;
48+
break;
49+
}
50+
}
51+
52+
return result;
53+
}
54+
55+
/* requires cfg80211_drv_mutex to be held! */
56+
static struct cfg80211_registered_device *
57+
__cfg80211_drv_from_info(struct genl_info *info)
58+
{
59+
int ifindex;
60+
struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
61+
struct net_device *dev;
62+
int err = -EINVAL;
63+
64+
if (info->attrs[NL80211_ATTR_WIPHY]) {
65+
bywiphy = cfg80211_drv_by_wiphy(
66+
nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
67+
err = -ENODEV;
68+
}
69+
70+
if (info->attrs[NL80211_ATTR_IFINDEX]) {
71+
ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
72+
dev = dev_get_by_index(&init_net, ifindex);
73+
if (dev) {
74+
if (dev->ieee80211_ptr)
75+
byifidx =
76+
wiphy_to_dev(dev->ieee80211_ptr->wiphy);
77+
dev_put(dev);
78+
}
79+
err = -ENODEV;
80+
}
81+
82+
if (bywiphy && byifidx) {
83+
if (bywiphy != byifidx)
84+
return ERR_PTR(-EINVAL);
85+
else
86+
return bywiphy; /* == byifidx */
87+
}
88+
if (bywiphy)
89+
return bywiphy;
90+
91+
if (byifidx)
92+
return byifidx;
93+
94+
return ERR_PTR(err);
95+
}
96+
97+
struct cfg80211_registered_device *
98+
cfg80211_get_dev_from_info(struct genl_info *info)
99+
{
100+
struct cfg80211_registered_device *drv;
101+
102+
mutex_lock(&cfg80211_drv_mutex);
103+
drv = __cfg80211_drv_from_info(info);
104+
105+
/* if it is not an error we grab the lock on
106+
* it to assure it won't be going away while
107+
* we operate on it */
108+
if (!IS_ERR(drv))
109+
mutex_lock(&drv->mtx);
110+
111+
mutex_unlock(&cfg80211_drv_mutex);
112+
113+
return drv;
114+
}
115+
116+
struct cfg80211_registered_device *
117+
cfg80211_get_dev_from_ifindex(int ifindex)
118+
{
119+
struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
120+
struct net_device *dev;
121+
122+
mutex_lock(&cfg80211_drv_mutex);
123+
dev = dev_get_by_index(&init_net, ifindex);
124+
if (!dev)
125+
goto out;
126+
if (dev->ieee80211_ptr) {
127+
drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
128+
mutex_lock(&drv->mtx);
129+
} else
130+
drv = ERR_PTR(-ENODEV);
131+
dev_put(dev);
132+
out:
133+
mutex_unlock(&cfg80211_drv_mutex);
134+
return drv;
135+
}
136+
137+
void cfg80211_put_dev(struct cfg80211_registered_device *drv)
138+
{
139+
BUG_ON(IS_ERR(drv));
140+
mutex_unlock(&drv->mtx);
141+
}
142+
143+
int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
144+
char *newname)
145+
{
146+
int idx, taken = -1, result, digits;
147+
148+
/* prohibit calling the thing phy%d when %d is not its number */
149+
sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
150+
if (taken == strlen(newname) && idx != rdev->idx) {
151+
/* count number of places needed to print idx */
152+
digits = 1;
153+
while (idx /= 10)
154+
digits++;
155+
/*
156+
* deny the name if it is phy<idx> where <idx> is printed
157+
* without leading zeroes. taken == strlen(newname) here
158+
*/
159+
if (taken == strlen(PHY_NAME) + digits)
160+
return -EINVAL;
161+
}
162+
163+
/* this will check for collisions */
164+
result = device_rename(&rdev->wiphy.dev, newname);
165+
if (result)
166+
return result;
167+
168+
if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
169+
rdev->wiphy.debugfsdir,
170+
rdev->wiphy.debugfsdir->d_parent,
171+
newname))
172+
printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
173+
newname);
174+
175+
nl80211_notify_dev_rename(rdev);
176+
177+
return 0;
178+
}
179+
39180
/* exported functions */
40181

41182
struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
@@ -204,10 +345,16 @@ static int cfg80211_init(void)
204345
if (err)
205346
goto out_fail_notifier;
206347

348+
err = nl80211_init();
349+
if (err)
350+
goto out_fail_nl80211;
351+
207352
ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
208353

209354
return 0;
210355

356+
out_fail_nl80211:
357+
unregister_netdevice_notifier(&cfg80211_netdev_notifier);
211358
out_fail_notifier:
212359
wiphy_sysfs_exit();
213360
out_fail_sysfs:
@@ -218,6 +365,7 @@ subsys_initcall(cfg80211_init);
218365
static void cfg80211_exit(void)
219366
{
220367
debugfs_remove(ieee80211_debugfs_dir);
368+
nl80211_exit();
221369
unregister_netdevice_notifier(&cfg80211_netdev_notifier);
222370
wiphy_sysfs_exit();
223371
}

0 commit comments

Comments
 (0)