1 /* Copyright (C) 2010-2017 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "debugfs.h"
19 #include "main.h"
20
21 #include <linux/dcache.h>
22 #include <linux/debugfs.h>
23 #include <linux/err.h>
24 #include <linux/errno.h>
25 #include <linux/export.h>
26 #include <linux/fs.h>
27 #include <linux/netdevice.h>
28 #include <linux/printk.h>
29 #include <linux/sched.h> /* for linux/wait.h */
30 #include <linux/seq_file.h>
31 #include <linux/stat.h>
32 #include <linux/stddef.h>
33 #include <linux/stringify.h>
34 #include <linux/sysfs.h>
35 #include <net/net_namespace.h>
36
37 #include "bat_algo.h"
38 #include "bridge_loop_avoidance.h"
39 #include "distributed-arp-table.h"
40 #include "gateway_client.h"
41 #include "icmp_socket.h"
42 #include "log.h"
43 #include "multicast.h"
44 #include "network-coding.h"
45 #include "originator.h"
46 #include "translation-table.h"
47
48 static struct dentry *batadv_debugfs;
49
batadv_algorithms_open(struct inode * inode,struct file * file)50 static int batadv_algorithms_open(struct inode *inode, struct file *file)
51 {
52 return single_open(file, batadv_algo_seq_print_text, NULL);
53 }
54
neighbors_open(struct inode * inode,struct file * file)55 static int neighbors_open(struct inode *inode, struct file *file)
56 {
57 struct net_device *net_dev = (struct net_device *)inode->i_private;
58
59 return single_open(file, batadv_hardif_neigh_seq_print_text, net_dev);
60 }
61
batadv_originators_open(struct inode * inode,struct file * file)62 static int batadv_originators_open(struct inode *inode, struct file *file)
63 {
64 struct net_device *net_dev = (struct net_device *)inode->i_private;
65
66 return single_open(file, batadv_orig_seq_print_text, net_dev);
67 }
68
69 /**
70 * batadv_originators_hardif_open - handles debugfs output for the
71 * originator table of an hard interface
72 * @inode: inode pointer to debugfs file
73 * @file: pointer to the seq_file
74 *
75 * Return: 0 on success or negative error number in case of failure
76 */
batadv_originators_hardif_open(struct inode * inode,struct file * file)77 static int batadv_originators_hardif_open(struct inode *inode,
78 struct file *file)
79 {
80 struct net_device *net_dev = (struct net_device *)inode->i_private;
81
82 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
83 }
84
batadv_gateways_open(struct inode * inode,struct file * file)85 static int batadv_gateways_open(struct inode *inode, struct file *file)
86 {
87 struct net_device *net_dev = (struct net_device *)inode->i_private;
88
89 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
90 }
91
batadv_transtable_global_open(struct inode * inode,struct file * file)92 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
93 {
94 struct net_device *net_dev = (struct net_device *)inode->i_private;
95
96 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
97 }
98
99 #ifdef CONFIG_BATMAN_ADV_BLA
batadv_bla_claim_table_open(struct inode * inode,struct file * file)100 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
101 {
102 struct net_device *net_dev = (struct net_device *)inode->i_private;
103
104 return single_open(file, batadv_bla_claim_table_seq_print_text,
105 net_dev);
106 }
107
batadv_bla_backbone_table_open(struct inode * inode,struct file * file)108 static int batadv_bla_backbone_table_open(struct inode *inode,
109 struct file *file)
110 {
111 struct net_device *net_dev = (struct net_device *)inode->i_private;
112
113 return single_open(file, batadv_bla_backbone_table_seq_print_text,
114 net_dev);
115 }
116
117 #endif
118
119 #ifdef CONFIG_BATMAN_ADV_DAT
120 /**
121 * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
122 * @inode: inode which was opened
123 * @file: file handle to be initialized
124 *
125 * Return: 0 on success or negative error number in case of failure
126 */
batadv_dat_cache_open(struct inode * inode,struct file * file)127 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
128 {
129 struct net_device *net_dev = (struct net_device *)inode->i_private;
130
131 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
132 }
133 #endif
134
batadv_transtable_local_open(struct inode * inode,struct file * file)135 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
136 {
137 struct net_device *net_dev = (struct net_device *)inode->i_private;
138
139 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
140 }
141
142 struct batadv_debuginfo {
143 struct attribute attr;
144 const struct file_operations fops;
145 };
146
147 #ifdef CONFIG_BATMAN_ADV_NC
batadv_nc_nodes_open(struct inode * inode,struct file * file)148 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
149 {
150 struct net_device *net_dev = (struct net_device *)inode->i_private;
151
152 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
153 }
154 #endif
155
156 #ifdef CONFIG_BATMAN_ADV_MCAST
157 /**
158 * batadv_mcast_flags_open - prepare file handler for reads from mcast_flags
159 * @inode: inode which was opened
160 * @file: file handle to be initialized
161 *
162 * Return: 0 on success or negative error number in case of failure
163 */
batadv_mcast_flags_open(struct inode * inode,struct file * file)164 static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
165 {
166 struct net_device *net_dev = (struct net_device *)inode->i_private;
167
168 return single_open(file, batadv_mcast_flags_seq_print_text, net_dev);
169 }
170 #endif
171
172 #define BATADV_DEBUGINFO(_name, _mode, _open) \
173 struct batadv_debuginfo batadv_debuginfo_##_name = { \
174 .attr = { \
175 .name = __stringify(_name), \
176 .mode = _mode, \
177 }, \
178 .fops = { \
179 .owner = THIS_MODULE, \
180 .open = _open, \
181 .read = seq_read, \
182 .llseek = seq_lseek, \
183 .release = single_release, \
184 }, \
185 }
186
187 /* the following attributes are general and therefore they will be directly
188 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
189 */
190 static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
191
192 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
193 &batadv_debuginfo_routing_algos,
194 NULL,
195 };
196
197 /* The following attributes are per soft interface */
198 static BATADV_DEBUGINFO(neighbors, 0444, neighbors_open);
199 static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
200 static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
201 static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
202 #ifdef CONFIG_BATMAN_ADV_BLA
203 static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
204 static BATADV_DEBUGINFO(bla_backbone_table, 0444,
205 batadv_bla_backbone_table_open);
206 #endif
207 #ifdef CONFIG_BATMAN_ADV_DAT
208 static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
209 #endif
210 static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
211 #ifdef CONFIG_BATMAN_ADV_NC
212 static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
213 #endif
214 #ifdef CONFIG_BATMAN_ADV_MCAST
215 static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
216 #endif
217
218 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
219 &batadv_debuginfo_neighbors,
220 &batadv_debuginfo_originators,
221 &batadv_debuginfo_gateways,
222 &batadv_debuginfo_transtable_global,
223 #ifdef CONFIG_BATMAN_ADV_BLA
224 &batadv_debuginfo_bla_claim_table,
225 &batadv_debuginfo_bla_backbone_table,
226 #endif
227 #ifdef CONFIG_BATMAN_ADV_DAT
228 &batadv_debuginfo_dat_cache,
229 #endif
230 &batadv_debuginfo_transtable_local,
231 #ifdef CONFIG_BATMAN_ADV_NC
232 &batadv_debuginfo_nc_nodes,
233 #endif
234 #ifdef CONFIG_BATMAN_ADV_MCAST
235 &batadv_debuginfo_mcast_flags,
236 #endif
237 NULL,
238 };
239
240 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
241 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
242 .attr = { \
243 .name = __stringify(_name), \
244 .mode = _mode, \
245 }, \
246 .fops = { \
247 .owner = THIS_MODULE, \
248 .open = _open, \
249 .read = seq_read, \
250 .llseek = seq_lseek, \
251 .release = single_release, \
252 }, \
253 }
254
255 static BATADV_HARDIF_DEBUGINFO(originators, 0444,
256 batadv_originators_hardif_open);
257
258 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
259 &batadv_hardif_debuginfo_originators,
260 NULL,
261 };
262
batadv_debugfs_init(void)263 void batadv_debugfs_init(void)
264 {
265 struct batadv_debuginfo **bat_debug;
266 struct dentry *file;
267
268 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
269 if (batadv_debugfs == ERR_PTR(-ENODEV))
270 batadv_debugfs = NULL;
271
272 if (!batadv_debugfs)
273 goto err;
274
275 for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
276 file = debugfs_create_file(((*bat_debug)->attr).name,
277 S_IFREG | ((*bat_debug)->attr).mode,
278 batadv_debugfs, NULL,
279 &(*bat_debug)->fops);
280 if (!file) {
281 pr_err("Can't add general debugfs file: %s\n",
282 ((*bat_debug)->attr).name);
283 goto err;
284 }
285 }
286
287 return;
288 err:
289 debugfs_remove_recursive(batadv_debugfs);
290 batadv_debugfs = NULL;
291 }
292
batadv_debugfs_destroy(void)293 void batadv_debugfs_destroy(void)
294 {
295 debugfs_remove_recursive(batadv_debugfs);
296 batadv_debugfs = NULL;
297 }
298
299 /**
300 * batadv_debugfs_add_hardif - creates the base directory for a hard interface
301 * in debugfs.
302 * @hard_iface: hard interface which should be added.
303 *
304 * Return: 0 on success or negative error number in case of failure
305 */
batadv_debugfs_add_hardif(struct batadv_hard_iface * hard_iface)306 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
307 {
308 struct net *net = dev_net(hard_iface->net_dev);
309 struct batadv_debuginfo **bat_debug;
310 struct dentry *file;
311
312 if (!batadv_debugfs)
313 goto out;
314
315 if (net != &init_net)
316 return 0;
317
318 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
319 batadv_debugfs);
320 if (!hard_iface->debug_dir)
321 goto out;
322
323 for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
324 file = debugfs_create_file(((*bat_debug)->attr).name,
325 S_IFREG | ((*bat_debug)->attr).mode,
326 hard_iface->debug_dir,
327 hard_iface->net_dev,
328 &(*bat_debug)->fops);
329 if (!file)
330 goto rem_attr;
331 }
332
333 return 0;
334 rem_attr:
335 debugfs_remove_recursive(hard_iface->debug_dir);
336 hard_iface->debug_dir = NULL;
337 out:
338 return -ENOMEM;
339 }
340
341 /**
342 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
343 * @hard_iface: hard interface which was renamed
344 */
batadv_debugfs_rename_hardif(struct batadv_hard_iface * hard_iface)345 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
346 {
347 const char *name = hard_iface->net_dev->name;
348 struct dentry *dir;
349 struct dentry *d;
350
351 dir = hard_iface->debug_dir;
352 if (!dir)
353 return;
354
355 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
356 if (!d)
357 pr_err("Can't rename debugfs dir to %s\n", name);
358 }
359
360 /**
361 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
362 * in debugfs.
363 * @hard_iface: hard interface which is deleted.
364 */
batadv_debugfs_del_hardif(struct batadv_hard_iface * hard_iface)365 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
366 {
367 struct net *net = dev_net(hard_iface->net_dev);
368
369 if (net != &init_net)
370 return;
371
372 if (batadv_debugfs) {
373 debugfs_remove_recursive(hard_iface->debug_dir);
374 hard_iface->debug_dir = NULL;
375 }
376 }
377
batadv_debugfs_add_meshif(struct net_device * dev)378 int batadv_debugfs_add_meshif(struct net_device *dev)
379 {
380 struct batadv_priv *bat_priv = netdev_priv(dev);
381 struct batadv_debuginfo **bat_debug;
382 struct net *net = dev_net(dev);
383 struct dentry *file;
384
385 if (!batadv_debugfs)
386 goto out;
387
388 if (net != &init_net)
389 return 0;
390
391 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
392 if (!bat_priv->debug_dir)
393 goto out;
394
395 if (batadv_socket_setup(bat_priv) < 0)
396 goto rem_attr;
397
398 if (batadv_debug_log_setup(bat_priv) < 0)
399 goto rem_attr;
400
401 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
402 file = debugfs_create_file(((*bat_debug)->attr).name,
403 S_IFREG | ((*bat_debug)->attr).mode,
404 bat_priv->debug_dir,
405 dev, &(*bat_debug)->fops);
406 if (!file) {
407 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
408 dev->name, ((*bat_debug)->attr).name);
409 goto rem_attr;
410 }
411 }
412
413 if (batadv_nc_init_debugfs(bat_priv) < 0)
414 goto rem_attr;
415
416 return 0;
417 rem_attr:
418 debugfs_remove_recursive(bat_priv->debug_dir);
419 bat_priv->debug_dir = NULL;
420 out:
421 return -ENOMEM;
422 }
423
424 /**
425 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
426 * @dev: net_device which was renamed
427 */
batadv_debugfs_rename_meshif(struct net_device * dev)428 void batadv_debugfs_rename_meshif(struct net_device *dev)
429 {
430 struct batadv_priv *bat_priv = netdev_priv(dev);
431 const char *name = dev->name;
432 struct dentry *dir;
433 struct dentry *d;
434
435 dir = bat_priv->debug_dir;
436 if (!dir)
437 return;
438
439 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
440 if (!d)
441 pr_err("Can't rename debugfs dir to %s\n", name);
442 }
443
444 /**
445 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
446 * @dev: netdev struct of the soft interface
447 */
batadv_debugfs_del_meshif(struct net_device * dev)448 void batadv_debugfs_del_meshif(struct net_device *dev)
449 {
450 struct batadv_priv *bat_priv = netdev_priv(dev);
451 struct net *net = dev_net(dev);
452
453 if (net != &init_net)
454 return;
455
456 batadv_debug_log_cleanup(bat_priv);
457
458 if (batadv_debugfs) {
459 debugfs_remove_recursive(bat_priv->debug_dir);
460 bat_priv->debug_dir = NULL;
461 }
462 }
463