secure_seq.c 3.9 KB

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