Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * VLAN     An implementation of 802.1Q VLAN tagging.
0004  *
0005  * Authors: Ben Greear <greearb@candelatech.com>
0006  */
0007 #ifndef _LINUX_IF_VLAN_H_
0008 #define _LINUX_IF_VLAN_H_
0009 
0010 #include <linux/netdevice.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/rtnetlink.h>
0013 #include <linux/bug.h>
0014 #include <uapi/linux/if_vlan.h>
0015 
0016 #define VLAN_HLEN   4       /* The additional bytes required by VLAN
0017                      * (in addition to the Ethernet header)
0018                      */
0019 #define VLAN_ETH_HLEN   18      /* Total octets in header.   */
0020 #define VLAN_ETH_ZLEN   64      /* Min. octets in frame sans FCS */
0021 
0022 /*
0023  * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
0024  */
0025 #define VLAN_ETH_DATA_LEN   1500    /* Max. octets in payload    */
0026 #define VLAN_ETH_FRAME_LEN  1518    /* Max. octets in frame sans FCS */
0027 
0028 #define VLAN_MAX_DEPTH  8       /* Max. number of nested VLAN tags parsed */
0029 
0030 /*
0031  *  struct vlan_hdr - vlan header
0032  *  @h_vlan_TCI: priority and VLAN ID
0033  *  @h_vlan_encapsulated_proto: packet type ID or len
0034  */
0035 struct vlan_hdr {
0036     __be16  h_vlan_TCI;
0037     __be16  h_vlan_encapsulated_proto;
0038 };
0039 
0040 /**
0041  *  struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
0042  *  @h_dest: destination ethernet address
0043  *  @h_source: source ethernet address
0044  *  @h_vlan_proto: ethernet protocol
0045  *  @h_vlan_TCI: priority and VLAN ID
0046  *  @h_vlan_encapsulated_proto: packet type ID or len
0047  */
0048 struct vlan_ethhdr {
0049     struct_group(addrs,
0050         unsigned char   h_dest[ETH_ALEN];
0051         unsigned char   h_source[ETH_ALEN];
0052     );
0053     __be16      h_vlan_proto;
0054     __be16      h_vlan_TCI;
0055     __be16      h_vlan_encapsulated_proto;
0056 };
0057 
0058 #include <linux/skbuff.h>
0059 
0060 static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
0061 {
0062     return (struct vlan_ethhdr *)skb_mac_header(skb);
0063 }
0064 
0065 #define VLAN_PRIO_MASK      0xe000 /* Priority Code Point */
0066 #define VLAN_PRIO_SHIFT     13
0067 #define VLAN_CFI_MASK       0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */
0068 #define VLAN_VID_MASK       0x0fff /* VLAN Identifier */
0069 #define VLAN_N_VID      4096
0070 
0071 /* found in socket.c */
0072 extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
0073 
0074 static inline bool is_vlan_dev(const struct net_device *dev)
0075 {
0076         return dev->priv_flags & IFF_802_1Q_VLAN;
0077 }
0078 
0079 #define skb_vlan_tag_present(__skb) ((__skb)->vlan_present)
0080 #define skb_vlan_tag_get(__skb)     ((__skb)->vlan_tci)
0081 #define skb_vlan_tag_get_id(__skb)  ((__skb)->vlan_tci & VLAN_VID_MASK)
0082 #define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK))
0083 #define skb_vlan_tag_get_prio(__skb)    (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
0084 
0085 static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
0086 {
0087     ASSERT_RTNL();
0088     return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
0089 }
0090 
0091 static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
0092 {
0093     ASSERT_RTNL();
0094     call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
0095 }
0096 
0097 static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
0098 {
0099     ASSERT_RTNL();
0100     return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
0101 }
0102 
0103 static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
0104 {
0105     ASSERT_RTNL();
0106     call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
0107 }
0108 
0109 /**
0110  *  struct vlan_pcpu_stats - VLAN percpu rx/tx stats
0111  *  @rx_packets: number of received packets
0112  *  @rx_bytes: number of received bytes
0113  *  @rx_multicast: number of received multicast packets
0114  *  @tx_packets: number of transmitted packets
0115  *  @tx_bytes: number of transmitted bytes
0116  *  @syncp: synchronization point for 64bit counters
0117  *  @rx_errors: number of rx errors
0118  *  @tx_dropped: number of tx drops
0119  */
0120 struct vlan_pcpu_stats {
0121     u64_stats_t     rx_packets;
0122     u64_stats_t     rx_bytes;
0123     u64_stats_t     rx_multicast;
0124     u64_stats_t     tx_packets;
0125     u64_stats_t     tx_bytes;
0126     struct u64_stats_sync   syncp;
0127     u32         rx_errors;
0128     u32         tx_dropped;
0129 };
0130 
0131 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
0132 
0133 extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
0134                            __be16 vlan_proto, u16 vlan_id);
0135 extern int vlan_for_each(struct net_device *dev,
0136              int (*action)(struct net_device *dev, int vid,
0137                        void *arg), void *arg);
0138 extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
0139 extern u16 vlan_dev_vlan_id(const struct net_device *dev);
0140 extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
0141 
0142 /**
0143  *  struct vlan_priority_tci_mapping - vlan egress priority mappings
0144  *  @priority: skb priority
0145  *  @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
0146  *  @next: pointer to next struct
0147  */
0148 struct vlan_priority_tci_mapping {
0149     u32                 priority;
0150     u16                 vlan_qos;
0151     struct vlan_priority_tci_mapping    *next;
0152 };
0153 
0154 struct proc_dir_entry;
0155 struct netpoll;
0156 
0157 /**
0158  *  struct vlan_dev_priv - VLAN private device data
0159  *  @nr_ingress_mappings: number of ingress priority mappings
0160  *  @ingress_priority_map: ingress priority mappings
0161  *  @nr_egress_mappings: number of egress priority mappings
0162  *  @egress_priority_map: hash of egress priority mappings
0163  *  @vlan_proto: VLAN encapsulation protocol
0164  *  @vlan_id: VLAN identifier
0165  *  @flags: device flags
0166  *  @real_dev: underlying netdevice
0167  *  @dev_tracker: refcount tracker for @real_dev reference
0168  *  @real_dev_addr: address of underlying netdevice
0169  *  @dent: proc dir entry
0170  *  @vlan_pcpu_stats: ptr to percpu rx stats
0171  */
0172 struct vlan_dev_priv {
0173     unsigned int                nr_ingress_mappings;
0174     u32                 ingress_priority_map[8];
0175     unsigned int                nr_egress_mappings;
0176     struct vlan_priority_tci_mapping    *egress_priority_map[16];
0177 
0178     __be16                  vlan_proto;
0179     u16                 vlan_id;
0180     u16                 flags;
0181 
0182     struct net_device           *real_dev;
0183     netdevice_tracker           dev_tracker;
0184 
0185     unsigned char               real_dev_addr[ETH_ALEN];
0186 
0187     struct proc_dir_entry           *dent;
0188     struct vlan_pcpu_stats __percpu     *vlan_pcpu_stats;
0189 #ifdef CONFIG_NET_POLL_CONTROLLER
0190     struct netpoll              *netpoll;
0191 #endif
0192 };
0193 
0194 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
0195 {
0196     return netdev_priv(dev);
0197 }
0198 
0199 static inline u16
0200 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
0201 {
0202     struct vlan_priority_tci_mapping *mp;
0203 
0204     smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
0205 
0206     mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
0207     while (mp) {
0208         if (mp->priority == skprio) {
0209             return mp->vlan_qos; /* This should already be shifted
0210                           * to mask correctly with the
0211                           * VLAN's TCI */
0212         }
0213         mp = mp->next;
0214     }
0215     return 0;
0216 }
0217 
0218 extern bool vlan_do_receive(struct sk_buff **skb);
0219 
0220 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
0221 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
0222 
0223 extern int vlan_vids_add_by_dev(struct net_device *dev,
0224                 const struct net_device *by_dev);
0225 extern void vlan_vids_del_by_dev(struct net_device *dev,
0226                  const struct net_device *by_dev);
0227 
0228 extern bool vlan_uses_dev(const struct net_device *dev);
0229 
0230 #else
0231 static inline struct net_device *
0232 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
0233              __be16 vlan_proto, u16 vlan_id)
0234 {
0235     return NULL;
0236 }
0237 
0238 static inline int
0239 vlan_for_each(struct net_device *dev,
0240           int (*action)(struct net_device *dev, int vid, void *arg),
0241           void *arg)
0242 {
0243     return 0;
0244 }
0245 
0246 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
0247 {
0248     BUG();
0249     return NULL;
0250 }
0251 
0252 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
0253 {
0254     BUG();
0255     return 0;
0256 }
0257 
0258 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
0259 {
0260     BUG();
0261     return 0;
0262 }
0263 
0264 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
0265                            u32 skprio)
0266 {
0267     return 0;
0268 }
0269 
0270 static inline bool vlan_do_receive(struct sk_buff **skb)
0271 {
0272     return false;
0273 }
0274 
0275 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
0276 {
0277     return 0;
0278 }
0279 
0280 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
0281 {
0282 }
0283 
0284 static inline int vlan_vids_add_by_dev(struct net_device *dev,
0285                        const struct net_device *by_dev)
0286 {
0287     return 0;
0288 }
0289 
0290 static inline void vlan_vids_del_by_dev(struct net_device *dev,
0291                     const struct net_device *by_dev)
0292 {
0293 }
0294 
0295 static inline bool vlan_uses_dev(const struct net_device *dev)
0296 {
0297     return false;
0298 }
0299 #endif
0300 
0301 /**
0302  * eth_type_vlan - check for valid vlan ether type.
0303  * @ethertype: ether type to check
0304  *
0305  * Returns true if the ether type is a vlan ether type.
0306  */
0307 static inline bool eth_type_vlan(__be16 ethertype)
0308 {
0309     switch (ethertype) {
0310     case htons(ETH_P_8021Q):
0311     case htons(ETH_P_8021AD):
0312         return true;
0313     default:
0314         return false;
0315     }
0316 }
0317 
0318 static inline bool vlan_hw_offload_capable(netdev_features_t features,
0319                        __be16 proto)
0320 {
0321     if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
0322         return true;
0323     if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
0324         return true;
0325     return false;
0326 }
0327 
0328 /**
0329  * __vlan_insert_inner_tag - inner VLAN tag inserting
0330  * @skb: skbuff to tag
0331  * @vlan_proto: VLAN encapsulation protocol
0332  * @vlan_tci: VLAN TCI to insert
0333  * @mac_len: MAC header length including outer vlan headers
0334  *
0335  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
0336  * Returns error if skb_cow_head fails.
0337  *
0338  * Does not change skb->protocol so this function can be used during receive.
0339  */
0340 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
0341                       __be16 vlan_proto, u16 vlan_tci,
0342                       unsigned int mac_len)
0343 {
0344     struct vlan_ethhdr *veth;
0345 
0346     if (skb_cow_head(skb, VLAN_HLEN) < 0)
0347         return -ENOMEM;
0348 
0349     skb_push(skb, VLAN_HLEN);
0350 
0351     /* Move the mac header sans proto to the beginning of the new header. */
0352     if (likely(mac_len > ETH_TLEN))
0353         memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
0354     skb->mac_header -= VLAN_HLEN;
0355 
0356     veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
0357 
0358     /* first, the ethernet type */
0359     if (likely(mac_len >= ETH_TLEN)) {
0360         /* h_vlan_encapsulated_proto should already be populated, and
0361          * skb->data has space for h_vlan_proto
0362          */
0363         veth->h_vlan_proto = vlan_proto;
0364     } else {
0365         /* h_vlan_encapsulated_proto should not be populated, and
0366          * skb->data has no space for h_vlan_proto
0367          */
0368         veth->h_vlan_encapsulated_proto = skb->protocol;
0369     }
0370 
0371     /* now, the TCI */
0372     veth->h_vlan_TCI = htons(vlan_tci);
0373 
0374     return 0;
0375 }
0376 
0377 /**
0378  * __vlan_insert_tag - regular VLAN tag inserting
0379  * @skb: skbuff to tag
0380  * @vlan_proto: VLAN encapsulation protocol
0381  * @vlan_tci: VLAN TCI to insert
0382  *
0383  * Inserts the VLAN tag into @skb as part of the payload
0384  * Returns error if skb_cow_head fails.
0385  *
0386  * Does not change skb->protocol so this function can be used during receive.
0387  */
0388 static inline int __vlan_insert_tag(struct sk_buff *skb,
0389                     __be16 vlan_proto, u16 vlan_tci)
0390 {
0391     return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
0392 }
0393 
0394 /**
0395  * vlan_insert_inner_tag - inner VLAN tag inserting
0396  * @skb: skbuff to tag
0397  * @vlan_proto: VLAN encapsulation protocol
0398  * @vlan_tci: VLAN TCI to insert
0399  * @mac_len: MAC header length including outer vlan headers
0400  *
0401  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
0402  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
0403  *
0404  * Following the skb_unshare() example, in case of error, the calling function
0405  * doesn't have to worry about freeing the original skb.
0406  *
0407  * Does not change skb->protocol so this function can be used during receive.
0408  */
0409 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
0410                             __be16 vlan_proto,
0411                             u16 vlan_tci,
0412                             unsigned int mac_len)
0413 {
0414     int err;
0415 
0416     err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
0417     if (err) {
0418         dev_kfree_skb_any(skb);
0419         return NULL;
0420     }
0421     return skb;
0422 }
0423 
0424 /**
0425  * vlan_insert_tag - regular VLAN tag inserting
0426  * @skb: skbuff to tag
0427  * @vlan_proto: VLAN encapsulation protocol
0428  * @vlan_tci: VLAN TCI to insert
0429  *
0430  * Inserts the VLAN tag into @skb as part of the payload
0431  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
0432  *
0433  * Following the skb_unshare() example, in case of error, the calling function
0434  * doesn't have to worry about freeing the original skb.
0435  *
0436  * Does not change skb->protocol so this function can be used during receive.
0437  */
0438 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
0439                           __be16 vlan_proto, u16 vlan_tci)
0440 {
0441     return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
0442 }
0443 
0444 /**
0445  * vlan_insert_tag_set_proto - regular VLAN tag inserting
0446  * @skb: skbuff to tag
0447  * @vlan_proto: VLAN encapsulation protocol
0448  * @vlan_tci: VLAN TCI to insert
0449  *
0450  * Inserts the VLAN tag into @skb as part of the payload
0451  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
0452  *
0453  * Following the skb_unshare() example, in case of error, the calling function
0454  * doesn't have to worry about freeing the original skb.
0455  */
0456 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
0457                             __be16 vlan_proto,
0458                             u16 vlan_tci)
0459 {
0460     skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
0461     if (skb)
0462         skb->protocol = vlan_proto;
0463     return skb;
0464 }
0465 
0466 /**
0467  * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
0468  * @skb: skbuff to clear
0469  *
0470  * Clears the VLAN information from @skb
0471  */
0472 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
0473 {
0474     skb->vlan_present = 0;
0475 }
0476 
0477 /**
0478  * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
0479  * @dst: skbuff to copy to
0480  * @src: skbuff to copy from
0481  *
0482  * Copies VLAN information from @src to @dst (for branchless code)
0483  */
0484 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
0485 {
0486     dst->vlan_present = src->vlan_present;
0487     dst->vlan_proto = src->vlan_proto;
0488     dst->vlan_tci = src->vlan_tci;
0489 }
0490 
0491 /*
0492  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
0493  * @skb: skbuff to tag
0494  *
0495  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
0496  *
0497  * Following the skb_unshare() example, in case of error, the calling function
0498  * doesn't have to worry about freeing the original skb.
0499  */
0500 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
0501 {
0502     skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
0503                     skb_vlan_tag_get(skb));
0504     if (likely(skb))
0505         __vlan_hwaccel_clear_tag(skb);
0506     return skb;
0507 }
0508 
0509 /**
0510  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
0511  * @skb: skbuff to tag
0512  * @vlan_proto: VLAN encapsulation protocol
0513  * @vlan_tci: VLAN TCI to insert
0514  *
0515  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
0516  */
0517 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
0518                       __be16 vlan_proto, u16 vlan_tci)
0519 {
0520     skb->vlan_proto = vlan_proto;
0521     skb->vlan_tci = vlan_tci;
0522     skb->vlan_present = 1;
0523 }
0524 
0525 /**
0526  * __vlan_get_tag - get the VLAN ID that is part of the payload
0527  * @skb: skbuff to query
0528  * @vlan_tci: buffer to store value
0529  *
0530  * Returns error if the skb is not of VLAN type
0531  */
0532 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
0533 {
0534     struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
0535 
0536     if (!eth_type_vlan(veth->h_vlan_proto))
0537         return -EINVAL;
0538 
0539     *vlan_tci = ntohs(veth->h_vlan_TCI);
0540     return 0;
0541 }
0542 
0543 /**
0544  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
0545  * @skb: skbuff to query
0546  * @vlan_tci: buffer to store value
0547  *
0548  * Returns error if @skb->vlan_tci is not set correctly
0549  */
0550 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
0551                      u16 *vlan_tci)
0552 {
0553     if (skb_vlan_tag_present(skb)) {
0554         *vlan_tci = skb_vlan_tag_get(skb);
0555         return 0;
0556     } else {
0557         *vlan_tci = 0;
0558         return -EINVAL;
0559     }
0560 }
0561 
0562 /**
0563  * vlan_get_tag - get the VLAN ID from the skb
0564  * @skb: skbuff to query
0565  * @vlan_tci: buffer to store value
0566  *
0567  * Returns error if the skb is not VLAN tagged
0568  */
0569 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
0570 {
0571     if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
0572         return __vlan_hwaccel_get_tag(skb, vlan_tci);
0573     } else {
0574         return __vlan_get_tag(skb, vlan_tci);
0575     }
0576 }
0577 
0578 /**
0579  * vlan_get_protocol - get protocol EtherType.
0580  * @skb: skbuff to query
0581  * @type: first vlan protocol
0582  * @depth: buffer to store length of eth and vlan tags in bytes
0583  *
0584  * Returns the EtherType of the packet, regardless of whether it is
0585  * vlan encapsulated (normal or hardware accelerated) or not.
0586  */
0587 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
0588                      int *depth)
0589 {
0590     unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
0591 
0592     /* if type is 802.1Q/AD then the header should already be
0593      * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
0594      * ETH_HLEN otherwise
0595      */
0596     if (eth_type_vlan(type)) {
0597         if (vlan_depth) {
0598             if (WARN_ON(vlan_depth < VLAN_HLEN))
0599                 return 0;
0600             vlan_depth -= VLAN_HLEN;
0601         } else {
0602             vlan_depth = ETH_HLEN;
0603         }
0604         do {
0605             struct vlan_hdr vhdr, *vh;
0606 
0607             vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
0608             if (unlikely(!vh || !--parse_depth))
0609                 return 0;
0610 
0611             type = vh->h_vlan_encapsulated_proto;
0612             vlan_depth += VLAN_HLEN;
0613         } while (eth_type_vlan(type));
0614     }
0615 
0616     if (depth)
0617         *depth = vlan_depth;
0618 
0619     return type;
0620 }
0621 
0622 /**
0623  * vlan_get_protocol - get protocol EtherType.
0624  * @skb: skbuff to query
0625  *
0626  * Returns the EtherType of the packet, regardless of whether it is
0627  * vlan encapsulated (normal or hardware accelerated) or not.
0628  */
0629 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
0630 {
0631     return __vlan_get_protocol(skb, skb->protocol, NULL);
0632 }
0633 
0634 /* A getter for the SKB protocol field which will handle VLAN tags consistently
0635  * whether VLAN acceleration is enabled or not.
0636  */
0637 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
0638 {
0639     if (!skip_vlan)
0640         /* VLAN acceleration strips the VLAN header from the skb and
0641          * moves it to skb->vlan_proto
0642          */
0643         return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
0644 
0645     return vlan_get_protocol(skb);
0646 }
0647 
0648 static inline void vlan_set_encap_proto(struct sk_buff *skb,
0649                     struct vlan_hdr *vhdr)
0650 {
0651     __be16 proto;
0652     unsigned short *rawp;
0653 
0654     /*
0655      * Was a VLAN packet, grab the encapsulated protocol, which the layer
0656      * three protocols care about.
0657      */
0658 
0659     proto = vhdr->h_vlan_encapsulated_proto;
0660     if (eth_proto_is_802_3(proto)) {
0661         skb->protocol = proto;
0662         return;
0663     }
0664 
0665     rawp = (unsigned short *)(vhdr + 1);
0666     if (*rawp == 0xFFFF)
0667         /*
0668          * This is a magic hack to spot IPX packets. Older Novell
0669          * breaks the protocol design and runs IPX over 802.3 without
0670          * an 802.2 LLC layer. We look for FFFF which isn't a used
0671          * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
0672          * but does for the rest.
0673          */
0674         skb->protocol = htons(ETH_P_802_3);
0675     else
0676         /*
0677          * Real 802.2 LLC
0678          */
0679         skb->protocol = htons(ETH_P_802_2);
0680 }
0681 
0682 /**
0683  * skb_vlan_tagged - check if skb is vlan tagged.
0684  * @skb: skbuff to query
0685  *
0686  * Returns true if the skb is tagged, regardless of whether it is hardware
0687  * accelerated or not.
0688  */
0689 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
0690 {
0691     if (!skb_vlan_tag_present(skb) &&
0692         likely(!eth_type_vlan(skb->protocol)))
0693         return false;
0694 
0695     return true;
0696 }
0697 
0698 /**
0699  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
0700  * @skb: skbuff to query
0701  *
0702  * Returns true if the skb is tagged with multiple vlan headers, regardless
0703  * of whether it is hardware accelerated or not.
0704  */
0705 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
0706 {
0707     __be16 protocol = skb->protocol;
0708 
0709     if (!skb_vlan_tag_present(skb)) {
0710         struct vlan_ethhdr *veh;
0711 
0712         if (likely(!eth_type_vlan(protocol)))
0713             return false;
0714 
0715         if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
0716             return false;
0717 
0718         veh = (struct vlan_ethhdr *)skb->data;
0719         protocol = veh->h_vlan_encapsulated_proto;
0720     }
0721 
0722     if (!eth_type_vlan(protocol))
0723         return false;
0724 
0725     return true;
0726 }
0727 
0728 /**
0729  * vlan_features_check - drop unsafe features for skb with multiple tags.
0730  * @skb: skbuff to query
0731  * @features: features to be checked
0732  *
0733  * Returns features without unsafe ones if the skb has multiple tags.
0734  */
0735 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
0736                             netdev_features_t features)
0737 {
0738     if (skb_vlan_tagged_multi(skb)) {
0739         /* In the case of multi-tagged packets, use a direct mask
0740          * instead of using netdev_interesect_features(), to make
0741          * sure that only devices supporting NETIF_F_HW_CSUM will
0742          * have checksum offloading support.
0743          */
0744         features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
0745                 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
0746                 NETIF_F_HW_VLAN_STAG_TX;
0747     }
0748 
0749     return features;
0750 }
0751 
0752 /**
0753  * compare_vlan_header - Compare two vlan headers
0754  * @h1: Pointer to vlan header
0755  * @h2: Pointer to vlan header
0756  *
0757  * Compare two vlan headers, returns 0 if equal.
0758  *
0759  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
0760  */
0761 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
0762                         const struct vlan_hdr *h2)
0763 {
0764 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
0765     return *(u32 *)h1 ^ *(u32 *)h2;
0766 #else
0767     return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
0768            ((__force u32)h1->h_vlan_encapsulated_proto ^
0769         (__force u32)h2->h_vlan_encapsulated_proto);
0770 #endif
0771 }
0772 #endif /* !(_LINUX_IF_VLAN_H_) */