Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux-2.6-microblaze.git] / net / batman-adv / debugfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2018  B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner
5  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "debugfs.h"
20 #include "main.h"
21
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>
28 #include <linux/fs.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>
38
39 #include "bat_algo.h"
40 #include "bridge_loop_avoidance.h"
41 #include "distributed-arp-table.h"
42 #include "gateway_client.h"
43 #include "icmp_socket.h"
44 #include "log.h"
45 #include "multicast.h"
46 #include "network-coding.h"
47 #include "originator.h"
48 #include "translation-table.h"
49
50 static struct dentry *batadv_debugfs;
51
52 /**
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
56  */
57 void batadv_debugfs_deprecated(struct file *file, const char *alt)
58 {
59         struct dentry *dentry = file_dentry(file);
60         const char *name = dentry->d_name.name;
61
62         pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of debugfs file \"%s\".\n%s",
63                             current->comm, task_pid_nr(current), name, alt);
64 }
65
66 static int batadv_algorithms_open(struct inode *inode, struct file *file)
67 {
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);
71 }
72
73 static int neighbors_open(struct inode *inode, struct file *file)
74 {
75         struct net_device *net_dev = (struct net_device *)inode->i_private;
76
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);
80 }
81
82 static int batadv_originators_open(struct inode *inode, struct file *file)
83 {
84         struct net_device *net_dev = (struct net_device *)inode->i_private;
85
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);
89 }
90
91 /**
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
96  *
97  * Return: 0 on success or negative error number in case of failure
98  */
99 static int batadv_originators_hardif_open(struct inode *inode,
100                                           struct file *file)
101 {
102         struct net_device *net_dev = (struct net_device *)inode->i_private;
103
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);
107 }
108
109 static int batadv_gateways_open(struct inode *inode, struct file *file)
110 {
111         struct net_device *net_dev = (struct net_device *)inode->i_private;
112
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);
116 }
117
118 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
119 {
120         struct net_device *net_dev = (struct net_device *)inode->i_private;
121
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);
125 }
126
127 #ifdef CONFIG_BATMAN_ADV_BLA
128 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
129 {
130         struct net_device *net_dev = (struct net_device *)inode->i_private;
131
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,
135                            net_dev);
136 }
137
138 static int batadv_bla_backbone_table_open(struct inode *inode,
139                                           struct file *file)
140 {
141         struct net_device *net_dev = (struct net_device *)inode->i_private;
142
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,
146                            net_dev);
147 }
148
149 #endif
150
151 #ifdef CONFIG_BATMAN_ADV_DAT
152 /**
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
156  *
157  * Return: 0 on success or negative error number in case of failure
158  */
159 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
160 {
161         struct net_device *net_dev = (struct net_device *)inode->i_private;
162
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);
166 }
167 #endif
168
169 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
170 {
171         struct net_device *net_dev = (struct net_device *)inode->i_private;
172
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);
176 }
177
178 struct batadv_debuginfo {
179         struct attribute attr;
180         const struct file_operations fops;
181 };
182
183 #ifdef CONFIG_BATMAN_ADV_NC
184 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
185 {
186         struct net_device *net_dev = (struct net_device *)inode->i_private;
187
188         batadv_debugfs_deprecated(file, "");
189         return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
190 }
191 #endif
192
193 #ifdef CONFIG_BATMAN_ADV_MCAST
194 /**
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
198  *
199  * Return: 0 on success or negative error number in case of failure
200  */
201 static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
202 {
203         struct net_device *net_dev = (struct net_device *)inode->i_private;
204
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);
208 }
209 #endif
210
211 #define BATADV_DEBUGINFO(_name, _mode, _open)           \
212 struct batadv_debuginfo batadv_debuginfo_##_name = {    \
213         .attr = {                                       \
214                 .name = __stringify(_name),             \
215                 .mode = _mode,                          \
216         },                                              \
217         .fops = {                                       \
218                 .owner = THIS_MODULE,                   \
219                 .open = _open,                          \
220                 .read   = seq_read,                     \
221                 .llseek = seq_lseek,                    \
222                 .release = single_release,              \
223         },                                              \
224 }
225
226 /* the following attributes are general and therefore they will be directly
227  * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
228  */
229 static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
230
231 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
232         &batadv_debuginfo_routing_algos,
233         NULL,
234 };
235
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);
245 #endif
246 #ifdef CONFIG_BATMAN_ADV_DAT
247 static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
248 #endif
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);
252 #endif
253 #ifdef CONFIG_BATMAN_ADV_MCAST
254 static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
255 #endif
256
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,
265 #endif
266 #ifdef CONFIG_BATMAN_ADV_DAT
267         &batadv_debuginfo_dat_cache,
268 #endif
269         &batadv_debuginfo_transtable_local,
270 #ifdef CONFIG_BATMAN_ADV_NC
271         &batadv_debuginfo_nc_nodes,
272 #endif
273 #ifdef CONFIG_BATMAN_ADV_MCAST
274         &batadv_debuginfo_mcast_flags,
275 #endif
276         NULL,
277 };
278
279 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open)            \
280 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = {     \
281         .attr = {                                               \
282                 .name = __stringify(_name),                     \
283                 .mode = _mode,                                  \
284         },                                                      \
285         .fops = {                                               \
286                 .owner = THIS_MODULE,                           \
287                 .open = _open,                                  \
288                 .read   = seq_read,                             \
289                 .llseek = seq_lseek,                            \
290                 .release = single_release,                      \
291         },                                                      \
292 }
293
294 static BATADV_HARDIF_DEBUGINFO(originators, 0444,
295                                batadv_originators_hardif_open);
296
297 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
298         &batadv_hardif_debuginfo_originators,
299         NULL,
300 };
301
302 /**
303  * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
304  */
305 void batadv_debugfs_init(void)
306 {
307         struct batadv_debuginfo **bat_debug;
308         struct dentry *file;
309
310         batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
311         if (batadv_debugfs == ERR_PTR(-ENODEV))
312                 batadv_debugfs = NULL;
313
314         if (!batadv_debugfs)
315                 goto err;
316
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);
322                 if (!file) {
323                         pr_err("Can't add general debugfs file: %s\n",
324                                ((*bat_debug)->attr).name);
325                         goto err;
326                 }
327         }
328
329         return;
330 err:
331         debugfs_remove_recursive(batadv_debugfs);
332         batadv_debugfs = NULL;
333 }
334
335 /**
336  * batadv_debugfs_destroy() - Remove all debugfs entries
337  */
338 void batadv_debugfs_destroy(void)
339 {
340         debugfs_remove_recursive(batadv_debugfs);
341         batadv_debugfs = NULL;
342 }
343
344 /**
345  * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
346  *  in debugfs.
347  * @hard_iface: hard interface which should be added.
348  *
349  * Return: 0 on success or negative error number in case of failure
350  */
351 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
352 {
353         struct net *net = dev_net(hard_iface->net_dev);
354         struct batadv_debuginfo **bat_debug;
355         struct dentry *file;
356
357         if (!batadv_debugfs)
358                 goto out;
359
360         if (net != &init_net)
361                 return 0;
362
363         hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
364                                                    batadv_debugfs);
365         if (!hard_iface->debug_dir)
366                 goto out;
367
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,
372                                            hard_iface->net_dev,
373                                            &(*bat_debug)->fops);
374                 if (!file)
375                         goto rem_attr;
376         }
377
378         return 0;
379 rem_attr:
380         debugfs_remove_recursive(hard_iface->debug_dir);
381         hard_iface->debug_dir = NULL;
382 out:
383         return -ENOMEM;
384 }
385
386 /**
387  * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
388  * @hard_iface: hard interface which was renamed
389  */
390 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
391 {
392         const char *name = hard_iface->net_dev->name;
393         struct dentry *dir;
394         struct dentry *d;
395
396         dir = hard_iface->debug_dir;
397         if (!dir)
398                 return;
399
400         d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
401         if (!d)
402                 pr_err("Can't rename debugfs dir to %s\n", name);
403 }
404
405 /**
406  * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
407  *  in debugfs.
408  * @hard_iface: hard interface which is deleted.
409  */
410 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
411 {
412         struct net *net = dev_net(hard_iface->net_dev);
413
414         if (net != &init_net)
415                 return;
416
417         if (batadv_debugfs) {
418                 debugfs_remove_recursive(hard_iface->debug_dir);
419                 hard_iface->debug_dir = NULL;
420         }
421 }
422
423 /**
424  * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
425  * @dev: netdev struct of the soft interface
426  *
427  * Return: 0 on success or negative error number in case of failure
428  */
429 int batadv_debugfs_add_meshif(struct net_device *dev)
430 {
431         struct batadv_priv *bat_priv = netdev_priv(dev);
432         struct batadv_debuginfo **bat_debug;
433         struct net *net = dev_net(dev);
434         struct dentry *file;
435
436         if (!batadv_debugfs)
437                 goto out;
438
439         if (net != &init_net)
440                 return 0;
441
442         bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
443         if (!bat_priv->debug_dir)
444                 goto out;
445
446         if (batadv_socket_setup(bat_priv) < 0)
447                 goto rem_attr;
448
449         if (batadv_debug_log_setup(bat_priv) < 0)
450                 goto rem_attr;
451
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,
455                                            bat_priv->debug_dir,
456                                            dev, &(*bat_debug)->fops);
457                 if (!file) {
458                         batadv_err(dev, "Can't add debugfs file: %s/%s\n",
459                                    dev->name, ((*bat_debug)->attr).name);
460                         goto rem_attr;
461                 }
462         }
463
464         if (batadv_nc_init_debugfs(bat_priv) < 0)
465                 goto rem_attr;
466
467         return 0;
468 rem_attr:
469         debugfs_remove_recursive(bat_priv->debug_dir);
470         bat_priv->debug_dir = NULL;
471 out:
472         return -ENOMEM;
473 }
474
475 /**
476  * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
477  * @dev: net_device which was renamed
478  */
479 void batadv_debugfs_rename_meshif(struct net_device *dev)
480 {
481         struct batadv_priv *bat_priv = netdev_priv(dev);
482         const char *name = dev->name;
483         struct dentry *dir;
484         struct dentry *d;
485
486         dir = bat_priv->debug_dir;
487         if (!dir)
488                 return;
489
490         d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
491         if (!d)
492                 pr_err("Can't rename debugfs dir to %s\n", name);
493 }
494
495 /**
496  * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
497  * @dev: netdev struct of the soft interface
498  */
499 void batadv_debugfs_del_meshif(struct net_device *dev)
500 {
501         struct batadv_priv *bat_priv = netdev_priv(dev);
502         struct net *net = dev_net(dev);
503
504         if (net != &init_net)
505                 return;
506
507         batadv_debug_log_cleanup(bat_priv);
508
509         if (batadv_debugfs) {
510                 debugfs_remove_recursive(bat_priv->debug_dir);
511                 bat_priv->debug_dir = NULL;
512         }
513 }