Generic TCP-option framework and adoption for TCP-SMC and TCP-MD5
Subsystems (5)
Christoph Paasch
cover Generic TCP-option framework and adoption for TCP-SMC and TCP-MD5
Final post to the mptcp-list before submission to netdev :)
---
This patchset introduces a generic framework for handling TCP-options.
TCP-options like TCP_MD5 and SMC are rather rare use-cases, but their implementation is rather intrusive to the TCP-stack. Other, more recent TCP extensions like TCP-crypt, MPTCP or TCP-AO would make this situation even worse.
This new framework allows to add these TCP-options in a modular way. Writing, reading and acting upon these options is done through callbacks that get registered to a TCP-socket. A TCP-socket has a list of "extra" TCP-options that it will use.
We make TCP-SMC and TCP-MD5SIG adopt this new framework. As can be seen, there is now no more TCP-SMC code in the TCP-files and the TCP-MD5 code has been reduced to a bare minimum.
This patchset is admittedly rather big, but we wanted to show where the framework will lead to and what it enables. Suggestions as to how to better structure the patchset is appreciated.
One point of discussion might be whether or not we should use static-keys before accessing the tcp_option_list to avoid branching and the additional access to the tcp_sock, request-sock, time-wait-sock structure.
The way static keys could be used is that they get incremented each time a socket starts using one of the new TCP-options and decremented when the socket eventually gets destroyed. A caveat of this design would be that if a host keeps on creating/closing these sockets in a sequence, each time we go into the slow path of the static keys occuring potentially a big overhead to update all the jump-labels.
For now we opted for a simple if (unlikely(!hlist_empty(...)) check.
Feedback is very welcome!
Thanks,
Mat & Christoph
Christoph Paasch (13):
tcp: Write options after the header has been fully done
tcp: Pass sock and skb to tcp_options_write
tcp: Allow tcp_fast_parse_options to drop segments
tcp_smc: Make smc_parse_options return 1 on success
tcp_smc: Make SMC use TCP extra-option framework
tcp_md5: Don't pass along md5-key
tcp_md5: Detect key inside tcp_v4_send_ack instead of passing it as an
argument
tcp_md5: Detect key inside tcp_v6_send_response instead of passing it
as an argument
tcp_md5: Check for TCP_MD5 after TCP Timestamps in
tcp_established_options
tcp_md5: Move TCP-MD5 code out of TCP itself
tcp_md5: Use tcp_extra_options in output path
tcp_md5: Cleanup TCP-code
tcp_md5: Use TCP extra-options on the input path
Mat Martineau (1):
tcp: Register handlers for extra TCP options
drivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/inet_diag.h | 1 + include/linux/tcp.h | 43 +- include/linux/tcp_md5.h | 39 ++ include/net/inet_sock.h | 3 +- include/net/tcp.h | 212 +++--- net/ipv4/Makefile | 1 + net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 391 ++++++++--- net/ipv4/tcp_diag.c | 81 +-- net/ipv4/tcp_input.c | 137 ++-- net/ipv4/tcp_ipv4.c | 552 ++-------------- net/ipv4/tcp_md5.c | 1359 ++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_minisocks.c | 77 +-- net/ipv4/tcp_output.c | 182 +---- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 386 ++--------- net/smc/af_smc.c | 190 +++++- 18 files changed, 2222 insertions(+), 1446 deletions(-) create mode 100644 include/linux/tcp_md5.h create mode 100644 net/ipv4/tcp_md5.c
Signature · 1 line
2.15.0
Christoph Paasch
RFC 1/14 tcp: Write options after the header has been fully done
The generic TCP-option framework will need to have access to the full TCP-header (e.g., if we want to compute a checksum for TCP-MD5).
Thus, we move the call to tcp_options_write() to after all the fields in the header have been filled out.
--- net/ipv4/tcp_output.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_output.c +2 -2
@@ -1126,7 +1126,6 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, } } -tcp_options_write ((__be32 *)(th +1 ), tp, &opts);skb_shinfo (skb)->gso_type = sk->sk_gso_type;if (likely (!(tcb->tcp_flags &TCPHDR_SYN ))) { th->window =htons (tcp_select_window (sk));
@@ -1137,6 +1136,7 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,*/ th->window =htons (min (tp->rcv_wnd,65535U )); } +tcp_options_write ((__be32 *)(th +1 ), tp, &opts);#ifdef CONFIG_TCP_MD5SIG /* Calculate the MD5 hash, as we have all we need now */ if (md5) {
@@ -3242,8 +3242,8 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ th->window =htons (min (req->rsk_rcv_wnd,65535U )); -tcp_options_write ((__be32 *)(th +1 ),NULL , &opts); th->doff = (tcp_header_size >>2 ); +tcp_options_write ((__be32 *)(th +1 ),NULL , &opts);__TCP_INC_STATS (sock_net (sk),TCP_MIB_OUTSEGS );#ifdef CONFIG_TCP_MD5SIG
Signature · 1 line
2.15.0
Christoph Paasch
RFC 2/14 tcp: Pass sock and skb to tcp_options_write
In reply to Christoph PaaschAn upcoming patch adds a configurable, per-socket list of TCP options to populate in the TCP header. This requires tcp_options_write() to know the socket (to use the options list) and the skb (to provide visibility to the packet data for options like TCP_MD5SIG).
--- net/ipv4/tcp_output.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
net/ipv4/tcp_output.c +7 -3
@@ -444,10 +444,14 @@ struct tcp_out_options {* At least SACK_PERM as the first option is known to lead to a disaster * (but it may well be that other scenarios fail similarly). */ -static void tcp_options_write (__be32 *ptr,struct tcp_sock *tp, +static void tcp_options_write (__be32 *ptr,struct sk_buff *skb,struct sock *sk,struct tcp_out_options *opts) {u16 options = opts->options;/* mungable copy */ +struct tcp_sock *tp =NULL ; + +if (sk_fullsock (sk)) + tp =tcp_sk (sk);if (unlikely (OPTION_MD5 & options)) { *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
@@ -1136,7 +1140,7 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,*/ th->window =htons (min (tp->rcv_wnd,65535U )); } -tcp_options_write ((__be32 *)(th +1 ), tp, &opts); +tcp_options_write ((__be32 *)(th +1 ), skb, sk, &opts);#ifdef CONFIG_TCP_MD5SIG /* Calculate the MD5 hash, as we have all we need now */ if (md5) {
@@ -3243,7 +3247,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ th->window =htons (min (req->rsk_rcv_wnd,65535U )); th->doff = (tcp_header_size >>2 ); -tcp_options_write ((__be32 *)(th +1 ),NULL , &opts); +tcp_options_write ((__be32 *)(th +1 ), skb,req_to_sk (req), &opts);__TCP_INC_STATS (sock_net (sk),TCP_MIB_OUTSEGS );#ifdef CONFIG_TCP_MD5SIG
Signature · 1 line
2.15.0
Christoph Paasch
RFC 3/14 tcp: Allow tcp_fast_parse_options to drop segments
In reply to Christoph PaaschAfter parsing the TCP-options, some option-kinds might trigger a drop of the segment (e.g., as is the case for TCP_MD5). As we are moving to consolidate the TCP_MD5-code in follow-up patches, we need to add the capability to drop a segment right after parsing the options in tcp_fast_parse_options().
Originally, tcp_fast_parse_options() returned false, when there is no timestamp option, except in the case of the slow-path processing through tcp_parse_options() where it always returns true.
So, the return-value of tcp_fast_parse_options() was kind of inconsistent. With this patch, we make it return true when the segment should get dropped based on the parsed options, and false otherwise.
In tcp_validate_incoming, we will then just check for tp->rx_opt.saw_tstamp to see if we should verify PAWS.
The goto will be used in a follow-up patch to check whether one of the options triggers a drop of the segment.
--- net/ipv4/tcp_input.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
net/ipv4/tcp_input.c +10 -5
@@ -3827,6 +3827,8 @@ static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr/* Fast parse options. This hopes to only see timestamps. * If it is wrong it falls back on tcp_parse_options(). +* +* Returns true if we should drop this packet based on present TCP-options. */ static bool tcp_fast_parse_options(const struct net *net, const struct sk_buff *skb,
@@ -3837,18 +3839,19 @@ static bool tcp_fast_parse_options(const struct net *net,*/ if (th->doff == (sizeof (*th) /4 )) { tp->rx_opt.saw_tstamp =0 ; -return false; + goto extra_opt_check; }else if (tp->rx_opt.tstamp_ok && th->doff == ((sizeof (*th) +TCPOLEN_TSTAMP_ALIGNED ) /4 )) {if (tcp_parse_aligned_timestamp (tp, th)) -return true; + goto extra_opt_check; }tcp_parse_options (net, skb, &tp->rx_opt,1 ,NULL );if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) tp->rx_opt.rcv_tsecr -= tp->tsoffset; -return true; +extra_opt_check: +return false; }#ifdef CONFIG_TCP_MD5SIG
@@ -5168,9 +5171,11 @@ static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,struct tcp_sock *tp =tcp_sk (sk);bool rst_seq_match = false; +if (tcp_fast_parse_options (sock_net (sk), skb, th, tp)) + goto discard; +/* RFC1323: H1. Apply PAWS check first. */ -if (tcp_fast_parse_options (sock_net (sk), skb, th, tp) && - tp->rx_opt.saw_tstamp && +if (tp->rx_opt.saw_tstamp &&tcp_paws_discard (sk, skb)) {if (!th->rst) {NET_INC_STATS (sock_net (sk),LINUX_MIB_PAWSESTABREJECTED );
Signature · 1 line
2.15.0
Christoph Paasch
RFC 5/14 tcp: Register handlers for extra TCP options
In reply to Christoph PaaschFrom: Mat Martineau <mathew.j.martineau(a)linux.intel.com>
Allow additional TCP options to be handled by registered hook functions.
Registered options have a priority that determines the order in which options are prepared and written. Lower priority numbers are handled first.
Option parsing will call the provided 'parse' function when a TCP option number is not recognized by the normal option parsing code.
After parsing, there are two places where we post-process the options. First, a 'check' callback that allows to drop the packet based on the parsed options (e.g., usefull for TCP MD5SIG). Then, a 'post_process' function that gets called after other validity checks (aka, in-window, PAWS,...). This post_process function can then update other state for this particular extra-option.
In the output-path, the 'prepare' function determines the required space for registered options and store associated data. 'write' adds the option to the TCP header.
A static key is used to minimize the performance impact.
These additional TCP-options are stored in hlists of the TCP-socket. To pass the state and options around during the 3-way handshake and in time-wait state, the hlists are also on the tcp_request_sock and tcp_timewait_sock.
The list is copied from the listener to the request-socket (calling into the 'copy' callback). Then, moved from the request-socket to the TCP-socket and finally to the time-wait socket.
--- drivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/tcp.h | 28 ++++ include/net/tcp.h | 109 ++++++++++++- net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 327 ++++++++++++++++++++++++++++++++++++++- net/ipv4/tcp_input.c | 49 +++++- net/ipv4/tcp_ipv4.c | 97 +++++++++--- net/ipv4/tcp_minisocks.c | 32 +++- net/ipv4/tcp_output.c | 40 ++--- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 32 ++++ 11 files changed, 674 insertions(+), 54 deletions(-)
drivers/infiniband/hw/cxgb4/cm.c +1 -1
@@ -3746,7 +3746,7 @@ static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)*/ memset (&tmp_opt,0 ,sizeof (tmp_opt));tcp_clear_options (&tmp_opt); -tcp_parse_options (&init_net, skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (&init_net, skb, &tmp_opt,0 ,NULL ,NULL ); req =__skb_push (skb,sizeof (*req));memset (req,0 ,sizeof (*req));
include/linux/tcp.h +28 -0
@@ -115,6 +115,24 @@ static inline void tcp_clear_options(struct tcp_options_received *rx_opt)#endif } +#define OPTION_SACK_ADVERTISE (1 << 0) +#define OPTION_TS (1 << 1) +#define OPTION_MD5 (1 << 2) +#define OPTION_WSCALE (1 << 3) +#define OPTION_FAST_OPEN_COOKIE (1 << 8) +#define OPTION_SMC (1 << 9) + +struct tcp_out_options { +u16 options;/* bit field of OPTION_* */ +u16 mss;/* 0 to disable */ +u8 ws;/* window scale, 0 to disable */ +u8 num_sack_blocks;/* number of SACK blocks to include */ +u8 hash_size;/* bytes in hash_location */ +__u8 *hash_location;/* temporary pointer, overloaded */ +__u32 tsval, tsecr;/* need to include OPTION_TS */ +struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ +}; +/* This is the max number of SACKS that we'll generate and process. It's safe * to increase this, although since: * size = TCPOLEN_SACK_BASE_ALIGNED (4) + n * TCPOLEN_SACK_PERBLOCK (8)
@@ -137,6 +155,7 @@ struct tcp_request_sock {* FastOpen it's the seq# * after data-in-SYN. */ +struct hlist_head tcp_option_list; };static inline struct tcp_request_sock *tcp_rsk (const struct request_sock *req)
@@ -373,6 +392,8 @@ struct tcp_sock {*/ struct request_sock *fastopen_rsk;u32 *saved_syn; + +struct hlist_head tcp_option_list; };enum tsq_enum {
@@ -400,6 +421,11 @@ static inline struct tcp_sock *tcp_sk(const struct sock *sk)return (struct tcp_sock *)sk; } +static inline struct sock *tcp_to_sk (const struct tcp_sock *tp) +{ +return (struct sock *)tp; +} +struct tcp_timewait_sock {struct inet_timewait_sock tw_sk;#define tw_rcv_nxt tw_sk.__tw_common.skc_tw_rcv_nxt
@@ -412,6 +438,8 @@ struct tcp_timewait_sock {u32 tw_last_oow_ack_time;long tw_ts_recent_stamp; + +struct hlist_head tcp_option_list;#ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *tw_md5_key;#endif
include/net/tcp.h +108 -1
125 lines not on this page · raw message
net/ipv4/syncookies.c +5 -1
@@ -313,7 +313,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL , sk);if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { tsoff = secure_tcp_ts_off(sock_net(sk),
@@ -325,6 +325,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) goto out; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tcp_opt)) + goto out; + ret =NULL ; req =inet_reqsk_alloc (&tcp_request_sock_ops, sk, false);/* for safety */ if (!req)
net/ipv4/tcp.c +326 -1
345 lines not on this page · raw message
net/ipv4/tcp_input.c +41 -8
121 lines not on this page · raw message
net/ipv4/tcp_ipv4.c +79 -18
180 lines not on this page · raw message
net/ipv4/tcp_minisocks.c +27 -5
@@ -95,9 +95,10 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,struct tcp_timewait_sock *tcptw =tcp_twsk ((struct sock *)tw);bool paws_reject = false; - tmp_opt.saw_tstamp =0 ; +tcp_clear_options (&tmp_opt);if (th->doff > (sizeof (*th) >>2 ) && tcptw->tw_ts_recent_stamp) { -tcp_parse_options (twsk_net (tw), skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (twsk_net (tw), skb, &tmp_opt,0 ,NULL , + (struct sock *)tw);if (tmp_opt.saw_tstamp) {if (tmp_opt.rcv_tsecr)
@@ -108,6 +109,10 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb, } } +if (unlikely (!hlist_empty (&tcptw->tcp_option_list)) && +tcp_extopt_check ((struct sock *)tw, skb, &tmp_opt)) +return TCP_TW_SUCCESS ; +if (tw->tw_substate ==TCP_FIN_WAIT2 ) {/* Just repeat all the checks of tcp_rcv_state_process() */
@@ -251,7 +256,7 @@ EXPORT_SYMBOL(tcp_timewait_state_process);void tcp_time_wait (struct sock *sk,int state,int timeo) {const struct inet_connection_sock *icsk =inet_csk (sk); -const struct tcp_sock *tp =tcp_sk (sk); +struct tcp_sock *tp =tcp_sk (sk);struct inet_timewait_sock *tw;struct inet_timewait_death_row *tcp_death_row = &sock_net (sk)->ipv4.tcp_death_row;
@@ -271,6 +276,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp; tcptw->tw_ts_offset = tp->tsoffset; tcptw->tw_last_oow_ack_time =0 ; +INIT_HLIST_HEAD (&tcptw->tcp_option_list);#if IS_ENABLED (CONFIG_IPV6 )if (tw->tw_family ==PF_INET6 ) {
@@ -284,6 +290,10 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) }#endif +if (unlikely (!hlist_empty (&tp->tcp_option_list))) { +tcp_extopt_move (sk, (struct sock *)tw); +INIT_HLIST_HEAD (&tp->tcp_option_list); + }#ifdef CONFIG_TCP_MD5SIG /* * The timewait bucket does not have the key DB from the
@@ -341,6 +351,9 @@ void tcp_twsk_destructor(struct sock *sk)if (twsk->tw_md5_key)kfree_rcu (twsk->tw_md5_key, rcu);#endif + +if (unlikely (!hlist_empty (&twsk->tcp_option_list))) +tcp_extopt_destroy (sk); }EXPORT_SYMBOL_GPL (tcp_twsk_destructor);
@@ -470,6 +483,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,INIT_LIST_HEAD (&newtp->tsq_node);INIT_LIST_HEAD (&newtp->tsorted_sent_queue); +INIT_HLIST_HEAD (&newtp->tcp_option_list);tcp_init_wl (newtp, treq->rcv_isn);
@@ -545,6 +559,9 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,if (newtp->af_specific->md5_lookup (sk, newsk)) newtp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (unlikely (!hlist_empty (&treq->tcp_option_list))) + newtp->tcp_header_len +=tcp_extopt_add_header (req_to_sk (req), newsk); +if (skb->len >=TCP_MSS_DEFAULT + newtp->tcp_header_len) newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len; newtp->rx_opt.mss_clamp = req->mss;
@@ -587,9 +604,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,bool paws_reject = false;bool own_req; - tmp_opt.saw_tstamp =0 ; +tcp_clear_options (&tmp_opt);if (th->doff > (sizeof (struct tcphdr )>>2 )) { -tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 ,NULL , +req_to_sk (req));if (tmp_opt.saw_tstamp) { tmp_opt.ts_recent = req->ts_recent;
@@ -604,6 +622,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, } } +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list)) && +tcp_extopt_check (req_to_sk (req), skb, &tmp_opt)) +return NULL ; +/* Check for pure retransmitted SYN. */ if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn && flg == TCP_FLAG_SYN &&
net/ipv4/tcp_output.c +22 -18
@@ -398,13 +398,6 @@ static inline bool tcp_urg_mode(const struct tcp_sock *tp)return tp->snd_una != tp->snd_up; } -#define OPTION_SACK_ADVERTISE (1 << 0) -#define OPTION_TS (1 << 1) -#define OPTION_MD5 (1 << 2) -#define OPTION_WSCALE (1 << 3) -#define OPTION_FAST_OPEN_COOKIE (1 << 8) -#define OPTION_SMC (1 << 9) -static void smc_options_write (__be32 *ptr,u16 *options) {#if IS_ENABLED (CONFIG_SMC )
@@ -420,17 +413,6 @@ static void smc_options_write(__be32 *ptr, u16 *options)#endif } -struct tcp_out_options { -u16 options;/* bit field of OPTION_* */ -u16 mss;/* 0 to disable */ -u8 ws;/* window scale, 0 to disable */ -u8 num_sack_blocks;/* number of SACK blocks to include */ -u8 hash_size;/* bytes in hash_location */ -__u8 *hash_location;/* temporary pointer, overloaded */ -__u32 tsval, tsecr;/* need to include OPTION_TS */ -struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ -}; -/* Write previously computed TCP options to the packet. * * Beware: Something in the Internet is very sensitive to the ordering of
@@ -447,12 +429,15 @@ struct tcp_out_options {static void tcp_options_write (__be32 *ptr,struct sk_buff *skb,struct sock *sk,struct tcp_out_options *opts) { +struct hlist_head *extopt_list;u16 options = opts->options;/* mungable copy */ struct tcp_sock *tp =NULL ;if (sk_fullsock (sk)) tp =tcp_sk (sk); + extopt_list =tcp_extopt_get_list (sk); +if (unlikely (OPTION_MD5 & options)) { *ptr++ =htonl ((TCPOPT_NOP <<24 ) | (TCPOPT_NOP <<16 ) | (TCPOPT_MD5SIG <<8 ) |TCPOLEN_MD5SIG );
@@ -543,6 +528,9 @@ static void tcp_options_write(__be32 *ptr, struct sk_buff *skb, struct sock *sk, }smc_options_write (ptr, &options); + +if (unlikely (!hlist_empty (extopt_list))) +tcp_extopt_write (ptr, skb, opts, sk); } static void smc_set_option(const struct tcp_sock *tp,
@@ -645,6 +633,10 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,smc_set_option (tp, opts, &remaining); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + remaining -=tcp_extopt_prepare (skb,TCPHDR_SYN , remaining, + opts,tcp_to_sk (tp)); +return MAX_TCP_OPTION_SPACE - remaining; }
@@ -708,6 +700,11 @@ static unsigned int tcp_synack_options(const struct sock *sk,smc_set_option_cond (tcp_sk (sk), ireq, opts, &remaining); +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) + remaining -=tcp_extopt_prepare (skb,TCPHDR_SYN |TCPHDR_ACK , + remaining, opts, +req_to_sk (req)); +return MAX_TCP_OPTION_SPACE - remaining; }
@@ -741,6 +738,10 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb size +=TCPOLEN_TSTAMP_ALIGNED ; } +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + size +=tcp_extopt_prepare (skb,0 ,MAX_TCP_OPTION_SPACE - size, + opts,tcp_to_sk (tp)); + eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;if (unlikely (eff_sacks)) {const unsigned int remaining =MAX_TCP_OPTION_SPACE - size;
@@ -3303,6 +3304,9 @@ static void tcp_connect_init(struct sock *sk) tp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + tp->tcp_header_len +=tcp_extopt_add_header (sk, sk); +/* If user gave his TCP_MAXSEG, record it to clamp */ if (tp->rx_opt.user_mss) tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
net/ipv6/syncookies.c +5 -1
@@ -162,7 +162,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL , sk);if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { tsoff = secure_tcpv6_ts_off(sock_net(sk),
@@ -174,6 +174,10 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) goto out; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tcp_opt)) + goto out; + ret =NULL ; req =inet_reqsk_alloc (&tcp6_request_sock_ops, sk, false);if (!req)
net/ipv6/tcp_ipv6.c +32 -0
@@ -500,6 +500,9 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,static void tcp_v6_reqsk_destructor (struct request_sock *req) { +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) +tcp_extopt_destroy (req_to_sk (req)); +kfree (inet_rsk (req)->ipv6_opt);kfree_skb (inet_rsk (req)->pktopts); }
@@ -789,6 +792,8 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32unsigned int tot_len =sizeof (struct tcphdr );struct dst_entry *dst;__be32 *topt; +struct hlist_head *extopt_list =NULL ; +struct tcp_out_options extraopts;if (tsecr) tot_len +=TCPOLEN_TSTAMP_ALIGNED ;
@@ -797,6 +802,25 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 tot_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (sk) + extopt_list =tcp_extopt_get_list (sk); + +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining =MAX_TCP_OPTION_SPACE - tot_len; +u8 extraflags = rst ?TCPHDR_RST :0 ; +int used; + +if (!rst || !th->ack) + extraflags |=TCPHDR_ACK ; + +memset (&extraopts,0 ,sizeof (extraopts)); + + used =tcp_extopt_response_prepare (skb, extraflags, remaining, + &extraopts, sk); + + tot_len += used; + } + buff =alloc_skb (MAX_HEADER +sizeof (struct ipv6hdr ) + tot_len,GFP_ATOMIC );if (!buff)
@@ -837,6 +861,9 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 }#endif +if (unlikely (extopt_list && !hlist_empty (extopt_list))) +tcp_extopt_response_write (topt, skb, t1, &extraopts, sk); +memset (&fl6,0 ,sizeof (fl6)); fl6.daddr =ipv6_hdr (skb)->saddr; fl6.saddr =ipv6_hdr (skb)->daddr;
@@ -1196,6 +1223,11 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * newinet->inet_daddr = newinet->inet_saddr =LOOPBACK4_IPV6 ; newinet->inet_rcv_saddr =LOOPBACK4_IPV6 ; +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) { +tcp_extopt_move (req_to_sk (req), newsk); +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list); + } +#ifdef CONFIG_TCP_MD5SIG /* Copy over the MD5 key from the original socket */ key =tcp_v6_md5_do_lookup (sk, &newsk->sk_v6_daddr);
Signature · 1 line
2.15.0
Mat Martineau
Re: [MPTCP] [RFC final 05/14] tcp: Register handlers for extra TCP options
Christoph,
Some comment/commit-message notes below.
Quoted 19 lines · Christoph Paasch
From: Mat Martineau <mathew.j.martineau(a)linux.intel.com>
Allow additional TCP options to be handled by registered hook functions.
Registered options have a priority that determines the order in which options are prepared and written. Lower priority numbers are handled first.
Option parsing will call the provided 'parse' function when a TCP option number is not recognized by the normal option parsing code.
After parsing, there are two places where we post-process the options. First, a 'check' callback that allows to drop the packet based on the parsed options (e.g., usefull for TCP MD5SIG). Then, a '
post_process' function that gets called after other validity checks (aka, in-window, PAWS,...). Thispost_processfunction can then update other state for this particular extra-option.
Quoted 5 lines
In the output-path, the 'prepare' function determines the required space for registered options and store associated data. 'write' adds the option to the TCP header.
A static key is used to minimize the performance impact.
This line should be deleted now that the static branching was removed.
Quoted 173 lines
These additional TCP-options are stored in hlists of the TCP-socket. To pass the state and options around during the 3-way handshake and in time-wait state, the hlists are also on the
tcp_request_sockandtcp_timewait_sock.
The list is copied from the listener to the request-socket (calling into the 'copy' callback). Then, moved from the request-socket to the TCP-socket and finally to the time-wait socket.Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>Signed-off-by Christoph Paasch <cpaasch(a)apple.com>---
drivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/tcp.h | 28 ++++ include/net/tcp.h | 109 ++++++++++++- net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 327 ++++++++++++++++++++++++++++++++++++++- net/ipv4/tcp_input.c | 49 +++++- net/ipv4/tcp_ipv4.c | 97 +++++++++--- net/ipv4/tcp_minisocks.c | 32 +++- net/ipv4/tcp_output.c | 40 ++--- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 32 ++++ 11 files changed, 674 insertions(+), 54 deletions(-)drivers/infiniband/hw/cxgb4/cm.c +1
-1@@ -3746,7 +3746,7 @@ static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)*/ memset (&tmp_opt,0 ,sizeof (tmp_opt));tcp_clear_options (&tmp_opt); -tcp_parse_options (&init_net, skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (&init_net, skb, &tmp_opt,0 ,NULL ,NULL ); req =__skb_push (skb,sizeof (*req));memset (req,0 ,sizeof (*req));include/linux/tcp.h +28
-0@@ -115,6 +115,24 @@ static inline void tcp_clear_options(struct tcp_options_received *rx_opt)#endif } +#define OPTION_SACK_ADVERTISE (1 << 0) +#define OPTION_TS (1 << 1) +#define OPTION_MD5 (1 << 2) +#define OPTION_WSCALE (1 << 3) +#define OPTION_FAST_OPEN_COOKIE (1 << 8) +#define OPTION_SMC (1 << 9) + +struct tcp_out_options { +u16 options;/* bit field of OPTION_* */ +u16 mss;/* 0 to disable */ +u8 ws;/* window scale, 0 to disable */ +u8 num_sack_blocks;/* number of SACK blocks to include */ +u8 hash_size;/* bytes in hash_location */ +__u8 *hash_location;/* temporary pointer, overloaded */ +__u32 tsval, tsecr;/* need to include OPTION_TS */ +struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ +}; +/* This is the max number of SACKS that we'll generate and process. It's safe * to increase this, although since: * size = TCPOLEN_SACK_BASE_ALIGNED (4) + n * TCPOLEN_SACK_PERBLOCK (8) @@ -137,6 +155,7 @@ struct tcp_request_sock {* FastOpen it's the seq# * after data-in-SYN. */ +struct hlist_head tcp_option_list; };static inline struct tcp_request_sock *tcp_rsk (const struct request_sock *req)@@ -373,6 +392,8 @@ struct tcp_sock {*/ struct request_sock *fastopen_rsk;u32 *saved_syn; + +struct hlist_head tcp_option_list; };enum tsq_enum {@@ -400,6 +421,11 @@ static inline struct tcp_sock *tcp_sk(const struct sock *sk)return (struct tcp_sock *)sk; } +static inline struct sock *tcp_to_sk (const struct tcp_sock *tp) +{ +return (struct sock *)tp; +} +struct tcp_timewait_sock {struct inet_timewait_sock tw_sk;#define tw_rcv_nxt tw_sk.__tw_common.skc_tw_rcv_nxt@@ -412,6 +438,8 @@ struct tcp_timewait_sock {u32 tw_last_oow_ack_time;long tw_ts_recent_stamp; + +struct hlist_head tcp_option_list;#ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *tw_md5_key;#endif include/net/tcp.h +45
-1@@ -202,6 +202,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo);#define TCPOLEN_FASTOPEN_BASE 2#define TCPOLEN_EXP_FASTOPEN_BASE 4#define TCPOLEN_EXP_SMC_BASE 6 +#define TCPOLEN_EXP_BASE 6/* But this is what stacks really send out. */ #define TCPOLEN_TSTAMP_ALIGNED 12@@ -403,7 +404,8 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len);void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx, -int estab,struct tcp_fastopen_cookie *foc); +int estab,struct tcp_fastopen_cookie *foc, +struct sock *sk);const u8 *tcp_parse_md5sig_option (const struct tcphdr *th);/* @@ -2063,4 +2065,109 @@ static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)#if IS_ENABLED (CONFIG_SMC )extern struct static_key_false tcp_have_smc;#endif + +struct tcp_extopt_store ; + +struct tcp_extopt_ops { +u32 option_kind; +unsigned char priority; +void (*parse)(int opsize,const unsigned char *opptr, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx, +struct sock *sk, +struct tcp_extopt_store *store); +bool (*check)(struct sock *sk, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx, +struct tcp_extopt_store *store); +void (*post_process)(struct sock *sk, +struct tcp_options_received *opt_rx, +struct tcp_extopt_store *store); +/* Return the number of bytes consumed */ +unsigned int (*prepare)(struct sk_buff *skb,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk, +struct tcp_extopt_store *store); +__be32 *(*write)(__be32 *ptr,struct sk_buff *skb, +struct tcp_out_options *opts,struct sock *sk, +struct tcp_extopt_store *store); +int (*response_prepare)(struct sk_buff *orig,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk, +struct tcp_extopt_store *store); +__be32 *(*response_write)(__be32 *ptr,struct sk_buff *orig, +struct tcphdr *th, +struct tcp_out_options *opts, +const struct sock *sk, +struct tcp_extopt_store *store); +int (*add_header_len)(const struct sock *orig, +const struct sock *sk, +struct tcp_extopt_store *store); + struct tcp_extopt_store *(*copy)(struct sock *listener, + struct request_sock *req,
include/net/tcp.h static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk) · line 2119
Quoted 10 lines
include/net/tcp.h +10
-0+struct tcp_options_received *opt, + struct tcp_extopt_store *from); +struct tcp_extopt_store *(*move)(struct sock *from,struct sock *to, +struct tcp_extopt_store *store); +void (*destroy)(struct tcp_extopt_store *store); +struct module *owner; +}; + +/* Every extra-option in a TCP-socket gets stored in a _store structure which +* has to have tcp_extopt_store as the first element.
Comment can be updated to recommend container_of() instead of positional requirement.
Quoted 358 lines
include/net/tcp.h +53
-0+* +* That way, code registering extra options can pass this to +* tcp_register_extopt, and the surrounding structure can contain per-option +* state. +*/ +struct tcp_extopt_store { +struct hlist_node list; +const struct tcp_extopt_ops *ops; +}; + +struct hlist_head *tcp_extopt_get_list (const struct sock *sk); + +struct tcp_extopt_store *tcp_extopt_find_kind (u32 kind,const struct sock *sk); + +void tcp_extopt_parse (u32 opcode,int opsize,const unsigned char *opptr, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx,struct sock *sk); + +bool tcp_extopt_check (struct sock *sk,const struct sk_buff *skb, +struct tcp_options_received *opt_rx); + +void tcp_extopt_post_process (struct sock *sk, +struct tcp_options_received *opt_rx); + +unsigned int tcp_extopt_prepare (struct sk_buff *skb,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk); + +void tcp_extopt_write (__be32 *ptr,struct sk_buff *skb, +struct tcp_out_options *opts,struct sock *sk); + +int tcp_extopt_response_prepare (struct sk_buff *orig,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk); + +void tcp_extopt_response_write (__be32 *ptr,struct sk_buff *orig, +struct tcphdr *th,struct tcp_out_options *opts, +const struct sock *sk); + +int tcp_extopt_add_header (const struct sock *orig,const struct sock *sk); + +/* Socket lock must be held when calling this function */ +int tcp_register_extopt (struct tcp_extopt_store *store,struct sock *sk); + +void tcp_extopt_copy (struct sock *listener,struct request_sock *req, +struct tcp_options_received *opt); + +void tcp_extopt_move (struct sock *from,struct sock *to); + +void tcp_extopt_destroy (struct sock *sk); +#endif /* _TCP_H */ net/ipv4/syncookies.c +5
-1@@ -313,7 +313,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL , sk);if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { tsoff = secure_tcp_ts_off(sock_net(sk),@@ -325,6 +325,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) goto out; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tcp_opt)) + goto out; + ret =NULL ; req =inet_reqsk_alloc (&tcp_request_sock_ops, sk, false);/* for safety */ if (!req)net/ipv4/tcp.c +265
-0@@ -416,6 +416,7 @@ void tcp_init_sock(struct sock *sk)tcp_init_xmit_timers (sk);INIT_LIST_HEAD (&tp->tsq_node);INIT_LIST_HEAD (&tp->tsorted_sent_queue); +INIT_HLIST_HEAD (&tp->tcp_option_list); icsk->icsk_rto =TCP_TIMEOUT_INIT ; tp->mdev_us =jiffies_to_usecs (TCP_TIMEOUT_INIT );@@ -3473,6 +3474,331 @@ EXPORT_SYMBOL(tcp_md5_hash_key);#endif +struct hlist_head *tcp_extopt_get_list (const struct sock *sk) +{ +if (sk_fullsock (sk)) +return &tcp_sk (sk)->tcp_option_list; +else if (sk->sk_state ==TCP_NEW_SYN_RECV ) +return &tcp_rsk (inet_reqsk (sk))->tcp_option_list; +else if (sk->sk_state ==TCP_TIME_WAIT ) +return &tcp_twsk (sk)->tcp_option_list; + +return NULL ; +} +EXPORT_SYMBOL_GPL (tcp_extopt_get_list); + +/* Caller must ensure that rcu is locked */ +struct tcp_extopt_store *tcp_extopt_find_kind (u32 kind,const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + + lhead =tcp_extopt_get_list (sk); + +hlist_for_each_entry_rcu (entry, lhead, list) { +if (entry->ops->option_kind == kind) +return entry; + } + +return NULL ; +} +EXPORT_SYMBOL_GPL (tcp_extopt_find_kind); + +void tcp_extopt_parse (u32 opcode,int opsize,const unsigned char *opptr, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx,struct sock *sk) +{ +struct tcp_extopt_store *entry; + +rcu_read_lock (); + entry =tcp_extopt_find_kind (opcode, sk); + +if (entry && entry->ops->parse) + entry->ops->parse (opsize, opptr, skb, opt_rx, sk, entry); +rcu_read_unlock (); +} + +bool tcp_extopt_check (struct sock *sk,const struct sk_buff *skb, +struct tcp_options_received *opt_rx) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +bool drop = false; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +bool ret = false; + +if (entry->ops->check) + ret = entry->ops->check (sk, skb, opt_rx, entry); + +if (ret) + drop = true; + } +rcu_read_unlock (); + +return drop; +} +EXPORT_SYMBOL_GPL (tcp_extopt_check); + +void tcp_extopt_post_process (struct sock *sk, +struct tcp_options_received *opt_rx) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (entry->ops->post_process) + entry->ops->post_process (sk, opt_rx, entry); + } +rcu_read_unlock (); +} + +unsigned int tcp_extopt_prepare (struct sk_buff *skb,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +unsigned int used =0 ; + +if (!sk) +return 0 ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->prepare)) +continue ; + + used += entry->ops->prepare (skb, flags, remaining - used, opts, + sk, entry); + } +rcu_read_unlock (); + +return roundup (used,4 ); +} + +void tcp_extopt_write (__be32 *ptr,struct sk_buff *skb, +struct tcp_out_options *opts,struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + +if (!sk) +return ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->write)) +continue ; + + ptr = entry->ops->write (ptr, skb, opts, sk, entry); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_write); + +int tcp_extopt_response_prepare (struct sk_buff *orig,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +unsigned int used =0 ; + +if (!sk) +return 0 ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +int ret; + +if (unlikely (!entry->ops->response_prepare)) +continue ; + + ret = entry->ops->response_prepare (orig, flags, + remaining - used, opts, + sk, entry); + + used += ret; + } +rcu_read_unlock (); + +return roundup (used,4 ); +} +EXPORT_SYMBOL_GPL (tcp_extopt_response_prepare); + +void tcp_extopt_response_write (__be32 *ptr,struct sk_buff *orig, +struct tcphdr *th,struct tcp_out_options *opts, +const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + +if (!sk) +return ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->response_write)) +continue ; + + ptr = entry->ops->response_write (ptr, orig, th, opts, sk, entry); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_response_write); + +int tcp_extopt_add_header (const struct sock *orig,const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +int tcp_header_len =0 ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->add_header_len)) +continue ; + + tcp_header_len += entry->ops->add_header_len (orig, sk, entry); + } +rcu_read_unlock (); + +return tcp_header_len; +} + +/* Socket lock must be held when calling this function */ +int tcp_register_extopt (struct tcp_extopt_store *store,struct sock *sk) +{ +struct hlist_node *add_before =NULL ; +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +int ret =0 ; + + lhead =tcp_extopt_get_list (sk); + +if (!store->ops->option_kind) +return -EINVAL ; + +if (!try_module_get (store->ops->owner)) +return -ENOENT ; + +hlist_for_each_entry_rcu (entry, lhead, list) { +if (entry->ops->option_kind == store->ops->option_kind) { +pr_notice ("Option kind %u already registered\n" , + store->ops->option_kind); +module_put (store->ops->owner); +return -EEXIST ; + } + +if (entry->ops->priority <= store->ops->priority) + add_before = &entry->list; + } + +if (add_before) +hlist_add_behind_rcu (&store->list, add_before); +else +hlist_add_head_rcu (&store->list, lhead); + +pr_debug ("Option kind %u registered\n" , store->ops->option_kind); + +return ret; +} +EXPORT_SYMBOL_GPL (tcp_register_extopt); + +void tcp_extopt_copy (struct sock *listener,struct request_sock *req, +struct tcp_options_received *opt) +{ +struct tcp_extopt_store *entry; +struct hlist_head *from, *to; + + from =tcp_extopt_get_list (listener); + to =tcp_extopt_get_list (req_to_sk (req)); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, from, list) { +struct tcp_extopt_store *new; + +if (!try_module_get (entry->ops->owner)) { +pr_err ("%s Module get failed while copying\n" , __func__);
net/ipv4/tcp.c EXPORT_SYMBOL(tcp_md5_hash_key); · line 3750
Quoted 10 lines
net/ipv4/tcp.c +10
-0+continue ; + } + + new = entry->ops->copy (listener, req, opt, entry); +if (!new) { +module_put (entry->ops->owner); +continue ; + } + +hlist_add_tail_rcu (&new->list, to);
Does this require that req is locked? Or is it expected to be under construction at this point so it won't be accessed in parallel?
net/ipv4/tcp.c EXPORT_SYMBOL(tcp_md5_hash_key); · line 3755
Quoted 5 lines
net/ipv4/tcp.c +5
-0+ } +rcu_read_unlock (); +} + +void tcp_extopt_move (struct sock *from,struct sock *to)
Any locking notes for this function?
Mat
Quoted 699 lines
net/ipv4/tcp.c +46
-1+{ +struct tcp_extopt_store *entry; +struct hlist_head *lfrom, *lto; +struct hlist_node *tmp; + + lfrom =tcp_extopt_get_list (from); + lto =tcp_extopt_get_list (to); + +rcu_read_lock (); +hlist_for_each_entry_safe (entry, tmp, lfrom, list) { +hlist_del_rcu (&entry->list); + +if (entry->ops->move) { + entry = entry->ops->move (from, to, entry); +if (!entry) +continue ; + } + +hlist_add_tail_rcu (&entry->list, lto); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_move); + +void tcp_extopt_destroy (struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +struct hlist_node *tmp; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_safe (entry, tmp, lhead, list) { +struct module *owner = entry->ops->owner; + +hlist_del_rcu (&entry->list); + + entry->ops->destroy (entry); + +module_put (owner); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_destroy); +void tcp_done (struct sock *sk) {struct request_sock *req =tcp_sk (sk)->fastopen_rsk;@@ -3622,7 +3948,6 @@ void __init tcp_init(void)INIT_HLIST_HEAD (&tcp_hashinfo.bhash[i].chain); } - cnt = tcp_hashinfo.ehash_mask +1 ; sysctl_tcp_max_orphans = cnt /2 ;net/ipv4/tcp_input.c +14
-5@@ -3696,7 +3696,7 @@ static int smc_parse_options(const struct tcphdr *th,void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx,int estab, -struct tcp_fastopen_cookie *foc) +struct tcp_fastopen_cookie *foc,struct sock *sk) {const unsigned char *ptr;const struct tcphdr *th =tcp_hdr (skb);@@ -3796,9 +3796,18 @@ void tcp_parse_options(const struct net *net,tcp_parse_fastopen_option (opsize -TCPOLEN_EXP_FASTOPEN_BASE , ptr +2 , th->syn, foc, true); - else -smc_parse_options (th ,opt_rx ,ptr , -opsize ); + else if (smc_parse_options(th, opt_rx, ptr, + opsize)) + break; + else if (opsize >= TCPOLEN_EXP_BASE) + tcp_extopt_parse(get_unaligned_be32(ptr), + opsize, ptr, skb, + opt_rx, sk); +break ; + +default : +tcp_extopt_parse (opcode, opsize, ptr, skb, + opt_rx, sk);break ; }@@ -3849,11 +3858,13 @@ static bool tcp_fast_parse_options(const struct net *net, goto extra_opt_check; } -tcp_parse_options (net, skb, &tp->rx_opt,1 ,NULL ); +tcp_parse_options (net, skb, &tp->rx_opt,1 ,NULL ,tcp_to_sk (tp));if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) tp->rx_opt.rcv_tsecr -= tp->tsoffset;
extra_opt_check:net/ipv4/tcp_input.c +2
-0+if (unlikely (!hlist_empty (&tp->tcp_option_list))) +return tcp_extopt_check (tcp_to_sk (tp), skb, &tp->rx_opt);return false; }net/ipv4/tcp_input.c +25
-3@@ -5327,6 +5338,9 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, tp->rx_opt.saw_tstamp =0 ; +if (!hlist_empty (&tp->tcp_option_list)) + goto slow_path; +/* pred_flags is 0xS?10 << 16 + snd_wnd * if header_prediction is to be made * 'S' will always be tp->tcp_header_len >> 2 @@ -5514,7 +5528,7 @@ static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,/* Get original SYNACK MSS value if user MSS sets mss_clamp */ tcp_clear_options (&opt); opt.user_mss = opt.mss_clamp =0 ; -tcp_parse_options (sock_net (sk), synack, &opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), synack, &opt,0 ,NULL , sk); mss = opt.mss_clamp; }@@ -5577,10 +5591,14 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,int saved_clamp = tp->rx_opt.mss_clamp;bool fastopen_fail; -tcp_parse_options (sock_net (sk), skb, &tp->rx_opt,0 , &foc); +tcp_parse_options (sock_net (sk), skb, &tp->rx_opt,0 , &foc, sk);if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) tp->rx_opt.rcv_tsecr -= tp->tsoffset; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tp->rx_opt)) + goto discard; +if (th->ack) {/* rfc793: * "If the state is SYN-SENT then @@ -5663,6 +5681,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, tp->tcp_header_len =sizeof (struct tcphdr ); } +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_post_process (sk, &tp->rx_opt); +tcp_sync_mss (sk, icsk->icsk_pmtu_cookie);tcp_initialize_rcv_mss (sk);@@ -5756,6 +5777,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,tcp_ecn_rcv_syn (tp, th); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_post_process (sk, &tp->rx_opt); +tcp_mtup_init (sk);tcp_sync_mss (sk, icsk->icsk_pmtu_cookie);tcp_initialize_rcv_mss (sk);@@ -6239,12 +6263,17 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,tcp_rsk (req)->af_specific = af_ops;tcp_rsk (req)->ts_off =0 ; +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list);tcp_clear_options (&tmp_opt); tmp_opt.mss_clamp = af_ops->mss_clamp; tmp_opt.user_mss = tp->rx_opt.user_mss;tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 , - want_cookie ?NULL : &foc); + want_cookie ?NULL : &foc, sk); + +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tmp_opt)) + goto drop_and_free;if (want_cookie && !tmp_opt.saw_tstamp)tcp_clear_options (&tmp_opt);@@ -6305,6 +6334,10 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,tcp_reqsk_record_syn (sk, req, skb); fastopen_sk =tcp_try_fastopen (sk, skb, req, &foc, dst); } + +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_copy (sk, req, &tmp_opt); +if (fastopen_sk) { af_ops->send_synack (fastopen_sk, dst, &fl, req, &foc,TCP_SYNACK_FASTOPEN );net/ipv4/tcp_ipv4.c +79
-18@@ -600,10 +600,9 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)const struct tcphdr *th =tcp_hdr (skb);struct {struct tcphdr th; -#ifdef CONFIG_TCP_MD5SIG -__be32 opt[(TCPOLEN_MD5SIG_ALIGNED >>2 )]; -#endif +__be32 opt[(MAX_TCP_OPTION_SPACE >>2 )]; } rep; +struct hlist_head *extopt_list =NULL ;struct ip_reply_arg arg;#ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *key =NULL ;@@ -613,6 +612,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)struct sock *sk1 =NULL ;#endif struct net *net; +int offset =0 ;/* Never send a reset in response to a reset. */ if (th->rst)@@ -624,6 +624,9 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)if (!sk &&skb_rtable (skb)->rt_type !=RTN_LOCAL )return ; +if (sk) + extopt_list =tcp_extopt_get_list (sk); +/* Swap the send and the receive. */ memset (&rep,0 ,sizeof (rep)); rep.th.dest = th->source;@@ -678,19 +681,44 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb) goto out; } +#endif + +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining; +struct tcp_out_options opts; +int used; + + remaining =sizeof (rep.opt); +#ifdef CONFIG_TCP_MD5SIG +if (key) + remaining -=TCPOLEN_MD5SIG_ALIGNED ; +#endif + +memset (&opts,0 ,sizeof (opts)); + + used =tcp_extopt_response_prepare (skb,TCPHDR_RST , remaining, + &opts, sk); + + arg.iov[0 ].iov_len += used; + rep.th.doff = arg.iov[0 ].iov_len /4 ; + +tcp_extopt_response_write (&rep.opt[0 ], skb, &rep.th, &opts, sk); + offset += used /4 ; + } +#ifdef CONFIG_TCP_MD5SIG if (key) { - rep.opt[0 ] =htonl ((TCPOPT_NOP <<24 ) | - (TCPOPT_NOP <<16 ) | - (TCPOPT_MD5SIG <<8 ) | -TCPOLEN_MD5SIG ); + rep.opt[offset++] =htonl ((TCPOPT_NOP <<24 ) | + (TCPOPT_NOP <<16 ) | + (TCPOPT_MD5SIG <<8 ) | +TCPOLEN_MD5SIG );/* Update length and the length the header thinks exists */ arg.iov[0 ].iov_len +=TCPOLEN_MD5SIG_ALIGNED ; rep.th.doff = arg.iov[0 ].iov_len /4 ; -tcp_v4_md5_hash_hdr ((__u8 *) &rep.opt[1 ], - key,ip_hdr (skb)->saddr, -ip_hdr (skb)->daddr, &rep.th); +tcp_v4_md5_hash_hdr ((__u8 *)&rep.opt[offset], + key,ip_hdr (skb)->saddr, +ip_hdr (skb)->daddr, &rep.th); }#endif arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,@@ -742,14 +770,14 @@ static void tcp_v4_send_ack(const struct sock *sk,const struct tcphdr *th =tcp_hdr (skb);struct {struct tcphdr th; -__be32 opt[(TCPOLEN_TSTAMP_ALIGNED >>2 ) -#ifdef CONFIG_TCP_MD5SIG - + (TCPOLEN_MD5SIG_ALIGNED >>2 ) -#endif - ]; +__be32 opt[(MAX_TCP_OPTION_SPACE >>2 )]; } rep; +struct hlist_head *extopt_list =NULL ;struct net *net =sock_net (sk);struct ip_reply_arg arg; +int offset =0 ; + + extopt_list =tcp_extopt_get_list (sk);memset (&rep.th,0 ,sizeof (struct tcphdr ));memset (&arg,0 ,sizeof (arg));@@ -763,6 +791,7 @@ static void tcp_v4_send_ack(const struct sock *sk, rep.opt[1 ] =htonl (tsval); rep.opt[2 ] =htonl (tsecr); arg.iov[0 ].iov_len +=TCPOLEN_TSTAMP_ALIGNED ; + offset +=3 ; }/* Swap the send and the receive. */ @@ -774,22 +803,45 @@ static void tcp_v4_send_ack(const struct sock *sk, rep.th.ack =1 ; rep.th.window =htons (win); +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining; +struct tcp_out_options opts; +int used; + + remaining =sizeof (rep.th) +sizeof (rep.opt) - arg.iov[0 ].iov_len; +#ifdef CONFIG_TCP_MD5SIG -if (key) { -int offset = (tsecr) ?3 :0 ; +if (key) + remaining -=TCPOLEN_MD5SIG_ALIGNED ; +#endif + +memset (&opts,0 ,sizeof (opts)); + used =tcp_extopt_response_prepare (skb,TCPHDR_ACK , remaining, + &opts, sk); + + arg.iov[0 ].iov_len += used; + rep.th.doff = arg.iov[0 ].iov_len /4 ; + +tcp_extopt_response_write (&rep.opt[offset], skb, &rep.th, &opts, sk); + offset += used /4 ; + } + +#ifdef CONFIG_TCP_MD5SIG +if (key) { rep.opt[offset++] =htonl ((TCPOPT_NOP <<24 ) | (TCPOPT_NOP <<16 ) | (TCPOPT_MD5SIG <<8 ) |TCPOLEN_MD5SIG ); arg.iov[0 ].iov_len +=TCPOLEN_MD5SIG_ALIGNED ; - rep.th.doff = arg.iov[0 ].iov_len/4 ; + rep.th.doff = arg.iov[0 ].iov_len /4 ;tcp_v4_md5_hash_hdr ((__u8 *) &rep.opt[offset], key,ip_hdr (skb)->saddr,ip_hdr (skb)->daddr, &rep.th); }#endif + arg.flags = reply_flags; arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, ip_hdr(skb)->saddr, /* XXX */@@ -893,6 +945,9 @@ static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,*/ static void tcp_v4_reqsk_destructor (struct request_sock *req) { +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) +tcp_extopt_destroy (req_to_sk (req)); +kfree (rcu_dereference_protected (inet_rsk (req)->ireq_opt,1 )); }@@ -1387,6 +1442,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,tcp_initialize_rcv_mss (newsk); +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) { +tcp_extopt_move (req_to_sk (req), newsk); +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list); + }#ifdef CONFIG_TCP_MD5SIG /* Copy over the MD5 key from the original socket */ key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,@@ -1907,6 +1966,8 @@ void tcp_v4_destroy_sock(struct sock *sk)/* Cleans up our, hopefully empty, out_of_order_queue. */ skb_rbtree_purge (&tp->out_of_order_queue); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_destroy (sk);#ifdef CONFIG_TCP_MD5SIG /* Clean up the MD5 key list, if any */ if (tp->md5sig_info) {net/ipv4/tcp_minisocks.c +27
-5@@ -95,9 +95,10 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,struct tcp_timewait_sock *tcptw =tcp_twsk ((struct sock *)tw);bool paws_reject = false; - tmp_opt.saw_tstamp =0 ; +tcp_clear_options (&tmp_opt);if (th->doff > (sizeof (*th) >>2 ) && tcptw->tw_ts_recent_stamp) { -tcp_parse_options (twsk_net (tw), skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (twsk_net (tw), skb, &tmp_opt,0 ,NULL , + (struct sock *)tw);if (tmp_opt.saw_tstamp) {if (tmp_opt.rcv_tsecr)@@ -108,6 +109,10 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb, } } +if (unlikely (!hlist_empty (&tcptw->tcp_option_list)) && +tcp_extopt_check ((struct sock *)tw, skb, &tmp_opt)) +return TCP_TW_SUCCESS ; +if (tw->tw_substate ==TCP_FIN_WAIT2 ) {/* Just repeat all the checks of tcp_rcv_state_process() */ @@ -251,7 +256,7 @@ EXPORT_SYMBOL(tcp_timewait_state_process);void tcp_time_wait (struct sock *sk,int state,int timeo) {const struct inet_connection_sock *icsk =inet_csk (sk); -const struct tcp_sock *tp =tcp_sk (sk); +struct tcp_sock *tp =tcp_sk (sk);struct inet_timewait_sock *tw;struct inet_timewait_death_row *tcp_death_row = &sock_net (sk)->ipv4.tcp_death_row;@@ -271,6 +276,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp; tcptw->tw_ts_offset = tp->tsoffset; tcptw->tw_last_oow_ack_time =0 ; +INIT_HLIST_HEAD (&tcptw->tcp_option_list);#if IS_ENABLED (CONFIG_IPV6 )if (tw->tw_family ==PF_INET6 ) {@@ -284,6 +290,10 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) }#endif +if (unlikely (!hlist_empty (&tp->tcp_option_list))) { +tcp_extopt_move (sk, (struct sock *)tw); +INIT_HLIST_HEAD (&tp->tcp_option_list); + }#ifdef CONFIG_TCP_MD5SIG /* * The timewait bucket does not have the key DB from the @@ -341,6 +351,9 @@ void tcp_twsk_destructor(struct sock *sk)if (twsk->tw_md5_key)kfree_rcu (twsk->tw_md5_key, rcu);#endif + +if (unlikely (!hlist_empty (&twsk->tcp_option_list))) +tcp_extopt_destroy (sk); }EXPORT_SYMBOL_GPL (tcp_twsk_destructor);@@ -470,6 +483,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,INIT_LIST_HEAD (&newtp->tsq_node);INIT_LIST_HEAD (&newtp->tsorted_sent_queue); +INIT_HLIST_HEAD (&newtp->tcp_option_list);tcp_init_wl (newtp, treq->rcv_isn);@@ -545,6 +559,9 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,if (newtp->af_specific->md5_lookup (sk, newsk)) newtp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (unlikely (!hlist_empty (&treq->tcp_option_list))) + newtp->tcp_header_len +=tcp_extopt_add_header (req_to_sk (req), newsk); +if (skb->len >=TCP_MSS_DEFAULT + newtp->tcp_header_len) newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len; newtp->rx_opt.mss_clamp = req->mss;@@ -587,9 +604,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,bool paws_reject = false;bool own_req; - tmp_opt.saw_tstamp =0 ; +tcp_clear_options (&tmp_opt);if (th->doff > (sizeof (struct tcphdr )>>2 )) { -tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 ,NULL , +req_to_sk (req));if (tmp_opt.saw_tstamp) { tmp_opt.ts_recent = req->ts_recent;@@ -604,6 +622,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, } } +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list)) && +tcp_extopt_check (req_to_sk (req), skb, &tmp_opt)) +return NULL ; +/* Check for pure retransmitted SYN. */ if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn && flg == TCP_FLAG_SYN &&net/ipv4/tcp_output.c +22
-18@@ -398,13 +398,6 @@ static inline bool tcp_urg_mode(const struct tcp_sock *tp)return tp->snd_una != tp->snd_up; } -#define OPTION_SACK_ADVERTISE (1 << 0) -#define OPTION_TS (1 << 1) -#define OPTION_MD5 (1 << 2) -#define OPTION_WSCALE (1 << 3) -#define OPTION_FAST_OPEN_COOKIE (1 << 8) -#define OPTION_SMC (1 << 9) -static void smc_options_write (__be32 *ptr,u16 *options) {#if IS_ENABLED (CONFIG_SMC )@@ -420,17 +413,6 @@ static void smc_options_write(__be32 *ptr, u16 *options)#endif } -struct tcp_out_options { -u16 options;/* bit field of OPTION_* */ -u16 mss;/* 0 to disable */ -u8 ws;/* window scale, 0 to disable */ -u8 num_sack_blocks;/* number of SACK blocks to include */ -u8 hash_size;/* bytes in hash_location */ -__u8 *hash_location;/* temporary pointer, overloaded */ -__u32 tsval, tsecr;/* need to include OPTION_TS */ -struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ -}; -/* Write previously computed TCP options to the packet. * * Beware: Something in the Internet is very sensitive to the ordering of @@ -447,12 +429,15 @@ struct tcp_out_options {static void tcp_options_write (__be32 *ptr,struct sk_buff *skb,struct sock *sk,struct tcp_out_options *opts) { +struct hlist_head *extopt_list;u16 options = opts->options;/* mungable copy */ struct tcp_sock *tp =NULL ;if (sk_fullsock (sk)) tp =tcp_sk (sk); + extopt_list =tcp_extopt_get_list (sk); +if (unlikely (OPTION_MD5 & options)) { *ptr++ =htonl ((TCPOPT_NOP <<24 ) | (TCPOPT_NOP <<16 ) | (TCPOPT_MD5SIG <<8 ) |TCPOLEN_MD5SIG );@@ -543,6 +528,9 @@ static void tcp_options_write(__be32 *ptr, struct sk_buff *skb, struct sock *sk, }smc_options_write (ptr, &options); + +if (unlikely (!hlist_empty (extopt_list))) +tcp_extopt_write (ptr, skb, opts, sk); } static void smc_set_option(const struct tcp_sock *tp,@@ -645,6 +633,10 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,smc_set_option (tp, opts, &remaining); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + remaining -=tcp_extopt_prepare (skb,TCPHDR_SYN , remaining, + opts,tcp_to_sk (tp)); +return MAX_TCP_OPTION_SPACE - remaining; }@@ -708,6 +700,11 @@ static unsigned int tcp_synack_options(const struct sock *sk,smc_set_option_cond (tcp_sk (sk), ireq, opts, &remaining); +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) + remaining -=tcp_extopt_prepare (skb,TCPHDR_SYN |TCPHDR_ACK , + remaining, opts, +req_to_sk (req)); +return MAX_TCP_OPTION_SPACE - remaining; }@@ -741,6 +738,10 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb size +=TCPOLEN_TSTAMP_ALIGNED ; } +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + size +=tcp_extopt_prepare (skb,0 ,MAX_TCP_OPTION_SPACE - size, + opts,tcp_to_sk (tp)); + eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;if (unlikely (eff_sacks)) {const unsigned int remaining =MAX_TCP_OPTION_SPACE - size;@@ -3303,6 +3304,9 @@ static void tcp_connect_init(struct sock *sk) tp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + tp->tcp_header_len +=tcp_extopt_add_header (sk, sk); +/* If user gave his TCP_MAXSEG, record it to clamp */ if (tp->rx_opt.user_mss) tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;net/ipv6/syncookies.c +5
-1@@ -162,7 +162,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL , sk);if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { tsoff = secure_tcpv6_ts_off(sock_net(sk),@@ -174,6 +174,10 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) goto out; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tcp_opt)) + goto out; + ret =NULL ; req =inet_reqsk_alloc (&tcp6_request_sock_ops, sk, false);if (!req)net/ipv6/tcp_ipv6.c +32
-0@@ -500,6 +500,9 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,static void tcp_v6_reqsk_destructor (struct request_sock *req) { +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) +tcp_extopt_destroy (req_to_sk (req)); +kfree (inet_rsk (req)->ipv6_opt);kfree_skb (inet_rsk (req)->pktopts); }@@ -789,6 +792,8 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32unsigned int tot_len =sizeof (struct tcphdr );struct dst_entry *dst;__be32 *topt; +struct hlist_head *extopt_list =NULL ; +struct tcp_out_options extraopts;if (tsecr) tot_len +=TCPOLEN_TSTAMP_ALIGNED ;@@ -797,6 +802,25 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 tot_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (sk) + extopt_list =tcp_extopt_get_list (sk); + +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining =MAX_TCP_OPTION_SPACE - tot_len; +u8 extraflags = rst ?TCPHDR_RST :0 ; +int used; + +if (!rst || !th->ack) + extraflags |=TCPHDR_ACK ; + +memset (&extraopts,0 ,sizeof (extraopts)); + + used =tcp_extopt_response_prepare (skb, extraflags, remaining, + &extraopts, sk); + + tot_len += used; + } + buff =alloc_skb (MAX_HEADER +sizeof (struct ipv6hdr ) + tot_len,GFP_ATOMIC );if (!buff)@@ -837,6 +861,9 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 }#endif +if (unlikely (extopt_list && !hlist_empty (extopt_list))) +tcp_extopt_response_write (topt, skb, t1, &extraopts, sk); +memset (&fl6,0 ,sizeof (fl6)); fl6.daddr =ipv6_hdr (skb)->saddr; fl6.saddr =ipv6_hdr (skb)->daddr;@@ -1196,6 +1223,11 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * newinet->inet_daddr = newinet->inet_saddr =LOOPBACK4_IPV6 ; newinet->inet_rcv_saddr =LOOPBACK4_IPV6 ; +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) { +tcp_extopt_move (req_to_sk (req), newsk); +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list); + } +#ifdef CONFIG_TCP_MD5SIG /* Copy over the MD5 key from the original socket */ key =tcp_v6_md5_do_lookup (sk, &newsk->sk_v6_daddr);Signature · 1 line
2.15.0
--
Mat Martineau
Intel OTC
Christoph Paasch
Re: [MPTCP] [RFC final 05/14] tcp: Register handlers for extra TCP options
Quoted 27 lines · Mat Martineau
Christoph,
Some comment/commit-message notes below.
Quoted 19 lines · Christoph Paasch
From: Mat Martineau <mathew.j.martineau(a)linux.intel.com>
Allow additional TCP options to be handled by registered hook functions.
Registered options have a priority that determines the order in which options are prepared and written. Lower priority numbers are handled first.
Option parsing will call the provided 'parse' function when a TCP option number is not recognized by the normal option parsing code.
After parsing, there are two places where we post-process the options. First, a 'check' callback that allows to drop the packet based on the parsed options (e.g., usefull for TCP MD5SIG). Then, a '
post_process' function that gets called after other validity checks (aka, in-window, PAWS,...). Thispost_processfunction can then update other state for this particular extra-option.
Quoted 7 lines
Quoted 5 lines
In the output-path, the 'prepare' function determines the required space for registered options and store associated data. 'write' adds the option to the TCP header.
A static key is used to minimize the performance impact.
This line should be deleted now that the static branching was removed.
Good catch, updating it.
Quoted 179 lines
Quoted 179 lines
These additional TCP-options are stored in hlists of the TCP-socket. To pass the state and options around during the 3-way handshake and in time-wait state, the hlists are also on the
tcp_request_sockandtcp_timewait_sock.
The list is copied from the listener to the request-socket (calling into the 'copy' callback). Then, moved from the request-socket to the TCP-socket and finally to the time-wait socket.Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>Signed-off-by Christoph Paasch <cpaasch(a)apple.com>---
drivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/tcp.h | 28 ++++ include/net/tcp.h | 109 ++++++++++++- net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 327 ++++++++++++++++++++++++++++++++++++++- net/ipv4/tcp_input.c | 49 +++++- net/ipv4/tcp_ipv4.c | 97 +++++++++--- net/ipv4/tcp_minisocks.c | 32 +++- net/ipv4/tcp_output.c | 40 ++--- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 32 ++++ 11 files changed, 674 insertions(+), 54 deletions(-)drivers/infiniband/hw/cxgb4/cm.c +1
-1@@ -3746,7 +3746,7 @@ static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)*/ memset (&tmp_opt,0 ,sizeof (tmp_opt));tcp_clear_options (&tmp_opt); -tcp_parse_options (&init_net, skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (&init_net, skb, &tmp_opt,0 ,NULL ,NULL ); req =__skb_push (skb,sizeof (*req));memset (req,0 ,sizeof (*req));include/linux/tcp.h +28
-0@@ -115,6 +115,24 @@ static inline void tcp_clear_options(struct tcp_options_received *rx_opt)#endif } +#define OPTION_SACK_ADVERTISE (1 << 0) +#define OPTION_TS (1 << 1) +#define OPTION_MD5 (1 << 2) +#define OPTION_WSCALE (1 << 3) +#define OPTION_FAST_OPEN_COOKIE (1 << 8) +#define OPTION_SMC (1 << 9) + +struct tcp_out_options { +u16 options;/* bit field of OPTION_* */ +u16 mss;/* 0 to disable */ +u8 ws;/* window scale, 0 to disable */ +u8 num_sack_blocks;/* number of SACK blocks to include */ +u8 hash_size;/* bytes in hash_location */ +__u8 *hash_location;/* temporary pointer, overloaded */ +__u32 tsval, tsecr;/* need to include OPTION_TS */ +struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ +}; +/* This is the max number of SACKS that we'll generate and process. It's safe * to increase this, although since: * size = TCPOLEN_SACK_BASE_ALIGNED (4) + n * TCPOLEN_SACK_PERBLOCK (8) @@ -137,6 +155,7 @@ struct tcp_request_sock {* FastOpen it's the seq# * after data-in-SYN. */ +struct hlist_head tcp_option_list; };static inline struct tcp_request_sock *tcp_rsk (const struct request_sock *req)@@ -373,6 +392,8 @@ struct tcp_sock {*/ struct request_sock *fastopen_rsk;u32 *saved_syn; + +struct hlist_head tcp_option_list; };enum tsq_enum {@@ -400,6 +421,11 @@ static inline struct tcp_sock *tcp_sk(const struct sock *sk)return (struct tcp_sock *)sk; } +static inline struct sock *tcp_to_sk (const struct tcp_sock *tp) +{ +return (struct sock *)tp; +} +struct tcp_timewait_sock {struct inet_timewait_sock tw_sk;#define tw_rcv_nxt tw_sk.__tw_common.skc_tw_rcv_nxt@@ -412,6 +438,8 @@ struct tcp_timewait_sock {u32 tw_last_oow_ack_time;long tw_ts_recent_stamp; + +struct hlist_head tcp_option_list;#ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *tw_md5_key;#endif include/net/tcp.h +50
-1@@ -202,6 +202,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo);#define TCPOLEN_FASTOPEN_BASE 2#define TCPOLEN_EXP_FASTOPEN_BASE 4#define TCPOLEN_EXP_SMC_BASE 6 +#define TCPOLEN_EXP_BASE 6/* But this is what stacks really send out. */ #define TCPOLEN_TSTAMP_ALIGNED 12@@ -403,7 +404,8 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len);void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx, -int estab,struct tcp_fastopen_cookie *foc); +int estab,struct tcp_fastopen_cookie *foc, +struct sock *sk);const u8 *tcp_parse_md5sig_option (const struct tcphdr *th);/* @@ -2063,4 +2065,109 @@ static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)#if IS_ENABLED (CONFIG_SMC )extern struct static_key_false tcp_have_smc;#endif + +struct tcp_extopt_store ; + +struct tcp_extopt_ops { +u32 option_kind; +unsigned char priority; +void (*parse)(int opsize,const unsigned char *opptr, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx, +struct sock *sk, +struct tcp_extopt_store *store); +bool (*check)(struct sock *sk, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx, +struct tcp_extopt_store *store); +void (*post_process)(struct sock *sk, +struct tcp_options_received *opt_rx, +struct tcp_extopt_store *store); +/* Return the number of bytes consumed */ +unsigned int (*prepare)(struct sk_buff *skb,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk, +struct tcp_extopt_store *store); +__be32 *(*write)(__be32 *ptr,struct sk_buff *skb, +struct tcp_out_options *opts,struct sock *sk, +struct tcp_extopt_store *store); +int (*response_prepare)(struct sk_buff *orig,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk, +struct tcp_extopt_store *store); +__be32 *(*response_write)(__be32 *ptr,struct sk_buff *orig, +struct tcphdr *th, +struct tcp_out_options *opts, +const struct sock *sk, +struct tcp_extopt_store *store); +int (*add_header_len)(const struct sock *orig, +const struct sock *sk, +struct tcp_extopt_store *store); +struct tcp_extopt_store *(*copy)(struct sock *listener, +struct request_sock *req, +struct tcp_options_received *opt, +struct tcp_extopt_store *from); +struct tcp_extopt_store *(*move)(struct sock *from,struct sock *to, +struct tcp_extopt_store *store); +void (*destroy)(struct tcp_extopt_store *store);
Quoted 8 lines
include/net/tcp.h static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk) · line 2119
Quoted 5 lines
include/net/tcp.h +5
-0+struct module *owner; +}; + +/* Every extra-option in a TCP-socket gets stored in a _store structure which +* has to have tcp_extopt_store as the first element. Comment can be updated to recommend
container_of()instead of positional requirement.
Updated it as well.
Quoted 364 lines
Quoted 364 lines
include/net/tcp.h +53
-0+* +* That way, code registering extra options can pass this to +* tcp_register_extopt, and the surrounding structure can contain per-option +* state. +*/ +struct tcp_extopt_store { +struct hlist_node list; +const struct tcp_extopt_ops *ops; +}; + +struct hlist_head *tcp_extopt_get_list (const struct sock *sk); + +struct tcp_extopt_store *tcp_extopt_find_kind (u32 kind,const struct sock *sk); + +void tcp_extopt_parse (u32 opcode,int opsize,const unsigned char *opptr, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx,struct sock *sk); + +bool tcp_extopt_check (struct sock *sk,const struct sk_buff *skb, +struct tcp_options_received *opt_rx); + +void tcp_extopt_post_process (struct sock *sk, +struct tcp_options_received *opt_rx); + +unsigned int tcp_extopt_prepare (struct sk_buff *skb,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk); + +void tcp_extopt_write (__be32 *ptr,struct sk_buff *skb, +struct tcp_out_options *opts,struct sock *sk); + +int tcp_extopt_response_prepare (struct sk_buff *orig,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk); + +void tcp_extopt_response_write (__be32 *ptr,struct sk_buff *orig, +struct tcphdr *th,struct tcp_out_options *opts, +const struct sock *sk); + +int tcp_extopt_add_header (const struct sock *orig,const struct sock *sk); + +/* Socket lock must be held when calling this function */ +int tcp_register_extopt (struct tcp_extopt_store *store,struct sock *sk); + +void tcp_extopt_copy (struct sock *listener,struct request_sock *req, +struct tcp_options_received *opt); + +void tcp_extopt_move (struct sock *from,struct sock *to); + +void tcp_extopt_destroy (struct sock *sk); +#endif /* _TCP_H */ net/ipv4/syncookies.c +5
-1@@ -313,7 +313,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL , sk);if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { tsoff = secure_tcp_ts_off(sock_net(sk),@@ -325,6 +325,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) goto out; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tcp_opt)) + goto out; + ret =NULL ; req =inet_reqsk_alloc (&tcp_request_sock_ops, sk, false);/* for safety */ if (!req)net/ipv4/tcp.c +270
-0@@ -416,6 +416,7 @@ void tcp_init_sock(struct sock *sk)tcp_init_xmit_timers (sk);INIT_LIST_HEAD (&tp->tsq_node);INIT_LIST_HEAD (&tp->tsorted_sent_queue); +INIT_HLIST_HEAD (&tp->tcp_option_list); icsk->icsk_rto =TCP_TIMEOUT_INIT ; tp->mdev_us =jiffies_to_usecs (TCP_TIMEOUT_INIT );@@ -3473,6 +3474,331 @@ EXPORT_SYMBOL(tcp_md5_hash_key);#endif +struct hlist_head *tcp_extopt_get_list (const struct sock *sk) +{ +if (sk_fullsock (sk)) +return &tcp_sk (sk)->tcp_option_list; +else if (sk->sk_state ==TCP_NEW_SYN_RECV ) +return &tcp_rsk (inet_reqsk (sk))->tcp_option_list; +else if (sk->sk_state ==TCP_TIME_WAIT ) +return &tcp_twsk (sk)->tcp_option_list; + +return NULL ; +} +EXPORT_SYMBOL_GPL (tcp_extopt_get_list); + +/* Caller must ensure that rcu is locked */ +struct tcp_extopt_store *tcp_extopt_find_kind (u32 kind,const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + + lhead =tcp_extopt_get_list (sk); + +hlist_for_each_entry_rcu (entry, lhead, list) { +if (entry->ops->option_kind == kind) +return entry; + } + +return NULL ; +} +EXPORT_SYMBOL_GPL (tcp_extopt_find_kind); + +void tcp_extopt_parse (u32 opcode,int opsize,const unsigned char *opptr, +const struct sk_buff *skb, +struct tcp_options_received *opt_rx,struct sock *sk) +{ +struct tcp_extopt_store *entry; + +rcu_read_lock (); + entry =tcp_extopt_find_kind (opcode, sk); + +if (entry && entry->ops->parse) + entry->ops->parse (opsize, opptr, skb, opt_rx, sk, entry); +rcu_read_unlock (); +} + +bool tcp_extopt_check (struct sock *sk,const struct sk_buff *skb, +struct tcp_options_received *opt_rx) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +bool drop = false; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +bool ret = false; + +if (entry->ops->check) + ret = entry->ops->check (sk, skb, opt_rx, entry); + +if (ret) + drop = true; + } +rcu_read_unlock (); + +return drop; +} +EXPORT_SYMBOL_GPL (tcp_extopt_check); + +void tcp_extopt_post_process (struct sock *sk, +struct tcp_options_received *opt_rx) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (entry->ops->post_process) + entry->ops->post_process (sk, opt_rx, entry); + } +rcu_read_unlock (); +} + +unsigned int tcp_extopt_prepare (struct sk_buff *skb,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +unsigned int used =0 ; + +if (!sk) +return 0 ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->prepare)) +continue ; + + used += entry->ops->prepare (skb, flags, remaining - used, opts, + sk, entry); + } +rcu_read_unlock (); + +return roundup (used,4 ); +} + +void tcp_extopt_write (__be32 *ptr,struct sk_buff *skb, +struct tcp_out_options *opts,struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + +if (!sk) +return ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->write)) +continue ; + + ptr = entry->ops->write (ptr, skb, opts, sk, entry); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_write); + +int tcp_extopt_response_prepare (struct sk_buff *orig,u8 flags, +unsigned int remaining, +struct tcp_out_options *opts, +const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +unsigned int used =0 ; + +if (!sk) +return 0 ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +int ret; + +if (unlikely (!entry->ops->response_prepare)) +continue ; + + ret = entry->ops->response_prepare (orig, flags, + remaining - used, opts, + sk, entry); + + used += ret; + } +rcu_read_unlock (); + +return roundup (used,4 ); +} +EXPORT_SYMBOL_GPL (tcp_extopt_response_prepare); + +void tcp_extopt_response_write (__be32 *ptr,struct sk_buff *orig, +struct tcphdr *th,struct tcp_out_options *opts, +const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; + +if (!sk) +return ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->response_write)) +continue ; + + ptr = entry->ops->response_write (ptr, orig, th, opts, sk, entry); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_response_write); + +int tcp_extopt_add_header (const struct sock *orig,const struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +int tcp_header_len =0 ; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, lhead, list) { +if (unlikely (!entry->ops->add_header_len)) +continue ; + + tcp_header_len += entry->ops->add_header_len (orig, sk, entry); + } +rcu_read_unlock (); + +return tcp_header_len; +} + +/* Socket lock must be held when calling this function */ +int tcp_register_extopt (struct tcp_extopt_store *store,struct sock *sk) +{ +struct hlist_node *add_before =NULL ; +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +int ret =0 ; + + lhead =tcp_extopt_get_list (sk); + +if (!store->ops->option_kind) +return -EINVAL ; + +if (!try_module_get (store->ops->owner)) +return -ENOENT ; + +hlist_for_each_entry_rcu (entry, lhead, list) { +if (entry->ops->option_kind == store->ops->option_kind) { +pr_notice ("Option kind %u already registered\n" , + store->ops->option_kind); +module_put (store->ops->owner); +return -EEXIST ; + } + +if (entry->ops->priority <= store->ops->priority) + add_before = &entry->list; + } + +if (add_before) +hlist_add_behind_rcu (&store->list, add_before); +else +hlist_add_head_rcu (&store->list, lhead); + +pr_debug ("Option kind %u registered\n" , store->ops->option_kind); + +return ret; +} +EXPORT_SYMBOL_GPL (tcp_register_extopt); + +void tcp_extopt_copy (struct sock *listener,struct request_sock *req, +struct tcp_options_received *opt) +{ +struct tcp_extopt_store *entry; +struct hlist_head *from, *to; + + from =tcp_extopt_get_list (listener); + to =tcp_extopt_get_list (req_to_sk (req)); + +rcu_read_lock (); +hlist_for_each_entry_rcu (entry, from, list) { +struct tcp_extopt_store *new; + +if (!try_module_get (entry->ops->owner)) { +pr_err ("%s Module get failed while copying\n" , __func__); +continue ; + } + + new = entry->ops->copy (listener, req, opt, entry); +if (!new) {
Quoted 8 lines
net/ipv4/tcp.c EXPORT_SYMBOL(tcp_md5_hash_key); · line 3750
Quoted 5 lines
net/ipv4/tcp.c +5
-0+module_put (entry->ops->owner); +continue ; + } + +hlist_add_tail_rcu (&new->list, to);Does this require that req is locked? Or is it expected to be under construction at this point so it won't be accessed in parallel?
It's the latter, because this is during construction so no parallel access possible.
Also, request_socks don't have locks.
Quoted 8 lines
net/ipv4/tcp.c EXPORT_SYMBOL(tcp_md5_hash_key); · line 3755
Quoted 6 lines
net/ipv4/tcp.c +5
-0+ } +rcu_read_unlock (); +} + +void tcp_extopt_move (struct sock *from,struct sock *to)Any locking notes for this function?
Oh, I see an issue here. tcp_check_req can be called in parallel on the same request-socket.
It is even possible that we create the "same" child and later on destroy one of them (see inet_csk_complete_hashdance()).
The conflict gets detected in inet_ehash_nolisten() and I need to move the call to tcp_extopt_move to the same place as tcp_move_syn().
Christoph
Quoted 707 lines
Mat
Quoted 699 lines
net/ipv4/tcp.c +46
-1+{ +struct tcp_extopt_store *entry; +struct hlist_head *lfrom, *lto; +struct hlist_node *tmp; + + lfrom =tcp_extopt_get_list (from); + lto =tcp_extopt_get_list (to); + +rcu_read_lock (); +hlist_for_each_entry_safe (entry, tmp, lfrom, list) { +hlist_del_rcu (&entry->list); + +if (entry->ops->move) { + entry = entry->ops->move (from, to, entry); +if (!entry) +continue ; + } + +hlist_add_tail_rcu (&entry->list, lto); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_move); + +void tcp_extopt_destroy (struct sock *sk) +{ +struct tcp_extopt_store *entry; +struct hlist_head *lhead; +struct hlist_node *tmp; + + lhead =tcp_extopt_get_list (sk); + +rcu_read_lock (); +hlist_for_each_entry_safe (entry, tmp, lhead, list) { +struct module *owner = entry->ops->owner; + +hlist_del_rcu (&entry->list); + + entry->ops->destroy (entry); + +module_put (owner); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (tcp_extopt_destroy); +void tcp_done (struct sock *sk) {struct request_sock *req =tcp_sk (sk)->fastopen_rsk;@@ -3622,7 +3948,6 @@ void __init tcp_init(void)INIT_HLIST_HEAD (&tcp_hashinfo.bhash[i].chain); } - cnt = tcp_hashinfo.ehash_mask +1 ; sysctl_tcp_max_orphans = cnt /2 ;net/ipv4/tcp_input.c +14
-5@@ -3696,7 +3696,7 @@ static int smc_parse_options(const struct tcphdr *th,void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx,int estab, -struct tcp_fastopen_cookie *foc) +struct tcp_fastopen_cookie *foc,struct sock *sk) {const unsigned char *ptr;const struct tcphdr *th =tcp_hdr (skb);@@ -3796,9 +3796,18 @@ void tcp_parse_options(const struct net *net,tcp_parse_fastopen_option (opsize -TCPOLEN_EXP_FASTOPEN_BASE , ptr +2 , th->syn, foc, true); - else -smc_parse_options (th ,opt_rx ,ptr , -opsize ); + else if (smc_parse_options(th, opt_rx, ptr, + opsize)) + break; + else if (opsize >= TCPOLEN_EXP_BASE) + tcp_extopt_parse(get_unaligned_be32(ptr), + opsize, ptr, skb, + opt_rx, sk); +break ; + +default : +tcp_extopt_parse (opcode, opsize, ptr, skb, + opt_rx, sk);break ; }@@ -3849,11 +3858,13 @@ static bool tcp_fast_parse_options(const struct net *net, goto extra_opt_check; } -tcp_parse_options (net, skb, &tp->rx_opt,1 ,NULL ); +tcp_parse_options (net, skb, &tp->rx_opt,1 ,NULL ,tcp_to_sk (tp));if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) tp->rx_opt.rcv_tsecr -= tp->tsoffset;
extra_opt_check:net/ipv4/tcp_input.c +2
-0+if (unlikely (!hlist_empty (&tp->tcp_option_list))) +return tcp_extopt_check (tcp_to_sk (tp), skb, &tp->rx_opt);return false; }net/ipv4/tcp_input.c +25
-3@@ -5327,6 +5338,9 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, tp->rx_opt.saw_tstamp =0 ; +if (!hlist_empty (&tp->tcp_option_list)) + goto slow_path; +/* pred_flags is 0xS?10 << 16 + snd_wnd * if header_prediction is to be made * 'S' will always be tp->tcp_header_len >> 2 @@ -5514,7 +5528,7 @@ static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,/* Get original SYNACK MSS value if user MSS sets mss_clamp */ tcp_clear_options (&opt); opt.user_mss = opt.mss_clamp =0 ; -tcp_parse_options (sock_net (sk), synack, &opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), synack, &opt,0 ,NULL , sk); mss = opt.mss_clamp; }@@ -5577,10 +5591,14 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,int saved_clamp = tp->rx_opt.mss_clamp;bool fastopen_fail; -tcp_parse_options (sock_net (sk), skb, &tp->rx_opt,0 , &foc); +tcp_parse_options (sock_net (sk), skb, &tp->rx_opt,0 , &foc, sk);if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) tp->rx_opt.rcv_tsecr -= tp->tsoffset; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tp->rx_opt)) + goto discard; +if (th->ack) {/* rfc793: * "If the state is SYN-SENT then @@ -5663,6 +5681,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, tp->tcp_header_len =sizeof (struct tcphdr ); } +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_post_process (sk, &tp->rx_opt); +tcp_sync_mss (sk, icsk->icsk_pmtu_cookie);tcp_initialize_rcv_mss (sk);@@ -5756,6 +5777,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,tcp_ecn_rcv_syn (tp, th); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_post_process (sk, &tp->rx_opt); +tcp_mtup_init (sk);tcp_sync_mss (sk, icsk->icsk_pmtu_cookie);tcp_initialize_rcv_mss (sk);@@ -6239,12 +6263,17 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,tcp_rsk (req)->af_specific = af_ops;tcp_rsk (req)->ts_off =0 ; +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list);tcp_clear_options (&tmp_opt); tmp_opt.mss_clamp = af_ops->mss_clamp; tmp_opt.user_mss = tp->rx_opt.user_mss;tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 , - want_cookie ?NULL : &foc); + want_cookie ?NULL : &foc, sk); + +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tmp_opt)) + goto drop_and_free;if (want_cookie && !tmp_opt.saw_tstamp)tcp_clear_options (&tmp_opt);@@ -6305,6 +6334,10 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,tcp_reqsk_record_syn (sk, req, skb); fastopen_sk =tcp_try_fastopen (sk, skb, req, &foc, dst); } + +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_copy (sk, req, &tmp_opt); +if (fastopen_sk) { af_ops->send_synack (fastopen_sk, dst, &fl, req, &foc,TCP_SYNACK_FASTOPEN );net/ipv4/tcp_ipv4.c +79
-18@@ -600,10 +600,9 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)const struct tcphdr *th =tcp_hdr (skb);struct {struct tcphdr th; -#ifdef CONFIG_TCP_MD5SIG -__be32 opt[(TCPOLEN_MD5SIG_ALIGNED >>2 )]; -#endif +__be32 opt[(MAX_TCP_OPTION_SPACE >>2 )]; } rep; +struct hlist_head *extopt_list =NULL ;struct ip_reply_arg arg;#ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *key =NULL ;@@ -613,6 +612,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)struct sock *sk1 =NULL ;#endif struct net *net; +int offset =0 ;/* Never send a reset in response to a reset. */ if (th->rst)@@ -624,6 +624,9 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)if (!sk &&skb_rtable (skb)->rt_type !=RTN_LOCAL )return ; +if (sk) + extopt_list =tcp_extopt_get_list (sk); +/* Swap the send and the receive. */ memset (&rep,0 ,sizeof (rep)); rep.th.dest = th->source;@@ -678,19 +681,44 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb) goto out; } +#endif + +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining; +struct tcp_out_options opts; +int used; + + remaining =sizeof (rep.opt); +#ifdef CONFIG_TCP_MD5SIG +if (key) + remaining -=TCPOLEN_MD5SIG_ALIGNED ; +#endif + +memset (&opts,0 ,sizeof (opts)); + + used =tcp_extopt_response_prepare (skb,TCPHDR_RST , remaining, + &opts, sk); + + arg.iov[0 ].iov_len += used; + rep.th.doff = arg.iov[0 ].iov_len /4 ; + +tcp_extopt_response_write (&rep.opt[0 ], skb, &rep.th, &opts, sk); + offset += used /4 ; + } +#ifdef CONFIG_TCP_MD5SIG if (key) { - rep.opt[0 ] =htonl ((TCPOPT_NOP <<24 ) | - (TCPOPT_NOP <<16 ) | - (TCPOPT_MD5SIG <<8 ) | -TCPOLEN_MD5SIG ); + rep.opt[offset++] =htonl ((TCPOPT_NOP <<24 ) | + (TCPOPT_NOP <<16 ) | + (TCPOPT_MD5SIG <<8 ) | +TCPOLEN_MD5SIG );/* Update length and the length the header thinks exists */ arg.iov[0 ].iov_len +=TCPOLEN_MD5SIG_ALIGNED ; rep.th.doff = arg.iov[0 ].iov_len /4 ; -tcp_v4_md5_hash_hdr ((__u8 *) &rep.opt[1 ], - key,ip_hdr (skb)->saddr, -ip_hdr (skb)->daddr, &rep.th); +tcp_v4_md5_hash_hdr ((__u8 *)&rep.opt[offset], + key,ip_hdr (skb)->saddr, +ip_hdr (skb)->daddr, &rep.th); }#endif arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,@@ -742,14 +770,14 @@ static void tcp_v4_send_ack(const struct sock *sk,const struct tcphdr *th =tcp_hdr (skb);struct {struct tcphdr th; -__be32 opt[(TCPOLEN_TSTAMP_ALIGNED >>2 ) -#ifdef CONFIG_TCP_MD5SIG - + (TCPOLEN_MD5SIG_ALIGNED >>2 ) -#endif - ]; +__be32 opt[(MAX_TCP_OPTION_SPACE >>2 )]; } rep; +struct hlist_head *extopt_list =NULL ;struct net *net =sock_net (sk);struct ip_reply_arg arg; +int offset =0 ; + + extopt_list =tcp_extopt_get_list (sk);memset (&rep.th,0 ,sizeof (struct tcphdr ));memset (&arg,0 ,sizeof (arg));@@ -763,6 +791,7 @@ static void tcp_v4_send_ack(const struct sock *sk, rep.opt[1 ] =htonl (tsval); rep.opt[2 ] =htonl (tsecr); arg.iov[0 ].iov_len +=TCPOLEN_TSTAMP_ALIGNED ; + offset +=3 ; }/* Swap the send and the receive. */ @@ -774,22 +803,45 @@ static void tcp_v4_send_ack(const struct sock *sk, rep.th.ack =1 ; rep.th.window =htons (win); +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining; +struct tcp_out_options opts; +int used; + + remaining =sizeof (rep.th) +sizeof (rep.opt) - arg.iov[0 ].iov_len; +#ifdef CONFIG_TCP_MD5SIG -if (key) { -int offset = (tsecr) ?3 :0 ; +if (key) + remaining -=TCPOLEN_MD5SIG_ALIGNED ; +#endif + +memset (&opts,0 ,sizeof (opts)); + used =tcp_extopt_response_prepare (skb,TCPHDR_ACK , remaining, + &opts, sk); + + arg.iov[0 ].iov_len += used; + rep.th.doff = arg.iov[0 ].iov_len /4 ; + +tcp_extopt_response_write (&rep.opt[offset], skb, &rep.th, &opts, sk); + offset += used /4 ; + } + +#ifdef CONFIG_TCP_MD5SIG +if (key) { rep.opt[offset++] =htonl ((TCPOPT_NOP <<24 ) | (TCPOPT_NOP <<16 ) | (TCPOPT_MD5SIG <<8 ) |TCPOLEN_MD5SIG ); arg.iov[0 ].iov_len +=TCPOLEN_MD5SIG_ALIGNED ; - rep.th.doff = arg.iov[0 ].iov_len/4 ; + rep.th.doff = arg.iov[0 ].iov_len /4 ;tcp_v4_md5_hash_hdr ((__u8 *) &rep.opt[offset], key,ip_hdr (skb)->saddr,ip_hdr (skb)->daddr, &rep.th); }#endif + arg.flags = reply_flags; arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, ip_hdr(skb)->saddr, /* XXX */@@ -893,6 +945,9 @@ static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,*/ static void tcp_v4_reqsk_destructor (struct request_sock *req) { +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) +tcp_extopt_destroy (req_to_sk (req)); +kfree (rcu_dereference_protected (inet_rsk (req)->ireq_opt,1 )); }@@ -1387,6 +1442,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,tcp_initialize_rcv_mss (newsk); +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) { +tcp_extopt_move (req_to_sk (req), newsk); +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list); + }#ifdef CONFIG_TCP_MD5SIG /* Copy over the MD5 key from the original socket */ key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,@@ -1907,6 +1966,8 @@ void tcp_v4_destroy_sock(struct sock *sk)/* Cleans up our, hopefully empty, out_of_order_queue. */ skb_rbtree_purge (&tp->out_of_order_queue); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) +tcp_extopt_destroy (sk);#ifdef CONFIG_TCP_MD5SIG /* Clean up the MD5 key list, if any */ if (tp->md5sig_info) {net/ipv4/tcp_minisocks.c +27
-5@@ -95,9 +95,10 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,struct tcp_timewait_sock *tcptw =tcp_twsk ((struct sock *)tw);bool paws_reject = false; - tmp_opt.saw_tstamp =0 ; +tcp_clear_options (&tmp_opt);if (th->doff > (sizeof (*th) >>2 ) && tcptw->tw_ts_recent_stamp) { -tcp_parse_options (twsk_net (tw), skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (twsk_net (tw), skb, &tmp_opt,0 ,NULL , + (struct sock *)tw);if (tmp_opt.saw_tstamp) {if (tmp_opt.rcv_tsecr)@@ -108,6 +109,10 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb, } } +if (unlikely (!hlist_empty (&tcptw->tcp_option_list)) && +tcp_extopt_check ((struct sock *)tw, skb, &tmp_opt)) +return TCP_TW_SUCCESS ; +if (tw->tw_substate ==TCP_FIN_WAIT2 ) {/* Just repeat all the checks of tcp_rcv_state_process() */ @@ -251,7 +256,7 @@ EXPORT_SYMBOL(tcp_timewait_state_process);void tcp_time_wait (struct sock *sk,int state,int timeo) {const struct inet_connection_sock *icsk =inet_csk (sk); -const struct tcp_sock *tp =tcp_sk (sk); +struct tcp_sock *tp =tcp_sk (sk);struct inet_timewait_sock *tw;struct inet_timewait_death_row *tcp_death_row = &sock_net (sk)->ipv4.tcp_death_row;@@ -271,6 +276,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp; tcptw->tw_ts_offset = tp->tsoffset; tcptw->tw_last_oow_ack_time =0 ; +INIT_HLIST_HEAD (&tcptw->tcp_option_list);#if IS_ENABLED (CONFIG_IPV6 )if (tw->tw_family ==PF_INET6 ) {@@ -284,6 +290,10 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) }#endif +if (unlikely (!hlist_empty (&tp->tcp_option_list))) { +tcp_extopt_move (sk, (struct sock *)tw); +INIT_HLIST_HEAD (&tp->tcp_option_list); + }#ifdef CONFIG_TCP_MD5SIG /* * The timewait bucket does not have the key DB from the @@ -341,6 +351,9 @@ void tcp_twsk_destructor(struct sock *sk)if (twsk->tw_md5_key)kfree_rcu (twsk->tw_md5_key, rcu);#endif + +if (unlikely (!hlist_empty (&twsk->tcp_option_list))) +tcp_extopt_destroy (sk); }EXPORT_SYMBOL_GPL (tcp_twsk_destructor);@@ -470,6 +483,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,INIT_LIST_HEAD (&newtp->tsq_node);INIT_LIST_HEAD (&newtp->tsorted_sent_queue); +INIT_HLIST_HEAD (&newtp->tcp_option_list);tcp_init_wl (newtp, treq->rcv_isn);@@ -545,6 +559,9 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,if (newtp->af_specific->md5_lookup (sk, newsk)) newtp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (unlikely (!hlist_empty (&treq->tcp_option_list))) + newtp->tcp_header_len +=tcp_extopt_add_header (req_to_sk (req), newsk); +if (skb->len >=TCP_MSS_DEFAULT + newtp->tcp_header_len) newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len; newtp->rx_opt.mss_clamp = req->mss;@@ -587,9 +604,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,bool paws_reject = false;bool own_req; - tmp_opt.saw_tstamp =0 ; +tcp_clear_options (&tmp_opt);if (th->doff > (sizeof (struct tcphdr )>>2 )) { -tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tmp_opt,0 ,NULL , +req_to_sk (req));if (tmp_opt.saw_tstamp) { tmp_opt.ts_recent = req->ts_recent;@@ -604,6 +622,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, } } +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list)) && +tcp_extopt_check (req_to_sk (req), skb, &tmp_opt)) +return NULL ; +/* Check for pure retransmitted SYN. */ if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn && flg == TCP_FLAG_SYN &&net/ipv4/tcp_output.c +22
-18@@ -398,13 +398,6 @@ static inline bool tcp_urg_mode(const struct tcp_sock *tp)return tp->snd_una != tp->snd_up; } -#define OPTION_SACK_ADVERTISE (1 << 0) -#define OPTION_TS (1 << 1) -#define OPTION_MD5 (1 << 2) -#define OPTION_WSCALE (1 << 3) -#define OPTION_FAST_OPEN_COOKIE (1 << 8) -#define OPTION_SMC (1 << 9) -static void smc_options_write (__be32 *ptr,u16 *options) {#if IS_ENABLED (CONFIG_SMC )@@ -420,17 +413,6 @@ static void smc_options_write(__be32 *ptr, u16 *options)#endif } -struct tcp_out_options { -u16 options;/* bit field of OPTION_* */ -u16 mss;/* 0 to disable */ -u8 ws;/* window scale, 0 to disable */ -u8 num_sack_blocks;/* number of SACK blocks to include */ -u8 hash_size;/* bytes in hash_location */ -__u8 *hash_location;/* temporary pointer, overloaded */ -__u32 tsval, tsecr;/* need to include OPTION_TS */ -struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ -}; -/* Write previously computed TCP options to the packet. * * Beware: Something in the Internet is very sensitive to the ordering of @@ -447,12 +429,15 @@ struct tcp_out_options {static void tcp_options_write (__be32 *ptr,struct sk_buff *skb,struct sock *sk,struct tcp_out_options *opts) { +struct hlist_head *extopt_list;u16 options = opts->options;/* mungable copy */ struct tcp_sock *tp =NULL ;if (sk_fullsock (sk)) tp =tcp_sk (sk); + extopt_list =tcp_extopt_get_list (sk); +if (unlikely (OPTION_MD5 & options)) { *ptr++ =htonl ((TCPOPT_NOP <<24 ) | (TCPOPT_NOP <<16 ) | (TCPOPT_MD5SIG <<8 ) |TCPOLEN_MD5SIG );@@ -543,6 +528,9 @@ static void tcp_options_write(__be32 *ptr, struct sk_buff *skb, struct sock *sk, }smc_options_write (ptr, &options); + +if (unlikely (!hlist_empty (extopt_list))) +tcp_extopt_write (ptr, skb, opts, sk); } static void smc_set_option(const struct tcp_sock *tp,@@ -645,6 +633,10 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,smc_set_option (tp, opts, &remaining); +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + remaining -=tcp_extopt_prepare (skb,TCPHDR_SYN , remaining, + opts,tcp_to_sk (tp)); +return MAX_TCP_OPTION_SPACE - remaining; }@@ -708,6 +700,11 @@ static unsigned int tcp_synack_options(const struct sock *sk,smc_set_option_cond (tcp_sk (sk), ireq, opts, &remaining); +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) + remaining -=tcp_extopt_prepare (skb,TCPHDR_SYN |TCPHDR_ACK , + remaining, opts, +req_to_sk (req)); +return MAX_TCP_OPTION_SPACE - remaining; }@@ -741,6 +738,10 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb size +=TCPOLEN_TSTAMP_ALIGNED ; } +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + size +=tcp_extopt_prepare (skb,0 ,MAX_TCP_OPTION_SPACE - size, + opts,tcp_to_sk (tp)); + eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;if (unlikely (eff_sacks)) {const unsigned int remaining =MAX_TCP_OPTION_SPACE - size;@@ -3303,6 +3304,9 @@ static void tcp_connect_init(struct sock *sk) tp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (unlikely (!hlist_empty (&tp->tcp_option_list))) + tp->tcp_header_len +=tcp_extopt_add_header (sk, sk); +/* If user gave his TCP_MAXSEG, record it to clamp */ if (tp->rx_opt.user_mss) tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;net/ipv6/syncookies.c +5
-1@@ -162,7 +162,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL , sk);if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { tsoff = secure_tcpv6_ts_off(sock_net(sk),@@ -174,6 +174,10 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) goto out; +if (unlikely (!hlist_empty (&tp->tcp_option_list)) && +tcp_extopt_check (sk, skb, &tcp_opt)) + goto out; + ret =NULL ; req =inet_reqsk_alloc (&tcp6_request_sock_ops, sk, false);if (!req)net/ipv6/tcp_ipv6.c +32
-0@@ -500,6 +500,9 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,static void tcp_v6_reqsk_destructor (struct request_sock *req) { +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) +tcp_extopt_destroy (req_to_sk (req)); +kfree (inet_rsk (req)->ipv6_opt);kfree_skb (inet_rsk (req)->pktopts); }@@ -789,6 +792,8 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32unsigned int tot_len =sizeof (struct tcphdr );struct dst_entry *dst;__be32 *topt; +struct hlist_head *extopt_list =NULL ; +struct tcp_out_options extraopts;if (tsecr) tot_len +=TCPOLEN_TSTAMP_ALIGNED ;@@ -797,6 +802,25 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 tot_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif +if (sk) + extopt_list =tcp_extopt_get_list (sk); + +if (unlikely (extopt_list && !hlist_empty (extopt_list))) { +unsigned int remaining =MAX_TCP_OPTION_SPACE - tot_len; +u8 extraflags = rst ?TCPHDR_RST :0 ; +int used; + +if (!rst || !th->ack) + extraflags |=TCPHDR_ACK ; + +memset (&extraopts,0 ,sizeof (extraopts)); + + used =tcp_extopt_response_prepare (skb, extraflags, remaining, + &extraopts, sk); + + tot_len += used; + } + buff =alloc_skb (MAX_HEADER +sizeof (struct ipv6hdr ) + tot_len,GFP_ATOMIC );if (!buff)@@ -837,6 +861,9 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 }#endif +if (unlikely (extopt_list && !hlist_empty (extopt_list))) +tcp_extopt_response_write (topt, skb, t1, &extraopts, sk); +memset (&fl6,0 ,sizeof (fl6)); fl6.daddr =ipv6_hdr (skb)->saddr; fl6.saddr =ipv6_hdr (skb)->daddr;@@ -1196,6 +1223,11 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * newinet->inet_daddr = newinet->inet_saddr =LOOPBACK4_IPV6 ; newinet->inet_rcv_saddr =LOOPBACK4_IPV6 ; +if (unlikely (!hlist_empty (&tcp_rsk (req)->tcp_option_list))) { +tcp_extopt_move (req_to_sk (req), newsk); +INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list); + } +#ifdef CONFIG_TCP_MD5SIG /* Copy over the MD5 key from the original socket */ key =tcp_v6_md5_do_lookup (sk, &newsk->sk_v6_daddr);Signature · 1 line
2.15.0--
Mat Martineau
Intel OTC
Christoph Paasch
RFC 7/14 tcp_md5: Don't pass along md5-key
In reply to Christoph PaaschIt is much cleaner to store the key-pointer in tcp_out_options. It allows to remove some MD5-specific code out of the function-arguments and paves the way to adopting the TCP-option framework with TCP-MD5.
--- include/linux/tcp.h | 1 + net/ipv4/tcp_output.c | 46 +++++++++++++++++++--------------------------- 2 files changed, 20 insertions(+), 27 deletions(-)
include/linux/tcp.h +1 -0
@@ -131,6 +131,7 @@ struct tcp_out_options {__u8 *hash_location;/* temporary pointer, overloaded */ __u32 tsval, tsecr;/* need to include OPTION_TS */ struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ +struct tcp_md5sig_key *md5;/* TCP_MD5 signature key */ };/* This is the max number of SACKS that we'll generate and process. It's safe
net/ipv4/tcp_output.c +19 -27
149 lines not on this page · raw message
Signature · 1 line
2.15.0
Christoph Paasch
RFC 8/14 tcp_md5: Detect key inside tcp_v4_send_ack instead of passing it as an argument
In reply to Christoph PaaschThis will simplify to consolidate the TCP_MD5-code into a single place.
--- net/ipv4/tcp_ipv4.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
net/ipv4/tcp_ipv4.c +14 -4
@@ -764,7 +764,6 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)static void tcp_v4_send_ack (const struct sock *sk,struct sk_buff *skb,u32 seq,u32 ack,u32 win,u32 tsval,u32 tsecr,int oif, -struct tcp_md5sig_key *key,int reply_flags,u8 tos) {const struct tcphdr *th =tcp_hdr (skb);
@@ -773,6 +772,9 @@ static void tcp_v4_send_ack(const struct sock *sk,__be32 opt[(MAX_TCP_OPTION_SPACE >>2 )]; } rep;struct hlist_head *extopt_list =NULL ; +#ifdef CONFIG_TCP_MD5SIG +struct tcp_md5sig_key *key; +#endif struct net *net =sock_net (sk);struct ip_reply_arg arg;int offset =0 ;
@@ -803,6 +805,17 @@ static void tcp_v4_send_ack(const struct sock *sk, rep.th.ack =1 ; rep.th.window =htons (win); +#ifdef CONFIG_TCP_MD5SIG +if (sk->sk_state ==TCP_TIME_WAIT ) { + key =tcp_twsk_md5_key (tcp_twsk (sk)); + }else if (sk->sk_state ==TCP_NEW_SYN_RECV ) { + key =tcp_md5_do_lookup (sk, (union tcp_md5_addr *)&ip_hdr (skb)->saddr, +AF_INET ); + }else { + key =NULL ;/* Should not happen */ + } +#endif +if (unlikely (extopt_list && !hlist_empty (extopt_list))) {unsigned int remaining;struct tcp_out_options opts;
@@ -872,7 +885,6 @@ static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)tcp_time_stamp_raw () + tcptw->tw_ts_offset, tcptw->tw_ts_recent, tw->tw_bound_dev_if, -tcp_twsk_md5_key (tcptw), tw->tw_transparent ?IP_REPLY_ARG_NOSRCCHECK :0 , tw->tw_tos );
@@ -900,8 +912,6 @@ static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,tcp_time_stamp_raw () +tcp_rsk (req)->ts_off, req->ts_recent,0 , -tcp_md5_do_lookup (sk, (union tcp_md5_addr *)&ip_hdr (skb)->saddr, -AF_INET ),inet_rsk (req)->no_srccheck ?IP_REPLY_ARG_NOSRCCHECK :0 , ip_hdr(skb)->tos); }
Signature · 1 line
2.15.0
Christoph Paasch
RFC 9/14 tcp_md5: Detect key inside tcp_v6_send_response instead of passing it as an argument
In reply to Christoph PaaschWe want to move all the TCP-MD5 code to a single place which enables us to factor the TCP-MD5 code out of the TCP-stack into the extra-option framework.
Detection of whether or not to drop the segment (as done in tcp_v6_send_reset()) has now been moved to tcp_v6_send_response(). So we needed to adapt the latter so that it can handle the case where we want to exit without sending anything.
--- net/ipv6/tcp_ipv6.c | 119 +++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 62 deletions(-)
net/ipv6/tcp_ipv6.c +57 -62
@@ -82,12 +82,6 @@ static const struct inet_connection_sock_af_ops ipv6_specific;#ifdef CONFIG_TCP_MD5SIG static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific; -#else -static struct tcp_md5sig_key *tcp_v6_md5_do_lookup (const struct sock *sk, -const struct in6_addr *addr) -{ -return NULL ; -}#endif static void inet6_sk_rx_dst_set (struct sock *sk,const struct sk_buff *skb)
@@ -780,12 +774,11 @@ static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {static void tcp_v6_send_response (const struct sock *sk,struct sk_buff *skb,u32 seq,u32 ack,u32 win,u32 tsval,u32 tsecr, -int oif,struct tcp_md5sig_key *key,int rst, -u8 tclass,__be32 label) +int oif,int rst,u8 tclass,__be32 label) {const struct tcphdr *th =tcp_hdr (skb);struct tcphdr *t1; -struct sk_buff *buff; +struct sk_buff *buff =NULL ;struct flowi6 fl6;struct net *net = sk ?sock_net (sk) :dev_net (skb_dst (skb)->dev);struct sock *ctl_sk = net->ipv6.tcp_sk;
@@ -794,10 +787,54 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32__be32 *topt;struct hlist_head *extopt_list =NULL ;struct tcp_out_options extraopts; +#ifdef CONFIG_TCP_MD5SIG +struct tcp_md5sig_key *key =NULL ; +const __u8 *hash_location =NULL ; +struct ipv6hdr *ipv6h =ipv6_hdr (skb); +#endif if (tsecr) tot_len +=TCPOLEN_TSTAMP_ALIGNED ;#ifdef CONFIG_TCP_MD5SIG +rcu_read_lock (); + hash_location =tcp_parse_md5sig_option (th); +if (sk &&sk_fullsock (sk)) { + key =tcp_v6_md5_do_lookup (sk, &ipv6h->saddr); + }else if (sk && sk->sk_state ==TCP_TIME_WAIT ) { +struct tcp_timewait_sock *tcptw =tcp_twsk (sk); + + key =tcp_twsk_md5_key (tcptw); + }else if (sk && sk->sk_state ==TCP_NEW_SYN_RECV ) { + key =tcp_v6_md5_do_lookup (sk, &ipv6h->saddr); + }else if (hash_location) { +unsigned char newhash[16 ]; +struct sock *sk1 =NULL ; +int genhash; + +/* active side is lost. Try to find listening socket through +* source port, and then find md5 key through listening socket. +* we are not loose security here: +* Incoming packet is checked with md5 hash with finding key, +* no RST generated if md5 hash doesn't match. +*/ + sk1 =inet6_lookup_listener (dev_net (skb_dst (skb)->dev), + &tcp_hashinfo,NULL ,0 , + &ipv6h->saddr, + th->source, &ipv6h->daddr, +ntohs (th->source),tcp_v6_iif (skb), +tcp_v6_sdif (skb)); +if (!sk1) + goto out; + + key =tcp_v6_md5_do_lookup (sk1, &ipv6h->saddr); +if (!key) + goto out; + + genhash =tcp_v6_md5_hash_skb (newhash, key,NULL , skb); +if (genhash ||memcmp (hash_location, newhash,16 ) !=0 ) + goto out; + } +if (key) tot_len +=TCPOLEN_MD5SIG_ALIGNED ;#endif
@@ -824,7 +861,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 buff =alloc_skb (MAX_HEADER +sizeof (struct ipv6hdr ) + tot_len,GFP_ATOMIC );if (!buff) -return ; + goto out;skb_reserve (buff,MAX_HEADER +sizeof (struct ipv6hdr ) + tot_len);
@@ -901,24 +938,21 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32TCP_INC_STATS (net,TCP_MIB_OUTSEGS );if (rst)TCP_INC_STATS (net,TCP_MIB_OUTRSTS ); -return ; + buff =NULL ; } +out:kfree_skb (buff); + +#ifdef CONFIG_TCP_MD5SIG +rcu_read_unlock (); +#endif }static void tcp_v6_send_reset (const struct sock *sk,struct sk_buff *skb) {const struct tcphdr *th =tcp_hdr (skb);u32 seq =0 , ack_seq =0 ; -struct tcp_md5sig_key *key =NULL ; -#ifdef CONFIG_TCP_MD5SIG -const __u8 *hash_location =NULL ; -struct ipv6hdr *ipv6h =ipv6_hdr (skb); -unsigned char newhash[16 ]; -int genhash; -struct sock *sk1 =NULL ; -#endif int oif =0 ;if (th->rst)
@@ -930,38 +964,6 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb)if (!sk && !ipv6_unicast_destination (skb))return ; -#ifdef CONFIG_TCP_MD5SIG -rcu_read_lock (); - hash_location =tcp_parse_md5sig_option (th); -if (sk &&sk_fullsock (sk)) { - key =tcp_v6_md5_do_lookup (sk, &ipv6h->saddr); - }else if (hash_location) { -/* -* active side is lost. Try to find listening socket through -* source port, and then find md5 key through listening socket. -* we are not loose security here: -* Incoming packet is checked with md5 hash with finding key, -* no RST generated if md5 hash doesn't match. -*/ - sk1 =inet6_lookup_listener (dev_net (skb_dst (skb)->dev), - &tcp_hashinfo,NULL ,0 , - &ipv6h->saddr, - th->source, &ipv6h->daddr, -ntohs (th->source),tcp_v6_iif (skb), -tcp_v6_sdif (skb)); -if (!sk1) - goto out; - - key =tcp_v6_md5_do_lookup (sk1, &ipv6h->saddr); -if (!key) - goto out; - - genhash =tcp_v6_md5_hash_skb (newhash, key,NULL , skb); -if (genhash ||memcmp (hash_location, newhash,16 ) !=0 ) - goto out; - } -#endif -if (th->ack) seq =ntohl (th->ack_seq);else
@@ -973,20 +975,14 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb)trace_tcp_send_reset (sk, skb); } -tcp_v6_send_response (sk, skb, seq, ack_seq,0 ,0 ,0 , oif, key,1 ,0 ,0 ); - -#ifdef CONFIG_TCP_MD5SIG -out: -rcu_read_unlock (); -#endif +tcp_v6_send_response (sk, skb, seq, ack_seq,0 ,0 ,0 , oif,1 ,0 ,0 ); }static void tcp_v6_send_ack (const struct sock *sk,struct sk_buff *skb,u32 seq,u32 ack,u32 win,u32 tsval,u32 tsecr,int oif, -struct tcp_md5sig_key *key,u8 tclass, -__be32 label) +u8 tclass,__be32 label) { -tcp_v6_send_response (sk, skb, seq, ack, win, tsval, tsecr, oif, key,0 , +tcp_v6_send_response (sk, skb, seq, ack, win, tsval, tsecr, oif,0 , tclass, label); }
@@ -998,7 +994,7 @@ static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)tcp_v6_send_ack (sk, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,tcp_time_stamp_raw () + tcptw->tw_ts_offset, - tcptw->tw_ts_recent, tw->tw_bound_dev_if,tcp_twsk_md5_key (tcptw), + tcptw->tw_ts_recent, tw->tw_bound_dev_if, tw->tw_tclass,cpu_to_be32 (tw->tw_flowlabel));inet_twsk_put (tw);
@@ -1021,7 +1017,6 @@ static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb, req->rsk_rcv_wnd >>inet_rsk (req)->rcv_wscale,tcp_time_stamp_raw () +tcp_rsk (req)->ts_off, req->ts_recent, sk->sk_bound_dev_if, -tcp_v6_md5_do_lookup (sk, &ipv6_hdr (skb)->saddr), 0, 0); }
Signature · 1 line
2.15.0
Christoph Paasch
RFC 10/14 tcp_md5: Check for TCP_MD5 after TCP Timestamps in tcp_established_options
In reply to Christoph PaaschIt really does not matter, because we never use TCP timestamps when TCP_MD5 is enabled (see tcp_syn_options).
Moving TCP_MD5 a bit lower allows for easier adoption of the tcp_extra_option framework.
--- net/ipv4/tcp_output.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
net/ipv4/tcp_output.c +7 -7
@@ -662,6 +662,13 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb opts->options =0 ; +if (likely (tp->rx_opt.tstamp_ok)) { + opts->options |=OPTION_TS ; + opts->tsval = skb ?tcp_skb_timestamp (skb) + tp->tsoffset :0 ; + opts->tsecr = tp->rx_opt.ts_recent; + size +=TCPOLEN_TSTAMP_ALIGNED ; + } +#ifdef CONFIG_TCP_MD5SIG opts->md5 = tp->af_specific->md5_lookup (sk, sk);if (unlikely (opts->md5)) {
@@ -672,13 +679,6 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb opts->md5 =NULL ;#endif -if (likely (tp->rx_opt.tstamp_ok)) { - opts->options |=OPTION_TS ; - opts->tsval = skb ?tcp_skb_timestamp (skb) + tp->tsoffset :0 ; - opts->tsecr = tp->rx_opt.ts_recent; - size +=TCPOLEN_TSTAMP_ALIGNED ; - } -if (unlikely (!hlist_empty (&tp->tcp_option_list))) size +=tcp_extopt_prepare (skb,0 ,MAX_TCP_OPTION_SPACE - size, opts,tcp_to_sk (tp));
Signature · 1 line
2.15.0
Christoph Paasch
RFC 11/14 tcp_md5: Move TCP-MD5 code out of TCP itself
In reply to Christoph PaaschThis is all just copy-pasting the TCP_MD5-code into functions that are placed in net/ipv4/tcp_md5.c.
--- include/linux/inet_diag.h | 1 + include/linux/tcp_md5.h | 138 ++++++ include/net/tcp.h | 77 ---- net/ipv4/Makefile | 1 + net/ipv4/tcp.c | 133 +----- net/ipv4/tcp_diag.c | 81 +--- net/ipv4/tcp_input.c | 38 -- net/ipv4/tcp_ipv4.c | 520 ++------------------- net/ipv4/tcp_md5.c | 1102 +++++++++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_minisocks.c | 27 +- net/ipv4/tcp_output.c | 4 +- net/ipv6/tcp_ipv6.c | 318 +------------ 12 files changed, 1305 insertions(+), 1135 deletions(-) create mode 100644 include/linux/tcp_md5.h create mode 100644 net/ipv4/tcp_md5.c
include/linux/inet_diag.h +1 -0
@@ -2,6 +2,7 @@#ifndef _INET_DIAG_H_#define _INET_DIAG_H_ 1 +#include <linux/user_namespace.h> #include <uapi/linux/inet_diag.h> struct net ;
include/linux/tcp_md5.h new file +138 -0
138 lines not on this page · raw message
include/net/tcp.h +0 -77
95 lines not on this page · raw message
net/ipv4/Makefile +1 -0
@@ -61,6 +61,7 @@ obj-$(CONFIG_TCP_CONG_LP) += tcp_lp.o obj-$(CONFIG_TCP_CONG_YEAH) += tcp_yeah.o obj-$(CONFIG_TCP_CONG_ILLINOIS) += tcp_illinois.o obj-$(CONFIG_NETLABEL) += cipso_ipv4.o +obj-$(CONFIG_TCP_MD5SIG) += tcp_md5.o obj-$(CONFIG_XFRM) += xfrm4_policy.o xfrm4_state.o xfrm4_input.o \ xfrm4_output.o xfrm4_protocol.o
net/ipv4/tcp.c +1 -132
145 lines not on this page · raw message
net/ipv4/tcp_diag.c +6 -75
103 lines not on this page · raw message
net/ipv4/tcp_input.c +0 -38
@@ -3847,44 +3847,6 @@ static bool tcp_fast_parse_options(const struct net *net,return false; } -#ifdef CONFIG_TCP_MD5SIG -/* -* Parse MD5 Signature option -*/ -const u8 *tcp_parse_md5sig_option (const struct tcphdr *th) -{ -int length = (th->doff <<2 ) -sizeof (*th); -const u8 *ptr = (const u8 *)(th +1 ); - -/* If the TCP option is too short, we can short cut */ -if (length <TCPOLEN_MD5SIG ) -return NULL ; - -while (length >0 ) { -int opcode = *ptr++; -int opsize; - -switch (opcode) { -case TCPOPT_EOL : -return NULL ; -case TCPOPT_NOP : - length--; -continue ; -default : - opsize = *ptr++; -if (opsize <2 || opsize > length) -return NULL ; -if (opcode ==TCPOPT_MD5SIG ) -return opsize ==TCPOLEN_MD5SIG ? ptr :NULL ; - } - ptr += opsize -2 ; - length -= opsize; - } -return NULL ; -} -EXPORT_SYMBOL (tcp_parse_md5sig_option); -#endif -/* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM * * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
net/ipv4/tcp_ipv4.c +34 -486
647 lines not on this page · raw message
net/ipv4/tcp_md5.c new file +1102 -0
1,102 lines not on this page · raw message
net/ipv4/tcp_minisocks.c +5 -22
@@ -22,6 +22,7 @@#include <linux/module.h> #include <linux/slab.h> #include <linux/sysctl.h> +#include <linux/tcp_md5.h> #include <linux/workqueue.h> #include <linux/static_key.h> #include <net/tcp.h>
@@ -295,21 +296,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)INIT_HLIST_HEAD (&tp->tcp_option_list); }#ifdef CONFIG_TCP_MD5SIG -/* -* The timewait bucket does not have the key DB from the -* sock structure. We just make a quick copy of the -* md5 key being used (if indeed we are using one) -* so the timewait ack generating code has the key. -*/ -do { -struct tcp_md5sig_key *key; - tcptw->tw_md5_key =NULL ; - key = tp->af_specific->md5_lookup (sk, sk); -if (key) { - tcptw->tw_md5_key =kmemdup (key,sizeof (*key),GFP_ATOMIC ); -BUG_ON (tcptw->tw_md5_key && !tcp_alloc_md5sig_pool ()); - } - }while (0 ); +tcp_md5_time_wait (sk, tw);#endif /* Get the TIME_WAIT timeout firing. */
@@ -346,13 +333,10 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)void tcp_twsk_destructor (struct sock *sk) {#ifdef CONFIG_TCP_MD5SIG -struct tcp_timewait_sock *twsk =tcp_twsk (sk); - -if (twsk->tw_md5_key) -kfree_rcu (twsk->tw_md5_key, rcu); +tcp_md5_twsk_destructor (sk);#endif -if (unlikely (!hlist_empty (&twsk->tcp_option_list))) +if (unlikely (!hlist_empty (&tcp_twsk (sk)->tcp_option_list)))tcp_extopt_destroy (sk); }EXPORT_SYMBOL_GPL (tcp_twsk_destructor);
@@ -538,8 +522,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk, newtp->tsoffset = treq->ts_off;#ifdef CONFIG_TCP_MD5SIG newtp->md5sig_info =NULL ;/*XXX*/ -if (newtp->af_specific->md5_lookup (sk, newsk)) - newtp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ; +tcp_md5_add_header_len (sk, newsk);#endif if (unlikely (!hlist_empty (&treq->tcp_option_list))) newtp->tcp_header_len +=tcp_extopt_add_header (req_to_sk (req), newsk);
net/ipv4/tcp_output.c +2 -2
@@ -42,6 +42,7 @@#include <linux/gfp.h> #include <linux/module.h> #include <linux/static_key.h> +#include <linux/tcp_md5.h> #include <trace/events/tcp.h>
@@ -3238,8 +3239,7 @@ static void tcp_connect_init(struct sock *sk) tp->tcp_header_len +=TCPOLEN_TSTAMP_ALIGNED ;#ifdef CONFIG_TCP_MD5SIG -if (tp->af_specific->md5_lookup (sk, sk)) - tp->tcp_header_len +=TCPOLEN_MD5SIG_ALIGNED ; +tcp_md5_add_header_len (sk, sk);#endif if (unlikely (!hlist_empty (&tp->tcp_option_list)))
net/ipv6/tcp_ipv6.c +15 -303
391 lines not on this page · raw message
Signature · 1 line
2.15.0
Christoph Paasch
RFC 12/14 tcp_md5: Use tcp_extra_options in output path
In reply to Christoph PaaschThis patch starts making use of the extra_option framework for TCP_MD5.
One tricky part is that extra_options are called at the end of the tcp_syn_options(), while TCP_MD5 is called at the beginning.
TCP_MD5 is called at the beginning because it wants to disable TCP-timestamps (for option-space reasons). So, in the _prepare-function of the extra options we need to undo the work that was done when enabling TCP timestamps.
Another thing to note is that in tcp_v4_send_reset (and its IPv6 counterpart), we were looking previously for the listening-socket (if sk == NULL) in case there was an MD5 signature in the TCP-option space of the incoming packet.
With the extra-option framework we can't do this anymore, because extra-options are part of the TCP-socket's tcp_option_list. If there is no socket, it means we can't parse the option.
This shouldn't thave an impact, because when we receive a segment and there is not established socket, we will match on the listening socket (if it's still there). Then, when we decide to respond with a RST in tcp_rcv_state_process, we will give to tcp_v4_send_reset() the listening-socket and thus will parse the TCP_MD5 option.
--- include/linux/tcp.h | 10 +- include/linux/tcp_md5.h | 64 ----- net/ipv4/tcp_ipv4.c | 54 ---- net/ipv4/tcp_md5.c | 696 +++++++++++++++++++++++++++++++++-------------- net/ipv4/tcp_minisocks.c | 12 - net/ipv4/tcp_output.c | 68 +---- net/ipv6/tcp_ipv6.c | 23 -- 7 files changed, 488 insertions(+), 439 deletions(-)
include/linux/tcp.h +2 -8
@@ -127,11 +127,11 @@ struct tcp_out_options {u16 mss;/* 0 to disable */ u8 ws;/* window scale, 0 to disable */ u8 num_sack_blocks;/* number of SACK blocks to include */ -u8 hash_size;/* bytes in hash_location */ -__u8 *hash_location;/* temporary pointer, overloaded */ __u32 tsval, tsecr;/* need to include OPTION_TS */ struct tcp_fastopen_cookie *fastopen_cookie;/* Fast open cookie */ +#ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *md5;/* TCP_MD5 signature key */ +#endif };/* This is the max number of SACKS that we'll generate and process. It's safe
@@ -380,9 +380,6 @@ struct tcp_sock {#ifdef CONFIG_TCP_MD5SIG /* TCP AF-Specific parts; only used by MD5 Signature support so far */ const struct tcp_sock_af_ops *af_specific; - -/* TCP MD5 Signature Option information */ -struct tcp_md5sig_info __rcu *md5sig_info;#endif /* TCP fastopen related information */
@@ -440,9 +437,6 @@ struct tcp_timewait_sock {long tw_ts_recent_stamp;struct hlist_head tcp_option_list; -#ifdef CONFIG_TCP_MD5SIG -struct tcp_md5sig_key *tw_md5_key; -#endif };static inline struct tcp_timewait_sock *tcp_twsk (const struct sock *sk)
include/linux/tcp_md5.h +0 -64
85 lines not on this page · raw message
net/ipv4/tcp_ipv4.c +0 -54
113 lines not on this page · raw message
net/ipv4/tcp_md5.c +485 -211
863 lines not on this page · raw message
net/ipv4/tcp_minisocks.c +0 -12
@@ -22,7 +22,6 @@#include <linux/module.h> #include <linux/slab.h> #include <linux/sysctl.h> -#include <linux/tcp_md5.h> #include <linux/workqueue.h> #include <linux/static_key.h> #include <net/tcp.h>
@@ -295,9 +294,6 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)tcp_extopt_move (sk, (struct sock *)tw);INIT_HLIST_HEAD (&tp->tcp_option_list); } -#ifdef CONFIG_TCP_MD5SIG -tcp_md5_time_wait (sk, tw); -#endif /* Get the TIME_WAIT timeout firing. */ if (timeo < rto)
@@ -332,10 +328,6 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)void tcp_twsk_destructor (struct sock *sk) { -#ifdef CONFIG_TCP_MD5SIG -tcp_md5_twsk_destructor (sk); -#endif -if (unlikely (!hlist_empty (&tcp_twsk (sk)->tcp_option_list)))tcp_extopt_destroy (sk); }
@@ -520,10 +512,6 @@ struct sock *tcp_create_openreq_child(const struct sock *sk, newtp->tcp_header_len =sizeof (struct tcphdr ); } newtp->tsoffset = treq->ts_off; -#ifdef CONFIG_TCP_MD5SIG - newtp->md5sig_info =NULL ;/*XXX*/ -tcp_md5_add_header_len (sk, newsk); -#endif if (unlikely (!hlist_empty (&treq->tcp_option_list))) newtp->tcp_header_len +=tcp_extopt_add_header (req_to_sk (req), newsk);
net/ipv4/tcp_output.c +1 -67
128 lines not on this page · raw message
net/ipv6/tcp_ipv6.c +0 -23
@@ -577,20 +577,6 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32if (tsecr) tot_len +=TCPOLEN_TSTAMP_ALIGNED ; -#ifdef CONFIG_TCP_MD5SIG -{ -int ret; - - ret =tcp_v6_md5_send_response_prepare (skb,0 , -MAX_TCP_OPTION_SPACE - tot_len, - &extraopts, sk); - -if (ret ==-1 ) - goto out; - - tot_len += ret; -} -#endif if (sk) extopt_list =tcp_extopt_get_list (sk);
@@ -639,11 +625,6 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 *topt++ =htonl (tsecr); } -#ifdef CONFIG_TCP_MD5SIG -if (extraopts.md5) -tcp_v6_md5_send_response_write (topt, skb, t1, &extraopts, sk); -#endif -if (unlikely (extopt_list && !hlist_empty (extopt_list)))tcp_extopt_response_write (topt, skb, t1, &extraopts, sk);
@@ -962,10 +943,6 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *INIT_HLIST_HEAD (&tcp_rsk (req)->tcp_option_list); } -#ifdef CONFIG_TCP_MD5SIG -tcp_v6_md5_syn_recv_sock (sk, newsk); -#endif -if (__inet_inherit_port (sk, newsk) <0 ) {inet_csk_prepare_forced_close (newsk);tcp_done (newsk);
Signature · 1 line
2.15.0
Christoph Paasch
RFC 13/14 tcp_md5: Cleanup TCP-code
In reply to Christoph PaaschNow that we have consolidated the TCP_MD5 output path, we can cleanup TCP and its callbacks to MD5.
These callbacks are solely there to handle the different address-familiese (v4, v6 and v4mapped).
Now that we have isolated the TCP_MD5-code it is acceptable to add a bit more complexity inside tcp_md5.c to handle these address-families at the benefit of getting rid of these callbacks in tcp_sock, together with its assignments in tcp_v4/6_connect,...
--- include/linux/tcp.h | 5 - include/linux/tcp_md5.h | 18 +-- include/net/tcp.h | 24 ---- net/ipv4/tcp.c | 2 +- net/ipv4/tcp_ipv4.c | 8 -- net/ipv4/tcp_md5.c | 340 ++++++++++++++++++++++-------------------------- net/ipv6/tcp_ipv6.c | 17 --- 7 files changed, 155 insertions(+), 259 deletions(-)
include/linux/tcp.h +0 -5
@@ -377,11 +377,6 @@ struct tcp_sock {* while socket was owned by user. */ -#ifdef CONFIG_TCP_MD5SIG -/* TCP AF-Specific parts; only used by MD5 Signature support so far */ -const struct tcp_sock_af_ops *af_specific; -#endif -/* TCP fastopen related information */ struct tcp_fastopen_request *fastopen_req;/* fastopen_rsk points to request_sock that resulted in this big
include/linux/tcp_md5.h +2 -16
@@ -26,28 +26,14 @@ struct tcp_md5sig_key {struct rcu_head rcu; }; -extern const struct tcp_sock_af_ops tcp_sock_ipv4_specific; -extern const struct tcp_sock_af_ops tcp_sock_ipv6_specific; -extern const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific; -/* - functions */ -int tcp_v4_md5_hash_skb (char *md5_hash,const struct tcp_md5sig_key *key, -const struct sock *sk,const struct sk_buff *skb); -struct tcp_md5sig_key *tcp_v4_md5_lookup (const struct sock *sk, -const struct sock *addr_sk); +int tcp_md5_parse_keys (struct sock *sk,int optname,char __user *optval, +int optlen);bool tcp_v4_inbound_md5_hash (const struct sock *sk,const struct sk_buff *skb); -struct tcp_md5sig_key *tcp_v6_md5_lookup (const struct sock *sk, -const struct sock *addr_sk); - -int tcp_v6_md5_hash_skb (char *md5_hash, -const struct tcp_md5sig_key *key, -const struct sock *sk, -const struct sk_buff *skb); -bool tcp_v6_inbound_md5_hash (const struct sock *sk,const struct sk_buff *skb);
include/net/tcp.h +0 -24
@@ -1729,32 +1729,8 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,const struct tcp_request_sock_ops *af_ops, struct sock *sk, struct sk_buff *skb); -/* TCP af-specific functions */ -struct tcp_sock_af_ops { -#ifdef CONFIG_TCP_MD5SIG -struct tcp_md5sig_key *(*md5_lookup) (const struct sock *sk, -const struct sock *addr_sk); -int (*calc_md5_hash)(char *location, -const struct tcp_md5sig_key *md5, -const struct sock *sk, -const struct sk_buff *skb); -int (*md5_parse)(struct sock *sk, -int optname, -char __user *optval, -int optlen); -#endif -}; -struct tcp_request_sock_ops {u16 mss_clamp; -#ifdef CONFIG_TCP_MD5SIG -struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk, -const struct sock *addr_sk); -int (*calc_md5_hash) (char *location, -const struct tcp_md5sig_key *md5, -const struct sock *sk, -const struct sk_buff *skb); -#endif void (*init_req)(struct request_sock *req,const struct sock *sk_listener,struct sk_buff *skb);
net/ipv4/tcp.c +1 -1
@@ -2795,7 +2795,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level,case TCP_MD5SIG :case TCP_MD5SIG_EXT :/* Read the IP->Key mappings from userspace */ - err = tp->af_specific->md5_parse (sk, optname, optval, optlen); + err =tcp_md5_parse_keys (sk, optname, optval, optlen);break ;#endif case TCP_USER_TIMEOUT :
net/ipv4/tcp_ipv4.c +0 -8
@@ -889,10 +889,6 @@ struct request_sock_ops tcp_request_sock_ops __read_mostly = {static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = { .mss_clamp =TCP_MSS_DEFAULT , -#ifdef CONFIG_TCP_MD5SIG - .req_md5_lookup = tcp_v4_md5_lookup, - .calc_md5_hash = tcp_v4_md5_hash_skb, -#endif .init_req = tcp_v4_init_req, #ifdef CONFIG_SYN_COOKIES .cookie_init_seq = cookie_v4_init_sequence,
@@ -1450,10 +1446,6 @@ static int tcp_v4_init_sock(struct sock *sk) icsk->icsk_af_ops = &ipv4_specific; -#ifdef CONFIG_TCP_MD5SIG -tcp_sk (sk)->af_specific = &tcp_sock_ipv4_specific; -#endif -return 0 ; }
net/ipv4/tcp_md5.c +152 -188
422 lines not on this page · raw message
net/ipv6/tcp_ipv6.c +0 -17
@@ -208,9 +208,6 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, icsk->icsk_af_ops = &ipv6_mapped; sk->sk_backlog_rcv = tcp_v4_do_rcv; -#ifdef CONFIG_TCP_MD5SIG - tp->af_specific = &tcp_sock_ipv6_mapped_specific; -#endif err =tcp_v4_connect (sk, (struct sockaddr *)&sin,sizeof (sin));
@@ -218,9 +215,6 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, icsk->icsk_ext_hdr_len = exthdrlen; icsk->icsk_af_ops = &ipv6_specific; sk->sk_backlog_rcv = tcp_v6_do_rcv; -#ifdef CONFIG_TCP_MD5SIG - tp->af_specific = &tcp_sock_ipv6_specific; -#endif goto failure; } np->saddr = sk->sk_v6_rcv_saddr;
@@ -543,10 +537,6 @@ struct request_sock_ops tcp6_request_sock_ops __read_mostly = {static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = { .mss_clamp =IPV6_MIN_MTU -sizeof (struct tcphdr ) -sizeof (struct ipv6hdr ), -#ifdef CONFIG_TCP_MD5SIG - .req_md5_lookup = tcp_v6_md5_lookup, - .calc_md5_hash = tcp_v6_md5_hash_skb, -#endif .init_req = tcp_v6_init_req, #ifdef CONFIG_SYN_COOKIES .cookie_init_seq = cookie_v6_init_sequence,
@@ -821,9 +811,6 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *inet_csk (newsk)->icsk_af_ops = &ipv6_mapped; newsk->sk_backlog_rcv = tcp_v4_do_rcv; -#ifdef CONFIG_TCP_MD5SIG - newtp->af_specific = &tcp_sock_ipv6_mapped_specific; -#endif newnp->ipv6_mc_list =NULL ; newnp->ipv6_ac_list =NULL ;
@@ -1430,10 +1417,6 @@ static int tcp_v6_init_sock(struct sock *sk) icsk->icsk_af_ops = &ipv6_specific; -#ifdef CONFIG_TCP_MD5SIG -tcp_sk (sk)->af_specific = &tcp_sock_ipv6_specific; -#endif -return 0 ; }
Signature · 1 line
2.15.0
Christoph Paasch
RFC 14/14 tcp_md5: Use TCP extra-options on the input path
In reply to Christoph PaaschThe checks are now being done through the extra-option framework. For TCP MD5 this means that the check happens a bit later than usual.
--- include/linux/tcp_md5.h | 23 +---------------------- net/ipv4/tcp_input.c | 8 -------- net/ipv4/tcp_ipv4.c | 9 --------- net/ipv4/tcp_md5.c | 29 ++++++++++++++++++++++++----- net/ipv6/tcp_ipv6.c | 9 --------- 5 files changed, 25 insertions(+), 53 deletions(-)
include/linux/tcp_md5.h +1 -22
@@ -31,30 +31,9 @@ struct tcp_md5sig_key {int tcp_md5_parse_keys (struct sock *sk,int optname,char __user *optval,int optlen); -bool tcp_v4_inbound_md5_hash (const struct sock *sk, -const struct sk_buff *skb); - -bool tcp_v6_inbound_md5_hash (const struct sock *sk, -const struct sk_buff *skb); -int tcp_md5_diag_get_aux (struct sock *sk,bool net_admin,struct sk_buff *skb);int tcp_md5_diag_get_aux_size (struct sock *sk,bool net_admin); -#else - -static inline bool tcp_v4_inbound_md5_hash (const struct sock *sk, -const struct sk_buff *skb) -{ -return false; -} - -static inline bool tcp_v6_inbound_md5_hash (const struct sock *sk, -const struct sk_buff *skb) -{ -return false; -} - -#endif - +#endif /* CONFIG_TCP_MD5SIG */ #endif /* _LINUX_TCP_MD5_H */
net/ipv4/tcp_input.c +0 -8
@@ -3754,14 +3754,6 @@ void tcp_parse_options(const struct net *net,TCP_SKB_CB (skb)->sacked = (ptr -2 ) - (unsigned char *)th; }break ; -#ifdef CONFIG_TCP_MD5SIG -case TCPOPT_MD5SIG : -/* -* The MD5 Hash has already been -* checked (see tcp_v{4,6}_do_rcv()). -*/ -break ; -#endif case TCPOPT_FASTOPEN : tcp_parse_fastopen_option( opsize - TCPOLEN_FASTOPEN_BASE,
net/ipv4/tcp_ipv4.c +0 -9
@@ -62,7 +62,6 @@#include <linux/init.h> #include <linux/times.h> #include <linux/slab.h> -#include <linux/tcp_md5.h> #include <net/net_namespace.h> #include <net/icmp.h>
@@ -1249,11 +1248,6 @@ int tcp_v4_rcv(struct sk_buff *skb)struct sock *nsk; sk = req->rsk_listener; -if (unlikely (tcp_v4_inbound_md5_hash (sk, skb))) { -sk_drops_add (sk, skb); -reqsk_put (req); - goto discard_it; - }if (unlikely (sk->sk_state !=TCP_LISTEN )) {inet_csk_reqsk_queue_drop_and_put (sk, req); goto lookup;
@@ -1293,9 +1287,6 @@ int tcp_v4_rcv(struct sk_buff *skb)if (!xfrm4_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; -if (tcp_v4_inbound_md5_hash (sk, skb)) - goto discard_and_relse; -nf_reset (skb);if (tcp_filter (sk, skb))
net/ipv4/tcp_md5.c +24 -5
@@ -29,6 +29,10 @@ static DEFINE_PER_CPU(struct tcp_md5sig_pool, tcp_md5sig_pool);static DEFINE_MUTEX (tcp_md5sig_mutex);static bool tcp_md5sig_pool_populated; +static bool tcp_inbound_md5_hash (struct sock *sk,const struct sk_buff *skb, +struct tcp_options_received *opt_rx, +struct tcp_extopt_store *store); + static unsigned int tcp_md5_extopt_prepare(struct sk_buff *skb, u8 flags, unsigned int remaining, struct tcp_out_options *opts,
@@ -76,6 +80,7 @@ struct tcp_md5_extopt {static const struct tcp_extopt_ops tcp_md5_extra_ops = { .option_kind =TCPOPT_MD5SIG , + .check = tcp_inbound_md5_hash, .prepare = tcp_md5_extopt_prepare, .write = tcp_md5_extopt_write, .response_prepare = tcp_md5_send_response_prepare,
@@ -863,8 +868,8 @@ static struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk, }/* Called with rcu_read_lock() */ -bool tcp_v4_inbound_md5_hash (const struct sock *sk, -const struct sk_buff *skb) +static bool tcp_v4_inbound_md5_hash (const struct sock *sk, +const struct sk_buff *skb) {/* This gets called for each TCP segment that arrives * so we want to be efficient.
@@ -918,8 +923,8 @@ bool tcp_v4_inbound_md5_hash(const struct sock *sk, }#if IS_ENABLED (CONFIG_IPV6 ) -bool tcp_v6_inbound_md5_hash (const struct sock *sk, -const struct sk_buff *skb) +static bool tcp_v6_inbound_md5_hash (const struct sock *sk, +const struct sk_buff *skb) {const __u8 *hash_location =NULL ;struct tcp_md5sig_key *hash_expected;
@@ -961,7 +966,6 @@ bool tcp_v6_inbound_md5_hash(const struct sock *sk,return false; } -EXPORT_SYMBOL_GPL (tcp_v6_inbound_md5_hash);static struct tcp_md5sig_key *tcp_v6_md5_lookup (const struct sock *sk,const struct sock *addr_sk)
@@ -971,6 +975,21 @@ static struct tcp_md5sig_key *tcp_v6_md5_lookup(const struct sock *sk,EXPORT_SYMBOL_GPL (tcp_v6_md5_lookup);#endif +static bool tcp_inbound_md5_hash (struct sock *sk,const struct sk_buff *skb, +struct tcp_options_received *opt_rx, +struct tcp_extopt_store *store) +{ +if (skb->protocol ==htons (ETH_P_IP )) { +return tcp_v4_inbound_md5_hash (sk, skb); +#if IS_ENABLED (CONFIG_IPV6 ) + } else { +return tcp_v6_inbound_md5_hash (sk, skb); +#endif + } + +return false; +} +static void tcp_diag_md5sig_fill (struct tcp_diag_md5sig *info,const struct tcp_md5sig_key *key) {
net/ipv6/tcp_ipv6.c +0 -9
@@ -43,7 +43,6 @@#include <linux/ipv6.h> #include <linux/icmpv6.h> #include <linux/random.h> -#include <linux/tcp_md5.h> #include <net/tcp.h> #include <net/ndisc.h>
@@ -1173,11 +1172,6 @@ static int tcp_v6_rcv(struct sk_buff *skb)struct sock *nsk; sk = req->rsk_listener; -if (tcp_v6_inbound_md5_hash (sk, skb)) { -sk_drops_add (sk, skb); -reqsk_put (req); - goto discard_it; - }if (unlikely (sk->sk_state !=TCP_LISTEN )) {inet_csk_reqsk_queue_drop_and_put (sk, req); goto lookup;
@@ -1214,9 +1208,6 @@ static int tcp_v6_rcv(struct sk_buff *skb)if (!xfrm6_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; -if (tcp_v6_inbound_md5_hash (sk, skb)) - goto discard_and_relse; -if (tcp_filter (sk, skb)) goto discard_and_relse; th = (const struct tcphdr *)skb->data;
Signature · 1 line
2.15.0
Christoph Paasch
FYI, the SMC-patches did not make it as I had already the "Cc:" for the SMC-author (and my git send-mail automatically added her) and I was too lazy to remove the Cc again from the commit-message ;-)
Christoph
Quoted 91 lines · Christoph Paasch
Final post to the mptcp-list before submission to netdev :)
---
This patchset introduces a generic framework for handling TCP-options.
TCP-options like
TCP_MD5and SMC are rather rare use-cases, but their implementation is rather intrusive to the TCP-stack. Other, more recent TCP extensions like TCP-crypt, MPTCP or TCP-AO would make this situation even worse.This new framework allows to add these TCP-options in a modular way. Writing, reading and acting upon these options is done through callbacks that get registered to a TCP-socket. A TCP-socket has a list of "extra" TCP-options that it will use.
We make TCP-SMC and TCP-MD5SIG adopt this new framework. As can be seen, there is now no more TCP-SMC code in the TCP-files and the TCP-MD5 code has been reduced to a bare minimum.
This patchset is admittedly rather big, but we wanted to show where the framework will lead to and what it enables. Suggestions as to how to better structure the patchset is appreciated.
One point of discussion might be whether or not we should use static-keys before accessing the
tcp_option_listto avoid branching and the additional access to thetcp_sock, request-sock, time-wait-sock structure.
The way static keys could be used is that they get incremented each time a socket starts using one of the new TCP-options and decremented when the socket eventually gets destroyed. A caveat of this design would be that if a host keeps on creating/closing these sockets in a sequence, each time we go into the slow path of the static keys occuring potentially a big overhead to update all the jump-labels.
For now we opted for a simple if (unlikely(!hlist_empty(...)) check.Feedback is very welcome!
Thanks,
Mat & ChristophChristoph Paasch (13):
tcp: Write options after the header has been fully done
tcp: Pass sock and skb totcp_options_write
tcp: Allowtcp_fast_parse_optionsto drop segments
tcp_smc: Makesmc_parse_optionsreturn 1 on success
tcp_smc: Make SMC use TCP extra-option framework
tcp_md5: Don't pass along md5-key
tcp_md5: Detect key insidetcp_v4_send_ackinstead of passing it as an
argument
tcp_md5: Detect key insidetcp_v6_send_responseinstead of passing it
as an argument
tcp_md5: Check forTCP_MD5after TCP Timestamps in
tcp_established_options
tcp_md5: Move TCP-MD5 code out of TCP itself
tcp_md5: Usetcp_extra_optionsin output path
tcp_md5: Cleanup TCP-code
tcp_md5: Use TCP extra-options on the input pathMat Martineau (1):
tcp: Register handlers for extra TCP optionsdrivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/inet_diag.h | 1 + include/linux/tcp.h | 43 +- include/linux/tcp_md5.h | 39 ++ include/net/inet_sock.h | 3 +- include/net/tcp.h | 212 +++--- net/ipv4/Makefile | 1 + net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 391 ++++++++--- net/ipv4/tcp_diag.c | 81 +-- net/ipv4/tcp_input.c | 137 ++-- net/ipv4/tcp_ipv4.c | 552 ++-------------- net/ipv4/tcp_md5.c | 1359 ++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_minisocks.c | 77 +-- net/ipv4/tcp_output.c | 182 +---- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 386 ++--------- net/smc/af_smc.c | 190 +++++- 18 files changed, 2222 insertions(+), 1446 deletions(-) create mode 100644 include/linux/tcp_md5.h create mode 100644 net/ipv4/tcp_md5.cSignature · 6 lines
2.15.0 _______________________________________________ mptcp mailing list mptcp(a)lists.01.org https://lists.01.org/mailman/listinfo/mptcp
Mat Martineau
Christoph,
Quoted 1 line · Christoph Paasch
Final post to the mptcp-list before submission to netdev :)
Looks good - other than the comments I had on the framework patch earlier today, I don't have any other requests.
Which net-next commit ID did you base the patch set on? I can push a branch to git.kernel.org, but it would be best to match your git history.
Mat
Quoted 80 lines
---
This patchset introduces a generic framework for handling TCP-options.
TCP-options like
TCP_MD5and SMC are rather rare use-cases, but their implementation is rather intrusive to the TCP-stack. Other, more recent TCP extensions like TCP-crypt, MPTCP or TCP-AO would make this situation even worse.This new framework allows to add these TCP-options in a modular way. Writing, reading and acting upon these options is done through callbacks that get registered to a TCP-socket. A TCP-socket has a list of "extra" TCP-options that it will use.
We make TCP-SMC and TCP-MD5SIG adopt this new framework. As can be seen, there is now no more TCP-SMC code in the TCP-files and the TCP-MD5 code has been reduced to a bare minimum.
This patchset is admittedly rather big, but we wanted to show where the framework will lead to and what it enables. Suggestions as to how to better structure the patchset is appreciated.
One point of discussion might be whether or not we should use static-keys before accessing the
tcp_option_listto avoid branching and the additional access to thetcp_sock, request-sock, time-wait-sock structure.
The way static keys could be used is that they get incremented each time a socket starts using one of the new TCP-options and decremented when the socket eventually gets destroyed. A caveat of this design would be that if a host keeps on creating/closing these sockets in a sequence, each time we go into the slow path of the static keys occuring potentially a big overhead to update all the jump-labels.
For now we opted for a simple if (unlikely(!hlist_empty(...)) check.Feedback is very welcome!
Thanks,
Mat & ChristophChristoph Paasch (13):
tcp: Write options after the header has been fully done
tcp: Pass sock and skb totcp_options_write
tcp: Allowtcp_fast_parse_optionsto drop segments
tcp_smc: Makesmc_parse_optionsreturn 1 on success
tcp_smc: Make SMC use TCP extra-option framework
tcp_md5: Don't pass along md5-key
tcp_md5: Detect key insidetcp_v4_send_ackinstead of passing it as an
argument
tcp_md5: Detect key insidetcp_v6_send_responseinstead of passing it
as an argument
tcp_md5: Check forTCP_MD5after TCP Timestamps in
tcp_established_options
tcp_md5: Move TCP-MD5 code out of TCP itself
tcp_md5: Usetcp_extra_optionsin output path
tcp_md5: Cleanup TCP-code
tcp_md5: Use TCP extra-options on the input pathMat Martineau (1):
tcp: Register handlers for extra TCP optionsdrivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/inet_diag.h | 1 + include/linux/tcp.h | 43 +- include/linux/tcp_md5.h | 39 ++ include/net/inet_sock.h | 3 +- include/net/tcp.h | 212 +++--- net/ipv4/Makefile | 1 + net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 391 ++++++++--- net/ipv4/tcp_diag.c | 81 +-- net/ipv4/tcp_input.c | 137 ++-- net/ipv4/tcp_ipv4.c | 552 ++-------------- net/ipv4/tcp_md5.c | 1359 ++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_minisocks.c | 77 +-- net/ipv4/tcp_output.c | 182 +---- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 386 ++--------- net/smc/af_smc.c | 190 +++++- 18 files changed, 2222 insertions(+), 1446 deletions(-)
Quoted 7 lines
create mode 100644
include/linux/tcp_md5.h
create mode 100644net/ipv4/tcp_md5.cSignature · 1 line
2.15.0
--
Mat Martineau
Intel OTC
Christoph Paasch
Hello Mat,
Quoted 9 lines · Mat Martineau
Christoph,
Quoted 1 line · Christoph Paasch
Final post to the mptcp-list before submission to netdev :)
Looks good - other than the comments I had on the framework patch earlier today, I don't have any other requests.
Nice! I found a little bug with racing tcp_check_req() - so I have to fix that. But nothing big. I will build & test once more and then submit.
Quoted 2 lines
Which net-next commit ID did you base the patch set on? I can push a branch to git.kernel.org, but it would be best to match your git history.
At the moment I'm based on c30abd5e40dd ("Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net")
but I update regularly.
Thanks for all your feedback!
Christoph
Quoted 96 lines
Mat
Quoted 80 lines
---
This patchset introduces a generic framework for handling TCP-options.
TCP-options like
TCP_MD5and SMC are rather rare use-cases, but their implementation is rather intrusive to the TCP-stack. Other, more recent TCP extensions like TCP-crypt, MPTCP or TCP-AO would make this situation even worse.This new framework allows to add these TCP-options in a modular way. Writing, reading and acting upon these options is done through callbacks that get registered to a TCP-socket. A TCP-socket has a list of "extra" TCP-options that it will use.
We make TCP-SMC and TCP-MD5SIG adopt this new framework. As can be seen, there is now no more TCP-SMC code in the TCP-files and the TCP-MD5 code has been reduced to a bare minimum.
This patchset is admittedly rather big, but we wanted to show where the framework will lead to and what it enables. Suggestions as to how to better structure the patchset is appreciated.
One point of discussion might be whether or not we should use static-keys before accessing the
tcp_option_listto avoid branching and the additional access to thetcp_sock, request-sock, time-wait-sock structure.
The way static keys could be used is that they get incremented each time a socket starts using one of the new TCP-options and decremented when the socket eventually gets destroyed. A caveat of this design would be that if a host keeps on creating/closing these sockets in a sequence, each time we go into the slow path of the static keys occuring potentially a big overhead to update all the jump-labels.
For now we opted for a simple if (unlikely(!hlist_empty(...)) check.Feedback is very welcome!
Thanks,
Mat & ChristophChristoph Paasch (13):
tcp: Write options after the header has been fully done
tcp: Pass sock and skb totcp_options_write
tcp: Allowtcp_fast_parse_optionsto drop segments
tcp_smc: Makesmc_parse_optionsreturn 1 on success
tcp_smc: Make SMC use TCP extra-option framework
tcp_md5: Don't pass along md5-key
tcp_md5: Detect key insidetcp_v4_send_ackinstead of passing it as an
argument
tcp_md5: Detect key insidetcp_v6_send_responseinstead of passing it
as an argument
tcp_md5: Check forTCP_MD5after TCP Timestamps in
tcp_established_options
tcp_md5: Move TCP-MD5 code out of TCP itself
tcp_md5: Usetcp_extra_optionsin output path
tcp_md5: Cleanup TCP-code
tcp_md5: Use TCP extra-options on the input pathMat Martineau (1):
tcp: Register handlers for extra TCP optionsdrivers/infiniband/hw/cxgb4/cm.c | 2 +- include/linux/inet_diag.h | 1 + include/linux/tcp.h | 43 +- include/linux/tcp_md5.h | 39 ++ include/net/inet_sock.h | 3 +- include/net/tcp.h | 212 +++--- net/ipv4/Makefile | 1 + net/ipv4/syncookies.c | 6 +- net/ipv4/tcp.c | 391 ++++++++--- net/ipv4/tcp_diag.c | 81 +-- net/ipv4/tcp_input.c | 137 ++-- net/ipv4/tcp_ipv4.c | 552 ++-------------- net/ipv4/tcp_md5.c | 1359 ++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_minisocks.c | 77 +-- net/ipv4/tcp_output.c | 182 +---- net/ipv6/syncookies.c | 6 +- net/ipv6/tcp_ipv6.c | 386 ++--------- net/smc/af_smc.c | 190 +++++- 18 files changed, 2222 insertions(+), 1446 deletions(-)Quoted 7 lines
create mode 100644
include/linux/tcp_md5.h
create mode 100644net/ipv4/tcp_md5.cSignature · 1 line
2.15.0--
Mat Martineau
Intel OTC
Mat Martineau
Quoted 16 lines · Christoph Paasch
Hello Mat,
Quoted 9 lines · Mat Martineau
Christoph,
Quoted 1 line · Christoph Paasch
Final post to the mptcp-list before submission to netdev :)
Looks good - other than the comments I had on the framework patch earlier today, I don't have any other requests.
Nice! I found a little bug with racing
tcp_check_req()- so I have to fix that. But nothing big. I will build & test once more and then submit.
Quoted 9 lines
Quoted 2 lines
Which net-next commit ID did you base the patch set on? I can push a branch to git.kernel.org, but it would be best to match your git history.
At the moment I'm based on
c30abd5e40dd("Mergegit://git.kernel.org/pub/scm/linux/kernel/git/davem/net")
but I update regularly.Thanks for all your feedback!
And thank you for all your work refactoring, updating, and testing MD5/SMC and the options framework!
I pushed the commits to
https://git.kernel.org/pub/scm/linux/kernel/git/martineau/linux.git/log/?h=md5
That should trigger a build by the 0-Day test infrastructure
(https://01.org/lkp/), which will send email if it finds issues.
--
Mat Martineau
Intel OTC