ulpevent.c 29 KB

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