secure_seq.c 4.2 KB

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