ulpevent.c 29 KB

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