Commit e871cae7 authored by David S. Miller's avatar David S. Miller

Merge branch 'sctp-clean-up-sctp_sendmsg'

Xin Long says:

====================
sctp: clean up sctp_sendmsg

This cleanup mostly does three things:

 - extract some codes into functions to make sendmsg more readable.

 - tidy up some codes to avoid the unnecessary checks.

 - adjust some logic so that it will be easier to add the send flags
   and cmsgs features that I will post after this.

To make it easy to review and to check if the code is compatible with
before, this patchset is to do it step by step in 9 patches.

NOTE:
There will be a conflict when merging
Commit 2277c7cd ("sctp: Add LSM hooks") from selinux tree,
the solution is to:

1. remove all the lines in [B]:

    <<<<<<< HEAD
    [A]
    =======
    [B]
    >>>>>>> 2277c7cd... sctp: Add LSM hooks

2. and apply the following diff-output:

diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index 980621e..d6803c8 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -1686,6 +1686,7 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
 	struct net *net = sock_net(sk);
 	struct sctp_association *asoc;
 	enum sctp_scope scope;
+	struct sctp_af *af;
 	int err = -EINVAL;

 	*tp = NULL;
@@ -1711,6 +1712,22 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,

 	scope = sctp_scope(daddr);

+	/* Label connection socket for first association 1-to-many
+	 * style for client sequence socket()->sendmsg(). This
+	 * needs to be done before sctp_assoc_add_peer() as that will
+	 * set up the initial packet that needs to account for any
+	 * security ip options (CIPSO/CALIPSO) added to the packet.
+	 */
+	af = sctp_get_af_specific(daddr->sa.sa_family);
+	if (!af)
+		return -EINVAL;
+
+	err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
+					 (struct sockaddr *)daddr,
+					 af->sockaddr_len);
+	if (err < 0)
+		return err;
+
 	asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
 	if (!asoc)
 		return -ENOMEM;
