ulpevent.c 27 KB

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