ulpevent.c 27 KB

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