if_vlan.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #ifdef __KERNEL__
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #define VLAN_HLEN 4 /* The additional bytes (on top of the Ethernet header)
  18. * that VLAN requires.
  19. */
  20. #define VLAN_ETH_ALEN 6 /* Octets in one ethernet addr */
  21. #define VLAN_ETH_HLEN 18 /* Total octets in header. */
  22. #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
  23. /*
  24. * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
  25. */
  26. #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
  27. #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
  28. /*
  29. * struct vlan_hdr - vlan header
  30. * @h_vlan_TCI: priority and VLAN ID
  31. * @h_vlan_encapsulated_proto: packet type ID or len
  32. */
  33. struct vlan_hdr {
  34. __be16 h_vlan_TCI;
  35. __be16 h_vlan_encapsulated_proto;
  36. };
  37. /**
  38. * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
  39. * @h_dest: destination ethernet address
  40. * @h_source: source ethernet address
  41. * @h_vlan_proto: ethernet protocol (always 0x8100)
  42. * @h_vlan_TCI: priority and VLAN ID
  43. * @h_vlan_encapsulated_proto: packet type ID or len
  44. */
  45. struct vlan_ethhdr {
  46. unsigned char h_dest[ETH_ALEN];
  47. unsigned char h_source[ETH_ALEN];
  48. __be16 h_vlan_proto;
  49. __be16 h_vlan_TCI;
  50. __be16 h_vlan_encapsulated_proto;
  51. };
  52. #include <linux/skbuff.h>
  53. static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
  54. {
  55. return (struct vlan_ethhdr *)skb_mac_header(skb);
  56. }
  57. #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
  58. #define VLAN_PRIO_SHIFT 13
  59. #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator */
  60. #define VLAN_TAG_PRESENT VLAN_CFI_MASK
  61. #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
  62. /* found in socket.c */
  63. extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
  64. /* if this changes, algorithm will have to be reworked because this
  65. * depends on completely exhausting the VLAN identifier space. Thus
  66. * it gives constant time look-up, but in many cases it wastes memory.
  67. */
  68. #define VLAN_GROUP_ARRAY_LEN 4096
  69. #define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
  70. #define VLAN_GROUP_ARRAY_PART_LEN (VLAN_GROUP_ARRAY_LEN/VLAN_GROUP_ARRAY_SPLIT_PARTS)
  71. struct vlan_group {
  72. struct net_device *real_dev; /* The ethernet(like) device
  73. * the vlan is attached to.
  74. */
  75. unsigned int nr_vlans;
  76. struct hlist_node hlist; /* linked list */
  77. struct net_device **vlan_devices_arrays[VLAN_GROUP_ARRAY_SPLIT_PARTS];
  78. struct rcu_head rcu;
  79. };
  80. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  81. u16 vlan_id)
  82. {
  83. struct net_device **array;
  84. array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  85. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  86. }
  87. static inline void vlan_group_set_device(struct vlan_group *vg,
  88. u16 vlan_id,
  89. struct net_device *dev)
  90. {
  91. struct net_device **array;
  92. if (!vg)
  93. return;
  94. array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  95. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  96. }
  97. #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  98. #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  99. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  100. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  101. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  102. extern int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  103. u16 vlan_tci, int polling);
  104. extern int vlan_hwaccel_do_receive(struct sk_buff *skb);
  105. extern int vlan_gro_receive(struct napi_struct *napi, struct vlan_group *grp,
  106. unsigned int vlan_tci, struct sk_buff *skb);
  107. extern int vlan_gro_frags(struct napi_struct *napi, struct vlan_group *grp,
  108. unsigned int vlan_tci);
  109. #else
  110. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  111. {
  112. BUG();
  113. return NULL;
  114. }
  115. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  116. {
  117. BUG();
  118. return 0;
  119. }
  120. static inline int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  121. u16 vlan_tci, int polling)
  122. {
  123. BUG();
  124. return NET_XMIT_SUCCESS;
  125. }
  126. static inline int vlan_hwaccel_do_receive(struct sk_buff *skb)
  127. {
  128. return 0;
  129. }
  130. static inline int vlan_gro_receive(struct napi_struct *napi,
  131. struct vlan_group *grp,
  132. unsigned int vlan_tci, struct sk_buff *skb)
  133. {
  134. return NET_RX_DROP;
  135. }
  136. static inline int vlan_gro_frags(struct napi_struct *napi,
  137. struct vlan_group *grp, unsigned int vlan_tci)
  138. {
  139. return NET_RX_DROP;
  140. }
  141. #endif
  142. /**
  143. * vlan_hwaccel_rx - netif_rx wrapper for VLAN RX acceleration
  144. * @skb: buffer
  145. * @grp: vlan group
  146. * @vlan_tci: VLAN TCI as received from the card
  147. */
  148. static inline int vlan_hwaccel_rx(struct sk_buff *skb,
  149. struct vlan_group *grp,
  150. u16 vlan_tci)
  151. {
  152. return __vlan_hwaccel_rx(skb, grp, vlan_tci, 0);
  153. }
  154. /**
  155. * vlan_hwaccel_receive_skb - netif_receive_skb wrapper for VLAN RX acceleration
  156. * @skb: buffer
  157. * @grp: vlan group
  158. * @vlan_tci: VLAN TCI as received from the card
  159. */
  160. static inline int vlan_hwaccel_receive_skb(struct sk_buff *skb,
  161. struct vlan_group *grp,
  162. u16 vlan_tci)
  163. {
  164. return __vlan_hwaccel_rx(skb, grp, vlan_tci, 1);
  165. }
  166. /**
  167. * __vlan_put_tag - regular VLAN tag inserting
  168. * @skb: skbuff to tag
  169. * @vlan_tci: VLAN TCI to insert
  170. *
  171. * Inserts the VLAN tag into @skb as part of the payload
  172. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  173. *
  174. * Following the skb_unshare() example, in case of error, the calling function
  175. * doesn't have to worry about freeing the original skb.
  176. */
  177. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  178. {
  179. struct vlan_ethhdr *veth;
  180. if (skb_cow_head(skb, VLAN_HLEN) < 0) {
  181. kfree_skb(skb);
  182. return NULL;
  183. }
  184. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  185. /* Move the mac addresses to the beginning of the new header. */
  186. memmove(skb->data, skb->data + VLAN_HLEN, 2 * VLAN_ETH_ALEN);
  187. skb->mac_header -= VLAN_HLEN;
  188. /* first, the ethernet type */
  189. veth->h_vlan_proto = htons(ETH_P_8021Q);
  190. /* now, the TCI */
  191. veth->h_vlan_TCI = htons(vlan_tci);
  192. skb->protocol = htons(ETH_P_8021Q);
  193. return skb;
  194. }
  195. /**
  196. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  197. * @skb: skbuff to tag
  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. u16 vlan_tci)
  204. {
  205. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  206. return skb;
  207. }
  208. #define HAVE_VLAN_PUT_TAG
  209. /**
  210. * vlan_put_tag - inserts VLAN tag according to device features
  211. * @skb: skbuff to tag
  212. * @vlan_tci: VLAN TCI to insert
  213. *
  214. * Assumes skb->dev is the target that will xmit this frame.
  215. * Returns a VLAN tagged skb.
  216. */
  217. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  218. {
  219. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  220. return __vlan_hwaccel_put_tag(skb, vlan_tci);
  221. } else {
  222. return __vlan_put_tag(skb, vlan_tci);
  223. }
  224. }
  225. /**
  226. * __vlan_get_tag - get the VLAN ID that is part of the payload
  227. * @skb: skbuff to query
  228. * @vlan_tci: buffer to store vlaue
  229. *
  230. * Returns error if the skb is not of VLAN type
  231. */
  232. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  233. {
  234. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  235. if (veth->h_vlan_proto != htons(ETH_P_8021Q)) {
  236. return -EINVAL;
  237. }
  238. *vlan_tci = ntohs(veth->h_vlan_TCI);
  239. return 0;
  240. }
  241. /**
  242. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  243. * @skb: skbuff to query
  244. * @vlan_tci: buffer to store vlaue
  245. *
  246. * Returns error if @skb->vlan_tci is not set correctly
  247. */
  248. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  249. u16 *vlan_tci)
  250. {
  251. if (vlan_tx_tag_present(skb)) {
  252. *vlan_tci = vlan_tx_tag_get(skb);
  253. return 0;
  254. } else {
  255. *vlan_tci = 0;
  256. return -EINVAL;
  257. }
  258. }
  259. #define HAVE_VLAN_GET_TAG
  260. /**
  261. * vlan_get_tag - get the VLAN ID from the skb
  262. * @skb: skbuff to query
  263. * @vlan_tci: buffer to store vlaue
  264. *
  265. * Returns error if the skb is not VLAN tagged
  266. */
  267. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  268. {
  269. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  270. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  271. } else {
  272. return __vlan_get_tag(skb, vlan_tci);
  273. }
  274. }
  275. #endif /* __KERNEL__ */
  276. /* VLAN IOCTLs are found in sockios.h */
  277. /* Passed in vlan_ioctl_args structure to determine behaviour. */
  278. enum vlan_ioctl_cmds {
  279. ADD_VLAN_CMD,
  280. DEL_VLAN_CMD,
  281. SET_VLAN_INGRESS_PRIORITY_CMD,
  282. SET_VLAN_EGRESS_PRIORITY_CMD,
  283. GET_VLAN_INGRESS_PRIORITY_CMD,
  284. GET_VLAN_EGRESS_PRIORITY_CMD,
  285. SET_VLAN_NAME_TYPE_CMD,
  286. SET_VLAN_FLAG_CMD,
  287. GET_VLAN_REALDEV_NAME_CMD, /* If this works, you know it's a VLAN device, btw */
  288. GET_VLAN_VID_CMD /* Get the VID of this VLAN (specified by name) */
  289. };
  290. enum vlan_flags {
  291. VLAN_FLAG_REORDER_HDR = 0x1,
  292. VLAN_FLAG_GVRP = 0x2,
  293. };
  294. enum vlan_name_types {
  295. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  296. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  297. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  298. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  299. VLAN_NAME_TYPE_HIGHEST
  300. };
  301. struct vlan_ioctl_args {
  302. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  303. char device1[24];
  304. union {
  305. char device2[24];
  306. int VID;
  307. unsigned int skb_priority;
  308. unsigned int name_type;
  309. unsigned int bind_type;
  310. unsigned int flag; /* Matches vlan_dev_info flags */
  311. } u;
  312. short vlan_qos;
  313. };
  314. #endif /* !(_LINUX_IF_VLAN_H_) */