secure_seq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/cryptohash.h>
  4. #include <linux/module.h>
  5. #include <linux/cache.h>
  6. #include <linux/random.h>
  7. #include <linux/hrtimer.h>
  8. #include <linux/ktime.h>
  9. #include <linux/string.h>
  10. #include <net/secure_seq.h>
  11. #if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
  12. #define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
  13. static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
  14. static void net_secret_init(void)
  15. {
  16. u32 tmp;
  17. int i;
  18. if (likely(net_secret[0]))
  19. return;
  20. for (i = NET_SECRET_SIZE; i > 0;) {
  21. do {
  22. get_random_bytes(&tmp, sizeof(tmp));
  23. } while (!tmp);
  24. cmpxchg(&net_secret[--i], 0, tmp);
  25. }
  26. }
  27. #endif
  28. #ifdef CONFIG_INET
  29. static u32 seq_scale(u32 seq)
  30. {
  31. /*
  32. * As close as possible to RFC 793, which
  33. * suggests using a 250 kHz clock.
  34. * Further reading shows this assumes 2 Mb/s networks.
  35. * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
  36. * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
  37. * we also need to limit the resolution so that the u32 seq
  38. * overlaps less than one time per MSL (2 minutes).
  39. * Choosing a clock of 64 ns period is OK. (period of 274 s)
  40. */
  41. return seq + (ktime_to_ns(ktime_get_real()) >> 6);
  42. }
  43. #endif
  44. #if IS_ENABLED(CONFIG_IPV6)
  45. __u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
  46. __be16 sport, __be16 dport)
  47. {
  48. u32 secret[MD5_MESSAGE_BYTES / 4];
  49. u32 hash[MD5_DIGEST_WORDS];
  50. u32 i;
  51. net_secret_init();
  52. memcpy(hash, saddr, 16);
  53. for (i = 0; i < 4; i++)
  54. secret[i] = net_secret[i] + (__force u32)daddr[i];
  55. secret[4] = net_secret[4] +
  56. (((__force u16)sport << 16) + (__force u16)dport);
  57. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  58. secret[i] = net_secret[i];
  59. md5_transform(hash, secret);
  60. return seq_scale(hash[0]);
  61. }
  62. EXPORT_SYMBOL(secure_tcpv6_sequence_number);
  63. u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
  64. __be16 dport)
  65. {
  66. u32 secret[MD5_MESSAGE_BYTES / 4];
  67. u32 hash[MD5_DIGEST_WORDS];
  68. u32 i;
  69. net_secret_init();
  70. memcpy(hash, saddr, 16);
  71. for (i = 0; i < 4; i++)
  72. secret[i] = net_secret[i] + (__force u32) daddr[i];
  73. secret[4] = net_secret[4] + (__force u32)dport;
  74. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  75. secret[i] = net_secret[i];
  76. md5_transform(hash, secret);
  77. return hash[0];
  78. }
  79. EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
  80. #endif
  81. #ifdef CONFIG_INET
  82. __u32 secure_ip_id(__be32 daddr)
  83. {
  84. u32 hash[MD5_DIGEST_WORDS];
  85. net_secret_init();
  86. hash[0] = (__force __u32) daddr;
  87. hash[1] = net_secret[13];
  88. hash[2] = net_secret[14];
  89. hash[3] = net_secret[15];
  90. md5_transform(hash, net_secret);
  91. return hash[0];
  92. }
  93. __u32 secure_ipv6_id(const __be32 daddr[4])
  94. {
  95. __u32 hash[4];
  96. net_secret_init();
  97. memcpy(hash, daddr, 16);
  98. md5_transform(hash, net_secret);
  99. return hash[0];
  100. }
  101. __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
  102. __be16 sport, __be16 dport)
  103. {
  104. u32 hash[MD5_DIGEST_WORDS];
  105. net_secret_init();
  106. hash[0] = (__force u32)saddr;
  107. hash[1] = (__force u32)daddr;
  108. hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
  109. hash[3] = net_secret[15];
  110. md5_transform(hash, net_secret);
  111. return seq_scale(hash[0]);
  112. }
  113. u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  114. {
  115. u32 hash[MD5_DIGEST_WORDS];
  116. net_secret_init();
  117. hash[0] = (__force u32)saddr;
  118. hash[1] = (__force u32)daddr;
  119. hash[2] = (__force u32)dport ^ net_secret[14];
  120. hash[3] = net_secret[15];
  121. md5_transform(hash, net_secret);
  122. return hash[0];
  123. }
  124. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  125. #endif
  126. #if IS_ENABLED(CONFIG_IP_DCCP)
  127. u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
  128. __be16 sport, __be16 dport)
  129. {
  130. u32 hash[MD5_DIGEST_WORDS];
  131. u64 seq;
  132. net_secret_init();
  133. hash[0] = (__force u32)saddr;
  134. hash[1] = (__force u32)daddr;
  135. hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
  136. hash[3] = net_secret[15];
  137. md5_transform(hash, net_secret);
  138. seq = hash[0] | (((u64)hash[1]) << 32);
  139. seq += ktime_to_ns(ktime_get_real());
  140. seq &= (1ull << 48) - 1;
  141. return seq;
  142. }
  143. EXPORT_SYMBOL(secure_dccp_sequence_number);
  144. #if IS_ENABLED(CONFIG_IPV6)
  145. u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
  146. __be16 sport, __be16 dport)
  147. {
  148. u32 secret[MD5_MESSAGE_BYTES / 4];
  149. u32 hash[MD5_DIGEST_WORDS];
  150. u64 seq;
  151. u32 i;
  152. net_secret_init();
  153. memcpy(hash, saddr, 16);
  154. for (i = 0; i < 4; i++)
  155. secret[i] = net_secret[i] + daddr[i];
  156. secret[4] = net_secret[4] +
  157. (((__force u16)sport << 16) + (__force u16)dport);
  158. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  159. secret[i] = net_secret[i];
  160. md5_transform(hash, secret);
  161. seq = hash[0] | (((u64)hash[1]) << 32);
  162. seq += ktime_to_ns(ktime_get_real());
  163. seq &= (1ull << 48) - 1;
  164. return seq;
  165. }
  166. EXPORT_SYMBOL(secure_dccpv6_sequence_number);
  167. #endif
  168. #endif