secure_seq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #endif
  68. #ifdef CONFIG_INET
  69. __u32 secure_ip_id(__be32 daddr)
  70. {
  71. u32 hash[MD5_DIGEST_WORDS];
  72. hash[0] = (__force __u32) daddr;
  73. hash[1] = net_secret[13];
  74. hash[2] = net_secret[14];
  75. hash[3] = net_secret[15];
  76. md5_transform(hash, net_secret);
  77. return hash[0];
  78. }
  79. __u32 secure_ipv6_id(const __be32 daddr[4])
  80. {
  81. __u32 hash[4];
  82. memcpy(hash, daddr, 16);
  83. md5_transform(hash, net_secret);
  84. return hash[0];
  85. }
  86. __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
  87. __be16 sport, __be16 dport)
  88. {
  89. u32 hash[MD5_DIGEST_WORDS];
  90. hash[0] = (__force u32)saddr;
  91. hash[1] = (__force u32)daddr;
  92. hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
  93. hash[3] = net_secret[15];
  94. md5_transform(hash, net_secret);
  95. return seq_scale(hash[0]);
  96. }
  97. u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  98. {
  99. u32 hash[MD5_DIGEST_WORDS];
  100. hash[0] = (__force u32)saddr;
  101. hash[1] = (__force u32)daddr;
  102. hash[2] = (__force u32)dport ^ net_secret[14];
  103. hash[3] = net_secret[15];
  104. md5_transform(hash, net_secret);
  105. return hash[0];
  106. }
  107. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  108. #endif
  109. #if IS_ENABLED(CONFIG_IP_DCCP)
  110. u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
  111. __be16 sport, __be16 dport)
  112. {
  113. u32 hash[MD5_DIGEST_WORDS];
  114. u64 seq;
  115. hash[0] = (__force u32)saddr;
  116. hash[1] = (__force u32)daddr;
  117. hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
  118. hash[3] = net_secret[15];
  119. md5_transform(hash, net_secret);
  120. seq = hash[0] | (((u64)hash[1]) << 32);
  121. seq += ktime_to_ns(ktime_get_real());
  122. seq &= (1ull << 48) - 1;
  123. return seq;
  124. }
  125. EXPORT_SYMBOL(secure_dccp_sequence_number);
  126. #if IS_ENABLED(CONFIG_IPV6)
  127. u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
  128. __be16 sport, __be16 dport)
  129. {
  130. u32 secret[MD5_MESSAGE_BYTES / 4];
  131. u32 hash[MD5_DIGEST_WORDS];
  132. u64 seq;
  133. u32 i;
  134. memcpy(hash, saddr, 16);
  135. for (i = 0; i < 4; i++)
  136. secret[i] = net_secret[i] + daddr[i];
  137. secret[4] = net_secret[4] +
  138. (((__force u16)sport << 16) + (__force u16)dport);
  139. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  140. secret[i] = net_secret[i];
  141. md5_transform(hash, secret);
  142. seq = hash[0] | (((u64)hash[1]) << 32);
  143. seq += ktime_to_ns(ktime_get_real());
  144. seq &= (1ull << 48) - 1;
  145. return seq;
  146. }
  147. EXPORT_SYMBOL(secure_dccpv6_sequence_number);
  148. #endif
  149. #endif