Commit 2cb5c8e3 authored by Neil Horman's avatar Neil Horman Committed by David S. Miller

sctp: Add peeloff-flags socket option

Based on a request raised on the sctp devel list, there is a need to
augment the sctp_peeloff operation while specifying the O_CLOEXEC and
O_NONBLOCK flags (simmilar to the socket syscall).  Since modifying the
SCTP_SOCKOPT_PEELOFF socket option would break user space ABI for existing
programs, this patch creates a new socket option
SCTP_SOCKOPT_PEELOFF_FLAGS, which accepts a third flags parameter to
allow atomic assignment of the socket descriptor flags.

Tested successfully by myself and the requestor
Signed-off-by: default avatarNeil Horman <nhorman@tuxdriver.com>
CC: Vlad Yasevich <vyasevich@gmail.com>
CC: "David S. Miller" <davem@davemloft.net>
CC: Andreas Steinmetz <ast@domdv.de>
CC: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Acked-by: default avatarMarcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 15324b25
...@@ -121,6 +121,7 @@ typedef __s32 sctp_assoc_t; ...@@ -121,6 +121,7 @@ typedef __s32 sctp_assoc_t;
#define SCTP_RESET_STREAMS 119 #define SCTP_RESET_STREAMS 119
#define SCTP_RESET_ASSOC 120 #define SCTP_RESET_ASSOC 120
#define SCTP_ADD_STREAMS 121 #define SCTP_ADD_STREAMS 121
#define SCTP_SOCKOPT_PEELOFF_FLAGS 122
/* PR-SCTP policies */ /* PR-SCTP policies */
#define SCTP_PR_SCTP_NONE 0x0000 #define SCTP_PR_SCTP_NONE 0x0000
...@@ -978,6 +979,11 @@ typedef struct { ...@@ -978,6 +979,11 @@ typedef struct {
int sd; int sd;
} sctp_peeloff_arg_t; } sctp_peeloff_arg_t;
typedef struct {
sctp_peeloff_arg_t p_arg;
unsigned flags;
} sctp_peeloff_flags_arg_t;
/* /*
* Peer Address Thresholds socket option * Peer Address Thresholds socket option
*/ */
......
...@@ -4933,11 +4933,47 @@ int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp) ...@@ -4933,11 +4933,47 @@ int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
} }
EXPORT_SYMBOL(sctp_do_peeloff); EXPORT_SYMBOL(sctp_do_peeloff);
static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
struct file **newfile, unsigned flags)
{
struct socket *newsock;
int retval;
retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
if (retval < 0)
goto out;
/* Map the socket to an unused fd that can be returned to the user. */
retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
if (retval < 0) {
sock_release(newsock);
goto out;
}
*newfile = sock_alloc_file(newsock, 0, NULL);
if (IS_ERR(*newfile)) {
put_unused_fd(retval);
sock_release(newsock);
retval = PTR_ERR(*newfile);
*newfile = NULL;
return retval;
}
pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
retval);
peeloff->sd = retval;
if (flags & SOCK_NONBLOCK)
(*newfile)->f_flags |= O_NONBLOCK;
out:
return retval;
}
static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
{ {
sctp_peeloff_arg_t peeloff; sctp_peeloff_arg_t peeloff;
struct socket *newsock; struct file *newfile = NULL;
struct file *newfile;
int retval = 0; int retval = 0;
if (len < sizeof(sctp_peeloff_arg_t)) if (len < sizeof(sctp_peeloff_arg_t))
...@@ -4946,26 +4982,44 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval ...@@ -4946,26 +4982,44 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval
if (copy_from_user(&peeloff, optval, len)) if (copy_from_user(&peeloff, optval, len))
return -EFAULT; return -EFAULT;
retval = sctp_do_peeloff(sk, peeloff.associd, &newsock); retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
if (retval < 0) if (retval < 0)
goto out; goto out;
/* Map the socket to an unused fd that can be returned to the user. */ /* Return the fd mapped to the new socket. */
retval = get_unused_fd_flags(0); if (put_user(len, optlen)) {
if (retval < 0) { fput(newfile);
sock_release(newsock); put_unused_fd(retval);
goto out; return -EFAULT;
} }
newfile = sock_alloc_file(newsock, 0, NULL); if (copy_to_user(optval, &peeloff, len)) {
if (IS_ERR(newfile)) { fput(newfile);
put_unused_fd(retval); put_unused_fd(retval);
sock_release(newsock); return -EFAULT;
return PTR_ERR(newfile);
} }
fd_install(retval, newfile);
out:
return retval;
}
pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
retval); char __user *optval, int __user *optlen)
{
sctp_peeloff_flags_arg_t peeloff;
struct file *newfile = NULL;
int retval = 0;
if (len < sizeof(sctp_peeloff_flags_arg_t))
return -EINVAL;
len = sizeof(sctp_peeloff_flags_arg_t);
if (copy_from_user(&peeloff, optval, len))
return -EFAULT;
retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
&newfile, peeloff.flags);
if (retval < 0)
goto out;
/* Return the fd mapped to the new socket. */ /* Return the fd mapped to the new socket. */
if (put_user(len, optlen)) { if (put_user(len, optlen)) {
...@@ -4973,7 +5027,7 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval ...@@ -4973,7 +5027,7 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval
put_unused_fd(retval); put_unused_fd(retval);
return -EFAULT; return -EFAULT;
} }
peeloff.sd = retval;
if (copy_to_user(optval, &peeloff, len)) { if (copy_to_user(optval, &peeloff, len)) {
fput(newfile); fput(newfile);
put_unused_fd(retval); put_unused_fd(retval);
...@@ -6759,6 +6813,9 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname, ...@@ -6759,6 +6813,9 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname,
case SCTP_SOCKOPT_PEELOFF: case SCTP_SOCKOPT_PEELOFF:
retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
break; break;
case SCTP_SOCKOPT_PEELOFF_FLAGS:
retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
break;
case SCTP_PEER_ADDR_PARAMS: case SCTP_PEER_ADDR_PARAMS:
retval = sctp_getsockopt_peer_addr_params(sk, len, optval, retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
optlen); optlen);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment