ulpevent.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001 Intel Corp.
  6. * Copyright (c) 2001 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * These functions manipulate an sctp event. The struct ulpevent is used
  10. * to carry notifications and data to the ULP (sockets).
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  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 License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * 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 the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Jon Grimm <jgrimm@us.ibm.com>
  37. * La Monte H.P. Yarroll <piggy@acm.org>
  38. * Ardelle Fan <ardelle.fan@intel.com>
  39. * Sridhar Samudrala <sri@us.ibm.com>
  40. *
  41. * Any bugs reported given to us we will try to fix... any fixes shared will
  42. * be incorporated into the next SCTP release.
  43. */
  44. #include <linux/types.h>
  45. #include <linux/skbuff.h>
  46. #include <net/sctp/structs.h>
  47. #include <net/sctp/sctp.h>
  48. #include <net/sctp/sm.h>
  49. static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
  50. struct sctp_association *asoc);
  51. static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
  52. static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
  53. /* Initialize an ULP event from an given skb. */
  54. SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,
  55. int msg_flags,
  56. unsigned int len)
  57. {
  58. memset(event, 0, sizeof(struct sctp_ulpevent));
  59. event->msg_flags = msg_flags;
  60. event->rmem_len = len;
  61. }
  62. /* Create a new sctp_ulpevent. */
  63. SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
  64. gfp_t gfp)
  65. {
  66. struct sctp_ulpevent *event;
  67. struct sk_buff *skb;
  68. skb = alloc_skb(size, gfp);
  69. if (!skb)
  70. goto fail;
  71. event = sctp_skb2event(skb);
  72. sctp_ulpevent_init(event, msg_flags, skb->truesize);
  73. return event;
  74. fail:
  75. return NULL;
  76. }
  77. /* Is this a MSG_NOTIFICATION? */
  78. int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
  79. {
  80. return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
  81. }
  82. /* Hold the association in case the msg_name needs read out of
  83. * the association.
  84. */
  85. static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
  86. const struct sctp_association *asoc)
  87. {
  88. struct sk_buff *skb;
  89. /* Cast away the const, as we are just wanting to
  90. * bump the reference count.
  91. */
  92. sctp_association_hold((struct sctp_association *)asoc);
  93. skb = sctp_event2skb(event);
  94. event->asoc = (struct sctp_association *)asoc;
  95. atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
  96. sctp_skb_set_owner_r(skb, asoc->base.sk);
  97. }
  98. /* A simple destructor to give up the reference to the association. */
  99. static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
  100. {
  101. struct sctp_association *asoc = event->asoc;
  102. atomic_sub(event->rmem_len, &asoc->rmem_alloc);
  103. sctp_association_put(asoc);
  104. }
  105. /* Create and initialize an SCTP_ASSOC_CHANGE event.
  106. *
  107. * 5.3.1.1 SCTP_ASSOC_CHANGE
  108. *
  109. * Communication notifications inform the ULP that an SCTP association
  110. * has either begun or ended. The identifier for a new association is
  111. * provided by this notification.
  112. *
  113. * Note: There is no field checking here. If a field is unused it will be
  114. * zero'd out.
  115. */
  116. struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
  117. const struct sctp_association *asoc,
  118. __u16 flags, __u16 state, __u16 error, __u16 outbound,
  119. __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
  120. {
  121. struct sctp_ulpevent *event;
  122. struct sctp_assoc_change *sac;
  123. struct sk_buff *skb;
  124. /* If the lower layer passed in the chunk, it will be
  125. * an ABORT, so we need to include it in the sac_info.
  126. */
  127. if (chunk) {
  128. /* sctp_inqu_pop() has allready pulled off the chunk
  129. * header. We need to put it back temporarily
  130. */
  131. skb_push(chunk->skb, sizeof(sctp_chunkhdr_t));
  132. /* Copy the chunk data to a new skb and reserve enough
  133. * head room to use as notification.
  134. */
  135. skb = skb_copy_expand(chunk->skb,
  136. sizeof(struct sctp_assoc_change), 0, gfp);
  137. if (!skb)
  138. goto fail;
  139. /* put back the chunk header now that we have a copy */
  140. skb_pull(chunk->skb, sizeof(sctp_chunkhdr_t));
  141. /* Embed the event fields inside the cloned skb. */
  142. event = sctp_skb2event(skb);
  143. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  144. /* Include the notification structure */
  145. sac = (struct sctp_assoc_change *)
  146. skb_push(skb, sizeof(struct sctp_assoc_change));
  147. /* Trim the buffer to the right length. */
  148. skb_trim(skb, sizeof(struct sctp_assoc_change) +
  149. ntohs(chunk->chunk_hdr->length));
  150. } else {
  151. event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
  152. MSG_NOTIFICATION, gfp);
  153. if (!event)
  154. goto fail;
  155. skb = sctp_event2skb(event);
  156. sac = (struct sctp_assoc_change *) skb_put(skb,
  157. sizeof(struct sctp_assoc_change));
  158. }
  159. /* Socket Extensions for SCTP
  160. * 5.3.1.1 SCTP_ASSOC_CHANGE
  161. *
  162. * sac_type:
  163. * It should be SCTP_ASSOC_CHANGE.
  164. */
  165. sac->sac_type = SCTP_ASSOC_CHANGE;
  166. /* Socket Extensions for SCTP
  167. * 5.3.1.1 SCTP_ASSOC_CHANGE
  168. *
  169. * sac_state: 32 bits (signed integer)
  170. * This field holds one of a number of values that communicate the
  171. * event that happened to the association.
  172. */
  173. sac->sac_state = state;
  174. /* Socket Extensions for SCTP
  175. * 5.3.1.1 SCTP_ASSOC_CHANGE
  176. *
  177. * sac_flags: 16 bits (unsigned integer)
  178. * Currently unused.
  179. */
  180. sac->sac_flags = 0;
  181. /* Socket Extensions for SCTP
  182. * 5.3.1.1 SCTP_ASSOC_CHANGE
  183. *
  184. * sac_length: sizeof (__u32)
  185. * This field is the total length of the notification data, including
  186. * the notification header.
  187. */
  188. sac->sac_length = sizeof(struct sctp_assoc_change);
  189. /* Socket Extensions for SCTP
  190. * 5.3.1.1 SCTP_ASSOC_CHANGE
  191. *
  192. * sac_error: 32 bits (signed integer)
  193. *
  194. * If the state was reached due to a error condition (e.g.
  195. * COMMUNICATION_LOST) any relevant error information is available in
  196. * this field. This corresponds to the protocol error codes defined in
  197. * [SCTP].
  198. */
  199. sac->sac_error = error;
  200. /* Socket Extensions for SCTP
  201. * 5.3.1.1 SCTP_ASSOC_CHANGE
  202. *
  203. * sac_outbound_streams: 16 bits (unsigned integer)
  204. * sac_inbound_streams: 16 bits (unsigned integer)
  205. *
  206. * The maximum number of streams allowed in each direction are
  207. * available in sac_outbound_streams and sac_inbound streams.
  208. */
  209. sac->sac_outbound_streams = outbound;
  210. sac->sac_inbound_streams = inbound;
  211. /* Socket Extensions for SCTP
  212. * 5.3.1.1 SCTP_ASSOC_CHANGE
  213. *
  214. * sac_assoc_id: sizeof (sctp_assoc_t)
  215. *
  216. * The association id field, holds the identifier for the association.
  217. * All notifications for a given association have the same association
  218. * identifier. For TCP style socket, this field is ignored.
  219. */
  220. sctp_ulpevent_set_owner(event, asoc);
  221. sac->sac_assoc_id = sctp_assoc2id(asoc);
  222. return event;
  223. fail:
  224. return NULL;
  225. }
  226. /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
  227. *
  228. * Socket Extensions for SCTP - draft-01
  229. * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  230. *
  231. * When a destination address on a multi-homed peer encounters a change
  232. * an interface details event is sent.
  233. */
  234. struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
  235. const struct sctp_association *asoc,
  236. const struct sockaddr_storage *aaddr,
  237. int flags, int state, int error, gfp_t gfp)
  238. {
  239. struct sctp_ulpevent *event;
  240. struct sctp_paddr_change *spc;
  241. struct sk_buff *skb;
  242. event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
  243. MSG_NOTIFICATION, gfp);
  244. if (!event)
  245. goto fail;
  246. skb = sctp_event2skb(event);
  247. spc = (struct sctp_paddr_change *)
  248. skb_put(skb, sizeof(struct sctp_paddr_change));
  249. /* Sockets API Extensions for SCTP
  250. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  251. *
  252. * spc_type:
  253. *
  254. * It should be SCTP_PEER_ADDR_CHANGE.
  255. */
  256. spc->spc_type = SCTP_PEER_ADDR_CHANGE;
  257. /* Sockets API Extensions for SCTP
  258. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  259. *
  260. * spc_length: sizeof (__u32)
  261. *
  262. * This field is the total length of the notification data, including
  263. * the notification header.
  264. */
  265. spc->spc_length = sizeof(struct sctp_paddr_change);
  266. /* Sockets API Extensions for SCTP
  267. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  268. *
  269. * spc_flags: 16 bits (unsigned integer)
  270. * Currently unused.
  271. */
  272. spc->spc_flags = 0;
  273. /* Sockets API Extensions for SCTP
  274. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  275. *
  276. * spc_state: 32 bits (signed integer)
  277. *
  278. * This field holds one of a number of values that communicate the
  279. * event that happened to the address.
  280. */
  281. spc->spc_state = state;
  282. /* Sockets API Extensions for SCTP
  283. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  284. *
  285. * spc_error: 32 bits (signed integer)
  286. *
  287. * If the state was reached due to any error condition (e.g.
  288. * ADDRESS_UNREACHABLE) any relevant error information is available in
  289. * this field.
  290. */
  291. spc->spc_error = error;
  292. /* Socket Extensions for SCTP
  293. * 5.3.1.1 SCTP_ASSOC_CHANGE
  294. *
  295. * spc_assoc_id: sizeof (sctp_assoc_t)
  296. *
  297. * The association id field, holds the identifier for the association.
  298. * All notifications for a given association have the same association
  299. * identifier. For TCP style socket, this field is ignored.
  300. */
  301. sctp_ulpevent_set_owner(event, asoc);
  302. spc->spc_assoc_id = sctp_assoc2id(asoc);
  303. /* Sockets API Extensions for SCTP
  304. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  305. *
  306. * spc_aaddr: sizeof (struct sockaddr_storage)
  307. *
  308. * The affected address field, holds the remote peer's address that is
  309. * encountering the change of state.
  310. */
  311. memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
  312. /* Map ipv4 address into v4-mapped-on-v6 address. */
  313. sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
  314. sctp_sk(asoc->base.sk),
  315. (union sctp_addr *)&spc->spc_aaddr);
  316. return event;
  317. fail:
  318. return NULL;
  319. }
  320. /* Create and initialize an SCTP_REMOTE_ERROR notification.
  321. *
  322. * Note: This assumes that the chunk->skb->data already points to the
  323. * operation error payload.
  324. *
  325. * Socket Extensions for SCTP - draft-01
  326. * 5.3.1.3 SCTP_REMOTE_ERROR
  327. *
  328. * A remote peer may send an Operational Error message to its peer.
  329. * This message indicates a variety of error conditions on an
  330. * association. The entire error TLV as it appears on the wire is
  331. * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
  332. * specification [SCTP] and any extensions for a list of possible
  333. * error formats.
  334. */
  335. struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
  336. const struct sctp_association *asoc, struct sctp_chunk *chunk,
  337. __u16 flags, gfp_t gfp)
  338. {
  339. struct sctp_ulpevent *event;
  340. struct sctp_remote_error *sre;
  341. struct sk_buff *skb;
  342. sctp_errhdr_t *ch;
  343. __be16 cause;
  344. int elen;
  345. ch = (sctp_errhdr_t *)(chunk->skb->data);
  346. cause = ch->cause;
  347. elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
  348. /* Pull off the ERROR header. */
  349. skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
  350. /* Copy the skb to a new skb with room for us to prepend
  351. * notification with.
  352. */
  353. skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
  354. 0, gfp);
  355. /* Pull off the rest of the cause TLV from the chunk. */
  356. skb_pull(chunk->skb, elen);
  357. if (!skb)
  358. goto fail;
  359. /* Embed the event fields inside the cloned skb. */
  360. event = sctp_skb2event(skb);
  361. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  362. sre = (struct sctp_remote_error *)
  363. skb_push(skb, sizeof(struct sctp_remote_error));
  364. /* Trim the buffer to the right length. */
  365. skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
  366. /* Socket Extensions for SCTP
  367. * 5.3.1.3 SCTP_REMOTE_ERROR
  368. *
  369. * sre_type:
  370. * It should be SCTP_REMOTE_ERROR.
  371. */
  372. sre->sre_type = SCTP_REMOTE_ERROR;
  373. /*
  374. * Socket Extensions for SCTP
  375. * 5.3.1.3 SCTP_REMOTE_ERROR
  376. *
  377. * sre_flags: 16 bits (unsigned integer)
  378. * Currently unused.
  379. */
  380. sre->sre_flags = 0;
  381. /* Socket Extensions for SCTP
  382. * 5.3.1.3 SCTP_REMOTE_ERROR
  383. *
  384. * sre_length: sizeof (__u32)
  385. *
  386. * This field is the total length of the notification data,
  387. * including the notification header.
  388. */
  389. sre->sre_length = skb->len;
  390. /* Socket Extensions for SCTP
  391. * 5.3.1.3 SCTP_REMOTE_ERROR
  392. *
  393. * sre_error: 16 bits (unsigned integer)
  394. * This value represents one of the Operational Error causes defined in
  395. * the SCTP specification, in network byte order.
  396. */
  397. sre->sre_error = cause;
  398. /* Socket Extensions for SCTP
  399. * 5.3.1.3 SCTP_REMOTE_ERROR
  400. *
  401. * sre_assoc_id: sizeof (sctp_assoc_t)
  402. *
  403. * The association id field, holds the identifier for the association.
  404. * All notifications for a given association have the same association
  405. * identifier. For TCP style socket, this field is ignored.
  406. */
  407. sctp_ulpevent_set_owner(event, asoc);
  408. sre->sre_assoc_id = sctp_assoc2id(asoc);
  409. return event;
  410. fail:
  411. return NULL;
  412. }
  413. /* Create and initialize a SCTP_SEND_FAILED notification.
  414. *
  415. * Socket Extensions for SCTP - draft-01
  416. * 5.3.1.4 SCTP_SEND_FAILED
  417. */
  418. struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
  419. const struct sctp_association *asoc, struct sctp_chunk *chunk,
  420. __u16 flags, __u32 error, gfp_t gfp)
  421. {
  422. struct sctp_ulpevent *event;
  423. struct sctp_send_failed *ssf;
  424. struct sk_buff *skb;
  425. /* Pull off any padding. */
  426. int len = ntohs(chunk->chunk_hdr->length);
  427. /* Make skb with more room so we can prepend notification. */
  428. skb = skb_copy_expand(chunk->skb,
  429. sizeof(struct sctp_send_failed), /* headroom */
  430. 0, /* tailroom */
  431. gfp);
  432. if (!skb)
  433. goto fail;
  434. /* Pull off the common chunk header and DATA header. */
  435. skb_pull(skb, sizeof(struct sctp_data_chunk));
  436. len -= sizeof(struct sctp_data_chunk);
  437. /* Embed the event fields inside the cloned skb. */
  438. event = sctp_skb2event(skb);
  439. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  440. ssf = (struct sctp_send_failed *)
  441. skb_push(skb, sizeof(struct sctp_send_failed));
  442. /* Socket Extensions for SCTP
  443. * 5.3.1.4 SCTP_SEND_FAILED
  444. *
  445. * ssf_type:
  446. * It should be SCTP_SEND_FAILED.
  447. */
  448. ssf->ssf_type = SCTP_SEND_FAILED;
  449. /* Socket Extensions for SCTP
  450. * 5.3.1.4 SCTP_SEND_FAILED
  451. *
  452. * ssf_flags: 16 bits (unsigned integer)
  453. * The flag value will take one of the following values
  454. *
  455. * SCTP_DATA_UNSENT - Indicates that the data was never put on
  456. * the wire.
  457. *
  458. * SCTP_DATA_SENT - Indicates that the data was put on the wire.
  459. * Note that this does not necessarily mean that the
  460. * data was (or was not) successfully delivered.
  461. */
  462. ssf->ssf_flags = flags;
  463. /* Socket Extensions for SCTP
  464. * 5.3.1.4 SCTP_SEND_FAILED
  465. *
  466. * ssf_length: sizeof (__u32)
  467. * This field is the total length of the notification data, including
  468. * the notification header.
  469. */
  470. ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
  471. skb_trim(skb, ssf->ssf_length);
  472. /* Socket Extensions for SCTP
  473. * 5.3.1.4 SCTP_SEND_FAILED
  474. *
  475. * ssf_error: 16 bits (unsigned integer)
  476. * This value represents the reason why the send failed, and if set,
  477. * will be a SCTP protocol error code as defined in [SCTP] section
  478. * 3.3.10.
  479. */
  480. ssf->ssf_error = error;
  481. /* Socket Extensions for SCTP
  482. * 5.3.1.4 SCTP_SEND_FAILED
  483. *
  484. * ssf_info: sizeof (struct sctp_sndrcvinfo)
  485. * The original send information associated with the undelivered
  486. * message.
  487. */
  488. memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
  489. /* Per TSVWG discussion with Randy. Allow the application to
  490. * ressemble a fragmented message.
  491. */
  492. ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
  493. /* Socket Extensions for SCTP
  494. * 5.3.1.4 SCTP_SEND_FAILED
  495. *
  496. * ssf_assoc_id: sizeof (sctp_assoc_t)
  497. * The association id field, sf_assoc_id, holds the identifier for the
  498. * association. All notifications for a given association have the
  499. * same association identifier. For TCP style socket, this field is
  500. * ignored.
  501. */
  502. sctp_ulpevent_set_owner(event, asoc);
  503. ssf->ssf_assoc_id = sctp_assoc2id(asoc);
  504. return event;
  505. fail:
  506. return NULL;
  507. }
  508. /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
  509. *
  510. * Socket Extensions for SCTP - draft-01
  511. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  512. */
  513. struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
  514. const struct sctp_association *asoc,
  515. __u16 flags, gfp_t gfp)
  516. {
  517. struct sctp_ulpevent *event;
  518. struct sctp_shutdown_event *sse;
  519. struct sk_buff *skb;
  520. event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
  521. MSG_NOTIFICATION, gfp);
  522. if (!event)
  523. goto fail;
  524. skb = sctp_event2skb(event);
  525. sse = (struct sctp_shutdown_event *)
  526. skb_put(skb, sizeof(struct sctp_shutdown_event));
  527. /* Socket Extensions for SCTP
  528. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  529. *
  530. * sse_type
  531. * It should be SCTP_SHUTDOWN_EVENT
  532. */
  533. sse->sse_type = SCTP_SHUTDOWN_EVENT;
  534. /* Socket Extensions for SCTP
  535. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  536. *
  537. * sse_flags: 16 bits (unsigned integer)
  538. * Currently unused.
  539. */
  540. sse->sse_flags = 0;
  541. /* Socket Extensions for SCTP
  542. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  543. *
  544. * sse_length: sizeof (__u32)
  545. * This field is the total length of the notification data, including
  546. * the notification header.
  547. */
  548. sse->sse_length = sizeof(struct sctp_shutdown_event);
  549. /* Socket Extensions for SCTP
  550. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  551. *
  552. * sse_assoc_id: sizeof (sctp_assoc_t)
  553. * The association id field, holds the identifier for the association.
  554. * All notifications for a given association have the same association
  555. * identifier. For TCP style socket, this field is ignored.
  556. */
  557. sctp_ulpevent_set_owner(event, asoc);
  558. sse->sse_assoc_id = sctp_assoc2id(asoc);
  559. return event;
  560. fail:
  561. return NULL;
  562. }
  563. /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
  564. *
  565. * Socket Extensions for SCTP
  566. * 5.3.1.6 SCTP_ADAPTATION_INDICATION
  567. */
  568. struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
  569. const struct sctp_association *asoc, gfp_t gfp)
  570. {
  571. struct sctp_ulpevent *event;
  572. struct sctp_adaptation_event *sai;
  573. struct sk_buff *skb;
  574. event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
  575. MSG_NOTIFICATION, gfp);
  576. if (!event)
  577. goto fail;
  578. skb = sctp_event2skb(event);
  579. sai = (struct sctp_adaptation_event *)
  580. skb_put(skb, sizeof(struct sctp_adaptation_event));
  581. sai->sai_type = SCTP_ADAPTATION_INDICATION;
  582. sai->sai_flags = 0;
  583. sai->sai_length = sizeof(struct sctp_adaptation_event);
  584. sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
  585. sctp_ulpevent_set_owner(event, asoc);
  586. sai->sai_assoc_id = sctp_assoc2id(asoc);
  587. return event;
  588. fail:
  589. return NULL;
  590. }
  591. /* A message has been received. Package this message as a notification
  592. * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
  593. * even if filtered out later.
  594. *
  595. * Socket Extensions for SCTP
  596. * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
  597. */
  598. struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
  599. struct sctp_chunk *chunk,
  600. gfp_t gfp)
  601. {
  602. struct sctp_ulpevent *event = NULL;
  603. struct sk_buff *skb;
  604. size_t padding, len;
  605. /* Clone the original skb, sharing the data. */
  606. skb = skb_clone(chunk->skb, gfp);
  607. if (!skb)
  608. goto fail;
  609. /* First calculate the padding, so we don't inadvertently
  610. * pass up the wrong length to the user.
  611. *
  612. * RFC 2960 - Section 3.2 Chunk Field Descriptions
  613. *
  614. * The total length of a chunk(including Type, Length and Value fields)
  615. * MUST be a multiple of 4 bytes. If the length of the chunk is not a
  616. * multiple of 4 bytes, the sender MUST pad the chunk with all zero
  617. * bytes and this padding is not included in the chunk length field.
  618. * The sender should never pad with more than 3 bytes. The receiver
  619. * MUST ignore the padding bytes.
  620. */
  621. len = ntohs(chunk->chunk_hdr->length);
  622. padding = WORD_ROUND(len) - len;
  623. /* Fixup cloned skb with just this chunks data. */
  624. skb_trim(skb, chunk->chunk_end - padding - skb->data);
  625. /* Embed the event fields inside the cloned skb. */
  626. event = sctp_skb2event(skb);
  627. /* Initialize event with flags 0 and correct length
  628. * Since this is a clone of the original skb, only account for
  629. * the data of this chunk as other chunks will be accounted separately.
  630. */
  631. sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
  632. sctp_ulpevent_receive_data(event, asoc);
  633. event->stream = ntohs(chunk->subh.data_hdr->stream);
  634. event->ssn = ntohs(chunk->subh.data_hdr->ssn);
  635. event->ppid = chunk->subh.data_hdr->ppid;
  636. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  637. event->flags |= SCTP_UNORDERED;
  638. event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  639. }
  640. event->tsn = ntohl(chunk->subh.data_hdr->tsn);
  641. event->msg_flags |= chunk->chunk_hdr->flags;
  642. event->iif = sctp_chunk_iif(chunk);
  643. fail:
  644. return event;
  645. }
  646. /* Create a partial delivery related event.
  647. *
  648. * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
  649. *
  650. * When a receiver is engaged in a partial delivery of a
  651. * message this notification will be used to indicate
  652. * various events.
  653. */
  654. struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
  655. const struct sctp_association *asoc, __u32 indication,
  656. gfp_t gfp)
  657. {
  658. struct sctp_ulpevent *event;
  659. struct sctp_pdapi_event *pd;
  660. struct sk_buff *skb;
  661. event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
  662. MSG_NOTIFICATION, gfp);
  663. if (!event)
  664. goto fail;
  665. skb = sctp_event2skb(event);
  666. pd = (struct sctp_pdapi_event *)
  667. skb_put(skb, sizeof(struct sctp_pdapi_event));
  668. /* pdapi_type
  669. * It should be SCTP_PARTIAL_DELIVERY_EVENT
  670. *
  671. * pdapi_flags: 16 bits (unsigned integer)
  672. * Currently unused.
  673. */
  674. pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
  675. pd->pdapi_flags = 0;
  676. /* pdapi_length: 32 bits (unsigned integer)
  677. *
  678. * This field is the total length of the notification data, including
  679. * the notification header. It will generally be sizeof (struct
  680. * sctp_pdapi_event).
  681. */
  682. pd->pdapi_length = sizeof(struct sctp_pdapi_event);
  683. /* pdapi_indication: 32 bits (unsigned integer)
  684. *
  685. * This field holds the indication being sent to the application.
  686. */
  687. pd->pdapi_indication = indication;
  688. /* pdapi_assoc_id: sizeof (sctp_assoc_t)
  689. *
  690. * The association id field, holds the identifier for the association.
  691. */
  692. sctp_ulpevent_set_owner(event, asoc);
  693. pd->pdapi_assoc_id = sctp_assoc2id(asoc);
  694. return event;
  695. fail:
  696. return NULL;
  697. }
  698. /* Return the notification type, assuming this is a notification
  699. * event.
  700. */
  701. __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
  702. {
  703. union sctp_notification *notification;
  704. struct sk_buff *skb;
  705. skb = sctp_event2skb((struct sctp_ulpevent *)event);
  706. notification = (union sctp_notification *) skb->data;
  707. return notification->sn_header.sn_type;
  708. }
  709. /* Copy out the sndrcvinfo into a msghdr. */
  710. void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
  711. struct msghdr *msghdr)
  712. {
  713. struct sctp_sndrcvinfo sinfo;
  714. if (sctp_ulpevent_is_notification(event))
  715. return;
  716. /* Sockets API Extensions for SCTP
  717. * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
  718. *
  719. * sinfo_stream: 16 bits (unsigned integer)
  720. *
  721. * For recvmsg() the SCTP stack places the message's stream number in
  722. * this value.
  723. */
  724. sinfo.sinfo_stream = event->stream;
  725. /* sinfo_ssn: 16 bits (unsigned integer)
  726. *
  727. * For recvmsg() this value contains the stream sequence number that
  728. * the remote endpoint placed in the DATA chunk. For fragmented
  729. * messages this is the same number for all deliveries of the message
  730. * (if more than one recvmsg() is needed to read the message).
  731. */
  732. sinfo.sinfo_ssn = event->ssn;
  733. /* sinfo_ppid: 32 bits (unsigned integer)
  734. *
  735. * In recvmsg() this value is
  736. * the same information that was passed by the upper layer in the peer
  737. * application. Please note that byte order issues are NOT accounted
  738. * for and this information is passed opaquely by the SCTP stack from
  739. * one end to the other.
  740. */
  741. sinfo.sinfo_ppid = event->ppid;
  742. /* sinfo_flags: 16 bits (unsigned integer)
  743. *
  744. * This field may contain any of the following flags and is composed of
  745. * a bitwise OR of these values.
  746. *
  747. * recvmsg() flags:
  748. *
  749. * SCTP_UNORDERED - This flag is present when the message was sent
  750. * non-ordered.
  751. */
  752. sinfo.sinfo_flags = event->flags;
  753. /* sinfo_tsn: 32 bit (unsigned integer)
  754. *
  755. * For the receiving side, this field holds a TSN that was
  756. * assigned to one of the SCTP Data Chunks.
  757. */
  758. sinfo.sinfo_tsn = event->tsn;
  759. /* sinfo_cumtsn: 32 bit (unsigned integer)
  760. *
  761. * This field will hold the current cumulative TSN as
  762. * known by the underlying SCTP layer. Note this field is
  763. * ignored when sending and only valid for a receive
  764. * operation when sinfo_flags are set to SCTP_UNORDERED.
  765. */
  766. sinfo.sinfo_cumtsn = event->cumtsn;
  767. /* sinfo_assoc_id: sizeof (sctp_assoc_t)
  768. *
  769. * The association handle field, sinfo_assoc_id, holds the identifier
  770. * for the association announced in the COMMUNICATION_UP notification.
  771. * All notifications for a given association have the same identifier.
  772. * Ignored for one-to-one style sockets.
  773. */
  774. sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
  775. /* context value that is set via SCTP_CONTEXT socket option. */
  776. sinfo.sinfo_context = event->asoc->default_rcv_context;
  777. /* These fields are not used while receiving. */
  778. sinfo.sinfo_timetolive = 0;
  779. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
  780. sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
  781. }
  782. /* Do accounting for bytes received and hold a reference to the association
  783. * for each skb.
  784. */
  785. static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
  786. struct sctp_association *asoc)
  787. {
  788. struct sk_buff *skb, *frag;
  789. skb = sctp_event2skb(event);
  790. /* Set the owner and charge rwnd for bytes received. */
  791. sctp_ulpevent_set_owner(event, asoc);
  792. sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
  793. if (!skb->data_len)
  794. return;
  795. /* Note: Not clearing the entire event struct as this is just a
  796. * fragment of the real event. However, we still need to do rwnd
  797. * accounting.
  798. * In general, the skb passed from IP can have only 1 level of
  799. * fragments. But we allow multiple levels of fragments.
  800. */
  801. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
  802. sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
  803. }
  804. }
  805. /* Do accounting for bytes just read by user and release the references to
  806. * the association.
  807. */
  808. static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
  809. {
  810. struct sk_buff *skb, *frag;
  811. unsigned int len;
  812. /* Current stack structures assume that the rcv buffer is
  813. * per socket. For UDP style sockets this is not true as
  814. * multiple associations may be on a single UDP-style socket.
  815. * Use the local private area of the skb to track the owning
  816. * association.
  817. */
  818. skb = sctp_event2skb(event);
  819. len = skb->len;
  820. if (!skb->data_len)
  821. goto done;
  822. /* Don't forget the fragments. */
  823. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
  824. /* NOTE: skb_shinfos are recursive. Although IP returns
  825. * skb's with only 1 level of fragments, SCTP reassembly can
  826. * increase the levels.
  827. */
  828. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  829. }
  830. done:
  831. sctp_assoc_rwnd_increase(event->asoc, len);
  832. sctp_ulpevent_release_owner(event);
  833. }
  834. static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
  835. {
  836. struct sk_buff *skb, *frag;
  837. skb = sctp_event2skb(event);
  838. if (!skb->data_len)
  839. goto done;
  840. /* Don't forget the fragments. */
  841. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
  842. /* NOTE: skb_shinfos are recursive. Although IP returns
  843. * skb's with only 1 level of fragments, SCTP reassembly can
  844. * increase the levels.
  845. */
  846. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  847. }
  848. done:
  849. sctp_ulpevent_release_owner(event);
  850. }
  851. /* Free a ulpevent that has an owner. It includes releasing the reference
  852. * to the owner, updating the rwnd in case of a DATA event and freeing the
  853. * skb.
  854. */
  855. void sctp_ulpevent_free(struct sctp_ulpevent *event)
  856. {
  857. if (sctp_ulpevent_is_notification(event))
  858. sctp_ulpevent_release_owner(event);
  859. else
  860. sctp_ulpevent_release_data(event);
  861. kfree_skb(sctp_event2skb(event));
  862. }
  863. /* Purge the skb lists holding ulpevents. */
  864. void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
  865. {
  866. struct sk_buff *skb;
  867. while ((skb = skb_dequeue(list)) != NULL)
  868. sctp_ulpevent_free(sctp_skb2event(skb));
  869. }