if_vlan.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 bool is_vlan_dev(const struct net_device *dev)
  67. {
  68. return dev->priv_flags & IFF_802_1Q_VLAN;
  69. }
  70. #define skb_vlan_tag_present(__skb) ((__skb)->vlan_tci & VLAN_TAG_PRESENT)
  71. #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
  72. #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
  73. #define skb_vlan_tag_get_prio(__skb) ((__skb)->vlan_tci & VLAN_PRIO_MASK)
  74. static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
  75. {
  76. ASSERT_RTNL();
  77. return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
  78. }
  79. static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
  80. {
  81. ASSERT_RTNL();
  82. call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
  83. }
  84. static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
  85. {
  86. ASSERT_RTNL();
  87. return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
  88. }
  89. static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
  90. {
  91. ASSERT_RTNL();
  92. call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
  93. }
  94. /**
  95. * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
  96. * @rx_packets: number of received packets
  97. * @rx_bytes: number of received bytes
  98. * @rx_multicast: number of received multicast packets
  99. * @tx_packets: number of transmitted packets
  100. * @tx_bytes: number of transmitted bytes
  101. * @syncp: synchronization point for 64bit counters
  102. * @rx_errors: number of rx errors
  103. * @tx_dropped: number of tx drops
  104. */
  105. struct vlan_pcpu_stats {
  106. u64 rx_packets;
  107. u64 rx_bytes;
  108. u64 rx_multicast;
  109. u64 tx_packets;
  110. u64 tx_bytes;
  111. struct u64_stats_sync syncp;
  112. u32 rx_errors;
  113. u32 tx_dropped;
  114. };
  115. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  116. extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
  117. __be16 vlan_proto, u16 vlan_id);
  118. extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
  119. extern u16 vlan_dev_vlan_id(const struct net_device *dev);
  120. extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
  121. /**
  122. * struct vlan_priority_tci_mapping - vlan egress priority mappings
  123. * @priority: skb priority
  124. * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
  125. * @next: pointer to next struct
  126. */
  127. struct vlan_priority_tci_mapping {
  128. u32 priority;
  129. u16 vlan_qos;
  130. struct vlan_priority_tci_mapping *next;
  131. };
  132. struct proc_dir_entry;
  133. struct netpoll;
  134. /**
  135. * struct vlan_dev_priv - VLAN private device data
  136. * @nr_ingress_mappings: number of ingress priority mappings
  137. * @ingress_priority_map: ingress priority mappings
  138. * @nr_egress_mappings: number of egress priority mappings
  139. * @egress_priority_map: hash of egress priority mappings
  140. * @vlan_proto: VLAN encapsulation protocol
  141. * @vlan_id: VLAN identifier
  142. * @flags: device flags
  143. * @real_dev: underlying netdevice
  144. * @real_dev_addr: address of underlying netdevice
  145. * @dent: proc dir entry
  146. * @vlan_pcpu_stats: ptr to percpu rx stats
  147. */
  148. struct vlan_dev_priv {
  149. unsigned int nr_ingress_mappings;
  150. u32 ingress_priority_map[8];
  151. unsigned int nr_egress_mappings;
  152. struct vlan_priority_tci_mapping *egress_priority_map[16];
  153. __be16 vlan_proto;
  154. u16 vlan_id;
  155. u16 flags;
  156. struct net_device *real_dev;
  157. unsigned char real_dev_addr[ETH_ALEN];
  158. struct proc_dir_entry *dent;
  159. struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
  160. #ifdef CONFIG_NET_POLL_CONTROLLER
  161. struct netpoll *netpoll;
  162. #endif
  163. unsigned int nest_level;
  164. };
  165. static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
  166. {
  167. return netdev_priv(dev);
  168. }
  169. static inline u16
  170. vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
  171. {
  172. struct vlan_priority_tci_mapping *mp;
  173. smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
  174. mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
  175. while (mp) {
  176. if (mp->priority == skprio) {
  177. return mp->vlan_qos; /* This should already be shifted
  178. * to mask correctly with the
  179. * VLAN's TCI */
  180. }
  181. mp = mp->next;
  182. }
  183. return 0;
  184. }
  185. extern bool vlan_do_receive(struct sk_buff **skb);
  186. extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
  187. extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
  188. extern int vlan_vids_add_by_dev(struct net_device *dev,
  189. const struct net_device *by_dev);
  190. extern void vlan_vids_del_by_dev(struct net_device *dev,
  191. const struct net_device *by_dev);
  192. extern bool vlan_uses_dev(const struct net_device *dev);
  193. static inline int vlan_get_encap_level(struct net_device *dev)
  194. {
  195. BUG_ON(!is_vlan_dev(dev));
  196. return vlan_dev_priv(dev)->nest_level;
  197. }
  198. #else
  199. static inline struct net_device *
  200. __vlan_find_dev_deep_rcu(struct net_device *real_dev,
  201. __be16 vlan_proto, u16 vlan_id)
  202. {
  203. return NULL;
  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. static inline int vlan_get_encap_level(struct net_device *dev)
  250. {
  251. BUG();
  252. return 0;
  253. }
  254. #endif
  255. /**
  256. * eth_type_vlan - check for valid vlan ether type.
  257. * @ethertype: ether type to check
  258. *
  259. * Returns true if the ether type is a vlan ether type.
  260. */
  261. static inline bool eth_type_vlan(__be16 ethertype)
  262. {
  263. switch (ethertype) {
  264. case htons(ETH_P_8021Q):
  265. case htons(ETH_P_8021AD):
  266. return true;
  267. default:
  268. return false;
  269. }
  270. }
  271. static inline bool vlan_hw_offload_capable(netdev_features_t features,
  272. __be16 proto)
  273. {
  274. if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
  275. return true;
  276. if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
  277. return true;
  278. return false;
  279. }
  280. /**
  281. * __vlan_insert_inner_tag - inner VLAN tag inserting
  282. * @skb: skbuff to tag
  283. * @vlan_proto: VLAN encapsulation protocol
  284. * @vlan_tci: VLAN TCI to insert
  285. * @mac_len: MAC header length including outer vlan headers
  286. *
  287. * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
  288. * Returns error if skb_cow_head fails.
  289. *
  290. * Does not change skb->protocol so this function can be used during receive.
  291. */
  292. static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
  293. __be16 vlan_proto, u16 vlan_tci,
  294. unsigned int mac_len)
  295. {
  296. struct vlan_ethhdr *veth;
  297. if (skb_cow_head(skb, VLAN_HLEN) < 0)
  298. return -ENOMEM;
  299. skb_push(skb, VLAN_HLEN);
  300. /* Move the mac header sans proto to the beginning of the new header. */
  301. if (likely(mac_len > ETH_TLEN))
  302. memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
  303. skb->mac_header -= VLAN_HLEN;
  304. veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
  305. /* first, the ethernet type */
  306. if (likely(mac_len >= ETH_TLEN)) {
  307. /* h_vlan_encapsulated_proto should already be populated, and
  308. * skb->data has space for h_vlan_proto
  309. */
  310. veth->h_vlan_proto = vlan_proto;
  311. } else {
  312. /* h_vlan_encapsulated_proto should not be populated, and
  313. * skb->data has no space for h_vlan_proto
  314. */
  315. veth->h_vlan_encapsulated_proto = skb->protocol;
  316. }
  317. /* now, the TCI */
  318. veth->h_vlan_TCI = htons(vlan_tci);
  319. return 0;
  320. }
  321. /**
  322. * __vlan_insert_tag - regular VLAN tag inserting
  323. * @skb: skbuff to tag
  324. * @vlan_proto: VLAN encapsulation protocol
  325. * @vlan_tci: VLAN TCI to insert
  326. *
  327. * Inserts the VLAN tag into @skb as part of the payload
  328. * Returns error if skb_cow_head fails.
  329. *
  330. * Does not change skb->protocol so this function can be used during receive.
  331. */
  332. static inline int __vlan_insert_tag(struct sk_buff *skb,
  333. __be16 vlan_proto, u16 vlan_tci)
  334. {
  335. return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
  336. }
  337. /**
  338. * vlan_insert_inner_tag - inner VLAN tag inserting
  339. * @skb: skbuff to tag
  340. * @vlan_proto: VLAN encapsulation protocol
  341. * @vlan_tci: VLAN TCI to insert
  342. * @mac_len: MAC header length including outer vlan headers
  343. *
  344. * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
  345. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  346. *
  347. * Following the skb_unshare() example, in case of error, the calling function
  348. * doesn't have to worry about freeing the original skb.
  349. *
  350. * Does not change skb->protocol so this function can be used during receive.
  351. */
  352. static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
  353. __be16 vlan_proto,
  354. u16 vlan_tci,
  355. unsigned int mac_len)
  356. {
  357. int err;
  358. err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
  359. if (err) {
  360. dev_kfree_skb_any(skb);
  361. return NULL;
  362. }
  363. return skb;
  364. }
  365. /**
  366. * vlan_insert_tag - regular VLAN tag inserting
  367. * @skb: skbuff to tag
  368. * @vlan_proto: VLAN encapsulation protocol
  369. * @vlan_tci: VLAN TCI to insert
  370. *
  371. * Inserts the VLAN tag into @skb as part of the payload
  372. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  373. *
  374. * Following the skb_unshare() example, in case of error, the calling function
  375. * doesn't have to worry about freeing the original skb.
  376. *
  377. * Does not change skb->protocol so this function can be used during receive.
  378. */
  379. static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
  380. __be16 vlan_proto, u16 vlan_tci)
  381. {
  382. return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
  383. }
  384. /**
  385. * vlan_insert_tag_set_proto - regular VLAN tag inserting
  386. * @skb: skbuff to tag
  387. * @vlan_proto: VLAN encapsulation protocol
  388. * @vlan_tci: VLAN TCI to insert
  389. *
  390. * Inserts the VLAN tag into @skb as part of the payload
  391. * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
  392. *
  393. * Following the skb_unshare() example, in case of error, the calling function
  394. * doesn't have to worry about freeing the original skb.
  395. */
  396. static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
  397. __be16 vlan_proto,
  398. u16 vlan_tci)
  399. {
  400. skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
  401. if (skb)
  402. skb->protocol = vlan_proto;
  403. return skb;
  404. }
  405. /*
  406. * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
  407. * @skb: skbuff to tag
  408. *
  409. * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
  410. *
  411. * Following the skb_unshare() example, in case of error, the calling function
  412. * doesn't have to worry about freeing the original skb.
  413. */
  414. static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
  415. {
  416. skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
  417. skb_vlan_tag_get(skb));
  418. if (likely(skb))
  419. skb->vlan_tci = 0;
  420. return skb;
  421. }
  422. /**
  423. * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
  424. * @skb: skbuff to tag
  425. * @vlan_proto: VLAN encapsulation protocol
  426. * @vlan_tci: VLAN TCI to insert
  427. *
  428. * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
  429. */
  430. static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
  431. __be16 vlan_proto, u16 vlan_tci)
  432. {
  433. skb->vlan_proto = vlan_proto;
  434. skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
  435. }
  436. /**
  437. * __vlan_get_tag - get the VLAN ID that is part of the payload
  438. * @skb: skbuff to query
  439. * @vlan_tci: buffer to store value
  440. *
  441. * Returns error if the skb is not of VLAN type
  442. */
  443. static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  444. {
  445. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
  446. if (!eth_type_vlan(veth->h_vlan_proto))
  447. return -EINVAL;
  448. *vlan_tci = ntohs(veth->h_vlan_TCI);
  449. return 0;
  450. }
  451. /**
  452. * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
  453. * @skb: skbuff to query
  454. * @vlan_tci: buffer to store value
  455. *
  456. * Returns error if @skb->vlan_tci is not set correctly
  457. */
  458. static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
  459. u16 *vlan_tci)
  460. {
  461. if (skb_vlan_tag_present(skb)) {
  462. *vlan_tci = skb_vlan_tag_get(skb);
  463. return 0;
  464. } else {
  465. *vlan_tci = 0;
  466. return -EINVAL;
  467. }
  468. }
  469. #define HAVE_VLAN_GET_TAG
  470. /**
  471. * vlan_get_tag - get the VLAN ID from the skb
  472. * @skb: skbuff to query
  473. * @vlan_tci: buffer to store value
  474. *
  475. * Returns error if the skb is not VLAN tagged
  476. */
  477. static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
  478. {
  479. if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
  480. return __vlan_hwaccel_get_tag(skb, vlan_tci);
  481. } else {
  482. return __vlan_get_tag(skb, vlan_tci);
  483. }
  484. }
  485. /**
  486. * vlan_get_protocol - get protocol EtherType.
  487. * @skb: skbuff to query
  488. * @type: first vlan protocol
  489. * @depth: buffer to store length of eth and vlan tags in bytes
  490. *
  491. * Returns the EtherType of the packet, regardless of whether it is
  492. * vlan encapsulated (normal or hardware accelerated) or not.
  493. */
  494. static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,
  495. int *depth)
  496. {
  497. unsigned int vlan_depth = skb->mac_len;
  498. /* if type is 802.1Q/AD then the header should already be
  499. * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
  500. * ETH_HLEN otherwise
  501. */
  502. if (eth_type_vlan(type)) {
  503. if (vlan_depth) {
  504. if (WARN_ON(vlan_depth < VLAN_HLEN))
  505. return 0;
  506. vlan_depth -= VLAN_HLEN;
  507. } else {
  508. vlan_depth = ETH_HLEN;
  509. }
  510. do {
  511. struct vlan_hdr *vh;
  512. if (unlikely(!pskb_may_pull(skb,
  513. vlan_depth + VLAN_HLEN)))
  514. return 0;
  515. vh = (struct vlan_hdr *)(skb->data + vlan_depth);
  516. type = vh->h_vlan_encapsulated_proto;
  517. vlan_depth += VLAN_HLEN;
  518. } while (eth_type_vlan(type));
  519. }
  520. if (depth)
  521. *depth = vlan_depth;
  522. return type;
  523. }
  524. /**
  525. * vlan_get_protocol - get protocol EtherType.
  526. * @skb: skbuff to query
  527. *
  528. * Returns the EtherType of the packet, regardless of whether it is
  529. * vlan encapsulated (normal or hardware accelerated) or not.
  530. */
  531. static inline __be16 vlan_get_protocol(struct sk_buff *skb)
  532. {
  533. return __vlan_get_protocol(skb, skb->protocol, NULL);
  534. }
  535. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  536. struct vlan_hdr *vhdr)
  537. {
  538. __be16 proto;
  539. unsigned short *rawp;
  540. /*
  541. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  542. * three protocols care about.
  543. */
  544. proto = vhdr->h_vlan_encapsulated_proto;
  545. if (eth_proto_is_802_3(proto)) {
  546. skb->protocol = proto;
  547. return;
  548. }
  549. rawp = (unsigned short *)(vhdr + 1);
  550. if (*rawp == 0xFFFF)
  551. /*
  552. * This is a magic hack to spot IPX packets. Older Novell
  553. * breaks the protocol design and runs IPX over 802.3 without
  554. * an 802.2 LLC layer. We look for FFFF which isn't a used
  555. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  556. * but does for the rest.
  557. */
  558. skb->protocol = htons(ETH_P_802_3);
  559. else
  560. /*
  561. * Real 802.2 LLC
  562. */
  563. skb->protocol = htons(ETH_P_802_2);
  564. }
  565. /**
  566. * skb_vlan_tagged - check if skb is vlan tagged.
  567. * @skb: skbuff to query
  568. *
  569. * Returns true if the skb is tagged, regardless of whether it is hardware
  570. * accelerated or not.
  571. */
  572. static inline bool skb_vlan_tagged(const struct sk_buff *skb)
  573. {
  574. if (!skb_vlan_tag_present(skb) &&
  575. likely(!eth_type_vlan(skb->protocol)))
  576. return false;
  577. return true;
  578. }
  579. /**
  580. * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
  581. * @skb: skbuff to query
  582. *
  583. * Returns true if the skb is tagged with multiple vlan headers, regardless
  584. * of whether it is hardware accelerated or not.
  585. */
  586. static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
  587. {
  588. __be16 protocol = skb->protocol;
  589. if (!skb_vlan_tag_present(skb)) {
  590. struct vlan_ethhdr *veh;
  591. if (likely(!eth_type_vlan(protocol)))
  592. return false;
  593. if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
  594. return false;
  595. veh = (struct vlan_ethhdr *)skb->data;
  596. protocol = veh->h_vlan_encapsulated_proto;
  597. }
  598. if (!eth_type_vlan(protocol))
  599. return false;
  600. return true;
  601. }
  602. /**
  603. * vlan_features_check - drop unsafe features for skb with multiple tags.
  604. * @skb: skbuff to query
  605. * @features: features to be checked
  606. *
  607. * Returns features without unsafe ones if the skb has multiple tags.
  608. */
  609. static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
  610. netdev_features_t features)
  611. {
  612. if (skb_vlan_tagged_multi(skb)) {
  613. /* In the case of multi-tagged packets, use a direct mask
  614. * instead of using netdev_interesect_features(), to make
  615. * sure that only devices supporting NETIF_F_HW_CSUM will
  616. * have checksum offloading support.
  617. */
  618. features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
  619. NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
  620. NETIF_F_HW_VLAN_STAG_TX;
  621. }
  622. return features;
  623. }
  624. /**
  625. * compare_vlan_header - Compare two vlan headers
  626. * @h1: Pointer to vlan header
  627. * @h2: Pointer to vlan header
  628. *
  629. * Compare two vlan headers, returns 0 if equal.
  630. *
  631. * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
  632. */
  633. static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
  634. const struct vlan_hdr *h2)
  635. {
  636. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  637. return *(u32 *)h1 ^ *(u32 *)h2;
  638. #else
  639. return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
  640. ((__force u32)h1->h_vlan_encapsulated_proto ^
  641. (__force u32)h2->h_vlan_encapsulated_proto);
  642. #endif
  643. }
  644. #endif /* !(_LINUX_IF_VLAN_H_) */