secure_seq.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 struct net *net,
  48. const __be32 *saddr, const __be32 *daddr)
  49. {
  50. const struct {
  51. struct in6_addr saddr;
  52. struct in6_addr daddr;
  53. } __aligned(SIPHASH_ALIGNMENT) combined = {
  54. .saddr = *(struct in6_addr *)saddr,
  55. .daddr = *(struct in6_addr *)daddr,
  56. };
  57. if (net->ipv4.sysctl_tcp_timestamps != 1)
  58. return 0;
  59. ts_secret_init();
  60. return siphash(&combined, offsetofend(typeof(combined), daddr),
  61. &ts_secret);
  62. }
  63. EXPORT_SYMBOL(secure_tcpv6_ts_off);
  64. u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr,
  65. __be16 sport, __be16 dport)
  66. {
  67. const struct {
  68. struct in6_addr saddr;
  69. struct in6_addr daddr;
  70. __be16 sport;
  71. __be16 dport;
  72. } __aligned(SIPHASH_ALIGNMENT) combined = {
  73. .saddr = *(struct in6_addr *)saddr,
  74. .daddr = *(struct in6_addr *)daddr,
  75. .sport = sport,
  76. .dport = dport
  77. };
  78. u32 hash;
  79. net_secret_init();
  80. hash = siphash(&combined, offsetofend(typeof(combined), dport),
  81. &net_secret);
  82. return seq_scale(hash);
  83. }
  84. EXPORT_SYMBOL(secure_tcpv6_seq);
  85. u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
  86. __be16 dport)
  87. {
  88. const struct {
  89. struct in6_addr saddr;
  90. struct in6_addr daddr;
  91. __be16 dport;
  92. } __aligned(SIPHASH_ALIGNMENT) combined = {
  93. .saddr = *(struct in6_addr *)saddr,
  94. .daddr = *(struct in6_addr *)daddr,
  95. .dport = dport
  96. };
  97. net_secret_init();
  98. return siphash(&combined, offsetofend(typeof(combined), dport),
  99. &net_secret);
  100. }
  101. EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
  102. #endif
  103. #ifdef CONFIG_INET
  104. u32 secure_tcp_ts_off(const struct net *net, __be32 saddr, __be32 daddr)
  105. {
  106. if (net->ipv4.sysctl_tcp_timestamps != 1)
  107. return 0;
  108. ts_secret_init();
  109. return siphash_2u32((__force u32)saddr, (__force u32)daddr,
  110. &ts_secret);
  111. }
  112. /* secure_tcp_seq_and_tsoff(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
  113. * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
  114. * it would be easy enough to have the former function use siphash_4u32, passing
  115. * the arguments as separate u32.
  116. */
  117. u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
  118. __be16 sport, __be16 dport)
  119. {
  120. u32 hash;
  121. net_secret_init();
  122. hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  123. (__force u32)sport << 16 | (__force u32)dport,
  124. &net_secret);
  125. return seq_scale(hash);
  126. }
  127. EXPORT_SYMBOL_GPL(secure_tcp_seq);
  128. u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  129. {
  130. net_secret_init();
  131. return siphash_3u32((__force u32)saddr, (__force u32)daddr,
  132. (__force u16)dport, &net_secret);
  133. }
  134. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  135. #endif
  136. #if IS_ENABLED(CONFIG_IP_DCCP)
  137. u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
  138. __be16 sport, __be16 dport)
  139. {
  140. u64 seq;
  141. net_secret_init();
  142. seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  143. (__force u32)sport << 16 | (__force u32)dport,
  144. &net_secret);
  145. seq += ktime_get_real_ns();
  146. seq &= (1ull << 48) - 1;
  147. return seq;
  148. }
  149. EXPORT_SYMBOL(secure_dccp_sequence_number);
  150. #if IS_ENABLED(CONFIG_IPV6)
  151. u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
  152. __be16 sport, __be16 dport)
  153. {
  154. const struct {
  155. struct in6_addr saddr;
  156. struct in6_addr daddr;
  157. __be16 sport;
  158. __be16 dport;
  159. } __aligned(SIPHASH_ALIGNMENT) combined = {
  160. .saddr = *(struct in6_addr *)saddr,
  161. .daddr = *(struct in6_addr *)daddr,
  162. .sport = sport,
  163. .dport = dport
  164. };
  165. u64 seq;
  166. net_secret_init();
  167. seq = siphash(&combined, offsetofend(typeof(combined), dport),
  168. &net_secret);
  169. seq += ktime_get_real_ns();
  170. seq &= (1ull << 48) - 1;
  171. return seq;
  172. }
  173. EXPORT_SYMBOL(secure_dccpv6_sequence_number);
  174. #endif
  175. #endif