inet_ecn.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. static inline __u8 INET_ECN_encapsulate(__u8 outer, __u8 inner)
  27. {
  28. outer &= ~INET_ECN_MASK;
  29. outer |= !INET_ECN_is_ce(inner) ? (inner & INET_ECN_MASK) :
  30. INET_ECN_ECT_0;
  31. return outer;
  32. }
  33. static inline void INET_ECN_xmit(struct sock *sk)
  34. {
  35. inet_sk(sk)->tos |= INET_ECN_ECT_0;
  36. if (inet6_sk(sk) != NULL)
  37. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  38. }
  39. static inline void INET_ECN_dontxmit(struct sock *sk)
  40. {
  41. inet_sk(sk)->tos &= ~INET_ECN_MASK;
  42. if (inet6_sk(sk) != NULL)
  43. inet6_sk(sk)->tclass &= ~INET_ECN_MASK;
  44. }
  45. #define IP6_ECN_flow_init(label) do { \
  46. (label) &= ~htonl(INET_ECN_MASK << 20); \
  47. } while (0)
  48. #define IP6_ECN_flow_xmit(sk, label) do { \
  49. if (INET_ECN_is_capable(inet6_sk(sk)->tclass)) \
  50. (label) |= htonl(INET_ECN_ECT_0 << 20); \
  51. } while (0)
  52. static inline int IP_ECN_set_ce(struct iphdr *iph)
  53. {
  54. u32 check = (__force u32)iph->check;
  55. u32 ecn = (iph->tos + 1) & INET_ECN_MASK;
  56. /*
  57. * After the last operation we have (in binary):
  58. * INET_ECN_NOT_ECT => 01
  59. * INET_ECN_ECT_1 => 10
  60. * INET_ECN_ECT_0 => 11
  61. * INET_ECN_CE => 00
  62. */
  63. if (!(ecn & 2))
  64. return !ecn;
  65. /*
  66. * The following gives us:
  67. * INET_ECN_ECT_1 => check += htons(0xFFFD)
  68. * INET_ECN_ECT_0 => check += htons(0xFFFE)
  69. */
  70. check += (__force u16)htons(0xFFFB) + (__force u16)htons(ecn);
  71. iph->check = (__force __sum16)(check + (check>=0xFFFF));
  72. iph->tos |= INET_ECN_CE;
  73. return 1;
  74. }
  75. static inline void IP_ECN_clear(struct iphdr *iph)
  76. {
  77. iph->tos &= ~INET_ECN_MASK;
  78. }
  79. static inline void ipv4_copy_dscp(unsigned int dscp, struct iphdr *inner)
  80. {
  81. dscp &= ~INET_ECN_MASK;
  82. ipv4_change_dsfield(inner, INET_ECN_MASK, dscp);
  83. }
  84. struct ipv6hdr;
  85. static inline int IP6_ECN_set_ce(struct ipv6hdr *iph)
  86. {
  87. if (INET_ECN_is_not_ect(ipv6_get_dsfield(iph)))
  88. return 0;
  89. *(__be32*)iph |= htonl(INET_ECN_CE << 20);
  90. return 1;
  91. }
  92. static inline void IP6_ECN_clear(struct ipv6hdr *iph)
  93. {
  94. *(__be32*)iph &= ~htonl(INET_ECN_MASK << 20);
  95. }
  96. static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)
  97. {
  98. dscp &= ~INET_ECN_MASK;
  99. ipv6_change_dsfield(inner, INET_ECN_MASK, dscp);
  100. }
  101. static inline int INET_ECN_set_ce(struct sk_buff *skb)
  102. {
  103. switch (skb->protocol) {
  104. case cpu_to_be16(ETH_P_IP):
  105. if (skb->network_header + sizeof(struct iphdr) <= skb->tail)
  106. return IP_ECN_set_ce(ip_hdr(skb));
  107. break;
  108. case cpu_to_be16(ETH_P_IPV6):
  109. if (skb->network_header + sizeof(struct ipv6hdr) <= skb->tail)
  110. return IP6_ECN_set_ce(ipv6_hdr(skb));
  111. break;
  112. }
  113. return 0;
  114. }
  115. #endif