Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* SCTP kernel implementation
0003  * (C) Copyright IBM Corp. 2001, 2004
0004  * Copyright (c) 1999-2000 Cisco, Inc.
0005  * Copyright (c) 1999-2001 Motorola, Inc.
0006  * Copyright (c) 2001 Intel Corp.
0007  * Copyright (c) 2001 Nokia, Inc.
0008  * Copyright (c) 2001 La Monte H.P. Yarroll
0009  *
0010  * These functions manipulate an sctp event.   The struct ulpevent is used
0011  * to carry notifications and data to the ULP (sockets).
0012  *
0013  * Please send any bug reports or fixes you make to the
0014  * email address(es):
0015  *    lksctp developers <linux-sctp@vger.kernel.org>
0016  *
0017  * Written or modified by:
0018  *    Jon Grimm             <jgrimm@us.ibm.com>
0019  *    La Monte H.P. Yarroll <piggy@acm.org>
0020  *    Ardelle Fan       <ardelle.fan@intel.com>
0021  *    Sridhar Samudrala     <sri@us.ibm.com>
0022  */
0023 
0024 #include <linux/slab.h>
0025 #include <linux/types.h>
0026 #include <linux/skbuff.h>
0027 #include <net/sctp/structs.h>
0028 #include <net/sctp/sctp.h>
0029 #include <net/sctp/sm.h>
0030 
0031 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
0032                        struct sctp_association *asoc);
0033 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
0034 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
0035 
0036 
0037 /* Initialize an ULP event from an given skb.  */
0038 static void sctp_ulpevent_init(struct sctp_ulpevent *event,
0039                    __u16 msg_flags,
0040                    unsigned int len)
0041 {
0042     memset(event, 0, sizeof(struct sctp_ulpevent));
0043     event->msg_flags = msg_flags;
0044     event->rmem_len = len;
0045 }
0046 
0047 /* Create a new sctp_ulpevent.  */
0048 static struct sctp_ulpevent *sctp_ulpevent_new(int size, __u16 msg_flags,
0049                            gfp_t gfp)
0050 {
0051     struct sctp_ulpevent *event;
0052     struct sk_buff *skb;
0053 
0054     skb = alloc_skb(size, gfp);
0055     if (!skb)
0056         goto fail;
0057 
0058     event = sctp_skb2event(skb);
0059     sctp_ulpevent_init(event, msg_flags, skb->truesize);
0060 
0061     return event;
0062 
0063 fail:
0064     return NULL;
0065 }
0066 
0067 /* Is this a MSG_NOTIFICATION?  */
0068 int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
0069 {
0070     return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
0071 }
0072 
0073 /* Hold the association in case the msg_name needs read out of
0074  * the association.
0075  */
0076 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
0077                        const struct sctp_association *asoc)
0078 {
0079     struct sctp_chunk *chunk = event->chunk;
0080     struct sk_buff *skb;
0081 
0082     /* Cast away the const, as we are just wanting to
0083      * bump the reference count.
0084      */
0085     sctp_association_hold((struct sctp_association *)asoc);
0086     skb = sctp_event2skb(event);
0087     event->asoc = (struct sctp_association *)asoc;
0088     atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
0089     sctp_skb_set_owner_r(skb, asoc->base.sk);
0090     if (chunk && chunk->head_skb && !chunk->head_skb->sk)
0091         chunk->head_skb->sk = asoc->base.sk;
0092 }
0093 
0094 /* A simple destructor to give up the reference to the association. */
0095 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
0096 {
0097     struct sctp_association *asoc = event->asoc;
0098 
0099     atomic_sub(event->rmem_len, &asoc->rmem_alloc);
0100     sctp_association_put(asoc);
0101 }
0102 
0103 /* Create and initialize an SCTP_ASSOC_CHANGE event.
0104  *
0105  * 5.3.1.1 SCTP_ASSOC_CHANGE
0106  *
0107  * Communication notifications inform the ULP that an SCTP association
0108  * has either begun or ended. The identifier for a new association is
0109  * provided by this notification.
0110  *
0111  * Note: There is no field checking here.  If a field is unused it will be
0112  * zero'd out.
0113  */
0114 struct sctp_ulpevent  *sctp_ulpevent_make_assoc_change(
0115     const struct sctp_association *asoc,
0116     __u16 flags, __u16 state, __u16 error, __u16 outbound,
0117     __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
0118 {
0119     struct sctp_ulpevent *event;
0120     struct sctp_assoc_change *sac;
0121     struct sk_buff *skb;
0122 
0123     /* If the lower layer passed in the chunk, it will be
0124      * an ABORT, so we need to include it in the sac_info.
0125      */
0126     if (chunk) {
0127         /* Copy the chunk data to a new skb and reserve enough
0128          * head room to use as notification.
0129          */
0130         skb = skb_copy_expand(chunk->skb,
0131                       sizeof(struct sctp_assoc_change), 0, gfp);
0132 
0133         if (!skb)
0134             goto fail;
0135 
0136         /* Embed the event fields inside the cloned skb.  */
0137         event = sctp_skb2event(skb);
0138         sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
0139 
0140         /* Include the notification structure */
0141         sac = skb_push(skb, sizeof(struct sctp_assoc_change));
0142 
0143         /* Trim the buffer to the right length.  */
0144         skb_trim(skb, sizeof(struct sctp_assoc_change) +
0145              ntohs(chunk->chunk_hdr->length) -
0146              sizeof(struct sctp_chunkhdr));
0147     } else {
0148         event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
0149                   MSG_NOTIFICATION, gfp);
0150         if (!event)
0151             goto fail;
0152 
0153         skb = sctp_event2skb(event);
0154         sac = skb_put(skb, sizeof(struct sctp_assoc_change));
0155     }
0156 
0157     /* Socket Extensions for SCTP
0158      * 5.3.1.1 SCTP_ASSOC_CHANGE
0159      *
0160      * sac_type:
0161      * It should be SCTP_ASSOC_CHANGE.
0162      */
0163     sac->sac_type = SCTP_ASSOC_CHANGE;
0164 
0165     /* Socket Extensions for SCTP
0166      * 5.3.1.1 SCTP_ASSOC_CHANGE
0167      *
0168      * sac_state: 32 bits (signed integer)
0169      * This field holds one of a number of values that communicate the
0170      * event that happened to the association.
0171      */
0172     sac->sac_state = state;
0173 
0174     /* Socket Extensions for SCTP
0175      * 5.3.1.1 SCTP_ASSOC_CHANGE
0176      *
0177      * sac_flags: 16 bits (unsigned integer)
0178      * Currently unused.
0179      */
0180     sac->sac_flags = 0;
0181 
0182     /* Socket Extensions for SCTP
0183      * 5.3.1.1 SCTP_ASSOC_CHANGE
0184      *
0185      * sac_length: sizeof (__u32)
0186      * This field is the total length of the notification data, including
0187      * the notification header.
0188      */
0189     sac->sac_length = skb->len;
0190 
0191     /* Socket Extensions for SCTP
0192      * 5.3.1.1 SCTP_ASSOC_CHANGE
0193      *
0194      * sac_error:  32 bits (signed integer)
0195      *
0196      * If the state was reached due to a error condition (e.g.
0197      * COMMUNICATION_LOST) any relevant error information is available in
0198      * this field. This corresponds to the protocol error codes defined in
0199      * [SCTP].
0200      */
0201     sac->sac_error = error;
0202 
0203     /* Socket Extensions for SCTP
0204      * 5.3.1.1 SCTP_ASSOC_CHANGE
0205      *
0206      * sac_outbound_streams:  16 bits (unsigned integer)
0207      * sac_inbound_streams:  16 bits (unsigned integer)
0208      *
0209      * The maximum number of streams allowed in each direction are
0210      * available in sac_outbound_streams and sac_inbound streams.
0211      */
0212     sac->sac_outbound_streams = outbound;
0213     sac->sac_inbound_streams = inbound;
0214 
0215     /* Socket Extensions for SCTP
0216      * 5.3.1.1 SCTP_ASSOC_CHANGE
0217      *
0218      * sac_assoc_id: sizeof (sctp_assoc_t)
0219      *
0220      * The association id field, holds the identifier for the association.
0221      * All notifications for a given association have the same association
0222      * identifier.  For TCP style socket, this field is ignored.
0223      */
0224     sctp_ulpevent_set_owner(event, asoc);
0225     sac->sac_assoc_id = sctp_assoc2id(asoc);
0226 
0227     return event;
0228 
0229 fail:
0230     return NULL;
0231 }
0232 
0233 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
0234  *
0235  * Socket Extensions for SCTP - draft-01
0236  * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0237  *
0238  * When a destination address on a multi-homed peer encounters a change
0239  * an interface details event is sent.
0240  */
0241 static struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
0242     const struct sctp_association *asoc,
0243     const struct sockaddr_storage *aaddr,
0244     int flags, int state, int error, gfp_t gfp)
0245 {
0246     struct sctp_ulpevent *event;
0247     struct sctp_paddr_change  *spc;
0248     struct sk_buff *skb;
0249 
0250     event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
0251                   MSG_NOTIFICATION, gfp);
0252     if (!event)
0253         goto fail;
0254 
0255     skb = sctp_event2skb(event);
0256     spc = skb_put(skb, sizeof(struct sctp_paddr_change));
0257 
0258     /* Sockets API Extensions for SCTP
0259      * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0260      *
0261      * spc_type:
0262      *
0263      *    It should be SCTP_PEER_ADDR_CHANGE.
0264      */
0265     spc->spc_type = SCTP_PEER_ADDR_CHANGE;
0266 
0267     /* Sockets API Extensions for SCTP
0268      * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0269      *
0270      * spc_length: sizeof (__u32)
0271      *
0272      * This field is the total length of the notification data, including
0273      * the notification header.
0274      */
0275     spc->spc_length = sizeof(struct sctp_paddr_change);
0276 
0277     /* Sockets API Extensions for SCTP
0278      * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0279      *
0280      * spc_flags: 16 bits (unsigned integer)
0281      * Currently unused.
0282      */
0283     spc->spc_flags = 0;
0284 
0285     /* Sockets API Extensions for SCTP
0286      * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0287      *
0288      * spc_state:  32 bits (signed integer)
0289      *
0290      * This field holds one of a number of values that communicate the
0291      * event that happened to the address.
0292      */
0293     spc->spc_state = state;
0294 
0295     /* Sockets API Extensions for SCTP
0296      * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0297      *
0298      * spc_error:  32 bits (signed integer)
0299      *
0300      * If the state was reached due to any error condition (e.g.
0301      * ADDRESS_UNREACHABLE) any relevant error information is available in
0302      * this field.
0303      */
0304     spc->spc_error = error;
0305 
0306     /* Socket Extensions for SCTP
0307      * 5.3.1.1 SCTP_ASSOC_CHANGE
0308      *
0309      * spc_assoc_id: sizeof (sctp_assoc_t)
0310      *
0311      * The association id field, holds the identifier for the association.
0312      * All notifications for a given association have the same association
0313      * identifier.  For TCP style socket, this field is ignored.
0314      */
0315     sctp_ulpevent_set_owner(event, asoc);
0316     spc->spc_assoc_id = sctp_assoc2id(asoc);
0317 
0318     /* Sockets API Extensions for SCTP
0319      * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
0320      *
0321      * spc_aaddr: sizeof (struct sockaddr_storage)
0322      *
0323      * The affected address field, holds the remote peer's address that is
0324      * encountering the change of state.
0325      */
0326     memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
0327 
0328     /* Map ipv4 address into v4-mapped-on-v6 address.  */
0329     sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_to_user(
0330                     sctp_sk(asoc->base.sk),
0331                     (union sctp_addr *)&spc->spc_aaddr);
0332 
0333     return event;
0334 
0335 fail:
0336     return NULL;
0337 }
0338 
0339 void sctp_ulpevent_notify_peer_addr_change(struct sctp_transport *transport,
0340                        int state, int error)
0341 {
0342     struct sctp_association *asoc = transport->asoc;
0343     struct sockaddr_storage addr;
0344     struct sctp_ulpevent *event;
0345 
0346     if (asoc->state < SCTP_STATE_ESTABLISHED)
0347         return;
0348 
0349     memset(&addr, 0, sizeof(struct sockaddr_storage));
0350     memcpy(&addr, &transport->ipaddr, transport->af_specific->sockaddr_len);
0351 
0352     event = sctp_ulpevent_make_peer_addr_change(asoc, &addr, 0, state,
0353                             error, GFP_ATOMIC);
0354     if (event)
0355         asoc->stream.si->enqueue_event(&asoc->ulpq, event);
0356 }
0357 
0358 /* Create and initialize an SCTP_REMOTE_ERROR notification.
0359  *
0360  * Note: This assumes that the chunk->skb->data already points to the
0361  * operation error payload.
0362  *
0363  * Socket Extensions for SCTP - draft-01
0364  * 5.3.1.3 SCTP_REMOTE_ERROR
0365  *
0366  * A remote peer may send an Operational Error message to its peer.
0367  * This message indicates a variety of error conditions on an
0368  * association. The entire error TLV as it appears on the wire is
0369  * included in a SCTP_REMOTE_ERROR event.  Please refer to the SCTP
0370  * specification [SCTP] and any extensions for a list of possible
0371  * error formats.
0372  */
0373 struct sctp_ulpevent *
0374 sctp_ulpevent_make_remote_error(const struct sctp_association *asoc,
0375                 struct sctp_chunk *chunk, __u16 flags,
0376                 gfp_t gfp)
0377 {
0378     struct sctp_remote_error *sre;
0379     struct sctp_ulpevent *event;
0380     struct sctp_errhdr *ch;
0381     struct sk_buff *skb;
0382     __be16 cause;
0383     int elen;
0384 
0385     ch = (struct sctp_errhdr *)(chunk->skb->data);
0386     cause = ch->cause;
0387     elen = SCTP_PAD4(ntohs(ch->length)) - sizeof(*ch);
0388 
0389     /* Pull off the ERROR header.  */
0390     skb_pull(chunk->skb, sizeof(*ch));
0391 
0392     /* Copy the skb to a new skb with room for us to prepend
0393      * notification with.
0394      */
0395     skb = skb_copy_expand(chunk->skb, sizeof(*sre), 0, gfp);
0396 
0397     /* Pull off the rest of the cause TLV from the chunk.  */
0398     skb_pull(chunk->skb, elen);
0399     if (!skb)
0400         goto fail;
0401 
0402     /* Embed the event fields inside the cloned skb.  */
0403     event = sctp_skb2event(skb);
0404     sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
0405 
0406     sre = skb_push(skb, sizeof(*sre));
0407 
0408     /* Trim the buffer to the right length.  */
0409     skb_trim(skb, sizeof(*sre) + elen);
0410 
0411     /* RFC6458, Section 6.1.3. SCTP_REMOTE_ERROR */
0412     memset(sre, 0, sizeof(*sre));
0413     sre->sre_type = SCTP_REMOTE_ERROR;
0414     sre->sre_flags = 0;
0415     sre->sre_length = skb->len;
0416     sre->sre_error = cause;
0417     sctp_ulpevent_set_owner(event, asoc);
0418     sre->sre_assoc_id = sctp_assoc2id(asoc);
0419 
0420     return event;
0421 fail:
0422     return NULL;
0423 }
0424 
0425 /* Create and initialize a SCTP_SEND_FAILED notification.
0426  *
0427  * Socket Extensions for SCTP - draft-01
0428  * 5.3.1.4 SCTP_SEND_FAILED
0429  */
0430 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
0431     const struct sctp_association *asoc, struct sctp_chunk *chunk,
0432     __u16 flags, __u32 error, gfp_t gfp)
0433 {
0434     struct sctp_ulpevent *event;
0435     struct sctp_send_failed *ssf;
0436     struct sk_buff *skb;
0437 
0438     /* Pull off any padding. */
0439     int len = ntohs(chunk->chunk_hdr->length);
0440 
0441     /* Make skb with more room so we can prepend notification.  */
0442     skb = skb_copy_expand(chunk->skb,
0443                   sizeof(struct sctp_send_failed), /* headroom */
0444                   0,                               /* tailroom */
0445                   gfp);
0446     if (!skb)
0447         goto fail;
0448 
0449     /* Pull off the common chunk header and DATA header.  */
0450     skb_pull(skb, sctp_datachk_len(&asoc->stream));
0451     len -= sctp_datachk_len(&asoc->stream);
0452 
0453     /* Embed the event fields inside the cloned skb.  */
0454     event = sctp_skb2event(skb);
0455     sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
0456 
0457     ssf = skb_push(skb, sizeof(struct sctp_send_failed));
0458 
0459     /* Socket Extensions for SCTP
0460      * 5.3.1.4 SCTP_SEND_FAILED
0461      *
0462      * ssf_type:
0463      * It should be SCTP_SEND_FAILED.
0464      */
0465     ssf->ssf_type = SCTP_SEND_FAILED;
0466 
0467     /* Socket Extensions for SCTP
0468      * 5.3.1.4 SCTP_SEND_FAILED
0469      *
0470      * ssf_flags: 16 bits (unsigned integer)
0471      * The flag value will take one of the following values
0472      *
0473      * SCTP_DATA_UNSENT - Indicates that the data was never put on
0474      *                    the wire.
0475      *
0476      * SCTP_DATA_SENT   - Indicates that the data was put on the wire.
0477      *                    Note that this does not necessarily mean that the
0478      *                    data was (or was not) successfully delivered.
0479      */
0480     ssf->ssf_flags = flags;
0481 
0482     /* Socket Extensions for SCTP
0483      * 5.3.1.4 SCTP_SEND_FAILED
0484      *
0485      * ssf_length: sizeof (__u32)
0486      * This field is the total length of the notification data, including
0487      * the notification header.
0488      */
0489     ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
0490     skb_trim(skb, ssf->ssf_length);
0491 
0492     /* Socket Extensions for SCTP
0493      * 5.3.1.4 SCTP_SEND_FAILED
0494      *
0495      * ssf_error: 16 bits (unsigned integer)
0496      * This value represents the reason why the send failed, and if set,
0497      * will be a SCTP protocol error code as defined in [SCTP] section
0498      * 3.3.10.
0499      */
0500     ssf->ssf_error = error;
0501 
0502     /* Socket Extensions for SCTP
0503      * 5.3.1.4 SCTP_SEND_FAILED
0504      *
0505      * ssf_info: sizeof (struct sctp_sndrcvinfo)
0506      * The original send information associated with the undelivered
0507      * message.
0508      */
0509     memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
0510 
0511     /* Per TSVWG discussion with Randy. Allow the application to
0512      * reassemble a fragmented message.
0513      */
0514     ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
0515 
0516     /* Socket Extensions for SCTP
0517      * 5.3.1.4 SCTP_SEND_FAILED
0518      *
0519      * ssf_assoc_id: sizeof (sctp_assoc_t)
0520      * The association id field, sf_assoc_id, holds the identifier for the
0521      * association.  All notifications for a given association have the
0522      * same association identifier.  For TCP style socket, this field is
0523      * ignored.
0524      */
0525     sctp_ulpevent_set_owner(event, asoc);
0526     ssf->ssf_assoc_id = sctp_assoc2id(asoc);
0527     return event;
0528 
0529 fail:
0530     return NULL;
0531 }
0532 
0533 struct sctp_ulpevent *sctp_ulpevent_make_send_failed_event(
0534     const struct sctp_association *asoc, struct sctp_chunk *chunk,
0535     __u16 flags, __u32 error, gfp_t gfp)
0536 {
0537     struct sctp_send_failed_event *ssf;
0538     struct sctp_ulpevent *event;
0539     struct sk_buff *skb;
0540     int len;
0541 
0542     skb = skb_copy_expand(chunk->skb, sizeof(*ssf), 0, gfp);
0543     if (!skb)
0544         return NULL;
0545 
0546     len = ntohs(chunk->chunk_hdr->length);
0547     len -= sctp_datachk_len(&asoc->stream);
0548 
0549     skb_pull(skb, sctp_datachk_len(&asoc->stream));
0550     event = sctp_skb2event(skb);
0551     sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
0552 
0553     ssf = skb_push(skb, sizeof(*ssf));
0554     ssf->ssf_type = SCTP_SEND_FAILED_EVENT;
0555     ssf->ssf_flags = flags;
0556     ssf->ssf_length = sizeof(*ssf) + len;
0557     skb_trim(skb, ssf->ssf_length);
0558     ssf->ssf_error = error;
0559 
0560     ssf->ssfe_info.snd_sid = chunk->sinfo.sinfo_stream;
0561     ssf->ssfe_info.snd_ppid = chunk->sinfo.sinfo_ppid;
0562     ssf->ssfe_info.snd_context = chunk->sinfo.sinfo_context;
0563     ssf->ssfe_info.snd_assoc_id = chunk->sinfo.sinfo_assoc_id;
0564     ssf->ssfe_info.snd_flags = chunk->chunk_hdr->flags;
0565 
0566     sctp_ulpevent_set_owner(event, asoc);
0567     ssf->ssf_assoc_id = sctp_assoc2id(asoc);
0568 
0569     return event;
0570 }
0571 
0572 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
0573  *
0574  * Socket Extensions for SCTP - draft-01
0575  * 5.3.1.5 SCTP_SHUTDOWN_EVENT
0576  */
0577 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
0578     const struct sctp_association *asoc,
0579     __u16 flags, gfp_t gfp)
0580 {
0581     struct sctp_ulpevent *event;
0582     struct sctp_shutdown_event *sse;
0583     struct sk_buff *skb;
0584 
0585     event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
0586                   MSG_NOTIFICATION, gfp);
0587     if (!event)
0588         goto fail;
0589 
0590     skb = sctp_event2skb(event);
0591     sse = skb_put(skb, sizeof(struct sctp_shutdown_event));
0592 
0593     /* Socket Extensions for SCTP
0594      * 5.3.1.5 SCTP_SHUTDOWN_EVENT
0595      *
0596      * sse_type
0597      * It should be SCTP_SHUTDOWN_EVENT
0598      */
0599     sse->sse_type = SCTP_SHUTDOWN_EVENT;
0600 
0601     /* Socket Extensions for SCTP
0602      * 5.3.1.5 SCTP_SHUTDOWN_EVENT
0603      *
0604      * sse_flags: 16 bits (unsigned integer)
0605      * Currently unused.
0606      */
0607     sse->sse_flags = 0;
0608 
0609     /* Socket Extensions for SCTP
0610      * 5.3.1.5 SCTP_SHUTDOWN_EVENT
0611      *
0612      * sse_length: sizeof (__u32)
0613      * This field is the total length of the notification data, including
0614      * the notification header.
0615      */
0616     sse->sse_length = sizeof(struct sctp_shutdown_event);
0617 
0618     /* Socket Extensions for SCTP
0619      * 5.3.1.5 SCTP_SHUTDOWN_EVENT
0620      *
0621      * sse_assoc_id: sizeof (sctp_assoc_t)
0622      * The association id field, holds the identifier for the association.
0623      * All notifications for a given association have the same association
0624      * identifier.  For TCP style socket, this field is ignored.
0625      */
0626     sctp_ulpevent_set_owner(event, asoc);
0627     sse->sse_assoc_id = sctp_assoc2id(asoc);
0628 
0629     return event;
0630 
0631 fail:
0632     return NULL;
0633 }
0634 
0635 /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
0636  *
0637  * Socket Extensions for SCTP
0638  * 5.3.1.6 SCTP_ADAPTATION_INDICATION
0639  */
0640 struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
0641     const struct sctp_association *asoc, gfp_t gfp)
0642 {
0643     struct sctp_ulpevent *event;
0644     struct sctp_adaptation_event *sai;
0645     struct sk_buff *skb;
0646 
0647     event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
0648                   MSG_NOTIFICATION, gfp);
0649     if (!event)
0650         goto fail;
0651 
0652     skb = sctp_event2skb(event);
0653     sai = skb_put(skb, sizeof(struct sctp_adaptation_event));
0654 
0655     sai->sai_type = SCTP_ADAPTATION_INDICATION;
0656     sai->sai_flags = 0;
0657     sai->sai_length = sizeof(struct sctp_adaptation_event);
0658     sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
0659     sctp_ulpevent_set_owner(event, asoc);
0660     sai->sai_assoc_id = sctp_assoc2id(asoc);
0661 
0662     return event;
0663 
0664 fail:
0665     return NULL;
0666 }
0667 
0668 /* A message has been received.  Package this message as a notification
0669  * to pass it to the upper layers.  Go ahead and calculate the sndrcvinfo
0670  * even if filtered out later.
0671  *
0672  * Socket Extensions for SCTP
0673  * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
0674  */
0675 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
0676                         struct sctp_chunk *chunk,
0677                         gfp_t gfp)
0678 {
0679     struct sctp_ulpevent *event = NULL;
0680     struct sk_buff *skb = chunk->skb;
0681     struct sock *sk = asoc->base.sk;
0682     size_t padding, datalen;
0683     int rx_count;
0684 
0685     /*
0686      * check to see if we need to make space for this
0687      * new skb, expand the rcvbuffer if needed, or drop
0688      * the frame
0689      */
0690     if (asoc->ep->rcvbuf_policy)
0691         rx_count = atomic_read(&asoc->rmem_alloc);
0692     else
0693         rx_count = atomic_read(&sk->sk_rmem_alloc);
0694 
0695     datalen = ntohs(chunk->chunk_hdr->length);
0696 
0697     if (rx_count >= sk->sk_rcvbuf || !sk_rmem_schedule(sk, skb, datalen))
0698         goto fail;
0699 
0700     /* Clone the original skb, sharing the data.  */
0701     skb = skb_clone(chunk->skb, gfp);
0702     if (!skb)
0703         goto fail;
0704 
0705     /* Now that all memory allocations for this chunk succeeded, we
0706      * can mark it as received so the tsn_map is updated correctly.
0707      */
0708     if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
0709                  ntohl(chunk->subh.data_hdr->tsn),
0710                  chunk->transport))
0711         goto fail_mark;
0712 
0713     /* First calculate the padding, so we don't inadvertently
0714      * pass up the wrong length to the user.
0715      *
0716      * RFC 2960 - Section 3.2  Chunk Field Descriptions
0717      *
0718      * The total length of a chunk(including Type, Length and Value fields)
0719      * MUST be a multiple of 4 bytes.  If the length of the chunk is not a
0720      * multiple of 4 bytes, the sender MUST pad the chunk with all zero
0721      * bytes and this padding is not included in the chunk length field.
0722      * The sender should never pad with more than 3 bytes.  The receiver
0723      * MUST ignore the padding bytes.
0724      */
0725     padding = SCTP_PAD4(datalen) - datalen;
0726 
0727     /* Fixup cloned skb with just this chunks data.  */
0728     skb_trim(skb, chunk->chunk_end - padding - skb->data);
0729 
0730     /* Embed the event fields inside the cloned skb.  */
0731     event = sctp_skb2event(skb);
0732 
0733     /* Initialize event with flags 0  and correct length
0734      * Since this is a clone of the original skb, only account for
0735      * the data of this chunk as other chunks will be accounted separately.
0736      */
0737     sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
0738 
0739     /* And hold the chunk as we need it for getting the IP headers
0740      * later in recvmsg
0741      */
0742     sctp_chunk_hold(chunk);
0743     event->chunk = chunk;
0744 
0745     sctp_ulpevent_receive_data(event, asoc);
0746 
0747     event->stream = ntohs(chunk->subh.data_hdr->stream);
0748     if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
0749         event->flags |= SCTP_UNORDERED;
0750         event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
0751     }
0752     event->tsn = ntohl(chunk->subh.data_hdr->tsn);
0753     event->msg_flags |= chunk->chunk_hdr->flags;
0754 
0755     return event;
0756 
0757 fail_mark:
0758     kfree_skb(skb);
0759 fail:
0760     return NULL;
0761 }
0762 
0763 /* Create a partial delivery related event.
0764  *
0765  * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
0766  *
0767  *   When a receiver is engaged in a partial delivery of a
0768  *   message this notification will be used to indicate
0769  *   various events.
0770  */
0771 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
0772                     const struct sctp_association *asoc,
0773                     __u32 indication, __u32 sid, __u32 seq,
0774                     __u32 flags, gfp_t gfp)
0775 {
0776     struct sctp_ulpevent *event;
0777     struct sctp_pdapi_event *pd;
0778     struct sk_buff *skb;
0779 
0780     event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
0781                   MSG_NOTIFICATION, gfp);
0782     if (!event)
0783         goto fail;
0784 
0785     skb = sctp_event2skb(event);
0786     pd = skb_put(skb, sizeof(struct sctp_pdapi_event));
0787 
0788     /* pdapi_type
0789      *   It should be SCTP_PARTIAL_DELIVERY_EVENT
0790      *
0791      * pdapi_flags: 16 bits (unsigned integer)
0792      *   Currently unused.
0793      */
0794     pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
0795     pd->pdapi_flags = flags;
0796     pd->pdapi_stream = sid;
0797     pd->pdapi_seq = seq;
0798 
0799     /* pdapi_length: 32 bits (unsigned integer)
0800      *
0801      * This field is the total length of the notification data, including
0802      * the notification header.  It will generally be sizeof (struct
0803      * sctp_pdapi_event).
0804      */
0805     pd->pdapi_length = sizeof(struct sctp_pdapi_event);
0806 
0807     /*  pdapi_indication: 32 bits (unsigned integer)
0808      *
0809      * This field holds the indication being sent to the application.
0810      */
0811     pd->pdapi_indication = indication;
0812 
0813     /*  pdapi_assoc_id: sizeof (sctp_assoc_t)
0814      *
0815      * The association id field, holds the identifier for the association.
0816      */
0817     sctp_ulpevent_set_owner(event, asoc);
0818     pd->pdapi_assoc_id = sctp_assoc2id(asoc);
0819 
0820     return event;
0821 fail:
0822     return NULL;
0823 }
0824 
0825 struct sctp_ulpevent *sctp_ulpevent_make_authkey(
0826     const struct sctp_association *asoc, __u16 key_id,
0827     __u32 indication, gfp_t gfp)
0828 {
0829     struct sctp_ulpevent *event;
0830     struct sctp_authkey_event *ak;
0831     struct sk_buff *skb;
0832 
0833     event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
0834                   MSG_NOTIFICATION, gfp);
0835     if (!event)
0836         goto fail;
0837 
0838     skb = sctp_event2skb(event);
0839     ak = skb_put(skb, sizeof(struct sctp_authkey_event));
0840 
0841     ak->auth_type = SCTP_AUTHENTICATION_EVENT;
0842     ak->auth_flags = 0;
0843     ak->auth_length = sizeof(struct sctp_authkey_event);
0844 
0845     ak->auth_keynumber = key_id;
0846     ak->auth_altkeynumber = 0;
0847     ak->auth_indication = indication;
0848 
0849     /*
0850      * The association id field, holds the identifier for the association.
0851      */
0852     sctp_ulpevent_set_owner(event, asoc);
0853     ak->auth_assoc_id = sctp_assoc2id(asoc);
0854 
0855     return event;
0856 fail:
0857     return NULL;
0858 }
0859 
0860 /*
0861  * Socket Extensions for SCTP
0862  * 6.3.10. SCTP_SENDER_DRY_EVENT
0863  */
0864 struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
0865     const struct sctp_association *asoc, gfp_t gfp)
0866 {
0867     struct sctp_ulpevent *event;
0868     struct sctp_sender_dry_event *sdry;
0869     struct sk_buff *skb;
0870 
0871     event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
0872                   MSG_NOTIFICATION, gfp);
0873     if (!event)
0874         return NULL;
0875 
0876     skb = sctp_event2skb(event);
0877     sdry = skb_put(skb, sizeof(struct sctp_sender_dry_event));
0878 
0879     sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
0880     sdry->sender_dry_flags = 0;
0881     sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
0882     sctp_ulpevent_set_owner(event, asoc);
0883     sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
0884 
0885     return event;
0886 }
0887 
0888 struct sctp_ulpevent *sctp_ulpevent_make_stream_reset_event(
0889     const struct sctp_association *asoc, __u16 flags, __u16 stream_num,
0890     __be16 *stream_list, gfp_t gfp)
0891 {
0892     struct sctp_stream_reset_event *sreset;
0893     struct sctp_ulpevent *event;
0894     struct sk_buff *skb;
0895     int length, i;
0896 
0897     length = sizeof(struct sctp_stream_reset_event) + 2 * stream_num;
0898     event = sctp_ulpevent_new(length, MSG_NOTIFICATION, gfp);
0899     if (!event)
0900         return NULL;
0901 
0902     skb = sctp_event2skb(event);
0903     sreset = skb_put(skb, length);
0904 
0905     sreset->strreset_type = SCTP_STREAM_RESET_EVENT;
0906     sreset->strreset_flags = flags;
0907     sreset->strreset_length = length;
0908     sctp_ulpevent_set_owner(event, asoc);
0909     sreset->strreset_assoc_id = sctp_assoc2id(asoc);
0910 
0911     for (i = 0; i < stream_num; i++)
0912         sreset->strreset_stream_list[i] = ntohs(stream_list[i]);
0913 
0914     return event;
0915 }
0916 
0917 struct sctp_ulpevent *sctp_ulpevent_make_assoc_reset_event(
0918     const struct sctp_association *asoc, __u16 flags, __u32 local_tsn,
0919     __u32 remote_tsn, gfp_t gfp)
0920 {
0921     struct sctp_assoc_reset_event *areset;
0922     struct sctp_ulpevent *event;
0923     struct sk_buff *skb;
0924 
0925     event = sctp_ulpevent_new(sizeof(struct sctp_assoc_reset_event),
0926                   MSG_NOTIFICATION, gfp);
0927     if (!event)
0928         return NULL;
0929 
0930     skb = sctp_event2skb(event);
0931     areset = skb_put(skb, sizeof(struct sctp_assoc_reset_event));
0932 
0933     areset->assocreset_type = SCTP_ASSOC_RESET_EVENT;
0934     areset->assocreset_flags = flags;
0935     areset->assocreset_length = sizeof(struct sctp_assoc_reset_event);
0936     sctp_ulpevent_set_owner(event, asoc);
0937     areset->assocreset_assoc_id = sctp_assoc2id(asoc);
0938     areset->assocreset_local_tsn = local_tsn;
0939     areset->assocreset_remote_tsn = remote_tsn;
0940 
0941     return event;
0942 }
0943 
0944 struct sctp_ulpevent *sctp_ulpevent_make_stream_change_event(
0945     const struct sctp_association *asoc, __u16 flags,
0946     __u32 strchange_instrms, __u32 strchange_outstrms, gfp_t gfp)
0947 {
0948     struct sctp_stream_change_event *schange;
0949     struct sctp_ulpevent *event;
0950     struct sk_buff *skb;
0951 
0952     event = sctp_ulpevent_new(sizeof(struct sctp_stream_change_event),
0953                   MSG_NOTIFICATION, gfp);
0954     if (!event)
0955         return NULL;
0956 
0957     skb = sctp_event2skb(event);
0958     schange = skb_put(skb, sizeof(struct sctp_stream_change_event));
0959 
0960     schange->strchange_type = SCTP_STREAM_CHANGE_EVENT;
0961     schange->strchange_flags = flags;
0962     schange->strchange_length = sizeof(struct sctp_stream_change_event);
0963     sctp_ulpevent_set_owner(event, asoc);
0964     schange->strchange_assoc_id = sctp_assoc2id(asoc);
0965     schange->strchange_instrms = strchange_instrms;
0966     schange->strchange_outstrms = strchange_outstrms;
0967 
0968     return event;
0969 }
0970 
0971 /* Return the notification type, assuming this is a notification
0972  * event.
0973  */
0974 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
0975 {
0976     union sctp_notification *notification;
0977     struct sk_buff *skb;
0978 
0979     skb = sctp_event2skb(event);
0980     notification = (union sctp_notification *) skb->data;
0981     return notification->sn_header.sn_type;
0982 }
0983 
0984 /* RFC6458, Section 5.3.2. SCTP Header Information Structure
0985  * (SCTP_SNDRCV, DEPRECATED)
0986  */
0987 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
0988                    struct msghdr *msghdr)
0989 {
0990     struct sctp_sndrcvinfo sinfo;
0991 
0992     if (sctp_ulpevent_is_notification(event))
0993         return;
0994 
0995     memset(&sinfo, 0, sizeof(sinfo));
0996     sinfo.sinfo_stream = event->stream;
0997     sinfo.sinfo_ssn = event->ssn;
0998     sinfo.sinfo_ppid = event->ppid;
0999     sinfo.sinfo_flags = event->flags;
1000     sinfo.sinfo_tsn = event->tsn;
1001     sinfo.sinfo_cumtsn = event->cumtsn;
1002     sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
1003     /* Context value that is set via SCTP_CONTEXT socket option. */
1004     sinfo.sinfo_context = event->asoc->default_rcv_context;
1005     /* These fields are not used while receiving. */
1006     sinfo.sinfo_timetolive = 0;
1007 
1008     put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
1009          sizeof(sinfo), &sinfo);
1010 }
1011 
1012 /* RFC6458, Section 5.3.5 SCTP Receive Information Structure
1013  * (SCTP_SNDRCV)
1014  */
1015 void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
1016                 struct msghdr *msghdr)
1017 {
1018     struct sctp_rcvinfo rinfo;
1019 
1020     if (sctp_ulpevent_is_notification(event))
1021         return;
1022 
1023     memset(&rinfo, 0, sizeof(struct sctp_rcvinfo));
1024     rinfo.rcv_sid = event->stream;
1025     rinfo.rcv_ssn = event->ssn;
1026     rinfo.rcv_ppid = event->ppid;
1027     rinfo.rcv_flags = event->flags;
1028     rinfo.rcv_tsn = event->tsn;
1029     rinfo.rcv_cumtsn = event->cumtsn;
1030     rinfo.rcv_assoc_id = sctp_assoc2id(event->asoc);
1031     rinfo.rcv_context = event->asoc->default_rcv_context;
1032 
1033     put_cmsg(msghdr, IPPROTO_SCTP, SCTP_RCVINFO,
1034          sizeof(rinfo), &rinfo);
1035 }
1036 
1037 /* RFC6458, Section 5.3.6. SCTP Next Receive Information Structure
1038  * (SCTP_NXTINFO)
1039  */
1040 static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
1041                      struct msghdr *msghdr,
1042                      const struct sk_buff *skb)
1043 {
1044     struct sctp_nxtinfo nxtinfo;
1045 
1046     memset(&nxtinfo, 0, sizeof(nxtinfo));
1047     nxtinfo.nxt_sid = event->stream;
1048     nxtinfo.nxt_ppid = event->ppid;
1049     nxtinfo.nxt_flags = event->flags;
1050     if (sctp_ulpevent_is_notification(event))
1051         nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
1052     nxtinfo.nxt_length = skb->len;
1053     nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
1054 
1055     put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
1056          sizeof(nxtinfo), &nxtinfo);
1057 }
1058 
1059 void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
1060                 struct msghdr *msghdr,
1061                 struct sock *sk)
1062 {
1063     struct sk_buff *skb;
1064     int err;
1065 
1066     skb = sctp_skb_recv_datagram(sk, MSG_PEEK | MSG_DONTWAIT, &err);
1067     if (skb != NULL) {
1068         __sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
1069                          msghdr, skb);
1070         /* Just release refcount here. */
1071         kfree_skb(skb);
1072     }
1073 }
1074 
1075 /* Do accounting for bytes received and hold a reference to the association
1076  * for each skb.
1077  */
1078 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
1079                        struct sctp_association *asoc)
1080 {
1081     struct sk_buff *skb, *frag;
1082 
1083     skb = sctp_event2skb(event);
1084     /* Set the owner and charge rwnd for bytes received.  */
1085     sctp_ulpevent_set_owner(event, asoc);
1086     sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
1087 
1088     if (!skb->data_len)
1089         return;
1090 
1091     /* Note:  Not clearing the entire event struct as this is just a
1092      * fragment of the real event.  However, we still need to do rwnd
1093      * accounting.
1094      * In general, the skb passed from IP can have only 1 level of
1095      * fragments. But we allow multiple levels of fragments.
1096      */
1097     skb_walk_frags(skb, frag)
1098         sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
1099 }
1100 
1101 /* Do accounting for bytes just read by user and release the references to
1102  * the association.
1103  */
1104 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
1105 {
1106     struct sk_buff *skb, *frag;
1107     unsigned int    len;
1108 
1109     /* Current stack structures assume that the rcv buffer is
1110      * per socket.   For UDP style sockets this is not true as
1111      * multiple associations may be on a single UDP-style socket.
1112      * Use the local private area of the skb to track the owning
1113      * association.
1114      */
1115 
1116     skb = sctp_event2skb(event);
1117     len = skb->len;
1118 
1119     if (!skb->data_len)
1120         goto done;
1121 
1122     /* Don't forget the fragments. */
1123     skb_walk_frags(skb, frag) {
1124         /* NOTE:  skb_shinfos are recursive. Although IP returns
1125          * skb's with only 1 level of fragments, SCTP reassembly can
1126          * increase the levels.
1127          */
1128         sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1129     }
1130 
1131 done:
1132     sctp_assoc_rwnd_increase(event->asoc, len);
1133     sctp_chunk_put(event->chunk);
1134     sctp_ulpevent_release_owner(event);
1135 }
1136 
1137 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1138 {
1139     struct sk_buff *skb, *frag;
1140 
1141     skb = sctp_event2skb(event);
1142 
1143     if (!skb->data_len)
1144         goto done;
1145 
1146     /* Don't forget the fragments. */
1147     skb_walk_frags(skb, frag) {
1148         /* NOTE:  skb_shinfos are recursive. Although IP returns
1149          * skb's with only 1 level of fragments, SCTP reassembly can
1150          * increase the levels.
1151          */
1152         sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1153     }
1154 
1155 done:
1156     sctp_chunk_put(event->chunk);
1157     sctp_ulpevent_release_owner(event);
1158 }
1159 
1160 /* Free a ulpevent that has an owner.  It includes releasing the reference
1161  * to the owner, updating the rwnd in case of a DATA event and freeing the
1162  * skb.
1163  */
1164 void sctp_ulpevent_free(struct sctp_ulpevent *event)
1165 {
1166     if (sctp_ulpevent_is_notification(event))
1167         sctp_ulpevent_release_owner(event);
1168     else
1169         sctp_ulpevent_release_data(event);
1170 
1171     kfree_skb(sctp_event2skb(event));
1172 }
1173 
1174 /* Purge the skb lists holding ulpevents. */
1175 unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1176 {
1177     struct sk_buff *skb;
1178     unsigned int data_unread = 0;
1179 
1180     while ((skb = skb_dequeue(list)) != NULL) {
1181         struct sctp_ulpevent *event = sctp_skb2event(skb);
1182 
1183         if (!sctp_ulpevent_is_notification(event))
1184             data_unread += skb->len;
1185 
1186         sctp_ulpevent_free(event);
1187     }
1188 
1189     return data_unread;
1190 }