ulpevent.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. kfree_skb(skb);
  622. fail:
  623. return NULL;
  624. }
  625. /* Create a partial delivery related event.
  626. *
  627. * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
  628. *
  629. * When a receiver is engaged in a partial delivery of a
  630. * message this notification will be used to indicate
  631. * various events.
  632. */
  633. struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
  634. const struct sctp_association *asoc,
  635. __u32 indication, __u32 sid, __u32 seq,
  636. __u32 flags, 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 = flags;
  655. pd->pdapi_stream = sid;
  656. pd->pdapi_seq = seq;
  657. /* pdapi_length: 32 bits (unsigned integer)
  658. *
  659. * This field is the total length of the notification data, including
  660. * the notification header. It will generally be sizeof (struct
  661. * sctp_pdapi_event).
  662. */
  663. pd->pdapi_length = sizeof(struct sctp_pdapi_event);
  664. /* pdapi_indication: 32 bits (unsigned integer)
  665. *
  666. * This field holds the indication being sent to the application.
  667. */
  668. pd->pdapi_indication = indication;
  669. /* pdapi_assoc_id: sizeof (sctp_assoc_t)
  670. *
  671. * The association id field, holds the identifier for the association.
  672. */
  673. sctp_ulpevent_set_owner(event, asoc);
  674. pd->pdapi_assoc_id = sctp_assoc2id(asoc);
  675. return event;
  676. fail:
  677. return NULL;
  678. }
  679. struct sctp_ulpevent *sctp_ulpevent_make_authkey(
  680. const struct sctp_association *asoc, __u16 key_id,
  681. __u32 indication, gfp_t gfp)
  682. {
  683. struct sctp_ulpevent *event;
  684. struct sctp_authkey_event *ak;
  685. struct sk_buff *skb;
  686. event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
  687. MSG_NOTIFICATION, gfp);
  688. if (!event)
  689. goto fail;
  690. skb = sctp_event2skb(event);
  691. ak = skb_put(skb, sizeof(struct sctp_authkey_event));
  692. ak->auth_type = SCTP_AUTHENTICATION_EVENT;
  693. ak->auth_flags = 0;
  694. ak->auth_length = sizeof(struct sctp_authkey_event);
  695. ak->auth_keynumber = key_id;
  696. ak->auth_altkeynumber = 0;
  697. ak->auth_indication = indication;
  698. /*
  699. * The association id field, holds the identifier for the association.
  700. */
  701. sctp_ulpevent_set_owner(event, asoc);
  702. ak->auth_assoc_id = sctp_assoc2id(asoc);
  703. return event;
  704. fail:
  705. return NULL;
  706. }
  707. /*
  708. * Socket Extensions for SCTP
  709. * 6.3.10. SCTP_SENDER_DRY_EVENT
  710. */
  711. struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
  712. const struct sctp_association *asoc, gfp_t gfp)
  713. {
  714. struct sctp_ulpevent *event;
  715. struct sctp_sender_dry_event *sdry;
  716. struct sk_buff *skb;
  717. event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
  718. MSG_NOTIFICATION, gfp);
  719. if (!event)
  720. return NULL;
  721. skb = sctp_event2skb(event);
  722. sdry = skb_put(skb, sizeof(struct sctp_sender_dry_event));
  723. sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
  724. sdry->sender_dry_flags = 0;
  725. sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
  726. sctp_ulpevent_set_owner(event, asoc);
  727. sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
  728. return event;
  729. }
  730. struct sctp_ulpevent *sctp_ulpevent_make_stream_reset_event(
  731. const struct sctp_association *asoc, __u16 flags, __u16 stream_num,
  732. __be16 *stream_list, gfp_t gfp)
  733. {
  734. struct sctp_stream_reset_event *sreset;
  735. struct sctp_ulpevent *event;
  736. struct sk_buff *skb;
  737. int length, i;
  738. length = sizeof(struct sctp_stream_reset_event) + 2 * stream_num;
  739. event = sctp_ulpevent_new(length, MSG_NOTIFICATION, gfp);
  740. if (!event)
  741. return NULL;
  742. skb = sctp_event2skb(event);
  743. sreset = skb_put(skb, length);
  744. sreset->strreset_type = SCTP_STREAM_RESET_EVENT;
  745. sreset->strreset_flags = flags;
  746. sreset->strreset_length = length;
  747. sctp_ulpevent_set_owner(event, asoc);
  748. sreset->strreset_assoc_id = sctp_assoc2id(asoc);
  749. for (i = 0; i < stream_num; i++)
  750. sreset->strreset_stream_list[i] = ntohs(stream_list[i]);
  751. return event;
  752. }
  753. struct sctp_ulpevent *sctp_ulpevent_make_assoc_reset_event(
  754. const struct sctp_association *asoc, __u16 flags, __u32 local_tsn,
  755. __u32 remote_tsn, gfp_t gfp)
  756. {
  757. struct sctp_assoc_reset_event *areset;
  758. struct sctp_ulpevent *event;
  759. struct sk_buff *skb;
  760. event = sctp_ulpevent_new(sizeof(struct sctp_assoc_reset_event),
  761. MSG_NOTIFICATION, gfp);
  762. if (!event)
  763. return NULL;
  764. skb = sctp_event2skb(event);
  765. areset = skb_put(skb, sizeof(struct sctp_assoc_reset_event));
  766. areset->assocreset_type = SCTP_ASSOC_RESET_EVENT;
  767. areset->assocreset_flags = flags;
  768. areset->assocreset_length = sizeof(struct sctp_assoc_reset_event);
  769. sctp_ulpevent_set_owner(event, asoc);
  770. areset->assocreset_assoc_id = sctp_assoc2id(asoc);
  771. areset->assocreset_local_tsn = local_tsn;
  772. areset->assocreset_remote_tsn = remote_tsn;
  773. return event;
  774. }
  775. struct sctp_ulpevent *sctp_ulpevent_make_stream_change_event(
  776. const struct sctp_association *asoc, __u16 flags,
  777. __u32 strchange_instrms, __u32 strchange_outstrms, gfp_t gfp)
  778. {
  779. struct sctp_stream_change_event *schange;
  780. struct sctp_ulpevent *event;
  781. struct sk_buff *skb;
  782. event = sctp_ulpevent_new(sizeof(struct sctp_stream_change_event),
  783. MSG_NOTIFICATION, gfp);
  784. if (!event)
  785. return NULL;
  786. skb = sctp_event2skb(event);
  787. schange = skb_put(skb, sizeof(struct sctp_stream_change_event));
  788. schange->strchange_type = SCTP_STREAM_CHANGE_EVENT;
  789. schange->strchange_flags = flags;
  790. schange->strchange_length = sizeof(struct sctp_stream_change_event);
  791. sctp_ulpevent_set_owner(event, asoc);
  792. schange->strchange_assoc_id = sctp_assoc2id(asoc);
  793. schange->strchange_instrms = strchange_instrms;
  794. schange->strchange_outstrms = strchange_outstrms;
  795. return event;
  796. }
  797. /* Return the notification type, assuming this is a notification
  798. * event.
  799. */
  800. __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
  801. {
  802. union sctp_notification *notification;
  803. struct sk_buff *skb;
  804. skb = sctp_event2skb(event);
  805. notification = (union sctp_notification *) skb->data;
  806. return notification->sn_header.sn_type;
  807. }
  808. /* RFC6458, Section 5.3.2. SCTP Header Information Structure
  809. * (SCTP_SNDRCV, DEPRECATED)
  810. */
  811. void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
  812. struct msghdr *msghdr)
  813. {
  814. struct sctp_sndrcvinfo sinfo;
  815. if (sctp_ulpevent_is_notification(event))
  816. return;
  817. memset(&sinfo, 0, sizeof(sinfo));
  818. sinfo.sinfo_stream = event->stream;
  819. sinfo.sinfo_ssn = event->ssn;
  820. sinfo.sinfo_ppid = event->ppid;
  821. sinfo.sinfo_flags = event->flags;
  822. sinfo.sinfo_tsn = event->tsn;
  823. sinfo.sinfo_cumtsn = event->cumtsn;
  824. sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
  825. /* Context value that is set via SCTP_CONTEXT socket option. */
  826. sinfo.sinfo_context = event->asoc->default_rcv_context;
  827. /* These fields are not used while receiving. */
  828. sinfo.sinfo_timetolive = 0;
  829. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
  830. sizeof(sinfo), &sinfo);
  831. }
  832. /* RFC6458, Section 5.3.5 SCTP Receive Information Structure
  833. * (SCTP_SNDRCV)
  834. */
  835. void sctp_ulpevent_read_rcvinfo(const struct sctp_ulpevent *event,
  836. struct msghdr *msghdr)
  837. {
  838. struct sctp_rcvinfo rinfo;
  839. if (sctp_ulpevent_is_notification(event))
  840. return;
  841. memset(&rinfo, 0, sizeof(struct sctp_rcvinfo));
  842. rinfo.rcv_sid = event->stream;
  843. rinfo.rcv_ssn = event->ssn;
  844. rinfo.rcv_ppid = event->ppid;
  845. rinfo.rcv_flags = event->flags;
  846. rinfo.rcv_tsn = event->tsn;
  847. rinfo.rcv_cumtsn = event->cumtsn;
  848. rinfo.rcv_assoc_id = sctp_assoc2id(event->asoc);
  849. rinfo.rcv_context = event->asoc->default_rcv_context;
  850. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_RCVINFO,
  851. sizeof(rinfo), &rinfo);
  852. }
  853. /* RFC6458, Section 5.3.6. SCTP Next Receive Information Structure
  854. * (SCTP_NXTINFO)
  855. */
  856. static void __sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
  857. struct msghdr *msghdr,
  858. const struct sk_buff *skb)
  859. {
  860. struct sctp_nxtinfo nxtinfo;
  861. memset(&nxtinfo, 0, sizeof(nxtinfo));
  862. nxtinfo.nxt_sid = event->stream;
  863. nxtinfo.nxt_ppid = event->ppid;
  864. nxtinfo.nxt_flags = event->flags;
  865. if (sctp_ulpevent_is_notification(event))
  866. nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
  867. nxtinfo.nxt_length = skb->len;
  868. nxtinfo.nxt_assoc_id = sctp_assoc2id(event->asoc);
  869. put_cmsg(msghdr, IPPROTO_SCTP, SCTP_NXTINFO,
  870. sizeof(nxtinfo), &nxtinfo);
  871. }
  872. void sctp_ulpevent_read_nxtinfo(const struct sctp_ulpevent *event,
  873. struct msghdr *msghdr,
  874. struct sock *sk)
  875. {
  876. struct sk_buff *skb;
  877. int err;
  878. skb = sctp_skb_recv_datagram(sk, MSG_PEEK, 1, &err);
  879. if (skb != NULL) {
  880. __sctp_ulpevent_read_nxtinfo(sctp_skb2event(skb),
  881. msghdr, skb);
  882. /* Just release refcount here. */
  883. kfree_skb(skb);
  884. }
  885. }
  886. /* Do accounting for bytes received and hold a reference to the association
  887. * for each skb.
  888. */
  889. static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
  890. struct sctp_association *asoc)
  891. {
  892. struct sk_buff *skb, *frag;
  893. skb = sctp_event2skb(event);
  894. /* Set the owner and charge rwnd for bytes received. */
  895. sctp_ulpevent_set_owner(event, asoc);
  896. sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
  897. if (!skb->data_len)
  898. return;
  899. /* Note: Not clearing the entire event struct as this is just a
  900. * fragment of the real event. However, we still need to do rwnd
  901. * accounting.
  902. * In general, the skb passed from IP can have only 1 level of
  903. * fragments. But we allow multiple levels of fragments.
  904. */
  905. skb_walk_frags(skb, frag)
  906. sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
  907. }
  908. /* Do accounting for bytes just read by user and release the references to
  909. * the association.
  910. */
  911. static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
  912. {
  913. struct sk_buff *skb, *frag;
  914. unsigned int len;
  915. /* Current stack structures assume that the rcv buffer is
  916. * per socket. For UDP style sockets this is not true as
  917. * multiple associations may be on a single UDP-style socket.
  918. * Use the local private area of the skb to track the owning
  919. * association.
  920. */
  921. skb = sctp_event2skb(event);
  922. len = skb->len;
  923. if (!skb->data_len)
  924. goto done;
  925. /* Don't forget the fragments. */
  926. skb_walk_frags(skb, frag) {
  927. /* NOTE: skb_shinfos are recursive. Although IP returns
  928. * skb's with only 1 level of fragments, SCTP reassembly can
  929. * increase the levels.
  930. */
  931. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  932. }
  933. done:
  934. sctp_assoc_rwnd_increase(event->asoc, len);
  935. sctp_chunk_put(event->chunk);
  936. sctp_ulpevent_release_owner(event);
  937. }
  938. static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
  939. {
  940. struct sk_buff *skb, *frag;
  941. skb = sctp_event2skb(event);
  942. if (!skb->data_len)
  943. goto done;
  944. /* Don't forget the fragments. */
  945. skb_walk_frags(skb, frag) {
  946. /* NOTE: skb_shinfos are recursive. Although IP returns
  947. * skb's with only 1 level of fragments, SCTP reassembly can
  948. * increase the levels.
  949. */
  950. sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
  951. }
  952. done:
  953. sctp_chunk_put(event->chunk);
  954. sctp_ulpevent_release_owner(event);
  955. }
  956. /* Free a ulpevent that has an owner. It includes releasing the reference
  957. * to the owner, updating the rwnd in case of a DATA event and freeing the
  958. * skb.
  959. */
  960. void sctp_ulpevent_free(struct sctp_ulpevent *event)
  961. {
  962. if (sctp_ulpevent_is_notification(event))
  963. sctp_ulpevent_release_owner(event);
  964. else
  965. sctp_ulpevent_release_data(event);
  966. kfree_skb(sctp_event2skb(event));
  967. }
  968. /* Purge the skb lists holding ulpevents. */
  969. unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
  970. {
  971. struct sk_buff *skb;
  972. unsigned int data_unread = 0;
  973. while ((skb = skb_dequeue(list)) != NULL) {
  974. struct sctp_ulpevent *event = sctp_skb2event(skb);
  975. if (!sctp_ulpevent_is_notification(event))
  976. data_unread += skb->len;
  977. sctp_ulpevent_free(event);
  978. }
  979. return data_unread;
  980. }