secure_seq.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 siphash_key_t ts_secret __read_mostly;
  21. static __always_inline void net_secret_init(void)
  22. {
  23. net_get_random_once(&net_secret, sizeof(net_secret));
  24. }
  25. static __always_inline void ts_secret_init(void)
  26. {
  27. net_get_random_once(&ts_secret, sizeof(ts_secret));
  28. }
  29. #endif
  30. #ifdef CONFIG_INET
  31. static u32 seq_scale(u32 seq)
  32. {
  33. /*
  34. * As close as possible to RFC 793, which
  35. * suggests using a 250 kHz clock.
  36. * Further reading shows this assumes 2 Mb/s networks.
  37. * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
  38. * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
  39. * we also need to limit the resolution so that the u32 seq
  40. * overlaps less than one time per MSL (2 minutes).
  41. * Choosing a clock of 64 ns period is OK. (period of 274 s)
  42. */
  43. return seq + (ktime_get_real_ns() >> 6);
  44. }
  45. #endif
  46. #if IS_ENABLED(CONFIG_IPV6)
  47. u32 secure_tcpv6_ts_off(const __be32 *saddr, const __be32 *daddr)
  48. {
  49. const struct {
  50. struct in6_addr saddr;
  51. struct in6_addr daddr;
  52. } __aligned(SIPHASH_ALIGNMENT) combined = {
  53. .saddr = *(struct in6_addr *)saddr,
  54. .daddr = *(struct in6_addr *)daddr,
  55. };
  56. if (sysctl_tcp_timestamps != 1)
  57. return 0;
  58. ts_secret_init();
  59. return siphash(&combined, offsetofend(typeof(combined), daddr),
  60. &ts_secret);
  61. }
  62. EXPORT_SYMBOL(secure_tcpv6_ts_off);
  63. u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr,
  64. __be16 sport, __be16 dport)
  65. {
  66. const struct {
  67. struct in6_addr saddr;
  68. struct in6_addr daddr;
  69. __be16 sport;
  70. __be16 dport;
  71. } __aligned(SIPHASH_ALIGNMENT) combined = {
  72. .saddr = *(struct in6_addr *)saddr,
  73. .daddr = *(struct in6_addr *)daddr,
  74. .sport = sport,
  75. .dport = dport
  76. };
  77. u32 hash;
  78. net_secret_init();
  79. hash = siphash(&combined, offsetofend(typeof(combined), dport),
  80. &net_secret);
  81. return seq_scale(hash);
  82. }
  83. EXPORT_SYMBOL(secure_tcpv6_seq);
  84. u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
  85. __be16 dport)
  86. {
  87. const struct {
  88. struct in6_addr saddr;
  89. struct in6_addr daddr;
  90. __be16 dport;
  91. } __aligned(SIPHASH_ALIGNMENT) combined = {
  92. .saddr = *(struct in6_addr *)saddr,
  93. .daddr = *(struct in6_addr *)daddr,
  94. .dport = dport
  95. };
  96. net_secret_init();
  97. return siphash(&combined, offsetofend(typeof(combined), dport),
  98. &net_secret);
  99. }
  100. EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
  101. #endif
  102. #ifdef CONFIG_INET
  103. u32 secure_tcp_ts_off(__be32 saddr, __be32 daddr)
  104. {
  105. if (sysctl_tcp_timestamps != 1)
  106. return 0;
  107. ts_secret_init();
  108. return siphash_2u32((__force u32)saddr, (__force u32)daddr,
  109. &ts_secret);
  110. }
  111. /* secure_tcp_seq_and_tsoff(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
  112. * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
  113. * it would be easy enough to have the former function use siphash_4u32, passing
  114. * the arguments as separate u32.
  115. */
  116. u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
  117. __be16 sport, __be16 dport)
  118. {
  119. u32 hash;
  120. net_secret_init();
  121. hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  122. (__force u32)sport << 16 | (__force u32)dport,
  123. &net_secret);
  124. return seq_scale(hash);
  125. }
  126. u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  127. {
  128. net_secret_init();
  129. return siphash_3u32((__force u32)saddr, (__force u32)daddr,
  130. (__force u16)dport, &net_secret);
  131. }
  132. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  133. #endif
  134. #if IS_ENABLED(CONFIG_IP_DCCP)
  135. u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
  136. __be16 sport, __be16 dport)
  137. {
  138. u64 seq;
  139. net_secret_init();
  140. seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  141. (__force u32)sport << 16 | (__force u32)dport,
  142. &net_secret);
  143. seq += ktime_get_real_ns();
  144. seq &= (1ull << 48) - 1;
  145. return seq;
  146. }
  147. EXPORT_SYMBOL(secure_dccp_sequence_number);
  148. #if IS_ENABLED(CONFIG_IPV6)
  149. u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
  150. __be16 sport, __be16 dport)
  151. {
  152. const struct {
  153. struct in6_addr saddr;
  154. struct in6_addr daddr;
  155. __be16 sport;
  156. __be16 dport;
  157. } __aligned(SIPHASH_ALIGNMENT) combined = {
  158. .saddr = *(struct in6_addr *)saddr,
  159. .daddr = *(struct in6_addr *)daddr,
  160. .sport = sport,
  161. .dport = dport
  162. };
  163. u64 seq;
  164. net_secret_init();
  165. seq = siphash(&combined, offsetofend(typeof(combined), dport),
  166. &net_secret);
  167. seq += ktime_get_real_ns();
  168. seq &= (1ull << 48) - 1;
  169. return seq;
  170. }
  171. EXPORT_SYMBOL(secure_dccpv6_sequence_number);
  172. #endif
  173. #endif