ulpevent.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. __u16 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, __u16 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 sctp_chunk *chunk = event->chunk;
  84. struct sk_buff *skb;
  85. /* Cast away the const, as we are just wanting to
  86. * bump the reference count.
  87. */
  88. sctp_association_hold((struct sctp_association *)asoc);
  89. skb = sctp_event2skb(event);
  90. event->asoc = (struct sctp_association *)asoc;
  91. atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
  92. sctp_skb_set_owner_r(skb, asoc->base.sk);
  93. if (chunk && chunk->head_skb && !chunk->head_skb->sk)
  94. chunk->head_skb->sk = asoc->base.sk;
  95. }
  96. /* A simple destructor to give up the reference to the association. */
  97. static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
  98. {
  99. struct sctp_association *asoc = event->asoc;
  100. atomic_sub(event->rmem_len, &asoc->rmem_alloc);
  101. sctp_association_put(asoc);
  102. }
  103. /* Create and initialize an SCTP_ASSOC_CHANGE event.
  104. *
  105. * 5.3.1.1 SCTP_ASSOC_CHANGE
  106. *
  107. * Communication notifications inform the ULP that an SCTP association
  108. * has either begun or ended. The identifier for a new association is
  109. * provided by this notification.
  110. *
  111. * Note: There is no field checking here. If a field is unused it will be
  112. * zero'd out.
  113. */
  114. struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
  115. const struct sctp_association *asoc,
  116. __u16 flags, __u16 state, __u16 error, __u16 outbound,
  117. __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
  118. {
  119. struct sctp_ulpevent *event;
  120. struct sctp_assoc_change *sac;
  121. struct sk_buff *skb;
  122. /* If the lower layer passed in the chunk, it will be
  123. * an ABORT, so we need to include it in the sac_info.
  124. */
  125. if (chunk) {
  126. /* Copy the chunk data to a new skb and reserve enough
  127. * head room to use as notification.
  128. */
  129. skb = skb_copy_expand(chunk->skb,
  130. sizeof(struct sctp_assoc_change), 0, gfp);
  131. if (!skb)
  132. goto fail;
  133. /* Embed the event fields inside the cloned skb. */
  134. event = sctp_skb2event(skb);
  135. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  136. /* Include the notification structure */
  137. sac = skb_push(skb, sizeof(struct sctp_assoc_change));
  138. /* Trim the buffer to the right length. */
  139. skb_trim(skb, sizeof(struct sctp_assoc_change) +
  140. ntohs(chunk->chunk_hdr->length) -
  141. sizeof(struct sctp_chunkhdr));
  142. } else {
  143. event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
  144. MSG_NOTIFICATION, gfp);
  145. if (!event)
  146. goto fail;
  147. skb = sctp_event2skb(event);
  148. sac = skb_put(skb, sizeof(struct sctp_assoc_change));
  149. }
  150. /* Socket Extensions for SCTP
  151. * 5.3.1.1 SCTP_ASSOC_CHANGE
  152. *
  153. * sac_type:
  154. * It should be SCTP_ASSOC_CHANGE.
  155. */
  156. sac->sac_type = SCTP_ASSOC_CHANGE;
  157. /* Socket Extensions for SCTP
  158. * 5.3.1.1 SCTP_ASSOC_CHANGE
  159. *
  160. * sac_state: 32 bits (signed integer)
  161. * This field holds one of a number of values that communicate the
  162. * event that happened to the association.
  163. */
  164. sac->sac_state = state;
  165. /* Socket Extensions for SCTP
  166. * 5.3.1.1 SCTP_ASSOC_CHANGE
  167. *
  168. * sac_flags: 16 bits (unsigned integer)
  169. * Currently unused.
  170. */
  171. sac->sac_flags = 0;
  172. /* Socket Extensions for SCTP
  173. * 5.3.1.1 SCTP_ASSOC_CHANGE
  174. *
  175. * sac_length: sizeof (__u32)
  176. * This field is the total length of the notification data, including
  177. * the notification header.
  178. */
  179. sac->sac_length = skb->len;
  180. /* Socket Extensions for SCTP
  181. * 5.3.1.1 SCTP_ASSOC_CHANGE
  182. *
  183. * sac_error: 32 bits (signed integer)
  184. *
  185. * If the state was reached due to a error condition (e.g.
  186. * COMMUNICATION_LOST) any relevant error information is available in
  187. * this field. This corresponds to the protocol error codes defined in
  188. * [SCTP].
  189. */
  190. sac->sac_error = error;
  191. /* Socket Extensions for SCTP
  192. * 5.3.1.1 SCTP_ASSOC_CHANGE
  193. *
  194. * sac_outbound_streams: 16 bits (unsigned integer)
  195. * sac_inbound_streams: 16 bits (unsigned integer)
  196. *
  197. * The maximum number of streams allowed in each direction are
  198. * available in sac_outbound_streams and sac_inbound streams.
  199. */
  200. sac->sac_outbound_streams = outbound;
  201. sac->sac_inbound_streams = inbound;
  202. /* Socket Extensions for SCTP
  203. * 5.3.1.1 SCTP_ASSOC_CHANGE
  204. *
  205. * sac_assoc_id: sizeof (sctp_assoc_t)
  206. *
  207. * The association id field, holds the identifier for the association.
  208. * All notifications for a given association have the same association
  209. * identifier. For TCP style socket, this field is ignored.
  210. */
  211. sctp_ulpevent_set_owner(event, asoc);
  212. sac->sac_assoc_id = sctp_assoc2id(asoc);
  213. return event;
  214. fail:
  215. return NULL;
  216. }
  217. /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
  218. *
  219. * Socket Extensions for SCTP - draft-01
  220. * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  221. *
  222. * When a destination address on a multi-homed peer encounters a change
  223. * an interface details event is sent.
  224. */
  225. struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
  226. const struct sctp_association *asoc,
  227. const struct sockaddr_storage *aaddr,
  228. int flags, int state, int error, gfp_t gfp)
  229. {
  230. struct sctp_ulpevent *event;
  231. struct sctp_paddr_change *spc;
  232. struct sk_buff *skb;
  233. event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
  234. MSG_NOTIFICATION, gfp);
  235. if (!event)
  236. goto fail;
  237. skb = sctp_event2skb(event);
  238. spc = 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_to_user(
  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 *
  326. sctp_ulpevent_make_remote_error(const struct sctp_association *asoc,
  327. struct sctp_chunk *chunk, __u16 flags,
  328. gfp_t gfp)
  329. {
  330. struct sctp_remote_error *sre;
  331. struct sctp_ulpevent *event;
  332. struct sctp_errhdr *ch;
  333. struct sk_buff *skb;
  334. __be16 cause;
  335. int elen;
  336. ch = (struct sctp_errhdr *)(chunk->skb->data);
  337. cause = ch->cause;
  338. elen = SCTP_PAD4(ntohs(ch->length)) - sizeof(*ch);
  339. /* Pull off the ERROR header. */
  340. skb_pull(chunk->skb, sizeof(*ch));
  341. /* Copy the skb to a new skb with room for us to prepend
  342. * notification with.
  343. */
  344. skb = skb_copy_expand(chunk->skb, sizeof(*sre), 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 = skb_push(skb, sizeof(*sre));
  353. /* Trim the buffer to the right length. */
  354. skb_trim(skb, sizeof(*sre) + elen);
  355. /* RFC6458, Section 6.1.3. SCTP_REMOTE_ERROR */
  356. memset(sre, 0, sizeof(*sre));
  357. sre->sre_type = SCTP_REMOTE_ERROR;
  358. sre->sre_flags = 0;
  359. sre->sre_length = skb->len;
  360. sre->sre_error = cause;
  361. sctp_ulpevent_set_owner(event, asoc);
  362. sre->sre_assoc_id = sctp_assoc2id(asoc);
  363. return event;
  364. fail:
  365. return NULL;
  366. }
  367. /* Create and initialize a SCTP_SEND_FAILED notification.
  368. *
  369. * Socket Extensions for SCTP - draft-01
  370. * 5.3.1.4 SCTP_SEND_FAILED
  371. */
  372. struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
  373. const struct sctp_association *asoc, struct sctp_chunk *chunk,
  374. __u16 flags, __u32 error, gfp_t gfp)
  375. {
  376. struct sctp_ulpevent *event;
  377. struct sctp_send_failed *ssf;
  378. struct sk_buff *skb;
  379. /* Pull off any padding. */
  380. int len = ntohs(chunk->chunk_hdr->length);
  381. /* Make skb with more room so we can prepend notification. */
  382. skb = skb_copy_expand(chunk->skb,
  383. sizeof(struct sctp_send_failed), /* headroom */
  384. 0, /* tailroom */
  385. gfp);
  386. if (!skb)
  387. goto fail;
  388. /* Pull off the common chunk header and DATA header. */
  389. skb_pull(skb, sctp_datachk_len(&asoc->stream));
  390. len -= sctp_datachk_len(&asoc->stream);
  391. /* Embed the event fields inside the cloned skb. */
  392. event = sctp_skb2event(skb);
  393. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  394. ssf = skb_push(skb, sizeof(struct sctp_send_failed));
  395. /* Socket Extensions for SCTP
  396. * 5.3.1.4 SCTP_SEND_FAILED
  397. *
  398. * ssf_type:
  399. * It should be SCTP_SEND_FAILED.
  400. */
  401. ssf->ssf_type = SCTP_SEND_FAILED;
  402. /* Socket Extensions for SCTP
  403. * 5.3.1.4 SCTP_SEND_FAILED
  404. *
  405. * ssf_flags: 16 bits (unsigned integer)
  406. * The flag value will take one of the following values
  407. *
  408. * SCTP_DATA_UNSENT - Indicates that the data was never put on
  409. * the wire.
  410. *
  411. * SCTP_DATA_SENT - Indicates that the data was put on the wire.
  412. * Note that this does not necessarily mean that the
  413. * data was (or was not) successfully delivered.
  414. */
  415. ssf->ssf_flags = flags;
  416. /* Socket Extensions for SCTP
  417. * 5.3.1.4 SCTP_SEND_FAILED
  418. *
  419. * ssf_length: sizeof (__u32)
  420. * This field is the total length of the notification data, including
  421. * the notification header.
  422. */
  423. ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
  424. skb_trim(skb, ssf->ssf_length);
  425. /* Socket Extensions for SCTP
  426. * 5.3.1.4 SCTP_SEND_FAILED
  427. *
  428. * ssf_error: 16 bits (unsigned integer)
  429. * This value represents the reason why the send failed, and if set,
  430. * will be a SCTP protocol error code as defined in [SCTP] section
  431. * 3.3.10.
  432. */
  433. ssf->ssf_error = error;
  434. /* Socket Extensions for SCTP
  435. * 5.3.1.4 SCTP_SEND_FAILED
  436. *
  437. * ssf_info: sizeof (struct sctp_sndrcvinfo)
  438. * The original send information associated with the undelivered
  439. * message.
  440. */
  441. memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
  442. /* Per TSVWG discussion with Randy. Allow the application to
  443. * reassemble a fragmented message.
  444. */
  445. ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
  446. /* Socket Extensions for SCTP
  447. * 5.3.1.4 SCTP_SEND_FAILED
  448. *
  449. * ssf_assoc_id: sizeof (sctp_assoc_t)
  450. * The association id field, sf_assoc_id, holds the identifier for the
  451. * association. All notifications for a given association have the
  452. * same association identifier. For TCP style socket, this field is
  453. * ignored.
  454. */
  455. sctp_ulpevent_set_owner(event, asoc);
  456. ssf->ssf_assoc_id = sctp_assoc2id(asoc);
  457. return event;
  458. fail:
  459. return NULL;
  460. }
  461. /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
  462. *
  463. * Socket Extensions for SCTP - draft-01
  464. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  465. */
  466. struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
  467. const struct sctp_association *asoc,
  468. __u16 flags, gfp_t gfp)
  469. {
  470. struct sctp_ulpevent *event;
  471. struct sctp_shutdown_event *sse;
  472. struct sk_buff *skb;
  473. event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
  474. MSG_NOTIFICATION, gfp);
  475. if (!event)
  476. goto fail;
  477. skb = sctp_event2skb(event);
  478. sse = skb_put(skb, sizeof(struct sctp_shutdown_event));
  479. /* Socket Extensions for SCTP
  480. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  481. *
  482. * sse_type
  483. * It should be SCTP_SHUTDOWN_EVENT
  484. */
  485. sse->sse_type = SCTP_SHUTDOWN_EVENT;
  486. /* Socket Extensions for SCTP
  487. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  488. *
  489. * sse_flags: 16 bits (unsigned integer)
  490. * Currently unused.
  491. */
  492. sse->sse_flags = 0;
  493. /* Socket Extensions for SCTP
  494. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  495. *
  496. * sse_length: sizeof (__u32)
  497. * This field is the total length of the notification data, including
  498. * the notification header.
  499. */
  500. sse->sse_length = sizeof(struct sctp_shutdown_event);
  501. /* Socket Extensions for SCTP
  502. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  503. *
  504. * sse_assoc_id: sizeof (sctp_assoc_t)
  505. * The association id field, holds the identifier for the association.
  506. * All notifications for a given association have the same association
  507. * identifier. For TCP style socket, this field is ignored.
  508. */
  509. sctp_ulpevent_set_owner(event, asoc);
  510. sse->sse_assoc_id = sctp_assoc2id(asoc);
  511. return event;
  512. fail:
  513. return NULL;
  514. }
  515. /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
  516. *
  517. * Socket Extensions for SCTP
  518. * 5.3.1.6 SCTP_ADAPTATION_INDICATION
  519. */
  520. struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
  521. const struct sctp_association *asoc, gfp_t gfp)
  522. {
  523. struct sctp_ulpevent *event;
  524. struct sctp_adaptation_event *sai;
  525. struct sk_buff *skb;
  526. event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
  527. MSG_NOTIFICATION, gfp);
  528. if (!event)
  529. goto fail;
  530. skb = sctp_event2skb(event);
  531. sai = skb_put(skb, sizeof(struct sctp_adaptation_event));
  532. sai->sai_type = SCTP_ADAPTATION_INDICATION;
  533. sai->sai_flags = 0;
  534. sai->sai_length = sizeof(struct sctp_adaptation_event);
  535. sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
  536. sctp_ulpevent_set_owner(event, asoc);
  537. sai->sai_assoc_id = sctp_assoc2id(asoc);
  538. return event;
  539. fail:
  540. return NULL;
  541. }
  542. /* A message has been received. Package this message as a notification
  543. * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
  544. * even if filtered out later.
  545. *
  546. * Socket Extensions for SCTP
  547. * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
  548. */
  549. struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
  550. struct sctp_chunk *chunk,
  551. gfp_t gfp)
  552. {
  553. struct sctp_ulpevent *event = NULL;
  554. struct sk_buff *skb;
  555. size_t padding, len;
  556. int rx_count;
  557. /*
  558. * check to see if we need to make space for this
  559. * new skb, expand the rcvbuffer if needed, or drop
  560. * the frame
  561. */
  562. if (asoc->ep->rcvbuf_policy)
  563. rx_count = atomic_read(&asoc->rmem_alloc);
  564. else
  565. rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
  566. if (rx_count >= asoc->base.sk->sk_rcvbuf) {
  567. if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
  568. (!sk_rmem_schedule(asoc->base.sk, chunk->skb,
  569. chunk->skb->truesize)))
  570. goto fail;
  571. }
  572. /* Clone the original skb, sharing the data. */
  573. skb = skb_clone(chunk->skb, gfp);
  574. if (!skb)
  575. goto fail;
  576. /* Now that all memory allocations for this chunk succeeded, we
  577. * can mark it as received so the tsn_map is updated correctly.
  578. */
  579. if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
  580. ntohl(chunk->subh.data_hdr->tsn),
  581. chunk->transport))
  582. goto fail_mark;
  583. /* First calculate the padding, so we don't inadvertently
  584. * pass up the wrong length to the user.
  585. *
  586. * RFC 2960 - Section 3.2 Chunk Field Descriptions
  587. *
  588. * The total length of a chunk(including Type, Length and Value fields)
  589. * MUST be a multiple of 4 bytes. If the length of the chunk is not a
  590. * multiple of 4 bytes, the sender MUST pad the chunk with all zero
  591. * bytes and this padding is not included in the chunk length field.
  592. * The sender should never pad with more than 3 bytes. The receiver
  593. * MUST ignore the padding bytes.
  594. */
  595. len = ntohs(chunk->chunk_hdr->length);
  596. padding = SCTP_PAD4(len) - len;
  597. /* Fixup cloned skb with just this chunks data. */
  598. skb_trim(skb, chunk->chunk_end - padding - skb->data);
  599. /* Embed the event fields inside the cloned skb. */
  600. event = sctp_skb2event(skb);
  601. /* Initialize event with flags 0 and correct length
  602. * Since this is a clone of the original skb, only account for
  603. * the data of this chunk as other chunks will be accounted separately.
  604. */
  605. sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
  606. /* And hold the chunk as we need it for getting the IP headers
  607. * later in recvmsg
  608. */
  609. sctp_chunk_hold(chunk);
  610. event->chunk = chunk;
  611. sctp_ulpevent_receive_data(event, asoc);
  612. event->stream = ntohs(chunk->subh.data_hdr->stream);
  613. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  614. event->flags |= SCTP_UNORDERED;
  615. event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  616. }
  617. event->tsn = ntohl(chunk->subh.data_hdr->tsn);
  618. event->msg_flags |= chunk->chunk_hdr->flags;
  619. return event;
  620. fail_mark:
  621. sctp_chunk_put(chunk);
  622. kfree_skb(skb);
  623. fail:
  624. return NULL;
  625. }
  626. /* Create a partial delivery related event.
  627. *
  628. * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
  629. *
  630. * When a receiver is engaged in a partial delivery of a
  631. * message this notification will be used to indicate
  632. * various events.
  633. */
  634. struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
  635. const struct sctp_association *asoc, __u32 indication,
  636. gfp_t gfp)
  637. {
  638. struct sctp_ulpevent *event;
  639. struct sctp_pdapi_event *pd;
  640. struct sk_buff *skb;
  641. event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
  642. MSG_NOTIFICATION, gfp);
  643. if (!event)
  644. goto fail;
  645. skb = sctp_event2skb(event);
  646. pd = skb_put(skb, sizeof(struct sctp_pdapi_event));
  647. /* pdapi_type
  648. * It should be SCTP_PARTIAL_DELIVERY_EVENT
  649. *
  650. * pdapi_flags: 16 bits (unsigned integer)
  651. * Currently unused.
  652. */
  653. pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
  654. pd->pdapi_flags = 0;
  655. /* pdapi_length: 32 bits (unsigned integer)
  656. *
  657. * This field is the total length of the notification data, including
  658. * the notification header. It will generally be sizeof (struct
  659. * sctp_pdapi_event).
  660. */
  661. pd->pdapi_length = sizeof(struct sctp_pdapi_event);
  662. /* pdapi_indication: 32 bits (unsigned integer)
  663. *
  664. * This field holds the indication being sent to the application.
  665. */
  666. pd->pdapi_indication = indication;
  667. /* pdapi_assoc_id: sizeof (sctp_assoc_t)
  668. *
  669. * The association id field, holds the identifier for the association.
  670. */
  671. sctp_ulpevent_set_owner(event, asoc);
  672. pd->pdapi_assoc_id = sctp_assoc2id(asoc);
  673. return event;
  674. fail:
  675. return NULL;
  676. }
  677. struct sctp_ulpevent *sctp_ulpevent_make_authkey(
  678. const struct sctp_association *asoc, __u16 key_id,
  679. __u32 indication, gfp_t gfp)
  680. {
  681. struct sctp_ulpevent *event;
  682. struct sctp_authkey_event *ak;
  683. struct sk_buff *skb;
  684. event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
  685. MSG_NOTIFICATION, gfp);
  686. if (!event)
  687. goto fail;
  688. skb = sctp_event2skb(event);
  689. ak = skb_put(skb, sizeof(struct sctp_authkey_event));
  690. ak->auth_type = SCTP_AUTHENTICATION_EVENT;
  691. ak->auth_flags = 0;
  692. ak->auth_length = sizeof(struct sctp_authkey_event);
  693. ak->auth_keynumber = key_id;
  694. ak->auth_altkeynumber = 0;
  695. ak->auth_indication = indication;
  696. /*
  697. * The association id field, holds the identifier for the association.
  698. */
  699. sctp_ulpevent_set_owner(event, asoc);
  700. ak->auth_assoc_id = sctp_assoc2id(asoc);
  701. return event;
  702. fail:
  703. return NULL;
  704. }
  705. /*
  706. * Socket Extensions for SCTP
  707. * 6.3.10. SCTP_SENDER_DRY_EVENT
  708. */
  709. struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
  710. const struct sctp_association *asoc, gfp_t gfp)
  711. {
  712. struct sctp_ulpevent *event;
  713. struct sctp_sender_dry_event *sdry;
  714. struct sk_buff *skb;
  715. event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
  716. MSG_NOTIFICATION, gfp);
  717. if (!event)
  718. return NULL;
  719. skb = sctp_event2skb(event);
  720. sdry = skb_put(skb, sizeof(struct sctp_sender_dry_event));
  721. sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
  722. sdry->sender_dry_flags = 0;
  723. sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
  724. sctp_ulpevent_set_owner(event, asoc);
  725. sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
  726. return event;
  727. }
  728. struct sctp_ulpevent *sctp_ulpevent_make_stream_reset_event(
  729. const struct sctp_association *asoc, __u16 flags, __u16 stream_num,
  730. __be16 *stream_list, gfp_t gfp)
  731. {
  732. struct sctp_stream_reset_event *sreset;
  733. struct sctp_ulpevent *event;
  734. struct sk_buff *skb;
  735. int length, i;
  736. length = sizeof(struct sctp_stream_reset_event) + 2 * stream_num;
  737. event = sctp_ulpevent_new(length, MSG_NOTIFICATION, gfp);
  738. if (!event)
  739. return NULL;
  740. skb = sctp_event2skb(event);
  741. sreset = skb_put(skb, length);
  742. sreset->strreset_type = SCTP_STREAM_RESET_EVENT;
  743. sreset->strreset_flags = flags;
  744. sreset->strreset_length = length;
  745. sctp_ulpevent_set_owner(event, asoc);
  746. sreset->strreset_assoc_id = sctp_assoc2id(asoc);
  747. for (i = 0; i < stream_num; i++)
  748. sreset->strreset_stream_list[i] = ntohs(stream_list[i]);
  749. return event;
  750. }
  751. struct sctp_ulpevent *sctp_ulpevent_make_assoc_reset_event(
  752. const struct sctp_association *asoc, __u16 flags, __u32 local_tsn,
  753. __u32 remote_tsn, gfp_t gfp)
  754. {
  755. struct sctp_assoc_reset_event *areset;
  756. struct sctp_ulpevent *event;
  757. struct sk_buff *skb;
  758. event = sctp_ulpevent_new(sizeof(struct sctp_assoc_reset_event),
  759. MSG_NOTIFICATION, gfp);
  760. if (!event)
  761. return NULL;
  762. skb = sctp_event2skb(event);
  763. areset = skb_put(skb, sizeof(struct sctp_assoc_reset_event));
  764. areset->assocreset_type = SCTP_ASSOC_RESET_EVENT;
  765. areset->assocreset_flags = flags;
  766. areset->assocreset_length = sizeof(struct sctp_assoc_reset_event);
  767. sctp_ulpevent_set_owner(event, asoc);
  768. areset->assocreset_assoc_id = sctp_assoc2id(asoc);
  769. areset->assocreset_local_tsn = local_tsn;
  770. areset->assocreset_remote_tsn = remote_tsn;
  771. return event;
  772. }
  773. struct sctp_ulpevent *sctp_ulpevent_make_stream_change_event(
  774. const struct sctp_association *asoc, __u16 flags,
  775. __u32 strchange_instrms, __u32 strchange_outstrms, gfp_t gfp)
  776. {
  777. struct sctp_stream_change_event *schange;
  778. struct sctp_ulpevent *event;
  779. struct sk_buff *skb;
  780. event = sctp_ulpevent_new(sizeof(struct sctp_stream_change_event),
  781. MSG_NOTIFICATION, gfp);
  782. if (!event)
  783. return NULL;
  784. skb = sctp_event2skb(event);
  785. schange = skb_put(skb, sizeof(struct sctp_stream_change_event));
  786. schange->strchange_type = SCTP_STREAM_CHANGE_EVENT;
  787. schange->strchange_flags = flags;
  788. schange->strchange_length = sizeof(struct sctp_stream_change_event);
  789. sctp_ulpevent_set_owner(event, asoc);
  790. schange->strchange_assoc_id = sctp_assoc2id(asoc);
  791. schange->strchange_instrms = strchange_instrms;
  792. schange->strchange_outstrms = strchange_outstrms;
  793. return event;
  794. }
  795. /* Return the notification type, assuming this is a notification
  796. * event.
  797. */
  798. __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
  799. {
  800. union sctp_notification *notification;
  801. struct sk_buff *skb;
  802. skb = sctp_event2skb(event);
  803. notification = (union sctp_notification *) skb->data;
  804. return notification->sn_header.sn_type;
  805. }
  806. /* RFC6458, Section 5.3.2. SCTP Header Information Structure
  807. * (SCTP_SNDRCV, DEPRECATED)
  808. */
  809. void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
  810. struct msghdr *msghdr)
  811. {
  812. struct sctp_sndrcvinfo sinfo;
  813. if (sctp_ulpevent_is_notification(event))
  814. return;
  815. memset(&sinfo, 0, sizeof(sinfo));
  816. sinfo.sinfo_stream = event->stream;
  817. sinfo.sinfo_ssn = event->ssn;
  818. sinfo.sinfo_ppid = event->ppid;
  819. sinfo.sinfo_flags = event->flags;
  820. sinfo.sinfo_tsn = event->tsn;
  821. sinfo.sinfo_cumtsn = event->cumtsn;
  822. sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
  823. /* Context value that is set via SCTP_CONTEXT socket option. */
  824. sinfo.sinfo_context = event->asoc->default_rcv_context;
  825. /* These fields are not used while receiving. */
  826. sinfo.sinfo_timetolive = 0;
  827. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
  828. sizeof(sinfo), &sinfo);
  829. }
  830. /* RFC6458, Section 5.3.5 SCTP Receive Information Structure
  831. * (SCTP_SNDRCV)
  832. */
  833. void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
  834. struct msghdr *msghdr)
  835. {
  836. struct sctp_rcvinfo rinfo;
  837. if (sctp_ulpevent_is_notification(event))
  838. return;
  839. memset(&rinfo, 0, sizeof(struct sctp_rcvinfo));
  840. rinfo.rcv_sid = event->stream;
  841. rinfo.rcv_ssn = event->ssn;
  842. rinfo.rcv_ppid = event->ppid;
  843. rinfo.rcv_flags = event->flags;
  844. rinfo.rcv_tsn = event->tsn;
  845. rinfo.rcv_cumtsn = event->cumtsn;
  846. rinfo.rcv_assoc_id = sctp_assoc2id(event->asoc);
  847. rinfo.rcv_context = event->asoc->default_rcv_context;
  848. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_RCVINFO,
  849. sizeof(rinfo), &rinfo);
  850. }
  851. /* RFC6458, Section 5.3.6. SCTP Next Receive Information Structure
  852. * (SCTP_NXTINFO)
  853. */
  854. static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
  855. struct msghdr *msghdr,
  856. const struct sk_buff *skb)
  857. {
  858. struct sctp_nxtinfo nxtinfo;
  859. memset(&nxtinfo, 0, sizeof(nxtinfo));
  860. nxtinfo.nxt_sid = event->stream;
  861. nxtinfo.nxt_ppid = event->ppid;
  862. nxtinfo.nxt_flags = event->flags;
  863. if (sctp_ulpevent_is_notification(event))
  864. nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
  865. nxtinfo.nxt_length = skb->len;
  866. nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
  867. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
  868. sizeof(nxtinfo), &nxtinfo);
  869. }
  870. void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
  871. struct msghdr *msghdr,
  872. struct sock *sk)
  873. {
  874. struct sk_buff *skb;
  875. int err;
  876. skb = sctp_skb_recv_datagram(sk, MSG_PEEK, 1, &err);
  877. if (skb != NULL) {
  878. __sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
  879. msghdr, skb);
  880. /* Just release refcount here. */
  881. kfree_skb(skb);
  882. }
  883. }
  884. /* Do accounting for bytes received and hold a reference to the association
  885. * for each skb.
  886. */
  887. static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
  888. struct sctp_association *asoc)
  889. {
  890. struct sk_buff *skb, *frag;
  891. skb = sctp_event2skb(event);
  892. /* Set the owner and charge rwnd for bytes received. */
  893. sctp_ulpevent_set_owner(event, asoc);
  894. sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
  895. if (!skb->data_len)
  896. return;
  897. /* Note: Not clearing the entire event struct as this is just a
  898. * fragment of the real event. However, we still need to do rwnd
  899. * accounting.
  900. * In general, the skb passed from IP can have only 1 level of
  901. * fragments. But we allow multiple levels of fragments.
  902. */
  903. skb_walk_frags(skb, frag)
  904. sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
  905. }
  906. /* Do accounting for bytes just read by user and release the references to
  907. * the association.
  908. */
  909. static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
  910. {
  911. struct sk_buff *skb, *frag;
  912. unsigned int len;
  913. /* Current stack structures assume that the rcv buffer is
  914. * per socket. For UDP style sockets this is not true as
  915. * multiple associations may be on a single UDP-style socket.
  916. * Use the local private area of the skb to track the owning
  917. * association.
  918. */
  919. skb = sctp_event2skb(event);
  920. len = skb->len;
  921. if (!skb->data_len)
  922. goto done;
  923. /* Don't forget the fragments. */
  924. skb_walk_frags(skb, frag) {
  925. /* NOTE: skb_shinfos are recursive. Although IP returns
  926. * skb's with only 1 level of fragments, SCTP reassembly can
  927. * increase the levels.
  928. */
  929. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  930. }
  931. done:
  932. sctp_assoc_rwnd_increase(event->asoc, len);
  933. sctp_chunk_put(event->chunk);
  934. sctp_ulpevent_release_owner(event);
  935. }
  936. static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
  937. {
  938. struct sk_buff *skb, *frag;
  939. skb = sctp_event2skb(event);
  940. if (!skb->data_len)
  941. goto done;
  942. /* Don't forget the fragments. */
  943. skb_walk_frags(skb, frag) {
  944. /* NOTE: skb_shinfos are recursive. Although IP returns
  945. * skb's with only 1 level of fragments, SCTP reassembly can
  946. * increase the levels.
  947. */
  948. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  949. }
  950. done:
  951. sctp_chunk_put(event->chunk);
  952. sctp_ulpevent_release_owner(event);
  953. }
  954. /* Free a ulpevent that has an owner. It includes releasing the reference
  955. * to the owner, updating the rwnd in case of a DATA event and freeing the
  956. * skb.
  957. */
  958. void sctp_ulpevent_free(struct sctp_ulpevent *event)
  959. {
  960. if (sctp_ulpevent_is_notification(event))
  961. sctp_ulpevent_release_owner(event);
  962. else
  963. sctp_ulpevent_release_data(event);
  964. kfree_skb(sctp_event2skb(event));
  965. }
  966. /* Purge the skb lists holding ulpevents. */
  967. unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
  968. {
  969. struct sk_buff *skb;
  970. unsigned int data_unread = 0;
  971. while ((skb = skb_dequeue(list)) != NULL) {
  972. struct sctp_ulpevent *event = sctp_skb2event(skb);
  973. if (!sctp_ulpevent_is_notification(event))
  974. data_unread += skb->len;
  975. sctp_ulpevent_free(event);
  976. }
  977. return data_unread;
  978. }