vlanproc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. * vlanproc.c VLAN 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: Ben Greear, <greearb@candelatech.com> coppied from wanproc.c
  8. * by: Gene Kozin <genek@compuserve.com>
  9. *
  10. * Copyright: (c) 1998 Ben Greear
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. * ============================================================================
  17. * Jan 20, 1998 Ben Greear Initial Version
  18. *****************************************************************************/
  19. #include <linux/module.h>
  20. #include <linux/stddef.h> /* offsetof(), etc. */
  21. #include <linux/errno.h> /* return codes */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h> /* kmalloc(), kfree() */
  24. #include <linux/mm.h>
  25. #include <linux/string.h> /* inline mem*, str* functions */
  26. #include <linux/init.h> /* __initfunc et al. */
  27. #include <asm/byteorder.h> /* htons(), etc. */
  28. #include <asm/uaccess.h> /* copy_to_user */
  29. #include <asm/io.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/fs.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_vlan.h>
  35. #include <net/net_namespace.h>
  36. #include <net/netns/generic.h>
  37. #include "vlanproc.h"
  38. #include "vlan.h"
  39. /****** Function Prototypes *************************************************/
  40. /* Methods for preparing data for reading proc entries */
  41. static int vlan_seq_show(struct seq_file *seq, void *v);
  42. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos);
  43. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  44. static void vlan_seq_stop(struct seq_file *seq, void *);
  45. static int vlandev_seq_show(struct seq_file *seq, void *v);
  46. /*
  47. * Global Data
  48. */
  49. /*
  50. * Names of the proc directory entries
  51. */
  52. static const char name_root[] = "vlan";
  53. static const char name_conf[] = "config";
  54. /*
  55. * Structures for interfacing with the /proc filesystem.
  56. * VLAN creates its own directory /proc/net/vlan with the folowing
  57. * entries:
  58. * config device status/configuration
  59. * <device> entry for each device
  60. */
  61. /*
  62. * Generic /proc/net/vlan/<file> file and inode operations
  63. */
  64. static const struct seq_operations vlan_seq_ops = {
  65. .start = vlan_seq_start,
  66. .next = vlan_seq_next,
  67. .stop = vlan_seq_stop,
  68. .show = vlan_seq_show,
  69. };
  70. static int vlan_seq_open(struct inode *inode, struct file *file)
  71. {
  72. return seq_open_net(inode, file, &vlan_seq_ops,
  73. sizeof(struct seq_net_private));
  74. }
  75. static const struct file_operations vlan_fops = {
  76. .owner = THIS_MODULE,
  77. .open = vlan_seq_open,
  78. .read = seq_read,
  79. .llseek = seq_lseek,
  80. .release = seq_release_net,
  81. };
  82. /*
  83. * /proc/net/vlan/<device> file and inode operations
  84. */
  85. static int vlandev_seq_open(struct inode *inode, struct file *file)
  86. {
  87. return single_open(file, vlandev_seq_show, PDE(inode)->data);
  88. }
  89. static const struct file_operations vlandev_fops = {
  90. .owner = THIS_MODULE,
  91. .open = vlandev_seq_open,
  92. .read = seq_read,
  93. .llseek = seq_lseek,
  94. .release = single_release,
  95. };
  96. /*
  97. * Proc filesystem derectory entries.
  98. */
  99. /* Strings */
  100. static const char *vlan_name_type_str[VLAN_NAME_TYPE_HIGHEST] = {
  101. [VLAN_NAME_TYPE_RAW_PLUS_VID] = "VLAN_NAME_TYPE_RAW_PLUS_VID",
  102. [VLAN_NAME_TYPE_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD",
  103. [VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD",
  104. [VLAN_NAME_TYPE_PLUS_VID] = "VLAN_NAME_TYPE_PLUS_VID",
  105. };
  106. /*
  107. * Interface functions
  108. */
  109. /*
  110. * Clean up /proc/net/vlan entries
  111. */
  112. void vlan_proc_cleanup(struct net *net)
  113. {
  114. struct vlan_net *vn = net_generic(net, vlan_net_id);
  115. if (vn->proc_vlan_conf)
  116. remove_proc_entry(name_conf, vn->proc_vlan_dir);
  117. if (vn->proc_vlan_dir)
  118. proc_net_remove(net, name_root);
  119. /* Dynamically added entries should be cleaned up as their vlan_device
  120. * is removed, so we should not have to take care of it here...
  121. */
  122. }
  123. /*
  124. * Create /proc/net/vlan entries
  125. */
  126. int vlan_proc_init(struct net *net)
  127. {
  128. struct vlan_net *vn = net_generic(net, vlan_net_id);
  129. vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
  130. if (!vn->proc_vlan_dir)
  131. goto err;
  132. vn->proc_vlan_conf = proc_create(name_conf, S_IFREG|S_IRUSR|S_IWUSR,
  133. vn->proc_vlan_dir, &vlan_fops);
  134. if (!vn->proc_vlan_conf)
  135. goto err;
  136. return 0;
  137. err:
  138. pr_err("%s: can't create entry in proc filesystem!\n", __func__);
  139. vlan_proc_cleanup(net);
  140. return -ENOBUFS;
  141. }
  142. /*
  143. * Add directory entry for VLAN device.
  144. */
  145. int vlan_proc_add_dev(struct net_device *vlandev)
  146. {
  147. struct vlan_dev_info *dev_info = vlan_dev_info(vlandev);
  148. struct vlan_net *vn = net_generic(dev_net(vlandev), vlan_net_id);
  149. dev_info->dent =
  150. proc_create_data(vlandev->name, S_IFREG|S_IRUSR|S_IWUSR,
  151. vn->proc_vlan_dir, &vlandev_fops, vlandev);
  152. if (!dev_info->dent)
  153. return -ENOBUFS;
  154. return 0;
  155. }
  156. /*
  157. * Delete directory entry for VLAN device.
  158. */
  159. int vlan_proc_rem_dev(struct net_device *vlandev)
  160. {
  161. struct vlan_net *vn = net_generic(dev_net(vlandev), vlan_net_id);
  162. /** NOTE: This will consume the memory pointed to by dent, it seems. */
  163. if (vlan_dev_info(vlandev)->dent) {
  164. remove_proc_entry(vlan_dev_info(vlandev)->dent->name,
  165. vn->proc_vlan_dir);
  166. vlan_dev_info(vlandev)->dent = NULL;
  167. }
  168. return 0;
  169. }
  170. /****** Proc filesystem entry points ****************************************/
  171. /*
  172. * The following few functions build the content of /proc/net/vlan/config
  173. */
  174. /* start read of /proc/net/vlan/config */
  175. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
  176. __acquires(dev_base_lock)
  177. {
  178. struct net_device *dev;
  179. struct net *net = seq_file_net(seq);
  180. loff_t i = 1;
  181. read_lock(&dev_base_lock);
  182. if (*pos == 0)
  183. return SEQ_START_TOKEN;
  184. for_each_netdev(net, dev) {
  185. if (!is_vlan_dev(dev))
  186. continue;
  187. if (i++ == *pos)
  188. return dev;
  189. }
  190. return NULL;
  191. }
  192. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  193. {
  194. struct net_device *dev;
  195. struct net *net = seq_file_net(seq);
  196. ++*pos;
  197. dev = (struct net_device *)v;
  198. if (v == SEQ_START_TOKEN)
  199. dev = net_device_entry(&net->dev_base_head);
  200. for_each_netdev_continue(net, dev) {
  201. if (!is_vlan_dev(dev))
  202. continue;
  203. return dev;
  204. }
  205. return NULL;
  206. }
  207. static void vlan_seq_stop(struct seq_file *seq, void *v)
  208. __releases(dev_base_lock)
  209. {
  210. read_unlock(&dev_base_lock);
  211. }
  212. static int vlan_seq_show(struct seq_file *seq, void *v)
  213. {
  214. struct net *net = seq_file_net(seq);
  215. struct vlan_net *vn = net_generic(net, vlan_net_id);
  216. if (v == SEQ_START_TOKEN) {
  217. const char *nmtype = NULL;
  218. seq_puts(seq, "VLAN Dev name | VLAN ID\n");
  219. if (vn->name_type < ARRAY_SIZE(vlan_name_type_str))
  220. nmtype = vlan_name_type_str[vn->name_type];
  221. seq_printf(seq, "Name-Type: %s\n",
  222. nmtype ? nmtype : "UNKNOWN");
  223. } else {
  224. const struct net_device *vlandev = v;
  225. const struct vlan_dev_info *dev_info = vlan_dev_info(vlandev);
  226. seq_printf(seq, "%-15s| %d | %s\n", vlandev->name,
  227. dev_info->vlan_id, dev_info->real_dev->name);
  228. }
  229. return 0;
  230. }
  231. static int vlandev_seq_show(struct seq_file *seq, void *offset)
  232. {
  233. struct net_device *vlandev = (struct net_device *) seq->private;
  234. const struct vlan_dev_info *dev_info = vlan_dev_info(vlandev);
  235. struct net_device_stats *stats = &vlandev->stats;
  236. static const char fmt[] = "%30s %12lu\n";
  237. int i;
  238. if (!(vlandev->priv_flags & IFF_802_1Q_VLAN))
  239. return 0;
  240. seq_printf(seq,
  241. "%s VID: %d REORDER_HDR: %i dev->priv_flags: %hx\n",
  242. vlandev->name, dev_info->vlan_id,
  243. (int)(dev_info->flags & 1), vlandev->priv_flags);
  244. seq_printf(seq, fmt, "total frames received", stats->rx_packets);
  245. seq_printf(seq, fmt, "total bytes received", stats->rx_bytes);
  246. seq_printf(seq, fmt, "Broadcast/Multicast Rcvd", stats->multicast);
  247. seq_puts(seq, "\n");
  248. seq_printf(seq, fmt, "total frames transmitted", stats->tx_packets);
  249. seq_printf(seq, fmt, "total bytes transmitted", stats->tx_bytes);
  250. seq_printf(seq, fmt, "total headroom inc",
  251. dev_info->cnt_inc_headroom_on_tx);
  252. seq_printf(seq, fmt, "total encap on xmit",
  253. dev_info->cnt_encap_on_xmit);
  254. seq_printf(seq, "Device: %s", dev_info->real_dev->name);
  255. /* now show all PRIORITY mappings relating to this VLAN */
  256. seq_printf(seq, "\nINGRESS priority mappings: "
  257. "0:%u 1:%u 2:%u 3:%u 4:%u 5:%u 6:%u 7:%u\n",
  258. dev_info->ingress_priority_map[0],
  259. dev_info->ingress_priority_map[1],
  260. dev_info->ingress_priority_map[2],
  261. dev_info->ingress_priority_map[3],
  262. dev_info->ingress_priority_map[4],
  263. dev_info->ingress_priority_map[5],
  264. dev_info->ingress_priority_map[6],
  265. dev_info->ingress_priority_map[7]);
  266. seq_printf(seq, "EGRESSS priority Mappings: ");
  267. for (i = 0; i < 16; i++) {
  268. const struct vlan_priority_tci_mapping *mp
  269. = dev_info->egress_priority_map[i];
  270. while (mp) {
  271. seq_printf(seq, "%u:%hu ",
  272. mp->priority, ((mp->vlan_qos >> 13) & 0x7));
  273. mp = mp->next;
  274. }
  275. }
  276. seq_puts(seq, "\n");
  277. return 0;
  278. }