1 /* Copyright (C) 2010-2015 B.A.T.M.A.N. contributors:
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.
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.
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/>.
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>
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>
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"
56 static struct dentry *batadv_debugfs;
58 #ifdef CONFIG_BATMAN_ADV_DEBUG
59 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
61 static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
63 static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
66 return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
69 static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
74 char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
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;
83 static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
87 static char debug_log_buf[256];
93 spin_lock_bh(&debug_log->lock);
95 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
98 for (p = debug_log_buf; *p != 0; p++)
99 batadv_emit_log_char(debug_log, *p);
101 spin_unlock_bh(&debug_log->lock);
103 wake_up(&debug_log->queue_wait);
108 int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
111 char tmp_log_buf[256];
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);
122 static int batadv_log_open(struct inode *inode, struct file *file)
124 if (!try_module_get(THIS_MODULE))
127 nonseekable_open(inode, file);
128 file->private_data = inode->i_private;
132 static int batadv_log_release(struct inode *inode, struct file *file)
134 module_put(THIS_MODULE);
138 static int batadv_log_empty(struct batadv_priv_debug_log *debug_log)
140 return !(debug_log->log_start - debug_log->log_end);
143 static ssize_t batadv_log_read(struct file *file, char __user *buf,
144 size_t count, loff_t *ppos)
146 struct batadv_priv *bat_priv = file->private_data;
147 struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
152 if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
161 if (!access_ok(VERIFY_WRITE, buf, count))
164 error = wait_event_interruptible(debug_log->queue_wait,
165 (!batadv_log_empty(debug_log)));
170 spin_lock_bh(&debug_log->lock);
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);
178 debug_log->log_start++;
180 spin_unlock_bh(&debug_log->lock);
182 error = __put_user(c, buf);
184 spin_lock_bh(&debug_log->lock);
190 spin_unlock_bh(&debug_log->lock);
198 static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
200 struct batadv_priv *bat_priv = file->private_data;
201 struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
203 poll_wait(file, &debug_log->queue_wait, wait);
205 if (!batadv_log_empty(debug_log))
206 return POLLIN | POLLRDNORM;
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,
217 .owner = THIS_MODULE,
220 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
224 if (!bat_priv->debug_dir)
227 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
228 if (!bat_priv->debug_log)
231 spin_lock_init(&bat_priv->debug_log->lock);
232 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
234 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
235 bat_priv->debug_dir, bat_priv,
246 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
248 kfree(bat_priv->debug_log);
249 bat_priv->debug_log = NULL;
251 #else /* CONFIG_BATMAN_ADV_DEBUG */
252 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
257 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
262 static int batadv_algorithms_open(struct inode *inode, struct file *file)
264 return single_open(file, batadv_algo_seq_print_text, NULL);
267 static int batadv_originators_open(struct inode *inode, struct file *file)
269 struct net_device *net_dev = (struct net_device *)inode->i_private;
271 return single_open(file, batadv_orig_seq_print_text, net_dev);
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
280 static int batadv_originators_hardif_open(struct inode *inode,
283 struct net_device *net_dev = (struct net_device *)inode->i_private;
285 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
288 static int batadv_gateways_open(struct inode *inode, struct file *file)
290 struct net_device *net_dev = (struct net_device *)inode->i_private;
292 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
295 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
297 struct net_device *net_dev = (struct net_device *)inode->i_private;
299 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
302 #ifdef CONFIG_BATMAN_ADV_BLA
303 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
305 struct net_device *net_dev = (struct net_device *)inode->i_private;
307 return single_open(file, batadv_bla_claim_table_seq_print_text,
311 static int batadv_bla_backbone_table_open(struct inode *inode,
314 struct net_device *net_dev = (struct net_device *)inode->i_private;
316 return single_open(file, batadv_bla_backbone_table_seq_print_text,
322 #ifdef CONFIG_BATMAN_ADV_DAT
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
328 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
330 struct net_device *net_dev = (struct net_device *)inode->i_private;
332 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
336 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
338 struct net_device *net_dev = (struct net_device *)inode->i_private;
340 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
343 struct batadv_debuginfo {
344 struct attribute attr;
345 const struct file_operations fops;
348 #ifdef CONFIG_BATMAN_ADV_NC
349 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
351 struct net_device *net_dev = (struct net_device *)inode->i_private;
353 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
357 #define BATADV_DEBUGINFO(_name, _mode, _open) \
358 struct batadv_debuginfo batadv_debuginfo_##_name = { \
359 .attr = { .name = __stringify(_name), \
361 .fops = { .owner = THIS_MODULE, \
364 .llseek = seq_lseek, \
365 .release = single_release, \
369 /* the following attributes are general and therefore they will be directly
370 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
372 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
374 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
375 &batadv_debuginfo_routing_algos,
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);
389 #ifdef CONFIG_BATMAN_ADV_DAT
390 static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
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);
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,
406 #ifdef CONFIG_BATMAN_ADV_DAT
407 &batadv_debuginfo_dat_cache,
409 &batadv_debuginfo_transtable_local,
410 #ifdef CONFIG_BATMAN_ADV_NC
411 &batadv_debuginfo_nc_nodes,
416 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
417 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
419 .name = __stringify(_name), \
423 .owner = THIS_MODULE, \
426 .llseek = seq_lseek, \
427 .release = single_release, \
431 static BATADV_HARDIF_DEBUGINFO(originators, S_IRUGO,
432 batadv_originators_hardif_open);
434 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
435 &batadv_hardif_debuginfo_originators,
439 void batadv_debugfs_init(void)
441 struct batadv_debuginfo **bat_debug;
444 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
445 if (batadv_debugfs == ERR_PTR(-ENODEV))
446 batadv_debugfs = NULL;
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);
457 pr_err("Can't add general debugfs file: %s\n",
458 ((*bat_debug)->attr).name);
465 debugfs_remove_recursive(batadv_debugfs);
466 batadv_debugfs = NULL;
469 void batadv_debugfs_destroy(void)
471 debugfs_remove_recursive(batadv_debugfs);
472 batadv_debugfs = NULL;
476 * batadv_debugfs_add_hardif - creates the base directory for a hard interface
478 * @hard_iface: hard interface which should be added.
480 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
482 struct batadv_debuginfo **bat_debug;
488 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
490 if (!hard_iface->debug_dir)
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,
498 &(*bat_debug)->fops);
505 debugfs_remove_recursive(hard_iface->debug_dir);
506 hard_iface->debug_dir = NULL;
512 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
513 * @hard_iface: hard interface which was renamed
515 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
517 const char *name = hard_iface->net_dev->name;
521 dir = hard_iface->debug_dir;
525 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
527 pr_err("Can't rename debugfs dir to %s\n", name);
531 * batadv_debugfs_del_hardif - delete the base directory for a hard interface
533 * @hard_iface: hard interface which is deleted.
535 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
537 if (batadv_debugfs) {
538 debugfs_remove_recursive(hard_iface->debug_dir);
539 hard_iface->debug_dir = NULL;
543 int batadv_debugfs_add_meshif(struct net_device *dev)
545 struct batadv_priv *bat_priv = netdev_priv(dev);
546 struct batadv_debuginfo **bat_debug;
552 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
553 if (!bat_priv->debug_dir)
556 if (batadv_socket_setup(bat_priv) < 0)
559 if (batadv_debug_log_setup(bat_priv) < 0)
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,
566 dev, &(*bat_debug)->fops);
568 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
569 dev->name, ((*bat_debug)->attr).name);
574 if (batadv_nc_init_debugfs(bat_priv) < 0)
579 debugfs_remove_recursive(bat_priv->debug_dir);
580 bat_priv->debug_dir = NULL;
586 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
587 * @dev: net_device which was renamed
589 void batadv_debugfs_rename_meshif(struct net_device *dev)
591 struct batadv_priv *bat_priv = netdev_priv(dev);
592 const char *name = dev->name;
596 dir = bat_priv->debug_dir;
600 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
602 pr_err("Can't rename debugfs dir to %s\n", name);
605 void batadv_debugfs_del_meshif(struct net_device *dev)
607 struct batadv_priv *bat_priv = netdev_priv(dev);
609 batadv_debug_log_cleanup(bat_priv);
611 if (batadv_debugfs) {
612 debugfs_remove_recursive(bat_priv->debug_dir);
613 bat_priv->debug_dir = NULL;