1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (C) 2010-2020 B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner 5 */ 6 7#include "debugfs.h" 8#include "main.h" 9 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> 15#include <linux/fs.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> 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 39/** 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 53static int batadv_algorithms_open(struct inode *inode, struct file *file) 54{ 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); 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 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); 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 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); 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 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); 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 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); 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 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); 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 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, 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 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, 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 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); 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 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); 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 175 batadv_debugfs_deprecated(file, ""); 176 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 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); 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; 295 296 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL); 297 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); 302} 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. 317 */ 318void batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface) 319{ 320 struct net *net = dev_net(hard_iface->net_dev); 321 struct batadv_debuginfo **bat_debug; 322 323 if (net != &init_net) 324 return; 325 326 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name, 327 batadv_debugfs); 328 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); 334} 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; 344 345 dir = hard_iface->debug_dir; 346 if (!dir) 347 return; 348 349 debugfs_rename(dir->d_parent, dir, dir->d_parent, name); 350} 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); 381 382 if (net != &init_net) 383 return 0; 384 385 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs); 386 387 batadv_socket_setup(bat_priv); 388 389 if (batadv_debug_log_setup(bat_priv) < 0) 390 goto rem_attr; 391 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); 397 398 batadv_nc_init_debugfs(bat_priv); 399 400 return 0; 401rem_attr: 402 debugfs_remove_recursive(bat_priv->debug_dir); 403 bat_priv->debug_dir = NULL; 404 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; 416 417 dir = bat_priv->debug_dir; 418 if (!dir) 419 return; 420 421 debugfs_rename(dir->d_parent, dir, dir->d_parent, name); 422} 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} 443