vlanproc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. static void *vlan_seq_from_index(struct seq_file *seq, loff_t *pos)
  137. {
  138. unsigned long ifindex = *pos;
  139. struct net_device *dev;
  140. for_each_netdev_dump(seq_file_net(seq), dev, ifindex) {
  141. if (!is_vlan_dev(dev))
  142. continue;
  143. *pos = dev->ifindex;
  144. return dev;
  145. }
  146. return NULL;
  147. }
  148. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
  149. __acquires(rcu)
  150. {
  151. rcu_read_lock();
  152. if (*pos == 0)
  153. return SEQ_START_TOKEN;
  154. return vlan_seq_from_index(seq, pos);
  155. }
  156. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  157. {
  158. ++*pos;
  159. return vlan_seq_from_index(seq, pos);
  160. }
  161. static void vlan_seq_stop(struct seq_file *seq, void *v)
  162. __releases(rcu)
  163. {
  164. rcu_read_unlock();
  165. }
  166. static int vlan_seq_show(struct seq_file *seq, void *v)
  167. {
  168. struct net *net = seq_file_net(seq);
  169. struct vlan_net *vn = net_generic(net, vlan_net_id);
  170. if (v == SEQ_START_TOKEN) {
  171. const char *nmtype = NULL;
  172. seq_puts(seq, "VLAN Dev name | VLAN ID\n");
  173. if (vn->name_type < ARRAY_SIZE(vlan_name_type_str))
  174. nmtype = vlan_name_type_str[vn->name_type];
  175. seq_printf(seq, "Name-Type: %s\n",
  176. nmtype ? nmtype : "UNKNOWN");
  177. } else {
  178. const struct net_device *vlandev = v;
  179. const struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  180. seq_printf(seq, "%-15s| %d | %s\n", vlandev->name,
  181. vlan->vlan_id, vlan->real_dev->name);
  182. }
  183. return 0;
  184. }
  185. static int vlandev_seq_show(struct seq_file *seq, void *offset)
  186. {
  187. struct net_device *vlandev = (struct net_device *) seq->private;
  188. const struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  189. struct rtnl_link_stats64 temp;
  190. const struct rtnl_link_stats64 *stats;
  191. static const char fmt64[] = "%30s %12llu\n";
  192. int i;
  193. if (!is_vlan_dev(vlandev))
  194. return 0;
  195. stats = dev_get_stats(vlandev, &temp);
  196. seq_printf(seq,
  197. "%s VID: %d REORDER_HDR: %i dev->priv_flags: %x\n",
  198. vlandev->name, vlan->vlan_id,
  199. (int)(vlan->flags & 1), (u32)vlandev->priv_flags);
  200. seq_printf(seq, fmt64, "total frames received", stats->rx_packets);
  201. seq_printf(seq, fmt64, "total bytes received", stats->rx_bytes);
  202. seq_printf(seq, fmt64, "Broadcast/Multicast Rcvd", stats->multicast);
  203. seq_puts(seq, "\n");
  204. seq_printf(seq, fmt64, "total frames transmitted", stats->tx_packets);
  205. seq_printf(seq, fmt64, "total bytes transmitted", stats->tx_bytes);
  206. seq_printf(seq, "Device: %s", vlan->real_dev->name);
  207. /* now show all PRIORITY mappings relating to this VLAN */
  208. seq_printf(seq, "\nINGRESS priority mappings: "
  209. "0:%u 1:%u 2:%u 3:%u 4:%u 5:%u 6:%u 7:%u\n",
  210. vlan->ingress_priority_map[0],
  211. vlan->ingress_priority_map[1],
  212. vlan->ingress_priority_map[2],
  213. vlan->ingress_priority_map[3],
  214. vlan->ingress_priority_map[4],
  215. vlan->ingress_priority_map[5],
  216. vlan->ingress_priority_map[6],
  217. vlan->ingress_priority_map[7]);
  218. seq_printf(seq, " EGRESS priority mappings: ");
  219. for (i = 0; i < 16; i++) {
  220. const struct vlan_priority_tci_mapping *mp
  221. = vlan->egress_priority_map[i];
  222. while (mp) {
  223. seq_printf(seq, "%u:%d ",
  224. mp->priority, ((mp->vlan_qos >> 13) & 0x7));
  225. mp = mp->next;
  226. }
  227. }
  228. seq_puts(seq, "\n");
  229. return 0;
  230. }