debugfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #include "debugfs.h"
  7. #include "main.h"
  8. #include <asm/current.h>
  9. #include <linux/dcache.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/errno.h>
  12. #include <linux/export.h>
  13. #include <linux/fs.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/printk.h>
  16. #include <linux/sched.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/stat.h>
  19. #include <linux/stddef.h>
  20. #include <linux/stringify.h>
  21. #include <linux/sysfs.h>
  22. #include <net/net_namespace.h>
  23. #include "bat_algo.h"
  24. #include "bridge_loop_avoidance.h"
  25. #include "distributed-arp-table.h"
  26. #include "gateway_client.h"
  27. #include "icmp_socket.h"
  28. #include "log.h"
  29. #include "multicast.h"
  30. #include "network-coding.h"
  31. #include "originator.h"
  32. #include "translation-table.h"
  33. static struct dentry *batadv_debugfs;
  34. /**
  35. * batadv_debugfs_deprecated() - Log use of deprecated batadv debugfs access
  36. * @file: file which was accessed
  37. * @alt: explanation what can be used as alternative
  38. */
  39. void batadv_debugfs_deprecated(struct file *file, const char *alt)
  40. {
  41. struct dentry *dentry = file_dentry(file);
  42. const char *name = dentry->d_name.name;
  43. pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of debugfs file \"%s\".\n%s",
  44. current->comm, task_pid_nr(current), name, alt);
  45. }
  46. static int batadv_algorithms_open(struct inode *inode, struct file *file)
  47. {
  48. batadv_debugfs_deprecated(file,
  49. "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
  50. return single_open(file, batadv_algo_seq_print_text, NULL);
  51. }
  52. static int neighbors_open(struct inode *inode, struct file *file)
  53. {
  54. struct net_device *net_dev = (struct net_device *)inode->i_private;
  55. batadv_debugfs_deprecated(file,
  56. "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
  57. return single_open(file, batadv_hardif_neigh_seq_print_text, net_dev);
  58. }
  59. static int batadv_originators_open(struct inode *inode, struct file *file)
  60. {
  61. struct net_device *net_dev = (struct net_device *)inode->i_private;
  62. batadv_debugfs_deprecated(file,
  63. "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
  64. return single_open(file, batadv_orig_seq_print_text, net_dev);
  65. }
  66. /**
  67. * batadv_originators_hardif_open() - handles debugfs output for the originator
  68. * table of an hard interface
  69. * @inode: inode pointer to debugfs file
  70. * @file: pointer to the seq_file
  71. *
  72. * Return: 0 on success or negative error number in case of failure
  73. */
  74. static int batadv_originators_hardif_open(struct inode *inode,
  75. struct file *file)
  76. {
  77. struct net_device *net_dev = (struct net_device *)inode->i_private;
  78. batadv_debugfs_deprecated(file,
  79. "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
  80. return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
  81. }
  82. static int batadv_gateways_open(struct inode *inode, struct file *file)
  83. {
  84. struct net_device *net_dev = (struct net_device *)inode->i_private;
  85. batadv_debugfs_deprecated(file,
  86. "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
  87. return single_open(file, batadv_gw_client_seq_print_text, net_dev);
  88. }
  89. static int batadv_transtable_global_open(struct inode *inode, struct file *file)
  90. {
  91. struct net_device *net_dev = (struct net_device *)inode->i_private;
  92. batadv_debugfs_deprecated(file,
  93. "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
  94. return single_open(file, batadv_tt_global_seq_print_text, net_dev);
  95. }
  96. #ifdef CONFIG_BATMAN_ADV_BLA
  97. static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
  98. {
  99. struct net_device *net_dev = (struct net_device *)inode->i_private;
  100. batadv_debugfs_deprecated(file,
  101. "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
  102. return single_open(file, batadv_bla_claim_table_seq_print_text,
  103. net_dev);
  104. }
  105. static int batadv_bla_backbone_table_open(struct inode *inode,
  106. struct file *file)
  107. {
  108. struct net_device *net_dev = (struct net_device *)inode->i_private;
  109. batadv_debugfs_deprecated(file,
  110. "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
  111. return single_open(file, batadv_bla_backbone_table_seq_print_text,
  112. net_dev);
  113. }
  114. #endif
  115. #ifdef CONFIG_BATMAN_ADV_DAT
  116. /**
  117. * batadv_dat_cache_open() - Prepare file handler for reads from dat_cache
  118. * @inode: inode which was opened
  119. * @file: file handle to be initialized
  120. *
  121. * Return: 0 on success or negative error number in case of failure
  122. */
  123. static int batadv_dat_cache_open(struct inode *inode, struct file *file)
  124. {
  125. struct net_device *net_dev = (struct net_device *)inode->i_private;
  126. batadv_debugfs_deprecated(file,
  127. "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
  128. return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
  129. }
  130. #endif
  131. static int batadv_transtable_local_open(struct inode *inode, struct file *file)
  132. {
  133. struct net_device *net_dev = (struct net_device *)inode->i_private;
  134. batadv_debugfs_deprecated(file,
  135. "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
  136. return single_open(file, batadv_tt_local_seq_print_text, net_dev);
  137. }
  138. struct batadv_debuginfo {
  139. struct attribute attr;
  140. const struct file_operations fops;
  141. };
  142. #ifdef CONFIG_BATMAN_ADV_NC
  143. static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
  144. {
  145. struct net_device *net_dev = (struct net_device *)inode->i_private;
  146. batadv_debugfs_deprecated(file, "");
  147. return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
  148. }
  149. #endif
  150. #ifdef CONFIG_BATMAN_ADV_MCAST
  151. /**
  152. * batadv_mcast_flags_open() - prepare file handler for reads from mcast_flags
  153. * @inode: inode which was opened
  154. * @file: file handle to be initialized
  155. *
  156. * Return: 0 on success or negative error number in case of failure
  157. */
  158. static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
  159. {
  160. struct net_device *net_dev = (struct net_device *)inode->i_private;
  161. batadv_debugfs_deprecated(file,
  162. "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
  163. return single_open(file, batadv_mcast_flags_seq_print_text, net_dev);
  164. }
  165. #endif
  166. #define BATADV_DEBUGINFO(_name, _mode, _open) \
  167. struct batadv_debuginfo batadv_debuginfo_##_name = { \
  168. .attr = { \
  169. .name = __stringify(_name), \
  170. .mode = _mode, \
  171. }, \
  172. .fops = { \
  173. .owner = THIS_MODULE, \
  174. .open = _open, \
  175. .read = seq_read, \
  176. .llseek = seq_lseek, \
  177. .release = single_release, \
  178. }, \
  179. }
  180. /* the following attributes are general and therefore they will be directly
  181. * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
  182. */
  183. static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
  184. static struct batadv_debuginfo *batadv_general_debuginfos[] = {
  185. &batadv_debuginfo_routing_algos,
  186. NULL,
  187. };
  188. /* The following attributes are per soft interface */
  189. static BATADV_DEBUGINFO(neighbors, 0444, neighbors_open);
  190. static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
  191. static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
  192. static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
  193. #ifdef CONFIG_BATMAN_ADV_BLA
  194. static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
  195. static BATADV_DEBUGINFO(bla_backbone_table, 0444,
  196. batadv_bla_backbone_table_open);
  197. #endif
  198. #ifdef CONFIG_BATMAN_ADV_DAT
  199. static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
  200. #endif
  201. static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
  202. #ifdef CONFIG_BATMAN_ADV_NC
  203. static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
  204. #endif
  205. #ifdef CONFIG_BATMAN_ADV_MCAST
  206. static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
  207. #endif
  208. static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
  209. &batadv_debuginfo_neighbors,
  210. &batadv_debuginfo_originators,
  211. &batadv_debuginfo_gateways,
  212. &batadv_debuginfo_transtable_global,
  213. #ifdef CONFIG_BATMAN_ADV_BLA
  214. &batadv_debuginfo_bla_claim_table,
  215. &batadv_debuginfo_bla_backbone_table,
  216. #endif
  217. #ifdef CONFIG_BATMAN_ADV_DAT
  218. &batadv_debuginfo_dat_cache,
  219. #endif
  220. &batadv_debuginfo_transtable_local,
  221. #ifdef CONFIG_BATMAN_ADV_NC
  222. &batadv_debuginfo_nc_nodes,
  223. #endif
  224. #ifdef CONFIG_BATMAN_ADV_MCAST
  225. &batadv_debuginfo_mcast_flags,
  226. #endif
  227. NULL,
  228. };
  229. #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
  230. struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
  231. .attr = { \
  232. .name = __stringify(_name), \
  233. .mode = _mode, \
  234. }, \
  235. .fops = { \
  236. .owner = THIS_MODULE, \
  237. .open = _open, \
  238. .read = seq_read, \
  239. .llseek = seq_lseek, \
  240. .release = single_release, \
  241. }, \
  242. }
  243. static BATADV_HARDIF_DEBUGINFO(originators, 0444,
  244. batadv_originators_hardif_open);
  245. static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
  246. &batadv_hardif_debuginfo_originators,
  247. NULL,
  248. };
  249. /**
  250. * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
  251. */
  252. void batadv_debugfs_init(void)
  253. {
  254. struct batadv_debuginfo **bat_debug;
  255. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  256. for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug)
  257. debugfs_create_file(((*bat_debug)->attr).name,
  258. S_IFREG | ((*bat_debug)->attr).mode,
  259. batadv_debugfs, NULL, &(*bat_debug)->fops);
  260. }
  261. /**
  262. * batadv_debugfs_destroy() - Remove all debugfs entries
  263. */
  264. void batadv_debugfs_destroy(void)
  265. {
  266. debugfs_remove_recursive(batadv_debugfs);
  267. batadv_debugfs = NULL;
  268. }
  269. /**
  270. * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
  271. * in debugfs.
  272. * @hard_iface: hard interface which should be added.
  273. */
  274. void batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
  275. {
  276. struct net *net = dev_net(hard_iface->net_dev);
  277. struct batadv_debuginfo **bat_debug;
  278. if (net != &init_net)
  279. return;
  280. hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
  281. batadv_debugfs);
  282. for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug)
  283. debugfs_create_file(((*bat_debug)->attr).name,
  284. S_IFREG | ((*bat_debug)->attr).mode,
  285. hard_iface->debug_dir, hard_iface->net_dev,
  286. &(*bat_debug)->fops);
  287. }
  288. /**
  289. * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
  290. * @hard_iface: hard interface which was renamed
  291. */
  292. void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
  293. {
  294. const char *name = hard_iface->net_dev->name;
  295. struct dentry *dir;
  296. dir = hard_iface->debug_dir;
  297. if (!dir)
  298. return;
  299. debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
  300. }
  301. /**
  302. * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
  303. * in debugfs.
  304. * @hard_iface: hard interface which is deleted.
  305. */
  306. void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
  307. {
  308. struct net *net = dev_net(hard_iface->net_dev);
  309. if (net != &init_net)
  310. return;
  311. if (batadv_debugfs) {
  312. debugfs_remove_recursive(hard_iface->debug_dir);
  313. hard_iface->debug_dir = NULL;
  314. }
  315. }
  316. /**
  317. * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
  318. * @dev: netdev struct of the soft interface
  319. *
  320. * Return: 0 on success or negative error number in case of failure
  321. */
  322. int batadv_debugfs_add_meshif(struct net_device *dev)
  323. {
  324. struct batadv_priv *bat_priv = netdev_priv(dev);
  325. struct batadv_debuginfo **bat_debug;
  326. struct net *net = dev_net(dev);
  327. if (net != &init_net)
  328. return 0;
  329. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  330. batadv_socket_setup(bat_priv);
  331. if (batadv_debug_log_setup(bat_priv) < 0)
  332. goto rem_attr;
  333. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug)
  334. debugfs_create_file(((*bat_debug)->attr).name,
  335. S_IFREG | ((*bat_debug)->attr).mode,
  336. bat_priv->debug_dir, dev,
  337. &(*bat_debug)->fops);
  338. batadv_nc_init_debugfs(bat_priv);
  339. return 0;
  340. rem_attr:
  341. debugfs_remove_recursive(bat_priv->debug_dir);
  342. bat_priv->debug_dir = NULL;
  343. return -ENOMEM;
  344. }
  345. /**
  346. * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
  347. * @dev: net_device which was renamed
  348. */
  349. void batadv_debugfs_rename_meshif(struct net_device *dev)
  350. {
  351. struct batadv_priv *bat_priv = netdev_priv(dev);
  352. const char *name = dev->name;
  353. struct dentry *dir;
  354. dir = bat_priv->debug_dir;
  355. if (!dir)
  356. return;
  357. debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
  358. }
  359. /**
  360. * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
  361. * @dev: netdev struct of the soft interface
  362. */
  363. void batadv_debugfs_del_meshif(struct net_device *dev)
  364. {
  365. struct batadv_priv *bat_priv = netdev_priv(dev);
  366. struct net *net = dev_net(dev);
  367. if (net != &init_net)
  368. return;
  369. batadv_debug_log_cleanup(bat_priv);
  370. if (batadv_debugfs) {
  371. debugfs_remove_recursive(bat_priv->debug_dir);
  372. bat_priv->debug_dir = NULL;
  373. }
  374. }