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. {
  68. struct wan_device *wandev;
  69. loff_t l = *pos;
  70. lock_kernel();
  71. if (!l--)
  72. return SEQ_START_TOKEN;
  73. for (wandev = wanrouter_router_devlist; l-- && wandev;
  74. wandev = wandev->next)
  75. ;
  76. return wandev;
  77. }
  78. static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  79. {
  80. struct wan_device *wandev = v;
  81. (*pos)++;
  82. return (v == SEQ_START_TOKEN) ? wanrouter_router_devlist : wandev->next;
  83. }
  84. static void r_stop(struct seq_file *m, void *v)
  85. {
  86. unlock_kernel();
  87. }
  88. static int config_show(struct seq_file *m, void *v)
  89. {
  90. struct wan_device *p = v;
  91. if (v == SEQ_START_TOKEN) {
  92. seq_puts(m, "Device name | port |IRQ|DMA| mem.addr |"
  93. "mem.size|option1|option2|option3|option4\n");
  94. return 0;
  95. }
  96. if (!p->state)
  97. return 0;
  98. seq_printf(m, "%-15s|0x%-4X|%3u|%3u| 0x%-8lX |0x%-6X|%7u|%7u|%7u|%7u\n",
  99. p->name, p->ioport, p->irq, p->dma, p->maddr, p->msize,
  100. p->hw_opt[0], p->hw_opt[1], p->hw_opt[2], p->hw_opt[3]);
  101. return 0;
  102. }
  103. static int status_show(struct seq_file *m, void *v)
  104. {
  105. struct wan_device *p = v;
  106. if (v == SEQ_START_TOKEN) {
  107. seq_puts(m, "Device name |protocol|station|interface|"
  108. "clocking|baud rate| MTU |ndev|link state\n");
  109. return 0;
  110. }
  111. if (!p->state)
  112. return 0;
  113. seq_printf(m, "%-15s|%-8s| %-7s| %-9s|%-8s|%9u|%5u|%3u |",
  114. p->name,
  115. PROT_DECODE(p->config_id),
  116. p->config_id == WANCONFIG_FR ?
  117. (p->station ? "Node" : "CPE") :
  118. (p->config_id == WANCONFIG_X25 ?
  119. (p->station ? "DCE" : "DTE") :
  120. ("N/A")),
  121. p->interface ? "V.35" : "RS-232",
  122. p->clocking ? "internal" : "external",
  123. p->bps,
  124. p->mtu,
  125. p->ndev);
  126. switch (p->state) {
  127. case WAN_UNCONFIGURED:
  128. seq_printf(m, "%-12s\n", "unconfigured");
  129. break;
  130. case WAN_DISCONNECTED:
  131. seq_printf(m, "%-12s\n", "disconnected");
  132. break;
  133. case WAN_CONNECTING:
  134. seq_printf(m, "%-12s\n", "connecting");
  135. break;
  136. case WAN_CONNECTED:
  137. seq_printf(m, "%-12s\n", "connected");
  138. break;
  139. default:
  140. seq_printf(m, "%-12s\n", "invalid");
  141. break;
  142. }
  143. return 0;
  144. }
  145. static const struct seq_operations config_op = {
  146. .start = r_start,
  147. .next = r_next,
  148. .stop = r_stop,
  149. .show = config_show,
  150. };
  151. static const struct seq_operations status_op = {
  152. .start = r_start,
  153. .next = r_next,
  154. .stop = r_stop,
  155. .show = status_show,
  156. };
  157. static int config_open(struct inode *inode, struct file *file)
  158. {
  159. return seq_open(file, &config_op);
  160. }
  161. static int status_open(struct inode *inode, struct file *file)
  162. {
  163. return seq_open(file, &status_op);
  164. }
  165. static const struct file_operations config_fops = {
  166. .owner = THIS_MODULE,
  167. .open = config_open,
  168. .read = seq_read,
  169. .llseek = seq_lseek,
  170. .release = seq_release,
  171. };
  172. static const struct file_operations status_fops = {
  173. .owner = THIS_MODULE,
  174. .open = status_open,
  175. .read = seq_read,
  176. .llseek = seq_lseek,
  177. .release = seq_release,
  178. };
  179. static int wandev_show(struct seq_file *m, void *v)
  180. {
  181. struct wan_device *wandev = m->private;
  182. if (wandev->magic != ROUTER_MAGIC)
  183. return 0;
  184. if (!wandev->state) {
  185. seq_puts(m, "device is not configured!\n");
  186. return 0;
  187. }
  188. /* Update device statistics */
  189. if (wandev->update) {
  190. int err = wandev->update(wandev);
  191. if (err == -EAGAIN) {
  192. seq_puts(m, "Device is busy!\n");
  193. return 0;
  194. }
  195. if (err) {
  196. seq_puts(m, "Device is not configured!\n");
  197. return 0;
  198. }
  199. }
  200. seq_printf(m, PROC_STATS_FORMAT,
  201. "total packets received", wandev->stats.rx_packets);
  202. seq_printf(m, PROC_STATS_FORMAT,
  203. "total packets transmitted", wandev->stats.tx_packets);
  204. seq_printf(m, PROC_STATS_FORMAT,
  205. "total bytes received", wandev->stats.rx_bytes);
  206. seq_printf(m, PROC_STATS_FORMAT,
  207. "total bytes transmitted", wandev->stats.tx_bytes);
  208. seq_printf(m, PROC_STATS_FORMAT,
  209. "bad packets received", wandev->stats.rx_errors);
  210. seq_printf(m, PROC_STATS_FORMAT,
  211. "packet transmit problems", wandev->stats.tx_errors);
  212. seq_printf(m, PROC_STATS_FORMAT,
  213. "received frames dropped", wandev->stats.rx_dropped);
  214. seq_printf(m, PROC_STATS_FORMAT,
  215. "transmit frames dropped", wandev->stats.tx_dropped);
  216. seq_printf(m, PROC_STATS_FORMAT,
  217. "multicast packets received", wandev->stats.multicast);
  218. seq_printf(m, PROC_STATS_FORMAT,
  219. "transmit collisions", wandev->stats.collisions);
  220. seq_printf(m, PROC_STATS_FORMAT,
  221. "receive length errors", wandev->stats.rx_length_errors);
  222. seq_printf(m, PROC_STATS_FORMAT,
  223. "receiver overrun errors", wandev->stats.rx_over_errors);
  224. seq_printf(m, PROC_STATS_FORMAT,
  225. "CRC errors", wandev->stats.rx_crc_errors);
  226. seq_printf(m, PROC_STATS_FORMAT,
  227. "frame format errors (aborts)", wandev->stats.rx_frame_errors);
  228. seq_printf(m, PROC_STATS_FORMAT,
  229. "receiver fifo overrun", wandev->stats.rx_fifo_errors);
  230. seq_printf(m, PROC_STATS_FORMAT,
  231. "receiver missed packet", wandev->stats.rx_missed_errors);
  232. seq_printf(m, PROC_STATS_FORMAT,
  233. "aborted frames transmitted", wandev->stats.tx_aborted_errors);
  234. return 0;
  235. }
  236. static int wandev_open(struct inode *inode, struct file *file)
  237. {
  238. return single_open(file, wandev_show, PDE(inode)->data);
  239. }
  240. static const struct file_operations wandev_fops = {
  241. .owner = THIS_MODULE,
  242. .open = wandev_open,
  243. .read = seq_read,
  244. .llseek = seq_lseek,
  245. .release = single_release,
  246. .ioctl = wanrouter_ioctl,
  247. };
  248. /*
  249. * Initialize router proc interface.
  250. */
  251. int __init wanrouter_proc_init(void)
  252. {
  253. struct proc_dir_entry *p;
  254. proc_router = proc_mkdir(ROUTER_NAME, init_net.proc_net);
  255. if (!proc_router)
  256. goto fail;
  257. p = create_proc_entry("config", S_IRUGO, proc_router);
  258. if (!p)
  259. goto fail_config;
  260. p->proc_fops = &config_fops;
  261. p = create_proc_entry("status", S_IRUGO, proc_router);
  262. if (!p)
  263. goto fail_stat;
  264. p->proc_fops = &status_fops;
  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 = create_proc_entry(wandev->name, S_IRUGO, proc_router);
  290. if (!wandev->dent)
  291. return -ENOMEM;
  292. wandev->dent->proc_fops = &wandev_fops;
  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. */