wanproc.c 9.1 KB

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