blob: bb7ec1a3915ddbda397e920d9d281eddbd62527f [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
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000039static inline unsigned int vlan_proto_idx(__be16 proto)
40{
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:
47 BUG();
Patrick McHardy8da63a62013-04-21 00:09:32 +000048 return 0;
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];
Jiri Pirko536d1d42011-07-20 04:54:49 +000060 return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
61}
62
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000063static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
64 __be16 vlan_proto,
65 u16 vlan_id)
66{
67 return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
68}
69
Jiri Pirko536d1d42011-07-20 04:54:49 +000070static inline void vlan_group_set_device(struct vlan_group *vg,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000071 __be16 vlan_proto, u16 vlan_id,
Jiri Pirko536d1d42011-07-20 04:54:49 +000072 struct net_device *dev)
73{
74 struct net_device **array;
75 if (!vg)
76 return;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000077 array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
78 [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
Jiri Pirko536d1d42011-07-20 04:54:49 +000079 array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
80}
81
David Lamparter69ecca82011-07-17 08:53:12 +000082/* Must be invoked with rcu_read_lock or with RTNL. */
83static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000084 __be16 vlan_proto, u16 vlan_id)
David Lamparter69ecca82011-07-17 08:53:12 +000085{
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000086 struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
David Lamparter69ecca82011-07-17 08:53:12 +000087
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000088 if (vlan_info)
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000089 return vlan_group_get_device(&vlan_info->grp,
90 vlan_proto, vlan_id);
David Lamparter69ecca82011-07-17 08:53:12 +000091
92 return NULL;
93}
94
Davide Caratti7dad9932018-11-07 11:28:18 +010095static inline netdev_features_t vlan_tnl_features(struct net_device *real_dev)
96{
97 netdev_features_t ret;
98
99 ret = real_dev->hw_enc_features &
100 (NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO | NETIF_F_GSO_ENCAP_ALL);
101
102 if ((ret & NETIF_F_GSO_ENCAP_ALL) && (ret & NETIF_F_CSUM_MASK))
103 return (ret & ~NETIF_F_CSUM_MASK) | NETIF_F_HW_CSUM;
104 return 0;
105}
106
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000107#define vlan_group_for_each_dev(grp, i, dev) \
108 for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
109 if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
110 (i) % VLAN_N_VID)))
111
Gal Pressman9daae9b2018-03-28 17:46:54 +0300112int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto);
113void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* found in vlan_dev.c */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700116void vlan_dev_set_ingress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700117 u32 skb_prio, u16 vlan_prio);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700118int vlan_dev_set_egress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700119 u32 skb_prio, u16 vlan_prio);
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700120int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700121void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000123int vlan_check_real_dev(struct net_device *real_dev,
David Ahern33fa3822018-05-17 12:29:47 -0700124 __be16 protocol, u16 vlan_id,
125 struct netlink_ext_ack *extack);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700126void vlan_setup(struct net_device *dev);
David Ahern42ab19e2017-10-04 17:48:47 -0700127int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack);
Eric Dumazet23289a32009-10-27 07:06:36 +0000128void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
Eric Dumazet9bbd9172020-01-07 01:42:24 -0800129void vlan_dev_uninit(struct net_device *dev);
Mike Manning308453a2016-05-27 17:45:07 +0100130bool vlan_dev_inherit_address(struct net_device *dev,
131 struct net_device *real_dev);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700132
Patrick McHardy7750f402008-07-08 03:23:36 -0700133static inline u32 vlan_get_ingress_priority(struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700134 u16 vlan_tci)
Patrick McHardy7750f402008-07-08 03:23:36 -0700135{
Jiri Pirko7da82c02011-12-08 04:11:15 +0000136 struct vlan_dev_priv *vip = vlan_dev_priv(dev);
Patrick McHardy7750f402008-07-08 03:23:36 -0700137
Eric Dumazet05423b22009-10-26 18:40:35 -0700138 return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
Patrick McHardy7750f402008-07-08 03:23:36 -0700139}
140
Patrick McHardy70c03b42008-07-05 21:26:57 -0700141#ifdef CONFIG_VLAN_8021Q_GVRP
Joe Perches348662a2013-10-18 13:48:22 -0700142int vlan_gvrp_request_join(const struct net_device *dev);
143void vlan_gvrp_request_leave(const struct net_device *dev);
144int vlan_gvrp_init_applicant(struct net_device *dev);
145void vlan_gvrp_uninit_applicant(struct net_device *dev);
146int vlan_gvrp_init(void);
147void vlan_gvrp_uninit(void);
Patrick McHardy70c03b42008-07-05 21:26:57 -0700148#else
149static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
150static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
151static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
152static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
153static inline int vlan_gvrp_init(void) { return 0; }
154static inline void vlan_gvrp_uninit(void) {}
155#endif
156
David Ward86fbe9b2013-02-08 17:17:07 +0000157#ifdef CONFIG_VLAN_8021Q_MVRP
Joe Perches348662a2013-10-18 13:48:22 -0700158int vlan_mvrp_request_join(const struct net_device *dev);
159void vlan_mvrp_request_leave(const struct net_device *dev);
160int vlan_mvrp_init_applicant(struct net_device *dev);
161void vlan_mvrp_uninit_applicant(struct net_device *dev);
162int vlan_mvrp_init(void);
163void vlan_mvrp_uninit(void);
David Ward86fbe9b2013-02-08 17:17:07 +0000164#else
165static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
166static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
167static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
168static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
169static inline int vlan_mvrp_init(void) { return 0; }
170static inline void vlan_mvrp_uninit(void) {}
171#endif
172
Stephen Hemmingerb30200612008-10-28 22:12:36 -0700173extern const char vlan_fullname[];
174extern const char vlan_version[];
Joe Perches348662a2013-10-18 13:48:22 -0700175int vlan_netlink_init(void);
176void vlan_netlink_fini(void);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700177
178extern struct rtnl_link_ops vlan_link_ops;
179
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300180extern unsigned int vlan_net_id;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700181
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700182struct proc_dir_entry;
183
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700184struct vlan_net {
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700185 /* /proc/net/vlan */
186 struct proc_dir_entry *proc_vlan_dir;
187 /* /proc/net/vlan/config */
188 struct proc_dir_entry *proc_vlan_conf;
Pavel Emelyanov7a17a2f2008-04-16 00:54:39 -0700189 /* Determines interface naming scheme. */
190 unsigned short name_type;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#endif /* !(__BEN_VLAN_802_1Q_INC__) */