1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
10 #include <asm/current.h>
11 #include <linux/dcache.h>
12 #include <linux/debugfs.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/export.h>
17 #include <linux/netdevice.h>
18 #include <linux/printk.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
21 #include <linux/stat.h>
22 #include <linux/stddef.h>
23 #include <linux/stringify.h>
24 #include <linux/sysfs.h>
25 #include <net/net_namespace.h>
28 #include "bridge_loop_avoidance.h"
29 #include "distributed-arp-table.h"
30 #include "gateway_client.h"
31 #include "icmp_socket.h"
33 #include "multicast.h"
34 #include "network-coding.h"
35 #include "originator.h"
36 #include "translation-table.h"
38 static struct dentry
*batadv_debugfs
;
41 * batadv_debugfs_deprecated() - Log use of deprecated batadv debugfs access
42 * @file: file which was accessed
43 * @alt: explanation what can be used as alternative
45 void batadv_debugfs_deprecated(struct file
*file
, const char *alt
)
47 struct dentry
*dentry
= file_dentry(file
);
48 const char *name
= dentry
->d_name
.name
;
50 pr_warn_ratelimited(DEPRECATED
"%s (pid %d) Use of debugfs file \"%s\".\n%s",
51 current
->comm
, task_pid_nr(current
), name
, alt
);
54 static int batadv_algorithms_open(struct inode
*inode
, struct file
*file
)
56 batadv_debugfs_deprecated(file
,
57 "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
58 return single_open(file
, batadv_algo_seq_print_text
, NULL
);
61 static int neighbors_open(struct inode
*inode
, struct file
*file
)
63 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
65 batadv_debugfs_deprecated(file
,
66 "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
67 return single_open(file
, batadv_hardif_neigh_seq_print_text
, net_dev
);
70 static int batadv_originators_open(struct inode
*inode
, struct file
*file
)
72 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
74 batadv_debugfs_deprecated(file
,
75 "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
76 return single_open(file
, batadv_orig_seq_print_text
, net_dev
);
80 * batadv_originators_hardif_open() - handles debugfs output for the originator
81 * table of an hard interface
82 * @inode: inode pointer to debugfs file
83 * @file: pointer to the seq_file
85 * Return: 0 on success or negative error number in case of failure
87 static int batadv_originators_hardif_open(struct inode
*inode
,
90 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
92 batadv_debugfs_deprecated(file
,
93 "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
94 return single_open(file
, batadv_orig_hardif_seq_print_text
, net_dev
);
97 static int batadv_gateways_open(struct inode
*inode
, struct file
*file
)
99 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
101 batadv_debugfs_deprecated(file
,
102 "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
103 return single_open(file
, batadv_gw_client_seq_print_text
, net_dev
);
106 static int batadv_transtable_global_open(struct inode
*inode
, struct file
*file
)
108 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
110 batadv_debugfs_deprecated(file
,
111 "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
112 return single_open(file
, batadv_tt_global_seq_print_text
, net_dev
);
115 #ifdef CONFIG_BATMAN_ADV_BLA
116 static int batadv_bla_claim_table_open(struct inode
*inode
, struct file
*file
)
118 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
120 batadv_debugfs_deprecated(file
,
121 "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
122 return single_open(file
, batadv_bla_claim_table_seq_print_text
,
126 static int batadv_bla_backbone_table_open(struct inode
*inode
,
129 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
131 batadv_debugfs_deprecated(file
,
132 "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
133 return single_open(file
, batadv_bla_backbone_table_seq_print_text
,
139 #ifdef CONFIG_BATMAN_ADV_DAT
141 * batadv_dat_cache_open() - Prepare file handler for reads from dat_cache
142 * @inode: inode which was opened
143 * @file: file handle to be initialized
145 * Return: 0 on success or negative error number in case of failure
147 static int batadv_dat_cache_open(struct inode
*inode
, struct file
*file
)
149 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
151 batadv_debugfs_deprecated(file
,
152 "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
153 return single_open(file
, batadv_dat_cache_seq_print_text
, net_dev
);
157 static int batadv_transtable_local_open(struct inode
*inode
, struct file
*file
)
159 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
161 batadv_debugfs_deprecated(file
,
162 "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
163 return single_open(file
, batadv_tt_local_seq_print_text
, net_dev
);
166 struct batadv_debuginfo
{
167 struct attribute attr
;
168 const struct file_operations fops
;
171 #ifdef CONFIG_BATMAN_ADV_NC
172 static int batadv_nc_nodes_open(struct inode
*inode
, struct file
*file
)
174 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
176 batadv_debugfs_deprecated(file
, "");
177 return single_open(file
, batadv_nc_nodes_seq_print_text
, net_dev
);
181 #ifdef CONFIG_BATMAN_ADV_MCAST
183 * batadv_mcast_flags_open() - prepare file handler for reads from mcast_flags
184 * @inode: inode which was opened
185 * @file: file handle to be initialized
187 * Return: 0 on success or negative error number in case of failure
189 static int batadv_mcast_flags_open(struct inode
*inode
, struct file
*file
)
191 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
193 batadv_debugfs_deprecated(file
,
194 "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
195 return single_open(file
, batadv_mcast_flags_seq_print_text
, net_dev
);
199 #define BATADV_DEBUGINFO(_name, _mode, _open) \
200 struct batadv_debuginfo batadv_debuginfo_##_name = { \
202 .name = __stringify(_name), \
206 .owner = THIS_MODULE, \
209 .llseek = seq_lseek, \
210 .release = single_release, \
214 /* the following attributes are general and therefore they will be directly
215 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
217 static BATADV_DEBUGINFO(routing_algos
, 0444, batadv_algorithms_open
);
219 static struct batadv_debuginfo
*batadv_general_debuginfos
[] = {
220 &batadv_debuginfo_routing_algos
,
224 /* The following attributes are per soft interface */
225 static BATADV_DEBUGINFO(neighbors
, 0444, neighbors_open
);
226 static BATADV_DEBUGINFO(originators
, 0444, batadv_originators_open
);
227 static BATADV_DEBUGINFO(gateways
, 0444, batadv_gateways_open
);
228 static BATADV_DEBUGINFO(transtable_global
, 0444, batadv_transtable_global_open
);
229 #ifdef CONFIG_BATMAN_ADV_BLA
230 static BATADV_DEBUGINFO(bla_claim_table
, 0444, batadv_bla_claim_table_open
);
231 static BATADV_DEBUGINFO(bla_backbone_table
, 0444,
232 batadv_bla_backbone_table_open
);
234 #ifdef CONFIG_BATMAN_ADV_DAT
235 static BATADV_DEBUGINFO(dat_cache
, 0444, batadv_dat_cache_open
);
237 static BATADV_DEBUGINFO(transtable_local
, 0444, batadv_transtable_local_open
);
238 #ifdef CONFIG_BATMAN_ADV_NC
239 static BATADV_DEBUGINFO(nc_nodes
, 0444, batadv_nc_nodes_open
);
241 #ifdef CONFIG_BATMAN_ADV_MCAST
242 static BATADV_DEBUGINFO(mcast_flags
, 0444, batadv_mcast_flags_open
);
245 static struct batadv_debuginfo
*batadv_mesh_debuginfos
[] = {
246 &batadv_debuginfo_neighbors
,
247 &batadv_debuginfo_originators
,
248 &batadv_debuginfo_gateways
,
249 &batadv_debuginfo_transtable_global
,
250 #ifdef CONFIG_BATMAN_ADV_BLA
251 &batadv_debuginfo_bla_claim_table
,
252 &batadv_debuginfo_bla_backbone_table
,
254 #ifdef CONFIG_BATMAN_ADV_DAT
255 &batadv_debuginfo_dat_cache
,
257 &batadv_debuginfo_transtable_local
,
258 #ifdef CONFIG_BATMAN_ADV_NC
259 &batadv_debuginfo_nc_nodes
,
261 #ifdef CONFIG_BATMAN_ADV_MCAST
262 &batadv_debuginfo_mcast_flags
,
267 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
268 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
270 .name = __stringify(_name), \
274 .owner = THIS_MODULE, \
277 .llseek = seq_lseek, \
278 .release = single_release, \
282 static BATADV_HARDIF_DEBUGINFO(originators
, 0444,
283 batadv_originators_hardif_open
);
285 static struct batadv_debuginfo
*batadv_hardif_debuginfos
[] = {
286 &batadv_hardif_debuginfo_originators
,
291 * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
293 void batadv_debugfs_init(void)
295 struct batadv_debuginfo
**bat_debug
;
298 batadv_debugfs
= debugfs_create_dir(BATADV_DEBUGFS_SUBDIR
, NULL
);
299 if (batadv_debugfs
== ERR_PTR(-ENODEV
))
300 batadv_debugfs
= NULL
;
305 for (bat_debug
= batadv_general_debuginfos
; *bat_debug
; ++bat_debug
) {
306 file
= debugfs_create_file(((*bat_debug
)->attr
).name
,
307 S_IFREG
| ((*bat_debug
)->attr
).mode
,
308 batadv_debugfs
, NULL
,
309 &(*bat_debug
)->fops
);
311 pr_err("Can't add general debugfs file: %s\n",
312 ((*bat_debug
)->attr
).name
);
319 debugfs_remove_recursive(batadv_debugfs
);
320 batadv_debugfs
= NULL
;
324 * batadv_debugfs_destroy() - Remove all debugfs entries
326 void batadv_debugfs_destroy(void)
328 debugfs_remove_recursive(batadv_debugfs
);
329 batadv_debugfs
= NULL
;
333 * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
335 * @hard_iface: hard interface which should be added.
337 * Return: 0 on success or negative error number in case of failure
339 int batadv_debugfs_add_hardif(struct batadv_hard_iface
*hard_iface
)
341 struct net
*net
= dev_net(hard_iface
->net_dev
);
342 struct batadv_debuginfo
**bat_debug
;
348 if (net
!= &init_net
)
351 hard_iface
->debug_dir
= debugfs_create_dir(hard_iface
->net_dev
->name
,
353 if (!hard_iface
->debug_dir
)
356 for (bat_debug
= batadv_hardif_debuginfos
; *bat_debug
; ++bat_debug
) {
357 file
= debugfs_create_file(((*bat_debug
)->attr
).name
,
358 S_IFREG
| ((*bat_debug
)->attr
).mode
,
359 hard_iface
->debug_dir
,
361 &(*bat_debug
)->fops
);
368 debugfs_remove_recursive(hard_iface
->debug_dir
);
369 hard_iface
->debug_dir
= NULL
;
375 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
376 * @hard_iface: hard interface which was renamed
378 void batadv_debugfs_rename_hardif(struct batadv_hard_iface
*hard_iface
)
380 const char *name
= hard_iface
->net_dev
->name
;
384 dir
= hard_iface
->debug_dir
;
388 d
= debugfs_rename(dir
->d_parent
, dir
, dir
->d_parent
, name
);
390 pr_err("Can't rename debugfs dir to %s\n", name
);
394 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
396 * @hard_iface: hard interface which is deleted.
398 void batadv_debugfs_del_hardif(struct batadv_hard_iface
*hard_iface
)
400 struct net
*net
= dev_net(hard_iface
->net_dev
);
402 if (net
!= &init_net
)
405 if (batadv_debugfs
) {
406 debugfs_remove_recursive(hard_iface
->debug_dir
);
407 hard_iface
->debug_dir
= NULL
;
412 * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
413 * @dev: netdev struct of the soft interface
415 * Return: 0 on success or negative error number in case of failure
417 int batadv_debugfs_add_meshif(struct net_device
*dev
)
419 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
420 struct batadv_debuginfo
**bat_debug
;
421 struct net
*net
= dev_net(dev
);
427 if (net
!= &init_net
)
430 bat_priv
->debug_dir
= debugfs_create_dir(dev
->name
, batadv_debugfs
);
431 if (!bat_priv
->debug_dir
)
434 if (batadv_socket_setup(bat_priv
) < 0)
437 if (batadv_debug_log_setup(bat_priv
) < 0)
440 for (bat_debug
= batadv_mesh_debuginfos
; *bat_debug
; ++bat_debug
) {
441 file
= debugfs_create_file(((*bat_debug
)->attr
).name
,
442 S_IFREG
| ((*bat_debug
)->attr
).mode
,
444 dev
, &(*bat_debug
)->fops
);
446 batadv_err(dev
, "Can't add debugfs file: %s/%s\n",
447 dev
->name
, ((*bat_debug
)->attr
).name
);
452 if (batadv_nc_init_debugfs(bat_priv
) < 0)
457 debugfs_remove_recursive(bat_priv
->debug_dir
);
458 bat_priv
->debug_dir
= NULL
;
464 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
465 * @dev: net_device which was renamed
467 void batadv_debugfs_rename_meshif(struct net_device
*dev
)
469 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
470 const char *name
= dev
->name
;
474 dir
= bat_priv
->debug_dir
;
478 d
= debugfs_rename(dir
->d_parent
, dir
, dir
->d_parent
, name
);
480 pr_err("Can't rename debugfs dir to %s\n", name
);
484 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
485 * @dev: netdev struct of the soft interface
487 void batadv_debugfs_del_meshif(struct net_device
*dev
)
489 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
490 struct net
*net
= dev_net(dev
);
492 if (net
!= &init_net
)
495 batadv_debug_log_cleanup(bat_priv
);
497 if (batadv_debugfs
) {
498 debugfs_remove_recursive(bat_priv
->debug_dir
);
499 bat_priv
->debug_dir
= NULL
;