1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2020 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/errno.h>
14 #include <linux/export.h>
16 #include <linux/netdevice.h>
17 #include <linux/printk.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
20 #include <linux/stat.h>
21 #include <linux/stddef.h>
22 #include <linux/stringify.h>
23 #include <linux/sysfs.h>
24 #include <net/net_namespace.h>
27 #include "bridge_loop_avoidance.h"
28 #include "distributed-arp-table.h"
29 #include "gateway_client.h"
30 #include "icmp_socket.h"
32 #include "multicast.h"
33 #include "network-coding.h"
34 #include "originator.h"
35 #include "translation-table.h"
37 static struct dentry
*batadv_debugfs
;
40 * batadv_debugfs_deprecated() - Log use of deprecated batadv debugfs access
41 * @file: file which was accessed
42 * @alt: explanation what can be used as alternative
44 void batadv_debugfs_deprecated(struct file
*file
, const char *alt
)
46 struct dentry
*dentry
= file_dentry(file
);
47 const char *name
= dentry
->d_name
.name
;
49 pr_warn_ratelimited(DEPRECATED
"%s (pid %d) Use of debugfs file \"%s\".\n%s",
50 current
->comm
, task_pid_nr(current
), name
, alt
);
53 static int batadv_algorithms_open(struct inode
*inode
, struct file
*file
)
55 batadv_debugfs_deprecated(file
,
56 "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
57 return single_open(file
, batadv_algo_seq_print_text
, NULL
);
60 static int neighbors_open(struct inode
*inode
, struct file
*file
)
62 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
64 batadv_debugfs_deprecated(file
,
65 "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
66 return single_open(file
, batadv_hardif_neigh_seq_print_text
, net_dev
);
69 static int batadv_originators_open(struct inode
*inode
, struct file
*file
)
71 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
73 batadv_debugfs_deprecated(file
,
74 "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
75 return single_open(file
, batadv_orig_seq_print_text
, net_dev
);
79 * batadv_originators_hardif_open() - handles debugfs output for the originator
80 * table of an hard interface
81 * @inode: inode pointer to debugfs file
82 * @file: pointer to the seq_file
84 * Return: 0 on success or negative error number in case of failure
86 static int batadv_originators_hardif_open(struct inode
*inode
,
89 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
91 batadv_debugfs_deprecated(file
,
92 "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
93 return single_open(file
, batadv_orig_hardif_seq_print_text
, net_dev
);
96 static int batadv_gateways_open(struct inode
*inode
, struct file
*file
)
98 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
100 batadv_debugfs_deprecated(file
,
101 "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
102 return single_open(file
, batadv_gw_client_seq_print_text
, net_dev
);
105 static int batadv_transtable_global_open(struct inode
*inode
, struct file
*file
)
107 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
109 batadv_debugfs_deprecated(file
,
110 "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
111 return single_open(file
, batadv_tt_global_seq_print_text
, net_dev
);
114 #ifdef CONFIG_BATMAN_ADV_BLA
115 static int batadv_bla_claim_table_open(struct inode
*inode
, struct file
*file
)
117 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
119 batadv_debugfs_deprecated(file
,
120 "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
121 return single_open(file
, batadv_bla_claim_table_seq_print_text
,
125 static int batadv_bla_backbone_table_open(struct inode
*inode
,
128 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
130 batadv_debugfs_deprecated(file
,
131 "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
132 return single_open(file
, batadv_bla_backbone_table_seq_print_text
,
138 #ifdef CONFIG_BATMAN_ADV_DAT
140 * batadv_dat_cache_open() - Prepare file handler for reads from dat_cache
141 * @inode: inode which was opened
142 * @file: file handle to be initialized
144 * Return: 0 on success or negative error number in case of failure
146 static int batadv_dat_cache_open(struct inode
*inode
, struct file
*file
)
148 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
150 batadv_debugfs_deprecated(file
,
151 "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
152 return single_open(file
, batadv_dat_cache_seq_print_text
, net_dev
);
156 static int batadv_transtable_local_open(struct inode
*inode
, struct file
*file
)
158 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
160 batadv_debugfs_deprecated(file
,
161 "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
162 return single_open(file
, batadv_tt_local_seq_print_text
, net_dev
);
165 struct batadv_debuginfo
{
166 struct attribute attr
;
167 const struct file_operations fops
;
170 #ifdef CONFIG_BATMAN_ADV_NC
171 static int batadv_nc_nodes_open(struct inode
*inode
, struct file
*file
)
173 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
175 batadv_debugfs_deprecated(file
, "");
176 return single_open(file
, batadv_nc_nodes_seq_print_text
, net_dev
);
180 #ifdef CONFIG_BATMAN_ADV_MCAST
182 * batadv_mcast_flags_open() - prepare file handler for reads from mcast_flags
183 * @inode: inode which was opened
184 * @file: file handle to be initialized
186 * Return: 0 on success or negative error number in case of failure
188 static int batadv_mcast_flags_open(struct inode
*inode
, struct file
*file
)
190 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
192 batadv_debugfs_deprecated(file
,
193 "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
194 return single_open(file
, batadv_mcast_flags_seq_print_text
, net_dev
);
198 #define BATADV_DEBUGINFO(_name, _mode, _open) \
199 struct batadv_debuginfo batadv_debuginfo_##_name = { \
201 .name = __stringify(_name), \
205 .owner = THIS_MODULE, \
208 .llseek = seq_lseek, \
209 .release = single_release, \
213 /* the following attributes are general and therefore they will be directly
214 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
216 static BATADV_DEBUGINFO(routing_algos
, 0444, batadv_algorithms_open
);
218 static struct batadv_debuginfo
*batadv_general_debuginfos
[] = {
219 &batadv_debuginfo_routing_algos
,
223 /* The following attributes are per soft interface */
224 static BATADV_DEBUGINFO(neighbors
, 0444, neighbors_open
);
225 static BATADV_DEBUGINFO(originators
, 0444, batadv_originators_open
);
226 static BATADV_DEBUGINFO(gateways
, 0444, batadv_gateways_open
);
227 static BATADV_DEBUGINFO(transtable_global
, 0444, batadv_transtable_global_open
);
228 #ifdef CONFIG_BATMAN_ADV_BLA
229 static BATADV_DEBUGINFO(bla_claim_table
, 0444, batadv_bla_claim_table_open
);
230 static BATADV_DEBUGINFO(bla_backbone_table
, 0444,
231 batadv_bla_backbone_table_open
);
233 #ifdef CONFIG_BATMAN_ADV_DAT
234 static BATADV_DEBUGINFO(dat_cache
, 0444, batadv_dat_cache_open
);
236 static BATADV_DEBUGINFO(transtable_local
, 0444, batadv_transtable_local_open
);
237 #ifdef CONFIG_BATMAN_ADV_NC
238 static BATADV_DEBUGINFO(nc_nodes
, 0444, batadv_nc_nodes_open
);
240 #ifdef CONFIG_BATMAN_ADV_MCAST
241 static BATADV_DEBUGINFO(mcast_flags
, 0444, batadv_mcast_flags_open
);
244 static struct batadv_debuginfo
*batadv_mesh_debuginfos
[] = {
245 &batadv_debuginfo_neighbors
,
246 &batadv_debuginfo_originators
,
247 &batadv_debuginfo_gateways
,
248 &batadv_debuginfo_transtable_global
,
249 #ifdef CONFIG_BATMAN_ADV_BLA
250 &batadv_debuginfo_bla_claim_table
,
251 &batadv_debuginfo_bla_backbone_table
,
253 #ifdef CONFIG_BATMAN_ADV_DAT
254 &batadv_debuginfo_dat_cache
,
256 &batadv_debuginfo_transtable_local
,
257 #ifdef CONFIG_BATMAN_ADV_NC
258 &batadv_debuginfo_nc_nodes
,
260 #ifdef CONFIG_BATMAN_ADV_MCAST
261 &batadv_debuginfo_mcast_flags
,
266 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
267 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
269 .name = __stringify(_name), \
273 .owner = THIS_MODULE, \
276 .llseek = seq_lseek, \
277 .release = single_release, \
281 static BATADV_HARDIF_DEBUGINFO(originators
, 0444,
282 batadv_originators_hardif_open
);
284 static struct batadv_debuginfo
*batadv_hardif_debuginfos
[] = {
285 &batadv_hardif_debuginfo_originators
,
290 * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
292 void batadv_debugfs_init(void)
294 struct batadv_debuginfo
**bat_debug
;
296 batadv_debugfs
= debugfs_create_dir(BATADV_DEBUGFS_SUBDIR
, NULL
);
298 for (bat_debug
= batadv_general_debuginfos
; *bat_debug
; ++bat_debug
)
299 debugfs_create_file(((*bat_debug
)->attr
).name
,
300 S_IFREG
| ((*bat_debug
)->attr
).mode
,
301 batadv_debugfs
, NULL
, &(*bat_debug
)->fops
);
305 * batadv_debugfs_destroy() - Remove all debugfs entries
307 void batadv_debugfs_destroy(void)
309 debugfs_remove_recursive(batadv_debugfs
);
310 batadv_debugfs
= NULL
;
314 * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
316 * @hard_iface: hard interface which should be added.
318 void batadv_debugfs_add_hardif(struct batadv_hard_iface
*hard_iface
)
320 struct net
*net
= dev_net(hard_iface
->net_dev
);
321 struct batadv_debuginfo
**bat_debug
;
323 if (net
!= &init_net
)
326 hard_iface
->debug_dir
= debugfs_create_dir(hard_iface
->net_dev
->name
,
329 for (bat_debug
= batadv_hardif_debuginfos
; *bat_debug
; ++bat_debug
)
330 debugfs_create_file(((*bat_debug
)->attr
).name
,
331 S_IFREG
| ((*bat_debug
)->attr
).mode
,
332 hard_iface
->debug_dir
, hard_iface
->net_dev
,
333 &(*bat_debug
)->fops
);
337 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
338 * @hard_iface: hard interface which was renamed
340 void batadv_debugfs_rename_hardif(struct batadv_hard_iface
*hard_iface
)
342 const char *name
= hard_iface
->net_dev
->name
;
345 dir
= hard_iface
->debug_dir
;
349 debugfs_rename(dir
->d_parent
, dir
, dir
->d_parent
, name
);
353 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
355 * @hard_iface: hard interface which is deleted.
357 void batadv_debugfs_del_hardif(struct batadv_hard_iface
*hard_iface
)
359 struct net
*net
= dev_net(hard_iface
->net_dev
);
361 if (net
!= &init_net
)
364 if (batadv_debugfs
) {
365 debugfs_remove_recursive(hard_iface
->debug_dir
);
366 hard_iface
->debug_dir
= NULL
;
371 * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
372 * @dev: netdev struct of the soft interface
374 * Return: 0 on success or negative error number in case of failure
376 int batadv_debugfs_add_meshif(struct net_device
*dev
)
378 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
379 struct batadv_debuginfo
**bat_debug
;
380 struct net
*net
= dev_net(dev
);
382 if (net
!= &init_net
)
385 bat_priv
->debug_dir
= debugfs_create_dir(dev
->name
, batadv_debugfs
);
387 batadv_socket_setup(bat_priv
);
389 if (batadv_debug_log_setup(bat_priv
) < 0)
392 for (bat_debug
= batadv_mesh_debuginfos
; *bat_debug
; ++bat_debug
)
393 debugfs_create_file(((*bat_debug
)->attr
).name
,
394 S_IFREG
| ((*bat_debug
)->attr
).mode
,
395 bat_priv
->debug_dir
, dev
,
396 &(*bat_debug
)->fops
);
398 batadv_nc_init_debugfs(bat_priv
);
402 debugfs_remove_recursive(bat_priv
->debug_dir
);
403 bat_priv
->debug_dir
= NULL
;
408 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
409 * @dev: net_device which was renamed
411 void batadv_debugfs_rename_meshif(struct net_device
*dev
)
413 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
414 const char *name
= dev
->name
;
417 dir
= bat_priv
->debug_dir
;
421 debugfs_rename(dir
->d_parent
, dir
, dir
->d_parent
, name
);
425 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
426 * @dev: netdev struct of the soft interface
428 void batadv_debugfs_del_meshif(struct net_device
*dev
)
430 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
431 struct net
*net
= dev_net(dev
);
433 if (net
!= &init_net
)
436 batadv_debug_log_cleanup(bat_priv
);
438 if (batadv_debugfs
) {
439 debugfs_remove_recursive(bat_priv
->debug_dir
);
440 bat_priv
->debug_dir
= NULL
;