ulpevent.c 30 KB

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