11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * VLAN An implementation of 802.1Q VLAN tagging. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: Ben Greear <greearb@candelatech.com> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 81da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 91da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds #ifndef _LINUX_IF_VLAN_H_ 131da177e4SLinus Torvalds #define _LINUX_IF_VLAN_H_ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/netdevice.h> 160610d11bSStephen Hemminger #include <linux/etherdevice.h> 1765ac6a5fSJesse Gross #include <linux/rtnetlink.h> 18187f1882SPaul Gortmaker #include <linux/bug.h> 19607ca46eSDavid Howells #include <uapi/linux/if_vlan.h> 201da177e4SLinus Torvalds 21a3f671b3SJoe Perches #define VLAN_HLEN 4 /* The additional bytes required by VLAN 22a3f671b3SJoe Perches * (in addition to the Ethernet header) 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds #define VLAN_ETH_HLEN 18 /* Total octets in header. */ 251da177e4SLinus Torvalds #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */ 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds /* 281da177e4SLinus Torvalds * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */ 311da177e4SLinus Torvalds #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */ 321da177e4SLinus Torvalds 33740c15d0SPatrick McHardy /* 34740c15d0SPatrick McHardy * struct vlan_hdr - vlan header 35740c15d0SPatrick McHardy * @h_vlan_TCI: priority and VLAN ID 36740c15d0SPatrick McHardy * @h_vlan_encapsulated_proto: packet type ID or len 37740c15d0SPatrick McHardy */ 38740c15d0SPatrick McHardy struct vlan_hdr { 39740c15d0SPatrick McHardy __be16 h_vlan_TCI; 40740c15d0SPatrick McHardy __be16 h_vlan_encapsulated_proto; 41740c15d0SPatrick McHardy }; 42740c15d0SPatrick McHardy 43740c15d0SPatrick McHardy /** 44740c15d0SPatrick McHardy * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr) 45740c15d0SPatrick McHardy * @h_dest: destination ethernet address 46740c15d0SPatrick McHardy * @h_source: source ethernet address 47a7bf5804SOlaf Hering * @h_vlan_proto: ethernet protocol 48740c15d0SPatrick McHardy * @h_vlan_TCI: priority and VLAN ID 49740c15d0SPatrick McHardy * @h_vlan_encapsulated_proto: packet type ID or len 50740c15d0SPatrick McHardy */ 511da177e4SLinus Torvalds struct vlan_ethhdr { 52740c15d0SPatrick McHardy unsigned char h_dest[ETH_ALEN]; 53740c15d0SPatrick McHardy unsigned char h_source[ETH_ALEN]; 54740c15d0SPatrick McHardy __be16 h_vlan_proto; 55740c15d0SPatrick McHardy __be16 h_vlan_TCI; 56740c15d0SPatrick McHardy __be16 h_vlan_encapsulated_proto; 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #include <linux/skbuff.h> 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb) 621da177e4SLinus Torvalds { 6398e399f8SArnaldo Carvalho de Melo return (struct vlan_ethhdr *)skb_mac_header(skb); 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 6605423b24SEric Dumazet #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */ 6705423b24SEric Dumazet #define VLAN_PRIO_SHIFT 13 6805423b24SEric Dumazet #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator */ 6905423b24SEric Dumazet #define VLAN_TAG_PRESENT VLAN_CFI_MASK 7005423b24SEric Dumazet #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */ 71b738127dSJesse Gross #define VLAN_N_VID 4096 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* found in socket.c */ 74881d966bSEric W. Biederman extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *)); 751da177e4SLinus Torvalds 76b618aaa9SJiri Pirko static inline bool is_vlan_dev(const struct net_device *dev) 776dcbbe25SNeil Horman { 786dcbbe25SNeil Horman return dev->priv_flags & IFF_802_1Q_VLAN; 796dcbbe25SNeil Horman } 806dcbbe25SNeil Horman 81df8a39deSJiri Pirko #define skb_vlan_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT) 82df8a39deSJiri Pirko #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT) 83df8a39deSJiri Pirko #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK) 84f6a66927SHadar Hen Zion #define skb_vlan_tag_get_prio(__skb) ((__skb)->vlan_tci & VLAN_PRIO_MASK) 851da177e4SLinus Torvalds 86e267cb96SDavid S. Miller /** 87e267cb96SDavid S. Miller * struct vlan_pcpu_stats - VLAN percpu rx/tx stats 88e267cb96SDavid S. Miller * @rx_packets: number of received packets 89e267cb96SDavid S. Miller * @rx_bytes: number of received bytes 90e267cb96SDavid S. Miller * @rx_multicast: number of received multicast packets 91e267cb96SDavid S. Miller * @tx_packets: number of transmitted packets 92e267cb96SDavid S. Miller * @tx_bytes: number of transmitted bytes 93e267cb96SDavid S. Miller * @syncp: synchronization point for 64bit counters 94e267cb96SDavid S. Miller * @rx_errors: number of rx errors 95e267cb96SDavid S. Miller * @tx_dropped: number of tx drops 96e267cb96SDavid S. Miller */ 97e267cb96SDavid S. Miller struct vlan_pcpu_stats { 98e267cb96SDavid S. Miller u64 rx_packets; 99e267cb96SDavid S. Miller u64 rx_bytes; 100e267cb96SDavid S. Miller u64 rx_multicast; 101e267cb96SDavid S. Miller u64 tx_packets; 102e267cb96SDavid S. Miller u64 tx_bytes; 103e267cb96SDavid S. Miller struct u64_stats_sync syncp; 104e267cb96SDavid S. Miller u32 rx_errors; 105e267cb96SDavid S. Miller u32 tx_dropped; 106e267cb96SDavid S. Miller }; 107e267cb96SDavid S. Miller 1081e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE) 1091e85c9b6SHannes Frederic Sowa 110f06c7f9fSdingtianhong extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev, 1111e85c9b6SHannes Frederic Sowa __be16 vlan_proto, u16 vlan_id); 1121e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev); 1131e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev); 11471e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev); 1151e85c9b6SHannes Frederic Sowa 1161e85c9b6SHannes Frederic Sowa /** 1171e85c9b6SHannes Frederic Sowa * struct vlan_priority_tci_mapping - vlan egress priority mappings 1181e85c9b6SHannes Frederic Sowa * @priority: skb priority 1191e85c9b6SHannes Frederic Sowa * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000 1201e85c9b6SHannes Frederic Sowa * @next: pointer to next struct 1211e85c9b6SHannes Frederic Sowa */ 1221e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping { 1231e85c9b6SHannes Frederic Sowa u32 priority; 1241e85c9b6SHannes Frederic Sowa u16 vlan_qos; 1251e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping *next; 1261e85c9b6SHannes Frederic Sowa }; 1271e85c9b6SHannes Frederic Sowa 128e267cb96SDavid S. Miller struct proc_dir_entry; 129e267cb96SDavid S. Miller struct netpoll; 130e267cb96SDavid S. Miller 131e267cb96SDavid S. Miller /** 132e267cb96SDavid S. Miller * struct vlan_dev_priv - VLAN private device data 133e267cb96SDavid S. Miller * @nr_ingress_mappings: number of ingress priority mappings 134e267cb96SDavid S. Miller * @ingress_priority_map: ingress priority mappings 135e267cb96SDavid S. Miller * @nr_egress_mappings: number of egress priority mappings 136e267cb96SDavid S. Miller * @egress_priority_map: hash of egress priority mappings 137e267cb96SDavid S. Miller * @vlan_proto: VLAN encapsulation protocol 138e267cb96SDavid S. Miller * @vlan_id: VLAN identifier 139e267cb96SDavid S. Miller * @flags: device flags 140e267cb96SDavid S. Miller * @real_dev: underlying netdevice 141e267cb96SDavid S. Miller * @real_dev_addr: address of underlying netdevice 142e267cb96SDavid S. Miller * @dent: proc dir entry 143e267cb96SDavid S. Miller * @vlan_pcpu_stats: ptr to percpu rx stats 144e267cb96SDavid S. Miller */ 145e267cb96SDavid S. Miller struct vlan_dev_priv { 146e267cb96SDavid S. Miller unsigned int nr_ingress_mappings; 147e267cb96SDavid S. Miller u32 ingress_priority_map[8]; 148e267cb96SDavid S. Miller unsigned int nr_egress_mappings; 149e267cb96SDavid S. Miller struct vlan_priority_tci_mapping *egress_priority_map[16]; 150e267cb96SDavid S. Miller 151e267cb96SDavid S. Miller __be16 vlan_proto; 152e267cb96SDavid S. Miller u16 vlan_id; 153e267cb96SDavid S. Miller u16 flags; 154e267cb96SDavid S. Miller 155e267cb96SDavid S. Miller struct net_device *real_dev; 156e267cb96SDavid S. Miller unsigned char real_dev_addr[ETH_ALEN]; 157e267cb96SDavid S. Miller 158e267cb96SDavid S. Miller struct proc_dir_entry *dent; 159e267cb96SDavid S. Miller struct vlan_pcpu_stats __percpu *vlan_pcpu_stats; 160e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER 161e267cb96SDavid S. Miller struct netpoll *netpoll; 162e267cb96SDavid S. Miller #endif 163d38569abSVlad Yasevich unsigned int nest_level; 164e267cb96SDavid S. Miller }; 165e267cb96SDavid S. Miller 166e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev) 167e267cb96SDavid S. Miller { 168e267cb96SDavid S. Miller return netdev_priv(dev); 169e267cb96SDavid S. Miller } 170e267cb96SDavid S. Miller 171e267cb96SDavid S. Miller static inline u16 172e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio) 173e267cb96SDavid S. Miller { 174e267cb96SDavid S. Miller struct vlan_priority_tci_mapping *mp; 175e267cb96SDavid S. Miller 176e267cb96SDavid S. Miller smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */ 177e267cb96SDavid S. Miller 178e267cb96SDavid S. Miller mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)]; 179e267cb96SDavid S. Miller while (mp) { 180e267cb96SDavid S. Miller if (mp->priority == skprio) { 181e267cb96SDavid S. Miller return mp->vlan_qos; /* This should already be shifted 182e267cb96SDavid S. Miller * to mask correctly with the 183e267cb96SDavid S. Miller * VLAN's TCI */ 184e267cb96SDavid S. Miller } 185e267cb96SDavid S. Miller mp = mp->next; 186e267cb96SDavid S. Miller } 187e267cb96SDavid S. Miller return 0; 188e267cb96SDavid S. Miller } 189e267cb96SDavid S. Miller 19048cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb); 1919b22ea56SPatrick McHardy 19280d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid); 19380d5c368SPatrick McHardy extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid); 19487002b03SJiri Pirko 195348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev, 196348a1443SJiri Pirko const struct net_device *by_dev); 197348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev, 198348a1443SJiri Pirko const struct net_device *by_dev); 1999b361c13SJiri Pirko 2009b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev); 201e1618d46SVlad Yasevich 202e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev) 203e1618d46SVlad Yasevich { 204e1618d46SVlad Yasevich BUG_ON(!is_vlan_dev(dev)); 205e1618d46SVlad Yasevich return vlan_dev_priv(dev)->nest_level; 206e1618d46SVlad Yasevich } 2077750f403SPatrick McHardy #else 208cec9c133SJiri Pirko static inline struct net_device * 209f06c7f9fSdingtianhong __vlan_find_dev_deep_rcu(struct net_device *real_dev, 2109fae27b3SPatrick McHardy __be16 vlan_proto, u16 vlan_id) 211cec9c133SJiri Pirko { 212cec9c133SJiri Pirko return NULL; 213cec9c133SJiri Pirko } 214cec9c133SJiri Pirko 21522d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev) 21622d1ba74SPatrick McHardy { 21722d1ba74SPatrick McHardy BUG(); 21822d1ba74SPatrick McHardy return NULL; 21922d1ba74SPatrick McHardy } 22022d1ba74SPatrick McHardy 22122d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev) 22222d1ba74SPatrick McHardy { 22322d1ba74SPatrick McHardy BUG(); 22422d1ba74SPatrick McHardy return 0; 22522d1ba74SPatrick McHardy } 22622d1ba74SPatrick McHardy 22771e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev) 22871e415e4Sdingtianhong { 22971e415e4Sdingtianhong BUG(); 23071e415e4Sdingtianhong return 0; 23171e415e4Sdingtianhong } 23271e415e4Sdingtianhong 233d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev, 234d3243539SEyal Perry u32 skprio) 235d3243539SEyal Perry { 236d3243539SEyal Perry return 0; 237d3243539SEyal Perry } 238d3243539SEyal Perry 23948cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb) 2409b22ea56SPatrick McHardy { 2413701e513SJesse Gross return false; 2429b22ea56SPatrick McHardy } 243e1c096e2SHerbert Xu 2449fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid) 24587002b03SJiri Pirko { 24687002b03SJiri Pirko return 0; 24787002b03SJiri Pirko } 24887002b03SJiri Pirko 2499fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid) 25087002b03SJiri Pirko { 25187002b03SJiri Pirko } 252348a1443SJiri Pirko 253348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev, 254348a1443SJiri Pirko const struct net_device *by_dev) 255348a1443SJiri Pirko { 256348a1443SJiri Pirko return 0; 257348a1443SJiri Pirko } 258348a1443SJiri Pirko 259348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev, 260348a1443SJiri Pirko const struct net_device *by_dev) 261348a1443SJiri Pirko { 262348a1443SJiri Pirko } 2639b361c13SJiri Pirko 2649b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev) 2659b361c13SJiri Pirko { 2669b361c13SJiri Pirko return false; 2679b361c13SJiri Pirko } 268e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev) 269e1618d46SVlad Yasevich { 270e1618d46SVlad Yasevich BUG(); 271e1618d46SVlad Yasevich return 0; 272e1618d46SVlad Yasevich } 2737750f403SPatrick McHardy #endif 2741da177e4SLinus Torvalds 275fe19c4f9SEric Garver /** 276fe19c4f9SEric Garver * eth_type_vlan - check for valid vlan ether type. 277fe19c4f9SEric Garver * @ethertype: ether type to check 278fe19c4f9SEric Garver * 279fe19c4f9SEric Garver * Returns true if the ether type is a vlan ether type. 280fe19c4f9SEric Garver */ 281fe19c4f9SEric Garver static inline bool eth_type_vlan(__be16 ethertype) 282fe19c4f9SEric Garver { 283fe19c4f9SEric Garver switch (ethertype) { 284fe19c4f9SEric Garver case htons(ETH_P_8021Q): 285fe19c4f9SEric Garver case htons(ETH_P_8021AD): 286fe19c4f9SEric Garver return true; 287fe19c4f9SEric Garver default: 288fe19c4f9SEric Garver return false; 289fe19c4f9SEric Garver } 290fe19c4f9SEric Garver } 291fe19c4f9SEric Garver 29286a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features, 29386a9bad3SPatrick McHardy __be16 proto) 29486a9bad3SPatrick McHardy { 29586a9bad3SPatrick McHardy if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX) 29686a9bad3SPatrick McHardy return true; 2978ad227ffSPatrick McHardy if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX) 2988ad227ffSPatrick McHardy return true; 29986a9bad3SPatrick McHardy return false; 30086a9bad3SPatrick McHardy } 30186a9bad3SPatrick McHardy 3029bb8582eSPatrick McHardy /** 303cbe7128cSToshiaki Makita * __vlan_insert_inner_tag - inner VLAN tag inserting 304cbe7128cSToshiaki Makita * @skb: skbuff to tag 305cbe7128cSToshiaki Makita * @vlan_proto: VLAN encapsulation protocol 306cbe7128cSToshiaki Makita * @vlan_tci: VLAN TCI to insert 307cbe7128cSToshiaki Makita * @mac_len: MAC header length including outer vlan headers 308cbe7128cSToshiaki Makita * 309cbe7128cSToshiaki Makita * Inserts the VLAN tag into @skb as part of the payload at offset mac_len 310cbe7128cSToshiaki Makita * Returns error if skb_cow_head failes. 311cbe7128cSToshiaki Makita * 312cbe7128cSToshiaki Makita * Does not change skb->protocol so this function can be used during receive. 313cbe7128cSToshiaki Makita */ 314cbe7128cSToshiaki Makita static inline int __vlan_insert_inner_tag(struct sk_buff *skb, 315cbe7128cSToshiaki Makita __be16 vlan_proto, u16 vlan_tci, 316cbe7128cSToshiaki Makita unsigned int mac_len) 317cbe7128cSToshiaki Makita { 318cbe7128cSToshiaki Makita struct vlan_ethhdr *veth; 319cbe7128cSToshiaki Makita 320cbe7128cSToshiaki Makita if (skb_cow_head(skb, VLAN_HLEN) < 0) 321cbe7128cSToshiaki Makita return -ENOMEM; 322cbe7128cSToshiaki Makita 323cbe7128cSToshiaki Makita skb_push(skb, VLAN_HLEN); 324cbe7128cSToshiaki Makita 325cbe7128cSToshiaki Makita /* Move the mac header sans proto to the beginning of the new header. */ 326*c769accdSToshiaki Makita if (likely(mac_len > ETH_TLEN)) 327cbe7128cSToshiaki Makita memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN); 328cbe7128cSToshiaki Makita skb->mac_header -= VLAN_HLEN; 329cbe7128cSToshiaki Makita 330cbe7128cSToshiaki Makita veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN); 331cbe7128cSToshiaki Makita 332cbe7128cSToshiaki Makita /* first, the ethernet type */ 333*c769accdSToshiaki Makita if (likely(mac_len >= ETH_TLEN)) { 334*c769accdSToshiaki Makita /* h_vlan_encapsulated_proto should already be populated, and 335*c769accdSToshiaki Makita * skb->data has space for h_vlan_proto 336*c769accdSToshiaki Makita */ 337cbe7128cSToshiaki Makita veth->h_vlan_proto = vlan_proto; 338*c769accdSToshiaki Makita } else { 339*c769accdSToshiaki Makita /* h_vlan_encapsulated_proto should not be populated, and 340*c769accdSToshiaki Makita * skb->data has no space for h_vlan_proto 341*c769accdSToshiaki Makita */ 342*c769accdSToshiaki Makita veth->h_vlan_encapsulated_proto = skb->protocol; 343*c769accdSToshiaki Makita } 344cbe7128cSToshiaki Makita 345cbe7128cSToshiaki Makita /* now, the TCI */ 346cbe7128cSToshiaki Makita veth->h_vlan_TCI = htons(vlan_tci); 347cbe7128cSToshiaki Makita 348cbe7128cSToshiaki Makita return 0; 349cbe7128cSToshiaki Makita } 350cbe7128cSToshiaki Makita 351cbe7128cSToshiaki Makita /** 35215255a43SJiri Pirko * __vlan_insert_tag - regular VLAN tag inserting 35315255a43SJiri Pirko * @skb: skbuff to tag 35415255a43SJiri Pirko * @vlan_proto: VLAN encapsulation protocol 35515255a43SJiri Pirko * @vlan_tci: VLAN TCI to insert 35615255a43SJiri Pirko * 35715255a43SJiri Pirko * Inserts the VLAN tag into @skb as part of the payload 35815255a43SJiri Pirko * Returns error if skb_cow_head failes. 35915255a43SJiri Pirko * 36015255a43SJiri Pirko * Does not change skb->protocol so this function can be used during receive. 36115255a43SJiri Pirko */ 36215255a43SJiri Pirko static inline int __vlan_insert_tag(struct sk_buff *skb, 36315255a43SJiri Pirko __be16 vlan_proto, u16 vlan_tci) 36415255a43SJiri Pirko { 365cbe7128cSToshiaki Makita return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN); 366cbe7128cSToshiaki Makita } 36715255a43SJiri Pirko 368cbe7128cSToshiaki Makita /** 369cbe7128cSToshiaki Makita * vlan_insert_inner_tag - inner VLAN tag inserting 370cbe7128cSToshiaki Makita * @skb: skbuff to tag 371cbe7128cSToshiaki Makita * @vlan_proto: VLAN encapsulation protocol 372cbe7128cSToshiaki Makita * @vlan_tci: VLAN TCI to insert 373cbe7128cSToshiaki Makita * @mac_len: MAC header length including outer vlan headers 374cbe7128cSToshiaki Makita * 375cbe7128cSToshiaki Makita * Inserts the VLAN tag into @skb as part of the payload at offset mac_len 376cbe7128cSToshiaki Makita * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 377cbe7128cSToshiaki Makita * 378cbe7128cSToshiaki Makita * Following the skb_unshare() example, in case of error, the calling function 379cbe7128cSToshiaki Makita * doesn't have to worry about freeing the original skb. 380cbe7128cSToshiaki Makita * 381cbe7128cSToshiaki Makita * Does not change skb->protocol so this function can be used during receive. 382cbe7128cSToshiaki Makita */ 383cbe7128cSToshiaki Makita static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb, 384cbe7128cSToshiaki Makita __be16 vlan_proto, 385cbe7128cSToshiaki Makita u16 vlan_tci, 386cbe7128cSToshiaki Makita unsigned int mac_len) 387cbe7128cSToshiaki Makita { 388cbe7128cSToshiaki Makita int err; 38915255a43SJiri Pirko 390cbe7128cSToshiaki Makita err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len); 391cbe7128cSToshiaki Makita if (err) { 392cbe7128cSToshiaki Makita dev_kfree_skb_any(skb); 393cbe7128cSToshiaki Makita return NULL; 394cbe7128cSToshiaki Makita } 395cbe7128cSToshiaki Makita return skb; 39615255a43SJiri Pirko } 39715255a43SJiri Pirko 39815255a43SJiri Pirko /** 3990b5c9db1SJiri Pirko * vlan_insert_tag - regular VLAN tag inserting 4001da177e4SLinus Torvalds * @skb: skbuff to tag 40186a9bad3SPatrick McHardy * @vlan_proto: VLAN encapsulation protocol 4029bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 4031da177e4SLinus Torvalds * 4041da177e4SLinus Torvalds * Inserts the VLAN tag into @skb as part of the payload 4051da177e4SLinus Torvalds * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 4061da177e4SLinus Torvalds * 4071da177e4SLinus Torvalds * Following the skb_unshare() example, in case of error, the calling function 4081da177e4SLinus Torvalds * doesn't have to worry about freeing the original skb. 4090b5c9db1SJiri Pirko * 4100b5c9db1SJiri Pirko * Does not change skb->protocol so this function can be used during receive. 4111da177e4SLinus Torvalds */ 41286a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, 41386a9bad3SPatrick McHardy __be16 vlan_proto, u16 vlan_tci) 4141da177e4SLinus Torvalds { 415cbe7128cSToshiaki Makita return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN); 4160b5c9db1SJiri Pirko } 4171da177e4SLinus Torvalds 4180b5c9db1SJiri Pirko /** 41962749e2cSJiri Pirko * vlan_insert_tag_set_proto - regular VLAN tag inserting 4200b5c9db1SJiri Pirko * @skb: skbuff to tag 42162749e2cSJiri Pirko * @vlan_proto: VLAN encapsulation protocol 4220b5c9db1SJiri Pirko * @vlan_tci: VLAN TCI to insert 4230b5c9db1SJiri Pirko * 4240b5c9db1SJiri Pirko * Inserts the VLAN tag into @skb as part of the payload 4250b5c9db1SJiri Pirko * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 4260b5c9db1SJiri Pirko * 4270b5c9db1SJiri Pirko * Following the skb_unshare() example, in case of error, the calling function 4280b5c9db1SJiri Pirko * doesn't have to worry about freeing the original skb. 4290b5c9db1SJiri Pirko */ 43062749e2cSJiri Pirko static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb, 43162749e2cSJiri Pirko __be16 vlan_proto, 43262749e2cSJiri Pirko u16 vlan_tci) 4330b5c9db1SJiri Pirko { 43486a9bad3SPatrick McHardy skb = vlan_insert_tag(skb, vlan_proto, vlan_tci); 4350b5c9db1SJiri Pirko if (skb) 43686a9bad3SPatrick McHardy skb->protocol = vlan_proto; 4371da177e4SLinus Torvalds return skb; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4405968250cSJiri Pirko /* 4415968250cSJiri Pirko * __vlan_hwaccel_push_inside - pushes vlan tag to the payload 4425968250cSJiri Pirko * @skb: skbuff to tag 4435968250cSJiri Pirko * 4445968250cSJiri Pirko * Pushes the VLAN tag from @skb->vlan_tci inside to the payload. 4455968250cSJiri Pirko * 4465968250cSJiri Pirko * Following the skb_unshare() example, in case of error, the calling function 4475968250cSJiri Pirko * doesn't have to worry about freeing the original skb. 4485968250cSJiri Pirko */ 4495968250cSJiri Pirko static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb) 4505968250cSJiri Pirko { 4515968250cSJiri Pirko skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, 452df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 4535968250cSJiri Pirko if (likely(skb)) 4545968250cSJiri Pirko skb->vlan_tci = 0; 4555968250cSJiri Pirko return skb; 4565968250cSJiri Pirko } 4575968250cSJiri Pirko 4581da177e4SLinus Torvalds /** 4591da177e4SLinus Torvalds * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting 4601da177e4SLinus Torvalds * @skb: skbuff to tag 46186a9bad3SPatrick McHardy * @vlan_proto: VLAN encapsulation protocol 4629bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 4631da177e4SLinus Torvalds * 4646aa895b0SPatrick McHardy * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest 4651da177e4SLinus Torvalds */ 466b960a0acSJiri Pirko static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb, 467b960a0acSJiri Pirko __be16 vlan_proto, u16 vlan_tci) 4681da177e4SLinus Torvalds { 46986a9bad3SPatrick McHardy skb->vlan_proto = vlan_proto; 47005423b24SEric Dumazet skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /** 4741da177e4SLinus Torvalds * __vlan_get_tag - get the VLAN ID that is part of the payload 4751da177e4SLinus Torvalds * @skb: skbuff to query 476f4fb874cSVivien Didelot * @vlan_tci: buffer to store value 4771da177e4SLinus Torvalds * 4781da177e4SLinus Torvalds * Returns error if the skb is not of VLAN type 4791da177e4SLinus Torvalds */ 4809bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 4811da177e4SLinus Torvalds { 4821da177e4SLinus Torvalds struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data; 4831da177e4SLinus Torvalds 484fe19c4f9SEric Garver if (!eth_type_vlan(veth->h_vlan_proto)) 4851da177e4SLinus Torvalds return -EINVAL; 4861da177e4SLinus Torvalds 4879bb8582eSPatrick McHardy *vlan_tci = ntohs(veth->h_vlan_TCI); 4881da177e4SLinus Torvalds return 0; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /** 4921da177e4SLinus Torvalds * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[] 4931da177e4SLinus Torvalds * @skb: skbuff to query 494f4fb874cSVivien Didelot * @vlan_tci: buffer to store value 4951da177e4SLinus Torvalds * 4966aa895b0SPatrick McHardy * Returns error if @skb->vlan_tci is not set correctly 4971da177e4SLinus Torvalds */ 49818149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb, 4999bb8582eSPatrick McHardy u16 *vlan_tci) 5001da177e4SLinus Torvalds { 501df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 502df8a39deSJiri Pirko *vlan_tci = skb_vlan_tag_get(skb); 5031da177e4SLinus Torvalds return 0; 5041da177e4SLinus Torvalds } else { 5059bb8582eSPatrick McHardy *vlan_tci = 0; 5061da177e4SLinus Torvalds return -EINVAL; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds /** 5131da177e4SLinus Torvalds * vlan_get_tag - get the VLAN ID from the skb 5141da177e4SLinus Torvalds * @skb: skbuff to query 515f4fb874cSVivien Didelot * @vlan_tci: buffer to store value 5161da177e4SLinus Torvalds * 5171da177e4SLinus Torvalds * Returns error if the skb is not VLAN tagged 5181da177e4SLinus Torvalds */ 5199bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 5201da177e4SLinus Torvalds { 521f646968fSPatrick McHardy if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) { 5229bb8582eSPatrick McHardy return __vlan_hwaccel_get_tag(skb, vlan_tci); 5231da177e4SLinus Torvalds } else { 5249bb8582eSPatrick McHardy return __vlan_get_tag(skb, vlan_tci); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5280a85df00SHao Zheng /** 5290a85df00SHao Zheng * vlan_get_protocol - get protocol EtherType. 5300a85df00SHao Zheng * @skb: skbuff to query 531d4bcef3fSToshiaki Makita * @type: first vlan protocol 532d4bcef3fSToshiaki Makita * @depth: buffer to store length of eth and vlan tags in bytes 5330a85df00SHao Zheng * 5340a85df00SHao Zheng * Returns the EtherType of the packet, regardless of whether it is 5350a85df00SHao Zheng * vlan encapsulated (normal or hardware accelerated) or not. 5360a85df00SHao Zheng */ 537d4bcef3fSToshiaki Makita static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, 538d4bcef3fSToshiaki Makita int *depth) 5390a85df00SHao Zheng { 540d4bcef3fSToshiaki Makita unsigned int vlan_depth = skb->mac_len; 5410a85df00SHao Zheng 542d4bcef3fSToshiaki Makita /* if type is 802.1Q/AD then the header should already be 543d4bcef3fSToshiaki Makita * present at mac_len - VLAN_HLEN (if mac_len > 0), or at 544d4bcef3fSToshiaki Makita * ETH_HLEN otherwise 545d4bcef3fSToshiaki Makita */ 546fe19c4f9SEric Garver if (eth_type_vlan(type)) { 547d4bcef3fSToshiaki Makita if (vlan_depth) { 548d4bcef3fSToshiaki Makita if (WARN_ON(vlan_depth < VLAN_HLEN)) 549d4bcef3fSToshiaki Makita return 0; 550d4bcef3fSToshiaki Makita vlan_depth -= VLAN_HLEN; 551d4bcef3fSToshiaki Makita } else { 552d4bcef3fSToshiaki Makita vlan_depth = ETH_HLEN; 553d4bcef3fSToshiaki Makita } 554d4bcef3fSToshiaki Makita do { 555d4bcef3fSToshiaki Makita struct vlan_hdr *vh; 556d4bcef3fSToshiaki Makita 557d4bcef3fSToshiaki Makita if (unlikely(!pskb_may_pull(skb, 558d4bcef3fSToshiaki Makita vlan_depth + VLAN_HLEN))) 559d4bcef3fSToshiaki Makita return 0; 560d4bcef3fSToshiaki Makita 561d4bcef3fSToshiaki Makita vh = (struct vlan_hdr *)(skb->data + vlan_depth); 562d4bcef3fSToshiaki Makita type = vh->h_vlan_encapsulated_proto; 563d4bcef3fSToshiaki Makita vlan_depth += VLAN_HLEN; 564fe19c4f9SEric Garver } while (eth_type_vlan(type)); 5650a85df00SHao Zheng } 5660a85df00SHao Zheng 567d4bcef3fSToshiaki Makita if (depth) 568d4bcef3fSToshiaki Makita *depth = vlan_depth; 569d4bcef3fSToshiaki Makita 570d4bcef3fSToshiaki Makita return type; 571d4bcef3fSToshiaki Makita } 572d4bcef3fSToshiaki Makita 573d4bcef3fSToshiaki Makita /** 574d4bcef3fSToshiaki Makita * vlan_get_protocol - get protocol EtherType. 575d4bcef3fSToshiaki Makita * @skb: skbuff to query 576d4bcef3fSToshiaki Makita * 577d4bcef3fSToshiaki Makita * Returns the EtherType of the packet, regardless of whether it is 578d4bcef3fSToshiaki Makita * vlan encapsulated (normal or hardware accelerated) or not. 579d4bcef3fSToshiaki Makita */ 580d4bcef3fSToshiaki Makita static inline __be16 vlan_get_protocol(struct sk_buff *skb) 581d4bcef3fSToshiaki Makita { 582d4bcef3fSToshiaki Makita return __vlan_get_protocol(skb, skb->protocol, NULL); 5830a85df00SHao Zheng } 584396cf943SPravin B Shelar 585396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb, 586396cf943SPravin B Shelar struct vlan_hdr *vhdr) 587396cf943SPravin B Shelar { 588396cf943SPravin B Shelar __be16 proto; 589da8c8724SCong Wang unsigned short *rawp; 590396cf943SPravin B Shelar 591396cf943SPravin B Shelar /* 592396cf943SPravin B Shelar * Was a VLAN packet, grab the encapsulated protocol, which the layer 593396cf943SPravin B Shelar * three protocols care about. 594396cf943SPravin B Shelar */ 595396cf943SPravin B Shelar 596396cf943SPravin B Shelar proto = vhdr->h_vlan_encapsulated_proto; 5979545b22dSAlexander Duyck if (eth_proto_is_802_3(proto)) { 598396cf943SPravin B Shelar skb->protocol = proto; 599396cf943SPravin B Shelar return; 600396cf943SPravin B Shelar } 601396cf943SPravin B Shelar 602da8c8724SCong Wang rawp = (unsigned short *)(vhdr + 1); 603da8c8724SCong Wang if (*rawp == 0xFFFF) 604396cf943SPravin B Shelar /* 605396cf943SPravin B Shelar * This is a magic hack to spot IPX packets. Older Novell 606396cf943SPravin B Shelar * breaks the protocol design and runs IPX over 802.3 without 607396cf943SPravin B Shelar * an 802.2 LLC layer. We look for FFFF which isn't a used 608396cf943SPravin B Shelar * 802.2 SSAP/DSAP. This won't work for fault tolerant netware 609396cf943SPravin B Shelar * but does for the rest. 610396cf943SPravin B Shelar */ 611396cf943SPravin B Shelar skb->protocol = htons(ETH_P_802_3); 612396cf943SPravin B Shelar else 613396cf943SPravin B Shelar /* 614396cf943SPravin B Shelar * Real 802.2 LLC 615396cf943SPravin B Shelar */ 616396cf943SPravin B Shelar skb->protocol = htons(ETH_P_802_2); 617396cf943SPravin B Shelar } 61844a40855SVlad Yasevich 619f5a7fb88SToshiaki Makita /** 620f5a7fb88SToshiaki Makita * skb_vlan_tagged - check if skb is vlan tagged. 621f5a7fb88SToshiaki Makita * @skb: skbuff to query 622f5a7fb88SToshiaki Makita * 623f5a7fb88SToshiaki Makita * Returns true if the skb is tagged, regardless of whether it is hardware 624f5a7fb88SToshiaki Makita * accelerated or not. 625f5a7fb88SToshiaki Makita */ 626f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged(const struct sk_buff *skb) 627f5a7fb88SToshiaki Makita { 628f5a7fb88SToshiaki Makita if (!skb_vlan_tag_present(skb) && 629fe19c4f9SEric Garver likely(!eth_type_vlan(skb->protocol))) 630f5a7fb88SToshiaki Makita return false; 631f5a7fb88SToshiaki Makita 632f5a7fb88SToshiaki Makita return true; 633f5a7fb88SToshiaki Makita } 634f5a7fb88SToshiaki Makita 635f5a7fb88SToshiaki Makita /** 636f5a7fb88SToshiaki Makita * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers. 637f5a7fb88SToshiaki Makita * @skb: skbuff to query 638f5a7fb88SToshiaki Makita * 639f5a7fb88SToshiaki Makita * Returns true if the skb is tagged with multiple vlan headers, regardless 640f5a7fb88SToshiaki Makita * of whether it is hardware accelerated or not. 641f5a7fb88SToshiaki Makita */ 642f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged_multi(const struct sk_buff *skb) 643f5a7fb88SToshiaki Makita { 644f5a7fb88SToshiaki Makita __be16 protocol = skb->protocol; 645f5a7fb88SToshiaki Makita 646f5a7fb88SToshiaki Makita if (!skb_vlan_tag_present(skb)) { 647f5a7fb88SToshiaki Makita struct vlan_ethhdr *veh; 648f5a7fb88SToshiaki Makita 649fe19c4f9SEric Garver if (likely(!eth_type_vlan(protocol))) 650f5a7fb88SToshiaki Makita return false; 651f5a7fb88SToshiaki Makita 652f5a7fb88SToshiaki Makita veh = (struct vlan_ethhdr *)skb->data; 653f5a7fb88SToshiaki Makita protocol = veh->h_vlan_encapsulated_proto; 654f5a7fb88SToshiaki Makita } 655f5a7fb88SToshiaki Makita 656fe19c4f9SEric Garver if (!eth_type_vlan(protocol)) 657f5a7fb88SToshiaki Makita return false; 658f5a7fb88SToshiaki Makita 659f5a7fb88SToshiaki Makita return true; 660f5a7fb88SToshiaki Makita } 661f5a7fb88SToshiaki Makita 6628cb65d00SToshiaki Makita /** 6638cb65d00SToshiaki Makita * vlan_features_check - drop unsafe features for skb with multiple tags. 6648cb65d00SToshiaki Makita * @skb: skbuff to query 6658cb65d00SToshiaki Makita * @features: features to be checked 6668cb65d00SToshiaki Makita * 6678cb65d00SToshiaki Makita * Returns features without unsafe ones if the skb has multiple tags. 6688cb65d00SToshiaki Makita */ 6698cb65d00SToshiaki Makita static inline netdev_features_t vlan_features_check(const struct sk_buff *skb, 6708cb65d00SToshiaki Makita netdev_features_t features) 6718cb65d00SToshiaki Makita { 67235d2f80bSVlad Yasevich if (skb_vlan_tagged_multi(skb)) { 67335d2f80bSVlad Yasevich /* In the case of multi-tagged packets, use a direct mask 67435d2f80bSVlad Yasevich * instead of using netdev_interesect_features(), to make 67535d2f80bSVlad Yasevich * sure that only devices supporting NETIF_F_HW_CSUM will 67635d2f80bSVlad Yasevich * have checksum offloading support. 67735d2f80bSVlad Yasevich */ 67835d2f80bSVlad Yasevich features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | 67935d2f80bSVlad Yasevich NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX | 68035d2f80bSVlad Yasevich NETIF_F_HW_VLAN_STAG_TX; 68135d2f80bSVlad Yasevich } 6828cb65d00SToshiaki Makita 6838cb65d00SToshiaki Makita return features; 6848cb65d00SToshiaki Makita } 6858cb65d00SToshiaki Makita 68666e5133fSToshiaki Makita /** 68766e5133fSToshiaki Makita * compare_vlan_header - Compare two vlan headers 68866e5133fSToshiaki Makita * @h1: Pointer to vlan header 68966e5133fSToshiaki Makita * @h2: Pointer to vlan header 69066e5133fSToshiaki Makita * 69166e5133fSToshiaki Makita * Compare two vlan headers, returns 0 if equal. 69266e5133fSToshiaki Makita * 69366e5133fSToshiaki Makita * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits. 69466e5133fSToshiaki Makita */ 69566e5133fSToshiaki Makita static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1, 69666e5133fSToshiaki Makita const struct vlan_hdr *h2) 69766e5133fSToshiaki Makita { 69866e5133fSToshiaki Makita #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 69966e5133fSToshiaki Makita return *(u32 *)h1 ^ *(u32 *)h2; 70066e5133fSToshiaki Makita #else 70166e5133fSToshiaki Makita return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) | 70266e5133fSToshiaki Makita ((__force u32)h1->h_vlan_encapsulated_proto ^ 70366e5133fSToshiaki Makita (__force u32)h2->h_vlan_encapsulated_proto); 70466e5133fSToshiaki Makita #endif 70566e5133fSToshiaki Makita } 7061da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */ 707