vlan.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. struct rcu_head rcu;
  32. };
  33. static inline int vlan_proto_idx(__be16 proto)
  34. {
  35. switch (proto) {
  36. case htons(ETH_P_8021Q):
  37. return VLAN_PROTO_8021Q;
  38. case htons(ETH_P_8021AD):
  39. return VLAN_PROTO_8021AD;
  40. default:
  41. WARN(1, "invalid VLAN protocol: 0x%04x\n", ntohs(proto));
  42. return -EINVAL;
  43. }
  44. }
  45. static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
  46. unsigned int pidx,
  47. u16 vlan_id)
  48. {
  49. struct net_device **array;
  50. array = vg->vlan_devices_arrays[pidx]
  51. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  52. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  53. }
  54. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  55. __be16 vlan_proto,
  56. u16 vlan_id)
  57. {
  58. int pidx = vlan_proto_idx(vlan_proto);
  59. if (pidx < 0)
  60. return NULL;
  61. return __vlan_group_get_device(vg, pidx, vlan_id);
  62. }
  63. static inline void vlan_group_set_device(struct vlan_group *vg,
  64. __be16 vlan_proto, u16 vlan_id,
  65. struct net_device *dev)
  66. {
  67. int pidx = vlan_proto_idx(vlan_proto);
  68. struct net_device **array;
  69. if (!vg || pidx < 0)
  70. return;
  71. array = vg->vlan_devices_arrays[pidx]
  72. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  73. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  74. }
  75. /* Must be invoked with rcu_read_lock or with RTNL. */
  76. static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
  77. __be16 vlan_proto, u16 vlan_id)
  78. {
  79. struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
  80. if (vlan_info)
  81. return vlan_group_get_device(&vlan_info->grp,
  82. vlan_proto, vlan_id);
  83. return NULL;
  84. }
  85. static inline netdev_features_t vlan_tnl_features(struct net_device *real_dev)
  86. {
  87. netdev_features_t ret;
  88. ret = real_dev->hw_enc_features &
  89. (NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO | NETIF_F_GSO_ENCAP_ALL);
  90. if ((ret & NETIF_F_GSO_ENCAP_ALL) && (ret & NETIF_F_CSUM_MASK))
  91. return (ret & ~NETIF_F_CSUM_MASK) | NETIF_F_HW_CSUM;
  92. return 0;
  93. }
  94. #define vlan_group_for_each_dev(grp, i, dev) \
  95. for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
  96. if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
  97. (i) % VLAN_N_VID)))
  98. int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto);
  99. void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto);
  100. /* found in vlan_dev.c */
  101. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  102. u32 skb_prio, u16 vlan_prio);
  103. int vlan_dev_set_egress_priority(const struct net_device *dev,
  104. u32 skb_prio, u16 vlan_prio);
  105. int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
  106. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
  107. int vlan_check_real_dev(struct net_device *real_dev,
  108. __be16 protocol, u16 vlan_id,
  109. struct netlink_ext_ack *extack);
  110. void vlan_setup(struct net_device *dev);
  111. int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack);
  112. void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
  113. void vlan_dev_uninit(struct net_device *dev);
  114. bool vlan_dev_inherit_address(struct net_device *dev,
  115. struct net_device *real_dev);
  116. static inline u32 vlan_get_ingress_priority(struct net_device *dev,
  117. u16 vlan_tci)
  118. {
  119. struct vlan_dev_priv *vip = vlan_dev_priv(dev);
  120. return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
  121. }
  122. #ifdef CONFIG_VLAN_8021Q_GVRP
  123. int vlan_gvrp_request_join(const struct net_device *dev);
  124. void vlan_gvrp_request_leave(const struct net_device *dev);
  125. int vlan_gvrp_init_applicant(struct net_device *dev);
  126. void vlan_gvrp_uninit_applicant(struct net_device *dev);
  127. int vlan_gvrp_init(void);
  128. void vlan_gvrp_uninit(void);
  129. #else
  130. static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
  131. static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
  132. static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
  133. static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
  134. static inline int vlan_gvrp_init(void) { return 0; }
  135. static inline void vlan_gvrp_uninit(void) {}
  136. #endif
  137. #ifdef CONFIG_VLAN_8021Q_MVRP
  138. int vlan_mvrp_request_join(const struct net_device *dev);
  139. void vlan_mvrp_request_leave(const struct net_device *dev);
  140. int vlan_mvrp_init_applicant(struct net_device *dev);
  141. void vlan_mvrp_uninit_applicant(struct net_device *dev);
  142. int vlan_mvrp_init(void);
  143. void vlan_mvrp_uninit(void);
  144. #else
  145. static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
  146. static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
  147. static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
  148. static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
  149. static inline int vlan_mvrp_init(void) { return 0; }
  150. static inline void vlan_mvrp_uninit(void) {}
  151. #endif
  152. extern const char vlan_fullname[];
  153. extern const char vlan_version[];
  154. int vlan_netlink_init(void);
  155. void vlan_netlink_fini(void);
  156. extern struct rtnl_link_ops vlan_link_ops;
  157. extern unsigned int vlan_net_id;
  158. struct proc_dir_entry;
  159. struct vlan_net {
  160. /* /proc/net/vlan */
  161. struct proc_dir_entry *proc_vlan_dir;
  162. /* /proc/net/vlan/config */
  163. struct proc_dir_entry *proc_vlan_conf;
  164. /* Determines interface naming scheme. */
  165. unsigned short name_type;
  166. };
  167. #endif /* !(__BEN_VLAN_802_1Q_INC__) */