12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076 |
- #include <linux/slab.h>
- #include <linux/types.h>
- #include <linux/skbuff.h>
- #include <net/sctp/structs.h>
- #include <net/sctp/sctp.h>
- #include <net/sctp/sm.h>
- static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
- struct sctp_association *asoc);
- static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
- static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
- static void sctp_ulpevent_init(struct sctp_ulpevent *event,
- __u16 msg_flags,
- unsigned int len)
- {
- memset(event, 0, sizeof(struct sctp_ulpevent));
- event->msg_flags = msg_flags;
- event->rmem_len = len;
- }
- static struct sctp_ulpevent *sctp_ulpevent_new(int size, __u16 msg_flags,
- gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sk_buff *skb;
- skb = alloc_skb(size, gfp);
- if (!skb)
- goto fail;
- event = sctp_skb2event(skb);
- sctp_ulpevent_init(event, msg_flags, skb->truesize);
- return event;
- fail:
- return NULL;
- }
- int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
- {
- return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
- }
- static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
- const struct sctp_association *asoc)
- {
- struct sctp_chunk *chunk = event->chunk;
- struct sk_buff *skb;
-
- sctp_association_hold((struct sctp_association *)asoc);
- skb = sctp_event2skb(event);
- event->asoc = (struct sctp_association *)asoc;
- atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
- sctp_skb_set_owner_r(skb, asoc->base.sk);
- if (chunk && chunk->head_skb && !chunk->head_skb->sk)
- chunk->head_skb->sk = asoc->base.sk;
- }
- static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
- {
- struct sctp_association *asoc = event->asoc;
- atomic_sub(event->rmem_len, &asoc->rmem_alloc);
- sctp_association_put(asoc);
- }
- struct sctp_ulpevent *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)
- {
- struct sctp_ulpevent *event;
- struct sctp_assoc_change *sac;
- struct sk_buff *skb;
-
- if (chunk) {
-
- skb = skb_copy_expand(chunk->skb,
- sizeof(struct sctp_assoc_change), 0, gfp);
- if (!skb)
- goto fail;
-
- event = sctp_skb2event(skb);
- sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
-
- sac = (struct sctp_assoc_change *)
- skb_push(skb, sizeof(struct sctp_assoc_change));
-
- skb_trim(skb, sizeof(struct sctp_assoc_change) +
- ntohs(chunk->chunk_hdr->length) -
- sizeof(sctp_chunkhdr_t));
- } else {
- event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
- MSG_NOTIFICATION, gfp);
- if (!event)
- goto fail;
- skb = sctp_event2skb(event);
- sac = (struct sctp_assoc_change *) skb_put(skb,
- sizeof(struct sctp_assoc_change));
- }
-
- sac->sac_type = SCTP_ASSOC_CHANGE;
-
- sac->sac_state = state;
-
- sac->sac_flags = 0;
-
- sac->sac_length = skb->len;
-
- sac->sac_error = error;
-
- sac->sac_outbound_streams = outbound;
- sac->sac_inbound_streams = inbound;
-
- sctp_ulpevent_set_owner(event, asoc);
- sac->sac_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *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)
- {
- struct sctp_ulpevent *event;
- struct sctp_paddr_change *spc;
- struct sk_buff *skb;
- event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
- MSG_NOTIFICATION, gfp);
- if (!event)
- goto fail;
- skb = sctp_event2skb(event);
- spc = (struct sctp_paddr_change *)
- skb_put(skb, sizeof(struct sctp_paddr_change));
-
- spc->spc_type = SCTP_PEER_ADDR_CHANGE;
-
- spc->spc_length = sizeof(struct sctp_paddr_change);
-
- spc->spc_flags = 0;
-
- spc->spc_state = state;
-
- spc->spc_error = error;
-
- sctp_ulpevent_set_owner(event, asoc);
- spc->spc_assoc_id = sctp_assoc2id(asoc);
-
- memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
-
- sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_to_user(
- sctp_sk(asoc->base.sk),
- (union sctp_addr *)&spc->spc_aaddr);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *
- sctp_ulpevent_make_remote_error(const struct sctp_association *asoc,
- struct sctp_chunk *chunk, __u16 flags,
- gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_remote_error *sre;
- struct sk_buff *skb;
- sctp_errhdr_t *ch;
- __be16 cause;
- int elen;
- ch = (sctp_errhdr_t *)(chunk->skb->data);
- cause = ch->cause;
- elen = SCTP_PAD4(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
-
- skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
-
- skb = skb_copy_expand(chunk->skb, sizeof(*sre), 0, gfp);
-
- skb_pull(chunk->skb, elen);
- if (!skb)
- goto fail;
-
- event = sctp_skb2event(skb);
- sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
- sre = (struct sctp_remote_error *) skb_push(skb, sizeof(*sre));
-
- skb_trim(skb, sizeof(*sre) + elen);
-
- memset(sre, 0, sizeof(*sre));
- sre->sre_type = SCTP_REMOTE_ERROR;
- sre->sre_flags = 0;
- sre->sre_length = skb->len;
- sre->sre_error = cause;
- sctp_ulpevent_set_owner(event, asoc);
- sre->sre_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
- const struct sctp_association *asoc, struct sctp_chunk *chunk,
- __u16 flags, __u32 error, gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_send_failed *ssf;
- struct sk_buff *skb;
-
- int len = ntohs(chunk->chunk_hdr->length);
-
- skb = skb_copy_expand(chunk->skb,
- sizeof(struct sctp_send_failed),
- 0,
- gfp);
- if (!skb)
- goto fail;
-
- skb_pull(skb, sizeof(struct sctp_data_chunk));
- len -= sizeof(struct sctp_data_chunk);
-
- event = sctp_skb2event(skb);
- sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
- ssf = (struct sctp_send_failed *)
- skb_push(skb, sizeof(struct sctp_send_failed));
-
- ssf->ssf_type = SCTP_SEND_FAILED;
-
- ssf->ssf_flags = flags;
-
- ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
- skb_trim(skb, ssf->ssf_length);
-
- ssf->ssf_error = error;
-
- memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
-
- ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
-
- sctp_ulpevent_set_owner(event, asoc);
- ssf->ssf_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
- const struct sctp_association *asoc,
- __u16 flags, gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_shutdown_event *sse;
- struct sk_buff *skb;
- event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
- MSG_NOTIFICATION, gfp);
- if (!event)
- goto fail;
- skb = sctp_event2skb(event);
- sse = (struct sctp_shutdown_event *)
- skb_put(skb, sizeof(struct sctp_shutdown_event));
-
- sse->sse_type = SCTP_SHUTDOWN_EVENT;
-
- sse->sse_flags = 0;
-
- sse->sse_length = sizeof(struct sctp_shutdown_event);
-
- sctp_ulpevent_set_owner(event, asoc);
- sse->sse_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
- const struct sctp_association *asoc, gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_adaptation_event *sai;
- struct sk_buff *skb;
- event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
- MSG_NOTIFICATION, gfp);
- if (!event)
- goto fail;
- skb = sctp_event2skb(event);
- sai = (struct sctp_adaptation_event *)
- skb_put(skb, sizeof(struct sctp_adaptation_event));
- sai->sai_type = SCTP_ADAPTATION_INDICATION;
- sai->sai_flags = 0;
- sai->sai_length = sizeof(struct sctp_adaptation_event);
- sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
- sctp_ulpevent_set_owner(event, asoc);
- sai->sai_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
- struct sctp_chunk *chunk,
- gfp_t gfp)
- {
- struct sctp_ulpevent *event = NULL;
- struct sk_buff *skb;
- size_t padding, len;
- int rx_count;
-
- if (asoc->ep->rcvbuf_policy)
- rx_count = atomic_read(&asoc->rmem_alloc);
- else
- rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
- if (rx_count >= asoc->base.sk->sk_rcvbuf) {
- if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
- (!sk_rmem_schedule(asoc->base.sk, chunk->skb,
- chunk->skb->truesize)))
- goto fail;
- }
-
- skb = skb_clone(chunk->skb, gfp);
- if (!skb)
- goto fail;
-
- if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
- ntohl(chunk->subh.data_hdr->tsn),
- chunk->transport))
- goto fail_mark;
-
- len = ntohs(chunk->chunk_hdr->length);
- padding = SCTP_PAD4(len) - len;
-
- skb_trim(skb, chunk->chunk_end - padding - skb->data);
-
- event = sctp_skb2event(skb);
-
- sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
-
- sctp_chunk_hold(chunk);
- event->chunk = chunk;
- sctp_ulpevent_receive_data(event, asoc);
- event->stream = ntohs(chunk->subh.data_hdr->stream);
- event->ssn = ntohs(chunk->subh.data_hdr->ssn);
- event->ppid = chunk->subh.data_hdr->ppid;
- if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
- event->flags |= SCTP_UNORDERED;
- event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
- }
- event->tsn = ntohl(chunk->subh.data_hdr->tsn);
- event->msg_flags |= chunk->chunk_hdr->flags;
- return event;
- fail_mark:
- kfree_skb(skb);
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
- const struct sctp_association *asoc, __u32 indication,
- gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_pdapi_event *pd;
- struct sk_buff *skb;
- event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
- MSG_NOTIFICATION, gfp);
- if (!event)
- goto fail;
- skb = sctp_event2skb(event);
- pd = (struct sctp_pdapi_event *)
- skb_put(skb, sizeof(struct sctp_pdapi_event));
-
- pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
- pd->pdapi_flags = 0;
-
- pd->pdapi_length = sizeof(struct sctp_pdapi_event);
-
- pd->pdapi_indication = indication;
-
- sctp_ulpevent_set_owner(event, asoc);
- pd->pdapi_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_authkey(
- const struct sctp_association *asoc, __u16 key_id,
- __u32 indication, gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_authkey_event *ak;
- struct sk_buff *skb;
- event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
- MSG_NOTIFICATION, gfp);
- if (!event)
- goto fail;
- skb = sctp_event2skb(event);
- ak = (struct sctp_authkey_event *)
- skb_put(skb, sizeof(struct sctp_authkey_event));
- ak->auth_type = SCTP_AUTHENTICATION_EVENT;
- ak->auth_flags = 0;
- ak->auth_length = sizeof(struct sctp_authkey_event);
- ak->auth_keynumber = key_id;
- ak->auth_altkeynumber = 0;
- ak->auth_indication = indication;
-
- sctp_ulpevent_set_owner(event, asoc);
- ak->auth_assoc_id = sctp_assoc2id(asoc);
- return event;
- fail:
- return NULL;
- }
- struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
- const struct sctp_association *asoc, gfp_t gfp)
- {
- struct sctp_ulpevent *event;
- struct sctp_sender_dry_event *sdry;
- struct sk_buff *skb;
- event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
- MSG_NOTIFICATION, gfp);
- if (!event)
- return NULL;
- skb = sctp_event2skb(event);
- sdry = (struct sctp_sender_dry_event *)
- skb_put(skb, sizeof(struct sctp_sender_dry_event));
- sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
- sdry->sender_dry_flags = 0;
- sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
- sctp_ulpevent_set_owner(event, asoc);
- sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
- return event;
- }
- __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
- {
- union sctp_notification *notification;
- struct sk_buff *skb;
- skb = sctp_event2skb(event);
- notification = (union sctp_notification *) skb->data;
- return notification->sn_header.sn_type;
- }
- void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
- struct msghdr *msghdr)
- {
- struct sctp_sndrcvinfo sinfo;
- if (sctp_ulpevent_is_notification(event))
- return;
- memset(&sinfo, 0, sizeof(sinfo));
- sinfo.sinfo_stream = event->stream;
- sinfo.sinfo_ssn = event->ssn;
- sinfo.sinfo_ppid = event->ppid;
- sinfo.sinfo_flags = event->flags;
- sinfo.sinfo_tsn = event->tsn;
- sinfo.sinfo_cumtsn = event->cumtsn;
- sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
-
- sinfo.sinfo_context = event->asoc->default_rcv_context;
-
- sinfo.sinfo_timetolive = 0;
- put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
- sizeof(sinfo), &sinfo);
- }
- void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
- struct msghdr *msghdr)
- {
- struct sctp_rcvinfo rinfo;
- if (sctp_ulpevent_is_notification(event))
- return;
- memset(&rinfo, 0, sizeof(struct sctp_rcvinfo));
- rinfo.rcv_sid = event->stream;
- rinfo.rcv_ssn = event->ssn;
- rinfo.rcv_ppid = event->ppid;
- rinfo.rcv_flags = event->flags;
- rinfo.rcv_tsn = event->tsn;
- rinfo.rcv_cumtsn = event->cumtsn;
- rinfo.rcv_assoc_id = sctp_assoc2id(event->asoc);
- rinfo.rcv_context = event->asoc->default_rcv_context;
- put_cmsg(msghdr, IPPROTO_SCTP, SCTP_RCVINFO,
- sizeof(rinfo), &rinfo);
- }
- static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
- struct msghdr *msghdr,
- const struct sk_buff *skb)
- {
- struct sctp_nxtinfo nxtinfo;
- memset(&nxtinfo, 0, sizeof(nxtinfo));
- nxtinfo.nxt_sid = event->stream;
- nxtinfo.nxt_ppid = event->ppid;
- nxtinfo.nxt_flags = event->flags;
- if (sctp_ulpevent_is_notification(event))
- nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
- nxtinfo.nxt_length = skb->len;
- nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
- put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
- sizeof(nxtinfo), &nxtinfo);
- }
- void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
- struct msghdr *msghdr,
- struct sock *sk)
- {
- struct sk_buff *skb;
- int err;
- skb = sctp_skb_recv_datagram(sk, MSG_PEEK, 1, &err);
- if (skb != NULL) {
- __sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
- msghdr, skb);
-
- kfree_skb(skb);
- }
- }
- static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
- struct sctp_association *asoc)
- {
- struct sk_buff *skb, *frag;
- skb = sctp_event2skb(event);
-
- sctp_ulpevent_set_owner(event, asoc);
- sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
- if (!skb->data_len)
- return;
-
- skb_walk_frags(skb, frag)
- sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
- }
- static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
- {
- struct sk_buff *skb, *frag;
- unsigned int len;
-
- skb = sctp_event2skb(event);
- len = skb->len;
- if (!skb->data_len)
- goto done;
-
- skb_walk_frags(skb, frag) {
-
- sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
- }
- done:
- sctp_assoc_rwnd_increase(event->asoc, len);
- sctp_chunk_put(event->chunk);
- sctp_ulpevent_release_owner(event);
- }
- static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
- {
- struct sk_buff *skb, *frag;
- skb = sctp_event2skb(event);
- if (!skb->data_len)
- goto done;
-
- skb_walk_frags(skb, frag) {
-
- sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
- }
- done:
- sctp_chunk_put(event->chunk);
- sctp_ulpevent_release_owner(event);
- }
- void sctp_ulpevent_free(struct sctp_ulpevent *event)
- {
- if (sctp_ulpevent_is_notification(event))
- sctp_ulpevent_release_owner(event);
- else
- sctp_ulpevent_release_data(event);
- kfree_skb(sctp_event2skb(event));
- }
- unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
- {
- struct sk_buff *skb;
- unsigned int data_unread = 0;
- while ((skb = skb_dequeue(list)) != NULL) {
- struct sctp_ulpevent *event = sctp_skb2event(skb);
- if (!sctp_ulpevent_is_notification(event))
- data_unread += skb->len;
- sctp_ulpevent_free(event);
- }
- return data_unread;
- }
|