ulpevent.c 27 KB

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