if_vlan.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/bug.h>
  18. #include <uapi/linux/if_vlan.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
  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. static inline int is_vlan_dev(struct net_device *dev)
  67. {
  68. return dev->priv_flags & IFF_802_1Q_VLAN;
  69. }
  70. #define vlan_tx_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  71. #define vlan_tx_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  72. #define vlan_tx_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
  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. __be16 vlan_proto, 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. /**
  79. * struct vlan_priority_tci_mapping - vlan egress priority mappings
  80. * @priority: skb priority
  81. * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
  82. * @next: pointer to next struct
  83. */
  84. struct vlan_priority_tci_mapping {
  85. u32 priority;
  86. u16 vlan_qos;
  87. struct vlan_priority_tci_mapping *next;
  88. };
  89. /**
  90. * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
  91. * @rx_packets: number of received packets
  92. * @rx_bytes: number of received bytes
  93. * @rx_multicast: number of received multicast packets
  94. * @tx_packets: number of transmitted packets
  95. * @tx_bytes: number of transmitted bytes
  96. * @syncp: synchronization point for 64bit counters
  97. * @rx_errors: number of rx errors
  98. * @tx_dropped: number of tx drops
  99. */
  100. struct vlan_pcpu_stats {
  101. u64 rx_packets;
  102. u64 rx_bytes;
  103. u64 rx_multicast;
  104. u64 tx_packets;
  105. u64 tx_bytes;
  106. struct u64_stats_sync syncp;
  107. u32 rx_errors;
  108. u32 tx_dropped;
  109. };
  110. struct proc_dir_entry;
  111. struct netpoll;
  112. /**
  113. * struct vlan_dev_priv - VLAN private device data
  114. * @nr_ingress_mappings: number of ingress priority mappings
  115. * @ingress_priority_map: ingress priority mappings
  116. * @nr_egress_mappings: number of egress priority mappings
  117. * @egress_priority_map: hash of egress priority mappings
  118. * @vlan_proto: VLAN encapsulation protocol
  119. * @vlan_id: VLAN identifier
  120. * @flags: device flags
  121. * @real_dev: underlying netdevice
  122. * @real_dev_addr: address of underlying netdevice
  123. * @dent: proc dir entry
  124. * @vlan_pcpu_stats: ptr to percpu rx stats
  125. */
  126. struct vlan_dev_priv {
  127. unsigned int nr_ingress_mappings;
  128. u32 ingress_priority_map[8];
  129. unsigned int nr_egress_mappings;
  130. struct vlan_priority_tci_mapping *egress_priority_map[16];
  131. __be16 vlan_proto;
  132. u16 vlan_id;
  133. u16 flags;
  134. struct net_device *real_dev;
  135. unsigned char real_dev_addr[ETH_ALEN];
  136. struct proc_dir_entry *dent;
  137. struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
  138. #ifdef CONFIG_NET_POLL_CONTROLLER
  139. struct netpoll *netpoll;
  140. #endif
  141. };
  142. static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
  143. {
  144. return netdev_priv(dev);
  145. }
  146. static inline u16
  147. vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
  148. {
  149. struct vlan_priority_tci_mapping *mp;
  150. smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
  151. mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
  152. while (mp) {
  153. if (mp->priority == skprio) {
  154. return mp->vlan_qos; /* This should already be shifted
  155. * to mask correctly with the
  156. * VLAN's TCI */
  157. }
  158. mp = mp->next;
  159. }
  160. return 0;
  161. }
  162. extern bool vlan_do_receive(struct sk_buff **skb);
  163. extern struct sk_buff *vlan_untag(struct sk_buff *skb);
  164. extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
  165. extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
  166. extern int vlan_vids_add_by_dev(struct net_device *dev,
  167. const struct net_device *by_dev);
  168. extern void vlan_vids_del_by_dev(struct net_device *dev,
  169. const struct net_device *by_dev);
  170. extern bool vlan_uses_dev(const struct net_device *dev);
  171. #else
  172. static inline struct net_device *
  173. __vlan_find_dev_deep(struct net_device *real_dev,
  174. __be16 vlan_proto, u16 vlan_id)
  175. {
  176. return NULL;
  177. }
  178. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  179. {
  180. BUG();
  181. return NULL;
  182. }
  183. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  184. {
  185. BUG();
  186. return 0;
  187. }
  188. static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
  189. u32 skprio)
  190. {
  191. return 0;
  192. }
  193. static inline bool vlan_do_receive(struct sk_buff **skb)
  194. {
  195. return false;
  196. }
  197. static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
  198. {
  199. return skb;
  200. }
  201. static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  202. {
  203. return 0;
  204. }
  205. static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  206. {
  207. }
  208. static inline int vlan_vids_add_by_dev(struct net_device *dev,
  209. const struct net_device *by_dev)
  210. {
  211. return 0;
  212. }
  213. static inline void vlan_vids_del_by_dev(struct net_device *dev,
  214. const struct net_device *by_dev)
  215. {
  216. }
  217. static inline bool vlan_uses_dev(const struct net_device *dev)
  218. {
  219. return false;
  220. }
  221. #endif
  222. static inline bool vlan_hw_offload_capable(netdev_features_t features,
  223. __be16 proto)
  224. {
  225. if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
  226. return true;
  227. if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
  228. return true;
  229. return false;
  230. }
  231. /**
  232. * vlan_insert_tag - regular VLAN tag inserting
  233. * @skb: skbuff to tag
  234. * @vlan_proto: VLAN encapsulation protocol
  235. * @vlan_tci: VLAN TCI to insert
  236. *
  237. * Inserts the VLAN tag into @skb as part of the payload
  238. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  239. *
  240. * Following the skb_unshare() example, in case of error, the calling function
  241. * doesn't have to worry about freeing the original skb.
  242. *
  243. * Does not change skb->protocol so this function can be used during receive.
  244. */
  245. static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
  246. __be16 vlan_proto, u16 vlan_tci)
  247. {
  248. struct vlan_ethhdr *veth;
  249. if (skb_cow_head(skb, VLAN_HLEN) < 0) {
  250. kfree_skb(skb);
  251. return NULL;
  252. }
  253. veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
  254. /* Move the mac addresses to the beginning of the new header. */
  255. memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
  256. skb->mac_header -= VLAN_HLEN;
  257. /* first, the ethernet type */
  258. veth->h_vlan_proto = vlan_proto;
  259. /* now, the TCI */
  260. veth->h_vlan_TCI = htons(vlan_tci);
  261. return skb;
  262. }
  263. /**
  264. * __vlan_put_tag - regular VLAN tag inserting
  265. * @skb: skbuff to tag
  266. * @vlan_tci: VLAN TCI to insert
  267. *
  268. * Inserts the VLAN tag into @skb as part of the payload
  269. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  270. *
  271. * Following the skb_unshare() example, in case of error, the calling function
  272. * doesn't have to worry about freeing the original skb.
  273. */
  274. static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb,
  275. __be16 vlan_proto, u16 vlan_tci)
  276. {
  277. skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
  278. if (skb)
  279. skb->protocol = vlan_proto;
  280. return skb;
  281. }
  282. /**
  283. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  284. * @skb: skbuff to tag
  285. * @vlan_proto: VLAN encapsulation protocol
  286. * @vlan_tci: VLAN TCI to insert
  287. *
  288. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  289. */
  290. static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
  291. __be16 vlan_proto,
  292. u16 vlan_tci)
  293. {
  294. skb->vlan_proto = vlan_proto;
  295. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  296. return skb;
  297. }
  298. /**
  299. * vlan_put_tag - inserts VLAN tag according to device features
  300. * @skb: skbuff to tag
  301. * @vlan_tci: VLAN TCI to insert
  302. *
  303. * Assumes skb->dev is the target that will xmit this frame.
  304. * Returns a VLAN tagged skb.
  305. */
  306. static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb,
  307. __be16 vlan_proto, u16 vlan_tci)
  308. {
  309. if (vlan_hw_offload_capable(skb->dev->features, vlan_proto)) {
  310. return __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  311. } else {
  312. return __vlan_put_tag(skb, vlan_proto, vlan_tci);
  313. }
  314. }
  315. /**
  316. * __vlan_get_tag - get the VLAN ID that is part of the payload
  317. * @skb: skbuff to query
  318. * @vlan_tci: buffer to store vlaue
  319. *
  320. * Returns error if the skb is not of VLAN type
  321. */
  322. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  323. {
  324. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  325. if (veth->h_vlan_proto != htons(ETH_P_8021Q) &&
  326. veth->h_vlan_proto != htons(ETH_P_8021AD))
  327. return -EINVAL;
  328. *vlan_tci = ntohs(veth->h_vlan_TCI);
  329. return 0;
  330. }
  331. /**
  332. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  333. * @skb: skbuff to query
  334. * @vlan_tci: buffer to store vlaue
  335. *
  336. * Returns error if @skb->vlan_tci is not set correctly
  337. */
  338. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  339. u16 *vlan_tci)
  340. {
  341. if (vlan_tx_tag_present(skb)) {
  342. *vlan_tci = vlan_tx_tag_get(skb);
  343. return 0;
  344. } else {
  345. *vlan_tci = 0;
  346. return -EINVAL;
  347. }
  348. }
  349. #define HAVE_VLAN_GET_TAG
  350. /**
  351. * vlan_get_tag - get the VLAN ID from the skb
  352. * @skb: skbuff to query
  353. * @vlan_tci: buffer to store vlaue
  354. *
  355. * Returns error if the skb is not VLAN tagged
  356. */
  357. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  358. {
  359. if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
  360. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  361. } else {
  362. return __vlan_get_tag(skb, vlan_tci);
  363. }
  364. }
  365. /**
  366. * vlan_get_protocol - get protocol EtherType.
  367. * @skb: skbuff to query
  368. *
  369. * Returns the EtherType of the packet, regardless of whether it is
  370. * vlan encapsulated (normal or hardware accelerated) or not.
  371. */
  372. static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
  373. {
  374. __be16 protocol = 0;
  375. if (vlan_tx_tag_present(skb) ||
  376. skb->protocol != cpu_to_be16(ETH_P_8021Q))
  377. protocol = skb->protocol;
  378. else {
  379. __be16 proto, *protop;
  380. protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
  381. h_vlan_encapsulated_proto),
  382. sizeof(proto), &proto);
  383. if (likely(protop))
  384. protocol = *protop;
  385. }
  386. return protocol;
  387. }
  388. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  389. struct vlan_hdr *vhdr)
  390. {
  391. __be16 proto;
  392. unsigned short *rawp;
  393. /*
  394. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  395. * three protocols care about.
  396. */
  397. proto = vhdr->h_vlan_encapsulated_proto;
  398. if (ntohs(proto) >= ETH_P_802_3_MIN) {
  399. skb->protocol = proto;
  400. return;
  401. }
  402. rawp = (unsigned short *)(vhdr + 1);
  403. if (*rawp == 0xFFFF)
  404. /*
  405. * This is a magic hack to spot IPX packets. Older Novell
  406. * breaks the protocol design and runs IPX over 802.3 without
  407. * an 802.2 LLC layer. We look for FFFF which isn't a used
  408. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  409. * but does for the rest.
  410. */
  411. skb->protocol = htons(ETH_P_802_3);
  412. else
  413. /*
  414. * Real 802.2 LLC
  415. */
  416. skb->protocol = htons(ETH_P_802_2);
  417. }
  418. #endif /* !(_LINUX_IF_VLAN_H_) */