if_vlan.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 vlan_group;
  17. struct net_device;
  18. struct packet_type;
  19. struct vlan_collection;
  20. struct vlan_dev_info;
  21. struct hlist_node;
  22. #include <linux/proc_fs.h> /* for proc_dir_entry */
  23. #include <linux/netdevice.h>
  24. #define VLAN_HLEN 4 /* The additional bytes (on top of the Ethernet header)
  25. * that VLAN requires.
  26. */
  27. #define VLAN_ETH_ALEN 6 /* Octets in one ethernet addr */
  28. #define VLAN_ETH_HLEN 18 /* Total octets in header. */
  29. #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
  30. /*
  31. * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
  32. */
  33. #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
  34. #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
  35. struct vlan_ethhdr {
  36. unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
  37. unsigned char h_source[ETH_ALEN]; /* source ether addr */
  38. unsigned short h_vlan_proto; /* Should always be 0x8100 */
  39. unsigned short h_vlan_TCI; /* Encapsulates priority and VLAN ID */
  40. unsigned short h_vlan_encapsulated_proto; /* packet type ID field (or len) */
  41. };
  42. #include <linux/skbuff.h>
  43. static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
  44. {
  45. return (struct vlan_ethhdr *)skb->mac.raw;
  46. }
  47. struct vlan_hdr {
  48. unsigned short h_vlan_TCI; /* Encapsulates priority and VLAN ID */
  49. unsigned short h_vlan_encapsulated_proto; /* packet type ID field (or len) */
  50. };
  51. #define VLAN_VID_MASK 0xfff
  52. /* found in socket.c */
  53. extern void vlan_ioctl_set(int (*hook)(void __user *));
  54. #define VLAN_NAME "vlan"
  55. /* if this changes, algorithm will have to be reworked because this
  56. * depends on completely exhausting the VLAN identifier space. Thus
  57. * it gives constant time look-up, but in many cases it wastes memory.
  58. */
  59. #define VLAN_GROUP_ARRAY_LEN 4096
  60. struct vlan_group {
  61. int real_dev_ifindex; /* The ifindex of the ethernet(like) device the vlan is attached to. */
  62. struct hlist_node hlist; /* linked list */
  63. struct net_device *vlan_devices[VLAN_GROUP_ARRAY_LEN];
  64. struct rcu_head rcu;
  65. };
  66. struct vlan_priority_tci_mapping {
  67. unsigned long priority;
  68. unsigned short vlan_qos; /* This should be shifted when first set, so we only do it
  69. * at provisioning time.
  70. * ((skb->priority << 13) & 0xE000)
  71. */
  72. struct vlan_priority_tci_mapping *next;
  73. };
  74. /* Holds information that makes sense if this device is a VLAN device. */
  75. struct vlan_dev_info {
  76. /** This will be the mapping that correlates skb->priority to
  77. * 3 bits of VLAN QOS tags...
  78. */
  79. unsigned long ingress_priority_map[8];
  80. struct vlan_priority_tci_mapping *egress_priority_map[16]; /* hash table */
  81. unsigned short vlan_id; /* The VLAN Identifier for this interface. */
  82. unsigned short flags; /* (1 << 0) re_order_header This option will cause the
  83. * VLAN code to move around the ethernet header on
  84. * ingress to make the skb look **exactly** like it
  85. * came in from an ethernet port. This destroys some of
  86. * the VLAN information in the skb, but it fixes programs
  87. * like DHCP that use packet-filtering and don't understand
  88. * 802.1Q
  89. */
  90. struct dev_mc_list *old_mc_list; /* old multi-cast list for the VLAN interface..
  91. * we save this so we can tell what changes were
  92. * made, in order to feed the right changes down
  93. * to the real hardware...
  94. */
  95. int old_allmulti; /* similar to above. */
  96. int old_promiscuity; /* similar to above. */
  97. struct net_device *real_dev; /* the underlying device/interface */
  98. struct proc_dir_entry *dent; /* Holds the proc data */
  99. unsigned long cnt_inc_headroom_on_tx; /* How many times did we have to grow the skb on TX. */
  100. unsigned long cnt_encap_on_xmit; /* How many times did we have to encapsulate the skb on TX. */
  101. struct net_device_stats dev_stats; /* Device stats (rx-bytes, tx-pkts, etc...) */
  102. };
  103. #define VLAN_DEV_INFO(x) ((struct vlan_dev_info *)(x->priv))
  104. /* inline functions */
  105. static inline struct net_device_stats *vlan_dev_get_stats(struct net_device *dev)
  106. {
  107. return &(VLAN_DEV_INFO(dev)->dev_stats);
  108. }
  109. static inline __u32 vlan_get_ingress_priority(struct net_device *dev,
  110. unsigned short vlan_tag)
  111. {
  112. struct vlan_dev_info *vip = VLAN_DEV_INFO(dev);
  113. return vip->ingress_priority_map[(vlan_tag >> 13) & 0x7];
  114. }
  115. /* VLAN tx hw acceleration helpers. */
  116. struct vlan_skb_tx_cookie {
  117. u32 magic;
  118. u32 vlan_tag;
  119. };
  120. #define VLAN_TX_COOKIE_MAGIC 0x564c414e /* "VLAN" in ascii. */
  121. #define VLAN_TX_SKB_CB(__skb) ((struct vlan_skb_tx_cookie *)&((__skb)->cb[0]))
  122. #define vlan_tx_tag_present(__skb) \
  123. (VLAN_TX_SKB_CB(__skb)->magic == VLAN_TX_COOKIE_MAGIC)
  124. #define vlan_tx_tag_get(__skb) (VLAN_TX_SKB_CB(__skb)->vlan_tag)
  125. /* VLAN rx hw acceleration helper. This acts like netif_{rx,receive_skb}(). */
  126. static inline int __vlan_hwaccel_rx(struct sk_buff *skb,
  127. struct vlan_group *grp,
  128. unsigned short vlan_tag, int polling)
  129. {
  130. struct net_device_stats *stats;
  131. skb->real_dev = skb->dev;
  132. skb->dev = grp->vlan_devices[vlan_tag & VLAN_VID_MASK];
  133. if (skb->dev == NULL) {
  134. dev_kfree_skb_any(skb);
  135. /* Not NET_RX_DROP, this is not being dropped
  136. * due to congestion.
  137. */
  138. return 0;
  139. }
  140. skb->dev->last_rx = jiffies;
  141. stats = vlan_dev_get_stats(skb->dev);
  142. stats->rx_packets++;
  143. stats->rx_bytes += skb->len;
  144. skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tag);
  145. switch (skb->pkt_type) {
  146. case PACKET_BROADCAST:
  147. break;
  148. case PACKET_MULTICAST:
  149. stats->multicast++;
  150. break;
  151. case PACKET_OTHERHOST:
  152. /* Our lower layer thinks this is not local, let's make sure.
  153. * This allows the VLAN to have a different MAC than the underlying
  154. * device, and still route correctly.
  155. */
  156. if (!memcmp(eth_hdr(skb)->h_dest, skb->dev->dev_addr, ETH_ALEN))
  157. skb->pkt_type = PACKET_HOST;
  158. break;
  159. };
  160. return (polling ? netif_receive_skb(skb) : netif_rx(skb));
  161. }
  162. static inline int vlan_hwaccel_rx(struct sk_buff *skb,
  163. struct vlan_group *grp,
  164. unsigned short vlan_tag)
  165. {
  166. return __vlan_hwaccel_rx(skb, grp, vlan_tag, 0);
  167. }
  168. static inline int vlan_hwaccel_receive_skb(struct sk_buff *skb,
  169. struct vlan_group *grp,
  170. unsigned short vlan_tag)
  171. {
  172. return __vlan_hwaccel_rx(skb, grp, vlan_tag, 1);
  173. }
  174. /**
  175. * __vlan_put_tag - regular VLAN tag inserting
  176. * @skb: skbuff to tag
  177. * @tag: VLAN tag to insert
  178. *
  179. * Inserts the VLAN tag into @skb as part of the payload
  180. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  181. *
  182. * Following the skb_unshare() example, in case of error, the calling function
  183. * doesn't have to worry about freeing the original skb.
  184. */
  185. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb, unsigned short tag)
  186. {
  187. struct vlan_ethhdr *veth;
  188. if (skb_headroom(skb) < VLAN_HLEN) {
  189. struct sk_buff *sk_tmp = skb;
  190. skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
  191. kfree_skb(sk_tmp);
  192. if (!skb) {
  193. printk(KERN_ERR "vlan: failed to realloc headroom\n");
  194. return NULL;
  195. }
  196. } else {
  197. skb = skb_unshare(skb, GFP_ATOMIC);
  198. if (!skb) {
  199. printk(KERN_ERR "vlan: failed to unshare skbuff\n");
  200. return NULL;
  201. }
  202. }
  203. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  204. /* Move the mac addresses to the beginning of the new header. */
  205. memmove(skb->data, skb->data + VLAN_HLEN, 2 * VLAN_ETH_ALEN);
  206. /* first, the ethernet type */
  207. veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
  208. /* now, the tag */
  209. veth->h_vlan_TCI = htons(tag);
  210. skb->protocol = __constant_htons(ETH_P_8021Q);
  211. skb->mac.raw -= VLAN_HLEN;
  212. skb->nh.raw -= VLAN_HLEN;
  213. return skb;
  214. }
  215. /**
  216. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  217. * @skb: skbuff to tag
  218. * @tag: VLAN tag to insert
  219. *
  220. * Puts the VLAN tag in @skb->cb[] and lets the device do the rest
  221. */
  222. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb, unsigned short tag)
  223. {
  224. struct vlan_skb_tx_cookie *cookie;
  225. cookie = VLAN_TX_SKB_CB(skb);
  226. cookie->magic = VLAN_TX_COOKIE_MAGIC;
  227. cookie->vlan_tag = tag;
  228. return skb;
  229. }
  230. #define HAVE_VLAN_PUT_TAG
  231. /**
  232. * vlan_put_tag - inserts VLAN tag according to device features
  233. * @skb: skbuff to tag
  234. * @tag: VLAN tag to insert
  235. *
  236. * Assumes skb->dev is the target that will xmit this frame.
  237. * Returns a VLAN tagged skb.
  238. */
  239. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb, unsigned short tag)
  240. {
  241. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  242. return __vlan_hwaccel_put_tag(skb, tag);
  243. } else {
  244. return __vlan_put_tag(skb, tag);
  245. }
  246. }
  247. /**
  248. * __vlan_get_tag - get the VLAN ID that is part of the payload
  249. * @skb: skbuff to query
  250. * @tag: buffer to store vlaue
  251. *
  252. * Returns error if the skb is not of VLAN type
  253. */
  254. static inline int __vlan_get_tag(struct sk_buff *skb, unsigned short *tag)
  255. {
  256. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  257. if (veth->h_vlan_proto != __constant_htons(ETH_P_8021Q)) {
  258. return -EINVAL;
  259. }
  260. *tag = ntohs(veth->h_vlan_TCI);
  261. return 0;
  262. }
  263. /**
  264. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  265. * @skb: skbuff to query
  266. * @tag: buffer to store vlaue
  267. *
  268. * Returns error if @skb->cb[] is not set correctly
  269. */
  270. static inline int __vlan_hwaccel_get_tag(struct sk_buff *skb, unsigned short *tag)
  271. {
  272. struct vlan_skb_tx_cookie *cookie;
  273. cookie = VLAN_TX_SKB_CB(skb);
  274. if (cookie->magic == VLAN_TX_COOKIE_MAGIC) {
  275. *tag = cookie->vlan_tag;
  276. return 0;
  277. } else {
  278. *tag = 0;
  279. return -EINVAL;
  280. }
  281. }
  282. #define HAVE_VLAN_GET_TAG
  283. /**
  284. * vlan_get_tag - get the VLAN ID from the skb
  285. * @skb: skbuff to query
  286. * @tag: buffer to store vlaue
  287. *
  288. * Returns error if the skb is not VLAN tagged
  289. */
  290. static inline int vlan_get_tag(struct sk_buff *skb, unsigned short *tag)
  291. {
  292. if (skb->dev->features & NETIF_F_HW_VLAN_TX) {
  293. return __vlan_hwaccel_get_tag(skb, tag);
  294. } else {
  295. return __vlan_get_tag(skb, tag);
  296. }
  297. }
  298. #endif /* __KERNEL__ */
  299. /* VLAN IOCTLs are found in sockios.h */
  300. /* Passed in vlan_ioctl_args structure to determine behaviour. */
  301. enum vlan_ioctl_cmds {
  302. ADD_VLAN_CMD,
  303. DEL_VLAN_CMD,
  304. SET_VLAN_INGRESS_PRIORITY_CMD,
  305. SET_VLAN_EGRESS_PRIORITY_CMD,
  306. GET_VLAN_INGRESS_PRIORITY_CMD,
  307. GET_VLAN_EGRESS_PRIORITY_CMD,
  308. SET_VLAN_NAME_TYPE_CMD,
  309. SET_VLAN_FLAG_CMD,
  310. GET_VLAN_REALDEV_NAME_CMD, /* If this works, you know it's a VLAN device, btw */
  311. GET_VLAN_VID_CMD /* Get the VID of this VLAN (specified by name) */
  312. };
  313. enum vlan_name_types {
  314. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  315. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  316. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  317. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  318. VLAN_NAME_TYPE_HIGHEST
  319. };
  320. struct vlan_ioctl_args {
  321. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  322. char device1[24];
  323. union {
  324. char device2[24];
  325. int VID;
  326. unsigned int skb_priority;
  327. unsigned int name_type;
  328. unsigned int bind_type;
  329. unsigned int flag; /* Matches vlan_dev_info flags */
  330. } u;
  331. short vlan_qos;
  332. };
  333. #endif /* !(_LINUX_IF_VLAN_H_) */