mptcp: ip option handling demo patch
Subsystems (3)
Florian Westphal
cover mptcp: ip option handling demo patch
This patch adds infra to handle ip options and propagate them from the mptcp socket to the subflow sockets, and vice versa.
Given there are IP options that are subflow specific, I am not convinced this is a good idea to begin with, we might need an API that exposes the individual subflows.
I don't plan to continue working on this; this is just so things get archived and we can revisit this later when needed.
Florian Westphal
RFC PATCH 1/3 selftests: add mptcp ip set/getsockopt test program
Useless for the time being as this depends on an ugly hack to expose IP_OPTIONS set/getsockopt.
--- .../selftests/net/mptcp/mptcp_setsockopt.c | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 tools/testing/selftests/net/mptcp/mptcp_setsockopt.c
tools/testing/selftests/net/mptcp/mptcp_setsockopt.c new file +220 -0
@@ -0,0 +1,220 @@ +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> + +#include <arpa/inet.h> + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/wait.h> + +#include <netinet/ip.h> + +#ifndef IPPROTO_MPTCP +#define IPPROTO_MPTCP 262 +#endif + +#define OPT_SIZE 40 + +enum test_ipopt_state { +TEST_OPTS_NONE , +TEST_OPTS_SET , +TEST_OPTS_RX_SET , +}; + +static void die (const char *s) +{ +perror (s); +exit (1 ); +} + +static void xgetsockopt (int sockfd,int level,int optname, +void *optval,socklen_t *optlen) +{ +int err =getsockopt (sockfd, level, optname, optval, optlen); + +if (err <0 ) +die ("getsockopt" ); +} + +static void xsetsockopt (int sockfd,int level,int optname, +const void *optval,socklen_t optlen) +{ +int err =setsockopt (sockfd, level, optname, optval, optlen); + +if (err <0 ) +die ("setsockopt" ); +} + +static void xbind (int fd,struct sockaddr_in *sin,socklen_t l) +{ +int err =bind (fd, (void *)sin, l); + +if (err <0 ) +die ("bind" ); +} + +static void set_ipopts (int fd) +{ +char options[OPT_SIZE ]; +uint8_t olen =5 ; + +memset (options,0 ,sizeof (options)); + + options[0 ] =IPOPT_TS |IPOPT_TS_TSONLY ; + options[1 ] =sizeof (options); + options[2 ] = olen; + +xsetsockopt (fd,SOL_IP ,IP_OPTIONS , options,sizeof (options)); +} + +static void check_ip_opts (int fd,enum test_ipopt_state state, +const char *prefix) +{ +socklen_t bufsz =128 ; +char buf[128 ]; + +memset (buf,0 ,sizeof (buf)); + +xgetsockopt (fd,SOL_IP ,IP_OPTIONS , buf, &bufsz); + +if (state ==TEST_OPTS_NONE ) { +printf ("%s: expect IP_OPTIONS are cleared " , prefix); +if (bufsz ==0 ) +puts ("[ OK ]" ); +else +printf ("but got %d [ FAIL ]\n" , (int )bufsz); + +return ; + } + +printf ("%s: expect IP_OPTIONS are set " , prefix); +if (bufsz ==OPT_SIZE && + buf[0 ] == (IPOPT_TS |IPOPT_TS_TSONLY ) && + buf[1 ] ==OPT_SIZE ) +puts ("[ OK ]" ); +else if (bufsz ==0 ) +puts ("[ FAIL ]" ); +else +printf (" -- opt length %d, 0x%x/0x%x/0x%x [ FAIL ]\n" , (int )bufsz, + buf[0 ], buf[1 ], buf[2 ]); +} + +static int server_fd (void ) +{ +int fd =socket (AF_INET ,SOCK_STREAM ,IPPROTO_MPTCP ); +struct sockaddr_in sin; +static const int on =1 ; + +if (fd <0 ) +die ("socket" ); + +if (alarm (9 )) +die ("alarm" ); + +xsetsockopt (fd,SOL_SOCKET ,SO_REUSEPORT , &on,sizeof (on)); + + sin.sin_family =AF_INET ; + sin.sin_addr.s_addr =inet_addr ("127.0.0.1" ); + sin.sin_port =htons (0x4242 ); +xbind (fd, &sin,sizeof (sin)); + +listen (fd,10 ); + +return fd; +} + +static void server_handle_client (int fd,enum test_ipopt_state opts, +const char *testname) +{ +char buf[8192 ]; +int fdc, err; + +if (opts ==TEST_OPTS_SET ) +set_ipopts (fd); + + fdc =accept (fd,NULL ,NULL ); +if (fdc <0 ) +die ("accept" ); + +if (opts ==TEST_OPTS_SET ) +set_ipopts (fdc); + +do { + err = (int )read (fdc, buf,sizeof (buf)); +if (err <0 ) +die ("read" ); +write (fdc,"." ,1 ); + }while (err >0 ); + +check_ip_opts (fdc, opts, testname); +close (fdc); +} + +static void client (enum test_ipopt_state state,const char *testname) +{ +int err, fd =socket (AF_INET ,SOCK_STREAM ,IPPROTO_MPTCP ); +struct sockaddr_in sin; +char buf[64 ];; + +if (fd <0 ) +die ("socket" ); + + sin.sin_family =AF_INET ; + sin.sin_addr.s_addr =inet_addr ("127.0.0.1" ); + sin.sin_port =htons (0x4242 ); + +if (state ==TEST_OPTS_SET ) +set_ipopts (fd); + + err =connect (fd, (void *)&sin,sizeof (sin)); +if (err <0 ) +die ("connect" ); + +write (fd,"Test" ,sizeof ("Test" )); +read (fd, buf,sizeof (buf)); + +check_ip_opts (fd, state, testname); +close (fd); +} + +int main (void ) +{ +pid_t ret, pid =fork (); +int wstatus; + +if (pid <0 ) +die ("fork" ); + +if (pid ==0 ) { +int fd =server_fd (); + +if (fd <0 ) +die ("server_fd" ); + +server_handle_client (fd,TEST_OPTS_NONE ,"srv1" ); +server_handle_client (fd,TEST_OPTS_RX_SET ,"srv2" ); +server_handle_client (fd,TEST_OPTS_SET ,"srv3" ); + +close (fd); +exit (0 ); + } + +sleep (1 ); +client (TEST_OPTS_NONE ,"client1" ); +client (TEST_OPTS_SET ,"client2" ); +client (TEST_OPTS_NONE ,"client3" ); + +do { + ret =wait (&wstatus); + }while (ret != pid); + +if (WIFEXITED (wstatus)) +return WEXITSTATUS (wstatus); + +fprintf (stderr,"Bad return value from %d (signaled %d)\n" , (int )pid,WIFSIGNALED (wstatus)); +return 111 ; +}
Signature · 1 line
2.24.1
Florian Westphal
RFC PATCH 2/3 mptcp: propagate mptcp-socket level ip options to the subflows
In reply to Florian WestphalUpper layers typically update inet_opts and icsk_ext_hdr_len, then call the sync_mss callback.
We will need to propagate this to the individual subflows.
This is modeled after tcp semantics, i.e. if there are ip options on incoming connection, these will be propagated to the mptcp socket. This is questionable, because ip options may be path specific (e.g. source routing), so it might make sense to NOT copy them to the mptcp socket.
--- net/mptcp/protocol.c | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+)
net/mptcp/protocol.c +78 -0
@@ -74,10 +74,41 @@ static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)return !msk->first; } +static void mptcp_copy_inet_opts (struct sock *nsk,const struct sock *sk, +bool nsk_locked,bool sk_locked) +{ +struct ip_options_rcu *inet_opt; +unsigned int len; + + inet_opt =rcu_dereference_check (inet_sk (sk)->inet_opt, sk_locked); +if (!inet_opt) +return ; + + len =sizeof (*inet_opt) + inet_opt->opt.optlen; + inet_opt =kmemdup (inet_opt, len,GFP_ATOMIC ); +if (inet_opt) { +struct ip_options_rcu *old; + + old =rcu_replace_pointer (inet_sk (nsk)->inet_opt, + inet_opt, nsk_locked); +kfree_rcu (old, rcu); + } +} + +static void mptcp_sync_mss_one (struct sock *ssk,const struct sock *sk) +{ +mptcp_copy_inet_opts (ssk, sk,lockdep_sock_is_held (ssk), +lockdep_sock_is_held (sk)); + +/* don't use msk->icsk_pmtu_cookie; mtu is per-ssk */ +tcp_sync_mss (ssk,inet_csk (ssk)->icsk_pmtu_cookie); +} +static struct socket *__mptcp_socket_create (struct mptcp_sock *msk,int state) {struct mptcp_subflow_context *subflow;struct sock *sk = (struct sock *)msk; +struct inet_connection_sock *icsk;struct socket *ssock;int err;
@@ -95,6 +126,9 @@ static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state) msk->first = ssock->sk; msk->subflow = ssock; subflow =mptcp_subflow_ctx (ssock->sk); + icsk =inet_csk (ssock->sk); + icsk->icsk_ext_hdr_len =inet_csk (sk)->icsk_ext_hdr_len; +mptcp_copy_inet_opts (ssock->sk, sk, true,lockdep_sock_is_held (sk));list_add (&subflow->node, &msk->conn_list); subflow->request_mptcp =1 ;
@@ -668,6 +702,23 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,static unsigned int mptcp_sync_mss (struct sock *sk,u32 pmtu) { +u16 ext_hdr_len =inet_csk (sk)->icsk_ext_hdr_len; +struct mptcp_sock *msk =mptcp_sk (sk); +struct mptcp_subflow_context *subflow; + +sock_owned_by_me ((const struct sock *)msk); + +inet_csk (sk)->icsk_pmtu_cookie = pmtu; + +list_for_each_entry (subflow, &msk->conn_list, node) { +struct sock *ssk =mptcp_subflow_tcp_sock (subflow); + +lock_sock (ssk); +inet_csk (ssk)->icsk_ext_hdr_len = ext_hdr_len; +mptcp_sync_mss_one (ssk, (struct sock *)msk); +release_sock (ssk); + } +return 0 ; }
@@ -808,9 +859,17 @@ static struct sock *mptcp_sk_clone_lock(const struct sock *sk)inet_sk (nsk)->pinet6 =mptcp_inet6_sk (nsk);#endif +RCU_INIT_POINTER (inet_sk (nsk)->inet_opt,NULL ); +mptcp_copy_inet_opts (nsk, sk,lockdep_sock_is_held (nsk), +lockdep_sock_is_held (sk));return nsk; } +static bool mptcp_sk_has_ipopts (struct sock *sk) +{ +return rcu_access_pointer (inet_sk (sk)->inet_opt) !=NULL ; +} +static struct sock *mptcp_accept (struct sock *sk,int flags,int *err,bool kern) {
@@ -835,6 +894,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,struct mptcp_subflow_context *subflow;struct sock *new_mptcp_sock;struct sock *ssk = newsk; +bool sk_has_ip_opts;u64 ack_seq; subflow =mptcp_subflow_ctx (newsk);
@@ -871,6 +931,17 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, } newsk = new_mptcp_sock;mptcp_copy_inaddrs (newsk, ssk); + + sk_has_ip_opts =mptcp_sk_has_ipopts (newsk); + +if (!sk_has_ip_opts &&mptcp_sk_has_ipopts (ssk)) { +rcu_read_lock (); +mptcp_copy_inet_opts (newsk, ssk, +lockdep_sock_is_held (newsk), +lockdep_sock_is_held (ssk)); +rcu_read_unlock (); + } +list_add (&subflow->node, &msk->conn_list);/* will be fully established at mptcp_stream_accept()
@@ -890,6 +961,13 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, subflow->conn = new_mptcp_sock;if (unlikely (!skb_queue_empty (&ssk->sk_receive_queue)))mptcp_subflow_data_available (ssk); + +if (sk_has_ip_opts) { +rcu_read_lock (); +mptcp_sync_mss_one (ssk, new_mptcp_sock); +rcu_read_unlock (); + } +release_sock (ssk); }
Signature · 1 line
2.24.1
Florian Westphal
RFC PATCH 3/3 testing only: allow IP_OPTIONS set/getsockopt
In reply to Florian WestphalThis is WRONG, it will not work with ipv6. We would need to add approprate callbacks for this.
--- net/mptcp/protocol.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +29 -1
@@ -982,6 +982,24 @@ static void mptcp_destroy(struct sock *sk)__skb_ext_put (msk->cached_ext); } +static bool is_ok (int level,int optname) +{ +if (level ==SOL_IP && optname ==IP_OPTIONS ) +return true; + +return false; +} + +static int do_mptcp_setsockopt (struct sock *sk,int level,int optname, +char __user *optval,unsigned int optlen) +{ +if (is_ok (level, optname)) +return ip_setsockopt (sk, level, optname, + optval, optlen); + +return -EOPNOTSUPP ; +} +static int mptcp_setsockopt (struct sock *sk,int level,int optname,char __user *optval,unsigned int optlen) {
@@ -1004,6 +1022,16 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,release_sock (sk); +return do_mptcp_setsockopt (sk, level, optname, optval, optlen); +} + +static int do_mptcp_getsockopt (struct sock *sk,int level,int optname, +char __user *optval,int __user *option) +{ +if (is_ok (level, optname)) +return ip_getsockopt (sk, level, optname, + optval, option); +return -EOPNOTSUPP ; }
@@ -1029,7 +1057,7 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,release_sock (sk); -return -EOPNOTSUPP ; +return do_mptcp_getsockopt (sk, level, optname, optval, option); }#define MPTCP_DEFERRED_ALL TCPF_DELACK_TIMER_DEFERRED
Signature · 1 line
2.24.1