ulpevent.c 29 KB

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