====================
Acked-by: default avatarNeil Horman <nhorman@tuxdriver.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents e4e31cf0 0a3920d2
...@@ -1606,396 +1606,214 @@ static int sctp_error(struct sock *sk, int flags, int err) ...@@ -1606,396 +1606,214 @@ static int sctp_error(struct sock *sk, int flags, int err)
static int sctp_msghdr_parse(const struct msghdr *msg, static int sctp_msghdr_parse(const struct msghdr *msg,
struct sctp_cmsgs *cmsgs); struct sctp_cmsgs *cmsgs);
static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
struct sctp_sndrcvinfo *srinfo,
const struct msghdr *msg, size_t msg_len)
{ {
struct net *net = sock_net(sk); __u16 sflags;
struct sctp_sock *sp;
struct sctp_endpoint *ep;
struct sctp_association *new_asoc = NULL, *asoc = NULL;
struct sctp_transport *transport, *chunk_tp;
struct sctp_chunk *chunk;
union sctp_addr to;
struct sockaddr *msg_name = NULL;
struct sctp_sndrcvinfo default_sinfo;
struct sctp_sndrcvinfo *sinfo;
struct sctp_initmsg *sinit;
sctp_assoc_t associd = 0;
struct sctp_cmsgs cmsgs = { NULL };
enum sctp_scope scope;
bool fill_sinfo_ttl = false, wait_connect = false;
struct sctp_datamsg *datamsg;
int msg_flags = msg->msg_flags;
__u16 sinfo_flags = 0;
long timeo;
int err; int err;
err = 0; if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
sp = sctp_sk(sk); return -EPIPE;
ep = sp->ep;
pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
msg, msg_len, ep);
/* We cannot send a message over a TCP-style listening socket. */ if (msg_len > sk->sk_sndbuf)
if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { return -EMSGSIZE;
err = -EPIPE;
goto out_nounlock;
}
/* Parse out the SCTP CMSGs. */ memset(cmsgs, 0, sizeof(*cmsgs));
err = sctp_msghdr_parse(msg, &cmsgs); err = sctp_msghdr_parse(msg, cmsgs);
if (err) { if (err) {
pr_debug("%s: msghdr parse err:%x\n", __func__, err); pr_debug("%s: msghdr parse err:%x\n", __func__, err);
goto out_nounlock; return err;
} }
/* Fetch the destination address for this packet. This memset(srinfo, 0, sizeof(*srinfo));
* address only selects the association--it is not necessarily if (cmsgs->srinfo) {
* the address we will send to. srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
* For a peeled-off socket, msg_name is ignored. srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
*/ srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
int msg_namelen = msg->msg_namelen; srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, }
msg_namelen);
if (err)
return err;
if (msg_namelen > sizeof(to)) if (cmsgs->sinfo) {
msg_namelen = sizeof(to); srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
memcpy(&to, msg->msg_name, msg_namelen); srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
msg_name = msg->msg_name; srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
srinfo->sinfo_context = cmsgs->sinfo->snd_context;
srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
} }
sinit = cmsgs.init; sflags = srinfo->sinfo_flags;
if (cmsgs.sinfo != NULL) { if (!sflags && msg_len)
memset(&default_sinfo, 0, sizeof(default_sinfo)); return 0;
default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
sinfo = &default_sinfo; if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
fill_sinfo_ttl = true; return -EINVAL;
} else {
sinfo = cmsgs.srinfo;
}
/* Did the user specify SNDINFO/SNDRCVINFO? */
if (sinfo) {
sinfo_flags = sinfo->sinfo_flags;
associd = sinfo->sinfo_assoc_id;
}
pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__, if (((sflags & SCTP_EOF) && msg_len > 0) ||
msg_len, sinfo_flags); (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
return -EINVAL;
/* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { return -EINVAL;
err = -EINVAL;
goto out_nounlock;
}
/* If SCTP_EOF is set, no data can be sent. Disallow sending zero return 0;
* length messages when SCTP_EOF|SCTP_ABORT is not set. }
* If SCTP_ABORT is set, the message length could be non zero with
* the msg_iov set to the user abort reason.
*/
if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
(!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
err = -EINVAL;
goto out_nounlock;
}
/* If SCTP_ADDR_OVER is set, there must be an address static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
* specified in msg_name. struct sctp_cmsgs *cmsgs,
*/ union sctp_addr *daddr,
if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { struct sctp_transport **tp)
err = -EINVAL; {
goto out_nounlock; struct sctp_endpoint *ep = sctp_sk(sk)->ep;
} struct net *net = sock_net(sk);
struct sctp_association *asoc;
enum sctp_scope scope;
int err = -EINVAL;
transport = NULL; *tp = NULL;
pr_debug("%s: about to look up association\n", __func__); if (sflags & (SCTP_EOF | SCTP_ABORT))
return -EINVAL;
lock_sock(sk); if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
sctp_sstate(sk, CLOSING)))
return -EADDRNOTAVAIL;
/* If a msg_name has been specified, assume this is to be used. */ if (sctp_endpoint_is_peeled_off(ep, daddr))
if (msg_name) { return -EADDRNOTAVAIL;
/* Look for a matching association on the endpoint. */
asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
/* If we could not find a matching association on the if (!ep->base.bind_addr.port) {
* endpoint, make sure that it is not a TCP-style if (sctp_autobind(sk))
* socket that already has an association or there is return -EAGAIN;
* no peeled-off association on another socket.
*/
if (!asoc &&
((sctp_style(sk, TCP) &&
(sctp_sstate(sk, ESTABLISHED) ||
sctp_sstate(sk, CLOSING))) ||
sctp_endpoint_is_peeled_off(ep, &to))) {
err = -EADDRNOTAVAIL;
goto out_unlock;
}
} else { } else {
asoc = sctp_id2assoc(sk, associd); if (ep->base.bind_addr.port < inet_prot_sock(net) &&
if (!asoc) { !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
err = -EPIPE; return -EACCES;
goto out_unlock;
}
} }
if (asoc) { scope = sctp_scope(daddr);
pr_debug("%s: just looked up association:%p\n", __func__, asoc);
/* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
* socket that has an association in CLOSED state. This can if (!asoc)
* happen when an accepted socket has an association that is return -ENOMEM;
* already CLOSED.
*/
if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
err = -EPIPE;
goto out_unlock;
}
if (sinfo_flags & SCTP_EOF) { if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
pr_debug("%s: shutting down association:%p\n", err = -ENOMEM;
__func__, asoc); goto free;
}
sctp_primitive_SHUTDOWN(net, asoc, NULL); if (cmsgs->init) {
err = 0; struct sctp_initmsg *init = cmsgs->init;
goto out_unlock;
if (init->sinit_num_ostreams) {
__u16 outcnt = init->sinit_num_ostreams;
asoc->c.sinit_num_ostreams = outcnt;
/* outcnt has been changed, need to re-init stream */
err = sctp_stream_init(&asoc->stream, outcnt, 0,
GFP_KERNEL);
if (err)
goto free;
} }
if (sinfo_flags & SCTP_ABORT) {
chunk = sctp_make_abort_user(asoc, msg, msg_len); if (init->sinit_max_instreams)
if (!chunk) { asoc->c.sinit_max_instreams = init->sinit_max_instreams;
err = -ENOMEM;
goto out_unlock;
}
pr_debug("%s: aborting association:%p\n", if (init->sinit_max_attempts)
__func__, asoc); asoc->max_init_attempts = init->sinit_max_attempts;
sctp_primitive_ABORT(net, asoc, chunk); if (init->sinit_max_init_timeo)
err = 0; asoc->max_init_timeo =
goto out_unlock; msecs_to_jiffies(init->sinit_max_init_timeo);
}
} }
/* Do we need to create the association? */ *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
if (!asoc) { if (!*tp) {
pr_debug("%s: there is no association yet\n", __func__); err = -ENOMEM;
goto free;
}
if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { return 0;
err = -EINVAL;
goto out_unlock;
}
/* Check for invalid stream against the stream counts, free:
* either the default or the user specified stream counts. sctp_association_free(asoc);
*/ return err;
if (sinfo) { }
if (!sinit || !sinit->sinit_num_ostreams) {
/* Check against the defaults. */
if (sinfo->sinfo_stream >=
sp->initmsg.sinit_num_ostreams) {
err = -EINVAL;
goto out_unlock;
}
} else {
/* Check against the requested. */
if (sinfo->sinfo_stream >=
sinit->sinit_num_ostreams) {
err = -EINVAL;
goto out_unlock;
}
}
}
/* static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
* API 3.1.2 bind() - UDP Style Syntax __u16 sflags, struct msghdr *msg,
* If a bind() or sctp_bindx() is not called prior to a size_t msg_len)
* sendmsg() call that initiates a new association, the {
* system picks an ephemeral port and will choose an address struct sock *sk = asoc->base.sk;
* set equivalent to binding with a wildcard address. struct net *net = sock_net(sk);
*/
if (!ep->base.bind_addr.port) {
if (sctp_autobind(sk)) {
err = -EAGAIN;
goto out_unlock;
}
} else {
/*
* If an unprivileged user inherits a one-to-many
* style socket with open associations on a privileged
* port, it MAY be permitted to accept new associations,
* but it SHOULD NOT be permitted to open new
* associations.
*/
if (ep->base.bind_addr.port < inet_prot_sock(net) &&
!ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
err = -EACCES;
goto out_unlock;
}
}
scope = sctp_scope(&to); if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); return -EPIPE;
if (!new_asoc) {
err = -ENOMEM;
goto out_unlock;
}
asoc = new_asoc;
err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
if (err < 0) {
err = -ENOMEM;
goto out_free;
}
/* If the SCTP_INIT ancillary data is specified, set all if (sflags & SCTP_EOF) {
* the association init values accordingly. pr_debug("%s: shutting down association:%p\n", __func__, asoc);
*/ sctp_primitive_SHUTDOWN(net, asoc, NULL);
if (sinit) {
if (sinit->sinit_num_ostreams) {
__u16 outcnt = sinit->sinit_num_ostreams;
asoc->c.sinit_num_ostreams = outcnt;
/* outcnt has been changed, so re-init stream */
err = sctp_stream_init(&asoc->stream, outcnt, 0,
GFP_KERNEL);
if (err)
goto out_free;
}
if (sinit->sinit_max_instreams) {
asoc->c.sinit_max_instreams =
sinit->sinit_max_instreams;
}
if (sinit->sinit_max_attempts) {
asoc->max_init_attempts
= sinit->sinit_max_attempts;
}
if (sinit->sinit_max_init_timeo) {
asoc->max_init_timeo =
msecs_to_jiffies(sinit->sinit_max_init_timeo);
}
}
/* Prime the peer's transport structures. */ return 0;
transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
if (!transport) {
err = -ENOMEM;
goto out_free;
}
} }
/* ASSERT: we have a valid association at this point. */ if (sflags & SCTP_ABORT) {
pr_debug("%s: we have a valid association\n", __func__); struct sctp_chunk *chunk;
if (!sinfo) { chunk = sctp_make_abort_user(asoc, msg, msg_len);
/* If the user didn't specify SNDINFO/SNDRCVINFO, make up if (!chunk)
* one with some defaults. return -ENOMEM;
*/
memset(&default_sinfo, 0, sizeof(default_sinfo));
default_sinfo.sinfo_stream = asoc->default_stream;
default_sinfo.sinfo_flags = asoc->default_flags;
default_sinfo.sinfo_ppid = asoc->default_ppid;
default_sinfo.sinfo_context = asoc->default_context;
default_sinfo.sinfo_timetolive = asoc->default_timetolive;
default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
sinfo = &default_sinfo;
} else if (fill_sinfo_ttl) {
/* In case SNDINFO was specified, we still need to fill
* it with a default ttl from the assoc here.
*/
sinfo->sinfo_timetolive = asoc->default_timetolive;
}
/* API 7.1.7, the sndbuf size per association bounds the pr_debug("%s: aborting association:%p\n", __func__, asoc);
* maximum size of data that can be sent in a single send call. sctp_primitive_ABORT(net, asoc, chunk);
*/
if (msg_len > sk->sk_sndbuf) { return 0;
err = -EMSGSIZE;
goto out_free;
} }
if (asoc->pmtu_pending) return 1;
sctp_assoc_pending_pmtu(asoc); }
/* If fragmentation is disabled and the message length exceeds the static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
* association fragmentation point, return EMSGSIZE. The I-D struct msghdr *msg, size_t msg_len,
* does not specify what this error is, but this looks like struct sctp_transport *transport,
* a great fit. struct sctp_sndrcvinfo *sinfo)
*/ {
if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { struct sock *sk = asoc->base.sk;
err = -EMSGSIZE; struct net *net = sock_net(sk);
goto out_free; struct sctp_datamsg *datamsg;
} bool wait_connect = false;
struct sctp_chunk *chunk;
long timeo;
int err;
/* Check for invalid stream. */
if (sinfo->sinfo_stream >= asoc->stream.outcnt) { if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
err = -EINVAL; err = -EINVAL;
goto out_free; goto err;
} }
/* Allocate sctp_stream_out_ext if not already done */
if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) { if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream); err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
if (err) if (err)
goto out_free; goto err;
} }
if (sctp_wspace(asoc) < msg_len) if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc)); err = -EMSGSIZE;
goto err;
timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
if (!sctp_wspace(asoc)) {
/* sk can be changed by peel off when waiting for buf. */
err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
if (err) {
if (err == -ESRCH) {
/* asoc is already dead. */
new_asoc = NULL;
err = -EPIPE;
}
goto out_free;
}
} }
/* If an address is passed with the sendto/sendmsg call, it is used
* to override the primary destination address in the TCP model, or
* when SCTP_ADDR_OVER flag is set in the UDP model.
*/
if ((sctp_style(sk, TCP) && msg_name) ||
(sinfo_flags & SCTP_ADDR_OVER)) {
chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
if (!chunk_tp) {
err = -EINVAL;
goto out_free;
}
} else
chunk_tp = NULL;
/* Auto-connect, if we aren't connected already. */
if (sctp_state(asoc, CLOSED)) { if (sctp_state(asoc, CLOSED)) {
err = sctp_primitive_ASSOCIATE(net, asoc, NULL); err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
if (err < 0) if (err)
goto out_free; goto err;
/* If stream interleave is enabled, wait_connect has to be if (sctp_sk(sk)->strm_interleave) {
* done earlier than data enqueue, as it needs to make data
* or idata according to asoc->intl_enable which is set
* after connection is done.
*/
if (sctp_sk(asoc->base.sk)->strm_interleave) {
timeo = sock_sndtimeo(sk, 0); timeo = sock_sndtimeo(sk, 0);
err = sctp_wait_for_connect(asoc, &timeo); err = sctp_wait_for_connect(asoc, &timeo);
if (err) if (err)
goto out_unlock; goto err;
} else { } else {
wait_connect = true; wait_connect = true;
} }
...@@ -2003,73 +1821,165 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) ...@@ -2003,73 +1821,165 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
pr_debug("%s: we associated primitively\n", __func__); pr_debug("%s: we associated primitively\n", __func__);
} }
/* Break the message into multiple chunks of maximum size. */ if (asoc->pmtu_pending)
sctp_assoc_pending_pmtu(asoc);
if (sctp_wspace(asoc) < msg_len)
sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
if (!sctp_wspace(asoc)) {
timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
if (err)
goto err;
}
datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter); datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
if (IS_ERR(datamsg)) { if (IS_ERR(datamsg)) {
err = PTR_ERR(datamsg); err = PTR_ERR(datamsg);
goto out_free; goto err;
} }
asoc->force_delay = !!(msg->msg_flags & MSG_MORE); asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
/* Now send the (possibly) fragmented message. */
list_for_each_entry(chunk, &datamsg->chunks, frag_list) { list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
sctp_chunk_hold(chunk); sctp_chunk_hold(chunk);
/* Do accounting for the write space. */
sctp_set_owner_w(chunk); sctp_set_owner_w(chunk);
chunk->transport = transport;
chunk->transport = chunk_tp;
} }
/* Send it to the lower layers. Note: all chunks
* must either fail or succeed. The lower layer
* works that way today. Keep it that way or this
* breaks.
*/
err = sctp_primitive_SEND(net, asoc, datamsg); err = sctp_primitive_SEND(net, asoc, datamsg);
/* Did the lower layer accept the chunk? */
if (err) { if (err) {
sctp_datamsg_free(datamsg); sctp_datamsg_free(datamsg);
goto out_free; goto err;
} }
pr_debug("%s: we sent primitively\n", __func__); pr_debug("%s: we sent primitively\n", __func__);
sctp_datamsg_put(datamsg); sctp_datamsg_put(datamsg);
err = msg_len;
if (unlikely(wait_connect)) { if (unlikely(wait_connect)) {
timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT); timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
sctp_wait_for_connect(asoc, &timeo); sctp_wait_for_connect(asoc, &timeo);
} }
/* If we are already past ASSOCIATE, the lower err = msg_len;
* layers are responsible for association cleanup.
*/
goto out_unlock;
out_free: err:
if (new_asoc) return err;
sctp_association_free(asoc); }
out_unlock:
release_sock(sk);
out_nounlock: static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
return sctp_error(sk, msg_flags, err); const struct msghdr *msg,
struct sctp_cmsgs *cmsgs)
{
union sctp_addr *daddr = NULL;
int err;
#if 0 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
do_sock_err: int len = msg->msg_namelen;
if (msg_len)
err = msg_len;
else
err = sock_error(sk);
goto out;
do_interrupted: if (len > sizeof(*daddr))
if (msg_len) len = sizeof(*daddr);
err = msg_len;
goto out; daddr = (union sctp_addr *)msg->msg_name;
#endif /* 0 */
err = sctp_verify_addr(sk, daddr, len);
if (err)
return ERR_PTR(err);
}
return daddr;
}
static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
struct sctp_sndrcvinfo *sinfo,
struct sctp_cmsgs *cmsgs)
{
if (!cmsgs->srinfo && !cmsgs->sinfo) {
sinfo->sinfo_stream = asoc->default_stream;
sinfo->sinfo_ppid = asoc->default_ppid;
sinfo->sinfo_context = asoc->default_context;
sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
}
if (!cmsgs->srinfo)
sinfo->sinfo_timetolive = asoc->default_timetolive;
}
static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
{
struct sctp_endpoint *ep = sctp_sk(sk)->ep;
struct sctp_transport *transport = NULL;
struct sctp_sndrcvinfo _sinfo, *sinfo;
struct sctp_association *asoc;
struct sctp_cmsgs cmsgs;
union sctp_addr *daddr;
bool new = false;
__u16 sflags;
int err;
/* Parse and get snd_info */
err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
if (err)
goto out;
sinfo = &_sinfo;
sflags = sinfo->sinfo_flags;
/* Get daddr from msg */
daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
if (IS_ERR(daddr)) {
err = PTR_ERR(daddr);
goto out;
}
lock_sock(sk);
/* Get and check or create asoc */
if (daddr) {
asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
if (asoc) {
err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
msg_len);
if (err <= 0)
goto out_unlock;
} else {
err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
&transport);
if (err)
goto out_unlock;
asoc = transport->asoc;
new = true;
}
if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
transport = NULL;
} else {
asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
if (!asoc) {
err = -EPIPE;
goto out_unlock;
}
err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
if (err <= 0)
goto out_unlock;
}
/* Update snd_info with the asoc */
sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
/* Send msg to the asoc */
err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
if (err < 0 && err != -ESRCH && new)
sctp_association_free(asoc);
out_unlock:
release_sock(sk);
out:
return sctp_error(sk, msg->msg_flags, err);
} }
/* This is an extended version of skb_pull() that removes the data from the /* This is an extended version of skb_pull() that removes the data from the
......
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