debugfs.c 12 KB

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