wanproc.c 9.0 KB

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