Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef __BEN_VLAN_802_1Q_INC__ |
| 3 | #define __BEN_VLAN_802_1Q_INC__ |
| 4 | |
| 5 | #include <linux/if_vlan.h> |
Eric Dumazet | 9618e2f | 2010-06-24 00:55:06 +0000 | [diff] [blame] | 6 | #include <linux/u64_stats_sync.h> |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 7 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 9 | /* 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 McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 16 | enum vlan_protos { |
| 17 | VLAN_PROTO_8021Q = 0, |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 18 | VLAN_PROTO_8021AD, |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 19 | VLAN_PROTO_NUM, |
| 20 | }; |
| 21 | |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 22 | struct vlan_group { |
| 23 | unsigned int nr_vlan_devs; |
| 24 | struct hlist_node hlist; /* linked list */ |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 25 | struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM] |
| 26 | [VLAN_GROUP_ARRAY_SPLIT_PARTS]; |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct 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 Fainelli | d018684 | 2020-09-24 17:27:44 -0700 | [diff] [blame] | 39 | static inline int vlan_proto_idx(__be16 proto) |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 40 | { |
| 41 | switch (proto) { |
Joe Perches | f0e7882 | 2014-03-12 10:04:15 -0700 | [diff] [blame] | 42 | case htons(ETH_P_8021Q): |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 43 | return VLAN_PROTO_8021Q; |
Joe Perches | f0e7882 | 2014-03-12 10:04:15 -0700 | [diff] [blame] | 44 | case htons(ETH_P_8021AD): |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 45 | return VLAN_PROTO_8021AD; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 46 | default: |
Florian Fainelli | d018684 | 2020-09-24 17:27:44 -0700 | [diff] [blame] | 47 | WARN(1, "invalid VLAN protocol: 0x%04x\n", ntohs(proto)); |
| 48 | return -EINVAL; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg, |
| 53 | unsigned int pidx, |
| 54 | u16 vlan_id) |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 55 | { |
| 56 | struct net_device **array; |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 57 | |
| 58 | array = vg->vlan_devices_arrays[pidx] |
| 59 | [vlan_id / VLAN_GROUP_ARRAY_PART_LEN]; |
Di Zhu | c1102e9d | 2021-04-19 21:56:41 +0800 | [diff] [blame] | 60 | |
| 61 | /* paired with smp_wmb() in vlan_group_prealloc_vid() */ |
| 62 | smp_rmb(); |
| 63 | |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 64 | return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL; |
| 65 | } |
| 66 | |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 67 | static inline struct net_device *vlan_group_get_device(struct vlan_group *vg, |
| 68 | __be16 vlan_proto, |
| 69 | u16 vlan_id) |
| 70 | { |
Florian Fainelli | d018684 | 2020-09-24 17:27:44 -0700 | [diff] [blame] | 71 | 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 McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 79 | static inline void vlan_group_set_device(struct vlan_group *vg, |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 80 | __be16 vlan_proto, u16 vlan_id, |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 81 | struct net_device *dev) |
| 82 | { |
Florian Fainelli | d018684 | 2020-09-24 17:27:44 -0700 | [diff] [blame] | 83 | int pidx = vlan_proto_idx(vlan_proto); |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 84 | struct net_device **array; |
Florian Fainelli | d018684 | 2020-09-24 17:27:44 -0700 | [diff] [blame] | 85 | |
| 86 | if (!vg || pidx < 0) |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 87 | return; |
Florian Fainelli | d018684 | 2020-09-24 17:27:44 -0700 | [diff] [blame] | 88 | array = vg->vlan_devices_arrays[pidx] |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 89 | [vlan_id / VLAN_GROUP_ARRAY_PART_LEN]; |
Jiri Pirko | 536d1d4 | 2011-07-20 04:54:49 +0000 | [diff] [blame] | 90 | array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev; |
| 91 | } |
| 92 | |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 93 | /* Must be invoked with rcu_read_lock or with RTNL. */ |
| 94 | 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] | 95 | __be16 vlan_proto, u16 vlan_id) |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 96 | { |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 97 | struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info); |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 98 | |
Jiri Pirko | 5b9ea6e | 2011-12-08 04:11:18 +0000 | [diff] [blame] | 99 | if (vlan_info) |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 100 | return vlan_group_get_device(&vlan_info->grp, |
| 101 | vlan_proto, vlan_id); |
David Lamparter | 69ecca8 | 2011-07-17 08:53:12 +0000 | [diff] [blame] | 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Davide Caratti | 7dad993 | 2018-11-07 11:28:18 +0100 | [diff] [blame] | 106 | static 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 Kicinski | 9d72b8d | 2021-06-17 21:55:56 -0700 | [diff] [blame] | 111 | (NETIF_F_CSUM_MASK | NETIF_F_GSO_SOFTWARE | |
| 112 | NETIF_F_GSO_ENCAP_ALL); |
Davide Caratti | 7dad993 | 2018-11-07 11:28:18 +0100 | [diff] [blame] | 113 | |
| 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 McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 119 | #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 Pressman | 9daae9b | 2018-03-28 17:46:54 +0300 | [diff] [blame] | 124 | int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto); |
| 125 | void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto); |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* found in vlan_dev.c */ |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 128 | void vlan_dev_set_ingress_priority(const struct net_device *dev, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 129 | u32 skb_prio, u16 vlan_prio); |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 130 | int vlan_dev_set_egress_priority(const struct net_device *dev, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 131 | u32 skb_prio, u16 vlan_prio); |
Patrick McHardy | b3ce032 | 2008-07-05 21:26:27 -0700 | [diff] [blame] | 132 | int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask); |
Kees Cook | 9c153d3 | 2021-06-02 13:27:41 -0700 | [diff] [blame] | 133 | void vlan_dev_get_realdev_name(const struct net_device *dev, char *result, |
| 134 | size_t size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Patrick McHardy | 1fd9b1f | 2013-04-19 02:04:29 +0000 | [diff] [blame] | 136 | int vlan_check_real_dev(struct net_device *real_dev, |
David Ahern | 33fa382 | 2018-05-17 12:29:47 -0700 | [diff] [blame] | 137 | __be16 protocol, u16 vlan_id, |
| 138 | struct netlink_ext_ack *extack); |
Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame] | 139 | void vlan_setup(struct net_device *dev); |
David Ahern | 42ab19e | 2017-10-04 17:48:47 -0700 | [diff] [blame] | 140 | int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack); |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 141 | void unregister_vlan_dev(struct net_device *dev, struct list_head *head); |
Eric Dumazet | 9bbd917 | 2020-01-07 01:42:24 -0800 | [diff] [blame] | 142 | void vlan_dev_uninit(struct net_device *dev); |
Mike Manning | 308453a | 2016-05-27 17:45:07 +0100 | [diff] [blame] | 143 | bool vlan_dev_inherit_address(struct net_device *dev, |
| 144 | struct net_device *real_dev); |
Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame] | 145 | |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 146 | static inline u32 vlan_get_ingress_priority(struct net_device *dev, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 147 | u16 vlan_tci) |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 148 | { |
Jiri Pirko | 7da82c0 | 2011-12-08 04:11:15 +0000 | [diff] [blame] | 149 | struct vlan_dev_priv *vip = vlan_dev_priv(dev); |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 150 | |
Eric Dumazet | 05423b2 | 2009-10-26 18:40:35 -0700 | [diff] [blame] | 151 | return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7]; |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Patrick McHardy | 70c03b4 | 2008-07-05 21:26:57 -0700 | [diff] [blame] | 154 | #ifdef CONFIG_VLAN_8021Q_GVRP |
Joe Perches | 348662a | 2013-10-18 13:48:22 -0700 | [diff] [blame] | 155 | int vlan_gvrp_request_join(const struct net_device *dev); |
| 156 | void vlan_gvrp_request_leave(const struct net_device *dev); |
| 157 | int vlan_gvrp_init_applicant(struct net_device *dev); |
| 158 | void vlan_gvrp_uninit_applicant(struct net_device *dev); |
| 159 | int vlan_gvrp_init(void); |
| 160 | void vlan_gvrp_uninit(void); |
Patrick McHardy | 70c03b4 | 2008-07-05 21:26:57 -0700 | [diff] [blame] | 161 | #else |
| 162 | static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; } |
| 163 | static inline void vlan_gvrp_request_leave(const struct net_device *dev) {} |
| 164 | static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; } |
| 165 | static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {} |
| 166 | static inline int vlan_gvrp_init(void) { return 0; } |
| 167 | static inline void vlan_gvrp_uninit(void) {} |
| 168 | #endif |
| 169 | |
David Ward | 86fbe9b | 2013-02-08 17:17:07 +0000 | [diff] [blame] | 170 | #ifdef CONFIG_VLAN_8021Q_MVRP |
Joe Perches | 348662a | 2013-10-18 13:48:22 -0700 | [diff] [blame] | 171 | int vlan_mvrp_request_join(const struct net_device *dev); |
| 172 | void vlan_mvrp_request_leave(const struct net_device *dev); |
| 173 | int vlan_mvrp_init_applicant(struct net_device *dev); |
| 174 | void vlan_mvrp_uninit_applicant(struct net_device *dev); |
| 175 | int vlan_mvrp_init(void); |
| 176 | void vlan_mvrp_uninit(void); |
David Ward | 86fbe9b | 2013-02-08 17:17:07 +0000 | [diff] [blame] | 177 | #else |
| 178 | static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; } |
| 179 | static inline void vlan_mvrp_request_leave(const struct net_device *dev) {} |
| 180 | static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; } |
| 181 | static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {} |
| 182 | static inline int vlan_mvrp_init(void) { return 0; } |
| 183 | static inline void vlan_mvrp_uninit(void) {} |
| 184 | #endif |
| 185 | |
Stephen Hemminger | b3020061 | 2008-10-28 22:12:36 -0700 | [diff] [blame] | 186 | extern const char vlan_fullname[]; |
| 187 | extern const char vlan_version[]; |
Joe Perches | 348662a | 2013-10-18 13:48:22 -0700 | [diff] [blame] | 188 | int vlan_netlink_init(void); |
| 189 | void vlan_netlink_fini(void); |
Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame] | 190 | |
| 191 | extern struct rtnl_link_ops vlan_link_ops; |
| 192 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 193 | extern unsigned int vlan_net_id; |
Pavel Emelyanov | d9ed0f0 | 2008-04-16 00:49:09 -0700 | [diff] [blame] | 194 | |
Pavel Emelyanov | a59a8c1 | 2008-04-16 00:51:51 -0700 | [diff] [blame] | 195 | struct proc_dir_entry; |
| 196 | |
Pavel Emelyanov | d9ed0f0 | 2008-04-16 00:49:09 -0700 | [diff] [blame] | 197 | struct vlan_net { |
Pavel Emelyanov | a59a8c1 | 2008-04-16 00:51:51 -0700 | [diff] [blame] | 198 | /* /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 Emelyanov | 7a17a2f | 2008-04-16 00:54:39 -0700 | [diff] [blame] | 202 | /* Determines interface naming scheme. */ |
| 203 | unsigned short name_type; |
Pavel Emelyanov | d9ed0f0 | 2008-04-16 00:49:09 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | #endif /* !(__BEN_VLAN_802_1Q_INC__) */ |