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