ulpevent.c 26 KB

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