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