Skip to contentSkip to conversation

Add support for TCP-options via timestamps.

PATCH In review Florian Westphal 2 participants 3 messages Mar 31, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for TCP-options via timestamps.

Allow the use of SACK and window scaling when syncookies are used and the client supports tcp timestamps. Options are then restored from the timestamp echo.

Based on earlier work by Glenn Griffin.
This patch avoids increasing the size of structs by encoding TCP options into the least significant bits of the timestamp and by not using any 'timestamp offset'.

The downside is that the timestamp sent in the packet after the synack will increase by several seconds.

---

Glenns original patch:
http://marc.info/?l=linux-netdev&m=120309729612244&w=2

Also see
http://marc.info/?l=linux-netdev&m=120309729512241&w=2
for the previous discussion of Glenns patch set.

 include/net/request_sock.h |    2 +-
 include/net/tcp.h          |    3 +
 net/ipv4/syncookies.c      |   94 +++++++++++++++++++++++++++++++++++++++++--
 net/ipv4/tcp_ipv4.c        |    5 +-
 net/ipv4/tcp_output.c      |    6 ++-
 net/ipv6/syncookies.c      |   53 +++++++++++++++++++++++--
 net/ipv6/tcp_ipv6.c        |    5 +-
 7 files changed, 151 insertions(+), 17 deletions(-)
include/net/request_sock.h +1 -1
index 0369f98..b987813 100644
@@ -45,7 +45,7 @@ struct request_sock {
 	struct request_sock		*dl_next; /* Must be first member! */
 	u16				mss;
 	u8				retrans;
-	u8				__pad;
+	u8				cookie_ts; /* syncookie: encode tcpopts in timestamp */
 	/* The following two fields can be easily recomputed I think -AK */
 	u32				window_clamp; /* window clamp at creation time */
 	u32				rcv_wnd;	  /* rcv_wnd offered first time */
include/net/tcp.h +3 -0
index 723b368..83ad43b 100644
@@ -442,6 +442,8 @@ extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
 extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, 
 				     __u16 *mss);
 
