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> 19*607ca46eSDavid 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 47740c15d0SPatrick McHardy * @h_vlan_proto: ethernet protocol (always 0x8100) 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 766dcbbe25SNeil Horman static inline int is_vlan_dev(struct net_device *dev) 776dcbbe25SNeil Horman { 786dcbbe25SNeil Horman return dev->priv_flags & IFF_802_1Q_VLAN; 796dcbbe25SNeil Horman } 806dcbbe25SNeil Horman 8105423b24SEric Dumazet #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT) 8248cc32d3SFlorian Zumbiehl #define vlan_tx_nonzero_tag_present(__skb) \ 8348cc32d3SFlorian Zumbiehl (vlan_tx_tag_present(__skb) && ((__skb)->vlan_tci & VLAN_VID_MASK)) 8405423b24SEric Dumazet #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT) 851da177e4SLinus Torvalds 867750f403SPatrick McHardy #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE) 8765ac6a5fSJesse Gross 88cec9c133SJiri Pirko extern struct net_device *__vlan_find_dev_deep(struct net_device *real_dev, 89cec9c133SJiri Pirko u16 vlan_id); 9022d1ba74SPatrick McHardy extern struct net_device *vlan_dev_real_dev(const struct net_device *dev); 9122d1ba74SPatrick McHardy extern u16 vlan_dev_vlan_id(const struct net_device *dev); 9222d1ba74SPatrick McHardy 9348cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb); 94bcc6d479SJiri Pirko extern struct sk_buff *vlan_untag(struct sk_buff *skb); 959b22ea56SPatrick McHardy 9687002b03SJiri Pirko extern int vlan_vid_add(struct net_device *dev, unsigned short vid); 9787002b03SJiri Pirko extern void vlan_vid_del(struct net_device *dev, unsigned short vid); 9887002b03SJiri Pirko 99348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev, 100348a1443SJiri Pirko const struct net_device *by_dev); 101348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev, 102348a1443SJiri Pirko const struct net_device *by_dev); 1039b361c13SJiri Pirko 1049b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev); 1057750f403SPatrick McHardy #else 106cec9c133SJiri Pirko static inline struct net_device * 107cec9c133SJiri Pirko __vlan_find_dev_deep(struct net_device *real_dev, u16 vlan_id) 108cec9c133SJiri Pirko { 109cec9c133SJiri Pirko return NULL; 110cec9c133SJiri Pirko } 111cec9c133SJiri Pirko 11222d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev) 11322d1ba74SPatrick McHardy { 11422d1ba74SPatrick McHardy BUG(); 11522d1ba74SPatrick McHardy return NULL; 11622d1ba74SPatrick McHardy } 11722d1ba74SPatrick McHardy 11822d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev) 11922d1ba74SPatrick McHardy { 12022d1ba74SPatrick McHardy BUG(); 12122d1ba74SPatrick McHardy return 0; 12222d1ba74SPatrick McHardy } 12322d1ba74SPatrick McHardy 12448cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb) 1259b22ea56SPatrick McHardy { 1263701e513SJesse Gross return false; 1279b22ea56SPatrick McHardy } 128e1c096e2SHerbert Xu 1296139e75fSDavid S. Miller static inline struct sk_buff *vlan_untag(struct sk_buff *skb) 130bcc6d479SJiri Pirko { 131bcc6d479SJiri Pirko return skb; 132bcc6d479SJiri Pirko } 13387002b03SJiri Pirko 13487002b03SJiri Pirko static inline int vlan_vid_add(struct net_device *dev, unsigned short vid) 13587002b03SJiri Pirko { 13687002b03SJiri Pirko return 0; 13787002b03SJiri Pirko } 13887002b03SJiri Pirko 13987002b03SJiri Pirko static inline void vlan_vid_del(struct net_device *dev, unsigned short vid) 14087002b03SJiri Pirko { 14187002b03SJiri Pirko } 142348a1443SJiri Pirko 143348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev, 144348a1443SJiri Pirko const struct net_device *by_dev) 145348a1443SJiri Pirko { 146348a1443SJiri Pirko return 0; 147348a1443SJiri Pirko } 148348a1443SJiri Pirko 149348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev, 150348a1443SJiri Pirko const struct net_device *by_dev) 151348a1443SJiri Pirko { 152348a1443SJiri Pirko } 1539b361c13SJiri Pirko 1549b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev) 1559b361c13SJiri Pirko { 1569b361c13SJiri Pirko return false; 1579b361c13SJiri Pirko } 1587750f403SPatrick McHardy #endif 1591da177e4SLinus Torvalds 1609bb8582eSPatrick McHardy /** 1610b5c9db1SJiri Pirko * vlan_insert_tag - regular VLAN tag inserting 1621da177e4SLinus Torvalds * @skb: skbuff to tag 1639bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 1641da177e4SLinus Torvalds * 1651da177e4SLinus Torvalds * Inserts the VLAN tag into @skb as part of the payload 1661da177e4SLinus Torvalds * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 1671da177e4SLinus Torvalds * 1681da177e4SLinus Torvalds * Following the skb_unshare() example, in case of error, the calling function 1691da177e4SLinus Torvalds * doesn't have to worry about freeing the original skb. 1700b5c9db1SJiri Pirko * 1710b5c9db1SJiri Pirko * Does not change skb->protocol so this function can be used during receive. 1721da177e4SLinus Torvalds */ 1730b5c9db1SJiri Pirko static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, u16 vlan_tci) 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds struct vlan_ethhdr *veth; 1761da177e4SLinus Torvalds 17711a100f8SPatrick McHardy if (skb_cow_head(skb, VLAN_HLEN) < 0) { 17811a100f8SPatrick McHardy kfree_skb(skb); 1791da177e4SLinus Torvalds return NULL; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN); 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Move the mac addresses to the beginning of the new header. */ 184a3f671b3SJoe Perches memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN); 185e4dd6188SJarek Poplawski skb->mac_header -= VLAN_HLEN; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* first, the ethernet type */ 18857d3ae84SPatrick McHardy veth->h_vlan_proto = htons(ETH_P_8021Q); 1891da177e4SLinus Torvalds 1909bb8582eSPatrick McHardy /* now, the TCI */ 1919bb8582eSPatrick McHardy veth->h_vlan_TCI = htons(vlan_tci); 1921da177e4SLinus Torvalds 1930b5c9db1SJiri Pirko return skb; 1940b5c9db1SJiri Pirko } 1951da177e4SLinus Torvalds 1960b5c9db1SJiri Pirko /** 1970b5c9db1SJiri Pirko * __vlan_put_tag - regular VLAN tag inserting 1980b5c9db1SJiri Pirko * @skb: skbuff to tag 1990b5c9db1SJiri Pirko * @vlan_tci: VLAN TCI to insert 2000b5c9db1SJiri Pirko * 2010b5c9db1SJiri Pirko * Inserts the VLAN tag into @skb as part of the payload 2020b5c9db1SJiri Pirko * Returns a VLAN tagged skb. If a new skb is created, @skb is freed. 2030b5c9db1SJiri Pirko * 2040b5c9db1SJiri Pirko * Following the skb_unshare() example, in case of error, the calling function 2050b5c9db1SJiri Pirko * doesn't have to worry about freeing the original skb. 2060b5c9db1SJiri Pirko */ 2070b5c9db1SJiri Pirko static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, u16 vlan_tci) 2080b5c9db1SJiri Pirko { 2090b5c9db1SJiri Pirko skb = vlan_insert_tag(skb, vlan_tci); 2100b5c9db1SJiri Pirko if (skb) 2110b5c9db1SJiri Pirko skb->protocol = htons(ETH_P_8021Q); 2121da177e4SLinus Torvalds return skb; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /** 2161da177e4SLinus Torvalds * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting 2171da177e4SLinus Torvalds * @skb: skbuff to tag 2189bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 2191da177e4SLinus Torvalds * 2206aa895b0SPatrick McHardy * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest 2211da177e4SLinus Torvalds */ 2229bb8582eSPatrick McHardy static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb, 2239bb8582eSPatrick McHardy u16 vlan_tci) 2241da177e4SLinus Torvalds { 22505423b24SEric Dumazet skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci; 2261da177e4SLinus Torvalds return skb; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds #define HAVE_VLAN_PUT_TAG 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /** 2321da177e4SLinus Torvalds * vlan_put_tag - inserts VLAN tag according to device features 2331da177e4SLinus Torvalds * @skb: skbuff to tag 2349bb8582eSPatrick McHardy * @vlan_tci: VLAN TCI to insert 2351da177e4SLinus Torvalds * 2361da177e4SLinus Torvalds * Assumes skb->dev is the target that will xmit this frame. 2371da177e4SLinus Torvalds * Returns a VLAN tagged skb. 2381da177e4SLinus Torvalds */ 2399bb8582eSPatrick McHardy static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, u16 vlan_tci) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds if (skb->dev->features & NETIF_F_HW_VLAN_TX) { 2429bb8582eSPatrick McHardy return __vlan_hwaccel_put_tag(skb, vlan_tci); 2431da177e4SLinus Torvalds } else { 2449bb8582eSPatrick McHardy return __vlan_put_tag(skb, vlan_tci); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /** 2491da177e4SLinus Torvalds * __vlan_get_tag - get the VLAN ID that is part of the payload 2501da177e4SLinus Torvalds * @skb: skbuff to query 2519bb8582eSPatrick McHardy * @vlan_tci: buffer to store vlaue 2521da177e4SLinus Torvalds * 2531da177e4SLinus Torvalds * Returns error if the skb is not of VLAN type 2541da177e4SLinus Torvalds */ 2559bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data; 2581da177e4SLinus Torvalds 25957d3ae84SPatrick McHardy if (veth->h_vlan_proto != htons(ETH_P_8021Q)) { 2601da177e4SLinus Torvalds return -EINVAL; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2639bb8582eSPatrick McHardy *vlan_tci = ntohs(veth->h_vlan_TCI); 2641da177e4SLinus Torvalds return 0; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /** 2681da177e4SLinus Torvalds * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[] 2691da177e4SLinus Torvalds * @skb: skbuff to query 2709bb8582eSPatrick McHardy * @vlan_tci: buffer to store vlaue 2711da177e4SLinus Torvalds * 2726aa895b0SPatrick McHardy * Returns error if @skb->vlan_tci is not set correctly 2731da177e4SLinus Torvalds */ 27418149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb, 2759bb8582eSPatrick McHardy u16 *vlan_tci) 2761da177e4SLinus Torvalds { 2776aa895b0SPatrick McHardy if (vlan_tx_tag_present(skb)) { 27805423b24SEric Dumazet *vlan_tci = vlan_tx_tag_get(skb); 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } else { 2819bb8582eSPatrick McHardy *vlan_tci = 0; 2821da177e4SLinus Torvalds return -EINVAL; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /** 2891da177e4SLinus Torvalds * vlan_get_tag - get the VLAN ID from the skb 2901da177e4SLinus Torvalds * @skb: skbuff to query 2919bb8582eSPatrick McHardy * @vlan_tci: buffer to store vlaue 2921da177e4SLinus Torvalds * 2931da177e4SLinus Torvalds * Returns error if the skb is not VLAN tagged 2941da177e4SLinus Torvalds */ 2959bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds if (skb->dev->features & NETIF_F_HW_VLAN_TX) { 2989bb8582eSPatrick McHardy return __vlan_hwaccel_get_tag(skb, vlan_tci); 2991da177e4SLinus Torvalds } else { 3009bb8582eSPatrick McHardy return __vlan_get_tag(skb, vlan_tci); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3040a85df00SHao Zheng /** 3050a85df00SHao Zheng * vlan_get_protocol - get protocol EtherType. 3060a85df00SHao Zheng * @skb: skbuff to query 3070a85df00SHao Zheng * 3080a85df00SHao Zheng * Returns the EtherType of the packet, regardless of whether it is 3090a85df00SHao Zheng * vlan encapsulated (normal or hardware accelerated) or not. 3100a85df00SHao Zheng */ 3110a85df00SHao Zheng static inline __be16 vlan_get_protocol(const struct sk_buff *skb) 3120a85df00SHao Zheng { 3130a85df00SHao Zheng __be16 protocol = 0; 3140a85df00SHao Zheng 3150a85df00SHao Zheng if (vlan_tx_tag_present(skb) || 3160a85df00SHao Zheng skb->protocol != cpu_to_be16(ETH_P_8021Q)) 3170a85df00SHao Zheng protocol = skb->protocol; 3180a85df00SHao Zheng else { 3190a85df00SHao Zheng __be16 proto, *protop; 3200a85df00SHao Zheng protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr, 3210a85df00SHao Zheng h_vlan_encapsulated_proto), 3220a85df00SHao Zheng sizeof(proto), &proto); 3230a85df00SHao Zheng if (likely(protop)) 3240a85df00SHao Zheng protocol = *protop; 3250a85df00SHao Zheng } 3260a85df00SHao Zheng 3270a85df00SHao Zheng return protocol; 3280a85df00SHao Zheng } 329396cf943SPravin B Shelar 330396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb, 331396cf943SPravin B Shelar struct vlan_hdr *vhdr) 332396cf943SPravin B Shelar { 333396cf943SPravin B Shelar __be16 proto; 334396cf943SPravin B Shelar unsigned char *rawp; 335396cf943SPravin B Shelar 336396cf943SPravin B Shelar /* 337396cf943SPravin B Shelar * Was a VLAN packet, grab the encapsulated protocol, which the layer 338396cf943SPravin B Shelar * three protocols care about. 339396cf943SPravin B Shelar */ 340396cf943SPravin B Shelar 341396cf943SPravin B Shelar proto = vhdr->h_vlan_encapsulated_proto; 342396cf943SPravin B Shelar if (ntohs(proto) >= 1536) { 343396cf943SPravin B Shelar skb->protocol = proto; 344396cf943SPravin B Shelar return; 345396cf943SPravin B Shelar } 346396cf943SPravin B Shelar 347396cf943SPravin B Shelar rawp = skb->data; 348396cf943SPravin B Shelar if (*(unsigned short *) rawp == 0xFFFF) 349396cf943SPravin B Shelar /* 350396cf943SPravin B Shelar * This is a magic hack to spot IPX packets. Older Novell 351396cf943SPravin B Shelar * breaks the protocol design and runs IPX over 802.3 without 352396cf943SPravin B Shelar * an 802.2 LLC layer. We look for FFFF which isn't a used 353396cf943SPravin B Shelar * 802.2 SSAP/DSAP. This won't work for fault tolerant netware 354396cf943SPravin B Shelar * but does for the rest. 355396cf943SPravin B Shelar */ 356396cf943SPravin B Shelar skb->protocol = htons(ETH_P_802_3); 357396cf943SPravin B Shelar else 358396cf943SPravin B Shelar /* 359396cf943SPravin B Shelar * Real 802.2 LLC 360396cf943SPravin B Shelar */ 361396cf943SPravin B Shelar skb->protocol = htons(ETH_P_802_2); 362396cf943SPravin B Shelar } 3631da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */ 364