if_vlan.h 8.9 KB

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