gso.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _NET_GSO_H
  3. #define _NET_GSO_H
  4. #include <linux/skbuff.h>
  5. /* Keeps track of mac header offset relative to skb->head.
  6. * It is useful for TSO of Tunneling protocol. e.g. GRE.
  7. * For non-tunnel skb it points to skb_mac_header() and for
  8. * tunnel skb it points to outer mac header.
  9. * Keeps track of level of encapsulation of network headers.
  10. */
  11. struct skb_gso_cb {
  12. union {
  13. int mac_offset;
  14. int data_offset;
  15. };
  16. int encap_level;
  17. __wsum csum;
  18. __u16 csum_start;
  19. };
  20. #define SKB_GSO_CB_OFFSET 32
  21. #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET))
  22. static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
  23. {
  24. return (skb_mac_header(inner_skb) - inner_skb->head) -
  25. SKB_GSO_CB(inner_skb)->mac_offset;
  26. }
  27. static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
  28. {
  29. int new_headroom, headroom;
  30. int ret;
  31. headroom = skb_headroom(skb);
  32. ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
  33. if (ret)
  34. return ret;
  35. new_headroom = skb_headroom(skb);
  36. SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
  37. return 0;
  38. }
  39. static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res)
  40. {
  41. /* Do not update partial checksums if remote checksum is enabled. */
  42. if (skb->remcsum_offload)
  43. return;
  44. SKB_GSO_CB(skb)->csum = res;
  45. SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head;
  46. }
  47. /* Compute the checksum for a gso segment. First compute the checksum value
  48. * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and
  49. * then add in skb->csum (checksum from csum_start to end of packet).
  50. * skb->csum and csum_start are then updated to reflect the checksum of the
  51. * resultant packet starting from the transport header-- the resultant checksum
  52. * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo
  53. * header.
  54. */
  55. static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
  56. {
  57. unsigned char *csum_start = skb_transport_header(skb);
  58. int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start;
  59. __wsum partial = SKB_GSO_CB(skb)->csum;
  60. SKB_GSO_CB(skb)->csum = res;
  61. SKB_GSO_CB(skb)->csum_start = csum_start - skb->head;
  62. return csum_fold(csum_partial(csum_start, plen, partial));
  63. }
  64. struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
  65. netdev_features_t features, bool tx_path);
  66. static inline struct sk_buff *skb_gso_segment(struct sk_buff *skb,
  67. netdev_features_t features)
  68. {
  69. return __skb_gso_segment(skb, features, true);
  70. }
  71. struct sk_buff *skb_eth_gso_segment(struct sk_buff *skb,
  72. netdev_features_t features, __be16 type);
  73. struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
  74. netdev_features_t features);
  75. bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu);
  76. bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len);
  77. static inline void skb_gso_error_unwind(struct sk_buff *skb, __be16 protocol,
  78. int pulled_hlen, u16 mac_offset,
  79. int mac_len)
  80. {
  81. skb->protocol = protocol;
  82. skb->encapsulation = 1;
  83. skb_push(skb, pulled_hlen);
  84. skb_reset_transport_header(skb);
  85. skb->mac_header = mac_offset;
  86. skb->network_header = skb->mac_header + mac_len;
  87. skb->mac_len = mac_len;
  88. }
  89. #endif /* _NET_GSO_H */