1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * VLAN An implementation of 802.1Q VLAN tagging.
5 * Authors: Ben Greear <greearb@candelatech.com>
7 #ifndef _LINUX_IF_VLAN_H_
8 #define _LINUX_IF_VLAN_H_
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>
16 #define VLAN_HLEN 4 /* The additional bytes required by VLAN
17 * (in addition to the Ethernet header)
19 #define VLAN_ETH_HLEN 18 /* Total octets in header. */
20 #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
23 * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
25 #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
26 #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
28 #define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */
31 * struct vlan_hdr - vlan header
32 * @h_vlan_TCI: priority and VLAN ID
33 * @h_vlan_encapsulated_proto: packet type ID or len
37 __be16 h_vlan_encapsulated_proto;
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
49 unsigned char h_dest[ETH_ALEN];
50 unsigned char h_source[ETH_ALEN];
53 __be16 h_vlan_encapsulated_proto;
56 #include <linux/skbuff.h>
58 static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
60 return (struct vlan_ethhdr *)skb_mac_header(skb);
63 /* Prefer this version in TX path, instead of
64 * skb_reset_mac_header() + vlan_eth_hdr()
66 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
68 return (struct vlan_ethhdr *)skb->data;
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
77 /* found in socket.c */
78 extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
80 static inline bool is_vlan_dev(const struct net_device *dev)
82 return dev->priv_flags & IFF_802_1Q_VLAN;
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)
91 static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
94 return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
97 static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
100 call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
103 static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
106 return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
109 static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
112 call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
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
126 struct vlan_pcpu_stats {
132 struct u64_stats_sync syncp;
137 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
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);
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
154 struct vlan_priority_tci_mapping {
157 struct vlan_priority_tci_mapping *next;
160 struct proc_dir_entry;
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
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];
187 struct net_device *real_dev;
188 unsigned char real_dev_addr[ETH_ALEN];
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;
197 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
199 return netdev_priv(dev);
203 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
205 struct vlan_priority_tci_mapping *mp;
207 smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
209 mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
211 if (mp->priority == skprio) {
212 return mp->vlan_qos; /* This should already be shifted
213 * to mask correctly with the
221 extern bool vlan_do_receive(struct sk_buff **skb);
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);
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);
231 extern bool vlan_uses_dev(const struct net_device *dev);
234 static inline struct net_device *
235 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
236 __be16 vlan_proto, u16 vlan_id)
242 vlan_for_each(struct net_device *dev,
243 int (*action)(struct net_device *dev, int vid, void *arg),
249 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
255 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
261 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
267 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
273 static inline bool vlan_do_receive(struct sk_buff **skb)
278 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
283 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
287 static inline int vlan_vids_add_by_dev(struct net_device *dev,
288 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)
298 static inline bool vlan_uses_dev(const struct net_device *dev)
305 * eth_type_vlan - check for valid vlan ether type.
306 * @ethertype: ether type to check
308 * Returns true if the ether type is a vlan ether type.
310 static inline bool eth_type_vlan(__be16 ethertype)
313 case htons(ETH_P_8021Q):
314 case htons(ETH_P_8021AD):
321 static inline bool vlan_hw_offload_capable(netdev_features_t features,
324 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
326 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
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
338 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
339 * Returns error if skb_cow_head fails.
341 * Does not change skb->protocol so this function can be used during receive.
343 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
344 __be16 vlan_proto, u16 vlan_tci,
345 unsigned int mac_len)
347 struct vlan_ethhdr *veth;
349 if (skb_cow_head(skb, VLAN_HLEN) < 0)
352 skb_push(skb, VLAN_HLEN);
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;
359 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
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
366 veth->h_vlan_proto = vlan_proto;
368 /* h_vlan_encapsulated_proto should not be populated, and
369 * skb->data has no space for h_vlan_proto
371 veth->h_vlan_encapsulated_proto = skb->protocol;
375 veth->h_vlan_TCI = htons(vlan_tci);
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
386 * Inserts the VLAN tag into @skb as part of the payload
387 * Returns error if skb_cow_head fails.
389 * Does not change skb->protocol so this function can be used during receive.
391 static inline int __vlan_insert_tag(struct sk_buff *skb,
392 __be16 vlan_proto, u16 vlan_tci)
394 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
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
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.
407 * Following the skb_unshare() example, in case of error, the calling function
408 * doesn't have to worry about freeing the original skb.
410 * Does not change skb->protocol so this function can be used during receive.
412 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
415 unsigned int mac_len)
419 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
421 dev_kfree_skb_any(skb);
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
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.
436 * Following the skb_unshare() example, in case of error, the calling function
437 * doesn't have to worry about freeing the original skb.
439 * Does not change skb->protocol so this function can be used during receive.
441 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
442 __be16 vlan_proto, u16 vlan_tci)
444 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
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
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.
456 * Following the skb_unshare() example, in case of error, the calling function
457 * doesn't have to worry about freeing the original skb.
459 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
463 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
465 skb->protocol = vlan_proto;
470 * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
471 * @skb: skbuff to clear
473 * Clears the VLAN information from @skb
475 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
477 skb->vlan_present = 0;
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
485 * Copies VLAN information from @src to @dst (for branchless code)
487 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
489 dst->vlan_present = src->vlan_present;
490 dst->vlan_proto = src->vlan_proto;
491 dst->vlan_tci = src->vlan_tci;
495 * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
496 * @skb: skbuff to tag
498 * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
500 * Following the skb_unshare() example, in case of error, the calling function
501 * doesn't have to worry about freeing the original skb.
503 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
505 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
506 skb_vlan_tag_get(skb));
508 __vlan_hwaccel_clear_tag(skb);
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
518 * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
520 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
521 __be16 vlan_proto, u16 vlan_tci)
523 skb->vlan_proto = vlan_proto;
524 skb->vlan_tci = vlan_tci;
525 skb->vlan_present = 1;
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
533 * Returns error if the skb is not of VLAN type
535 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
537 struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb);
539 if (!eth_type_vlan(veth->h_vlan_proto))
542 *vlan_tci = ntohs(veth->h_vlan_TCI);
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
551 * Returns error if @skb->vlan_tci is not set correctly
553 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
556 if (skb_vlan_tag_present(skb)) {
557 *vlan_tci = skb_vlan_tag_get(skb);
566 * vlan_get_tag - get the VLAN ID from the skb
567 * @skb: skbuff to query
568 * @vlan_tci: buffer to store value
570 * Returns error if the skb is not VLAN tagged
572 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
574 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
575 return __vlan_hwaccel_get_tag(skb, vlan_tci);
577 return __vlan_get_tag(skb, vlan_tci);
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
587 * Returns the EtherType of the packet, regardless of whether it is
588 * vlan encapsulated (normal or hardware accelerated) or not.
590 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
593 unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
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
599 if (eth_type_vlan(type)) {
601 if (WARN_ON(vlan_depth < VLAN_HLEN))
603 vlan_depth -= VLAN_HLEN;
605 vlan_depth = ETH_HLEN;
608 struct vlan_hdr vhdr, *vh;
610 vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
611 if (unlikely(!vh || !--parse_depth))
614 type = vh->h_vlan_encapsulated_proto;
615 vlan_depth += VLAN_HLEN;
616 } while (eth_type_vlan(type));
626 * vlan_get_protocol - get protocol EtherType.
627 * @skb: skbuff to query
629 * Returns the EtherType of the packet, regardless of whether it is
630 * vlan encapsulated (normal or hardware accelerated) or not.
632 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
634 return __vlan_get_protocol(skb, skb->protocol, NULL);
637 /* This version of __vlan_get_protocol() also pulls mac header in skb->head */
638 static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb,
639 __be16 type, int *depth)
643 type = __vlan_get_protocol(skb, type, &maclen);
646 if (!pskb_may_pull(skb, maclen))
654 /* A getter for the SKB protocol field which will handle VLAN tags consistently
655 * whether VLAN acceleration is enabled or not.
657 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
660 /* VLAN acceleration strips the VLAN header from the skb and
661 * moves it to skb->vlan_proto
663 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
665 return vlan_get_protocol(skb);
668 static inline void vlan_set_encap_proto(struct sk_buff *skb,
669 struct vlan_hdr *vhdr)
672 unsigned short *rawp;
675 * Was a VLAN packet, grab the encapsulated protocol, which the layer
676 * three protocols care about.
679 proto = vhdr->h_vlan_encapsulated_proto;
680 if (eth_proto_is_802_3(proto)) {
681 skb->protocol = proto;
685 rawp = (unsigned short *)(vhdr + 1);
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.
694 skb->protocol = htons(ETH_P_802_3);
699 skb->protocol = htons(ETH_P_802_2);
703 * skb_vlan_tagged - check if skb is vlan tagged.
704 * @skb: skbuff to query
706 * Returns true if the skb is tagged, regardless of whether it is hardware
707 * accelerated or not.
709 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
711 if (!skb_vlan_tag_present(skb) &&
712 likely(!eth_type_vlan(skb->protocol)))
719 * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
720 * @skb: skbuff to query
722 * Returns true if the skb is tagged with multiple vlan headers, regardless
723 * of whether it is hardware accelerated or not.
725 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
727 __be16 protocol = skb->protocol;
729 if (!skb_vlan_tag_present(skb)) {
730 struct vlan_ethhdr *veh;
732 if (likely(!eth_type_vlan(protocol)))
735 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
738 veh = skb_vlan_eth_hdr(skb);
739 protocol = veh->h_vlan_encapsulated_proto;
742 if (!eth_type_vlan(protocol))
749 * vlan_features_check - drop unsafe features for skb with multiple tags.
750 * @skb: skbuff to query
751 * @features: features to be checked
753 * Returns features without unsafe ones if the skb has multiple tags.
755 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
756 netdev_features_t features)
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.
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;
773 * compare_vlan_header - Compare two vlan headers
774 * @h1: Pointer to vlan header
775 * @h2: Pointer to vlan header
777 * Compare two vlan headers, returns 0 if equal.
779 * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
781 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
782 const struct vlan_hdr *h2)
784 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
785 return *(u32 *)h1 ^ *(u32 *)h2;
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);
792 #endif /* !(_LINUX_IF_VLAN_H_) */