ulpevent.c 30 KB

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