blob: 44df1c3df02d3ad0b880a6946a321874516e3967 [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
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000095#define vlan_group_for_each_dev(grp, i, dev) \
96 for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
97 if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
98 (i) % VLAN_N_VID)))
99
Gal Pressman9daae9b2018-03-28 17:46:54 +0300100int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto);
101void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* found in vlan_dev.c */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700104void vlan_dev_set_ingress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700105 u32 skb_prio, u16 vlan_prio);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700106int vlan_dev_set_egress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700107 u32 skb_prio, u16 vlan_prio);
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700108int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700109void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000111int vlan_check_real_dev(struct net_device *real_dev,
David Ahern33fa3822018-05-17 12:29:47 -0700112 __be16 protocol, u16 vlan_id,
113 struct netlink_ext_ack *extack);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700114void vlan_setup(struct net_device *dev);
David Ahern42ab19e2017-10-04 17:48:47 -0700115int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack);
Eric Dumazet23289a32009-10-27 07:06:36 +0000116void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
Mike Manning308453a2016-05-27 17:45:07 +0100117bool vlan_dev_inherit_address(struct net_device *dev,
118 struct net_device *real_dev);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700119
Patrick McHardy7750f402008-07-08 03:23:36 -0700120static inline u32 vlan_get_ingress_priority(struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700121 u16 vlan_tci)
Patrick McHardy7750f402008-07-08 03:23:36 -0700122{
Jiri Pirko7da82c02011-12-08 04:11:15 +0000123 struct vlan_dev_priv *vip = vlan_dev_priv(dev);
Patrick McHardy7750f402008-07-08 03:23:36 -0700124
Eric Dumazet05423b22009-10-26 18:40:35 -0700125 return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
Patrick McHardy7750f402008-07-08 03:23:36 -0700126}
127
Patrick McHardy70c03b42008-07-05 21:26:57 -0700128#ifdef CONFIG_VLAN_8021Q_GVRP
Joe Perches348662a2013-10-18 13:48:22 -0700129int vlan_gvrp_request_join(const struct net_device *dev);
130void vlan_gvrp_request_leave(const struct net_device *dev);
131int vlan_gvrp_init_applicant(struct net_device *dev);
132void vlan_gvrp_uninit_applicant(struct net_device *dev);
133int vlan_gvrp_init(void);
134void vlan_gvrp_uninit(void);
Patrick McHardy70c03b42008-07-05 21:26:57 -0700135#else
136static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
137static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
138static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
139static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
140static inline int vlan_gvrp_init(void) { return 0; }
141static inline void vlan_gvrp_uninit(void) {}
142#endif
143
David Ward86fbe9b2013-02-08 17:17:07 +0000144#ifdef CONFIG_VLAN_8021Q_MVRP
Joe Perches348662a2013-10-18 13:48:22 -0700145int vlan_mvrp_request_join(const struct net_device *dev);
146void vlan_mvrp_request_leave(const struct net_device *dev);
147int vlan_mvrp_init_applicant(struct net_device *dev);
148void vlan_mvrp_uninit_applicant(struct net_device *dev);
149int vlan_mvrp_init(void);
150void vlan_mvrp_uninit(void);
David Ward86fbe9b2013-02-08 17:17:07 +0000151#else
152static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
153static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
154static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
155static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
156static inline int vlan_mvrp_init(void) { return 0; }
157static inline void vlan_mvrp_uninit(void) {}
158#endif
159
Stephen Hemmingerb30200612008-10-28 22:12:36 -0700160extern const char vlan_fullname[];
161extern const char vlan_version[];
Joe Perches348662a2013-10-18 13:48:22 -0700162int vlan_netlink_init(void);
163void vlan_netlink_fini(void);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700164
165extern struct rtnl_link_ops vlan_link_ops;
166
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300167extern unsigned int vlan_net_id;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700168
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700169struct proc_dir_entry;
170
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700171struct vlan_net {
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700172 /* /proc/net/vlan */
173 struct proc_dir_entry *proc_vlan_dir;
174 /* /proc/net/vlan/config */
175 struct proc_dir_entry *proc_vlan_conf;
Pavel Emelyanov7a17a2f2008-04-16 00:54:39 -0700176 /* Determines interface naming scheme. */
177 unsigned short name_type;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700178};
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif /* !(__BEN_VLAN_802_1Q_INC__) */