mac80211: Fix memory leak in ieee80211_if_write()
[linux-2.6.git] / 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 "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         read_unlock(&dev_base_lock);
37
38         if (ret != -EINVAL)
39                 ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);
40
41         return ret;
42 }
43
44 static ssize_t ieee80211_if_write(
45         struct ieee80211_sub_if_data *sdata,
46         const char __user *userbuf,
47         size_t count, loff_t *ppos,
48         ssize_t (*write)(struct ieee80211_sub_if_data *, const char *, int))
49 {
50         u8 *buf;
51         ssize_t ret;
52
53         buf = kmalloc(count, GFP_KERNEL);
54         if (!buf)
55                 return -ENOMEM;
56
57         ret = -EFAULT;
58         if (copy_from_user(buf, userbuf, count))
59                 goto freebuf;
60
61         ret = -ENODEV;
62         rtnl_lock();
63         if (sdata->dev->reg_state == NETREG_REGISTERED)
64                 ret = (*write)(sdata, buf, count);
65         rtnl_unlock();
66
67 freebuf:
68         kfree(buf);
69         return ret;
70 }
71
72 #define IEEE80211_IF_FMT(name, field, format_string)                    \
73 static ssize_t ieee80211_if_fmt_##name(                                 \
74         const struct ieee80211_sub_if_data *sdata, char *buf,           \
75         int buflen)                                                     \
76 {                                                                       \
77         return scnprintf(buf, buflen, format_string, sdata->field);     \
78 }
79 #define IEEE80211_IF_FMT_DEC(name, field)                               \
80                 IEEE80211_IF_FMT(name, field, "%d\n")
81 #define IEEE80211_IF_FMT_HEX(name, field)                               \
82                 IEEE80211_IF_FMT(name, field, "%#x\n")
83 #define IEEE80211_IF_FMT_SIZE(name, field)                              \
84                 IEEE80211_IF_FMT(name, field, "%zd\n")
85
86 #define IEEE80211_IF_FMT_ATOMIC(name, field)                            \
87 static ssize_t ieee80211_if_fmt_##name(                                 \
88         const struct ieee80211_sub_if_data *sdata,                      \
89         char *buf, int buflen)                                          \
90 {                                                                       \
91         return scnprintf(buf, buflen, "%d\n", atomic_read(&sdata->field));\
92 }
93
94 #define IEEE80211_IF_FMT_MAC(name, field)                               \
95 static ssize_t ieee80211_if_fmt_##name(                                 \
96         const struct ieee80211_sub_if_data *sdata, char *buf,           \
97         int buflen)                                                     \
98 {                                                                       \
99         return scnprintf(buf, buflen, "%pM\n", sdata->field);           \
100 }
101
102 #define __IEEE80211_IF_FILE(name, _write)                               \
103 static ssize_t ieee80211_if_read_##name(struct file *file,              \
104                                         char __user *userbuf,           \
105                                         size_t count, loff_t *ppos)     \
106 {                                                                       \
107         return ieee80211_if_read(file->private_data,                    \
108                                  userbuf, count, ppos,                  \
109                                  ieee80211_if_fmt_##name);              \
110 }                                                                       \
111 static const struct file_operations name##_ops = {                      \
112         .read = ieee80211_if_read_##name,                               \
113         .write = (_write),                                              \
114         .open = mac80211_open_file_generic,                             \
115 }
116
117 #define __IEEE80211_IF_FILE_W(name)                                     \
118 static ssize_t ieee80211_if_write_##name(struct file *file,             \
119                                          const char __user *userbuf,    \
120                                          size_t count, loff_t *ppos)    \
121 {                                                                       \
122         return ieee80211_if_write(file->private_data, userbuf, count,   \
123                                   ppos, ieee80211_if_parse_##name);     \
124 }                                                                       \
125 __IEEE80211_IF_FILE(name, ieee80211_if_write_##name)
126
127
128 #define IEEE80211_IF_FILE(name, field, format)                          \
129                 IEEE80211_IF_FMT_##format(name, field)                  \
130                 __IEEE80211_IF_FILE(name, NULL)
131
132 /* common attributes */
133 IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
134 IEEE80211_IF_FILE(rc_rateidx_mask_2ghz, rc_rateidx_mask[IEEE80211_BAND_2GHZ],
135                   HEX);
136 IEEE80211_IF_FILE(rc_rateidx_mask_5ghz, rc_rateidx_mask[IEEE80211_BAND_5GHZ],
137                   HEX);
138
139 /* STA attributes */
140 IEEE80211_IF_FILE(bssid, u.mgd.bssid, MAC);
141 IEEE80211_IF_FILE(aid, u.mgd.aid, DEC);
142
143 static int ieee80211_set_smps(struct ieee80211_sub_if_data *sdata,
144                               enum ieee80211_smps_mode smps_mode)
145 {
146         struct ieee80211_local *local = sdata->local;
147         int err;
148
149         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_STATIC_SMPS) &&
150             smps_mode == IEEE80211_SMPS_STATIC)
151                 return -EINVAL;
152
153         /* auto should be dynamic if in PS mode */
154         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS) &&
155             (smps_mode == IEEE80211_SMPS_DYNAMIC ||
156              smps_mode == IEEE80211_SMPS_AUTOMATIC))
157                 return -EINVAL;
158
159         /* supported only on managed interfaces for now */
160         if (sdata->vif.type != NL80211_IFTYPE_STATION)
161                 return -EOPNOTSUPP;
162
163         mutex_lock(&local->iflist_mtx);
164         err = __ieee80211_request_smps(sdata, smps_mode);
165         mutex_unlock(&local->iflist_mtx);
166
167         return err;
168 }
169
170 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
171         [IEEE80211_SMPS_AUTOMATIC] = "auto",
172         [IEEE80211_SMPS_OFF] = "off",
173         [IEEE80211_SMPS_STATIC] = "static",
174         [IEEE80211_SMPS_DYNAMIC] = "dynamic",
175 };
176
177 static ssize_t ieee80211_if_fmt_smps(const struct ieee80211_sub_if_data *sdata,
178                                      char *buf, int buflen)
179 {
180         if (sdata->vif.type != NL80211_IFTYPE_STATION)
181                 return -EOPNOTSUPP;
182
183         return snprintf(buf, buflen, "request: %s\nused: %s\n",
184                         smps_modes[sdata->u.mgd.req_smps],
185                         smps_modes[sdata->u.mgd.ap_smps]);
186 }
187
188 static ssize_t ieee80211_if_parse_smps(struct ieee80211_sub_if_data *sdata,
189                                        const char *buf, int buflen)
190 {
191         enum ieee80211_smps_mode mode;
192
193         for (mode = 0; mode < IEEE80211_SMPS_NUM_MODES; mode++) {
194                 if (strncmp(buf, smps_modes[mode], buflen) == 0) {
195                         int err = ieee80211_set_smps(sdata, mode);
196                         if (!err)
197                                 return buflen;
198                         return err;
199                 }
200         }
201
202         return -EINVAL;
203 }
204
205 __IEEE80211_IF_FILE_W(smps);
206
207 /* AP attributes */
208 IEEE80211_IF_FILE(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
209 IEEE80211_IF_FILE(dtim_count, u.ap.dtim_count, DEC);
210
211 static ssize_t ieee80211_if_fmt_num_buffered_multicast(
212         const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
213 {
214         return scnprintf(buf, buflen, "%u\n",
215                          skb_queue_len(&sdata->u.ap.ps_bc_buf));
216 }
217 __IEEE80211_IF_FILE(num_buffered_multicast, NULL);
218
219 /* WDS attributes */
220 IEEE80211_IF_FILE(peer, u.wds.remote_addr, MAC);
221
222 #ifdef CONFIG_MAC80211_MESH
223 /* Mesh stats attributes */
224 IEEE80211_IF_FILE(fwded_mcast, u.mesh.mshstats.fwded_mcast, DEC);
225 IEEE80211_IF_FILE(fwded_unicast, u.mesh.mshstats.fwded_unicast, DEC);
226 IEEE80211_IF_FILE(fwded_frames, u.mesh.mshstats.fwded_frames, DEC);
227 IEEE80211_IF_FILE(dropped_frames_ttl, u.mesh.mshstats.dropped_frames_ttl, DEC);
228 IEEE80211_IF_FILE(dropped_frames_no_route,
229                 u.mesh.mshstats.dropped_frames_no_route, DEC);
230 IEEE80211_IF_FILE(estab_plinks, u.mesh.mshstats.estab_plinks, ATOMIC);
231
232 /* Mesh parameters */
233 IEEE80211_IF_FILE(dot11MeshMaxRetries,
234                 u.mesh.mshcfg.dot11MeshMaxRetries, DEC);
235 IEEE80211_IF_FILE(dot11MeshRetryTimeout,
236                 u.mesh.mshcfg.dot11MeshRetryTimeout, DEC);
237 IEEE80211_IF_FILE(dot11MeshConfirmTimeout,
238                 u.mesh.mshcfg.dot11MeshConfirmTimeout, DEC);
239 IEEE80211_IF_FILE(dot11MeshHoldingTimeout,
240                 u.mesh.mshcfg.dot11MeshHoldingTimeout, DEC);
241 IEEE80211_IF_FILE(dot11MeshTTL, u.mesh.mshcfg.dot11MeshTTL, DEC);
242 IEEE80211_IF_FILE(auto_open_plinks, u.mesh.mshcfg.auto_open_plinks, DEC);
243 IEEE80211_IF_FILE(dot11MeshMaxPeerLinks,
244                 u.mesh.mshcfg.dot11MeshMaxPeerLinks, DEC);
245 IEEE80211_IF_FILE(dot11MeshHWMPactivePathTimeout,
246                 u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout, DEC);
247 IEEE80211_IF_FILE(dot11MeshHWMPpreqMinInterval,
248                 u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval, DEC);
249 IEEE80211_IF_FILE(dot11MeshHWMPnetDiameterTraversalTime,
250                 u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime, DEC);
251 IEEE80211_IF_FILE(dot11MeshHWMPmaxPREQretries,
252                 u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries, DEC);
253 IEEE80211_IF_FILE(path_refresh_time,
254                 u.mesh.mshcfg.path_refresh_time, DEC);
255 IEEE80211_IF_FILE(min_discovery_timeout,
256                 u.mesh.mshcfg.min_discovery_timeout, DEC);
257 IEEE80211_IF_FILE(dot11MeshHWMPRootMode,
258                 u.mesh.mshcfg.dot11MeshHWMPRootMode, DEC);
259 #endif
260
261
262 #define DEBUGFS_ADD(name) \
263         debugfs_create_file(#name, 0400, sdata->debugfs.dir, \
264                             sdata, &name##_ops);
265
266 #define DEBUGFS_ADD_MODE(name, mode) \
267         debugfs_create_file(#name, mode, sdata->debugfs.dir, \
268                             sdata, &name##_ops);
269
270 static void add_sta_files(struct ieee80211_sub_if_data *sdata)
271 {
272         DEBUGFS_ADD(drop_unencrypted);
273         DEBUGFS_ADD(rc_rateidx_mask_2ghz);
274         DEBUGFS_ADD(rc_rateidx_mask_5ghz);
275
276         DEBUGFS_ADD(bssid);
277         DEBUGFS_ADD(aid);
278         DEBUGFS_ADD_MODE(smps, 0600);
279 }
280
281 static void add_ap_files(struct ieee80211_sub_if_data *sdata)
282 {
283         DEBUGFS_ADD(drop_unencrypted);
284         DEBUGFS_ADD(rc_rateidx_mask_2ghz);
285         DEBUGFS_ADD(rc_rateidx_mask_5ghz);
286
287         DEBUGFS_ADD(num_sta_ps);
288         DEBUGFS_ADD(dtim_count);
289         DEBUGFS_ADD(num_buffered_multicast);
290 }
291
292 static void add_wds_files(struct ieee80211_sub_if_data *sdata)
293 {
294         DEBUGFS_ADD(drop_unencrypted);
295         DEBUGFS_ADD(rc_rateidx_mask_2ghz);
296         DEBUGFS_ADD(rc_rateidx_mask_5ghz);
297
298         DEBUGFS_ADD(peer);
299 }
300
301 static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
302 {
303         DEBUGFS_ADD(drop_unencrypted);
304         DEBUGFS_ADD(rc_rateidx_mask_2ghz);
305         DEBUGFS_ADD(rc_rateidx_mask_5ghz);
306 }
307
308 static void add_monitor_files(struct ieee80211_sub_if_data *sdata)
309 {
310 }
311
312 #ifdef CONFIG_MAC80211_MESH
313
314 static void add_mesh_stats(struct ieee80211_sub_if_data *sdata)
315 {
316         struct dentry *dir = debugfs_create_dir("mesh_stats",
317                                                 sdata->debugfs.dir);
318
319 #define MESHSTATS_ADD(name)\
320         debugfs_create_file(#name, 0400, dir, sdata, &name##_ops);
321
322         MESHSTATS_ADD(fwded_mcast);
323         MESHSTATS_ADD(fwded_unicast);
324         MESHSTATS_ADD(fwded_frames);
325         MESHSTATS_ADD(dropped_frames_ttl);
326         MESHSTATS_ADD(dropped_frames_no_route);
327         MESHSTATS_ADD(estab_plinks);
328 #undef MESHSTATS_ADD
329 }
330
331 static void add_mesh_config(struct ieee80211_sub_if_data *sdata)
332 {
333         struct dentry *dir = debugfs_create_dir("mesh_config",
334                                                 sdata->debugfs.dir);
335
336 #define MESHPARAMS_ADD(name) \
337         debugfs_create_file(#name, 0600, dir, sdata, &name##_ops);
338
339         MESHPARAMS_ADD(dot11MeshMaxRetries);
340         MESHPARAMS_ADD(dot11MeshRetryTimeout);
341         MESHPARAMS_ADD(dot11MeshConfirmTimeout);
342         MESHPARAMS_ADD(dot11MeshHoldingTimeout);
343         MESHPARAMS_ADD(dot11MeshTTL);
344         MESHPARAMS_ADD(auto_open_plinks);
345         MESHPARAMS_ADD(dot11MeshMaxPeerLinks);
346         MESHPARAMS_ADD(dot11MeshHWMPactivePathTimeout);
347         MESHPARAMS_ADD(dot11MeshHWMPpreqMinInterval);
348         MESHPARAMS_ADD(dot11MeshHWMPnetDiameterTraversalTime);
349         MESHPARAMS_ADD(dot11MeshHWMPmaxPREQretries);
350         MESHPARAMS_ADD(path_refresh_time);
351         MESHPARAMS_ADD(min_discovery_timeout);
352
353 #undef MESHPARAMS_ADD
354 }
355 #endif
356
357 static void add_files(struct ieee80211_sub_if_data *sdata)
358 {
359         if (!sdata->debugfs.dir)
360                 return;
361
362         switch (sdata->vif.type) {
363         case NL80211_IFTYPE_MESH_POINT:
364 #ifdef CONFIG_MAC80211_MESH
365                 add_mesh_stats(sdata);
366                 add_mesh_config(sdata);
367 #endif
368                 break;
369         case NL80211_IFTYPE_STATION:
370                 add_sta_files(sdata);
371                 break;
372         case NL80211_IFTYPE_ADHOC:
373                 /* XXX */
374                 break;
375         case NL80211_IFTYPE_AP:
376                 add_ap_files(sdata);
377                 break;
378         case NL80211_IFTYPE_WDS:
379                 add_wds_files(sdata);
380                 break;
381         case NL80211_IFTYPE_MONITOR:
382                 add_monitor_files(sdata);
383                 break;
384         case NL80211_IFTYPE_AP_VLAN:
385                 add_vlan_files(sdata);
386                 break;
387         default:
388                 break;
389         }
390 }
391
392 void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
393 {
394         char buf[10+IFNAMSIZ];
395
396         sprintf(buf, "netdev:%s", sdata->name);
397         sdata->debugfs.dir = debugfs_create_dir(buf,
398                 sdata->local->hw.wiphy->debugfsdir);
399         add_files(sdata);
400 }
401
402 void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
403 {
404         if (!sdata->debugfs.dir)
405                 return;
406
407         debugfs_remove_recursive(sdata->debugfs.dir);
408         sdata->debugfs.dir = NULL;
409 }
410
411 void ieee80211_debugfs_rename_netdev(struct ieee80211_sub_if_data *sdata)
412 {
413         struct dentry *dir;
414         char buf[10 + IFNAMSIZ];
415
416         dir = sdata->debugfs.dir;
417
418         if (!dir)
419                 return;
420
421         sprintf(buf, "netdev:%s", sdata->name);
422         if (!debugfs_rename(dir->d_parent, dir, dir->d_parent, buf))
423                 printk(KERN_ERR "mac80211: debugfs: failed to rename debugfs "
424                        "dir to %s\n", buf);
425 }