secure_seq.c 4.1 KB

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