Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __BEN_VLAN_802_1Q_INC__
0003 #define __BEN_VLAN_802_1Q_INC__
0004 
0005 #include <linux/if_vlan.h>
0006 #include <linux/u64_stats_sync.h>
0007 #include <linux/list.h>
0008 
0009 /* if this changes, algorithm will have to be reworked because this
0010  * depends on completely exhausting the VLAN identifier space.  Thus
0011  * it gives constant time look-up, but in many cases it wastes memory.
0012  */
0013 #define VLAN_GROUP_ARRAY_SPLIT_PARTS  8
0014 #define VLAN_GROUP_ARRAY_PART_LEN     (VLAN_N_VID/VLAN_GROUP_ARRAY_SPLIT_PARTS)
0015 
0016 enum vlan_protos {
0017     VLAN_PROTO_8021Q    = 0,
0018     VLAN_PROTO_8021AD,
0019     VLAN_PROTO_NUM,
0020 };
0021 
0022 struct vlan_group {
0023     unsigned int        nr_vlan_devs;
0024     struct hlist_node   hlist;  /* linked list */
0025     struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM]
0026                            [VLAN_GROUP_ARRAY_SPLIT_PARTS];
0027 };
0028 
0029 struct vlan_info {
0030     struct net_device   *real_dev; /* The ethernet(like) device
0031                         * the vlan is attached to.
0032                         */
0033     struct vlan_group   grp;
0034     struct list_head    vid_list;
0035     unsigned int        nr_vids;
0036     struct rcu_head     rcu;
0037 };
0038 
0039 static inline int vlan_proto_idx(__be16 proto)
0040 {
0041     switch (proto) {
0042     case htons(ETH_P_8021Q):
0043         return VLAN_PROTO_8021Q;
0044     case htons(ETH_P_8021AD):
0045         return VLAN_PROTO_8021AD;
0046     default:
0047         WARN(1, "invalid VLAN protocol: 0x%04x\n", ntohs(proto));
0048         return -EINVAL;
0049     }
0050 }
0051 
0052 static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
0053                              unsigned int pidx,
0054                              u16 vlan_id)
0055 {
0056     struct net_device **array;
0057 
0058     array = vg->vlan_devices_arrays[pidx]
0059                        [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
0060 
0061     /* paired with smp_wmb() in vlan_group_prealloc_vid() */
0062     smp_rmb();
0063 
0064     return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
0065 }
0066 
0067 static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
0068                                __be16 vlan_proto,
0069                                u16 vlan_id)
0070 {
0071     int pidx = vlan_proto_idx(vlan_proto);
0072 
0073     if (pidx < 0)
0074         return NULL;
0075 
0076     return __vlan_group_get_device(vg, pidx, vlan_id);
0077 }
0078 
0079 static inline void vlan_group_set_device(struct vlan_group *vg,
0080                      __be16 vlan_proto, u16 vlan_id,
0081                      struct net_device *dev)
0082 {
0083     int pidx = vlan_proto_idx(vlan_proto);
0084     struct net_device **array;
0085 
0086     if (!vg || pidx < 0)
0087         return;
0088     array = vg->vlan_devices_arrays[pidx]
0089                        [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
0090     array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
0091 }
0092 
0093 /* Must be invoked with rcu_read_lock or with RTNL. */
0094 static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
0095                            __be16 vlan_proto, u16 vlan_id)
0096 {
0097     struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
0098 
0099     if (vlan_info)
0100         return vlan_group_get_device(&vlan_info->grp,
0101                          vlan_proto, vlan_id);
0102 
0103     return NULL;
0104 }
0105 
0106 static inline netdev_features_t vlan_tnl_features(struct net_device *real_dev)
0107 {
0108     netdev_features_t ret;
0109 
0110     ret = real_dev->hw_enc_features &
0111           (NETIF_F_CSUM_MASK | NETIF_F_GSO_SOFTWARE |
0112            NETIF_F_GSO_ENCAP_ALL);
0113 
0114     if ((ret & NETIF_F_GSO_ENCAP_ALL) && (ret & NETIF_F_CSUM_MASK))
0115         return (ret & ~NETIF_F_CSUM_MASK) | NETIF_F_HW_CSUM;
0116     return 0;
0117 }
0118 
0119 #define vlan_group_for_each_dev(grp, i, dev) \
0120     for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
0121         if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
0122                                 (i) % VLAN_N_VID)))
0123 
0124 int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto);
0125 void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto);
0126 
0127 /* found in vlan_dev.c */
0128 void vlan_dev_set_ingress_priority(const struct net_device *dev,
0129                    u32 skb_prio, u16 vlan_prio);
0130 int vlan_dev_set_egress_priority(const struct net_device *dev,
0131                  u32 skb_prio, u16 vlan_prio);
0132 void vlan_dev_free_egress_priority(const struct net_device *dev);
0133 int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
0134 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result,
0135                    size_t size);
0136 
0137 int vlan_check_real_dev(struct net_device *real_dev,
0138             __be16 protocol, u16 vlan_id,
0139             struct netlink_ext_ack *extack);
0140 void vlan_setup(struct net_device *dev);
0141 int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack);
0142 void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
0143 bool vlan_dev_inherit_address(struct net_device *dev,
0144                   struct net_device *real_dev);
0145 
0146 static inline u32 vlan_get_ingress_priority(struct net_device *dev,
0147                         u16 vlan_tci)
0148 {
0149     struct vlan_dev_priv *vip = vlan_dev_priv(dev);
0150 
0151     return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
0152 }
0153 
0154 #ifdef CONFIG_VLAN_8021Q_GVRP
0155 int vlan_gvrp_request_join(const struct net_device *dev);
0156 void vlan_gvrp_request_leave(const struct net_device *dev);
0157 int vlan_gvrp_init_applicant(struct net_device *dev);
0158 void vlan_gvrp_uninit_applicant(struct net_device *dev);
0159 int vlan_gvrp_init(void);
0160 void vlan_gvrp_uninit(void);
0161 #else
0162 static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
0163 static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
0164 static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
0165 static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
0166 static inline int vlan_gvrp_init(void) { return 0; }
0167 static inline void vlan_gvrp_uninit(void) {}
0168 #endif
0169 
0170 #ifdef CONFIG_VLAN_8021Q_MVRP
0171 int vlan_mvrp_request_join(const struct net_device *dev);
0172 void vlan_mvrp_request_leave(const struct net_device *dev);
0173 int vlan_mvrp_init_applicant(struct net_device *dev);
0174 void vlan_mvrp_uninit_applicant(struct net_device *dev);
0175 int vlan_mvrp_init(void);
0176 void vlan_mvrp_uninit(void);
0177 #else
0178 static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
0179 static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
0180 static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
0181 static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
0182 static inline int vlan_mvrp_init(void) { return 0; }
0183 static inline void vlan_mvrp_uninit(void) {}
0184 #endif
0185 
0186 extern const char vlan_fullname[];
0187 extern const char vlan_version[];
0188 int vlan_netlink_init(void);
0189 void vlan_netlink_fini(void);
0190 
0191 extern struct rtnl_link_ops vlan_link_ops;
0192 
0193 extern unsigned int vlan_net_id;
0194 
0195 struct proc_dir_entry;
0196 
0197 struct vlan_net {
0198     /* /proc/net/vlan */
0199     struct proc_dir_entry *proc_vlan_dir;
0200     /* /proc/net/vlan/config */
0201     struct proc_dir_entry *proc_vlan_conf;
0202     /* Determines interface naming scheme. */
0203     unsigned short name_type;
0204 };
0205 
0206 #endif /* !(__BEN_VLAN_802_1Q_INC__) */