if_vlan.h 12 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. /* 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. #define VLAN_NAME "vlan"
  63. /* if this changes, algorithm will have to be reworked because this
  64. * depends on completely exhausting the VLAN identifier space. Thus
  65. * it gives constant time look-up, but in many cases it wastes memory.
  66. */
  67. #define VLAN_GROUP_ARRAY_LEN 4096
  68. #define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
  69. #define VLAN_GROUP_ARRAY_PART_LEN (VLAN_GROUP_ARRAY_LEN/VLAN_GROUP_ARRAY_SPLIT_PARTS)
  70. struct vlan_group {
  71. int real_dev_ifindex; /* The ifindex of the ethernet(like) device the vlan is attached to. */
  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. unsigned int vlan_id)
  78. {
  79. struct net_device **array;
  80. array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  81. return array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN];
  82. }
  83. static inline void vlan_group_set_device(struct vlan_group *vg,
  84. unsigned int 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. struct vlan_priority_tci_mapping {
  94. u32 priority;
  95. unsigned short vlan_qos; /* This should be shifted when first set, so we only do it
  96. * at provisioning time.
  97. * ((skb->priority << 13) & 0xE000)
  98. */
  99. struct vlan_priority_tci_mapping *next;
  100. };
  101. /* Holds information that makes sense if this device is a VLAN device. */
  102. struct vlan_dev_info {
  103. /** This will be the mapping that correlates skb->priority to
  104. * 3 bits of VLAN QOS tags...
  105. */
  106. unsigned int nr_ingress_mappings;
  107. u32 ingress_priority_map[8];
  108. unsigned int nr_egress_mappings;
  109. struct vlan_priority_tci_mapping *egress_priority_map[16]; /* hash table */
  110. unsigned short vlan_id; /* The VLAN Identifier for this interface. */
  111. unsigned short flags; /* (1 << 0) re_order_header This option will cause the
  112. * VLAN code to move around the ethernet header on
  113. * ingress to make the skb look **exactly** like it
  114. * came in from an ethernet port. This destroys some of
  115. * the VLAN information in the skb, but it fixes programs
  116. * like DHCP that use packet-filtering and don't understand
  117. * 802.1Q
  118. */
  119. struct net_device *real_dev; /* the underlying device/interface */
  120. unsigned char real_dev_addr[ETH_ALEN];
  121. struct proc_dir_entry *dent; /* Holds the proc data */
  122. unsigned long cnt_inc_headroom_on_tx; /* How many times did we have to grow the skb on TX. */
  123. unsigned long cnt_encap_on_xmit; /* How many times did we have to encapsulate the skb on TX. */
  124. struct net_device_stats dev_stats; /* Device stats (rx-bytes, tx-pkts, etc...) */
  125. };
  126. #define VLAN_DEV_INFO(x) ((struct vlan_dev_info *)(x->priv))
  127. /* inline functions */
  128. static inline struct net_device_stats *vlan_dev_get_stats(struct net_device *dev)
  129. {
  130. return &(VLAN_DEV_INFO(dev)->dev_stats);
  131. }
  132. static inline __u32 vlan_get_ingress_priority(struct net_device *dev,
  133. unsigned short vlan_tag)
  134. {
  135. struct vlan_dev_info *vip = VLAN_DEV_INFO(dev);
  136. return vip->ingress_priority_map[(vlan_tag >> 13) & 0x7];
  137. }
  138. /* VLAN tx hw acceleration helpers. */
  139. struct vlan_skb_tx_cookie {
  140. u32 magic;
  141. u32 vlan_tag;
  142. };
  143. #define VLAN_TX_COOKIE_MAGIC 0x564c414e /* "VLAN" in ascii. */
  144. #define VLAN_TX_SKB_CB(__skb) ((struct vlan_skb_tx_cookie *)&((__skb)->cb[0]))
  145. #define vlan_tx_tag_present(__skb) \
  146. (VLAN_TX_SKB_CB(__skb)->magic == VLAN_TX_COOKIE_MAGIC)
  147. #define vlan_tx_tag_get(__skb) (VLAN_TX_SKB_CB(__skb)->vlan_tag)
  148. /* VLAN rx hw acceleration helper. This acts like netif_{rx,receive_skb}(). */
  149. static inline int __vlan_hwaccel_rx(struct sk_buff *skb,
  150. struct vlan_group *grp,
  151. unsigned short vlan_tag, int polling)
  152. {
  153. struct net_device_stats *stats;
  154. if (skb_bond_should_drop(skb)) {
  155. dev_kfree_skb_any(skb);
  156. return NET_RX_DROP;
  157. }
  158. skb->dev = vlan_group_get_device(grp, vlan_tag & VLAN_VID_MASK);
  159. if (skb->dev == NULL) {
  160. dev_kfree_skb_any(skb);
  161. /* Not NET_RX_DROP, this is not being dropped
  162. * due to congestion.
  163. */
  164. return 0;
  165. }
  166. skb->dev->last_rx = jiffies;
  167. stats = vlan_dev_get_stats(skb->dev);
  168. stats->rx_packets++;
  169. stats->rx_bytes += skb->len;
  170. skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tag);
  171. switch (skb->pkt_type) {
  172. case PACKET_BROADCAST:
  173. break;
  174. case PACKET_MULTICAST:
  175. stats->multicast++;
  176. break;
  177. case PACKET_OTHERHOST:
  178. /* Our lower layer thinks this is not local, let's make sure.
  179. * This allows the VLAN to have a different MAC than the underlying
  180. * device, and still route correctly.
  181. */
  182. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  183. skb->dev->dev_addr))
  184. skb->pkt_type = PACKET_HOST;
  185. break;
  186. };
  187. return (polling ? netif_receive_skb(skb) : netif_rx(skb));
  188. }
  189. static inline int vlan_hwaccel_rx(struct sk_buff *skb,
  190. struct vlan_group *grp,
  191. unsigned short vlan_tag)
  192. {
  193. return __vlan_hwaccel_rx(skb, grp, vlan_tag, 0);
  194. }
  195. static inline int vlan_hwaccel_receive_skb(struct sk_buff *skb,
  196. struct vlan_group *grp,
  197. unsigned short vlan_tag)
  198. {
  199. return __vlan_hwaccel_rx(skb, grp, vlan_tag, 1);
  200. }
  201. /**
  202. * __vlan_put_tag - regular VLAN tag inserting
  203. * @skb: skbuff to tag
  204. * @tag: VLAN tag to insert
  205. *
  206. * Inserts the VLAN tag into @skb as part of the payload
  207. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  208. *
  209. * Following the skb_unshare() example, in case of error, the calling function
  210. * doesn't have to worry about freeing the original skb.
  211. */
  212. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, unsigned short tag)
  213. {
  214. struct vlan_ethhdr *veth;
  215. if (skb_headroom(skb) < VLAN_HLEN) {
  216. struct sk_buff *sk_tmp = skb;
  217. skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
  218. kfree_skb(sk_tmp);
  219. if (!skb) {
  220. printk(KERN_ERR "vlan: failed to realloc headroom\n");
  221. return NULL;
  222. }
  223. } else {
  224. skb = skb_unshare(skb, GFP_ATOMIC);
  225. if (!skb) {
  226. printk(KERN_ERR "vlan: failed to unshare skbuff\n");
  227. return NULL;
  228. }
  229. }
  230. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  231. /* Move the mac addresses to the beginning of the new header. */
  232. memmove(skb->data, skb->data + VLAN_HLEN, 2 * VLAN_ETH_ALEN);
  233. /* first, the ethernet type */
  234. veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
  235. /* now, the tag */
  236. veth->h_vlan_TCI = htons(tag);
  237. skb->protocol = __constant_htons(ETH_P_8021Q);
  238. skb->mac_header -= VLAN_HLEN;
  239. skb->network_header -= VLAN_HLEN;
  240. return skb;
  241. }
  242. /**
  243. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  244. * @skb: skbuff to tag
  245. * @tag: VLAN tag to insert
  246. *
  247. * Puts the VLAN tag in @skb->cb[] and lets the device do the rest
  248. */
  249. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb, unsigned short tag)
  250. {
  251. struct vlan_skb_tx_cookie *cookie;
  252. cookie = VLAN_TX_SKB_CB(skb);
  253. cookie->magic = VLAN_TX_COOKIE_MAGIC;
  254. cookie->vlan_tag = tag;
  255. return skb;
  256. }
  257. #define HAVE_VLAN_PUT_TAG
  258. /**
  259. * vlan_put_tag - inserts VLAN tag according to device features
  260. * @skb: skbuff to tag
  261. * @tag: VLAN tag to insert
  262. *
  263. * Assumes skb->dev is the target that will xmit this frame.
  264. * Returns a VLAN tagged skb.
  265. */
  266. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, unsigned short tag)
  267. {
  268. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  269. return __vlan_hwaccel_put_tag(skb, tag);
  270. } else {
  271. return __vlan_put_tag(skb, tag);
  272. }
  273. }
  274. /**
  275. * __vlan_get_tag - get the VLAN ID that is part of the payload
  276. * @skb: skbuff to query
  277. * @tag: buffer to store vlaue
  278. *
  279. * Returns error if the skb is not of VLAN type
  280. */
  281. static inline int __vlan_get_tag(struct sk_buff *skb, unsigned short *tag)
  282. {
  283. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  284. if (veth->h_vlan_proto != __constant_htons(ETH_P_8021Q)) {
  285. return -EINVAL;
  286. }
  287. *tag = ntohs(veth->h_vlan_TCI);
  288. return 0;
  289. }
  290. /**
  291. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  292. * @skb: skbuff to query
  293. * @tag: buffer to store vlaue
  294. *
  295. * Returns error if @skb->cb[] is not set correctly
  296. */
  297. static inline int __vlan_hwaccel_get_tag(struct sk_buff *skb, unsigned short *tag)
  298. {
  299. struct vlan_skb_tx_cookie *cookie;
  300. cookie = VLAN_TX_SKB_CB(skb);
  301. if (cookie->magic == VLAN_TX_COOKIE_MAGIC) {
  302. *tag = cookie->vlan_tag;
  303. return 0;
  304. } else {
  305. *tag = 0;
  306. return -EINVAL;
  307. }
  308. }
  309. #define HAVE_VLAN_GET_TAG
  310. /**
  311. * vlan_get_tag - get the VLAN ID from the skb
  312. * @skb: skbuff to query
  313. * @tag: buffer to store vlaue
  314. *
  315. * Returns error if the skb is not VLAN tagged
  316. */
  317. static inline int vlan_get_tag(struct sk_buff *skb, unsigned short *tag)
  318. {
  319. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  320. return __vlan_hwaccel_get_tag(skb, tag);
  321. } else {
  322. return __vlan_get_tag(skb, tag);
  323. }
  324. }
  325. #endif /* __KERNEL__ */
  326. /* VLAN IOCTLs are found in sockios.h */
  327. /* Passed in vlan_ioctl_args structure to determine behaviour. */
  328. enum vlan_ioctl_cmds {
  329. ADD_VLAN_CMD,
  330. DEL_VLAN_CMD,
  331. SET_VLAN_INGRESS_PRIORITY_CMD,
  332. SET_VLAN_EGRESS_PRIORITY_CMD,
  333. GET_VLAN_INGRESS_PRIORITY_CMD,
  334. GET_VLAN_EGRESS_PRIORITY_CMD,
  335. SET_VLAN_NAME_TYPE_CMD,
  336. SET_VLAN_FLAG_CMD,
  337. GET_VLAN_REALDEV_NAME_CMD, /* If this works, you know it's a VLAN device, btw */
  338. GET_VLAN_VID_CMD /* Get the VID of this VLAN (specified by name) */
  339. };
  340. enum vlan_flags {
  341. VLAN_FLAG_REORDER_HDR = 0x1,
  342. };
  343. enum vlan_name_types {
  344. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  345. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  346. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  347. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  348. VLAN_NAME_TYPE_HIGHEST
  349. };
  350. struct vlan_ioctl_args {
  351. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  352. char device1[24];
  353. union {
  354. char device2[24];
  355. int VID;
  356. unsigned int skb_priority;
  357. unsigned int name_type;
  358. unsigned int bind_type;
  359. unsigned int flag; /* Matches vlan_dev_info flags */
  360. } u;
  361. short vlan_qos;
  362. };
  363. #endif /* !(_LINUX_IF_VLAN_H_) */