inet_ecn.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, from), to);
  110. return 1;
  111. }
  112. static inline void IP6_ECN_clear(struct ipv6hdr *iph)
  113. {
  114. *(__be32*)iph &= ~htonl(INET_ECN_MASK << 20);
  115. }
  116. static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)
  117. {
  118. dscp &= ~INET_ECN_MASK;
  119. ipv6_change_dsfield(inner, INET_ECN_MASK, dscp);
  120. }
  121. static inline int INET_ECN_set_ce(struct sk_buff *skb)
  122. {
  123. switch (skb->protocol) {
  124. case cpu_to_be16(ETH_P_IP):
  125. if (skb_network_header(skb) + sizeof(struct iphdr) <=
  126. skb_tail_pointer(skb))
  127. return IP_ECN_set_ce(ip_hdr(skb));
  128. break;
  129. case cpu_to_be16(ETH_P_IPV6):
  130. if (skb_network_header(skb) + sizeof(struct ipv6hdr) <=
  131. skb_tail_pointer(skb))
  132. return IP6_ECN_set_ce(skb, ipv6_hdr(skb));
  133. break;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * RFC 6040 4.2
  139. * To decapsulate the inner header at the tunnel egress, a compliant
  140. * tunnel egress MUST set the outgoing ECN field to the codepoint at the
  141. * intersection of the appropriate arriving inner header (row) and outer
  142. * header (column) in Figure 4
  143. *
  144. * +---------+------------------------------------------------+
  145. * |Arriving | Arriving Outer Header |
  146. * | Inner +---------+------------+------------+------------+
  147. * | Header | Not-ECT | ECT(0) | ECT(1) | CE |
  148. * +---------+---------+------------+------------+------------+
  149. * | Not-ECT | Not-ECT |Not-ECT(!!!)|Not-ECT(!!!)| <drop>(!!!)|
  150. * | ECT(0) | ECT(0) | ECT(0) | ECT(1) | CE |
  151. * | ECT(1) | ECT(1) | ECT(1) (!) | ECT(1) | CE |
  152. * | CE | CE | CE | CE(!!!)| CE |
  153. * +---------+---------+------------+------------+------------+
  154. *
  155. * Figure 4: New IP in IP Decapsulation Behaviour
  156. *
  157. * returns 0 on success
  158. * 1 if something is broken and should be logged (!!! above)
  159. * 2 if packet should be dropped
  160. */
  161. static inline int INET_ECN_decapsulate(struct sk_buff *skb,
  162. __u8 outer, __u8 inner)
  163. {
  164. if (INET_ECN_is_not_ect(inner)) {
  165. switch (outer & INET_ECN_MASK) {
  166. case INET_ECN_NOT_ECT:
  167. return 0;
  168. case INET_ECN_ECT_0:
  169. case INET_ECN_ECT_1:
  170. return 1;
  171. case INET_ECN_CE:
  172. return 2;
  173. }
  174. }
  175. if (INET_ECN_is_ce(outer))
  176. INET_ECN_set_ce(skb);
  177. return 0;
  178. }
  179. static inline int IP_ECN_decapsulate(const struct iphdr *oiph,
  180. struct sk_buff *skb)
  181. {
  182. __u8 inner;
  183. if (skb->protocol == htons(ETH_P_IP))
  184. inner = ip_hdr(skb)->tos;
  185. else if (skb->protocol == htons(ETH_P_IPV6))
  186. inner = ipv6_get_dsfield(ipv6_hdr(skb));
  187. else
  188. return 0;
  189. return INET_ECN_decapsulate(skb, oiph->tos, inner);
  190. }
  191. static inline int IP6_ECN_decapsulate(const struct ipv6hdr *oipv6h,
  192. struct sk_buff *skb)
  193. {
  194. __u8 inner;
  195. if (skb->protocol == htons(ETH_P_IP))
  196. inner = ip_hdr(skb)->tos;
  197. else if (skb->protocol == htons(ETH_P_IPV6))
  198. inner = ipv6_get_dsfield(ipv6_hdr(skb));
  199. else
  200. return 0;
  201. return INET_ECN_decapsulate(skb, ipv6_get_dsfield(oipv6h), inner);
  202. }
  203. #endif