Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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