secure_seq.c 4.5 KB

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