add updated mac80211 - this no longer relies on patching includes in the kernel trees...
[openwrt.git] / package / mac80211 / src / 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         return scnprintf(buf, buflen, MAC_FMT "\n", MAC_ARG(sdata->field));\
70 }
71
72 #define __IEEE80211_IF_FILE(name)                                       \
73 static ssize_t ieee80211_if_read_##name(struct file *file,              \
74                                         char __user *userbuf,           \
75                                         size_t count, loff_t *ppos)     \
76 {                                                                       \
77         return ieee80211_if_read(file->private_data,                    \
78                                  userbuf, count, ppos,                  \
79                                  ieee80211_if_fmt_##name);              \
80 }                                                                       \
81 static const struct file_operations name##_ops = {                      \
82         .read = ieee80211_if_read_##name,                               \
83         .open = mac80211_open_file_generic,                             \
84 }
85
86 #define IEEE80211_IF_FILE(name, field, format)                          \
87                 IEEE80211_IF_FMT_##format(name, field)                  \
88                 __IEEE80211_IF_FILE(name)
89
90 /* common attributes */
91 IEEE80211_IF_FILE(channel_use, channel_use, DEC);
92 IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
93 IEEE80211_IF_FILE(eapol, eapol, DEC);
94 IEEE80211_IF_FILE(ieee8021_x, ieee802_1x, DEC);
95
96 /* STA/IBSS attributes */
97 IEEE80211_IF_FILE(state, u.sta.state, DEC);
98 IEEE80211_IF_FILE(bssid, u.sta.bssid, MAC);
99 IEEE80211_IF_FILE(prev_bssid, u.sta.prev_bssid, MAC);
100 IEEE80211_IF_FILE(ssid_len, u.sta.ssid_len, SIZE);
101 IEEE80211_IF_FILE(aid, u.sta.aid, DEC);
102 IEEE80211_IF_FILE(ap_capab, u.sta.ap_capab, HEX);
103 IEEE80211_IF_FILE(capab, u.sta.capab, HEX);
104 IEEE80211_IF_FILE(extra_ie_len, u.sta.extra_ie_len, SIZE);
105 IEEE80211_IF_FILE(auth_tries, u.sta.auth_tries, DEC);
106 IEEE80211_IF_FILE(assoc_tries, u.sta.assoc_tries, DEC);
107 IEEE80211_IF_FILE(auth_algs, u.sta.auth_algs, HEX);
108 IEEE80211_IF_FILE(auth_alg, u.sta.auth_alg, DEC);
109 IEEE80211_IF_FILE(auth_transaction, u.sta.auth_transaction, DEC);
110
111 static ssize_t ieee80211_if_fmt_flags(
112         const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
113 {
114         return scnprintf(buf, buflen, "%s%s%s%s%s%s%s\n",
115                  sdata->u.sta.flags & IEEE80211_STA_SSID_SET ? "SSID\n" : "",
116                  sdata->u.sta.flags & IEEE80211_STA_BSSID_SET ? "BSSID\n" : "",
117                  sdata->u.sta.flags & IEEE80211_STA_PREV_BSSID_SET ? "prev BSSID\n" : "",
118                  sdata->u.sta.flags & IEEE80211_STA_AUTHENTICATED ? "AUTH\n" : "",
119                  sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED ? "ASSOC\n" : "",
120                  sdata->u.sta.flags & IEEE80211_STA_PROBEREQ_POLL ? "PROBEREQ POLL\n" : "",
121                  sdata->flags & IEEE80211_SDATA_USE_PROTECTION ? "CTS prot\n" : "");
122 }
123 __IEEE80211_IF_FILE(flags);
124
125 /* AP attributes */
126 IEEE80211_IF_FILE(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
127 IEEE80211_IF_FILE(dtim_period, u.ap.dtim_period, DEC);
128 IEEE80211_IF_FILE(dtim_count, u.ap.dtim_count, DEC);
129 IEEE80211_IF_FILE(num_beacons, u.ap.num_beacons, DEC);
130 IEEE80211_IF_FILE(force_unicast_rateidx, u.ap.force_unicast_rateidx, DEC);
131 IEEE80211_IF_FILE(max_ratectrl_rateidx, u.ap.max_ratectrl_rateidx, DEC);
132
133 static ssize_t ieee80211_if_fmt_num_buffered_multicast(
134         const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
135 {
136         return scnprintf(buf, buflen, "%u\n",
137                          skb_queue_len(&sdata->u.ap.ps_bc_buf));
138 }
139 __IEEE80211_IF_FILE(num_buffered_multicast);
140
141 static ssize_t ieee80211_if_fmt_beacon_head_len(
142         const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
143 {
144         if (sdata->u.ap.beacon_head)
145                 return scnprintf(buf, buflen, "%d\n",
146                                  sdata->u.ap.beacon_head_len);
147         return scnprintf(buf, buflen, "\n");
148 }
149 __IEEE80211_IF_FILE(beacon_head_len);
150
151 static ssize_t ieee80211_if_fmt_beacon_tail_len(
152         const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
153 {
154         if (sdata->u.ap.beacon_tail)
155                 return scnprintf(buf, buflen, "%d\n",
156                                  sdata->u.ap.beacon_tail_len);
157         return scnprintf(buf, buflen, "\n");
158 }
159 __IEEE80211_IF_FILE(beacon_tail_len);
160
161 /* WDS attributes */
162 IEEE80211_IF_FILE(peer, u.wds.remote_addr, MAC);
163
164 #define DEBUGFS_ADD(name, type)\
165         sdata->debugfs.type.name = debugfs_create_file(#name, 0444,\
166                 sdata->debugfsdir, sdata, &name##_ops);
167
168 static void add_sta_files(struct ieee80211_sub_if_data *sdata)
169 {
170         DEBUGFS_ADD(channel_use, sta);
171         DEBUGFS_ADD(drop_unencrypted, sta);
172         DEBUGFS_ADD(eapol, sta);
173         DEBUGFS_ADD(ieee8021_x, sta);
174         DEBUGFS_ADD(state, sta);
175         DEBUGFS_ADD(bssid, sta);
176         DEBUGFS_ADD(prev_bssid, sta);
177         DEBUGFS_ADD(ssid_len, sta);
178         DEBUGFS_ADD(aid, sta);
179         DEBUGFS_ADD(ap_capab, sta);
180         DEBUGFS_ADD(capab, sta);
181         DEBUGFS_ADD(extra_ie_len, sta);
182         DEBUGFS_ADD(auth_tries, sta);
183         DEBUGFS_ADD(assoc_tries, sta);
184         DEBUGFS_ADD(auth_algs, sta);
185         DEBUGFS_ADD(auth_alg, sta);
186         DEBUGFS_ADD(auth_transaction, sta);
187         DEBUGFS_ADD(flags, sta);
188 }
189
190 static void add_ap_files(struct ieee80211_sub_if_data *sdata)
191 {
192         DEBUGFS_ADD(channel_use, ap);
193         DEBUGFS_ADD(drop_unencrypted, ap);
194         DEBUGFS_ADD(eapol, ap);
195         DEBUGFS_ADD(ieee8021_x, ap);
196         DEBUGFS_ADD(num_sta_ps, ap);
197         DEBUGFS_ADD(dtim_period, ap);
198         DEBUGFS_ADD(dtim_count, ap);
199         DEBUGFS_ADD(num_beacons, ap);
200         DEBUGFS_ADD(force_unicast_rateidx, ap);
201         DEBUGFS_ADD(max_ratectrl_rateidx, ap);
202         DEBUGFS_ADD(num_buffered_multicast, ap);
203         DEBUGFS_ADD(beacon_head_len, ap);
204         DEBUGFS_ADD(beacon_tail_len, ap);
205 }
206
207 static void add_wds_files(struct ieee80211_sub_if_data *sdata)
208 {
209         DEBUGFS_ADD(channel_use, wds);
210         DEBUGFS_ADD(drop_unencrypted, wds);
211         DEBUGFS_ADD(eapol, wds);
212         DEBUGFS_ADD(ieee8021_x, wds);
213         DEBUGFS_ADD(peer, wds);
214 }
215
216 static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
217 {
218         DEBUGFS_ADD(channel_use, vlan);
219         DEBUGFS_ADD(drop_unencrypted, vlan);
220         DEBUGFS_ADD(eapol, vlan);
221         DEBUGFS_ADD(ieee8021_x, vlan);
222 }
223
224 static void add_monitor_files(struct ieee80211_sub_if_data *sdata)
225 {
226 }
227
228 static void add_files(struct ieee80211_sub_if_data *sdata)
229 {
230         if (!sdata->debugfsdir)
231                 return;
232
233         switch (sdata->type) {
234         case IEEE80211_IF_TYPE_STA:
235         case IEEE80211_IF_TYPE_IBSS:
236                 add_sta_files(sdata);
237                 break;
238         case IEEE80211_IF_TYPE_AP:
239                 add_ap_files(sdata);
240                 break;
241         case IEEE80211_IF_TYPE_WDS:
242                 add_wds_files(sdata);
243                 break;
244         case IEEE80211_IF_TYPE_MNTR:
245                 add_monitor_files(sdata);
246                 break;
247         case IEEE80211_IF_TYPE_VLAN:
248                 add_vlan_files(sdata);
249                 break;
250         default:
251                 break;
252         }
253 }
254
255 #define DEBUGFS_DEL(name, type)                                 \
256         do {                                                    \
257                 debugfs_remove(sdata->debugfs.type.name);       \
258                 sdata->debugfs.type.name = NULL;                \
259         } while (0)
260
261 static void del_sta_files(struct ieee80211_sub_if_data *sdata)
262 {
263         DEBUGFS_DEL(channel_use, sta);
264         DEBUGFS_DEL(drop_unencrypted, sta);
265         DEBUGFS_DEL(eapol, sta);
266         DEBUGFS_DEL(ieee8021_x, sta);
267         DEBUGFS_DEL(state, sta);
268         DEBUGFS_DEL(bssid, sta);
269         DEBUGFS_DEL(prev_bssid, sta);
270         DEBUGFS_DEL(ssid_len, sta);
271         DEBUGFS_DEL(aid, sta);
272         DEBUGFS_DEL(ap_capab, sta);
273         DEBUGFS_DEL(capab, sta);
274         DEBUGFS_DEL(extra_ie_len, sta);
275         DEBUGFS_DEL(auth_tries, sta);
276         DEBUGFS_DEL(assoc_tries, sta);
277         DEBUGFS_DEL(auth_algs, sta);
278         DEBUGFS_DEL(auth_alg, sta);
279         DEBUGFS_DEL(auth_transaction, sta);
280         DEBUGFS_DEL(flags, sta);
281 }
282
283 static void del_ap_files(struct ieee80211_sub_if_data *sdata)
284 {
285         DEBUGFS_DEL(channel_use, ap);
286         DEBUGFS_DEL(drop_unencrypted, ap);
287         DEBUGFS_DEL(eapol, ap);
288         DEBUGFS_DEL(ieee8021_x, ap);
289         DEBUGFS_DEL(num_sta_ps, ap);
290         DEBUGFS_DEL(dtim_period, ap);
291         DEBUGFS_DEL(dtim_count, ap);
292         DEBUGFS_DEL(num_beacons, ap);
293         DEBUGFS_DEL(force_unicast_rateidx, ap);
294         DEBUGFS_DEL(max_ratectrl_rateidx, ap);
295         DEBUGFS_DEL(num_buffered_multicast, ap);
296         DEBUGFS_DEL(beacon_head_len, ap);
297         DEBUGFS_DEL(beacon_tail_len, ap);
298 }
299
300 static void del_wds_files(struct ieee80211_sub_if_data *sdata)
301 {
302         DEBUGFS_DEL(channel_use, wds);
303         DEBUGFS_DEL(drop_unencrypted, wds);
304         DEBUGFS_DEL(eapol, wds);
305         DEBUGFS_DEL(ieee8021_x, wds);
306         DEBUGFS_DEL(peer, wds);
307 }
308
309 static void del_vlan_files(struct ieee80211_sub_if_data *sdata)
310 {
311         DEBUGFS_DEL(channel_use, vlan);
312         DEBUGFS_DEL(drop_unencrypted, vlan);
313         DEBUGFS_DEL(eapol, vlan);
314         DEBUGFS_DEL(ieee8021_x, vlan);
315 }
316
317 static void del_monitor_files(struct ieee80211_sub_if_data *sdata)
318 {
319 }
320
321 static void del_files(struct ieee80211_sub_if_data *sdata, int type)
322 {
323         if (!sdata->debugfsdir)
324                 return;
325
326         switch (type) {
327         case IEEE80211_IF_TYPE_STA:
328         case IEEE80211_IF_TYPE_IBSS:
329                 del_sta_files(sdata);
330                 break;
331         case IEEE80211_IF_TYPE_AP:
332                 del_ap_files(sdata);
333                 break;
334         case IEEE80211_IF_TYPE_WDS:
335                 del_wds_files(sdata);
336                 break;
337         case IEEE80211_IF_TYPE_MNTR:
338                 del_monitor_files(sdata);
339                 break;
340         case IEEE80211_IF_TYPE_VLAN:
341                 del_vlan_files(sdata);
342                 break;
343         default:
344                 break;
345         }
346 }
347
348 static int notif_registered;
349
350 void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
351 {
352         char buf[10+IFNAMSIZ];
353
354         if (!notif_registered)
355                 return;
356
357         sprintf(buf, "netdev:%s", sdata->dev->name);
358         sdata->debugfsdir = debugfs_create_dir(buf,
359                 sdata->local->hw.wiphy->debugfsdir);
360 }
361
362 void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
363 {
364         del_files(sdata, sdata->type);
365         debugfs_remove(sdata->debugfsdir);
366         sdata->debugfsdir = NULL;
367 }
368
369 void ieee80211_debugfs_change_if_type(struct ieee80211_sub_if_data *sdata,
370                                       int oldtype)
371 {
372         del_files(sdata, oldtype);
373         add_files(sdata);
374 }
375
376 static int netdev_notify(struct notifier_block * nb,
377                          unsigned long state,
378                          void *ndev)
379 {
380         struct net_device *dev = ndev;
381         struct dentry *dir;
382         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
383         char buf[10+IFNAMSIZ];
384
385         if (state != NETDEV_CHANGENAME)
386                 return 0;
387
388         if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
389                 return 0;
390
391         if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
392                 return 0;
393
394         sprintf(buf, "netdev:%s", dev->name);
395         dir = sdata->debugfsdir;
396         if (!debugfs_rename(dir->d_parent, dir, dir->d_parent, buf))
397                 printk(KERN_ERR "mac80211: debugfs: failed to rename debugfs "
398                        "dir to %s\n", buf);
399
400         return 0;
401 }
402
403 static struct notifier_block mac80211_debugfs_netdev_notifier = {
404         .notifier_call = netdev_notify,
405 };
406
407 void ieee80211_debugfs_netdev_init(void)
408 {
409         int err;
410
411         err = register_netdevice_notifier(&mac80211_debugfs_netdev_notifier);
412         if (err) {
413                 printk(KERN_ERR
414                        "mac80211: failed to install netdev notifier,"
415                        " disabling per-netdev debugfs!\n");
416         } else
417                 notif_registered = 1;
418 }
419
420 void ieee80211_debugfs_netdev_exit(void)
421 {
422         unregister_netdevice_notifier(&mac80211_debugfs_netdev_notifier);
423         notif_registered = 0;
424 }