The paired receive-window state is now part of the live TCP socket
semantics, so repair and restore need a way to preserve it.

Extend TCP_REPAIR_WINDOW with the advertise-time scaling snapshot while
keeping old userspace working. The kernel now accepts exactly the legacy
layout and the extended layout. Legacy restore leaves the snapshot
unknown so the socket falls back safely until a fresh local window
advertisement refreshes the pair, while the extended layout restores the
exact snapshot.

Signed-off-by: Wesley Atwell <[email protected]>
---
 include/uapi/linux/tcp.h |  1 +
 net/ipv4/tcp.c           | 34 ++++++++++++++++++++++++++++------
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h
index 03772dd4d399..3a799f4c0e1e 100644
--- a/include/uapi/linux/tcp.h
+++ b/include/uapi/linux/tcp.h
@@ -159,6 +159,7 @@ struct tcp_repair_window {
 
        __u32   rcv_wnd;
        __u32   rcv_wup;
+       __u32   rcv_wnd_scaling_ratio; /* 0 means advertise-time basis unknown 
*/
 };
 
 enum {
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index cec9ae1bf875..dd2b4fe61bd8 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -3551,17 +3551,25 @@ static inline bool tcp_can_repair_sock(const struct 
sock *sk)
                (sk->sk_state != TCP_LISTEN);
 }
 
+/* Keep accepting the pre-extension TCP_REPAIR_WINDOW layout so legacy
+ * userspace can restore sockets without fabricating a snapshot basis.
+ */
+static inline int tcp_repair_window_legacy_size(void)
+{
+       return offsetof(struct tcp_repair_window, rcv_wnd_scaling_ratio);
+}
+
 static int tcp_repair_set_window(struct tcp_sock *tp, sockptr_t optbuf, int 
len)
 {
-       struct tcp_repair_window opt;
+       struct tcp_repair_window opt = {};
 
        if (!tp->repair)
                return -EPERM;
 
-       if (len != sizeof(opt))
+       if (len != tcp_repair_window_legacy_size() && len != sizeof(opt))
                return -EINVAL;
 
-       if (copy_from_sockptr(&opt, optbuf, sizeof(opt)))
+       if (copy_from_sockptr(&opt, optbuf, len))
                return -EFAULT;
 
        if (opt.max_window < opt.snd_wnd)
@@ -3577,7 +3585,20 @@ static int tcp_repair_set_window(struct tcp_sock *tp, 
sockptr_t optbuf, int len)
        tp->snd_wnd     = opt.snd_wnd;
        tp->max_window  = opt.max_window;
 
-       tp->rcv_wnd     = opt.rcv_wnd;
+       if (len == tcp_repair_window_legacy_size()) {
+               /* Legacy repair UAPI has no advertise-time basis for 
tp->rcv_wnd.
+                * Mark the snapshot unknown until a fresh local advertisement
+                * re-establishes the pair.
+                */
+               tcp_set_rcv_wnd_unknown(tp, opt.rcv_wnd);
+               tp->rcv_wup     = opt.rcv_wup;
+               return 0;
+       }
+
+       if (opt.rcv_wnd_scaling_ratio > U8_MAX)
+               return -EINVAL;
+
+       tcp_set_rcv_wnd_snapshot(tp, opt.rcv_wnd, opt.rcv_wnd_scaling_ratio);
        tp->rcv_wup     = opt.rcv_wup;
 
        return 0;
@@ -4667,12 +4688,12 @@ int do_tcp_getsockopt(struct sock *sk, int level,
                break;
 
        case TCP_REPAIR_WINDOW: {
-               struct tcp_repair_window opt;
+               struct tcp_repair_window opt = {};
 
                if (copy_from_sockptr(&len, optlen, sizeof(int)))
                        return -EFAULT;
 
-               if (len != sizeof(opt))
+               if (len != tcp_repair_window_legacy_size() && len != 
sizeof(opt))
                        return -EINVAL;
 
                if (!tp->repair)
@@ -4683,6 +4704,7 @@ int do_tcp_getsockopt(struct sock *sk, int level,
                opt.max_window  = tp->max_window;
                opt.rcv_wnd     = tp->rcv_wnd;
                opt.rcv_wup     = tp->rcv_wup;
+               opt.rcv_wnd_scaling_ratio = tp->rcv_wnd_scaling_ratio;
 
                if (copy_to_sockptr(optval, &opt, len))
                        return -EFAULT;
-- 
2.34.1


Reply via email to