sctp: add support for SCTP AUTH Information for sendmsg
authorXin Long <lucien.xin@gmail.com>
Wed, 14 Mar 2018 11:05:31 +0000 (19:05 +0800)
committerDavid S. Miller <davem@davemloft.net>
Wed, 14 Mar 2018 17:48:27 +0000 (13:48 -0400)
This patch is to add support for SCTP AUTH Information for sendmsg,
as described in section 5.3.8 of RFC6458.

With this option, you can provide shared key identifier used for
sending the user message.

It's also a necessary send info for sctp_sendv.

Note that it reuses sinfo->sinfo_tsn to indicate if this option is
set and sinfo->sinfo_ssn to save the shkey ID which can be 0.

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/chunk.c
net/sctp/socket.c

index 49ad67bbdbb56096ec49293e13f29bcc1a67b097..012fb3e2f4cf60514c1f9544012ad54c565e0ad7 100644 (file)
@@ -2118,6 +2118,7 @@ struct sctp_cmsgs {
        struct sctp_sndrcvinfo *srinfo;
        struct sctp_sndinfo *sinfo;
        struct sctp_prinfo *prinfo;
+       struct sctp_authinfo *authinfo;
        struct msghdr *addrs_msg;
 };
 
index e94b6d297ad9a4be19715199270dffb9b77da49d..47e781ebde059e89b213d5e5533f90341689a93f 100644 (file)
@@ -273,6 +273,18 @@ struct sctp_prinfo {
        __u32 pr_value;
 };
 
+/* 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
+ *
+ *   This cmsghdr structure specifies SCTP options for sendmsg().
+ *
+ *   cmsg_level    cmsg_type      cmsg_data[]
+ *   ------------  ------------   -------------------
+ *   IPPROTO_SCTP  SCTP_AUTHINFO  struct sctp_authinfo
+ */
+struct sctp_authinfo {
+       __u16 auth_keynumber;
+};
+
 /*
  *  sinfo_flags: 16 bits (unsigned integer)
  *
@@ -310,7 +322,7 @@ 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) */
+       SCTP_AUTHINFO,          /* 5.3.8 SCTP AUTH Information Structure */
 #define SCTP_AUTHINFO  SCTP_AUTHINFO
        SCTP_DSTADDRV4,         /* 5.3.9 SCTP Destination IPv4 Address Structure */
 #define SCTP_DSTADDRV4 SCTP_DSTADDRV4
index 9f28a9aae97655e36220103a902706d3978d76ba..f889a84f264db9f71cd448804c5a4646c729c664 100644 (file)
@@ -206,7 +206,16 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
                        max_data -= SCTP_PAD4(sizeof(struct sctp_auth_chunk) +
                                              hmac_desc->hmac_len);
 
-               shkey = asoc->shkey;
+               if (sinfo->sinfo_tsn &&
+                   sinfo->sinfo_ssn != asoc->active_key_id) {
+                       shkey = sctp_auth_get_shkey(asoc, sinfo->sinfo_ssn);
+                       if (!shkey) {
+                               err = -EINVAL;
+                               goto errout;
+                       }
+               } else {
+                       shkey = asoc->shkey;
+               }
        }
 
        /* Check what's our max considering the above */
index 003a4ad89c015af7385cfc36840a9d67275ab162..9ffdecbc35313d71497be72683bf00638d0db8e4 100644 (file)
@@ -1987,6 +1987,14 @@ static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
 
        if (!cmsgs->srinfo && !cmsgs->prinfo)
                sinfo->sinfo_timetolive = asoc->default_timetolive;
+
+       if (cmsgs->authinfo) {
+               /* Reuse sinfo_tsn to indicate that authinfo was set and
+                * sinfo_ssn to save the keyid on tx path.
+                */
+               sinfo->sinfo_tsn = 1;
+               sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
+       }
 }
 
 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
@@ -7874,6 +7882,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_AUTHINFO:
+                       /* SCTP Socket API Extension
+                        * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
+                        *
+                        * This cmsghdr structure specifies SCTP options for sendmsg().
+                        *
+                        * cmsg_level    cmsg_type      cmsg_data[]
+                        * ------------  ------------   ---------------------
+                        * IPPROTO_SCTP  SCTP_AUTHINFO  struct sctp_authinfo
+                        */
+                       if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
+                               return -EINVAL;
+
+                       cmsgs->authinfo = CMSG_DATA(cmsg);
+                       break;
                case SCTP_DSTADDRV4:
                case SCTP_DSTADDRV6:
                        /* SCTP Socket API Extension