+extern __u32 cookie_init_timestamp(struct request_sock *req);
+
 /* From net/ipv6/syncookies.c */
 extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
 extern __u32 cookie_v6_init_sequence(struct sock *sk, struct sk_buff *skb,
@@ -956,6 +958,7 @@ static inline void tcp_openreq_init(struct request_sock *req,
 	struct inet_request_sock *ireq = inet_rsk(req);
 
 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
+	req->cookie_ts = 0;
 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
 	req->mss = rx_opt->mss_clamp;
 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
net/ipv4/syncookies.c +89 -5

144 lines not on this page · raw message

net/ipv4/tcp_ipv4.c +2 -3
index ef141b8..0d4d990 100644
@@ -1300,10 +1300,8 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 
 	tcp_parse_options(skb, &tmp_opt, 0);
 
-	if (want_cookie) {
+	if (want_cookie && !tmp_opt.saw_tstamp)
 		tcp_clear_options(&tmp_opt);
-		tmp_opt.saw_tstamp = 0;
-	}
 
 	if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
 		/* Some OSes (unknown ones, but I see them on web server, which
@@ -1331,6 +1329,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 	if (want_cookie) {
 #ifdef CONFIG_SYN_COOKIES
 		syn_flood_warning(skb);
+		req->cookie_ts = tmp_opt.tstamp_ok;
 #endif
 		isn = cookie_v4_init_sequence(sk, skb, &req->mss);
 	} else if (!isn) {
net/ipv4/tcp_output.c +5 -1
index a627616..76b3653 100644
@@ -2233,7 +2233,11 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
 
 	/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
 	th->window = htons(min(req->rcv_wnd, 65535U));
-
+#ifdef CONFIG_SYN_COOKIES
+	if (unlikely(req->cookie_ts))
+		TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
+	else
+#endif
 	TCP_SKB_CB(skb)->when = tcp_time_stamp;
 	tcp_syn_build_options((__be32 *)(th + 1), dst_metric(dst, RTAX_ADVMSS), ireq->tstamp_ok,
 			      ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale,
net/ipv6/syncookies.c +49 -4

91 lines not on this page · raw message

net/ipv6/tcp_ipv6.c +2 -3
index 6d851c3..822dbca 100644
@@ -1289,10 +1289,8 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
 
 	tcp_parse_options(skb, &tmp_opt, 0);
 
-	if (want_cookie) {
+	if (want_cookie && !tmp_opt.saw_tstamp)
 		tcp_clear_options(&tmp_opt);
-		tmp_opt.saw_tstamp = 0;
-	}
 
 	tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
 	tcp_openreq_init(req, &tmp_opt, skb);
@@ -1306,6 +1304,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
 
 	if (want_cookie) {
 		isn = cookie_v6_init_sequence(sk, skb, &req->mss);
+		req->cookie_ts = tmp_opt.tstamp_ok;
 	} else if (!isn) {
 		if (ipv6_opt_accepted(sk, skb) ||
 		    np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
Signature · 1 line
1.5.3.7
#1 lore raw
Quoted 2 lines

The downside is that the timestamp sent in the packet after the synack will increase by several seconds.

I didn't see any huge downsides quickly scanning over it. This could cause a difference of upto ~500 jiffies from what we will send on the first packet sent after receiving an ack. If you were connecting to another linux peer I don't believe this would cause any huge issues, and the rtt calculations would quickly recover once actual data communications took place. The problem is the interpretation of our timestamp is dependant on the remote systems logic and while smart peers could quickly recover from the relatively small discrepancy during the handshake it's hard to determine what effect it will have on other hosts.

Considering this only matters when we are already in the midst of a DOS attack, and tcp timestamps would otherwise be disabled it seems like an okay tradeoff to me.

Quoted 8 lines
net/ipv4/syncookies.c +8 -0
+static u32 options_encode(u32 options)
+{
+	u32 ts, ts_now = tcp_time_stamp;
+
+	if (unlikely(options > ts_now)) { /* recent overflow */
+		options |= ~(TSMASK);
+		return options;
+	}

net/ipv4/syncookies.c static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, · line 76

Quoted 10 lines
net/ipv4/syncookies.c +10 -0
+	ts = ts_now & ~TSMASK;
+	ts |= options;
+	if (ts > ts_now) { /* try to fix up ... */
+		ts >>= TSBITS;
+		ts--;
+		ts <<= TSBITS;
+		ts |= options;
+	}
+        return ts;
+}

I may be missing something obvious, but I'm failing to see where the initial if(options > ts_now) does anything different from the more generic logic below it. In order for ts_now to be smaller than options it would need to have all bits above TSBITS off. If that's the case then

ts >>= TSBITS;
ts--;
ts <<= TSBITS;

will flip all those bits on just as you do with

options |= ~(TSMASK)

Also the 'return ts' line is indented using spaces rather than tabs.

--Glenn Griffin

#2 lore raw
Quoted 12 lines · Glenn Griffin
Quoted 12 lines
net/ipv4/syncookies.c +12 -0
+	u32 ts, ts_now = tcp_time_stamp;
+
+	if (unlikely(options > ts_now)) { /* recent overflow */
+		options |= ~(TSMASK);
+		return options;
+	}
+	ts = ts_now & ~TSMASK;
+	ts |= options;
+	if (ts > ts_now) { /* try to fix up ... */
+		ts >>= TSBITS;
+		ts--;
+		ts <<= TSBITS;
Quoted 8 lines

net/ipv4/syncookies.c static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, · line 76

Quoted 4 lines
net/ipv4/syncookies.c +4 -0
+		ts |= options;
+	}
+        return ts;
+}

I may be missing something obvious, but I'm failing to see where the initial if(options > ts_now) does anything different from the more generic logic below it.

You're right, I'll drop the first one.

Quoted 1 line

Also the 'return ts' line is indented using spaces rather than tabs.

Yes. I'll fix this, thanks.

I think it might be better to avoid duplicating 'cookie_check_timestamp()' in ipv6/syncookies.c; so i'll change that as well.

I'll wait a few hours before submitting an updated version in case others wish to provide feedback.

Glenn, should I add your SoB-Line when I send an updated version? After all, this patch re-uses some of your earlier work.

Thanks again for reviewing this.