inet_ecn.h 6.1 KB

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