if_vlan.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * VLAN An implementation of 802.1Q VLAN tagging.
  4. *
  5. * Authors: Ben Greear <greearb@candelatech.com>
  6. */
  7. #ifndef _LINUX_IF_VLAN_H_
  8. #define _LINUX_IF_VLAN_H_
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/bug.h>
  13. #include <uapi/linux/if_vlan.h>
  14. #define VLAN_HLEN 4 /* The additional bytes required by VLAN
  15. * (in addition to the Ethernet header)
  16. */
  17. #define VLAN_ETH_HLEN 18 /* Total octets in header. */
  18. #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
  19. /*
  20. * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
  21. */
  22. #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
  23. #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
  24. #define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */
  25. /*
  26. * struct vlan_hdr - vlan header
  27. * @h_vlan_TCI: priority and VLAN ID
  28. * @h_vlan_encapsulated_proto: packet type ID or len
  29. */
  30. struct vlan_hdr {
  31. __be16 h_vlan_TCI;
  32. __be16 h_vlan_encapsulated_proto;
  33. };
  34. /**
  35. * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
  36. * @h_dest: destination ethernet address
  37. * @h_source: source ethernet address
  38. * @h_vlan_proto: ethernet protocol
  39. * @h_vlan_TCI: priority and VLAN ID
  40. * @h_vlan_encapsulated_proto: packet type ID or len
  41. */
  42. struct vlan_ethhdr {
  43. unsigned char h_dest[ETH_ALEN];
  44. unsigned char h_source[ETH_ALEN];
  45. __be16 h_vlan_proto;
  46. __be16 h_vlan_TCI;
  47. __be16 h_vlan_encapsulated_proto;
  48. };
  49. #include <linux/skbuff.h>
  50. static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
  51. {
  52. return (struct vlan_ethhdr *)skb_mac_header(skb);
  53. }
  54. #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
  55. #define VLAN_PRIO_SHIFT 13
  56. #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */
  57. #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
  58. #define VLAN_N_VID 4096
  59. /* found in socket.c */
  60. extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
  61. static inline bool is_vlan_dev(const struct net_device *dev)
  62. {
  63. return dev->priv_flags & IFF_802_1Q_VLAN;
  64. }
  65. #define skb_vlan_tag_present(__skb) ((__skb)->vlan_present)
  66. #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci)
  67. #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
  68. #define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK))
  69. #define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
  70. static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
  71. {
  72. ASSERT_RTNL();
  73. return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
  74. }
  75. static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
  76. {
  77. ASSERT_RTNL();
  78. call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
  79. }
  80. static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
  81. {
  82. ASSERT_RTNL();
  83. return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
  84. }
  85. static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
  86. {
  87. ASSERT_RTNL();
  88. call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
  89. }
  90. /**
  91. * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
  92. * @rx_packets: number of received packets
  93. * @rx_bytes: number of received bytes
  94. * @rx_multicast: number of received multicast packets
  95. * @tx_packets: number of transmitted packets
  96. * @tx_bytes: number of transmitted bytes
  97. * @syncp: synchronization point for 64bit counters
  98. * @rx_errors: number of rx errors
  99. * @tx_dropped: number of tx drops
  100. */
  101. struct vlan_pcpu_stats {
  102. u64 rx_packets;
  103. u64 rx_bytes;
  104. u64 rx_multicast;
  105. u64 tx_packets;
  106. u64 tx_bytes;
  107. struct u64_stats_sync syncp;
  108. u32 rx_errors;
  109. u32 tx_dropped;
  110. };
  111. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  112. extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
  113. __be16 vlan_proto, u16 vlan_id);
  114. extern int vlan_for_each(struct net_device *dev,
  115. int (*action)(struct net_device *dev, int vid,
  116. void *arg), void *arg);
  117. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  118. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  119. extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
  120. /**
  121. * struct vlan_priority_tci_mapping - vlan egress priority mappings
  122. * @priority: skb priority
  123. * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
  124. * @next: pointer to next struct
  125. */
  126. struct vlan_priority_tci_mapping {
  127. u32 priority;
  128. u16 vlan_qos;
  129. struct vlan_priority_tci_mapping *next;
  130. };
  131. struct proc_dir_entry;
  132. struct netpoll;
  133. /**
  134. * struct vlan_dev_priv - VLAN private device data
  135. * @nr_ingress_mappings: number of ingress priority mappings
  136. * @ingress_priority_map: ingress priority mappings
  137. * @nr_egress_mappings: number of egress priority mappings
  138. * @egress_priority_map: hash of egress priority mappings
  139. * @vlan_proto: VLAN encapsulation protocol
  140. * @vlan_id: VLAN identifier
  141. * @flags: device flags
  142. * @real_dev: underlying netdevice
  143. * @real_dev_addr: address of underlying netdevice
  144. * @dent: proc dir entry
  145. * @vlan_pcpu_stats: ptr to percpu rx stats
  146. */
  147. struct vlan_dev_priv {
  148. unsigned int nr_ingress_mappings;
  149. u32 ingress_priority_map[8];
  150. unsigned int nr_egress_mappings;
  151. struct vlan_priority_tci_mapping *egress_priority_map[16];
  152. __be16 vlan_proto;
  153. u16 vlan_id;
  154. u16 flags;
  155. struct net_device *real_dev;
  156. unsigned char real_dev_addr[ETH_ALEN];
  157. struct proc_dir_entry *dent;
  158. struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
  159. #ifdef CONFIG_NET_POLL_CONTROLLER
  160. struct netpoll *netpoll;
  161. #endif
  162. };
  163. static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
  164. {
  165. return netdev_priv(dev);
  166. }
  167. static inline u16
  168. vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
  169. {
  170. struct vlan_priority_tci_mapping *mp;
  171. smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
  172. mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
  173. while (mp) {
  174. if (mp->priority == skprio) {
  175. return mp->vlan_qos; /* This should already be shifted
  176. * to mask correctly with the
  177. * VLAN's TCI */
  178. }
  179. mp = mp->next;
  180. }
  181. return 0;
  182. }
  183. extern bool vlan_do_receive(struct sk_buff **skb);
  184. extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
  185. extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
  186. extern int vlan_vids_add_by_dev(struct net_device *dev,
  187. const struct net_device *by_dev);
  188. extern void vlan_vids_del_by_dev(struct net_device *dev,
  189. const struct net_device *by_dev);
  190. extern bool vlan_uses_dev(const struct net_device *dev);
  191. #else
  192. static inline struct net_device *
  193. __vlan_find_dev_deep_rcu(struct net_device *real_dev,
  194. __be16 vlan_proto, u16 vlan_id)
  195. {
  196. return NULL;
  197. }
  198. static inline int
  199. vlan_for_each(struct net_device *dev,
  200. int (*action)(struct net_device *dev, int vid, void *arg),
  201. void *arg)
  202. {
  203. return 0;
  204. }
  205. static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  206. {
  207. BUG();
  208. return NULL;
  209. }
  210. static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
  211. {
  212. BUG();
  213. return 0;
  214. }
  215. static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
  216. {
  217. BUG();
  218. return 0;
  219. }
  220. static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
  221. u32 skprio)
  222. {
  223. return 0;
  224. }
  225. static inline bool vlan_do_receive(struct sk_buff **skb)
  226. {
  227. return false;
  228. }
  229. static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  230. {
  231. return 0;
  232. }
  233. static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  234. {
  235. }
  236. static inline int vlan_vids_add_by_dev(struct net_device *dev,
  237. const struct net_device *by_dev)
  238. {
  239. return 0;
  240. }
  241. static inline void vlan_vids_del_by_dev(struct net_device *dev,
  242. const struct net_device *by_dev)
  243. {
  244. }
  245. static inline bool vlan_uses_dev(const struct net_device *dev)
  246. {
  247. return false;
  248. }
  249. #endif
  250. /**
  251. * eth_type_vlan - check for valid vlan ether type.
  252. * @ethertype: ether type to check
  253. *
  254. * Returns true if the ether type is a vlan ether type.
  255. */
  256. static inline bool eth_type_vlan(__be16 ethertype)
  257. {
  258. switch (ethertype) {
  259. case htons(ETH_P_8021Q):
  260. case htons(ETH_P_8021AD):
  261. return true;
  262. default:
  263. return false;
  264. }
  265. }
  266. static inline bool vlan_hw_offload_capable(netdev_features_t features,
  267. __be16 proto)
  268. {
  269. if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
  270. return true;
  271. if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
  272. return true;
  273. return false;
  274. }
  275. /**
  276. * __vlan_insert_inner_tag - inner VLAN tag inserting
  277. * @skb: skbuff to tag
  278. * @vlan_proto: VLAN encapsulation protocol
  279. * @vlan_tci: VLAN TCI to insert
  280. * @mac_len: MAC header length including outer vlan headers
  281. *
  282. * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
  283. * Returns error if skb_cow_head fails.
  284. *
  285. * Does not change skb->protocol so this function can be used during receive.
  286. */
  287. static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
  288. __be16 vlan_proto, u16 vlan_tci,
  289. unsigned int mac_len)
  290. {
  291. struct vlan_ethhdr *veth;
  292. if (skb_cow_head(skb, VLAN_HLEN) < 0)
  293. return -ENOMEM;
  294. skb_push(skb, VLAN_HLEN);
  295. /* Move the mac header sans proto to the beginning of the new header. */
  296. if (likely(mac_len > ETH_TLEN))
  297. memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
  298. skb->mac_header -= VLAN_HLEN;
  299. veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
  300. /* first, the ethernet type */
  301. if (likely(mac_len >= ETH_TLEN)) {
  302. /* h_vlan_encapsulated_proto should already be populated, and
  303. * skb->data has space for h_vlan_proto
  304. */
  305. veth->h_vlan_proto = vlan_proto;
  306. } else {
  307. /* h_vlan_encapsulated_proto should not be populated, and
  308. * skb->data has no space for h_vlan_proto
  309. */
  310. veth->h_vlan_encapsulated_proto = skb->protocol;
  311. }
  312. /* now, the TCI */
  313. veth->h_vlan_TCI = htons(vlan_tci);
  314. return 0;
  315. }
  316. /**
  317. * __vlan_insert_tag - regular VLAN tag inserting
  318. * @skb: skbuff to tag
  319. * @vlan_proto: VLAN encapsulation protocol
  320. * @vlan_tci: VLAN TCI to insert
  321. *
  322. * Inserts the VLAN tag into @skb as part of the payload
  323. * Returns error if skb_cow_head fails.
  324. *
  325. * Does not change skb->protocol so this function can be used during receive.
  326. */
  327. static inline int __vlan_insert_tag(struct sk_buff *skb,
  328. __be16 vlan_proto, u16 vlan_tci)
  329. {
  330. return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
  331. }
  332. /**
  333. * vlan_insert_inner_tag - inner VLAN tag inserting
  334. * @skb: skbuff to tag
  335. * @vlan_proto: VLAN encapsulation protocol
  336. * @vlan_tci: VLAN TCI to insert
  337. * @mac_len: MAC header length including outer vlan headers
  338. *
  339. * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
  340. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  341. *
  342. * Following the skb_unshare() example, in case of error, the calling function
  343. * doesn't have to worry about freeing the original skb.
  344. *
  345. * Does not change skb->protocol so this function can be used during receive.
  346. */
  347. static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
  348. __be16 vlan_proto,
  349. u16 vlan_tci,
  350. unsigned int mac_len)
  351. {
  352. int err;
  353. err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
  354. if (err) {
  355. dev_kfree_skb_any(skb);
  356. return NULL;
  357. }
  358. return skb;
  359. }
  360. /**
  361. * vlan_insert_tag - regular VLAN tag inserting
  362. * @skb: skbuff to tag
  363. * @vlan_proto: VLAN encapsulation protocol
  364. * @vlan_tci: VLAN TCI to insert
  365. *
  366. * Inserts the VLAN tag into @skb as part of the payload
  367. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  368. *
  369. * Following the skb_unshare() example, in case of error, the calling function
  370. * doesn't have to worry about freeing the original skb.
  371. *
  372. * Does not change skb->protocol so this function can be used during receive.
  373. */
  374. static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
  375. __be16 vlan_proto, u16 vlan_tci)
  376. {
  377. return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
  378. }
  379. /**
  380. * vlan_insert_tag_set_proto - regular VLAN tag inserting
  381. * @skb: skbuff to tag
  382. * @vlan_proto: VLAN encapsulation protocol
  383. * @vlan_tci: VLAN TCI to insert
  384. *
  385. * Inserts the VLAN tag into @skb as part of the payload
  386. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  387. *
  388. * Following the skb_unshare() example, in case of error, the calling function
  389. * doesn't have to worry about freeing the original skb.
  390. */
  391. static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
  392. __be16 vlan_proto,
  393. u16 vlan_tci)
  394. {
  395. skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
  396. if (skb)
  397. skb->protocol = vlan_proto;
  398. return skb;
  399. }
  400. /**
  401. * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
  402. * @skb: skbuff to clear
  403. *
  404. * Clears the VLAN information from @skb
  405. */
  406. static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
  407. {
  408. skb->vlan_present = 0;
  409. }
  410. /**
  411. * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
  412. * @dst: skbuff to copy to
  413. * @src: skbuff to copy from
  414. *
  415. * Copies VLAN information from @src to @dst (for branchless code)
  416. */
  417. static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
  418. {
  419. dst->vlan_present = src->vlan_present;
  420. dst->vlan_proto = src->vlan_proto;
  421. dst->vlan_tci = src->vlan_tci;
  422. }
  423. /*
  424. * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
  425. * @skb: skbuff to tag
  426. *
  427. * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
  428. *
  429. * Following the skb_unshare() example, in case of error, the calling function
  430. * doesn't have to worry about freeing the original skb.
  431. */
  432. static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
  433. {
  434. skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
  435. skb_vlan_tag_get(skb));
  436. if (likely(skb))
  437. __vlan_hwaccel_clear_tag(skb);
  438. return skb;
  439. }
  440. /**
  441. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  442. * @skb: skbuff to tag
  443. * @vlan_proto: VLAN encapsulation protocol
  444. * @vlan_tci: VLAN TCI to insert
  445. *
  446. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  447. */
  448. static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
  449. __be16 vlan_proto, u16 vlan_tci)
  450. {
  451. skb->vlan_proto = vlan_proto;
  452. skb->vlan_tci = vlan_tci;
  453. skb->vlan_present = 1;
  454. }
  455. /**
  456. * __vlan_get_tag - get the VLAN ID that is part of the payload
  457. * @skb: skbuff to query
  458. * @vlan_tci: buffer to store value
  459. *
  460. * Returns error if the skb is not of VLAN type
  461. */
  462. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  463. {
  464. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  465. if (!eth_type_vlan(veth->h_vlan_proto))
  466. return -EINVAL;
  467. *vlan_tci = ntohs(veth->h_vlan_TCI);
  468. return 0;
  469. }
  470. /**
  471. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  472. * @skb: skbuff to query
  473. * @vlan_tci: buffer to store value
  474. *
  475. * Returns error if @skb->vlan_tci is not set correctly
  476. */
  477. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  478. u16 *vlan_tci)
  479. {
  480. if (skb_vlan_tag_present(skb)) {
  481. *vlan_tci = skb_vlan_tag_get(skb);
  482. return 0;
  483. } else {
  484. *vlan_tci = 0;
  485. return -EINVAL;
  486. }
  487. }
  488. /**
  489. * vlan_get_tag - get the VLAN ID from the skb
  490. * @skb: skbuff to query
  491. * @vlan_tci: buffer to store value
  492. *
  493. * Returns error if the skb is not VLAN tagged
  494. */
  495. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  496. {
  497. if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
  498. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  499. } else {
  500. return __vlan_get_tag(skb, vlan_tci);
  501. }
  502. }
  503. /**
  504. * vlan_get_protocol - get protocol EtherType.
  505. * @skb: skbuff to query
  506. * @type: first vlan protocol
  507. * @depth: buffer to store length of eth and vlan tags in bytes
  508. *
  509. * Returns the EtherType of the packet, regardless of whether it is
  510. * vlan encapsulated (normal or hardware accelerated) or not.
  511. */
  512. static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
  513. int *depth)
  514. {
  515. unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
  516. /* if type is 802.1Q/AD then the header should already be
  517. * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
  518. * ETH_HLEN otherwise
  519. */
  520. if (eth_type_vlan(type)) {
  521. if (vlan_depth) {
  522. if (WARN_ON(vlan_depth < VLAN_HLEN))
  523. return 0;
  524. vlan_depth -= VLAN_HLEN;
  525. } else {
  526. vlan_depth = ETH_HLEN;
  527. }
  528. do {
  529. struct vlan_hdr vhdr, *vh;
  530. vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
  531. if (unlikely(!vh || !--parse_depth))
  532. return 0;
  533. type = vh->h_vlan_encapsulated_proto;
  534. vlan_depth += VLAN_HLEN;
  535. } while (eth_type_vlan(type));
  536. }
  537. if (depth)
  538. *depth = vlan_depth;
  539. return type;
  540. }
  541. /**
  542. * vlan_get_protocol - get protocol EtherType.
  543. * @skb: skbuff to query
  544. *
  545. * Returns the EtherType of the packet, regardless of whether it is
  546. * vlan encapsulated (normal or hardware accelerated) or not.
  547. */
  548. static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
  549. {
  550. return __vlan_get_protocol(skb, skb->protocol, NULL);
  551. }
  552. /* A getter for the SKB protocol field which will handle VLAN tags consistently
  553. * whether VLAN acceleration is enabled or not.
  554. */
  555. static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
  556. {
  557. if (!skip_vlan)
  558. /* VLAN acceleration strips the VLAN header from the skb and
  559. * moves it to skb->vlan_proto
  560. */
  561. return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
  562. return vlan_get_protocol(skb);
  563. }
  564. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  565. struct vlan_hdr *vhdr)
  566. {
  567. __be16 proto;
  568. unsigned short *rawp;
  569. /*
  570. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  571. * three protocols care about.
  572. */
  573. proto = vhdr->h_vlan_encapsulated_proto;
  574. if (eth_proto_is_802_3(proto)) {
  575. skb->protocol = proto;
  576. return;
  577. }
  578. rawp = (unsigned short *)(vhdr + 1);
  579. if (*rawp == 0xFFFF)
  580. /*
  581. * This is a magic hack to spot IPX packets. Older Novell
  582. * breaks the protocol design and runs IPX over 802.3 without
  583. * an 802.2 LLC layer. We look for FFFF which isn't a used
  584. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  585. * but does for the rest.
  586. */
  587. skb->protocol = htons(ETH_P_802_3);
  588. else
  589. /*
  590. * Real 802.2 LLC
  591. */
  592. skb->protocol = htons(ETH_P_802_2);
  593. }
  594. /**
  595. * skb_vlan_tagged - check if skb is vlan tagged.
  596. * @skb: skbuff to query
  597. *
  598. * Returns true if the skb is tagged, regardless of whether it is hardware
  599. * accelerated or not.
  600. */
  601. static inline bool skb_vlan_tagged(const struct sk_buff *skb)
  602. {
  603. if (!skb_vlan_tag_present(skb) &&
  604. likely(!eth_type_vlan(skb->protocol)))
  605. return false;
  606. return true;
  607. }
  608. /**
  609. * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
  610. * @skb: skbuff to query
  611. *
  612. * Returns true if the skb is tagged with multiple vlan headers, regardless
  613. * of whether it is hardware accelerated or not.
  614. */
  615. static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
  616. {
  617. __be16 protocol = skb->protocol;
  618. if (!skb_vlan_tag_present(skb)) {
  619. struct vlan_ethhdr *veh;
  620. if (likely(!eth_type_vlan(protocol)))
  621. return false;
  622. if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
  623. return false;
  624. veh = (struct vlan_ethhdr *)skb->data;
  625. protocol = veh->h_vlan_encapsulated_proto;
  626. }
  627. if (!eth_type_vlan(protocol))
  628. return false;
  629. return true;
  630. }
  631. /**
  632. * vlan_features_check - drop unsafe features for skb with multiple tags.
  633. * @skb: skbuff to query
  634. * @features: features to be checked
  635. *
  636. * Returns features without unsafe ones if the skb has multiple tags.
  637. */
  638. static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
  639. netdev_features_t features)
  640. {
  641. if (skb_vlan_tagged_multi(skb)) {
  642. /* In the case of multi-tagged packets, use a direct mask
  643. * instead of using netdev_interesect_features(), to make
  644. * sure that only devices supporting NETIF_F_HW_CSUM will
  645. * have checksum offloading support.
  646. */
  647. features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
  648. NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
  649. NETIF_F_HW_VLAN_STAG_TX;
  650. }
  651. return features;
  652. }
  653. /**
  654. * compare_vlan_header - Compare two vlan headers
  655. * @h1: Pointer to vlan header
  656. * @h2: Pointer to vlan header
  657. *
  658. * Compare two vlan headers, returns 0 if equal.
  659. *
  660. * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
  661. */
  662. static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
  663. const struct vlan_hdr *h2)
  664. {
  665. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  666. return *(u32 *)h1 ^ *(u32 *)h2;
  667. #else
  668. return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
  669. ((__force u32)h1->h_vlan_encapsulated_proto ^
  670. (__force u32)h2->h_vlan_encapsulated_proto);
  671. #endif
  672. }
  673. #endif /* !(_LINUX_IF_VLAN_H_) */