blob: cc15579780669a613165336387a3f8eabc669dec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __BEN_VLAN_802_1Q_INC__
2#define __BEN_VLAN_802_1Q_INC__
3
4#include <linux/if_vlan.h>
Eric Dumazet9618e2f2010-06-24 00:55:06 +00005#include <linux/u64_stats_sync.h>
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +00006#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +00008/* if this changes, algorithm will have to be reworked because this
9 * depends on completely exhausting the VLAN identifier space. Thus
10 * it gives constant time look-up, but in many cases it wastes memory.
11 */
12#define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
13#define VLAN_GROUP_ARRAY_PART_LEN (VLAN_N_VID/VLAN_GROUP_ARRAY_SPLIT_PARTS)
14
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000015enum vlan_protos {
16 VLAN_PROTO_8021Q = 0,
Patrick McHardy8ad227f2013-04-19 02:04:31 +000017 VLAN_PROTO_8021AD,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000018 VLAN_PROTO_NUM,
19};
20
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000021struct vlan_group {
22 unsigned int nr_vlan_devs;
23 struct hlist_node hlist; /* linked list */
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000024 struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM]
25 [VLAN_GROUP_ARRAY_SPLIT_PARTS];
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000026};
27
28struct vlan_info {
29 struct net_device *real_dev; /* The ethernet(like) device
30 * the vlan is attached to.
31 */
32 struct vlan_group grp;
33 struct list_head vid_list;
34 unsigned int nr_vids;
35 struct rcu_head rcu;
36};
37
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000038static inline unsigned int vlan_proto_idx(__be16 proto)
39{
40 switch (proto) {
Joe Perchesf0e78822014-03-12 10:04:15 -070041 case htons(ETH_P_8021Q):
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000042 return VLAN_PROTO_8021Q;
Joe Perchesf0e78822014-03-12 10:04:15 -070043 case htons(ETH_P_8021AD):
Patrick McHardy8ad227f2013-04-19 02:04:31 +000044 return VLAN_PROTO_8021AD;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000045 default:
46 BUG();
Patrick McHardy8da63a62013-04-21 00:09:32 +000047 return 0;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000048 }
49}
50
51static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
52 unsigned int pidx,
53 u16 vlan_id)
Jiri Pirko536d1d42011-07-20 04:54:49 +000054{
55 struct net_device **array;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000056
57 array = vg->vlan_devices_arrays[pidx]
58 [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
Jiri Pirko536d1d42011-07-20 04:54:49 +000059 return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
60}
61
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000062static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
63 __be16 vlan_proto,
64 u16 vlan_id)
65{
66 return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
67}
68
Jiri Pirko536d1d42011-07-20 04:54:49 +000069static inline void vlan_group_set_device(struct vlan_group *vg,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000070 __be16 vlan_proto, u16 vlan_id,
Jiri Pirko536d1d42011-07-20 04:54:49 +000071 struct net_device *dev)
72{
73 struct net_device **array;
74 if (!vg)
75 return;
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000076 array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
77 [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
Jiri Pirko536d1d42011-07-20 04:54:49 +000078 array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
79}
80
David Lamparter69ecca82011-07-17 08:53:12 +000081/* Must be invoked with rcu_read_lock or with RTNL. */
82static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000083 __be16 vlan_proto, u16 vlan_id)
David Lamparter69ecca82011-07-17 08:53:12 +000084{
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000085 struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
David Lamparter69ecca82011-07-17 08:53:12 +000086
Jiri Pirko5b9ea6e2011-12-08 04:11:18 +000087 if (vlan_info)
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000088 return vlan_group_get_device(&vlan_info->grp,
89 vlan_proto, vlan_id);
David Lamparter69ecca82011-07-17 08:53:12 +000090
91 return NULL;
92}
93
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +000094#define vlan_group_for_each_dev(grp, i, dev) \
95 for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
96 if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
97 (i) % VLAN_N_VID)))
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* found in vlan_dev.c */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700100void vlan_dev_set_ingress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700101 u32 skb_prio, u16 vlan_prio);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700102int vlan_dev_set_egress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700103 u32 skb_prio, u16 vlan_prio);
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700104int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700105void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Patrick McHardy1fd9b1f2013-04-19 02:04:29 +0000107int vlan_check_real_dev(struct net_device *real_dev,
108 __be16 protocol, u16 vlan_id);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700109void vlan_setup(struct net_device *dev);
110int register_vlan_dev(struct net_device *dev);
Eric Dumazet23289a32009-10-27 07:06:36 +0000111void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
Mike Manning308453a2016-05-27 17:45:07 +0100112bool vlan_dev_inherit_address(struct net_device *dev,
113 struct net_device *real_dev);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700114
Patrick McHardy7750f402008-07-08 03:23:36 -0700115static inline u32 vlan_get_ingress_priority(struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700116 u16 vlan_tci)
Patrick McHardy7750f402008-07-08 03:23:36 -0700117{
Jiri Pirko7da82c02011-12-08 04:11:15 +0000118 struct vlan_dev_priv *vip = vlan_dev_priv(dev);
Patrick McHardy7750f402008-07-08 03:23:36 -0700119
Eric Dumazet05423b22009-10-26 18:40:35 -0700120 return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
Patrick McHardy7750f402008-07-08 03:23:36 -0700121}
122
Patrick McHardy70c03b42008-07-05 21:26:57 -0700123#ifdef CONFIG_VLAN_8021Q_GVRP
Joe Perches348662a2013-10-18 13:48:22 -0700124int vlan_gvrp_request_join(const struct net_device *dev);
125void vlan_gvrp_request_leave(const struct net_device *dev);
126int vlan_gvrp_init_applicant(struct net_device *dev);
127void vlan_gvrp_uninit_applicant(struct net_device *dev);
128int vlan_gvrp_init(void);
129void vlan_gvrp_uninit(void);
Patrick McHardy70c03b42008-07-05 21:26:57 -0700130#else
131static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
132static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
133static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
134static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
135static inline int vlan_gvrp_init(void) { return 0; }
136static inline void vlan_gvrp_uninit(void) {}
137#endif
138
David Ward86fbe9b2013-02-08 17:17:07 +0000139#ifdef CONFIG_VLAN_8021Q_MVRP
Joe Perches348662a2013-10-18 13:48:22 -0700140int vlan_mvrp_request_join(const struct net_device *dev);
141void vlan_mvrp_request_leave(const struct net_device *dev);
142int vlan_mvrp_init_applicant(struct net_device *dev);
143void vlan_mvrp_uninit_applicant(struct net_device *dev);
144int vlan_mvrp_init(void);
145void vlan_mvrp_uninit(void);
David Ward86fbe9b2013-02-08 17:17:07 +0000146#else
147static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
148static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
149static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
150static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
151static inline int vlan_mvrp_init(void) { return 0; }
152static inline void vlan_mvrp_uninit(void) {}
153#endif
154
Stephen Hemmingerb30200612008-10-28 22:12:36 -0700155extern const char vlan_fullname[];
156extern const char vlan_version[];
Joe Perches348662a2013-10-18 13:48:22 -0700157int vlan_netlink_init(void);
158void vlan_netlink_fini(void);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700159
160extern struct rtnl_link_ops vlan_link_ops;
161
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700162extern int vlan_net_id;
163
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700164struct proc_dir_entry;
165
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700166struct vlan_net {
Pavel Emelyanova59a8c12008-04-16 00:51:51 -0700167 /* /proc/net/vlan */
168 struct proc_dir_entry *proc_vlan_dir;
169 /* /proc/net/vlan/config */
170 struct proc_dir_entry *proc_vlan_conf;
Pavel Emelyanov7a17a2f2008-04-16 00:54:39 -0700171 /* Determines interface naming scheme. */
172 unsigned short name_type;
Pavel Emelyanovd9ed0f02008-04-16 00:49:09 -0700173};
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#endif /* !(__BEN_VLAN_802_1Q_INC__) */