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