blob: 1a705a4ef7fa8ead466b0f12fa88b5f2d9b0081f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __BEN_VLAN_802_1Q_INC__
3#define __BEN_VLAN_802_1Q_INC__
4
5#include <linux/if_vlan.h>
Eric Dumazet9618e2f2010-06-24 00:55:06 +00006#include <linux/u64_stats_sync.h>
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +00007#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +00009/* 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
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000016enum vlan_protos {
17 VLAN_PROTO_8021Q = 0,
Patrick McHardy8ad227f2013-04-19 02:04:31 +000018 VLAN_PROTO_8021AD,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000019 VLAN_PROTO_NUM,
20};
21
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000022struct vlan_group {
23 unsigned int nr_vlan_devs;
24 struct hlist_node hlist; /* linked list */
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000025 struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM]
26 [VLAN_GROUP_ARRAY_SPLIT_PARTS];
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000027};
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
Florian Fainellid0186842020-09-24 17:27:44 -070039static inline int vlan_proto_idx(__be16 proto)
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000040{
41 switch (proto) {
Joe Perchesf0e78822014-03-12 10:04:15 -070042 case htons(ETH_P_8021Q):
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000043 return VLAN_PROTO_8021Q;
Joe Perchesf0e78822014-03-12 10:04:15 -070044 case htons(ETH_P_8021AD):
Patrick McHardy8ad227f2013-04-19 02:04:31 +000045 return VLAN_PROTO_8021AD;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000046 default:
Florian Fainellid0186842020-09-24 17:27:44 -070047 WARN(1, "invalid VLAN protocol: 0x%04x\n", ntohs(proto));
48 return -EINVAL;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000049 }
50}
51
52static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
53 unsigned int pidx,
54 u16 vlan_id)
Jiri Pirko536d1d42011-07-20 04:54:49 +000055{
56 struct net_device **array;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000057
58 array = vg->vlan_devices_arrays[pidx]
59 [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
Di Zhuc1102e9d2021-04-19 21:56:41 +080060
61 /* paired with smp_wmb() in vlan_group_prealloc_vid() */
62 smp_rmb();
63
Jiri Pirko536d1d42011-07-20 04:54:49 +000064 return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
65}
66
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000067static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
68 __be16 vlan_proto,
69 u16 vlan_id)
70{
Florian Fainellid0186842020-09-24 17:27:44 -070071 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);
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000077}
78
Jiri Pirko536d1d42011-07-20 04:54:49 +000079static inline void vlan_group_set_device(struct vlan_group *vg,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000080 __be16 vlan_proto, u16 vlan_id,
Jiri Pirko536d1d42011-07-20 04:54:49 +000081 struct net_device *dev)
82{
Florian Fainellid0186842020-09-24 17:27:44 -070083 int pidx = vlan_proto_idx(vlan_proto);
Jiri Pirko536d1d42011-07-20 04:54:49 +000084 struct net_device **array;
Florian Fainellid0186842020-09-24 17:27:44 -070085
86 if (!vg || pidx < 0)
Jiri Pirko536d1d42011-07-20 04:54:49 +000087 return;
Florian Fainellid0186842020-09-24 17:27:44 -070088 array = vg->vlan_devices_arrays[pidx]
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000089 [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
Jiri Pirko536d1d42011-07-20 04:54:49 +000090 array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
91}
92
David Lamparter69ecca82011-07-17 08:53:12 +000093/* Must be invoked with rcu_read_lock or with RTNL. */
94static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000095 __be16 vlan_proto, u16 vlan_id)
David Lamparter69ecca82011-07-17 08:53:12 +000096{
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000097 struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
David Lamparter69ecca82011-07-17 08:53:12 +000098
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000099 if (vlan_info)
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000100 return vlan_group_get_device(&vlan_info->grp,
101 vlan_proto, vlan_id);
David Lamparter69ecca82011-07-17 08:53:12 +0000102
103 return NULL;
104}
105
Davide Caratti7dad9932018-11-07 11:28:18 +0100106static 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 &
Jakub Kicinski9d72b8d2021-06-17 21:55:56 -0700111 (NETIF_F_CSUM_MASK | NETIF_F_GSO_SOFTWARE |
112 NETIF_F_GSO_ENCAP_ALL);
Davide Caratti7dad9932018-11-07 11:28:18 +0100113
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
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000119#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
Gal Pressman9daae9b2018-03-28 17:46:54 +0300124int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto);
125void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* found in vlan_dev.c */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700128void vlan_dev_set_ingress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700129 u32 skb_prio, u16 vlan_prio);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700130int vlan_dev_set_egress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700131 u32 skb_prio, u16 vlan_prio);
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700132int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
Kees Cook9c153d32021-06-02 13:27:41 -0700133void vlan_dev_get_realdev_name(const struct net_device *dev, char *result,
134 size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000136int vlan_check_real_dev(struct net_device *real_dev,
David Ahern33fa3822018-05-17 12:29:47 -0700137 __be16 protocol, u16 vlan_id,
138 struct netlink_ext_ack *extack);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700139void vlan_setup(struct net_device *dev);
David Ahern42ab19e2017-10-04 17:48:47 -0700140int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack);
Eric Dumazet23289a32009-10-27 07:06:36 +0000141void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
Eric Dumazet9bbd9172020-01-07 01:42:24 -0800142void vlan_dev_uninit(struct net_device *dev);
Mike Manning308453a2016-05-27 17:45:07 +0100143bool vlan_dev_inherit_address(struct net_device *dev,
144 struct net_device *real_dev);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700145
Patrick McHardy7750f402008-07-08 03:23:36 -0700146static inline u32 vlan_get_ingress_priority(struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700147 u16 vlan_tci)
Patrick McHardy7750f402008-07-08 03:23:36 -0700148{
Jiri Pirko7da82c02011-12-08 04:11:15 +0000149 struct vlan_dev_priv *vip = vlan_dev_priv(dev);
Patrick McHardy7750f402008-07-08 03:23:36 -0700150
Eric Dumazet05423b22009-10-26 18:40:35 -0700151 return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
Patrick McHardy7750f402008-07-08 03:23:36 -0700152}
153
Patrick McHardy70c03b42008-07-05 21:26:57 -0700154#ifdef CONFIG_VLAN_8021Q_GVRP
Joe Perches348662a2013-10-18 13:48:22 -0700155int 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);
Patrick McHardy70c03b42008-07-05 21:26:57 -0700161#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
David Ward86fbe9b2013-02-08 17:17:07 +0000170#ifdef CONFIG_VLAN_8021Q_MVRP
Joe Perches348662a2013-10-18 13:48:22 -0700171int 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);
David Ward86fbe9b2013-02-08 17:17:07 +0000177#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
Stephen Hemmingerb30200612008-10-28 22:12:36 -0700186extern const char vlan_fullname[];
187extern const char vlan_version[];
Joe Perches348662a2013-10-18 13:48:22 -0700188int vlan_netlink_init(void);
189void vlan_netlink_fini(void);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700190
191extern struct rtnl_link_ops vlan_link_ops;
192
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300193extern unsigned int vlan_net_id;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700194
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700195struct proc_dir_entry;
196
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700197struct vlan_net {
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700198 /* /proc/net/vlan */
199 struct proc_dir_entry *proc_vlan_dir;
200 /* /proc/net/vlan/config */
201 struct proc_dir_entry *proc_vlan_conf;
Pavel Emelyanov7a17a2f2008-04-16 00:54:39 -0700202 /* Determines interface naming scheme. */
203 unsigned short name_type;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700204};
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#endif /* !(__BEN_VLAN_802_1Q_INC__) */