xref: /OK3568_Linux_fs/kernel/net/sctp/ulpevent.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* SCTP kernel implementation
3*4882a593Smuzhiyun  * (C) Copyright IBM Corp. 2001, 2004
4*4882a593Smuzhiyun  * Copyright (c) 1999-2000 Cisco, Inc.
5*4882a593Smuzhiyun  * Copyright (c) 1999-2001 Motorola, Inc.
6*4882a593Smuzhiyun  * Copyright (c) 2001 Intel Corp.
7*4882a593Smuzhiyun  * Copyright (c) 2001 Nokia, Inc.
8*4882a593Smuzhiyun  * Copyright (c) 2001 La Monte H.P. Yarroll
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * These functions manipulate an sctp event.   The struct ulpevent is used
11*4882a593Smuzhiyun  * to carry notifications and data to the ULP (sockets).
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Please send any bug reports or fixes you make to the
14*4882a593Smuzhiyun  * email address(es):
15*4882a593Smuzhiyun  *    lksctp developers <linux-sctp@vger.kernel.org>
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * Written or modified by:
18*4882a593Smuzhiyun  *    Jon Grimm             <jgrimm@us.ibm.com>
19*4882a593Smuzhiyun  *    La Monte H.P. Yarroll <piggy@acm.org>
20*4882a593Smuzhiyun  *    Ardelle Fan	    <ardelle.fan@intel.com>
21*4882a593Smuzhiyun  *    Sridhar Samudrala     <sri@us.ibm.com>
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/types.h>
26*4882a593Smuzhiyun #include <linux/skbuff.h>
27*4882a593Smuzhiyun #include <net/sctp/structs.h>
28*4882a593Smuzhiyun #include <net/sctp/sctp.h>
29*4882a593Smuzhiyun #include <net/sctp/sm.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
32*4882a593Smuzhiyun 				       struct sctp_association *asoc);
33*4882a593Smuzhiyun static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
34*4882a593Smuzhiyun static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* Initialize an ULP event from an given skb.  */
sctp_ulpevent_init(struct sctp_ulpevent * event,__u16 msg_flags,unsigned int len)38*4882a593Smuzhiyun static void sctp_ulpevent_init(struct sctp_ulpevent *event,
39*4882a593Smuzhiyun 			       __u16 msg_flags,
40*4882a593Smuzhiyun 			       unsigned int len)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	memset(event, 0, sizeof(struct sctp_ulpevent));
43*4882a593Smuzhiyun 	event->msg_flags = msg_flags;
44*4882a593Smuzhiyun 	event->rmem_len = len;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /* Create a new sctp_ulpevent.  */
sctp_ulpevent_new(int size,__u16 msg_flags,gfp_t gfp)48*4882a593Smuzhiyun static struct sctp_ulpevent *sctp_ulpevent_new(int size, __u16 msg_flags,
49*4882a593Smuzhiyun 					       gfp_t gfp)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
52*4882a593Smuzhiyun 	struct sk_buff *skb;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	skb = alloc_skb(size, gfp);
55*4882a593Smuzhiyun 	if (!skb)
56*4882a593Smuzhiyun 		goto fail;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	event = sctp_skb2event(skb);
59*4882a593Smuzhiyun 	sctp_ulpevent_init(event, msg_flags, skb->truesize);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return event;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun fail:
64*4882a593Smuzhiyun 	return NULL;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* Is this a MSG_NOTIFICATION?  */
sctp_ulpevent_is_notification(const struct sctp_ulpevent * event)68*4882a593Smuzhiyun int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* Hold the association in case the msg_name needs read out of
74*4882a593Smuzhiyun  * the association.
75*4882a593Smuzhiyun  */
sctp_ulpevent_set_owner(struct sctp_ulpevent * event,const struct sctp_association * asoc)76*4882a593Smuzhiyun static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
77*4882a593Smuzhiyun 					   const struct sctp_association *asoc)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	struct sctp_chunk *chunk = event->chunk;
80*4882a593Smuzhiyun 	struct sk_buff *skb;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* Cast away the const, as we are just wanting to
83*4882a593Smuzhiyun 	 * bump the reference count.
84*4882a593Smuzhiyun 	 */
85*4882a593Smuzhiyun 	sctp_association_hold((struct sctp_association *)asoc);
86*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
87*4882a593Smuzhiyun 	event->asoc = (struct sctp_association *)asoc;
88*4882a593Smuzhiyun 	atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
89*4882a593Smuzhiyun 	sctp_skb_set_owner_r(skb, asoc->base.sk);
90*4882a593Smuzhiyun 	if (chunk && chunk->head_skb && !chunk->head_skb->sk)
91*4882a593Smuzhiyun 		chunk->head_skb->sk = asoc->base.sk;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* A simple destructor to give up the reference to the association. */
sctp_ulpevent_release_owner(struct sctp_ulpevent * event)95*4882a593Smuzhiyun static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct sctp_association *asoc = event->asoc;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	atomic_sub(event->rmem_len, &asoc->rmem_alloc);
100*4882a593Smuzhiyun 	sctp_association_put(asoc);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Create and initialize an SCTP_ASSOC_CHANGE event.
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * 5.3.1.1 SCTP_ASSOC_CHANGE
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * Communication notifications inform the ULP that an SCTP association
108*4882a593Smuzhiyun  * has either begun or ended. The identifier for a new association is
109*4882a593Smuzhiyun  * provided by this notification.
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  * Note: There is no field checking here.  If a field is unused it will be
112*4882a593Smuzhiyun  * zero'd out.
113*4882a593Smuzhiyun  */
sctp_ulpevent_make_assoc_change(const struct sctp_association * asoc,__u16 flags,__u16 state,__u16 error,__u16 outbound,__u16 inbound,struct sctp_chunk * chunk,gfp_t gfp)114*4882a593Smuzhiyun struct sctp_ulpevent  *sctp_ulpevent_make_assoc_change(
115*4882a593Smuzhiyun 	const struct sctp_association *asoc,
116*4882a593Smuzhiyun 	__u16 flags, __u16 state, __u16 error, __u16 outbound,
117*4882a593Smuzhiyun 	__u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
120*4882a593Smuzhiyun 	struct sctp_assoc_change *sac;
121*4882a593Smuzhiyun 	struct sk_buff *skb;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* If the lower layer passed in the chunk, it will be
124*4882a593Smuzhiyun 	 * an ABORT, so we need to include it in the sac_info.
125*4882a593Smuzhiyun 	 */
126*4882a593Smuzhiyun 	if (chunk) {
127*4882a593Smuzhiyun 		/* Copy the chunk data to a new skb and reserve enough
128*4882a593Smuzhiyun 		 * head room to use as notification.
129*4882a593Smuzhiyun 		 */
130*4882a593Smuzhiyun 		skb = skb_copy_expand(chunk->skb,
131*4882a593Smuzhiyun 				      sizeof(struct sctp_assoc_change), 0, gfp);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		if (!skb)
134*4882a593Smuzhiyun 			goto fail;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		/* Embed the event fields inside the cloned skb.  */
137*4882a593Smuzhiyun 		event = sctp_skb2event(skb);
138*4882a593Smuzhiyun 		sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 		/* Include the notification structure */
141*4882a593Smuzhiyun 		sac = skb_push(skb, sizeof(struct sctp_assoc_change));
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		/* Trim the buffer to the right length.  */
144*4882a593Smuzhiyun 		skb_trim(skb, sizeof(struct sctp_assoc_change) +
145*4882a593Smuzhiyun 			 ntohs(chunk->chunk_hdr->length) -
146*4882a593Smuzhiyun 			 sizeof(struct sctp_chunkhdr));
147*4882a593Smuzhiyun 	} else {
148*4882a593Smuzhiyun 		event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
149*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
150*4882a593Smuzhiyun 		if (!event)
151*4882a593Smuzhiyun 			goto fail;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		skb = sctp_event2skb(event);
154*4882a593Smuzhiyun 		sac = skb_put(skb, sizeof(struct sctp_assoc_change));
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
158*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
159*4882a593Smuzhiyun 	 *
160*4882a593Smuzhiyun 	 * sac_type:
161*4882a593Smuzhiyun 	 * It should be SCTP_ASSOC_CHANGE.
162*4882a593Smuzhiyun 	 */
163*4882a593Smuzhiyun 	sac->sac_type = SCTP_ASSOC_CHANGE;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
166*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
167*4882a593Smuzhiyun 	 *
168*4882a593Smuzhiyun 	 * sac_state: 32 bits (signed integer)
169*4882a593Smuzhiyun 	 * This field holds one of a number of values that communicate the
170*4882a593Smuzhiyun 	 * event that happened to the association.
171*4882a593Smuzhiyun 	 */
172*4882a593Smuzhiyun 	sac->sac_state = state;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
175*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
176*4882a593Smuzhiyun 	 *
177*4882a593Smuzhiyun 	 * sac_flags: 16 bits (unsigned integer)
178*4882a593Smuzhiyun 	 * Currently unused.
179*4882a593Smuzhiyun 	 */
180*4882a593Smuzhiyun 	sac->sac_flags = 0;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
183*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
184*4882a593Smuzhiyun 	 *
185*4882a593Smuzhiyun 	 * sac_length: sizeof (__u32)
186*4882a593Smuzhiyun 	 * This field is the total length of the notification data, including
187*4882a593Smuzhiyun 	 * the notification header.
188*4882a593Smuzhiyun 	 */
189*4882a593Smuzhiyun 	sac->sac_length = skb->len;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
192*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
193*4882a593Smuzhiyun 	 *
194*4882a593Smuzhiyun 	 * sac_error:  32 bits (signed integer)
195*4882a593Smuzhiyun 	 *
196*4882a593Smuzhiyun 	 * If the state was reached due to a error condition (e.g.
197*4882a593Smuzhiyun 	 * COMMUNICATION_LOST) any relevant error information is available in
198*4882a593Smuzhiyun 	 * this field. This corresponds to the protocol error codes defined in
199*4882a593Smuzhiyun 	 * [SCTP].
200*4882a593Smuzhiyun 	 */
201*4882a593Smuzhiyun 	sac->sac_error = error;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
204*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
205*4882a593Smuzhiyun 	 *
206*4882a593Smuzhiyun 	 * sac_outbound_streams:  16 bits (unsigned integer)
207*4882a593Smuzhiyun 	 * sac_inbound_streams:  16 bits (unsigned integer)
208*4882a593Smuzhiyun 	 *
209*4882a593Smuzhiyun 	 * The maximum number of streams allowed in each direction are
210*4882a593Smuzhiyun 	 * available in sac_outbound_streams and sac_inbound streams.
211*4882a593Smuzhiyun 	 */
212*4882a593Smuzhiyun 	sac->sac_outbound_streams = outbound;
213*4882a593Smuzhiyun 	sac->sac_inbound_streams = inbound;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
216*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
217*4882a593Smuzhiyun 	 *
218*4882a593Smuzhiyun 	 * sac_assoc_id: sizeof (sctp_assoc_t)
219*4882a593Smuzhiyun 	 *
220*4882a593Smuzhiyun 	 * The association id field, holds the identifier for the association.
221*4882a593Smuzhiyun 	 * All notifications for a given association have the same association
222*4882a593Smuzhiyun 	 * identifier.  For TCP style socket, this field is ignored.
223*4882a593Smuzhiyun 	 */
224*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
225*4882a593Smuzhiyun 	sac->sac_assoc_id = sctp_assoc2id(asoc);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	return event;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun fail:
230*4882a593Smuzhiyun 	return NULL;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * Socket Extensions for SCTP - draft-01
236*4882a593Smuzhiyun  * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * When a destination address on a multi-homed peer encounters a change
239*4882a593Smuzhiyun  * an interface details event is sent.
240*4882a593Smuzhiyun  */
sctp_ulpevent_make_peer_addr_change(const struct sctp_association * asoc,const struct sockaddr_storage * aaddr,int flags,int state,int error,gfp_t gfp)241*4882a593Smuzhiyun static struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
242*4882a593Smuzhiyun 	const struct sctp_association *asoc,
243*4882a593Smuzhiyun 	const struct sockaddr_storage *aaddr,
244*4882a593Smuzhiyun 	int flags, int state, int error, gfp_t gfp)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
247*4882a593Smuzhiyun 	struct sctp_paddr_change  *spc;
248*4882a593Smuzhiyun 	struct sk_buff *skb;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
251*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
252*4882a593Smuzhiyun 	if (!event)
253*4882a593Smuzhiyun 		goto fail;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
256*4882a593Smuzhiyun 	spc = skb_put(skb, sizeof(struct sctp_paddr_change));
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* Sockets API Extensions for SCTP
259*4882a593Smuzhiyun 	 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
260*4882a593Smuzhiyun 	 *
261*4882a593Smuzhiyun 	 * spc_type:
262*4882a593Smuzhiyun 	 *
263*4882a593Smuzhiyun 	 *    It should be SCTP_PEER_ADDR_CHANGE.
264*4882a593Smuzhiyun 	 */
265*4882a593Smuzhiyun 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/* Sockets API Extensions for SCTP
268*4882a593Smuzhiyun 	 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
269*4882a593Smuzhiyun 	 *
270*4882a593Smuzhiyun 	 * spc_length: sizeof (__u32)
271*4882a593Smuzhiyun 	 *
272*4882a593Smuzhiyun 	 * This field is the total length of the notification data, including
273*4882a593Smuzhiyun 	 * the notification header.
274*4882a593Smuzhiyun 	 */
275*4882a593Smuzhiyun 	spc->spc_length = sizeof(struct sctp_paddr_change);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/* Sockets API Extensions for SCTP
278*4882a593Smuzhiyun 	 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
279*4882a593Smuzhiyun 	 *
280*4882a593Smuzhiyun 	 * spc_flags: 16 bits (unsigned integer)
281*4882a593Smuzhiyun 	 * Currently unused.
282*4882a593Smuzhiyun 	 */
283*4882a593Smuzhiyun 	spc->spc_flags = 0;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* Sockets API Extensions for SCTP
286*4882a593Smuzhiyun 	 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
287*4882a593Smuzhiyun 	 *
288*4882a593Smuzhiyun 	 * spc_state:  32 bits (signed integer)
289*4882a593Smuzhiyun 	 *
290*4882a593Smuzhiyun 	 * This field holds one of a number of values that communicate the
291*4882a593Smuzhiyun 	 * event that happened to the address.
292*4882a593Smuzhiyun 	 */
293*4882a593Smuzhiyun 	spc->spc_state = state;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Sockets API Extensions for SCTP
296*4882a593Smuzhiyun 	 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
297*4882a593Smuzhiyun 	 *
298*4882a593Smuzhiyun 	 * spc_error:  32 bits (signed integer)
299*4882a593Smuzhiyun 	 *
300*4882a593Smuzhiyun 	 * If the state was reached due to any error condition (e.g.
301*4882a593Smuzhiyun 	 * ADDRESS_UNREACHABLE) any relevant error information is available in
302*4882a593Smuzhiyun 	 * this field.
303*4882a593Smuzhiyun 	 */
304*4882a593Smuzhiyun 	spc->spc_error = error;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
307*4882a593Smuzhiyun 	 * 5.3.1.1 SCTP_ASSOC_CHANGE
308*4882a593Smuzhiyun 	 *
309*4882a593Smuzhiyun 	 * spc_assoc_id: sizeof (sctp_assoc_t)
310*4882a593Smuzhiyun 	 *
311*4882a593Smuzhiyun 	 * The association id field, holds the identifier for the association.
312*4882a593Smuzhiyun 	 * All notifications for a given association have the same association
313*4882a593Smuzhiyun 	 * identifier.  For TCP style socket, this field is ignored.
314*4882a593Smuzhiyun 	 */
315*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
316*4882a593Smuzhiyun 	spc->spc_assoc_id = sctp_assoc2id(asoc);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* Sockets API Extensions for SCTP
319*4882a593Smuzhiyun 	 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
320*4882a593Smuzhiyun 	 *
321*4882a593Smuzhiyun 	 * spc_aaddr: sizeof (struct sockaddr_storage)
322*4882a593Smuzhiyun 	 *
323*4882a593Smuzhiyun 	 * The affected address field, holds the remote peer's address that is
324*4882a593Smuzhiyun 	 * encountering the change of state.
325*4882a593Smuzhiyun 	 */
326*4882a593Smuzhiyun 	memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	/* Map ipv4 address into v4-mapped-on-v6 address.  */
329*4882a593Smuzhiyun 	sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_to_user(
330*4882a593Smuzhiyun 					sctp_sk(asoc->base.sk),
331*4882a593Smuzhiyun 					(union sctp_addr *)&spc->spc_aaddr);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	return event;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun fail:
336*4882a593Smuzhiyun 	return NULL;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun 
sctp_ulpevent_notify_peer_addr_change(struct sctp_transport * transport,int state,int error)339*4882a593Smuzhiyun void sctp_ulpevent_notify_peer_addr_change(struct sctp_transport *transport,
340*4882a593Smuzhiyun 					   int state, int error)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	struct sctp_association *asoc = transport->asoc;
343*4882a593Smuzhiyun 	struct sockaddr_storage addr;
344*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	if (asoc->state < SCTP_STATE_ESTABLISHED)
347*4882a593Smuzhiyun 		return;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	memset(&addr, 0, sizeof(struct sockaddr_storage));
350*4882a593Smuzhiyun 	memcpy(&addr, &transport->ipaddr, transport->af_specific->sockaddr_len);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	event = sctp_ulpevent_make_peer_addr_change(asoc, &addr, 0, state,
353*4882a593Smuzhiyun 						    error, GFP_ATOMIC);
354*4882a593Smuzhiyun 	if (event)
355*4882a593Smuzhiyun 		asoc->stream.si->enqueue_event(&asoc->ulpq, event);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /* Create and initialize an SCTP_REMOTE_ERROR notification.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * Note: This assumes that the chunk->skb->data already points to the
361*4882a593Smuzhiyun  * operation error payload.
362*4882a593Smuzhiyun  *
363*4882a593Smuzhiyun  * Socket Extensions for SCTP - draft-01
364*4882a593Smuzhiyun  * 5.3.1.3 SCTP_REMOTE_ERROR
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  * A remote peer may send an Operational Error message to its peer.
367*4882a593Smuzhiyun  * This message indicates a variety of error conditions on an
368*4882a593Smuzhiyun  * association. The entire error TLV as it appears on the wire is
369*4882a593Smuzhiyun  * included in a SCTP_REMOTE_ERROR event.  Please refer to the SCTP
370*4882a593Smuzhiyun  * specification [SCTP] and any extensions for a list of possible
371*4882a593Smuzhiyun  * error formats.
372*4882a593Smuzhiyun  */
373*4882a593Smuzhiyun struct sctp_ulpevent *
sctp_ulpevent_make_remote_error(const struct sctp_association * asoc,struct sctp_chunk * chunk,__u16 flags,gfp_t gfp)374*4882a593Smuzhiyun sctp_ulpevent_make_remote_error(const struct sctp_association *asoc,
375*4882a593Smuzhiyun 				struct sctp_chunk *chunk, __u16 flags,
376*4882a593Smuzhiyun 				gfp_t gfp)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun 	struct sctp_remote_error *sre;
379*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
380*4882a593Smuzhiyun 	struct sctp_errhdr *ch;
381*4882a593Smuzhiyun 	struct sk_buff *skb;
382*4882a593Smuzhiyun 	__be16 cause;
383*4882a593Smuzhiyun 	int elen;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	ch = (struct sctp_errhdr *)(chunk->skb->data);
386*4882a593Smuzhiyun 	cause = ch->cause;
387*4882a593Smuzhiyun 	elen = SCTP_PAD4(ntohs(ch->length)) - sizeof(*ch);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* Pull off the ERROR header.  */
390*4882a593Smuzhiyun 	skb_pull(chunk->skb, sizeof(*ch));
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/* Copy the skb to a new skb with room for us to prepend
393*4882a593Smuzhiyun 	 * notification with.
394*4882a593Smuzhiyun 	 */
395*4882a593Smuzhiyun 	skb = skb_copy_expand(chunk->skb, sizeof(*sre), 0, gfp);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	/* Pull off the rest of the cause TLV from the chunk.  */
398*4882a593Smuzhiyun 	skb_pull(chunk->skb, elen);
399*4882a593Smuzhiyun 	if (!skb)
400*4882a593Smuzhiyun 		goto fail;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	/* Embed the event fields inside the cloned skb.  */
403*4882a593Smuzhiyun 	event = sctp_skb2event(skb);
404*4882a593Smuzhiyun 	sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	sre = skb_push(skb, sizeof(*sre));
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/* Trim the buffer to the right length.  */
409*4882a593Smuzhiyun 	skb_trim(skb, sizeof(*sre) + elen);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	/* RFC6458, Section 6.1.3. SCTP_REMOTE_ERROR */
412*4882a593Smuzhiyun 	memset(sre, 0, sizeof(*sre));
413*4882a593Smuzhiyun 	sre->sre_type = SCTP_REMOTE_ERROR;
414*4882a593Smuzhiyun 	sre->sre_flags = 0;
415*4882a593Smuzhiyun 	sre->sre_length = skb->len;
416*4882a593Smuzhiyun 	sre->sre_error = cause;
417*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
418*4882a593Smuzhiyun 	sre->sre_assoc_id = sctp_assoc2id(asoc);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	return event;
421*4882a593Smuzhiyun fail:
422*4882a593Smuzhiyun 	return NULL;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun /* Create and initialize a SCTP_SEND_FAILED notification.
426*4882a593Smuzhiyun  *
427*4882a593Smuzhiyun  * Socket Extensions for SCTP - draft-01
428*4882a593Smuzhiyun  * 5.3.1.4 SCTP_SEND_FAILED
429*4882a593Smuzhiyun  */
sctp_ulpevent_make_send_failed(const struct sctp_association * asoc,struct sctp_chunk * chunk,__u16 flags,__u32 error,gfp_t gfp)430*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
431*4882a593Smuzhiyun 	const struct sctp_association *asoc, struct sctp_chunk *chunk,
432*4882a593Smuzhiyun 	__u16 flags, __u32 error, gfp_t gfp)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
435*4882a593Smuzhiyun 	struct sctp_send_failed *ssf;
436*4882a593Smuzhiyun 	struct sk_buff *skb;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	/* Pull off any padding. */
439*4882a593Smuzhiyun 	int len = ntohs(chunk->chunk_hdr->length);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	/* Make skb with more room so we can prepend notification.  */
442*4882a593Smuzhiyun 	skb = skb_copy_expand(chunk->skb,
443*4882a593Smuzhiyun 			      sizeof(struct sctp_send_failed), /* headroom */
444*4882a593Smuzhiyun 			      0,                               /* tailroom */
445*4882a593Smuzhiyun 			      gfp);
446*4882a593Smuzhiyun 	if (!skb)
447*4882a593Smuzhiyun 		goto fail;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	/* Pull off the common chunk header and DATA header.  */
450*4882a593Smuzhiyun 	skb_pull(skb, sctp_datachk_len(&asoc->stream));
451*4882a593Smuzhiyun 	len -= sctp_datachk_len(&asoc->stream);
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	/* Embed the event fields inside the cloned skb.  */
454*4882a593Smuzhiyun 	event = sctp_skb2event(skb);
455*4882a593Smuzhiyun 	sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	ssf = skb_push(skb, sizeof(struct sctp_send_failed));
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
460*4882a593Smuzhiyun 	 * 5.3.1.4 SCTP_SEND_FAILED
461*4882a593Smuzhiyun 	 *
462*4882a593Smuzhiyun 	 * ssf_type:
463*4882a593Smuzhiyun 	 * It should be SCTP_SEND_FAILED.
464*4882a593Smuzhiyun 	 */
465*4882a593Smuzhiyun 	ssf->ssf_type = SCTP_SEND_FAILED;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
468*4882a593Smuzhiyun 	 * 5.3.1.4 SCTP_SEND_FAILED
469*4882a593Smuzhiyun 	 *
470*4882a593Smuzhiyun 	 * ssf_flags: 16 bits (unsigned integer)
471*4882a593Smuzhiyun 	 * The flag value will take one of the following values
472*4882a593Smuzhiyun 	 *
473*4882a593Smuzhiyun 	 * SCTP_DATA_UNSENT - Indicates that the data was never put on
474*4882a593Smuzhiyun 	 *                    the wire.
475*4882a593Smuzhiyun 	 *
476*4882a593Smuzhiyun 	 * SCTP_DATA_SENT   - Indicates that the data was put on the wire.
477*4882a593Smuzhiyun 	 *                    Note that this does not necessarily mean that the
478*4882a593Smuzhiyun 	 *                    data was (or was not) successfully delivered.
479*4882a593Smuzhiyun 	 */
480*4882a593Smuzhiyun 	ssf->ssf_flags = flags;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
483*4882a593Smuzhiyun 	 * 5.3.1.4 SCTP_SEND_FAILED
484*4882a593Smuzhiyun 	 *
485*4882a593Smuzhiyun 	 * ssf_length: sizeof (__u32)
486*4882a593Smuzhiyun 	 * This field is the total length of the notification data, including
487*4882a593Smuzhiyun 	 * the notification header.
488*4882a593Smuzhiyun 	 */
489*4882a593Smuzhiyun 	ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
490*4882a593Smuzhiyun 	skb_trim(skb, ssf->ssf_length);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
493*4882a593Smuzhiyun 	 * 5.3.1.4 SCTP_SEND_FAILED
494*4882a593Smuzhiyun 	 *
495*4882a593Smuzhiyun 	 * ssf_error: 16 bits (unsigned integer)
496*4882a593Smuzhiyun 	 * This value represents the reason why the send failed, and if set,
497*4882a593Smuzhiyun 	 * will be a SCTP protocol error code as defined in [SCTP] section
498*4882a593Smuzhiyun 	 * 3.3.10.
499*4882a593Smuzhiyun 	 */
500*4882a593Smuzhiyun 	ssf->ssf_error = error;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
503*4882a593Smuzhiyun 	 * 5.3.1.4 SCTP_SEND_FAILED
504*4882a593Smuzhiyun 	 *
505*4882a593Smuzhiyun 	 * ssf_info: sizeof (struct sctp_sndrcvinfo)
506*4882a593Smuzhiyun 	 * The original send information associated with the undelivered
507*4882a593Smuzhiyun 	 * message.
508*4882a593Smuzhiyun 	 */
509*4882a593Smuzhiyun 	memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	/* Per TSVWG discussion with Randy. Allow the application to
512*4882a593Smuzhiyun 	 * reassemble a fragmented message.
513*4882a593Smuzhiyun 	 */
514*4882a593Smuzhiyun 	ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
517*4882a593Smuzhiyun 	 * 5.3.1.4 SCTP_SEND_FAILED
518*4882a593Smuzhiyun 	 *
519*4882a593Smuzhiyun 	 * ssf_assoc_id: sizeof (sctp_assoc_t)
520*4882a593Smuzhiyun 	 * The association id field, sf_assoc_id, holds the identifier for the
521*4882a593Smuzhiyun 	 * association.  All notifications for a given association have the
522*4882a593Smuzhiyun 	 * same association identifier.  For TCP style socket, this field is
523*4882a593Smuzhiyun 	 * ignored.
524*4882a593Smuzhiyun 	 */
525*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
526*4882a593Smuzhiyun 	ssf->ssf_assoc_id = sctp_assoc2id(asoc);
527*4882a593Smuzhiyun 	return event;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun fail:
530*4882a593Smuzhiyun 	return NULL;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
sctp_ulpevent_make_send_failed_event(const struct sctp_association * asoc,struct sctp_chunk * chunk,__u16 flags,__u32 error,gfp_t gfp)533*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_send_failed_event(
534*4882a593Smuzhiyun 	const struct sctp_association *asoc, struct sctp_chunk *chunk,
535*4882a593Smuzhiyun 	__u16 flags, __u32 error, gfp_t gfp)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun 	struct sctp_send_failed_event *ssf;
538*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
539*4882a593Smuzhiyun 	struct sk_buff *skb;
540*4882a593Smuzhiyun 	int len;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	skb = skb_copy_expand(chunk->skb, sizeof(*ssf), 0, gfp);
543*4882a593Smuzhiyun 	if (!skb)
544*4882a593Smuzhiyun 		return NULL;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	len = ntohs(chunk->chunk_hdr->length);
547*4882a593Smuzhiyun 	len -= sctp_datachk_len(&asoc->stream);
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	skb_pull(skb, sctp_datachk_len(&asoc->stream));
550*4882a593Smuzhiyun 	event = sctp_skb2event(skb);
551*4882a593Smuzhiyun 	sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	ssf = skb_push(skb, sizeof(*ssf));
554*4882a593Smuzhiyun 	ssf->ssf_type = SCTP_SEND_FAILED_EVENT;
555*4882a593Smuzhiyun 	ssf->ssf_flags = flags;
556*4882a593Smuzhiyun 	ssf->ssf_length = sizeof(*ssf) + len;
557*4882a593Smuzhiyun 	skb_trim(skb, ssf->ssf_length);
558*4882a593Smuzhiyun 	ssf->ssf_error = error;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	ssf->ssfe_info.snd_sid = chunk->sinfo.sinfo_stream;
561*4882a593Smuzhiyun 	ssf->ssfe_info.snd_ppid = chunk->sinfo.sinfo_ppid;
562*4882a593Smuzhiyun 	ssf->ssfe_info.snd_context = chunk->sinfo.sinfo_context;
563*4882a593Smuzhiyun 	ssf->ssfe_info.snd_assoc_id = chunk->sinfo.sinfo_assoc_id;
564*4882a593Smuzhiyun 	ssf->ssfe_info.snd_flags = chunk->chunk_hdr->flags;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
567*4882a593Smuzhiyun 	ssf->ssf_assoc_id = sctp_assoc2id(asoc);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	return event;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
573*4882a593Smuzhiyun  *
574*4882a593Smuzhiyun  * Socket Extensions for SCTP - draft-01
575*4882a593Smuzhiyun  * 5.3.1.5 SCTP_SHUTDOWN_EVENT
576*4882a593Smuzhiyun  */
sctp_ulpevent_make_shutdown_event(const struct sctp_association * asoc,__u16 flags,gfp_t gfp)577*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
578*4882a593Smuzhiyun 	const struct sctp_association *asoc,
579*4882a593Smuzhiyun 	__u16 flags, gfp_t gfp)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
582*4882a593Smuzhiyun 	struct sctp_shutdown_event *sse;
583*4882a593Smuzhiyun 	struct sk_buff *skb;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
586*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
587*4882a593Smuzhiyun 	if (!event)
588*4882a593Smuzhiyun 		goto fail;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
591*4882a593Smuzhiyun 	sse = skb_put(skb, sizeof(struct sctp_shutdown_event));
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
594*4882a593Smuzhiyun 	 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
595*4882a593Smuzhiyun 	 *
596*4882a593Smuzhiyun 	 * sse_type
597*4882a593Smuzhiyun 	 * It should be SCTP_SHUTDOWN_EVENT
598*4882a593Smuzhiyun 	 */
599*4882a593Smuzhiyun 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
602*4882a593Smuzhiyun 	 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
603*4882a593Smuzhiyun 	 *
604*4882a593Smuzhiyun 	 * sse_flags: 16 bits (unsigned integer)
605*4882a593Smuzhiyun 	 * Currently unused.
606*4882a593Smuzhiyun 	 */
607*4882a593Smuzhiyun 	sse->sse_flags = 0;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
610*4882a593Smuzhiyun 	 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
611*4882a593Smuzhiyun 	 *
612*4882a593Smuzhiyun 	 * sse_length: sizeof (__u32)
613*4882a593Smuzhiyun 	 * This field is the total length of the notification data, including
614*4882a593Smuzhiyun 	 * the notification header.
615*4882a593Smuzhiyun 	 */
616*4882a593Smuzhiyun 	sse->sse_length = sizeof(struct sctp_shutdown_event);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	/* Socket Extensions for SCTP
619*4882a593Smuzhiyun 	 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
620*4882a593Smuzhiyun 	 *
621*4882a593Smuzhiyun 	 * sse_assoc_id: sizeof (sctp_assoc_t)
622*4882a593Smuzhiyun 	 * The association id field, holds the identifier for the association.
623*4882a593Smuzhiyun 	 * All notifications for a given association have the same association
624*4882a593Smuzhiyun 	 * identifier.  For TCP style socket, this field is ignored.
625*4882a593Smuzhiyun 	 */
626*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
627*4882a593Smuzhiyun 	sse->sse_assoc_id = sctp_assoc2id(asoc);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	return event;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun fail:
632*4882a593Smuzhiyun 	return NULL;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
636*4882a593Smuzhiyun  *
637*4882a593Smuzhiyun  * Socket Extensions for SCTP
638*4882a593Smuzhiyun  * 5.3.1.6 SCTP_ADAPTATION_INDICATION
639*4882a593Smuzhiyun  */
sctp_ulpevent_make_adaptation_indication(const struct sctp_association * asoc,gfp_t gfp)640*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
641*4882a593Smuzhiyun 	const struct sctp_association *asoc, gfp_t gfp)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
644*4882a593Smuzhiyun 	struct sctp_adaptation_event *sai;
645*4882a593Smuzhiyun 	struct sk_buff *skb;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
648*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
649*4882a593Smuzhiyun 	if (!event)
650*4882a593Smuzhiyun 		goto fail;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
653*4882a593Smuzhiyun 	sai = skb_put(skb, sizeof(struct sctp_adaptation_event));
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
656*4882a593Smuzhiyun 	sai->sai_flags = 0;
657*4882a593Smuzhiyun 	sai->sai_length = sizeof(struct sctp_adaptation_event);
658*4882a593Smuzhiyun 	sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
659*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
660*4882a593Smuzhiyun 	sai->sai_assoc_id = sctp_assoc2id(asoc);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	return event;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun fail:
665*4882a593Smuzhiyun 	return NULL;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun /* A message has been received.  Package this message as a notification
669*4882a593Smuzhiyun  * to pass it to the upper layers.  Go ahead and calculate the sndrcvinfo
670*4882a593Smuzhiyun  * even if filtered out later.
671*4882a593Smuzhiyun  *
672*4882a593Smuzhiyun  * Socket Extensions for SCTP
673*4882a593Smuzhiyun  * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
674*4882a593Smuzhiyun  */
sctp_ulpevent_make_rcvmsg(struct sctp_association * asoc,struct sctp_chunk * chunk,gfp_t gfp)675*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
676*4882a593Smuzhiyun 						struct sctp_chunk *chunk,
677*4882a593Smuzhiyun 						gfp_t gfp)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	struct sctp_ulpevent *event = NULL;
680*4882a593Smuzhiyun 	struct sk_buff *skb = chunk->skb;
681*4882a593Smuzhiyun 	struct sock *sk = asoc->base.sk;
682*4882a593Smuzhiyun 	size_t padding, datalen;
683*4882a593Smuzhiyun 	int rx_count;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	/*
686*4882a593Smuzhiyun 	 * check to see if we need to make space for this
687*4882a593Smuzhiyun 	 * new skb, expand the rcvbuffer if needed, or drop
688*4882a593Smuzhiyun 	 * the frame
689*4882a593Smuzhiyun 	 */
690*4882a593Smuzhiyun 	if (asoc->ep->rcvbuf_policy)
691*4882a593Smuzhiyun 		rx_count = atomic_read(&asoc->rmem_alloc);
692*4882a593Smuzhiyun 	else
693*4882a593Smuzhiyun 		rx_count = atomic_read(&sk->sk_rmem_alloc);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	datalen = ntohs(chunk->chunk_hdr->length);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	if (rx_count >= sk->sk_rcvbuf || !sk_rmem_schedule(sk, skb, datalen))
698*4882a593Smuzhiyun 		goto fail;
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	/* Clone the original skb, sharing the data.  */
701*4882a593Smuzhiyun 	skb = skb_clone(chunk->skb, gfp);
702*4882a593Smuzhiyun 	if (!skb)
703*4882a593Smuzhiyun 		goto fail;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	/* Now that all memory allocations for this chunk succeeded, we
706*4882a593Smuzhiyun 	 * can mark it as received so the tsn_map is updated correctly.
707*4882a593Smuzhiyun 	 */
708*4882a593Smuzhiyun 	if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
709*4882a593Smuzhiyun 			     ntohl(chunk->subh.data_hdr->tsn),
710*4882a593Smuzhiyun 			     chunk->transport))
711*4882a593Smuzhiyun 		goto fail_mark;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	/* First calculate the padding, so we don't inadvertently
714*4882a593Smuzhiyun 	 * pass up the wrong length to the user.
715*4882a593Smuzhiyun 	 *
716*4882a593Smuzhiyun 	 * RFC 2960 - Section 3.2  Chunk Field Descriptions
717*4882a593Smuzhiyun 	 *
718*4882a593Smuzhiyun 	 * The total length of a chunk(including Type, Length and Value fields)
719*4882a593Smuzhiyun 	 * MUST be a multiple of 4 bytes.  If the length of the chunk is not a
720*4882a593Smuzhiyun 	 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
721*4882a593Smuzhiyun 	 * bytes and this padding is not included in the chunk length field.
722*4882a593Smuzhiyun 	 * The sender should never pad with more than 3 bytes.  The receiver
723*4882a593Smuzhiyun 	 * MUST ignore the padding bytes.
724*4882a593Smuzhiyun 	 */
725*4882a593Smuzhiyun 	padding = SCTP_PAD4(datalen) - datalen;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	/* Fixup cloned skb with just this chunks data.  */
728*4882a593Smuzhiyun 	skb_trim(skb, chunk->chunk_end - padding - skb->data);
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	/* Embed the event fields inside the cloned skb.  */
731*4882a593Smuzhiyun 	event = sctp_skb2event(skb);
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	/* Initialize event with flags 0  and correct length
734*4882a593Smuzhiyun 	 * Since this is a clone of the original skb, only account for
735*4882a593Smuzhiyun 	 * the data of this chunk as other chunks will be accounted separately.
736*4882a593Smuzhiyun 	 */
737*4882a593Smuzhiyun 	sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	/* And hold the chunk as we need it for getting the IP headers
740*4882a593Smuzhiyun 	 * later in recvmsg
741*4882a593Smuzhiyun 	 */
742*4882a593Smuzhiyun 	sctp_chunk_hold(chunk);
743*4882a593Smuzhiyun 	event->chunk = chunk;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	sctp_ulpevent_receive_data(event, asoc);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	event->stream = ntohs(chunk->subh.data_hdr->stream);
748*4882a593Smuzhiyun 	if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
749*4882a593Smuzhiyun 		event->flags |= SCTP_UNORDERED;
750*4882a593Smuzhiyun 		event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
751*4882a593Smuzhiyun 	}
752*4882a593Smuzhiyun 	event->tsn = ntohl(chunk->subh.data_hdr->tsn);
753*4882a593Smuzhiyun 	event->msg_flags |= chunk->chunk_hdr->flags;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	return event;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun fail_mark:
758*4882a593Smuzhiyun 	kfree_skb(skb);
759*4882a593Smuzhiyun fail:
760*4882a593Smuzhiyun 	return NULL;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun /* Create a partial delivery related event.
764*4882a593Smuzhiyun  *
765*4882a593Smuzhiyun  * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
766*4882a593Smuzhiyun  *
767*4882a593Smuzhiyun  *   When a receiver is engaged in a partial delivery of a
768*4882a593Smuzhiyun  *   message this notification will be used to indicate
769*4882a593Smuzhiyun  *   various events.
770*4882a593Smuzhiyun  */
sctp_ulpevent_make_pdapi(const struct sctp_association * asoc,__u32 indication,__u32 sid,__u32 seq,__u32 flags,gfp_t gfp)771*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
772*4882a593Smuzhiyun 					const struct sctp_association *asoc,
773*4882a593Smuzhiyun 					__u32 indication, __u32 sid, __u32 seq,
774*4882a593Smuzhiyun 					__u32 flags, gfp_t gfp)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
777*4882a593Smuzhiyun 	struct sctp_pdapi_event *pd;
778*4882a593Smuzhiyun 	struct sk_buff *skb;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
781*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
782*4882a593Smuzhiyun 	if (!event)
783*4882a593Smuzhiyun 		goto fail;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
786*4882a593Smuzhiyun 	pd = skb_put(skb, sizeof(struct sctp_pdapi_event));
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	/* pdapi_type
789*4882a593Smuzhiyun 	 *   It should be SCTP_PARTIAL_DELIVERY_EVENT
790*4882a593Smuzhiyun 	 *
791*4882a593Smuzhiyun 	 * pdapi_flags: 16 bits (unsigned integer)
792*4882a593Smuzhiyun 	 *   Currently unused.
793*4882a593Smuzhiyun 	 */
794*4882a593Smuzhiyun 	pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
795*4882a593Smuzhiyun 	pd->pdapi_flags = flags;
796*4882a593Smuzhiyun 	pd->pdapi_stream = sid;
797*4882a593Smuzhiyun 	pd->pdapi_seq = seq;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	/* pdapi_length: 32 bits (unsigned integer)
800*4882a593Smuzhiyun 	 *
801*4882a593Smuzhiyun 	 * This field is the total length of the notification data, including
802*4882a593Smuzhiyun 	 * the notification header.  It will generally be sizeof (struct
803*4882a593Smuzhiyun 	 * sctp_pdapi_event).
804*4882a593Smuzhiyun 	 */
805*4882a593Smuzhiyun 	pd->pdapi_length = sizeof(struct sctp_pdapi_event);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	/*  pdapi_indication: 32 bits (unsigned integer)
808*4882a593Smuzhiyun 	 *
809*4882a593Smuzhiyun 	 * This field holds the indication being sent to the application.
810*4882a593Smuzhiyun 	 */
811*4882a593Smuzhiyun 	pd->pdapi_indication = indication;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	/*  pdapi_assoc_id: sizeof (sctp_assoc_t)
814*4882a593Smuzhiyun 	 *
815*4882a593Smuzhiyun 	 * The association id field, holds the identifier for the association.
816*4882a593Smuzhiyun 	 */
817*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
818*4882a593Smuzhiyun 	pd->pdapi_assoc_id = sctp_assoc2id(asoc);
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	return event;
821*4882a593Smuzhiyun fail:
822*4882a593Smuzhiyun 	return NULL;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
sctp_ulpevent_make_authkey(const struct sctp_association * asoc,__u16 key_id,__u32 indication,gfp_t gfp)825*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_authkey(
826*4882a593Smuzhiyun 	const struct sctp_association *asoc, __u16 key_id,
827*4882a593Smuzhiyun 	__u32 indication, gfp_t gfp)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
830*4882a593Smuzhiyun 	struct sctp_authkey_event *ak;
831*4882a593Smuzhiyun 	struct sk_buff *skb;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
834*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
835*4882a593Smuzhiyun 	if (!event)
836*4882a593Smuzhiyun 		goto fail;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
839*4882a593Smuzhiyun 	ak = skb_put(skb, sizeof(struct sctp_authkey_event));
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	ak->auth_type = SCTP_AUTHENTICATION_EVENT;
842*4882a593Smuzhiyun 	ak->auth_flags = 0;
843*4882a593Smuzhiyun 	ak->auth_length = sizeof(struct sctp_authkey_event);
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	ak->auth_keynumber = key_id;
846*4882a593Smuzhiyun 	ak->auth_altkeynumber = 0;
847*4882a593Smuzhiyun 	ak->auth_indication = indication;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	/*
850*4882a593Smuzhiyun 	 * The association id field, holds the identifier for the association.
851*4882a593Smuzhiyun 	 */
852*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
853*4882a593Smuzhiyun 	ak->auth_assoc_id = sctp_assoc2id(asoc);
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	return event;
856*4882a593Smuzhiyun fail:
857*4882a593Smuzhiyun 	return NULL;
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun /*
861*4882a593Smuzhiyun  * Socket Extensions for SCTP
862*4882a593Smuzhiyun  * 6.3.10. SCTP_SENDER_DRY_EVENT
863*4882a593Smuzhiyun  */
sctp_ulpevent_make_sender_dry_event(const struct sctp_association * asoc,gfp_t gfp)864*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
865*4882a593Smuzhiyun 	const struct sctp_association *asoc, gfp_t gfp)
866*4882a593Smuzhiyun {
867*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
868*4882a593Smuzhiyun 	struct sctp_sender_dry_event *sdry;
869*4882a593Smuzhiyun 	struct sk_buff *skb;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
872*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
873*4882a593Smuzhiyun 	if (!event)
874*4882a593Smuzhiyun 		return NULL;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
877*4882a593Smuzhiyun 	sdry = skb_put(skb, sizeof(struct sctp_sender_dry_event));
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
880*4882a593Smuzhiyun 	sdry->sender_dry_flags = 0;
881*4882a593Smuzhiyun 	sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
882*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
883*4882a593Smuzhiyun 	sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	return event;
886*4882a593Smuzhiyun }
887*4882a593Smuzhiyun 
sctp_ulpevent_make_stream_reset_event(const struct sctp_association * asoc,__u16 flags,__u16 stream_num,__be16 * stream_list,gfp_t gfp)888*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_stream_reset_event(
889*4882a593Smuzhiyun 	const struct sctp_association *asoc, __u16 flags, __u16 stream_num,
890*4882a593Smuzhiyun 	__be16 *stream_list, gfp_t gfp)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun 	struct sctp_stream_reset_event *sreset;
893*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
894*4882a593Smuzhiyun 	struct sk_buff *skb;
895*4882a593Smuzhiyun 	int length, i;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	length = sizeof(struct sctp_stream_reset_event) + 2 * stream_num;
898*4882a593Smuzhiyun 	event = sctp_ulpevent_new(length, MSG_NOTIFICATION, gfp);
899*4882a593Smuzhiyun 	if (!event)
900*4882a593Smuzhiyun 		return NULL;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
903*4882a593Smuzhiyun 	sreset = skb_put(skb, length);
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	sreset->strreset_type = SCTP_STREAM_RESET_EVENT;
906*4882a593Smuzhiyun 	sreset->strreset_flags = flags;
907*4882a593Smuzhiyun 	sreset->strreset_length = length;
908*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
909*4882a593Smuzhiyun 	sreset->strreset_assoc_id = sctp_assoc2id(asoc);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	for (i = 0; i < stream_num; i++)
912*4882a593Smuzhiyun 		sreset->strreset_stream_list[i] = ntohs(stream_list[i]);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	return event;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun 
sctp_ulpevent_make_assoc_reset_event(const struct sctp_association * asoc,__u16 flags,__u32 local_tsn,__u32 remote_tsn,gfp_t gfp)917*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_assoc_reset_event(
918*4882a593Smuzhiyun 	const struct sctp_association *asoc, __u16 flags, __u32 local_tsn,
919*4882a593Smuzhiyun 	__u32 remote_tsn, gfp_t gfp)
920*4882a593Smuzhiyun {
921*4882a593Smuzhiyun 	struct sctp_assoc_reset_event *areset;
922*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
923*4882a593Smuzhiyun 	struct sk_buff *skb;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_assoc_reset_event),
926*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
927*4882a593Smuzhiyun 	if (!event)
928*4882a593Smuzhiyun 		return NULL;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
931*4882a593Smuzhiyun 	areset = skb_put(skb, sizeof(struct sctp_assoc_reset_event));
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	areset->assocreset_type = SCTP_ASSOC_RESET_EVENT;
934*4882a593Smuzhiyun 	areset->assocreset_flags = flags;
935*4882a593Smuzhiyun 	areset->assocreset_length = sizeof(struct sctp_assoc_reset_event);
936*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
937*4882a593Smuzhiyun 	areset->assocreset_assoc_id = sctp_assoc2id(asoc);
938*4882a593Smuzhiyun 	areset->assocreset_local_tsn = local_tsn;
939*4882a593Smuzhiyun 	areset->assocreset_remote_tsn = remote_tsn;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	return event;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun 
sctp_ulpevent_make_stream_change_event(const struct sctp_association * asoc,__u16 flags,__u32 strchange_instrms,__u32 strchange_outstrms,gfp_t gfp)944*4882a593Smuzhiyun struct sctp_ulpevent *sctp_ulpevent_make_stream_change_event(
945*4882a593Smuzhiyun 	const struct sctp_association *asoc, __u16 flags,
946*4882a593Smuzhiyun 	__u32 strchange_instrms, __u32 strchange_outstrms, gfp_t gfp)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	struct sctp_stream_change_event *schange;
949*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
950*4882a593Smuzhiyun 	struct sk_buff *skb;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	event = sctp_ulpevent_new(sizeof(struct sctp_stream_change_event),
953*4882a593Smuzhiyun 				  MSG_NOTIFICATION, gfp);
954*4882a593Smuzhiyun 	if (!event)
955*4882a593Smuzhiyun 		return NULL;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
958*4882a593Smuzhiyun 	schange = skb_put(skb, sizeof(struct sctp_stream_change_event));
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	schange->strchange_type = SCTP_STREAM_CHANGE_EVENT;
961*4882a593Smuzhiyun 	schange->strchange_flags = flags;
962*4882a593Smuzhiyun 	schange->strchange_length = sizeof(struct sctp_stream_change_event);
963*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
964*4882a593Smuzhiyun 	schange->strchange_assoc_id = sctp_assoc2id(asoc);
965*4882a593Smuzhiyun 	schange->strchange_instrms = strchange_instrms;
966*4882a593Smuzhiyun 	schange->strchange_outstrms = strchange_outstrms;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	return event;
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun /* Return the notification type, assuming this is a notification
972*4882a593Smuzhiyun  * event.
973*4882a593Smuzhiyun  */
sctp_ulpevent_get_notification_type(const struct sctp_ulpevent * event)974*4882a593Smuzhiyun __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
975*4882a593Smuzhiyun {
976*4882a593Smuzhiyun 	union sctp_notification *notification;
977*4882a593Smuzhiyun 	struct sk_buff *skb;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
980*4882a593Smuzhiyun 	notification = (union sctp_notification *) skb->data;
981*4882a593Smuzhiyun 	return notification->sn_header.sn_type;
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun /* RFC6458, Section 5.3.2. SCTP Header Information Structure
985*4882a593Smuzhiyun  * (SCTP_SNDRCV, DEPRECATED)
986*4882a593Smuzhiyun  */
sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent * event,struct msghdr * msghdr)987*4882a593Smuzhiyun void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
988*4882a593Smuzhiyun 				   struct msghdr *msghdr)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun 	struct sctp_sndrcvinfo sinfo;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	if (sctp_ulpevent_is_notification(event))
993*4882a593Smuzhiyun 		return;
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	memset(&sinfo, 0, sizeof(sinfo));
996*4882a593Smuzhiyun 	sinfo.sinfo_stream = event->stream;
997*4882a593Smuzhiyun 	sinfo.sinfo_ssn = event->ssn;
998*4882a593Smuzhiyun 	sinfo.sinfo_ppid = event->ppid;
999*4882a593Smuzhiyun 	sinfo.sinfo_flags = event->flags;
1000*4882a593Smuzhiyun 	sinfo.sinfo_tsn = event->tsn;
1001*4882a593Smuzhiyun 	sinfo.sinfo_cumtsn = event->cumtsn;
1002*4882a593Smuzhiyun 	sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
1003*4882a593Smuzhiyun 	/* Context value that is set via SCTP_CONTEXT socket option. */
1004*4882a593Smuzhiyun 	sinfo.sinfo_context = event->asoc->default_rcv_context;
1005*4882a593Smuzhiyun 	/* These fields are not used while receiving. */
1006*4882a593Smuzhiyun 	sinfo.sinfo_timetolive = 0;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
1009*4882a593Smuzhiyun 		 sizeof(sinfo), &sinfo);
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun /* RFC6458, Section 5.3.5 SCTP Receive Information Structure
1013*4882a593Smuzhiyun  * (SCTP_SNDRCV)
1014*4882a593Smuzhiyun  */
sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent * event,struct msghdr * msghdr)1015*4882a593Smuzhiyun void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
1016*4882a593Smuzhiyun 				struct msghdr *msghdr)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun 	struct sctp_rcvinfo rinfo;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	if (sctp_ulpevent_is_notification(event))
1021*4882a593Smuzhiyun 		return;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	memset(&rinfo, 0, sizeof(struct sctp_rcvinfo));
1024*4882a593Smuzhiyun 	rinfo.rcv_sid = event->stream;
1025*4882a593Smuzhiyun 	rinfo.rcv_ssn = event->ssn;
1026*4882a593Smuzhiyun 	rinfo.rcv_ppid = event->ppid;
1027*4882a593Smuzhiyun 	rinfo.rcv_flags = event->flags;
1028*4882a593Smuzhiyun 	rinfo.rcv_tsn = event->tsn;
1029*4882a593Smuzhiyun 	rinfo.rcv_cumtsn = event->cumtsn;
1030*4882a593Smuzhiyun 	rinfo.rcv_assoc_id = sctp_assoc2id(event->asoc);
1031*4882a593Smuzhiyun 	rinfo.rcv_context = event->asoc->default_rcv_context;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	put_cmsg(msghdr, IPPROTO_SCTP, SCTP_RCVINFO,
1034*4882a593Smuzhiyun 		 sizeof(rinfo), &rinfo);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun /* RFC6458, Section 5.3.6. SCTP Next Receive Information Structure
1038*4882a593Smuzhiyun  * (SCTP_NXTINFO)
1039*4882a593Smuzhiyun  */
__sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent * event,struct msghdr * msghdr,const struct sk_buff * skb)1040*4882a593Smuzhiyun static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
1041*4882a593Smuzhiyun 					 struct msghdr *msghdr,
1042*4882a593Smuzhiyun 					 const struct sk_buff *skb)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	struct sctp_nxtinfo nxtinfo;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	memset(&nxtinfo, 0, sizeof(nxtinfo));
1047*4882a593Smuzhiyun 	nxtinfo.nxt_sid = event->stream;
1048*4882a593Smuzhiyun 	nxtinfo.nxt_ppid = event->ppid;
1049*4882a593Smuzhiyun 	nxtinfo.nxt_flags = event->flags;
1050*4882a593Smuzhiyun 	if (sctp_ulpevent_is_notification(event))
1051*4882a593Smuzhiyun 		nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
1052*4882a593Smuzhiyun 	nxtinfo.nxt_length = skb->len;
1053*4882a593Smuzhiyun 	nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
1056*4882a593Smuzhiyun 		 sizeof(nxtinfo), &nxtinfo);
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun 
sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent * event,struct msghdr * msghdr,struct sock * sk)1059*4882a593Smuzhiyun void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
1060*4882a593Smuzhiyun 				struct msghdr *msghdr,
1061*4882a593Smuzhiyun 				struct sock *sk)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun 	struct sk_buff *skb;
1064*4882a593Smuzhiyun 	int err;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	skb = sctp_skb_recv_datagram(sk, MSG_PEEK, 1, &err);
1067*4882a593Smuzhiyun 	if (skb != NULL) {
1068*4882a593Smuzhiyun 		__sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
1069*4882a593Smuzhiyun 					     msghdr, skb);
1070*4882a593Smuzhiyun 		/* Just release refcount here. */
1071*4882a593Smuzhiyun 		kfree_skb(skb);
1072*4882a593Smuzhiyun 	}
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun /* Do accounting for bytes received and hold a reference to the association
1076*4882a593Smuzhiyun  * for each skb.
1077*4882a593Smuzhiyun  */
sctp_ulpevent_receive_data(struct sctp_ulpevent * event,struct sctp_association * asoc)1078*4882a593Smuzhiyun static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
1079*4882a593Smuzhiyun 				       struct sctp_association *asoc)
1080*4882a593Smuzhiyun {
1081*4882a593Smuzhiyun 	struct sk_buff *skb, *frag;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
1084*4882a593Smuzhiyun 	/* Set the owner and charge rwnd for bytes received.  */
1085*4882a593Smuzhiyun 	sctp_ulpevent_set_owner(event, asoc);
1086*4882a593Smuzhiyun 	sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	if (!skb->data_len)
1089*4882a593Smuzhiyun 		return;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	/* Note:  Not clearing the entire event struct as this is just a
1092*4882a593Smuzhiyun 	 * fragment of the real event.  However, we still need to do rwnd
1093*4882a593Smuzhiyun 	 * accounting.
1094*4882a593Smuzhiyun 	 * In general, the skb passed from IP can have only 1 level of
1095*4882a593Smuzhiyun 	 * fragments. But we allow multiple levels of fragments.
1096*4882a593Smuzhiyun 	 */
1097*4882a593Smuzhiyun 	skb_walk_frags(skb, frag)
1098*4882a593Smuzhiyun 		sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
1099*4882a593Smuzhiyun }
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun /* Do accounting for bytes just read by user and release the references to
1102*4882a593Smuzhiyun  * the association.
1103*4882a593Smuzhiyun  */
sctp_ulpevent_release_data(struct sctp_ulpevent * event)1104*4882a593Smuzhiyun static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun 	struct sk_buff *skb, *frag;
1107*4882a593Smuzhiyun 	unsigned int	len;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	/* Current stack structures assume that the rcv buffer is
1110*4882a593Smuzhiyun 	 * per socket.   For UDP style sockets this is not true as
1111*4882a593Smuzhiyun 	 * multiple associations may be on a single UDP-style socket.
1112*4882a593Smuzhiyun 	 * Use the local private area of the skb to track the owning
1113*4882a593Smuzhiyun 	 * association.
1114*4882a593Smuzhiyun 	 */
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
1117*4882a593Smuzhiyun 	len = skb->len;
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	if (!skb->data_len)
1120*4882a593Smuzhiyun 		goto done;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	/* Don't forget the fragments. */
1123*4882a593Smuzhiyun 	skb_walk_frags(skb, frag) {
1124*4882a593Smuzhiyun 		/* NOTE:  skb_shinfos are recursive. Although IP returns
1125*4882a593Smuzhiyun 		 * skb's with only 1 level of fragments, SCTP reassembly can
1126*4882a593Smuzhiyun 		 * increase the levels.
1127*4882a593Smuzhiyun 		 */
1128*4882a593Smuzhiyun 		sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1129*4882a593Smuzhiyun 	}
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun done:
1132*4882a593Smuzhiyun 	sctp_assoc_rwnd_increase(event->asoc, len);
1133*4882a593Smuzhiyun 	sctp_chunk_put(event->chunk);
1134*4882a593Smuzhiyun 	sctp_ulpevent_release_owner(event);
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun 
sctp_ulpevent_release_frag_data(struct sctp_ulpevent * event)1137*4882a593Smuzhiyun static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	struct sk_buff *skb, *frag;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	skb = sctp_event2skb(event);
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	if (!skb->data_len)
1144*4882a593Smuzhiyun 		goto done;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	/* Don't forget the fragments. */
1147*4882a593Smuzhiyun 	skb_walk_frags(skb, frag) {
1148*4882a593Smuzhiyun 		/* NOTE:  skb_shinfos are recursive. Although IP returns
1149*4882a593Smuzhiyun 		 * skb's with only 1 level of fragments, SCTP reassembly can
1150*4882a593Smuzhiyun 		 * increase the levels.
1151*4882a593Smuzhiyun 		 */
1152*4882a593Smuzhiyun 		sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1153*4882a593Smuzhiyun 	}
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun done:
1156*4882a593Smuzhiyun 	sctp_chunk_put(event->chunk);
1157*4882a593Smuzhiyun 	sctp_ulpevent_release_owner(event);
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun /* Free a ulpevent that has an owner.  It includes releasing the reference
1161*4882a593Smuzhiyun  * to the owner, updating the rwnd in case of a DATA event and freeing the
1162*4882a593Smuzhiyun  * skb.
1163*4882a593Smuzhiyun  */
sctp_ulpevent_free(struct sctp_ulpevent * event)1164*4882a593Smuzhiyun void sctp_ulpevent_free(struct sctp_ulpevent *event)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun 	if (sctp_ulpevent_is_notification(event))
1167*4882a593Smuzhiyun 		sctp_ulpevent_release_owner(event);
1168*4882a593Smuzhiyun 	else
1169*4882a593Smuzhiyun 		sctp_ulpevent_release_data(event);
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	kfree_skb(sctp_event2skb(event));
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun /* Purge the skb lists holding ulpevents. */
sctp_queue_purge_ulpevents(struct sk_buff_head * list)1175*4882a593Smuzhiyun unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1176*4882a593Smuzhiyun {
1177*4882a593Smuzhiyun 	struct sk_buff *skb;
1178*4882a593Smuzhiyun 	unsigned int data_unread = 0;
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	while ((skb = skb_dequeue(list)) != NULL) {
1181*4882a593Smuzhiyun 		struct sctp_ulpevent *event = sctp_skb2event(skb);
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 		if (!sctp_ulpevent_is_notification(event))
1184*4882a593Smuzhiyun 			data_unread += skb->len;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 		sctp_ulpevent_free(event);
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	return data_unread;
1190*4882a593Smuzhiyun }
1191