Skip to content

Commit 2347c80

Browse files
gejrdavem330
authored andcommitted
net: sctp: implement rfc6458, 5.3.6. SCTP_NXTINFO cmsg support
This patch implements section 5.3.6. of RFC6458, that is, support for 'SCTP Next Receive Information Structure' (SCTP_NXTINFO) which is placed into ancillary data cmsghdr structure for each recvmsg() call, if this information is already available when delivering the current message. This option can be enabled/disabled via setsockopt(2) on SOL_SCTP level by setting an int value with 1/0 for SCTP_RECVNXTINFO in user space applications as per RFC6458, section 8.1.30. The sctp_nxtinfo structure is defined as per RFC as below ... struct sctp_nxtinfo { uint16_t nxt_sid; uint16_t nxt_flags; uint32_t nxt_ppid; uint32_t nxt_length; sctp_assoc_t nxt_assoc_id; }; ... and provided under cmsg_level IPPROTO_SCTP, cmsg_type SCTP_NXTINFO, while cmsg_data[] contains struct sctp_nxtinfo. Joint work with Daniel Borkmann. Signed-off-by: Geir Ola Vaagland <geirola@gmail.com> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0d3a421 commit 2347c80

6 files changed

Lines changed: 125 additions & 23 deletions

File tree

‎include/net/sctp/sctp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void sctp_copy_sock(struct sock *newsk, struct sock *sk,
109109
struct sctp_association *asoc);
110110
extern struct percpu_counter sctp_sockets_allocated;
111111
int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
112+
struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
112113

113114
/*
114115
* sctp/primitive.c

‎include/net/sctp/structs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ struct sctp_sock {
218218
__u32 adaptation_ind;
219219
__u32 pd_point;
220220
__u8 recvrcvinfo;
221+
__u8 recvnxtinfo;
221222

222223
atomic_t pd_mode;
223224
/* Receive to here while partial delivery is in effect. */

‎include/net/sctp/ulpevent.h‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
132132
struct msghdr *);
133133
void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
134134
struct msghdr *);
135+
void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
136+
struct msghdr *, struct sock *sk);
135137

136138
__u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event);
137139

@@ -158,10 +160,3 @@ static inline int sctp_ulpevent_is_enabled(const struct sctp_ulpevent *event,
158160
}
159161

160162
#endif /* __sctp_ulpevent_h__ */
161-
162-
163-
164-
165-
166-
167-

‎include/uapi/linux/sctp.h‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef __s32 sctp_assoc_t;
9696
#define SCTP_AUTO_ASCONF 30
9797
#define SCTP_PEER_ADDR_THLDS 31
9898
#define SCTP_RECVRCVINFO 32
99+
#define SCTP_RECVNXTINFO 33
99100

100101
/* Internal Socket Options. Some of the sctp library functions are
101102
* implemented using these socket options.
@@ -111,6 +112,13 @@ typedef __s32 sctp_assoc_t;
111112
#define SCTP_SOCKOPT_CONNECTX3 111 /* CONNECTX requests (updated) */
112113
#define SCTP_GET_ASSOC_STATS 112 /* Read only */
113114

