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 76d38569abSVlad Yasevich static inline bool is_vlan_dev(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) 841da177e4SLinus Torvalds 85e267cb96SDavid S. Miller /** 86e267cb96SDavid S. Miller * struct vlan_pcpu_stats - VLAN percpu rx/tx stats 87e267cb96SDavid S. Miller * @rx_packets: number of received packets 88e267cb96SDavid S. Miller * @rx_bytes: number of received bytes 89e267cb96SDavid S. Miller * @rx_multicast: number of received multicast packets 90e267cb96SDavid S. Miller * @tx_packets: number of transmitted packets 91e267cb96SDavid S. Miller * @tx_bytes: number of transmitted bytes 92e267cb96SDavid S. Miller * @syncp: synchronization point for 64bit counters 93e267cb96SDavid S. Miller * @rx_errors: number of rx errors 94e267cb96SDavid S. Miller * @tx_dropped: number of tx drops 95e267cb96SDavid S. Miller */ 96e267cb96SDavid S. Miller struct vlan_pcpu_stats { 97e267cb96SDavid S. Miller u64 rx_packets; 98e267cb96SDavid S. Miller u64 rx_bytes; 99e267cb96SDavid S. Miller u64 rx_multicast; 100e267cb96SDavid S. Miller u64 tx_packets; 101e267cb96SDavid S. Miller u64 tx_bytes; 102e267cb96SDavid S. Miller struct u64_stats_sync syncp; 103e267cb96SDavid S. Miller u32 rx_errors; 104e267cb96SDavid S. Miller u32 tx_dropped; 105e267cb96SDavid S. Miller }; 106e267cb96SDavid S. Miller 1071e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE) 1081e85c9b6SHannes Frederic Sowa 109f06c7f9fSdingtianhong extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev, 1101e85c9b6SHannes Frederic Sowa __be16 vlan_proto, u16 vlan_id); 1111e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev); 1121e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev); 11371e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev); 1141e85c9b6SHannes Frederic Sowa 1151e85c9b6SHannes Frederic Sowa /** 1161e85c9b6SHannes Frederic Sowa * struct vlan_priority_tci_mapping - vlan egress priority mappings 1171e85c9b6SHannes Frederic Sowa * @priority: skb priority 1181e85c9b6SHannes Frederic Sowa * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000 1191e85c9b6SHannes Frederic Sowa * @next: pointer to next struct 1201e85c9b6SHannes Frederic Sowa */ 1211e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping { 1221e85c9b6SHannes Frederic Sowa u32 priority; 1231e85c9b6SHannes Frederic Sowa u16 vlan_qos; 1241e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping *next; 1251e85c9b6SHannes Frederic Sowa }; 1261e85c9b6SHannes Frederic Sowa 127e267cb96SDavid S. Miller struct proc_dir_entry; 128e267cb96SDavid S. Miller struct netpoll; 129e267cb96SDavid S. Miller 130e267cb96SDavid S. Miller /** 131e267cb96SDavid S. Miller * struct vlan_dev_priv - VLAN private device data 132e267cb96SDavid S. Miller * @nr_ingress_mappings: number of ingress priority mappings 133e267cb96SDavid S. Miller * @ingress_priority_map: ingress priority mappings 134e267cb96SDavid S. Miller * @nr_egress_mappings: number of egress priority mappings 135e267cb96SDavid S. Miller * @egress_priority_map: hash of egress priority mappings 136e267cb96SDavid S. Miller * @vlan_proto: VLAN encapsulation protocol 137e267cb96SDavid S. Miller * @vlan_id: VLAN identifier 138e267cb96SDavid S. Miller * @flags: device flags 139e267cb96SDavid S. Miller * @real_dev: underlying netdevice 140e267cb96SDavid S. Miller * @real_dev_addr: address of underlying netdevice 141e267cb96SDavid S. Miller * @dent: proc dir entry 142e267cb96SDavid S. Miller * @vlan_pcpu_stats: ptr to percpu rx stats 143e267cb96SDavid S. Miller */ 144e267cb96SDavid S. Miller struct vlan_dev_priv { 145e267cb96SDavid S. Miller unsigned int nr_ingress_mappings; 146e267cb96SDavid S. Miller u32 ingress_priority_map[8]; 147e267cb96SDavid S. Miller unsigned int nr_egress_mappings; 148e267cb96SDavid S. Miller struct vlan_priority_tci_mapping *egress_priority_map[16]; 149e267cb96SDavid S. Miller 150e267cb96SDavid S. Miller __be16 vlan_proto; 151e267cb96SDavid S. Miller u16 vlan_id; 152e267cb96SDavid S. Miller u16 flags; 153e267cb96SDavid S. Miller 154e267cb96SDavid S. Miller struct net_device *real_dev; 155e267cb96SDavid S. Miller unsigned char real_dev_addr[ETH_ALEN]; 156e267cb96SDavid S. Miller 157e267cb96SDavid S. Miller struct proc_dir_entry *dent; 158e267cb96SDavid S. Miller struct vlan_pcpu_stats __percpu *vlan_pcpu_stats; 159e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER 160e267cb96SDavid S. Miller struct netpoll *netpoll; 161e267cb96SDavid S. Miller #endif 162d38569abSVlad Yasevich unsigned int nest_level; 163e267cb96SDavid S. Miller }; 164e267cb96SDavid S. Miller 165e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev) 166e267cb96SDavid S. Miller { 167e267cb96SDavid S. Miller return netdev_priv(dev); 168e267cb96SDavid S. Miller } 169e267cb96SDavid S. Miller 170e267cb96SDavid S. Miller static inline u16 171e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio) 172e267cb96SDavid S. Miller { 173e267cb96SDavid S. Miller struct vlan_priority_tci_mapping *mp; 174e267cb96SDavid S. Miller 175e267cb96SDavid S. Miller smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */ 176e267cb96SDavid S. Miller 177e267cb96SDavid S. Miller mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)]; 178e267cb96SDavid S. Miller while (mp) { 179e267cb96SDavid S. Miller if (mp->priority == skprio) { 180e267cb96SDavid S. Miller return mp->vlan_qos; /* This should already be shifted 181e267cb96SDavid S. Miller * to mask correctly with the 182e267cb96SDavid S. Miller * VLAN's TCI */ 183e267cb96SDavid S. Miller } 184e267cb96SDavid S. Miller mp = mp->next; 185e267cb96SDavid S. Miller } 186e267cb96SDavid S. Miller return 0; 187e267cb96SDavid S. Miller } 188e267cb96SDavid S. Miller 18948cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb); 1909b22ea56SPatrick McHardy 19180d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid); 19280d5c368SPatrick McHardy extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid); 19387002b03SJiri Pirko 194348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev, 195348a1443SJiri Pirko const struct net_device *by_dev); 196348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev, 197348a1443SJiri Pirko const struct net_device *by_dev); 1989b361c13SJiri Pirko 1999b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev); 200e1618d46SVlad Yasevich 201e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev) 202e1618d46SVlad Yasevich { 203e1618d46SVlad Yasevich BUG_ON(!is_vlan_dev(dev)); 204e1618d46SVlad Yasevich return vlan_dev_priv(dev)->nest_level; 205e1618d46SVlad Yasevich } 2067750f403SPatrick McHardy #else 207cec9c133SJiri Pirko static inline struct net_device * 208f06c7f9fSdingtianhong __vlan_find_dev_deep_rcu(struct net_device *real_dev, 2099fae27b3SPatrick McHardy __be16 vlan_proto, u16 vlan_id) 210cec9c133SJiri Pirko { 211cec9c133SJiri Pirko return NULL; 212cec9c133SJiri Pirko } 213cec9c133SJiri Pirko 21422d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev) 21522d1ba74SPatrick McHardy { 21622d1ba74SPatrick McHardy BUG(); 21722d1ba74SPatrick McHardy return NULL; 21822d1ba74SPatrick McHardy } 21922d1ba74SPatrick McHardy 22022d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev) 22122d1ba74SPatrick McHardy { 22222d1ba74SPatrick McHardy BUG(); 22322d1ba74SPatrick McHardy return 0; 22422d1ba74SPatrick McHardy } 22522d1ba74SPatrick McHardy 22671e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev) 22771e415e4Sdingtianhong { 22871e415e4Sdingtianhong BUG(); 22971e415e4Sdingtianhong return 0; 23071e415e4Sdingtianhong } 23171e415e4Sdingtianhong 232d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev, 233d3243539SEyal Perry u32 skprio) 234d3243539SEyal Perry { 235d3243539SEyal Perry return 0; 236d3243539SEyal Perry } 237d3243539SEyal Perry 23848cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb) 2399b22ea56SPatrick McHardy { 2403701e513SJesse Gross return false; 2419b22ea56SPatrick McHardy } 242e1c096e2SHerbert Xu 2439fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid) 24487002b03SJiri Pirko { 24587002b03SJiri Pirko return 0; 24687002b03SJiri Pirko } 24787002b03SJiri Pirko 2489fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid) 24987002b03SJiri Pirko { 25087002b03SJiri Pirko } 251348a1443SJiri Pirko 252348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev, 253348a1443SJiri Pirko const struct net_device *by_dev) 254348a1443SJiri Pirko { 255348a1443SJiri Pirko return 0; 256348a1443SJiri Pirko } 257348a1443SJiri Pirko 258348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev, 259348a1443SJiri Pirko const struct net_device *by_dev) 260348a1443SJiri Pirko { 261348a1443SJiri Pirko } 2629b361c13SJiri Pirko 2639b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev) 2649b361c13SJiri Pirko { 2659b361c13SJiri Pirko return false; 2669b361c13SJiri Pirko } 267e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev) 268e1618d46SVlad Yasevich { 269e1618d46SVlad Yasevich BUG(); 270e1618d46SVlad Yasevich return 0; 271e1618d46SVlad Yasevich } 2727750f403SPatrick McHardy #endif 2731da177e4SLinus Torvalds 27486a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features, 27586a9bad3SPatrick McHardy __be16 proto) 27686a9bad3SPatrick McHardy { 27786a9bad3SPatrick McHardy if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX) 27886a9bad3SPatrick McHardy return true; 2798ad227ffSPatrick McHardy if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX) 2808ad227ffSPatrick McHardy return true; 28186a9bad3SPatrick McHardy return false; 28286a9bad3SPatrick McHardy } 28386a9bad3SPatrick McHardy 2849bb8582eSPatrick McHardy /** 28515255a43SJiri Pirko * __vlan_insert_tag - regular VLAN tag inserting 28615255a43SJiri Pirko * @skb: skbuff to tag 28715255a43SJiri Pirko * @vlan_proto: VLAN encapsulation protocol 28815255a43SJiri Pirko * @vlan_tci: VLAN TCI to insert 28915255a43SJiri Pirko * 29015255a43SJiri Pirko * Inserts the VLAN tag into @skb as part of the payload 29115255a43SJiri Pirko * Returns error if skb_cow_head failes. 29215255a43SJiri Pirko * 29315255a43SJiri Pirko * Does not change skb->protocol so this function can be used during receive. 29415255a43SJiri Pirko */ 29515255a43SJiri Pirko static inline int __vlan_insert_tag(struct sk_buff *skb, 29615255a43SJiri Pirko __be16 vlan_proto, u16 vlan_tci) 29715255a43SJiri Pirko { 29815255a43SJiri Pirko struct vlan_ethhdr *veth; 29915255a43SJiri Pirko 30015255a43SJiri Pirko if (skb_cow_head(skb, VLAN_HLEN) < 0) 30115255a43SJiri Pirko return -ENOMEM; 30215255a43SJiri Pirko 30315255a43SJiri Pirko veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN); 30415255a43SJiri Pirko 30515255a43SJiri Pirko /* Move the mac addresses to the beginning of the new header. */ 30615255a43SJiri Pirko memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN); 30715255a43SJiri Pirko skb->mac_header -= VLAN_HLEN; 30815255a43SJiri Pirko 30915255a43SJiri Pirko /* first, the ethernet type */ 31015255a43SJiri Pirko veth->h_vlan_proto = vlan_proto; 31115255a43SJiri Pirko 31215255a43SJiri Pirko /* now, the TCI */ 31315255a43SJiri Pirko veth->h_vlan_TCI = htons(vlan_tci); 31415255a43SJiri Pirko 31515255a43SJiri Pirko return 0; 31615255a43SJiri Pirko } 31715255a43SJiri Pirko 31815255a43SJiri Pirko /** 3190b5c9db1SJiri Pirko * vlan_insert_tag - regular VLAN tag inserting 3201da177e4SLinus Torvalds * @skb: skbuff to tag 32186a9bad3SPatrick McHardy * @vlan_proto: VLAN encapsulation protocol 3229bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 3231da177e4SLinus Torvalds * 3241da177e4SLinus Torvalds * Inserts the VLAN tag into @skb as part of the payload 3251da177e4SLinus Torvalds * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 3261da177e4SLinus Torvalds * 3271da177e4SLinus Torvalds * Following the skb_unshare() example, in case of error, the calling function 3281da177e4SLinus Torvalds * doesn't have to worry about freeing the original skb. 3290b5c9db1SJiri Pirko * 3300b5c9db1SJiri Pirko * Does not change skb->protocol so this function can be used during receive. 3311da177e4SLinus Torvalds */ 33286a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, 33386a9bad3SPatrick McHardy __be16 vlan_proto, u16 vlan_tci) 3341da177e4SLinus Torvalds { 33515255a43SJiri Pirko int err; 3361da177e4SLinus Torvalds 33715255a43SJiri Pirko err = __vlan_insert_tag(skb, vlan_proto, vlan_tci); 33815255a43SJiri Pirko if (err) { 339fb7c03dfSEric W. Biederman dev_kfree_skb_any(skb); 3401da177e4SLinus Torvalds return NULL; 3411da177e4SLinus Torvalds } 3420b5c9db1SJiri Pirko return skb; 3430b5c9db1SJiri Pirko } 3441da177e4SLinus Torvalds 3450b5c9db1SJiri Pirko /** 34662749e2cSJiri Pirko * vlan_insert_tag_set_proto - regular VLAN tag inserting 3470b5c9db1SJiri Pirko * @skb: skbuff to tag 34862749e2cSJiri Pirko * @vlan_proto: VLAN encapsulation protocol 3490b5c9db1SJiri Pirko * @vlan_tci: VLAN TCI to insert 3500b5c9db1SJiri Pirko * 3510b5c9db1SJiri Pirko * Inserts the VLAN tag into @skb as part of the payload 3520b5c9db1SJiri Pirko * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 3530b5c9db1SJiri Pirko * 3540b5c9db1SJiri Pirko * Following the skb_unshare() example, in case of error, the calling function 3550b5c9db1SJiri Pirko * doesn't have to worry about freeing the original skb. 3560b5c9db1SJiri Pirko */ 35762749e2cSJiri Pirko static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb, 35862749e2cSJiri Pirko __be16 vlan_proto, 35962749e2cSJiri Pirko u16 vlan_tci) 3600b5c9db1SJiri Pirko { 36186a9bad3SPatrick McHardy skb = vlan_insert_tag(skb, vlan_proto, vlan_tci); 3620b5c9db1SJiri Pirko if (skb) 36386a9bad3SPatrick McHardy skb->protocol = vlan_proto; 3641da177e4SLinus Torvalds return skb; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3675968250cSJiri Pirko /* 3685968250cSJiri Pirko * __vlan_hwaccel_push_inside - pushes vlan tag to the payload 3695968250cSJiri Pirko * @skb: skbuff to tag 3705968250cSJiri Pirko * 3715968250cSJiri Pirko * Pushes the VLAN tag from @skb->vlan_tci inside to the payload. 3725968250cSJiri Pirko * 3735968250cSJiri Pirko * Following the skb_unshare() example, in case of error, the calling function 3745968250cSJiri Pirko * doesn't have to worry about freeing the original skb. 3755968250cSJiri Pirko */ 3765968250cSJiri Pirko static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb) 3775968250cSJiri Pirko { 3785968250cSJiri Pirko skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, 379df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 3805968250cSJiri Pirko if (likely(skb)) 3815968250cSJiri Pirko skb->vlan_tci = 0; 3825968250cSJiri Pirko return skb; 3835968250cSJiri Pirko } 3845968250cSJiri Pirko /* 3855968250cSJiri Pirko * vlan_hwaccel_push_inside - pushes vlan tag to the payload 3865968250cSJiri Pirko * @skb: skbuff to tag 3875968250cSJiri Pirko * 3885968250cSJiri Pirko * Checks is tag is present in @skb->vlan_tci and if it is, it pushes the 3895968250cSJiri Pirko * VLAN tag from @skb->vlan_tci inside to the payload. 3905968250cSJiri Pirko * 3915968250cSJiri Pirko * Following the skb_unshare() example, in case of error, the calling function 3925968250cSJiri Pirko * doesn't have to worry about freeing the original skb. 3935968250cSJiri Pirko */ 3945968250cSJiri Pirko static inline struct sk_buff *vlan_hwaccel_push_inside(struct sk_buff *skb) 3955968250cSJiri Pirko { 396df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) 3975968250cSJiri Pirko skb = __vlan_hwaccel_push_inside(skb); 3985968250cSJiri Pirko return skb; 3995968250cSJiri Pirko } 4005968250cSJiri Pirko 4011da177e4SLinus Torvalds /** 4021da177e4SLinus Torvalds * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting 4031da177e4SLinus Torvalds * @skb: skbuff to tag 40486a9bad3SPatrick McHardy * @vlan_proto: VLAN encapsulation protocol 4059bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 4061da177e4SLinus Torvalds * 4076aa895b0SPatrick McHardy * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest 4081da177e4SLinus Torvalds */ 409b960a0acSJiri Pirko static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb, 410b960a0acSJiri Pirko __be16 vlan_proto, u16 vlan_tci) 4111da177e4SLinus Torvalds { 41286a9bad3SPatrick McHardy skb->vlan_proto = vlan_proto; 41305423b24SEric Dumazet skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /** 4171da177e4SLinus Torvalds * __vlan_get_tag - get the VLAN ID that is part of the payload 4181da177e4SLinus Torvalds * @skb: skbuff to query 419*f4fb874cSVivien Didelot * @vlan_tci: buffer to store value 4201da177e4SLinus Torvalds * 4211da177e4SLinus Torvalds * Returns error if the skb is not of VLAN type 4221da177e4SLinus Torvalds */ 4239bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data; 4261da177e4SLinus Torvalds 4278ad227ffSPatrick McHardy if (veth->h_vlan_proto != htons(ETH_P_8021Q) && 4288ad227ffSPatrick McHardy veth->h_vlan_proto != htons(ETH_P_8021AD)) 4291da177e4SLinus Torvalds return -EINVAL; 4301da177e4SLinus Torvalds 4319bb8582eSPatrick McHardy *vlan_tci = ntohs(veth->h_vlan_TCI); 4321da177e4SLinus Torvalds return 0; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds /** 4361da177e4SLinus Torvalds * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[] 4371da177e4SLinus Torvalds * @skb: skbuff to query 438*f4fb874cSVivien Didelot * @vlan_tci: buffer to store value 4391da177e4SLinus Torvalds * 4406aa895b0SPatrick McHardy * Returns error if @skb->vlan_tci is not set correctly 4411da177e4SLinus Torvalds */ 44218149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb, 4439bb8582eSPatrick McHardy u16 *vlan_tci) 4441da177e4SLinus Torvalds { 445df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 446df8a39deSJiri Pirko *vlan_tci = skb_vlan_tag_get(skb); 4471da177e4SLinus Torvalds return 0; 4481da177e4SLinus Torvalds } else { 4499bb8582eSPatrick McHardy *vlan_tci = 0; 4501da177e4SLinus Torvalds return -EINVAL; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /** 4571da177e4SLinus Torvalds * vlan_get_tag - get the VLAN ID from the skb 4581da177e4SLinus Torvalds * @skb: skbuff to query 459*f4fb874cSVivien Didelot * @vlan_tci: buffer to store value 4601da177e4SLinus Torvalds * 4611da177e4SLinus Torvalds * Returns error if the skb is not VLAN tagged 4621da177e4SLinus Torvalds */ 4639bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 4641da177e4SLinus Torvalds { 465f646968fSPatrick McHardy if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) { 4669bb8582eSPatrick McHardy return __vlan_hwaccel_get_tag(skb, vlan_tci); 4671da177e4SLinus Torvalds } else { 4689bb8582eSPatrick McHardy return __vlan_get_tag(skb, vlan_tci); 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4720a85df00SHao Zheng /** 4730a85df00SHao Zheng * vlan_get_protocol - get protocol EtherType. 4740a85df00SHao Zheng * @skb: skbuff to query 475d4bcef3fSToshiaki Makita * @type: first vlan protocol 476d4bcef3fSToshiaki Makita * @depth: buffer to store length of eth and vlan tags in bytes 4770a85df00SHao Zheng * 4780a85df00SHao Zheng * Returns the EtherType of the packet, regardless of whether it is 4790a85df00SHao Zheng * vlan encapsulated (normal or hardware accelerated) or not. 4800a85df00SHao Zheng */ 481d4bcef3fSToshiaki Makita static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, 482d4bcef3fSToshiaki Makita int *depth) 4830a85df00SHao Zheng { 484d4bcef3fSToshiaki Makita unsigned int vlan_depth = skb->mac_len; 4850a85df00SHao Zheng 486d4bcef3fSToshiaki Makita /* if type is 802.1Q/AD then the header should already be 487d4bcef3fSToshiaki Makita * present at mac_len - VLAN_HLEN (if mac_len > 0), or at 488d4bcef3fSToshiaki Makita * ETH_HLEN otherwise 489d4bcef3fSToshiaki Makita */ 490d4bcef3fSToshiaki Makita if (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) { 491d4bcef3fSToshiaki Makita if (vlan_depth) { 492d4bcef3fSToshiaki Makita if (WARN_ON(vlan_depth < VLAN_HLEN)) 493d4bcef3fSToshiaki Makita return 0; 494d4bcef3fSToshiaki Makita vlan_depth -= VLAN_HLEN; 495d4bcef3fSToshiaki Makita } else { 496d4bcef3fSToshiaki Makita vlan_depth = ETH_HLEN; 497d4bcef3fSToshiaki Makita } 498d4bcef3fSToshiaki Makita do { 499d4bcef3fSToshiaki Makita struct vlan_hdr *vh; 500d4bcef3fSToshiaki Makita 501d4bcef3fSToshiaki Makita if (unlikely(!pskb_may_pull(skb, 502d4bcef3fSToshiaki Makita vlan_depth + VLAN_HLEN))) 503d4bcef3fSToshiaki Makita return 0; 504d4bcef3fSToshiaki Makita 505d4bcef3fSToshiaki Makita vh = (struct vlan_hdr *)(skb->data + vlan_depth); 506d4bcef3fSToshiaki Makita type = vh->h_vlan_encapsulated_proto; 507d4bcef3fSToshiaki Makita vlan_depth += VLAN_HLEN; 508d4bcef3fSToshiaki Makita } while (type == htons(ETH_P_8021Q) || 509d4bcef3fSToshiaki Makita type == htons(ETH_P_8021AD)); 5100a85df00SHao Zheng } 5110a85df00SHao Zheng 512d4bcef3fSToshiaki Makita if (depth) 513d4bcef3fSToshiaki Makita *depth = vlan_depth; 514d4bcef3fSToshiaki Makita 515d4bcef3fSToshiaki Makita return type; 516d4bcef3fSToshiaki Makita } 517d4bcef3fSToshiaki Makita 518d4bcef3fSToshiaki Makita /** 519d4bcef3fSToshiaki Makita * vlan_get_protocol - get protocol EtherType. 520d4bcef3fSToshiaki Makita * @skb: skbuff to query 521d4bcef3fSToshiaki Makita * 522d4bcef3fSToshiaki Makita * Returns the EtherType of the packet, regardless of whether it is 523d4bcef3fSToshiaki Makita * vlan encapsulated (normal or hardware accelerated) or not. 524d4bcef3fSToshiaki Makita */ 525d4bcef3fSToshiaki Makita static inline __be16 vlan_get_protocol(struct sk_buff *skb) 526d4bcef3fSToshiaki Makita { 527d4bcef3fSToshiaki Makita return __vlan_get_protocol(skb, skb->protocol, NULL); 5280a85df00SHao Zheng } 529396cf943SPravin B Shelar 530396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb, 531396cf943SPravin B Shelar struct vlan_hdr *vhdr) 532396cf943SPravin B Shelar { 533396cf943SPravin B Shelar __be16 proto; 534da8c8724SCong Wang unsigned short *rawp; 535396cf943SPravin B Shelar 536396cf943SPravin B Shelar /* 537396cf943SPravin B Shelar * Was a VLAN packet, grab the encapsulated protocol, which the layer 538396cf943SPravin B Shelar * three protocols care about. 539396cf943SPravin B Shelar */ 540396cf943SPravin B Shelar 541396cf943SPravin B Shelar proto = vhdr->h_vlan_encapsulated_proto; 5429545b22dSAlexander Duyck if (eth_proto_is_802_3(proto)) { 543396cf943SPravin B Shelar skb->protocol = proto; 544396cf943SPravin B Shelar return; 545396cf943SPravin B Shelar } 546396cf943SPravin B Shelar 547da8c8724SCong Wang rawp = (unsigned short *)(vhdr + 1); 548da8c8724SCong Wang if (*rawp == 0xFFFF) 549396cf943SPravin B Shelar /* 550396cf943SPravin B Shelar * This is a magic hack to spot IPX packets. Older Novell 551396cf943SPravin B Shelar * breaks the protocol design and runs IPX over 802.3 without 552396cf943SPravin B Shelar * an 802.2 LLC layer. We look for FFFF which isn't a used 553396cf943SPravin B Shelar * 802.2 SSAP/DSAP. This won't work for fault tolerant netware 554396cf943SPravin B Shelar * but does for the rest. 555396cf943SPravin B Shelar */ 556396cf943SPravin B Shelar skb->protocol = htons(ETH_P_802_3); 557396cf943SPravin B Shelar else 558396cf943SPravin B Shelar /* 559396cf943SPravin B Shelar * Real 802.2 LLC 560396cf943SPravin B Shelar */ 561396cf943SPravin B Shelar skb->protocol = htons(ETH_P_802_2); 562396cf943SPravin B Shelar } 56344a40855SVlad Yasevich 564f5a7fb88SToshiaki Makita /** 565f5a7fb88SToshiaki Makita * skb_vlan_tagged - check if skb is vlan tagged. 566f5a7fb88SToshiaki Makita * @skb: skbuff to query 567f5a7fb88SToshiaki Makita * 568f5a7fb88SToshiaki Makita * Returns true if the skb is tagged, regardless of whether it is hardware 569f5a7fb88SToshiaki Makita * accelerated or not. 570f5a7fb88SToshiaki Makita */ 571f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged(const struct sk_buff *skb) 572f5a7fb88SToshiaki Makita { 573f5a7fb88SToshiaki Makita if (!skb_vlan_tag_present(skb) && 574f5a7fb88SToshiaki Makita likely(skb->protocol != htons(ETH_P_8021Q) && 575f5a7fb88SToshiaki Makita skb->protocol != htons(ETH_P_8021AD))) 576f5a7fb88SToshiaki Makita return false; 577f5a7fb88SToshiaki Makita 578f5a7fb88SToshiaki Makita return true; 579f5a7fb88SToshiaki Makita } 580f5a7fb88SToshiaki Makita 581f5a7fb88SToshiaki Makita /** 582f5a7fb88SToshiaki Makita * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers. 583f5a7fb88SToshiaki Makita * @skb: skbuff to query 584f5a7fb88SToshiaki Makita * 585f5a7fb88SToshiaki Makita * Returns true if the skb is tagged with multiple vlan headers, regardless 586f5a7fb88SToshiaki Makita * of whether it is hardware accelerated or not. 587f5a7fb88SToshiaki Makita */ 588f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged_multi(const struct sk_buff *skb) 589f5a7fb88SToshiaki Makita { 590f5a7fb88SToshiaki Makita __be16 protocol = skb->protocol; 591f5a7fb88SToshiaki Makita 592f5a7fb88SToshiaki Makita if (!skb_vlan_tag_present(skb)) { 593f5a7fb88SToshiaki Makita struct vlan_ethhdr *veh; 594f5a7fb88SToshiaki Makita 595f5a7fb88SToshiaki Makita if (likely(protocol != htons(ETH_P_8021Q) && 596f5a7fb88SToshiaki Makita protocol != htons(ETH_P_8021AD))) 597f5a7fb88SToshiaki Makita return false; 598f5a7fb88SToshiaki Makita 599f5a7fb88SToshiaki Makita veh = (struct vlan_ethhdr *)skb->data; 600f5a7fb88SToshiaki Makita protocol = veh->h_vlan_encapsulated_proto; 601f5a7fb88SToshiaki Makita } 602f5a7fb88SToshiaki Makita 603f5a7fb88SToshiaki Makita if (protocol != htons(ETH_P_8021Q) && protocol != htons(ETH_P_8021AD)) 604f5a7fb88SToshiaki Makita return false; 605f5a7fb88SToshiaki Makita 606f5a7fb88SToshiaki Makita return true; 607f5a7fb88SToshiaki Makita } 608f5a7fb88SToshiaki Makita 6098cb65d00SToshiaki Makita /** 6108cb65d00SToshiaki Makita * vlan_features_check - drop unsafe features for skb with multiple tags. 6118cb65d00SToshiaki Makita * @skb: skbuff to query 6128cb65d00SToshiaki Makita * @features: features to be checked 6138cb65d00SToshiaki Makita * 6148cb65d00SToshiaki Makita * Returns features without unsafe ones if the skb has multiple tags. 6158cb65d00SToshiaki Makita */ 6168cb65d00SToshiaki Makita static inline netdev_features_t vlan_features_check(const struct sk_buff *skb, 6178cb65d00SToshiaki Makita netdev_features_t features) 6188cb65d00SToshiaki Makita { 6198cb65d00SToshiaki Makita if (skb_vlan_tagged_multi(skb)) 6208cb65d00SToshiaki Makita features = netdev_intersect_features(features, 6218cb65d00SToshiaki Makita NETIF_F_SG | 6228cb65d00SToshiaki Makita NETIF_F_HIGHDMA | 6238cb65d00SToshiaki Makita NETIF_F_FRAGLIST | 6248cb65d00SToshiaki Makita NETIF_F_GEN_CSUM | 6258cb65d00SToshiaki Makita NETIF_F_HW_VLAN_CTAG_TX | 6268cb65d00SToshiaki Makita NETIF_F_HW_VLAN_STAG_TX); 6278cb65d00SToshiaki Makita 6288cb65d00SToshiaki Makita return features; 6298cb65d00SToshiaki Makita } 6308cb65d00SToshiaki Makita 6311da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */ 632