if_vlan.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #define VLAN_HLEN 4 /* The additional bytes (on top of the Ethernet header)
  19. * that VLAN requires.
  20. */
  21. #define VLAN_ETH_ALEN 6 /* Octets in one ethernet addr */
  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. /* if this changes, algorithm will have to be reworked because this
  67. * depends on completely exhausting the VLAN identifier space. Thus
  68. * it gives constant time look-up, but in many cases it wastes memory.
  69. */
  70. #define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
  71. #define VLAN_GROUP_ARRAY_PART_LEN (VLAN_N_VID/VLAN_GROUP_ARRAY_SPLIT_PARTS)
  72. struct vlan_group {
  73. struct net_device *real_dev; /* The ethernet(like) device
  74. * the vlan is attached to.
  75. */
  76. unsigned int nr_vlans;
  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. static inline int is_vlan_dev(struct net_device *dev)
  99. {
  100. return dev->priv_flags & IFF_802_1Q_VLAN;
  101. }
  102. #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  103. #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  104. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  105. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  106. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  107. extern int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  108. u16 vlan_tci, int polling);
  109. extern bool vlan_do_receive(struct sk_buff **skb);
  110. extern struct sk_buff *vlan_untag(struct sk_buff *skb);
  111. extern gro_result_t
  112. vlan_gro_receive(struct napi_struct *napi, struct vlan_group *grp,
  113. unsigned int vlan_tci, struct sk_buff *skb);
  114. extern gro_result_t
  115. vlan_gro_frags(struct napi_struct *napi, struct vlan_group *grp,
  116. unsigned int vlan_tci);
  117. #else
  118. static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
  119. u16 vlan_id)
  120. {
  121. return NULL;
  122. }
  123. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  124. {
  125. BUG();
  126. return NULL;
  127. }
  128. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  129. {
  130. BUG();
  131. return 0;
  132. }
  133. static inline int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  134. u16 vlan_tci, int polling)
  135. {
  136. BUG();
  137. return NET_XMIT_SUCCESS;
  138. }
  139. static inline bool vlan_do_receive(struct sk_buff **skb)
  140. {
  141. if ((*skb)->vlan_tci & VLAN_VID_MASK)
  142. (*skb)->pkt_type = PACKET_OTHERHOST;
  143. return false;
  144. }
  145. static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
  146. {
  147. return skb;
  148. }
  149. static inline gro_result_t
  150. vlan_gro_receive(struct napi_struct *napi, struct vlan_group *grp,
  151. unsigned int vlan_tci, struct sk_buff *skb)
  152. {
  153. return GRO_DROP;
  154. }
  155. static inline gro_result_t
  156. vlan_gro_frags(struct napi_struct *napi, struct vlan_group *grp,
  157. unsigned int vlan_tci)
  158. {
  159. return GRO_DROP;
  160. }
  161. #endif
  162. /**
  163. * vlan_hwaccel_rx - netif_rx wrapper for VLAN RX acceleration
  164. * @skb: buffer
  165. * @grp: vlan group
  166. * @vlan_tci: VLAN TCI as received from the card
  167. */
  168. static inline int vlan_hwaccel_rx(struct sk_buff *skb,
  169. struct vlan_group *grp,
  170. u16 vlan_tci)
  171. {
  172. return __vlan_hwaccel_rx(skb, grp, vlan_tci, 0);
  173. }
  174. /**
  175. * vlan_hwaccel_receive_skb - netif_receive_skb wrapper for VLAN RX acceleration
  176. * @skb: buffer
  177. * @grp: vlan group
  178. * @vlan_tci: VLAN TCI as received from the card
  179. */
  180. static inline int vlan_hwaccel_receive_skb(struct sk_buff *skb,
  181. struct vlan_group *grp,
  182. u16 vlan_tci)
  183. {
  184. return __vlan_hwaccel_rx(skb, grp, vlan_tci, 1);
  185. }
  186. /**
  187. * vlan_insert_tag - regular VLAN tag inserting
  188. * @skb: skbuff to tag
  189. * @vlan_tci: VLAN TCI to insert
  190. *
  191. * Inserts the VLAN tag into @skb as part of the payload
  192. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  193. *
  194. * Following the skb_unshare() example, in case of error, the calling function
  195. * doesn't have to worry about freeing the original skb.
  196. *
  197. * Does not change skb->protocol so this function can be used during receive.
  198. */
  199. static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, u16 vlan_tci)
  200. {
  201. struct vlan_ethhdr *veth;
  202. if (skb_cow_head(skb, VLAN_HLEN) < 0) {
  203. kfree_skb(skb);
  204. return NULL;
  205. }
  206. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  207. /* Move the mac addresses to the beginning of the new header. */
  208. memmove(skb->data, skb->data + VLAN_HLEN, 2 * VLAN_ETH_ALEN);
  209. skb->mac_header -= VLAN_HLEN;
  210. /* first, the ethernet type */
  211. veth->h_vlan_proto = htons(ETH_P_8021Q);
  212. /* now, the TCI */
  213. veth->h_vlan_TCI = htons(vlan_tci);
  214. return skb;
  215. }
  216. /**
  217. * __vlan_put_tag - regular VLAN tag inserting
  218. * @skb: skbuff to tag
  219. * @vlan_tci: VLAN TCI to insert
  220. *
  221. * Inserts the VLAN tag into @skb as part of the payload
  222. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  223. *
  224. * Following the skb_unshare() example, in case of error, the calling function
  225. * doesn't have to worry about freeing the original skb.
  226. */
  227. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  228. {
  229. skb = vlan_insert_tag(skb, vlan_tci);
  230. if (skb)
  231. skb->protocol = htons(ETH_P_8021Q);
  232. return skb;
  233. }
  234. /**
  235. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  236. * @skb: skbuff to tag
  237. * @vlan_tci: VLAN TCI to insert
  238. *
  239. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  240. */
  241. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
  242. u16 vlan_tci)
  243. {
  244. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  245. return skb;
  246. }
  247. #define HAVE_VLAN_PUT_TAG
  248. /**
  249. * vlan_put_tag - inserts VLAN tag according to device features
  250. * @skb: skbuff to tag
  251. * @vlan_tci: VLAN TCI to insert
  252. *
  253. * Assumes skb->dev is the target that will xmit this frame.
  254. * Returns a VLAN tagged skb.
  255. */
  256. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, u16 vlan_tci)
  257. {
  258. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  259. return __vlan_hwaccel_put_tag(skb, vlan_tci);
  260. } else {
  261. return __vlan_put_tag(skb, vlan_tci);
  262. }
  263. }
  264. /**
  265. * __vlan_get_tag - get the VLAN ID that is part of the payload
  266. * @skb: skbuff to query
  267. * @vlan_tci: buffer to store vlaue
  268. *
  269. * Returns error if the skb is not of VLAN type
  270. */
  271. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  272. {
  273. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  274. if (veth->h_vlan_proto != htons(ETH_P_8021Q)) {
  275. return -EINVAL;
  276. }
  277. *vlan_tci = ntohs(veth->h_vlan_TCI);
  278. return 0;
  279. }
  280. /**
  281. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  282. * @skb: skbuff to query
  283. * @vlan_tci: buffer to store vlaue
  284. *
  285. * Returns error if @skb->vlan_tci is not set correctly
  286. */
  287. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  288. u16 *vlan_tci)
  289. {
  290. if (vlan_tx_tag_present(skb)) {
  291. *vlan_tci = vlan_tx_tag_get(skb);
  292. return 0;
  293. } else {
  294. *vlan_tci = 0;
  295. return -EINVAL;
  296. }
  297. }
  298. #define HAVE_VLAN_GET_TAG
  299. /**
  300. * vlan_get_tag - get the VLAN ID from the skb
  301. * @skb: skbuff to query
  302. * @vlan_tci: buffer to store vlaue
  303. *
  304. * Returns error if the skb is not VLAN tagged
  305. */
  306. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  307. {
  308. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  309. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  310. } else {
  311. return __vlan_get_tag(skb, vlan_tci);
  312. }
  313. }
  314. /**
  315. * vlan_get_protocol - get protocol EtherType.
  316. * @skb: skbuff to query
  317. *
  318. * Returns the EtherType of the packet, regardless of whether it is
  319. * vlan encapsulated (normal or hardware accelerated) or not.
  320. */
  321. static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
  322. {
  323. __be16 protocol = 0;
  324. if (vlan_tx_tag_present(skb) ||
  325. skb->protocol != cpu_to_be16(ETH_P_8021Q))
  326. protocol = skb->protocol;
  327. else {
  328. __be16 proto, *protop;
  329. protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
  330. h_vlan_encapsulated_proto),
  331. sizeof(proto), &proto);
  332. if (likely(protop))
  333. protocol = *protop;
  334. }
  335. return protocol;
  336. }
  337. #endif /* __KERNEL__ */
  338. /* VLAN IOCTLs are found in sockios.h */
  339. /* Passed in vlan_ioctl_args structure to determine behaviour. */
  340. enum vlan_ioctl_cmds {
  341. ADD_VLAN_CMD,
  342. DEL_VLAN_CMD,
  343. SET_VLAN_INGRESS_PRIORITY_CMD,
  344. SET_VLAN_EGRESS_PRIORITY_CMD,
  345. GET_VLAN_INGRESS_PRIORITY_CMD,
  346. GET_VLAN_EGRESS_PRIORITY_CMD,
  347. SET_VLAN_NAME_TYPE_CMD,
  348. SET_VLAN_FLAG_CMD,
  349. GET_VLAN_REALDEV_NAME_CMD, /* If this works, you know it's a VLAN device, btw */
  350. GET_VLAN_VID_CMD /* Get the VID of this VLAN (specified by name) */
  351. };
  352. enum vlan_flags {
  353. VLAN_FLAG_REORDER_HDR = 0x1,
  354. VLAN_FLAG_GVRP = 0x2,
  355. VLAN_FLAG_LOOSE_BINDING = 0x4,
  356. };
  357. enum vlan_name_types {
  358. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  359. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  360. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  361. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  362. VLAN_NAME_TYPE_HIGHEST
  363. };
  364. struct vlan_ioctl_args {
  365. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  366. char device1[24];
  367. union {
  368. char device2[24];
  369. int VID;
  370. unsigned int skb_priority;
  371. unsigned int name_type;
  372. unsigned int bind_type;
  373. unsigned int flag; /* Matches vlan_dev_info flags */
  374. } u;
  375. short vlan_qos;
  376. };
  377. #endif /* !(_LINUX_IF_VLAN_H_) */