inet_ecn.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef _INET_ECN_H_
  2. #define _INET_ECN_H_
  3. #include <linux/ip.h>
  4. #include <linux/skbuff.h>
  5. #include <net/inet_sock.h>
  6. #include <net/dsfield.h>
  7. enum {
  8. INET_ECN_NOT_ECT = 0,
  9. INET_ECN_ECT_1 = 1,
  10. INET_ECN_ECT_0 = 2,
  11. INET_ECN_CE = 3,
  12. INET_ECN_MASK = 3,
  13. };
  14. static inline int INET_ECN_is_ce(__u8 dsfield)
  15. {
  16. return (dsfield & INET_ECN_MASK) == INET_ECN_CE;
  17. }
  18. static inline int INET_ECN_is_not_ect(__u8 dsfield)
  19. {
  20. return (dsfield & INET_ECN_MASK) == INET_ECN_NOT_ECT;
  21. }
  22. static inline int INET_ECN_is_capable(__u8 dsfield)
  23. {
  24. return dsfield & INET_ECN_ECT_0;
  25. }
  26. /*
  27. * RFC 3168 9.1.1
  28. * The full-functionality option for ECN encapsulation is to copy the
  29. * ECN codepoint of the inside header to the outside header on
  30. * encapsulation if the inside header is not-ECT or ECT, and to set the
  31. * ECN codepoint of the outside header to ECT(0) if the ECN codepoint of
  32. * the inside header is CE.
  33. */
  34. static inline __u8 INET_ECN_encapsulate(__u8 outer, __u8 inner)
  35. {
  36. outer &= ~INET_ECN_MASK;
  37. outer |= !INET_ECN_is_ce(inner) ? (inner & INET_ECN_MASK) :
  38. INET_ECN_ECT_0;
  39. return outer;
  40. }
  41. static inline void INET_ECN_xmit(struct sock *sk)
  42. {
  43. inet_sk(sk)->tos |= INET_ECN_ECT_0;
  44. if (inet6_sk(sk) != NULL)
  45. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  46. }
  47. static inline void INET_ECN_dontxmit(struct sock *sk)
  48. {
  49. inet_sk(sk)->tos &= ~INET_ECN_MASK;
  50. if (inet6_sk(sk) != NULL)
  51. inet6_sk(sk)->tclass &= ~INET_ECN_MASK;
  52. }
  53. #define IP6_ECN_flow_init(label) do { \
  54. (label) &= ~htonl(INET_ECN_MASK << 20); \
  55. } while (0)
  56. #define IP6_ECN_flow_xmit(sk, label) do { \
  57. if (INET_ECN_is_capable(inet6_sk(sk)->tclass)) \
  58. (label) |= htonl(INET_ECN_ECT_0 << 20); \
  59. } while (0)
  60. static inline int IP_ECN_set_ce(struct iphdr *iph)
  61. {
  62. u32 check = (__force u32)iph->check;
  63. u32 ecn = (iph->tos + 1) & INET_ECN_MASK;
  64. /*
  65. * After the last operation we have (in binary):
  66. * INET_ECN_NOT_ECT => 01
  67. * INET_ECN_ECT_1 => 10
  68. * INET_ECN_ECT_0 => 11
  69. * INET_ECN_CE => 00
  70. */
  71. if (!(ecn & 2))
  72. return !ecn;
  73. /*
  74. * The following gives us:
  75. * INET_ECN_ECT_1 => check += htons(0xFFFD)
  76. * INET_ECN_ECT_0 => check += htons(0xFFFE)
  77. */
  78. check += (__force u16)htons(0xFFFB) + (__force u16)htons(ecn);
  79. iph->check = (__force __sum16)(check + (check>=0xFFFF));
  80. iph->tos |= INET_ECN_CE;
  81. return 1;
  82. }
  83. static inline void IP_ECN_clear(struct iphdr *iph)
  84. {
  85. iph->tos &= ~INET_ECN_MASK;
  86. }
  87. static inline void ipv4_copy_dscp(unsigned int dscp, struct iphdr *inner)
  88. {
  89. dscp &= ~INET_ECN_MASK;
  90. ipv4_change_dsfield(inner, INET_ECN_MASK, dscp);
  91. }
  92. struct ipv6hdr;
  93. static inline int IP6_ECN_set_ce(struct ipv6hdr *iph)
  94. {
  95. if (INET_ECN_is_not_ect(ipv6_get_dsfield(iph)))
  96. return 0;
  97. *(__be32*)iph |= htonl(INET_ECN_CE << 20);
  98. return 1;
  99. }
  100. static inline void IP6_ECN_clear(struct ipv6hdr *iph)
  101. {
  102. *(__be32*)iph &= ~htonl(INET_ECN_MASK << 20);
  103. }
  104. static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)
  105. {
  106. dscp &= ~INET_ECN_MASK;
  107. ipv6_change_dsfield(inner, INET_ECN_MASK, dscp);
  108. }
  109. static inline int INET_ECN_set_ce(struct sk_buff *skb)
  110. {
  111. switch (skb->protocol) {
  112. case cpu_to_be16(ETH_P_IP):
  113. if (skb->network_header + sizeof(struct iphdr) <= skb->tail)
  114. return IP_ECN_set_ce(ip_hdr(skb));
  115. break;
  116. case cpu_to_be16(ETH_P_IPV6):
  117. if (skb->network_header + sizeof(struct ipv6hdr) <= skb->tail)
  118. return IP6_ECN_set_ce(ipv6_hdr(skb));
  119. break;
  120. }
  121. return 0;
  122. }
  123. #endif