inet_ecn.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. extern int sysctl_tunnel_ecn_log;
  15. static inline int INET_ECN_is_ce(__u8 dsfield)
  16. {
  17. return (dsfield & INET_ECN_MASK) == INET_ECN_CE;
  18. }
  19. static inline int INET_ECN_is_not_ect(__u8 dsfield)
  20. {
  21. return (dsfield & INET_ECN_MASK) == INET_ECN_NOT_ECT;
  22. }
  23. static inline int INET_ECN_is_capable(__u8 dsfield)
  24. {
  25. return dsfield & INET_ECN_ECT_0;
  26. }
  27. /*
  28. * RFC 3168 9.1.1
  29. * The full-functionality option for ECN encapsulation is to copy the
  30. * ECN codepoint of the inside header to the outside header on
  31. * encapsulation if the inside header is not-ECT or ECT, and to set the
  32. * ECN codepoint of the outside header to ECT(0) if the ECN codepoint of
  33. * the inside header is CE.
  34. */
  35. static inline __u8 INET_ECN_encapsulate(__u8 outer, __u8 inner)
  36. {
  37. outer &= ~INET_ECN_MASK;
  38. outer |= !INET_ECN_is_ce(inner) ? (inner & INET_ECN_MASK) :
  39. INET_ECN_ECT_0;
  40. return outer;
  41. }
  42. static inline void INET_ECN_xmit(struct sock *sk)
  43. {
  44. inet_sk(sk)->tos |= INET_ECN_ECT_0;
  45. if (inet6_sk(sk) != NULL)
  46. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  47. }
  48. static inline void INET_ECN_dontxmit(struct sock *sk)
  49. {
  50. inet_sk(sk)->tos &= ~INET_ECN_MASK;
  51. if (inet6_sk(sk) != NULL)
  52. inet6_sk(sk)->tclass &= ~INET_ECN_MASK;
  53. }
  54. #define IP6_ECN_flow_init(label) do { \
  55. (label) &= ~htonl(INET_ECN_MASK << 20); \
  56. } while (0)
  57. #define IP6_ECN_flow_xmit(sk, label) do { \
  58. if (INET_ECN_is_capable(inet6_sk(sk)->tclass)) \
  59. (label) |= htonl(INET_ECN_ECT_0 << 20); \
  60. } while (0)
  61. static inline int IP_ECN_set_ce(struct iphdr *iph)
  62. {
  63. u32 check = (__force u32)iph->check;
  64. u32 ecn = (iph->tos + 1) & INET_ECN_MASK;
  65. /*
  66. * After the last operation we have (in binary):
  67. * INET_ECN_NOT_ECT => 01
  68. * INET_ECN_ECT_1 => 10
  69. * INET_ECN_ECT_0 => 11
  70. * INET_ECN_CE => 00
  71. */
  72. if (!(ecn & 2))
  73. return !ecn;
  74. /*
  75. * The following gives us:
  76. * INET_ECN_ECT_1 => check += htons(0xFFFD)
  77. * INET_ECN_ECT_0 => check += htons(0xFFFE)
  78. */
  79. check += (__force u16)htons(0xFFFB) + (__force u16)htons(ecn);
  80. iph->check = (__force __sum16)(check + (check>=0xFFFF));
  81. iph->tos |= INET_ECN_CE;
  82. return 1;
  83. }
  84. static inline void IP_ECN_clear(struct iphdr *iph)
  85. {
  86. iph->tos &= ~INET_ECN_MASK;
  87. }
  88. static inline void ipv4_copy_dscp(unsigned int dscp, struct iphdr *inner)
  89. {
  90. dscp &= ~INET_ECN_MASK;
  91. ipv4_change_dsfield(inner, INET_ECN_MASK, dscp);
  92. }
  93. struct ipv6hdr;
  94. /* Note:
  95. * IP_ECN_set_ce() has to tweak IPV4 checksum when setting CE,
  96. * meaning both changes have no effect on skb->csum if/when CHECKSUM_COMPLETE
  97. * In IPv6 case, no checksum compensates the change in IPv6 header,
  98. * so we have to update skb->csum.
  99. */
  100. static inline int IP6_ECN_set_ce(struct sk_buff *skb, struct ipv6hdr *iph)
  101. {
  102. __be32 from, to;
  103. if (INET_ECN_is_not_ect(ipv6_get_dsfield(iph)))
  104. return 0;
  105. from = *(__be32 *)iph;
  106. to = from | htonl(INET_ECN_CE << 20);
  107. *(__be32 *)iph = to;
  108. if (skb->ip_summed == CHECKSUM_COMPLETE)
  109. skb->csum = csum_add(csum_sub(skb->csum, (__force __wsum)from),
  110. (__force __wsum)to);
  111. return 1;
  112. }
  113. static inline void IP6_ECN_clear(struct ipv6hdr *iph)
  114. {
  115. *(__be32*)iph &= ~htonl(INET_ECN_MASK << 20);
  116. }
  117. static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)
  118. {
  119. dscp &= ~INET_ECN_MASK;
  120. ipv6_change_dsfield(inner, INET_ECN_MASK, dscp);
  121. }
  122. static inline int INET_ECN_set_ce(struct sk_buff *skb)
  123. {
  124. switch (skb->protocol) {
  125. case cpu_to_be16(ETH_P_IP):
  126. if (skb_network_header(skb) + sizeof(struct iphdr) <=
  127. skb_tail_pointer(skb))
  128. return IP_ECN_set_ce(ip_hdr(skb));
  129. break;
  130. case cpu_to_be16(ETH_P_IPV6):
  131. if (skb_network_header(skb) + sizeof(struct ipv6hdr) <=
  132. skb_tail_pointer(skb))
  133. return IP6_ECN_set_ce(skb, ipv6_hdr(skb));
  134. break;
  135. }
  136. return 0;
  137. }
  138. /*
  139. * RFC 6040 4.2
  140. * To decapsulate the inner header at the tunnel egress, a compliant
  141. * tunnel egress MUST set the outgoing ECN field to the codepoint at the
  142. * intersection of the appropriate arriving inner header (row) and outer
  143. * header (column) in Figure 4
  144. *
  145. * +---------+------------------------------------------------+
  146. * |Arriving | Arriving Outer Header |
  147. * | Inner +---------+------------+------------+------------+
  148. * | Header | Not-ECT | ECT(0) | ECT(1) | CE |
  149. * +---------+---------+------------+------------+------------+
  150. * | Not-ECT | Not-ECT |Not-ECT(!!!)|Not-ECT(!!!)| <drop>(!!!)|
  151. * | ECT(0) | ECT(0) | ECT(0) | ECT(1) | CE |
  152. * | ECT(1) | ECT(1) | ECT(1) (!) | ECT(1) | CE |
  153. * | CE | CE | CE | CE(!!!)| CE |
  154. * +---------+---------+------------+------------+------------+
  155. *
  156. * Figure 4: New IP in IP Decapsulation Behaviour
  157. *
  158. * returns 0 on success
  159. * 1 if something is broken and should be logged (!!! above)
  160. * 2 if packet should be dropped
  161. */
  162. static inline int INET_ECN_decapsulate(struct sk_buff *skb,
  163. __u8 outer, __u8 inner)
  164. {
  165. if (INET_ECN_is_not_ect(inner)) {
  166. switch (outer & INET_ECN_MASK) {
  167. case INET_ECN_NOT_ECT:
  168. return 0;
  169. case INET_ECN_ECT_0:
  170. case INET_ECN_ECT_1:
  171. return 1;
  172. case INET_ECN_CE:
  173. return 2;
  174. }
  175. }
  176. if (INET_ECN_is_ce(outer))
  177. INET_ECN_set_ce(skb);
  178. return 0;
  179. }
  180. static inline int IP_ECN_decapsulate(const struct iphdr *oiph,
  181. struct sk_buff *skb)
  182. {
  183. __u8 inner;
  184. if (skb->protocol == htons(ETH_P_IP))
  185. inner = ip_hdr(skb)->tos;
  186. else if (skb->protocol == htons(ETH_P_IPV6))
  187. inner = ipv6_get_dsfield(ipv6_hdr(skb));
  188. else
  189. return 0;
  190. return INET_ECN_decapsulate(skb, oiph->tos, inner);
  191. }
  192. static inline int IP6_ECN_decapsulate(const struct ipv6hdr *oipv6h,
  193. struct sk_buff *skb)
  194. {
  195. __u8 inner;
  196. if (skb->protocol == htons(ETH_P_IP))
  197. inner = ip_hdr(skb)->tos;
  198. else if (skb->protocol == htons(ETH_P_IPV6))
  199. inner = ipv6_get_dsfield(ipv6_hdr(skb));
  200. else
  201. return 0;
  202. return INET_ECN_decapsulate(skb, ipv6_get_dsfield(oipv6h), inner);
  203. }
  204. #endif