secure_seq.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
  13. static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
  14. static void net_secret_init(void)
  15. {
  16. net_get_random_once(net_secret, sizeof(net_secret));
  17. }
  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. net_secret_init();
  42. memcpy(hash, saddr, 16);
  43. for (i = 0; i < 4; i++)
  44. secret[i] = net_secret[i] + (__force u32)daddr[i];
  45. secret[4] = net_secret[4] +
  46. (((__force u16)sport << 16) + (__force u16)dport);
  47. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  48. secret[i] = net_secret[i];
  49. md5_transform(hash, secret);
  50. return seq_scale(hash[0]);
  51. }
  52. EXPORT_SYMBOL(secure_tcpv6_sequence_number);
  53. u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
  54. __be16 dport)
  55. {
  56. u32 secret[MD5_MESSAGE_BYTES / 4];
  57. u32 hash[MD5_DIGEST_WORDS];
  58. u32 i;
  59. net_secret_init();
  60. memcpy(hash, saddr, 16);
  61. for (i = 0; i < 4; i++)
  62. secret[i] = net_secret[i] + (__force u32) daddr[i];
  63. secret[4] = net_secret[4] + (__force u32)dport;
  64. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  65. secret[i] = net_secret[i];
  66. md5_transform(hash, secret);
  67. return hash[0];
  68. }
  69. EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
  70. #endif
  71. #ifdef CONFIG_INET
  72. __u32 secure_ip_id(__be32 daddr)
  73. {
  74. u32 hash[MD5_DIGEST_WORDS];
  75. net_secret_init();
  76. hash[0] = (__force __u32) daddr;
  77. hash[1] = net_secret[13];
  78. hash[2] = net_secret[14];
  79. hash[3] = net_secret[15];
  80. md5_transform(hash, net_secret);
  81. return hash[0];
  82. }
  83. __u32 secure_ipv6_id(const __be32 daddr[4])
  84. {
  85. __u32 hash[4];
  86. net_secret_init();
  87. memcpy(hash, daddr, 16);
  88. md5_transform(hash, net_secret);
  89. return hash[0];
  90. }
  91. __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
  92. __be16 sport, __be16 dport)
  93. {
  94. u32 hash[MD5_DIGEST_WORDS];
  95. net_secret_init();
  96. hash[0] = (__force u32)saddr;
  97. hash[1] = (__force u32)daddr;
  98. hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
  99. hash[3] = net_secret[15];
  100. md5_transform(hash, net_secret);
  101. return seq_scale(hash[0]);
  102. }
  103. u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  104. {
  105. u32 hash[MD5_DIGEST_WORDS];
  106. net_secret_init();
  107. hash[0] = (__force u32)saddr;
  108. hash[1] = (__force u32)daddr;
  109. hash[2] = (__force u32)dport ^ net_secret[14];
  110. hash[3] = net_secret[15];
  111. md5_transform(hash, net_secret);
  112. return hash[0];
  113. }
  114. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  115. #endif
  116. #if IS_ENABLED(CONFIG_IP_DCCP)
  117. u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
  118. __be16 sport, __be16 dport)
  119. {
  120. u32 hash[MD5_DIGEST_WORDS];
  121. u64 seq;
  122. net_secret_init();
  123. hash[0] = (__force u32)saddr;
  124. hash[1] = (__force u32)daddr;
  125. hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
  126. hash[3] = net_secret[15];
  127. md5_transform(hash, net_secret);
  128. seq = hash[0] | (((u64)hash[1]) << 32);
  129. seq += ktime_to_ns(ktime_get_real());
  130. seq &= (1ull << 48) - 1;
  131. return seq;
  132. }
  133. EXPORT_SYMBOL(secure_dccp_sequence_number);
  134. #if IS_ENABLED(CONFIG_IPV6)
  135. u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
  136. __be16 sport, __be16 dport)
  137. {
  138. u32 secret[MD5_MESSAGE_BYTES / 4];
  139. u32 hash[MD5_DIGEST_WORDS];
  140. u64 seq;
  141. u32 i;
  142. net_secret_init();
  143. memcpy(hash, saddr, 16);
  144. for (i = 0; i < 4; i++)
  145. secret[i] = net_secret[i] + daddr[i];
  146. secret[4] = net_secret[4] +
  147. (((__force u16)sport << 16) + (__force u16)dport);
  148. for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
  149. secret[i] = net_secret[i];
  150. md5_transform(hash, secret);
  151. seq = hash[0] | (((u64)hash[1]) << 32);
  152. seq += ktime_to_ns(ktime_get_real());
  153. seq &= (1ull << 48) - 1;
  154. return seq;
  155. }
  156. EXPORT_SYMBOL(secure_dccpv6_sequence_number);
  157. #endif
  158. #endif