115+
/* These are bit fields for msghdr->msg_flags. See section 5.1. */
116+
/* On user space Linux, these live in <bits/socket.h> as an enum. */
117+
enum sctp_msg_flags {
118+
MSG_NOTIFICATION = 0x8000,
119+
#define MSG_NOTIFICATION MSG_NOTIFICATION
120+
};
121+
114122
/* 5.3.1 SCTP Initiation Structure (SCTP_INIT)
115123
*
116124
* This cmsghdr structure provides information for initializing new
@@ -187,18 +195,38 @@ struct sctp_rcvinfo {
187195
sctp_assoc_t rcv_assoc_id;
188196
};
189197

198+
/* 5.3.6 SCTP Next Receive Information Structure (SCTP_NXTINFO)
199+
*
200+
* This cmsghdr structure describes SCTP receive information
201+
* of the next message that will be delivered through recvmsg()
202+
* if this information is already available when delivering
203+
* the current message.
204+
*
205+
* cmsg_level cmsg_type cmsg_data[]
206+
* ------------ ------------ -------------------
207+
* IPPROTO_SCTP SCTP_NXTINFO struct sctp_nxtinfo
208+
*/
209+
struct sctp_nxtinfo {
210+
__u16 nxt_sid;
211+
__u16 nxt_flags;
212+
__u32 nxt_ppid;
213+
__u32 nxt_length;
214+
sctp_assoc_t nxt_assoc_id;
215+
};
216+
190217
/*
191218
* sinfo_flags: 16 bits (unsigned integer)
192219
*
193220
* This field may contain any of the following flags and is composed of
194221
* a bitwise OR of these values.
195222
*/
196223
enum sctp_sinfo_flags {
197-
SCTP_UNORDERED = 1, /* Send/receive message unordered. */
198-
SCTP_ADDR_OVER = 2, /* Override the primary destination. */
199-
SCTP_ABORT=4, /* Send an ABORT message to the peer. */
200-
SCTP_SACK_IMMEDIATELY = 8, /* SACK should be sent without delay */
201-
SCTP_EOF=MSG_FIN, /* Initiate graceful shutdown process. */
224+
SCTP_UNORDERED = (1 << 0), /* Send/receive message unordered. */
225+
SCTP_ADDR_OVER = (1 << 1), /* Override the primary destination. */
226+
SCTP_ABORT = (1 << 2), /* Send an ABORT message to the peer. */
227+
SCTP_SACK_IMMEDIATELY = (1 << 3), /* SACK should be sent without delay. */
228+
SCTP_NOTIFICATION = MSG_NOTIFICATION, /* Next message is not user msg but notification. */
229+
SCTP_EOF = MSG_FIN, /* Initiate graceful shutdown process. */
202230
};
203231

204232
typedef union {
@@ -217,6 +245,8 @@ typedef enum sctp_cmsg_type {
217245
#define SCTP_SNDINFO SCTP_SNDINFO
218246
SCTP_RCVINFO, /* 5.3.5 SCTP Receive Information Structure */
219247
#define SCTP_RCVINFO SCTP_RCVINFO
248+
SCTP_NXTINFO, /* 5.3.6 SCTP Next Receive Information Structure */
249+
#define SCTP_NXTINFO SCTP_NXTINFO
220250
} sctp_cmsg_t;
221251

222252
/*
@@ -844,13 +874,6 @@ struct sctp_assoc_stats {
844874
__u64 sas_ictrlchunks; /* Control chunks received */
845875
};
846876

847-
/* These are bit fields for msghdr->msg_flags. See section 5.1. */
848-
/* On user space Linux, these live in <bits/socket.h> as an enum. */
849-
enum sctp_msg_flags {
850-
MSG_NOTIFICATION = 0x8000,
851-
#define MSG_NOTIFICATION MSG_NOTIFICATION
852-
};
853-
854877
/*
855878
* 8.1 sctp_bindx()
856879
*

‎net/sctp/socket.c‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,6 @@ static int sctp_skb_pull(struct sk_buff *skb, int len)
20602060
* flags - flags sent or received with the user message, see Section
20612061
* 5 for complete description of the flags.
20622062
*/
2063-
static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2064-
20652063
static int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
20662064
struct msghdr *msg, size_t len, int noblock,
20672065
int flags, int *addr_len)
@@ -2112,6 +2110,9 @@ static int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
21122110
sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
21132111
}
21142112

2113+
/* Check if we allow SCTP_NXTINFO. */
2114+
if (sp->recvnxtinfo)
2115+
sctp_ulpevent_read_nxtinfo(event, msg, sk);
21152116
/* Check if we allow SCTP_RCVINFO. */
21162117
if (sp->recvrcvinfo)
21172118
sctp_ulpevent_read_rcvinfo(event, msg);
@@ -3611,6 +3612,22 @@ static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
36113612
return 0;
36123613
}
36133614

