ulpevent.c 30 KB

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