inet_ecn.h 6.0 KB

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