blob: 452856c27d20b730788378cb5b6ba83ab8d275dd [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
Olivier Deprez157378f2022-04-04 15:47:50 +02002/* Copyright (C) 2010-2020 B.A.T.M.A.N. contributors:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 *
4 * Marek Lindner
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6
7#include "debugfs.h"
8#include "main.h"
9
David Brazdil0f672f62019-12-10 10:32:29 +000010#include <asm/current.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#include <linux/dcache.h>
12#include <linux/debugfs.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#include <linux/errno.h>
14#include <linux/export.h>
15#include <linux/fs.h>
16#include <linux/netdevice.h>
17#include <linux/printk.h>
David Brazdil0f672f62019-12-10 10:32:29 +000018#include <linux/sched.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#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>
25
26#include "bat_algo.h"
27#include "bridge_loop_avoidance.h"
28#include "distributed-arp-table.h"
29#include "gateway_client.h"
30#include "icmp_socket.h"
31#include "log.h"
32#include "multicast.h"
33#include "network-coding.h"
34#include "originator.h"
35#include "translation-table.h"
36
37static struct dentry *batadv_debugfs;
38
David Brazdil0f672f62019-12-10 10:32:29 +000039/**
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
43 */
44void batadv_debugfs_deprecated(struct file *file, const char *alt)
45{
46 struct dentry *dentry = file_dentry(file);
47 const char *name = dentry->d_name.name;
48
49 pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of debugfs file \"%s\".\n%s",
50 current->comm, task_pid_nr(current), name, alt);
51}
52
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053static int batadv_algorithms_open(struct inode *inode, struct file *file)
54{
David Brazdil0f672f62019-12-10 10:32:29 +000055 batadv_debugfs_deprecated(file,
56 "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057 return single_open(file, batadv_algo_seq_print_text, NULL);
58}
59
60static int neighbors_open(struct inode *inode, struct file *file)
61{
62 struct net_device *net_dev = (struct net_device *)inode->i_private;
63
David Brazdil0f672f62019-12-10 10:32:29 +000064 batadv_debugfs_deprecated(file,
65 "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066 return single_open(file, batadv_hardif_neigh_seq_print_text, net_dev);
67}
68
69static int batadv_originators_open(struct inode *inode, struct file *file)
70{
71 struct net_device *net_dev = (struct net_device *)inode->i_private;
72
David Brazdil0f672f62019-12-10 10:32:29 +000073 batadv_debugfs_deprecated(file,
74 "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 return single_open(file, batadv_orig_seq_print_text, net_dev);
76}
77
78/**
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
83 *
84 * Return: 0 on success or negative error number in case of failure
85 */
86static int batadv_originators_hardif_open(struct inode *inode,
87 struct file *file)
88{
89 struct net_device *net_dev = (struct net_device *)inode->i_private;
90
David Brazdil0f672f62019-12-10 10:32:29 +000091 batadv_debugfs_deprecated(file,
92 "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
94}
95
96static int batadv_gateways_open(struct inode *inode, struct file *file)
97{
98 struct net_device *net_dev = (struct net_device *)inode->i_private;
99
David Brazdil0f672f62019-12-10 10:32:29 +0000100 batadv_debugfs_deprecated(file,
101 "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
103}
104
105static int batadv_transtable_global_open(struct inode *inode, struct file *file)
106{
107 struct net_device *net_dev = (struct net_device *)inode->i_private;
108
David Brazdil0f672f62019-12-10 10:32:29 +0000109 batadv_debugfs_deprecated(file,
110 "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
112}
113
114#ifdef CONFIG_BATMAN_ADV_BLA
115static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
116{
117 struct net_device *net_dev = (struct net_device *)inode->i_private;
118
David Brazdil0f672f62019-12-10 10:32:29 +0000119 batadv_debugfs_deprecated(file,
120 "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 return single_open(file, batadv_bla_claim_table_seq_print_text,
122 net_dev);
123}
124
125static int batadv_bla_backbone_table_open(struct inode *inode,
126 struct file *file)
127{
128 struct net_device *net_dev = (struct net_device *)inode->i_private;
129
David Brazdil0f672f62019-12-10 10:32:29 +0000130 batadv_debugfs_deprecated(file,
131 "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 return single_open(file, batadv_bla_backbone_table_seq_print_text,
133 net_dev);
134}
135
136#endif
137
138#ifdef CONFIG_BATMAN_ADV_DAT
139/**
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
143 *
144 * Return: 0 on success or negative error number in case of failure
145 */
146static int batadv_dat_cache_open(struct inode *inode, struct file *file)
147{
148 struct net_device *net_dev = (struct net_device *)inode->i_private;
149
David Brazdil0f672f62019-12-10 10:32:29 +0000150 batadv_debugfs_deprecated(file,
151 "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
153}
154#endif
155
156static int batadv_transtable_local_open(struct inode *inode, struct file *file)
157{
158 struct net_device *net_dev = (struct net_device *)inode->i_private;
159
David Brazdil0f672f62019-12-10 10:32:29 +0000160 batadv_debugfs_deprecated(file,
161 "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
163}
164
165struct batadv_debuginfo {
166 struct attribute attr;
167 const struct file_operations fops;
168};
169
170#ifdef CONFIG_BATMAN_ADV_NC
171static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
172{
173 struct net_device *net_dev = (struct net_device *)inode->i_private;
174
David Brazdil0f672f62019-12-10 10:32:29 +0000175 batadv_debugfs_deprecated(file, "");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000176 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
177}
178#endif
179
180#ifdef CONFIG_BATMAN_ADV_MCAST
181/**
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
185 *
186 * Return: 0 on success or negative error number in case of failure
187 */
188static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
189{
190 struct net_device *net_dev = (struct net_device *)inode->i_private;
191
David Brazdil0f672f62019-12-10 10:32:29 +0000192 batadv_debugfs_deprecated(file,
193 "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194 return single_open(file, batadv_mcast_flags_seq_print_text, net_dev);
195}
196#endif
197
198#define BATADV_DEBUGINFO(_name, _mode, _open) \
199struct batadv_debuginfo batadv_debuginfo_##_name = { \
200 .attr = { \
201 .name = __stringify(_name), \
202 .mode = _mode, \
203 }, \
204 .fops = { \
205 .owner = THIS_MODULE, \
206 .open = _open, \
207 .read = seq_read, \
208 .llseek = seq_lseek, \
209 .release = single_release, \
210 }, \
211}
212
213/* the following attributes are general and therefore they will be directly
214 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
215 */
216static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
217
218static struct batadv_debuginfo *batadv_general_debuginfos[] = {
219 &batadv_debuginfo_routing_algos,
220 NULL,
221};
222
223/* The following attributes are per soft interface */
224static BATADV_DEBUGINFO(neighbors, 0444, neighbors_open);
225static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
226static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
227static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
228#ifdef CONFIG_BATMAN_ADV_BLA
229static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
230static BATADV_DEBUGINFO(bla_backbone_table, 0444,
231 batadv_bla_backbone_table_open);
232#endif
233#ifdef CONFIG_BATMAN_ADV_DAT
234static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
235#endif
236static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
237#ifdef CONFIG_BATMAN_ADV_NC
238static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
239#endif
240#ifdef CONFIG_BATMAN_ADV_MCAST
241static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
242#endif
243
244static 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,
252#endif
253#ifdef CONFIG_BATMAN_ADV_DAT
254 &batadv_debuginfo_dat_cache,
255#endif
256 &batadv_debuginfo_transtable_local,
257#ifdef CONFIG_BATMAN_ADV_NC
258 &batadv_debuginfo_nc_nodes,
259#endif
260#ifdef CONFIG_BATMAN_ADV_MCAST
261 &batadv_debuginfo_mcast_flags,
262#endif
263 NULL,
264};
265
266#define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
267struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
268 .attr = { \
269 .name = __stringify(_name), \
270 .mode = _mode, \
271 }, \
272 .fops = { \
273 .owner = THIS_MODULE, \
274 .open = _open, \
275 .read = seq_read, \
276 .llseek = seq_lseek, \
277 .release = single_release, \
278 }, \
279}
280
281static BATADV_HARDIF_DEBUGINFO(originators, 0444,
282 batadv_originators_hardif_open);
283
284static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
285 &batadv_hardif_debuginfo_originators,
286 NULL,
287};
288
289/**
290 * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
291 */
292void batadv_debugfs_init(void)
293{
294 struct batadv_debuginfo **bat_debug;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295
296 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297
David Brazdil0f672f62019-12-10 10:32:29 +0000298 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302}
303
304/**
305 * batadv_debugfs_destroy() - Remove all debugfs entries
306 */
307void batadv_debugfs_destroy(void)
308{
309 debugfs_remove_recursive(batadv_debugfs);
310 batadv_debugfs = NULL;
311}
312
313/**
314 * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
315 * in debugfs.
316 * @hard_iface: hard interface which should be added.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317 */
David Brazdil0f672f62019-12-10 10:32:29 +0000318void batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000319{
320 struct net *net = dev_net(hard_iface->net_dev);
321 struct batadv_debuginfo **bat_debug;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322
323 if (net != &init_net)
David Brazdil0f672f62019-12-10 10:32:29 +0000324 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325
326 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
327 batadv_debugfs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000328
David Brazdil0f672f62019-12-10 10:32:29 +0000329 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334}
335
336/**
337 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
338 * @hard_iface: hard interface which was renamed
339 */
340void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
341{
342 const char *name = hard_iface->net_dev->name;
343 struct dentry *dir;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344
345 dir = hard_iface->debug_dir;
346 if (!dir)
347 return;
348
David Brazdil0f672f62019-12-10 10:32:29 +0000349 debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350}
351
352/**
353 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
354 * in debugfs.
355 * @hard_iface: hard interface which is deleted.
356 */
357void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
358{
359 struct net *net = dev_net(hard_iface->net_dev);
360
361 if (net != &init_net)
362 return;
363
364 if (batadv_debugfs) {
365 debugfs_remove_recursive(hard_iface->debug_dir);
366 hard_iface->debug_dir = NULL;
367 }
368}
369
370/**
371 * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
372 * @dev: netdev struct of the soft interface
373 *
374 * Return: 0 on success or negative error number in case of failure
375 */
376int batadv_debugfs_add_meshif(struct net_device *dev)
377{
378 struct batadv_priv *bat_priv = netdev_priv(dev);
379 struct batadv_debuginfo **bat_debug;
380 struct net *net = dev_net(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000381
382 if (net != &init_net)
383 return 0;
384
385 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386
David Brazdil0f672f62019-12-10 10:32:29 +0000387 batadv_socket_setup(bat_priv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388
389 if (batadv_debug_log_setup(bat_priv) < 0)
390 goto rem_attr;
391
David Brazdil0f672f62019-12-10 10:32:29 +0000392 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000397
David Brazdil0f672f62019-12-10 10:32:29 +0000398 batadv_nc_init_debugfs(bat_priv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399
400 return 0;
401rem_attr:
402 debugfs_remove_recursive(bat_priv->debug_dir);
403 bat_priv->debug_dir = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000404 return -ENOMEM;
405}
406
407/**
408 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
409 * @dev: net_device which was renamed
410 */
411void batadv_debugfs_rename_meshif(struct net_device *dev)
412{
413 struct batadv_priv *bat_priv = netdev_priv(dev);
414 const char *name = dev->name;
415 struct dentry *dir;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416
417 dir = bat_priv->debug_dir;
418 if (!dir)
419 return;
420
David Brazdil0f672f62019-12-10 10:32:29 +0000421 debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422}
423
424/**
425 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
426 * @dev: netdev struct of the soft interface
427 */
428void batadv_debugfs_del_meshif(struct net_device *dev)
429{
430 struct batadv_priv *bat_priv = netdev_priv(dev);
431 struct net *net = dev_net(dev);
432
433 if (net != &init_net)
434 return;
435
436 batadv_debug_log_cleanup(bat_priv);
437
438 if (batadv_debugfs) {
439 debugfs_remove_recursive(bat_priv->debug_dir);
440 bat_priv->debug_dir = NULL;
441 }
442}