vlan.h 6.3 KB

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