Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * VLAN An implementation of 802.1Q VLAN tagging. |
| 4 | * |
| 5 | * Authors: Ben Greear <greearb@candelatech.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #ifndef _LINUX_IF_VLAN_H_ |
| 8 | #define _LINUX_IF_VLAN_H_ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/netdevice.h> |
Stephen Hemminger | 0610d11 | 2006-07-14 16:34:22 -0700 | [diff] [blame] | 11 | #include <linux/etherdevice.h> |
Jesse Gross | 65ac6a5 | 2010-10-20 13:56:05 +0000 | [diff] [blame] | 12 | #include <linux/rtnetlink.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 13 | #include <linux/bug.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 14 | #include <uapi/linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Joe Perches | a3f671b | 2012-03-18 17:37:56 +0000 | [diff] [blame] | 16 | #define VLAN_HLEN 4 /* The additional bytes required by VLAN |
| 17 | * (in addition to the Ethernet header) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define VLAN_ETH_HLEN 18 /* Total octets in header. */ |
| 20 | #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */ |
| 21 | |
| 22 | /* |
| 23 | * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan |
| 24 | */ |
| 25 | #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */ |
| 26 | #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */ |
| 27 | |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 28 | #define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */ |
| 29 | |
Patrick McHardy | 740c15d | 2008-01-21 00:18:53 -0800 | [diff] [blame] | 30 | /* |
| 31 | * struct vlan_hdr - vlan header |
| 32 | * @h_vlan_TCI: priority and VLAN ID |
| 33 | * @h_vlan_encapsulated_proto: packet type ID or len |
| 34 | */ |
| 35 | struct vlan_hdr { |
| 36 | __be16 h_vlan_TCI; |
| 37 | __be16 h_vlan_encapsulated_proto; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr) |
| 42 | * @h_dest: destination ethernet address |
| 43 | * @h_source: source ethernet address |
Olaf Hering | a7bf580 | 2013-06-14 17:07:01 +0200 | [diff] [blame] | 44 | * @h_vlan_proto: ethernet protocol |
Patrick McHardy | 740c15d | 2008-01-21 00:18:53 -0800 | [diff] [blame] | 45 | * @h_vlan_TCI: priority and VLAN ID |
| 46 | * @h_vlan_encapsulated_proto: packet type ID or len |
| 47 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct vlan_ethhdr { |
Patrick McHardy | 740c15d | 2008-01-21 00:18:53 -0800 | [diff] [blame] | 49 | unsigned char h_dest[ETH_ALEN]; |
| 50 | unsigned char h_source[ETH_ALEN]; |
| 51 | __be16 h_vlan_proto; |
| 52 | __be16 h_vlan_TCI; |
| 53 | __be16 h_vlan_encapsulated_proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | #include <linux/skbuff.h> |
| 57 | |
| 58 | static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb) |
| 59 | { |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 60 | return (struct vlan_ethhdr *)skb_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Eric Dumazet | 05423b2 | 2009-10-26 18:40:35 -0700 | [diff] [blame] | 63 | #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */ |
| 64 | #define VLAN_PRIO_SHIFT 13 |
Michał Mirosław | a2e768b | 2018-11-20 13:20:31 +0100 | [diff] [blame] | 65 | #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */ |
Eric Dumazet | 05423b2 | 2009-10-26 18:40:35 -0700 | [diff] [blame] | 66 | #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */ |
Jesse Gross | b738127 | 2010-10-20 13:56:02 +0000 | [diff] [blame] | 67 | #define VLAN_N_VID 4096 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* found in socket.c */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 70 | extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Jiri Pirko | b618aaa | 2015-12-04 15:01:31 +0100 | [diff] [blame] | 72 | static inline bool is_vlan_dev(const struct net_device *dev) |
Neil Horman | 6dcbbe2 | 2011-05-24 08:31:08 +0000 | [diff] [blame] | 73 | { |
| 74 | return dev->priv_flags & IFF_802_1Q_VLAN; |
| 75 | } |
| 76 | |
Michał Mirosław | 0c4b2d3 | 2018-11-10 19:58:36 +0100 | [diff] [blame] | 77 | #define skb_vlan_tag_present(__skb) ((__skb)->vlan_present) |
| 78 | #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci) |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 79 | #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK) |
Michał Mirosław | a2e768b | 2018-11-20 13:20:31 +0100 | [diff] [blame] | 80 | #define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK)) |
Michał Mirosław | 9b31914 | 2018-11-07 18:07:03 +0100 | [diff] [blame] | 81 | #define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Gal Pressman | 9daae9b | 2018-03-28 17:46:54 +0300 | [diff] [blame] | 83 | static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev) |
| 84 | { |
| 85 | ASSERT_RTNL(); |
| 86 | return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev)); |
| 87 | } |
| 88 | |
| 89 | static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev) |
| 90 | { |
| 91 | ASSERT_RTNL(); |
| 92 | call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev); |
| 93 | } |
| 94 | |
| 95 | static inline int vlan_get_rx_stag_filter_info(struct net_device *dev) |
| 96 | { |
| 97 | ASSERT_RTNL(); |
| 98 | return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev)); |
| 99 | } |
| 100 | |
| 101 | static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev) |
| 102 | { |
| 103 | ASSERT_RTNL(); |
| 104 | call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev); |
| 105 | } |
| 106 | |
David S. Miller | e267cb9 | 2013-11-11 00:42:07 -0500 | [diff] [blame] | 107 | /** |
| 108 | * struct vlan_pcpu_stats - VLAN percpu rx/tx stats |
| 109 | * @rx_packets: number of received packets |
| 110 | * @rx_bytes: number of received bytes |
| 111 | * @rx_multicast: number of received multicast packets |
| 112 | * @tx_packets: number of transmitted packets |
| 113 | * @tx_bytes: number of transmitted bytes |
| 114 | * @syncp: synchronization point for 64bit counters |
| 115 | * @rx_errors: number of rx errors |
| 116 | * @tx_dropped: number of tx drops |
| 117 | */ |
| 118 | struct vlan_pcpu_stats { |
| 119 | u64 rx_packets; |
| 120 | u64 rx_bytes; |
| 121 | u64 rx_multicast; |
| 122 | u64 tx_packets; |
| 123 | u64 tx_bytes; |
| 124 | struct u64_stats_sync syncp; |
| 125 | u32 rx_errors; |
| 126 | u32 tx_dropped; |
| 127 | }; |
| 128 | |
Hannes Frederic Sowa | 1e85c9b | 2014-01-06 01:41:20 +0100 | [diff] [blame] | 129 | #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE) |
| 130 | |
dingtianhong | f06c7f9f | 2014-05-09 14:58:05 +0800 | [diff] [blame] | 131 | extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev, |
Hannes Frederic Sowa | 1e85c9b | 2014-01-06 01:41:20 +0100 | [diff] [blame] | 132 | __be16 vlan_proto, u16 vlan_id); |
Ivan Khoronzhuk | 960abf6 | 2018-11-08 22:27:55 +0200 | [diff] [blame] | 133 | extern int vlan_for_each(struct net_device *dev, |
| 134 | int (*action)(struct net_device *dev, int vid, |
| 135 | void *arg), void *arg); |
Hannes Frederic Sowa | 1e85c9b | 2014-01-06 01:41:20 +0100 | [diff] [blame] | 136 | extern struct net_device *vlan_dev_real_dev(const struct net_device *dev); |
| 137 | extern u16 vlan_dev_vlan_id(const struct net_device *dev); |
dingtianhong | 71e415e | 2014-03-25 17:44:42 +0800 | [diff] [blame] | 138 | extern __be16 vlan_dev_vlan_proto(const struct net_device *dev); |
Hannes Frederic Sowa | 1e85c9b | 2014-01-06 01:41:20 +0100 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * struct vlan_priority_tci_mapping - vlan egress priority mappings |
| 142 | * @priority: skb priority |
| 143 | * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000 |
| 144 | * @next: pointer to next struct |
| 145 | */ |
| 146 | struct vlan_priority_tci_mapping { |
| 147 | u32 priority; |
| 148 | u16 vlan_qos; |
| 149 | struct vlan_priority_tci_mapping *next; |
| 150 | }; |
| 151 | |
David S. Miller | e267cb9 | 2013-11-11 00:42:07 -0500 | [diff] [blame] | 152 | struct proc_dir_entry; |
| 153 | struct netpoll; |
| 154 | |
| 155 | /** |
| 156 | * struct vlan_dev_priv - VLAN private device data |
| 157 | * @nr_ingress_mappings: number of ingress priority mappings |
| 158 | * @ingress_priority_map: ingress priority mappings |
| 159 | * @nr_egress_mappings: number of egress priority mappings |
| 160 | * @egress_priority_map: hash of egress priority mappings |
| 161 | * @vlan_proto: VLAN encapsulation protocol |
| 162 | * @vlan_id: VLAN identifier |
| 163 | * @flags: device flags |
| 164 | * @real_dev: underlying netdevice |
| 165 | * @real_dev_addr: address of underlying netdevice |
| 166 | * @dent: proc dir entry |
| 167 | * @vlan_pcpu_stats: ptr to percpu rx stats |
| 168 | */ |
| 169 | struct vlan_dev_priv { |
| 170 | unsigned int nr_ingress_mappings; |
| 171 | u32 ingress_priority_map[8]; |
| 172 | unsigned int nr_egress_mappings; |
| 173 | struct vlan_priority_tci_mapping *egress_priority_map[16]; |
| 174 | |
| 175 | __be16 vlan_proto; |
| 176 | u16 vlan_id; |
| 177 | u16 flags; |
| 178 | |
| 179 | struct net_device *real_dev; |
| 180 | unsigned char real_dev_addr[ETH_ALEN]; |
| 181 | |
| 182 | struct proc_dir_entry *dent; |
| 183 | struct vlan_pcpu_stats __percpu *vlan_pcpu_stats; |
| 184 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 185 | struct netpoll *netpoll; |
| 186 | #endif |
| 187 | }; |
| 188 | |
| 189 | static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev) |
| 190 | { |
| 191 | return netdev_priv(dev); |
| 192 | } |
| 193 | |
| 194 | static inline u16 |
| 195 | vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio) |
| 196 | { |
| 197 | struct vlan_priority_tci_mapping *mp; |
| 198 | |
| 199 | smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */ |
| 200 | |
| 201 | mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)]; |
| 202 | while (mp) { |
| 203 | if (mp->priority == skprio) { |
| 204 | return mp->vlan_qos; /* This should already be shifted |
| 205 | * to mask correctly with the |
| 206 | * VLAN's TCI */ |
| 207 | } |
| 208 | mp = mp->next; |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Florian Zumbiehl | 48cc32d3 | 2012-10-07 15:51:58 +0000 | [diff] [blame] | 213 | extern bool vlan_do_receive(struct sk_buff **skb); |
Patrick McHardy | 9b22ea5 | 2008-11-04 14:49:57 -0800 | [diff] [blame] | 214 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 215 | extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid); |
| 216 | extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid); |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 217 | |
Jiri Pirko | 348a144 | 2011-12-08 04:11:19 +0000 | [diff] [blame] | 218 | extern int vlan_vids_add_by_dev(struct net_device *dev, |
| 219 | const struct net_device *by_dev); |
| 220 | extern void vlan_vids_del_by_dev(struct net_device *dev, |
| 221 | const struct net_device *by_dev); |
Jiri Pirko | 9b361c1 | 2012-08-23 03:26:52 +0000 | [diff] [blame] | 222 | |
| 223 | extern bool vlan_uses_dev(const struct net_device *dev); |
Vlad Yasevich | e1618d4 | 2014-05-20 10:59:26 -0400 | [diff] [blame] | 224 | |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 225 | #else |
Jiri Pirko | cec9c13 | 2011-07-20 04:54:05 +0000 | [diff] [blame] | 226 | static inline struct net_device * |
dingtianhong | f06c7f9f | 2014-05-09 14:58:05 +0800 | [diff] [blame] | 227 | __vlan_find_dev_deep_rcu(struct net_device *real_dev, |
Patrick McHardy | 9fae27b | 2013-04-20 23:51:41 +0000 | [diff] [blame] | 228 | __be16 vlan_proto, u16 vlan_id) |
Jiri Pirko | cec9c13 | 2011-07-20 04:54:05 +0000 | [diff] [blame] | 229 | { |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Ivan Khoronzhuk | 960abf6 | 2018-11-08 22:27:55 +0200 | [diff] [blame] | 233 | static inline int |
| 234 | vlan_for_each(struct net_device *dev, |
| 235 | int (*action)(struct net_device *dev, int vid, void *arg), |
| 236 | void *arg) |
| 237 | { |
| 238 | return 0; |
| 239 | } |
| 240 | |
Patrick McHardy | 22d1ba7 | 2008-07-08 03:23:57 -0700 | [diff] [blame] | 241 | static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev) |
| 242 | { |
| 243 | BUG(); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | static inline u16 vlan_dev_vlan_id(const struct net_device *dev) |
| 248 | { |
| 249 | BUG(); |
| 250 | return 0; |
| 251 | } |
| 252 | |
dingtianhong | 71e415e | 2014-03-25 17:44:42 +0800 | [diff] [blame] | 253 | static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev) |
| 254 | { |
| 255 | BUG(); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Eyal Perry | d324353 | 2013-11-06 15:37:23 +0200 | [diff] [blame] | 259 | static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev, |
| 260 | u32 skprio) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | |
Florian Zumbiehl | 48cc32d3 | 2012-10-07 15:51:58 +0000 | [diff] [blame] | 265 | static inline bool vlan_do_receive(struct sk_buff **skb) |
Patrick McHardy | 9b22ea5 | 2008-11-04 14:49:57 -0800 | [diff] [blame] | 266 | { |
Jesse Gross | 3701e51 | 2010-10-20 13:56:06 +0000 | [diff] [blame] | 267 | return false; |
Patrick McHardy | 9b22ea5 | 2008-11-04 14:49:57 -0800 | [diff] [blame] | 268 | } |
Herbert Xu | e1c096e | 2009-01-06 10:50:09 -0800 | [diff] [blame] | 269 | |
Patrick McHardy | 9fae27b | 2013-04-20 23:51:41 +0000 | [diff] [blame] | 270 | static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid) |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 271 | { |
| 272 | return 0; |
| 273 | } |
| 274 | |
Patrick McHardy | 9fae27b | 2013-04-20 23:51:41 +0000 | [diff] [blame] | 275 | static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid) |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 276 | { |
| 277 | } |
Jiri Pirko | 348a144 | 2011-12-08 04:11:19 +0000 | [diff] [blame] | 278 | |
| 279 | static inline int vlan_vids_add_by_dev(struct net_device *dev, |
| 280 | const struct net_device *by_dev) |
| 281 | { |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static inline void vlan_vids_del_by_dev(struct net_device *dev, |
| 286 | const struct net_device *by_dev) |
| 287 | { |
| 288 | } |
Jiri Pirko | 9b361c1 | 2012-08-23 03:26:52 +0000 | [diff] [blame] | 289 | |
| 290 | static inline bool vlan_uses_dev(const struct net_device *dev) |
| 291 | { |
| 292 | return false; |
| 293 | } |
Patrick McHardy | 7750f40 | 2008-07-08 03:23:36 -0700 | [diff] [blame] | 294 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 296 | /** |
| 297 | * eth_type_vlan - check for valid vlan ether type. |
| 298 | * @ethertype: ether type to check |
| 299 | * |
| 300 | * Returns true if the ether type is a vlan ether type. |
| 301 | */ |
| 302 | static inline bool eth_type_vlan(__be16 ethertype) |
| 303 | { |
| 304 | switch (ethertype) { |
| 305 | case htons(ETH_P_8021Q): |
| 306 | case htons(ETH_P_8021AD): |
| 307 | return true; |
| 308 | default: |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 313 | static inline bool vlan_hw_offload_capable(netdev_features_t features, |
| 314 | __be16 proto) |
| 315 | { |
| 316 | if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX) |
| 317 | return true; |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 318 | if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX) |
| 319 | return true; |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 323 | /** |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 324 | * __vlan_insert_inner_tag - inner VLAN tag inserting |
| 325 | * @skb: skbuff to tag |
| 326 | * @vlan_proto: VLAN encapsulation protocol |
| 327 | * @vlan_tci: VLAN TCI to insert |
| 328 | * @mac_len: MAC header length including outer vlan headers |
| 329 | * |
| 330 | * Inserts the VLAN tag into @skb as part of the payload at offset mac_len |
Thadeu Lima de Souza Cascardo | 8051ac7 | 2018-05-31 09:20:20 -0300 | [diff] [blame] | 331 | * Returns error if skb_cow_head fails. |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 332 | * |
| 333 | * Does not change skb->protocol so this function can be used during receive. |
| 334 | */ |
| 335 | static inline int __vlan_insert_inner_tag(struct sk_buff *skb, |
| 336 | __be16 vlan_proto, u16 vlan_tci, |
| 337 | unsigned int mac_len) |
| 338 | { |
| 339 | struct vlan_ethhdr *veth; |
| 340 | |
| 341 | if (skb_cow_head(skb, VLAN_HLEN) < 0) |
| 342 | return -ENOMEM; |
| 343 | |
| 344 | skb_push(skb, VLAN_HLEN); |
| 345 | |
| 346 | /* Move the mac header sans proto to the beginning of the new header. */ |
Toshiaki Makita | c769acc | 2018-03-29 19:05:30 +0900 | [diff] [blame] | 347 | if (likely(mac_len > ETH_TLEN)) |
| 348 | memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN); |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 349 | skb->mac_header -= VLAN_HLEN; |
| 350 | |
| 351 | veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN); |
| 352 | |
| 353 | /* first, the ethernet type */ |
Toshiaki Makita | c769acc | 2018-03-29 19:05:30 +0900 | [diff] [blame] | 354 | if (likely(mac_len >= ETH_TLEN)) { |
| 355 | /* h_vlan_encapsulated_proto should already be populated, and |
| 356 | * skb->data has space for h_vlan_proto |
| 357 | */ |
| 358 | veth->h_vlan_proto = vlan_proto; |
| 359 | } else { |
| 360 | /* h_vlan_encapsulated_proto should not be populated, and |
| 361 | * skb->data has no space for h_vlan_proto |
| 362 | */ |
| 363 | veth->h_vlan_encapsulated_proto = skb->protocol; |
| 364 | } |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 365 | |
| 366 | /* now, the TCI */ |
| 367 | veth->h_vlan_TCI = htons(vlan_tci); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /** |
Jiri Pirko | 15255a4 | 2014-11-19 14:05:00 +0100 | [diff] [blame] | 373 | * __vlan_insert_tag - regular VLAN tag inserting |
| 374 | * @skb: skbuff to tag |
| 375 | * @vlan_proto: VLAN encapsulation protocol |
| 376 | * @vlan_tci: VLAN TCI to insert |
| 377 | * |
| 378 | * Inserts the VLAN tag into @skb as part of the payload |
Thadeu Lima de Souza Cascardo | 8051ac7 | 2018-05-31 09:20:20 -0300 | [diff] [blame] | 379 | * Returns error if skb_cow_head fails. |
Jiri Pirko | 15255a4 | 2014-11-19 14:05:00 +0100 | [diff] [blame] | 380 | * |
| 381 | * Does not change skb->protocol so this function can be used during receive. |
| 382 | */ |
| 383 | static inline int __vlan_insert_tag(struct sk_buff *skb, |
| 384 | __be16 vlan_proto, u16 vlan_tci) |
| 385 | { |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 386 | return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN); |
| 387 | } |
Jiri Pirko | 15255a4 | 2014-11-19 14:05:00 +0100 | [diff] [blame] | 388 | |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 389 | /** |
| 390 | * vlan_insert_inner_tag - inner VLAN tag inserting |
| 391 | * @skb: skbuff to tag |
| 392 | * @vlan_proto: VLAN encapsulation protocol |
| 393 | * @vlan_tci: VLAN TCI to insert |
| 394 | * @mac_len: MAC header length including outer vlan headers |
| 395 | * |
| 396 | * Inserts the VLAN tag into @skb as part of the payload at offset mac_len |
| 397 | * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. |
| 398 | * |
| 399 | * Following the skb_unshare() example, in case of error, the calling function |
| 400 | * doesn't have to worry about freeing the original skb. |
| 401 | * |
| 402 | * Does not change skb->protocol so this function can be used during receive. |
| 403 | */ |
| 404 | static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb, |
| 405 | __be16 vlan_proto, |
| 406 | u16 vlan_tci, |
| 407 | unsigned int mac_len) |
| 408 | { |
| 409 | int err; |
Jiri Pirko | 15255a4 | 2014-11-19 14:05:00 +0100 | [diff] [blame] | 410 | |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 411 | err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len); |
| 412 | if (err) { |
| 413 | dev_kfree_skb_any(skb); |
| 414 | return NULL; |
| 415 | } |
| 416 | return skb; |
Jiri Pirko | 15255a4 | 2014-11-19 14:05:00 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 420 | * vlan_insert_tag - regular VLAN tag inserting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | * @skb: skbuff to tag |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 422 | * @vlan_proto: VLAN encapsulation protocol |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 423 | * @vlan_tci: VLAN TCI to insert |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | * |
| 425 | * Inserts the VLAN tag into @skb as part of the payload |
| 426 | * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 427 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * Following the skb_unshare() example, in case of error, the calling function |
| 429 | * doesn't have to worry about freeing the original skb. |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 430 | * |
| 431 | * Does not change skb->protocol so this function can be used during receive. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | */ |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 433 | static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, |
| 434 | __be16 vlan_proto, u16 vlan_tci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Toshiaki Makita | cbe7128 | 2018-03-13 14:51:28 +0900 | [diff] [blame] | 436 | return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN); |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 439 | /** |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 440 | * vlan_insert_tag_set_proto - regular VLAN tag inserting |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 441 | * @skb: skbuff to tag |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 442 | * @vlan_proto: VLAN encapsulation protocol |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 443 | * @vlan_tci: VLAN TCI to insert |
| 444 | * |
| 445 | * Inserts the VLAN tag into @skb as part of the payload |
| 446 | * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. |
| 447 | * |
| 448 | * Following the skb_unshare() example, in case of error, the calling function |
| 449 | * doesn't have to worry about freeing the original skb. |
| 450 | */ |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 451 | static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb, |
| 452 | __be16 vlan_proto, |
| 453 | u16 vlan_tci) |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 454 | { |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 455 | skb = vlan_insert_tag(skb, vlan_proto, vlan_tci); |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 456 | if (skb) |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 457 | skb->protocol = vlan_proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return skb; |
| 459 | } |
| 460 | |
Michał Mirosław | c8accd5 | 2018-11-07 18:07:02 +0100 | [diff] [blame] | 461 | /** |
| 462 | * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info |
| 463 | * @skb: skbuff to clear |
| 464 | * |
| 465 | * Clears the VLAN information from @skb |
| 466 | */ |
| 467 | static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb) |
| 468 | { |
Michał Mirosław | 0c4b2d3 | 2018-11-10 19:58:36 +0100 | [diff] [blame] | 469 | skb->vlan_present = 0; |
Michał Mirosław | c8accd5 | 2018-11-07 18:07:02 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Michał Mirosław | e0a6b80 | 2018-11-07 18:07:02 +0100 | [diff] [blame] | 472 | /** |
| 473 | * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb |
| 474 | * @dst: skbuff to copy to |
| 475 | * @src: skbuff to copy from |
| 476 | * |
| 477 | * Copies VLAN information from @src to @dst (for branchless code) |
| 478 | */ |
| 479 | static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src) |
| 480 | { |
Michał Mirosław | 0c4b2d3 | 2018-11-10 19:58:36 +0100 | [diff] [blame] | 481 | dst->vlan_present = src->vlan_present; |
Michał Mirosław | e0a6b80 | 2018-11-07 18:07:02 +0100 | [diff] [blame] | 482 | dst->vlan_proto = src->vlan_proto; |
| 483 | dst->vlan_tci = src->vlan_tci; |
| 484 | } |
| 485 | |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 486 | /* |
| 487 | * __vlan_hwaccel_push_inside - pushes vlan tag to the payload |
| 488 | * @skb: skbuff to tag |
| 489 | * |
| 490 | * Pushes the VLAN tag from @skb->vlan_tci inside to the payload. |
| 491 | * |
| 492 | * Following the skb_unshare() example, in case of error, the calling function |
| 493 | * doesn't have to worry about freeing the original skb. |
| 494 | */ |
| 495 | static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb) |
| 496 | { |
| 497 | skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 498 | skb_vlan_tag_get(skb)); |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 499 | if (likely(skb)) |
Michał Mirosław | c8accd5 | 2018-11-07 18:07:02 +0100 | [diff] [blame] | 500 | __vlan_hwaccel_clear_tag(skb); |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 501 | return skb; |
| 502 | } |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | /** |
| 505 | * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting |
| 506 | * @skb: skbuff to tag |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 507 | * @vlan_proto: VLAN encapsulation protocol |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 508 | * @vlan_tci: VLAN TCI to insert |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | * |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 510 | * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | */ |
Jiri Pirko | b960a0ac | 2014-11-19 14:04:56 +0100 | [diff] [blame] | 512 | static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb, |
| 513 | __be16 vlan_proto, u16 vlan_tci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 515 | skb->vlan_proto = vlan_proto; |
Michał Mirosław | 0c4b2d3 | 2018-11-10 19:58:36 +0100 | [diff] [blame] | 516 | skb->vlan_tci = vlan_tci; |
| 517 | skb->vlan_present = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | * __vlan_get_tag - get the VLAN ID that is part of the payload |
| 522 | * @skb: skbuff to query |
Vivien Didelot | f4fb874 | 2015-05-27 21:07:26 -0400 | [diff] [blame] | 523 | * @vlan_tci: buffer to store value |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 524 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | * Returns error if the skb is not of VLAN type |
| 526 | */ |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 527 | static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
| 529 | struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data; |
| 530 | |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 531 | if (!eth_type_vlan(veth->h_vlan_proto)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 534 | *vlan_tci = ntohs(veth->h_vlan_TCI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[] |
| 540 | * @skb: skbuff to query |
Vivien Didelot | f4fb874 | 2015-05-27 21:07:26 -0400 | [diff] [blame] | 541 | * @vlan_tci: buffer to store value |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 542 | * |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 543 | * Returns error if @skb->vlan_tci is not set correctly |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | */ |
Patrick McHardy | 1814993 | 2008-02-05 16:20:22 -0800 | [diff] [blame] | 545 | static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb, |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 546 | u16 *vlan_tci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 548 | if (skb_vlan_tag_present(skb)) { |
| 549 | *vlan_tci = skb_vlan_tag_get(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | return 0; |
| 551 | } else { |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 552 | *vlan_tci = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return -EINVAL; |
| 554 | } |
| 555 | } |
| 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | /** |
| 558 | * vlan_get_tag - get the VLAN ID from the skb |
| 559 | * @skb: skbuff to query |
Vivien Didelot | f4fb874 | 2015-05-27 21:07:26 -0400 | [diff] [blame] | 560 | * @vlan_tci: buffer to store value |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 561 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | * Returns error if the skb is not VLAN tagged |
| 563 | */ |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 564 | static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 566 | if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) { |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 567 | return __vlan_hwaccel_get_tag(skb, vlan_tci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } else { |
Patrick McHardy | 9bb8582 | 2008-07-08 03:24:44 -0700 | [diff] [blame] | 569 | return __vlan_get_tag(skb, vlan_tci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Hao Zheng | 0a85df0 | 2010-11-11 13:47:57 +0000 | [diff] [blame] | 573 | /** |
| 574 | * vlan_get_protocol - get protocol EtherType. |
| 575 | * @skb: skbuff to query |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 576 | * @type: first vlan protocol |
| 577 | * @depth: buffer to store length of eth and vlan tags in bytes |
Hao Zheng | 0a85df0 | 2010-11-11 13:47:57 +0000 | [diff] [blame] | 578 | * |
| 579 | * Returns the EtherType of the packet, regardless of whether it is |
| 580 | * vlan encapsulated (normal or hardware accelerated) or not. |
| 581 | */ |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 582 | static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type, |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 583 | int *depth) |
Hao Zheng | 0a85df0 | 2010-11-11 13:47:57 +0000 | [diff] [blame] | 584 | { |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 585 | unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH; |
Hao Zheng | 0a85df0 | 2010-11-11 13:47:57 +0000 | [diff] [blame] | 586 | |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 587 | /* if type is 802.1Q/AD then the header should already be |
| 588 | * present at mac_len - VLAN_HLEN (if mac_len > 0), or at |
| 589 | * ETH_HLEN otherwise |
| 590 | */ |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 591 | if (eth_type_vlan(type)) { |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 592 | if (vlan_depth) { |
| 593 | if (WARN_ON(vlan_depth < VLAN_HLEN)) |
| 594 | return 0; |
| 595 | vlan_depth -= VLAN_HLEN; |
| 596 | } else { |
| 597 | vlan_depth = ETH_HLEN; |
| 598 | } |
| 599 | do { |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 600 | struct vlan_hdr vhdr, *vh; |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 601 | |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 602 | vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr); |
| 603 | if (unlikely(!vh || !--parse_depth)) |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 604 | return 0; |
| 605 | |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 606 | type = vh->h_vlan_encapsulated_proto; |
| 607 | vlan_depth += VLAN_HLEN; |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 608 | } while (eth_type_vlan(type)); |
Hao Zheng | 0a85df0 | 2010-11-11 13:47:57 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 611 | if (depth) |
| 612 | *depth = vlan_depth; |
| 613 | |
| 614 | return type; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * vlan_get_protocol - get protocol EtherType. |
| 619 | * @skb: skbuff to query |
| 620 | * |
| 621 | * Returns the EtherType of the packet, regardless of whether it is |
| 622 | * vlan encapsulated (normal or hardware accelerated) or not. |
| 623 | */ |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 624 | static inline __be16 vlan_get_protocol(const struct sk_buff *skb) |
Toshiaki Makita | d4bcef3 | 2015-01-29 20:37:07 +0900 | [diff] [blame] | 625 | { |
| 626 | return __vlan_get_protocol(skb, skb->protocol, NULL); |
Hao Zheng | 0a85df0 | 2010-11-11 13:47:57 +0000 | [diff] [blame] | 627 | } |
Pravin B Shelar | 396cf94 | 2011-11-18 13:15:54 -0800 | [diff] [blame] | 628 | |
Toke Høiland-Jørgensen | 469aced | 2020-07-07 13:03:25 +0200 | [diff] [blame] | 629 | /* A getter for the SKB protocol field which will handle VLAN tags consistently |
| 630 | * whether VLAN acceleration is enabled or not. |
| 631 | */ |
| 632 | static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan) |
| 633 | { |
| 634 | if (!skip_vlan) |
| 635 | /* VLAN acceleration strips the VLAN header from the skb and |
| 636 | * moves it to skb->vlan_proto |
| 637 | */ |
| 638 | return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol; |
| 639 | |
| 640 | return vlan_get_protocol(skb); |
| 641 | } |
| 642 | |
Pravin B Shelar | 396cf94 | 2011-11-18 13:15:54 -0800 | [diff] [blame] | 643 | static inline void vlan_set_encap_proto(struct sk_buff *skb, |
| 644 | struct vlan_hdr *vhdr) |
| 645 | { |
| 646 | __be16 proto; |
Cong Wang | da8c872 | 2013-02-21 23:32:27 +0000 | [diff] [blame] | 647 | unsigned short *rawp; |
Pravin B Shelar | 396cf94 | 2011-11-18 13:15:54 -0800 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | * Was a VLAN packet, grab the encapsulated protocol, which the layer |
| 651 | * three protocols care about. |
| 652 | */ |
| 653 | |
| 654 | proto = vhdr->h_vlan_encapsulated_proto; |
Alexander Duyck | 9545b22 | 2015-05-04 14:34:10 -0700 | [diff] [blame] | 655 | if (eth_proto_is_802_3(proto)) { |
Pravin B Shelar | 396cf94 | 2011-11-18 13:15:54 -0800 | [diff] [blame] | 656 | skb->protocol = proto; |
| 657 | return; |
| 658 | } |
| 659 | |
Cong Wang | da8c872 | 2013-02-21 23:32:27 +0000 | [diff] [blame] | 660 | rawp = (unsigned short *)(vhdr + 1); |
| 661 | if (*rawp == 0xFFFF) |
Pravin B Shelar | 396cf94 | 2011-11-18 13:15:54 -0800 | [diff] [blame] | 662 | /* |
| 663 | * This is a magic hack to spot IPX packets. Older Novell |
| 664 | * breaks the protocol design and runs IPX over 802.3 without |
| 665 | * an 802.2 LLC layer. We look for FFFF which isn't a used |
| 666 | * 802.2 SSAP/DSAP. This won't work for fault tolerant netware |
| 667 | * but does for the rest. |
| 668 | */ |
| 669 | skb->protocol = htons(ETH_P_802_3); |
| 670 | else |
| 671 | /* |
| 672 | * Real 802.2 LLC |
| 673 | */ |
| 674 | skb->protocol = htons(ETH_P_802_2); |
| 675 | } |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 676 | |
Toshiaki Makita | f5a7fb8 | 2015-03-27 14:31:11 +0900 | [diff] [blame] | 677 | /** |
| 678 | * skb_vlan_tagged - check if skb is vlan tagged. |
| 679 | * @skb: skbuff to query |
| 680 | * |
| 681 | * Returns true if the skb is tagged, regardless of whether it is hardware |
| 682 | * accelerated or not. |
| 683 | */ |
| 684 | static inline bool skb_vlan_tagged(const struct sk_buff *skb) |
| 685 | { |
| 686 | if (!skb_vlan_tag_present(skb) && |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 687 | likely(!eth_type_vlan(skb->protocol))) |
Toshiaki Makita | f5a7fb8 | 2015-03-27 14:31:11 +0900 | [diff] [blame] | 688 | return false; |
| 689 | |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers. |
| 695 | * @skb: skbuff to query |
| 696 | * |
| 697 | * Returns true if the skb is tagged with multiple vlan headers, regardless |
| 698 | * of whether it is hardware accelerated or not. |
| 699 | */ |
Toshiaki Makita | 7ce2367 | 2018-04-17 18:46:14 +0900 | [diff] [blame] | 700 | static inline bool skb_vlan_tagged_multi(struct sk_buff *skb) |
Toshiaki Makita | f5a7fb8 | 2015-03-27 14:31:11 +0900 | [diff] [blame] | 701 | { |
| 702 | __be16 protocol = skb->protocol; |
| 703 | |
| 704 | if (!skb_vlan_tag_present(skb)) { |
| 705 | struct vlan_ethhdr *veh; |
| 706 | |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 707 | if (likely(!eth_type_vlan(protocol))) |
Toshiaki Makita | f5a7fb8 | 2015-03-27 14:31:11 +0900 | [diff] [blame] | 708 | return false; |
| 709 | |
Toshiaki Makita | 7ce2367 | 2018-04-17 18:46:14 +0900 | [diff] [blame] | 710 | if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN))) |
| 711 | return false; |
| 712 | |
Toshiaki Makita | f5a7fb8 | 2015-03-27 14:31:11 +0900 | [diff] [blame] | 713 | veh = (struct vlan_ethhdr *)skb->data; |
| 714 | protocol = veh->h_vlan_encapsulated_proto; |
| 715 | } |
| 716 | |
Eric Garver | fe19c4f | 2016-09-07 12:56:58 -0400 | [diff] [blame] | 717 | if (!eth_type_vlan(protocol)) |
Toshiaki Makita | f5a7fb8 | 2015-03-27 14:31:11 +0900 | [diff] [blame] | 718 | return false; |
| 719 | |
| 720 | return true; |
| 721 | } |
| 722 | |
Toshiaki Makita | 8cb65d0 | 2015-03-27 14:31:12 +0900 | [diff] [blame] | 723 | /** |
| 724 | * vlan_features_check - drop unsafe features for skb with multiple tags. |
| 725 | * @skb: skbuff to query |
| 726 | * @features: features to be checked |
| 727 | * |
| 728 | * Returns features without unsafe ones if the skb has multiple tags. |
| 729 | */ |
Toshiaki Makita | 7ce2367 | 2018-04-17 18:46:14 +0900 | [diff] [blame] | 730 | static inline netdev_features_t vlan_features_check(struct sk_buff *skb, |
Toshiaki Makita | 8cb65d0 | 2015-03-27 14:31:12 +0900 | [diff] [blame] | 731 | netdev_features_t features) |
| 732 | { |
Vlad Yasevich | 35d2f80 | 2017-05-23 13:38:41 -0400 | [diff] [blame] | 733 | if (skb_vlan_tagged_multi(skb)) { |
| 734 | /* In the case of multi-tagged packets, use a direct mask |
| 735 | * instead of using netdev_interesect_features(), to make |
| 736 | * sure that only devices supporting NETIF_F_HW_CSUM will |
| 737 | * have checksum offloading support. |
| 738 | */ |
| 739 | features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | |
| 740 | NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX | |
| 741 | NETIF_F_HW_VLAN_STAG_TX; |
| 742 | } |
Toshiaki Makita | 8cb65d0 | 2015-03-27 14:31:12 +0900 | [diff] [blame] | 743 | |
| 744 | return features; |
| 745 | } |
| 746 | |
Toshiaki Makita | 66e5133 | 2015-06-01 21:55:06 +0900 | [diff] [blame] | 747 | /** |
| 748 | * compare_vlan_header - Compare two vlan headers |
| 749 | * @h1: Pointer to vlan header |
| 750 | * @h2: Pointer to vlan header |
| 751 | * |
| 752 | * Compare two vlan headers, returns 0 if equal. |
| 753 | * |
| 754 | * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits. |
| 755 | */ |
| 756 | static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1, |
| 757 | const struct vlan_hdr *h2) |
| 758 | { |
| 759 | #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
| 760 | return *(u32 *)h1 ^ *(u32 *)h2; |
| 761 | #else |
| 762 | return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) | |
| 763 | ((__force u32)h1->h_vlan_encapsulated_proto ^ |
| 764 | (__force u32)h2->h_vlan_encapsulated_proto); |
| 765 | #endif |
| 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | #endif /* !(_LINUX_IF_VLAN_H_) */ |