secure_seq.c 4.3 KB

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