vlan.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef __BEN_VLAN_802_1Q_INC__
  2. #define __BEN_VLAN_802_1Q_INC__
  3. #include <linux/if_vlan.h>
  4. #include <linux/u64_stats_sync.h>
  5. #include <linux/list.h>
  6. /* if this changes, algorithm will have to be reworked because this
  7. * depends on completely exhausting the VLAN identifier space. Thus
  8. * it gives constant time look-up, but in many cases it wastes memory.
  9. */
  10. #define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
  11. #define VLAN_GROUP_ARRAY_PART_LEN (VLAN_N_VID/VLAN_GROUP_ARRAY_SPLIT_PARTS)
  12. enum vlan_protos {
  13. VLAN_PROTO_8021Q = 0,
  14. VLAN_PROTO_8021AD,
  15. VLAN_PROTO_NUM,
  16. };
  17. struct vlan_group {
  18. unsigned int nr_vlan_devs;
  19. struct hlist_node hlist; /* linked list */
  20. struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM]
  21. [VLAN_GROUP_ARRAY_SPLIT_PARTS];
  22. };
  23. struct vlan_info {
  24. struct net_device *real_dev; /* The ethernet(like) device
  25. * the vlan is attached to.
  26. */
  27. struct vlan_group grp;
  28. struct list_head vid_list;
  29. unsigned int nr_vids;
  30. struct rcu_head rcu;
  31. };
  32. static inline unsigned int vlan_proto_idx(__be16 proto)
  33. {
  34. switch (proto) {
  35. case htons(ETH_P_8021Q):
  36. return VLAN_PROTO_8021Q;
  37. case htons(ETH_P_8021AD):
  38. return VLAN_PROTO_8021AD;
  39. default:
  40. BUG();
  41. return 0;
  42. }
  43. }
  44. static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
  45. unsigned int pidx,
  46. u16 vlan_id)
  47. {
  48. struct net_device **array;
  49. array = vg->vlan_devices_arrays[pidx]
  50. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  51. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  52. }
  53. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  54. __be16 vlan_proto,
  55. u16 vlan_id)
  56. {
  57. return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
  58. }
  59. static inline void vlan_group_set_device(struct vlan_group *vg,
  60. __be16 vlan_proto, u16 vlan_id,
  61. struct net_device *dev)
  62. {
  63. struct net_device **array;
  64. if (!vg)
  65. return;
  66. array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
  67. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  68. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  69. }
  70. /* Must be invoked with rcu_read_lock or with RTNL. */
  71. static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
  72. __be16 vlan_proto, u16 vlan_id)
  73. {
  74. struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
  75. if (vlan_info)
  76. return vlan_group_get_device(&vlan_info->grp,
  77. vlan_proto, vlan_id);
  78. return NULL;
  79. }
  80. #define vlan_group_for_each_dev(grp, i, dev) \
  81. for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
  82. if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
  83. (i) % VLAN_N_VID)))
  84. /* found in vlan_dev.c */
  85. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  86. u32 skb_prio, u16 vlan_prio);
  87. int vlan_dev_set_egress_priority(const struct net_device *dev,
  88. u32 skb_prio, u16 vlan_prio);
  89. int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
  90. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
  91. int vlan_check_real_dev(struct net_device *real_dev,
  92. __be16 protocol, u16 vlan_id);
  93. void vlan_setup(struct net_device *dev);
  94. int register_vlan_dev(struct net_device *dev);
  95. void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
  96. bool vlan_dev_inherit_address(struct net_device *dev,
  97. struct net_device *real_dev);
  98. static inline u32 vlan_get_ingress_priority(struct net_device *dev,
  99. u16 vlan_tci)
  100. {
  101. struct vlan_dev_priv *vip = vlan_dev_priv(dev);
  102. return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
  103. }
  104. #ifdef CONFIG_VLAN_8021Q_GVRP
  105. int vlan_gvrp_request_join(const struct net_device *dev);
  106. void vlan_gvrp_request_leave(const struct net_device *dev);
  107. int vlan_gvrp_init_applicant(struct net_device *dev);
  108. void vlan_gvrp_uninit_applicant(struct net_device *dev);
  109. int vlan_gvrp_init(void);
  110. void vlan_gvrp_uninit(void);
  111. #else
  112. static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
  113. static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
  114. static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
  115. static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
  116. static inline int vlan_gvrp_init(void) { return 0; }
  117. static inline void vlan_gvrp_uninit(void) {}
  118. #endif
  119. #ifdef CONFIG_VLAN_8021Q_MVRP
  120. int vlan_mvrp_request_join(const struct net_device *dev);
  121. void vlan_mvrp_request_leave(const struct net_device *dev);
  122. int vlan_mvrp_init_applicant(struct net_device *dev);
  123. void vlan_mvrp_uninit_applicant(struct net_device *dev);
  124. int vlan_mvrp_init(void);
  125. void vlan_mvrp_uninit(void);
  126. #else
  127. static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
  128. static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
  129. static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
  130. static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
  131. static inline int vlan_mvrp_init(void) { return 0; }
  132. static inline void vlan_mvrp_uninit(void) {}
  133. #endif
  134. extern const char vlan_fullname[];
  135. extern const char vlan_version[];
  136. int vlan_netlink_init(void);
  137. void vlan_netlink_fini(void);
  138. extern struct rtnl_link_ops vlan_link_ops;
  139. extern int vlan_net_id;
  140. struct proc_dir_entry;
  141. struct vlan_net {
  142. /* /proc/net/vlan */
  143. struct proc_dir_entry *proc_vlan_dir;
  144. /* /proc/net/vlan/config */
  145. struct proc_dir_entry *proc_vlan_conf;
  146. /* Determines interface naming scheme. */
  147. unsigned short name_type;
  148. };
  149. #endif /* !(__BEN_VLAN_802_1Q_INC__) */