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