ulpevent.c 30 KB

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