if_vlan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #include <linux/rtnetlink.h>
  18. #include <linux/bug.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. struct vlan_info;
  67. static inline int is_vlan_dev(struct net_device *dev)
  68. {
  69. return dev->priv_flags & IFF_802_1Q_VLAN;
  70. }
  71. #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  72. #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  73. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  74. extern struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
  75. u16 vlan_id);
  76. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  77. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  78. extern bool vlan_do_receive(struct sk_buff **skb, bool last_handler);
  79. extern struct sk_buff *vlan_untag(struct sk_buff *skb);
  80. extern int vlan_vid_add(struct net_device *dev, unsigned short vid);
  81. extern void vlan_vid_del(struct net_device *dev, unsigned short vid);
  82. extern int vlan_vids_add_by_dev(struct net_device *dev,
  83. const struct net_device *by_dev);
  84. extern void vlan_vids_del_by_dev(struct net_device *dev,
  85. const struct net_device *by_dev);
  86. #else
  87. static inline struct net_device *
  88. __vlan_find_dev_deep(struct net_device *real_dev, u16 vlan_id)
  89. {
  90. return NULL;
  91. }
  92. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  93. {
  94. BUG();
  95. return NULL;
  96. }
  97. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  98. {
  99. BUG();
  100. return 0;
  101. }
  102. static inline bool vlan_do_receive(struct sk_buff **skb, bool last_handler)
  103. {
  104. if (((*skb)->vlan_tci & VLAN_VID_MASK) && last_handler)
  105. (*skb)->pkt_type = PACKET_OTHERHOST;
  106. return false;
  107. }
  108. static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
  109. {
  110. return skb;
  111. }
  112. static inline int vlan_vid_add(struct net_device *dev, unsigned short vid)
  113. {
  114. return 0;
  115. }
  116. static inline void vlan_vid_del(struct net_device *dev, unsigned short vid)
  117. {
  118. }
  119. static inline int vlan_vids_add_by_dev(struct net_device *dev,
  120. const struct net_device *by_dev)
  121. {
  122. return 0;
  123. }
  124. static inline void vlan_vids_del_by_dev(struct net_device *dev,
  125. const struct net_device *by_dev)
  126. {
  127. }
  128. #endif
  129. /**
  130. * vlan_insert_tag - regular VLAN tag inserting
  131. * @skb: skbuff to tag
  132. * @vlan_tci: VLAN TCI to insert
  133. *
  134. * Inserts the VLAN tag into @skb as part of the payload
  135. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  136. *
  137. * Following the skb_unshare() example, in case of error, the calling function
  138. * doesn't have to worry about freeing the original skb.
  139. *
  140. * Does not change skb->protocol so this function can be used during receive.
  141. */
  142. static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, u16 vlan_tci)
  143. {
  144. struct vlan_ethhdr *veth;
  145. if (skb_cow_head(skb, VLAN_HLEN) < 0) {
  146. kfree_skb(skb);
  147. return NULL;
  148. }
  149. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  150. /* Move the mac addresses to the beginning of the new header. */
  151. memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
  152. skb->mac_header -= VLAN_HLEN;
  153. /* first, the ethernet type */
  154. veth->h_vlan_proto = htons(ETH_P_8021Q);
  155. /* now, the TCI */
  156. veth->h_vlan_TCI = htons(vlan_tci);
  157. return skb;
  158. }
  159. /**
  160. * __vlan_put_tag - regular VLAN tag inserting
  161. * @skb: skbuff to tag
  162. * @vlan_tci: VLAN TCI to insert
  163. *
  164. * Inserts the VLAN tag into @skb as part of the payload
  165. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  166. *
  167. * Following the skb_unshare() example, in case of error, the calling function
  168. * doesn't have to worry about freeing the original skb.
  169. */
  170. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  171. {
  172. skb = vlan_insert_tag(skb, vlan_tci);
  173. if (skb)
  174. skb->protocol = htons(ETH_P_8021Q);
  175. return skb;
  176. }
  177. /**
  178. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  179. * @skb: skbuff to tag
  180. * @vlan_tci: VLAN TCI to insert
  181. *
  182. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  183. */
  184. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
  185. u16 vlan_tci)
  186. {
  187. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  188. return skb;
  189. }
  190. #define HAVE_VLAN_PUT_TAG
  191. /**
  192. * vlan_put_tag - inserts VLAN tag according to device features
  193. * @skb: skbuff to tag
  194. * @vlan_tci: VLAN TCI to insert
  195. *
  196. * Assumes skb->dev is the target that will xmit this frame.
  197. * Returns a VLAN tagged skb.
  198. */
  199. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  200. {
  201. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  202. return __vlan_hwaccel_put_tag(skb, vlan_tci);
  203. } else {
  204. return __vlan_put_tag(skb, vlan_tci);
  205. }
  206. }
  207. /**
  208. * __vlan_get_tag - get the VLAN ID that is part of the payload
  209. * @skb: skbuff to query
  210. * @vlan_tci: buffer to store vlaue
  211. *
  212. * Returns error if the skb is not of VLAN type
  213. */
  214. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  215. {
  216. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  217. if (veth->h_vlan_proto != htons(ETH_P_8021Q)) {
  218. return -EINVAL;
  219. }
  220. *vlan_tci = ntohs(veth->h_vlan_TCI);
  221. return 0;
  222. }
  223. /**
  224. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  225. * @skb: skbuff to query
  226. * @vlan_tci: buffer to store vlaue
  227. *
  228. * Returns error if @skb->vlan_tci is not set correctly
  229. */
  230. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  231. u16 *vlan_tci)
  232. {
  233. if (vlan_tx_tag_present(skb)) {
  234. *vlan_tci = vlan_tx_tag_get(skb);
  235. return 0;
  236. } else {
  237. *vlan_tci = 0;
  238. return -EINVAL;
  239. }
  240. }
  241. #define HAVE_VLAN_GET_TAG
  242. /**
  243. * vlan_get_tag - get the VLAN ID from the skb
  244. * @skb: skbuff to query
  245. * @vlan_tci: buffer to store vlaue
  246. *
  247. * Returns error if the skb is not VLAN tagged
  248. */
  249. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  250. {
  251. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  252. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  253. } else {
  254. return __vlan_get_tag(skb, vlan_tci);
  255. }
  256. }
  257. /**
  258. * vlan_get_protocol - get protocol EtherType.
  259. * @skb: skbuff to query
  260. *
  261. * Returns the EtherType of the packet, regardless of whether it is
  262. * vlan encapsulated (normal or hardware accelerated) or not.
  263. */
  264. static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
  265. {
  266. __be16 protocol = 0;
  267. if (vlan_tx_tag_present(skb) ||
  268. skb->protocol != cpu_to_be16(ETH_P_8021Q))
  269. protocol = skb->protocol;
  270. else {
  271. __be16 proto, *protop;
  272. protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
  273. h_vlan_encapsulated_proto),
  274. sizeof(proto), &proto);
  275. if (likely(protop))
  276. protocol = *protop;
  277. }
  278. return protocol;
  279. }
  280. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  281. struct vlan_hdr *vhdr)
  282. {
  283. __be16 proto;
  284. unsigned char *rawp;
  285. /*
  286. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  287. * three protocols care about.
  288. */
  289. proto = vhdr->h_vlan_encapsulated_proto;
  290. if (ntohs(proto) >= 1536) {
  291. skb->protocol = proto;
  292. return;
  293. }
  294. rawp = skb->data;
  295. if (*(unsigned short *) rawp == 0xFFFF)
  296. /*
  297. * This is a magic hack to spot IPX packets. Older Novell
  298. * breaks the protocol design and runs IPX over 802.3 without
  299. * an 802.2 LLC layer. We look for FFFF which isn't a used
  300. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  301. * but does for the rest.
  302. */
  303. skb->protocol = htons(ETH_P_802_3);
  304. else
  305. /*
  306. * Real 802.2 LLC
  307. */
  308. skb->protocol = htons(ETH_P_802_2);
  309. }
  310. #endif /* __KERNEL__ */
  311. /* VLAN IOCTLs are found in sockios.h */
  312. /* Passed in vlan_ioctl_args structure to determine behaviour. */
  313. enum vlan_ioctl_cmds {
  314. ADD_VLAN_CMD,
  315. DEL_VLAN_CMD,
  316. SET_VLAN_INGRESS_PRIORITY_CMD,
  317. SET_VLAN_EGRESS_PRIORITY_CMD,
  318. GET_VLAN_INGRESS_PRIORITY_CMD,
  319. GET_VLAN_EGRESS_PRIORITY_CMD,
  320. SET_VLAN_NAME_TYPE_CMD,
  321. SET_VLAN_FLAG_CMD,
  322. GET_VLAN_REALDEV_NAME_CMD, /* If this works, you know it's a VLAN device, btw */
  323. GET_VLAN_VID_CMD /* Get the VID of this VLAN (specified by name) */
  324. };
  325. enum vlan_flags {
  326. VLAN_FLAG_REORDER_HDR = 0x1,
  327. VLAN_FLAG_GVRP = 0x2,
  328. VLAN_FLAG_LOOSE_BINDING = 0x4,
  329. };
  330. enum vlan_name_types {
  331. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  332. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  333. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  334. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  335. VLAN_NAME_TYPE_HIGHEST
  336. };
  337. struct vlan_ioctl_args {
  338. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  339. char device1[24];
  340. union {
  341. char device2[24];
  342. int VID;
  343. unsigned int skb_priority;
  344. unsigned int name_type;
  345. unsigned int bind_type;
  346. unsigned int flag; /* Matches vlan_dev_priv flags */
  347. } u;
  348. short vlan_qos;
  349. };
  350. #endif /* !(_LINUX_IF_VLAN_H_) */