GNU Linux-libre 4.4.295-gnu1
[releases.git] / net / batman-adv / debugfs.c
1 /* Copyright (C) 2010-2015 B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "debugfs.h"
19 #include "main.h"
20
21 #include <linux/compiler.h>
22 #include <linux/dcache.h>
23 #include <linux/debugfs.h>
24 #include <linux/device.h>
25 #include <linux/errno.h>
26 #include <linux/export.h>
27 #include <linux/fcntl.h>
28 #include <linux/fs.h>
29 #include <linux/jiffies.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/netdevice.h>
33 #include <linux/poll.h>
34 #include <linux/printk.h>
35 #include <linux/sched.h> /* for linux/wait.h */
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/stat.h>
40 #include <linux/stddef.h>
41 #include <linux/stringify.h>
42 #include <linux/sysfs.h>
43 #include <linux/types.h>
44 #include <linux/uaccess.h>
45 #include <linux/wait.h>
46 #include <stdarg.h>
47
48 #include "bridge_loop_avoidance.h"
49 #include "distributed-arp-table.h"
50 #include "gateway_client.h"
51 #include "icmp_socket.h"
52 #include "network-coding.h"
53 #include "originator.h"
54 #include "translation-table.h"
55
56 static struct dentry *batadv_debugfs;
57
58 #ifdef CONFIG_BATMAN_ADV_DEBUG
59 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
60
61 static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
62
63 static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
64                                   size_t idx)
65 {
66         return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
67 }
68
69 static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
70                                  char c)
71 {
72         char *char_addr;
73
74         char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
75         *char_addr = c;
76         debug_log->log_end++;
77
78         if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
79                 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
80 }
81
82 __printf(2, 3)
83 static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
84                              const char *fmt, ...)
85 {
86         va_list args;
87         static char debug_log_buf[256];
88         char *p;
89
90         if (!debug_log)
91                 return 0;
92
93         spin_lock_bh(&debug_log->lock);
94         va_start(args, fmt);
95         vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
96         va_end(args);
97
98         for (p = debug_log_buf; *p != 0; p++)
99                 batadv_emit_log_char(debug_log, *p);
100
101         spin_unlock_bh(&debug_log->lock);
102
103         wake_up(&debug_log->queue_wait);
104
105         return 0;
106 }
107
108 int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
109 {
110         va_list args;
111         char tmp_log_buf[256];
112
113         va_start(args, fmt);
114         vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
115         batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
116                           jiffies_to_msecs(jiffies), tmp_log_buf);
117         va_end(args);
118
119         return 0;
120 }
121
122 static int batadv_log_open(struct inode *inode, struct file *file)
123 {
124         if (!try_module_get(THIS_MODULE))
125                 return -EBUSY;
126
127         nonseekable_open(inode, file);
128         file->private_data = inode->i_private;
129         return 0;
130 }
131
132 static int batadv_log_release(struct inode *inode, struct file *file)
133 {
134         module_put(THIS_MODULE);
135         return 0;
136 }
137
138 static int batadv_log_empty(struct batadv_priv_debug_log *debug_log)
139 {
140         return !(debug_log->log_start - debug_log->log_end);
141 }
142
143 static ssize_t batadv_log_read(struct file *file, char __user *buf,
144                                size_t count, loff_t *ppos)
145 {
146         struct batadv_priv *bat_priv = file->private_data;
147         struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
148         int error, i = 0;
149         char *char_addr;
150         char c;
151
152         if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
153                 return -EAGAIN;
154
155         if (!buf)
156                 return -EINVAL;
157
158         if (count == 0)
159                 return 0;
160
161         if (!access_ok(VERIFY_WRITE, buf, count))
162                 return -EFAULT;
163
164         error = wait_event_interruptible(debug_log->queue_wait,
165                                          (!batadv_log_empty(debug_log)));
166
167         if (error)
168                 return error;
169
170         spin_lock_bh(&debug_log->lock);
171
172         while ((!error) && (i < count) &&
173                (debug_log->log_start != debug_log->log_end)) {
174                 char_addr = batadv_log_char_addr(debug_log,
175                                                  debug_log->log_start);
176                 c = *char_addr;
177
178                 debug_log->log_start++;
179
180                 spin_unlock_bh(&debug_log->lock);
181
182                 error = __put_user(c, buf);
183
184                 spin_lock_bh(&debug_log->lock);
185
186                 buf++;
187                 i++;
188         }
189
190         spin_unlock_bh(&debug_log->lock);
191
192         if (!error)
193                 return i;
194
195         return error;
196 }
197
198 static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
199 {
200         struct batadv_priv *bat_priv = file->private_data;
201         struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
202
203         poll_wait(file, &debug_log->queue_wait, wait);
204
205         if (!batadv_log_empty(debug_log))
206                 return POLLIN | POLLRDNORM;
207
208         return 0;
209 }
210
211 static const struct file_operations batadv_log_fops = {
212         .open           = batadv_log_open,
213         .release        = batadv_log_release,
214         .read           = batadv_log_read,
215         .poll           = batadv_log_poll,
216         .llseek         = no_llseek,
217         .owner          = THIS_MODULE,
218 };
219
220 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
221 {
222         struct dentry *d;
223
224         if (!bat_priv->debug_dir)
225                 goto err;
226
227         bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
228         if (!bat_priv->debug_log)
229                 goto err;
230
231         spin_lock_init(&bat_priv->debug_log->lock);
232         init_waitqueue_head(&bat_priv->debug_log->queue_wait);
233
234         d = debugfs_create_file("log", S_IFREG | S_IRUSR,
235                                 bat_priv->debug_dir, bat_priv,
236                                 &batadv_log_fops);
237         if (!d)
238                 goto err;
239
240         return 0;
241
242 err:
243         return -ENOMEM;
244 }
245
246 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
247 {
248         kfree(bat_priv->debug_log);
249         bat_priv->debug_log = NULL;
250 }
251 #else /* CONFIG_BATMAN_ADV_DEBUG */
252 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
253 {
254         return 0;
255 }
256
257 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
258 {
259 }
260 #endif
261
262 static int batadv_algorithms_open(struct inode *inode, struct file *file)
263 {
264         return single_open(file, batadv_algo_seq_print_text, NULL);
265 }
266
267 static int batadv_originators_open(struct inode *inode, struct file *file)
268 {
269         struct net_device *net_dev = (struct net_device *)inode->i_private;
270
271         return single_open(file, batadv_orig_seq_print_text, net_dev);
272 }
273
274 /**
275  * batadv_originators_hardif_open - handles debugfs output for the
276  *  originator table of an hard interface
277  * @inode: inode pointer to debugfs file
278  * @file: pointer to the seq_file
279  */
280 static int batadv_originators_hardif_open(struct inode *inode,
281                                           struct file *file)
282 {
283         struct net_device *net_dev = (struct net_device *)inode->i_private;
284
285         return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
286 }
287
288 static int batadv_gateways_open(struct inode *inode, struct file *file)
289 {
290         struct net_device *net_dev = (struct net_device *)inode->i_private;
291
292         return single_open(file, batadv_gw_client_seq_print_text, net_dev);
293 }
294
295 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
296 {
297         struct net_device *net_dev = (struct net_device *)inode->i_private;
298
299         return single_open(file, batadv_tt_global_seq_print_text, net_dev);
300 }
301
302 #ifdef CONFIG_BATMAN_ADV_BLA
303 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
304 {
305         struct net_device *net_dev = (struct net_device *)inode->i_private;
306
307         return single_open(file, batadv_bla_claim_table_seq_print_text,
308                            net_dev);
309 }
310
311 static int batadv_bla_backbone_table_open(struct inode *inode,
312                                           struct file *file)
313 {
314         struct net_device *net_dev = (struct net_device *)inode->i_private;
315
316         return single_open(file, batadv_bla_backbone_table_seq_print_text,
317                            net_dev);
318 }
319
320 #endif
321
322 #ifdef CONFIG_BATMAN_ADV_DAT
323 /**
324  * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
325  * @inode: inode which was opened
326  * @file: file handle to be initialized
327  */
328 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
329 {
330         struct net_device *net_dev = (struct net_device *)inode->i_private;
331
332         return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
333 }
334 #endif
335
336 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
337 {
338         struct net_device *net_dev = (struct net_device *)inode->i_private;
339
340         return single_open(file, batadv_tt_local_seq_print_text, net_dev);
341 }
342
343 struct batadv_debuginfo {
344         struct attribute attr;
345         const struct file_operations fops;
346 };
347
348 #ifdef CONFIG_BATMAN_ADV_NC
349 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
350 {
351         struct net_device *net_dev = (struct net_device *)inode->i_private;
352
353         return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
354 }
355 #endif
356
357 #define BATADV_DEBUGINFO(_name, _mode, _open)           \
358 struct batadv_debuginfo batadv_debuginfo_##_name = {    \
359         .attr = { .name = __stringify(_name),           \
360                   .mode = _mode, },                     \
361         .fops = { .owner = THIS_MODULE,                 \
362                   .open = _open,                        \
363                   .read = seq_read,                     \
364                   .llseek = seq_lseek,                  \
365                   .release = single_release,            \
366                 }                                       \
367 }
368
369 /* the following attributes are general and therefore they will be directly
370  * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
371  */
372 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
373
374 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
375         &batadv_debuginfo_routing_algos,
376         NULL,
377 };
378
379 /* The following attributes are per soft interface */
380 static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
381 static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
382 static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
383                         batadv_transtable_global_open);
384 #ifdef CONFIG_BATMAN_ADV_BLA
385 static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
386 static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
387                         batadv_bla_backbone_table_open);
388 #endif
389 #ifdef CONFIG_BATMAN_ADV_DAT
390 static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
391 #endif
392 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
393                         batadv_transtable_local_open);
394 #ifdef CONFIG_BATMAN_ADV_NC
395 static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
396 #endif
397
398 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
399         &batadv_debuginfo_originators,
400         &batadv_debuginfo_gateways,
401         &batadv_debuginfo_transtable_global,
402 #ifdef CONFIG_BATMAN_ADV_BLA
403         &batadv_debuginfo_bla_claim_table,
404         &batadv_debuginfo_bla_backbone_table,
405 #endif
406 #ifdef CONFIG_BATMAN_ADV_DAT
407         &batadv_debuginfo_dat_cache,
408 #endif
409         &batadv_debuginfo_transtable_local,
410 #ifdef CONFIG_BATMAN_ADV_NC
411         &batadv_debuginfo_nc_nodes,
412 #endif
413         NULL,
414 };
415
416 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open)            \
417 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = {     \
418         .attr = {                                               \
419                 .name = __stringify(_name),                     \
420                 .mode = _mode,                                  \
421         },                                                      \
422         .fops = {                                               \
423                 .owner = THIS_MODULE,                           \
424                 .open = _open,                                  \
425                 .read   = seq_read,                             \
426                 .llseek = seq_lseek,                            \
427                 .release = single_release,                      \
428         },                                                      \
429 }
430
431 static BATADV_HARDIF_DEBUGINFO(originators, S_IRUGO,
432                                batadv_originators_hardif_open);
433
434 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
435         &batadv_hardif_debuginfo_originators,
436         NULL,
437 };
438
439 void batadv_debugfs_init(void)
440 {
441         struct batadv_debuginfo **bat_debug;
442         struct dentry *file;
443
444         batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
445         if (batadv_debugfs == ERR_PTR(-ENODEV))
446                 batadv_debugfs = NULL;
447
448         if (!batadv_debugfs)
449                 goto err;
450
451         for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
452                 file = debugfs_create_file(((*bat_debug)->attr).name,
453                                            S_IFREG | ((*bat_debug)->attr).mode,
454                                            batadv_debugfs, NULL,
455                                            &(*bat_debug)->fops);
456                 if (!file) {
457                         pr_err("Can't add general debugfs file: %s\n",
458                                ((*bat_debug)->attr).name);
459                         goto err;
460                 }
461         }
462
463         return;
464 err:
465         debugfs_remove_recursive(batadv_debugfs);
466         batadv_debugfs = NULL;
467 }
468
469 void batadv_debugfs_destroy(void)
470 {
471         debugfs_remove_recursive(batadv_debugfs);
472         batadv_debugfs = NULL;
473 }
474
475 /**
476  * batadv_debugfs_add_hardif - creates the base directory for a hard interface
477  *  in debugfs.
478  * @hard_iface: hard interface which should be added.
479  */
480 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
481 {
482         struct batadv_debuginfo **bat_debug;
483         struct dentry *file;
484
485         if (!batadv_debugfs)
486                 goto out;
487
488         hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
489                                                    batadv_debugfs);
490         if (!hard_iface->debug_dir)
491                 goto out;
492
493         for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
494                 file = debugfs_create_file(((*bat_debug)->attr).name,
495                                            S_IFREG | ((*bat_debug)->attr).mode,
496                                            hard_iface->debug_dir,
497                                            hard_iface->net_dev,
498                                            &(*bat_debug)->fops);
499                 if (!file)
500                         goto rem_attr;
501         }
502
503         return 0;
504 rem_attr:
505         debugfs_remove_recursive(hard_iface->debug_dir);
506         hard_iface->debug_dir = NULL;
507 out:
508         return -ENOMEM;
509 }
510
511 /**
512  * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
513  * @hard_iface: hard interface which was renamed
514  */
515 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
516 {
517         const char *name = hard_iface->net_dev->name;
518         struct dentry *dir;
519         struct dentry *d;
520
521         dir = hard_iface->debug_dir;
522         if (!dir)
523                 return;
524
525         d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
526         if (!d)
527                 pr_err("Can't rename debugfs dir to %s\n", name);
528 }
529
530 /**
531  * batadv_debugfs_del_hardif - delete the base directory for a hard interface
532  *  in debugfs.
533  * @hard_iface: hard interface which is deleted.
534  */
535 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
536 {
537         if (batadv_debugfs) {
538                 debugfs_remove_recursive(hard_iface->debug_dir);
539                 hard_iface->debug_dir = NULL;
540         }
541 }
542
543 int batadv_debugfs_add_meshif(struct net_device *dev)
544 {
545         struct batadv_priv *bat_priv = netdev_priv(dev);
546         struct batadv_debuginfo **bat_debug;
547         struct dentry *file;
548
549         if (!batadv_debugfs)
550                 goto out;
551
552         bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
553         if (!bat_priv->debug_dir)
554                 goto out;
555
556         if (batadv_socket_setup(bat_priv) < 0)
557                 goto rem_attr;
558
559         if (batadv_debug_log_setup(bat_priv) < 0)
560                 goto rem_attr;
561
562         for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
563                 file = debugfs_create_file(((*bat_debug)->attr).name,
564                                            S_IFREG | ((*bat_debug)->attr).mode,
565                                            bat_priv->debug_dir,
566                                            dev, &(*bat_debug)->fops);
567                 if (!file) {
568                         batadv_err(dev, "Can't add debugfs file: %s/%s\n",
569                                    dev->name, ((*bat_debug)->attr).name);
570                         goto rem_attr;
571                 }
572         }
573
574         if (batadv_nc_init_debugfs(bat_priv) < 0)
575                 goto rem_attr;
576
577         return 0;
578 rem_attr:
579         debugfs_remove_recursive(bat_priv->debug_dir);
580         bat_priv->debug_dir = NULL;
581 out:
582         return -ENOMEM;
583 }
584
585 /**
586  * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
587  * @dev: net_device which was renamed
588  */
589 void batadv_debugfs_rename_meshif(struct net_device *dev)
590 {
591         struct batadv_priv *bat_priv = netdev_priv(dev);
592         const char *name = dev->name;
593         struct dentry *dir;
594         struct dentry *d;
595
596         dir = bat_priv->debug_dir;
597         if (!dir)
598                 return;
599
600         d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
601         if (!d)
602                 pr_err("Can't rename debugfs dir to %s\n", name);
603 }
604
605 void batadv_debugfs_del_meshif(struct net_device *dev)
606 {
607         struct batadv_priv *bat_priv = netdev_priv(dev);
608
609         batadv_debug_log_cleanup(bat_priv);
610
611         if (batadv_debugfs) {
612                 debugfs_remove_recursive(bat_priv->debug_dir);
613                 bat_priv->debug_dir = NULL;
614         }
615 }