vlanproc.c 7.5 KB

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