debugfs.c 11 KB

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