Support arbitrary initial TCP timestamps
Subsystems (3)
PATCH 1/3 Support arbitrary initial TCP timestamps
Introduce the ability to send arbitrary initial tcp timestamps that are not tied directly to jiffies. The basic conecpt is every tcp_request_sock and tcp_sock now has a ts_off offset that represents the difference between tcp_time_stamp and the timestamp we send and expect to
receive.
This has the advantage of not divulging system information (uptime) depending on the policy chosen for the initial timestamps.
A policy where ts_off is always set to zero should produce no change in behavior. The policy implemented is not intended for real use, but just as a simple example. A realistic example would probably be similar to the tcp init sequence generator.
--- include/linux/tcp.h | 6 ++++++ include/net/tcp.h | 2 +- net/ipv4/tcp_input.c | 12 ++++++------ net/ipv4/tcp_ipv4.c | 2 +- net/ipv4/tcp_minisocks.c | 5 +++-- net/ipv4/tcp_output.c | 8 +++++--- net/ipv6/tcp_ipv6.c | 2 +- 7 files changed, 23 insertions(+), 14 deletions(-)
include/linux/tcp.h +6 -0
@@ -232,6 +232,7 @@ struct tcp_request_sock {#endif u32 rcv_isn;u32 snt_isn; +u32 ts_off; };static inline struct tcp_request_sock *tcp_rsk (const struct request_sock *req)
@@ -311,6 +312,11 @@ struct tcp_sock {struct tcp_options_received rx_opt;/* +* offset of transmitted tcp timestamp from jiffies +*/ +u32 ts_off; + +/* * Slow start and congestion control (see also Nagle, and Karn & Partridge) */ u32 snd_ssthresh;/* Slow start size threshold */
include/net/tcp.h +1 -1
@@ -395,7 +395,7 @@ extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk,extern void tcp_parse_options (struct sk_buff *skb,struct tcp_options_received *opt_rx, -int estab); +int estab,u32 tsecr_off);/* * TCP v4 functions exported for the inet6 API
net/ipv4/tcp_input.c +6 -6
@@ -3264,7 +3264,7 @@ uninteresting_ack:* the fast version below fails. */ void tcp_parse_options (struct sk_buff *skb,struct tcp_options_received *opt_rx, -int estab) +int estab,u32 tsecr_off) {unsigned char *ptr;struct tcphdr *th =tcp_hdr (skb);
@@ -3322,7 +3322,7 @@ void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx, (!estab && sysctl_tcp_timestamps))) { opt_rx->saw_tstamp =1 ; opt_rx->rcv_tsval =ntohl (get_unaligned ((__be32 *)ptr)); - opt_rx->rcv_tsecr =ntohl (get_unaligned ((__be32 *)(ptr+4 ))); + opt_rx->rcv_tsecr =ntohl (get_unaligned ((__be32 *)(ptr+4 ))) - tsecr_off; }break ;case TCPOPT_SACK_PERM :
@@ -3374,11 +3374,11 @@ static int tcp_fast_parse_options(struct sk_buff *skb, struct tcphdr *th, ++ptr; tp->rx_opt.rcv_tsval =ntohl (*ptr); ++ptr; - tp->rx_opt.rcv_tsecr =ntohl (*ptr); + tp->rx_opt.rcv_tsecr =ntohl (*ptr) - tp->ts_off;return 1 ; } } -tcp_parse_options (skb, &tp->rx_opt,1 ); +tcp_parse_options (skb, &tp->rx_opt,1 , tp->ts_off);return 1 ; }
@@ -4615,7 +4615,7 @@ int tcp_rcv_established(struct sock *sk, struct sk_buff *skb, ++ptr; tp->rx_opt.rcv_tsval =ntohl (*ptr); ++ptr; - tp->rx_opt.rcv_tsecr =ntohl (*ptr); + tp->rx_opt.rcv_tsecr =ntohl (*ptr) - tp->ts_off;/* If PAWS failed, check it more carefully in slow path */ if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) <0 )
@@ -4824,7 +4824,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,struct inet_connection_sock *icsk =inet_csk (sk);int saved_clamp = tp->rx_opt.mss_clamp; -tcp_parse_options (skb, &tp->rx_opt,0 ); +tcp_parse_options (skb, &tp->rx_opt,0 , tp->ts_off);if (th->ack) {/* rfc793:
net/ipv4/tcp_ipv4.c +1 -1
@@ -1295,7 +1295,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb) tmp_opt.mss_clamp =536 ; tmp_opt.user_mss =tcp_sk (sk)->rx_opt.user_mss; -tcp_parse_options (skb, &tmp_opt,0 ); +tcp_parse_options (skb, &tmp_opt,0 ,0 );if (want_cookie) {tcp_clear_options (&tmp_opt);
net/ipv4/tcp_minisocks.c +3 -2
@@ -102,7 +102,7 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb, tmp_opt.saw_tstamp =0 ;if (th->doff > (sizeof (*th) >>2 ) && tcptw->tw_ts_recent_stamp) { -tcp_parse_options (skb, &tmp_opt,0 ); +tcp_parse_options (skb, &tmp_opt,0 ,0 );if (tmp_opt.saw_tstamp) { tmp_opt.ts_recent = tcptw->tw_ts_recent;
@@ -442,6 +442,7 @@ struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req,inet_csk_reset_keepalive_timer (newsk,keepalive_time_when (newtp)); + newtp->ts_off = treq->ts_off; newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;if ((newtp->rx_opt.sack_ok = ireq->sack_ok) !=0 ) {if (sysctl_tcp_fack)
@@ -502,7 +503,7 @@ struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb, tmp_opt.saw_tstamp =0 ;if (th->doff > (sizeof (struct tcphdr )>>2 )) { -tcp_parse_options (skb, &tmp_opt,0 ); +tcp_parse_options (skb, &tmp_opt,0 ,tcp_rsk (req)->ts_off);if (tmp_opt.saw_tstamp) { tmp_opt.ts_recent = req->ts_recent;
net/ipv4/tcp_output.c +5 -3
@@ -583,7 +583,7 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, (sysctl_flags &SYSCTL_FLAG_SACK ), (sysctl_flags &SYSCTL_FLAG_WSCALE ), tp->rx_opt.rcv_wscale, - tcb->when, + tcb->when + tp->ts_off, tp->rx_opt.ts_recent, #ifdef CONFIG_TCP_MD5SIG
@@ -592,7 +592,7 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, NULL); }else { tcp_build_and_update_options((__be32 *)(th + 1), - tp, tcb->when, + tp, tcb->when + tp->ts_off, #ifdef CONFIG_TCP_MD5SIG md5 ? &md5_hash_location :#endif
@@ -2227,9 +2227,10 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst, th->window =htons (min (req->rcv_wnd,65535U ));TCP_SKB_CB (skb)->when = tcp_time_stamp; +tcp_rsk (req)->ts_off =1000 -TCP_SKB_CB (skb)->when; tcp_syn_build_options((__be32 *)(th + 1), dst_metric(dst, RTAX_ADVMSS), ireq->tstamp_ok, ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale, - TCP_SKB_CB(skb)->when, + TCP_SKB_CB(skb)->when + tcp_rsk(req)->ts_off, req->ts_recent, ( #ifdef CONFIG_TCP_MD5SIG
@@ -2335,6 +2336,7 @@ int tcp_connect(struct sock *sk)/* Send it off. */ TCP_SKB_CB (buff)->when = tcp_time_stamp; + tp->ts_off =2000 -TCP_SKB_CB (buff)->when; tp->retrans_stamp =TCP_SKB_CB (buff)->when;skb_header_release (buff);__tcp_add_write_queue_tail (sk, buff);
net/ipv6/tcp_ipv6.c +1 -1
@@ -1264,7 +1264,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb) tmp_opt.mss_clamp =IPV6_MIN_MTU -sizeof (struct tcphdr ) -sizeof (struct ipv6hdr ); tmp_opt.user_mss = tp->rx_opt.user_mss; -tcp_parse_options (skb, &tmp_opt,0 ); +tcp_parse_options (skb, &tmp_opt,0 ,0 ); tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;tcp_openreq_init (req, &tmp_opt, skb);
Signature · 1 line
1.5.4
PATCH 2/3 Enable the use of TCP options with syncookies
Allow the use of extended tcp options when syncookies are in use and the client supports tcp timestamps.
The general concept is to encode the tcp options as the initial timestamp that we send to the requestor in our synack. We can then decode the timestamp echo reply value on the returning ack.
This implementation encodes the following options in the timestamp, snd_wscale, rcv_wscale, sack_ok, and also the necessary bits to calculate the rtt, and ts_off accurately. Note that there are still 5 unused bits that could be used for future options.
--- include/linux/tcp.h | 3 ++ include/net/tcp.h | 4 ++ net/ipv4/syncookies.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++-- net/ipv4/tcp_ipv4.c | 5 +-- net/ipv4/tcp_output.c | 10 +++++- 5 files changed, 104 insertions(+), 8 deletions(-)
include/linux/tcp.h +3 -0
@@ -233,6 +233,9 @@ struct tcp_request_sock {u32 rcv_isn;u32 snt_isn;u32 ts_off; +#ifdef CONFIG_SYN_COOKIES +u8 want_cookie_ts; +#endif };static inline struct tcp_request_sock *tcp_rsk (const struct request_sock *req)
include/net/tcp.h +4 -0
@@ -438,6 +438,7 @@ extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb, struct ip_options *opt);extern __u32 cookie_v4_init_sequence (struct sock *sk,struct sk_buff *skb,__u16 *mss); +extern __u32 cookie_init_timestamp (struct request_sock *req);/* tcp_output.c */
@@ -958,6 +959,9 @@ static inline void tcp_openreq_init(struct request_sock *req, ireq->acked =0 ; ireq->ecn_ok =0 ; ireq->rmt_port =tcp_hdr (skb)->source; +#ifdef CONFIG_SYN_COOKIES +tcp_rsk (req)->want_cookie_ts =0 ; +#endif }extern void tcp_enter_memory_pressure (void );
net/ipv4/syncookies.c +86 -4
145 lines not on this page · raw message
net/ipv4/tcp_ipv4.c +2 -3
@@ -1297,10 +1297,8 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)tcp_parse_options (skb, &tmp_opt,0 ,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
@@ -1328,6 +1326,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)if (want_cookie) {#ifdef CONFIG_SYN_COOKIES syn_flood_warning (skb); +tcp_rsk (req)->want_cookie_ts = tmp_opt.saw_tstamp;#endif isn =cookie_v4_init_sequence (sk, skb, &req->mss); }else if (!isn) {
net/ipv4/tcp_output.c +9 -1
@@ -2227,7 +2227,15 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst, th->window =htons (min (req->rcv_wnd,65535U ));TCP_SKB_CB (skb)->when = tcp_time_stamp; -tcp_rsk (req)->ts_off =1000 -TCP_SKB_CB (skb)->when; + +#ifdef CONFIG_SYN_COOKIES +if (tcp_rsk (req)->want_cookie_ts) +tcp_rsk (req)->ts_off =cookie_init_timestamp (req) - +TCP_SKB_CB (skb)->when; + else +#endif +tcp_rsk (req)->ts_off =1000 -TCP_SKB_CB (skb)->when; + tcp_syn_build_options((__be32 *)(th + 1), dst_metric(dst, RTAX_ADVMSS), ireq->tstamp_ok, ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale, TCP_SKB_CB(skb)->when + tcp_rsk(req)->ts_off,
Signature · 1 line
1.5.4
PATCH 3/3 Add IPv6 Support to TCP SYN cookies
Support IPv6 syncookies
--- include/net/tcp.h | 10 ++ net/ipv4/syncookies.c | 9 +- net/ipv4/tcp_input.c | 1 + net/ipv4/tcp_minisocks.c | 2 + net/ipv4/tcp_output.c | 1 + net/ipv6/Makefile | 1 + net/ipv6/syncookies.c | 284 ++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/tcp_ipv6.c | 78 +++++++++---- 8 files changed, 359 insertions(+), 27 deletions(-) create mode 100644 net/ipv6/syncookies.c
include/net/tcp.h +10 -0
@@ -29,6 +29,7 @@#include <linux/skbuff.h> #include <linux/dmaengine.h> #include <linux/crypto.h> +#include <linux/cryptohash.h> #include <net/inet_connection_sock.h> #include <net/inet_timewait_sock.h>
@@ -434,11 +435,19 @@ extern int tcp_disconnect(struct sock *sk, int flags);extern void tcp_unhash (struct sock *sk);/* From syncookies.c */ +extern __u32 syncookie_secret[2 ][16 -3 +SHA_DIGEST_WORDS ];extern struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb,struct ip_options *opt);extern __u32 cookie_v4_init_sequence (struct sock *sk,struct sk_buff *skb,__u16 *mss);extern __u32 cookie_init_timestamp (struct request_sock *req); +extern __u32 cookie_check_timestamp (__u32 ts, +struct tcp_options_received *tcp_opt); + +/* 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, +__u16 *mss);/* tcp_output.c */
@@ -1336,6 +1345,7 @@ extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);extern void tcp_proc_unregister (struct tcp_seq_afinfo *afinfo);extern struct request_sock_ops tcp_request_sock_ops; +extern struct request_sock_ops tcp6_request_sock_ops;extern int tcp_v4_destroy_sock (struct sock *sk);
net/ipv4/syncookies.c +4 -5
@@ -10,8 +10,6 @@* 2 of the License, or (at your option) any later version. * * $Id: syncookies.c,v 1.18 2002/02/01 22:01:04 davem Exp $ -* -* Missing: IPv6 support. */ #include <linux/tcp.h>
@@ -23,7 +21,8 @@extern int sysctl_tcp_syncookies; -static __u32 syncookie_secret[2 ][16 -3 +SHA_DIGEST_WORDS ]; +__u32 syncookie_secret[2 ][16 -3 +SHA_DIGEST_WORDS ]; +EXPORT_SYMBOL (syncookie_secret);static __init int init_syncookies (void ) {
@@ -216,8 +215,7 @@ static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,* The next 4 lsb are for rcv_wscale * The next 1 lsb are for sack_ok */ -static __u32 cookie_check_timestamp (__u32 ts, -struct tcp_options_received *tcp_opt) +__u32 cookie_check_timestamp (__u32 ts,struct tcp_options_received *tcp_opt) {__u32 init_ts = ts &TSMASK ;
@@ -249,6 +247,7 @@ static __u32 cookie_check_timestamp(__u32 ts,return init_ts; } +EXPORT_SYMBOL (cookie_check_timestamp);struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb,struct ip_options *opt)
net/ipv4/tcp_input.c +1 -0
@@ -5326,6 +5326,7 @@ discard:EXPORT_SYMBOL (sysctl_tcp_ecn);EXPORT_SYMBOL (sysctl_tcp_reordering); +EXPORT_SYMBOL (sysctl_tcp_adv_win_scale);EXPORT_SYMBOL (tcp_parse_options);EXPORT_SYMBOL (tcp_rcv_established);EXPORT_SYMBOL (tcp_rcv_state_process);
net/ipv4/tcp_minisocks.c +2 -0
@@ -35,6 +35,8 @@#endif int sysctl_tcp_syncookies__read_mostly =SYNC_INIT ; +EXPORT_SYMBOL (sysctl_tcp_syncookies); +int sysctl_tcp_abort_on_overflow__read_mostly ;struct inet_timewait_death_row tcp_death_row = {
net/ipv4/tcp_output.c +1 -0
@@ -2570,6 +2570,7 @@ void tcp_send_probe0(struct sock *sk) } } +EXPORT_SYMBOL (tcp_select_initial_window);EXPORT_SYMBOL (tcp_connect);EXPORT_SYMBOL (tcp_make_synack);EXPORT_SYMBOL (tcp_simple_retransmit);
net/ipv6/Makefile +1 -0
@@ -16,6 +16,7 @@ ipv6-$(CONFIG_XFRM) += xfrm6_policy.o xfrm6_state.o xfrm6_input.o \ ipv6-$(CONFIG_NETFILTER) += netfilter.o ipv6-$(CONFIG_IPV6_MULTIPLE_TABLES) += fib6_rules.o ipv6-$(CONFIG_PROC_FS) += proc.o +ipv6-$(CONFIG_SYN_COOKIES) += syncookies.o ipv6-objs += $(ipv6-y)
net/ipv6/syncookies.c new file +284 -0
284 lines not on this page · raw message
net/ipv6/tcp_ipv6.c +56 -22
133 lines not on this page · raw message
Signature · 1 line
1.5.4
David Miller
maintainerFrom: Glenn Griffin <[email protected]>
Date: Fri, 15 Feb 2008 09:47:08 -0800
Quoted 9 lines
Introduce the ability to send arbitrary initial tcp timestamps that are not tied directly to jiffies. The basic conecpt is every
tcp_request_sockandtcp_socknow has ats_offoffset that represents the difference betweentcp_time_stampand the timestamp we send and expect to
receive.This has the advantage of not divulging system information (uptime) depending on the policy chosen for the initial timestamps.
Quoted 6 lines
A policy where
ts_offis always set to zero should produce no change in behavior. The policy implemented is not intended for real use, but just as a simple example. A realistic example would probably be similar to the tcp init sequence generator.Signed-off-by Glenn Griffin <[email protected]>
Adding yet another member to the already bloated tcp_sock structure to implement this is too high a cost.
I would instead prefer that there be some global random number calculated when the first TCP socket is created, and use that as a global offset. You can even recompute it every few hours if you like.
We do similar things already elsewhere.
Quoted 2 lines
Adding yet another member to the already bloated
tcp_sockstructure to implement this is too high a cost.
Yes, I was worried that would be deemed too high of a cost, but it was the most efficient way I could think to accomplish what I wanted.
Quoted 4 lines
I would instead prefer that there be some global random number calculated when the first TCP socket is created, and use that as a global offset. You can even recompute it every few hours if you like.
That would work fine if my mine purpose was to randomize the tcp timestamp to mitigate the leak in information regarding uptime, but despite the brief description, that's only a side effect of what I intended to do. What I wanted was a way to be able to choose an initial tcp timestamp for a particular connection that was not tied directly to jiffies.
The two patches following this show my intended use case. I intend to enhance syncookie support to allow it to support advanced tcp options (sack and window scaling). Normally syncookies encode the bare minimum state of a connection in the ISN they choose, but the 32bit ISN isn't enough to encode advanced tcp options so you are left with a working but crippled tcp stack during a synflood attack. If in addition to choosing an ISN we are able to choose an initial tcp timestamp, we are then able to encode an additional 32 bits of information that can contain more of the advanced tcp options.
This stems from a discussion about implementing IPv6 support for syncookies, and the main concern being that syncookies disabled too many valuable tcp features to be relevant on modern systems. Many people stood in opposition to that statement, but it did not seem as though a general consensus was reached. http://lkml.org/lkml/2008/2/4/396
I'm always open to alternatives.
--Glenn
Quoted 18 lines
Quoted 2 lines
Adding yet another member to the already bloated
tcp_sockstructure to implement this is too high a cost.Yes, I was worried that would be deemed too high of a cost, but it was the most efficient way I could think to accomplish what I wanted.
Quoted 4 lines
I would instead prefer that there be some global random number calculated when the first TCP socket is created, and use that as a global offset. You can even recompute it every few hours if you like.
That would work fine if my mine purpose was to randomize the tcp timestamp to mitigate the leak in information regarding uptime, but despite the brief description, that's only a side effect of what I intended to do. What I wanted was a way to be able to choose an initial tcp timestamp for a particular connection that was not tied directly to jiffies.
Quoted 9 lines
The two patches following this show my intended use case. I intend to enhance syncookie support to allow it to support advanced tcp options (sack and window scaling). Normally syncookies encode the bare minimum state of a connection in the ISN they choose, but the 32bit ISN isn't enough to encode advanced tcp options so you are left with a working but crippled tcp stack during a synflood attack. If in addition to choosing an ISN we are able to choose an initial tcp timestamp, we are then able to encode an additional 32 bits of information that can contain more of the advanced tcp options.
Perhaps I should clarify. I don't see a way to implement the additional syncookie enhancements without storing an offset in some type of per-socket structure. Given that, is it still deemed too high of a cost? Is enhancing syncookies not deemed important enough to warrant the additional 4 bytes? What if there was an additional config variable to support arbitrary/random tcp timestamps, and then syncookies only used the additional options when that setting was chosen? Is there some possiblity I'm missing that could get this feature in a form suitable for inclusion? Thanks.
--Glenn