secure_seq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.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_aligned_key_t net_secret;
  20. #define EPHEMERAL_PORT_SHUFFLE_PERIOD (10 * HZ)
  21. static __always_inline void net_secret_init(void)
  22. {
  23. net_get_random_once(&net_secret, sizeof(net_secret));
  24. }
  25. #endif
  26. #ifdef CONFIG_INET
  27. static u32 seq_scale(u32 seq)
  28. {
  29. /*
  30. * As close as possible to RFC 793, which
  31. * suggests using a 250 kHz clock.
  32. * Further reading shows this assumes 2 Mb/s networks.
  33. * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
  34. * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
  35. * we also need to limit the resolution so that the u32 seq
  36. * overlaps less than one time per MSL (2 minutes).
  37. * Choosing a clock of 64 ns period is OK. (period of 274 s)
  38. */
  39. return seq + (ktime_get_real_ns() >> 6);
  40. }
  41. #endif
  42. #if IS_ENABLED(CONFIG_IPV6)
  43. union tcp_seq_and_ts_off
  44. secure_tcpv6_seq_and_ts_off(const struct net *net, const __be32 *saddr,
  45. const __be32 *daddr, __be16 sport, __be16 dport)
  46. {
  47. const struct {
  48. struct in6_addr saddr;
  49. struct in6_addr daddr;
  50. __be16 sport;
  51. __be16 dport;
  52. } __aligned(SIPHASH_ALIGNMENT) combined = {
  53. .saddr = *(struct in6_addr *)saddr,
  54. .daddr = *(struct in6_addr *)daddr,
  55. .sport = sport,
  56. .dport = dport
  57. };
  58. union tcp_seq_and_ts_off st;
  59. net_secret_init();
  60. st.hash64 = siphash(&combined, offsetofend(typeof(combined), dport),
  61. &net_secret);
  62. if (READ_ONCE(net->ipv4.sysctl_tcp_timestamps) != 1)
  63. st.ts_off = 0;
  64. st.seq = seq_scale(st.seq);
  65. return st;
  66. }
  67. EXPORT_SYMBOL(secure_tcpv6_seq_and_ts_off);
  68. u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
  69. __be16 dport)
  70. {
  71. const struct {
  72. struct in6_addr saddr;
  73. struct in6_addr daddr;
  74. unsigned int timeseed;
  75. __be16 dport;
  76. } __aligned(SIPHASH_ALIGNMENT) combined = {
  77. .saddr = *(struct in6_addr *)saddr,
  78. .daddr = *(struct in6_addr *)daddr,
  79. .timeseed = jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
  80. .dport = dport,
  81. };
  82. net_secret_init();
  83. return siphash(&combined, offsetofend(typeof(combined), dport),
  84. &net_secret);
  85. }
  86. EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
  87. #endif
  88. #ifdef CONFIG_INET
  89. /* secure_tcp_seq_and_tsoff(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
  90. * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
  91. * it would be easy enough to have the former function use siphash_4u32, passing
  92. * the arguments as separate u32.
  93. */
  94. union tcp_seq_and_ts_off
  95. secure_tcp_seq_and_ts_off(const struct net *net, __be32 saddr, __be32 daddr,
  96. __be16 sport, __be16 dport)
  97. {
  98. u32 ports = (__force u32)sport << 16 | (__force u32)dport;
  99. union tcp_seq_and_ts_off st;
  100. net_secret_init();
  101. st.hash64 = siphash_3u32((__force u32)saddr, (__force u32)daddr,
  102. ports, &net_secret);
  103. if (READ_ONCE(net->ipv4.sysctl_tcp_timestamps) != 1)
  104. st.ts_off = 0;
  105. st.seq = seq_scale(st.seq);
  106. return st;
  107. }
  108. EXPORT_SYMBOL_GPL(secure_tcp_seq_and_ts_off);
  109. u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
  110. {
  111. net_secret_init();
  112. return siphash_4u32((__force u32)saddr, (__force u32)daddr,
  113. (__force u16)dport,
  114. jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
  115. &net_secret);
  116. }
  117. EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
  118. #endif