/* SCTP kernel implementation1* (C) Copyright IBM Corp. 2001, 20042* Copyright (c) 1999-2000 Cisco, Inc.3* Copyright (c) 1999-2001 Motorola, Inc.4* Copyright (c) 2001 Intel Corp.5* Copyright (c) 2001 Nokia, Inc.6* Copyright (c) 2001 La Monte H.P. Yarroll7*8* These functions manipulate an sctp event. The struct ulpevent is used9* to carry notifications and data to the ULP (sockets).10*11* This SCTP implementation is free software;12* you can redistribute it and/or modify it under the terms of13* the GNU General Public License as published by14* the Free Software Foundation; either version 2, or (at your option)15* any later version.16*17* This SCTP implementation is distributed in the hope that it18* will be useful, but WITHOUT ANY WARRANTY; without even the implied19* ************************20* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.21* See the GNU General Public License for more details.22*23* You should have received a copy of the GNU General Public License24* along with GNU CC; see the file COPYING. If not, write to25* the Free Software Foundation, 59 Temple Place - Suite 330,26* Boston, MA 02111-1307, USA.27*28* Please send any bug reports or fixes you make to the29* email address(es):30* lksctp developers <[email protected]>31*32* Or submit a bug report through the following website:33* http://www.sf.net/projects/lksctp34*35* Written or modified by:36* Jon Grimm <[email protected]>37* La Monte H.P. Yarroll <[email protected]>38* Ardelle Fan <[email protected]>39* Sridhar Samudrala <[email protected]>40*41* Any bugs reported given to us we will try to fix... any fixes shared will42* be incorporated into the next SCTP release.43*/4445#include <linux/slab.h>46#include <linux/types.h>47#include <linux/skbuff.h>48#include <net/sctp/structs.h>49#include <net/sctp/sctp.h>50#include <net/sctp/sm.h>5152static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,53struct sctp_association *asoc);54static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);55static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);565758/* Initialize an ULP event from an given skb. */59SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,60int msg_flags,61unsigned int len)62{63memset(event, 0, sizeof(struct sctp_ulpevent));64event->msg_flags = msg_flags;65event->rmem_len = len;66}6768/* Create a new sctp_ulpevent. */69SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,70gfp_t gfp)71{72struct sctp_ulpevent *event;73struct sk_buff *skb;7475skb = alloc_skb(size, gfp);76if (!skb)77goto fail;7879event = sctp_skb2event(skb);80sctp_ulpevent_init(event, msg_flags, skb->truesize);8182return event;8384fail:85return NULL;86}8788/* Is this a MSG_NOTIFICATION? */89int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)90{91return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);92}9394/* Hold the association in case the msg_name needs read out of95* the association.96*/97static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,98const struct sctp_association *asoc)99{100struct sk_buff *skb;101102/* Cast away the const, as we are just wanting to103* bump the reference count.104*/105sctp_association_hold((struct sctp_association *)asoc);106skb = sctp_event2skb(event);107event->asoc = (struct sctp_association *)asoc;108atomic_add(event->rmem_len, &event->asoc->rmem_alloc);109sctp_skb_set_owner_r(skb, asoc->base.sk);110}111112/* A simple destructor to give up the reference to the association. */113static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)114{115struct sctp_association *asoc = event->asoc;116117atomic_sub(event->rmem_len, &asoc->rmem_alloc);118sctp_association_put(asoc);119}120121/* Create and initialize an SCTP_ASSOC_CHANGE event.122*123* 5.3.1.1 SCTP_ASSOC_CHANGE124*125* Communication notifications inform the ULP that an SCTP association126* has either begun or ended. The identifier for a new association is127* provided by this notification.128*129* Note: There is no field checking here. If a field is unused it will be130* zero'd out.131*/132struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(133const struct sctp_association *asoc,134__u16 flags, __u16 state, __u16 error, __u16 outbound,135__u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)136{137struct sctp_ulpevent *event;138struct sctp_assoc_change *sac;139struct sk_buff *skb;140141/* If the lower layer passed in the chunk, it will be142* an ABORT, so we need to include it in the sac_info.143*/144if (chunk) {145/* Copy the chunk data to a new skb and reserve enough146* head room to use as notification.147*/148skb = skb_copy_expand(chunk->skb,149sizeof(struct sctp_assoc_change), 0, gfp);150151if (!skb)152goto fail;153154/* Embed the event fields inside the cloned skb. */155event = sctp_skb2event(skb);156sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);157158/* Include the notification structure */159sac = (struct sctp_assoc_change *)160skb_push(skb, sizeof(struct sctp_assoc_change));161162/* Trim the buffer to the right length. */163skb_trim(skb, sizeof(struct sctp_assoc_change) +164ntohs(chunk->chunk_hdr->length) -165sizeof(sctp_chunkhdr_t));166} else {167event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),168MSG_NOTIFICATION, gfp);169if (!event)170goto fail;171172skb = sctp_event2skb(event);173sac = (struct sctp_assoc_change *) skb_put(skb,174sizeof(struct sctp_assoc_change));175}176177/* Socket Extensions for SCTP178* 5.3.1.1 SCTP_ASSOC_CHANGE179*180* sac_type:181* It should be SCTP_ASSOC_CHANGE.182*/183sac->sac_type = SCTP_ASSOC_CHANGE;184185/* Socket Extensions for SCTP186* 5.3.1.1 SCTP_ASSOC_CHANGE187*188* sac_state: 32 bits (signed integer)189* This field holds one of a number of values that communicate the190* event that happened to the association.191*/192sac->sac_state = state;193194/* Socket Extensions for SCTP195* 5.3.1.1 SCTP_ASSOC_CHANGE196*197* sac_flags: 16 bits (unsigned integer)198* Currently unused.199*/200sac->sac_flags = 0;201202/* Socket Extensions for SCTP203* 5.3.1.1 SCTP_ASSOC_CHANGE204*205* sac_length: sizeof (__u32)206* This field is the total length of the notification data, including207* the notification header.208*/209sac->sac_length = skb->len;210211/* Socket Extensions for SCTP212* 5.3.1.1 SCTP_ASSOC_CHANGE213*214* sac_error: 32 bits (signed integer)215*216* If the state was reached due to a error condition (e.g.217* COMMUNICATION_LOST) any relevant error information is available in218* this field. This corresponds to the protocol error codes defined in219* [SCTP].220*/221sac->sac_error = error;222223/* Socket Extensions for SCTP224* 5.3.1.1 SCTP_ASSOC_CHANGE225*226* sac_outbound_streams: 16 bits (unsigned integer)227* sac_inbound_streams: 16 bits (unsigned integer)228*229* The maximum number of streams allowed in each direction are230* available in sac_outbound_streams and sac_inbound streams.231*/232sac->sac_outbound_streams = outbound;233sac->sac_inbound_streams = inbound;234235/* Socket Extensions for SCTP236* 5.3.1.1 SCTP_ASSOC_CHANGE237*238* sac_assoc_id: sizeof (sctp_assoc_t)239*240* The association id field, holds the identifier for the association.241* All notifications for a given association have the same association242* identifier. For TCP style socket, this field is ignored.243*/244sctp_ulpevent_set_owner(event, asoc);245sac->sac_assoc_id = sctp_assoc2id(asoc);246247return event;248249fail:250return NULL;251}252253/* Create and initialize an SCTP_PEER_ADDR_CHANGE event.254*255* Socket Extensions for SCTP - draft-01256* 5.3.1.2 SCTP_PEER_ADDR_CHANGE257*258* When a destination address on a multi-homed peer encounters a change259* an interface details event is sent.260*/261struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(262const struct sctp_association *asoc,263const struct sockaddr_storage *aaddr,264int flags, int state, int error, gfp_t gfp)265{266struct sctp_ulpevent *event;267struct sctp_paddr_change *spc;268struct sk_buff *skb;269270event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),271MSG_NOTIFICATION, gfp);272if (!event)273goto fail;274275skb = sctp_event2skb(event);276spc = (struct sctp_paddr_change *)277skb_put(skb, sizeof(struct sctp_paddr_change));278279/* Sockets API Extensions for SCTP280* Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE281*282* spc_type:283*284* It should be SCTP_PEER_ADDR_CHANGE.285*/286spc->spc_type = SCTP_PEER_ADDR_CHANGE;287288/* Sockets API Extensions for SCTP289* Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE290*291* spc_length: sizeof (__u32)292*293* This field is the total length of the notification data, including294* the notification header.295*/296spc->spc_length = sizeof(struct sctp_paddr_change);297298/* Sockets API Extensions for SCTP299* Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE300*301* spc_flags: 16 bits (unsigned integer)302* Currently unused.303*/304spc->spc_flags = 0;305306/* Sockets API Extensions for SCTP307* Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE308*309* spc_state: 32 bits (signed integer)310*311* This field holds one of a number of values that communicate the312* event that happened to the address.313*/314spc->spc_state = state;315316/* Sockets API Extensions for SCTP317* Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE318*319* spc_error: 32 bits (signed integer)320*321* If the state was reached due to any error condition (e.g.322* ADDRESS_UNREACHABLE) any relevant error information is available in323* this field.324*/325spc->spc_error = error;326327/* Socket Extensions for SCTP328* 5.3.1.1 SCTP_ASSOC_CHANGE329*330* spc_assoc_id: sizeof (sctp_assoc_t)331*332* The association id field, holds the identifier for the association.333* All notifications for a given association have the same association334* identifier. For TCP style socket, this field is ignored.335*/336sctp_ulpevent_set_owner(event, asoc);337spc->spc_assoc_id = sctp_assoc2id(asoc);338339/* Sockets API Extensions for SCTP340* Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE341*342* spc_aaddr: sizeof (struct sockaddr_storage)343*344* The affected address field, holds the remote peer's address that is345* encountering the change of state.346*/347memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));348349/* Map ipv4 address into v4-mapped-on-v6 address. */350sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(351sctp_sk(asoc->base.sk),352(union sctp_addr *)&spc->spc_aaddr);353354return event;355356fail:357return NULL;358}359360/* Create and initialize an SCTP_REMOTE_ERROR notification.361*362* Note: This assumes that the chunk->skb->data already points to the363* operation error payload.364*365* Socket Extensions for SCTP - draft-01366* 5.3.1.3 SCTP_REMOTE_ERROR367*368* A remote peer may send an Operational Error message to its peer.369* This message indicates a variety of error conditions on an370* association. The entire error TLV as it appears on the wire is371* included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP372* specification [SCTP] and any extensions for a list of possible373* error formats.374*/375struct sctp_ulpevent *sctp_ulpevent_make_remote_error(376const struct sctp_association *asoc, struct sctp_chunk *chunk,377__u16 flags, gfp_t gfp)378{379struct sctp_ulpevent *event;380struct sctp_remote_error *sre;381struct sk_buff *skb;382sctp_errhdr_t *ch;383__be16 cause;384int elen;385386ch = (sctp_errhdr_t *)(chunk->skb->data);387cause = ch->cause;388elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);389390/* Pull off the ERROR header. */391skb_pull(chunk->skb, sizeof(sctp_errhdr_t));392393/* Copy the skb to a new skb with room for us to prepend394* notification with.395*/396skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),3970, gfp);398399/* Pull off the rest of the cause TLV from the chunk. */400skb_pull(chunk->skb, elen);401if (!skb)402goto fail;403404/* Embed the event fields inside the cloned skb. */405event = sctp_skb2event(skb);406sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);407408sre = (struct sctp_remote_error *)409skb_push(skb, sizeof(struct sctp_remote_error));410411/* Trim the buffer to the right length. */412skb_trim(skb, sizeof(struct sctp_remote_error) + elen);413414/* Socket Extensions for SCTP415* 5.3.1.3 SCTP_REMOTE_ERROR416*417* sre_type:418* It should be SCTP_REMOTE_ERROR.419*/420sre->sre_type = SCTP_REMOTE_ERROR;421422/*423* Socket Extensions for SCTP424* 5.3.1.3 SCTP_REMOTE_ERROR425*426* sre_flags: 16 bits (unsigned integer)427* Currently unused.428*/429sre->sre_flags = 0;430431/* Socket Extensions for SCTP432* 5.3.1.3 SCTP_REMOTE_ERROR433*434* sre_length: sizeof (__u32)435*436* This field is the total length of the notification data,437* including the notification header.438*/439sre->sre_length = skb->len;440441/* Socket Extensions for SCTP442* 5.3.1.3 SCTP_REMOTE_ERROR443*444* sre_error: 16 bits (unsigned integer)445* This value represents one of the Operational Error causes defined in446* the SCTP specification, in network byte order.447*/448sre->sre_error = cause;449450/* Socket Extensions for SCTP451* 5.3.1.3 SCTP_REMOTE_ERROR452*453* sre_assoc_id: sizeof (sctp_assoc_t)454*455* The association id field, holds the identifier for the association.456* All notifications for a given association have the same association457* identifier. For TCP style socket, this field is ignored.458*/459sctp_ulpevent_set_owner(event, asoc);460sre->sre_assoc_id = sctp_assoc2id(asoc);461462return event;463464fail:465return NULL;466}467468/* Create and initialize a SCTP_SEND_FAILED notification.469*470* Socket Extensions for SCTP - draft-01471* 5.3.1.4 SCTP_SEND_FAILED472*/473struct sctp_ulpevent *sctp_ulpevent_make_send_failed(474const struct sctp_association *asoc, struct sctp_chunk *chunk,475__u16 flags, __u32 error, gfp_t gfp)476{477struct sctp_ulpevent *event;478struct sctp_send_failed *ssf;479struct sk_buff *skb;480481/* Pull off any padding. */482int len = ntohs(chunk->chunk_hdr->length);483484/* Make skb with more room so we can prepend notification. */485skb = skb_copy_expand(chunk->skb,486sizeof(struct sctp_send_failed), /* headroom */4870, /* tailroom */488gfp);489if (!skb)490goto fail;491492/* Pull off the common chunk header and DATA header. */493skb_pull(skb, sizeof(struct sctp_data_chunk));494len -= sizeof(struct sctp_data_chunk);495496/* Embed the event fields inside the cloned skb. */497event = sctp_skb2event(skb);498sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);499500ssf = (struct sctp_send_failed *)501skb_push(skb, sizeof(struct sctp_send_failed));502503/* Socket Extensions for SCTP504* 5.3.1.4 SCTP_SEND_FAILED505*506* ssf_type:507* It should be SCTP_SEND_FAILED.508*/509ssf->ssf_type = SCTP_SEND_FAILED;510511/* Socket Extensions for SCTP512* 5.3.1.4 SCTP_SEND_FAILED513*514* ssf_flags: 16 bits (unsigned integer)515* The flag value will take one of the following values516*517* SCTP_DATA_UNSENT - Indicates that the data was never put on518* the wire.519*520* SCTP_DATA_SENT - Indicates that the data was put on the wire.521* Note that this does not necessarily mean that the522* data was (or was not) successfully delivered.523*/524ssf->ssf_flags = flags;525526/* Socket Extensions for SCTP527* 5.3.1.4 SCTP_SEND_FAILED528*529* ssf_length: sizeof (__u32)530* This field is the total length of the notification data, including531* the notification header.532*/533ssf->ssf_length = sizeof(struct sctp_send_failed) + len;534skb_trim(skb, ssf->ssf_length);535536/* Socket Extensions for SCTP537* 5.3.1.4 SCTP_SEND_FAILED538*539* ssf_error: 16 bits (unsigned integer)540* This value represents the reason why the send failed, and if set,541* will be a SCTP protocol error code as defined in [SCTP] section542* 3.3.10.543*/544ssf->ssf_error = error;545546/* Socket Extensions for SCTP547* 5.3.1.4 SCTP_SEND_FAILED548*549* ssf_info: sizeof (struct sctp_sndrcvinfo)550* The original send information associated with the undelivered551* message.552*/553memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));554555/* Per TSVWG discussion with Randy. Allow the application to556* reassemble a fragmented message.557*/558ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;559560/* Socket Extensions for SCTP561* 5.3.1.4 SCTP_SEND_FAILED562*563* ssf_assoc_id: sizeof (sctp_assoc_t)564* The association id field, sf_assoc_id, holds the identifier for the565* association. All notifications for a given association have the566* same association identifier. For TCP style socket, this field is567* ignored.568*/569sctp_ulpevent_set_owner(event, asoc);570ssf->ssf_assoc_id = sctp_assoc2id(asoc);571return event;572573fail:574return NULL;575}576577/* Create and initialize a SCTP_SHUTDOWN_EVENT notification.578*579* Socket Extensions for SCTP - draft-01580* 5.3.1.5 SCTP_SHUTDOWN_EVENT581*/582struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(583const struct sctp_association *asoc,584__u16 flags, gfp_t gfp)585{586struct sctp_ulpevent *event;587struct sctp_shutdown_event *sse;588struct sk_buff *skb;589590event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),591MSG_NOTIFICATION, gfp);592if (!event)593goto fail;594595skb = sctp_event2skb(event);596sse = (struct sctp_shutdown_event *)597skb_put(skb, sizeof(struct sctp_shutdown_event));598599/* Socket Extensions for SCTP600* 5.3.1.5 SCTP_SHUTDOWN_EVENT601*602* sse_type603* It should be SCTP_SHUTDOWN_EVENT604*/605sse->sse_type = SCTP_SHUTDOWN_EVENT;606607/* Socket Extensions for SCTP608* 5.3.1.5 SCTP_SHUTDOWN_EVENT609*610* sse_flags: 16 bits (unsigned integer)611* Currently unused.612*/613sse->sse_flags = 0;614615/* Socket Extensions for SCTP616* 5.3.1.5 SCTP_SHUTDOWN_EVENT617*618* sse_length: sizeof (__u32)619* This field is the total length of the notification data, including620* the notification header.621*/622sse->sse_length = sizeof(struct sctp_shutdown_event);623624/* Socket Extensions for SCTP625* 5.3.1.5 SCTP_SHUTDOWN_EVENT626*627* sse_assoc_id: sizeof (sctp_assoc_t)628* The association id field, holds the identifier for the association.629* All notifications for a given association have the same association630* identifier. For TCP style socket, this field is ignored.631*/632sctp_ulpevent_set_owner(event, asoc);633sse->sse_assoc_id = sctp_assoc2id(asoc);634635return event;636637fail:638return NULL;639}640641/* Create and initialize a SCTP_ADAPTATION_INDICATION notification.642*643* Socket Extensions for SCTP644* 5.3.1.6 SCTP_ADAPTATION_INDICATION645*/646struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(647const struct sctp_association *asoc, gfp_t gfp)648{649struct sctp_ulpevent *event;650struct sctp_adaptation_event *sai;651struct sk_buff *skb;652653event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),654MSG_NOTIFICATION, gfp);655if (!event)656goto fail;657658skb = sctp_event2skb(event);659sai = (struct sctp_adaptation_event *)660skb_put(skb, sizeof(struct sctp_adaptation_event));661662sai->sai_type = SCTP_ADAPTATION_INDICATION;663sai->sai_flags = 0;664sai->sai_length = sizeof(struct sctp_adaptation_event);665sai->sai_adaptation_ind = asoc->peer.adaptation_ind;666sctp_ulpevent_set_owner(event, asoc);667sai->sai_assoc_id = sctp_assoc2id(asoc);668669return event;670671fail:672return NULL;673}674675/* A message has been received. Package this message as a notification676* to pass it to the upper layers. Go ahead and calculate the sndrcvinfo677* even if filtered out later.678*679* Socket Extensions for SCTP680* 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)681*/682struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,683struct sctp_chunk *chunk,684gfp_t gfp)685{686struct sctp_ulpevent *event = NULL;687struct sk_buff *skb;688size_t padding, len;689int rx_count;690691/*692* check to see if we need to make space for this693* new skb, expand the rcvbuffer if needed, or drop694* the frame695*/696if (asoc->ep->rcvbuf_policy)697rx_count = atomic_read(&asoc->rmem_alloc);698else699rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);700701if (rx_count >= asoc->base.sk->sk_rcvbuf) {702703if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||704(!sk_rmem_schedule(asoc->base.sk, chunk->skb->truesize)))705goto fail;706}707708/* Clone the original skb, sharing the data. */709skb = skb_clone(chunk->skb, gfp);710if (!skb)711goto fail;712713/* Now that all memory allocations for this chunk succeeded, we714* can mark it as received so the tsn_map is updated correctly.715*/716if (sctp_tsnmap_mark(&asoc->peer.tsn_map,717ntohl(chunk->subh.data_hdr->tsn)))718goto fail_mark;719720/* First calculate the padding, so we don't inadvertently721* pass up the wrong length to the user.722*723* RFC 2960 - Section 3.2 Chunk Field Descriptions724*725* The total length of a chunk(including Type, Length and Value fields)726* MUST be a multiple of 4 bytes. If the length of the chunk is not a727* multiple of 4 bytes, the sender MUST pad the chunk with all zero728* bytes and this padding is not included in the chunk length field.729* The sender should never pad with more than 3 bytes. The receiver730* MUST ignore the padding bytes.731*/732len = ntohs(chunk->chunk_hdr->length);733padding = WORD_ROUND(len) - len;734735/* Fixup cloned skb with just this chunks data. */736skb_trim(skb, chunk->chunk_end - padding - skb->data);737738/* Embed the event fields inside the cloned skb. */739event = sctp_skb2event(skb);740741/* Initialize event with flags 0 and correct length742* Since this is a clone of the original skb, only account for743* the data of this chunk as other chunks will be accounted separately.744*/745sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));746747sctp_ulpevent_receive_data(event, asoc);748749event->stream = ntohs(chunk->subh.data_hdr->stream);750event->ssn = ntohs(chunk->subh.data_hdr->ssn);751event->ppid = chunk->subh.data_hdr->ppid;752if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {753event->flags |= SCTP_UNORDERED;754event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);755}756event->tsn = ntohl(chunk->subh.data_hdr->tsn);757event->msg_flags |= chunk->chunk_hdr->flags;758event->iif = sctp_chunk_iif(chunk);759760return event;761762fail_mark:763kfree_skb(skb);764fail:765return NULL;766}767768/* Create a partial delivery related event.769*770* 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT771*772* When a receiver is engaged in a partial delivery of a773* message this notification will be used to indicate774* various events.775*/776struct sctp_ulpevent *sctp_ulpevent_make_pdapi(777const struct sctp_association *asoc, __u32 indication,778gfp_t gfp)779{780struct sctp_ulpevent *event;781struct sctp_pdapi_event *pd;782struct sk_buff *skb;783784event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),785MSG_NOTIFICATION, gfp);786if (!event)787goto fail;788789skb = sctp_event2skb(event);790pd = (struct sctp_pdapi_event *)791skb_put(skb, sizeof(struct sctp_pdapi_event));792793/* pdapi_type794* It should be SCTP_PARTIAL_DELIVERY_EVENT795*796* pdapi_flags: 16 bits (unsigned integer)797* Currently unused.798*/799pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;800pd->pdapi_flags = 0;801802/* pdapi_length: 32 bits (unsigned integer)803*804* This field is the total length of the notification data, including805* the notification header. It will generally be sizeof (struct806* sctp_pdapi_event).807*/808pd->pdapi_length = sizeof(struct sctp_pdapi_event);809810/* pdapi_indication: 32 bits (unsigned integer)811*812* This field holds the indication being sent to the application.813*/814pd->pdapi_indication = indication;815816/* pdapi_assoc_id: sizeof (sctp_assoc_t)817*818* The association id field, holds the identifier for the association.819*/820sctp_ulpevent_set_owner(event, asoc);821pd->pdapi_assoc_id = sctp_assoc2id(asoc);822823return event;824fail:825return NULL;826}827828struct sctp_ulpevent *sctp_ulpevent_make_authkey(829const struct sctp_association *asoc, __u16 key_id,830__u32 indication, gfp_t gfp)831{832struct sctp_ulpevent *event;833struct sctp_authkey_event *ak;834struct sk_buff *skb;835836event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),837MSG_NOTIFICATION, gfp);838if (!event)839goto fail;840841skb = sctp_event2skb(event);842ak = (struct sctp_authkey_event *)843skb_put(skb, sizeof(struct sctp_authkey_event));844845ak->auth_type = SCTP_AUTHENTICATION_EVENT;846ak->auth_flags = 0;847ak->auth_length = sizeof(struct sctp_authkey_event);848849ak->auth_keynumber = key_id;850ak->auth_altkeynumber = 0;851ak->auth_indication = indication;852853/*854* The association id field, holds the identifier for the association.855*/856sctp_ulpevent_set_owner(event, asoc);857ak->auth_assoc_id = sctp_assoc2id(asoc);858859return event;860fail:861return NULL;862}863864/*865* Socket Extensions for SCTP866* 6.3.10. SCTP_SENDER_DRY_EVENT867*/868struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(869const struct sctp_association *asoc, gfp_t gfp)870{871struct sctp_ulpevent *event;872struct sctp_sender_dry_event *sdry;873struct sk_buff *skb;874875event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),876MSG_NOTIFICATION, gfp);877if (!event)878return NULL;879880skb = sctp_event2skb(event);881sdry = (struct sctp_sender_dry_event *)882skb_put(skb, sizeof(struct sctp_sender_dry_event));883884sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;885sdry->sender_dry_flags = 0;886sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);887sctp_ulpevent_set_owner(event, asoc);888sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);889890return event;891}892893/* Return the notification type, assuming this is a notification894* event.895*/896__u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)897{898union sctp_notification *notification;899struct sk_buff *skb;900901skb = sctp_event2skb(event);902notification = (union sctp_notification *) skb->data;903return notification->sn_header.sn_type;904}905906/* Copy out the sndrcvinfo into a msghdr. */907void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,908struct msghdr *msghdr)909{910struct sctp_sndrcvinfo sinfo;911912if (sctp_ulpevent_is_notification(event))913return;914915/* Sockets API Extensions for SCTP916* Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)917*918* sinfo_stream: 16 bits (unsigned integer)919*920* For recvmsg() the SCTP stack places the message's stream number in921* this value.922*/923sinfo.sinfo_stream = event->stream;924/* sinfo_ssn: 16 bits (unsigned integer)925*926* For recvmsg() this value contains the stream sequence number that927* the remote endpoint placed in the DATA chunk. For fragmented928* messages this is the same number for all deliveries of the message929* (if more than one recvmsg() is needed to read the message).930*/931sinfo.sinfo_ssn = event->ssn;932/* sinfo_ppid: 32 bits (unsigned integer)933*934* In recvmsg() this value is935* the same information that was passed by the upper layer in the peer936* application. Please note that byte order issues are NOT accounted937* for and this information is passed opaquely by the SCTP stack from938* one end to the other.939*/940sinfo.sinfo_ppid = event->ppid;941/* sinfo_flags: 16 bits (unsigned integer)942*943* This field may contain any of the following flags and is composed of944* a bitwise OR of these values.945*946* recvmsg() flags:947*948* SCTP_UNORDERED - This flag is present when the message was sent949* non-ordered.950*/951sinfo.sinfo_flags = event->flags;952/* sinfo_tsn: 32 bit (unsigned integer)953*954* For the receiving side, this field holds a TSN that was955* assigned to one of the SCTP Data Chunks.956*/957sinfo.sinfo_tsn = event->tsn;958/* sinfo_cumtsn: 32 bit (unsigned integer)959*960* This field will hold the current cumulative TSN as961* known by the underlying SCTP layer. Note this field is962* ignored when sending and only valid for a receive963* operation when sinfo_flags are set to SCTP_UNORDERED.964*/965sinfo.sinfo_cumtsn = event->cumtsn;966/* sinfo_assoc_id: sizeof (sctp_assoc_t)967*968* The association handle field, sinfo_assoc_id, holds the identifier969* for the association announced in the COMMUNICATION_UP notification.970* All notifications for a given association have the same identifier.971* Ignored for one-to-one style sockets.972*/973sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);974975/* context value that is set via SCTP_CONTEXT socket option. */976sinfo.sinfo_context = event->asoc->default_rcv_context;977978/* These fields are not used while receiving. */979sinfo.sinfo_timetolive = 0;980981put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,982sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);983}984985/* Do accounting for bytes received and hold a reference to the association986* for each skb.987*/988static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,989struct sctp_association *asoc)990{991struct sk_buff *skb, *frag;992993skb = sctp_event2skb(event);994/* Set the owner and charge rwnd for bytes received. */995sctp_ulpevent_set_owner(event, asoc);996sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));997998if (!skb->data_len)999return;10001001/* Note: Not clearing the entire event struct as this is just a1002* fragment of the real event. However, we still need to do rwnd1003* accounting.1004* In general, the skb passed from IP can have only 1 level of1005* fragments. But we allow multiple levels of fragments.1006*/1007skb_walk_frags(skb, frag)1008sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);1009}10101011/* Do accounting for bytes just read by user and release the references to1012* the association.1013*/1014static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)1015{1016struct sk_buff *skb, *frag;1017unsigned int len;10181019/* Current stack structures assume that the rcv buffer is1020* per socket. For UDP style sockets this is not true as1021* multiple associations may be on a single UDP-style socket.1022* Use the local private area of the skb to track the owning1023* association.1024*/10251026skb = sctp_event2skb(event);1027len = skb->len;10281029if (!skb->data_len)1030goto done;10311032/* Don't forget the fragments. */1033skb_walk_frags(skb, frag) {1034/* NOTE: skb_shinfos are recursive. Although IP returns1035* skb's with only 1 level of fragments, SCTP reassembly can1036* increase the levels.1037*/1038sctp_ulpevent_release_frag_data(sctp_skb2event(frag));1039}10401041done:1042sctp_assoc_rwnd_increase(event->asoc, len);1043sctp_ulpevent_release_owner(event);1044}10451046static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)1047{1048struct sk_buff *skb, *frag;10491050skb = sctp_event2skb(event);10511052if (!skb->data_len)1053goto done;10541055/* Don't forget the fragments. */1056skb_walk_frags(skb, frag) {1057/* NOTE: skb_shinfos are recursive. Although IP returns1058* skb's with only 1 level of fragments, SCTP reassembly can1059* increase the levels.1060*/1061sctp_ulpevent_release_frag_data(sctp_skb2event(frag));1062}10631064done:1065sctp_ulpevent_release_owner(event);1066}10671068/* Free a ulpevent that has an owner. It includes releasing the reference1069* to the owner, updating the rwnd in case of a DATA event and freeing the1070* skb.1071*/1072void sctp_ulpevent_free(struct sctp_ulpevent *event)1073{1074if (sctp_ulpevent_is_notification(event))1075sctp_ulpevent_release_owner(event);1076else1077sctp_ulpevent_release_data(event);10781079kfree_skb(sctp_event2skb(event));1080}10811082/* Purge the skb lists holding ulpevents. */1083unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)1084{1085struct sk_buff *skb;1086unsigned int data_unread = 0;10871088while ((skb = skb_dequeue(list)) != NULL) {1089struct sctp_ulpevent *event = sctp_skb2event(skb);10901091if (!sctp_ulpevent_is_notification(event))1092data_unread += skb->len;10931094sctp_ulpevent_free(event);1095}10961097return data_unread;1098}109911001101