debugfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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, 0444, 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, 0444, neighbors_open);
  165. static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
  166. static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
  167. static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
  168. #ifdef CONFIG_BATMAN_ADV_BLA
  169. static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
  170. static BATADV_DEBUGINFO(bla_backbone_table, 0444,
  171. batadv_bla_backbone_table_open);
  172. #endif
  173. #ifdef CONFIG_BATMAN_ADV_DAT
  174. static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
  175. #endif
  176. static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
  177. #ifdef CONFIG_BATMAN_ADV_NC
  178. static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
  179. #endif
  180. #ifdef CONFIG_BATMAN_ADV_MCAST
  181. static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
  182. #endif
  183. static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
  184. &batadv_debuginfo_neighbors,
  185. &batadv_debuginfo_originators,
  186. &batadv_debuginfo_gateways,
  187. &batadv_debuginfo_transtable_global,
  188. #ifdef CONFIG_BATMAN_ADV_BLA
  189. &batadv_debuginfo_bla_claim_table,
  190. &batadv_debuginfo_bla_backbone_table,
  191. #endif
  192. #ifdef CONFIG_BATMAN_ADV_DAT
  193. &batadv_debuginfo_dat_cache,
  194. #endif
  195. &batadv_debuginfo_transtable_local,
  196. #ifdef CONFIG_BATMAN_ADV_NC
  197. &batadv_debuginfo_nc_nodes,
  198. #endif
  199. #ifdef CONFIG_BATMAN_ADV_MCAST
  200. &batadv_debuginfo_mcast_flags,
  201. #endif
  202. NULL,
  203. };
  204. #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
  205. struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
  206. .attr = { \
  207. .name = __stringify(_name), \
  208. .mode = _mode, \
  209. }, \
  210. .fops = { \
  211. .owner = THIS_MODULE, \
  212. .open = _open, \
  213. .read = seq_read, \
  214. .llseek = seq_lseek, \
  215. .release = single_release, \
  216. }, \
  217. }
  218. static BATADV_HARDIF_DEBUGINFO(originators, 0444,
  219. batadv_originators_hardif_open);
  220. static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
  221. &batadv_hardif_debuginfo_originators,
  222. NULL,
  223. };
  224. void batadv_debugfs_init(void)
  225. {
  226. struct batadv_debuginfo **bat_debug;
  227. struct dentry *file;
  228. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  229. if (batadv_debugfs == ERR_PTR(-ENODEV))
  230. batadv_debugfs = NULL;
  231. if (!batadv_debugfs)
  232. goto err;
  233. for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
  234. file = debugfs_create_file(((*bat_debug)->attr).name,
  235. S_IFREG | ((*bat_debug)->attr).mode,
  236. batadv_debugfs, NULL,
  237. &(*bat_debug)->fops);
  238. if (!file) {
  239. pr_err("Can't add general debugfs file: %s\n",
  240. ((*bat_debug)->attr).name);
  241. goto err;
  242. }
  243. }
  244. return;
  245. err:
  246. debugfs_remove_recursive(batadv_debugfs);
  247. batadv_debugfs = NULL;
  248. }
  249. void batadv_debugfs_destroy(void)
  250. {
  251. debugfs_remove_recursive(batadv_debugfs);
  252. batadv_debugfs = NULL;
  253. }
  254. /**
  255. * batadv_debugfs_add_hardif - creates the base directory for a hard interface
  256. * in debugfs.
  257. * @hard_iface: hard interface which should be added.
  258. *
  259. * Return: 0 on success or negative error number in case of failure
  260. */
  261. int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
  262. {
  263. struct net *net = dev_net(hard_iface->net_dev);
  264. struct batadv_debuginfo **bat_debug;
  265. struct dentry *file;
  266. if (!batadv_debugfs)
  267. goto out;
  268. if (net != &init_net)
  269. return 0;
  270. hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
  271. batadv_debugfs);
  272. if (!hard_iface->debug_dir)
  273. goto out;
  274. for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
  275. file = debugfs_create_file(((*bat_debug)->attr).name,
  276. S_IFREG | ((*bat_debug)->attr).mode,
  277. hard_iface->debug_dir,
  278. hard_iface->net_dev,
  279. &(*bat_debug)->fops);
  280. if (!file)
  281. goto rem_attr;
  282. }
  283. return 0;
  284. rem_attr:
  285. debugfs_remove_recursive(hard_iface->debug_dir);
  286. hard_iface->debug_dir = NULL;
  287. out:
  288. return -ENOMEM;
  289. }
  290. /**
  291. * batadv_debugfs_del_hardif - delete the base directory for a hard interface
  292. * in debugfs.
  293. * @hard_iface: hard interface which is deleted.
  294. */
  295. void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
  296. {
  297. struct net *net = dev_net(hard_iface->net_dev);
  298. if (net != &init_net)
  299. return;
  300. if (batadv_debugfs) {
  301. debugfs_remove_recursive(hard_iface->debug_dir);
  302. hard_iface->debug_dir = NULL;
  303. }
  304. }
  305. int batadv_debugfs_add_meshif(struct net_device *dev)
  306. {
  307. struct batadv_priv *bat_priv = netdev_priv(dev);
  308. struct batadv_debuginfo **bat_debug;
  309. struct net *net = dev_net(dev);
  310. struct dentry *file;
  311. if (!batadv_debugfs)
  312. goto out;
  313. if (net != &init_net)
  314. return 0;
  315. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  316. if (!bat_priv->debug_dir)
  317. goto out;
  318. if (batadv_socket_setup(bat_priv) < 0)
  319. goto rem_attr;
  320. if (batadv_debug_log_setup(bat_priv) < 0)
  321. goto rem_attr;
  322. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
  323. file = debugfs_create_file(((*bat_debug)->attr).name,
  324. S_IFREG | ((*bat_debug)->attr).mode,
  325. bat_priv->debug_dir,
  326. dev, &(*bat_debug)->fops);
  327. if (!file) {
  328. batadv_err(dev, "Can't add debugfs file: %s/%s\n",
  329. dev->name, ((*bat_debug)->attr).name);
  330. goto rem_attr;
  331. }
  332. }
  333. if (batadv_nc_init_debugfs(bat_priv) < 0)
  334. goto rem_attr;
  335. return 0;
  336. rem_attr:
  337. debugfs_remove_recursive(bat_priv->debug_dir);
  338. bat_priv->debug_dir = NULL;
  339. out:
  340. return -ENOMEM;
  341. }
  342. void batadv_debugfs_del_meshif(struct net_device *dev)
  343. {
  344. struct batadv_priv *bat_priv = netdev_priv(dev);
  345. struct net *net = dev_net(dev);
  346. if (net != &init_net)
  347. return;
  348. batadv_debug_log_cleanup(bat_priv);
  349. if (batadv_debugfs) {
  350. debugfs_remove_recursive(bat_priv->debug_dir);
  351. bat_priv->debug_dir = NULL;
  352. }
  353. }