Skip to content

Commit ee684b6

Browse files
avagindavem330
authored andcommitted
tcp: send packets with a socket timestamp
A socket timestamp is a sum of the global tcp_time_stamp and a per-socket offset. A socket offset is added in places where externally visible tcp timestamp option is parsed/initialized. Connections in the SYN_RECV state are not supported, global tcp_time_stamp is used for them, because repair mode doesn't support this state. In a future it can be implemented by the similar way as for TIME_WAIT sockets. Cc: "David S. Miller" <davem@davemloft.net> Cc: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> Cc: James Morris <jmorris@namei.org> Cc: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org> Cc: Patrick McHardy <kaber@trash.net> Cc: Eric Dumazet <edumazet@google.com> Cc: Pavel Emelyanov <xemul@parallels.com> Signed-off-by: Andrey Vagin <avagin@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 93be6ce commit ee684b6

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

net/ipv4/tcp_input.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3860,7 +3860,7 @@ static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr
38603860
++ptr;
38613861
tp->rx_opt.rcv_tsval = ntohl(*ptr);
38623862
++ptr;
3863-
tp->rx_opt.rcv_tsecr = ntohl(*ptr);
3863+
tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
38643864
return true;
38653865
}
38663866
return false;
@@ -3884,7 +3884,11 @@ static bool tcp_fast_parse_options(const struct sk_buff *skb,
38843884
if (tcp_parse_aligned_timestamp(tp, th))
38853885
return true;
38863886
}
3887+
38873888
tcp_parse_options(skb, &tp->rx_opt, hvpp, 1, NULL);
3889+
if (tp->rx_opt.saw_tstamp)
3890+
tp->rx_opt.rcv_tsecr -= tp->tsoffset;
3891+
38883892
return true;
38893893
}
38903894

@@ -5665,6 +5669,8 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
56655669
int saved_clamp = tp->rx_opt.mss_clamp;
56665670

56675671
tcp_parse_options(skb, &tp->rx_opt, &hash_location, 0, &foc);
5672+
if (tp->rx_opt.saw_tstamp)
5673+
tp->rx_opt.rcv_tsecr -= tp->tsoffset;
56685674