3615+
static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3616+
char __user *optval,
3617+
unsigned int optlen)
3618+
{
3619+
int val;
3620+
3621+
if (optlen < sizeof(int))
3622+
return -EINVAL;
3623+
if (get_user(val, (int __user *) optval))
3624+
return -EFAULT;
3625+
3626+
sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3627+
3628+
return 0;
3629+
}
3630+
36143631
/* API 6.2 setsockopt(), getsockopt()
36153632
*
36163633
* Applications use setsockopt() and getsockopt() to set or retrieve
@@ -3765,6 +3782,9 @@ static int sctp_setsockopt(struct sock *sk, int level, int optname,
37653782
case SCTP_RECVRCVINFO:
37663783
retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
37673784
break;
3785+
case SCTP_RECVNXTINFO:
3786+
retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
3787+
break;
37683788
default:
37693789
retval = -ENOPROTOOPT;
37703790
break;
@@ -4012,6 +4032,7 @@ static int sctp_init_sock(struct sock *sk)
40124032
sp->nodelay = 0;
40134033

40144034
sp->recvrcvinfo = 0;
4035+
sp->recvnxtinfo = 0;
40154036

40164037
/* Enable by default. */
40174038
sp->v4mapped = 1;
@@ -5814,6 +5835,26 @@ static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
58145835
return 0;
58155836
}
58165837

5838+
static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
5839+
char __user *optval,
5840+
int __user *optlen)
5841+
{
5842+
int val = 0;
5843+
5844+
if (len < sizeof(int))
5845+
return -EINVAL;
5846+
5847+
len = sizeof(int);
5848+
if (sctp_sk(sk)->recvnxtinfo)
5849+
val = 1;
5850+
if (put_user(len, optlen))
5851+
return -EFAULT;
5852+
if (copy_to_user(optval, &val, len))
5853+
return -EFAULT;
5854+
5855+
return 0;
5856+
}
5857+
58175858
static int sctp_getsockopt(struct sock *sk, int level, int optname,
58185859
char __user *optval, int __user *optlen)
58195860
{
@@ -5960,6 +6001,9 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname,
59606001
case SCTP_RECVRCVINFO:
59616002
retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
59626003
break;
6004+
case SCTP_RECVNXTINFO:
6005+
retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
6006+
break;
59636007
default:
59646008
retval = -ENOPROTOOPT;
59656009
break;
@@ -6602,8 +6646,8 @@ static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
66026646
* Note: This is pretty much the same routine as in core/datagram.c
66036647
* with a few changes to make lksctp work.
66046648
*/
6605-
static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6606-
int noblock, int *err)
6649+
struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6650+
int noblock, int *err)
66076651
{
66086652
int error;
66096653
struct sk_buff *skb;

‎net/sctp/ulpevent.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,44 @@ void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
911911
sizeof(rinfo), &rinfo);
912912
}
913913

914+
/* RFC6458, Section 5.3.6. SCTP Next Receive Information Structure
915+
* (SCTP_NXTINFO)
916+
*/
917+
static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
918+
struct msghdr *msghdr,
919+
const struct sk_buff *skb)
920+
{
921+
struct sctp_nxtinfo nxtinfo;
922+
923+
memset(&nxtinfo, 0, sizeof(nxtinfo));
924+
nxtinfo.nxt_sid = event->stream;
925+
nxtinfo.nxt_ppid = event->ppid;
926+
nxtinfo.nxt_flags = event->flags;
927+
if (sctp_ulpevent_is_notification(event))
928+
nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
929+
nxtinfo.nxt_length = skb->len;
930+
nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
931+
932+
put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
933+
sizeof(nxtinfo), &nxtinfo);
934+
}
935+
936+
void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
937+
struct msghdr *msghdr,
938+
struct sock *sk)
939+
{
940+
struct sk_buff *skb;
941+
int err;
942+
943+
skb = sctp_skb_recv_datagram(sk, MSG_PEEK, 1, &err);
944+
if (skb != NULL) {
945+
__sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
946+
msghdr, skb);
947+
/* Just release refcount here. */
948+
kfree_skb(skb);
949+
}
950+
}
951+
914952
/* Do accounting for bytes received and hold a reference to the association
915953
* for each skb.
916954
*/

0 commit comments

Comments
 (0)