vlanproc.c 8.4 KB

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