inet_ecn.h 5.6 KB

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