if_vlan.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * VLAN An implementation of 802.1Q VLAN tagging.
  3. *
  4. * Authors: Ben Greear <greearb@candelatech.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #ifndef _LINUX_IF_VLAN_H_
  13. #define _LINUX_IF_VLAN_H_
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/bug.h>
  18. #include <uapi/linux/if_vlan.h>
  19. #define VLAN_HLEN 4 /* The additional bytes required by VLAN
  20. * (in addition to the Ethernet header)
  21. */
  22. #define VLAN_ETH_HLEN 18 /* Total octets in header. */
  23. #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
  24. /*
  25. * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
  26. */
  27. #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
  28. #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
  29. /*
  30. * struct vlan_hdr - vlan header
  31. * @h_vlan_TCI: priority and VLAN ID
  32. * @h_vlan_encapsulated_proto: packet type ID or len
  33. */
  34. struct vlan_hdr {
  35. __be16 h_vlan_TCI;
  36. __be16 h_vlan_encapsulated_proto;
  37. };
  38. /**
  39. * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
  40. * @h_dest: destination ethernet address
  41. * @h_source: source ethernet address
  42. * @h_vlan_proto: ethernet protocol (always 0x8100)
  43. * @h_vlan_TCI: priority and VLAN ID
  44. * @h_vlan_encapsulated_proto: packet type ID or len
  45. */
  46. struct vlan_ethhdr {
  47. unsigned char h_dest[ETH_ALEN];
  48. unsigned char h_source[ETH_ALEN];
  49. __be16 h_vlan_proto;
  50. __be16 h_vlan_TCI;
  51. __be16 h_vlan_encapsulated_proto;
  52. };
  53. #include <linux/skbuff.h>
  54. static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
  55. {
  56. return (struct vlan_ethhdr *)skb_mac_header(skb);
  57. }
  58. #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
  59. #define VLAN_PRIO_SHIFT 13
  60. #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator */
  61. #define VLAN_TAG_PRESENT VLAN_CFI_MASK
  62. #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
  63. #define VLAN_N_VID 4096
  64. /* found in socket.c */
  65. extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
  66. static inline int is_vlan_dev(struct net_device *dev)
  67. {
  68. return dev->priv_flags & IFF_802_1Q_VLAN;
  69. }
  70. #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  71. #define vlan_tx_nonzero_tag_present(__skb) \
  72. (vlan_tx_tag_present(__skb) && ((__skb)->vlan_tci & VLAN_VID_MASK))
  73. #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  74. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  75. extern struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
  76. __be16 vlan_proto, u16 vlan_id);
  77. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  78. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  79. extern bool vlan_do_receive(struct sk_buff **skb);
  80. extern struct sk_buff *vlan_untag(struct sk_buff *skb);
  81. extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
  82. extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
  83. extern int vlan_vids_add_by_dev(struct net_device *dev,
  84. const struct net_device *by_dev);
  85. extern void vlan_vids_del_by_dev(struct net_device *dev,
  86. const struct net_device *by_dev);
  87. extern bool vlan_uses_dev(const struct net_device *dev);
  88. #else
  89. static inline struct net_device *
  90. __vlan_find_dev_deep(struct net_device *real_dev,
  91. __be16 vlan_proto, u16 vlan_id)
  92. {
  93. return NULL;
  94. }
  95. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  96. {
  97. BUG();
  98. return NULL;
  99. }
  100. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  101. {
  102. BUG();
  103. return 0;
  104. }
  105. static inline bool vlan_do_receive(struct sk_buff **skb)
  106. {
  107. return false;
  108. }
  109. static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
  110. {
  111. return skb;
  112. }
  113. static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  114. {
  115. return 0;
  116. }
  117. static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  118. {
  119. }
  120. static inline int vlan_vids_add_by_dev(struct net_device *dev,
  121. const struct net_device *by_dev)
  122. {
  123. return 0;
  124. }
  125. static inline void vlan_vids_del_by_dev(struct net_device *dev,
  126. const struct net_device *by_dev)
  127. {
  128. }
  129. static inline bool vlan_uses_dev(const struct net_device *dev)
  130. {
  131. return false;
  132. }
  133. #endif
  134. static inline bool vlan_hw_offload_capable(netdev_features_t features,
  135. __be16 proto)
  136. {
  137. if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
  138. return true;
  139. if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
  140. return true;
  141. return false;
  142. }
  143. /**
  144. * vlan_insert_tag - regular VLAN tag inserting
  145. * @skb: skbuff to tag
  146. * @vlan_proto: VLAN encapsulation protocol
  147. * @vlan_tci: VLAN TCI to insert
  148. *
  149. * Inserts the VLAN tag into @skb as part of the payload
  150. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  151. *
  152. * Following the skb_unshare() example, in case of error, the calling function
  153. * doesn't have to worry about freeing the original skb.
  154. *
  155. * Does not change skb->protocol so this function can be used during receive.
  156. */
  157. static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
  158. __be16 vlan_proto, u16 vlan_tci)
  159. {
  160. struct vlan_ethhdr *veth;
  161. if (skb_cow_head(skb, VLAN_HLEN) < 0) {
  162. kfree_skb(skb);
  163. return NULL;
  164. }
  165. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  166. /* Move the mac addresses to the beginning of the new header. */
  167. memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
  168. skb->mac_header -= VLAN_HLEN;
  169. /* first, the ethernet type */
  170. veth->h_vlan_proto = vlan_proto;
  171. /* now, the TCI */
  172. veth->h_vlan_TCI = htons(vlan_tci);
  173. return skb;
  174. }
  175. /**
  176. * __vlan_put_tag - regular VLAN tag inserting
  177. * @skb: skbuff to tag
  178. * @vlan_tci: VLAN TCI to insert
  179. *
  180. * Inserts the VLAN tag into @skb as part of the payload
  181. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  182. *
  183. * Following the skb_unshare() example, in case of error, the calling function
  184. * doesn't have to worry about freeing the original skb.
  185. */
  186. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb,
  187. __be16 vlan_proto, u16 vlan_tci)
  188. {
  189. skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
  190. if (skb)
  191. skb->protocol = vlan_proto;
  192. return skb;
  193. }
  194. /**
  195. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  196. * @skb: skbuff to tag
  197. * @vlan_proto: VLAN encapsulation protocol
  198. * @vlan_tci: VLAN TCI to insert
  199. *
  200. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  201. */
  202. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
  203. __be16 vlan_proto,
  204. u16 vlan_tci)
  205. {
  206. skb->vlan_proto = vlan_proto;
  207. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  208. return skb;
  209. }
  210. #define HAVE_VLAN_PUT_TAG
  211. /**
  212. * vlan_put_tag - inserts VLAN tag according to device features
  213. * @skb: skbuff to tag
  214. * @vlan_tci: VLAN TCI to insert
  215. *
  216. * Assumes skb->dev is the target that will xmit this frame.
  217. * Returns a VLAN tagged skb.
  218. */
  219. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb,
  220. __be16 vlan_proto, u16 vlan_tci)
  221. {
  222. if (vlan_hw_offload_capable(skb->dev->features, vlan_proto)) {
  223. return __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  224. } else {
  225. return __vlan_put_tag(skb, vlan_proto, vlan_tci);
  226. }
  227. }
  228. /**
  229. * __vlan_get_tag - get the VLAN ID that is part of the payload
  230. * @skb: skbuff to query
  231. * @vlan_tci: buffer to store vlaue
  232. *
  233. * Returns error if the skb is not of VLAN type
  234. */
  235. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  236. {
  237. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  238. if (veth->h_vlan_proto != htons(ETH_P_8021Q) &&
  239. veth->h_vlan_proto != htons(ETH_P_8021AD))
  240. return -EINVAL;
  241. *vlan_tci = ntohs(veth->h_vlan_TCI);
  242. return 0;
  243. }
  244. /**
  245. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  246. * @skb: skbuff to query
  247. * @vlan_tci: buffer to store vlaue
  248. *
  249. * Returns error if @skb->vlan_tci is not set correctly
  250. */
  251. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  252. u16 *vlan_tci)
  253. {
  254. if (vlan_tx_tag_present(skb)) {
  255. *vlan_tci = vlan_tx_tag_get(skb);
  256. return 0;
  257. } else {
  258. *vlan_tci = 0;
  259. return -EINVAL;
  260. }
  261. }
  262. #define HAVE_VLAN_GET_TAG
  263. /**
  264. * vlan_get_tag - get the VLAN ID from the skb
  265. * @skb: skbuff to query
  266. * @vlan_tci: buffer to store vlaue
  267. *
  268. * Returns error if the skb is not VLAN tagged
  269. */
  270. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  271. {
  272. if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
  273. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  274. } else {
  275. return __vlan_get_tag(skb, vlan_tci);
  276. }
  277. }
  278. /**
  279. * vlan_get_protocol - get protocol EtherType.
  280. * @skb: skbuff to query
  281. *
  282. * Returns the EtherType of the packet, regardless of whether it is
  283. * vlan encapsulated (normal or hardware accelerated) or not.
  284. */
  285. static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
  286. {
  287. __be16 protocol = 0;
  288. if (vlan_tx_tag_present(skb) ||
  289. skb->protocol != cpu_to_be16(ETH_P_8021Q))
  290. protocol = skb->protocol;
  291. else {
  292. __be16 proto, *protop;
  293. protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
  294. h_vlan_encapsulated_proto),
  295. sizeof(proto), &proto);
  296. if (likely(protop))
  297. protocol = *protop;
  298. }
  299. return protocol;
  300. }
  301. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  302. struct vlan_hdr *vhdr)
  303. {
  304. __be16 proto;
  305. unsigned short *rawp;
  306. /*
  307. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  308. * three protocols care about.
  309. */
  310. proto = vhdr->h_vlan_encapsulated_proto;
  311. if (ntohs(proto) >= ETH_P_802_3_MIN) {
  312. skb->protocol = proto;
  313. return;
  314. }
  315. rawp = (unsigned short *)(vhdr + 1);
  316. if (*rawp == 0xFFFF)
  317. /*
  318. * This is a magic hack to spot IPX packets. Older Novell
  319. * breaks the protocol design and runs IPX over 802.3 without
  320. * an 802.2 LLC layer. We look for FFFF which isn't a used
  321. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  322. * but does for the rest.
  323. */
  324. skb->protocol = htons(ETH_P_802_3);
  325. else
  326. /*
  327. * Real 802.2 LLC
  328. */
  329. skb->protocol = htons(ETH_P_802_2);
  330. }
  331. #endif /* !(_LINUX_IF_VLAN_H_) */