ulpevent.c 29 KB

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