wanproc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*****************************************************************************
  2. * wanproc.c WAN Router Module. /proc filesystem interface.
  3. *
  4. * This module is completely hardware-independent and provides
  5. * access to the router using Linux /proc filesystem.
  6. *
  7. * Author: Gideon Hack
  8. *
  9. * Copyright: (c) 1995-1999 Sangoma Technologies Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. * ============================================================================
  16. * Jun 02, 1999 Gideon Hack Updates for Linux 2.2.X kernels.
  17. * Jun 29, 1997 Alan Cox Merged with 1.0.3 vendor code
  18. * Jan 29, 1997 Gene Kozin v1.0.1. Implemented /proc read routines
  19. * Jan 30, 1997 Alan Cox Hacked around for 2.1
  20. * Dec 13, 1996 Gene Kozin Initial version (based on Sangoma's WANPIPE)
  21. *****************************************************************************/
  22. #include <linux/init.h> /* __initfunc et al. */
  23. #include <linux/stddef.h> /* offsetof(), etc. */
  24. #include <linux/errno.h> /* return codes */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/wanrouter.h> /* WAN router API definitions */
  28. #include <linux/seq_file.h>
  29. #include <linux/smp_lock.h>
  30. #include <net/net_namespace.h>
  31. #include <asm/io.h>
  32. #define PROC_STATS_FORMAT "%30s: %12lu\n"
  33. /****** Defines and Macros **************************************************/
  34. #define PROT_DECODE(prot) ((prot == WANCONFIG_FR) ? " FR" :\
  35. (prot == WANCONFIG_X25) ? " X25" : \
  36. (prot == WANCONFIG_PPP) ? " PPP" : \
  37. (prot == WANCONFIG_CHDLC) ? " CHDLC": \
  38. (prot == WANCONFIG_MPPP) ? " MPPP" : \
  39. " Unknown" )
  40. /****** Function Prototypes *************************************************/
  41. #ifdef CONFIG_PROC_FS
  42. /* Miscellaneous */
  43. /*
  44. * Structures for interfacing with the /proc filesystem.
  45. * Router creates its own directory /proc/net/router with the folowing
  46. * entries:
  47. * config device configuration
  48. * status global device statistics
  49. * <device> entry for each WAN device
  50. */
  51. /*
  52. * Generic /proc/net/router/<file> file and inode operations
  53. */
  54. /*
  55. * /proc/net/router
  56. */
  57. static struct proc_dir_entry *proc_router;
  58. /* Strings */
  59. /*
  60. * Interface functions
  61. */
  62. /****** Proc filesystem entry points ****************************************/
  63. /*
  64. * Iterator
  65. */
  66. static void *r_start(struct seq_file *m, loff_t *pos)
  67. __acquires(kernel_lock)
  68. {
  69. struct wan_device *wandev;
  70. loff_t l = *pos;
  71. lock_kernel();
  72. if (!l--)
  73. return SEQ_START_TOKEN;
  74. for (wandev = wanrouter_router_devlist; l-- && wandev;
  75. wandev = wandev->next)
  76. ;
  77. return wandev;
  78. }
  79. static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  80. {
  81. struct wan_device *wandev = v;
  82. (*pos)++;
  83. return (v == SEQ_START_TOKEN) ? wanrouter_router_devlist : wandev->next;
  84. }
  85. static void r_stop(struct seq_file *m, void *v)
  86. __releases(kernel_lock)
  87. {
  88. unlock_kernel();
  89. }
  90. static int config_show(struct seq_file *m, void *v)
  91. {
  92. struct wan_device *p = v;
  93. if (v == SEQ_START_TOKEN) {
  94. seq_puts(m, "Device name | port |IRQ|DMA| mem.addr |"
  95. "mem.size|option1|option2|option3|option4\n");
  96. return 0;
  97. }
  98. if (!p->state)
  99. return 0;
  100. seq_printf(m, "%-15s|0x%-4X|%3u|%3u| 0x%-8lX |0x%-6X|%7u|%7u|%7u|%7u\n",
  101. p->name, p->ioport, p->irq, p->dma, p->maddr, p->msize,
  102. p->hw_opt[0], p->hw_opt[1], p->hw_opt[2], p->hw_opt[3]);
  103. return 0;
  104. }
  105. static int status_show(struct seq_file *m, void *v)
  106. {
  107. struct wan_device *p = v;
  108. if (v == SEQ_START_TOKEN) {
  109. seq_puts(m, "Device name |protocol|station|interface|"
  110. "clocking|baud rate| MTU |ndev|link state\n");
  111. return 0;
  112. }
  113. if (!p->state)
  114. return 0;
  115. seq_printf(m, "%-15s|%-8s| %-7s| %-9s|%-8s|%9u|%5u|%3u |",
  116. p->name,
  117. PROT_DECODE(p->config_id),
  118. p->config_id == WANCONFIG_FR ?
  119. (p->station ? "Node" : "CPE") :
  120. (p->config_id == WANCONFIG_X25 ?
  121. (p->station ? "DCE" : "DTE") :
  122. ("N/A")),
  123. p->interface ? "V.35" : "RS-232",
  124. p->clocking ? "internal" : "external",
  125. p->bps,
  126. p->mtu,
  127. p->ndev);
  128. switch (p->state) {
  129. case WAN_UNCONFIGURED:
  130. seq_printf(m, "%-12s\n", "unconfigured");
  131. break;
  132. case WAN_DISCONNECTED:
  133. seq_printf(m, "%-12s\n", "disconnected");
  134. break;
  135. case WAN_CONNECTING:
  136. seq_printf(m, "%-12s\n", "connecting");
  137. break;
  138. case WAN_CONNECTED:
  139. seq_printf(m, "%-12s\n", "connected");
  140. break;
  141. default:
  142. seq_printf(m, "%-12s\n", "invalid");
  143. break;
  144. }
  145. return 0;
  146. }
  147. static const struct seq_operations config_op = {
  148. .start = r_start,
  149. .next = r_next,
  150. .stop = r_stop,
  151. .show = config_show,
  152. };
  153. static const struct seq_operations status_op = {
  154. .start = r_start,
  155. .next = r_next,
  156. .stop = r_stop,
  157. .show = status_show,
  158. };
  159. static int config_open(struct inode *inode, struct file *file)
  160. {
  161. return seq_open(file, &config_op);
  162. }
  163. static int status_open(struct inode *inode, struct file *file)
  164. {
  165. return seq_open(file, &status_op);
  166. }
  167. static const struct file_operations config_fops = {
  168. .owner = THIS_MODULE,
  169. .open = config_open,
  170. .read = seq_read,
  171. .llseek = seq_lseek,
  172. .release = seq_release,
  173. };
  174. static const struct file_operations status_fops = {
  175. .owner = THIS_MODULE,
  176. .open = status_open,
  177. .read = seq_read,
  178. .llseek = seq_lseek,
  179. .release = seq_release,
  180. };
  181. static int wandev_show(struct seq_file *m, void *v)
  182. {
  183. struct wan_device *wandev = m->private;
  184. if (wandev->magic != ROUTER_MAGIC)
  185. return 0;
  186. if (!wandev->state) {
  187. seq_puts(m, "device is not configured!\n");
  188. return 0;
  189. }
  190. /* Update device statistics */
  191. if (wandev->update) {
  192. int err = wandev->update(wandev);
  193. if (err == -EAGAIN) {
  194. seq_puts(m, "Device is busy!\n");
  195. return 0;
  196. }
  197. if (err) {
  198. seq_puts(m, "Device is not configured!\n");
  199. return 0;
  200. }
  201. }
  202. seq_printf(m, PROC_STATS_FORMAT,
  203. "total packets received", wandev->stats.rx_packets);
  204. seq_printf(m, PROC_STATS_FORMAT,
  205. "total packets transmitted", wandev->stats.tx_packets);
  206. seq_printf(m, PROC_STATS_FORMAT,
  207. "total bytes received", wandev->stats.rx_bytes);
  208. seq_printf(m, PROC_STATS_FORMAT,
  209. "total bytes transmitted", wandev->stats.tx_bytes);
  210. seq_printf(m, PROC_STATS_FORMAT,
  211. "bad packets received", wandev->stats.rx_errors);
  212. seq_printf(m, PROC_STATS_FORMAT,
  213. "packet transmit problems", wandev->stats.tx_errors);
  214. seq_printf(m, PROC_STATS_FORMAT,
  215. "received frames dropped", wandev->stats.rx_dropped);
  216. seq_printf(m, PROC_STATS_FORMAT,
  217. "transmit frames dropped", wandev->stats.tx_dropped);
  218. seq_printf(m, PROC_STATS_FORMAT,
  219. "multicast packets received", wandev->stats.multicast);
  220. seq_printf(m, PROC_STATS_FORMAT,
  221. "transmit collisions", wandev->stats.collisions);
  222. seq_printf(m, PROC_STATS_FORMAT,
  223. "receive length errors", wandev->stats.rx_length_errors);
  224. seq_printf(m, PROC_STATS_FORMAT,
  225. "receiver overrun errors", wandev->stats.rx_over_errors);
  226. seq_printf(m, PROC_STATS_FORMAT,
  227. "CRC errors", wandev->stats.rx_crc_errors);
  228. seq_printf(m, PROC_STATS_FORMAT,
  229. "frame format errors (aborts)", wandev->stats.rx_frame_errors);
  230. seq_printf(m, PROC_STATS_FORMAT,
  231. "receiver fifo overrun", wandev->stats.rx_fifo_errors);
  232. seq_printf(m, PROC_STATS_FORMAT,
  233. "receiver missed packet", wandev->stats.rx_missed_errors);
  234. seq_printf(m, PROC_STATS_FORMAT,
  235. "aborted frames transmitted", wandev->stats.tx_aborted_errors);
  236. return 0;
  237. }
  238. static int wandev_open(struct inode *inode, struct file *file)
  239. {
  240. return single_open(file, wandev_show, PDE(inode)->data);
  241. }
  242. static const struct file_operations wandev_fops = {
  243. .owner = THIS_MODULE,
  244. .open = wandev_open,
  245. .read = seq_read,
  246. .llseek = seq_lseek,
  247. .release = single_release,
  248. .unlocked_ioctl = wanrouter_ioctl,
  249. };
  250. /*
  251. * Initialize router proc interface.
  252. */
  253. int __init wanrouter_proc_init(void)
  254. {
  255. struct proc_dir_entry *p;
  256. proc_router = proc_mkdir(ROUTER_NAME, init_net.proc_net);
  257. if (!proc_router)
  258. goto fail;
  259. p = proc_create("config", S_IRUGO, proc_router, &config_fops);
  260. if (!p)
  261. goto fail_config;
  262. p = proc_create("status", S_IRUGO, proc_router, &status_fops);
  263. if (!p)
  264. goto fail_stat;
  265. return 0;
  266. fail_stat:
  267. remove_proc_entry("config", proc_router);
  268. fail_config:
  269. remove_proc_entry(ROUTER_NAME, init_net.proc_net);
  270. fail:
  271. return -ENOMEM;
  272. }
  273. /*
  274. * Clean up router proc interface.
  275. */
  276. void wanrouter_proc_cleanup(void)
  277. {
  278. remove_proc_entry("config", proc_router);
  279. remove_proc_entry("status", proc_router);
  280. remove_proc_entry(ROUTER_NAME, init_net.proc_net);
  281. }
  282. /*
  283. * Add directory entry for WAN device.
  284. */
  285. int wanrouter_proc_add(struct wan_device* wandev)
  286. {
  287. if (wandev->magic != ROUTER_MAGIC)
  288. return -EINVAL;
  289. wandev->dent = proc_create(wandev->name, S_IRUGO,
  290. proc_router, &wandev_fops);
  291. if (!wandev->dent)
  292. return -ENOMEM;
  293. wandev->dent->data = wandev;
  294. return 0;
  295. }
  296. /*
  297. * Delete directory entry for WAN device.
  298. */
  299. int wanrouter_proc_delete(struct wan_device* wandev)
  300. {
  301. if (wandev->magic != ROUTER_MAGIC)
  302. return -EINVAL;
  303. remove_proc_entry(wandev->name, proc_router);
  304. return 0;
  305. }
  306. #else
  307. /*
  308. * No /proc - output stubs
  309. */
  310. int __init wanrouter_proc_init(void)
  311. {
  312. return 0;
  313. }
  314. void wanrouter_proc_cleanup(void)
  315. {
  316. }
  317. int wanrouter_proc_add(struct wan_device *wandev)
  318. {
  319. return 0;
  320. }
  321. int wanrouter_proc_delete(struct wan_device *wandev)
  322. {
  323. return 0;
  324. }
  325. #endif
  326. /*
  327. * End
  328. */