secure_seq.c 4.6 KB

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