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,
219 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
223 if (!bat_priv->debug_dir)
226 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
227 if (!bat_priv->debug_log)
230 spin_lock_init(&bat_priv->debug_log->lock);
231 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
233 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
234 bat_priv->debug_dir, bat_priv,
245 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
247 kfree(bat_priv->debug_log);
248 bat_priv->debug_log = NULL;
250 #else /* CONFIG_BATMAN_ADV_DEBUG */
251 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
256 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
261 static int batadv_algorithms_open(struct inode *inode, struct file *file)
263 return single_open(file, batadv_algo_seq_print_text, NULL);
266 static int batadv_originators_open(struct inode *inode, struct file *file)
268 struct net_device *net_dev = (struct net_device *)inode->i_private;
270 return single_open(file, batadv_orig_seq_print_text, net_dev);
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
279 static int batadv_originators_hardif_open(struct inode *inode,
282 struct net_device *net_dev = (struct net_device *)inode->i_private;
284 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
287 static int batadv_gateways_open(struct inode *inode, struct file *file)
289 struct net_device *net_dev = (struct net_device *)inode->i_private;
291 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
294 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
296 struct net_device *net_dev = (struct net_device *)inode->i_private;
298 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
301 #ifdef CONFIG_BATMAN_ADV_BLA
302 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
304 struct net_device *net_dev = (struct net_device *)inode->i_private;
306 return single_open(file, batadv_bla_claim_table_seq_print_text,
310 static int batadv_bla_backbone_table_open(struct inode *inode,
313 struct net_device *net_dev = (struct net_device *)inode->i_private;
315 return single_open(file, batadv_bla_backbone_table_seq_print_text,
321 #ifdef CONFIG_BATMAN_ADV_DAT
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
327 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
329 struct net_device *net_dev = (struct net_device *)inode->i_private;
331 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
335 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
337 struct net_device *net_dev = (struct net_device *)inode->i_private;
339 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
342 struct batadv_debuginfo {
343 struct attribute attr;
344 const struct file_operations fops;
347 #ifdef CONFIG_BATMAN_ADV_NC
348 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
350 struct net_device *net_dev = (struct net_device *)inode->i_private;
352 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
356 #define BATADV_DEBUGINFO(_name, _mode, _open) \
357 struct batadv_debuginfo batadv_debuginfo_##_name = { \
358 .attr = { .name = __stringify(_name), \
360 .fops = { .owner = THIS_MODULE, \
363 .llseek = seq_lseek, \
364 .release = single_release, \
368 /* the following attributes are general and therefore they will be directly
369 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
371 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
373 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
374 &batadv_debuginfo_routing_algos,
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);
388 #ifdef CONFIG_BATMAN_ADV_DAT
389 static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
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);
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,
405 #ifdef CONFIG_BATMAN_ADV_DAT
406 &batadv_debuginfo_dat_cache,
408 &batadv_debuginfo_transtable_local,
409 #ifdef CONFIG_BATMAN_ADV_NC
410 &batadv_debuginfo_nc_nodes,
415 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
416 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
418 .name = __stringify(_name), \
422 .owner = THIS_MODULE, \
425 .llseek = seq_lseek, \
426 .release = single_release, \
430 static BATADV_HARDIF_DEBUGINFO(originators, S_IRUGO,
431 batadv_originators_hardif_open);
433 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
434 &batadv_hardif_debuginfo_originators,
438 void batadv_debugfs_init(void)
440 struct batadv_debuginfo **bat_debug;
443 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
444 if (batadv_debugfs == ERR_PTR(-ENODEV))
445 batadv_debugfs = NULL;
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);
456 pr_err("Can't add general debugfs file: %s\n",
457 ((*bat_debug)->attr).name);
464 debugfs_remove_recursive(batadv_debugfs);
465 batadv_debugfs = NULL;
468 void batadv_debugfs_destroy(void)
470 debugfs_remove_recursive(batadv_debugfs);
471 batadv_debugfs = NULL;
475 * batadv_debugfs_add_hardif - creates the base directory for a hard interface
477 * @hard_iface: hard interface which should be added.
479 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
481 struct batadv_debuginfo **bat_debug;
487 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
489 if (!hard_iface->debug_dir)
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,
497 &(*bat_debug)->fops);
504 debugfs_remove_recursive(hard_iface->debug_dir);
505 hard_iface->debug_dir = NULL;
511 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
512 * @hard_iface: hard interface which was renamed
514 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
516 const char *name = hard_iface->net_dev->name;
520 dir = hard_iface->debug_dir;
524 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
526 pr_err("Can't rename debugfs dir to %s\n", name);
530 * batadv_debugfs_del_hardif - delete the base directory for a hard interface
532 * @hard_iface: hard interface which is deleted.
534 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
536 if (batadv_debugfs) {
537 debugfs_remove_recursive(hard_iface->debug_dir);
538 hard_iface->debug_dir = NULL;
542 int batadv_debugfs_add_meshif(struct net_device *dev)
544 struct batadv_priv *bat_priv = netdev_priv(dev);
545 struct batadv_debuginfo **bat_debug;
551 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
552 if (!bat_priv->debug_dir)
555 if (batadv_socket_setup(bat_priv) < 0)
558 if (batadv_debug_log_setup(bat_priv) < 0)
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,
565 dev, &(*bat_debug)->fops);
567 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
568 dev->name, ((*bat_debug)->attr).name);
573 if (batadv_nc_init_debugfs(bat_priv) < 0)
578 debugfs_remove_recursive(bat_priv->debug_dir);
579 bat_priv->debug_dir = NULL;
585 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
586 * @dev: net_device which was renamed
588 void batadv_debugfs_rename_meshif(struct net_device *dev)
590 struct batadv_priv *bat_priv = netdev_priv(dev);
591 const char *name = dev->name;
595 dir = bat_priv->debug_dir;
599 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
601 pr_err("Can't rename debugfs dir to %s\n", name);
604 void batadv_debugfs_del_meshif(struct net_device *dev)
606 struct batadv_priv *bat_priv = netdev_priv(dev);
608 batadv_debug_log_cleanup(bat_priv);
610 if (batadv_debugfs) {
611 debugfs_remove_recursive(bat_priv->debug_dir);
612 bat_priv->debug_dir = NULL;