56695675
if (th->ack) {
56705676
/* rfc793:

net/ipv4/tcp_ipv4.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
726726
*/
727727

728728
static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
729-
u32 win, u32 ts, int oif,
729+
u32 win, u32 tsval, u32 tsecr, int oif,
730730
struct tcp_md5sig_key *key,
731731
int reply_flags, u8 tos)
732732
{
@@ -747,12 +747,12 @@ static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
747747

748748
arg.iov[0].iov_base = (unsigned char *)&rep;
749749
arg.iov[0].iov_len = sizeof(rep.th);
750-
if (ts) {
750+
if (tsecr) {
751751
rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
752752
(TCPOPT_TIMESTAMP << 8) |
753753
TCPOLEN_TIMESTAMP);
754-
rep.opt[1] = htonl(tcp_time_stamp);
755-
rep.opt[2] = htonl(ts);
754+
rep.opt[1] = htonl(tsval);
755+
rep.opt[2] = htonl(tsecr);
756756
arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
757757
}
758758

@@ -767,7 +767,7 @@ static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
767767

768768
#ifdef CONFIG_TCP_MD5SIG
769769
if (key) {
770-
int offset = (ts) ? 3 : 0;
770+
int offset = (tsecr) ? 3 : 0;
771771

772772
rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
773773
(TCPOPT_NOP << 16) |
@@ -802,6 +802,7 @@ static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
802802

803803
tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
804804
tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
805+
tcp_time_stamp + tcptw->tw_ts_offset,
805806
tcptw->tw_ts_recent,
806807
tw->tw_bound_dev_if,
807808
tcp_twsk_md5_key(tcptw),
@@ -821,6 +822,7 @@ static void tcp_v4_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
821822
tcp_v4_send_ack(skb, (sk->sk_state == TCP_LISTEN) ?
822823
tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt,
823824
tcp_rsk(req)->rcv_nxt, req->rcv_wnd,
825+
tcp_time_stamp,
824826
req->ts_recent,
825827
0,
826828
tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->daddr,

net/ipv4/tcp_minisocks.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
102102
tcp_parse_options(skb, &tmp_opt, &hash_location, 0, NULL);
103103

104104
if (tmp_opt.saw_tstamp) {
105+
tmp_opt.rcv_tsecr -= tcptw->tw_ts_offset;
105106
tmp_opt.ts_recent = tcptw->tw_ts_recent;
106107
tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
107108
paws_reject = tcp_paws_reject(&tmp_opt, th->rst);

net/ipv4/tcp_output.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
622622

623623
if (likely(sysctl_tcp_timestamps && *md5 == NULL)) {
624624
opts->options |= OPTION_TS;
625-
opts->tsval = TCP_SKB_CB(skb)->when;
625+
opts->tsval = TCP_SKB_CB(skb)->when + tp->tsoffset;
626626
opts->tsecr = tp->rx_opt.ts_recent;
627627
remaining -= TCPOLEN_TSTAMP_ALIGNED;
628628
}
@@ -806,7 +806,7 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
806806

807807
if (likely(tp->rx_opt.tstamp_ok)) {
808808
opts->options |= OPTION_TS;
809-
opts->tsval = tcb ? tcb->when : 0;
809+
opts->tsval = tcb ? tcb->when + tp->tsoffset : 0;
810810
opts->tsecr = tp->rx_opt.ts_recent;
811811
size += TCPOLEN_TSTAMP_ALIGNED;
812812
}

net/ipv6/tcp_ipv6.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
713713
#endif
714714

715715
static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
716-
u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass)
716+
u32 tsval, u32 tsecr,
717+
struct tcp_md5sig_key *key, int rst, u8 tclass)
717718
{
718719
const struct tcphdr *th = tcp_hdr(skb);
719720
struct tcphdr *t1;
@@ -725,7 +726,7 @@ static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
725726
struct dst_entry *dst;
726727
__be32 *topt;
727728

728-
if (ts)
729+
if (tsecr)
729730
tot_len += TCPOLEN_TSTAMP_ALIGNED;
730731
#ifdef CONFIG_TCP_MD5SIG
731732
if (key)
@@ -755,11 +756,11 @@ static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
755756

756757
topt = (__be32 *)(t1 + 1);
757758

758-
if (ts) {
759+
if (tsecr) {
759760
*topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
760761
(TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
761-
*topt++ = htonl(tcp_time_stamp);
762-
*topt++ = htonl(ts);
762+
*topt++ = htonl(tsval);
763+
*topt++ = htonl(tsecr);
763764
}
764765

765766
#ifdef CONFIG_TCP_MD5SIG
@@ -860,7 +861,7 @@ static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
860861
ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
861862
(th->doff << 2);
862863

863-
tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0);
864+
tcp_v6_send_response(skb, seq, ack_seq, 0, 0, 0, key, 1, 0);
864865

865866
#ifdef CONFIG_TCP_MD5SIG
866867
release_sk1:
@@ -871,10 +872,11 @@ static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
871872
#endif
872873
}
873874

874-
static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
875+
static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
876+
u32 win, u32 tsval, u32 tsecr,
875877
struct tcp_md5sig_key *key, u8 tclass)
876878
{
877-
tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass);
879+
tcp_v6_send_response(skb, seq, ack, win, tsval, tsecr, key, 0, tclass);
878880
}
879881

880882
static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
@@ -884,6 +886,7 @@ static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
884886

885887
tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
886888
tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
889+
tcp_time_stamp + tcptw->tw_ts_offset,
887890
tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw),
888891
tw->tw_tclass);
889892

@@ -893,7 +896,8 @@ static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
893896
static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
894897
struct request_sock *req)
895898
{
896-
tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
899+
tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1,
900+
req->rcv_wnd, tcp_time_stamp, req->ts_recent,
897901
tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0);
898902
}
899903

0 commit comments

Comments
 (0)