if_vlan.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. int killall;
  77. struct hlist_node hlist; /* linked list */
  78. struct net_device **vlan_devices_arrays[VLAN_GROUP_ARRAY_SPLIT_PARTS];
  79. struct rcu_head rcu;
  80. };
  81. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  82. u16 vlan_id)
  83. {
  84. struct net_device **array;
  85. array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  86. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  87. }
  88. static inline void vlan_group_set_device(struct vlan_group *vg,
  89. u16 vlan_id,
  90. struct net_device *dev)
  91. {
  92. struct net_device **array;
  93. if (!vg)
  94. return;
  95. array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  96. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  97. }
  98. #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  99. #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  100. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  101. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  102. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  103. extern int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  104. u16 vlan_tci, int polling);
  105. extern int vlan_hwaccel_do_receive(struct sk_buff *skb);
  106. extern gro_result_t
  107. vlan_gro_receive(struct napi_struct *napi, struct vlan_group *grp,
  108. unsigned int vlan_tci, struct sk_buff *skb);
  109. extern gro_result_t
  110. vlan_gro_frags(struct napi_struct *napi, struct vlan_group *grp,
  111. unsigned int vlan_tci);
  112. #else
  113. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  114. {
  115. BUG();
  116. return NULL;
  117. }
  118. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  119. {
  120. BUG();
  121. return 0;
  122. }
  123. static inline int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  124. u16 vlan_tci, int polling)
  125. {
  126. BUG();
  127. return NET_XMIT_SUCCESS;
  128. }
  129. static inline int vlan_hwaccel_do_receive(struct sk_buff *skb)
  130. {
  131. return 0;
  132. }
  133. static inline gro_result_t
  134. vlan_gro_receive(struct napi_struct *napi, struct vlan_group *grp,
  135. unsigned int vlan_tci, struct sk_buff *skb)
  136. {
  137. return GRO_DROP;
  138. }
  139. static inline gro_result_t
  140. vlan_gro_frags(struct napi_struct *napi, struct vlan_group *grp,
  141. unsigned int vlan_tci)
  142. {
  143. return GRO_DROP;
  144. }
  145. #endif
  146. /**
  147. * vlan_hwaccel_rx - netif_rx wrapper for VLAN RX acceleration
  148. * @skb: buffer
  149. * @grp: vlan group
  150. * @vlan_tci: VLAN TCI as received from the card
  151. */
  152. static inline int vlan_hwaccel_rx(struct sk_buff *skb,
  153. struct vlan_group *grp,
  154. u16 vlan_tci)
  155. {
  156. return __vlan_hwaccel_rx(skb, grp, vlan_tci, 0);
  157. }
  158. /**
  159. * vlan_hwaccel_receive_skb - netif_receive_skb wrapper for VLAN RX acceleration
  160. * @skb: buffer
  161. * @grp: vlan group
  162. * @vlan_tci: VLAN TCI as received from the card
  163. */
  164. static inline int vlan_hwaccel_receive_skb(struct sk_buff *skb,
  165. struct vlan_group *grp,
  166. u16 vlan_tci)
  167. {
  168. return __vlan_hwaccel_rx(skb, grp, vlan_tci, 1);
  169. }
  170. /**
  171. * __vlan_put_tag - regular VLAN tag inserting
  172. * @skb: skbuff to tag
  173. * @vlan_tci: VLAN TCI to insert
  174. *
  175. * Inserts the VLAN tag into @skb as part of the payload
  176. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  177. *
  178. * Following the skb_unshare() example, in case of error, the calling function
  179. * doesn't have to worry about freeing the original skb.
  180. */
  181. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  182. {
  183. struct vlan_ethhdr *veth;
  184. if (skb_cow_head(skb, VLAN_HLEN) < 0) {
  185. kfree_skb(skb);
  186. return NULL;
  187. }
  188. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  189. /* Move the mac addresses to the beginning of the new header. */
  190. memmove(skb->data, skb->data + VLAN_HLEN, 2 * VLAN_ETH_ALEN);
  191. skb->mac_header -= VLAN_HLEN;
  192. /* first, the ethernet type */
  193. veth->h_vlan_proto = htons(ETH_P_8021Q);
  194. /* now, the TCI */
  195. veth->h_vlan_TCI = htons(vlan_tci);
  196. skb->protocol = htons(ETH_P_8021Q);
  197. return skb;
  198. }
  199. /**
  200. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  201. * @skb: skbuff to tag
  202. * @vlan_tci: VLAN TCI to insert
  203. *
  204. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  205. */
  206. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
  207. u16 vlan_tci)
  208. {
  209. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  210. return skb;
  211. }
  212. #define HAVE_VLAN_PUT_TAG
  213. /**
  214. * vlan_put_tag - inserts VLAN tag according to device features
  215. * @skb: skbuff to tag
  216. * @vlan_tci: VLAN TCI to insert
  217. *
  218. * Assumes skb->dev is the target that will xmit this frame.
  219. * Returns a VLAN tagged skb.
  220. */
  221. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  222. {
  223. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  224. return __vlan_hwaccel_put_tag(skb, vlan_tci);
  225. } else {
  226. return __vlan_put_tag(skb, vlan_tci);
  227. }
  228. }
  229. /**
  230. * __vlan_get_tag - get the VLAN ID that is part of the payload
  231. * @skb: skbuff to query
  232. * @vlan_tci: buffer to store vlaue
  233. *
  234. * Returns error if the skb is not of VLAN type
  235. */
  236. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  237. {
  238. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  239. if (veth->h_vlan_proto != htons(ETH_P_8021Q)) {
  240. return -EINVAL;
  241. }
  242. *vlan_tci = ntohs(veth->h_vlan_TCI);
  243. return 0;
  244. }
  245. /**
  246. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  247. * @skb: skbuff to query
  248. * @vlan_tci: buffer to store vlaue
  249. *
  250. * Returns error if @skb->vlan_tci is not set correctly
  251. */
  252. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  253. u16 *vlan_tci)
  254. {
  255. if (vlan_tx_tag_present(skb)) {
  256. *vlan_tci = vlan_tx_tag_get(skb);
  257. return 0;
  258. } else {
  259. *vlan_tci = 0;
  260. return -EINVAL;
  261. }
  262. }
  263. #define HAVE_VLAN_GET_TAG
  264. /**
  265. * vlan_get_tag - get the VLAN ID from the skb
  266. * @skb: skbuff to query
  267. * @vlan_tci: buffer to store vlaue
  268. *
  269. * Returns error if the skb is not VLAN tagged
  270. */
  271. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  272. {
  273. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  274. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  275. } else {
  276. return __vlan_get_tag(skb, vlan_tci);
  277. }
  278. }
  279. #endif /* __KERNEL__ */
  280. /* VLAN IOCTLs are found in sockios.h */
  281. /* Passed in vlan_ioctl_args structure to determine behaviour. */
  282. enum vlan_ioctl_cmds {
  283. ADD_VLAN_CMD,
  284. DEL_VLAN_CMD,
  285. SET_VLAN_INGRESS_PRIORITY_CMD,
  286. SET_VLAN_EGRESS_PRIORITY_CMD,
  287. GET_VLAN_INGRESS_PRIORITY_CMD,
  288. GET_VLAN_EGRESS_PRIORITY_CMD,
  289. SET_VLAN_NAME_TYPE_CMD,
  290. SET_VLAN_FLAG_CMD,
  291. GET_VLAN_REALDEV_NAME_CMD, /* If this works, you know it's a VLAN device, btw */
  292. GET_VLAN_VID_CMD /* Get the VID of this VLAN (specified by name) */
  293. };
  294. enum vlan_flags {
  295. VLAN_FLAG_REORDER_HDR = 0x1,
  296. VLAN_FLAG_GVRP = 0x2,
  297. };
  298. enum vlan_name_types {
  299. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  300. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  301. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  302. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  303. VLAN_NAME_TYPE_HIGHEST
  304. };
  305. struct vlan_ioctl_args {
  306. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  307. char device1[24];
  308. union {
  309. char device2[24];
  310. int VID;
  311. unsigned int skb_priority;
  312. unsigned int name_type;
  313. unsigned int bind_type;
  314. unsigned int flag; /* Matches vlan_dev_info flags */
  315. } u;
  316. short vlan_qos;
  317. };
  318. #endif /* !(_LINUX_IF_VLAN_H_) */