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