]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
sctp: add support for SCTP_DSTADDRV4/6 Information for sendmsg
authorXin Long <lucien.xin@gmail.com>
Mon, 5 Mar 2018 12:44:19 +0000 (20:44 +0800)
committerDavid S. Miller <davem@davemloft.net>
Wed, 7 Mar 2018 15:55:29 +0000 (10:55 -0500)
This patch is to add support for Destination IPv4/6 Address options
for sendmsg, as described in section 5.3.9/10 of RFC6458.

With this option, you can provide more than one destination addrs
to sendmsg when creating asoc, like sctp_connectx.

It's also a necessary send info for sctp_sendv.

Signed-off-by: Xin Long <lucien.xin@gmail.com>
Acked-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/sctp/structs.h
include/uapi/linux/sctp.h
net/sctp/socket.c

index d40a2a329888e8646bd156c4c69712d0c46f4967..ec6e46b7e11956c3fc06606fb30871b35de442cd 100644 (file)
@@ -2113,6 +2113,7 @@ struct sctp_cmsgs {
        struct sctp_sndrcvinfo *srinfo;
        struct sctp_sndinfo *sinfo;
        struct sctp_prinfo *prinfo;
+       struct msghdr *addrs_msg;
 };
 
 /* Structure for tracking memory objects */
index 0dd1f82a4fa8922522f8be0d83ab218f4295eb6c..a1bc3509803306a5d3a314a53c7dba922fe1eb10 100644 (file)
@@ -308,6 +308,12 @@ typedef enum sctp_cmsg_type {
 #define SCTP_NXTINFO   SCTP_NXTINFO
        SCTP_PRINFO,            /* 5.3.7 SCTP PR-SCTP Information Structure */
 #define SCTP_PRINFO    SCTP_PRINFO
+       SCTP_AUTHINFO,          /* 5.3.8 SCTP AUTH Information Structure (RESERVED) */
+#define SCTP_AUTHINFO  SCTP_AUTHINFO
+       SCTP_DSTADDRV4,         /* 5.3.9 SCTP Destination IPv4 Address Structure */
+#define SCTP_DSTADDRV4 SCTP_DSTADDRV4
+       SCTP_DSTADDRV6,         /* 5.3.10 SCTP Destination IPv6 Address Structure */
+#define SCTP_DSTADDRV6 SCTP_DSTADDRV6
 } sctp_cmsg_t;
 
 /*
index fdde697b37e735d7fa6acb24ed4260f93c86a50d..067b57a330b129e2ce7e8218e44193d3e5e8561f 100644 (file)
@@ -1676,6 +1676,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 cmsghdr *cmsg;
        int err = -EINVAL;
 
        *tp = NULL;
@@ -1741,6 +1742,67 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
                goto free;
        }
 
+       if (!cmsgs->addrs_msg)
+               return 0;
+
+       /* sendv addr list parse */
+       for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
+               struct sctp_transport *transport;
+               struct sctp_association *old;
+               union sctp_addr _daddr;
+               int dlen;
+
+               if (cmsg->cmsg_level != IPPROTO_SCTP ||
+                   (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
+                    cmsg->cmsg_type != SCTP_DSTADDRV6))
+                       continue;
+
+               daddr = &_daddr;
+               memset(daddr, 0, sizeof(*daddr));
+               dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
+               if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
+                       if (dlen < sizeof(struct in_addr))
+                               goto free;
+
+                       dlen = sizeof(struct in_addr);
+                       daddr->v4.sin_family = AF_INET;
+                       daddr->v4.sin_port = htons(asoc->peer.port);
+                       memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
+               } else {
+                       if (dlen < sizeof(struct in6_addr))
+                               goto free;
+
+                       dlen = sizeof(struct in6_addr);
+                       daddr->v6.sin6_family = AF_INET6;
+                       daddr->v6.sin6_port = htons(asoc->peer.port);
+                       memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
+               }
+               err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
+               if (err)
+                       goto free;
+
+               old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
+               if (old && old != asoc) {
+                       if (old->state >= SCTP_STATE_ESTABLISHED)
+                               err = -EISCONN;
+                       else
+                               err = -EALREADY;
+                       goto free;
+               }
+
+               if (sctp_endpoint_is_peeled_off(ep, daddr)) {
+                       err = -EADDRNOTAVAIL;
+                       goto free;
+               }
+
+               transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
+                                               SCTP_UNKNOWN);
+               if (!transport) {
+                       err = -ENOMEM;
+                       goto free;
+               }
+       }
+
        return 0;
 
 free:
@@ -7778,6 +7840,21 @@ static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
                        if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
                                cmsgs->prinfo->pr_value = 0;
                        break;
+               case SCTP_DSTADDRV4:
+               case SCTP_DSTADDRV6:
+                       /* SCTP Socket API Extension
+                        * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
+                        *
+                        * This cmsghdr structure specifies SCTP options for sendmsg().
+                        *
+                        * cmsg_level    cmsg_type         cmsg_data[]
+                        * ------------  ------------   ---------------------
+                        * IPPROTO_SCTP  SCTP_DSTADDRV4 struct in_addr
+                        * ------------  ------------   ---------------------
+                        * IPPROTO_SCTP  SCTP_DSTADDRV6 struct in6_addr
+                        */
+                       cmsgs->addrs_msg = my_msg;
+                       break;
                default:
                        return -EINVAL;
                }