ulpevent.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001 Intel Corp.
  7. * Copyright (c) 2001 Nokia, Inc.
  8. * Copyright (c) 2001 La Monte H.P. Yarroll
  9. *
  10. * These functions manipulate an sctp event. The struct ulpevent is used
  11. * to carry notifications and data to the ULP (sockets).
  12. *
  13. * Please send any bug reports or fixes you make to the
  14. * email address(es):
  15. * lksctp developers <[email protected]>
  16. *
  17. * Written or modified by:
  18. * Jon Grimm <[email protected]>
  19. * La Monte H.P. Yarroll <[email protected]>
  20. * Ardelle Fan <[email protected]>
  21. * Sridhar Samudrala <[email protected]>
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/types.h>
  25. #include <linux/skbuff.h>
  26. #include <net/sctp/structs.h>
  27. #include <net/sctp/sctp.h>
  28. #include <net/sctp/sm.h>
  29. static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
  30. struct sctp_association *asoc);
  31. static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
  32. static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
  33. /* Initialize an ULP event from an given skb. */
  34. static void sctp_ulpevent_init(struct sctp_ulpevent *event,
  35. __u16 msg_flags,
  36. unsigned int len)
  37. {
  38. memset(event, 0, sizeof(struct sctp_ulpevent));
  39. event->msg_flags = msg_flags;
  40. event->rmem_len = len;
  41. }
  42. /* Create a new sctp_ulpevent. */
  43. static struct sctp_ulpevent *sctp_ulpevent_new(int size, __u16 msg_flags,
  44. gfp_t gfp)
  45. {
  46. struct sctp_ulpevent *event;
  47. struct sk_buff *skb;
  48. skb = alloc_skb(size, gfp);
  49. if (!skb)
  50. goto fail;
  51. event = sctp_skb2event(skb);
  52. sctp_ulpevent_init(event, msg_flags, skb->truesize);
  53. return event;
  54. fail:
  55. return NULL;
  56. }
  57. /* Is this a MSG_NOTIFICATION? */
  58. int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
  59. {
  60. return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
  61. }
  62. /* Hold the association in case the msg_name needs read out of
  63. * the association.
  64. */
  65. static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
  66. const struct sctp_association *asoc)
  67. {
  68. struct sctp_chunk *chunk = event->chunk;
  69. struct sk_buff *skb;
  70. /* Cast away the const, as we are just wanting to
  71. * bump the reference count.
  72. */
  73. sctp_association_hold((struct sctp_association *)asoc);
  74. skb = sctp_event2skb(event);
  75. event->asoc = (struct sctp_association *)asoc;
  76. atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
  77. sctp_skb_set_owner_r(skb, asoc->base.sk);
  78. if (chunk && chunk->head_skb && !chunk->head_skb->sk)
  79. chunk->head_skb->sk = asoc->base.sk;
  80. }
  81. /* A simple destructor to give up the reference to the association. */
  82. static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
  83. {
  84. struct sctp_association *asoc = event->asoc;
  85. atomic_sub(event->rmem_len, &asoc->rmem_alloc);
  86. sctp_association_put(asoc);
  87. }
  88. /* Create and initialize an SCTP_ASSOC_CHANGE event.
  89. *
  90. * 5.3.1.1 SCTP_ASSOC_CHANGE
  91. *
  92. * Communication notifications inform the ULP that an SCTP association
  93. * has either begun or ended. The identifier for a new association is
  94. * provided by this notification.
  95. *
  96. * Note: There is no field checking here. If a field is unused it will be
  97. * zero'd out.
  98. */
  99. struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
  100. const struct sctp_association *asoc,
  101. __u16 flags, __u16 state, __u16 error, __u16 outbound,
  102. __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
  103. {
  104. struct sctp_ulpevent *event;
  105. struct sctp_assoc_change *sac;
  106. struct sk_buff *skb;
  107. /* If the lower layer passed in the chunk, it will be
  108. * an ABORT, so we need to include it in the sac_info.
  109. */
  110. if (chunk) {
  111. /* Copy the chunk data to a new skb and reserve enough
  112. * head room to use as notification.
  113. */
  114. skb = skb_copy_expand(chunk->skb,
  115. sizeof(struct sctp_assoc_change), 0, gfp);
  116. if (!skb)
  117. goto fail;
  118. /* Embed the event fields inside the cloned skb. */
  119. event = sctp_skb2event(skb);
  120. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  121. /* Include the notification structure */
  122. sac = skb_push(skb, sizeof(struct sctp_assoc_change));
  123. /* Trim the buffer to the right length. */
  124. skb_trim(skb, sizeof(struct sctp_assoc_change) +
  125. ntohs(chunk->chunk_hdr->length) -
  126. sizeof(struct sctp_chunkhdr));
  127. } else {
  128. event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
  129. MSG_NOTIFICATION, gfp);
  130. if (!event)
  131. goto fail;
  132. skb = sctp_event2skb(event);
  133. sac = skb_put(skb, sizeof(struct sctp_assoc_change));
  134. }
  135. /* Socket Extensions for SCTP
  136. * 5.3.1.1 SCTP_ASSOC_CHANGE
  137. *
  138. * sac_type:
  139. * It should be SCTP_ASSOC_CHANGE.
  140. */
  141. sac->sac_type = SCTP_ASSOC_CHANGE;
  142. /* Socket Extensions for SCTP
  143. * 5.3.1.1 SCTP_ASSOC_CHANGE
  144. *
  145. * sac_state: 32 bits (signed integer)
  146. * This field holds one of a number of values that communicate the
  147. * event that happened to the association.
  148. */
  149. sac->sac_state = state;
  150. /* Socket Extensions for SCTP
  151. * 5.3.1.1 SCTP_ASSOC_CHANGE
  152. *
  153. * sac_flags: 16 bits (unsigned integer)
  154. * Currently unused.
  155. */
  156. sac->sac_flags = 0;
  157. /* Socket Extensions for SCTP
  158. * 5.3.1.1 SCTP_ASSOC_CHANGE
  159. *
  160. * sac_length: sizeof (__u32)
  161. * This field is the total length of the notification data, including
  162. * the notification header.
  163. */
  164. sac->sac_length = skb->len;
  165. /* Socket Extensions for SCTP
  166. * 5.3.1.1 SCTP_ASSOC_CHANGE
  167. *
  168. * sac_error: 32 bits (signed integer)
  169. *
  170. * If the state was reached due to a error condition (e.g.
  171. * COMMUNICATION_LOST) any relevant error information is available in
  172. * this field. This corresponds to the protocol error codes defined in
  173. * [SCTP].
  174. */
  175. sac->sac_error = error;
  176. /* Socket Extensions for SCTP
  177. * 5.3.1.1 SCTP_ASSOC_CHANGE
  178. *
  179. * sac_outbound_streams: 16 bits (unsigned integer)
  180. * sac_inbound_streams: 16 bits (unsigned integer)
  181. *
  182. * The maximum number of streams allowed in each direction are
  183. * available in sac_outbound_streams and sac_inbound streams.
  184. */
  185. sac->sac_outbound_streams = outbound;
  186. sac->sac_inbound_streams = inbound;
  187. /* Socket Extensions for SCTP
  188. * 5.3.1.1 SCTP_ASSOC_CHANGE
  189. *
  190. * sac_assoc_id: sizeof (sctp_assoc_t)
  191. *
  192. * The association id field, holds the identifier for the association.
  193. * All notifications for a given association have the same association
  194. * identifier. For TCP style socket, this field is ignored.
  195. */
  196. sctp_ulpevent_set_owner(event, asoc);
  197. sac->sac_assoc_id = sctp_assoc2id(asoc);
  198. return event;
  199. fail:
  200. return NULL;
  201. }
  202. /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
  203. *
  204. * Socket Extensions for SCTP - draft-01
  205. * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  206. *
  207. * When a destination address on a multi-homed peer encounters a change
  208. * an interface details event is sent.
  209. */
  210. static struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
  211. const struct sctp_association *asoc,
  212. const struct sockaddr_storage *aaddr,
  213. int flags, int state, int error, gfp_t gfp)
  214. {
  215. struct sctp_ulpevent *event;
  216. struct sctp_paddr_change *spc;
  217. struct sk_buff *skb;
  218. event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
  219. MSG_NOTIFICATION, gfp);
  220. if (!event)
  221. goto fail;
  222. skb = sctp_event2skb(event);
  223. spc = skb_put(skb, sizeof(struct sctp_paddr_change));
  224. /* Sockets API Extensions for SCTP
  225. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  226. *
  227. * spc_type:
  228. *
  229. * It should be SCTP_PEER_ADDR_CHANGE.
  230. */
  231. spc->spc_type = SCTP_PEER_ADDR_CHANGE;
  232. /* Sockets API Extensions for SCTP
  233. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  234. *
  235. * spc_length: sizeof (__u32)
  236. *
  237. * This field is the total length of the notification data, including
  238. * the notification header.
  239. */
  240. spc->spc_length = sizeof(struct sctp_paddr_change);
  241. /* Sockets API Extensions for SCTP
  242. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  243. *
  244. * spc_flags: 16 bits (unsigned integer)
  245. * Currently unused.
  246. */
  247. spc->spc_flags = 0;
  248. /* Sockets API Extensions for SCTP
  249. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  250. *
  251. * spc_state: 32 bits (signed integer)
  252. *
  253. * This field holds one of a number of values that communicate the
  254. * event that happened to the address.
  255. */
  256. spc->spc_state = state;
  257. /* Sockets API Extensions for SCTP
  258. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  259. *
  260. * spc_error: 32 bits (signed integer)
  261. *
  262. * If the state was reached due to any error condition (e.g.
  263. * ADDRESS_UNREACHABLE) any relevant error information is available in
  264. * this field.
  265. */
  266. spc->spc_error = error;
  267. /* Socket Extensions for SCTP
  268. * 5.3.1.1 SCTP_ASSOC_CHANGE
  269. *
  270. * spc_assoc_id: sizeof (sctp_assoc_t)
  271. *
  272. * The association id field, holds the identifier for the association.
  273. * All notifications for a given association have the same association
  274. * identifier. For TCP style socket, this field is ignored.
  275. */
  276. sctp_ulpevent_set_owner(event, asoc);
  277. spc->spc_assoc_id = sctp_assoc2id(asoc);
  278. /* Sockets API Extensions for SCTP
  279. * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
  280. *
  281. * spc_aaddr: sizeof (struct sockaddr_storage)
  282. *
  283. * The affected address field, holds the remote peer's address that is
  284. * encountering the change of state.
  285. */
  286. memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
  287. /* Map ipv4 address into v4-mapped-on-v6 address. */
  288. sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_to_user(
  289. sctp_sk(asoc->base.sk),
  290. (union sctp_addr *)&spc->spc_aaddr);
  291. return event;
  292. fail:
  293. return NULL;
  294. }
  295. void sctp_ulpevent_notify_peer_addr_change(struct sctp_transport *transport,
  296. int state, int error)
  297. {
  298. struct sctp_association *asoc = transport->asoc;
  299. struct sockaddr_storage addr;
  300. struct sctp_ulpevent *event;
  301. if (asoc->state < SCTP_STATE_ESTABLISHED)
  302. return;
  303. memset(&addr, 0, sizeof(struct sockaddr_storage));
  304. memcpy(&addr, &transport->ipaddr, transport->af_specific->sockaddr_len);
  305. event = sctp_ulpevent_make_peer_addr_change(asoc, &addr, 0, state,
  306. error, GFP_ATOMIC);
  307. if (event)
  308. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  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. struct sctp_ulpevent *sctp_ulpevent_make_send_failed_event(
  462. const struct sctp_association *asoc, struct sctp_chunk *chunk,
  463. __u16 flags, __u32 error, gfp_t gfp)
  464. {
  465. struct sctp_send_failed_event *ssf;
  466. struct sctp_ulpevent *event;
  467. struct sk_buff *skb;
  468. int len;
  469. skb = skb_copy_expand(chunk->skb, sizeof(*ssf), 0, gfp);
  470. if (!skb)
  471. return NULL;
  472. len = ntohs(chunk->chunk_hdr->length);
  473. len -= sctp_datachk_len(&asoc->stream);
  474. skb_pull(skb, sctp_datachk_len(&asoc->stream));
  475. event = sctp_skb2event(skb);
  476. sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
  477. ssf = skb_push(skb, sizeof(*ssf));
  478. ssf->ssf_type = SCTP_SEND_FAILED_EVENT;
  479. ssf->ssf_flags = flags;
  480. ssf->ssf_length = sizeof(*ssf) + len;
  481. skb_trim(skb, ssf->ssf_length);
  482. ssf->ssf_error = error;
  483. ssf->ssfe_info.snd_sid = chunk->sinfo.sinfo_stream;
  484. ssf->ssfe_info.snd_ppid = chunk->sinfo.sinfo_ppid;
  485. ssf->ssfe_info.snd_context = chunk->sinfo.sinfo_context;
  486. ssf->ssfe_info.snd_assoc_id = chunk->sinfo.sinfo_assoc_id;
  487. ssf->ssfe_info.snd_flags = chunk->chunk_hdr->flags;
  488. sctp_ulpevent_set_owner(event, asoc);
  489. ssf->ssf_assoc_id = sctp_assoc2id(asoc);
  490. return event;
  491. }
  492. /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
  493. *
  494. * Socket Extensions for SCTP - draft-01
  495. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  496. */
  497. struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
  498. const struct sctp_association *asoc,
  499. __u16 flags, gfp_t gfp)
  500. {
  501. struct sctp_ulpevent *event;
  502. struct sctp_shutdown_event *sse;
  503. struct sk_buff *skb;
  504. event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
  505. MSG_NOTIFICATION, gfp);
  506. if (!event)
  507. goto fail;
  508. skb = sctp_event2skb(event);
  509. sse = skb_put(skb, sizeof(struct sctp_shutdown_event));
  510. /* Socket Extensions for SCTP
  511. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  512. *
  513. * sse_type
  514. * It should be SCTP_SHUTDOWN_EVENT
  515. */
  516. sse->sse_type = SCTP_SHUTDOWN_EVENT;
  517. /* Socket Extensions for SCTP
  518. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  519. *
  520. * sse_flags: 16 bits (unsigned integer)
  521. * Currently unused.
  522. */
  523. sse->sse_flags = 0;
  524. /* Socket Extensions for SCTP
  525. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  526. *
  527. * sse_length: sizeof (__u32)
  528. * This field is the total length of the notification data, including
  529. * the notification header.
  530. */
  531. sse->sse_length = sizeof(struct sctp_shutdown_event);
  532. /* Socket Extensions for SCTP
  533. * 5.3.1.5 SCTP_SHUTDOWN_EVENT
  534. *
  535. * sse_assoc_id: sizeof (sctp_assoc_t)
  536. * The association id field, holds the identifier for the association.
  537. * All notifications for a given association have the same association
  538. * identifier. For TCP style socket, this field is ignored.
  539. */
  540. sctp_ulpevent_set_owner(event, asoc);
  541. sse->sse_assoc_id = sctp_assoc2id(asoc);
  542. return event;
  543. fail:
  544. return NULL;
  545. }
  546. /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
  547. *
  548. * Socket Extensions for SCTP
  549. * 5.3.1.6 SCTP_ADAPTATION_INDICATION
  550. */
  551. struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
  552. const struct sctp_association *asoc, gfp_t gfp)
  553. {
  554. struct sctp_ulpevent *event;
  555. struct sctp_adaptation_event *sai;
  556. struct sk_buff *skb;
  557. event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
  558. MSG_NOTIFICATION, gfp);
  559. if (!event)
  560. goto fail;
  561. skb = sctp_event2skb(event);
  562. sai = skb_put(skb, sizeof(struct sctp_adaptation_event));
  563. sai->sai_type = SCTP_ADAPTATION_INDICATION;
  564. sai->sai_flags = 0;
  565. sai->sai_length = sizeof(struct sctp_adaptation_event);
  566. sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
  567. sctp_ulpevent_set_owner(event, asoc);
  568. sai->sai_assoc_id = sctp_assoc2id(asoc);
  569. return event;
  570. fail:
  571. return NULL;
  572. }
  573. /* A message has been received. Package this message as a notification
  574. * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
  575. * even if filtered out later.
  576. *
  577. * Socket Extensions for SCTP
  578. * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
  579. */
  580. struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
  581. struct sctp_chunk *chunk,
  582. gfp_t gfp)
  583. {
  584. struct sctp_ulpevent *event = NULL;
  585. struct sk_buff *skb = chunk->skb;
  586. struct sock *sk = asoc->base.sk;
  587. size_t padding, datalen;
  588. int rx_count;
  589. /*
  590. * check to see if we need to make space for this
  591. * new skb, expand the rcvbuffer if needed, or drop
  592. * the frame
  593. */
  594. if (asoc->ep->rcvbuf_policy)
  595. rx_count = atomic_read(&asoc->rmem_alloc);
  596. else
  597. rx_count = atomic_read(&sk->sk_rmem_alloc);
  598. datalen = ntohs(chunk->chunk_hdr->length);
  599. if (rx_count >= sk->sk_rcvbuf || !sk_rmem_schedule(sk, skb, datalen))
  600. goto fail;
  601. /* Clone the original skb, sharing the data. */
  602. skb = skb_clone(chunk->skb, gfp);
  603. if (!skb)
  604. goto fail;
  605. /* Now that all memory allocations for this chunk succeeded, we
  606. * can mark it as received so the tsn_map is updated correctly.
  607. */
  608. if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
  609. ntohl(chunk->subh.data_hdr->tsn),
  610. chunk->transport))
  611. goto fail_mark;
  612. /* First calculate the padding, so we don't inadvertently
  613. * pass up the wrong length to the user.
  614. *
  615. * RFC 2960 - Section 3.2 Chunk Field Descriptions
  616. *
  617. * The total length of a chunk(including Type, Length and Value fields)
  618. * MUST be a multiple of 4 bytes. If the length of the chunk is not a
  619. * multiple of 4 bytes, the sender MUST pad the chunk with all zero
  620. * bytes and this padding is not included in the chunk length field.
  621. * The sender should never pad with more than 3 bytes. The receiver
  622. * MUST ignore the padding bytes.
  623. */
  624. padding = SCTP_PAD4(datalen) - datalen;
  625. /* Fixup cloned skb with just this chunks data. */
  626. skb_trim(skb, chunk->chunk_end - padding - skb->data);
  627. /* Embed the event fields inside the cloned skb. */
  628. event = sctp_skb2event(skb);
  629. /* Initialize event with flags 0 and correct length
  630. * Since this is a clone of the original skb, only account for
  631. * the data of this chunk as other chunks will be accounted separately.
  632. */
  633. sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
  634. /* And hold the chunk as we need it for getting the IP headers
  635. * later in recvmsg
  636. */
  637. sctp_chunk_hold(chunk);
  638. event->chunk = chunk;
  639. sctp_ulpevent_receive_data(event, asoc);
  640. event->stream = ntohs(chunk->subh.data_hdr->stream);
  641. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  642. event->flags |= SCTP_UNORDERED;
  643. event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  644. }
  645. event->tsn = ntohl(chunk->subh.data_hdr->tsn);
  646. event->msg_flags |= chunk->chunk_hdr->flags;
  647. return event;
  648. fail_mark:
  649. kfree_skb(skb);
  650. fail:
  651. return NULL;
  652. }
  653. /* Create a partial delivery related event.
  654. *
  655. * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
  656. *
  657. * When a receiver is engaged in a partial delivery of a
  658. * message this notification will be used to indicate
  659. * various events.
  660. */
  661. struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
  662. const struct sctp_association *asoc,
  663. __u32 indication, __u32 sid, __u32 seq,
  664. __u32 flags, gfp_t gfp)
  665. {
  666. struct sctp_ulpevent *event;
  667. struct sctp_pdapi_event *pd;
  668. struct sk_buff *skb;
  669. event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
  670. MSG_NOTIFICATION, gfp);
  671. if (!event)
  672. goto fail;
  673. skb = sctp_event2skb(event);
  674. pd = skb_put(skb, sizeof(struct sctp_pdapi_event));
  675. /* pdapi_type
  676. * It should be SCTP_PARTIAL_DELIVERY_EVENT
  677. *
  678. * pdapi_flags: 16 bits (unsigned integer)
  679. * Currently unused.
  680. */
  681. pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
  682. pd->pdapi_flags = flags;
  683. pd->pdapi_stream = sid;
  684. pd->pdapi_seq = seq;
  685. /* pdapi_length: 32 bits (unsigned integer)
  686. *
  687. * This field is the total length of the notification data, including
  688. * the notification header. It will generally be sizeof (struct
  689. * sctp_pdapi_event).
  690. */
  691. pd->pdapi_length = sizeof(struct sctp_pdapi_event);
  692. /* pdapi_indication: 32 bits (unsigned integer)
  693. *
  694. * This field holds the indication being sent to the application.
  695. */
  696. pd->pdapi_indication = indication;
  697. /* pdapi_assoc_id: sizeof (sctp_assoc_t)
  698. *
  699. * The association id field, holds the identifier for the association.
  700. */
  701. sctp_ulpevent_set_owner(event, asoc);
  702. pd->pdapi_assoc_id = sctp_assoc2id(asoc);
  703. return event;
  704. fail:
  705. return NULL;
  706. }
  707. struct sctp_ulpevent *sctp_ulpevent_make_authkey(
  708. const struct sctp_association *asoc, __u16 key_id,
  709. __u32 indication, gfp_t gfp)
  710. {
  711. struct sctp_ulpevent *event;
  712. struct sctp_authkey_event *ak;
  713. struct sk_buff *skb;
  714. event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
  715. MSG_NOTIFICATION, gfp);
  716. if (!event)
  717. goto fail;
  718. skb = sctp_event2skb(event);
  719. ak = skb_put(skb, sizeof(struct sctp_authkey_event));
  720. ak->auth_type = SCTP_AUTHENTICATION_EVENT;
  721. ak->auth_flags = 0;
  722. ak->auth_length = sizeof(struct sctp_authkey_event);
  723. ak->auth_keynumber = key_id;
  724. ak->auth_altkeynumber = 0;
  725. ak->auth_indication = indication;
  726. /*
  727. * The association id field, holds the identifier for the association.
  728. */
  729. sctp_ulpevent_set_owner(event, asoc);
  730. ak->auth_assoc_id = sctp_assoc2id(asoc);
  731. return event;
  732. fail:
  733. return NULL;
  734. }
  735. /*
  736. * Socket Extensions for SCTP
  737. * 6.3.10. SCTP_SENDER_DRY_EVENT
  738. */
  739. struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
  740. const struct sctp_association *asoc, gfp_t gfp)
  741. {
  742. struct sctp_ulpevent *event;
  743. struct sctp_sender_dry_event *sdry;
  744. struct sk_buff *skb;
  745. event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
  746. MSG_NOTIFICATION, gfp);
  747. if (!event)
  748. return NULL;
  749. skb = sctp_event2skb(event);
  750. sdry = skb_put(skb, sizeof(struct sctp_sender_dry_event));
  751. sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
  752. sdry->sender_dry_flags = 0;
  753. sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
  754. sctp_ulpevent_set_owner(event, asoc);
  755. sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
  756. return event;
  757. }
  758. struct sctp_ulpevent *sctp_ulpevent_make_stream_reset_event(
  759. const struct sctp_association *asoc, __u16 flags, __u16 stream_num,
  760. __be16 *stream_list, gfp_t gfp)
  761. {
  762. struct sctp_stream_reset_event *sreset;
  763. struct sctp_ulpevent *event;
  764. struct sk_buff *skb;
  765. int length, i;
  766. length = sizeof(struct sctp_stream_reset_event) + 2 * stream_num;
  767. event = sctp_ulpevent_new(length, MSG_NOTIFICATION, gfp);
  768. if (!event)
  769. return NULL;
  770. skb = sctp_event2skb(event);
  771. sreset = skb_put(skb, length);
  772. sreset->strreset_type = SCTP_STREAM_RESET_EVENT;
  773. sreset->strreset_flags = flags;
  774. sreset->strreset_length = length;
  775. sctp_ulpevent_set_owner(event, asoc);
  776. sreset->strreset_assoc_id = sctp_assoc2id(asoc);
  777. for (i = 0; i < stream_num; i++)
  778. sreset->strreset_stream_list[i] = ntohs(stream_list[i]);
  779. return event;
  780. }
  781. struct sctp_ulpevent *sctp_ulpevent_make_assoc_reset_event(
  782. const struct sctp_association *asoc, __u16 flags, __u32 local_tsn,
  783. __u32 remote_tsn, gfp_t gfp)
  784. {
  785. struct sctp_assoc_reset_event *areset;
  786. struct sctp_ulpevent *event;
  787. struct sk_buff *skb;
  788. event = sctp_ulpevent_new(sizeof(struct sctp_assoc_reset_event),
  789. MSG_NOTIFICATION, gfp);
  790. if (!event)
  791. return NULL;
  792. skb = sctp_event2skb(event);
  793. areset = skb_put(skb, sizeof(struct sctp_assoc_reset_event));
  794. areset->assocreset_type = SCTP_ASSOC_RESET_EVENT;
  795. areset->assocreset_flags = flags;
  796. areset->assocreset_length = sizeof(struct sctp_assoc_reset_event);
  797. sctp_ulpevent_set_owner(event, asoc);
  798. areset->assocreset_assoc_id = sctp_assoc2id(asoc);
  799. areset->assocreset_local_tsn = local_tsn;
  800. areset->assocreset_remote_tsn = remote_tsn;
  801. return event;
  802. }
  803. struct sctp_ulpevent *sctp_ulpevent_make_stream_change_event(
  804. const struct sctp_association *asoc, __u16 flags,
  805. __u32 strchange_instrms, __u32 strchange_outstrms, gfp_t gfp)
  806. {
  807. struct sctp_stream_change_event *schange;
  808. struct sctp_ulpevent *event;
  809. struct sk_buff *skb;
  810. event = sctp_ulpevent_new(sizeof(struct sctp_stream_change_event),
  811. MSG_NOTIFICATION, gfp);
  812. if (!event)
  813. return NULL;
  814. skb = sctp_event2skb(event);
  815. schange = skb_put(skb, sizeof(struct sctp_stream_change_event));
  816. schange->strchange_type = SCTP_STREAM_CHANGE_EVENT;
  817. schange->strchange_flags = flags;
  818. schange->strchange_length = sizeof(struct sctp_stream_change_event);
  819. sctp_ulpevent_set_owner(event, asoc);
  820. schange->strchange_assoc_id = sctp_assoc2id(asoc);
  821. schange->strchange_instrms = strchange_instrms;
  822. schange->strchange_outstrms = strchange_outstrms;
  823. return event;
  824. }
  825. /* Return the notification type, assuming this is a notification
  826. * event.
  827. */
  828. __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
  829. {
  830. union sctp_notification *notification;
  831. struct sk_buff *skb;
  832. skb = sctp_event2skb(event);
  833. notification = (union sctp_notification *) skb->data;
  834. return notification->sn_header.sn_type;
  835. }
  836. /* RFC6458, Section 5.3.2. SCTP Header Information Structure
  837. * (SCTP_SNDRCV, DEPRECATED)
  838. */
  839. void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
  840. struct msghdr *msghdr)
  841. {
  842. struct sctp_sndrcvinfo sinfo;
  843. if (sctp_ulpevent_is_notification(event))
  844. return;
  845. memset(&sinfo, 0, sizeof(sinfo));
  846. sinfo.sinfo_stream = event->stream;
  847. sinfo.sinfo_ssn = event->ssn;
  848. sinfo.sinfo_ppid = event->ppid;
  849. sinfo.sinfo_flags = event->flags;
  850. sinfo.sinfo_tsn = event->tsn;
  851. sinfo.sinfo_cumtsn = event->cumtsn;
  852. sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
  853. /* Context value that is set via SCTP_CONTEXT socket option. */
  854. sinfo.sinfo_context = event->asoc->default_rcv_context;
  855. /* These fields are not used while receiving. */
  856. sinfo.sinfo_timetolive = 0;
  857. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
  858. sizeof(sinfo), &sinfo);
  859. }
  860. /* RFC6458, Section 5.3.5 SCTP Receive Information Structure
  861. * (SCTP_SNDRCV)
  862. */
  863. void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
  864. struct msghdr *msghdr)
  865. {
  866. struct sctp_rcvinfo rinfo;
  867. if (sctp_ulpevent_is_notification(event))
  868. return;
  869. memset(&rinfo, 0, sizeof(struct sctp_rcvinfo));
  870. rinfo.rcv_sid = event->stream;
  871. rinfo.rcv_ssn = event->ssn;
  872. rinfo.rcv_ppid = event->ppid;
  873. rinfo.rcv_flags = event->flags;
  874. rinfo.rcv_tsn = event->tsn;
  875. rinfo.rcv_cumtsn = event->cumtsn;
  876. rinfo.rcv_assoc_id = sctp_assoc2id(event->asoc);
  877. rinfo.rcv_context = event->asoc->default_rcv_context;
  878. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_RCVINFO,
  879. sizeof(rinfo), &rinfo);
  880. }
  881. /* RFC6458, Section 5.3.6. SCTP Next Receive Information Structure
  882. * (SCTP_NXTINFO)
  883. */
  884. static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
  885. struct msghdr *msghdr,
  886. const struct sk_buff *skb)
  887. {
  888. struct sctp_nxtinfo nxtinfo;
  889. memset(&nxtinfo, 0, sizeof(nxtinfo));
  890. nxtinfo.nxt_sid = event->stream;
  891. nxtinfo.nxt_ppid = event->ppid;
  892. nxtinfo.nxt_flags = event->flags;
  893. if (sctp_ulpevent_is_notification(event))
  894. nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
  895. nxtinfo.nxt_length = skb->len;
  896. nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
  897. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
  898. sizeof(nxtinfo), &nxtinfo);
  899. }
  900. void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
  901. struct msghdr *msghdr,
  902. struct sock *sk)
  903. {
  904. struct sk_buff *skb;
  905. int err;
  906. skb = sctp_skb_recv_datagram(sk, MSG_PEEK | MSG_DONTWAIT, &err);
  907. if (skb != NULL) {
  908. __sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
  909. msghdr, skb);
  910. /* Just release refcount here. */
  911. kfree_skb(skb);
  912. }
  913. }
  914. /* Do accounting for bytes received and hold a reference to the association
  915. * for each skb.
  916. */
  917. static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
  918. struct sctp_association *asoc)
  919. {
  920. struct sk_buff *skb, *frag;
  921. skb = sctp_event2skb(event);
  922. /* Set the owner and charge rwnd for bytes received. */
  923. sctp_ulpevent_set_owner(event, asoc);
  924. sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
  925. if (!skb->data_len)
  926. return;
  927. /* Note: Not clearing the entire event struct as this is just a
  928. * fragment of the real event. However, we still need to do rwnd
  929. * accounting.
  930. * In general, the skb passed from IP can have only 1 level of
  931. * fragments. But we allow multiple levels of fragments.
  932. */
  933. skb_walk_frags(skb, frag)
  934. sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
  935. }
  936. /* Do accounting for bytes just read by user and release the references to
  937. * the association.
  938. */
  939. static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
  940. {
  941. struct sk_buff *skb, *frag;
  942. unsigned int len;
  943. /* Current stack structures assume that the rcv buffer is
  944. * per socket. For UDP style sockets this is not true as
  945. * multiple associations may be on a single UDP-style socket.
  946. * Use the local private area of the skb to track the owning
  947. * association.
  948. */
  949. skb = sctp_event2skb(event);
  950. len = skb->len;
  951. if (!skb->data_len)
  952. goto done;
  953. /* Don't forget the fragments. */
  954. skb_walk_frags(skb, frag) {
  955. /* NOTE: skb_shinfos are recursive. Although IP returns
  956. * skb's with only 1 level of fragments, SCTP reassembly can
  957. * increase the levels.
  958. */
  959. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  960. }
  961. done:
  962. sctp_assoc_rwnd_increase(event->asoc, len);
  963. sctp_chunk_put(event->chunk);
  964. sctp_ulpevent_release_owner(event);
  965. }
  966. static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
  967. {
  968. struct sk_buff *skb, *frag;
  969. skb = sctp_event2skb(event);
  970. if (!skb->data_len)
  971. goto done;
  972. /* Don't forget the fragments. */
  973. skb_walk_frags(skb, frag) {
  974. /* NOTE: skb_shinfos are recursive. Although IP returns
  975. * skb's with only 1 level of fragments, SCTP reassembly can
  976. * increase the levels.
  977. */
  978. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  979. }
  980. done:
  981. sctp_chunk_put(event->chunk);
  982. sctp_ulpevent_release_owner(event);
  983. }
  984. /* Free a ulpevent that has an owner. It includes releasing the reference
  985. * to the owner, updating the rwnd in case of a DATA event and freeing the
  986. * skb.
  987. */
  988. void sctp_ulpevent_free(struct sctp_ulpevent *event)
  989. {
  990. if (sctp_ulpevent_is_notification(event))
  991. sctp_ulpevent_release_owner(event);
  992. else
  993. sctp_ulpevent_release_data(event);
  994. kfree_skb(sctp_event2skb(event));
  995. }
  996. /* Purge the skb lists holding ulpevents. */
  997. unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
  998. {
  999. struct sk_buff *skb;
  1000. unsigned int data_unread = 0;
  1001. while ((skb = skb_dequeue(list)) != NULL) {
  1002. struct sctp_ulpevent *event = sctp_skb2event(skb);
  1003. if (!sctp_ulpevent_is_notification(event))
  1004. data_unread += skb->len;
  1005. sctp_ulpevent_free(event);
  1006. }
  1007. return data_unread;
  1008. }