if_vlan.h 9.3 KB

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