ulpevent.c 28 KB

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