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