Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __BEN_VLAN_802_1Q_INC__ |
| 2 | #define __BEN_VLAN_802_1Q_INC__ |
| 3 | |
| 4 | #include <linux/if_vlan.h> |
Eric Dumazet | 9618e2f | 2010-06-24 00:55:06 +0000 | [diff] [blame] | 5 | #include <linux/u64_stats_sync.h> |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 6 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 8 | /* 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 McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 15 | enum vlan_protos { |
| 16 | VLAN_PROTO_8021Q = 0, |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 17 | VLAN_PROTO_8021AD, |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 18 | VLAN_PROTO_NUM, |
| 19 | }; |
| 20 | |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 21 | struct vlan_group { |
| 22 | unsigned int nr_vlan_devs; |
| 23 | struct hlist_node hlist; /* linked list */ |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 24 | struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM] |
| 25 | [VLAN_GROUP_ARRAY_SPLIT_PARTS]; |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | struct 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 McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 38 | static inline unsigned int vlan_proto_idx(__be16 proto) |
| 39 | { |
| 40 | switch (proto) { |
Joe Perches | f0e7882 | 2014-03-12 10:04:15 -0700 | [diff] [blame] | 41 | case htons(ETH_P_8021Q): |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 42 | return VLAN_PROTO_8021Q; |
Joe Perches | f0e7882 | 2014-03-12 10:04:15 -0700 | [diff] [blame] | 43 | case htons(ETH_P_8021AD): |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 44 | return VLAN_PROTO_8021AD; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 45 | default: |
| 46 | BUG(); |
Patrick McHardy | 8da63a6 | 2013-04-21 00:09:32 +0000 | [diff] [blame] | 47 | return 0; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg, |
| 52 | unsigned int pidx, |
| 53 | u16 vlan_id) |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 54 | { |
| 55 | struct net_device **array; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 56 | |
| 57 | array = vg->vlan_devices_arrays[pidx] |
| 58 | [vlan_id / VLAN_GROUP_ARRAY_PART_LEN]; |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 59 | return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL; |
| 60 | } |
| 61 | |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 62 | static 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 Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 69 | static inline void vlan_group_set_device(struct vlan_group *vg, |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 70 | __be16 vlan_proto, u16 vlan_id, |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 71 | struct net_device *dev) |
| 72 | { |
| 73 | struct net_device **array; |
| 74 | if (!vg) |
| 75 | return; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 76 | array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)] |
| 77 | [vlan_id / VLAN_GROUP_ARRAY_PART_LEN]; |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 78 | array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev; |
| 79 | } |
| 80 | |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 81 | /* Must be invoked with rcu_read_lock or with RTNL. */ |
| 82 | static inline struct net_device *vlan_find_dev(struct net_device *real_dev, |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 83 | __be16 vlan_proto, u16 vlan_id) |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 84 | { |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 85 | struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info); |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 86 | |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 87 | if (vlan_info) |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 88 | return vlan_group_get_device(&vlan_info->grp, |
| 89 | vlan_proto, vlan_id); |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 94 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* found in vlan_dev.c */ |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 100 | void vlan_dev_set_ingress_priority(const struct net_device *dev, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 101 | u32 skb_prio, u16 vlan_prio); |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 102 | int vlan_dev_set_egress_priority(const struct net_device *dev, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 103 | u32 skb_prio, u16 vlan_prio); |
Patrick McHardy | b3ce032 | 2008-07-05 21:26:27 -0700 | [diff] [blame] | 104 | int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask); |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 105 | void vlan_dev_get_realdev_name(const struct net_device *dev, char *result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 107 | int vlan_check_real_dev(struct net_device *real_dev, |
| 108 | __be16 protocol, u16 vlan_id); |
Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame] | 109 | void vlan_setup(struct net_device *dev); |
| 110 | int register_vlan_dev(struct net_device *dev); |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 111 | void unregister_vlan_dev(struct net_device *dev, struct list_head *head); |
Eric Dumazet | a57180d | 2020-01-07 01:42:24 -0800 | [diff] [blame] | 112 | void vlan_dev_uninit(struct net_device *dev); |
Mike Manning | 308453a | 2016-05-27 17:45:07 +0100 | [diff] [blame] | 113 | bool vlan_dev_inherit_address(struct net_device *dev, |
| 114 | struct net_device *real_dev); |
Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame] | 115 | |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 116 | static inline u32 vlan_get_ingress_priority(struct net_device *dev, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 117 | u16 vlan_tci) |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 118 | { |
Jiri Pirko | 7da82c0 | 2011-12-08 04:11:15 +0000 | [diff] [blame] | 119 | struct vlan_dev_priv *vip = vlan_dev_priv(dev); |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 120 | |
Eric Dumazet | 05423b2 | 2009-10-26 18:40:35 -0700 | [diff] [blame] | 121 | return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7]; |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Patrick McHardy | 70c03b4 | 2008-07-05 21:26:57 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_VLAN_8021Q_GVRP |
Joe Perches | 348662a | 2013-10-18 13:48:22 -0700 | [diff] [blame] | 125 | int vlan_gvrp_request_join(const struct net_device *dev); |
| 126 | void vlan_gvrp_request_leave(const struct net_device *dev); |
| 127 | int vlan_gvrp_init_applicant(struct net_device *dev); |
| 128 | void vlan_gvrp_uninit_applicant(struct net_device *dev); |
| 129 | int vlan_gvrp_init(void); |
| 130 | void vlan_gvrp_uninit(void); |
Patrick McHardy | 70c03b4 | 2008-07-05 21:26:57 -0700 | [diff] [blame] | 131 | #else |
| 132 | static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; } |
| 133 | static inline void vlan_gvrp_request_leave(const struct net_device *dev) {} |
| 134 | static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; } |
| 135 | static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {} |
| 136 | static inline int vlan_gvrp_init(void) { return 0; } |
| 137 | static inline void vlan_gvrp_uninit(void) {} |
| 138 | #endif |
| 139 | |
David Ward | 86fbe9b | 2013-02-08 17:17:07 +0000 | [diff] [blame] | 140 | #ifdef CONFIG_VLAN_8021Q_MVRP |
Joe Perches | 348662a | 2013-10-18 13:48:22 -0700 | [diff] [blame] | 141 | int vlan_mvrp_request_join(const struct net_device *dev); |
| 142 | void vlan_mvrp_request_leave(const struct net_device *dev); |
| 143 | int vlan_mvrp_init_applicant(struct net_device *dev); |
| 144 | void vlan_mvrp_uninit_applicant(struct net_device *dev); |
| 145 | int vlan_mvrp_init(void); |
| 146 | void vlan_mvrp_uninit(void); |
David Ward | 86fbe9b | 2013-02-08 17:17:07 +0000 | [diff] [blame] | 147 | #else |
| 148 | static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; } |
| 149 | static inline void vlan_mvrp_request_leave(const struct net_device *dev) {} |
| 150 | static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; } |
| 151 | static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {} |
| 152 | static inline int vlan_mvrp_init(void) { return 0; } |
| 153 | static inline void vlan_mvrp_uninit(void) {} |
| 154 | #endif |
| 155 | |
Stephen Hemminger | b3020061 | 2008-10-28 22:12:36 -0700 | [diff] [blame] | 156 | extern const char vlan_fullname[]; |
| 157 | extern const char vlan_version[]; |
Joe Perches | 348662a | 2013-10-18 13:48:22 -0700 | [diff] [blame] | 158 | int vlan_netlink_init(void); |
| 159 | void vlan_netlink_fini(void); |
Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame] | 160 | |
| 161 | extern struct rtnl_link_ops vlan_link_ops; |
| 162 | |
Pavel Emelyanov | d9ed0f0 | 2008-04-16 00:49:09 -0700 | [diff] [blame] | 163 | extern int vlan_net_id; |
| 164 | |
Pavel Emelyanov | a59a8c1 | 2008-04-16 00:51:51 -0700 | [diff] [blame] | 165 | struct proc_dir_entry; |
| 166 | |
Pavel Emelyanov | d9ed0f0 | 2008-04-16 00:49:09 -0700 | [diff] [blame] | 167 | struct vlan_net { |
Pavel Emelyanov | a59a8c1 | 2008-04-16 00:51:51 -0700 | [diff] [blame] | 168 | /* /proc/net/vlan */ |
| 169 | struct proc_dir_entry *proc_vlan_dir; |
| 170 | /* /proc/net/vlan/config */ |
| 171 | struct proc_dir_entry *proc_vlan_conf; |
Pavel Emelyanov | 7a17a2f | 2008-04-16 00:54:39 -0700 | [diff] [blame] | 172 | /* Determines interface naming scheme. */ |
| 173 | unsigned short name_type; |
Pavel Emelyanov | d9ed0f0 | 2008-04-16 00:49:09 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | #endif /* !(__BEN_VLAN_802_1Q_INC__) */ |