GNU Linux-libre 4.4.288-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 };
218
219 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
220 {
221         struct dentry *d;
222
223         if (!bat_priv->debug_dir)
224                 goto err;
225
226         bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
227         if (!bat_priv->debug_log)
228                 goto err;
229
230         spin_lock_init(&bat_priv->debug_log->lock);
231         init_waitqueue_head(&bat_priv->debug_log->queue_wait);
232
233         d = debugfs_create_file("log", S_IFREG | S_IRUSR,
234                                 bat_priv->debug_dir, bat_priv,
235                                 &batadv_log_fops);
236         if (!d)
237                 goto err;
238
239         return 0;
240
241 err:
242         return -ENOMEM;
243 }
244
245 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
246 {
247         kfree(bat_priv->debug_log);
248         bat_priv->debug_log = NULL;
249 }
250 #else /* CONFIG_BATMAN_ADV_DEBUG */
251 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
252 {
253         return 0;
254 }
255
256 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
257 {
258 }
259 #endif
260
261 static int batadv_algorithms_open(struct inode *inode, struct file *file)
262 {
263         return single_open(file, batadv_algo_seq_print_text, NULL);
264 }
265
266 static int batadv_originators_open(struct inode *inode, struct file *file)
267 {
268         struct net_device *net_dev = (struct net_device *)inode->i_private;
269
270         return single_open(file, batadv_orig_seq_print_text, net_dev);
271 }
272
273 /**
274  * batadv_originators_hardif_open - handles debugfs output for the
275  *  originator table of an hard interface
276  * @inode: inode pointer to debugfs file
277  * @file: pointer to the seq_file
278  */
279 static int batadv_originators_hardif_open(struct inode *inode,
280                                           struct file *file)
281 {
282         struct net_device *net_dev = (struct net_device *)inode->i_private;
283
284         return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
285 }
286
287 static int batadv_gateways_open(struct inode *inode, struct file *file)
288 {
289         struct net_device *net_dev = (struct net_device *)inode->i_private;
290
291         return single_open(file, batadv_gw_client_seq_print_text, net_dev);
292 }
293
294 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
295 {
296         struct net_device *net_dev = (struct net_device *)inode->i_private;
297
298         return single_open(file, batadv_tt_global_seq_print_text, net_dev);
299 }
300
301 #ifdef CONFIG_BATMAN_ADV_BLA
302 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
303 {
304         struct net_device *net_dev = (struct net_device *)inode->i_private;
305
306         return single_open(file, batadv_bla_claim_table_seq_print_text,
307                            net_dev);
308 }
309
310 static int batadv_bla_backbone_table_open(struct inode *inode,
311                                           struct file *file)
312 {
313         struct net_device *net_dev = (struct net_device *)inode->i_private;
314
315         return single_open(file, batadv_bla_backbone_table_seq_print_text,
316                            net_dev);
317 }
318
319 #endif
320
321 #ifdef CONFIG_BATMAN_ADV_DAT
322 /**
323  * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
324  * @inode: inode which was opened
325  * @file: file handle to be initialized
326  */
327 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
328 {
329         struct net_device *net_dev = (struct net_device *)inode->i_private;
330
331         return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
332 }
333 #endif
334
335 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
336 {
337         struct net_device *net_dev = (struct net_device *)inode->i_private;
338
339         return single_open(file, batadv_tt_local_seq_print_text, net_dev);
340 }
341
342 struct batadv_debuginfo {
343         struct attribute attr;
344         const struct file_operations fops;
345 };
346
347 #ifdef CONFIG_BATMAN_ADV_NC
348 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
349 {
350         struct net_device *net_dev = (struct net_device *)inode->i_private;
351
352         return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
353 }
354 #endif
355
356 #define BATADV_DEBUGINFO(_name, _mode, _open)           \
357 struct batadv_debuginfo batadv_debuginfo_##_name = {    \
358         .attr = { .name = __stringify(_name),           \
359                   .mode = _mode, },                     \
360         .fops = { .owner = THIS_MODULE,                 \
361                   .open = _open,                        \
362                   .read = seq_read,                     \
363                   .llseek = seq_lseek,                  \
364                   .release = single_release,            \
365                 }                                       \
366 }
367
368 /* the following attributes are general and therefore they will be directly
369  * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
370  */
371 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
372
373 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
374         &batadv_debuginfo_routing_algos,
375         NULL,
376 };
377
378 /* The following attributes are per soft interface */
379 static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
380 static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
381 static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
382                         batadv_transtable_global_open);
383 #ifdef CONFIG_BATMAN_ADV_BLA
384 static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
385 static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
386                         batadv_bla_backbone_table_open);
387 #endif
388 #ifdef CONFIG_BATMAN_ADV_DAT
389 static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
390 #endif
391 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
392                         batadv_transtable_local_open);
393 #ifdef CONFIG_BATMAN_ADV_NC
394 static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
395 #endif
396
397 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
398         &batadv_debuginfo_originators,
399         &batadv_debuginfo_gateways,
400         &batadv_debuginfo_transtable_global,
401 #ifdef CONFIG_BATMAN_ADV_BLA
402         &batadv_debuginfo_bla_claim_table,
403         &batadv_debuginfo_bla_backbone_table,
404 #endif
405 #ifdef CONFIG_BATMAN_ADV_DAT
406         &batadv_debuginfo_dat_cache,
407 #endif
408         &batadv_debuginfo_transtable_local,
409 #ifdef CONFIG_BATMAN_ADV_NC
410         &batadv_debuginfo_nc_nodes,
411 #endif
412         NULL,
413 };
414
415 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open)            \
416 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = {     \
417         .attr = {                                               \
418                 .name = __stringify(_name),                     \
419                 .mode = _mode,                                  \
420         },                                                      \
421         .fops = {                                               \
422                 .owner = THIS_MODULE,                           \
423                 .open = _open,                                  \
424                 .read   = seq_read,                             \
425                 .llseek = seq_lseek,                            \
426                 .release = single_release,                      \
427         },                                                      \
428 }
429
430 static BATADV_HARDIF_DEBUGINFO(originators, S_IRUGO,
431                                batadv_originators_hardif_open);
432
433 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
434         &batadv_hardif_debuginfo_originators,
435         NULL,
436 };
437
438 void batadv_debugfs_init(void)
439 {
440         struct batadv_debuginfo **bat_debug;
441         struct dentry *file;
442
443         batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
444         if (batadv_debugfs == ERR_PTR(-ENODEV))
445                 batadv_debugfs = NULL;
446
447         if (!batadv_debugfs)
448                 goto err;
449
450         for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
451                 file = debugfs_create_file(((*bat_debug)->attr).name,
452                                            S_IFREG | ((*bat_debug)->attr).mode,
453                                            batadv_debugfs, NULL,
454                                            &(*bat_debug)->fops);
455                 if (!file) {
456                         pr_err("Can't add general debugfs file: %s\n",
457                                ((*bat_debug)->attr).name);
458                         goto err;
459                 }
460         }
461
462         return;
463 err:
464         debugfs_remove_recursive(batadv_debugfs);
465         batadv_debugfs = NULL;
466 }
467
468 void batadv_debugfs_destroy(void)
469 {
470         debugfs_remove_recursive(batadv_debugfs);
471         batadv_debugfs = NULL;
472 }
473
474 /**
475  * batadv_debugfs_add_hardif - creates the base directory for a hard interface
476  *  in debugfs.
477  * @hard_iface: hard interface which should be added.
478  */
479 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
480 {
481         struct batadv_debuginfo **bat_debug;
482         struct dentry *file;
483
484         if (!batadv_debugfs)
485                 goto out;
486
487         hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
488                                                    batadv_debugfs);
489         if (!hard_iface->debug_dir)
490                 goto out;
491
492         for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
493                 file = debugfs_create_file(((*bat_debug)->attr).name,
494                                            S_IFREG | ((*bat_debug)->attr).mode,
495                                            hard_iface->debug_dir,
496                                            hard_iface->net_dev,
497                                            &(*bat_debug)->fops);
498                 if (!file)
499                         goto rem_attr;
500         }
501
502         return 0;
503 rem_attr:
504         debugfs_remove_recursive(hard_iface->debug_dir);
505         hard_iface->debug_dir = NULL;
506 out:
507         return -ENOMEM;
508 }
509
510 /**
511  * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
512  * @hard_iface: hard interface which was renamed
513  */
514 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
515 {
516         const char *name = hard_iface->net_dev->name;
517         struct dentry *dir;
518         struct dentry *d;
519
520         dir = hard_iface->debug_dir;
521         if (!dir)
522                 return;
523
524         d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
525         if (!d)
526                 pr_err("Can't rename debugfs dir to %s\n", name);
527 }
528
529 /**
530  * batadv_debugfs_del_hardif - delete the base directory for a hard interface
531  *  in debugfs.
532  * @hard_iface: hard interface which is deleted.
533  */
534 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
535 {
536         if (batadv_debugfs) {
537                 debugfs_remove_recursive(hard_iface->debug_dir);
538                 hard_iface->debug_dir = NULL;
539         }
540 }
541
542 int batadv_debugfs_add_meshif(struct net_device *dev)
543 {
544         struct batadv_priv *bat_priv = netdev_priv(dev);
545         struct batadv_debuginfo **bat_debug;
546         struct dentry *file;
547
548         if (!batadv_debugfs)
549                 goto out;
550
551         bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
552         if (!bat_priv->debug_dir)
553                 goto out;
554
555         if (batadv_socket_setup(bat_priv) < 0)
556                 goto rem_attr;
557
558         if (batadv_debug_log_setup(bat_priv) < 0)
559                 goto rem_attr;
560
561         for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
562                 file = debugfs_create_file(((*bat_debug)->attr).name,
563                                            S_IFREG | ((*bat_debug)->attr).mode,
564                                            bat_priv->debug_dir,
565                                            dev, &(*bat_debug)->fops);
566                 if (!file) {
567                         batadv_err(dev, "Can't add debugfs file: %s/%s\n",
568                                    dev->name, ((*bat_debug)->attr).name);
569                         goto rem_attr;
570                 }
571         }
572
573         if (batadv_nc_init_debugfs(bat_priv) < 0)
574                 goto rem_attr;
575
576         return 0;
577 rem_attr:
578         debugfs_remove_recursive(bat_priv->debug_dir);
579         bat_priv->debug_dir = NULL;
580 out:
581         return -ENOMEM;
582 }
583
584 /**
585  * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
586  * @dev: net_device which was renamed
587  */
588 void batadv_debugfs_rename_meshif(struct net_device *dev)
589 {
590         struct batadv_priv *bat_priv = netdev_priv(dev);
591         const char *name = dev->name;
592         struct dentry *dir;
593         struct dentry *d;
594
595         dir = bat_priv->debug_dir;
596         if (!dir)
597                 return;
598
599         d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
600         if (!d)
601                 pr_err("Can't rename debugfs dir to %s\n", name);
602 }
603
604 void batadv_debugfs_del_meshif(struct net_device *dev)
605 {
606         struct batadv_priv *bat_priv = netdev_priv(dev);
607
608         batadv_debug_log_cleanup(bat_priv);
609
610         if (batadv_debugfs) {
611                 debugfs_remove_recursive(bat_priv->debug_dir);
612                 bat_priv->debug_dir = NULL;
613         }
614 }