29f7b98ba1fb277e7c9e9cb88efbd1ecaef9e7c6
[openwrt/svn-archive/archive.git] / package / mac80211 / src / net / mac80211 / debugfs_netdev.c
1 /*
2 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
3 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/if.h>
13 #include <linux/interrupt.h>
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/notifier.h>
17 #include <net/mac80211.h>
18 #include <net/cfg80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
21 #include "debugfs.h"
22 #include "debugfs_netdev.h"
23
24 static ssize_t ieee80211_if_read(
25 struct ieee80211_sub_if_data *sdata,
26 char __user *userbuf,
27 size_t count, loff_t *ppos,
28 ssize_t (*format)(const struct ieee80211_sub_if_data *, char *, int))
29 {
30 char buf[70];
31 ssize_t ret = -EINVAL;
32
33 read_lock(&dev_base_lock);
34 if (sdata->dev->reg_state == NETREG_REGISTERED) {
35 ret = (*format)(sdata, buf, sizeof(buf));
36 ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);
37 }
38 read_unlock(&dev_base_lock);
39 return ret;
40 }
41
42 #define IEEE80211_IF_FMT(name, field, format_string) \
43 static ssize_t ieee80211_if_fmt_##name( \
44 const struct ieee80211_sub_if_data *sdata, char *buf, \
45 int buflen) \
46 { \
47 return scnprintf(buf, buflen, format_string, sdata->field); \
48 }
49 #define IEEE80211_IF_FMT_DEC(name, field) \
50 IEEE80211_IF_FMT(name, field, "%d\n")
51 #define IEEE80211_IF_FMT_HEX(name, field) \
52 IEEE80211_IF_FMT(name, field, "%#x\n")
53 #define IEEE80211_IF_FMT_SIZE(name, field) \
54 IEEE80211_IF_FMT(name, field, "%zd\n")
55
56 #define IEEE80211_IF_FMT_ATOMIC(name, field) \
57 static ssize_t ieee80211_if_fmt_##name( \
58 const struct ieee80211_sub_if_data *sdata, \
59 char *buf, int buflen) \
60 { \
61 return scnprintf(buf, buflen, "%d\n", atomic_read(&sdata->field));\
62 }
63
64 #define IEEE80211_IF_FMT_MAC(name, field) \
65 static ssize_t ieee80211_if_fmt_##name( \
66 const struct ieee80211_sub_if_data *sdata, char *buf, \
67 int buflen) \
68 { \
69 DECLARE_MAC_BUF(mac); \
70 return scnprintf(buf, buflen, "%s\n", print_mac(mac, sdata->field));\
71 }
72
73 #define __IEEE80211_IF_FILE(name) \
74 static ssize_t ieee80211_if_read_##name(struct file *file, \
75 char __user *userbuf, \
76 size_t count, loff_t *ppos) \
77 { \
78 return ieee80211_if_read(file->private_data, \
79 userbuf, count, ppos, \
80 ieee80211_if_fmt_##name); \
81 } \
82 static const struct file_operations name##_ops = { \
83 .read = ieee80211_if_read_##name, \
84 .open = mac80211_open_file_generic, \
85 }
86
87 #define IEEE80211_IF_FILE(name, field, format) \
88 IEEE80211_IF_FMT_##format(name, field) \
89 __IEEE80211_IF_FILE(name)
90
91 /* common attributes */
92 IEEE80211_IF_FILE(channel_use, channel_use, DEC);
93 IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
94
95 /* STA/IBSS attributes */
96 IEEE80211_IF_FILE(state, u.sta.state, DEC);
97 IEEE80211_IF_FILE(bssid, u.sta.bssid, MAC);
98 IEEE80211_IF_FILE(prev_bssid, u.sta.prev_bssid, MAC);
99 IEEE80211_IF_FILE(ssid_len, u.sta.ssid_len, SIZE);
100 IEEE80211_IF_FILE(aid, u.sta.aid, DEC);
101 IEEE80211_IF_FILE(ap_capab, u.sta.ap_capab, HEX);
102 IEEE80211_IF_FILE(capab, u.sta.capab, HEX);
103 IEEE80211_IF_FILE(extra_ie_len, u.sta.extra_ie_len, SIZE);
104 IEEE80211_IF_FILE(auth_tries, u.sta.auth_tries, DEC);
105 IEEE80211_IF_FILE(assoc_tries, u.sta.assoc_tries, DEC);
106 IEEE80211_IF_FILE(auth_algs, u.sta.auth_algs, HEX);
107 IEEE80211_IF_FILE(auth_alg, u.sta.auth_alg, DEC);
108 IEEE80211_IF_FILE(auth_transaction, u.sta.auth_transaction, DEC);
109
110 static ssize_t ieee80211_if_fmt_flags(
111 const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
112 {
113 return scnprintf(buf, buflen, "%s%s%s%s%s%s%s\n",
114 sdata->u.sta.flags & IEEE80211_STA_SSID_SET ? "SSID\n" : "",
115 sdata->u.sta.flags & IEEE80211_STA_BSSID_SET ? "BSSID\n" : "",
116 sdata->u.sta.flags & IEEE80211_STA_PREV_BSSID_SET ? "prev BSSID\n" : "",
117 sdata->u.sta.flags & IEEE80211_STA_AUTHENTICATED ? "AUTH\n" : "",
118 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED ? "ASSOC\n" : "",
119 sdata->u.sta.flags & IEEE80211_STA_PROBEREQ_POLL ? "PROBEREQ POLL\n" : "",
120 sdata->bss_conf.use_cts_prot ? "CTS prot\n" : "");
121 }
122 __IEEE80211_IF_FILE(flags);
123
124 /* AP attributes */
125 IEEE80211_IF_FILE(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
126 IEEE80211_IF_FILE(dtim_count, u.ap.dtim_count, DEC);
127 IEEE80211_IF_FILE(num_beacons, u.ap.num_beacons, DEC);
128 IEEE80211_IF_FILE(force_unicast_rateidx, u.ap.force_unicast_rateidx, DEC);
129 IEEE80211_IF_FILE(max_ratectrl_rateidx, u.ap.max_ratectrl_rateidx, DEC);
130
131 static ssize_t ieee80211_if_fmt_num_buffered_multicast(
132 const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
133 {
134 return scnprintf(buf, buflen, "%u\n",
135 skb_queue_len(&sdata->u.ap.ps_bc_buf));
136 }
137 __IEEE80211_IF_FILE(num_buffered_multicast);
138
139 /* WDS attributes */
140 IEEE80211_IF_FILE(peer, u.wds.remote_addr, MAC);
141
142 #define DEBUGFS_ADD(name, type)\
143 sdata->debugfs.type.name = debugfs_create_file(#name, 0444,\
144 sdata->debugfsdir, sdata, &name##_ops);
145
146 static void add_sta_files(struct ieee80211_sub_if_data *sdata)
147 {
148 DEBUGFS_ADD(channel_use, sta);
149 DEBUGFS_ADD(drop_unencrypted, sta);
150 DEBUGFS_ADD(state, sta);
151 DEBUGFS_ADD(bssid, sta);
152 DEBUGFS_ADD(prev_bssid, sta);
153 DEBUGFS_ADD(ssid_len, sta);
154 DEBUGFS_ADD(aid, sta);
155 DEBUGFS_ADD(ap_capab, sta);
156 DEBUGFS_ADD(capab, sta);
157 DEBUGFS_ADD(extra_ie_len, sta);
158 DEBUGFS_ADD(auth_tries, sta);
159 DEBUGFS_ADD(assoc_tries, sta);
160 DEBUGFS_ADD(auth_algs, sta);
161 DEBUGFS_ADD(auth_alg, sta);
162 DEBUGFS_ADD(auth_transaction, sta);
163 DEBUGFS_ADD(flags, sta);
164 }
165
166 static void add_ap_files(struct ieee80211_sub_if_data *sdata)
167 {
168 DEBUGFS_ADD(channel_use, ap);
169 DEBUGFS_ADD(drop_unencrypted, ap);
170 DEBUGFS_ADD(num_sta_ps, ap);
171 DEBUGFS_ADD(dtim_count, ap);
172 DEBUGFS_ADD(num_beacons, ap);
173 DEBUGFS_ADD(force_unicast_rateidx, ap);
174 DEBUGFS_ADD(max_ratectrl_rateidx, ap);
175 DEBUGFS_ADD(num_buffered_multicast, ap);
176 }
177
178 static void add_wds_files(struct ieee80211_sub_if_data *sdata)
179 {
180 DEBUGFS_ADD(channel_use, wds);
181 DEBUGFS_ADD(drop_unencrypted, wds);
182 DEBUGFS_ADD(peer, wds);
183 }
184
185 static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
186 {
187 DEBUGFS_ADD(channel_use, vlan);
188 DEBUGFS_ADD(drop_unencrypted, vlan);
189 }
190
191 static void add_monitor_files(struct ieee80211_sub_if_data *sdata)
192 {
193 }
194
195 static void add_files(struct ieee80211_sub_if_data *sdata)
196 {
197 if (!sdata->debugfsdir)
198 return;
199
200 switch (sdata->vif.type) {
201 case IEEE80211_IF_TYPE_STA:
202 case IEEE80211_IF_TYPE_IBSS:
203 add_sta_files(sdata);
204 break;
205 case IEEE80211_IF_TYPE_AP:
206 add_ap_files(sdata);
207 break;
208 case IEEE80211_IF_TYPE_WDS:
209 add_wds_files(sdata);
210 break;
211 case IEEE80211_IF_TYPE_MNTR:
212 add_monitor_files(sdata);
213 break;
214 case IEEE80211_IF_TYPE_VLAN:
215 add_vlan_files(sdata);
216 break;
217 default:
218 break;
219 }
220 }
221
222 #define DEBUGFS_DEL(name, type) \
223 do { \
224 debugfs_remove(sdata->debugfs.type.name); \
225 sdata->debugfs.type.name = NULL; \
226 } while (0)
227
228 static void del_sta_files(struct ieee80211_sub_if_data *sdata)
229 {
230 DEBUGFS_DEL(channel_use, sta);
231 DEBUGFS_DEL(drop_unencrypted, sta);
232 DEBUGFS_DEL(state, sta);
233 DEBUGFS_DEL(bssid, sta);
234 DEBUGFS_DEL(prev_bssid, sta);
235 DEBUGFS_DEL(ssid_len, sta);
236 DEBUGFS_DEL(aid, sta);
237 DEBUGFS_DEL(ap_capab, sta);
238 DEBUGFS_DEL(capab, sta);
239 DEBUGFS_DEL(extra_ie_len, sta);
240 DEBUGFS_DEL(auth_tries, sta);
241 DEBUGFS_DEL(assoc_tries, sta);
242 DEBUGFS_DEL(auth_algs, sta);
243 DEBUGFS_DEL(auth_alg, sta);
244 DEBUGFS_DEL(auth_transaction, sta);
245 DEBUGFS_DEL(flags, sta);
246 }
247
248 static void del_ap_files(struct ieee80211_sub_if_data *sdata)
249 {
250 DEBUGFS_DEL(channel_use, ap);
251 DEBUGFS_DEL(drop_unencrypted, ap);
252 DEBUGFS_DEL(num_sta_ps, ap);
253 DEBUGFS_DEL(dtim_count, ap);
254 DEBUGFS_DEL(num_beacons, ap);
255 DEBUGFS_DEL(force_unicast_rateidx, ap);
256 DEBUGFS_DEL(max_ratectrl_rateidx, ap);
257 DEBUGFS_DEL(num_buffered_multicast, ap);
258 }
259
260 static void del_wds_files(struct ieee80211_sub_if_data *sdata)
261 {
262 DEBUGFS_DEL(channel_use, wds);
263 DEBUGFS_DEL(drop_unencrypted, wds);
264 DEBUGFS_DEL(peer, wds);
265 }
266
267 static void del_vlan_files(struct ieee80211_sub_if_data *sdata)
268 {
269 DEBUGFS_DEL(channel_use, vlan);
270 DEBUGFS_DEL(drop_unencrypted, vlan);
271 }
272
273 static void del_monitor_files(struct ieee80211_sub_if_data *sdata)
274 {
275 }
276
277 static void del_files(struct ieee80211_sub_if_data *sdata, int type)
278 {
279 if (!sdata->debugfsdir)
280 return;
281
282 switch (type) {
283 case IEEE80211_IF_TYPE_STA:
284 case IEEE80211_IF_TYPE_IBSS:
285 del_sta_files(sdata);
286 break;
287 case IEEE80211_IF_TYPE_AP:
288 del_ap_files(sdata);
289 break;
290 case IEEE80211_IF_TYPE_WDS:
291 del_wds_files(sdata);
292 break;
293 case IEEE80211_IF_TYPE_MNTR:
294 del_monitor_files(sdata);
295 break;
296 case IEEE80211_IF_TYPE_VLAN:
297 del_vlan_files(sdata);
298 break;
299 default:
300 break;
301 }
302 }
303
304 static int notif_registered;
305
306 void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
307 {
308 char buf[10+IFNAMSIZ];
309
310 if (!notif_registered)
311 return;
312
313 sprintf(buf, "netdev:%s", sdata->dev->name);
314 sdata->debugfsdir = debugfs_create_dir(buf,
315 sdata->local->hw.wiphy->debugfsdir);
316 }
317
318 void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
319 {
320 del_files(sdata, sdata->vif.type);
321 debugfs_remove(sdata->debugfsdir);
322 sdata->debugfsdir = NULL;
323 }
324
325 void ieee80211_debugfs_change_if_type(struct ieee80211_sub_if_data *sdata,
326 int oldtype)
327 {
328 del_files(sdata, oldtype);
329 add_files(sdata);
330 }
331
332 static int netdev_notify(struct notifier_block * nb,
333 unsigned long state,
334 void *ndev)
335 {
336 struct net_device *dev = ndev;
337 struct dentry *dir;
338 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
339 char buf[10+IFNAMSIZ];
340
341 if (state != NETDEV_CHANGENAME)
342 return 0;
343
344 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
345 return 0;
346
347 if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
348 return 0;
349
350 sprintf(buf, "netdev:%s", dev->name);
351 dir = sdata->debugfsdir;
352 if (!debugfs_rename(dir->d_parent, dir, dir->d_parent, buf))
353 printk(KERN_ERR "mac80211: debugfs: failed to rename debugfs "
354 "dir to %s\n", buf);
355
356 return 0;
357 }
358
359 static struct notifier_block mac80211_debugfs_netdev_notifier = {
360 .notifier_call = netdev_notify,
361 };
362
363 void ieee80211_debugfs_netdev_init(void)
364 {
365 int err;
366
367 err = register_netdevice_notifier(&mac80211_debugfs_netdev_notifier);
368 if (err) {
369 printk(KERN_ERR
370 "mac80211: failed to install netdev notifier,"
371 " disabling per-netdev debugfs!\n");
372 } else
373 notif_registered = 1;
374 }
375
376 void ieee80211_debugfs_netdev_exit(void)
377 {
378 unregister_netdevice_notifier(&mac80211_debugfs_netdev_notifier);
379 notif_registered = 0;
380 }