secure_seq.c 4.3 KB

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