secure_seq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/init.h>
  6. #include <linux/cryptohash.h>
  7. #include <linux/module.h>
  8. #include <linux/cache.h>
  9. #include <linux/random.h>
  10. #include <linux/hrtimer.h>
  11. #include <linux/ktime.h>
  12. #include <linux/string.h>
  13. #include <linux/net.h>
  14. #include <linux/siphash.h>
  15. #include <net/secure_seq.h>
  16. #if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
  17. #include <linux/in6.h>
  18. #include <net/tcp.h>
  19. static siphash_key_t net_secret __read_mostly;
  20. static __always_inline void net_secret_init(void)
  21. {
  22. net_get_random_once(&net_secret, sizeof(net_secret));
  23. }
  24. #endif
  25. #ifdef CONFIG_INET
  26. static u32 seq_scale(u32 seq)
  27. {
  28. /*
  29. * As close as possible to RFC 793, which
  30. * suggests using a 250 kHz clock.
  31. * Further reading shows this assumes 2 Mb/s networks.
  32. * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
  33. * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
  34. * we also need to limit the resolution so that the u32 seq
  35. * overlaps less than one time per MSL (2 minutes).
  36. * Choosing a clock of 64 ns period is OK. (period of 274 s)
  37. */
  38. return seq + (ktime_get_real_ns() >> 6);
  39. }
  40. #endif
  41. #if IS_ENABLED(CONFIG_IPV6)
  42. u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
  43. __be16 sport, __be16 dport, u32 *tsoff)
  44. {
  45. const struct {
  46. struct in6_addr saddr;
  47. struct in6_addr daddr;
  48. __be16 sport;
  49. __be16 dport;
  50. } __aligned(SIPHASH_ALIGNMENT) combined = {
  51. .saddr = *(struct in6_addr *)saddr,
  52. .daddr = *(struct in6_addr *)daddr,
  53. .sport = sport,
  54. .dport = dport
  55. };
  56. u64 hash;
  57. net_secret_init();
  58. hash = siphash(&combined, offsetofend(typeof(combined), dport),
  59. &net_secret);
  60. *tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
  61. return seq_scale(hash);
  62. }
  63. EXPORT_SYMBOL(secure_tcpv6_sequence_number);
  64. u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
  65. __be16 dport)
  66. {
  67. const struct {
  68. struct in6_addr saddr;
  69. struct in6_addr daddr;
  70. __be16 dport;
  71. } __aligned(SIPHASH_ALIGNMENT) combined = {
  72. .saddr = *(struct in6_addr *)saddr,
  73. .daddr = *(struct in6_addr *)daddr,
  74. .dport = dport
  75. };
  76. net_secret_init();
  77. return siphash(&combined, offsetofend(typeof(combined), dport),
  78. &net_secret);
  79. }
  80. EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
  81. #endif
  82. #ifdef CONFIG_INET
  83. /* secure_tcp_sequence_number(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
  84. * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
  85. * it would be easy enough to have the former function use siphash_4u32, passing
  86. * the arguments as separate u32.
  87. */
  88. u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
  89. __be16 sport, __be16 dport, u32 *tsoff)
  90. {
  91. u64 hash;
  92. net_secret_init();
  93. hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  94. (__force u32)sport << 16 | (__force u32)dport,
  95. &net_secret);
  96. *tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
  97. return seq_scale(hash);
  98. }
  99. u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  100. {
  101. net_secret_init();
  102. return siphash_3u32((__force u32)saddr, (__force u32)daddr,
  103. (__force u16)dport, &net_secret);
  104. }
  105. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  106. #endif
  107. #if IS_ENABLED(CONFIG_IP_DCCP)
  108. u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
  109. __be16 sport, __be16 dport)
  110. {
  111. u64 seq;
  112. net_secret_init();
  113. seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  114. (__force u32)sport << 16 | (__force u32)dport,
  115. &net_secret);
  116. seq += ktime_get_real_ns();
  117. seq &= (1ull << 48) - 1;
  118. return seq;
  119. }
  120. EXPORT_SYMBOL(secure_dccp_sequence_number);
  121. #if IS_ENABLED(CONFIG_IPV6)
  122. u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
  123. __be16 sport, __be16 dport)
  124. {
  125. const struct {
  126. struct in6_addr saddr;
  127. struct in6_addr daddr;
  128. __be16 sport;
  129. __be16 dport;
  130. } __aligned(SIPHASH_ALIGNMENT) combined = {
  131. .saddr = *(struct in6_addr *)saddr,
  132. .daddr = *(struct in6_addr *)daddr,
  133. .sport = sport,
  134. .dport = dport
  135. };
  136. u64 seq;
  137. net_secret_init();
  138. seq = siphash(&combined, offsetofend(typeof(combined), dport),
  139. &net_secret);
  140. seq += ktime_get_real_ns();
  141. seq &= (1ull << 48) - 1;
  142. return seq;
  143. }
  144. EXPORT_SYMBOL(secure_dccpv6_sequence_number);
  145. #endif
  146. #endif