1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2018 B.A.T.M.A.N. contributors:
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <asm/current.h>
23 #include <linux/dcache.h>
24 #include <linux/debugfs.h>
25 #include <linux/err.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
29 #include <linux/netdevice.h>
30 #include <linux/printk.h>
31 #include <linux/sched.h>
32 #include <linux/seq_file.h>
33 #include <linux/stat.h>
34 #include <linux/stddef.h>
35 #include <linux/stringify.h>
36 #include <linux/sysfs.h>
37 #include <net/net_namespace.h>
40 #include "bridge_loop_avoidance.h"
41 #include "distributed-arp-table.h"
42 #include "gateway_client.h"
43 #include "icmp_socket.h"
45 #include "multicast.h"
46 #include "network-coding.h"
47 #include "originator.h"
48 #include "translation-table.h"
50 static struct dentry *batadv_debugfs;
53 * batadv_debugfs_deprecated() - Log use of deprecated batadv debugfs access
54 * @file: file which was accessed
55 * @alt: explanation what can be used as alternative
57 void batadv_debugfs_deprecated(struct file *file, const char *alt)
59 struct dentry *dentry = file_dentry(file);
60 const char *name = dentry->d_name.name;
62 pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of debugfs file \"%s\".\n%s",
63 current->comm, task_pid_nr(current), name, alt);
66 static int batadv_algorithms_open(struct inode *inode, struct file *file)
68 batadv_debugfs_deprecated(file,
69 "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
70 return single_open(file, batadv_algo_seq_print_text, NULL);
73 static int neighbors_open(struct inode *inode, struct file *file)
75 struct net_device *net_dev = (struct net_device *)inode->i_private;
77 batadv_debugfs_deprecated(file,
78 "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
79 return single_open(file, batadv_hardif_neigh_seq_print_text, net_dev);
82 static int batadv_originators_open(struct inode *inode, struct file *file)
84 struct net_device *net_dev = (struct net_device *)inode->i_private;
86 batadv_debugfs_deprecated(file,
87 "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
88 return single_open(file, batadv_orig_seq_print_text, net_dev);
92 * batadv_originators_hardif_open() - handles debugfs output for the originator
93 * table of an hard interface
94 * @inode: inode pointer to debugfs file
95 * @file: pointer to the seq_file
97 * Return: 0 on success or negative error number in case of failure
99 static int batadv_originators_hardif_open(struct inode *inode,
102 struct net_device *net_dev = (struct net_device *)inode->i_private;
104 batadv_debugfs_deprecated(file,
105 "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
106 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
109 static int batadv_gateways_open(struct inode *inode, struct file *file)
111 struct net_device *net_dev = (struct net_device *)inode->i_private;
113 batadv_debugfs_deprecated(file,
114 "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
115 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
118 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
120 struct net_device *net_dev = (struct net_device *)inode->i_private;
122 batadv_debugfs_deprecated(file,
123 "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
124 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
127 #ifdef CONFIG_BATMAN_ADV_BLA
128 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
130 struct net_device *net_dev = (struct net_device *)inode->i_private;
132 batadv_debugfs_deprecated(file,
133 "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
134 return single_open(file, batadv_bla_claim_table_seq_print_text,
138 static int batadv_bla_backbone_table_open(struct inode *inode,
141 struct net_device *net_dev = (struct net_device *)inode->i_private;
143 batadv_debugfs_deprecated(file,
144 "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
145 return single_open(file, batadv_bla_backbone_table_seq_print_text,
151 #ifdef CONFIG_BATMAN_ADV_DAT
153 * batadv_dat_cache_open() - Prepare file handler for reads from dat_cache
154 * @inode: inode which was opened
155 * @file: file handle to be initialized
157 * Return: 0 on success or negative error number in case of failure
159 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
161 struct net_device *net_dev = (struct net_device *)inode->i_private;
163 batadv_debugfs_deprecated(file,
164 "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
165 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
169 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
171 struct net_device *net_dev = (struct net_device *)inode->i_private;
173 batadv_debugfs_deprecated(file,
174 "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
175 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
178 struct batadv_debuginfo {
179 struct attribute attr;
180 const struct file_operations fops;
183 #ifdef CONFIG_BATMAN_ADV_NC
184 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
186 struct net_device *net_dev = (struct net_device *)inode->i_private;
188 batadv_debugfs_deprecated(file, "");
189 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
193 #ifdef CONFIG_BATMAN_ADV_MCAST
195 * batadv_mcast_flags_open() - prepare file handler for reads from mcast_flags
196 * @inode: inode which was opened
197 * @file: file handle to be initialized
199 * Return: 0 on success or negative error number in case of failure
201 static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
203 struct net_device *net_dev = (struct net_device *)inode->i_private;
205 batadv_debugfs_deprecated(file,
206 "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
207 return single_open(file, batadv_mcast_flags_seq_print_text, net_dev);
211 #define BATADV_DEBUGINFO(_name, _mode, _open) \
212 struct batadv_debuginfo batadv_debuginfo_##_name = { \
214 .name = __stringify(_name), \
218 .owner = THIS_MODULE, \
221 .llseek = seq_lseek, \
222 .release = single_release, \
226 /* the following attributes are general and therefore they will be directly
227 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
229 static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
231 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
232 &batadv_debuginfo_routing_algos,
236 /* The following attributes are per soft interface */
237 static BATADV_DEBUGINFO(neighbors, 0444, neighbors_open);
238 static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
239 static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
240 static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
241 #ifdef CONFIG_BATMAN_ADV_BLA
242 static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
243 static BATADV_DEBUGINFO(bla_backbone_table, 0444,
244 batadv_bla_backbone_table_open);
246 #ifdef CONFIG_BATMAN_ADV_DAT
247 static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
249 static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
250 #ifdef CONFIG_BATMAN_ADV_NC
251 static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
253 #ifdef CONFIG_BATMAN_ADV_MCAST
254 static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
257 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
258 &batadv_debuginfo_neighbors,
259 &batadv_debuginfo_originators,
260 &batadv_debuginfo_gateways,
261 &batadv_debuginfo_transtable_global,
262 #ifdef CONFIG_BATMAN_ADV_BLA
263 &batadv_debuginfo_bla_claim_table,
264 &batadv_debuginfo_bla_backbone_table,
266 #ifdef CONFIG_BATMAN_ADV_DAT
267 &batadv_debuginfo_dat_cache,
269 &batadv_debuginfo_transtable_local,
270 #ifdef CONFIG_BATMAN_ADV_NC
271 &batadv_debuginfo_nc_nodes,
273 #ifdef CONFIG_BATMAN_ADV_MCAST
274 &batadv_debuginfo_mcast_flags,
279 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
280 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
282 .name = __stringify(_name), \
286 .owner = THIS_MODULE, \
289 .llseek = seq_lseek, \
290 .release = single_release, \
294 static BATADV_HARDIF_DEBUGINFO(originators, 0444,
295 batadv_originators_hardif_open);
297 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
298 &batadv_hardif_debuginfo_originators,
303 * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
305 void batadv_debugfs_init(void)
307 struct batadv_debuginfo **bat_debug;
310 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
311 if (batadv_debugfs == ERR_PTR(-ENODEV))
312 batadv_debugfs = NULL;
317 for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
318 file = debugfs_create_file(((*bat_debug)->attr).name,
319 S_IFREG | ((*bat_debug)->attr).mode,
320 batadv_debugfs, NULL,
321 &(*bat_debug)->fops);
323 pr_err("Can't add general debugfs file: %s\n",
324 ((*bat_debug)->attr).name);
331 debugfs_remove_recursive(batadv_debugfs);
332 batadv_debugfs = NULL;
336 * batadv_debugfs_destroy() - Remove all debugfs entries
338 void batadv_debugfs_destroy(void)
340 debugfs_remove_recursive(batadv_debugfs);
341 batadv_debugfs = NULL;
345 * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
347 * @hard_iface: hard interface which should be added.
349 * Return: 0 on success or negative error number in case of failure
351 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
353 struct net *net = dev_net(hard_iface->net_dev);
354 struct batadv_debuginfo **bat_debug;
360 if (net != &init_net)
363 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
365 if (!hard_iface->debug_dir)
368 for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
369 file = debugfs_create_file(((*bat_debug)->attr).name,
370 S_IFREG | ((*bat_debug)->attr).mode,
371 hard_iface->debug_dir,
373 &(*bat_debug)->fops);
380 debugfs_remove_recursive(hard_iface->debug_dir);
381 hard_iface->debug_dir = NULL;
387 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
388 * @hard_iface: hard interface which was renamed
390 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
392 const char *name = hard_iface->net_dev->name;
396 dir = hard_iface->debug_dir;
400 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
402 pr_err("Can't rename debugfs dir to %s\n", name);
406 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
408 * @hard_iface: hard interface which is deleted.
410 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
412 struct net *net = dev_net(hard_iface->net_dev);
414 if (net != &init_net)
417 if (batadv_debugfs) {
418 debugfs_remove_recursive(hard_iface->debug_dir);
419 hard_iface->debug_dir = NULL;
424 * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
425 * @dev: netdev struct of the soft interface
427 * Return: 0 on success or negative error number in case of failure
429 int batadv_debugfs_add_meshif(struct net_device *dev)
431 struct batadv_priv *bat_priv = netdev_priv(dev);
432 struct batadv_debuginfo **bat_debug;
433 struct net *net = dev_net(dev);
439 if (net != &init_net)
442 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
443 if (!bat_priv->debug_dir)
446 if (batadv_socket_setup(bat_priv) < 0)
449 if (batadv_debug_log_setup(bat_priv) < 0)
452 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
453 file = debugfs_create_file(((*bat_debug)->attr).name,
454 S_IFREG | ((*bat_debug)->attr).mode,
456 dev, &(*bat_debug)->fops);
458 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
459 dev->name, ((*bat_debug)->attr).name);
464 if (batadv_nc_init_debugfs(bat_priv) < 0)
469 debugfs_remove_recursive(bat_priv->debug_dir);
470 bat_priv->debug_dir = NULL;
476 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
477 * @dev: net_device which was renamed
479 void batadv_debugfs_rename_meshif(struct net_device *dev)
481 struct batadv_priv *bat_priv = netdev_priv(dev);
482 const char *name = dev->name;
486 dir = bat_priv->debug_dir;
490 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
492 pr_err("Can't rename debugfs dir to %s\n", name);
496 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
497 * @dev: netdev struct of the soft interface
499 void batadv_debugfs_del_meshif(struct net_device *dev)
501 struct batadv_priv *bat_priv = netdev_priv(dev);
502 struct net *net = dev_net(dev);
504 if (net != &init_net)
507 batadv_debug_log_cleanup(bat_priv);
509 if (batadv_debugfs) {
510 debugfs_remove_recursive(bat_priv->debug_dir);
511 bat_priv->debug_dir = NULL;