output.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. *
  6. * This file is part of the SCTP kernel implementation
  7. *
  8. * These functions handle output processing.
  9. *
  10. * This SCTP implementation is free software;
  11. * you can redistribute it and/or modify it under the terms of
  12. * the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This SCTP implementation is distributed in the hope that it
  17. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  18. * ************************
  19. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  20. * See the GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with GNU CC; see the file COPYING. If not, see
  24. * <http://www.gnu.org/licenses/>.
  25. *
  26. * Please send any bug reports or fixes you make to the
  27. * email address(es):
  28. * lksctp developers <linux-sctp@vger.kernel.org>
  29. *
  30. * Written or modified by:
  31. * La Monte H.P. Yarroll <piggy@acm.org>
  32. * Karl Knutson <karl@athena.chicago.il.us>
  33. * Jon Grimm <jgrimm@austin.ibm.com>
  34. * Sridhar Samudrala <sri@us.ibm.com>
  35. */
  36. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  37. #include <linux/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/wait.h>
  40. #include <linux/time.h>
  41. #include <linux/ip.h>
  42. #include <linux/ipv6.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <net/inet_ecn.h>
  46. #include <net/ip.h>
  47. #include <net/icmp.h>
  48. #include <net/net_namespace.h>
  49. #include <linux/socket.h> /* for sa_family_t */
  50. #include <net/sock.h>
  51. #include <net/sctp/sctp.h>
  52. #include <net/sctp/sm.h>
  53. #include <net/sctp/checksum.h>
  54. /* Forward declarations for private helpers. */
  55. static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
  56. struct sctp_chunk *chunk);
  57. static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
  58. struct sctp_chunk *chunk);
  59. static void sctp_packet_append_data(struct sctp_packet *packet,
  60. struct sctp_chunk *chunk);
  61. static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,
  62. struct sctp_chunk *chunk,
  63. u16 chunk_len);
  64. static void sctp_packet_reset(struct sctp_packet *packet)
  65. {
  66. packet->size = packet->overhead;
  67. packet->has_cookie_echo = 0;
  68. packet->has_sack = 0;
  69. packet->has_data = 0;
  70. packet->has_auth = 0;
  71. packet->ipfragok = 0;
  72. packet->auth = NULL;
  73. }
  74. /* Config a packet.
  75. * This appears to be a followup set of initializations.
  76. */
  77. struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
  78. __u32 vtag, int ecn_capable)
  79. {
  80. struct sctp_transport *tp = packet->transport;
  81. struct sctp_association *asoc = tp->asoc;
  82. pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
  83. packet->vtag = vtag;
  84. if (asoc && tp->dst) {
  85. struct sock *sk = asoc->base.sk;
  86. rcu_read_lock();
  87. if (__sk_dst_get(sk) != tp->dst) {
  88. dst_hold(tp->dst);
  89. sk_setup_caps(sk, tp->dst);
  90. }
  91. if (sk_can_gso(sk)) {
  92. struct net_device *dev = tp->dst->dev;
  93. packet->max_size = dev->gso_max_size;
  94. } else {
  95. packet->max_size = asoc->pathmtu;
  96. }
  97. rcu_read_unlock();
  98. } else {
  99. packet->max_size = tp->pathmtu;
  100. }
  101. if (ecn_capable && sctp_packet_empty(packet)) {
  102. struct sctp_chunk *chunk;
  103. /* If there a is a prepend chunk stick it on the list before
  104. * any other chunks get appended.
  105. */
  106. chunk = sctp_get_ecne_prepend(asoc);
  107. if (chunk)
  108. sctp_packet_append_chunk(packet, chunk);
  109. }
  110. return packet;
  111. }
  112. /* Initialize the packet structure. */
  113. struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
  114. struct sctp_transport *transport,
  115. __u16 sport, __u16 dport)
  116. {
  117. struct sctp_association *asoc = transport->asoc;
  118. size_t overhead;
  119. pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
  120. packet->transport = transport;
  121. packet->source_port = sport;
  122. packet->destination_port = dport;
  123. INIT_LIST_HEAD(&packet->chunk_list);
  124. if (asoc) {
  125. struct sctp_sock *sp = sctp_sk(asoc->base.sk);
  126. overhead = sp->pf->af->net_header_len;
  127. } else {
  128. overhead = sizeof(struct ipv6hdr);
  129. }
  130. overhead += sizeof(struct sctphdr);
  131. packet->overhead = overhead;
  132. sctp_packet_reset(packet);
  133. packet->vtag = 0;
  134. return packet;
  135. }
  136. /* Free a packet. */
  137. void sctp_packet_free(struct sctp_packet *packet)
  138. {
  139. struct sctp_chunk *chunk, *tmp;
  140. pr_debug("%s: packet:%p\n", __func__, packet);
  141. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  142. list_del_init(&chunk->list);
  143. sctp_chunk_free(chunk);
  144. }
  145. }
  146. /* This routine tries to append the chunk to the offered packet. If adding
  147. * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
  148. * is not present in the packet, it transmits the input packet.
  149. * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
  150. * as it can fit in the packet, but any more data that does not fit in this
  151. * packet can be sent only after receiving the COOKIE_ACK.
  152. */
  153. sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
  154. struct sctp_chunk *chunk,
  155. int one_packet, gfp_t gfp)
  156. {
  157. sctp_xmit_t retval;
  158. pr_debug("%s: packet:%p size:%Zu chunk:%p size:%d\n", __func__,
  159. packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
  160. switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
  161. case SCTP_XMIT_PMTU_FULL:
  162. if (!packet->has_cookie_echo) {
  163. int error = 0;
  164. error = sctp_packet_transmit(packet, gfp);
  165. if (error < 0)
  166. chunk->skb->sk->sk_err = -error;
  167. /* If we have an empty packet, then we can NOT ever
  168. * return PMTU_FULL.
  169. */
  170. if (!one_packet)
  171. retval = sctp_packet_append_chunk(packet,
  172. chunk);
  173. }
  174. break;
  175. case SCTP_XMIT_RWND_FULL:
  176. case SCTP_XMIT_OK:
  177. case SCTP_XMIT_DELAY:
  178. break;
  179. }
  180. return retval;
  181. }
  182. /* Try to bundle an auth chunk into the packet. */
  183. static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
  184. struct sctp_chunk *chunk)
  185. {
  186. struct sctp_association *asoc = pkt->transport->asoc;
  187. struct sctp_chunk *auth;
  188. sctp_xmit_t retval = SCTP_XMIT_OK;
  189. /* if we don't have an association, we can't do authentication */
  190. if (!asoc)
  191. return retval;
  192. /* See if this is an auth chunk we are bundling or if
  193. * auth is already bundled.
  194. */
  195. if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
  196. return retval;
  197. /* if the peer did not request this chunk to be authenticated,
  198. * don't do it
  199. */
  200. if (!chunk->auth)
  201. return retval;
  202. auth = sctp_make_auth(asoc);
  203. if (!auth)
  204. return retval;
  205. retval = __sctp_packet_append_chunk(pkt, auth);
  206. if (retval != SCTP_XMIT_OK)
  207. sctp_chunk_free(auth);
  208. return retval;
  209. }
  210. /* Try to bundle a SACK with the packet. */
  211. static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
  212. struct sctp_chunk *chunk)
  213. {
  214. sctp_xmit_t retval = SCTP_XMIT_OK;
  215. /* If sending DATA and haven't aleady bundled a SACK, try to
  216. * bundle one in to the packet.
  217. */
  218. if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
  219. !pkt->has_cookie_echo) {
  220. struct sctp_association *asoc;
  221. struct timer_list *timer;
  222. asoc = pkt->transport->asoc;
  223. timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
  224. /* If the SACK timer is running, we have a pending SACK */
  225. if (timer_pending(timer)) {
  226. struct sctp_chunk *sack;
  227. if (pkt->transport->sack_generation !=
  228. pkt->transport->asoc->peer.sack_generation)
  229. return retval;
  230. asoc->a_rwnd = asoc->rwnd;
  231. sack = sctp_make_sack(asoc);
  232. if (sack) {
  233. retval = __sctp_packet_append_chunk(pkt, sack);
  234. if (retval != SCTP_XMIT_OK) {
  235. sctp_chunk_free(sack);
  236. goto out;
  237. }
  238. asoc->peer.sack_needed = 0;
  239. if (del_timer(timer))
  240. sctp_association_put(asoc);
  241. }
  242. }
  243. }
  244. out:
  245. return retval;
  246. }
  247. /* Append a chunk to the offered packet reporting back any inability to do
  248. * so.
  249. */
  250. static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
  251. struct sctp_chunk *chunk)
  252. {
  253. sctp_xmit_t retval = SCTP_XMIT_OK;
  254. __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
  255. /* Check to see if this chunk will fit into the packet */
  256. retval = sctp_packet_will_fit(packet, chunk, chunk_len);
  257. if (retval != SCTP_XMIT_OK)
  258. goto finish;
  259. /* We believe that this chunk is OK to add to the packet */
  260. switch (chunk->chunk_hdr->type) {
  261. case SCTP_CID_DATA:
  262. /* Account for the data being in the packet */
  263. sctp_packet_append_data(packet, chunk);
  264. /* Disallow SACK bundling after DATA. */
  265. packet->has_sack = 1;
  266. /* Disallow AUTH bundling after DATA */
  267. packet->has_auth = 1;
  268. /* Let it be knows that packet has DATA in it */
  269. packet->has_data = 1;
  270. /* timestamp the chunk for rtx purposes */
  271. chunk->sent_at = jiffies;
  272. /* Mainly used for prsctp RTX policy */
  273. chunk->sent_count++;
  274. break;
  275. case SCTP_CID_COOKIE_ECHO:
  276. packet->has_cookie_echo = 1;
  277. break;
  278. case SCTP_CID_SACK:
  279. packet->has_sack = 1;
  280. if (chunk->asoc)
  281. chunk->asoc->stats.osacks++;
  282. break;
  283. case SCTP_CID_AUTH:
  284. packet->has_auth = 1;
  285. packet->auth = chunk;
  286. break;
  287. }
  288. /* It is OK to send this chunk. */
  289. list_add_tail(&chunk->list, &packet->chunk_list);
  290. packet->size += chunk_len;
  291. chunk->transport = packet->transport;
  292. finish:
  293. return retval;
  294. }
  295. /* Append a chunk to the offered packet reporting back any inability to do
  296. * so.
  297. */
  298. sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
  299. struct sctp_chunk *chunk)
  300. {
  301. sctp_xmit_t retval = SCTP_XMIT_OK;
  302. pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
  303. /* Data chunks are special. Before seeing what else we can
  304. * bundle into this packet, check to see if we are allowed to
  305. * send this DATA.
  306. */
  307. if (sctp_chunk_is_data(chunk)) {
  308. retval = sctp_packet_can_append_data(packet, chunk);
  309. if (retval != SCTP_XMIT_OK)
  310. goto finish;
  311. }
  312. /* Try to bundle AUTH chunk */
  313. retval = sctp_packet_bundle_auth(packet, chunk);
  314. if (retval != SCTP_XMIT_OK)
  315. goto finish;
  316. /* Try to bundle SACK chunk */
  317. retval = sctp_packet_bundle_sack(packet, chunk);
  318. if (retval != SCTP_XMIT_OK)
  319. goto finish;
  320. retval = __sctp_packet_append_chunk(packet, chunk);
  321. finish:
  322. return retval;
  323. }
  324. static void sctp_packet_release_owner(struct sk_buff *skb)
  325. {
  326. sk_free(skb->sk);
  327. }
  328. static void sctp_packet_set_owner_w(struct sk_buff *skb, struct sock *sk)
  329. {
  330. skb_orphan(skb);
  331. skb->sk = sk;
  332. skb->destructor = sctp_packet_release_owner;
  333. /*
  334. * The data chunks have already been accounted for in sctp_sendmsg(),
  335. * therefore only reserve a single byte to keep socket around until
  336. * the packet has been transmitted.
  337. */
  338. atomic_inc(&sk->sk_wmem_alloc);
  339. }
  340. /* All packets are sent to the network through this function from
  341. * sctp_outq_tail().
  342. *
  343. * The return value is a normal kernel error return value.
  344. */
  345. int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
  346. {
  347. struct sctp_transport *tp = packet->transport;
  348. struct sctp_association *asoc = tp->asoc;
  349. struct sctphdr *sh;
  350. struct sk_buff *nskb = NULL, *head = NULL;
  351. struct sctp_chunk *chunk, *tmp;
  352. struct sock *sk;
  353. int err = 0;
  354. int padding; /* How much padding do we need? */
  355. int pkt_size;
  356. __u8 has_data = 0;
  357. int gso = 0;
  358. int pktcount = 0;
  359. int auth_len = 0;
  360. struct dst_entry *dst;
  361. unsigned char *auth = NULL; /* pointer to auth in skb data */
  362. pr_debug("%s: packet:%p\n", __func__, packet);
  363. /* Do NOT generate a chunkless packet. */
  364. if (list_empty(&packet->chunk_list))
  365. return err;
  366. /* Set up convenience variables... */
  367. chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
  368. sk = chunk->skb->sk;
  369. /* Allocate the head skb, or main one if not in GSO */
  370. if (packet->size > tp->pathmtu && !packet->ipfragok) {
  371. if (sk_can_gso(sk)) {
  372. gso = 1;
  373. pkt_size = packet->overhead;
  374. } else {
  375. /* If this happens, we trash this packet and try
  376. * to build a new one, hopefully correct this
  377. * time. Application may notice this error.
  378. */
  379. pr_err_once("Trying to GSO but underlying device doesn't support it.");
  380. goto err;
  381. }
  382. } else {
  383. pkt_size = packet->size;
  384. }
  385. head = alloc_skb(pkt_size + MAX_HEADER, gfp);
  386. if (!head)
  387. goto err;
  388. if (gso) {
  389. NAPI_GRO_CB(head)->last = head;
  390. skb_shinfo(head)->gso_type = sk->sk_gso_type;
  391. }
  392. /* Make sure the outbound skb has enough header room reserved. */
  393. skb_reserve(head, packet->overhead + MAX_HEADER);
  394. /* Set the owning socket so that we know where to get the
  395. * destination IP address.
  396. */
  397. sctp_packet_set_owner_w(head, sk);
  398. if (!sctp_transport_dst_check(tp)) {
  399. sctp_transport_route(tp, NULL, sctp_sk(sk));
  400. if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
  401. sctp_assoc_sync_pmtu(sk, asoc);
  402. }
  403. }
  404. dst = dst_clone(tp->dst);
  405. if (!dst) {
  406. if (asoc)
  407. IP_INC_STATS(sock_net(asoc->base.sk),
  408. IPSTATS_MIB_OUTNOROUTES);
  409. goto nodst;
  410. }
  411. skb_dst_set(head, dst);
  412. /* Build the SCTP header. */
  413. sh = (struct sctphdr *)skb_push(head, sizeof(struct sctphdr));
  414. skb_reset_transport_header(head);
  415. sh->source = htons(packet->source_port);
  416. sh->dest = htons(packet->destination_port);
  417. /* From 6.8 Adler-32 Checksum Calculation:
  418. * After the packet is constructed (containing the SCTP common
  419. * header and one or more control or DATA chunks), the
  420. * transmitter shall:
  421. *
  422. * 1) Fill in the proper Verification Tag in the SCTP common
  423. * header and initialize the checksum field to 0's.
  424. */
  425. sh->vtag = htonl(packet->vtag);
  426. sh->checksum = 0;
  427. pr_debug("***sctp_transmit_packet***\n");
  428. do {
  429. /* Set up convenience variables... */
  430. chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
  431. pktcount++;
  432. /* Calculate packet size, so it fits in PMTU. Leave
  433. * other chunks for the next packets.
  434. */
  435. if (gso) {
  436. pkt_size = packet->overhead;
  437. list_for_each_entry(chunk, &packet->chunk_list, list) {
  438. int padded = SCTP_PAD4(chunk->skb->len);
  439. if (chunk == packet->auth)
  440. auth_len = padded;
  441. else if (auth_len + padded + packet->overhead >
  442. tp->pathmtu)
  443. goto nomem;
  444. else if (pkt_size + padded > tp->pathmtu)
  445. break;
  446. pkt_size += padded;
  447. }
  448. /* Allocate a new skb. */
  449. nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
  450. if (!nskb)
  451. goto nomem;
  452. /* Make sure the outbound skb has enough header
  453. * room reserved.
  454. */
  455. skb_reserve(nskb, packet->overhead + MAX_HEADER);
  456. } else {
  457. nskb = head;
  458. }
  459. /**
  460. * 3.2 Chunk Field Descriptions
  461. *
  462. * The total length of a chunk (including Type, Length and
  463. * Value fields) MUST be a multiple of 4 bytes. If the length
  464. * of the chunk is not a multiple of 4 bytes, the sender MUST
  465. * pad the chunk with all zero bytes and this padding is not
  466. * included in the chunk length field. The sender should
  467. * never pad with more than 3 bytes.
  468. *
  469. * [This whole comment explains SCTP_PAD4() below.]
  470. */
  471. pkt_size -= packet->overhead;
  472. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  473. list_del_init(&chunk->list);
  474. if (sctp_chunk_is_data(chunk)) {
  475. /* 6.3.1 C4) When data is in flight and when allowed
  476. * by rule C5, a new RTT measurement MUST be made each
  477. * round trip. Furthermore, new RTT measurements
  478. * SHOULD be made no more than once per round-trip
  479. * for a given destination transport address.
  480. */
  481. if (!chunk->resent && !tp->rto_pending) {
  482. chunk->rtt_in_progress = 1;
  483. tp->rto_pending = 1;
  484. }
  485. has_data = 1;
  486. }
  487. padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
  488. if (padding)
  489. memset(skb_put(chunk->skb, padding), 0, padding);
  490. /* if this is the auth chunk that we are adding,
  491. * store pointer where it will be added and put
  492. * the auth into the packet.
  493. */
  494. if (chunk == packet->auth)
  495. auth = skb_tail_pointer(nskb);
  496. memcpy(skb_put(nskb, chunk->skb->len),
  497. chunk->skb->data, chunk->skb->len);
  498. pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
  499. chunk,
  500. sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
  501. chunk->has_tsn ? "TSN" : "No TSN",
  502. chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
  503. ntohs(chunk->chunk_hdr->length), chunk->skb->len,
  504. chunk->rtt_in_progress);
  505. /* If this is a control chunk, this is our last
  506. * reference. Free data chunks after they've been
  507. * acknowledged or have failed.
  508. * Re-queue auth chunks if needed.
  509. */
  510. pkt_size -= SCTP_PAD4(chunk->skb->len);
  511. if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
  512. sctp_chunk_free(chunk);
  513. if (!pkt_size)
  514. break;
  515. }
  516. /* SCTP-AUTH, Section 6.2
  517. * The sender MUST calculate the MAC as described in RFC2104 [2]
  518. * using the hash function H as described by the MAC Identifier and
  519. * the shared association key K based on the endpoint pair shared key
  520. * described by the shared key identifier. The 'data' used for the
  521. * computation of the AUTH-chunk is given by the AUTH chunk with its
  522. * HMAC field set to zero (as shown in Figure 6) followed by all
  523. * chunks that are placed after the AUTH chunk in the SCTP packet.
  524. */
  525. if (auth)
  526. sctp_auth_calculate_hmac(asoc, nskb,
  527. (struct sctp_auth_chunk *)auth,
  528. gfp);
  529. if (packet->auth) {
  530. if (!list_empty(&packet->chunk_list)) {
  531. /* We will generate more packets, so re-queue
  532. * auth chunk.
  533. */
  534. list_add(&packet->auth->list,
  535. &packet->chunk_list);
  536. } else {
  537. sctp_chunk_free(packet->auth);
  538. packet->auth = NULL;
  539. }
  540. }
  541. if (!gso)
  542. break;
  543. if (skb_gro_receive(&head, nskb)) {
  544. kfree_skb(nskb);
  545. goto nomem;
  546. }
  547. nskb = NULL;
  548. if (WARN_ON_ONCE(skb_shinfo(head)->gso_segs >=
  549. sk->sk_gso_max_segs))
  550. goto nomem;
  551. } while (!list_empty(&packet->chunk_list));
  552. /* 2) Calculate the Adler-32 checksum of the whole packet,
  553. * including the SCTP common header and all the
  554. * chunks.
  555. *
  556. * Note: Adler-32 is no longer applicable, as has been replaced
  557. * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
  558. *
  559. * If it's a GSO packet, it's postponed to sctp_skb_segment.
  560. */
  561. if (!sctp_checksum_disable || gso) {
  562. if (!gso && (!(dst->dev->features & NETIF_F_SCTP_CRC) ||
  563. dst_xfrm(dst) || packet->ipfragok)) {
  564. sh->checksum = sctp_compute_cksum(head, 0);
  565. } else {
  566. /* no need to seed pseudo checksum for SCTP */
  567. head->ip_summed = CHECKSUM_PARTIAL;
  568. head->csum_start = skb_transport_header(head) - head->head;
  569. head->csum_offset = offsetof(struct sctphdr, checksum);
  570. }
  571. }
  572. /* IP layer ECN support
  573. * From RFC 2481
  574. * "The ECN-Capable Transport (ECT) bit would be set by the
  575. * data sender to indicate that the end-points of the
  576. * transport protocol are ECN-capable."
  577. *
  578. * Now setting the ECT bit all the time, as it should not cause
  579. * any problems protocol-wise even if our peer ignores it.
  580. *
  581. * Note: The works for IPv6 layer checks this bit too later
  582. * in transmission. See IP6_ECN_flow_xmit().
  583. */
  584. tp->af_specific->ecn_capable(sk);
  585. /* Set up the IP options. */
  586. /* BUG: not implemented
  587. * For v4 this all lives somewhere in sk->sk_opt...
  588. */
  589. /* Dump that on IP! */
  590. if (asoc) {
  591. asoc->stats.opackets += pktcount;
  592. if (asoc->peer.last_sent_to != tp)
  593. /* Considering the multiple CPU scenario, this is a
  594. * "correcter" place for last_sent_to. --xguo
  595. */
  596. asoc->peer.last_sent_to = tp;
  597. }
  598. if (has_data) {
  599. struct timer_list *timer;
  600. unsigned long timeout;
  601. /* Restart the AUTOCLOSE timer when sending data. */
  602. if (sctp_state(asoc, ESTABLISHED) &&
  603. asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
  604. timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
  605. timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
  606. if (!mod_timer(timer, jiffies + timeout))
  607. sctp_association_hold(asoc);
  608. }
  609. }
  610. pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
  611. if (gso) {
  612. /* Cleanup our debris for IP stacks */
  613. memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
  614. sizeof(struct inet6_skb_parm)));
  615. skb_shinfo(head)->gso_segs = pktcount;
  616. skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
  617. /* We have to refresh this in case we are xmiting to
  618. * more than one transport at a time
  619. */
  620. rcu_read_lock();
  621. if (__sk_dst_get(sk) != tp->dst) {
  622. dst_hold(tp->dst);
  623. sk_setup_caps(sk, tp->dst);
  624. }
  625. rcu_read_unlock();
  626. }
  627. head->ignore_df = packet->ipfragok;
  628. tp->af_specific->sctp_xmit(head, tp);
  629. goto out;
  630. nomem:
  631. if (packet->auth && list_empty(&packet->auth->list))
  632. sctp_chunk_free(packet->auth);
  633. nodst:
  634. /* FIXME: Returning the 'err' will effect all the associations
  635. * associated with a socket, although only one of the paths of the
  636. * association is unreachable.
  637. * The real failure of a transport or association can be passed on
  638. * to the user via notifications. So setting this error may not be
  639. * required.
  640. */
  641. /* err = -EHOSTUNREACH; */
  642. kfree_skb(head);
  643. err:
  644. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  645. list_del_init(&chunk->list);
  646. if (!sctp_chunk_is_data(chunk))
  647. sctp_chunk_free(chunk);
  648. }
  649. out:
  650. sctp_packet_reset(packet);
  651. return err;
  652. }
  653. /********************************************************************
  654. * 2nd Level Abstractions
  655. ********************************************************************/
  656. /* This private function check to see if a chunk can be added */
  657. static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
  658. struct sctp_chunk *chunk)
  659. {
  660. size_t datasize, rwnd, inflight, flight_size;
  661. struct sctp_transport *transport = packet->transport;
  662. struct sctp_association *asoc = transport->asoc;
  663. struct sctp_outq *q = &asoc->outqueue;
  664. /* RFC 2960 6.1 Transmission of DATA Chunks
  665. *
  666. * A) At any given time, the data sender MUST NOT transmit new data to
  667. * any destination transport address if its peer's rwnd indicates
  668. * that the peer has no buffer space (i.e. rwnd is 0, see Section
  669. * 6.2.1). However, regardless of the value of rwnd (including if it
  670. * is 0), the data sender can always have one DATA chunk in flight to
  671. * the receiver if allowed by cwnd (see rule B below). This rule
  672. * allows the sender to probe for a change in rwnd that the sender
  673. * missed due to the SACK having been lost in transit from the data
  674. * receiver to the data sender.
  675. */
  676. rwnd = asoc->peer.rwnd;
  677. inflight = q->outstanding_bytes;
  678. flight_size = transport->flight_size;
  679. datasize = sctp_data_size(chunk);
  680. if (datasize > rwnd && inflight > 0)
  681. /* We have (at least) one data chunk in flight,
  682. * so we can't fall back to rule 6.1 B).
  683. */
  684. return SCTP_XMIT_RWND_FULL;
  685. /* RFC 2960 6.1 Transmission of DATA Chunks
  686. *
  687. * B) At any given time, the sender MUST NOT transmit new data
  688. * to a given transport address if it has cwnd or more bytes
  689. * of data outstanding to that transport address.
  690. */
  691. /* RFC 7.2.4 & the Implementers Guide 2.8.
  692. *
  693. * 3) ...
  694. * When a Fast Retransmit is being performed the sender SHOULD
  695. * ignore the value of cwnd and SHOULD NOT delay retransmission.
  696. */
  697. if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
  698. flight_size >= transport->cwnd)
  699. return SCTP_XMIT_RWND_FULL;
  700. /* Nagle's algorithm to solve small-packet problem:
  701. * Inhibit the sending of new chunks when new outgoing data arrives
  702. * if any previously transmitted data on the connection remains
  703. * unacknowledged.
  704. */
  705. if (sctp_sk(asoc->base.sk)->nodelay)
  706. /* Nagle disabled */
  707. return SCTP_XMIT_OK;
  708. if (!sctp_packet_empty(packet))
  709. /* Append to packet */
  710. return SCTP_XMIT_OK;
  711. if (inflight == 0)
  712. /* Nothing unacked */
  713. return SCTP_XMIT_OK;
  714. if (!sctp_state(asoc, ESTABLISHED))
  715. return SCTP_XMIT_OK;
  716. /* Check whether this chunk and all the rest of pending data will fit
  717. * or delay in hopes of bundling a full sized packet.
  718. */
  719. if (chunk->skb->len + q->out_qlen >
  720. transport->pathmtu - packet->overhead - sizeof(sctp_data_chunk_t) - 4)
  721. /* Enough data queued to fill a packet */
  722. return SCTP_XMIT_OK;
  723. /* Don't delay large message writes that may have been fragmented */
  724. if (!chunk->msg->can_delay)
  725. return SCTP_XMIT_OK;
  726. /* Defer until all data acked or packet full */
  727. return SCTP_XMIT_DELAY;
  728. }
  729. /* This private function does management things when adding DATA chunk */
  730. static void sctp_packet_append_data(struct sctp_packet *packet,
  731. struct sctp_chunk *chunk)
  732. {
  733. struct sctp_transport *transport = packet->transport;
  734. size_t datasize = sctp_data_size(chunk);
  735. struct sctp_association *asoc = transport->asoc;
  736. u32 rwnd = asoc->peer.rwnd;
  737. /* Keep track of how many bytes are in flight over this transport. */
  738. transport->flight_size += datasize;
  739. /* Keep track of how many bytes are in flight to the receiver. */
  740. asoc->outqueue.outstanding_bytes += datasize;
  741. /* Update our view of the receiver's rwnd. */
  742. if (datasize < rwnd)
  743. rwnd -= datasize;
  744. else
  745. rwnd = 0;
  746. asoc->peer.rwnd = rwnd;
  747. /* Has been accepted for transmission. */
  748. if (!asoc->peer.prsctp_capable)
  749. chunk->msg->can_abandon = 0;
  750. sctp_chunk_assign_tsn(chunk);
  751. sctp_chunk_assign_ssn(chunk);
  752. }
  753. static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,
  754. struct sctp_chunk *chunk,
  755. u16 chunk_len)
  756. {
  757. size_t psize, pmtu, maxsize;
  758. sctp_xmit_t retval = SCTP_XMIT_OK;
  759. psize = packet->size;
  760. if (packet->transport->asoc)
  761. pmtu = packet->transport->asoc->pathmtu;
  762. else
  763. pmtu = packet->transport->pathmtu;
  764. /* Decide if we need to fragment or resubmit later. */
  765. if (psize + chunk_len > pmtu) {
  766. /* It's OK to fragment at IP level if any one of the following
  767. * is true:
  768. * 1. The packet is empty (meaning this chunk is greater
  769. * the MTU)
  770. * 2. The packet doesn't have any data in it yet and data
  771. * requires authentication.
  772. */
  773. if (sctp_packet_empty(packet) ||
  774. (!packet->has_data && chunk->auth)) {
  775. /* We no longer do re-fragmentation.
  776. * Just fragment at the IP layer, if we
  777. * actually hit this condition
  778. */
  779. packet->ipfragok = 1;
  780. goto out;
  781. }
  782. /* Similarly, if this chunk was built before a PMTU
  783. * reduction, we have to fragment it at IP level now. So
  784. * if the packet already contains something, we need to
  785. * flush.
  786. */
  787. maxsize = pmtu - packet->overhead;
  788. if (packet->auth)
  789. maxsize -= SCTP_PAD4(packet->auth->skb->len);
  790. if (chunk_len > maxsize)
  791. retval = SCTP_XMIT_PMTU_FULL;
  792. /* It is also okay to fragment if the chunk we are
  793. * adding is a control chunk, but only if current packet
  794. * is not a GSO one otherwise it causes fragmentation of
  795. * a large frame. So in this case we allow the
  796. * fragmentation by forcing it to be in a new packet.
  797. */
  798. if (!sctp_chunk_is_data(chunk) && packet->has_data)
  799. retval = SCTP_XMIT_PMTU_FULL;
  800. if (psize + chunk_len > packet->max_size)
  801. /* Hit GSO/PMTU limit, gotta flush */
  802. retval = SCTP_XMIT_PMTU_FULL;
  803. if (!packet->transport->burst_limited &&
  804. psize + chunk_len > (packet->transport->cwnd >> 1))
  805. /* Do not allow a single GSO packet to use more
  806. * than half of cwnd.
  807. */
  808. retval = SCTP_XMIT_PMTU_FULL;
  809. if (packet->transport->burst_limited &&
  810. psize + chunk_len > (packet->transport->burst_limited >> 1))
  811. /* Do not allow a single GSO packet to use more
  812. * than half of original cwnd.
  813. */
  814. retval = SCTP_XMIT_PMTU_FULL;
  815. /* Otherwise it will fit in the GSO packet */
  816. }
  817. out:
  818. return retval;
  819. }